2 * Copyright (c) 2013 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
13 #include <linux/ctype.h>
16 * gpio_to_device() - Convert global GPIO number to device, number
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 * @gpio: The numeric representation of the GPIO
23 * @desc: Returns description (desc->flags will always be 0)
24 * @return 0 if found, -ENOENT if not found
26 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
28 struct gpio_dev_priv *uc_priv;
32 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
34 ret = uclass_next_device(&dev)) {
35 uc_priv = dev->uclass_priv;
36 if (gpio >= uc_priv->gpio_base &&
37 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
39 desc->offset = gpio - uc_priv->gpio_base;
46 return ret ? ret : -ENOENT;
49 int gpio_lookup_name(const char *name, struct udevice **devp,
50 unsigned int *offsetp, unsigned int *gpiop)
52 struct gpio_dev_priv *uc_priv = NULL;
60 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
61 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
63 ret = uclass_next_device(&dev)) {
66 uc_priv = dev->uclass_priv;
68 offset = numeric - uc_priv->gpio_base;
69 /* Allow GPIOs to be numbered from 0 */
70 if (offset >= 0 && offset < uc_priv->gpio_count)
74 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
76 if (!strncasecmp(name, uc_priv->bank_name, len)) {
77 if (!strict_strtoul(name + len, 10, &offset))
83 return ret ? ret : -EINVAL;
90 *gpiop = uc_priv->gpio_base + offset;
95 int gpio_find_and_xlate(struct gpio_desc *desc,
96 struct fdtdec_phandle_args *args)
98 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
100 /* Use the first argument as the offset by default */
101 if (args->args_count > 0)
102 desc->offset = args->args[0];
107 return ops->xlate ? ops->xlate(desc->dev, desc, args) : 0;
110 static int dm_gpio_request(struct gpio_desc *desc, const char *label)
112 struct udevice *dev = desc->dev;
113 struct gpio_dev_priv *uc_priv;
117 uc_priv = dev->uclass_priv;
118 if (uc_priv->name[desc->offset])
123 if (gpio_get_ops(dev)->request) {
124 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
130 uc_priv->name[desc->offset] = str;
136 * gpio_request() - [COMPAT] Request GPIO
138 * label: Name for the requested GPIO
140 * The label is copied and allocated so the caller does not need to keep
141 * the pointer around.
143 * This function implements the API that's compatible with current
144 * GPIO API used in U-Boot. The request is forwarded to particular
145 * GPIO driver. Returns 0 on success, negative value on error.
147 int gpio_request(unsigned gpio, const char *label)
149 struct gpio_desc desc;
152 ret = gpio_to_device(gpio, &desc);
156 return dm_gpio_request(&desc, label);
160 * gpio_requestf() - [COMPAT] Request GPIO
162 * @fmt: Format string for the requested GPIO
163 * @...: Arguments for the printf() format string
165 * This function implements the API that's compatible with current
166 * GPIO API used in U-Boot. The request is forwarded to particular
167 * GPIO driver. Returns 0 on success, negative value on error.
169 int gpio_requestf(unsigned gpio, const char *fmt, ...)
175 vscnprintf(buf, sizeof(buf), fmt, args);
177 return gpio_request(gpio, buf);
180 int _dm_gpio_free(struct udevice *dev, uint offset)
182 struct gpio_dev_priv *uc_priv;
185 uc_priv = dev->uclass_priv;
186 if (!uc_priv->name[offset])
188 if (gpio_get_ops(dev)->free) {
189 ret = gpio_get_ops(dev)->free(dev, offset);
194 free(uc_priv->name[offset]);
195 uc_priv->name[offset] = NULL;
201 * gpio_free() - [COMPAT] Relinquish GPIO
204 * This function implements the API that's compatible with current
205 * GPIO API used in U-Boot. The request is forwarded to particular
206 * GPIO driver. Returns 0 on success, negative value on error.
208 int gpio_free(unsigned gpio)
210 struct gpio_desc desc;
213 ret = gpio_to_device(gpio, &desc);
217 return _dm_gpio_free(desc.dev, desc.offset);
220 static int check_reserved(struct gpio_desc *desc, const char *func)
222 struct gpio_dev_priv *uc_priv = desc->dev->uclass_priv;
224 if (!uc_priv->name[desc->offset]) {
225 printf("%s: %s: error: gpio %s%d not reserved\n",
226 desc->dev->name, func,
227 uc_priv->bank_name ? uc_priv->bank_name : "",
236 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
239 * This function implements the API that's compatible with current
240 * GPIO API used in U-Boot. The request is forwarded to particular
241 * GPIO driver. Returns 0 on success, negative value on error.
243 int gpio_direction_input(unsigned gpio)
245 struct gpio_desc desc;
248 ret = gpio_to_device(gpio, &desc);
251 ret = check_reserved(&desc, "dir_input");
255 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
259 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
261 * value: Logical value to be set on the GPIO pin
263 * This function implements the API that's compatible with current
264 * GPIO API used in U-Boot. The request is forwarded to particular
265 * GPIO driver. Returns 0 on success, negative value on error.
267 int gpio_direction_output(unsigned gpio, int value)
269 struct gpio_desc desc;
272 ret = gpio_to_device(gpio, &desc);
275 ret = check_reserved(&desc, "dir_output");
279 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
283 int dm_gpio_get_value(struct gpio_desc *desc)
288 ret = check_reserved(desc, "get_value");
292 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
294 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
297 int dm_gpio_set_value(struct gpio_desc *desc, int value)
301 ret = check_reserved(desc, "set_value");
305 if (desc->flags & GPIOD_ACTIVE_LOW)
307 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
311 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
313 struct udevice *dev = desc->dev;
314 struct dm_gpio_ops *ops = gpio_get_ops(dev);
317 ret = check_reserved(desc, "set_dir");
321 if (flags & GPIOD_IS_OUT) {
322 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
324 if (flags & GPIOD_ACTIVE_LOW)
326 ret = ops->direction_output(dev, desc->offset, value);
327 } else if (flags & GPIOD_IS_IN) {
328 ret = ops->direction_input(dev, desc->offset);
333 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
341 int dm_gpio_set_dir(struct gpio_desc *desc)
343 return dm_gpio_set_dir_flags(desc, desc->flags);
347 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
350 * This function implements the API that's compatible with current
351 * GPIO API used in U-Boot. The request is forwarded to particular
352 * GPIO driver. Returns the value of the GPIO pin, or negative value
355 int gpio_get_value(unsigned gpio)
359 struct gpio_desc desc;
361 ret = gpio_to_device(gpio, &desc);
364 return dm_gpio_get_value(&desc);
368 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
370 * value: Logical value to be set on the GPIO pin.
372 * This function implements the API that's compatible with current
373 * GPIO API used in U-Boot. The request is forwarded to particular
374 * GPIO driver. Returns 0 on success, negative value on error.
376 int gpio_set_value(unsigned gpio, int value)
378 struct gpio_desc desc;
381 ret = gpio_to_device(gpio, &desc);
384 return dm_gpio_set_value(&desc, value);
387 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
389 struct gpio_dev_priv *priv;
391 /* Must be called on an active device */
392 priv = dev->uclass_priv;
395 *bit_count = priv->gpio_count;
396 return priv->bank_name;
399 static const char * const gpio_function[GPIOF_COUNT] = {
407 int get_function(struct udevice *dev, int offset, bool skip_unused,
410 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
411 struct dm_gpio_ops *ops = gpio_get_ops(dev);
413 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
414 if (!device_active(dev))
416 if (offset < 0 || offset >= uc_priv->gpio_count)
419 *namep = uc_priv->name[offset];
420 if (skip_unused && !uc_priv->name[offset])
422 if (ops->get_function) {
425 ret = ops->get_function(dev, offset);
428 if (ret >= ARRAY_SIZE(gpio_function))
433 return GPIOF_UNKNOWN;
436 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
438 return get_function(dev, offset, true, namep);
441 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
443 return get_function(dev, offset, false, namep);
446 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
448 struct dm_gpio_ops *ops = gpio_get_ops(dev);
449 struct gpio_dev_priv *priv;
455 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
458 priv = dev->uclass_priv;
459 ret = gpio_get_raw_function(dev, offset, NULL);
463 len = snprintf(str, buffsize, "%s%d: %s",
464 priv->bank_name ? priv->bank_name : "",
465 offset, gpio_function[func]);
466 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
467 func == GPIOF_UNUSED) {
471 ret = ops->get_value(dev, offset);
474 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
475 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
486 * get a number comprised of multiple GPIO values. gpio_num_array points to
487 * the array of gpio pin numbers to scan, terminated by -1.
489 unsigned gpio_get_values_as_int(const int *gpio_num_array)
492 unsigned bitmask = 1;
496 ((gpio = *gpio_num_array++) != -1)) {
497 if (gpio_get_value(gpio))
504 /* We need to renumber the GPIOs when any driver is probed/removed */
505 static int gpio_renumber(struct udevice *removed_dev)
507 struct gpio_dev_priv *uc_priv;
513 ret = uclass_get(UCLASS_GPIO, &uc);
517 /* Ensure that we have a base for each bank */
519 uclass_foreach_dev(dev, uc) {
520 if (device_active(dev) && dev != removed_dev) {
521 uc_priv = dev->uclass_priv;
522 uc_priv->gpio_base = base;
523 base += uc_priv->gpio_count;
530 static int gpio_post_probe(struct udevice *dev)
532 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
534 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
538 return gpio_renumber(NULL);
541 static int gpio_pre_remove(struct udevice *dev)
543 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
546 for (i = 0; i < uc_priv->gpio_count; i++) {
547 if (uc_priv->name[i])
548 free(uc_priv->name[i]);
552 return gpio_renumber(dev);
555 UCLASS_DRIVER(gpio) = {
558 .post_probe = gpio_post_probe,
559 .pre_remove = gpio_pre_remove,
560 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),