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);
155 * gpio_free() - [COMPAT] Relinquish GPIO
158 * This function implements the API that's compatible with current
159 * GPIO API used in U-Boot. The request is forwarded to particular
160 * GPIO driver. Returns 0 on success, negative value on error.
162 int gpio_free(unsigned gpio)
164 struct gpio_dev_priv *uc_priv;
169 ret = gpio_to_device(gpio, &dev, &offset);
173 uc_priv = dev->uclass_priv;
174 if (!uc_priv->name[offset])
176 if (gpio_get_ops(dev)->free) {
177 ret = gpio_get_ops(dev)->free(dev, offset);
182 free(uc_priv->name[offset]);
183 uc_priv->name[offset] = NULL;
188 static int check_reserved(struct udevice *dev, unsigned offset,
191 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
193 if (!uc_priv->name[offset]) {
194 printf("%s: %s: error: gpio %s%d not reserved\n",
196 uc_priv->bank_name ? uc_priv->bank_name : "", offset);
204 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
207 * This function implements the API that's compatible with current
208 * GPIO API used in U-Boot. The request is forwarded to particular
209 * GPIO driver. Returns 0 on success, negative value on error.
211 int gpio_direction_input(unsigned gpio)
217 ret = gpio_to_device(gpio, &dev, &offset);
220 ret = check_reserved(dev, offset, "dir_input");
222 return ret ? ret : gpio_get_ops(dev)->direction_input(dev, offset);
226 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
228 * value: Logical value to be set on the GPIO pin
230 * This function implements the API that's compatible with current
231 * GPIO API used in U-Boot. The request is forwarded to particular
232 * GPIO driver. Returns 0 on success, negative value on error.
234 int gpio_direction_output(unsigned gpio, int value)
240 ret = gpio_to_device(gpio, &dev, &offset);
243 ret = check_reserved(dev, offset, "dir_output");
246 gpio_get_ops(dev)->direction_output(dev, offset, value);
250 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
253 * This function implements the API that's compatible with current
254 * GPIO API used in U-Boot. The request is forwarded to particular
255 * GPIO driver. Returns the value of the GPIO pin, or negative value
258 int gpio_get_value(unsigned gpio)
264 ret = gpio_to_device(gpio, &dev, &offset);
267 ret = check_reserved(dev, offset, "get_value");
269 return ret ? ret : gpio_get_ops(dev)->get_value(dev, offset);
273 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
275 * value: Logical value to be set on the GPIO pin.
277 * This function implements the API that's compatible with current
278 * GPIO API used in U-Boot. The request is forwarded to particular
279 * GPIO driver. Returns 0 on success, negative value on error.
281 int gpio_set_value(unsigned gpio, int value)
287 ret = gpio_to_device(gpio, &dev, &offset);
290 ret = check_reserved(dev, offset, "set_value");
292 return ret ? ret : gpio_get_ops(dev)->set_value(dev, offset, value);
295 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
297 struct gpio_dev_priv *priv;
299 /* Must be called on an active device */
300 priv = dev->uclass_priv;
303 *bit_count = priv->gpio_count;
304 return priv->bank_name;
307 static const char * const gpio_function[GPIOF_COUNT] = {
315 int get_function(struct udevice *dev, int offset, bool skip_unused,
318 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
319 struct dm_gpio_ops *ops = gpio_get_ops(dev);
321 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
322 if (!device_active(dev))
324 if (offset < 0 || offset >= uc_priv->gpio_count)
327 *namep = uc_priv->name[offset];
328 if (skip_unused && !uc_priv->name[offset])
330 if (ops->get_function) {
333 ret = ops->get_function(dev, offset);
336 if (ret >= ARRAY_SIZE(gpio_function))
341 return GPIOF_UNKNOWN;
344 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
346 return get_function(dev, offset, true, namep);
349 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
351 return get_function(dev, offset, false, namep);
354 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
356 struct dm_gpio_ops *ops = gpio_get_ops(dev);
357 struct gpio_dev_priv *priv;
363 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
366 priv = dev->uclass_priv;
367 ret = gpio_get_raw_function(dev, offset, NULL);
371 len = snprintf(str, buffsize, "%s%d: %s",
372 priv->bank_name ? priv->bank_name : "",
373 offset, gpio_function[func]);
374 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
375 func == GPIOF_UNUSED) {
379 ret = ops->get_value(dev, offset);
382 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
383 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
393 /* We need to renumber the GPIOs when any driver is probed/removed */
394 static int gpio_renumber(struct udevice *removed_dev)
396 struct gpio_dev_priv *uc_priv;
402 ret = uclass_get(UCLASS_GPIO, &uc);
406 /* Ensure that we have a base for each bank */
408 uclass_foreach_dev(dev, uc) {
409 if (device_active(dev) && dev != removed_dev) {
410 uc_priv = dev->uclass_priv;
411 uc_priv->gpio_base = base;
412 base += uc_priv->gpio_count;
419 static int gpio_post_probe(struct udevice *dev)
421 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
423 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
427 return gpio_renumber(NULL);
430 static int gpio_pre_remove(struct udevice *dev)
432 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
435 for (i = 0; i < uc_priv->gpio_count; i++) {
436 if (uc_priv->name[i])
437 free(uc_priv->name[i]);
441 return gpio_renumber(dev);
444 UCLASS_DRIVER(gpio) = {
447 .post_probe = gpio_post_probe,
448 .pre_remove = gpio_pre_remove,
449 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),