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 dir_flags; /* dir_flags (GPIOD_...) */
28 /* Access routines for GPIO dir flags */
29 static ulong *get_gpio_dir_flags(struct udevice *dev, unsigned int 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 static ulong invalid_dir_flags;
36 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
37 return &invalid_dir_flags;
40 return &state[offset].dir_flags;
44 static int get_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag)
46 return (*get_gpio_dir_flags(dev, offset) & flag) != 0;
49 static int set_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag,
52 ulong *gpio = get_gpio_dir_flags(dev, offset);
63 * Back-channel sandbox-internal-only access to GPIO state
66 int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
68 if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
69 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
70 return get_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE);
73 int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
75 return set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE, value);
78 int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
80 return get_gpio_flag(dev, offset, GPIOD_IS_OUT);
83 int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
85 set_gpio_flag(dev, offset, GPIOD_IS_OUT, output);
86 set_gpio_flag(dev, offset, GPIOD_IS_IN, !(output));
91 ulong sandbox_gpio_get_dir_flags(struct udevice *dev, unsigned int offset)
93 return *get_gpio_dir_flags(dev, offset);
96 int sandbox_gpio_set_dir_flags(struct udevice *dev, unsigned int offset,
99 *get_gpio_dir_flags(dev, offset) = flags;
105 * These functions implement the public interface within U-Boot
108 /* set GPIO port 'offset' as an input */
109 static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
111 debug("%s: offset:%u\n", __func__, offset);
113 return sandbox_gpio_set_direction(dev, offset, 0);
116 /* set GPIO port 'offset' as an output, with polarity 'value' */
117 static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
120 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
122 return sandbox_gpio_set_direction(dev, offset, 1) |
123 sandbox_gpio_set_value(dev, offset, value);
126 /* read GPIO IN value of port 'offset' */
127 static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
129 debug("%s: offset:%u\n", __func__, offset);
131 return sandbox_gpio_get_value(dev, offset);
134 /* write GPIO OUT value to port 'offset' */
135 static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
137 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
139 if (!sandbox_gpio_get_direction(dev, offset)) {
140 printf("sandbox_gpio: error: set_value on input gpio %u\n",
145 return sandbox_gpio_set_value(dev, offset, value);
148 static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
150 if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
152 if (get_gpio_flag(dev, offset, GPIOD_IS_IN))
155 return GPIOF_INPUT; /*GPIO is not configurated */
158 static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
159 struct ofnode_phandle_args *args)
161 desc->offset = args->args[0];
162 if (args->args_count < 2)
164 /* treat generic binding with gpio uclass */
165 gpio_xlate_offs_flags(dev, desc, args);
167 /* sandbox test specific, not defined in gpio.h */
168 if (args->args[1] & GPIO_IN)
169 desc->flags |= GPIOD_IS_IN;
171 if (args->args[1] & GPIO_OUT)
172 desc->flags |= GPIOD_IS_OUT;
174 if (args->args[1] & GPIO_OUT_ACTIVE)
175 desc->flags |= GPIOD_IS_OUT_ACTIVE;
180 static int sb_gpio_set_dir_flags(struct udevice *dev, unsigned int offset,
185 debug("%s: offset:%u, dir_flags = %lx\n", __func__, offset, flags);
187 dir_flags = get_gpio_dir_flags(dev, offset);
190 * For testing purposes keep the output value when switching to input.
191 * This allows us to manipulate the input value via the gpio command.
193 if (flags & GPIOD_IS_IN)
194 *dir_flags = (flags & ~GPIOD_IS_OUT_ACTIVE) |
195 (*dir_flags & GPIOD_IS_OUT_ACTIVE);
202 static int sb_gpio_get_dir_flags(struct udevice *dev, unsigned int offset,
205 debug("%s: offset:%u\n", __func__, offset);
206 *flags = *get_gpio_dir_flags(dev, offset);
211 #if CONFIG_IS_ENABLED(ACPIGEN)
212 static int sb_gpio_get_acpi(const struct gpio_desc *desc,
213 struct acpi_gpio *gpio)
217 /* Note that gpio_get_acpi() zeroes *gpio before calling here */
219 gpio->pins[0] = desc->offset;
220 ret = acpi_device_scope(desc->dev, gpio->resource,
221 sizeof(gpio->resource));
225 /* All of these values are just used for testing */
226 if (desc->flags & GPIOD_ACTIVE_LOW) {
227 gpio->pin0_addr = 0x80012 + desc->offset;
228 gpio->type = ACPI_GPIO_TYPE_INTERRUPT;
229 gpio->pull = ACPI_GPIO_PULL_DOWN;
230 gpio->interrupt_debounce_timeout = 4321;
232 /* We use the GpioInt part */
233 gpio->irq.pin = desc->offset;
234 gpio->irq.polarity = ACPI_IRQ_ACTIVE_BOTH;
235 gpio->irq.shared = ACPI_IRQ_SHARED;
236 gpio->irq.wake = ACPI_IRQ_WAKE;
238 /* The GpioIo part is only used for testing */
239 gpio->polarity = ACPI_GPIO_ACTIVE_LOW;
241 gpio->pin0_addr = 0xc00dc + desc->offset;
242 gpio->type = ACPI_GPIO_TYPE_IO;
243 gpio->pull = ACPI_GPIO_PULL_UP;
244 gpio->interrupt_debounce_timeout = 0;
246 /* The GpioInt part is not used */
248 /* We use the GpioIo part */
249 gpio->output_drive_strength = 1234;
250 gpio->io_shared = true;
251 gpio->io_restrict = ACPI_GPIO_IO_RESTRICT_INPUT;
258 static int sb_gpio_get_name(const struct udevice *dev, char *out_name)
260 return acpi_copy_name(out_name, "GPIO");
263 struct acpi_ops gpio_sandbox_acpi_ops = {
264 .get_name = sb_gpio_get_name,
268 static const struct dm_gpio_ops gpio_sandbox_ops = {
269 .direction_input = sb_gpio_direction_input,
270 .direction_output = sb_gpio_direction_output,
271 .get_value = sb_gpio_get_value,
272 .set_value = sb_gpio_set_value,
273 .get_function = sb_gpio_get_function,
274 .xlate = sb_gpio_xlate,
275 .set_dir_flags = sb_gpio_set_dir_flags,
276 .get_dir_flags = sb_gpio_get_dir_flags,
277 #if CONFIG_IS_ENABLED(ACPIGEN)
278 .get_acpi = sb_gpio_get_acpi,
282 static int sandbox_gpio_of_to_plat(struct udevice *dev)
284 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
286 uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count",
288 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
293 static int gpio_sandbox_probe(struct udevice *dev)
295 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
297 if (!dev_has_ofnode(dev))
298 /* Tell the uclass how many GPIOs we have */
299 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
302 calloc(sizeof(struct gpio_state), uc_priv->gpio_count));
307 static int gpio_sandbox_remove(struct udevice *dev)
309 free(dev_get_priv(dev));
314 static const struct udevice_id sandbox_gpio_ids[] = {
315 { .compatible = "sandbox,gpio" },
319 U_BOOT_DRIVER(sandbox_gpio) = {
320 .name = "sandbox_gpio",
322 .of_match = sandbox_gpio_ids,
323 .of_to_plat = sandbox_gpio_of_to_plat,
324 .probe = gpio_sandbox_probe,
325 .remove = gpio_sandbox_remove,
326 .ops = &gpio_sandbox_ops,
327 ACPI_OPS_PTR(&gpio_sandbox_acpi_ops)
330 U_BOOT_DRIVER_ALIAS(sandbox_gpio, sandbox_gpio_alias)
332 /* pincontrol: used only to check GPIO pin configuration (pinmux command) */
334 struct sb_pinctrl_priv {
336 struct list_head gpio_dev;
339 struct sb_gpio_bank {
340 struct udevice *gpio_dev;
341 struct list_head list;
344 static int sb_populate_gpio_dev_list(struct udevice *dev)
346 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
347 struct udevice *gpio_dev;
348 struct udevice *child;
349 struct sb_gpio_bank *gpio_bank;
353 * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
354 * a list with all gpio device reference which belongs to the
355 * current pin-controller. This list is used to find pin_name and
358 list_for_each_entry(child, &dev->child_head, sibling_node) {
359 ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
364 gpio_bank = malloc(sizeof(*gpio_bank));
366 dev_err(dev, "Not enough memory\n");
370 gpio_bank->gpio_dev = gpio_dev;
371 list_add_tail(&gpio_bank->list, &priv->gpio_dev);
377 static int sb_pinctrl_get_pins_count(struct udevice *dev)
379 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
380 struct gpio_dev_priv *uc_priv;
381 struct sb_gpio_bank *gpio_bank;
384 * if get_pins_count has already been executed once on this
385 * pin-controller, no need to run it again
387 if (priv->pinctrl_ngpios)
388 return priv->pinctrl_ngpios;
390 if (list_empty(&priv->gpio_dev))
391 sb_populate_gpio_dev_list(dev);
393 * walk through all banks to retrieve the pin-controller
396 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
397 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
399 priv->pinctrl_ngpios += uc_priv->gpio_count;
402 return priv->pinctrl_ngpios;
405 static struct udevice *sb_pinctrl_get_gpio_dev(struct udevice *dev,
406 unsigned int selector,
409 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
410 struct sb_gpio_bank *gpio_bank;
411 struct gpio_dev_priv *uc_priv;
414 if (list_empty(&priv->gpio_dev))
415 sb_populate_gpio_dev_list(dev);
417 /* look up for the bank which owns the requested pin */
418 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
419 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
421 if (selector < (pin_count + uc_priv->gpio_count)) {
423 * we found the bank, convert pin selector to
426 *idx = selector - pin_count;
428 return gpio_bank->gpio_dev;
430 pin_count += uc_priv->gpio_count;
436 static const char *sb_pinctrl_get_pin_name(struct udevice *dev,
437 unsigned int selector)
439 struct gpio_dev_priv *uc_priv;
440 struct udevice *gpio_dev;
441 unsigned int gpio_idx;
442 static char pin_name[PINNAME_SIZE];
444 /* look up for the bank which owns the requested pin */
445 gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
447 snprintf(pin_name, PINNAME_SIZE, "Error");
449 uc_priv = dev_get_uclass_priv(gpio_dev);
451 snprintf(pin_name, PINNAME_SIZE, "%s%d",
459 static char *get_dir_flags_string(ulong flags)
461 if (flags & GPIOD_OPEN_DRAIN)
462 return "drive-open-drain";
463 if (flags & GPIOD_OPEN_SOURCE)
464 return "drive-open-source";
465 if (flags & GPIOD_PULL_UP)
466 return "bias-pull-up";
467 if (flags & GPIOD_PULL_DOWN)
468 return "bias-pull-down";
472 static int sb_pinctrl_get_pin_muxing(struct udevice *dev,
473 unsigned int selector,
476 struct udevice *gpio_dev;
477 unsigned int gpio_idx;
481 /* look up for the bank which owns the requested pin */
482 gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
484 snprintf(buf, size, "Error");
486 function = sb_gpio_get_function(gpio_dev, gpio_idx);
487 dir_flags = *get_gpio_dir_flags(gpio_dev, gpio_idx);
489 snprintf(buf, size, "gpio %s %s",
490 function == GPIOF_OUTPUT ? "output" : "input",
491 get_dir_flags_string(dir_flags));
497 #if CONFIG_IS_ENABLED(ACPIGEN)
498 static int sb_pinctrl_get_name(const struct udevice *dev, char *out_name)
500 return acpi_copy_name(out_name, "PINC");
504 static int sandbox_pinctrl_probe(struct udevice *dev)
506 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
508 INIT_LIST_HEAD(&priv->gpio_dev);
513 static struct pinctrl_ops sandbox_pinctrl_gpio_ops = {
514 .get_pin_name = sb_pinctrl_get_pin_name,
515 .get_pins_count = sb_pinctrl_get_pins_count,
516 .get_pin_muxing = sb_pinctrl_get_pin_muxing,
519 #if CONFIG_IS_ENABLED(ACPIGEN)
520 struct acpi_ops pinctrl_sandbox_acpi_ops = {
521 .get_name = sb_pinctrl_get_name,
525 static const struct udevice_id sandbox_pinctrl_gpio_match[] = {
526 { .compatible = "sandbox,pinctrl-gpio" },
530 U_BOOT_DRIVER(sandbox_pinctrl_gpio) = {
531 .name = "sandbox_pinctrl_gpio",
532 .id = UCLASS_PINCTRL,
533 .of_match = sandbox_pinctrl_gpio_match,
534 .ops = &sandbox_pinctrl_gpio_ops,
535 .bind = dm_scan_fdt_dev,
536 .probe = sandbox_pinctrl_probe,
537 .priv_auto = sizeof(struct sb_pinctrl_priv),
538 ACPI_OPS_PTR(&pinctrl_sandbox_acpi_ops)