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