2 * Copyright (c) 2013 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <linux/ctype.h>
15 * gpio_to_device() - Convert global GPIO number to device, number
16 * gpio: The numeric representation of the GPIO
18 * Convert the GPIO number to an entry in the list of GPIOs
19 * or GPIO blocks registered with the GPIO controller. Returns
20 * entry on success, NULL on error.
22 static int gpio_to_device(unsigned int gpio, struct udevice **devp,
25 struct gpio_dev_priv *uc_priv;
29 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
31 ret = uclass_next_device(&dev)) {
32 uc_priv = dev->uclass_priv;
33 if (gpio >= uc_priv->gpio_base &&
34 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
36 *offset = gpio - uc_priv->gpio_base;
42 return ret ? ret : -EINVAL;
45 int gpio_lookup_name(const char *name, struct udevice **devp,
46 unsigned int *offsetp, unsigned int *gpiop)
48 struct gpio_dev_priv *uc_priv = NULL;
56 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
57 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
59 ret = uclass_next_device(&dev)) {
62 uc_priv = dev->uclass_priv;
64 offset = numeric - uc_priv->gpio_base;
65 /* Allow GPIOs to be numbered from 0 */
66 if (offset >= 0 && offset < uc_priv->gpio_count)
70 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
72 if (!strncasecmp(name, uc_priv->bank_name, len)) {
73 if (!strict_strtoul(name + len, 10, &offset))
79 return ret ? ret : -EINVAL;
86 *gpiop = uc_priv->gpio_base + offset;
92 * gpio_request() - [COMPAT] Request GPIO
94 * label: Name for the requested GPIO
96 * The label is copied and allocated so the caller does not need to keep
99 * This function implements the API that's compatible with current
100 * GPIO API used in U-Boot. The request is forwarded to particular
101 * GPIO driver. Returns 0 on success, negative value on error.
103 int gpio_request(unsigned gpio, const char *label)
105 struct gpio_dev_priv *uc_priv;
111 ret = gpio_to_device(gpio, &dev, &offset);
115 uc_priv = dev->uclass_priv;
116 if (uc_priv->name[offset])
121 if (gpio_get_ops(dev)->request) {
122 ret = gpio_get_ops(dev)->request(dev, offset, label);
128 uc_priv->name[offset] = str;
134 * gpio_free() - [COMPAT] Relinquish GPIO
137 * This function implements the API that's compatible with current
138 * GPIO API used in U-Boot. The request is forwarded to particular
139 * GPIO driver. Returns 0 on success, negative value on error.
141 int gpio_free(unsigned gpio)
143 struct gpio_dev_priv *uc_priv;
148 ret = gpio_to_device(gpio, &dev, &offset);
152 uc_priv = dev->uclass_priv;
153 if (!uc_priv->name[offset])
155 if (gpio_get_ops(dev)->free) {
156 ret = gpio_get_ops(dev)->free(dev, offset);
161 free(uc_priv->name[offset]);
162 uc_priv->name[offset] = NULL;
167 static int check_reserved(struct udevice *dev, unsigned offset,
170 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
172 if (!uc_priv->name[offset]) {
173 printf("%s: %s: error: gpio %s%d not reserved\n",
175 uc_priv->bank_name ? uc_priv->bank_name : "", offset);
183 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
186 * This function implements the API that's compatible with current
187 * GPIO API used in U-Boot. The request is forwarded to particular
188 * GPIO driver. Returns 0 on success, negative value on error.
190 int gpio_direction_input(unsigned gpio)
196 ret = gpio_to_device(gpio, &dev, &offset);
199 ret = check_reserved(dev, offset, "dir_input");
201 return ret ? ret : gpio_get_ops(dev)->direction_input(dev, offset);
205 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
207 * value: Logical value to be set on the GPIO pin
209 * This function implements the API that's compatible with current
210 * GPIO API used in U-Boot. The request is forwarded to particular
211 * GPIO driver. Returns 0 on success, negative value on error.
213 int gpio_direction_output(unsigned gpio, int value)
219 ret = gpio_to_device(gpio, &dev, &offset);
222 ret = check_reserved(dev, offset, "dir_output");
225 gpio_get_ops(dev)->direction_output(dev, offset, value);
229 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
232 * This function implements the API that's compatible with current
233 * GPIO API used in U-Boot. The request is forwarded to particular
234 * GPIO driver. Returns the value of the GPIO pin, or negative value
237 int gpio_get_value(unsigned gpio)
243 ret = gpio_to_device(gpio, &dev, &offset);
246 ret = check_reserved(dev, offset, "get_value");
248 return ret ? ret : gpio_get_ops(dev)->get_value(dev, offset);
252 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
254 * value: Logical value to be set on the GPIO pin.
256 * This function implements the API that's compatible with current
257 * GPIO API used in U-Boot. The request is forwarded to particular
258 * GPIO driver. Returns 0 on success, negative value on error.
260 int gpio_set_value(unsigned gpio, int value)
266 ret = gpio_to_device(gpio, &dev, &offset);
269 ret = check_reserved(dev, offset, "set_value");
271 return ret ? ret : gpio_get_ops(dev)->set_value(dev, offset, value);
274 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
276 struct gpio_dev_priv *priv;
278 /* Must be called on an active device */
279 priv = dev->uclass_priv;
282 *bit_count = priv->gpio_count;
283 return priv->bank_name;
286 static const char * const gpio_function[GPIOF_COUNT] = {
294 int get_function(struct udevice *dev, int offset, bool skip_unused,
297 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
298 struct dm_gpio_ops *ops = gpio_get_ops(dev);
300 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
301 if (!device_active(dev))
303 if (offset < 0 || offset >= uc_priv->gpio_count)
306 *namep = uc_priv->name[offset];
307 if (skip_unused && !uc_priv->name[offset])
309 if (ops->get_function) {
312 ret = ops->get_function(dev, offset);
315 if (ret >= ARRAY_SIZE(gpio_function))
320 return GPIOF_UNKNOWN;
323 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
325 return get_function(dev, offset, true, namep);
328 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
330 return get_function(dev, offset, false, namep);
333 /* We need to renumber the GPIOs when any driver is probed/removed */
334 static int gpio_renumber(struct udevice *removed_dev)
336 struct gpio_dev_priv *uc_priv;
342 ret = uclass_get(UCLASS_GPIO, &uc);
346 /* Ensure that we have a base for each bank */
348 uclass_foreach_dev(dev, uc) {
349 if (device_active(dev) && dev != removed_dev) {
350 uc_priv = dev->uclass_priv;
351 uc_priv->gpio_base = base;
352 base += uc_priv->gpio_count;
359 static int gpio_post_probe(struct udevice *dev)
361 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
363 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
367 return gpio_renumber(NULL);
370 static int gpio_pre_remove(struct udevice *dev)
372 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
375 for (i = 0; i < uc_priv->gpio_count; i++) {
376 if (uc_priv->name[i])
377 free(uc_priv->name[i]);
381 return gpio_renumber(dev);
384 UCLASS_DRIVER(gpio) = {
387 .post_probe = gpio_post_probe,
388 .pre_remove = gpio_pre_remove,
389 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),