gpio/pinctrl: sunxi: stop poking around in private vars
[linux-2.6-block.git] / drivers / gpio / gpiolib.c
CommitLineData
d2876d08
DB
1#include <linux/kernel.h>
2#include <linux/module.h>
ff77c352 3#include <linux/interrupt.h>
d2876d08
DB
4#include <linux/irq.h>
5#include <linux/spinlock.h>
1a989d0f 6#include <linux/list.h>
d8f388d8
DB
7#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
391c970c 12#include <linux/of_gpio.h>
ff77c352 13#include <linux/idr.h>
5a0e3ad6 14#include <linux/slab.h>
7b199811 15#include <linux/acpi.h>
53e7cac3 16#include <linux/gpio/driver.h>
0a6d3158 17#include <linux/gpio/machine.h>
c771c2f4 18#include <linux/pinctrl/consumer.h>
ff2b1359 19#include <linux/idr.h>
3c702e99
LW
20#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
23#include <uapi/linux/gpio.h>
d2876d08 24
664e3e5a
MW
25#include "gpiolib.h"
26
3f397c21
UKK
27#define CREATE_TRACE_POINTS
28#include <trace/events/gpio.h>
d2876d08 29
79a9becd 30/* Implementation infrastructure for GPIO interfaces.
d2876d08 31 *
79a9becd
AC
32 * The GPIO programming interface allows for inlining speed-critical
33 * get/set operations for common cases, so that access to SOC-integrated
34 * GPIOs can sometimes cost only an instruction or two per bit.
d2876d08
DB
35 */
36
37
38/* When debugging, extend minimal trust to callers and platform code.
39 * Also emit diagnostic messages that may help initial bringup, when
40 * board setup or driver bugs are most common.
41 *
42 * Otherwise, minimize overhead in what may be bitbanging codepaths.
43 */
44#ifdef DEBUG
45#define extra_checks 1
46#else
47#define extra_checks 0
48#endif
49
ff2b1359
LW
50/* Device and char device-related information */
51static DEFINE_IDA(gpio_ida);
3c702e99
LW
52static dev_t gpio_devt;
53#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
54static struct bus_type gpio_bus_type = {
55 .name = "gpio",
56};
ff2b1359 57
d2876d08
DB
58/* gpio_lock prevents conflicts during gpio_desc[] table updates.
59 * While any GPIO is requested, its gpio_chip is not removable;
60 * each GPIO's "requested" flag serves as a lock and refcount.
61 */
0eb4c6c2 62DEFINE_SPINLOCK(gpio_lock);
d2876d08 63
bae48da2
AC
64static DEFINE_MUTEX(gpio_lookup_lock);
65static LIST_HEAD(gpio_lookup_list);
ff2b1359 66LIST_HEAD(gpio_devices);
6d86750c
JH
67
68static void gpiochip_free_hogs(struct gpio_chip *chip);
69static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
70
71
d2876d08
DB
72static inline void desc_set_label(struct gpio_desc *d, const char *label)
73{
d2876d08 74 d->label = label;
d2876d08
DB
75}
76
372e722e
AC
77/**
78 * Convert a GPIO number to its descriptor
79 */
79a9becd 80struct gpio_desc *gpio_to_desc(unsigned gpio)
372e722e 81{
ff2b1359 82 struct gpio_device *gdev;
14e85c0e
AC
83 unsigned long flags;
84
85 spin_lock_irqsave(&gpio_lock, flags);
86
ff2b1359
LW
87 list_for_each_entry(gdev, &gpio_devices, list) {
88 if (gdev->chip->base <= gpio &&
89 gdev->chip->base + gdev->chip->ngpio > gpio) {
14e85c0e 90 spin_unlock_irqrestore(&gpio_lock, flags);
1c3cdb18 91 return &gdev->descs[gpio - gdev->chip->base];
14e85c0e
AC
92 }
93 }
94
95 spin_unlock_irqrestore(&gpio_lock, flags);
96
0e9a5edf
AC
97 if (!gpio_is_valid(gpio))
98 WARN(1, "invalid GPIO %d\n", gpio);
99
14e85c0e 100 return NULL;
372e722e 101}
79a9becd 102EXPORT_SYMBOL_GPL(gpio_to_desc);
372e722e 103
d468bf9e 104/**
bb1e88cc 105 * Get the GPIO descriptor corresponding to the given hw number for this chip.
d468bf9e 106 */
bb1e88cc
AC
107struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
108 u16 hwnum)
d468bf9e 109{
bb1e88cc 110 if (hwnum >= chip->ngpio)
b7d0a28a 111 return ERR_PTR(-EINVAL);
d468bf9e 112
1c3cdb18 113 return &chip->gpiodev->descs[hwnum];
d468bf9e 114}
372e722e
AC
115
116/**
117 * Convert a GPIO descriptor to the integer namespace.
118 * This should disappear in the future but is needed since we still
119 * use GPIO numbers for error messages and sysfs nodes
120 */
79a9becd 121int desc_to_gpio(const struct gpio_desc *desc)
372e722e 122{
1c3cdb18 123 return desc->chip->base + (desc - &desc->chip->gpiodev->descs[0]);
372e722e 124}
79a9becd 125EXPORT_SYMBOL_GPL(desc_to_gpio);
372e722e
AC
126
127
79a9becd
AC
128/**
129 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
130 * @desc: descriptor to return the chip of
131 */
132struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
372e722e 133{
bcabdef1 134 return desc ? desc->chip : NULL;
372e722e 135}
79a9becd 136EXPORT_SYMBOL_GPL(gpiod_to_chip);
d2876d08 137
8d0aab2f
AV
138/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
139static int gpiochip_find_base(int ngpio)
140{
ff2b1359 141 struct gpio_device *gdev;
83cabe33 142 int base = ARCH_NR_GPIOS - ngpio;
8d0aab2f 143
ff2b1359 144 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
83cabe33 145 /* found a free space? */
ff2b1359 146 if (gdev->chip->base + gdev->chip->ngpio <= base)
83cabe33
AC
147 break;
148 else
149 /* nope, check the space right before the chip */
ff2b1359 150 base = gdev->chip->base - ngpio;
8d0aab2f
AV
151 }
152
83cabe33 153 if (gpio_is_valid(base)) {
8d0aab2f 154 pr_debug("%s: found new base at %d\n", __func__, base);
83cabe33
AC
155 return base;
156 } else {
157 pr_err("%s: cannot find free range\n", __func__);
158 return -ENOSPC;
169b6a7a 159 }
169b6a7a
AV
160}
161
79a9becd
AC
162/**
163 * gpiod_get_direction - return the current direction of a GPIO
164 * @desc: GPIO to get the direction of
165 *
166 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
167 *
168 * This function may sleep if gpiod_cansleep() is true.
169 */
8e53b0f1 170int gpiod_get_direction(struct gpio_desc *desc)
80b0a602
MN
171{
172 struct gpio_chip *chip;
372e722e 173 unsigned offset;
80b0a602
MN
174 int status = -EINVAL;
175
372e722e
AC
176 chip = gpiod_to_chip(desc);
177 offset = gpio_chip_hwgpio(desc);
80b0a602
MN
178
179 if (!chip->get_direction)
180 return status;
181
372e722e 182 status = chip->get_direction(chip, offset);
80b0a602
MN
183 if (status > 0) {
184 /* GPIOF_DIR_IN, or other positive */
185 status = 1;
8e53b0f1 186 clear_bit(FLAG_IS_OUT, &desc->flags);
80b0a602
MN
187 }
188 if (status == 0) {
189 /* GPIOF_DIR_OUT */
8e53b0f1 190 set_bit(FLAG_IS_OUT, &desc->flags);
80b0a602
MN
191 }
192 return status;
193}
79a9becd 194EXPORT_SYMBOL_GPL(gpiod_get_direction);
80b0a602 195
1a989d0f
AC
196/*
197 * Add a new chip to the global chips list, keeping the list of chips sorted
ef7c7553 198 * by range(means [base, base + ngpio - 1]) order.
1a989d0f
AC
199 *
200 * Return -EBUSY if the new chip overlaps with some other chip's integer
201 * space.
202 */
ff2b1359 203static int gpiodev_add_to_list(struct gpio_device *gdev)
1a989d0f 204{
ff2b1359
LW
205 struct gpio_device *iterator;
206 struct gpio_device *previous = NULL;
207
208 if (!gdev->chip)
209 return -EINVAL;
1a989d0f 210
ff2b1359
LW
211 if (list_empty(&gpio_devices)) {
212 list_add_tail(&gdev->list, &gpio_devices);
e28ecca6 213 return 0;
1a989d0f
AC
214 }
215
ff2b1359
LW
216 list_for_each_entry(iterator, &gpio_devices, list) {
217 /*
218 * The list may contain dangling GPIO devices with no
219 * live chip assigned.
220 */
221 if (!iterator->chip)
222 continue;
223 if (iterator->chip->base >=
224 gdev->chip->base + gdev->chip->ngpio) {
ef7c7553
BJZ
225 /*
226 * Iterator is the first GPIO chip so there is no
227 * previous one
228 */
e28ecca6 229 if (!previous) {
ef7c7553
BJZ
230 goto found;
231 } else {
232 /*
233 * We found a valid range(means
234 * [base, base + ngpio - 1]) between previous
235 * and iterator chip.
236 */
ff2b1359
LW
237 if (previous->chip->base + previous->chip->ngpio
238 <= gdev->chip->base)
ef7c7553
BJZ
239 goto found;
240 }
1a989d0f 241 }
ef7c7553 242 previous = iterator;
1a989d0f
AC
243 }
244
e28ecca6
SM
245 /*
246 * We are beyond the last chip in the list and iterator now
247 * points to the head.
248 * Let iterator point to the last chip in the list.
249 */
250
ff2b1359
LW
251 iterator = list_last_entry(&gpio_devices, struct gpio_device, list);
252 if (iterator->chip->base + iterator->chip->ngpio <= gdev->chip->base) {
253 list_add(&gdev->list, &iterator->list);
96098df1 254 return 0;
1a989d0f
AC
255 }
256
ff2b1359 257 dev_err(&gdev->dev,
ef7c7553
BJZ
258 "GPIO integer space overlap, cannot add chip\n");
259 return -EBUSY;
1a989d0f 260
ef7c7553 261found:
ff2b1359 262 list_add_tail(&gdev->list, &iterator->list);
ef7c7553 263 return 0;
1a989d0f
AC
264}
265
f881bab0
LW
266/**
267 * Convert a GPIO name to its descriptor
268 */
269static struct gpio_desc *gpio_name_to_desc(const char * const name)
270{
ff2b1359 271 struct gpio_device *gdev;
f881bab0
LW
272 unsigned long flags;
273
274 spin_lock_irqsave(&gpio_lock, flags);
275
ff2b1359 276 list_for_each_entry(gdev, &gpio_devices, list) {
f881bab0
LW
277 int i;
278
ff2b1359 279 for (i = 0; i != gdev->chip->ngpio; ++i) {
1c3cdb18 280 struct gpio_desc *gpio = &gdev->descs[i];
f881bab0 281
d06165b3 282 if (!gpio->name || !name)
f881bab0
LW
283 continue;
284
285 if (!strcmp(gpio->name, name)) {
286 spin_unlock_irqrestore(&gpio_lock, flags);
287 return gpio;
288 }
289 }
290 }
291
292 spin_unlock_irqrestore(&gpio_lock, flags);
293
294 return NULL;
295}
296
5f3ca732
MP
297/*
298 * Takes the names from gc->names and checks if they are all unique. If they
299 * are, they are assigned to their gpio descriptors.
300 *
ed37915c 301 * Warning if one of the names is already used for a different GPIO.
5f3ca732
MP
302 */
303static int gpiochip_set_desc_names(struct gpio_chip *gc)
304{
305 int i;
306
307 if (!gc->names)
308 return 0;
309
310 /* First check all names if they are unique */
311 for (i = 0; i != gc->ngpio; ++i) {
312 struct gpio_desc *gpio;
313
314 gpio = gpio_name_to_desc(gc->names[i]);
f881bab0 315 if (gpio)
34ffd85d
LW
316 dev_warn(&gc->gpiodev->dev,
317 "Detected name collision for GPIO name '%s'\n",
f881bab0 318 gc->names[i]);
5f3ca732
MP
319 }
320
321 /* Then add all names to the GPIO descriptors */
322 for (i = 0; i != gc->ngpio; ++i)
1c3cdb18 323 gc->gpiodev->descs[i].name = gc->names[i];
5f3ca732
MP
324
325 return 0;
326}
327
3c702e99
LW
328/**
329 * gpio_ioctl() - ioctl handler for the GPIO chardev
330 */
331static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
332{
333 struct gpio_device *gdev = filp->private_data;
334 struct gpio_chip *chip = gdev->chip;
335 int __user *ip = (int __user *)arg;
336 struct gpiochip_info chipinfo;
337
338 /* We fail any subsequent ioctl():s when the chip is gone */
339 if (!chip)
340 return -ENODEV;
341
342 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
343 /* Fill in the struct and pass to userspace */
344 strncpy(chipinfo.name, dev_name(&gdev->dev),
345 sizeof(chipinfo.name));
346 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
347 chipinfo.lines = chip->ngpio;
348 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
349 return -EFAULT;
350 return 0;
351 }
352 return -EINVAL;
353}
354
355/**
356 * gpio_chrdev_open() - open the chardev for ioctl operations
357 * @inode: inode for this chardev
358 * @filp: file struct for storing private data
359 * Returns 0 on success
360 */
361static int gpio_chrdev_open(struct inode *inode, struct file *filp)
362{
363 struct gpio_device *gdev = container_of(inode->i_cdev,
364 struct gpio_device, chrdev);
365
366 /* Fail on open if the backing gpiochip is gone */
367 if (!gdev || !gdev->chip)
368 return -ENODEV;
369 get_device(&gdev->dev);
370 filp->private_data = gdev;
371 return 0;
372}
373
374/**
375 * gpio_chrdev_release() - close chardev after ioctl operations
376 * @inode: inode for this chardev
377 * @filp: file struct for storing private data
378 * Returns 0 on success
379 */
380static int gpio_chrdev_release(struct inode *inode, struct file *filp)
381{
382 struct gpio_device *gdev = container_of(inode->i_cdev,
383 struct gpio_device, chrdev);
384
385 if (!gdev)
386 return -ENODEV;
387 put_device(&gdev->dev);
388 return 0;
389}
390
391
392static const struct file_operations gpio_fileops = {
393 .release = gpio_chrdev_release,
394 .open = gpio_chrdev_open,
395 .owner = THIS_MODULE,
396 .llseek = noop_llseek,
397 .unlocked_ioctl = gpio_ioctl,
398 .compat_ioctl = gpio_ioctl,
399};
400
ff2b1359
LW
401static void gpiodevice_release(struct device *dev)
402{
403 struct gpio_device *gdev = dev_get_drvdata(dev);
404
3c702e99 405 cdev_del(&gdev->chrdev);
ff2b1359
LW
406 list_del(&gdev->list);
407 ida_simple_remove(&gpio_ida, gdev->id);
9efd9e69 408 kfree(gdev);
ff2b1359
LW
409}
410
d2876d08 411/**
b08ea35a 412 * gpiochip_add_data() - register a gpio_chip
d2876d08 413 * @chip: the chip to register, with chip->base initialized
14e85c0e 414 * Context: potentially before irqs will work
d2876d08
DB
415 *
416 * Returns a negative errno if the chip can't be registered, such as
417 * because the chip->base is invalid or already associated with a
418 * different chip. Otherwise it returns zero as a success code.
8d0aab2f 419 *
b08ea35a 420 * When gpiochip_add_data() is called very early during boot, so that GPIOs
c88402c2 421 * can be freely used, the chip->parent device must be registered before
d8f388d8
DB
422 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
423 * for GPIOs will fail rudely.
424 *
8d0aab2f
AV
425 * If chip->base is negative, this requests dynamic assignment of
426 * a range of valid GPIOs.
d2876d08 427 */
b08ea35a 428int gpiochip_add_data(struct gpio_chip *chip, void *data)
d2876d08
DB
429{
430 unsigned long flags;
431 int status = 0;
ff2b1359 432 unsigned i;
8d0aab2f 433 int base = chip->base;
ff2b1359 434 struct gpio_device *gdev;
d2876d08 435
ff2b1359
LW
436 /*
437 * First: allocate and populate the internal stat container, and
438 * set up the struct device.
439 */
440 gdev = kmalloc(sizeof(*gdev), GFP_KERNEL);
441 if (!gdev)
14e85c0e 442 return -ENOMEM;
3c702e99 443 gdev->dev.bus = &gpio_bus_type;
ff2b1359
LW
444 gdev->chip = chip;
445 chip->gpiodev = gdev;
446 if (chip->parent) {
447 gdev->dev.parent = chip->parent;
448 gdev->dev.of_node = chip->parent->of_node;
449 } else {
450#ifdef CONFIG_OF_GPIO
451 /* If the gpiochip has an assigned OF node this takes precedence */
452 if (chip->of_node)
453 gdev->dev.of_node = chip->of_node;
454#endif
455 }
456 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
457 if (gdev->id < 0) {
458 status = gdev->id;
459 goto err_free_gdev;
460 }
461 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
462 device_initialize(&gdev->dev);
463 dev_set_drvdata(&gdev->dev, gdev);
464 if (chip->parent && chip->parent->driver)
465 gdev->owner = chip->parent->driver->owner;
466 else if (chip->owner)
467 /* TODO: remove chip->owner */
468 gdev->owner = chip->owner;
469 else
470 gdev->owner = THIS_MODULE;
d2876d08 471
1c3cdb18
LW
472 gdev->descs = devm_kcalloc(&gdev->dev, chip->ngpio,
473 sizeof(gdev->descs[0]), GFP_KERNEL);
474 if (!gdev->descs) {
ff2b1359
LW
475 status = -ENOMEM;
476 goto err_free_gdev;
477 }
478
479 /* FIXME: move driver data into gpio_device dev_set_drvdata() */
b08ea35a
LW
480 chip->data = data;
481
5ed41cc4
BJZ
482 if (chip->ngpio == 0) {
483 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
ff2b1359 484 status = -EINVAL;
1c3cdb18 485 goto err_free_gdev;
5ed41cc4
BJZ
486 }
487
d2876d08
DB
488 spin_lock_irqsave(&gpio_lock, flags);
489
8d0aab2f
AV
490 if (base < 0) {
491 base = gpiochip_find_base(chip->ngpio);
492 if (base < 0) {
493 status = base;
225fce83 494 spin_unlock_irqrestore(&gpio_lock, flags);
1c3cdb18 495 goto err_free_gdev;
8d0aab2f
AV
496 }
497 chip->base = base;
498 }
499
ff2b1359 500 status = gpiodev_add_to_list(gdev);
05aa5203
JH
501 if (status) {
502 spin_unlock_irqrestore(&gpio_lock, flags);
1c3cdb18 503 goto err_free_gdev;
05aa5203 504 }
1a989d0f 505
ff2b1359 506 for (i = 0; i < chip->ngpio; i++) {
1c3cdb18 507 struct gpio_desc *desc = &gdev->descs[i];
05aa5203 508
ff2b1359 509 /* REVISIT: maybe a pointer to gpio_device is better */
05aa5203
JH
510 desc->chip = chip;
511
512 /* REVISIT: most hardware initializes GPIOs as inputs (often
513 * with pullups enabled) so power usage is minimized. Linux
514 * code should set the gpio direction first thing; but until
515 * it does, and in case chip->get_direction is not set, we may
516 * expose the wrong direction in sysfs.
517 */
518 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
d2876d08 519 }
14e85c0e 520
3bae4811
ZG
521 spin_unlock_irqrestore(&gpio_lock, flags);
522
f23f1516 523#ifdef CONFIG_PINCTRL
ff2b1359 524 /* FIXME: move pin ranges to gpio_device */
f23f1516
SH
525 INIT_LIST_HEAD(&chip->pin_ranges);
526#endif
527
5f3ca732
MP
528 status = gpiochip_set_desc_names(chip);
529 if (status)
530 goto err_remove_from_list;
531
28355f81
TV
532 status = of_gpiochip_add(chip);
533 if (status)
534 goto err_remove_chip;
535
664e3e5a 536 acpi_gpiochip_add(chip);
391c970c 537
3c702e99
LW
538 /*
539 * By first adding the chardev, and then adding the device,
540 * we get a device node entry in sysfs under
541 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
542 * coldplug of device nodes and other udev business.
543 */
544 cdev_init(&gdev->chrdev, &gpio_fileops);
545 gdev->chrdev.owner = THIS_MODULE;
546 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
547 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
548 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
549 if (status < 0)
550 chip_warn(chip, "failed to add char device %d:%d\n",
551 MAJOR(gpio_devt), gdev->id);
552 else
553 chip_dbg(chip, "added GPIO chardev (%d:%d)\n",
554 MAJOR(gpio_devt), gdev->id);
ff2b1359 555 status = device_add(&gdev->dev);
225fce83 556 if (status)
3c702e99 557 goto err_remove_chardev;
cedb1881 558
afbc4f31 559 status = gpiochip_sysfs_register(gdev);
ff2b1359
LW
560 if (status)
561 goto err_remove_device;
562
563 /* From this point, the .release() function cleans up gpio_device */
564 gdev->dev.release = gpiodevice_release;
565 get_device(&gdev->dev);
7589e59f 566 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
64842aad
GL
567 chip->base, chip->base + chip->ngpio - 1,
568 chip->label ? : "generic");
569
cedb1881 570 return 0;
3bae4811 571
ff2b1359
LW
572err_remove_device:
573 device_del(&gdev->dev);
3c702e99
LW
574err_remove_chardev:
575 cdev_del(&gdev->chrdev);
225fce83
JH
576err_remove_chip:
577 acpi_gpiochip_remove(chip);
6d86750c 578 gpiochip_free_hogs(chip);
225fce83 579 of_gpiochip_remove(chip);
5f3ca732 580err_remove_from_list:
225fce83 581 spin_lock_irqsave(&gpio_lock, flags);
ff2b1359 582 list_del(&gdev->list);
3bae4811 583 spin_unlock_irqrestore(&gpio_lock, flags);
ff2b1359
LW
584err_free_gdev:
585 ida_simple_remove(&gpio_ida, gdev->id);
586 kfree(gdev);
d2876d08 587 /* failures here can mean systems won't boot... */
7589e59f 588 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
cedb1881
AV
589 chip->base, chip->base + chip->ngpio - 1,
590 chip->label ? : "generic");
d2876d08
DB
591 return status;
592}
b08ea35a 593EXPORT_SYMBOL_GPL(gpiochip_add_data);
d2876d08
DB
594
595/**
596 * gpiochip_remove() - unregister a gpio_chip
597 * @chip: the chip to unregister
598 *
599 * A gpio_chip with any GPIOs still requested may not be removed.
600 */
e1db1706 601void gpiochip_remove(struct gpio_chip *chip)
d2876d08 602{
ff2b1359 603 struct gpio_device *gdev = chip->gpiodev;
fab28b89 604 struct gpio_desc *desc;
d2876d08 605 unsigned long flags;
1c3cdb18 606 unsigned i;
fab28b89 607 bool requested = false;
d2876d08 608
ff2b1359
LW
609 /* Numb the device, cancelling all outstanding operations */
610 gdev->chip = NULL;
01cca93a 611
ff2b1359 612 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
afbc4f31 613 gpiochip_sysfs_unregister(gdev);
00acc3dc 614 gpiochip_irqchip_remove(chip);
6072b9dc 615 acpi_gpiochip_remove(chip);
9ef0d6f7 616 gpiochip_remove_pin_ranges(chip);
f625d460 617 gpiochip_free_hogs(chip);
391c970c
AV
618 of_gpiochip_remove(chip);
619
6798acaa 620 spin_lock_irqsave(&gpio_lock, flags);
1c3cdb18
LW
621 for (i = 0; i < chip->ngpio; i++) {
622 desc = &gdev->descs[i];
fab28b89
JH
623 desc->chip = NULL;
624 if (test_bit(FLAG_REQUESTED, &desc->flags))
625 requested = true;
d2876d08 626 }
d2876d08 627 spin_unlock_irqrestore(&gpio_lock, flags);
14e85c0e 628
fab28b89 629 if (requested)
34ffd85d 630 dev_crit(&chip->gpiodev->dev,
58383c78 631 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
fab28b89 632
ff2b1359
LW
633 /*
634 * The gpiochip side puts its use of the device to rest here:
635 * if there are no userspace clients, the chardev and device will
636 * be removed, else it will be dangling until the last user is
637 * gone.
638 */
639 put_device(&gdev->dev);
d2876d08
DB
640}
641EXPORT_SYMBOL_GPL(gpiochip_remove);
642
594fa265
GL
643/**
644 * gpiochip_find() - iterator for locating a specific gpio_chip
645 * @data: data to pass to match function
646 * @callback: Callback function to check gpio_chip
647 *
648 * Similar to bus_find_device. It returns a reference to a gpio_chip as
649 * determined by a user supplied @match callback. The callback should return
650 * 0 if the device doesn't match and non-zero if it does. If the callback is
651 * non-zero, this function will return to the caller and not iterate over any
652 * more gpio_chips.
653 */
07ce8ec7 654struct gpio_chip *gpiochip_find(void *data,
6e2cf651 655 int (*match)(struct gpio_chip *chip,
3d0f7cf0 656 void *data))
594fa265 657{
ff2b1359 658 struct gpio_device *gdev;
125eef96 659 struct gpio_chip *chip;
594fa265 660 unsigned long flags;
594fa265
GL
661
662 spin_lock_irqsave(&gpio_lock, flags);
ff2b1359
LW
663 list_for_each_entry(gdev, &gpio_devices, list)
664 if (match(gdev->chip, data))
594fa265 665 break;
125eef96
AC
666
667 /* No match? */
ff2b1359 668 if (&gdev->list == &gpio_devices)
125eef96 669 chip = NULL;
ff2b1359
LW
670 else
671 chip = gdev->chip;
672
594fa265
GL
673 spin_unlock_irqrestore(&gpio_lock, flags);
674
675 return chip;
676}
8fa0c9bf 677EXPORT_SYMBOL_GPL(gpiochip_find);
d2876d08 678
79697ef9
AC
679static int gpiochip_match_name(struct gpio_chip *chip, void *data)
680{
681 const char *name = data;
682
683 return !strcmp(chip->label, name);
684}
685
686static struct gpio_chip *find_chip_by_name(const char *name)
687{
688 return gpiochip_find((void *)name, gpiochip_match_name);
689}
690
14250520
LW
691#ifdef CONFIG_GPIOLIB_IRQCHIP
692
693/*
694 * The following is irqchip helper code for gpiochips.
695 */
696
697/**
3f97d5fc
LW
698 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
699 * @gpiochip: the gpiochip to set the irqchip chain to
700 * @irqchip: the irqchip to chain to the gpiochip
14250520
LW
701 * @parent_irq: the irq number corresponding to the parent IRQ for this
702 * chained irqchip
703 * @parent_handler: the parent interrupt handler for the accumulated IRQ
3f97d5fc
LW
704 * coming out of the gpiochip. If the interrupt is nested rather than
705 * cascaded, pass NULL in this handler argument
14250520
LW
706 */
707void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
708 struct irq_chip *irqchip,
709 int parent_irq,
710 irq_flow_handler_t parent_handler)
711{
83141a77
LW
712 unsigned int offset;
713
83141a77
LW
714 if (!gpiochip->irqdomain) {
715 chip_err(gpiochip, "called %s before setting up irqchip\n",
716 __func__);
1c8732bb
LW
717 return;
718 }
719
3f97d5fc
LW
720 if (parent_handler) {
721 if (gpiochip->can_sleep) {
722 chip_err(gpiochip,
723 "you cannot have chained interrupts on a "
724 "chip that may sleep\n");
725 return;
726 }
3f97d5fc
LW
727 /*
728 * The parent irqchip is already using the chip_data for this
729 * irqchip, so our callbacks simply use the handler_data.
730 */
f7f87753
TG
731 irq_set_chained_handler_and_data(parent_irq, parent_handler,
732 gpiochip);
25e4fe92
DES
733
734 gpiochip->irq_parent = parent_irq;
3f97d5fc 735 }
83141a77
LW
736
737 /* Set the parent IRQ for all affected IRQs */
738 for (offset = 0; offset < gpiochip->ngpio; offset++)
739 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
740 parent_irq);
14250520
LW
741}
742EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
743
744/**
745 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
746 * @d: the irqdomain used by this irqchip
747 * @irq: the global irq number used by this GPIO irqchip irq
748 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
749 *
750 * This function will set up the mapping for a certain IRQ line on a
751 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
752 * stored inside the gpiochip.
753 */
754static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
755 irq_hw_number_t hwirq)
756{
757 struct gpio_chip *chip = d->host_data;
758
14250520 759 irq_set_chip_data(irq, chip);
a0a8bcf4
GS
760 /*
761 * This lock class tells lockdep that GPIO irqs are in a different
762 * category than their parents, so it won't report false recursion.
763 */
764 irq_set_lockdep_class(irq, chip->lock_key);
7633fb95 765 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
1c8732bb 766 /* Chips that can sleep need nested thread handlers */
295494af 767 if (chip->can_sleep && !chip->irq_not_threaded)
1c8732bb 768 irq_set_nested_thread(irq, 1);
14250520 769 irq_set_noprobe(irq);
23393d49 770
1333b90f
LW
771 /*
772 * No set-up of the hardware will happen if IRQ_TYPE_NONE
773 * is passed as default type.
774 */
775 if (chip->irq_default_type != IRQ_TYPE_NONE)
776 irq_set_irq_type(irq, chip->irq_default_type);
14250520
LW
777
778 return 0;
779}
780
c3626fde
LW
781static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
782{
1c8732bb
LW
783 struct gpio_chip *chip = d->host_data;
784
1c8732bb
LW
785 if (chip->can_sleep)
786 irq_set_nested_thread(irq, 0);
c3626fde
LW
787 irq_set_chip_and_handler(irq, NULL, NULL);
788 irq_set_chip_data(irq, NULL);
789}
790
14250520
LW
791static const struct irq_domain_ops gpiochip_domain_ops = {
792 .map = gpiochip_irq_map,
c3626fde 793 .unmap = gpiochip_irq_unmap,
14250520
LW
794 /* Virtually all GPIO irqchips are twocell:ed */
795 .xlate = irq_domain_xlate_twocell,
796};
797
798static int gpiochip_irq_reqres(struct irq_data *d)
799{
800 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
801
ff2b1359 802 if (!try_module_get(chip->gpiodev->owner))
5b76e79c
GS
803 return -ENODEV;
804
e3a2e878 805 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
14250520
LW
806 chip_err(chip,
807 "unable to lock HW IRQ %lu for IRQ\n",
808 d->hwirq);
ff2b1359 809 module_put(chip->gpiodev->owner);
14250520
LW
810 return -EINVAL;
811 }
812 return 0;
813}
814
815static void gpiochip_irq_relres(struct irq_data *d)
816{
817 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
818
e3a2e878 819 gpiochip_unlock_as_irq(chip, d->hwirq);
ff2b1359 820 module_put(chip->gpiodev->owner);
14250520
LW
821}
822
823static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
824{
825 return irq_find_mapping(chip->irqdomain, offset);
826}
827
828/**
829 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
830 * @gpiochip: the gpiochip to remove the irqchip from
831 *
832 * This is called only from gpiochip_remove()
833 */
834static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
835{
c3626fde
LW
836 unsigned int offset;
837
afa82fab
MW
838 acpi_gpiochip_free_interrupts(gpiochip);
839
25e4fe92
DES
840 if (gpiochip->irq_parent) {
841 irq_set_chained_handler(gpiochip->irq_parent, NULL);
842 irq_set_handler_data(gpiochip->irq_parent, NULL);
843 }
844
c3626fde
LW
845 /* Remove all IRQ mappings and delete the domain */
846 if (gpiochip->irqdomain) {
847 for (offset = 0; offset < gpiochip->ngpio; offset++)
e3893386
GS
848 irq_dispose_mapping(
849 irq_find_mapping(gpiochip->irqdomain, offset));
14250520 850 irq_domain_remove(gpiochip->irqdomain);
c3626fde 851 }
14250520
LW
852
853 if (gpiochip->irqchip) {
854 gpiochip->irqchip->irq_request_resources = NULL;
855 gpiochip->irqchip->irq_release_resources = NULL;
856 gpiochip->irqchip = NULL;
857 }
858}
859
860/**
861 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
862 * @gpiochip: the gpiochip to add the irqchip to
863 * @irqchip: the irqchip to add to the gpiochip
864 * @first_irq: if not dynamically assigned, the base (first) IRQ to
865 * allocate gpiochip irqs from
866 * @handler: the irq handler to use (often a predefined irq core function)
1333b90f
LW
867 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
868 * to have the core avoid setting up any default type in the hardware.
a0a8bcf4 869 * @lock_key: lockdep class
14250520
LW
870 *
871 * This function closely associates a certain irqchip with a certain
872 * gpiochip, providing an irq domain to translate the local IRQs to
873 * global irqs in the gpiolib core, and making sure that the gpiochip
874 * is passed as chip data to all related functions. Driver callbacks
09dd5f9e 875 * need to use gpiochip_get_data() to get their local state containers back
14250520
LW
876 * from the gpiochip passed as chip data. An irqdomain will be stored
877 * in the gpiochip that shall be used by the driver to handle IRQ number
878 * translation. The gpiochip will need to be initialized and registered
879 * before calling this function.
880 *
c3626fde
LW
881 * This function will handle two cell:ed simple IRQs and assumes all
882 * the pins on the gpiochip can generate a unique IRQ. Everything else
14250520
LW
883 * need to be open coded.
884 */
a0a8bcf4
GS
885int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
886 struct irq_chip *irqchip,
887 unsigned int first_irq,
888 irq_flow_handler_t handler,
889 unsigned int type,
890 struct lock_class_key *lock_key)
14250520
LW
891{
892 struct device_node *of_node;
893 unsigned int offset;
c3626fde 894 unsigned irq_base = 0;
14250520
LW
895
896 if (!gpiochip || !irqchip)
897 return -EINVAL;
898
58383c78 899 if (!gpiochip->parent) {
14250520
LW
900 pr_err("missing gpiochip .dev parent pointer\n");
901 return -EINVAL;
902 }
58383c78 903 of_node = gpiochip->parent->of_node;
14250520
LW
904#ifdef CONFIG_OF_GPIO
905 /*
20a8a968 906 * If the gpiochip has an assigned OF node this takes precedence
c88402c2
BJZ
907 * FIXME: get rid of this and use gpiochip->parent->of_node
908 * everywhere
14250520
LW
909 */
910 if (gpiochip->of_node)
911 of_node = gpiochip->of_node;
912#endif
913 gpiochip->irqchip = irqchip;
914 gpiochip->irq_handler = handler;
915 gpiochip->irq_default_type = type;
916 gpiochip->to_irq = gpiochip_to_irq;
a0a8bcf4 917 gpiochip->lock_key = lock_key;
14250520
LW
918 gpiochip->irqdomain = irq_domain_add_simple(of_node,
919 gpiochip->ngpio, first_irq,
920 &gpiochip_domain_ops, gpiochip);
921 if (!gpiochip->irqdomain) {
922 gpiochip->irqchip = NULL;
923 return -EINVAL;
924 }
8b67a1f0
RV
925
926 /*
927 * It is possible for a driver to override this, but only if the
928 * alternative functions are both implemented.
929 */
930 if (!irqchip->irq_request_resources &&
931 !irqchip->irq_release_resources) {
932 irqchip->irq_request_resources = gpiochip_irq_reqres;
933 irqchip->irq_release_resources = gpiochip_irq_relres;
934 }
14250520
LW
935
936 /*
937 * Prepare the mapping since the irqchip shall be orthogonal to
938 * any gpiochip calls. If the first_irq was zero, this is
939 * necessary to allocate descriptors for all IRQs.
940 */
c3626fde
LW
941 for (offset = 0; offset < gpiochip->ngpio; offset++) {
942 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
943 if (offset == 0)
944 /*
945 * Store the base into the gpiochip to be used when
946 * unmapping the irqs.
947 */
948 gpiochip->irq_base = irq_base;
949 }
14250520 950
afa82fab
MW
951 acpi_gpiochip_request_interrupts(gpiochip);
952
14250520
LW
953 return 0;
954}
a0a8bcf4 955EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
14250520
LW
956
957#else /* CONFIG_GPIOLIB_IRQCHIP */
958
959static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
960
961#endif /* CONFIG_GPIOLIB_IRQCHIP */
962
c771c2f4
JG
963/**
964 * gpiochip_generic_request() - request the gpio function for a pin
965 * @chip: the gpiochip owning the GPIO
966 * @offset: the offset of the GPIO to request for GPIO function
967 */
968int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
969{
970 return pinctrl_request_gpio(chip->base + offset);
971}
972EXPORT_SYMBOL_GPL(gpiochip_generic_request);
973
974/**
975 * gpiochip_generic_free() - free the gpio function from a pin
976 * @chip: the gpiochip to request the gpio function for
977 * @offset: the offset of the GPIO to free from GPIO function
978 */
979void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
980{
981 pinctrl_free_gpio(chip->base + offset);
982}
983EXPORT_SYMBOL_GPL(gpiochip_generic_free);
984
f23f1516 985#ifdef CONFIG_PINCTRL
165adc9c 986
586a87e6
CR
987/**
988 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
989 * @chip: the gpiochip to add the range for
d32651f6 990 * @pctldev: the pin controller to map to
586a87e6
CR
991 * @gpio_offset: the start offset in the current gpio_chip number space
992 * @pin_group: name of the pin group inside the pin controller
993 */
994int gpiochip_add_pingroup_range(struct gpio_chip *chip,
995 struct pinctrl_dev *pctldev,
996 unsigned int gpio_offset, const char *pin_group)
997{
998 struct gpio_pin_range *pin_range;
999 int ret;
1000
1001 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1002 if (!pin_range) {
1a2a99c6 1003 chip_err(chip, "failed to allocate pin ranges\n");
586a87e6
CR
1004 return -ENOMEM;
1005 }
1006
1007 /* Use local offset as range ID */
1008 pin_range->range.id = gpio_offset;
1009 pin_range->range.gc = chip;
1010 pin_range->range.name = chip->label;
1011 pin_range->range.base = chip->base + gpio_offset;
1012 pin_range->pctldev = pctldev;
1013
1014 ret = pinctrl_get_group_pins(pctldev, pin_group,
1015 &pin_range->range.pins,
1016 &pin_range->range.npins);
61c6375d
MN
1017 if (ret < 0) {
1018 kfree(pin_range);
586a87e6 1019 return ret;
61c6375d 1020 }
586a87e6
CR
1021
1022 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1023
1a2a99c6
AS
1024 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1025 gpio_offset, gpio_offset + pin_range->range.npins - 1,
586a87e6
CR
1026 pinctrl_dev_get_devname(pctldev), pin_group);
1027
1028 list_add_tail(&pin_range->node, &chip->pin_ranges);
1029
1030 return 0;
1031}
1032EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1033
3f0f8670
LW
1034/**
1035 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1036 * @chip: the gpiochip to add the range for
1037 * @pinctrl_name: the dev_name() of the pin controller to map to
316511c0
LW
1038 * @gpio_offset: the start offset in the current gpio_chip number space
1039 * @pin_offset: the start offset in the pin controller number space
3f0f8670
LW
1040 * @npins: the number of pins from the offset of each pin space (GPIO and
1041 * pin controller) to accumulate in this range
1042 */
1e63d7b9 1043int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
316511c0 1044 unsigned int gpio_offset, unsigned int pin_offset,
3f0f8670 1045 unsigned int npins)
f23f1516
SH
1046{
1047 struct gpio_pin_range *pin_range;
b4d4b1f0 1048 int ret;
f23f1516 1049
3f0f8670 1050 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
f23f1516 1051 if (!pin_range) {
1a2a99c6 1052 chip_err(chip, "failed to allocate pin ranges\n");
1e63d7b9 1053 return -ENOMEM;
f23f1516
SH
1054 }
1055
3f0f8670 1056 /* Use local offset as range ID */
316511c0 1057 pin_range->range.id = gpio_offset;
3f0f8670 1058 pin_range->range.gc = chip;
f23f1516 1059 pin_range->range.name = chip->label;
316511c0
LW
1060 pin_range->range.base = chip->base + gpio_offset;
1061 pin_range->range.pin_base = pin_offset;
f23f1516 1062 pin_range->range.npins = npins;
192c369c 1063 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
f23f1516 1064 &pin_range->range);
8f23ca1a 1065 if (IS_ERR(pin_range->pctldev)) {
b4d4b1f0 1066 ret = PTR_ERR(pin_range->pctldev);
1a2a99c6 1067 chip_err(chip, "could not create pin range\n");
3f0f8670 1068 kfree(pin_range);
b4d4b1f0 1069 return ret;
3f0f8670 1070 }
1a2a99c6
AS
1071 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1072 gpio_offset, gpio_offset + npins - 1,
316511c0
LW
1073 pinctl_name,
1074 pin_offset, pin_offset + npins - 1);
f23f1516
SH
1075
1076 list_add_tail(&pin_range->node, &chip->pin_ranges);
1e63d7b9
LW
1077
1078 return 0;
f23f1516 1079}
165adc9c 1080EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
f23f1516 1081
3f0f8670
LW
1082/**
1083 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1084 * @chip: the chip to remove all the mappings for
1085 */
f23f1516
SH
1086void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1087{
1088 struct gpio_pin_range *pin_range, *tmp;
1089
1090 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1091 list_del(&pin_range->node);
1092 pinctrl_remove_gpio_range(pin_range->pctldev,
1093 &pin_range->range);
3f0f8670 1094 kfree(pin_range);
f23f1516
SH
1095 }
1096}
165adc9c
LW
1097EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1098
1099#endif /* CONFIG_PINCTRL */
f23f1516 1100
d2876d08
DB
1101/* These "optional" allocation calls help prevent drivers from stomping
1102 * on each other, and help provide better diagnostics in debugfs.
1103 * They're called even less than the "set direction" calls.
1104 */
77c2d792 1105static int __gpiod_request(struct gpio_desc *desc, const char *label)
d2876d08 1106{
77c2d792
MW
1107 struct gpio_chip *chip = desc->chip;
1108 int status;
d2876d08
DB
1109 unsigned long flags;
1110
bcabdef1
AC
1111 spin_lock_irqsave(&gpio_lock, flags);
1112
d2876d08 1113 /* NOTE: gpio_request() can be called in early boot,
35e8bb51 1114 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
d2876d08
DB
1115 */
1116
1117 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1118 desc_set_label(desc, label ? : "?");
1119 status = 0;
438d8908 1120 } else {
d2876d08 1121 status = -EBUSY;
7460db56 1122 goto done;
35e8bb51
DB
1123 }
1124
1125 if (chip->request) {
1126 /* chip->request may sleep */
1127 spin_unlock_irqrestore(&gpio_lock, flags);
372e722e 1128 status = chip->request(chip, gpio_chip_hwgpio(desc));
35e8bb51
DB
1129 spin_lock_irqsave(&gpio_lock, flags);
1130
1131 if (status < 0) {
1132 desc_set_label(desc, NULL);
35e8bb51 1133 clear_bit(FLAG_REQUESTED, &desc->flags);
80b0a602 1134 goto done;
35e8bb51 1135 }
438d8908 1136 }
80b0a602
MN
1137 if (chip->get_direction) {
1138 /* chip->get_direction may sleep */
1139 spin_unlock_irqrestore(&gpio_lock, flags);
372e722e 1140 gpiod_get_direction(desc);
80b0a602
MN
1141 spin_lock_irqsave(&gpio_lock, flags);
1142 }
77c2d792 1143done:
923b93e4
LP
1144 if (status < 0) {
1145 /* Clear flags that might have been set by the caller before
1146 * requesting the GPIO.
1147 */
1148 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1149 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1150 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1151 }
77c2d792
MW
1152 spin_unlock_irqrestore(&gpio_lock, flags);
1153 return status;
1154}
1155
0eb4c6c2 1156int gpiod_request(struct gpio_desc *desc, const char *label)
77c2d792
MW
1157{
1158 int status = -EPROBE_DEFER;
1159 struct gpio_chip *chip;
1160
1161 if (!desc) {
1162 pr_warn("%s: invalid GPIO\n", __func__);
1163 return -EINVAL;
1164 }
1165
1166 chip = desc->chip;
1167 if (!chip)
1168 goto done;
1169
ff2b1359 1170 if (try_module_get(chip->gpiodev->owner)) {
77c2d792
MW
1171 status = __gpiod_request(desc, label);
1172 if (status < 0)
ff2b1359 1173 module_put(chip->gpiodev->owner);
77c2d792
MW
1174 }
1175
d2876d08
DB
1176done:
1177 if (status)
7589e59f 1178 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
77c2d792 1179
d2876d08
DB
1180 return status;
1181}
372e722e 1182
77c2d792 1183static bool __gpiod_free(struct gpio_desc *desc)
d2876d08 1184{
77c2d792 1185 bool ret = false;
d2876d08 1186 unsigned long flags;
35e8bb51 1187 struct gpio_chip *chip;
d2876d08 1188
3d599d1c
UKK
1189 might_sleep();
1190
372e722e 1191 gpiod_unexport(desc);
d8f388d8 1192
d2876d08
DB
1193 spin_lock_irqsave(&gpio_lock, flags);
1194
35e8bb51
DB
1195 chip = desc->chip;
1196 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1197 if (chip->free) {
1198 spin_unlock_irqrestore(&gpio_lock, flags);
9c4ba946 1199 might_sleep_if(chip->can_sleep);
372e722e 1200 chip->free(chip, gpio_chip_hwgpio(desc));
35e8bb51
DB
1201 spin_lock_irqsave(&gpio_lock, flags);
1202 }
d2876d08 1203 desc_set_label(desc, NULL);
07697461 1204 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
35e8bb51 1205 clear_bit(FLAG_REQUESTED, &desc->flags);
aca5ce14 1206 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
25553ff0 1207 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
f625d460 1208 clear_bit(FLAG_IS_HOGGED, &desc->flags);
77c2d792
MW
1209 ret = true;
1210 }
d2876d08
DB
1211
1212 spin_unlock_irqrestore(&gpio_lock, flags);
77c2d792
MW
1213 return ret;
1214}
1215
0eb4c6c2 1216void gpiod_free(struct gpio_desc *desc)
77c2d792
MW
1217{
1218 if (desc && __gpiod_free(desc))
ff2b1359 1219 module_put(desc->chip->gpiodev->owner);
77c2d792
MW
1220 else
1221 WARN_ON(extra_checks);
d2876d08 1222}
372e722e 1223
d2876d08
DB
1224/**
1225 * gpiochip_is_requested - return string iff signal was requested
1226 * @chip: controller managing the signal
1227 * @offset: of signal within controller's 0..(ngpio - 1) range
1228 *
1229 * Returns NULL if the GPIO is not currently requested, else a string.
9c8318ff
AC
1230 * The string returned is the label passed to gpio_request(); if none has been
1231 * passed it is a meaningless, non-NULL constant.
d2876d08
DB
1232 *
1233 * This function is for use by GPIO controller drivers. The label can
1234 * help with diagnostics, and knowing that the signal is used as a GPIO
1235 * can help avoid accidentally multiplexing it to another controller.
1236 */
1237const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1238{
6c0b4e6c 1239 struct gpio_desc *desc;
d2876d08 1240
48b5953e 1241 if (offset >= chip->ngpio)
d2876d08 1242 return NULL;
6c0b4e6c 1243
1c3cdb18 1244 desc = &chip->gpiodev->descs[offset];
6c0b4e6c 1245
372e722e 1246 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
d2876d08 1247 return NULL;
372e722e 1248 return desc->label;
d2876d08
DB
1249}
1250EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1251
77c2d792
MW
1252/**
1253 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1254 * @desc: GPIO descriptor to request
1255 * @label: label for the GPIO
1256 *
1257 * Function allows GPIO chip drivers to request and use their own GPIO
1258 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1259 * function will not increase reference count of the GPIO chip module. This
1260 * allows the GPIO chip module to be unloaded as needed (we assume that the
1261 * GPIO chip driver handles freeing the GPIOs it has requested).
1262 */
abdc08a3
AC
1263struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1264 const char *label)
77c2d792 1265{
abdc08a3
AC
1266 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1267 int err;
77c2d792 1268
abdc08a3
AC
1269 if (IS_ERR(desc)) {
1270 chip_err(chip, "failed to get GPIO descriptor\n");
1271 return desc;
1272 }
1273
1274 err = __gpiod_request(desc, label);
1275 if (err < 0)
1276 return ERR_PTR(err);
77c2d792 1277
abdc08a3 1278 return desc;
77c2d792 1279}
f7d4ad98 1280EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
77c2d792
MW
1281
1282/**
1283 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1284 * @desc: GPIO descriptor to free
1285 *
1286 * Function frees the given GPIO requested previously with
1287 * gpiochip_request_own_desc().
1288 */
1289void gpiochip_free_own_desc(struct gpio_desc *desc)
1290{
1291 if (desc)
1292 __gpiod_free(desc);
1293}
f7d4ad98 1294EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
d2876d08
DB
1295
1296/* Drivers MUST set GPIO direction before making get/set calls. In
1297 * some cases this is done in early boot, before IRQs are enabled.
1298 *
1299 * As a rule these aren't called more than once (except for drivers
1300 * using the open-drain emulation idiom) so these are natural places
1301 * to accumulate extra debugging checks. Note that we can't (yet)
1302 * rely on gpio_request() having been called beforehand.
1303 */
1304
79a9becd
AC
1305/**
1306 * gpiod_direction_input - set the GPIO direction to input
1307 * @desc: GPIO to set to input
1308 *
1309 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1310 * be called safely on it.
1311 *
1312 * Return 0 in case of success, else an error code.
1313 */
1314int gpiod_direction_input(struct gpio_desc *desc)
d2876d08 1315{
d2876d08 1316 struct gpio_chip *chip;
d2876d08
DB
1317 int status = -EINVAL;
1318
be1a4b13 1319 if (!desc || !desc->chip) {
bcabdef1
AC
1320 pr_warn("%s: invalid GPIO\n", __func__);
1321 return -EINVAL;
1322 }
1323
be1a4b13
LW
1324 chip = desc->chip;
1325 if (!chip->get || !chip->direction_input) {
6424de5a
MB
1326 gpiod_warn(desc,
1327 "%s: missing get() or direction_input() operations\n",
7589e59f 1328 __func__);
be1a4b13
LW
1329 return -EIO;
1330 }
1331
d82da797 1332 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
d2876d08
DB
1333 if (status == 0)
1334 clear_bit(FLAG_IS_OUT, &desc->flags);
3f397c21 1335
372e722e 1336 trace_gpio_direction(desc_to_gpio(desc), 1, status);
d82da797 1337
d2876d08
DB
1338 return status;
1339}
79a9becd 1340EXPORT_SYMBOL_GPL(gpiod_direction_input);
372e722e 1341
ef70bbe1 1342static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
d2876d08 1343{
d2876d08 1344 struct gpio_chip *chip;
d2876d08
DB
1345 int status = -EINVAL;
1346
d468bf9e
LW
1347 /* GPIOs used for IRQs shall not be set as output */
1348 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1349 gpiod_err(desc,
1350 "%s: tried to set a GPIO tied to an IRQ as output\n",
1351 __func__);
1352 return -EIO;
1353 }
1354
aca5ce14
LD
1355 /* Open drain pin should not be driven to 1 */
1356 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
372e722e 1357 return gpiod_direction_input(desc);
aca5ce14 1358
25553ff0
LD
1359 /* Open source pin should not be driven to 0 */
1360 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
372e722e 1361 return gpiod_direction_input(desc);
25553ff0 1362
be1a4b13
LW
1363 chip = desc->chip;
1364 if (!chip->set || !chip->direction_output) {
6424de5a
MB
1365 gpiod_warn(desc,
1366 "%s: missing set() or direction_output() operations\n",
1367 __func__);
be1a4b13
LW
1368 return -EIO;
1369 }
1370
d82da797 1371 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
d2876d08
DB
1372 if (status == 0)
1373 set_bit(FLAG_IS_OUT, &desc->flags);
372e722e
AC
1374 trace_gpio_value(desc_to_gpio(desc), 0, value);
1375 trace_gpio_direction(desc_to_gpio(desc), 0, status);
d2876d08
DB
1376 return status;
1377}
ef70bbe1
PZ
1378
1379/**
1380 * gpiod_direction_output_raw - set the GPIO direction to output
1381 * @desc: GPIO to set to output
1382 * @value: initial output value of the GPIO
1383 *
1384 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1385 * be called safely on it. The initial value of the output must be specified
1386 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1387 *
1388 * Return 0 in case of success, else an error code.
1389 */
1390int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1391{
1392 if (!desc || !desc->chip) {
1393 pr_warn("%s: invalid GPIO\n", __func__);
1394 return -EINVAL;
1395 }
1396 return _gpiod_direction_output_raw(desc, value);
1397}
1398EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1399
1400/**
90df4fe0 1401 * gpiod_direction_output - set the GPIO direction to output
ef70bbe1
PZ
1402 * @desc: GPIO to set to output
1403 * @value: initial output value of the GPIO
1404 *
1405 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1406 * be called safely on it. The initial value of the output must be specified
1407 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1408 * account.
1409 *
1410 * Return 0 in case of success, else an error code.
1411 */
1412int gpiod_direction_output(struct gpio_desc *desc, int value)
1413{
1414 if (!desc || !desc->chip) {
1415 pr_warn("%s: invalid GPIO\n", __func__);
1416 return -EINVAL;
1417 }
1418 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1419 value = !value;
1420 return _gpiod_direction_output_raw(desc, value);
1421}
79a9becd 1422EXPORT_SYMBOL_GPL(gpiod_direction_output);
d2876d08 1423
c4b5be98 1424/**
79a9becd 1425 * gpiod_set_debounce - sets @debounce time for a @gpio
c4b5be98
FB
1426 * @gpio: the gpio to set debounce time
1427 * @debounce: debounce time is microseconds
65d87656
LW
1428 *
1429 * returns -ENOTSUPP if the controller does not support setting
1430 * debounce.
c4b5be98 1431 */
79a9becd 1432int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
c4b5be98 1433{
c4b5be98 1434 struct gpio_chip *chip;
c4b5be98 1435
be1a4b13 1436 if (!desc || !desc->chip) {
bcabdef1
AC
1437 pr_warn("%s: invalid GPIO\n", __func__);
1438 return -EINVAL;
1439 }
1440
c4b5be98 1441 chip = desc->chip;
be1a4b13 1442 if (!chip->set || !chip->set_debounce) {
6424de5a
MB
1443 gpiod_dbg(desc,
1444 "%s: missing set() or set_debounce() operations\n",
1445 __func__);
65d87656 1446 return -ENOTSUPP;
be1a4b13
LW
1447 }
1448
d82da797 1449 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
c4b5be98 1450}
79a9becd 1451EXPORT_SYMBOL_GPL(gpiod_set_debounce);
372e722e 1452
79a9becd
AC
1453/**
1454 * gpiod_is_active_low - test whether a GPIO is active-low or not
1455 * @desc: the gpio descriptor to test
1456 *
1457 * Returns 1 if the GPIO is active-low, 0 otherwise.
1458 */
1459int gpiod_is_active_low(const struct gpio_desc *desc)
372e722e 1460{
79a9becd 1461 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
372e722e 1462}
79a9becd 1463EXPORT_SYMBOL_GPL(gpiod_is_active_low);
d2876d08
DB
1464
1465/* I/O calls are only valid after configuration completed; the relevant
1466 * "is this a valid GPIO" error checks should already have been done.
1467 *
1468 * "Get" operations are often inlinable as reading a pin value register,
1469 * and masking the relevant bit in that register.
1470 *
1471 * When "set" operations are inlinable, they involve writing that mask to
1472 * one register to set a low value, or a different register to set it high.
1473 * Otherwise locking is needed, so there may be little value to inlining.
1474 *
1475 *------------------------------------------------------------------------
1476 *
1477 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1478 * have requested the GPIO. That can include implicit requesting by
1479 * a direction setting call. Marking a gpio as requested locks its chip
1480 * in memory, guaranteeing that these table lookups need no more locking
1481 * and that gpiochip_remove() will fail.
1482 *
1483 * REVISIT when debugging, consider adding some instrumentation to ensure
1484 * that the GPIO was actually requested.
1485 */
1486
e20538b8 1487static int _gpiod_get_raw_value(const struct gpio_desc *desc)
d2876d08
DB
1488{
1489 struct gpio_chip *chip;
372e722e 1490 int offset;
e20538b8 1491 int value;
d2876d08 1492
372e722e
AC
1493 chip = desc->chip;
1494 offset = gpio_chip_hwgpio(desc);
e20538b8 1495 value = chip->get ? chip->get(chip, offset) : -EIO;
723a6303 1496 value = value < 0 ? value : !!value;
372e722e 1497 trace_gpio_value(desc_to_gpio(desc), 1, value);
3f397c21 1498 return value;
d2876d08 1499}
372e722e 1500
d2876d08 1501/**
79a9becd
AC
1502 * gpiod_get_raw_value() - return a gpio's raw value
1503 * @desc: gpio whose value will be returned
d2876d08 1504 *
79a9becd 1505 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
e20538b8 1506 * its ACTIVE_LOW status, or negative errno on failure.
79a9becd
AC
1507 *
1508 * This function should be called from contexts where we cannot sleep, and will
1509 * complain if the GPIO chip functions potentially sleep.
d2876d08 1510 */
79a9becd 1511int gpiod_get_raw_value(const struct gpio_desc *desc)
d2876d08 1512{
bcabdef1
AC
1513 if (!desc)
1514 return 0;
e4e449e8 1515 /* Should be using gpio_get_value_cansleep() */
d8e0ac08 1516 WARN_ON(desc->chip->can_sleep);
79a9becd 1517 return _gpiod_get_raw_value(desc);
d2876d08 1518}
79a9becd 1519EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
372e722e 1520
79a9becd
AC
1521/**
1522 * gpiod_get_value() - return a gpio's value
1523 * @desc: gpio whose value will be returned
1524 *
1525 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
e20538b8 1526 * account, or negative errno on failure.
79a9becd
AC
1527 *
1528 * This function should be called from contexts where we cannot sleep, and will
1529 * complain if the GPIO chip functions potentially sleep.
1530 */
1531int gpiod_get_value(const struct gpio_desc *desc)
372e722e 1532{
79a9becd
AC
1533 int value;
1534 if (!desc)
1535 return 0;
1536 /* Should be using gpio_get_value_cansleep() */
1537 WARN_ON(desc->chip->can_sleep);
1538
1539 value = _gpiod_get_raw_value(desc);
e20538b8
BA
1540 if (value < 0)
1541 return value;
1542
79a9becd
AC
1543 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1544 value = !value;
1545
1546 return value;
372e722e 1547}
79a9becd 1548EXPORT_SYMBOL_GPL(gpiod_get_value);
d2876d08 1549
aca5ce14
LD
1550/*
1551 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
79a9becd 1552 * @desc: gpio descriptor whose state need to be set.
20a8a968 1553 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
aca5ce14 1554 */
23600969 1555static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
aca5ce14
LD
1556{
1557 int err = 0;
372e722e
AC
1558 struct gpio_chip *chip = desc->chip;
1559 int offset = gpio_chip_hwgpio(desc);
1560
aca5ce14 1561 if (value) {
372e722e 1562 err = chip->direction_input(chip, offset);
aca5ce14 1563 if (!err)
372e722e 1564 clear_bit(FLAG_IS_OUT, &desc->flags);
aca5ce14 1565 } else {
372e722e 1566 err = chip->direction_output(chip, offset, 0);
aca5ce14 1567 if (!err)
372e722e 1568 set_bit(FLAG_IS_OUT, &desc->flags);
aca5ce14 1569 }
372e722e 1570 trace_gpio_direction(desc_to_gpio(desc), value, err);
aca5ce14 1571 if (err < 0)
6424de5a
MB
1572 gpiod_err(desc,
1573 "%s: Error in set_value for open drain err %d\n",
1574 __func__, err);
aca5ce14
LD
1575}
1576
25553ff0 1577/*
79a9becd
AC
1578 * _gpio_set_open_source_value() - Set the open source gpio's value.
1579 * @desc: gpio descriptor whose state need to be set.
20a8a968 1580 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
25553ff0 1581 */
23600969 1582static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
25553ff0
LD
1583{
1584 int err = 0;
372e722e
AC
1585 struct gpio_chip *chip = desc->chip;
1586 int offset = gpio_chip_hwgpio(desc);
1587
25553ff0 1588 if (value) {
372e722e 1589 err = chip->direction_output(chip, offset, 1);
25553ff0 1590 if (!err)
372e722e 1591 set_bit(FLAG_IS_OUT, &desc->flags);
25553ff0 1592 } else {
372e722e 1593 err = chip->direction_input(chip, offset);
25553ff0 1594 if (!err)
372e722e 1595 clear_bit(FLAG_IS_OUT, &desc->flags);
25553ff0 1596 }
372e722e 1597 trace_gpio_direction(desc_to_gpio(desc), !value, err);
25553ff0 1598 if (err < 0)
6424de5a
MB
1599 gpiod_err(desc,
1600 "%s: Error in set_value for open source err %d\n",
1601 __func__, err);
25553ff0
LD
1602}
1603
23600969 1604static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
d2876d08
DB
1605{
1606 struct gpio_chip *chip;
1607
372e722e 1608 chip = desc->chip;
372e722e
AC
1609 trace_gpio_value(desc_to_gpio(desc), 0, value);
1610 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1611 _gpio_set_open_drain_value(desc, value);
1612 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1613 _gpio_set_open_source_value(desc, value);
aca5ce14 1614 else
372e722e
AC
1615 chip->set(chip, gpio_chip_hwgpio(desc), value);
1616}
1617
5f424243
RI
1618/*
1619 * set multiple outputs on the same chip;
1620 * use the chip's set_multiple function if available;
1621 * otherwise set the outputs sequentially;
1622 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1623 * defines which outputs are to be changed
1624 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1625 * defines the values the outputs specified by mask are to be set to
1626 */
1627static void gpio_chip_set_multiple(struct gpio_chip *chip,
1628 unsigned long *mask, unsigned long *bits)
1629{
1630 if (chip->set_multiple) {
1631 chip->set_multiple(chip, mask, bits);
1632 } else {
1633 int i;
1634 for (i = 0; i < chip->ngpio; i++) {
1635 if (mask[BIT_WORD(i)] == 0) {
1636 /* no more set bits in this mask word;
1637 * skip ahead to the next word */
1638 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1639 continue;
1640 }
1641 /* set outputs if the corresponding mask bit is set */
38e003f4 1642 if (__test_and_clear_bit(i, mask))
5f424243 1643 chip->set(chip, i, test_bit(i, bits));
5f424243
RI
1644 }
1645 }
1646}
1647
3fff99bc
RI
1648static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1649 unsigned int array_size,
1650 struct gpio_desc **desc_array,
1651 int *value_array)
5f424243
RI
1652{
1653 int i = 0;
1654
1655 while (i < array_size) {
1656 struct gpio_chip *chip = desc_array[i]->chip;
1657 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1658 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1659 int count = 0;
1660
38e003f4 1661 if (!can_sleep)
5f424243 1662 WARN_ON(chip->can_sleep);
38e003f4 1663
5f424243
RI
1664 memset(mask, 0, sizeof(mask));
1665 do {
1666 struct gpio_desc *desc = desc_array[i];
1667 int hwgpio = gpio_chip_hwgpio(desc);
1668 int value = value_array[i];
1669
1670 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1671 value = !value;
1672 trace_gpio_value(desc_to_gpio(desc), 0, value);
1673 /*
1674 * collect all normal outputs belonging to the same chip
1675 * open drain and open source outputs are set individually
1676 */
1677 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
38e003f4 1678 _gpio_set_open_drain_value(desc, value);
5f424243
RI
1679 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1680 _gpio_set_open_source_value(desc, value);
1681 } else {
1682 __set_bit(hwgpio, mask);
38e003f4 1683 if (value)
5f424243 1684 __set_bit(hwgpio, bits);
38e003f4 1685 else
5f424243 1686 __clear_bit(hwgpio, bits);
5f424243
RI
1687 count++;
1688 }
1689 i++;
1690 } while ((i < array_size) && (desc_array[i]->chip == chip));
1691 /* push collected bits to outputs */
38e003f4 1692 if (count != 0)
5f424243 1693 gpio_chip_set_multiple(chip, mask, bits);
5f424243
RI
1694 }
1695}
1696
d2876d08 1697/**
79a9becd
AC
1698 * gpiod_set_raw_value() - assign a gpio's raw value
1699 * @desc: gpio whose value will be assigned
d2876d08 1700 * @value: value to assign
d2876d08 1701 *
79a9becd
AC
1702 * Set the raw value of the GPIO, i.e. the value of its physical line without
1703 * regard for its ACTIVE_LOW status.
1704 *
1705 * This function should be called from contexts where we cannot sleep, and will
1706 * complain if the GPIO chip functions potentially sleep.
d2876d08 1707 */
79a9becd 1708void gpiod_set_raw_value(struct gpio_desc *desc, int value)
372e722e 1709{
bcabdef1
AC
1710 if (!desc)
1711 return;
e4e449e8 1712 /* Should be using gpio_set_value_cansleep() */
d8e0ac08 1713 WARN_ON(desc->chip->can_sleep);
79a9becd 1714 _gpiod_set_raw_value(desc, value);
d2876d08 1715}
79a9becd 1716EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
d2876d08
DB
1717
1718/**
79a9becd
AC
1719 * gpiod_set_value() - assign a gpio's value
1720 * @desc: gpio whose value will be assigned
1721 * @value: value to assign
1722 *
1723 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1724 * account
d2876d08 1725 *
79a9becd
AC
1726 * This function should be called from contexts where we cannot sleep, and will
1727 * complain if the GPIO chip functions potentially sleep.
d2876d08 1728 */
79a9becd 1729void gpiod_set_value(struct gpio_desc *desc, int value)
d2876d08 1730{
bcabdef1 1731 if (!desc)
79a9becd
AC
1732 return;
1733 /* Should be using gpio_set_value_cansleep() */
1734 WARN_ON(desc->chip->can_sleep);
1735 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1736 value = !value;
1737 _gpiod_set_raw_value(desc, value);
372e722e 1738}
79a9becd 1739EXPORT_SYMBOL_GPL(gpiod_set_value);
d2876d08 1740
5f424243 1741/**
3fff99bc 1742 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
5f424243
RI
1743 * @array_size: number of elements in the descriptor / value arrays
1744 * @desc_array: array of GPIO descriptors whose values will be assigned
1745 * @value_array: array of values to assign
1746 *
1747 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1748 * without regard for their ACTIVE_LOW status.
1749 *
1750 * This function should be called from contexts where we cannot sleep, and will
1751 * complain if the GPIO chip functions potentially sleep.
1752 */
3fff99bc 1753void gpiod_set_raw_array_value(unsigned int array_size,
5f424243
RI
1754 struct gpio_desc **desc_array, int *value_array)
1755{
1756 if (!desc_array)
1757 return;
3fff99bc
RI
1758 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1759 value_array);
5f424243 1760}
3fff99bc 1761EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
5f424243
RI
1762
1763/**
3fff99bc 1764 * gpiod_set_array_value() - assign values to an array of GPIOs
5f424243
RI
1765 * @array_size: number of elements in the descriptor / value arrays
1766 * @desc_array: array of GPIO descriptors whose values will be assigned
1767 * @value_array: array of values to assign
1768 *
1769 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1770 * into account.
1771 *
1772 * This function should be called from contexts where we cannot sleep, and will
1773 * complain if the GPIO chip functions potentially sleep.
1774 */
3fff99bc
RI
1775void gpiod_set_array_value(unsigned int array_size,
1776 struct gpio_desc **desc_array, int *value_array)
5f424243
RI
1777{
1778 if (!desc_array)
1779 return;
3fff99bc
RI
1780 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1781 value_array);
5f424243 1782}
3fff99bc 1783EXPORT_SYMBOL_GPL(gpiod_set_array_value);
5f424243 1784
d2876d08 1785/**
79a9becd
AC
1786 * gpiod_cansleep() - report whether gpio value access may sleep
1787 * @desc: gpio to check
d2876d08 1788 *
d2876d08 1789 */
79a9becd 1790int gpiod_cansleep(const struct gpio_desc *desc)
372e722e 1791{
bcabdef1
AC
1792 if (!desc)
1793 return 0;
372e722e 1794 return desc->chip->can_sleep;
d2876d08 1795}
79a9becd 1796EXPORT_SYMBOL_GPL(gpiod_cansleep);
d2876d08 1797
0f6d504e 1798/**
79a9becd
AC
1799 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1800 * @desc: gpio whose IRQ will be returned (already requested)
0f6d504e 1801 *
79a9becd
AC
1802 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1803 * error.
0f6d504e 1804 */
79a9becd 1805int gpiod_to_irq(const struct gpio_desc *desc)
0f6d504e
DB
1806{
1807 struct gpio_chip *chip;
372e722e 1808 int offset;
0f6d504e 1809
bcabdef1
AC
1810 if (!desc)
1811 return -EINVAL;
372e722e
AC
1812 chip = desc->chip;
1813 offset = gpio_chip_hwgpio(desc);
1814 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
0f6d504e 1815}
79a9becd 1816EXPORT_SYMBOL_GPL(gpiod_to_irq);
0f6d504e 1817
d468bf9e 1818/**
e3a2e878 1819 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
d74be6df
AC
1820 * @chip: the chip the GPIO to lock belongs to
1821 * @offset: the offset of the GPIO to lock as IRQ
d468bf9e
LW
1822 *
1823 * This is used directly by GPIO drivers that want to lock down
f438acdf 1824 * a certain GPIO line to be used for IRQs.
d468bf9e 1825 */
e3a2e878 1826int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
372e722e 1827{
d74be6df 1828 if (offset >= chip->ngpio)
d468bf9e
LW
1829 return -EINVAL;
1830
1c3cdb18 1831 if (test_bit(FLAG_IS_OUT, &chip->gpiodev->descs[offset].flags)) {
d74be6df 1832 chip_err(chip,
d468bf9e
LW
1833 "%s: tried to flag a GPIO set as output for IRQ\n",
1834 __func__);
1835 return -EIO;
1836 }
1837
1c3cdb18 1838 set_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
d468bf9e 1839 return 0;
372e722e 1840}
e3a2e878 1841EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
d2876d08 1842
d468bf9e 1843/**
e3a2e878 1844 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
d74be6df
AC
1845 * @chip: the chip the GPIO to lock belongs to
1846 * @offset: the offset of the GPIO to lock as IRQ
d468bf9e
LW
1847 *
1848 * This is used directly by GPIO drivers that want to indicate
1849 * that a certain GPIO is no longer used exclusively for IRQ.
d2876d08 1850 */
e3a2e878 1851void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
d468bf9e 1852{
d74be6df 1853 if (offset >= chip->ngpio)
d468bf9e 1854 return;
d2876d08 1855
1c3cdb18 1856 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
d468bf9e 1857}
e3a2e878 1858EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
d468bf9e 1859
6cee3821
LW
1860bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
1861{
1862 if (offset >= chip->ngpio)
1863 return false;
1864
1865 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
1866}
1867EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
1868
79a9becd
AC
1869/**
1870 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1871 * @desc: gpio whose value will be returned
1872 *
1873 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
e20538b8 1874 * its ACTIVE_LOW status, or negative errno on failure.
79a9becd
AC
1875 *
1876 * This function is to be called from contexts that can sleep.
d2876d08 1877 */
79a9becd 1878int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
d2876d08 1879{
d2876d08 1880 might_sleep_if(extra_checks);
bcabdef1
AC
1881 if (!desc)
1882 return 0;
79a9becd 1883 return _gpiod_get_raw_value(desc);
d2876d08 1884}
79a9becd 1885EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
372e722e 1886
79a9becd
AC
1887/**
1888 * gpiod_get_value_cansleep() - return a gpio's value
1889 * @desc: gpio whose value will be returned
1890 *
1891 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
e20538b8 1892 * account, or negative errno on failure.
79a9becd
AC
1893 *
1894 * This function is to be called from contexts that can sleep.
1895 */
1896int gpiod_get_value_cansleep(const struct gpio_desc *desc)
d2876d08 1897{
3f397c21 1898 int value;
d2876d08
DB
1899
1900 might_sleep_if(extra_checks);
bcabdef1
AC
1901 if (!desc)
1902 return 0;
79a9becd
AC
1903
1904 value = _gpiod_get_raw_value(desc);
e20538b8
BA
1905 if (value < 0)
1906 return value;
1907
79a9becd
AC
1908 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1909 value = !value;
1910
3f397c21 1911 return value;
d2876d08 1912}
79a9becd 1913EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
372e722e 1914
79a9becd
AC
1915/**
1916 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1917 * @desc: gpio whose value will be assigned
1918 * @value: value to assign
1919 *
1920 * Set the raw value of the GPIO, i.e. the value of its physical line without
1921 * regard for its ACTIVE_LOW status.
1922 *
1923 * This function is to be called from contexts that can sleep.
1924 */
1925void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
372e722e 1926{
d2876d08 1927 might_sleep_if(extra_checks);
bcabdef1
AC
1928 if (!desc)
1929 return;
79a9becd 1930 _gpiod_set_raw_value(desc, value);
372e722e 1931}
79a9becd 1932EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
d2876d08 1933
79a9becd
AC
1934/**
1935 * gpiod_set_value_cansleep() - assign a gpio's value
1936 * @desc: gpio whose value will be assigned
1937 * @value: value to assign
1938 *
1939 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1940 * account
1941 *
1942 * This function is to be called from contexts that can sleep.
1943 */
1944void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
d2876d08 1945{
d2876d08 1946 might_sleep_if(extra_checks);
bcabdef1
AC
1947 if (!desc)
1948 return;
79a9becd
AC
1949
1950 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1951 value = !value;
1952 _gpiod_set_raw_value(desc, value);
372e722e 1953}
79a9becd 1954EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
d2876d08 1955
5f424243 1956/**
3fff99bc 1957 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
5f424243
RI
1958 * @array_size: number of elements in the descriptor / value arrays
1959 * @desc_array: array of GPIO descriptors whose values will be assigned
1960 * @value_array: array of values to assign
1961 *
1962 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1963 * without regard for their ACTIVE_LOW status.
1964 *
1965 * This function is to be called from contexts that can sleep.
1966 */
3fff99bc
RI
1967void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1968 struct gpio_desc **desc_array,
1969 int *value_array)
5f424243
RI
1970{
1971 might_sleep_if(extra_checks);
1972 if (!desc_array)
1973 return;
3fff99bc
RI
1974 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1975 value_array);
5f424243 1976}
3fff99bc 1977EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
5f424243
RI
1978
1979/**
3fff99bc 1980 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
5f424243
RI
1981 * @array_size: number of elements in the descriptor / value arrays
1982 * @desc_array: array of GPIO descriptors whose values will be assigned
1983 * @value_array: array of values to assign
1984 *
1985 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1986 * into account.
1987 *
1988 * This function is to be called from contexts that can sleep.
1989 */
3fff99bc
RI
1990void gpiod_set_array_value_cansleep(unsigned int array_size,
1991 struct gpio_desc **desc_array,
1992 int *value_array)
5f424243
RI
1993{
1994 might_sleep_if(extra_checks);
1995 if (!desc_array)
1996 return;
3fff99bc
RI
1997 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1998 value_array);
5f424243 1999}
3fff99bc 2000EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
5f424243 2001
bae48da2 2002/**
ad824783
AC
2003 * gpiod_add_lookup_table() - register GPIO device consumers
2004 * @table: table of consumers to register
bae48da2 2005 */
ad824783 2006void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
bae48da2
AC
2007{
2008 mutex_lock(&gpio_lookup_lock);
2009
ad824783 2010 list_add_tail(&table->list, &gpio_lookup_list);
bae48da2
AC
2011
2012 mutex_unlock(&gpio_lookup_lock);
2013}
2014
be9015ab
SK
2015/**
2016 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2017 * @table: table of consumers to unregister
2018 */
2019void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2020{
2021 mutex_lock(&gpio_lookup_lock);
2022
2023 list_del(&table->list);
2024
2025 mutex_unlock(&gpio_lookup_lock);
2026}
2027
bae48da2 2028static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
53e7cac3
AC
2029 unsigned int idx,
2030 enum gpio_lookup_flags *flags)
bae48da2
AC
2031{
2032 char prop_name[32]; /* 32 is max size of property name */
2033 enum of_gpio_flags of_flags;
2034 struct gpio_desc *desc;
dd34c37a 2035 unsigned int i;
bae48da2 2036
7f2e553a 2037 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
dd34c37a 2038 if (con_id)
9e089246 2039 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
7f2e553a 2040 gpio_suffixes[i]);
dd34c37a 2041 else
9e089246 2042 snprintf(prop_name, sizeof(prop_name), "%s",
7f2e553a 2043 gpio_suffixes[i]);
bae48da2 2044
dd34c37a
TR
2045 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2046 &of_flags);
06fc3b70 2047 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
dd34c37a
TR
2048 break;
2049 }
bae48da2
AC
2050
2051 if (IS_ERR(desc))
2052 return desc;
2053
2054 if (of_flags & OF_GPIO_ACTIVE_LOW)
53e7cac3 2055 *flags |= GPIO_ACTIVE_LOW;
bae48da2 2056
90b665f6
LP
2057 if (of_flags & OF_GPIO_SINGLE_ENDED) {
2058 if (of_flags & OF_GPIO_ACTIVE_LOW)
2059 *flags |= GPIO_OPEN_DRAIN;
2060 else
2061 *flags |= GPIO_OPEN_SOURCE;
2062 }
2063
bae48da2
AC
2064 return desc;
2065}
d2876d08 2066
81f59e9d 2067static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
53e7cac3
AC
2068 unsigned int idx,
2069 enum gpio_lookup_flags *flags)
372e722e 2070{
0d9a693c 2071 struct acpi_device *adev = ACPI_COMPANION(dev);
e01f440a
MW
2072 struct acpi_gpio_info info;
2073 struct gpio_desc *desc;
0d9a693c
MW
2074 char propname[32];
2075 int i;
e01f440a 2076
0d9a693c 2077 /* Try first from _DSD */
7f2e553a 2078 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
0d9a693c
MW
2079 if (con_id && strcmp(con_id, "gpios")) {
2080 snprintf(propname, sizeof(propname), "%s-%s",
7f2e553a 2081 con_id, gpio_suffixes[i]);
0d9a693c
MW
2082 } else {
2083 snprintf(propname, sizeof(propname), "%s",
7f2e553a 2084 gpio_suffixes[i]);
0d9a693c
MW
2085 }
2086
2087 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
2088 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2089 break;
2090 }
2091
2092 /* Then from plain _CRS GPIOs */
2093 if (IS_ERR(desc)) {
10cf4899
DT
2094 if (!acpi_can_fallback_to_crs(adev, con_id))
2095 return ERR_PTR(-ENOENT);
2096
0d9a693c
MW
2097 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2098 if (IS_ERR(desc))
2099 return desc;
2100 }
e01f440a 2101
52044723 2102 if (info.polarity == GPIO_ACTIVE_LOW)
53e7cac3 2103 *flags |= GPIO_ACTIVE_LOW;
e01f440a
MW
2104
2105 return desc;
81f59e9d
MW
2106}
2107
ad824783 2108static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
bae48da2
AC
2109{
2110 const char *dev_id = dev ? dev_name(dev) : NULL;
ad824783 2111 struct gpiod_lookup_table *table;
bae48da2
AC
2112
2113 mutex_lock(&gpio_lookup_lock);
2114
ad824783
AC
2115 list_for_each_entry(table, &gpio_lookup_list, list) {
2116 if (table->dev_id && dev_id) {
2117 /*
2118 * Valid strings on both ends, must be identical to have
2119 * a match
2120 */
2121 if (!strcmp(table->dev_id, dev_id))
2122 goto found;
2123 } else {
2124 /*
2125 * One of the pointers is NULL, so both must be to have
2126 * a match
2127 */
2128 if (dev_id == table->dev_id)
2129 goto found;
2130 }
2131 }
2132 table = NULL;
bae48da2 2133
ad824783
AC
2134found:
2135 mutex_unlock(&gpio_lookup_lock);
2136 return table;
2137}
bae48da2 2138
ad824783
AC
2139static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
2140 unsigned int idx,
2141 enum gpio_lookup_flags *flags)
2142{
2a3cf6a3 2143 struct gpio_desc *desc = ERR_PTR(-ENOENT);
ad824783
AC
2144 struct gpiod_lookup_table *table;
2145 struct gpiod_lookup *p;
bae48da2 2146
ad824783
AC
2147 table = gpiod_find_lookup_table(dev);
2148 if (!table)
2149 return desc;
bae48da2 2150
ad824783
AC
2151 for (p = &table->table[0]; p->chip_label; p++) {
2152 struct gpio_chip *chip;
bae48da2 2153
ad824783 2154 /* idx must always match exactly */
bae48da2
AC
2155 if (p->idx != idx)
2156 continue;
2157
ad824783
AC
2158 /* If the lookup entry has a con_id, require exact match */
2159 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2160 continue;
bae48da2 2161
ad824783 2162 chip = find_chip_by_name(p->chip_label);
bae48da2 2163
ad824783 2164 if (!chip) {
2a3cf6a3
AC
2165 dev_err(dev, "cannot find GPIO chip %s\n",
2166 p->chip_label);
2167 return ERR_PTR(-ENODEV);
ad824783 2168 }
bae48da2 2169
ad824783 2170 if (chip->ngpio <= p->chip_hwnum) {
2a3cf6a3
AC
2171 dev_err(dev,
2172 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2173 idx, chip->ngpio, chip->label);
2174 return ERR_PTR(-EINVAL);
bae48da2 2175 }
bae48da2 2176
bb1e88cc 2177 desc = gpiochip_get_desc(chip, p->chip_hwnum);
ad824783 2178 *flags = p->flags;
bae48da2 2179
2a3cf6a3 2180 return desc;
bae48da2
AC
2181 }
2182
bae48da2
AC
2183 return desc;
2184}
2185
66858527
RI
2186static int dt_gpio_count(struct device *dev, const char *con_id)
2187{
2188 int ret;
2189 char propname[32];
2190 unsigned int i;
2191
2192 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2193 if (con_id)
2194 snprintf(propname, sizeof(propname), "%s-%s",
2195 con_id, gpio_suffixes[i]);
2196 else
2197 snprintf(propname, sizeof(propname), "%s",
2198 gpio_suffixes[i]);
2199
2200 ret = of_gpio_named_count(dev->of_node, propname);
2201 if (ret >= 0)
2202 break;
2203 }
2204 return ret;
2205}
2206
2207static int platform_gpio_count(struct device *dev, const char *con_id)
2208{
2209 struct gpiod_lookup_table *table;
2210 struct gpiod_lookup *p;
2211 unsigned int count = 0;
2212
2213 table = gpiod_find_lookup_table(dev);
2214 if (!table)
2215 return -ENOENT;
2216
2217 for (p = &table->table[0]; p->chip_label; p++) {
2218 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2219 (!con_id && !p->con_id))
2220 count++;
2221 }
2222 if (!count)
2223 return -ENOENT;
2224
2225 return count;
2226}
2227
2228/**
2229 * gpiod_count - return the number of GPIOs associated with a device / function
2230 * or -ENOENT if no GPIO has been assigned to the requested function
2231 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2232 * @con_id: function within the GPIO consumer
2233 */
2234int gpiod_count(struct device *dev, const char *con_id)
2235{
2236 int count = -ENOENT;
2237
2238 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2239 count = dt_gpio_count(dev, con_id);
2240 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2241 count = acpi_gpio_count(dev, con_id);
2242
2243 if (count < 0)
2244 count = platform_gpio_count(dev, con_id);
2245
2246 return count;
2247}
2248EXPORT_SYMBOL_GPL(gpiod_count);
2249
bae48da2 2250/**
0879162f 2251 * gpiod_get - obtain a GPIO for a given GPIO function
ad824783 2252 * @dev: GPIO consumer, can be NULL for system-global GPIOs
bae48da2 2253 * @con_id: function within the GPIO consumer
39b2bbe3 2254 * @flags: optional GPIO initialization flags
bae48da2
AC
2255 *
2256 * Return the GPIO descriptor corresponding to the function con_id of device
2a3cf6a3 2257 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
20a8a968 2258 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
bae48da2 2259 */
b17d1bf1 2260struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
39b2bbe3 2261 enum gpiod_flags flags)
bae48da2 2262{
39b2bbe3 2263 return gpiod_get_index(dev, con_id, 0, flags);
bae48da2 2264}
b17d1bf1 2265EXPORT_SYMBOL_GPL(gpiod_get);
bae48da2 2266
29a1f233
TR
2267/**
2268 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2269 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2270 * @con_id: function within the GPIO consumer
39b2bbe3 2271 * @flags: optional GPIO initialization flags
29a1f233
TR
2272 *
2273 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2274 * the requested function it will return NULL. This is convenient for drivers
2275 * that need to handle optional GPIOs.
2276 */
b17d1bf1 2277struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
39b2bbe3
AC
2278 const char *con_id,
2279 enum gpiod_flags flags)
29a1f233 2280{
39b2bbe3 2281 return gpiod_get_index_optional(dev, con_id, 0, flags);
29a1f233 2282}
b17d1bf1 2283EXPORT_SYMBOL_GPL(gpiod_get_optional);
29a1f233 2284
923b93e4
LP
2285/**
2286 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2287 * @desc: gpio to be setup
2288 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2289 * of_get_gpio_hog()
2290 *
2291 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2292 */
2293static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2294{
2295 if (lflags & GPIO_ACTIVE_LOW)
2296 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2297 if (lflags & GPIO_OPEN_DRAIN)
2298 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2299 if (lflags & GPIO_OPEN_SOURCE)
2300 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2301}
f625d460
BP
2302
2303/**
2304 * gpiod_configure_flags - helper function to configure a given GPIO
2305 * @desc: gpio whose value will be assigned
2306 * @con_id: function within the GPIO consumer
f625d460
BP
2307 * @dflags: gpiod_flags - optional GPIO initialization flags
2308 *
2309 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2310 * requested function and/or index, or another IS_ERR() code if an error
2311 * occurred while trying to acquire the GPIO.
2312 */
2313static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
923b93e4 2314 enum gpiod_flags dflags)
f625d460
BP
2315{
2316 int status;
2317
f625d460
BP
2318 /* No particular flag request, return here... */
2319 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2320 pr_debug("no flags found for %s\n", con_id);
2321 return 0;
2322 }
2323
2324 /* Process flags */
2325 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2326 status = gpiod_direction_output(desc,
2327 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2328 else
2329 status = gpiod_direction_input(desc);
2330
2331 return status;
2332}
2333
bae48da2
AC
2334/**
2335 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
fdd6a5fe 2336 * @dev: GPIO consumer, can be NULL for system-global GPIOs
bae48da2
AC
2337 * @con_id: function within the GPIO consumer
2338 * @idx: index of the GPIO to obtain in the consumer
39b2bbe3 2339 * @flags: optional GPIO initialization flags
bae48da2
AC
2340 *
2341 * This variant of gpiod_get() allows to access GPIOs other than the first
2342 * defined one for functions that define several GPIOs.
2343 *
2a3cf6a3
AC
2344 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2345 * requested function and/or index, or another IS_ERR() code if an error
20a8a968 2346 * occurred while trying to acquire the GPIO.
bae48da2 2347 */
b17d1bf1 2348struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
bae48da2 2349 const char *con_id,
39b2bbe3
AC
2350 unsigned int idx,
2351 enum gpiod_flags flags)
bae48da2 2352{
35c5d7fd 2353 struct gpio_desc *desc = NULL;
bae48da2 2354 int status;
39b2bbe3 2355 enum gpio_lookup_flags lookupflags = 0;
bae48da2
AC
2356
2357 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2358
4d8440b9
RW
2359 if (dev) {
2360 /* Using device tree? */
2361 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2362 dev_dbg(dev, "using device tree for GPIO lookup\n");
2363 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2364 } else if (ACPI_COMPANION(dev)) {
2365 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2366 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2367 }
35c5d7fd
AC
2368 }
2369
2370 /*
2371 * Either we are not using DT or ACPI, or their lookup did not return
2372 * a result. In that case, use platform lookup as a fallback.
2373 */
2a3cf6a3 2374 if (!desc || desc == ERR_PTR(-ENOENT)) {
43a8785a 2375 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
39b2bbe3 2376 desc = gpiod_find(dev, con_id, idx, &lookupflags);
bae48da2
AC
2377 }
2378
2379 if (IS_ERR(desc)) {
351cfe0f 2380 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
bae48da2
AC
2381 return desc;
2382 }
2383
923b93e4
LP
2384 gpiod_parse_flags(desc, lookupflags);
2385
bae48da2 2386 status = gpiod_request(desc, con_id);
bae48da2
AC
2387 if (status < 0)
2388 return ERR_PTR(status);
2389
923b93e4 2390 status = gpiod_configure_flags(desc, con_id, flags);
39b2bbe3
AC
2391 if (status < 0) {
2392 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2393 gpiod_put(desc);
2394 return ERR_PTR(status);
2395 }
2396
bae48da2
AC
2397 return desc;
2398}
b17d1bf1 2399EXPORT_SYMBOL_GPL(gpiod_get_index);
bae48da2 2400
40b73183
MW
2401/**
2402 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2403 * @fwnode: handle of the firmware node
2404 * @propname: name of the firmware property representing the GPIO
2405 *
2406 * This function can be used for drivers that get their configuration
2407 * from firmware.
2408 *
2409 * Function properly finds the corresponding GPIO using whatever is the
2410 * underlying firmware interface and then makes sure that the GPIO
2411 * descriptor is requested before it is returned to the caller.
2412 *
2413 * In case of error an ERR_PTR() is returned.
2414 */
2415struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2416 const char *propname)
2417{
2418 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2419 bool active_low = false;
90b665f6 2420 bool single_ended = false;
40b73183
MW
2421 int ret;
2422
2423 if (!fwnode)
2424 return ERR_PTR(-EINVAL);
2425
2426 if (is_of_node(fwnode)) {
2427 enum of_gpio_flags flags;
2428
c181fb3e 2429 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
40b73183 2430 &flags);
90b665f6 2431 if (!IS_ERR(desc)) {
40b73183 2432 active_low = flags & OF_GPIO_ACTIVE_LOW;
90b665f6
LP
2433 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2434 }
40b73183
MW
2435 } else if (is_acpi_node(fwnode)) {
2436 struct acpi_gpio_info info;
2437
504a3374 2438 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
40b73183 2439 if (!IS_ERR(desc))
52044723 2440 active_low = info.polarity == GPIO_ACTIVE_LOW;
40b73183
MW
2441 }
2442
2443 if (IS_ERR(desc))
2444 return desc;
2445
40b73183
MW
2446 if (active_low)
2447 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2448
90b665f6
LP
2449 if (single_ended) {
2450 if (active_low)
2451 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2452 else
2453 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2454 }
2455
923b93e4
LP
2456 ret = gpiod_request(desc, NULL);
2457 if (ret)
2458 return ERR_PTR(ret);
2459
40b73183
MW
2460 return desc;
2461}
2462EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2463
29a1f233
TR
2464/**
2465 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2466 * function
2467 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2468 * @con_id: function within the GPIO consumer
2469 * @index: index of the GPIO to obtain in the consumer
39b2bbe3 2470 * @flags: optional GPIO initialization flags
29a1f233
TR
2471 *
2472 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2473 * specified index was assigned to the requested function it will return NULL.
2474 * This is convenient for drivers that need to handle optional GPIOs.
2475 */
b17d1bf1 2476struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
29a1f233 2477 const char *con_id,
39b2bbe3
AC
2478 unsigned int index,
2479 enum gpiod_flags flags)
29a1f233
TR
2480{
2481 struct gpio_desc *desc;
2482
39b2bbe3 2483 desc = gpiod_get_index(dev, con_id, index, flags);
29a1f233
TR
2484 if (IS_ERR(desc)) {
2485 if (PTR_ERR(desc) == -ENOENT)
2486 return NULL;
2487 }
2488
2489 return desc;
2490}
b17d1bf1 2491EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
29a1f233 2492
f625d460
BP
2493/**
2494 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2495 * @desc: gpio whose value will be assigned
2496 * @name: gpio line name
2497 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2498 * of_get_gpio_hog()
2499 * @dflags: gpiod_flags - optional GPIO initialization flags
2500 */
2501int gpiod_hog(struct gpio_desc *desc, const char *name,
2502 unsigned long lflags, enum gpiod_flags dflags)
2503{
2504 struct gpio_chip *chip;
2505 struct gpio_desc *local_desc;
2506 int hwnum;
2507 int status;
2508
2509 chip = gpiod_to_chip(desc);
2510 hwnum = gpio_chip_hwgpio(desc);
2511
923b93e4
LP
2512 gpiod_parse_flags(desc, lflags);
2513
f625d460
BP
2514 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2515 if (IS_ERR(local_desc)) {
a713890d
LW
2516 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2517 name, chip->label, hwnum);
f625d460
BP
2518 return PTR_ERR(local_desc);
2519 }
2520
923b93e4 2521 status = gpiod_configure_flags(desc, name, dflags);
f625d460 2522 if (status < 0) {
a713890d
LW
2523 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2524 name, chip->label, hwnum);
f625d460
BP
2525 gpiochip_free_own_desc(desc);
2526 return status;
2527 }
2528
2529 /* Mark GPIO as hogged so it can be identified and removed later */
2530 set_bit(FLAG_IS_HOGGED, &desc->flags);
2531
2532 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2533 desc_to_gpio(desc), name,
2534 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2535 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2536 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2537
2538 return 0;
2539}
2540
2541/**
2542 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2543 * @chip: gpio chip to act on
2544 *
2545 * This is only used by of_gpiochip_remove to free hogged gpios
2546 */
2547static void gpiochip_free_hogs(struct gpio_chip *chip)
2548{
2549 int id;
2550
2551 for (id = 0; id < chip->ngpio; id++) {
1c3cdb18
LW
2552 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
2553 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
f625d460
BP
2554 }
2555}
2556
66858527
RI
2557/**
2558 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2559 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2560 * @con_id: function within the GPIO consumer
2561 * @flags: optional GPIO initialization flags
2562 *
2563 * This function acquires all the GPIOs defined under a given function.
2564 *
2565 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2566 * no GPIO has been assigned to the requested function, or another IS_ERR()
2567 * code if an error occurred while trying to acquire the GPIOs.
2568 */
2569struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2570 const char *con_id,
2571 enum gpiod_flags flags)
2572{
2573 struct gpio_desc *desc;
2574 struct gpio_descs *descs;
2575 int count;
2576
2577 count = gpiod_count(dev, con_id);
2578 if (count < 0)
2579 return ERR_PTR(count);
2580
2581 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2582 GFP_KERNEL);
2583 if (!descs)
2584 return ERR_PTR(-ENOMEM);
2585
2586 for (descs->ndescs = 0; descs->ndescs < count; ) {
2587 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2588 if (IS_ERR(desc)) {
2589 gpiod_put_array(descs);
2590 return ERR_CAST(desc);
2591 }
2592 descs->desc[descs->ndescs] = desc;
2593 descs->ndescs++;
2594 }
2595 return descs;
2596}
2597EXPORT_SYMBOL_GPL(gpiod_get_array);
2598
2599/**
2600 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2601 * function
2602 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2603 * @con_id: function within the GPIO consumer
2604 * @flags: optional GPIO initialization flags
2605 *
2606 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2607 * assigned to the requested function it will return NULL.
2608 */
2609struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2610 const char *con_id,
2611 enum gpiod_flags flags)
2612{
2613 struct gpio_descs *descs;
2614
2615 descs = gpiod_get_array(dev, con_id, flags);
2616 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2617 return NULL;
2618
2619 return descs;
2620}
2621EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2622
bae48da2
AC
2623/**
2624 * gpiod_put - dispose of a GPIO descriptor
2625 * @desc: GPIO descriptor to dispose of
2626 *
2627 * No descriptor can be used after gpiod_put() has been called on it.
2628 */
2629void gpiod_put(struct gpio_desc *desc)
2630{
2631 gpiod_free(desc);
372e722e 2632}
bae48da2 2633EXPORT_SYMBOL_GPL(gpiod_put);
d2876d08 2634
66858527
RI
2635/**
2636 * gpiod_put_array - dispose of multiple GPIO descriptors
2637 * @descs: struct gpio_descs containing an array of descriptors
2638 */
2639void gpiod_put_array(struct gpio_descs *descs)
2640{
2641 unsigned int i;
2642
2643 for (i = 0; i < descs->ndescs; i++)
2644 gpiod_put(descs->desc[i]);
2645
2646 kfree(descs);
2647}
2648EXPORT_SYMBOL_GPL(gpiod_put_array);
2649
3c702e99
LW
2650static int __init gpiolib_dev_init(void)
2651{
2652 int ret;
2653
2654 /* Register GPIO sysfs bus */
2655 ret = bus_register(&gpio_bus_type);
2656 if (ret < 0) {
2657 pr_err("gpiolib: could not register GPIO bus type\n");
2658 return ret;
2659 }
2660
2661 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
2662 if (ret < 0) {
2663 pr_err("gpiolib: failed to allocate char dev region\n");
2664 bus_unregister(&gpio_bus_type);
2665 }
2666 return ret;
2667}
2668core_initcall(gpiolib_dev_init);
2669
d2876d08
DB
2670#ifdef CONFIG_DEBUG_FS
2671
d2876d08
DB
2672static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2673{
2674 unsigned i;
2675 unsigned gpio = chip->base;
1c3cdb18 2676 struct gpio_desc *gdesc = &chip->gpiodev->descs[0];
d2876d08 2677 int is_out;
d468bf9e 2678 int is_irq;
d2876d08
DB
2679
2680 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
ced433e2
MP
2681 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2682 if (gdesc->name) {
2683 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2684 gpio, gdesc->name);
2685 }
d2876d08 2686 continue;
ced433e2 2687 }
d2876d08 2688
372e722e 2689 gpiod_get_direction(gdesc);
d2876d08 2690 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
d468bf9e 2691 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
ced433e2
MP
2692 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2693 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
d2876d08
DB
2694 is_out ? "out" : "in ",
2695 chip->get
2696 ? (chip->get(chip, i) ? "hi" : "lo")
d468bf9e
LW
2697 : "? ",
2698 is_irq ? "IRQ" : " ");
d2876d08
DB
2699 seq_printf(s, "\n");
2700 }
2701}
2702
f9c4a31f 2703static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
d2876d08 2704{
362432ae 2705 unsigned long flags;
ff2b1359 2706 struct gpio_device *gdev = NULL;
cb1650d4 2707 loff_t index = *pos;
d2876d08 2708
f9c4a31f 2709 s->private = "";
d2876d08 2710
362432ae 2711 spin_lock_irqsave(&gpio_lock, flags);
ff2b1359 2712 list_for_each_entry(gdev, &gpio_devices, list)
362432ae
GL
2713 if (index-- == 0) {
2714 spin_unlock_irqrestore(&gpio_lock, flags);
ff2b1359 2715 return gdev;
f9c4a31f 2716 }
362432ae 2717 spin_unlock_irqrestore(&gpio_lock, flags);
f9c4a31f 2718
cb1650d4 2719 return NULL;
f9c4a31f
TR
2720}
2721
2722static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2723{
362432ae 2724 unsigned long flags;
ff2b1359 2725 struct gpio_device *gdev = v;
f9c4a31f
TR
2726 void *ret = NULL;
2727
362432ae 2728 spin_lock_irqsave(&gpio_lock, flags);
ff2b1359 2729 if (list_is_last(&gdev->list, &gpio_devices))
cb1650d4
AC
2730 ret = NULL;
2731 else
ff2b1359 2732 ret = list_entry(gdev->list.next, struct gpio_device, list);
362432ae 2733 spin_unlock_irqrestore(&gpio_lock, flags);
f9c4a31f
TR
2734
2735 s->private = "\n";
2736 ++*pos;
2737
2738 return ret;
2739}
2740
2741static void gpiolib_seq_stop(struct seq_file *s, void *v)
2742{
2743}
2744
2745static int gpiolib_seq_show(struct seq_file *s, void *v)
2746{
ff2b1359
LW
2747 struct gpio_device *gdev = v;
2748 struct gpio_chip *chip = gdev->chip;
2749 struct device *parent;
2750
2751 if (!chip) {
2752 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
2753 dev_name(&gdev->dev));
2754 return 0;
2755 }
f9c4a31f 2756
ff2b1359
LW
2757 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
2758 dev_name(&gdev->dev),
2759 chip->base, chip->base + chip->ngpio - 1);
2760 parent = chip->parent;
2761 if (parent)
2762 seq_printf(s, ", parent: %s/%s",
2763 parent->bus ? parent->bus->name : "no-bus",
2764 dev_name(parent));
f9c4a31f
TR
2765 if (chip->label)
2766 seq_printf(s, ", %s", chip->label);
2767 if (chip->can_sleep)
2768 seq_printf(s, ", can sleep");
2769 seq_printf(s, ":\n");
2770
2771 if (chip->dbg_show)
2772 chip->dbg_show(s, chip);
2773 else
2774 gpiolib_dbg_show(s, chip);
2775
d2876d08
DB
2776 return 0;
2777}
2778
f9c4a31f
TR
2779static const struct seq_operations gpiolib_seq_ops = {
2780 .start = gpiolib_seq_start,
2781 .next = gpiolib_seq_next,
2782 .stop = gpiolib_seq_stop,
2783 .show = gpiolib_seq_show,
2784};
2785
d2876d08
DB
2786static int gpiolib_open(struct inode *inode, struct file *file)
2787{
f9c4a31f 2788 return seq_open(file, &gpiolib_seq_ops);
d2876d08
DB
2789}
2790
828c0950 2791static const struct file_operations gpiolib_operations = {
f9c4a31f 2792 .owner = THIS_MODULE,
d2876d08
DB
2793 .open = gpiolib_open,
2794 .read = seq_read,
2795 .llseek = seq_lseek,
f9c4a31f 2796 .release = seq_release,
d2876d08
DB
2797};
2798
2799static int __init gpiolib_debugfs_init(void)
2800{
2801 /* /sys/kernel/debug/gpio */
2802 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2803 NULL, NULL, &gpiolib_operations);
2804 return 0;
2805}
2806subsys_initcall(gpiolib_debugfs_init);
2807
2808#endif /* DEBUG_FS */