]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpio/gpiolib.c
gpio: Add Tegra186 support
[mirror_ubuntu-bionic-kernel.git] / drivers / gpio / gpiolib.c
CommitLineData
923a654c 1#include <linux/bitmap.h>
d2876d08
DB
2#include <linux/kernel.h>
3#include <linux/module.h>
ff77c352 4#include <linux/interrupt.h>
d2876d08
DB
5#include <linux/irq.h>
6#include <linux/spinlock.h>
1a989d0f 7#include <linux/list.h>
d8f388d8
DB
8#include <linux/device.h>
9#include <linux/err.h>
10#include <linux/debugfs.h>
11#include <linux/seq_file.h>
12#include <linux/gpio.h>
391c970c 13#include <linux/of_gpio.h>
ff77c352 14#include <linux/idr.h>
5a0e3ad6 15#include <linux/slab.h>
7b199811 16#include <linux/acpi.h>
53e7cac3 17#include <linux/gpio/driver.h>
0a6d3158 18#include <linux/gpio/machine.h>
c771c2f4 19#include <linux/pinctrl/consumer.h>
3c702e99
LW
20#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
8b92e17e 23#include <linux/compat.h>
d7c51b47 24#include <linux/anon_inodes.h>
953b956a 25#include <linux/file.h>
61f922db
LW
26#include <linux/kfifo.h>
27#include <linux/poll.h>
28#include <linux/timekeeping.h>
3c702e99 29#include <uapi/linux/gpio.h>
d2876d08 30
664e3e5a
MW
31#include "gpiolib.h"
32
3f397c21
UKK
33#define CREATE_TRACE_POINTS
34#include <trace/events/gpio.h>
d2876d08 35
79a9becd 36/* Implementation infrastructure for GPIO interfaces.
d2876d08 37 *
79a9becd
AC
38 * The GPIO programming interface allows for inlining speed-critical
39 * get/set operations for common cases, so that access to SOC-integrated
40 * GPIOs can sometimes cost only an instruction or two per bit.
d2876d08
DB
41 */
42
43
44/* When debugging, extend minimal trust to callers and platform code.
45 * Also emit diagnostic messages that may help initial bringup, when
46 * board setup or driver bugs are most common.
47 *
48 * Otherwise, minimize overhead in what may be bitbanging codepaths.
49 */
50#ifdef DEBUG
51#define extra_checks 1
52#else
53#define extra_checks 0
54#endif
55
ff2b1359
LW
56/* Device and char device-related information */
57static DEFINE_IDA(gpio_ida);
3c702e99
LW
58static dev_t gpio_devt;
59#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
60static struct bus_type gpio_bus_type = {
61 .name = "gpio",
62};
ff2b1359 63
d2876d08
DB
64/* gpio_lock prevents conflicts during gpio_desc[] table updates.
65 * While any GPIO is requested, its gpio_chip is not removable;
66 * each GPIO's "requested" flag serves as a lock and refcount.
67 */
0eb4c6c2 68DEFINE_SPINLOCK(gpio_lock);
d2876d08 69
bae48da2
AC
70static DEFINE_MUTEX(gpio_lookup_lock);
71static LIST_HEAD(gpio_lookup_list);
ff2b1359 72LIST_HEAD(gpio_devices);
6d86750c
JH
73
74static void gpiochip_free_hogs(struct gpio_chip *chip);
e0d89728 75static int gpiochip_add_irqchip(struct gpio_chip *gpiochip);
6d86750c 76static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
79b804cb
MW
77static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
78static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
6d86750c 79
159f3cd9 80static bool gpiolib_initialized;
6d86750c 81
d2876d08
DB
82static inline void desc_set_label(struct gpio_desc *d, const char *label)
83{
d2876d08 84 d->label = label;
d2876d08
DB
85}
86
372e722e 87/**
950d55f5
TR
88 * gpio_to_desc - Convert a GPIO number to its descriptor
89 * @gpio: global GPIO number
90 *
91 * Returns:
92 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
93 * with the given number exists in the system.
372e722e 94 */
79a9becd 95struct gpio_desc *gpio_to_desc(unsigned gpio)
372e722e 96{
ff2b1359 97 struct gpio_device *gdev;
14e85c0e
AC
98 unsigned long flags;
99
100 spin_lock_irqsave(&gpio_lock, flags);
101
ff2b1359 102 list_for_each_entry(gdev, &gpio_devices, list) {
fdeb8e15
LW
103 if (gdev->base <= gpio &&
104 gdev->base + gdev->ngpio > gpio) {
14e85c0e 105 spin_unlock_irqrestore(&gpio_lock, flags);
fdeb8e15 106 return &gdev->descs[gpio - gdev->base];
14e85c0e
AC
107 }
108 }
109
110 spin_unlock_irqrestore(&gpio_lock, flags);
111
0e9a5edf
AC
112 if (!gpio_is_valid(gpio))
113 WARN(1, "invalid GPIO %d\n", gpio);
114
14e85c0e 115 return NULL;
372e722e 116}
79a9becd 117EXPORT_SYMBOL_GPL(gpio_to_desc);
372e722e 118
d468bf9e 119/**
950d55f5
TR
120 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
121 * hardware number for this chip
122 * @chip: GPIO chip
123 * @hwnum: hardware number of the GPIO for this chip
124 *
125 * Returns:
126 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
127 * in the given chip for the specified hardware number.
d468bf9e 128 */
bb1e88cc
AC
129struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
130 u16 hwnum)
d468bf9e 131{
fdeb8e15
LW
132 struct gpio_device *gdev = chip->gpiodev;
133
134 if (hwnum >= gdev->ngpio)
b7d0a28a 135 return ERR_PTR(-EINVAL);
d468bf9e 136
fdeb8e15 137 return &gdev->descs[hwnum];
d468bf9e 138}
372e722e
AC
139
140/**
950d55f5
TR
141 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
142 * @desc: GPIO descriptor
143 *
372e722e 144 * This should disappear in the future but is needed since we still
950d55f5
TR
145 * use GPIO numbers for error messages and sysfs nodes.
146 *
147 * Returns:
148 * The global GPIO number for the GPIO specified by its descriptor.
372e722e 149 */
79a9becd 150int desc_to_gpio(const struct gpio_desc *desc)
372e722e 151{
fdeb8e15 152 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
372e722e 153}
79a9becd 154EXPORT_SYMBOL_GPL(desc_to_gpio);
372e722e
AC
155
156
79a9becd
AC
157/**
158 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
159 * @desc: descriptor to return the chip of
160 */
161struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
372e722e 162{
fdeb8e15
LW
163 if (!desc || !desc->gdev || !desc->gdev->chip)
164 return NULL;
165 return desc->gdev->chip;
372e722e 166}
79a9becd 167EXPORT_SYMBOL_GPL(gpiod_to_chip);
d2876d08 168
8d0aab2f
AV
169/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
170static int gpiochip_find_base(int ngpio)
171{
ff2b1359 172 struct gpio_device *gdev;
83cabe33 173 int base = ARCH_NR_GPIOS - ngpio;
8d0aab2f 174
ff2b1359 175 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
83cabe33 176 /* found a free space? */
fdeb8e15 177 if (gdev->base + gdev->ngpio <= base)
83cabe33
AC
178 break;
179 else
180 /* nope, check the space right before the chip */
fdeb8e15 181 base = gdev->base - ngpio;
8d0aab2f
AV
182 }
183
83cabe33 184 if (gpio_is_valid(base)) {
8d0aab2f 185 pr_debug("%s: found new base at %d\n", __func__, base);
83cabe33
AC
186 return base;
187 } else {
188 pr_err("%s: cannot find free range\n", __func__);
189 return -ENOSPC;
169b6a7a 190 }
169b6a7a
AV
191}
192
79a9becd
AC
193/**
194 * gpiod_get_direction - return the current direction of a GPIO
195 * @desc: GPIO to get the direction of
196 *
197 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
198 *
199 * This function may sleep if gpiod_cansleep() is true.
200 */
8e53b0f1 201int gpiod_get_direction(struct gpio_desc *desc)
80b0a602
MN
202{
203 struct gpio_chip *chip;
372e722e 204 unsigned offset;
80b0a602
MN
205 int status = -EINVAL;
206
372e722e
AC
207 chip = gpiod_to_chip(desc);
208 offset = gpio_chip_hwgpio(desc);
80b0a602
MN
209
210 if (!chip->get_direction)
211 return status;
212
372e722e 213 status = chip->get_direction(chip, offset);
80b0a602
MN
214 if (status > 0) {
215 /* GPIOF_DIR_IN, or other positive */
216 status = 1;
8e53b0f1 217 clear_bit(FLAG_IS_OUT, &desc->flags);
80b0a602
MN
218 }
219 if (status == 0) {
220 /* GPIOF_DIR_OUT */
8e53b0f1 221 set_bit(FLAG_IS_OUT, &desc->flags);
80b0a602
MN
222 }
223 return status;
224}
79a9becd 225EXPORT_SYMBOL_GPL(gpiod_get_direction);
80b0a602 226
1a989d0f
AC
227/*
228 * Add a new chip to the global chips list, keeping the list of chips sorted
ef7c7553 229 * by range(means [base, base + ngpio - 1]) order.
1a989d0f
AC
230 *
231 * Return -EBUSY if the new chip overlaps with some other chip's integer
232 * space.
233 */
ff2b1359 234static int gpiodev_add_to_list(struct gpio_device *gdev)
1a989d0f 235{
a961f9b4 236 struct gpio_device *prev, *next;
1a989d0f 237
ff2b1359 238 if (list_empty(&gpio_devices)) {
a961f9b4 239 /* initial entry in list */
ff2b1359 240 list_add_tail(&gdev->list, &gpio_devices);
e28ecca6 241 return 0;
1a989d0f
AC
242 }
243
a961f9b4
BJZ
244 next = list_entry(gpio_devices.next, struct gpio_device, list);
245 if (gdev->base + gdev->ngpio <= next->base) {
246 /* add before first entry */
247 list_add(&gdev->list, &gpio_devices);
248 return 0;
1a989d0f
AC
249 }
250
a961f9b4
BJZ
251 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
252 if (prev->base + prev->ngpio <= gdev->base) {
253 /* add behind last entry */
254 list_add_tail(&gdev->list, &gpio_devices);
96098df1 255 return 0;
1a989d0f
AC
256 }
257
a961f9b4
BJZ
258 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
259 /* at the end of the list */
260 if (&next->list == &gpio_devices)
261 break;
1a989d0f 262
a961f9b4
BJZ
263 /* add between prev and next */
264 if (prev->base + prev->ngpio <= gdev->base
265 && gdev->base + gdev->ngpio <= next->base) {
266 list_add(&gdev->list, &prev->list);
267 return 0;
268 }
269 }
270
271 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
272 return -EBUSY;
1a989d0f
AC
273}
274
950d55f5 275/*
f881bab0
LW
276 * Convert a GPIO name to its descriptor
277 */
278static struct gpio_desc *gpio_name_to_desc(const char * const name)
279{
ff2b1359 280 struct gpio_device *gdev;
f881bab0
LW
281 unsigned long flags;
282
283 spin_lock_irqsave(&gpio_lock, flags);
284
ff2b1359 285 list_for_each_entry(gdev, &gpio_devices, list) {
f881bab0
LW
286 int i;
287
fdeb8e15
LW
288 for (i = 0; i != gdev->ngpio; ++i) {
289 struct gpio_desc *desc = &gdev->descs[i];
f881bab0 290
fdeb8e15 291 if (!desc->name || !name)
f881bab0
LW
292 continue;
293
fdeb8e15 294 if (!strcmp(desc->name, name)) {
f881bab0 295 spin_unlock_irqrestore(&gpio_lock, flags);
fdeb8e15 296 return desc;
f881bab0
LW
297 }
298 }
299 }
300
301 spin_unlock_irqrestore(&gpio_lock, flags);
302
303 return NULL;
304}
305
5f3ca732
MP
306/*
307 * Takes the names from gc->names and checks if they are all unique. If they
308 * are, they are assigned to their gpio descriptors.
309 *
ed37915c 310 * Warning if one of the names is already used for a different GPIO.
5f3ca732
MP
311 */
312static int gpiochip_set_desc_names(struct gpio_chip *gc)
313{
fdeb8e15 314 struct gpio_device *gdev = gc->gpiodev;
5f3ca732
MP
315 int i;
316
317 if (!gc->names)
318 return 0;
319
320 /* First check all names if they are unique */
321 for (i = 0; i != gc->ngpio; ++i) {
322 struct gpio_desc *gpio;
323
324 gpio = gpio_name_to_desc(gc->names[i]);
f881bab0 325 if (gpio)
fdeb8e15 326 dev_warn(&gdev->dev,
34ffd85d 327 "Detected name collision for GPIO name '%s'\n",
f881bab0 328 gc->names[i]);
5f3ca732
MP
329 }
330
331 /* Then add all names to the GPIO descriptors */
332 for (i = 0; i != gc->ngpio; ++i)
fdeb8e15 333 gdev->descs[i].name = gc->names[i];
5f3ca732
MP
334
335 return 0;
336}
337
d7c51b47
LW
338/*
339 * GPIO line handle management
340 */
341
342/**
343 * struct linehandle_state - contains the state of a userspace handle
344 * @gdev: the GPIO device the handle pertains to
345 * @label: consumer label used to tag descriptors
346 * @descs: the GPIO descriptors held by this handle
347 * @numdescs: the number of descriptors held in the descs array
348 */
349struct linehandle_state {
350 struct gpio_device *gdev;
351 const char *label;
352 struct gpio_desc *descs[GPIOHANDLES_MAX];
353 u32 numdescs;
354};
355
e3e847c7
LPC
356#define GPIOHANDLE_REQUEST_VALID_FLAGS \
357 (GPIOHANDLE_REQUEST_INPUT | \
358 GPIOHANDLE_REQUEST_OUTPUT | \
359 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
360 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
361 GPIOHANDLE_REQUEST_OPEN_SOURCE)
362
d7c51b47
LW
363static long linehandle_ioctl(struct file *filep, unsigned int cmd,
364 unsigned long arg)
365{
366 struct linehandle_state *lh = filep->private_data;
367 void __user *ip = (void __user *)arg;
368 struct gpiohandle_data ghd;
eec1d566 369 int vals[GPIOHANDLES_MAX];
d7c51b47
LW
370 int i;
371
372 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
eec1d566
LW
373 /* TODO: check if descriptors are really input */
374 int ret = gpiod_get_array_value_complex(false,
375 true,
376 lh->numdescs,
377 lh->descs,
378 vals);
379 if (ret)
380 return ret;
d7c51b47 381
3eded5d8 382 memset(&ghd, 0, sizeof(ghd));
eec1d566
LW
383 for (i = 0; i < lh->numdescs; i++)
384 ghd.values[i] = vals[i];
d7c51b47
LW
385
386 if (copy_to_user(ip, &ghd, sizeof(ghd)))
387 return -EFAULT;
388
389 return 0;
390 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
d7c51b47
LW
391 /* TODO: check if descriptors are really output */
392 if (copy_from_user(&ghd, ip, sizeof(ghd)))
393 return -EFAULT;
394
395 /* Clamp all values to [0,1] */
396 for (i = 0; i < lh->numdescs; i++)
397 vals[i] = !!ghd.values[i];
398
399 /* Reuse the array setting function */
400 gpiod_set_array_value_complex(false,
401 true,
402 lh->numdescs,
403 lh->descs,
404 vals);
405 return 0;
406 }
407 return -EINVAL;
408}
409
410#ifdef CONFIG_COMPAT
411static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
412 unsigned long arg)
413{
414 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
415}
416#endif
417
418static int linehandle_release(struct inode *inode, struct file *filep)
419{
420 struct linehandle_state *lh = filep->private_data;
421 struct gpio_device *gdev = lh->gdev;
422 int i;
423
424 for (i = 0; i < lh->numdescs; i++)
425 gpiod_free(lh->descs[i]);
426 kfree(lh->label);
427 kfree(lh);
428 put_device(&gdev->dev);
429 return 0;
430}
431
432static const struct file_operations linehandle_fileops = {
433 .release = linehandle_release,
434 .owner = THIS_MODULE,
435 .llseek = noop_llseek,
436 .unlocked_ioctl = linehandle_ioctl,
437#ifdef CONFIG_COMPAT
438 .compat_ioctl = linehandle_ioctl_compat,
439#endif
440};
441
442static int linehandle_create(struct gpio_device *gdev, void __user *ip)
443{
444 struct gpiohandle_request handlereq;
445 struct linehandle_state *lh;
953b956a 446 struct file *file;
d7c51b47 447 int fd, i, ret;
418ee8e9 448 u32 lflags;
d7c51b47
LW
449
450 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
451 return -EFAULT;
452 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
453 return -EINVAL;
454
418ee8e9
BG
455 lflags = handlereq.flags;
456
457 /* Return an error if an unknown flag is set */
458 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
459 return -EINVAL;
460
609aaf6a
BG
461 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
462 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
463 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
464 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
465 return -EINVAL;
466
d7c51b47
LW
467 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
468 if (!lh)
469 return -ENOMEM;
470 lh->gdev = gdev;
471 get_device(&gdev->dev);
472
473 /* Make sure this is terminated */
474 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
475 if (strlen(handlereq.consumer_label)) {
476 lh->label = kstrdup(handlereq.consumer_label,
477 GFP_KERNEL);
478 if (!lh->label) {
479 ret = -ENOMEM;
480 goto out_free_lh;
481 }
482 }
483
484 /* Request each GPIO */
485 for (i = 0; i < handlereq.lines; i++) {
486 u32 offset = handlereq.lineoffsets[i];
d7c51b47
LW
487 struct gpio_desc *desc;
488
e405f9fc
LPC
489 if (offset >= gdev->ngpio) {
490 ret = -EINVAL;
491 goto out_free_descs;
492 }
493
d7c51b47
LW
494 desc = &gdev->descs[offset];
495 ret = gpiod_request(desc, lh->label);
496 if (ret)
497 goto out_free_descs;
498 lh->descs[i] = desc;
499
500 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
501 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
502 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
503 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
504 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
505 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
506
507 /*
508 * Lines have to be requested explicitly for input
509 * or output, else the line will be treated "as is".
510 */
511 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
512 int val = !!handlereq.default_values[i];
513
514 ret = gpiod_direction_output(desc, val);
515 if (ret)
516 goto out_free_descs;
517 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
518 ret = gpiod_direction_input(desc);
519 if (ret)
520 goto out_free_descs;
521 }
522 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
523 offset);
524 }
e2f608be
LW
525 /* Let i point at the last handle */
526 i--;
d7c51b47
LW
527 lh->numdescs = handlereq.lines;
528
953b956a 529 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
d7c51b47
LW
530 if (fd < 0) {
531 ret = fd;
532 goto out_free_descs;
533 }
534
953b956a
LPC
535 file = anon_inode_getfile("gpio-linehandle",
536 &linehandle_fileops,
537 lh,
538 O_RDONLY | O_CLOEXEC);
539 if (IS_ERR(file)) {
540 ret = PTR_ERR(file);
541 goto out_put_unused_fd;
542 }
543
d7c51b47 544 handlereq.fd = fd;
d932cd49 545 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
953b956a
LPC
546 /*
547 * fput() will trigger the release() callback, so do not go onto
548 * the regular error cleanup path here.
549 */
550 fput(file);
551 put_unused_fd(fd);
552 return -EFAULT;
d932cd49 553 }
d7c51b47 554
953b956a
LPC
555 fd_install(fd, file);
556
d7c51b47
LW
557 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
558 lh->numdescs);
559
560 return 0;
561
953b956a
LPC
562out_put_unused_fd:
563 put_unused_fd(fd);
d7c51b47
LW
564out_free_descs:
565 for (; i >= 0; i--)
566 gpiod_free(lh->descs[i]);
567 kfree(lh->label);
568out_free_lh:
569 kfree(lh);
570 put_device(&gdev->dev);
571 return ret;
572}
573
61f922db
LW
574/*
575 * GPIO line event management
576 */
577
578/**
579 * struct lineevent_state - contains the state of a userspace event
580 * @gdev: the GPIO device the event pertains to
581 * @label: consumer label used to tag descriptors
582 * @desc: the GPIO descriptor held by this event
583 * @eflags: the event flags this line was requested with
584 * @irq: the interrupt that trigger in response to events on this GPIO
585 * @wait: wait queue that handles blocking reads of events
586 * @events: KFIFO for the GPIO events
587 * @read_lock: mutex lock to protect reads from colliding with adding
588 * new events to the FIFO
589 */
590struct lineevent_state {
591 struct gpio_device *gdev;
592 const char *label;
593 struct gpio_desc *desc;
594 u32 eflags;
595 int irq;
596 wait_queue_head_t wait;
597 DECLARE_KFIFO(events, struct gpioevent_data, 16);
598 struct mutex read_lock;
599};
600
ac7dbb99
LPC
601#define GPIOEVENT_REQUEST_VALID_FLAGS \
602 (GPIOEVENT_REQUEST_RISING_EDGE | \
603 GPIOEVENT_REQUEST_FALLING_EDGE)
604
61f922db
LW
605static unsigned int lineevent_poll(struct file *filep,
606 struct poll_table_struct *wait)
607{
608 struct lineevent_state *le = filep->private_data;
609 unsigned int events = 0;
610
611 poll_wait(filep, &le->wait, wait);
612
613 if (!kfifo_is_empty(&le->events))
614 events = POLLIN | POLLRDNORM;
615
616 return events;
617}
618
619
620static ssize_t lineevent_read(struct file *filep,
621 char __user *buf,
622 size_t count,
623 loff_t *f_ps)
624{
625 struct lineevent_state *le = filep->private_data;
626 unsigned int copied;
627 int ret;
628
629 if (count < sizeof(struct gpioevent_data))
630 return -EINVAL;
631
632 do {
633 if (kfifo_is_empty(&le->events)) {
634 if (filep->f_flags & O_NONBLOCK)
635 return -EAGAIN;
636
637 ret = wait_event_interruptible(le->wait,
638 !kfifo_is_empty(&le->events));
639 if (ret)
640 return ret;
641 }
642
643 if (mutex_lock_interruptible(&le->read_lock))
644 return -ERESTARTSYS;
645 ret = kfifo_to_user(&le->events, buf, count, &copied);
646 mutex_unlock(&le->read_lock);
647
648 if (ret)
649 return ret;
650
651 /*
652 * If we couldn't read anything from the fifo (a different
653 * thread might have been faster) we either return -EAGAIN if
654 * the file descriptor is non-blocking, otherwise we go back to
655 * sleep and wait for more data to arrive.
656 */
657 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
658 return -EAGAIN;
659
660 } while (copied == 0);
661
662 return copied;
663}
664
665static int lineevent_release(struct inode *inode, struct file *filep)
666{
667 struct lineevent_state *le = filep->private_data;
668 struct gpio_device *gdev = le->gdev;
669
670 free_irq(le->irq, le);
671 gpiod_free(le->desc);
672 kfree(le->label);
673 kfree(le);
674 put_device(&gdev->dev);
675 return 0;
676}
677
678static long lineevent_ioctl(struct file *filep, unsigned int cmd,
679 unsigned long arg)
680{
681 struct lineevent_state *le = filep->private_data;
682 void __user *ip = (void __user *)arg;
683 struct gpiohandle_data ghd;
684
685 /*
686 * We can get the value for an event line but not set it,
687 * because it is input by definition.
688 */
689 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
690 int val;
691
d82aa4a8
LPC
692 memset(&ghd, 0, sizeof(ghd));
693
61f922db
LW
694 val = gpiod_get_value_cansleep(le->desc);
695 if (val < 0)
696 return val;
697 ghd.values[0] = val;
698
699 if (copy_to_user(ip, &ghd, sizeof(ghd)))
700 return -EFAULT;
701
702 return 0;
703 }
704 return -EINVAL;
705}
706
707#ifdef CONFIG_COMPAT
708static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
709 unsigned long arg)
710{
711 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
712}
713#endif
714
715static const struct file_operations lineevent_fileops = {
716 .release = lineevent_release,
717 .read = lineevent_read,
718 .poll = lineevent_poll,
719 .owner = THIS_MODULE,
720 .llseek = noop_llseek,
721 .unlocked_ioctl = lineevent_ioctl,
722#ifdef CONFIG_COMPAT
723 .compat_ioctl = lineevent_ioctl_compat,
724#endif
725};
726
33265b17 727static irqreturn_t lineevent_irq_thread(int irq, void *p)
61f922db
LW
728{
729 struct lineevent_state *le = p;
730 struct gpioevent_data ge;
df1e76f2 731 int ret, level;
61f922db
LW
732
733 ge.timestamp = ktime_get_real_ns();
df1e76f2 734 level = gpiod_get_value_cansleep(le->desc);
61f922db 735
ad537b82
BG
736 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
737 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
61f922db
LW
738 if (level)
739 /* Emit low-to-high event */
740 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
741 else
742 /* Emit high-to-low event */
743 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
df1e76f2 744 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
61f922db
LW
745 /* Emit low-to-high event */
746 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
df1e76f2 747 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
61f922db
LW
748 /* Emit high-to-low event */
749 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
bc0207a5
AB
750 } else {
751 return IRQ_NONE;
61f922db
LW
752 }
753
754 ret = kfifo_put(&le->events, ge);
755 if (ret != 0)
756 wake_up_poll(&le->wait, POLLIN);
757
758 return IRQ_HANDLED;
759}
760
761static int lineevent_create(struct gpio_device *gdev, void __user *ip)
762{
763 struct gpioevent_request eventreq;
764 struct lineevent_state *le;
765 struct gpio_desc *desc;
953b956a 766 struct file *file;
61f922db
LW
767 u32 offset;
768 u32 lflags;
769 u32 eflags;
770 int fd;
771 int ret;
772 int irqflags = 0;
773
774 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
775 return -EFAULT;
776
777 le = kzalloc(sizeof(*le), GFP_KERNEL);
778 if (!le)
779 return -ENOMEM;
780 le->gdev = gdev;
781 get_device(&gdev->dev);
782
783 /* Make sure this is terminated */
784 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
785 if (strlen(eventreq.consumer_label)) {
786 le->label = kstrdup(eventreq.consumer_label,
787 GFP_KERNEL);
788 if (!le->label) {
789 ret = -ENOMEM;
790 goto out_free_le;
791 }
792 }
793
794 offset = eventreq.lineoffset;
795 lflags = eventreq.handleflags;
796 eflags = eventreq.eventflags;
797
b8b0e3d3
LPC
798 if (offset >= gdev->ngpio) {
799 ret = -EINVAL;
800 goto out_free_label;
801 }
802
ac7dbb99
LPC
803 /* Return an error if a unknown flag is set */
804 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
805 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
806 ret = -EINVAL;
807 goto out_free_label;
808 }
809
61f922db
LW
810 /* This is just wrong: we don't look for events on output lines */
811 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
812 ret = -EINVAL;
813 goto out_free_label;
814 }
815
816 desc = &gdev->descs[offset];
817 ret = gpiod_request(desc, le->label);
818 if (ret)
819 goto out_free_desc;
820 le->desc = desc;
821 le->eflags = eflags;
822
823 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
824 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
825 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
826 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
827 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
828 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
829
830 ret = gpiod_direction_input(desc);
831 if (ret)
832 goto out_free_desc;
833
834 le->irq = gpiod_to_irq(desc);
835 if (le->irq <= 0) {
836 ret = -ENODEV;
837 goto out_free_desc;
838 }
839
840 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
841 irqflags |= IRQF_TRIGGER_RISING;
842 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
843 irqflags |= IRQF_TRIGGER_FALLING;
844 irqflags |= IRQF_ONESHOT;
845 irqflags |= IRQF_SHARED;
846
847 INIT_KFIFO(le->events);
848 init_waitqueue_head(&le->wait);
849 mutex_init(&le->read_lock);
850
851 /* Request a thread to read the events */
852 ret = request_threaded_irq(le->irq,
853 NULL,
854 lineevent_irq_thread,
855 irqflags,
856 le->label,
857 le);
858 if (ret)
859 goto out_free_desc;
860
953b956a 861 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
61f922db
LW
862 if (fd < 0) {
863 ret = fd;
864 goto out_free_irq;
865 }
866
953b956a
LPC
867 file = anon_inode_getfile("gpio-event",
868 &lineevent_fileops,
869 le,
870 O_RDONLY | O_CLOEXEC);
871 if (IS_ERR(file)) {
872 ret = PTR_ERR(file);
873 goto out_put_unused_fd;
874 }
875
61f922db 876 eventreq.fd = fd;
d932cd49 877 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
953b956a
LPC
878 /*
879 * fput() will trigger the release() callback, so do not go onto
880 * the regular error cleanup path here.
881 */
882 fput(file);
883 put_unused_fd(fd);
884 return -EFAULT;
d932cd49 885 }
61f922db 886
953b956a
LPC
887 fd_install(fd, file);
888
61f922db
LW
889 return 0;
890
953b956a
LPC
891out_put_unused_fd:
892 put_unused_fd(fd);
61f922db
LW
893out_free_irq:
894 free_irq(le->irq, le);
895out_free_desc:
896 gpiod_free(le->desc);
897out_free_label:
898 kfree(le->label);
899out_free_le:
900 kfree(le);
901 put_device(&gdev->dev);
902 return ret;
903}
904
950d55f5 905/*
3c702e99
LW
906 * gpio_ioctl() - ioctl handler for the GPIO chardev
907 */
908static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
909{
910 struct gpio_device *gdev = filp->private_data;
911 struct gpio_chip *chip = gdev->chip;
8b92e17e 912 void __user *ip = (void __user *)arg;
3c702e99
LW
913
914 /* We fail any subsequent ioctl():s when the chip is gone */
915 if (!chip)
916 return -ENODEV;
917
521a2ad6 918 /* Fill in the struct and pass to userspace */
3c702e99 919 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
521a2ad6
LW
920 struct gpiochip_info chipinfo;
921
0f4bbb23
LPC
922 memset(&chipinfo, 0, sizeof(chipinfo));
923
3c702e99
LW
924 strncpy(chipinfo.name, dev_name(&gdev->dev),
925 sizeof(chipinfo.name));
926 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
df4878e9
LW
927 strncpy(chipinfo.label, gdev->label,
928 sizeof(chipinfo.label));
929 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
fdeb8e15 930 chipinfo.lines = gdev->ngpio;
3c702e99
LW
931 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
932 return -EFAULT;
933 return 0;
521a2ad6
LW
934 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
935 struct gpioline_info lineinfo;
936 struct gpio_desc *desc;
937
938 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
939 return -EFAULT;
1f1cc456 940 if (lineinfo.line_offset >= gdev->ngpio)
521a2ad6
LW
941 return -EINVAL;
942
943 desc = &gdev->descs[lineinfo.line_offset];
944 if (desc->name) {
945 strncpy(lineinfo.name, desc->name,
946 sizeof(lineinfo.name));
947 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
948 } else {
949 lineinfo.name[0] = '\0';
950 }
951 if (desc->label) {
214338e3
LW
952 strncpy(lineinfo.consumer, desc->label,
953 sizeof(lineinfo.consumer));
954 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
521a2ad6 955 } else {
214338e3 956 lineinfo.consumer[0] = '\0';
521a2ad6
LW
957 }
958
959 /*
960 * Userspace only need to know that the kernel is using
961 * this GPIO so it can't use it.
962 */
963 lineinfo.flags = 0;
9d8cc89c
LW
964 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
965 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
966 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
967 test_bit(FLAG_EXPORT, &desc->flags) ||
968 test_bit(FLAG_SYSFS, &desc->flags))
521a2ad6 969 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
9d8cc89c 970 if (test_bit(FLAG_IS_OUT, &desc->flags))
521a2ad6 971 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
9d8cc89c 972 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
521a2ad6 973 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
9d8cc89c 974 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
521a2ad6 975 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
9d8cc89c 976 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
521a2ad6
LW
977 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
978
979 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
980 return -EFAULT;
981 return 0;
d7c51b47
LW
982 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
983 return linehandle_create(gdev, ip);
61f922db
LW
984 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
985 return lineevent_create(gdev, ip);
3c702e99
LW
986 }
987 return -EINVAL;
988}
989
8b92e17e
LW
990#ifdef CONFIG_COMPAT
991static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
992 unsigned long arg)
993{
994 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
995}
996#endif
997
3c702e99
LW
998/**
999 * gpio_chrdev_open() - open the chardev for ioctl operations
1000 * @inode: inode for this chardev
1001 * @filp: file struct for storing private data
1002 * Returns 0 on success
1003 */
1004static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1005{
1006 struct gpio_device *gdev = container_of(inode->i_cdev,
1007 struct gpio_device, chrdev);
1008
1009 /* Fail on open if the backing gpiochip is gone */
fb505747 1010 if (!gdev->chip)
3c702e99
LW
1011 return -ENODEV;
1012 get_device(&gdev->dev);
1013 filp->private_data = gdev;
f4e81c52
LPC
1014
1015 return nonseekable_open(inode, filp);
3c702e99
LW
1016}
1017
1018/**
1019 * gpio_chrdev_release() - close chardev after ioctl operations
1020 * @inode: inode for this chardev
1021 * @filp: file struct for storing private data
1022 * Returns 0 on success
1023 */
1024static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1025{
1026 struct gpio_device *gdev = container_of(inode->i_cdev,
1027 struct gpio_device, chrdev);
1028
3c702e99
LW
1029 put_device(&gdev->dev);
1030 return 0;
1031}
1032
1033
1034static const struct file_operations gpio_fileops = {
1035 .release = gpio_chrdev_release,
1036 .open = gpio_chrdev_open,
1037 .owner = THIS_MODULE,
f4e81c52 1038 .llseek = no_llseek,
3c702e99 1039 .unlocked_ioctl = gpio_ioctl,
8b92e17e
LW
1040#ifdef CONFIG_COMPAT
1041 .compat_ioctl = gpio_ioctl_compat,
1042#endif
3c702e99
LW
1043};
1044
ff2b1359
LW
1045static void gpiodevice_release(struct device *dev)
1046{
1047 struct gpio_device *gdev = dev_get_drvdata(dev);
1048
1049 list_del(&gdev->list);
1050 ida_simple_remove(&gpio_ida, gdev->id);
476e2fc5
GR
1051 kfree(gdev->label);
1052 kfree(gdev->descs);
9efd9e69 1053 kfree(gdev);
ff2b1359
LW
1054}
1055
159f3cd9
GR
1056static int gpiochip_setup_dev(struct gpio_device *gdev)
1057{
1058 int status;
1059
1060 cdev_init(&gdev->chrdev, &gpio_fileops);
1061 gdev->chrdev.owner = THIS_MODULE;
159f3cd9 1062 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
111379dc
LG
1063
1064 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
159f3cd9 1065 if (status)
111379dc
LG
1066 return status;
1067
1068 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1069 MAJOR(gpio_devt), gdev->id);
159f3cd9
GR
1070
1071 status = gpiochip_sysfs_register(gdev);
1072 if (status)
1073 goto err_remove_device;
1074
1075 /* From this point, the .release() function cleans up gpio_device */
1076 gdev->dev.release = gpiodevice_release;
159f3cd9
GR
1077 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1078 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1079 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1080
1081 return 0;
1082
1083err_remove_device:
111379dc 1084 cdev_device_del(&gdev->chrdev, &gdev->dev);
159f3cd9
GR
1085 return status;
1086}
1087
1088static void gpiochip_setup_devs(void)
1089{
1090 struct gpio_device *gdev;
1091 int err;
1092
1093 list_for_each_entry(gdev, &gpio_devices, list) {
1094 err = gpiochip_setup_dev(gdev);
1095 if (err)
1096 pr_err("%s: Failed to initialize gpio device (%d)\n",
1097 dev_name(&gdev->dev), err);
1098 }
1099}
1100
d2876d08 1101/**
b08ea35a 1102 * gpiochip_add_data() - register a gpio_chip
d2876d08 1103 * @chip: the chip to register, with chip->base initialized
950d55f5 1104 * @data: driver-private data associated with this chip
d2876d08 1105 *
950d55f5 1106 * Context: potentially before irqs will work
8d0aab2f 1107 *
b08ea35a 1108 * When gpiochip_add_data() is called very early during boot, so that GPIOs
c88402c2 1109 * can be freely used, the chip->parent device must be registered before
d8f388d8
DB
1110 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1111 * for GPIOs will fail rudely.
1112 *
159f3cd9
GR
1113 * gpiochip_add_data() must only be called after gpiolib initialization,
1114 * ie after core_initcall().
1115 *
8d0aab2f
AV
1116 * If chip->base is negative, this requests dynamic assignment of
1117 * a range of valid GPIOs.
950d55f5
TR
1118 *
1119 * Returns:
1120 * A negative errno if the chip can't be registered, such as because the
1121 * chip->base is invalid or already associated with a different chip.
1122 * Otherwise it returns zero as a success code.
d2876d08 1123 */
b08ea35a 1124int gpiochip_add_data(struct gpio_chip *chip, void *data)
d2876d08
DB
1125{
1126 unsigned long flags;
1127 int status = 0;
ff2b1359 1128 unsigned i;
8d0aab2f 1129 int base = chip->base;
ff2b1359 1130 struct gpio_device *gdev;
d2876d08 1131
ff2b1359
LW
1132 /*
1133 * First: allocate and populate the internal stat container, and
1134 * set up the struct device.
1135 */
969f07b4 1136 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
ff2b1359 1137 if (!gdev)
14e85c0e 1138 return -ENOMEM;
3c702e99 1139 gdev->dev.bus = &gpio_bus_type;
ff2b1359
LW
1140 gdev->chip = chip;
1141 chip->gpiodev = gdev;
1142 if (chip->parent) {
1143 gdev->dev.parent = chip->parent;
1144 gdev->dev.of_node = chip->parent->of_node;
acc6e331
TR
1145 }
1146
ff2b1359
LW
1147#ifdef CONFIG_OF_GPIO
1148 /* If the gpiochip has an assigned OF node this takes precedence */
acc6e331
TR
1149 if (chip->of_node)
1150 gdev->dev.of_node = chip->of_node;
ff2b1359 1151#endif
acc6e331 1152
ff2b1359
LW
1153 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1154 if (gdev->id < 0) {
1155 status = gdev->id;
1156 goto err_free_gdev;
1157 }
1158 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1159 device_initialize(&gdev->dev);
1160 dev_set_drvdata(&gdev->dev, gdev);
1161 if (chip->parent && chip->parent->driver)
1162 gdev->owner = chip->parent->driver->owner;
1163 else if (chip->owner)
1164 /* TODO: remove chip->owner */
1165 gdev->owner = chip->owner;
1166 else
1167 gdev->owner = THIS_MODULE;
d2876d08 1168
476e2fc5 1169 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
1c3cdb18 1170 if (!gdev->descs) {
ff2b1359
LW
1171 status = -ENOMEM;
1172 goto err_free_gdev;
1173 }
1174
5ed41cc4
BJZ
1175 if (chip->ngpio == 0) {
1176 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
ff2b1359 1177 status = -EINVAL;
159f3cd9 1178 goto err_free_descs;
5ed41cc4 1179 }
df4878e9
LW
1180
1181 if (chip->label)
476e2fc5 1182 gdev->label = kstrdup(chip->label, GFP_KERNEL);
df4878e9 1183 else
476e2fc5 1184 gdev->label = kstrdup("unknown", GFP_KERNEL);
df4878e9
LW
1185 if (!gdev->label) {
1186 status = -ENOMEM;
476e2fc5 1187 goto err_free_descs;
df4878e9
LW
1188 }
1189
fdeb8e15 1190 gdev->ngpio = chip->ngpio;
43c54eca 1191 gdev->data = data;
5ed41cc4 1192
d2876d08
DB
1193 spin_lock_irqsave(&gpio_lock, flags);
1194
fdeb8e15
LW
1195 /*
1196 * TODO: this allocates a Linux GPIO number base in the global
1197 * GPIO numberspace for this chip. In the long run we want to
1198 * get *rid* of this numberspace and use only descriptors, but
1199 * it may be a pipe dream. It will not happen before we get rid
1200 * of the sysfs interface anyways.
1201 */
8d0aab2f
AV
1202 if (base < 0) {
1203 base = gpiochip_find_base(chip->ngpio);
1204 if (base < 0) {
1205 status = base;
225fce83 1206 spin_unlock_irqrestore(&gpio_lock, flags);
476e2fc5 1207 goto err_free_label;
8d0aab2f 1208 }
fdeb8e15
LW
1209 /*
1210 * TODO: it should not be necessary to reflect the assigned
1211 * base outside of the GPIO subsystem. Go over drivers and
1212 * see if anyone makes use of this, else drop this and assign
1213 * a poison instead.
1214 */
8d0aab2f
AV
1215 chip->base = base;
1216 }
fdeb8e15 1217 gdev->base = base;
8d0aab2f 1218
ff2b1359 1219 status = gpiodev_add_to_list(gdev);
05aa5203
JH
1220 if (status) {
1221 spin_unlock_irqrestore(&gpio_lock, flags);
476e2fc5 1222 goto err_free_label;
05aa5203 1223 }
1a989d0f 1224
545ebd9a
LW
1225 spin_unlock_irqrestore(&gpio_lock, flags);
1226
ff2b1359 1227 for (i = 0; i < chip->ngpio; i++) {
1c3cdb18 1228 struct gpio_desc *desc = &gdev->descs[i];
05aa5203 1229
fdeb8e15 1230 desc->gdev = gdev;
72d32000
LW
1231 /*
1232 * REVISIT: most hardware initializes GPIOs as inputs
1233 * (often with pullups enabled) so power usage is
1234 * minimized. Linux code should set the gpio direction
1235 * first thing; but until it does, and in case
1236 * chip->get_direction is not set, we may expose the
1237 * wrong direction in sysfs.
05aa5203 1238 */
72d32000
LW
1239
1240 if (chip->get_direction) {
1241 /*
1242 * If we have .get_direction, set up the initial
1243 * direction flag from the hardware.
1244 */
1245 int dir = chip->get_direction(chip, i);
1246
1247 if (!dir)
1248 set_bit(FLAG_IS_OUT, &desc->flags);
1249 } else if (!chip->direction_input) {
1250 /*
1251 * If the chip lacks the .direction_input callback
1252 * we logically assume all lines are outputs.
1253 */
1254 set_bit(FLAG_IS_OUT, &desc->flags);
1255 }
d2876d08 1256 }
14e85c0e 1257
f23f1516 1258#ifdef CONFIG_PINCTRL
20ec3e39 1259 INIT_LIST_HEAD(&gdev->pin_ranges);
f23f1516
SH
1260#endif
1261
5f3ca732
MP
1262 status = gpiochip_set_desc_names(chip);
1263 if (status)
1264 goto err_remove_from_list;
1265
79b804cb
MW
1266 status = gpiochip_irqchip_init_valid_mask(chip);
1267 if (status)
1268 goto err_remove_from_list;
1269
e0d89728
TR
1270 status = gpiochip_add_irqchip(chip);
1271 if (status)
1272 goto err_remove_chip;
1273
28355f81
TV
1274 status = of_gpiochip_add(chip);
1275 if (status)
1276 goto err_remove_chip;
1277
664e3e5a 1278 acpi_gpiochip_add(chip);
391c970c 1279
3c702e99
LW
1280 /*
1281 * By first adding the chardev, and then adding the device,
1282 * we get a device node entry in sysfs under
1283 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1284 * coldplug of device nodes and other udev business.
159f3cd9
GR
1285 * We can do this only if gpiolib has been initialized.
1286 * Otherwise, defer until later.
3c702e99 1287 */
159f3cd9
GR
1288 if (gpiolib_initialized) {
1289 status = gpiochip_setup_dev(gdev);
1290 if (status)
1291 goto err_remove_chip;
1292 }
cedb1881 1293 return 0;
3bae4811 1294
225fce83
JH
1295err_remove_chip:
1296 acpi_gpiochip_remove(chip);
6d86750c 1297 gpiochip_free_hogs(chip);
225fce83 1298 of_gpiochip_remove(chip);
79b804cb 1299 gpiochip_irqchip_free_valid_mask(chip);
5f3ca732 1300err_remove_from_list:
225fce83 1301 spin_lock_irqsave(&gpio_lock, flags);
ff2b1359 1302 list_del(&gdev->list);
3bae4811 1303 spin_unlock_irqrestore(&gpio_lock, flags);
476e2fc5
GR
1304err_free_label:
1305 kfree(gdev->label);
1306err_free_descs:
1307 kfree(gdev->descs);
ff2b1359
LW
1308err_free_gdev:
1309 ida_simple_remove(&gpio_ida, gdev->id);
d2876d08 1310 /* failures here can mean systems won't boot... */
7589e59f 1311 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
fdeb8e15
LW
1312 gdev->base, gdev->base + gdev->ngpio - 1,
1313 chip->label ? : "generic");
1314 kfree(gdev);
d2876d08
DB
1315 return status;
1316}
b08ea35a 1317EXPORT_SYMBOL_GPL(gpiochip_add_data);
d2876d08 1318
43c54eca
LW
1319/**
1320 * gpiochip_get_data() - get per-subdriver data for the chip
950d55f5
TR
1321 * @chip: GPIO chip
1322 *
1323 * Returns:
1324 * The per-subdriver data for the chip.
43c54eca
LW
1325 */
1326void *gpiochip_get_data(struct gpio_chip *chip)
1327{
1328 return chip->gpiodev->data;
1329}
1330EXPORT_SYMBOL_GPL(gpiochip_get_data);
1331
d2876d08
DB
1332/**
1333 * gpiochip_remove() - unregister a gpio_chip
1334 * @chip: the chip to unregister
1335 *
1336 * A gpio_chip with any GPIOs still requested may not be removed.
1337 */
e1db1706 1338void gpiochip_remove(struct gpio_chip *chip)
d2876d08 1339{
ff2b1359 1340 struct gpio_device *gdev = chip->gpiodev;
fab28b89 1341 struct gpio_desc *desc;
d2876d08 1342 unsigned long flags;
1c3cdb18 1343 unsigned i;
fab28b89 1344 bool requested = false;
d2876d08 1345
ff2b1359 1346 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
afbc4f31 1347 gpiochip_sysfs_unregister(gdev);
5018ada6 1348 gpiochip_free_hogs(chip);
bd203bd5
BJZ
1349 /* Numb the device, cancelling all outstanding operations */
1350 gdev->chip = NULL;
00acc3dc 1351 gpiochip_irqchip_remove(chip);
6072b9dc 1352 acpi_gpiochip_remove(chip);
9ef0d6f7 1353 gpiochip_remove_pin_ranges(chip);
391c970c 1354 of_gpiochip_remove(chip);
43c54eca
LW
1355 /*
1356 * We accept no more calls into the driver from this point, so
1357 * NULL the driver data pointer
1358 */
1359 gdev->data = NULL;
391c970c 1360
6798acaa 1361 spin_lock_irqsave(&gpio_lock, flags);
fdeb8e15 1362 for (i = 0; i < gdev->ngpio; i++) {
1c3cdb18 1363 desc = &gdev->descs[i];
fab28b89
JH
1364 if (test_bit(FLAG_REQUESTED, &desc->flags))
1365 requested = true;
d2876d08 1366 }
d2876d08 1367 spin_unlock_irqrestore(&gpio_lock, flags);
14e85c0e 1368
fab28b89 1369 if (requested)
fdeb8e15 1370 dev_crit(&gdev->dev,
58383c78 1371 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
fab28b89 1372
ff2b1359
LW
1373 /*
1374 * The gpiochip side puts its use of the device to rest here:
1375 * if there are no userspace clients, the chardev and device will
1376 * be removed, else it will be dangling until the last user is
1377 * gone.
1378 */
111379dc 1379 cdev_device_del(&gdev->chrdev, &gdev->dev);
ff2b1359 1380 put_device(&gdev->dev);
d2876d08
DB
1381}
1382EXPORT_SYMBOL_GPL(gpiochip_remove);
1383
0cf3292c
LD
1384static void devm_gpio_chip_release(struct device *dev, void *res)
1385{
1386 struct gpio_chip *chip = *(struct gpio_chip **)res;
1387
1388 gpiochip_remove(chip);
1389}
1390
1391static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1392
1393{
1394 struct gpio_chip **r = res;
1395
1396 if (!r || !*r) {
1397 WARN_ON(!r || !*r);
1398 return 0;
1399 }
1400
1401 return *r == data;
1402}
1403
1404/**
1405 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1406 * @dev: the device pointer on which irq_chip belongs to.
1407 * @chip: the chip to register, with chip->base initialized
950d55f5 1408 * @data: driver-private data associated with this chip
0cf3292c 1409 *
950d55f5 1410 * Context: potentially before irqs will work
0cf3292c
LD
1411 *
1412 * The gpio chip automatically be released when the device is unbound.
950d55f5
TR
1413 *
1414 * Returns:
1415 * A negative errno if the chip can't be registered, such as because the
1416 * chip->base is invalid or already associated with a different chip.
1417 * Otherwise it returns zero as a success code.
0cf3292c
LD
1418 */
1419int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1420 void *data)
1421{
1422 struct gpio_chip **ptr;
1423 int ret;
1424
1425 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1426 GFP_KERNEL);
1427 if (!ptr)
1428 return -ENOMEM;
1429
1430 ret = gpiochip_add_data(chip, data);
1431 if (ret < 0) {
1432 devres_free(ptr);
1433 return ret;
1434 }
1435
1436 *ptr = chip;
1437 devres_add(dev, ptr);
1438
1439 return 0;
1440}
1441EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1442
1443/**
1444 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1445 * @dev: device for which which resource was allocated
1446 * @chip: the chip to remove
1447 *
1448 * A gpio_chip with any GPIOs still requested may not be removed.
1449 */
1450void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1451{
1452 int ret;
1453
1454 ret = devres_release(dev, devm_gpio_chip_release,
1455 devm_gpio_chip_match, chip);
3988d663 1456 WARN_ON(ret);
0cf3292c
LD
1457}
1458EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1459
594fa265
GL
1460/**
1461 * gpiochip_find() - iterator for locating a specific gpio_chip
1462 * @data: data to pass to match function
950d55f5 1463 * @match: Callback function to check gpio_chip
594fa265
GL
1464 *
1465 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1466 * determined by a user supplied @match callback. The callback should return
1467 * 0 if the device doesn't match and non-zero if it does. If the callback is
1468 * non-zero, this function will return to the caller and not iterate over any
1469 * more gpio_chips.
1470 */
07ce8ec7 1471struct gpio_chip *gpiochip_find(void *data,
6e2cf651 1472 int (*match)(struct gpio_chip *chip,
3d0f7cf0 1473 void *data))
594fa265 1474{
ff2b1359 1475 struct gpio_device *gdev;
acf06ff7 1476 struct gpio_chip *chip = NULL;
594fa265 1477 unsigned long flags;
594fa265
GL
1478
1479 spin_lock_irqsave(&gpio_lock, flags);
ff2b1359 1480 list_for_each_entry(gdev, &gpio_devices, list)
acf06ff7
MY
1481 if (gdev->chip && match(gdev->chip, data)) {
1482 chip = gdev->chip;
594fa265 1483 break;
acf06ff7 1484 }
ff2b1359 1485
594fa265
GL
1486 spin_unlock_irqrestore(&gpio_lock, flags);
1487
1488 return chip;
1489}
8fa0c9bf 1490EXPORT_SYMBOL_GPL(gpiochip_find);
d2876d08 1491
79697ef9
AC
1492static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1493{
1494 const char *name = data;
1495
1496 return !strcmp(chip->label, name);
1497}
1498
1499static struct gpio_chip *find_chip_by_name(const char *name)
1500{
1501 return gpiochip_find((void *)name, gpiochip_match_name);
1502}
1503
14250520
LW
1504#ifdef CONFIG_GPIOLIB_IRQCHIP
1505
1506/*
1507 * The following is irqchip helper code for gpiochips.
1508 */
1509
79b804cb
MW
1510static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1511{
dc7b0387 1512 if (!gpiochip->irq.need_valid_mask)
79b804cb
MW
1513 return 0;
1514
dc7b0387 1515 gpiochip->irq.valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
79b804cb 1516 sizeof(long), GFP_KERNEL);
dc7b0387 1517 if (!gpiochip->irq.valid_mask)
79b804cb
MW
1518 return -ENOMEM;
1519
1520 /* Assume by default all GPIOs are valid */
dc7b0387 1521 bitmap_fill(gpiochip->irq.valid_mask, gpiochip->ngpio);
79b804cb
MW
1522
1523 return 0;
1524}
1525
1526static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1527{
dc7b0387
TR
1528 kfree(gpiochip->irq.valid_mask);
1529 gpiochip->irq.valid_mask = NULL;
79b804cb
MW
1530}
1531
1532static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1533 unsigned int offset)
1534{
1535 /* No mask means all valid */
dc7b0387 1536 if (likely(!gpiochip->irq.valid_mask))
79b804cb 1537 return true;
dc7b0387 1538 return test_bit(offset, gpiochip->irq.valid_mask);
79b804cb
MW
1539}
1540
14250520 1541/**
d245b3f9 1542 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
3f97d5fc
LW
1543 * @gpiochip: the gpiochip to set the irqchip chain to
1544 * @irqchip: the irqchip to chain to the gpiochip
14250520
LW
1545 * @parent_irq: the irq number corresponding to the parent IRQ for this
1546 * chained irqchip
1547 * @parent_handler: the parent interrupt handler for the accumulated IRQ
3f97d5fc
LW
1548 * coming out of the gpiochip. If the interrupt is nested rather than
1549 * cascaded, pass NULL in this handler argument
14250520 1550 */
d245b3f9
LW
1551static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1552 struct irq_chip *irqchip,
6f79309a 1553 unsigned int parent_irq,
d245b3f9 1554 irq_flow_handler_t parent_handler)
14250520 1555{
83141a77
LW
1556 unsigned int offset;
1557
f0fbe7bc 1558 if (!gpiochip->irq.domain) {
83141a77
LW
1559 chip_err(gpiochip, "called %s before setting up irqchip\n",
1560 __func__);
1c8732bb
LW
1561 return;
1562 }
1563
3f97d5fc
LW
1564 if (parent_handler) {
1565 if (gpiochip->can_sleep) {
1566 chip_err(gpiochip,
1567 "you cannot have chained interrupts on a "
1568 "chip that may sleep\n");
1569 return;
1570 }
3f97d5fc
LW
1571 /*
1572 * The parent irqchip is already using the chip_data for this
1573 * irqchip, so our callbacks simply use the handler_data.
1574 */
f7f87753
TG
1575 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1576 gpiochip);
25e4fe92 1577
39e5f096
TR
1578 gpiochip->irq.parents = &parent_irq;
1579 gpiochip->irq.num_parents = 1;
3f97d5fc 1580 }
83141a77
LW
1581
1582 /* Set the parent IRQ for all affected IRQs */
79b804cb
MW
1583 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1584 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1585 continue;
f0fbe7bc 1586 irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
83141a77 1587 parent_irq);
79b804cb 1588 }
14250520 1589}
d245b3f9
LW
1590
1591/**
1592 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1593 * @gpiochip: the gpiochip to set the irqchip chain to
1594 * @irqchip: the irqchip to chain to the gpiochip
1595 * @parent_irq: the irq number corresponding to the parent IRQ for this
1596 * chained irqchip
1597 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1598 * coming out of the gpiochip. If the interrupt is nested rather than
1599 * cascaded, pass NULL in this handler argument
1600 */
1601void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1602 struct irq_chip *irqchip,
6f79309a 1603 unsigned int parent_irq,
d245b3f9
LW
1604 irq_flow_handler_t parent_handler)
1605{
1606 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1607 parent_handler);
1608}
14250520
LW
1609EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1610
d245b3f9
LW
1611/**
1612 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1613 * @gpiochip: the gpiochip to set the irqchip nested handler to
1614 * @irqchip: the irqchip to nest to the gpiochip
1615 * @parent_irq: the irq number corresponding to the parent IRQ for this
1616 * nested irqchip
1617 */
1618void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1619 struct irq_chip *irqchip,
6f79309a 1620 unsigned int parent_irq)
d245b3f9 1621{
dc6bafee 1622 if (!gpiochip->irq.nested) {
d245b3f9
LW
1623 chip_err(gpiochip, "tried to nest a chained gpiochip\n");
1624 return;
1625 }
1626 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1627 NULL);
1628}
1629EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1630
14250520
LW
1631/**
1632 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1633 * @d: the irqdomain used by this irqchip
1634 * @irq: the global irq number used by this GPIO irqchip irq
1635 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1636 *
1637 * This function will set up the mapping for a certain IRQ line on a
1638 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1639 * stored inside the gpiochip.
1640 */
1b95b4eb
TR
1641int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1642 irq_hw_number_t hwirq)
14250520
LW
1643{
1644 struct gpio_chip *chip = d->host_data;
e0d89728 1645 int err = 0;
14250520 1646
dc749a09
GS
1647 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1648 return -ENXIO;
1649
14250520 1650 irq_set_chip_data(irq, chip);
a0a8bcf4
GS
1651 /*
1652 * This lock class tells lockdep that GPIO irqs are in a different
1653 * category than their parents, so it won't report false recursion.
1654 */
ca9df053 1655 irq_set_lockdep_class(irq, chip->irq.lock_key);
c7a0aa59 1656 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
d245b3f9 1657 /* Chips that use nested thread handlers have them marked */
dc6bafee 1658 if (chip->irq.nested)
1c8732bb 1659 irq_set_nested_thread(irq, 1);
14250520 1660 irq_set_noprobe(irq);
23393d49 1661
e0d89728
TR
1662 if (chip->irq.num_parents == 1)
1663 err = irq_set_parent(irq, chip->irq.parents[0]);
1664 else if (chip->irq.map)
1665 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1666
1667 if (err < 0)
1668 return err;
1669
1333b90f
LW
1670 /*
1671 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1672 * is passed as default type.
1673 */
3634eeb0
TR
1674 if (chip->irq.default_type != IRQ_TYPE_NONE)
1675 irq_set_irq_type(irq, chip->irq.default_type);
14250520
LW
1676
1677 return 0;
1678}
1b95b4eb 1679EXPORT_SYMBOL_GPL(gpiochip_irq_map);
14250520 1680
1b95b4eb 1681void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
c3626fde 1682{
1c8732bb
LW
1683 struct gpio_chip *chip = d->host_data;
1684
dc6bafee 1685 if (chip->irq.nested)
1c8732bb 1686 irq_set_nested_thread(irq, 0);
c3626fde
LW
1687 irq_set_chip_and_handler(irq, NULL, NULL);
1688 irq_set_chip_data(irq, NULL);
1689}
1b95b4eb 1690EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
c3626fde 1691
14250520
LW
1692static const struct irq_domain_ops gpiochip_domain_ops = {
1693 .map = gpiochip_irq_map,
c3626fde 1694 .unmap = gpiochip_irq_unmap,
14250520
LW
1695 /* Virtually all GPIO irqchips are twocell:ed */
1696 .xlate = irq_domain_xlate_twocell,
1697};
1698
1699static int gpiochip_irq_reqres(struct irq_data *d)
1700{
1701 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1702
ff2b1359 1703 if (!try_module_get(chip->gpiodev->owner))
5b76e79c
GS
1704 return -ENODEV;
1705
e3a2e878 1706 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
14250520
LW
1707 chip_err(chip,
1708 "unable to lock HW IRQ %lu for IRQ\n",
1709 d->hwirq);
ff2b1359 1710 module_put(chip->gpiodev->owner);
14250520
LW
1711 return -EINVAL;
1712 }
1713 return 0;
1714}
1715
1716static void gpiochip_irq_relres(struct irq_data *d)
1717{
1718 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1719
e3a2e878 1720 gpiochip_unlock_as_irq(chip, d->hwirq);
ff2b1359 1721 module_put(chip->gpiodev->owner);
14250520
LW
1722}
1723
1724static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1725{
dc749a09
GS
1726 if (!gpiochip_irqchip_irq_valid(chip, offset))
1727 return -ENXIO;
e0d89728 1728
f0fbe7bc 1729 return irq_create_mapping(chip->irq.domain, offset);
14250520
LW
1730}
1731
e0d89728
TR
1732/**
1733 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1734 * @gpiochip: the GPIO chip to add the IRQ chip to
1735 */
1736static int gpiochip_add_irqchip(struct gpio_chip *gpiochip)
1737{
1738 struct irq_chip *irqchip = gpiochip->irq.chip;
1739 const struct irq_domain_ops *ops;
1740 struct device_node *np;
1741 unsigned int type;
1742 unsigned int i;
1743
1744 if (!irqchip)
1745 return 0;
1746
1747 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
1748 chip_err(gpiochip, "you cannot have chained interrupts on a "
1749 "chip that may sleep\n");
1750 return -EINVAL;
1751 }
1752
1753 np = gpiochip->gpiodev->dev.of_node;
1754 type = gpiochip->irq.default_type;
1755
1756 /*
1757 * Specifying a default trigger is a terrible idea if DT or ACPI is
1758 * used to configure the interrupts, as you may end up with
1759 * conflicting triggers. Tell the user, and reset to NONE.
1760 */
1761 if (WARN(np && type != IRQ_TYPE_NONE,
1762 "%s: Ignoring %u default trigger\n", np->full_name, type))
1763 type = IRQ_TYPE_NONE;
1764
1765 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1766 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1767 "Ignoring %u default trigger\n", type);
1768 type = IRQ_TYPE_NONE;
1769 }
1770
1771 gpiochip->to_irq = gpiochip_to_irq;
1772 gpiochip->irq.default_type = type;
1773
1774 if (gpiochip->irq.domain_ops)
1775 ops = gpiochip->irq.domain_ops;
1776 else
1777 ops = &gpiochip_domain_ops;
1778
1779 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
1780 0, ops, gpiochip);
1781 if (!gpiochip->irq.domain)
1782 return -EINVAL;
1783
1784 /*
1785 * It is possible for a driver to override this, but only if the
1786 * alternative functions are both implemented.
1787 */
1788 if (!irqchip->irq_request_resources &&
1789 !irqchip->irq_release_resources) {
1790 irqchip->irq_request_resources = gpiochip_irq_reqres;
1791 irqchip->irq_release_resources = gpiochip_irq_relres;
1792 }
1793
1794 if (gpiochip->irq.parent_handler) {
1795 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1796
1797 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1798 /*
1799 * The parent IRQ chip is already using the chip_data
1800 * for this IRQ chip, so our callbacks simply use the
1801 * handler_data.
1802 */
1803 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1804 gpiochip->irq.parent_handler,
1805 data);
1806 }
1807
1808 gpiochip->irq.nested = false;
1809 } else {
1810 gpiochip->irq.nested = true;
1811 }
1812
1813 acpi_gpiochip_request_interrupts(gpiochip);
1814
1815 return 0;
1816}
1817
14250520
LW
1818/**
1819 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1820 * @gpiochip: the gpiochip to remove the irqchip from
1821 *
1822 * This is called only from gpiochip_remove()
1823 */
1824static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1825{
39e5f096 1826 unsigned int offset;
c3626fde 1827
afa82fab
MW
1828 acpi_gpiochip_free_interrupts(gpiochip);
1829
e0d89728 1830 if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
39e5f096
TR
1831 struct gpio_irq_chip *irq = &gpiochip->irq;
1832 unsigned int i;
1833
1834 for (i = 0; i < irq->num_parents; i++)
1835 irq_set_chained_handler_and_data(irq->parents[i],
1836 NULL, NULL);
25e4fe92
DES
1837 }
1838
c3626fde 1839 /* Remove all IRQ mappings and delete the domain */
f0fbe7bc 1840 if (gpiochip->irq.domain) {
39e5f096
TR
1841 unsigned int irq;
1842
79b804cb
MW
1843 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1844 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1845 continue;
f0fbe7bc
TR
1846
1847 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1848 irq_dispose_mapping(irq);
79b804cb 1849 }
f0fbe7bc
TR
1850
1851 irq_domain_remove(gpiochip->irq.domain);
c3626fde 1852 }
14250520 1853
da80ff81
TR
1854 if (gpiochip->irq.chip) {
1855 gpiochip->irq.chip->irq_request_resources = NULL;
1856 gpiochip->irq.chip->irq_release_resources = NULL;
1857 gpiochip->irq.chip = NULL;
14250520 1858 }
79b804cb
MW
1859
1860 gpiochip_irqchip_free_valid_mask(gpiochip);
14250520
LW
1861}
1862
1863/**
739e6f59 1864 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
14250520
LW
1865 * @gpiochip: the gpiochip to add the irqchip to
1866 * @irqchip: the irqchip to add to the gpiochip
1867 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1868 * allocate gpiochip irqs from
1869 * @handler: the irq handler to use (often a predefined irq core function)
1333b90f
LW
1870 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1871 * to have the core avoid setting up any default type in the hardware.
d245b3f9
LW
1872 * @nested: whether this is a nested irqchip calling handle_nested_irq()
1873 * in its IRQ handler
a0a8bcf4 1874 * @lock_key: lockdep class
14250520
LW
1875 *
1876 * This function closely associates a certain irqchip with a certain
1877 * gpiochip, providing an irq domain to translate the local IRQs to
1878 * global irqs in the gpiolib core, and making sure that the gpiochip
1879 * is passed as chip data to all related functions. Driver callbacks
09dd5f9e 1880 * need to use gpiochip_get_data() to get their local state containers back
14250520
LW
1881 * from the gpiochip passed as chip data. An irqdomain will be stored
1882 * in the gpiochip that shall be used by the driver to handle IRQ number
1883 * translation. The gpiochip will need to be initialized and registered
1884 * before calling this function.
1885 *
c3626fde
LW
1886 * This function will handle two cell:ed simple IRQs and assumes all
1887 * the pins on the gpiochip can generate a unique IRQ. Everything else
14250520
LW
1888 * need to be open coded.
1889 */
739e6f59
LW
1890int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1891 struct irq_chip *irqchip,
1892 unsigned int first_irq,
1893 irq_flow_handler_t handler,
1894 unsigned int type,
1895 bool nested,
1896 struct lock_class_key *lock_key)
14250520
LW
1897{
1898 struct device_node *of_node;
14250520
LW
1899
1900 if (!gpiochip || !irqchip)
1901 return -EINVAL;
1902
58383c78 1903 if (!gpiochip->parent) {
14250520
LW
1904 pr_err("missing gpiochip .dev parent pointer\n");
1905 return -EINVAL;
1906 }
dc6bafee 1907 gpiochip->irq.nested = nested;
58383c78 1908 of_node = gpiochip->parent->of_node;
14250520
LW
1909#ifdef CONFIG_OF_GPIO
1910 /*
20a8a968 1911 * If the gpiochip has an assigned OF node this takes precedence
c88402c2
BJZ
1912 * FIXME: get rid of this and use gpiochip->parent->of_node
1913 * everywhere
14250520
LW
1914 */
1915 if (gpiochip->of_node)
1916 of_node = gpiochip->of_node;
1917#endif
332e99d5 1918 /*
0a1e0053 1919 * Specifying a default trigger is a terrible idea if DT or ACPI is
332e99d5
MZ
1920 * used to configure the interrupts, as you may end-up with
1921 * conflicting triggers. Tell the user, and reset to NONE.
1922 */
1923 if (WARN(of_node && type != IRQ_TYPE_NONE,
7eb6ce2f 1924 "%pOF: Ignoring %d default trigger\n", of_node, type))
332e99d5 1925 type = IRQ_TYPE_NONE;
0a1e0053
MW
1926 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1927 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1928 "Ignoring %d default trigger\n", type);
1929 type = IRQ_TYPE_NONE;
1930 }
332e99d5 1931
da80ff81 1932 gpiochip->irq.chip = irqchip;
c7a0aa59 1933 gpiochip->irq.handler = handler;
3634eeb0 1934 gpiochip->irq.default_type = type;
14250520 1935 gpiochip->to_irq = gpiochip_to_irq;
ca9df053 1936 gpiochip->irq.lock_key = lock_key;
f0fbe7bc 1937 gpiochip->irq.domain = irq_domain_add_simple(of_node,
14250520
LW
1938 gpiochip->ngpio, first_irq,
1939 &gpiochip_domain_ops, gpiochip);
f0fbe7bc 1940 if (!gpiochip->irq.domain) {
da80ff81 1941 gpiochip->irq.chip = NULL;
14250520
LW
1942 return -EINVAL;
1943 }
8b67a1f0
RV
1944
1945 /*
1946 * It is possible for a driver to override this, but only if the
1947 * alternative functions are both implemented.
1948 */
1949 if (!irqchip->irq_request_resources &&
1950 !irqchip->irq_release_resources) {
1951 irqchip->irq_request_resources = gpiochip_irq_reqres;
1952 irqchip->irq_release_resources = gpiochip_irq_relres;
1953 }
14250520 1954
afa82fab
MW
1955 acpi_gpiochip_request_interrupts(gpiochip);
1956
14250520
LW
1957 return 0;
1958}
739e6f59 1959EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
14250520
LW
1960
1961#else /* CONFIG_GPIOLIB_IRQCHIP */
1962
e0d89728
TR
1963static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip)
1964{
1965 return 0;
1966}
1967
14250520 1968static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
79b804cb
MW
1969static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1970{
1971 return 0;
1972}
1973static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1974{ }
14250520
LW
1975
1976#endif /* CONFIG_GPIOLIB_IRQCHIP */
1977
c771c2f4
JG
1978/**
1979 * gpiochip_generic_request() - request the gpio function for a pin
1980 * @chip: the gpiochip owning the GPIO
1981 * @offset: the offset of the GPIO to request for GPIO function
1982 */
1983int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1984{
fdeb8e15 1985 return pinctrl_request_gpio(chip->gpiodev->base + offset);
c771c2f4
JG
1986}
1987EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1988
1989/**
1990 * gpiochip_generic_free() - free the gpio function from a pin
1991 * @chip: the gpiochip to request the gpio function for
1992 * @offset: the offset of the GPIO to free from GPIO function
1993 */
1994void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1995{
fdeb8e15 1996 pinctrl_free_gpio(chip->gpiodev->base + offset);
c771c2f4
JG
1997}
1998EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1999
2956b5d9
MW
2000/**
2001 * gpiochip_generic_config() - apply configuration for a pin
2002 * @chip: the gpiochip owning the GPIO
2003 * @offset: the offset of the GPIO to apply the configuration
2004 * @config: the configuration to be applied
2005 */
2006int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2007 unsigned long config)
2008{
2009 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2010}
2011EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2012
f23f1516 2013#ifdef CONFIG_PINCTRL
165adc9c 2014
586a87e6
CR
2015/**
2016 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2017 * @chip: the gpiochip to add the range for
d32651f6 2018 * @pctldev: the pin controller to map to
586a87e6
CR
2019 * @gpio_offset: the start offset in the current gpio_chip number space
2020 * @pin_group: name of the pin group inside the pin controller
2021 */
2022int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2023 struct pinctrl_dev *pctldev,
2024 unsigned int gpio_offset, const char *pin_group)
2025{
2026 struct gpio_pin_range *pin_range;
fdeb8e15 2027 struct gpio_device *gdev = chip->gpiodev;
586a87e6
CR
2028 int ret;
2029
2030 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2031 if (!pin_range) {
1a2a99c6 2032 chip_err(chip, "failed to allocate pin ranges\n");
586a87e6
CR
2033 return -ENOMEM;
2034 }
2035
2036 /* Use local offset as range ID */
2037 pin_range->range.id = gpio_offset;
2038 pin_range->range.gc = chip;
2039 pin_range->range.name = chip->label;
fdeb8e15 2040 pin_range->range.base = gdev->base + gpio_offset;
586a87e6
CR
2041 pin_range->pctldev = pctldev;
2042
2043 ret = pinctrl_get_group_pins(pctldev, pin_group,
2044 &pin_range->range.pins,
2045 &pin_range->range.npins);
61c6375d
MN
2046 if (ret < 0) {
2047 kfree(pin_range);
586a87e6 2048 return ret;
61c6375d 2049 }
586a87e6
CR
2050
2051 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2052
1a2a99c6
AS
2053 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2054 gpio_offset, gpio_offset + pin_range->range.npins - 1,
586a87e6
CR
2055 pinctrl_dev_get_devname(pctldev), pin_group);
2056
20ec3e39 2057 list_add_tail(&pin_range->node, &gdev->pin_ranges);
586a87e6
CR
2058
2059 return 0;
2060}
2061EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2062
3f0f8670
LW
2063/**
2064 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2065 * @chip: the gpiochip to add the range for
950d55f5 2066 * @pinctl_name: the dev_name() of the pin controller to map to
316511c0
LW
2067 * @gpio_offset: the start offset in the current gpio_chip number space
2068 * @pin_offset: the start offset in the pin controller number space
3f0f8670
LW
2069 * @npins: the number of pins from the offset of each pin space (GPIO and
2070 * pin controller) to accumulate in this range
950d55f5
TR
2071 *
2072 * Returns:
2073 * 0 on success, or a negative error-code on failure.
3f0f8670 2074 */
1e63d7b9 2075int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
316511c0 2076 unsigned int gpio_offset, unsigned int pin_offset,
3f0f8670 2077 unsigned int npins)
f23f1516
SH
2078{
2079 struct gpio_pin_range *pin_range;
fdeb8e15 2080 struct gpio_device *gdev = chip->gpiodev;
b4d4b1f0 2081 int ret;
f23f1516 2082
3f0f8670 2083 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
f23f1516 2084 if (!pin_range) {
1a2a99c6 2085 chip_err(chip, "failed to allocate pin ranges\n");
1e63d7b9 2086 return -ENOMEM;
f23f1516
SH
2087 }
2088
3f0f8670 2089 /* Use local offset as range ID */
316511c0 2090 pin_range->range.id = gpio_offset;
3f0f8670 2091 pin_range->range.gc = chip;
f23f1516 2092 pin_range->range.name = chip->label;
fdeb8e15 2093 pin_range->range.base = gdev->base + gpio_offset;
316511c0 2094 pin_range->range.pin_base = pin_offset;
f23f1516 2095 pin_range->range.npins = npins;
192c369c 2096 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
f23f1516 2097 &pin_range->range);
8f23ca1a 2098 if (IS_ERR(pin_range->pctldev)) {
b4d4b1f0 2099 ret = PTR_ERR(pin_range->pctldev);
1a2a99c6 2100 chip_err(chip, "could not create pin range\n");
3f0f8670 2101 kfree(pin_range);
b4d4b1f0 2102 return ret;
3f0f8670 2103 }
1a2a99c6
AS
2104 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2105 gpio_offset, gpio_offset + npins - 1,
316511c0
LW
2106 pinctl_name,
2107 pin_offset, pin_offset + npins - 1);
f23f1516 2108
20ec3e39 2109 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1e63d7b9
LW
2110
2111 return 0;
f23f1516 2112}
165adc9c 2113EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
f23f1516 2114
3f0f8670
LW
2115/**
2116 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2117 * @chip: the chip to remove all the mappings for
2118 */
f23f1516
SH
2119void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2120{
2121 struct gpio_pin_range *pin_range, *tmp;
20ec3e39 2122 struct gpio_device *gdev = chip->gpiodev;
f23f1516 2123
20ec3e39 2124 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
f23f1516
SH
2125 list_del(&pin_range->node);
2126 pinctrl_remove_gpio_range(pin_range->pctldev,
2127 &pin_range->range);
3f0f8670 2128 kfree(pin_range);
f23f1516
SH
2129 }
2130}
165adc9c
LW
2131EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2132
2133#endif /* CONFIG_PINCTRL */
f23f1516 2134
d2876d08
DB
2135/* These "optional" allocation calls help prevent drivers from stomping
2136 * on each other, and help provide better diagnostics in debugfs.
2137 * They're called even less than the "set direction" calls.
2138 */
fac9d885 2139static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
d2876d08 2140{
fdeb8e15 2141 struct gpio_chip *chip = desc->gdev->chip;
77c2d792 2142 int status;
d2876d08
DB
2143 unsigned long flags;
2144
bcabdef1
AC
2145 spin_lock_irqsave(&gpio_lock, flags);
2146
d2876d08 2147 /* NOTE: gpio_request() can be called in early boot,
35e8bb51 2148 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
d2876d08
DB
2149 */
2150
2151 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2152 desc_set_label(desc, label ? : "?");
2153 status = 0;
438d8908 2154 } else {
d2876d08 2155 status = -EBUSY;
7460db56 2156 goto done;
35e8bb51
DB
2157 }
2158
2159 if (chip->request) {
2160 /* chip->request may sleep */
2161 spin_unlock_irqrestore(&gpio_lock, flags);
372e722e 2162 status = chip->request(chip, gpio_chip_hwgpio(desc));
35e8bb51
DB
2163 spin_lock_irqsave(&gpio_lock, flags);
2164
2165 if (status < 0) {
2166 desc_set_label(desc, NULL);
35e8bb51 2167 clear_bit(FLAG_REQUESTED, &desc->flags);
80b0a602 2168 goto done;
35e8bb51 2169 }
438d8908 2170 }
80b0a602
MN
2171 if (chip->get_direction) {
2172 /* chip->get_direction may sleep */
2173 spin_unlock_irqrestore(&gpio_lock, flags);
372e722e 2174 gpiod_get_direction(desc);
80b0a602
MN
2175 spin_lock_irqsave(&gpio_lock, flags);
2176 }
77c2d792
MW
2177done:
2178 spin_unlock_irqrestore(&gpio_lock, flags);
2179 return status;
2180}
2181
fdeb8e15
LW
2182/*
2183 * This descriptor validation needs to be inserted verbatim into each
2184 * function taking a descriptor, so we need to use a preprocessor
54d77198
LW
2185 * macro to avoid endless duplication. If the desc is NULL it is an
2186 * optional GPIO and calls should just bail out.
fdeb8e15
LW
2187 */
2188#define VALIDATE_DESC(desc) do { \
54d77198
LW
2189 if (!desc) \
2190 return 0; \
bfbbe44d
LW
2191 if (IS_ERR(desc)) { \
2192 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2193 return PTR_ERR(desc); \
2194 } \
54d77198 2195 if (!desc->gdev) { \
bfbbe44d 2196 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
fdeb8e15
LW
2197 return -EINVAL; \
2198 } \
2199 if ( !desc->gdev->chip ) { \
2200 dev_warn(&desc->gdev->dev, \
2201 "%s: backing chip is gone\n", __func__); \
2202 return 0; \
2203 } } while (0)
2204
2205#define VALIDATE_DESC_VOID(desc) do { \
54d77198
LW
2206 if (!desc) \
2207 return; \
bfbbe44d
LW
2208 if (IS_ERR(desc)) { \
2209 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2210 return; \
2211 } \
54d77198 2212 if (!desc->gdev) { \
bfbbe44d 2213 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
fdeb8e15
LW
2214 return; \
2215 } \
2216 if (!desc->gdev->chip) { \
2217 dev_warn(&desc->gdev->dev, \
2218 "%s: backing chip is gone\n", __func__); \
2219 return; \
2220 } } while (0)
2221
2222
0eb4c6c2 2223int gpiod_request(struct gpio_desc *desc, const char *label)
77c2d792
MW
2224{
2225 int status = -EPROBE_DEFER;
fdeb8e15 2226 struct gpio_device *gdev;
77c2d792 2227
fdeb8e15
LW
2228 VALIDATE_DESC(desc);
2229 gdev = desc->gdev;
77c2d792 2230
fdeb8e15 2231 if (try_module_get(gdev->owner)) {
fac9d885 2232 status = gpiod_request_commit(desc, label);
77c2d792 2233 if (status < 0)
fdeb8e15 2234 module_put(gdev->owner);
33a68e86
LW
2235 else
2236 get_device(&gdev->dev);
77c2d792
MW
2237 }
2238
d2876d08 2239 if (status)
7589e59f 2240 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
77c2d792 2241
d2876d08
DB
2242 return status;
2243}
372e722e 2244
fac9d885 2245static bool gpiod_free_commit(struct gpio_desc *desc)
d2876d08 2246{
77c2d792 2247 bool ret = false;
d2876d08 2248 unsigned long flags;
35e8bb51 2249 struct gpio_chip *chip;
d2876d08 2250
3d599d1c
UKK
2251 might_sleep();
2252
372e722e 2253 gpiod_unexport(desc);
d8f388d8 2254
d2876d08
DB
2255 spin_lock_irqsave(&gpio_lock, flags);
2256
fdeb8e15 2257 chip = desc->gdev->chip;
35e8bb51
DB
2258 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2259 if (chip->free) {
2260 spin_unlock_irqrestore(&gpio_lock, flags);
9c4ba946 2261 might_sleep_if(chip->can_sleep);
372e722e 2262 chip->free(chip, gpio_chip_hwgpio(desc));
35e8bb51
DB
2263 spin_lock_irqsave(&gpio_lock, flags);
2264 }
d2876d08 2265 desc_set_label(desc, NULL);
07697461 2266 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
35e8bb51 2267 clear_bit(FLAG_REQUESTED, &desc->flags);
aca5ce14 2268 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
25553ff0 2269 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
f625d460 2270 clear_bit(FLAG_IS_HOGGED, &desc->flags);
77c2d792
MW
2271 ret = true;
2272 }
d2876d08
DB
2273
2274 spin_unlock_irqrestore(&gpio_lock, flags);
77c2d792
MW
2275 return ret;
2276}
2277
0eb4c6c2 2278void gpiod_free(struct gpio_desc *desc)
77c2d792 2279{
fac9d885 2280 if (desc && desc->gdev && gpiod_free_commit(desc)) {
fdeb8e15 2281 module_put(desc->gdev->owner);
33a68e86
LW
2282 put_device(&desc->gdev->dev);
2283 } else {
77c2d792 2284 WARN_ON(extra_checks);
33a68e86 2285 }
d2876d08 2286}
372e722e 2287
d2876d08
DB
2288/**
2289 * gpiochip_is_requested - return string iff signal was requested
2290 * @chip: controller managing the signal
2291 * @offset: of signal within controller's 0..(ngpio - 1) range
2292 *
2293 * Returns NULL if the GPIO is not currently requested, else a string.
9c8318ff
AC
2294 * The string returned is the label passed to gpio_request(); if none has been
2295 * passed it is a meaningless, non-NULL constant.
d2876d08
DB
2296 *
2297 * This function is for use by GPIO controller drivers. The label can
2298 * help with diagnostics, and knowing that the signal is used as a GPIO
2299 * can help avoid accidentally multiplexing it to another controller.
2300 */
2301const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2302{
6c0b4e6c 2303 struct gpio_desc *desc;
d2876d08 2304
48b5953e 2305 if (offset >= chip->ngpio)
d2876d08 2306 return NULL;
6c0b4e6c 2307
1c3cdb18 2308 desc = &chip->gpiodev->descs[offset];
6c0b4e6c 2309
372e722e 2310 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
d2876d08 2311 return NULL;
372e722e 2312 return desc->label;
d2876d08
DB
2313}
2314EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2315
77c2d792
MW
2316/**
2317 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
950d55f5
TR
2318 * @chip: GPIO chip
2319 * @hwnum: hardware number of the GPIO for which to request the descriptor
77c2d792
MW
2320 * @label: label for the GPIO
2321 *
2322 * Function allows GPIO chip drivers to request and use their own GPIO
2323 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2324 * function will not increase reference count of the GPIO chip module. This
2325 * allows the GPIO chip module to be unloaded as needed (we assume that the
2326 * GPIO chip driver handles freeing the GPIOs it has requested).
950d55f5
TR
2327 *
2328 * Returns:
2329 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2330 * code on failure.
77c2d792 2331 */
abdc08a3
AC
2332struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2333 const char *label)
77c2d792 2334{
abdc08a3
AC
2335 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2336 int err;
77c2d792 2337
abdc08a3
AC
2338 if (IS_ERR(desc)) {
2339 chip_err(chip, "failed to get GPIO descriptor\n");
2340 return desc;
2341 }
2342
fac9d885 2343 err = gpiod_request_commit(desc, label);
abdc08a3
AC
2344 if (err < 0)
2345 return ERR_PTR(err);
77c2d792 2346
abdc08a3 2347 return desc;
77c2d792 2348}
f7d4ad98 2349EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
77c2d792
MW
2350
2351/**
2352 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2353 * @desc: GPIO descriptor to free
2354 *
2355 * Function frees the given GPIO requested previously with
2356 * gpiochip_request_own_desc().
2357 */
2358void gpiochip_free_own_desc(struct gpio_desc *desc)
2359{
2360 if (desc)
fac9d885 2361 gpiod_free_commit(desc);
77c2d792 2362}
f7d4ad98 2363EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
d2876d08 2364
fdeb8e15
LW
2365/*
2366 * Drivers MUST set GPIO direction before making get/set calls. In
d2876d08
DB
2367 * some cases this is done in early boot, before IRQs are enabled.
2368 *
2369 * As a rule these aren't called more than once (except for drivers
2370 * using the open-drain emulation idiom) so these are natural places
2371 * to accumulate extra debugging checks. Note that we can't (yet)
2372 * rely on gpio_request() having been called beforehand.
2373 */
2374
79a9becd
AC
2375/**
2376 * gpiod_direction_input - set the GPIO direction to input
2377 * @desc: GPIO to set to input
2378 *
2379 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2380 * be called safely on it.
2381 *
2382 * Return 0 in case of success, else an error code.
2383 */
2384int gpiod_direction_input(struct gpio_desc *desc)
d2876d08 2385{
d2876d08 2386 struct gpio_chip *chip;
d2876d08
DB
2387 int status = -EINVAL;
2388
fdeb8e15
LW
2389 VALIDATE_DESC(desc);
2390 chip = desc->gdev->chip;
bcabdef1 2391
be1a4b13 2392 if (!chip->get || !chip->direction_input) {
6424de5a
MB
2393 gpiod_warn(desc,
2394 "%s: missing get() or direction_input() operations\n",
7589e59f 2395 __func__);
be1a4b13
LW
2396 return -EIO;
2397 }
2398
d82da797 2399 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
d2876d08
DB
2400 if (status == 0)
2401 clear_bit(FLAG_IS_OUT, &desc->flags);
3f397c21 2402
372e722e 2403 trace_gpio_direction(desc_to_gpio(desc), 1, status);
d82da797 2404
d2876d08
DB
2405 return status;
2406}
79a9becd 2407EXPORT_SYMBOL_GPL(gpiod_direction_input);
372e722e 2408
2956b5d9
MW
2409static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2410 enum pin_config_param mode)
2411{
2412 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2413
2414 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2415}
2416
fac9d885 2417static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
d2876d08 2418{
c663e5f5 2419 struct gpio_chip *gc = desc->gdev->chip;
ad17731d 2420 int val = !!value;
c663e5f5 2421 int ret;
d2876d08 2422
c663e5f5 2423 if (!gc->set || !gc->direction_output) {
6424de5a
MB
2424 gpiod_warn(desc,
2425 "%s: missing set() or direction_output() operations\n",
2426 __func__);
be1a4b13
LW
2427 return -EIO;
2428 }
2429
ad17731d 2430 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
c663e5f5 2431 if (!ret)
d2876d08 2432 set_bit(FLAG_IS_OUT, &desc->flags);
ad17731d 2433 trace_gpio_value(desc_to_gpio(desc), 0, val);
c663e5f5
LW
2434 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2435 return ret;
d2876d08 2436}
ef70bbe1
PZ
2437
2438/**
2439 * gpiod_direction_output_raw - set the GPIO direction to output
2440 * @desc: GPIO to set to output
2441 * @value: initial output value of the GPIO
2442 *
2443 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2444 * be called safely on it. The initial value of the output must be specified
2445 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2446 *
2447 * Return 0 in case of success, else an error code.
2448 */
2449int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2450{
fdeb8e15 2451 VALIDATE_DESC(desc);
fac9d885 2452 return gpiod_direction_output_raw_commit(desc, value);
ef70bbe1
PZ
2453}
2454EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2455
2456/**
90df4fe0 2457 * gpiod_direction_output - set the GPIO direction to output
ef70bbe1
PZ
2458 * @desc: GPIO to set to output
2459 * @value: initial output value of the GPIO
2460 *
2461 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2462 * be called safely on it. The initial value of the output must be specified
2463 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2464 * account.
2465 *
2466 * Return 0 in case of success, else an error code.
2467 */
2468int gpiod_direction_output(struct gpio_desc *desc, int value)
2469{
02e47980
LW
2470 struct gpio_chip *gc = desc->gdev->chip;
2471 int ret;
2472
fdeb8e15 2473 VALIDATE_DESC(desc);
ef70bbe1
PZ
2474 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2475 value = !value;
ad17731d
LW
2476 else
2477 value = !!value;
02e47980
LW
2478
2479 /* GPIOs used for IRQs shall not be set as output */
2480 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2481 gpiod_err(desc,
2482 "%s: tried to set a GPIO tied to an IRQ as output\n",
2483 __func__);
2484 return -EIO;
2485 }
2486
2487 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2488 /* First see if we can enable open drain in hardware */
2489 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2490 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2491 if (!ret)
2492 goto set_output_value;
2493 /* Emulate open drain by not actively driving the line high */
2494 if (value)
2495 return gpiod_direction_input(desc);
2496 }
2497 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2498 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2499 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2500 if (!ret)
2501 goto set_output_value;
2502 /* Emulate open source by not actively driving the line low */
2503 if (!value)
2504 return gpiod_direction_input(desc);
2505 } else {
2506 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2507 PIN_CONFIG_DRIVE_PUSH_PULL);
2508 }
2509
2510set_output_value:
fac9d885 2511 return gpiod_direction_output_raw_commit(desc, value);
ef70bbe1 2512}
79a9becd 2513EXPORT_SYMBOL_GPL(gpiod_direction_output);
d2876d08 2514
c4b5be98 2515/**
950d55f5
TR
2516 * gpiod_set_debounce - sets @debounce time for a GPIO
2517 * @desc: descriptor of the GPIO for which to set debounce time
2518 * @debounce: debounce time in microseconds
65d87656 2519 *
950d55f5
TR
2520 * Returns:
2521 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2522 * debounce time.
c4b5be98 2523 */
79a9becd 2524int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
c4b5be98 2525{
c4b5be98 2526 struct gpio_chip *chip;
2956b5d9 2527 unsigned long config;
c4b5be98 2528
fdeb8e15
LW
2529 VALIDATE_DESC(desc);
2530 chip = desc->gdev->chip;
2956b5d9 2531 if (!chip->set || !chip->set_config) {
6424de5a 2532 gpiod_dbg(desc,
2956b5d9 2533 "%s: missing set() or set_config() operations\n",
6424de5a 2534 __func__);
65d87656 2535 return -ENOTSUPP;
be1a4b13
LW
2536 }
2537
2956b5d9
MW
2538 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2539 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
c4b5be98 2540}
79a9becd 2541EXPORT_SYMBOL_GPL(gpiod_set_debounce);
372e722e 2542
79a9becd
AC
2543/**
2544 * gpiod_is_active_low - test whether a GPIO is active-low or not
2545 * @desc: the gpio descriptor to test
2546 *
2547 * Returns 1 if the GPIO is active-low, 0 otherwise.
2548 */
2549int gpiod_is_active_low(const struct gpio_desc *desc)
372e722e 2550{
fdeb8e15 2551 VALIDATE_DESC(desc);
79a9becd 2552 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
372e722e 2553}
79a9becd 2554EXPORT_SYMBOL_GPL(gpiod_is_active_low);
d2876d08
DB
2555
2556/* I/O calls are only valid after configuration completed; the relevant
2557 * "is this a valid GPIO" error checks should already have been done.
2558 *
2559 * "Get" operations are often inlinable as reading a pin value register,
2560 * and masking the relevant bit in that register.
2561 *
2562 * When "set" operations are inlinable, they involve writing that mask to
2563 * one register to set a low value, or a different register to set it high.
2564 * Otherwise locking is needed, so there may be little value to inlining.
2565 *
2566 *------------------------------------------------------------------------
2567 *
2568 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2569 * have requested the GPIO. That can include implicit requesting by
2570 * a direction setting call. Marking a gpio as requested locks its chip
2571 * in memory, guaranteeing that these table lookups need no more locking
2572 * and that gpiochip_remove() will fail.
2573 *
2574 * REVISIT when debugging, consider adding some instrumentation to ensure
2575 * that the GPIO was actually requested.
2576 */
2577
fac9d885 2578static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
d2876d08
DB
2579{
2580 struct gpio_chip *chip;
372e722e 2581 int offset;
e20538b8 2582 int value;
d2876d08 2583
fdeb8e15 2584 chip = desc->gdev->chip;
372e722e 2585 offset = gpio_chip_hwgpio(desc);
e20538b8 2586 value = chip->get ? chip->get(chip, offset) : -EIO;
723a6303 2587 value = value < 0 ? value : !!value;
372e722e 2588 trace_gpio_value(desc_to_gpio(desc), 1, value);
3f397c21 2589 return value;
d2876d08 2590}
372e722e 2591
eec1d566
LW
2592static int gpio_chip_get_multiple(struct gpio_chip *chip,
2593 unsigned long *mask, unsigned long *bits)
2594{
2595 if (chip->get_multiple) {
2596 return chip->get_multiple(chip, mask, bits);
2597 } else if (chip->get) {
2598 int i, value;
2599
2600 for_each_set_bit(i, mask, chip->ngpio) {
2601 value = chip->get(chip, i);
2602 if (value < 0)
2603 return value;
2604 __assign_bit(i, bits, value);
2605 }
2606 return 0;
2607 }
2608 return -EIO;
2609}
2610
2611int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2612 unsigned int array_size,
2613 struct gpio_desc **desc_array,
2614 int *value_array)
2615{
2616 int i = 0;
2617
2618 while (i < array_size) {
2619 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2620 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2621 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2622 int first, j, ret;
2623
2624 if (!can_sleep)
2625 WARN_ON(chip->can_sleep);
2626
2627 /* collect all inputs belonging to the same chip */
2628 first = i;
2629 memset(mask, 0, sizeof(mask));
2630 do {
2631 const struct gpio_desc *desc = desc_array[i];
2632 int hwgpio = gpio_chip_hwgpio(desc);
2633
2634 __set_bit(hwgpio, mask);
2635 i++;
2636 } while ((i < array_size) &&
2637 (desc_array[i]->gdev->chip == chip));
2638
2639 ret = gpio_chip_get_multiple(chip, mask, bits);
2640 if (ret)
2641 return ret;
2642
2643 for (j = first; j < i; j++) {
2644 const struct gpio_desc *desc = desc_array[j];
2645 int hwgpio = gpio_chip_hwgpio(desc);
2646 int value = test_bit(hwgpio, bits);
2647
2648 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2649 value = !value;
2650 value_array[j] = value;
2651 trace_gpio_value(desc_to_gpio(desc), 1, value);
2652 }
2653 }
2654 return 0;
2655}
2656
d2876d08 2657/**
79a9becd
AC
2658 * gpiod_get_raw_value() - return a gpio's raw value
2659 * @desc: gpio whose value will be returned
d2876d08 2660 *
79a9becd 2661 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
e20538b8 2662 * its ACTIVE_LOW status, or negative errno on failure.
79a9becd
AC
2663 *
2664 * This function should be called from contexts where we cannot sleep, and will
2665 * complain if the GPIO chip functions potentially sleep.
d2876d08 2666 */
79a9becd 2667int gpiod_get_raw_value(const struct gpio_desc *desc)
d2876d08 2668{
fdeb8e15 2669 VALIDATE_DESC(desc);
e4e449e8 2670 /* Should be using gpio_get_value_cansleep() */
fdeb8e15 2671 WARN_ON(desc->gdev->chip->can_sleep);
fac9d885 2672 return gpiod_get_raw_value_commit(desc);
d2876d08 2673}
79a9becd 2674EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
372e722e 2675
79a9becd
AC
2676/**
2677 * gpiod_get_value() - return a gpio's value
2678 * @desc: gpio whose value will be returned
2679 *
2680 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
e20538b8 2681 * account, or negative errno on failure.
79a9becd
AC
2682 *
2683 * This function should be called from contexts where we cannot sleep, and will
2684 * complain if the GPIO chip functions potentially sleep.
2685 */
2686int gpiod_get_value(const struct gpio_desc *desc)
372e722e 2687{
79a9becd 2688 int value;
fdeb8e15
LW
2689
2690 VALIDATE_DESC(desc);
79a9becd 2691 /* Should be using gpio_get_value_cansleep() */
fdeb8e15 2692 WARN_ON(desc->gdev->chip->can_sleep);
79a9becd 2693
fac9d885 2694 value = gpiod_get_raw_value_commit(desc);
e20538b8
BA
2695 if (value < 0)
2696 return value;
2697
79a9becd
AC
2698 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2699 value = !value;
2700
2701 return value;
372e722e 2702}
79a9becd 2703EXPORT_SYMBOL_GPL(gpiod_get_value);
d2876d08 2704
eec1d566
LW
2705/**
2706 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2707 * @array_size: number of elements in the descriptor / value arrays
2708 * @desc_array: array of GPIO descriptors whose values will be read
2709 * @value_array: array to store the read values
2710 *
2711 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2712 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2713 * else an error code.
2714 *
2715 * This function should be called from contexts where we cannot sleep,
2716 * and it will complain if the GPIO chip functions potentially sleep.
2717 */
2718int gpiod_get_raw_array_value(unsigned int array_size,
2719 struct gpio_desc **desc_array, int *value_array)
2720{
2721 if (!desc_array)
2722 return -EINVAL;
2723 return gpiod_get_array_value_complex(true, false, array_size,
2724 desc_array, value_array);
2725}
2726EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2727
2728/**
2729 * gpiod_get_array_value() - read values from an array of GPIOs
2730 * @array_size: number of elements in the descriptor / value arrays
2731 * @desc_array: array of GPIO descriptors whose values will be read
2732 * @value_array: array to store the read values
2733 *
2734 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2735 * into account. Return 0 in case of success, else an error code.
2736 *
2737 * This function should be called from contexts where we cannot sleep,
2738 * and it will complain if the GPIO chip functions potentially sleep.
2739 */
2740int gpiod_get_array_value(unsigned int array_size,
2741 struct gpio_desc **desc_array, int *value_array)
2742{
2743 if (!desc_array)
2744 return -EINVAL;
2745 return gpiod_get_array_value_complex(false, false, array_size,
2746 desc_array, value_array);
2747}
2748EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2749
aca5ce14 2750/*
fac9d885 2751 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
79a9becd 2752 * @desc: gpio descriptor whose state need to be set.
20a8a968 2753 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
aca5ce14 2754 */
fac9d885 2755static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
aca5ce14
LD
2756{
2757 int err = 0;
fdeb8e15 2758 struct gpio_chip *chip = desc->gdev->chip;
372e722e
AC
2759 int offset = gpio_chip_hwgpio(desc);
2760
aca5ce14 2761 if (value) {
372e722e 2762 err = chip->direction_input(chip, offset);
aca5ce14 2763 if (!err)
372e722e 2764 clear_bit(FLAG_IS_OUT, &desc->flags);
aca5ce14 2765 } else {
372e722e 2766 err = chip->direction_output(chip, offset, 0);
aca5ce14 2767 if (!err)
372e722e 2768 set_bit(FLAG_IS_OUT, &desc->flags);
aca5ce14 2769 }
372e722e 2770 trace_gpio_direction(desc_to_gpio(desc), value, err);
aca5ce14 2771 if (err < 0)
6424de5a
MB
2772 gpiod_err(desc,
2773 "%s: Error in set_value for open drain err %d\n",
2774 __func__, err);
aca5ce14
LD
2775}
2776
25553ff0 2777/*
79a9becd
AC
2778 * _gpio_set_open_source_value() - Set the open source gpio's value.
2779 * @desc: gpio descriptor whose state need to be set.
20a8a968 2780 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
25553ff0 2781 */
fac9d885 2782static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
25553ff0
LD
2783{
2784 int err = 0;
fdeb8e15 2785 struct gpio_chip *chip = desc->gdev->chip;
372e722e
AC
2786 int offset = gpio_chip_hwgpio(desc);
2787
25553ff0 2788 if (value) {
372e722e 2789 err = chip->direction_output(chip, offset, 1);
25553ff0 2790 if (!err)
372e722e 2791 set_bit(FLAG_IS_OUT, &desc->flags);
25553ff0 2792 } else {
372e722e 2793 err = chip->direction_input(chip, offset);
25553ff0 2794 if (!err)
372e722e 2795 clear_bit(FLAG_IS_OUT, &desc->flags);
25553ff0 2796 }
372e722e 2797 trace_gpio_direction(desc_to_gpio(desc), !value, err);
25553ff0 2798 if (err < 0)
6424de5a
MB
2799 gpiod_err(desc,
2800 "%s: Error in set_value for open source err %d\n",
2801 __func__, err);
25553ff0
LD
2802}
2803
fac9d885 2804static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
d2876d08
DB
2805{
2806 struct gpio_chip *chip;
2807
fdeb8e15 2808 chip = desc->gdev->chip;
372e722e 2809 trace_gpio_value(desc_to_gpio(desc), 0, value);
02e47980 2810 chip->set(chip, gpio_chip_hwgpio(desc), value);
372e722e
AC
2811}
2812
5f424243
RI
2813/*
2814 * set multiple outputs on the same chip;
2815 * use the chip's set_multiple function if available;
2816 * otherwise set the outputs sequentially;
2817 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2818 * defines which outputs are to be changed
2819 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2820 * defines the values the outputs specified by mask are to be set to
2821 */
2822static void gpio_chip_set_multiple(struct gpio_chip *chip,
2823 unsigned long *mask, unsigned long *bits)
2824{
2825 if (chip->set_multiple) {
2826 chip->set_multiple(chip, mask, bits);
2827 } else {
5e4e6fb3
AS
2828 unsigned int i;
2829
2830 /* set outputs if the corresponding mask bit is set */
2831 for_each_set_bit(i, mask, chip->ngpio)
2832 chip->set(chip, i, test_bit(i, bits));
5f424243
RI
2833 }
2834}
2835
44c7288f
LW
2836void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2837 unsigned int array_size,
2838 struct gpio_desc **desc_array,
2839 int *value_array)
5f424243
RI
2840{
2841 int i = 0;
2842
2843 while (i < array_size) {
fdeb8e15 2844 struct gpio_chip *chip = desc_array[i]->gdev->chip;
5f424243
RI
2845 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2846 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2847 int count = 0;
2848
38e003f4 2849 if (!can_sleep)
5f424243 2850 WARN_ON(chip->can_sleep);
38e003f4 2851
5f424243
RI
2852 memset(mask, 0, sizeof(mask));
2853 do {
2854 struct gpio_desc *desc = desc_array[i];
2855 int hwgpio = gpio_chip_hwgpio(desc);
2856 int value = value_array[i];
2857
2858 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2859 value = !value;
2860 trace_gpio_value(desc_to_gpio(desc), 0, value);
2861 /*
2862 * collect all normal outputs belonging to the same chip
2863 * open drain and open source outputs are set individually
2864 */
02e47980 2865 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
fac9d885 2866 gpio_set_open_drain_value_commit(desc, value);
02e47980 2867 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
fac9d885 2868 gpio_set_open_source_value_commit(desc, value);
5f424243
RI
2869 } else {
2870 __set_bit(hwgpio, mask);
38e003f4 2871 if (value)
5f424243 2872 __set_bit(hwgpio, bits);
38e003f4 2873 else
5f424243 2874 __clear_bit(hwgpio, bits);
5f424243
RI
2875 count++;
2876 }
2877 i++;
fdeb8e15
LW
2878 } while ((i < array_size) &&
2879 (desc_array[i]->gdev->chip == chip));
5f424243 2880 /* push collected bits to outputs */
38e003f4 2881 if (count != 0)
5f424243 2882 gpio_chip_set_multiple(chip, mask, bits);
5f424243
RI
2883 }
2884}
2885
d2876d08 2886/**
79a9becd
AC
2887 * gpiod_set_raw_value() - assign a gpio's raw value
2888 * @desc: gpio whose value will be assigned
d2876d08 2889 * @value: value to assign
d2876d08 2890 *
79a9becd
AC
2891 * Set the raw value of the GPIO, i.e. the value of its physical line without
2892 * regard for its ACTIVE_LOW status.
2893 *
2894 * This function should be called from contexts where we cannot sleep, and will
2895 * complain if the GPIO chip functions potentially sleep.
d2876d08 2896 */
79a9becd 2897void gpiod_set_raw_value(struct gpio_desc *desc, int value)
372e722e 2898{
fdeb8e15 2899 VALIDATE_DESC_VOID(desc);
1cfab8f8 2900 /* Should be using gpiod_set_value_cansleep() */
fdeb8e15 2901 WARN_ON(desc->gdev->chip->can_sleep);
fac9d885 2902 gpiod_set_raw_value_commit(desc, value);
d2876d08 2903}
79a9becd 2904EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
d2876d08
DB
2905
2906/**
79a9becd
AC
2907 * gpiod_set_value() - assign a gpio's value
2908 * @desc: gpio whose value will be assigned
2909 * @value: value to assign
2910 *
02e47980
LW
2911 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
2912 * OPEN_DRAIN and OPEN_SOURCE flags into account.
d2876d08 2913 *
79a9becd
AC
2914 * This function should be called from contexts where we cannot sleep, and will
2915 * complain if the GPIO chip functions potentially sleep.
d2876d08 2916 */
79a9becd 2917void gpiod_set_value(struct gpio_desc *desc, int value)
d2876d08 2918{
fdeb8e15 2919 VALIDATE_DESC_VOID(desc);
1cfab8f8 2920 /* Should be using gpiod_set_value_cansleep() */
fdeb8e15 2921 WARN_ON(desc->gdev->chip->can_sleep);
79a9becd
AC
2922 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2923 value = !value;
02e47980
LW
2924 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2925 gpio_set_open_drain_value_commit(desc, value);
2926 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2927 gpio_set_open_source_value_commit(desc, value);
2928 else
2929 gpiod_set_raw_value_commit(desc, value);
372e722e 2930}
79a9becd 2931EXPORT_SYMBOL_GPL(gpiod_set_value);
d2876d08 2932
5f424243 2933/**
3fff99bc 2934 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
5f424243
RI
2935 * @array_size: number of elements in the descriptor / value arrays
2936 * @desc_array: array of GPIO descriptors whose values will be assigned
2937 * @value_array: array of values to assign
2938 *
2939 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2940 * without regard for their ACTIVE_LOW status.
2941 *
2942 * This function should be called from contexts where we cannot sleep, and will
2943 * complain if the GPIO chip functions potentially sleep.
2944 */
3fff99bc 2945void gpiod_set_raw_array_value(unsigned int array_size,
5f424243
RI
2946 struct gpio_desc **desc_array, int *value_array)
2947{
2948 if (!desc_array)
2949 return;
44c7288f
LW
2950 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2951 value_array);
5f424243 2952}
3fff99bc 2953EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
5f424243
RI
2954
2955/**
3fff99bc 2956 * gpiod_set_array_value() - assign values to an array of GPIOs
5f424243
RI
2957 * @array_size: number of elements in the descriptor / value arrays
2958 * @desc_array: array of GPIO descriptors whose values will be assigned
2959 * @value_array: array of values to assign
2960 *
2961 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2962 * into account.
2963 *
2964 * This function should be called from contexts where we cannot sleep, and will
2965 * complain if the GPIO chip functions potentially sleep.
2966 */
3fff99bc
RI
2967void gpiod_set_array_value(unsigned int array_size,
2968 struct gpio_desc **desc_array, int *value_array)
5f424243
RI
2969{
2970 if (!desc_array)
2971 return;
44c7288f
LW
2972 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2973 value_array);
5f424243 2974}
3fff99bc 2975EXPORT_SYMBOL_GPL(gpiod_set_array_value);
5f424243 2976
d2876d08 2977/**
79a9becd
AC
2978 * gpiod_cansleep() - report whether gpio value access may sleep
2979 * @desc: gpio to check
d2876d08 2980 *
d2876d08 2981 */
79a9becd 2982int gpiod_cansleep(const struct gpio_desc *desc)
372e722e 2983{
fdeb8e15
LW
2984 VALIDATE_DESC(desc);
2985 return desc->gdev->chip->can_sleep;
d2876d08 2986}
79a9becd 2987EXPORT_SYMBOL_GPL(gpiod_cansleep);
d2876d08 2988
0f6d504e 2989/**
79a9becd
AC
2990 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2991 * @desc: gpio whose IRQ will be returned (already requested)
0f6d504e 2992 *
79a9becd
AC
2993 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2994 * error.
0f6d504e 2995 */
79a9becd 2996int gpiod_to_irq(const struct gpio_desc *desc)
0f6d504e 2997{
4c37ce86
LW
2998 struct gpio_chip *chip;
2999 int offset;
0f6d504e 3000
79bb71bd
LW
3001 /*
3002 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3003 * requires this function to not return zero on an invalid descriptor
3004 * but rather a negative error number.
3005 */
bfbbe44d 3006 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
79bb71bd
LW
3007 return -EINVAL;
3008
fdeb8e15 3009 chip = desc->gdev->chip;
372e722e 3010 offset = gpio_chip_hwgpio(desc);
4c37ce86
LW
3011 if (chip->to_irq) {
3012 int retirq = chip->to_irq(chip, offset);
3013
3014 /* Zero means NO_IRQ */
3015 if (!retirq)
3016 return -ENXIO;
3017
3018 return retirq;
3019 }
3020 return -ENXIO;
0f6d504e 3021}
79a9becd 3022EXPORT_SYMBOL_GPL(gpiod_to_irq);
0f6d504e 3023
d468bf9e 3024/**
e3a2e878 3025 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
d74be6df
AC
3026 * @chip: the chip the GPIO to lock belongs to
3027 * @offset: the offset of the GPIO to lock as IRQ
d468bf9e
LW
3028 *
3029 * This is used directly by GPIO drivers that want to lock down
f438acdf 3030 * a certain GPIO line to be used for IRQs.
d468bf9e 3031 */
e3a2e878 3032int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
372e722e 3033{
9c10280d
LW
3034 struct gpio_desc *desc;
3035
3036 desc = gpiochip_get_desc(chip, offset);
3037 if (IS_ERR(desc))
3038 return PTR_ERR(desc);
3039
60f8339e
LW
3040 /*
3041 * If it's fast: flush the direction setting if something changed
3042 * behind our back
3043 */
3044 if (!chip->can_sleep && chip->get_direction) {
9c10280d
LW
3045 int dir = chip->get_direction(chip, offset);
3046
3047 if (dir)
3048 clear_bit(FLAG_IS_OUT, &desc->flags);
3049 else
3050 set_bit(FLAG_IS_OUT, &desc->flags);
3051 }
d468bf9e 3052
9c10280d 3053 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
d74be6df 3054 chip_err(chip,
d468bf9e
LW
3055 "%s: tried to flag a GPIO set as output for IRQ\n",
3056 __func__);
3057 return -EIO;
3058 }
3059
9c10280d 3060 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3940c34a
LW
3061
3062 /*
3063 * If the consumer has not set up a label (such as when the
3064 * IRQ is referenced from .to_irq()) we set up a label here
3065 * so it is clear this is used as an interrupt.
3066 */
3067 if (!desc->label)
3068 desc_set_label(desc, "interrupt");
3069
d468bf9e 3070 return 0;
372e722e 3071}
e3a2e878 3072EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
d2876d08 3073
d468bf9e 3074/**
e3a2e878 3075 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
d74be6df
AC
3076 * @chip: the chip the GPIO to lock belongs to
3077 * @offset: the offset of the GPIO to lock as IRQ
d468bf9e
LW
3078 *
3079 * This is used directly by GPIO drivers that want to indicate
3080 * that a certain GPIO is no longer used exclusively for IRQ.
d2876d08 3081 */
e3a2e878 3082void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
d468bf9e 3083{
3940c34a
LW
3084 struct gpio_desc *desc;
3085
3086 desc = gpiochip_get_desc(chip, offset);
3087 if (IS_ERR(desc))
d468bf9e 3088 return;
d2876d08 3089
3940c34a
LW
3090 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3091
3092 /* If we only had this marking, erase it */
3093 if (desc->label && !strcmp(desc->label, "interrupt"))
3094 desc_set_label(desc, NULL);
d468bf9e 3095}
e3a2e878 3096EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
d468bf9e 3097
6cee3821
LW
3098bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3099{
3100 if (offset >= chip->ngpio)
3101 return false;
3102
3103 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3104}
3105EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3106
143b65d6
LW
3107bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3108{
3109 if (offset >= chip->ngpio)
3110 return false;
3111
3112 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3113}
3114EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3115
3116bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3117{
3118 if (offset >= chip->ngpio)
3119 return false;
3120
3121 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3122}
3123EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3124
05f479bf
CK
3125bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3126{
3127 if (offset >= chip->ngpio)
3128 return false;
3129
2cbfca66 3130 return !test_bit(FLAG_SLEEP_MAY_LOSE_VALUE,
05f479bf
CK
3131 &chip->gpiodev->descs[offset].flags);
3132}
3133EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3134
79a9becd
AC
3135/**
3136 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3137 * @desc: gpio whose value will be returned
3138 *
3139 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
e20538b8 3140 * its ACTIVE_LOW status, or negative errno on failure.
79a9becd
AC
3141 *
3142 * This function is to be called from contexts that can sleep.
d2876d08 3143 */
79a9becd 3144int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
d2876d08 3145{
d2876d08 3146 might_sleep_if(extra_checks);
fdeb8e15 3147 VALIDATE_DESC(desc);
fac9d885 3148 return gpiod_get_raw_value_commit(desc);
d2876d08 3149}
79a9becd 3150EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
372e722e 3151
79a9becd
AC
3152/**
3153 * gpiod_get_value_cansleep() - return a gpio's value
3154 * @desc: gpio whose value will be returned
3155 *
3156 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
e20538b8 3157 * account, or negative errno on failure.
79a9becd
AC
3158 *
3159 * This function is to be called from contexts that can sleep.
3160 */
3161int gpiod_get_value_cansleep(const struct gpio_desc *desc)
d2876d08 3162{
3f397c21 3163 int value;
d2876d08
DB
3164
3165 might_sleep_if(extra_checks);
fdeb8e15 3166 VALIDATE_DESC(desc);
fac9d885 3167 value = gpiod_get_raw_value_commit(desc);
e20538b8
BA
3168 if (value < 0)
3169 return value;
3170
79a9becd
AC
3171 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3172 value = !value;
3173
3f397c21 3174 return value;
d2876d08 3175}
79a9becd 3176EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
372e722e 3177
eec1d566
LW
3178/**
3179 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3180 * @array_size: number of elements in the descriptor / value arrays
3181 * @desc_array: array of GPIO descriptors whose values will be read
3182 * @value_array: array to store the read values
3183 *
3184 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3185 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3186 * else an error code.
3187 *
3188 * This function is to be called from contexts that can sleep.
3189 */
3190int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3191 struct gpio_desc **desc_array,
3192 int *value_array)
3193{
3194 might_sleep_if(extra_checks);
3195 if (!desc_array)
3196 return -EINVAL;
3197 return gpiod_get_array_value_complex(true, true, array_size,
3198 desc_array, value_array);
3199}
3200EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3201
3202/**
3203 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3204 * @array_size: number of elements in the descriptor / value arrays
3205 * @desc_array: array of GPIO descriptors whose values will be read
3206 * @value_array: array to store the read values
3207 *
3208 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3209 * into account. Return 0 in case of success, else an error code.
3210 *
3211 * This function is to be called from contexts that can sleep.
3212 */
3213int gpiod_get_array_value_cansleep(unsigned int array_size,
3214 struct gpio_desc **desc_array,
3215 int *value_array)
3216{
3217 might_sleep_if(extra_checks);
3218 if (!desc_array)
3219 return -EINVAL;
3220 return gpiod_get_array_value_complex(false, true, array_size,
3221 desc_array, value_array);
3222}
3223EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3224
79a9becd
AC
3225/**
3226 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3227 * @desc: gpio whose value will be assigned
3228 * @value: value to assign
3229 *
3230 * Set the raw value of the GPIO, i.e. the value of its physical line without
3231 * regard for its ACTIVE_LOW status.
3232 *
3233 * This function is to be called from contexts that can sleep.
3234 */
3235void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
372e722e 3236{
d2876d08 3237 might_sleep_if(extra_checks);
fdeb8e15 3238 VALIDATE_DESC_VOID(desc);
fac9d885 3239 gpiod_set_raw_value_commit(desc, value);
372e722e 3240}
79a9becd 3241EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
d2876d08 3242
79a9becd
AC
3243/**
3244 * gpiod_set_value_cansleep() - assign a gpio's value
3245 * @desc: gpio whose value will be assigned
3246 * @value: value to assign
3247 *
3248 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3249 * account
3250 *
3251 * This function is to be called from contexts that can sleep.
3252 */
3253void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
d2876d08 3254{
d2876d08 3255 might_sleep_if(extra_checks);
fdeb8e15 3256 VALIDATE_DESC_VOID(desc);
79a9becd
AC
3257 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3258 value = !value;
fac9d885 3259 gpiod_set_raw_value_commit(desc, value);
372e722e 3260}
79a9becd 3261EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
d2876d08 3262
5f424243 3263/**
3fff99bc 3264 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
5f424243
RI
3265 * @array_size: number of elements in the descriptor / value arrays
3266 * @desc_array: array of GPIO descriptors whose values will be assigned
3267 * @value_array: array of values to assign
3268 *
3269 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3270 * without regard for their ACTIVE_LOW status.
3271 *
3272 * This function is to be called from contexts that can sleep.
3273 */
3fff99bc
RI
3274void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3275 struct gpio_desc **desc_array,
3276 int *value_array)
5f424243
RI
3277{
3278 might_sleep_if(extra_checks);
3279 if (!desc_array)
3280 return;
44c7288f
LW
3281 gpiod_set_array_value_complex(true, true, array_size, desc_array,
3282 value_array);
5f424243 3283}
3fff99bc 3284EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
5f424243 3285
3946d187
DT
3286/**
3287 * gpiod_add_lookup_tables() - register GPIO device consumers
3288 * @tables: list of tables of consumers to register
3289 * @n: number of tables in the list
3290 */
3291void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3292{
3293 unsigned int i;
3294
3295 mutex_lock(&gpio_lookup_lock);
3296
3297 for (i = 0; i < n; i++)
3298 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3299
3300 mutex_unlock(&gpio_lookup_lock);
3301}
3302
5f424243 3303/**
3fff99bc 3304 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
5f424243
RI
3305 * @array_size: number of elements in the descriptor / value arrays
3306 * @desc_array: array of GPIO descriptors whose values will be assigned
3307 * @value_array: array of values to assign
3308 *
3309 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3310 * into account.
3311 *
3312 * This function is to be called from contexts that can sleep.
3313 */
3fff99bc
RI
3314void gpiod_set_array_value_cansleep(unsigned int array_size,
3315 struct gpio_desc **desc_array,
3316 int *value_array)
5f424243
RI
3317{
3318 might_sleep_if(extra_checks);
3319 if (!desc_array)
3320 return;
44c7288f
LW
3321 gpiod_set_array_value_complex(false, true, array_size, desc_array,
3322 value_array);
5f424243 3323}
3fff99bc 3324EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
5f424243 3325
bae48da2 3326/**
ad824783
AC
3327 * gpiod_add_lookup_table() - register GPIO device consumers
3328 * @table: table of consumers to register
bae48da2 3329 */
ad824783 3330void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
bae48da2
AC
3331{
3332 mutex_lock(&gpio_lookup_lock);
3333
ad824783 3334 list_add_tail(&table->list, &gpio_lookup_list);
bae48da2
AC
3335
3336 mutex_unlock(&gpio_lookup_lock);
3337}
226b2242 3338EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
bae48da2 3339
be9015ab
SK
3340/**
3341 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3342 * @table: table of consumers to unregister
3343 */
3344void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3345{
3346 mutex_lock(&gpio_lookup_lock);
3347
3348 list_del(&table->list);
3349
3350 mutex_unlock(&gpio_lookup_lock);
3351}
226b2242 3352EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
be9015ab 3353
ad824783 3354static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
bae48da2
AC
3355{
3356 const char *dev_id = dev ? dev_name(dev) : NULL;
ad824783 3357 struct gpiod_lookup_table *table;
bae48da2
AC
3358
3359 mutex_lock(&gpio_lookup_lock);
3360
ad824783
AC
3361 list_for_each_entry(table, &gpio_lookup_list, list) {
3362 if (table->dev_id && dev_id) {
3363 /*
3364 * Valid strings on both ends, must be identical to have
3365 * a match
3366 */
3367 if (!strcmp(table->dev_id, dev_id))
3368 goto found;
3369 } else {
3370 /*
3371 * One of the pointers is NULL, so both must be to have
3372 * a match
3373 */
3374 if (dev_id == table->dev_id)
3375 goto found;
3376 }
3377 }
3378 table = NULL;
bae48da2 3379
ad824783
AC
3380found:
3381 mutex_unlock(&gpio_lookup_lock);
3382 return table;
3383}
bae48da2 3384
ad824783
AC
3385static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3386 unsigned int idx,
3387 enum gpio_lookup_flags *flags)
3388{
2a3cf6a3 3389 struct gpio_desc *desc = ERR_PTR(-ENOENT);
ad824783
AC
3390 struct gpiod_lookup_table *table;
3391 struct gpiod_lookup *p;
bae48da2 3392
ad824783
AC
3393 table = gpiod_find_lookup_table(dev);
3394 if (!table)
3395 return desc;
bae48da2 3396
ad824783
AC
3397 for (p = &table->table[0]; p->chip_label; p++) {
3398 struct gpio_chip *chip;
bae48da2 3399
ad824783 3400 /* idx must always match exactly */
bae48da2
AC
3401 if (p->idx != idx)
3402 continue;
3403
ad824783
AC
3404 /* If the lookup entry has a con_id, require exact match */
3405 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3406 continue;
bae48da2 3407
ad824783 3408 chip = find_chip_by_name(p->chip_label);
bae48da2 3409
ad824783 3410 if (!chip) {
2a3cf6a3
AC
3411 dev_err(dev, "cannot find GPIO chip %s\n",
3412 p->chip_label);
3413 return ERR_PTR(-ENODEV);
ad824783 3414 }
bae48da2 3415
ad824783 3416 if (chip->ngpio <= p->chip_hwnum) {
2a3cf6a3
AC
3417 dev_err(dev,
3418 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3419 idx, chip->ngpio, chip->label);
3420 return ERR_PTR(-EINVAL);
bae48da2 3421 }
bae48da2 3422
bb1e88cc 3423 desc = gpiochip_get_desc(chip, p->chip_hwnum);
ad824783 3424 *flags = p->flags;
bae48da2 3425
2a3cf6a3 3426 return desc;
bae48da2
AC
3427 }
3428
bae48da2
AC
3429 return desc;
3430}
3431
66858527
RI
3432static int dt_gpio_count(struct device *dev, const char *con_id)
3433{
3434 int ret;
3435 char propname[32];
3436 unsigned int i;
3437
3438 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3439 if (con_id)
3440 snprintf(propname, sizeof(propname), "%s-%s",
3441 con_id, gpio_suffixes[i]);
3442 else
3443 snprintf(propname, sizeof(propname), "%s",
3444 gpio_suffixes[i]);
3445
3446 ret = of_gpio_named_count(dev->of_node, propname);
4033d4a4 3447 if (ret > 0)
66858527
RI
3448 break;
3449 }
4033d4a4 3450 return ret ? ret : -ENOENT;
66858527
RI
3451}
3452
3453static int platform_gpio_count(struct device *dev, const char *con_id)
3454{
3455 struct gpiod_lookup_table *table;
3456 struct gpiod_lookup *p;
3457 unsigned int count = 0;
3458
3459 table = gpiod_find_lookup_table(dev);
3460 if (!table)
3461 return -ENOENT;
3462
3463 for (p = &table->table[0]; p->chip_label; p++) {
3464 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3465 (!con_id && !p->con_id))
3466 count++;
3467 }
3468 if (!count)
3469 return -ENOENT;
3470
3471 return count;
3472}
3473
3474/**
3475 * gpiod_count - return the number of GPIOs associated with a device / function
3476 * or -ENOENT if no GPIO has been assigned to the requested function
3477 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3478 * @con_id: function within the GPIO consumer
3479 */
3480int gpiod_count(struct device *dev, const char *con_id)
3481{
3482 int count = -ENOENT;
3483
3484 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3485 count = dt_gpio_count(dev, con_id);
3486 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3487 count = acpi_gpio_count(dev, con_id);
3488
3489 if (count < 0)
3490 count = platform_gpio_count(dev, con_id);
3491
3492 return count;
3493}
3494EXPORT_SYMBOL_GPL(gpiod_count);
3495
bae48da2 3496/**
0879162f 3497 * gpiod_get - obtain a GPIO for a given GPIO function
ad824783 3498 * @dev: GPIO consumer, can be NULL for system-global GPIOs
bae48da2 3499 * @con_id: function within the GPIO consumer
39b2bbe3 3500 * @flags: optional GPIO initialization flags
bae48da2
AC
3501 *
3502 * Return the GPIO descriptor corresponding to the function con_id of device
2a3cf6a3 3503 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
20a8a968 3504 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
bae48da2 3505 */
b17d1bf1 3506struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
39b2bbe3 3507 enum gpiod_flags flags)
bae48da2 3508{
39b2bbe3 3509 return gpiod_get_index(dev, con_id, 0, flags);
bae48da2 3510}
b17d1bf1 3511EXPORT_SYMBOL_GPL(gpiod_get);
bae48da2 3512
29a1f233
TR
3513/**
3514 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3515 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3516 * @con_id: function within the GPIO consumer
39b2bbe3 3517 * @flags: optional GPIO initialization flags
29a1f233
TR
3518 *
3519 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3520 * the requested function it will return NULL. This is convenient for drivers
3521 * that need to handle optional GPIOs.
3522 */
b17d1bf1 3523struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
39b2bbe3
AC
3524 const char *con_id,
3525 enum gpiod_flags flags)
29a1f233 3526{
39b2bbe3 3527 return gpiod_get_index_optional(dev, con_id, 0, flags);
29a1f233 3528}
b17d1bf1 3529EXPORT_SYMBOL_GPL(gpiod_get_optional);
29a1f233 3530
f625d460
BP
3531
3532/**
3533 * gpiod_configure_flags - helper function to configure a given GPIO
3534 * @desc: gpio whose value will be assigned
3535 * @con_id: function within the GPIO consumer
85b03b30
JH
3536 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3537 * of_get_gpio_hog()
f625d460
BP
3538 * @dflags: gpiod_flags - optional GPIO initialization flags
3539 *
3540 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3541 * requested function and/or index, or another IS_ERR() code if an error
3542 * occurred while trying to acquire the GPIO.
3543 */
c29fd9eb 3544int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
85b03b30 3545 unsigned long lflags, enum gpiod_flags dflags)
f625d460
BP
3546{
3547 int status;
3548
85b03b30
JH
3549 if (lflags & GPIO_ACTIVE_LOW)
3550 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3551 if (lflags & GPIO_OPEN_DRAIN)
3552 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3553 if (lflags & GPIO_OPEN_SOURCE)
3554 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2cbfca66
AJ
3555 if (lflags & GPIO_SLEEP_MAY_LOSE_VALUE)
3556 set_bit(FLAG_SLEEP_MAY_LOSE_VALUE, &desc->flags);
85b03b30 3557
f625d460
BP
3558 /* No particular flag request, return here... */
3559 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3560 pr_debug("no flags found for %s\n", con_id);
3561 return 0;
3562 }
3563
3564 /* Process flags */
3565 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3566 status = gpiod_direction_output(desc,
ad17731d 3567 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
f625d460
BP
3568 else
3569 status = gpiod_direction_input(desc);
3570
3571 return status;
3572}
3573
bae48da2
AC
3574/**
3575 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
fdd6a5fe 3576 * @dev: GPIO consumer, can be NULL for system-global GPIOs
bae48da2
AC
3577 * @con_id: function within the GPIO consumer
3578 * @idx: index of the GPIO to obtain in the consumer
39b2bbe3 3579 * @flags: optional GPIO initialization flags
bae48da2
AC
3580 *
3581 * This variant of gpiod_get() allows to access GPIOs other than the first
3582 * defined one for functions that define several GPIOs.
3583 *
2a3cf6a3
AC
3584 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3585 * requested function and/or index, or another IS_ERR() code if an error
20a8a968 3586 * occurred while trying to acquire the GPIO.
bae48da2 3587 */
b17d1bf1 3588struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
bae48da2 3589 const char *con_id,
39b2bbe3
AC
3590 unsigned int idx,
3591 enum gpiod_flags flags)
bae48da2 3592{
35c5d7fd 3593 struct gpio_desc *desc = NULL;
bae48da2 3594 int status;
39b2bbe3 3595 enum gpio_lookup_flags lookupflags = 0;
bae48da2
AC
3596
3597 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3598
4d8440b9
RW
3599 if (dev) {
3600 /* Using device tree? */
3601 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3602 dev_dbg(dev, "using device tree for GPIO lookup\n");
3603 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3604 } else if (ACPI_COMPANION(dev)) {
3605 dev_dbg(dev, "using ACPI for GPIO lookup\n");
a31f5c3a 3606 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
4d8440b9 3607 }
35c5d7fd
AC
3608 }
3609
3610 /*
3611 * Either we are not using DT or ACPI, or their lookup did not return
3612 * a result. In that case, use platform lookup as a fallback.
3613 */
2a3cf6a3 3614 if (!desc || desc == ERR_PTR(-ENOENT)) {
43a8785a 3615 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
39b2bbe3 3616 desc = gpiod_find(dev, con_id, idx, &lookupflags);
bae48da2
AC
3617 }
3618
3619 if (IS_ERR(desc)) {
351cfe0f 3620 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
bae48da2
AC
3621 return desc;
3622 }
3623
3624 status = gpiod_request(desc, con_id);
bae48da2
AC
3625 if (status < 0)
3626 return ERR_PTR(status);
3627
85b03b30 3628 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
39b2bbe3
AC
3629 if (status < 0) {
3630 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3631 gpiod_put(desc);
3632 return ERR_PTR(status);
3633 }
3634
bae48da2
AC
3635 return desc;
3636}
b17d1bf1 3637EXPORT_SYMBOL_GPL(gpiod_get_index);
bae48da2 3638
40b73183
MW
3639/**
3640 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3641 * @fwnode: handle of the firmware node
3642 * @propname: name of the firmware property representing the GPIO
537b94da 3643 * @index: index of the GPIO to obtain in the consumer
a264d10f 3644 * @dflags: GPIO initialization flags
950d55f5 3645 * @label: label to attach to the requested GPIO
40b73183
MW
3646 *
3647 * This function can be used for drivers that get their configuration
3648 * from firmware.
3649 *
3650 * Function properly finds the corresponding GPIO using whatever is the
3651 * underlying firmware interface and then makes sure that the GPIO
3652 * descriptor is requested before it is returned to the caller.
3653 *
950d55f5 3654 * Returns:
ff21378a 3655 * On successful request the GPIO pin is configured in accordance with
a264d10f
AS
3656 * provided @dflags.
3657 *
40b73183
MW
3658 * In case of error an ERR_PTR() is returned.
3659 */
3660struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
537b94da 3661 const char *propname, int index,
b2987d74
AS
3662 enum gpiod_flags dflags,
3663 const char *label)
40b73183
MW
3664{
3665 struct gpio_desc *desc = ERR_PTR(-ENODEV);
a264d10f 3666 unsigned long lflags = 0;
40b73183 3667 bool active_low = false;
90b665f6 3668 bool single_ended = false;
4c0facdd 3669 bool open_drain = false;
40b73183
MW
3670 int ret;
3671
3672 if (!fwnode)
3673 return ERR_PTR(-EINVAL);
3674
3675 if (is_of_node(fwnode)) {
3676 enum of_gpio_flags flags;
3677
537b94da
BB
3678 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname,
3679 index, &flags);
90b665f6 3680 if (!IS_ERR(desc)) {
40b73183 3681 active_low = flags & OF_GPIO_ACTIVE_LOW;
90b665f6 3682 single_ended = flags & OF_GPIO_SINGLE_ENDED;
4c0facdd 3683 open_drain = flags & OF_GPIO_OPEN_DRAIN;
90b665f6 3684 }
40b73183
MW
3685 } else if (is_acpi_node(fwnode)) {
3686 struct acpi_gpio_info info;
3687
537b94da 3688 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
a31f5c3a 3689 if (!IS_ERR(desc)) {
52044723 3690 active_low = info.polarity == GPIO_ACTIVE_LOW;
a31f5c3a
AS
3691 ret = acpi_gpio_update_gpiod_flags(&dflags, info.flags);
3692 if (ret)
3693 pr_debug("Override GPIO initialization flags\n");
3694 }
40b73183
MW
3695 }
3696
3697 if (IS_ERR(desc))
3698 return desc;
3699
b2987d74 3700 ret = gpiod_request(desc, label);
85b03b30
JH
3701 if (ret)
3702 return ERR_PTR(ret);
3703
40b73183 3704 if (active_low)
a264d10f 3705 lflags |= GPIO_ACTIVE_LOW;
40b73183 3706
90b665f6 3707 if (single_ended) {
4c0facdd 3708 if (open_drain)
a264d10f 3709 lflags |= GPIO_OPEN_DRAIN;
90b665f6 3710 else
a264d10f
AS
3711 lflags |= GPIO_OPEN_SOURCE;
3712 }
3713
3714 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3715 if (ret < 0) {
3716 gpiod_put(desc);
3717 return ERR_PTR(ret);
90b665f6
LP
3718 }
3719
40b73183
MW
3720 return desc;
3721}
3722EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3723
29a1f233
TR
3724/**
3725 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3726 * function
3727 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3728 * @con_id: function within the GPIO consumer
3729 * @index: index of the GPIO to obtain in the consumer
39b2bbe3 3730 * @flags: optional GPIO initialization flags
29a1f233
TR
3731 *
3732 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3733 * specified index was assigned to the requested function it will return NULL.
3734 * This is convenient for drivers that need to handle optional GPIOs.
3735 */
b17d1bf1 3736struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
29a1f233 3737 const char *con_id,
39b2bbe3
AC
3738 unsigned int index,
3739 enum gpiod_flags flags)
29a1f233
TR
3740{
3741 struct gpio_desc *desc;
3742
39b2bbe3 3743 desc = gpiod_get_index(dev, con_id, index, flags);
29a1f233
TR
3744 if (IS_ERR(desc)) {
3745 if (PTR_ERR(desc) == -ENOENT)
3746 return NULL;
3747 }
3748
3749 return desc;
3750}
b17d1bf1 3751EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
29a1f233 3752
f625d460
BP
3753/**
3754 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3755 * @desc: gpio whose value will be assigned
3756 * @name: gpio line name
3757 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3758 * of_get_gpio_hog()
3759 * @dflags: gpiod_flags - optional GPIO initialization flags
3760 */
3761int gpiod_hog(struct gpio_desc *desc, const char *name,
3762 unsigned long lflags, enum gpiod_flags dflags)
3763{
3764 struct gpio_chip *chip;
3765 struct gpio_desc *local_desc;
3766 int hwnum;
3767 int status;
3768
3769 chip = gpiod_to_chip(desc);
3770 hwnum = gpio_chip_hwgpio(desc);
3771
3772 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3773 if (IS_ERR(local_desc)) {
c31a571d
LD
3774 status = PTR_ERR(local_desc);
3775 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3776 name, chip->label, hwnum, status);
3777 return status;
f625d460
BP
3778 }
3779
85b03b30 3780 status = gpiod_configure_flags(desc, name, lflags, dflags);
f625d460 3781 if (status < 0) {
c31a571d
LD
3782 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3783 name, chip->label, hwnum, status);
f625d460
BP
3784 gpiochip_free_own_desc(desc);
3785 return status;
3786 }
3787
3788 /* Mark GPIO as hogged so it can be identified and removed later */
3789 set_bit(FLAG_IS_HOGGED, &desc->flags);
3790
3791 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3792 desc_to_gpio(desc), name,
3793 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3794 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3795 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3796
3797 return 0;
3798}
3799
3800/**
3801 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3802 * @chip: gpio chip to act on
3803 *
3804 * This is only used by of_gpiochip_remove to free hogged gpios
3805 */
3806static void gpiochip_free_hogs(struct gpio_chip *chip)
3807{
3808 int id;
3809
3810 for (id = 0; id < chip->ngpio; id++) {
1c3cdb18
LW
3811 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3812 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
f625d460
BP
3813 }
3814}
3815
66858527
RI
3816/**
3817 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3818 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3819 * @con_id: function within the GPIO consumer
3820 * @flags: optional GPIO initialization flags
3821 *
3822 * This function acquires all the GPIOs defined under a given function.
3823 *
3824 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3825 * no GPIO has been assigned to the requested function, or another IS_ERR()
3826 * code if an error occurred while trying to acquire the GPIOs.
3827 */
3828struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3829 const char *con_id,
3830 enum gpiod_flags flags)
3831{
3832 struct gpio_desc *desc;
3833 struct gpio_descs *descs;
3834 int count;
3835
3836 count = gpiod_count(dev, con_id);
3837 if (count < 0)
3838 return ERR_PTR(count);
3839
3840 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3841 GFP_KERNEL);
3842 if (!descs)
3843 return ERR_PTR(-ENOMEM);
3844
3845 for (descs->ndescs = 0; descs->ndescs < count; ) {
3846 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3847 if (IS_ERR(desc)) {
3848 gpiod_put_array(descs);
3849 return ERR_CAST(desc);
3850 }
3851 descs->desc[descs->ndescs] = desc;
3852 descs->ndescs++;
3853 }
3854 return descs;
3855}
3856EXPORT_SYMBOL_GPL(gpiod_get_array);
3857
3858/**
3859 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3860 * function
3861 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3862 * @con_id: function within the GPIO consumer
3863 * @flags: optional GPIO initialization flags
3864 *
3865 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3866 * assigned to the requested function it will return NULL.
3867 */
3868struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3869 const char *con_id,
3870 enum gpiod_flags flags)
3871{
3872 struct gpio_descs *descs;
3873
3874 descs = gpiod_get_array(dev, con_id, flags);
3875 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3876 return NULL;
3877
3878 return descs;
3879}
3880EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3881
bae48da2
AC
3882/**
3883 * gpiod_put - dispose of a GPIO descriptor
3884 * @desc: GPIO descriptor to dispose of
3885 *
3886 * No descriptor can be used after gpiod_put() has been called on it.
3887 */
3888void gpiod_put(struct gpio_desc *desc)
3889{
3890 gpiod_free(desc);
372e722e 3891}
bae48da2 3892EXPORT_SYMBOL_GPL(gpiod_put);
d2876d08 3893
66858527
RI
3894/**
3895 * gpiod_put_array - dispose of multiple GPIO descriptors
3896 * @descs: struct gpio_descs containing an array of descriptors
3897 */
3898void gpiod_put_array(struct gpio_descs *descs)
3899{
3900 unsigned int i;
3901
3902 for (i = 0; i < descs->ndescs; i++)
3903 gpiod_put(descs->desc[i]);
3904
3905 kfree(descs);
3906}
3907EXPORT_SYMBOL_GPL(gpiod_put_array);
3908
3c702e99
LW
3909static int __init gpiolib_dev_init(void)
3910{
3911 int ret;
3912
3913 /* Register GPIO sysfs bus */
3914 ret = bus_register(&gpio_bus_type);
3915 if (ret < 0) {
3916 pr_err("gpiolib: could not register GPIO bus type\n");
3917 return ret;
3918 }
3919
3920 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3921 if (ret < 0) {
3922 pr_err("gpiolib: failed to allocate char dev region\n");
3923 bus_unregister(&gpio_bus_type);
159f3cd9
GR
3924 } else {
3925 gpiolib_initialized = true;
3926 gpiochip_setup_devs();
3c702e99
LW
3927 }
3928 return ret;
3929}
3930core_initcall(gpiolib_dev_init);
3931
d2876d08
DB
3932#ifdef CONFIG_DEBUG_FS
3933
fdeb8e15 3934static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
d2876d08
DB
3935{
3936 unsigned i;
fdeb8e15
LW
3937 struct gpio_chip *chip = gdev->chip;
3938 unsigned gpio = gdev->base;
3939 struct gpio_desc *gdesc = &gdev->descs[0];
d2876d08 3940 int is_out;
d468bf9e 3941 int is_irq;
d2876d08 3942
fdeb8e15 3943 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
ced433e2
MP
3944 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3945 if (gdesc->name) {
3946 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3947 gpio, gdesc->name);
3948 }
d2876d08 3949 continue;
ced433e2 3950 }
d2876d08 3951
372e722e 3952 gpiod_get_direction(gdesc);
d2876d08 3953 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
d468bf9e 3954 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
ced433e2
MP
3955 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3956 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
d2876d08
DB
3957 is_out ? "out" : "in ",
3958 chip->get
3959 ? (chip->get(chip, i) ? "hi" : "lo")
d468bf9e
LW
3960 : "? ",
3961 is_irq ? "IRQ" : " ");
d2876d08
DB
3962 seq_printf(s, "\n");
3963 }
3964}
3965
f9c4a31f 3966static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
d2876d08 3967{
362432ae 3968 unsigned long flags;
ff2b1359 3969 struct gpio_device *gdev = NULL;
cb1650d4 3970 loff_t index = *pos;
d2876d08 3971
f9c4a31f 3972 s->private = "";
d2876d08 3973
362432ae 3974 spin_lock_irqsave(&gpio_lock, flags);
ff2b1359 3975 list_for_each_entry(gdev, &gpio_devices, list)
362432ae
GL
3976 if (index-- == 0) {
3977 spin_unlock_irqrestore(&gpio_lock, flags);
ff2b1359 3978 return gdev;
f9c4a31f 3979 }
362432ae 3980 spin_unlock_irqrestore(&gpio_lock, flags);
f9c4a31f 3981
cb1650d4 3982 return NULL;
f9c4a31f
TR
3983}
3984
3985static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3986{
362432ae 3987 unsigned long flags;
ff2b1359 3988 struct gpio_device *gdev = v;
f9c4a31f
TR
3989 void *ret = NULL;
3990
362432ae 3991 spin_lock_irqsave(&gpio_lock, flags);
ff2b1359 3992 if (list_is_last(&gdev->list, &gpio_devices))
cb1650d4
AC
3993 ret = NULL;
3994 else
ff2b1359 3995 ret = list_entry(gdev->list.next, struct gpio_device, list);
362432ae 3996 spin_unlock_irqrestore(&gpio_lock, flags);
f9c4a31f
TR
3997
3998 s->private = "\n";
3999 ++*pos;
4000
4001 return ret;
4002}
4003
4004static void gpiolib_seq_stop(struct seq_file *s, void *v)
4005{
4006}
4007
4008static int gpiolib_seq_show(struct seq_file *s, void *v)
4009{
ff2b1359
LW
4010 struct gpio_device *gdev = v;
4011 struct gpio_chip *chip = gdev->chip;
4012 struct device *parent;
4013
4014 if (!chip) {
4015 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4016 dev_name(&gdev->dev));
4017 return 0;
4018 }
f9c4a31f 4019
ff2b1359
LW
4020 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4021 dev_name(&gdev->dev),
fdeb8e15 4022 gdev->base, gdev->base + gdev->ngpio - 1);
ff2b1359
LW
4023 parent = chip->parent;
4024 if (parent)
4025 seq_printf(s, ", parent: %s/%s",
4026 parent->bus ? parent->bus->name : "no-bus",
4027 dev_name(parent));
f9c4a31f
TR
4028 if (chip->label)
4029 seq_printf(s, ", %s", chip->label);
4030 if (chip->can_sleep)
4031 seq_printf(s, ", can sleep");
4032 seq_printf(s, ":\n");
4033
4034 if (chip->dbg_show)
4035 chip->dbg_show(s, chip);
4036 else
fdeb8e15 4037 gpiolib_dbg_show(s, gdev);
f9c4a31f 4038
d2876d08
DB
4039 return 0;
4040}
4041
f9c4a31f
TR
4042static const struct seq_operations gpiolib_seq_ops = {
4043 .start = gpiolib_seq_start,
4044 .next = gpiolib_seq_next,
4045 .stop = gpiolib_seq_stop,
4046 .show = gpiolib_seq_show,
4047};
4048
d2876d08
DB
4049static int gpiolib_open(struct inode *inode, struct file *file)
4050{
f9c4a31f 4051 return seq_open(file, &gpiolib_seq_ops);
d2876d08
DB
4052}
4053
828c0950 4054static const struct file_operations gpiolib_operations = {
f9c4a31f 4055 .owner = THIS_MODULE,
d2876d08
DB
4056 .open = gpiolib_open,
4057 .read = seq_read,
4058 .llseek = seq_lseek,
f9c4a31f 4059 .release = seq_release,
d2876d08
DB
4060};
4061
4062static int __init gpiolib_debugfs_init(void)
4063{
4064 /* /sys/kernel/debug/gpio */
4065 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4066 NULL, NULL, &gpiolib_operations);
4067 return 0;
4068}
4069subsys_initcall(gpiolib_debugfs_init);
4070
4071#endif /* DEBUG_FS */