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>
23 const char *label; /* label given by requester */
24 ulong flags; /* flags (GPIOD_...) */
27 /* Access routines for GPIO info */
28 static struct gpio_state *get_gpio_state(struct udevice *dev, uint offset)
30 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
31 struct gpio_state *state = dev_get_priv(dev);
33 if (offset >= uc_priv->gpio_count) {
34 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
38 return &state[offset];
41 /* Access routines for GPIO flags */
42 static ulong *get_gpio_flags(struct udevice *dev, unsigned int offset)
44 struct gpio_state *state = get_gpio_state(dev, offset);
53 static int get_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag)
55 return (*get_gpio_flags(dev, offset) & flag) != 0;
58 static int set_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag,
61 struct gpio_state *state = get_gpio_state(dev, offset);
66 state->flags &= ~flag;
72 * Back-channel sandbox-internal-only access to GPIO state
75 int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
77 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 if (state->flags & GPIOD_EXT_DRIVEN) {
84 val = state->flags & GPIOD_EXT_HIGH;
86 if (state->flags & GPIOD_EXT_PULL_UP)
88 else if (state->flags & GPIOD_EXT_PULL_DOWN)
91 val = state->flags & GPIOD_PULL_UP;
97 int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
99 set_gpio_flag(dev, offset, GPIOD_EXT_DRIVEN | GPIOD_EXT_HIGH, value);
104 int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
106 return get_gpio_flag(dev, offset, GPIOD_IS_OUT);
109 int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
111 set_gpio_flag(dev, offset, GPIOD_IS_OUT, output);
112 set_gpio_flag(dev, offset, GPIOD_IS_IN, !output);
117 ulong sandbox_gpio_get_flags(struct udevice *dev, uint offset)
119 ulong flags = *get_gpio_flags(dev, offset);
121 return flags & ~GPIOD_SANDBOX_MASK;
124 int sandbox_gpio_set_flags(struct udevice *dev, uint offset, ulong flags)
126 struct gpio_state *state = get_gpio_state(dev, offset);
128 state->flags = flags;
134 * These functions implement the public interface within U-Boot
137 /* set GPIO port 'offset' as an input */
138 static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
140 debug("%s: offset:%u\n", __func__, offset);
142 return sandbox_gpio_set_direction(dev, offset, 0);
145 /* set GPIO port 'offset' as an output, with polarity 'value' */
146 static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
151 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
153 ret = sandbox_gpio_set_direction(dev, offset, 1);
156 ret = set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE |
157 GPIOD_EXT_DRIVEN | GPIOD_EXT_HIGH, value);
164 /* read GPIO IN value of port 'offset' */
165 static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
167 debug("%s: offset:%u\n", __func__, offset);
169 return sandbox_gpio_get_value(dev, offset);
172 /* write GPIO OUT value to port 'offset' */
173 static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
177 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
179 if (!sandbox_gpio_get_direction(dev, offset)) {
180 printf("sandbox_gpio: error: set_value on input gpio %u\n",
185 ret = set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE |
186 GPIOD_EXT_DRIVEN | GPIOD_EXT_HIGH, value);
193 static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
195 if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
197 if (get_gpio_flag(dev, offset, GPIOD_IS_IN))
199 if (get_gpio_flag(dev, offset, GPIOD_IS_AF))
202 return GPIOF_INPUT; /*GPIO is not configurated */
205 static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
206 struct ofnode_phandle_args *args)
208 desc->offset = args->args[0];
209 if (args->args_count < 2)
211 /* treat generic binding with gpio uclass */
212 gpio_xlate_offs_flags(dev, desc, args);
214 /* sandbox test specific, not defined in gpio.h */
215 if (args->args[1] & GPIO_IN)
216 desc->flags |= GPIOD_IS_IN;
218 if (args->args[1] & GPIO_OUT)
219 desc->flags |= GPIOD_IS_OUT;
221 if (args->args[1] & GPIO_OUT_ACTIVE)
222 desc->flags |= GPIOD_IS_OUT_ACTIVE;
224 if (args->args[1] & GPIO_AF)
225 desc->flags |= GPIOD_IS_AF;
230 static int sb_gpio_set_flags(struct udevice *dev, unsigned int offset,
233 debug("%s: offset:%u, flags = %lx\n", __func__, offset, flags);
234 struct gpio_state *state = get_gpio_state(dev, offset);
236 if (flags & GPIOD_IS_OUT) {
237 flags |= GPIOD_EXT_DRIVEN;
238 if (flags & GPIOD_IS_OUT_ACTIVE)
239 flags |= GPIOD_EXT_HIGH;
241 flags &= ~GPIOD_EXT_HIGH;
243 flags |= state->flags & GPIOD_SANDBOX_MASK;
245 state->flags = flags;
250 static int sb_gpio_get_flags(struct udevice *dev, uint offset, ulong *flagsp)
252 debug("%s: offset:%u\n", __func__, offset);
253 *flagsp = *get_gpio_flags(dev, offset) & ~GPIOD_SANDBOX_MASK;
258 #if CONFIG_IS_ENABLED(ACPIGEN)
259 static int sb_gpio_get_acpi(const struct gpio_desc *desc,
260 struct acpi_gpio *gpio)
264 /* Note that gpio_get_acpi() zeroes *gpio before calling here */
266 gpio->pins[0] = desc->offset;
267 ret = acpi_device_scope(desc->dev, gpio->resource,
268 sizeof(gpio->resource));
272 /* All of these values are just used for testing */
273 if (desc->flags & GPIOD_ACTIVE_LOW) {
274 gpio->pin0_addr = 0x80012 + desc->offset;
275 gpio->type = ACPI_GPIO_TYPE_INTERRUPT;
276 gpio->pull = ACPI_GPIO_PULL_DOWN;
277 gpio->interrupt_debounce_timeout = 4321;
279 /* We use the GpioInt part */
280 gpio->irq.pin = desc->offset;
281 gpio->irq.polarity = ACPI_IRQ_ACTIVE_BOTH;
282 gpio->irq.shared = ACPI_IRQ_SHARED;
283 gpio->irq.wake = ACPI_IRQ_WAKE;
285 /* The GpioIo part is only used for testing */
286 gpio->polarity = ACPI_GPIO_ACTIVE_LOW;
288 gpio->pin0_addr = 0xc00dc + desc->offset;
289 gpio->type = ACPI_GPIO_TYPE_IO;
290 gpio->pull = ACPI_GPIO_PULL_UP;
291 gpio->interrupt_debounce_timeout = 0;
293 /* The GpioInt part is not used */
295 /* We use the GpioIo part */
296 gpio->output_drive_strength = 1234;
297 gpio->io_shared = true;
298 gpio->io_restrict = ACPI_GPIO_IO_RESTRICT_INPUT;
305 static int sb_gpio_get_name(const struct udevice *dev, char *out_name)
307 return acpi_copy_name(out_name, "GPIO");
310 struct acpi_ops gpio_sandbox_acpi_ops = {
311 .get_name = sb_gpio_get_name,
315 static const struct dm_gpio_ops gpio_sandbox_ops = {
316 .direction_input = sb_gpio_direction_input,
317 .direction_output = sb_gpio_direction_output,
318 .get_value = sb_gpio_get_value,
319 .set_value = sb_gpio_set_value,
320 .get_function = sb_gpio_get_function,
321 .xlate = sb_gpio_xlate,
322 .set_flags = sb_gpio_set_flags,
323 .get_flags = sb_gpio_get_flags,
324 #if CONFIG_IS_ENABLED(ACPIGEN)
325 .get_acpi = sb_gpio_get_acpi,
329 static int sandbox_gpio_of_to_plat(struct udevice *dev)
331 if (CONFIG_IS_ENABLED(OF_REAL)) {
332 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
334 uc_priv->gpio_count =
335 dev_read_u32_default(dev, "sandbox,gpio-count", 0);
336 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
342 static int gpio_sandbox_probe(struct udevice *dev)
344 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
346 if (!dev_has_ofnode(dev))
347 /* Tell the uclass how many GPIOs we have */
348 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
351 calloc(sizeof(struct gpio_state), uc_priv->gpio_count));
356 static int gpio_sandbox_remove(struct udevice *dev)
358 free(dev_get_priv(dev));
363 static const struct udevice_id sandbox_gpio_ids[] = {
364 { .compatible = "sandbox,gpio" },
368 U_BOOT_DRIVER(sandbox_gpio) = {
369 .name = "sandbox_gpio",
371 .of_match = sandbox_gpio_ids,
372 .of_to_plat = sandbox_gpio_of_to_plat,
373 .probe = gpio_sandbox_probe,
374 .remove = gpio_sandbox_remove,
375 .ops = &gpio_sandbox_ops,
376 ACPI_OPS_PTR(&gpio_sandbox_acpi_ops)
379 DM_DRIVER_ALIAS(sandbox_gpio, sandbox_gpio_alias)
381 #if CONFIG_IS_ENABLED(PINCTRL)
383 /* pincontrol: used only to check GPIO pin configuration (pinmux command) */
385 struct sb_pinctrl_priv {
387 struct list_head gpio_dev;
390 struct sb_gpio_bank {
391 struct udevice *gpio_dev;
392 struct list_head list;
395 static int sb_populate_gpio_dev_list(struct udevice *dev)
397 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
398 struct udevice *gpio_dev;
399 struct udevice *child;
400 struct sb_gpio_bank *gpio_bank;
404 * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
405 * a list with all gpio device reference which belongs to the
406 * current pin-controller. This list is used to find pin_name and
409 list_for_each_entry(child, &dev->child_head, sibling_node) {
410 ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
415 gpio_bank = malloc(sizeof(*gpio_bank));
417 dev_err(dev, "Not enough memory\n");
421 gpio_bank->gpio_dev = gpio_dev;
422 list_add_tail(&gpio_bank->list, &priv->gpio_dev);
428 static int sb_pinctrl_get_pins_count(struct udevice *dev)
430 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
431 struct gpio_dev_priv *uc_priv;
432 struct sb_gpio_bank *gpio_bank;
435 * if get_pins_count has already been executed once on this
436 * pin-controller, no need to run it again
438 if (priv->pinctrl_ngpios)
439 return priv->pinctrl_ngpios;
441 if (list_empty(&priv->gpio_dev))
442 sb_populate_gpio_dev_list(dev);
444 * walk through all banks to retrieve the pin-controller
447 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
448 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
450 priv->pinctrl_ngpios += uc_priv->gpio_count;
453 return priv->pinctrl_ngpios;
456 static struct udevice *sb_pinctrl_get_gpio_dev(struct udevice *dev,
457 unsigned int selector,
460 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
461 struct sb_gpio_bank *gpio_bank;
462 struct gpio_dev_priv *uc_priv;
465 if (list_empty(&priv->gpio_dev))
466 sb_populate_gpio_dev_list(dev);
468 /* look up for the bank which owns the requested pin */
469 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
470 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
472 if (selector < (pin_count + uc_priv->gpio_count)) {
474 * we found the bank, convert pin selector to
477 *idx = selector - pin_count;
479 return gpio_bank->gpio_dev;
481 pin_count += uc_priv->gpio_count;
487 static const char *sb_pinctrl_get_pin_name(struct udevice *dev,
488 unsigned int selector)
490 struct gpio_dev_priv *uc_priv;
491 struct udevice *gpio_dev;
492 unsigned int gpio_idx;
493 static char pin_name[PINNAME_SIZE];
495 /* look up for the bank which owns the requested pin */
496 gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
498 snprintf(pin_name, PINNAME_SIZE, "Error");
500 uc_priv = dev_get_uclass_priv(gpio_dev);
502 snprintf(pin_name, PINNAME_SIZE, "%s%d",
510 static char *get_flags_string(ulong flags)
512 if (flags & GPIOD_OPEN_DRAIN)
513 return "drive-open-drain";
514 if (flags & GPIOD_OPEN_SOURCE)
515 return "drive-open-source";
516 if (flags & GPIOD_PULL_UP)
517 return "bias-pull-up";
518 if (flags & GPIOD_PULL_DOWN)
519 return "bias-pull-down";
523 static int sb_pinctrl_get_pin_muxing(struct udevice *dev,
524 unsigned int selector,
527 struct udevice *gpio_dev;
528 unsigned int gpio_idx;
532 /* look up for the bank which owns the requested pin */
533 gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
535 snprintf(buf, size, "Error");
537 function = sb_gpio_get_function(gpio_dev, gpio_idx);
538 flags = *get_gpio_flags(gpio_dev, gpio_idx);
540 snprintf(buf, size, "gpio %s %s",
541 function == GPIOF_OUTPUT ? "output" : "input",
542 get_flags_string(flags));
548 #if CONFIG_IS_ENABLED(ACPIGEN)
549 static int sb_pinctrl_get_name(const struct udevice *dev, char *out_name)
551 return acpi_copy_name(out_name, "PINC");
555 static int sandbox_pinctrl_probe(struct udevice *dev)
557 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
559 INIT_LIST_HEAD(&priv->gpio_dev);
564 static struct pinctrl_ops sandbox_pinctrl_gpio_ops = {
565 .get_pin_name = sb_pinctrl_get_pin_name,
566 .get_pins_count = sb_pinctrl_get_pins_count,
567 .get_pin_muxing = sb_pinctrl_get_pin_muxing,
570 #if CONFIG_IS_ENABLED(ACPIGEN)
571 struct acpi_ops pinctrl_sandbox_acpi_ops = {
572 .get_name = sb_pinctrl_get_name,
576 static const struct udevice_id sandbox_pinctrl_gpio_match[] = {
577 { .compatible = "sandbox,pinctrl-gpio" },
581 U_BOOT_DRIVER(sandbox_pinctrl_gpio) = {
582 .name = "sandbox_pinctrl_gpio",
583 .id = UCLASS_PINCTRL,
584 .of_match = sandbox_pinctrl_gpio_match,
585 .ops = &sandbox_pinctrl_gpio_ops,
586 .bind = dm_scan_fdt_dev,
587 .probe = sandbox_pinctrl_probe,
588 .priv_auto = sizeof(struct sb_pinctrl_priv),
589 ACPI_OPS_PTR(&pinctrl_sandbox_acpi_ops)