blob: 4d73b954b26e89ed12f07eecdfc0f45064676874 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass8d30fcd2012-02-15 15:51:13 -08002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glass8d30fcd2012-02-15 15:51:13 -08004 */
5
6#include <common.h>
Simon Glasse2d8a712014-02-26 15:59:25 -07007#include <dm.h>
8#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glasse2d8a712014-02-26 15:59:25 -070010#include <malloc.h>
Simon Glass29126862020-07-07 13:11:44 -060011#include <acpi/acpi_device.h>
Simon Glass8d30fcd2012-02-15 15:51:13 -080012#include <asm/gpio.h>
Simon Glass29126862020-07-07 13:11:44 -060013#include <dm/acpi.h>
Simon Glass0fd3d912020-12-22 19:30:28 -070014#include <dm/device-internal.h>
Patrick Delaunaye5301ba2020-01-13 11:35:15 +010015#include <dm/device_compat.h>
Patrick Delaunayff526652020-01-13 11:35:14 +010016#include <dm/lists.h>
Simon Glass3a571232017-05-18 20:09:18 -060017#include <dm/of.h>
Patrick Delaunayff526652020-01-13 11:35:14 +010018#include <dm/pinctrl.h>
Simon Glass3669e0e2015-01-05 20:05:29 -070019#include <dt-bindings/gpio/gpio.h>
Patrick Delaunay2c0f7822020-01-13 11:35:13 +010020#include <dt-bindings/gpio/sandbox-gpio.h>
Simon Glass8d30fcd2012-02-15 15:51:13 -080021
Simon Glass8d30fcd2012-02-15 15:51:13 -080022
23struct gpio_state {
24 const char *label; /* label given by requester */
Simon Glassa03a0aa2021-02-04 21:21:59 -070025 ulong flags; /* flags (GPIOD_...) */
Simon Glass8d30fcd2012-02-15 15:51:13 -080026};
27
Simon Glassa03a0aa2021-02-04 21:21:59 -070028/* Access routines for GPIO info */
29static struct gpio_state *get_gpio_state(struct udevice *dev, uint offset)
Simon Glass8d30fcd2012-02-15 15:51:13 -080030{
Simon Glasse564f052015-03-05 12:25:20 -070031 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glasse2d8a712014-02-26 15:59:25 -070032 struct gpio_state *state = dev_get_priv(dev);
33
34 if (offset >= uc_priv->gpio_count) {
Simon Glasse2d8a712014-02-26 15:59:25 -070035 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
Simon Glassa03a0aa2021-02-04 21:21:59 -070036 return NULL;
Simon Glass8d30fcd2012-02-15 15:51:13 -080037 }
38
Simon Glassa03a0aa2021-02-04 21:21:59 -070039 return &state[offset];
40}
41
42/* Access routines for GPIO flags */
43static ulong *get_gpio_flags(struct udevice *dev, unsigned int offset)
44{
45 struct gpio_state *state = get_gpio_state(dev, offset);
46
47 if (!state)
48 return NULL;
49
50 return &state->flags;
Patrick Delaunayff526652020-01-13 11:35:14 +010051
Simon Glass8d30fcd2012-02-15 15:51:13 -080052}
53
Patrick Delaunayff526652020-01-13 11:35:14 +010054static int get_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag)
Simon Glass8d30fcd2012-02-15 15:51:13 -080055{
Simon Glassa03a0aa2021-02-04 21:21:59 -070056 return (*get_gpio_flags(dev, offset) & flag) != 0;
Simon Glass8d30fcd2012-02-15 15:51:13 -080057}
58
Patrick Delaunayff526652020-01-13 11:35:14 +010059static int set_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag,
Simon Glasse2d8a712014-02-26 15:59:25 -070060 int value)
Simon Glass8d30fcd2012-02-15 15:51:13 -080061{
Simon Glass1f212af2021-02-04 21:22:00 -070062 struct gpio_state *state = get_gpio_state(dev, offset);
Simon Glass8d30fcd2012-02-15 15:51:13 -080063
64 if (value)
Simon Glass1f212af2021-02-04 21:22:00 -070065 state->flags |= flag;
Simon Glass8d30fcd2012-02-15 15:51:13 -080066 else
Simon Glass1f212af2021-02-04 21:22:00 -070067 state->flags &= ~flag;
Simon Glass8d30fcd2012-02-15 15:51:13 -080068
69 return 0;
70}
71
Simon Glass8d30fcd2012-02-15 15:51:13 -080072/*
73 * Back-channel sandbox-internal-only access to GPIO state
74 */
75
Heiko Schocher54c5d082014-05-22 12:43:05 +020076int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glass8d30fcd2012-02-15 15:51:13 -080077{
Simon Glass1f212af2021-02-04 21:22:00 -070078 struct gpio_state *state = get_gpio_state(dev, offset);
79
Patrick Delaunayff526652020-01-13 11:35:14 +010080 if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
Simon Glasse2d8a712014-02-26 15:59:25 -070081 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
Simon Glass1f212af2021-02-04 21:22:00 -070082
83 return state->flags & GPIOD_EXT_HIGH ? true : false;
Simon Glass8d30fcd2012-02-15 15:51:13 -080084}
85
Heiko Schocher54c5d082014-05-22 12:43:05 +020086int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glass8d30fcd2012-02-15 15:51:13 -080087{
Simon Glass1f212af2021-02-04 21:22:00 -070088 set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE | GPIOD_EXT_HIGH, value);
89
90 return 0;
Simon Glass8d30fcd2012-02-15 15:51:13 -080091}
92
Heiko Schocher54c5d082014-05-22 12:43:05 +020093int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
Simon Glass8d30fcd2012-02-15 15:51:13 -080094{
Patrick Delaunayff526652020-01-13 11:35:14 +010095 return get_gpio_flag(dev, offset, GPIOD_IS_OUT);
Simon Glass8d30fcd2012-02-15 15:51:13 -080096}
97
Heiko Schocher54c5d082014-05-22 12:43:05 +020098int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
Simon Glass8d30fcd2012-02-15 15:51:13 -080099{
Patrick Delaunayff526652020-01-13 11:35:14 +0100100 set_gpio_flag(dev, offset, GPIOD_IS_OUT, output);
Simon Glass1f212af2021-02-04 21:22:00 -0700101 set_gpio_flag(dev, offset, GPIOD_IS_IN, !output);
Patrick Delaunayff526652020-01-13 11:35:14 +0100102
103 return 0;
104}
105
Simon Glassa03a0aa2021-02-04 21:21:59 -0700106ulong sandbox_gpio_get_flags(struct udevice *dev, uint offset)
Patrick Delaunayff526652020-01-13 11:35:14 +0100107{
Simon Glass1f212af2021-02-04 21:22:00 -0700108 ulong flags = *get_gpio_flags(dev, offset);
109
110 return flags & ~GPIOD_SANDBOX_MASK;
Patrick Delaunayff526652020-01-13 11:35:14 +0100111}
112
Simon Glassa03a0aa2021-02-04 21:21:59 -0700113int sandbox_gpio_set_flags(struct udevice *dev, uint offset, ulong flags)
Patrick Delaunayff526652020-01-13 11:35:14 +0100114{
Simon Glass1f212af2021-02-04 21:22:00 -0700115 struct gpio_state *state = get_gpio_state(dev, offset);
116
117 /*
118 * We don't need to clear GPIOD_EXT_HIGH here to make the tests pass,
119 * but this is handled in a future patch.
120 */
121 if (flags & GPIOD_IS_OUT_ACTIVE)
122 flags |= GPIOD_EXT_HIGH;
123 state->flags = (state->flags & GPIOD_SANDBOX_MASK) | flags;
Patrick Delaunayff526652020-01-13 11:35:14 +0100124
125 return 0;
Simon Glass8d30fcd2012-02-15 15:51:13 -0800126}
127
128/*
129 * These functions implement the public interface within U-Boot
130 */
131
Simon Glasse2d8a712014-02-26 15:59:25 -0700132/* set GPIO port 'offset' as an input */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200133static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
Simon Glass8d30fcd2012-02-15 15:51:13 -0800134{
Simon Glasse2d8a712014-02-26 15:59:25 -0700135 debug("%s: offset:%u\n", __func__, offset);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800136
Simon Glasse2d8a712014-02-26 15:59:25 -0700137 return sandbox_gpio_set_direction(dev, offset, 0);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800138}
139
Simon Glasse2d8a712014-02-26 15:59:25 -0700140/* set GPIO port 'offset' as an output, with polarity 'value' */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200141static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
Simon Glasse2d8a712014-02-26 15:59:25 -0700142 int value)
Simon Glass8d30fcd2012-02-15 15:51:13 -0800143{
Simon Glasse2d8a712014-02-26 15:59:25 -0700144 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800145
Simon Glasse2d8a712014-02-26 15:59:25 -0700146 return sandbox_gpio_set_direction(dev, offset, 1) |
147 sandbox_gpio_set_value(dev, offset, value);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800148}
149
Simon Glasse2d8a712014-02-26 15:59:25 -0700150/* read GPIO IN value of port 'offset' */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200151static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glass8d30fcd2012-02-15 15:51:13 -0800152{
Simon Glasse2d8a712014-02-26 15:59:25 -0700153 debug("%s: offset:%u\n", __func__, offset);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800154
Simon Glasse2d8a712014-02-26 15:59:25 -0700155 return sandbox_gpio_get_value(dev, offset);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800156}
157
Simon Glasse2d8a712014-02-26 15:59:25 -0700158/* write GPIO OUT value to port 'offset' */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200159static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glass8d30fcd2012-02-15 15:51:13 -0800160{
Simon Glasse2d8a712014-02-26 15:59:25 -0700161 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800162
Simon Glasse2d8a712014-02-26 15:59:25 -0700163 if (!sandbox_gpio_get_direction(dev, offset)) {
164 printf("sandbox_gpio: error: set_value on input gpio %u\n",
165 offset);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800166 return -1;
167 }
168
Simon Glasse2d8a712014-02-26 15:59:25 -0700169 return sandbox_gpio_set_value(dev, offset, value);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800170}
171
Simon Glass699ea962014-10-04 11:29:45 -0600172static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
173{
Patrick Delaunayff526652020-01-13 11:35:14 +0100174 if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
Simon Glass699ea962014-10-04 11:29:45 -0600175 return GPIOF_OUTPUT;
Patrick Delaunayff526652020-01-13 11:35:14 +0100176 if (get_gpio_flag(dev, offset, GPIOD_IS_IN))
177 return GPIOF_INPUT;
178
179 return GPIOF_INPUT; /*GPIO is not configurated */
Simon Glass699ea962014-10-04 11:29:45 -0600180}
181
Simon Glass3669e0e2015-01-05 20:05:29 -0700182static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
Simon Glass3a571232017-05-18 20:09:18 -0600183 struct ofnode_phandle_args *args)
Simon Glass3669e0e2015-01-05 20:05:29 -0700184{
185 desc->offset = args->args[0];
186 if (args->args_count < 2)
187 return 0;
Patrick Delaunay2c0f7822020-01-13 11:35:13 +0100188 /* treat generic binding with gpio uclass */
189 gpio_xlate_offs_flags(dev, desc, args);
190
191 /* sandbox test specific, not defined in gpio.h */
192 if (args->args[1] & GPIO_IN)
Simon Glass3669e0e2015-01-05 20:05:29 -0700193 desc->flags |= GPIOD_IS_IN;
Patrick Delaunayff526652020-01-13 11:35:14 +0100194
Patrick Delaunay2c0f7822020-01-13 11:35:13 +0100195 if (args->args[1] & GPIO_OUT)
Simon Glass3669e0e2015-01-05 20:05:29 -0700196 desc->flags |= GPIOD_IS_OUT;
Patrick Delaunayff526652020-01-13 11:35:14 +0100197
Patrick Delaunay2c0f7822020-01-13 11:35:13 +0100198 if (args->args[1] & GPIO_OUT_ACTIVE)
Simon Glass3669e0e2015-01-05 20:05:29 -0700199 desc->flags |= GPIOD_IS_OUT_ACTIVE;
200
201 return 0;
202}
203
Simon Glass13979fc2021-02-04 21:21:55 -0700204static int sb_gpio_set_flags(struct udevice *dev, unsigned int offset,
205 ulong flags)
Patrick Delaunayff526652020-01-13 11:35:14 +0100206{
Simon Glassa03a0aa2021-02-04 21:21:59 -0700207 debug("%s: offset:%u, flags = %lx\n", __func__, offset, flags);
Patrick Delaunayff526652020-01-13 11:35:14 +0100208
Simon Glass1f212af2021-02-04 21:22:00 -0700209 return sandbox_gpio_set_flags(dev, offset, flags);
Patrick Delaunayff526652020-01-13 11:35:14 +0100210}
211
Simon Glassa03a0aa2021-02-04 21:21:59 -0700212static int sb_gpio_get_flags(struct udevice *dev, uint offset, ulong *flagsp)
Patrick Delaunayff526652020-01-13 11:35:14 +0100213{
214 debug("%s: offset:%u\n", __func__, offset);
Simon Glassa03a0aa2021-02-04 21:21:59 -0700215 *flagsp = *get_gpio_flags(dev, offset);
Patrick Delaunayff526652020-01-13 11:35:14 +0100216
217 return 0;
218}
219
Simon Glass29126862020-07-07 13:11:44 -0600220#if CONFIG_IS_ENABLED(ACPIGEN)
221static int sb_gpio_get_acpi(const struct gpio_desc *desc,
222 struct acpi_gpio *gpio)
223{
224 int ret;
225
226 /* Note that gpio_get_acpi() zeroes *gpio before calling here */
227 gpio->pin_count = 1;
228 gpio->pins[0] = desc->offset;
229 ret = acpi_device_scope(desc->dev, gpio->resource,
230 sizeof(gpio->resource));
231 if (ret)
232 return log_ret(ret);
233
234 /* All of these values are just used for testing */
235 if (desc->flags & GPIOD_ACTIVE_LOW) {
236 gpio->pin0_addr = 0x80012 + desc->offset;
237 gpio->type = ACPI_GPIO_TYPE_INTERRUPT;
238 gpio->pull = ACPI_GPIO_PULL_DOWN;
239 gpio->interrupt_debounce_timeout = 4321;
240
241 /* We use the GpioInt part */
242 gpio->irq.pin = desc->offset;
243 gpio->irq.polarity = ACPI_IRQ_ACTIVE_BOTH;
244 gpio->irq.shared = ACPI_IRQ_SHARED;
245 gpio->irq.wake = ACPI_IRQ_WAKE;
246
247 /* The GpioIo part is only used for testing */
248 gpio->polarity = ACPI_GPIO_ACTIVE_LOW;
249 } else {
250 gpio->pin0_addr = 0xc00dc + desc->offset;
251 gpio->type = ACPI_GPIO_TYPE_IO;
252 gpio->pull = ACPI_GPIO_PULL_UP;
253 gpio->interrupt_debounce_timeout = 0;
254
255 /* The GpioInt part is not used */
256
257 /* We use the GpioIo part */
258 gpio->output_drive_strength = 1234;
259 gpio->io_shared = true;
260 gpio->io_restrict = ACPI_GPIO_IO_RESTRICT_INPUT;
261 gpio->polarity = 0;
262 }
263
264 return 0;
265}
266
267static int sb_gpio_get_name(const struct udevice *dev, char *out_name)
268{
269 return acpi_copy_name(out_name, "GPIO");
270}
271
272struct acpi_ops gpio_sandbox_acpi_ops = {
273 .get_name = sb_gpio_get_name,
274};
275#endif /* ACPIGEN */
276
Simon Glasse2d8a712014-02-26 15:59:25 -0700277static const struct dm_gpio_ops gpio_sandbox_ops = {
Simon Glasse2d8a712014-02-26 15:59:25 -0700278 .direction_input = sb_gpio_direction_input,
279 .direction_output = sb_gpio_direction_output,
280 .get_value = sb_gpio_get_value,
281 .set_value = sb_gpio_set_value,
Simon Glass699ea962014-10-04 11:29:45 -0600282 .get_function = sb_gpio_get_function,
Simon Glass3669e0e2015-01-05 20:05:29 -0700283 .xlate = sb_gpio_xlate,
Simon Glass13979fc2021-02-04 21:21:55 -0700284 .set_flags = sb_gpio_set_flags,
Simon Glass96487892021-02-04 21:21:56 -0700285 .get_flags = sb_gpio_get_flags,
Simon Glass29126862020-07-07 13:11:44 -0600286#if CONFIG_IS_ENABLED(ACPIGEN)
287 .get_acpi = sb_gpio_get_acpi,
288#endif
Simon Glasse2d8a712014-02-26 15:59:25 -0700289};
290
Simon Glassd1998a92020-12-03 16:55:21 -0700291static int sandbox_gpio_of_to_plat(struct udevice *dev)
Simon Glasse2d8a712014-02-26 15:59:25 -0700292{
Simon Glasse564f052015-03-05 12:25:20 -0700293 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glasse2d8a712014-02-26 15:59:25 -0700294
Simon Glass995b60b2018-02-03 10:36:59 -0700295 uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count",
296 0);
Simon Glass95795ad2017-05-18 20:09:20 -0600297 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
Simon Glasse2d8a712014-02-26 15:59:25 -0700298
299 return 0;
300}
301
Heiko Schocher54c5d082014-05-22 12:43:05 +0200302static int gpio_sandbox_probe(struct udevice *dev)
Simon Glasse2d8a712014-02-26 15:59:25 -0700303{
Simon Glasse564f052015-03-05 12:25:20 -0700304 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glasse2d8a712014-02-26 15:59:25 -0700305
Simon Glass7d14ee42020-12-19 10:40:13 -0700306 if (!dev_has_ofnode(dev))
Simon Glasse2d8a712014-02-26 15:59:25 -0700307 /* Tell the uclass how many GPIOs we have */
308 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
Simon Glasse2d8a712014-02-26 15:59:25 -0700309
Simon Glass0fd3d912020-12-22 19:30:28 -0700310 dev_set_priv(dev,
311 calloc(sizeof(struct gpio_state), uc_priv->gpio_count));
Simon Glasse2d8a712014-02-26 15:59:25 -0700312
313 return 0;
314}
315
Simon Glass62cb89d2014-10-04 11:29:46 -0600316static int gpio_sandbox_remove(struct udevice *dev)
317{
Simon Glass0fd3d912020-12-22 19:30:28 -0700318 free(dev_get_priv(dev));
Simon Glass62cb89d2014-10-04 11:29:46 -0600319
320 return 0;
321}
322
Simon Glassae7f4512014-06-11 23:29:45 -0600323static const struct udevice_id sandbox_gpio_ids[] = {
Simon Glasse2d8a712014-02-26 15:59:25 -0700324 { .compatible = "sandbox,gpio" },
325 { }
326};
327
Walter Lozanoe3e24702020-06-25 01:10:04 -0300328U_BOOT_DRIVER(sandbox_gpio) = {
329 .name = "sandbox_gpio",
Simon Glasse2d8a712014-02-26 15:59:25 -0700330 .id = UCLASS_GPIO,
331 .of_match = sandbox_gpio_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700332 .of_to_plat = sandbox_gpio_of_to_plat,
Simon Glasse2d8a712014-02-26 15:59:25 -0700333 .probe = gpio_sandbox_probe,
Simon Glass62cb89d2014-10-04 11:29:46 -0600334 .remove = gpio_sandbox_remove,
Simon Glasse2d8a712014-02-26 15:59:25 -0700335 .ops = &gpio_sandbox_ops,
Simon Glass29126862020-07-07 13:11:44 -0600336 ACPI_OPS_PTR(&gpio_sandbox_acpi_ops)
Simon Glasse2d8a712014-02-26 15:59:25 -0700337};
Patrick Delaunaye5301ba2020-01-13 11:35:15 +0100338
Simon Glassbdf8fd72020-12-28 20:34:57 -0700339DM_DRIVER_ALIAS(sandbox_gpio, sandbox_gpio_alias)
Walter Lozanoaddf3582020-06-25 01:10:06 -0300340
Patrick Delaunaye5301ba2020-01-13 11:35:15 +0100341/* pincontrol: used only to check GPIO pin configuration (pinmux command) */
342
343struct sb_pinctrl_priv {
344 int pinctrl_ngpios;
345 struct list_head gpio_dev;
346};
347
348struct sb_gpio_bank {
349 struct udevice *gpio_dev;
350 struct list_head list;
351};
352
353static int sb_populate_gpio_dev_list(struct udevice *dev)
354{
355 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
356 struct udevice *gpio_dev;
357 struct udevice *child;
358 struct sb_gpio_bank *gpio_bank;
359 int ret;
360
361 /*
362 * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
363 * a list with all gpio device reference which belongs to the
364 * current pin-controller. This list is used to find pin_name and
365 * pin muxing
366 */
367 list_for_each_entry(child, &dev->child_head, sibling_node) {
368 ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
369 &gpio_dev);
370 if (ret < 0)
371 continue;
372
373 gpio_bank = malloc(sizeof(*gpio_bank));
374 if (!gpio_bank) {
375 dev_err(dev, "Not enough memory\n");
376 return -ENOMEM;
377 }
378
379 gpio_bank->gpio_dev = gpio_dev;
380 list_add_tail(&gpio_bank->list, &priv->gpio_dev);
381 }
382
383 return 0;
384}
385
386static int sb_pinctrl_get_pins_count(struct udevice *dev)
387{
388 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
389 struct gpio_dev_priv *uc_priv;
390 struct sb_gpio_bank *gpio_bank;
391
392 /*
393 * if get_pins_count has already been executed once on this
394 * pin-controller, no need to run it again
395 */
396 if (priv->pinctrl_ngpios)
397 return priv->pinctrl_ngpios;
398
399 if (list_empty(&priv->gpio_dev))
400 sb_populate_gpio_dev_list(dev);
401 /*
402 * walk through all banks to retrieve the pin-controller
403 * pins number
404 */
405 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
406 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
407
408 priv->pinctrl_ngpios += uc_priv->gpio_count;
409 }
410
411 return priv->pinctrl_ngpios;
412}
413
414static struct udevice *sb_pinctrl_get_gpio_dev(struct udevice *dev,
415 unsigned int selector,
416 unsigned int *idx)
417{
418 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
419 struct sb_gpio_bank *gpio_bank;
420 struct gpio_dev_priv *uc_priv;
421 int pin_count = 0;
422
423 if (list_empty(&priv->gpio_dev))
424 sb_populate_gpio_dev_list(dev);
425
426 /* look up for the bank which owns the requested pin */
427 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
428 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
429
430 if (selector < (pin_count + uc_priv->gpio_count)) {
431 /*
432 * we found the bank, convert pin selector to
433 * gpio bank index
434 */
435 *idx = selector - pin_count;
436
437 return gpio_bank->gpio_dev;
438 }
439 pin_count += uc_priv->gpio_count;
440 }
441
442 return NULL;
443}
444
445static const char *sb_pinctrl_get_pin_name(struct udevice *dev,
446 unsigned int selector)
447{
448 struct gpio_dev_priv *uc_priv;
449 struct udevice *gpio_dev;
450 unsigned int gpio_idx;
451 static char pin_name[PINNAME_SIZE];
452
453 /* look up for the bank which owns the requested pin */
454 gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
455 if (!gpio_dev) {
456 snprintf(pin_name, PINNAME_SIZE, "Error");
457 } else {
458 uc_priv = dev_get_uclass_priv(gpio_dev);
459
460 snprintf(pin_name, PINNAME_SIZE, "%s%d",
461 uc_priv->bank_name,
462 gpio_idx);
463 }
464
465 return pin_name;
466}
467
Simon Glassa03a0aa2021-02-04 21:21:59 -0700468static char *get_flags_string(ulong flags)
Patrick Delaunaye5301ba2020-01-13 11:35:15 +0100469{
470 if (flags & GPIOD_OPEN_DRAIN)
471 return "drive-open-drain";
472 if (flags & GPIOD_OPEN_SOURCE)
473 return "drive-open-source";
474 if (flags & GPIOD_PULL_UP)
475 return "bias-pull-up";
476 if (flags & GPIOD_PULL_DOWN)
477 return "bias-pull-down";
478 return ".";
479}
480
481static int sb_pinctrl_get_pin_muxing(struct udevice *dev,
482 unsigned int selector,
483 char *buf, int size)
484{
485 struct udevice *gpio_dev;
486 unsigned int gpio_idx;
Simon Glassa03a0aa2021-02-04 21:21:59 -0700487 ulong flags;
Patrick Delaunaye5301ba2020-01-13 11:35:15 +0100488 int function;
489
490 /* look up for the bank which owns the requested pin */
491 gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
492 if (!gpio_dev) {
493 snprintf(buf, size, "Error");
494 } else {
495 function = sb_gpio_get_function(gpio_dev, gpio_idx);
Simon Glassa03a0aa2021-02-04 21:21:59 -0700496 flags = *get_gpio_flags(gpio_dev, gpio_idx);
Patrick Delaunaye5301ba2020-01-13 11:35:15 +0100497
498 snprintf(buf, size, "gpio %s %s",
499 function == GPIOF_OUTPUT ? "output" : "input",
Simon Glassa03a0aa2021-02-04 21:21:59 -0700500 get_flags_string(flags));
Patrick Delaunaye5301ba2020-01-13 11:35:15 +0100501 }
502
503 return 0;
504}
505
Simon Glass29126862020-07-07 13:11:44 -0600506#if CONFIG_IS_ENABLED(ACPIGEN)
507static int sb_pinctrl_get_name(const struct udevice *dev, char *out_name)
508{
509 return acpi_copy_name(out_name, "PINC");
510}
511#endif
512
Patrick Delaunaye5301ba2020-01-13 11:35:15 +0100513static int sandbox_pinctrl_probe(struct udevice *dev)
514{
515 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
516
517 INIT_LIST_HEAD(&priv->gpio_dev);
518
519 return 0;
520}
521
522static struct pinctrl_ops sandbox_pinctrl_gpio_ops = {
523 .get_pin_name = sb_pinctrl_get_pin_name,
524 .get_pins_count = sb_pinctrl_get_pins_count,
525 .get_pin_muxing = sb_pinctrl_get_pin_muxing,
526};
527
Simon Glass29126862020-07-07 13:11:44 -0600528#if CONFIG_IS_ENABLED(ACPIGEN)
529struct acpi_ops pinctrl_sandbox_acpi_ops = {
530 .get_name = sb_pinctrl_get_name,
531};
532#endif
533
Patrick Delaunaye5301ba2020-01-13 11:35:15 +0100534static const struct udevice_id sandbox_pinctrl_gpio_match[] = {
535 { .compatible = "sandbox,pinctrl-gpio" },
536 { /* sentinel */ }
537};
538
539U_BOOT_DRIVER(sandbox_pinctrl_gpio) = {
540 .name = "sandbox_pinctrl_gpio",
541 .id = UCLASS_PINCTRL,
542 .of_match = sandbox_pinctrl_gpio_match,
543 .ops = &sandbox_pinctrl_gpio_ops,
544 .bind = dm_scan_fdt_dev,
545 .probe = sandbox_pinctrl_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700546 .priv_auto = sizeof(struct sb_pinctrl_priv),
Simon Glass29126862020-07-07 13:11:44 -0600547 ACPI_OPS_PTR(&pinctrl_sandbox_acpi_ops)
Patrick Delaunaye5301ba2020-01-13 11:35:15 +0100548};