2 * Copyright (c) 2013 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
13 * gpio_to_device() - Convert global GPIO number to device, number
14 * gpio: The numeric representation of the GPIO
16 * Convert the GPIO number to an entry in the list of GPIOs
17 * or GPIO blocks registered with the GPIO controller. Returns
18 * entry on success, NULL on error.
20 static int gpio_to_device(unsigned int gpio, struct udevice **devp,
23 struct gpio_dev_priv *uc_priv;
27 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
29 ret = uclass_next_device(&dev)) {
30 uc_priv = dev->uclass_priv;
31 if (gpio >= uc_priv->gpio_base &&
32 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
34 *offset = gpio - uc_priv->gpio_base;
40 return ret ? ret : -EINVAL;
43 int gpio_lookup_name(const char *name, struct udevice **devp,
44 unsigned int *offsetp, unsigned int *gpiop)
46 struct gpio_dev_priv *uc_priv;
52 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
54 ret = uclass_next_device(&dev)) {
58 uc_priv = dev->uclass_priv;
59 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
61 if (!strncasecmp(name, uc_priv->bank_name, len)) {
62 if (strict_strtoul(name + len, 10, &offset))
69 *gpiop = uc_priv->gpio_base + offset;
74 return ret ? ret : -EINVAL;
78 * gpio_request() - [COMPAT] Request GPIO
80 * label: Name for the requested GPIO
82 * This function implements the API that's compatible with current
83 * GPIO API used in U-Boot. The request is forwarded to particular
84 * GPIO driver. Returns 0 on success, negative value on error.
86 int gpio_request(unsigned gpio, const char *label)
92 ret = gpio_to_device(gpio, &dev, &offset);
96 if (!gpio_get_ops(dev)->request)
99 return gpio_get_ops(dev)->request(dev, offset, label);
103 * gpio_free() - [COMPAT] Relinquish GPIO
106 * This function implements the API that's compatible with current
107 * GPIO API used in U-Boot. The request is forwarded to particular
108 * GPIO driver. Returns 0 on success, negative value on error.
110 int gpio_free(unsigned gpio)
116 ret = gpio_to_device(gpio, &dev, &offset);
120 if (!gpio_get_ops(dev)->free)
122 return gpio_get_ops(dev)->free(dev, offset);
126 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
129 * This function implements the API that's compatible with current
130 * GPIO API used in U-Boot. The request is forwarded to particular
131 * GPIO driver. Returns 0 on success, negative value on error.
133 int gpio_direction_input(unsigned gpio)
139 ret = gpio_to_device(gpio, &dev, &offset);
143 return gpio_get_ops(dev)->direction_input(dev, offset);
147 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
149 * value: Logical value to be set on the GPIO pin
151 * This function implements the API that's compatible with current
152 * GPIO API used in U-Boot. The request is forwarded to particular
153 * GPIO driver. Returns 0 on success, negative value on error.
155 int gpio_direction_output(unsigned gpio, int value)
161 ret = gpio_to_device(gpio, &dev, &offset);
165 return gpio_get_ops(dev)->direction_output(dev, offset, value);
169 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
172 * This function implements the API that's compatible with current
173 * GPIO API used in U-Boot. The request is forwarded to particular
174 * GPIO driver. Returns the value of the GPIO pin, or negative value
177 int gpio_get_value(unsigned gpio)
183 ret = gpio_to_device(gpio, &dev, &offset);
187 return gpio_get_ops(dev)->get_value(dev, offset);
191 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
193 * value: Logical value to be set on the GPIO pin.
195 * This function implements the API that's compatible with current
196 * GPIO API used in U-Boot. The request is forwarded to particular
197 * GPIO driver. Returns 0 on success, negative value on error.
199 int gpio_set_value(unsigned gpio, int value)
205 ret = gpio_to_device(gpio, &dev, &offset);
209 return gpio_get_ops(dev)->set_value(dev, offset, value);
212 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
214 struct gpio_dev_priv *priv;
216 /* Must be called on an active device */
217 priv = dev->uclass_priv;
220 *bit_count = priv->gpio_count;
221 return priv->bank_name;
224 /* We need to renumber the GPIOs when any driver is probed/removed */
225 static int gpio_renumber(void)
227 struct gpio_dev_priv *uc_priv;
233 ret = uclass_get(UCLASS_GPIO, &uc);
237 /* Ensure that we have a base for each bank */
239 uclass_foreach_dev(dev, uc) {
240 if (device_active(dev)) {
241 uc_priv = dev->uclass_priv;
242 uc_priv->gpio_base = base;
243 base += uc_priv->gpio_count;
250 static int gpio_post_probe(struct udevice *dev)
252 return gpio_renumber();
255 static int gpio_pre_remove(struct udevice *dev)
257 return gpio_renumber();
260 UCLASS_DRIVER(gpio) = {
263 .post_probe = gpio_post_probe,
264 .pre_remove = gpio_pre_remove,
265 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),