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_requestf() - [COMPAT] Request GPIO
136 * @fmt: Format string for the requested GPIO
137 * @...: Arguments for the printf() format string
139 * This function implements the API that's compatible with current
140 * GPIO API used in U-Boot. The request is forwarded to particular
141 * GPIO driver. Returns 0 on success, negative value on error.
143 int gpio_requestf(unsigned gpio, const char *fmt, ...)
149 vscnprintf(buf, sizeof(buf), fmt, args);
151 return gpio_request(gpio, buf);
154 int gpio_request_one(unsigned int gpio, enum gpio_flags flags,
159 ret = gpio_request(gpio, label);
163 if (flags == GPIOFLAG_INPUT)
164 gpio_direction_input(gpio);
165 else if (flags == GPIOFLAG_OUTPUT_INIT_LOW)
166 gpio_direction_output(gpio, 0);
167 else if (flags == GPIOFLAG_OUTPUT_INIT_HIGH)
168 gpio_direction_output(gpio, 1);
173 int gpio_request_array(const struct gpio *gpios, int count)
178 for (i = 0; i < count; i++) {
179 ret = gpio_request_one(gpios[i].gpio, gpios[i].flags,
182 printf("Failed to request GPIO%d (%u of %u): %d\n",
183 gpios[i].gpio, i, count, ret);
191 gpio_free(gpios[i].gpio);
197 * gpio_free() - [COMPAT] Relinquish GPIO
200 * This function implements the API that's compatible with current
201 * GPIO API used in U-Boot. The request is forwarded to particular
202 * GPIO driver. Returns 0 on success, negative value on error.
204 int gpio_free(unsigned gpio)
206 struct gpio_dev_priv *uc_priv;
211 ret = gpio_to_device(gpio, &dev, &offset);
215 uc_priv = dev->uclass_priv;
216 if (!uc_priv->name[offset])
218 if (gpio_get_ops(dev)->free) {
219 ret = gpio_get_ops(dev)->free(dev, offset);
224 free(uc_priv->name[offset]);
225 uc_priv->name[offset] = NULL;
230 int gpio_free_array(const struct gpio *gpios, int count)
235 for (i = 0; i < count; i++)
236 ret |= gpio_free(gpios[i].gpio);
241 static int check_reserved(struct udevice *dev, unsigned offset,
244 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
246 if (!uc_priv->name[offset]) {
247 printf("%s: %s: error: gpio %s%d not reserved\n",
249 uc_priv->bank_name ? uc_priv->bank_name : "", offset);
257 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
260 * This function implements the API that's compatible with current
261 * GPIO API used in U-Boot. The request is forwarded to particular
262 * GPIO driver. Returns 0 on success, negative value on error.
264 int gpio_direction_input(unsigned gpio)
270 ret = gpio_to_device(gpio, &dev, &offset);
273 ret = check_reserved(dev, offset, "dir_input");
275 return ret ? ret : gpio_get_ops(dev)->direction_input(dev, offset);
279 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
281 * value: Logical value to be set on the GPIO pin
283 * This function implements the API that's compatible with current
284 * GPIO API used in U-Boot. The request is forwarded to particular
285 * GPIO driver. Returns 0 on success, negative value on error.
287 int gpio_direction_output(unsigned gpio, int value)
293 ret = gpio_to_device(gpio, &dev, &offset);
296 ret = check_reserved(dev, offset, "dir_output");
299 gpio_get_ops(dev)->direction_output(dev, offset, value);
303 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
306 * This function implements the API that's compatible with current
307 * GPIO API used in U-Boot. The request is forwarded to particular
308 * GPIO driver. Returns the value of the GPIO pin, or negative value
311 int gpio_get_value(unsigned gpio)
317 ret = gpio_to_device(gpio, &dev, &offset);
320 ret = check_reserved(dev, offset, "get_value");
322 return ret ? ret : gpio_get_ops(dev)->get_value(dev, offset);
326 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
328 * value: Logical value to be set on the GPIO pin.
330 * This function implements the API that's compatible with current
331 * GPIO API used in U-Boot. The request is forwarded to particular
332 * GPIO driver. Returns 0 on success, negative value on error.
334 int gpio_set_value(unsigned gpio, int value)
340 ret = gpio_to_device(gpio, &dev, &offset);
343 ret = check_reserved(dev, offset, "set_value");
345 return ret ? ret : gpio_get_ops(dev)->set_value(dev, offset, value);
348 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
350 struct gpio_dev_priv *priv;
352 /* Must be called on an active device */
353 priv = dev->uclass_priv;
356 *bit_count = priv->gpio_count;
357 return priv->bank_name;
360 static const char * const gpio_function[GPIOF_COUNT] = {
368 int get_function(struct udevice *dev, int offset, bool skip_unused,
371 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
372 struct dm_gpio_ops *ops = gpio_get_ops(dev);
374 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
375 if (!device_active(dev))
377 if (offset < 0 || offset >= uc_priv->gpio_count)
380 *namep = uc_priv->name[offset];
381 if (skip_unused && !uc_priv->name[offset])
383 if (ops->get_function) {
386 ret = ops->get_function(dev, offset);
389 if (ret >= ARRAY_SIZE(gpio_function))
394 return GPIOF_UNKNOWN;
397 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
399 return get_function(dev, offset, true, namep);
402 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
404 return get_function(dev, offset, false, namep);
407 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
409 struct dm_gpio_ops *ops = gpio_get_ops(dev);
410 struct gpio_dev_priv *priv;
416 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
419 priv = dev->uclass_priv;
420 ret = gpio_get_raw_function(dev, offset, NULL);
424 len = snprintf(str, buffsize, "%s%d: %s",
425 priv->bank_name ? priv->bank_name : "",
426 offset, gpio_function[func]);
427 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
428 func == GPIOF_UNUSED) {
432 ret = ops->get_value(dev, offset);
435 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
436 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
447 * get a number comprised of multiple GPIO values. gpio_num_array points to
448 * the array of gpio pin numbers to scan, terminated by -1.
450 unsigned gpio_get_values_as_int(const int *gpio_num_array)
453 unsigned bitmask = 1;
457 ((gpio = *gpio_num_array++) != -1)) {
458 if (gpio_get_value(gpio))
465 /* We need to renumber the GPIOs when any driver is probed/removed */
466 static int gpio_renumber(struct udevice *removed_dev)
468 struct gpio_dev_priv *uc_priv;
474 ret = uclass_get(UCLASS_GPIO, &uc);
478 /* Ensure that we have a base for each bank */
480 uclass_foreach_dev(dev, uc) {
481 if (device_active(dev) && dev != removed_dev) {
482 uc_priv = dev->uclass_priv;
483 uc_priv->gpio_base = base;
484 base += uc_priv->gpio_count;
491 static int gpio_post_probe(struct udevice *dev)
493 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
495 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
499 return gpio_renumber(NULL);
502 static int gpio_pre_remove(struct udevice *dev)
504 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
507 for (i = 0; i < uc_priv->gpio_count; i++) {
508 if (uc_priv->name[i])
509 free(uc_priv->name[i]);
513 return gpio_renumber(dev);
516 UCLASS_DRIVER(gpio) = {
519 .post_probe = gpio_post_probe,
520 .pre_remove = gpio_pre_remove,
521 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),