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