1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors.
12 #include <dm/device_compat.h>
15 #include <dm/pinctrl.h>
16 #include <dt-bindings/gpio/gpio.h>
17 #include <dt-bindings/gpio/sandbox-gpio.h>
21 const char *label; /* label given by requester */
22 ulong dir_flags; /* dir_flags (GPIOD_...) */
25 /* Access routines for GPIO dir flags */
26 static ulong *get_gpio_dir_flags(struct udevice *dev, unsigned int offset)
28 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
29 struct gpio_state *state = dev_get_priv(dev);
31 if (offset >= uc_priv->gpio_count) {
32 static ulong invalid_dir_flags;
33 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
34 return &invalid_dir_flags;
37 return &state[offset].dir_flags;
41 static int get_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag)
43 return (*get_gpio_dir_flags(dev, offset) & flag) != 0;
46 static int set_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag,
49 ulong *gpio = get_gpio_dir_flags(dev, offset);
60 * Back-channel sandbox-internal-only access to GPIO state
63 int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
65 if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
66 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
67 return get_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE);
70 int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
72 return set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE, value);
75 int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
77 return get_gpio_flag(dev, offset, GPIOD_IS_OUT);
80 int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
82 set_gpio_flag(dev, offset, GPIOD_IS_OUT, output);
83 set_gpio_flag(dev, offset, GPIOD_IS_IN, !(output));
88 ulong sandbox_gpio_get_dir_flags(struct udevice *dev, unsigned int offset)
90 return *get_gpio_dir_flags(dev, offset);
93 int sandbox_gpio_set_dir_flags(struct udevice *dev, unsigned int offset,
96 *get_gpio_dir_flags(dev, offset) = flags;
102 * These functions implement the public interface within U-Boot
105 /* set GPIO port 'offset' as an input */
106 static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
108 debug("%s: offset:%u\n", __func__, offset);
110 return sandbox_gpio_set_direction(dev, offset, 0);
113 /* set GPIO port 'offset' as an output, with polarity 'value' */
114 static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
117 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
119 return sandbox_gpio_set_direction(dev, offset, 1) |
120 sandbox_gpio_set_value(dev, offset, value);
123 /* read GPIO IN value of port 'offset' */
124 static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
126 debug("%s: offset:%u\n", __func__, offset);
128 return sandbox_gpio_get_value(dev, offset);
131 /* write GPIO OUT value to port 'offset' */
132 static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
134 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
136 if (!sandbox_gpio_get_direction(dev, offset)) {
137 printf("sandbox_gpio: error: set_value on input gpio %u\n",
142 return sandbox_gpio_set_value(dev, offset, value);
145 static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
147 if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
149 if (get_gpio_flag(dev, offset, GPIOD_IS_IN))
152 return GPIOF_INPUT; /*GPIO is not configurated */
155 static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
156 struct ofnode_phandle_args *args)
158 desc->offset = args->args[0];
159 if (args->args_count < 2)
161 /* treat generic binding with gpio uclass */
162 gpio_xlate_offs_flags(dev, desc, args);
164 /* sandbox test specific, not defined in gpio.h */
165 if (args->args[1] & GPIO_IN)
166 desc->flags |= GPIOD_IS_IN;
168 if (args->args[1] & GPIO_OUT)
169 desc->flags |= GPIOD_IS_OUT;
171 if (args->args[1] & GPIO_OUT_ACTIVE)
172 desc->flags |= GPIOD_IS_OUT_ACTIVE;
177 static int sb_gpio_set_dir_flags(struct udevice *dev, unsigned int offset,
182 debug("%s: offset:%u, dir_flags = %lx\n", __func__, offset, flags);
184 dir_flags = get_gpio_dir_flags(dev, offset);
191 static int sb_gpio_get_dir_flags(struct udevice *dev, unsigned int offset,
194 debug("%s: offset:%u\n", __func__, offset);
195 *flags = *get_gpio_dir_flags(dev, offset);
200 static const struct dm_gpio_ops gpio_sandbox_ops = {
201 .direction_input = sb_gpio_direction_input,
202 .direction_output = sb_gpio_direction_output,
203 .get_value = sb_gpio_get_value,
204 .set_value = sb_gpio_set_value,
205 .get_function = sb_gpio_get_function,
206 .xlate = sb_gpio_xlate,
207 .set_dir_flags = sb_gpio_set_dir_flags,
208 .get_dir_flags = sb_gpio_get_dir_flags,
211 static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
213 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
215 uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count",
217 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
222 static int gpio_sandbox_probe(struct udevice *dev)
224 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
226 if (!dev_of_valid(dev))
227 /* Tell the uclass how many GPIOs we have */
228 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
230 dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
235 static int gpio_sandbox_remove(struct udevice *dev)
242 static const struct udevice_id sandbox_gpio_ids[] = {
243 { .compatible = "sandbox,gpio" },
247 U_BOOT_DRIVER(gpio_sandbox) = {
248 .name = "gpio_sandbox",
250 .of_match = sandbox_gpio_ids,
251 .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
252 .probe = gpio_sandbox_probe,
253 .remove = gpio_sandbox_remove,
254 .ops = &gpio_sandbox_ops,
257 /* pincontrol: used only to check GPIO pin configuration (pinmux command) */
259 struct sb_pinctrl_priv {
261 struct list_head gpio_dev;
264 struct sb_gpio_bank {
265 struct udevice *gpio_dev;
266 struct list_head list;
269 static int sb_populate_gpio_dev_list(struct udevice *dev)
271 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
272 struct udevice *gpio_dev;
273 struct udevice *child;
274 struct sb_gpio_bank *gpio_bank;
278 * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
279 * a list with all gpio device reference which belongs to the
280 * current pin-controller. This list is used to find pin_name and
283 list_for_each_entry(child, &dev->child_head, sibling_node) {
284 ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
289 gpio_bank = malloc(sizeof(*gpio_bank));
291 dev_err(dev, "Not enough memory\n");
295 gpio_bank->gpio_dev = gpio_dev;
296 list_add_tail(&gpio_bank->list, &priv->gpio_dev);
302 static int sb_pinctrl_get_pins_count(struct udevice *dev)
304 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
305 struct gpio_dev_priv *uc_priv;
306 struct sb_gpio_bank *gpio_bank;
309 * if get_pins_count has already been executed once on this
310 * pin-controller, no need to run it again
312 if (priv->pinctrl_ngpios)
313 return priv->pinctrl_ngpios;
315 if (list_empty(&priv->gpio_dev))
316 sb_populate_gpio_dev_list(dev);
318 * walk through all banks to retrieve the pin-controller
321 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
322 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
324 priv->pinctrl_ngpios += uc_priv->gpio_count;
327 return priv->pinctrl_ngpios;
330 static struct udevice *sb_pinctrl_get_gpio_dev(struct udevice *dev,
331 unsigned int selector,
334 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
335 struct sb_gpio_bank *gpio_bank;
336 struct gpio_dev_priv *uc_priv;
339 if (list_empty(&priv->gpio_dev))
340 sb_populate_gpio_dev_list(dev);
342 /* look up for the bank which owns the requested pin */
343 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
344 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
346 if (selector < (pin_count + uc_priv->gpio_count)) {
348 * we found the bank, convert pin selector to
351 *idx = selector - pin_count;
353 return gpio_bank->gpio_dev;
355 pin_count += uc_priv->gpio_count;
361 static const char *sb_pinctrl_get_pin_name(struct udevice *dev,
362 unsigned int selector)
364 struct gpio_dev_priv *uc_priv;
365 struct udevice *gpio_dev;
366 unsigned int gpio_idx;
367 static char pin_name[PINNAME_SIZE];
369 /* look up for the bank which owns the requested pin */
370 gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
372 snprintf(pin_name, PINNAME_SIZE, "Error");
374 uc_priv = dev_get_uclass_priv(gpio_dev);
376 snprintf(pin_name, PINNAME_SIZE, "%s%d",
384 static char *get_dir_flags_string(ulong flags)
386 if (flags & GPIOD_OPEN_DRAIN)
387 return "drive-open-drain";
388 if (flags & GPIOD_OPEN_SOURCE)
389 return "drive-open-source";
390 if (flags & GPIOD_PULL_UP)
391 return "bias-pull-up";
392 if (flags & GPIOD_PULL_DOWN)
393 return "bias-pull-down";
397 static int sb_pinctrl_get_pin_muxing(struct udevice *dev,
398 unsigned int selector,
401 struct udevice *gpio_dev;
402 unsigned int gpio_idx;
406 /* look up for the bank which owns the requested pin */
407 gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
409 snprintf(buf, size, "Error");
411 function = sb_gpio_get_function(gpio_dev, gpio_idx);
412 dir_flags = *get_gpio_dir_flags(gpio_dev, gpio_idx);
414 snprintf(buf, size, "gpio %s %s",
415 function == GPIOF_OUTPUT ? "output" : "input",
416 get_dir_flags_string(dir_flags));
422 static int sandbox_pinctrl_probe(struct udevice *dev)
424 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
426 INIT_LIST_HEAD(&priv->gpio_dev);
431 static struct pinctrl_ops sandbox_pinctrl_gpio_ops = {
432 .get_pin_name = sb_pinctrl_get_pin_name,
433 .get_pins_count = sb_pinctrl_get_pins_count,
434 .get_pin_muxing = sb_pinctrl_get_pin_muxing,
437 static const struct udevice_id sandbox_pinctrl_gpio_match[] = {
438 { .compatible = "sandbox,pinctrl-gpio" },
442 U_BOOT_DRIVER(sandbox_pinctrl_gpio) = {
443 .name = "sandbox_pinctrl_gpio",
444 .id = UCLASS_PINCTRL,
445 .of_match = sandbox_pinctrl_gpio_match,
446 .ops = &sandbox_pinctrl_gpio_ops,
447 .bind = dm_scan_fdt_dev,
448 .probe = sandbox_pinctrl_probe,
449 .priv_auto_alloc_size = sizeof(struct sb_pinctrl_priv),