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