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