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