1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors.
11 #include <acpi/acpi_device.h>
14 #include <dm/device-internal.h>
15 #include <dm/device_compat.h>
18 #include <dm/pinctrl.h>
19 #include <dt-bindings/gpio/gpio.h>
20 #include <dt-bindings/gpio/sandbox-gpio.h>
24 const char *label; /* label given by requester */
25 ulong flags; /* flags (GPIOD_...) */
28 /* Access routines for GPIO info */
29 static struct gpio_state *get_gpio_state(struct udevice *dev, uint offset)
31 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
32 struct gpio_state *state = dev_get_priv(dev);
34 if (offset >= uc_priv->gpio_count) {
35 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
39 return &state[offset];
42 /* Access routines for GPIO flags */
43 static ulong *get_gpio_flags(struct udevice *dev, unsigned int offset)
45 struct gpio_state *state = get_gpio_state(dev, offset);
54 static int get_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag)
56 return (*get_gpio_flags(dev, offset) & flag) != 0;
59 static int set_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag,
62 struct gpio_state *state = get_gpio_state(dev, offset);
67 state->flags &= ~flag;
73 * Back-channel sandbox-internal-only access to GPIO state
76 int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
78 struct gpio_state *state = get_gpio_state(dev, offset);
80 if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
81 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
83 return state->flags & GPIOD_EXT_HIGH ? true : false;
86 int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
88 set_gpio_flag(dev, offset, GPIOD_EXT_HIGH, value);
93 int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
95 return get_gpio_flag(dev, offset, GPIOD_IS_OUT);
98 int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
100 set_gpio_flag(dev, offset, GPIOD_IS_OUT, output);
101 set_gpio_flag(dev, offset, GPIOD_IS_IN, !output);
106 ulong sandbox_gpio_get_flags(struct udevice *dev, uint offset)
108 ulong flags = *get_gpio_flags(dev, offset);
110 return flags & ~GPIOD_SANDBOX_MASK;
113 int sandbox_gpio_set_flags(struct udevice *dev, uint offset, ulong flags)
115 struct gpio_state *state = get_gpio_state(dev, offset);
117 state->flags = flags;
123 * These functions implement the public interface within U-Boot
126 /* set GPIO port 'offset' as an input */
127 static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
129 debug("%s: offset:%u\n", __func__, offset);
131 return sandbox_gpio_set_direction(dev, offset, 0);
134 /* set GPIO port 'offset' as an output, with polarity 'value' */
135 static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
140 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
142 ret = sandbox_gpio_set_direction(dev, offset, 1);
145 ret = set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE | GPIOD_EXT_HIGH,
153 /* read GPIO IN value of port 'offset' */
154 static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
156 debug("%s: offset:%u\n", __func__, offset);
158 return sandbox_gpio_get_value(dev, offset);
161 /* write GPIO OUT value to port 'offset' */
162 static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
166 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
168 if (!sandbox_gpio_get_direction(dev, offset)) {
169 printf("sandbox_gpio: error: set_value on input gpio %u\n",
174 ret = set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE | GPIOD_EXT_HIGH,
182 static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
184 if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
186 if (get_gpio_flag(dev, offset, GPIOD_IS_IN))
189 return GPIOF_INPUT; /*GPIO is not configurated */
192 static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
193 struct ofnode_phandle_args *args)
195 desc->offset = args->args[0];
196 if (args->args_count < 2)
198 /* treat generic binding with gpio uclass */
199 gpio_xlate_offs_flags(dev, desc, args);
201 /* sandbox test specific, not defined in gpio.h */
202 if (args->args[1] & GPIO_IN)
203 desc->flags |= GPIOD_IS_IN;
205 if (args->args[1] & GPIO_OUT)
206 desc->flags |= GPIOD_IS_OUT;
208 if (args->args[1] & GPIO_OUT_ACTIVE)
209 desc->flags |= GPIOD_IS_OUT_ACTIVE;
214 static int sb_gpio_set_flags(struct udevice *dev, unsigned int offset,
217 debug("%s: offset:%u, flags = %lx\n", __func__, offset, flags);
218 struct gpio_state *state = get_gpio_state(dev, offset);
220 if (flags & GPIOD_IS_OUT) {
221 if (flags & GPIOD_IS_OUT_ACTIVE)
222 flags |= GPIOD_EXT_HIGH;
224 flags &= ~GPIOD_EXT_HIGH;
226 state->flags = flags;
231 static int sb_gpio_get_flags(struct udevice *dev, uint offset, ulong *flagsp)
233 debug("%s: offset:%u\n", __func__, offset);
234 *flagsp = *get_gpio_flags(dev, offset) & ~GPIOD_SANDBOX_MASK;
239 #if CONFIG_IS_ENABLED(ACPIGEN)
240 static int sb_gpio_get_acpi(const struct gpio_desc *desc,
241 struct acpi_gpio *gpio)
245 /* Note that gpio_get_acpi() zeroes *gpio before calling here */
247 gpio->pins[0] = desc->offset;
248 ret = acpi_device_scope(desc->dev, gpio->resource,
249 sizeof(gpio->resource));
253 /* All of these values are just used for testing */
254 if (desc->flags & GPIOD_ACTIVE_LOW) {
255 gpio->pin0_addr = 0x80012 + desc->offset;
256 gpio->type = ACPI_GPIO_TYPE_INTERRUPT;
257 gpio->pull = ACPI_GPIO_PULL_DOWN;
258 gpio->interrupt_debounce_timeout = 4321;
260 /* We use the GpioInt part */
261 gpio->irq.pin = desc->offset;
262 gpio->irq.polarity = ACPI_IRQ_ACTIVE_BOTH;
263 gpio->irq.shared = ACPI_IRQ_SHARED;
264 gpio->irq.wake = ACPI_IRQ_WAKE;
266 /* The GpioIo part is only used for testing */
267 gpio->polarity = ACPI_GPIO_ACTIVE_LOW;
269 gpio->pin0_addr = 0xc00dc + desc->offset;
270 gpio->type = ACPI_GPIO_TYPE_IO;
271 gpio->pull = ACPI_GPIO_PULL_UP;
272 gpio->interrupt_debounce_timeout = 0;
274 /* The GpioInt part is not used */
276 /* We use the GpioIo part */
277 gpio->output_drive_strength = 1234;
278 gpio->io_shared = true;
279 gpio->io_restrict = ACPI_GPIO_IO_RESTRICT_INPUT;
286 static int sb_gpio_get_name(const struct udevice *dev, char *out_name)
288 return acpi_copy_name(out_name, "GPIO");
291 struct acpi_ops gpio_sandbox_acpi_ops = {
292 .get_name = sb_gpio_get_name,
296 static const struct dm_gpio_ops gpio_sandbox_ops = {
297 .direction_input = sb_gpio_direction_input,
298 .direction_output = sb_gpio_direction_output,
299 .get_value = sb_gpio_get_value,
300 .set_value = sb_gpio_set_value,
301 .get_function = sb_gpio_get_function,
302 .xlate = sb_gpio_xlate,
303 .set_flags = sb_gpio_set_flags,
304 .get_flags = sb_gpio_get_flags,
305 #if CONFIG_IS_ENABLED(ACPIGEN)
306 .get_acpi = sb_gpio_get_acpi,
310 static int sandbox_gpio_of_to_plat(struct udevice *dev)
312 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
314 uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count",
316 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
321 static int gpio_sandbox_probe(struct udevice *dev)
323 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
325 if (!dev_has_ofnode(dev))
326 /* Tell the uclass how many GPIOs we have */
327 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
330 calloc(sizeof(struct gpio_state), uc_priv->gpio_count));
335 static int gpio_sandbox_remove(struct udevice *dev)
337 free(dev_get_priv(dev));
342 static const struct udevice_id sandbox_gpio_ids[] = {
343 { .compatible = "sandbox,gpio" },
347 U_BOOT_DRIVER(sandbox_gpio) = {
348 .name = "sandbox_gpio",
350 .of_match = sandbox_gpio_ids,
351 .of_to_plat = sandbox_gpio_of_to_plat,
352 .probe = gpio_sandbox_probe,
353 .remove = gpio_sandbox_remove,
354 .ops = &gpio_sandbox_ops,
355 ACPI_OPS_PTR(&gpio_sandbox_acpi_ops)
358 DM_DRIVER_ALIAS(sandbox_gpio, sandbox_gpio_alias)
360 /* pincontrol: used only to check GPIO pin configuration (pinmux command) */
362 struct sb_pinctrl_priv {
364 struct list_head gpio_dev;
367 struct sb_gpio_bank {
368 struct udevice *gpio_dev;
369 struct list_head list;
372 static int sb_populate_gpio_dev_list(struct udevice *dev)
374 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
375 struct udevice *gpio_dev;
376 struct udevice *child;
377 struct sb_gpio_bank *gpio_bank;
381 * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
382 * a list with all gpio device reference which belongs to the
383 * current pin-controller. This list is used to find pin_name and
386 list_for_each_entry(child, &dev->child_head, sibling_node) {
387 ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
392 gpio_bank = malloc(sizeof(*gpio_bank));
394 dev_err(dev, "Not enough memory\n");
398 gpio_bank->gpio_dev = gpio_dev;
399 list_add_tail(&gpio_bank->list, &priv->gpio_dev);
405 static int sb_pinctrl_get_pins_count(struct udevice *dev)
407 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
408 struct gpio_dev_priv *uc_priv;
409 struct sb_gpio_bank *gpio_bank;
412 * if get_pins_count has already been executed once on this
413 * pin-controller, no need to run it again
415 if (priv->pinctrl_ngpios)
416 return priv->pinctrl_ngpios;
418 if (list_empty(&priv->gpio_dev))
419 sb_populate_gpio_dev_list(dev);
421 * walk through all banks to retrieve the pin-controller
424 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
425 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
427 priv->pinctrl_ngpios += uc_priv->gpio_count;
430 return priv->pinctrl_ngpios;
433 static struct udevice *sb_pinctrl_get_gpio_dev(struct udevice *dev,
434 unsigned int selector,
437 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
438 struct sb_gpio_bank *gpio_bank;
439 struct gpio_dev_priv *uc_priv;
442 if (list_empty(&priv->gpio_dev))
443 sb_populate_gpio_dev_list(dev);
445 /* look up for the bank which owns the requested pin */
446 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
447 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
449 if (selector < (pin_count + uc_priv->gpio_count)) {
451 * we found the bank, convert pin selector to
454 *idx = selector - pin_count;
456 return gpio_bank->gpio_dev;
458 pin_count += uc_priv->gpio_count;
464 static const char *sb_pinctrl_get_pin_name(struct udevice *dev,
465 unsigned int selector)
467 struct gpio_dev_priv *uc_priv;
468 struct udevice *gpio_dev;
469 unsigned int gpio_idx;
470 static char pin_name[PINNAME_SIZE];
472 /* look up for the bank which owns the requested pin */
473 gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
475 snprintf(pin_name, PINNAME_SIZE, "Error");
477 uc_priv = dev_get_uclass_priv(gpio_dev);
479 snprintf(pin_name, PINNAME_SIZE, "%s%d",
487 static char *get_flags_string(ulong flags)
489 if (flags & GPIOD_OPEN_DRAIN)
490 return "drive-open-drain";
491 if (flags & GPIOD_OPEN_SOURCE)
492 return "drive-open-source";
493 if (flags & GPIOD_PULL_UP)
494 return "bias-pull-up";
495 if (flags & GPIOD_PULL_DOWN)
496 return "bias-pull-down";
500 static int sb_pinctrl_get_pin_muxing(struct udevice *dev,
501 unsigned int selector,
504 struct udevice *gpio_dev;
505 unsigned int gpio_idx;
509 /* look up for the bank which owns the requested pin */
510 gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
512 snprintf(buf, size, "Error");
514 function = sb_gpio_get_function(gpio_dev, gpio_idx);
515 flags = *get_gpio_flags(gpio_dev, gpio_idx);
517 snprintf(buf, size, "gpio %s %s",
518 function == GPIOF_OUTPUT ? "output" : "input",
519 get_flags_string(flags));
525 #if CONFIG_IS_ENABLED(ACPIGEN)
526 static int sb_pinctrl_get_name(const struct udevice *dev, char *out_name)
528 return acpi_copy_name(out_name, "PINC");
532 static int sandbox_pinctrl_probe(struct udevice *dev)
534 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
536 INIT_LIST_HEAD(&priv->gpio_dev);
541 static struct pinctrl_ops sandbox_pinctrl_gpio_ops = {
542 .get_pin_name = sb_pinctrl_get_pin_name,
543 .get_pins_count = sb_pinctrl_get_pins_count,
544 .get_pin_muxing = sb_pinctrl_get_pin_muxing,
547 #if CONFIG_IS_ENABLED(ACPIGEN)
548 struct acpi_ops pinctrl_sandbox_acpi_ops = {
549 .get_name = sb_pinctrl_get_name,
553 static const struct udevice_id sandbox_pinctrl_gpio_match[] = {
554 { .compatible = "sandbox,pinctrl-gpio" },
558 U_BOOT_DRIVER(sandbox_pinctrl_gpio) = {
559 .name = "sandbox_pinctrl_gpio",
560 .id = UCLASS_PINCTRL,
561 .of_match = sandbox_pinctrl_gpio_match,
562 .ops = &sandbox_pinctrl_gpio_ops,
563 .bind = dm_scan_fdt_dev,
564 .probe = sandbox_pinctrl_probe,
565 .priv_auto = sizeof(struct sb_pinctrl_priv),
566 ACPI_OPS_PTR(&pinctrl_sandbox_acpi_ops)