2 * Copyright (c) 2013 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
11 #include <linux/ctype.h>
14 * gpio_to_device() - Convert global GPIO number to device, number
15 * gpio: The numeric representation of the GPIO
17 * Convert the GPIO number to an entry in the list of GPIOs
18 * or GPIO blocks registered with the GPIO controller. Returns
19 * entry on success, NULL on error.
21 static int gpio_to_device(unsigned int gpio, struct udevice **devp,
24 struct gpio_dev_priv *uc_priv;
28 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
30 ret = uclass_next_device(&dev)) {
31 uc_priv = dev->uclass_priv;
32 if (gpio >= uc_priv->gpio_base &&
33 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
35 *offset = gpio - uc_priv->gpio_base;
41 return ret ? ret : -EINVAL;
44 int gpio_lookup_name(const char *name, struct udevice **devp,
45 unsigned int *offsetp, unsigned int *gpiop)
47 struct gpio_dev_priv *uc_priv = NULL;
55 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
56 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
58 ret = uclass_next_device(&dev)) {
61 uc_priv = dev->uclass_priv;
63 offset = numeric - uc_priv->gpio_base;
64 /* Allow GPIOs to be numbered from 0 */
65 if (offset >= 0 && offset < uc_priv->gpio_count)
69 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
71 if (!strncasecmp(name, uc_priv->bank_name, len)) {
72 if (!strict_strtoul(name + len, 10, &offset))
78 return ret ? ret : -EINVAL;
85 *gpiop = uc_priv->gpio_base + offset;
91 * gpio_request() - [COMPAT] Request GPIO
93 * label: Name for the requested GPIO
95 * This function implements the API that's compatible with current
96 * GPIO API used in U-Boot. The request is forwarded to particular
97 * GPIO driver. Returns 0 on success, negative value on error.
99 int gpio_request(unsigned gpio, const char *label)
105 ret = gpio_to_device(gpio, &dev, &offset);
109 if (!gpio_get_ops(dev)->request)
112 return gpio_get_ops(dev)->request(dev, offset, label);
116 * gpio_free() - [COMPAT] Relinquish GPIO
119 * This function implements the API that's compatible with current
120 * GPIO API used in U-Boot. The request is forwarded to particular
121 * GPIO driver. Returns 0 on success, negative value on error.
123 int gpio_free(unsigned gpio)
129 ret = gpio_to_device(gpio, &dev, &offset);
133 if (!gpio_get_ops(dev)->free)
135 return gpio_get_ops(dev)->free(dev, offset);
139 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
142 * This function implements the API that's compatible with current
143 * GPIO API used in U-Boot. The request is forwarded to particular
144 * GPIO driver. Returns 0 on success, negative value on error.
146 int gpio_direction_input(unsigned gpio)
152 ret = gpio_to_device(gpio, &dev, &offset);
156 return gpio_get_ops(dev)->direction_input(dev, offset);
160 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
162 * value: Logical value to be set on the GPIO pin
164 * This function implements the API that's compatible with current
165 * GPIO API used in U-Boot. The request is forwarded to particular
166 * GPIO driver. Returns 0 on success, negative value on error.
168 int gpio_direction_output(unsigned gpio, int value)
174 ret = gpio_to_device(gpio, &dev, &offset);
178 return gpio_get_ops(dev)->direction_output(dev, offset, value);
182 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
185 * This function implements the API that's compatible with current
186 * GPIO API used in U-Boot. The request is forwarded to particular
187 * GPIO driver. Returns the value of the GPIO pin, or negative value
190 int gpio_get_value(unsigned gpio)
196 ret = gpio_to_device(gpio, &dev, &offset);
200 return gpio_get_ops(dev)->get_value(dev, offset);
204 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
206 * value: Logical value to be set on the GPIO pin.
208 * This function implements the API that's compatible with current
209 * GPIO API used in U-Boot. The request is forwarded to particular
210 * GPIO driver. Returns 0 on success, negative value on error.
212 int gpio_set_value(unsigned gpio, int value)
218 ret = gpio_to_device(gpio, &dev, &offset);
222 return gpio_get_ops(dev)->set_value(dev, offset, value);
225 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
227 struct gpio_dev_priv *priv;
229 /* Must be called on an active device */
230 priv = dev->uclass_priv;
233 *bit_count = priv->gpio_count;
234 return priv->bank_name;
237 /* We need to renumber the GPIOs when any driver is probed/removed */
238 static int gpio_renumber(void)
240 struct gpio_dev_priv *uc_priv;
246 ret = uclass_get(UCLASS_GPIO, &uc);
250 /* Ensure that we have a base for each bank */
252 uclass_foreach_dev(dev, uc) {
253 if (device_active(dev)) {
254 uc_priv = dev->uclass_priv;
255 uc_priv->gpio_base = base;
256 base += uc_priv->gpio_count;
263 static int gpio_post_probe(struct udevice *dev)
265 return gpio_renumber();
268 static int gpio_pre_remove(struct udevice *dev)
270 return gpio_renumber();
273 UCLASS_DRIVER(gpio) = {
276 .post_probe = gpio_post_probe,
277 .pre_remove = gpio_pre_remove,
278 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),