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