blob: abdf448b11a3d76f5793eb6ad7cefcc29fe91ac0 [file] [log] [blame]
Googler9398cc32022-12-02 17:21:52 +08001// SPDX-License-Identifier: GPL-2.0
2#include <linux/bitmap.h>
Googleraf606d22022-10-26 21:40:12 -07003#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"
Googler9398cc32022-12-02 17:21:52 +080032#include "gpiolib-of.h"
33#include "gpiolib-acpi.h"
Googleraf606d22022-10-26 21:40:12 -070034
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 */
59static DEFINE_IDA(gpio_ida);
60static dev_t gpio_devt;
61#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
62static struct bus_type gpio_bus_type = {
63 .name = "gpio",
64};
65
Googler9398cc32022-12-02 17:21:52 +080066/*
67 * Number of GPIOs to use for the fast path in set array
68 */
69#define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
70
Googleraf606d22022-10-26 21:40:12 -070071/* 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 */
75DEFINE_SPINLOCK(gpio_lock);
76
77static DEFINE_MUTEX(gpio_lookup_lock);
78static LIST_HEAD(gpio_lookup_list);
79LIST_HEAD(gpio_devices);
80
Googler9398cc32022-12-02 17:21:52 +080081static DEFINE_MUTEX(gpio_machine_hogs_mutex);
82static LIST_HEAD(gpio_machine_hogs);
83
Googleraf606d22022-10-26 21:40:12 -070084static void gpiochip_free_hogs(struct gpio_chip *chip);
Googler9398cc32022-12-02 17:21:52 +080085static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
86 struct lock_class_key *lock_key,
87 struct lock_class_key *request_key);
Googleraf606d22022-10-26 21:40:12 -070088static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Googler9398cc32022-12-02 17:21:52 +080089static int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip);
Googleraf606d22022-10-26 21:40:12 -070090static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
91static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
92
93static bool gpiolib_initialized;
94
95static inline void desc_set_label(struct gpio_desc *d, const char *label)
96{
97 d->label = label;
98}
99
100/**
Googler9398cc32022-12-02 17:21:52 +0800101 * 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.
Googleraf606d22022-10-26 21:40:12 -0700107 */
108struct 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}
130EXPORT_SYMBOL_GPL(gpio_to_desc);
131
132/**
Googler9398cc32022-12-02 17:21:52 +0800133 * 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.
Googleraf606d22022-10-26 21:40:12 -0700141 */
142struct 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/**
Googler9398cc32022-12-02 17:21:52 +0800154 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
155 * @desc: GPIO descriptor
156 *
Googleraf606d22022-10-26 21:40:12 -0700157 * This should disappear in the future but is needed since we still
Googler9398cc32022-12-02 17:21:52 +0800158 * use GPIO numbers for error messages and sysfs nodes.
159 *
160 * Returns:
161 * The global GPIO number for the GPIO specified by its descriptor.
Googleraf606d22022-10-26 21:40:12 -0700162 */
163int desc_to_gpio(const struct gpio_desc *desc)
164{
165 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
166}
167EXPORT_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 */
174struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
175{
Googler9398cc32022-12-02 17:21:52 +0800176 if (!desc || !desc->gdev)
Googleraf606d22022-10-26 21:40:12 -0700177 return NULL;
178 return desc->gdev->chip;
179}
180EXPORT_SYMBOL_GPL(gpiod_to_chip);
181
182/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
183static 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 *
Googler9398cc32022-12-02 17:21:52 +0800210 * Returns 0 for output, 1 for input, or an error code in case of error.
Googleraf606d22022-10-26 21:40:12 -0700211 *
212 * This function may sleep if gpiod_cansleep() is true.
213 */
214int gpiod_get_direction(struct gpio_desc *desc)
215{
Googler9398cc32022-12-02 17:21:52 +0800216 struct gpio_chip *chip;
217 unsigned offset;
218 int ret;
Googleraf606d22022-10-26 21:40:12 -0700219
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)
Googler9398cc32022-12-02 17:21:52 +0800232 return -ENOTSUPP;
Googleraf606d22022-10-26 21:40:12 -0700233
Googler9398cc32022-12-02 17:21:52 +0800234 ret = chip->get_direction(chip, offset);
235 if (ret > 0) {
Googleraf606d22022-10-26 21:40:12 -0700236 /* GPIOF_DIR_IN, or other positive */
Googler9398cc32022-12-02 17:21:52 +0800237 ret = 1;
Googleraf606d22022-10-26 21:40:12 -0700238 clear_bit(FLAG_IS_OUT, &desc->flags);
239 }
Googler9398cc32022-12-02 17:21:52 +0800240 if (ret == 0) {
Googleraf606d22022-10-26 21:40:12 -0700241 /* GPIOF_DIR_OUT */
242 set_bit(FLAG_IS_OUT, &desc->flags);
243 }
Googler9398cc32022-12-02 17:21:52 +0800244 return ret;
Googleraf606d22022-10-26 21:40:12 -0700245}
246EXPORT_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 */
255static 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
Googler9398cc32022-12-02 17:21:52 +0800296/*
Googleraf606d22022-10-26 21:40:12 -0700297 * Convert a GPIO name to its descriptor
298 */
299static 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 */
333static 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
Googler9398cc32022-12-02 17:21:52 +0800359static 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
373static 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
385static 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
395static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
396{
397 bitmap_free(gpiochip->valid_mask);
398 gpiochip->valid_mask = NULL;
399}
400
401bool 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}
409EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
410
Googleraf606d22022-10-26 21:40:12 -0700411/*
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 */
422struct 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
436static 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;
Googler9398cc32022-12-02 17:21:52 +0800442 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
Googleraf606d22022-10-26 21:40:12 -0700443 int i;
444
445 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Googler9398cc32022-12-02 17:21:52 +0800446 /* 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;
Googleraf606d22022-10-26 21:40:12 -0700455
456 memset(&ghd, 0, sizeof(ghd));
Googler9398cc32022-12-02 17:21:52 +0800457 for (i = 0; i < lh->numdescs; i++)
458 ghd.values[i] = test_bit(i, vals);
Googleraf606d22022-10-26 21:40:12 -0700459
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) {
Googler9398cc32022-12-02 17:21:52 +0800465 /*
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;
Googleraf606d22022-10-26 21:40:12 -0700471
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++)
Googler9398cc32022-12-02 17:21:52 +0800477 __assign_bit(i, vals, ghd.values[i]);
Googleraf606d22022-10-26 21:40:12 -0700478
479 /* Reuse the array setting function */
Googler9398cc32022-12-02 17:21:52 +0800480 return gpiod_set_array_value_complex(false,
Googleraf606d22022-10-26 21:40:12 -0700481 true,
482 lh->numdescs,
483 lh->descs,
Googler9398cc32022-12-02 17:21:52 +0800484 NULL,
Googleraf606d22022-10-26 21:40:12 -0700485 vals);
486 }
487 return -EINVAL;
488}
489
490#ifdef CONFIG_COMPAT
491static 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
498static 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
512static 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
522static 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
Googler9398cc32022-12-02 17:21:52 +0800537 /* Return an error if an unknown flag is set */
538 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
539 return -EINVAL;
540
Googleraf606d22022-10-26 21:40:12 -0700541 /*
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
Googler9398cc32022-12-02 17:21:52 +0800549 /*
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
Googleraf606d22022-10-26 21:40:12 -0700564 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
Googler9398cc32022-12-02 17:21:52 +0800605 ret = gpiod_set_transitory(desc, false);
606 if (ret < 0)
607 goto out_free_descs;
608
Googleraf606d22022-10-26 21:40:12 -0700609 /*
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
664out_put_unused_fd:
665 put_unused_fd(fd);
666out_free_descs:
667 for (i = 0; i < count; i++)
668 gpiod_free(lh->descs[i]);
669 kfree(lh->label);
670out_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
Googler9398cc32022-12-02 17:21:52 +0800691 * @timestamp: cache for the timestamp storing it between hardirq
692 * and IRQ thread, used to bring the timestamp close to the actual
693 * event
Googleraf606d22022-10-26 21:40:12 -0700694 */
695struct 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;
Googler9398cc32022-12-02 17:21:52 +0800704 u64 timestamp;
Googleraf606d22022-10-26 21:40:12 -0700705};
706
707#define GPIOEVENT_REQUEST_VALID_FLAGS \
708 (GPIOEVENT_REQUEST_RISING_EDGE | \
709 GPIOEVENT_REQUEST_FALLING_EDGE)
710
Googler9398cc32022-12-02 17:21:52 +0800711static __poll_t lineevent_poll(struct file *filep,
Googleraf606d22022-10-26 21:40:12 -0700712 struct poll_table_struct *wait)
713{
714 struct lineevent_state *le = filep->private_data;
Googler9398cc32022-12-02 17:21:52 +0800715 __poll_t events = 0;
Googleraf606d22022-10-26 21:40:12 -0700716
717 poll_wait(filep, &le->wait, wait);
718
719 if (!kfifo_is_empty(&le->events))
Googler9398cc32022-12-02 17:21:52 +0800720 events = EPOLLIN | EPOLLRDNORM;
Googleraf606d22022-10-26 21:40:12 -0700721
722 return events;
723}
724
725
726static 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
771static 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
784static 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
814static 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
821static 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
833static irqreturn_t lineevent_irq_thread(int irq, void *p)
834{
835 struct lineevent_state *le = p;
836 struct gpioevent_data ge;
Googler9398cc32022-12-02 17:21:52 +0800837 int ret;
Googleraf606d22022-10-26 21:40:12 -0700838
839 /* Do not leak kernel stack to userspace */
840 memset(&ge, 0, sizeof(ge));
841
Googler9398cc32022-12-02 17:21:52 +0800842 /*
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;
Googleraf606d22022-10-26 21:40:12 -0700850
851 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
852 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Googler9398cc32022-12-02 17:21:52 +0800853 int level = gpiod_get_value_cansleep(le->desc);
Googleraf606d22022-10-26 21:40:12 -0700854 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;
Googler9398cc32022-12-02 17:21:52 +0800860 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
Googleraf606d22022-10-26 21:40:12 -0700861 /* Emit low-to-high event */
862 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Googler9398cc32022-12-02 17:21:52 +0800863 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Googleraf606d22022-10-26 21:40:12 -0700864 /* 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);
Googler9398cc32022-12-02 17:21:52 +0800871 if (ret)
872 wake_up_poll(&le->wait, EPOLLIN);
Googleraf606d22022-10-26 21:40:12 -0700873
874 return IRQ_HANDLED;
875}
876
Googler9398cc32022-12-02 17:21:52 +0800877static 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
Googleraf606d22022-10-26 21:40:12 -0700890static 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,
Googler9398cc32022-12-02 17:21:52 +0800981 lineevent_irq_handler,
Googleraf606d22022-10-26 21:40:12 -0700982 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
1019out_put_unused_fd:
1020 put_unused_fd(fd);
1021out_free_irq:
1022 free_irq(le->irq, le);
1023out_free_desc:
1024 gpiod_free(le->desc);
1025out_free_label:
1026 kfree(le->label);
1027out_free_le:
1028 kfree(le);
1029 put_device(&gdev->dev);
1030 return ret;
1031}
1032
Googler9398cc32022-12-02 17:21:52 +08001033/*
Googleraf606d22022-10-26 21:40:12 -07001034 * gpio_ioctl() - ioctl handler for the GPIO chardev
1035 */
1036static 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) ||
Googler9398cc32022-12-02 17:21:52 +08001096 test_bit(FLAG_SYSFS, &desc->flags) ||
1097 !pinctrl_gpio_can_use_line(chip->base + lineinfo.line_offset))
Googleraf606d22022-10-26 21:40:12 -07001098 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
1122static 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 */
1135static 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 */
Googler9398cc32022-12-02 17:21:52 +08001141 if (!gdev->chip)
Googleraf606d22022-10-26 21:40:12 -07001142 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 */
1155static 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
1165static 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
1176static 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);
Googler9398cc32022-12-02 17:21:52 +08001182 kfree_const(gdev->label);
Googleraf606d22022-10-26 21:40:12 -07001183 kfree(gdev->descs);
1184 kfree(gdev);
1185}
1186
1187static int gpiochip_setup_dev(struct gpio_device *gdev)
1188{
Googler9398cc32022-12-02 17:21:52 +08001189 int ret;
Googleraf606d22022-10-26 21:40:12 -07001190
1191 cdev_init(&gdev->chrdev, &gpio_fileops);
1192 gdev->chrdev.owner = THIS_MODULE;
1193 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
1194
Googler9398cc32022-12-02 17:21:52 +08001195 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)
Googleraf606d22022-10-26 21:40:12 -07001204 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
1214err_remove_device:
Googler9398cc32022-12-02 17:21:52 +08001215 cdev_device_del(&gdev->chrdev, &gdev->dev);
1216 return ret;
1217}
1218
1219static 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
1240static 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);
Googleraf606d22022-10-26 21:40:12 -07001252}
1253
1254static void gpiochip_setup_devs(void)
1255{
1256 struct gpio_device *gdev;
Googler9398cc32022-12-02 17:21:52 +08001257 int ret;
Googleraf606d22022-10-26 21:40:12 -07001258
1259 list_for_each_entry(gdev, &gpio_devices, list) {
Googler9398cc32022-12-02 17:21:52 +08001260 ret = gpiochip_setup_dev(gdev);
1261 if (ret)
Googleraf606d22022-10-26 21:40:12 -07001262 pr_err("%s: Failed to initialize gpio device (%d)\n",
Googler9398cc32022-12-02 17:21:52 +08001263 dev_name(&gdev->dev), ret);
Googleraf606d22022-10-26 21:40:12 -07001264 }
1265}
1266
Googler9398cc32022-12-02 17:21:52 +08001267int 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)
Googleraf606d22022-10-26 21:40:12 -07001270{
1271 unsigned long flags;
Googler9398cc32022-12-02 17:21:52 +08001272 int ret = 0;
Googleraf606d22022-10-26 21:40:12 -07001273 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;
Googler9398cc32022-12-02 17:21:52 +08001296 else
1297 chip->of_node = gdev->dev.of_node;
Googleraf606d22022-10-26 21:40:12 -07001298#endif
1299
1300 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1301 if (gdev->id < 0) {
Googler9398cc32022-12-02 17:21:52 +08001302 ret = gdev->id;
Googleraf606d22022-10-26 21:40:12 -07001303 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) {
Googler9398cc32022-12-02 17:21:52 +08001318 ret = -ENOMEM;
Googleraf606d22022-10-26 21:40:12 -07001319 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");
Googler9398cc32022-12-02 17:21:52 +08001324 ret = -EINVAL;
Googleraf606d22022-10-26 21:40:12 -07001325 goto err_free_descs;
1326 }
1327
Googler9398cc32022-12-02 17:21:52 +08001328 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);
Googleraf606d22022-10-26 21:40:12 -07001333 if (!gdev->label) {
Googler9398cc32022-12-02 17:21:52 +08001334 ret = -ENOMEM;
Googleraf606d22022-10-26 21:40:12 -07001335 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) {
Googler9398cc32022-12-02 17:21:52 +08001353 ret = base;
Googleraf606d22022-10-26 21:40:12 -07001354 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
Googler9398cc32022-12-02 17:21:52 +08001367 ret = gpiodev_add_to_list(gdev);
1368 if (ret) {
Googleraf606d22022-10-26 21:40:12 -07001369 spin_unlock_irqrestore(&gpio_lock, flags);
1370 goto err_free_label;
1371 }
1372
1373 spin_unlock_irqrestore(&gpio_lock, flags);
1374
Googler9398cc32022-12-02 17:21:52 +08001375 for (i = 0; i < chip->ngpio; i++)
1376 gdev->descs[i].gdev = gdev;
Googleraf606d22022-10-26 21:40:12 -07001377
1378#ifdef CONFIG_PINCTRL
1379 INIT_LIST_HEAD(&gdev->pin_ranges);
1380#endif
1381
Googler9398cc32022-12-02 17:21:52 +08001382 ret = gpiochip_set_desc_names(chip);
1383 if (ret)
Googleraf606d22022-10-26 21:40:12 -07001384 goto err_remove_from_list;
1385
Googler9398cc32022-12-02 17:21:52 +08001386 ret = gpiochip_alloc_valid_mask(chip);
1387 if (ret)
Googleraf606d22022-10-26 21:40:12 -07001388 goto err_remove_from_list;
1389
Googler9398cc32022-12-02 17:21:52 +08001390 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 }
Googleraf606d22022-10-26 21:40:12 -07001413
1414 acpi_gpiochip_add(chip);
1415
Googler9398cc32022-12-02 17:21:52 +08001416 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
Googleraf606d22022-10-26 21:40:12 -07001430 /*
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) {
Googler9398cc32022-12-02 17:21:52 +08001439 ret = gpiochip_setup_dev(gdev);
1440 if (ret)
1441 goto err_remove_irqchip;
Googleraf606d22022-10-26 21:40:12 -07001442 }
1443 return 0;
1444
Googler9398cc32022-12-02 17:21:52 +08001445err_remove_irqchip:
1446 gpiochip_irqchip_remove(chip);
1447err_remove_irqchip_mask:
1448 gpiochip_irqchip_free_valid_mask(chip);
1449err_remove_acpi_chip:
Googleraf606d22022-10-26 21:40:12 -07001450 acpi_gpiochip_remove(chip);
Googler9398cc32022-12-02 17:21:52 +08001451err_remove_of_chip:
Googleraf606d22022-10-26 21:40:12 -07001452 gpiochip_free_hogs(chip);
1453 of_gpiochip_remove(chip);
Googler9398cc32022-12-02 17:21:52 +08001454err_free_gpiochip_mask:
1455 gpiochip_remove_pin_ranges(chip);
1456 gpiochip_free_valid_mask(chip);
Googleraf606d22022-10-26 21:40:12 -07001457err_remove_from_list:
1458 spin_lock_irqsave(&gpio_lock, flags);
1459 list_del(&gdev->list);
1460 spin_unlock_irqrestore(&gpio_lock, flags);
1461err_free_label:
Googler9398cc32022-12-02 17:21:52 +08001462 kfree_const(gdev->label);
Googleraf606d22022-10-26 21:40:12 -07001463err_free_descs:
1464 kfree(gdev->descs);
1465err_free_ida:
1466 ida_simple_remove(&gpio_ida, gdev->id);
1467err_free_gdev:
1468 /* failures here can mean systems won't boot... */
Googler9398cc32022-12-02 17:21:52 +08001469 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
Googleraf606d22022-10-26 21:40:12 -07001470 gdev->base, gdev->base + gdev->ngpio - 1,
Googler9398cc32022-12-02 17:21:52 +08001471 chip->label ? : "generic", ret);
Googleraf606d22022-10-26 21:40:12 -07001472 kfree(gdev);
Googler9398cc32022-12-02 17:21:52 +08001473 return ret;
Googleraf606d22022-10-26 21:40:12 -07001474}
Googler9398cc32022-12-02 17:21:52 +08001475EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
Googleraf606d22022-10-26 21:40:12 -07001476
1477/**
1478 * gpiochip_get_data() - get per-subdriver data for the chip
Googler9398cc32022-12-02 17:21:52 +08001479 * @chip: GPIO chip
1480 *
1481 * Returns:
1482 * The per-subdriver data for the chip.
Googleraf606d22022-10-26 21:40:12 -07001483 */
1484void *gpiochip_get_data(struct gpio_chip *chip)
1485{
1486 return chip->gpiodev->data;
1487}
1488EXPORT_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 */
1496void 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);
Googler012a81c2022-09-15 14:55:24 +08001511 of_gpiochip_remove(chip);
Googler9398cc32022-12-02 17:21:52 +08001512 gpiochip_remove_pin_ranges(chip);
1513 gpiochip_free_valid_mask(chip);
Googleraf606d22022-10-26 21:40:12 -07001514 /*
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 */
Googler9398cc32022-12-02 17:21:52 +08001538 cdev_device_del(&gdev->chrdev, &gdev->dev);
Googleraf606d22022-10-26 21:40:12 -07001539 put_device(&gdev->dev);
1540}
1541EXPORT_SYMBOL_GPL(gpiochip_remove);
1542
1543static 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/**
Googler9398cc32022-12-02 17:21:52 +08001551 * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
1552 * @dev: pointer to the device that gpio_chip belongs to.
Googleraf606d22022-10-26 21:40:12 -07001553 * @chip: the chip to register, with chip->base initialized
Googler9398cc32022-12-02 17:21:52 +08001554 * @data: driver-private data associated with this chip
1555 *
Googleraf606d22022-10-26 21:40:12 -07001556 * Context: potentially before irqs will work
1557 *
Googler9726be62022-12-14 05:53:31 +00001558 * The gpio chip automatically be released when the device is unbound.
Googler9398cc32022-12-02 17:21:52 +08001559 *
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.
Googleraf606d22022-10-26 21:40:12 -07001564 */
1565int 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}
1587EXPORT_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
Googler9398cc32022-12-02 17:21:52 +08001592 * @match: Callback function to check gpio_chip
Googleraf606d22022-10-26 21:40:12 -07001593 *
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 */
1600struct 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}
1619EXPORT_SYMBOL_GPL(gpiochip_find);
1620
1621static 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
1628static 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
Googler9398cc32022-12-02 17:21:52 +08001639static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
Googleraf606d22022-10-26 21:40:12 -07001640{
Googler9398cc32022-12-02 17:21:52 +08001641 struct gpio_irq_chip *girq = &gc->irq;
Googler9726be62022-12-14 05:53:31 +00001642
Googler9398cc32022-12-02 17:21:52 +08001643 if (!girq->init_hw)
Googleraf606d22022-10-26 21:40:12 -07001644 return 0;
1645
Googler9398cc32022-12-02 17:21:52 +08001646 return girq->init_hw(gc);
1647}
1648
1649static 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)
Googleraf606d22022-10-26 21:40:12 -07001658 return -ENOMEM;
1659
Googler9398cc32022-12-02 17:21:52 +08001660 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
Googler9726be62022-12-14 05:53:31 +00001661
Googleraf606d22022-10-26 21:40:12 -07001662 return 0;
1663}
1664
1665static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1666{
Googler9398cc32022-12-02 17:21:52 +08001667 bitmap_free(gpiochip->irq.valid_mask);
1668 gpiochip->irq.valid_mask = NULL;
Googleraf606d22022-10-26 21:40:12 -07001669}
1670
Googler9398cc32022-12-02 17:21:52 +08001671bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1672 unsigned int offset)
Googleraf606d22022-10-26 21:40:12 -07001673{
Googler9398cc32022-12-02 17:21:52 +08001674 if (!gpiochip_line_is_valid(gpiochip, offset))
1675 return false;
Googleraf606d22022-10-26 21:40:12 -07001676 /* No mask means all valid */
Googler9398cc32022-12-02 17:21:52 +08001677 if (likely(!gpiochip->irq.valid_mask))
Googleraf606d22022-10-26 21:40:12 -07001678 return true;
Googler9398cc32022-12-02 17:21:52 +08001679 return test_bit(offset, gpiochip->irq.valid_mask);
Googleraf606d22022-10-26 21:40:12 -07001680}
Googler9398cc32022-12-02 17:21:52 +08001681EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
Googleraf606d22022-10-26 21:40:12 -07001682
1683/**
Googler9398cc32022-12-02 17:21:52 +08001684 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
1685 * @gc: the gpiochip to set the irqchip chain to
Googleraf606d22022-10-26 21:40:12 -07001686 * @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 */
Googler9398cc32022-12-02 17:21:52 +08001692static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc,
1693 unsigned int parent_irq,
1694 irq_flow_handler_t parent_handler)
Googleraf606d22022-10-26 21:40:12 -07001695{
Googler9398cc32022-12-02 17:21:52 +08001696 struct gpio_irq_chip *girq = &gc->irq;
1697 struct device *dev = &gc->gpiodev->dev;
Googler9726be62022-12-14 05:53:31 +00001698
Googler9398cc32022-12-02 17:21:52 +08001699 if (!girq->domain) {
1700 chip_err(gc, "called %s before setting up irqchip\n",
Googler9726be62022-12-14 05:53:31 +00001701 __func__);
Googleraf606d22022-10-26 21:40:12 -07001702 return;
1703 }
1704
Googler9726be62022-12-14 05:53:31 +00001705 if (parent_handler) {
Googler9398cc32022-12-02 17:21:52 +08001706 if (gc->can_sleep) {
1707 chip_err(gc,
1708 "you cannot have chained interrupts on a chip that may sleep\n");
Googler9726be62022-12-14 05:53:31 +00001709 return;
1710 }
Googler9398cc32022-12-02 17:21:52 +08001711 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;
Googler9726be62022-12-14 05:53:31 +00001720 /*
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,
Googler9398cc32022-12-02 17:21:52 +08001725 gc);
Googler9726be62022-12-14 05:53:31 +00001726 }
Googler38bda472022-08-19 10:07:08 -07001727}
Googler9398cc32022-12-02 17:21:52 +08001728
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 */
1738void 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}
Googleraf606d22022-10-26 21:40:12 -07001750EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1751
1752/**
Googler9398cc32022-12-02 17:21:52 +08001753 * 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 */
1759void 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}
1765EXPORT_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 */
1776static 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
1845static 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
1868static 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 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1928 ret = irq_domain_alloc_irqs_parent(d, irq, 1, &parent_fwspec);
1929 if (ret)
1930 chip_err(gc,
1931 "failed to allocate parent hwirq %d for hwirq %lu\n",
1932 parent_hwirq, hwirq);
1933
1934 return ret;
1935}
1936
1937static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *chip,
1938 unsigned int offset)
1939{
1940 return offset;
1941}
1942
1943static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1944{
1945 ops->activate = gpiochip_irq_domain_activate;
1946 ops->deactivate = gpiochip_irq_domain_deactivate;
1947 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1948 ops->free = irq_domain_free_irqs_common;
1949
1950 /*
1951 * We only allow overriding the translate() function for
1952 * hierarchical chips, and this should only be done if the user
1953 * really need something other than 1:1 translation.
1954 */
1955 if (!ops->translate)
1956 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1957}
1958
1959static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1960{
1961 if (!gc->irq.child_to_parent_hwirq ||
1962 !gc->irq.fwnode) {
1963 chip_err(gc, "missing irqdomain vital data\n");
1964 return -EINVAL;
1965 }
1966
1967 if (!gc->irq.child_offset_to_irq)
1968 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1969
1970 if (!gc->irq.populate_parent_fwspec)
1971 gc->irq.populate_parent_fwspec =
1972 gpiochip_populate_parent_fwspec_twocell;
1973
1974 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1975
1976 gc->irq.domain = irq_domain_create_hierarchy(
1977 gc->irq.parent_domain,
1978 0,
1979 gc->ngpio,
1980 gc->irq.fwnode,
1981 &gc->irq.child_irq_domain_ops,
1982 gc);
1983
1984 if (!gc->irq.domain)
1985 return -ENOMEM;
1986
1987 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1988
1989 return 0;
1990}
1991
1992static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1993{
1994 return !!gc->irq.parent_domain;
1995}
1996
1997void gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *chip,
1998 struct irq_fwspec *fwspec,
1999 unsigned int parent_hwirq,
2000 unsigned int parent_type)
2001{
2002 fwspec->param_count = 2;
2003 fwspec->param[0] = parent_hwirq;
2004 fwspec->param[1] = parent_type;
2005}
2006EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
2007
2008void gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *chip,
2009 struct irq_fwspec *fwspec,
2010 unsigned int parent_hwirq,
2011 unsigned int parent_type)
2012{
2013 fwspec->param_count = 4;
2014 fwspec->param[0] = 0;
2015 fwspec->param[1] = parent_hwirq;
2016 fwspec->param[2] = 0;
2017 fwspec->param[3] = parent_type;
2018}
2019EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
2020
2021#else
2022
2023static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
2024{
2025 return -EINVAL;
2026}
2027
2028static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
2029{
2030 return false;
2031}
2032
2033#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
2034
2035/**
Googleraf606d22022-10-26 21:40:12 -07002036 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
2037 * @d: the irqdomain used by this irqchip
2038 * @irq: the global irq number used by this GPIO irqchip irq
2039 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
2040 *
2041 * This function will set up the mapping for a certain IRQ line on a
2042 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
2043 * stored inside the gpiochip.
2044 */
Googler9398cc32022-12-02 17:21:52 +08002045int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
2046 irq_hw_number_t hwirq)
Googleraf606d22022-10-26 21:40:12 -07002047{
2048 struct gpio_chip *chip = d->host_data;
Googler9398cc32022-12-02 17:21:52 +08002049 int ret = 0;
2050
2051 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
2052 return -ENXIO;
Googleraf606d22022-10-26 21:40:12 -07002053
2054 irq_set_chip_data(irq, chip);
2055 /*
2056 * This lock class tells lockdep that GPIO irqs are in a different
2057 * category than their parents, so it won't report false recursion.
2058 */
Googler9398cc32022-12-02 17:21:52 +08002059 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
2060 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
2061 /* Chips that use nested thread handlers have them marked */
2062 if (chip->irq.threaded)
Googleraf606d22022-10-26 21:40:12 -07002063 irq_set_nested_thread(irq, 1);
2064 irq_set_noprobe(irq);
2065
Googler9398cc32022-12-02 17:21:52 +08002066 if (chip->irq.num_parents == 1)
2067 ret = irq_set_parent(irq, chip->irq.parents[0]);
2068 else if (chip->irq.map)
2069 ret = irq_set_parent(irq, chip->irq.map[hwirq]);
2070
2071 if (ret < 0)
2072 return ret;
2073
Googleraf606d22022-10-26 21:40:12 -07002074 /*
2075 * No set-up of the hardware will happen if IRQ_TYPE_NONE
2076 * is passed as default type.
2077 */
Googler9398cc32022-12-02 17:21:52 +08002078 if (chip->irq.default_type != IRQ_TYPE_NONE)
2079 irq_set_irq_type(irq, chip->irq.default_type);
Googleraf606d22022-10-26 21:40:12 -07002080
2081 return 0;
2082}
Googler9398cc32022-12-02 17:21:52 +08002083EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Googleraf606d22022-10-26 21:40:12 -07002084
Googler9398cc32022-12-02 17:21:52 +08002085void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Googleraf606d22022-10-26 21:40:12 -07002086{
2087 struct gpio_chip *chip = d->host_data;
2088
Googler9398cc32022-12-02 17:21:52 +08002089 if (chip->irq.threaded)
Googleraf606d22022-10-26 21:40:12 -07002090 irq_set_nested_thread(irq, 0);
2091 irq_set_chip_and_handler(irq, NULL, NULL);
2092 irq_set_chip_data(irq, NULL);
2093}
Googler9398cc32022-12-02 17:21:52 +08002094EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Googleraf606d22022-10-26 21:40:12 -07002095
2096static const struct irq_domain_ops gpiochip_domain_ops = {
2097 .map = gpiochip_irq_map,
2098 .unmap = gpiochip_irq_unmap,
2099 /* Virtually all GPIO irqchips are twocell:ed */
2100 .xlate = irq_domain_xlate_twocell,
2101};
2102
Googler9398cc32022-12-02 17:21:52 +08002103/*
2104 * TODO: move these activate/deactivate in under the hierarchicial
2105 * irqchip implementation as static once SPMI and SSBI (all external
2106 * users) are phased over.
2107 */
2108/**
2109 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
2110 * @domain: The IRQ domain used by this IRQ chip
2111 * @data: Outermost irq_data associated with the IRQ
2112 * @reserve: If set, only reserve an interrupt vector instead of assigning one
2113 *
2114 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
2115 * used as the activate function for the &struct irq_domain_ops. The host_data
2116 * for the IRQ domain must be the &struct gpio_chip.
2117 */
2118int gpiochip_irq_domain_activate(struct irq_domain *domain,
2119 struct irq_data *data, bool reserve)
2120{
2121 struct gpio_chip *chip = domain->host_data;
2122
2123 return gpiochip_lock_as_irq(chip, data->hwirq);
2124}
2125EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
2126
2127/**
2128 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
2129 * @domain: The IRQ domain used by this IRQ chip
2130 * @data: Outermost irq_data associated with the IRQ
2131 *
2132 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
2133 * be used as the deactivate function for the &struct irq_domain_ops. The
2134 * host_data for the IRQ domain must be the &struct gpio_chip.
2135 */
2136void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
2137 struct irq_data *data)
2138{
2139 struct gpio_chip *chip = domain->host_data;
2140
2141 return gpiochip_unlock_as_irq(chip, data->hwirq);
2142}
2143EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
2144
2145static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
2146{
2147 struct irq_domain *domain = chip->irq.domain;
2148
2149 if (!gpiochip_irqchip_irq_valid(chip, offset))
2150 return -ENXIO;
2151
2152#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2153 if (irq_domain_is_hierarchy(domain)) {
2154 struct irq_fwspec spec;
2155
2156 spec.fwnode = domain->fwnode;
2157 spec.param_count = 2;
2158 spec.param[0] = chip->irq.child_offset_to_irq(chip, offset);
2159 spec.param[1] = IRQ_TYPE_NONE;
2160
2161 return irq_create_fwspec_mapping(&spec);
2162 }
2163#endif
2164
2165 return irq_create_mapping(domain, offset);
2166}
2167
Googleraf606d22022-10-26 21:40:12 -07002168static int gpiochip_irq_reqres(struct irq_data *d)
2169{
2170 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2171
Googler9398cc32022-12-02 17:21:52 +08002172 return gpiochip_reqres_irq(chip, d->hwirq);
Googleraf606d22022-10-26 21:40:12 -07002173}
2174
2175static void gpiochip_irq_relres(struct irq_data *d)
2176{
2177 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2178
Googler9398cc32022-12-02 17:21:52 +08002179 gpiochip_relres_irq(chip, d->hwirq);
Googleraf606d22022-10-26 21:40:12 -07002180}
2181
Googler9398cc32022-12-02 17:21:52 +08002182static void gpiochip_irq_enable(struct irq_data *d)
Googleraf606d22022-10-26 21:40:12 -07002183{
Googler9398cc32022-12-02 17:21:52 +08002184 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2185
2186 gpiochip_enable_irq(chip, d->hwirq);
2187 if (chip->irq.irq_enable)
2188 chip->irq.irq_enable(d);
2189 else
2190 chip->irq.chip->irq_unmask(d);
2191}
2192
2193static void gpiochip_irq_disable(struct irq_data *d)
2194{
2195 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2196
2197 /*
2198 * Since we override .irq_disable() we need to mimic the
2199 * behaviour of __irq_disable() in irq/chip.c.
2200 * First call .irq_disable() if it exists, else mimic the
2201 * behaviour of mask_irq() which calls .irq_mask() if
2202 * it exists.
2203 */
2204 if (chip->irq.irq_disable)
2205 chip->irq.irq_disable(d);
2206 else if (chip->irq.chip->irq_mask)
2207 chip->irq.chip->irq_mask(d);
2208 gpiochip_disable_irq(chip, d->hwirq);
2209}
2210
2211static void gpiochip_set_irq_hooks(struct gpio_chip *gpiochip)
2212{
2213 struct irq_chip *irqchip = gpiochip->irq.chip;
2214
2215 if (!irqchip->irq_request_resources &&
2216 !irqchip->irq_release_resources) {
2217 irqchip->irq_request_resources = gpiochip_irq_reqres;
2218 irqchip->irq_release_resources = gpiochip_irq_relres;
2219 }
2220 if (WARN_ON(gpiochip->irq.irq_enable))
2221 return;
2222 /* Check if the irqchip already has this hook... */
2223 if (irqchip->irq_enable == gpiochip_irq_enable) {
2224 /*
2225 * ...and if so, give a gentle warning that this is bad
2226 * practice.
2227 */
2228 chip_info(gpiochip,
2229 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
2230 return;
2231 }
2232 gpiochip->irq.irq_enable = irqchip->irq_enable;
2233 gpiochip->irq.irq_disable = irqchip->irq_disable;
2234 irqchip->irq_enable = gpiochip_irq_enable;
2235 irqchip->irq_disable = gpiochip_irq_disable;
2236}
2237
2238/**
2239 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
2240 * @gpiochip: the GPIO chip to add the IRQ chip to
2241 * @lock_key: lockdep class for IRQ lock
2242 * @request_key: lockdep class for IRQ request
2243 */
2244static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
2245 struct lock_class_key *lock_key,
2246 struct lock_class_key *request_key)
2247{
2248 struct irq_chip *irqchip = gpiochip->irq.chip;
2249 const struct irq_domain_ops *ops = NULL;
2250 struct device_node *np;
2251 unsigned int type;
2252 unsigned int i;
2253
2254 if (!irqchip)
2255 return 0;
2256
2257 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
2258 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
2259 return -EINVAL;
2260 }
2261
2262 np = gpiochip->gpiodev->dev.of_node;
2263 type = gpiochip->irq.default_type;
2264
2265 /*
2266 * Specifying a default trigger is a terrible idea if DT or ACPI is
2267 * used to configure the interrupts, as you may end up with
2268 * conflicting triggers. Tell the user, and reset to NONE.
2269 */
2270 if (WARN(np && type != IRQ_TYPE_NONE,
2271 "%s: Ignoring %u default trigger\n", np->full_name, type))
2272 type = IRQ_TYPE_NONE;
2273
2274 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2275 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2276 "Ignoring %u default trigger\n", type);
2277 type = IRQ_TYPE_NONE;
2278 }
2279
2280 gpiochip->to_irq = gpiochip_to_irq;
2281 gpiochip->irq.default_type = type;
2282 gpiochip->irq.lock_key = lock_key;
2283 gpiochip->irq.request_key = request_key;
2284
2285 /* If a parent irqdomain is provided, let's build a hierarchy */
2286 if (gpiochip_hierarchy_is_hierarchical(gpiochip)) {
2287 int ret = gpiochip_hierarchy_add_domain(gpiochip);
2288 if (ret)
2289 return ret;
2290 } else {
2291 /* Some drivers provide custom irqdomain ops */
2292 if (gpiochip->irq.domain_ops)
2293 ops = gpiochip->irq.domain_ops;
2294
2295 if (!ops)
2296 ops = &gpiochip_domain_ops;
2297 gpiochip->irq.domain = irq_domain_add_simple(np,
2298 gpiochip->ngpio,
2299 gpiochip->irq.first,
2300 ops, gpiochip);
2301 if (!gpiochip->irq.domain)
2302 return -EINVAL;
2303 }
2304
2305 if (gpiochip->irq.parent_handler) {
2306 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
2307
2308 for (i = 0; i < gpiochip->irq.num_parents; i++) {
2309 /*
2310 * The parent IRQ chip is already using the chip_data
2311 * for this IRQ chip, so our callbacks simply use the
2312 * handler_data.
2313 */
2314 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
2315 gpiochip->irq.parent_handler,
2316 data);
2317 }
2318 }
2319
2320 gpiochip_set_irq_hooks(gpiochip);
2321
2322 acpi_gpiochip_request_interrupts(gpiochip);
2323
2324 return 0;
Googleraf606d22022-10-26 21:40:12 -07002325}
2326
2327/**
2328 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
2329 * @gpiochip: the gpiochip to remove the irqchip from
2330 *
2331 * This is called only from gpiochip_remove()
2332 */
2333static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
2334{
Googler9398cc32022-12-02 17:21:52 +08002335 struct irq_chip *irqchip = gpiochip->irq.chip;
Googleraf606d22022-10-26 21:40:12 -07002336 unsigned int offset;
2337
2338 acpi_gpiochip_free_interrupts(gpiochip);
2339
Googler9398cc32022-12-02 17:21:52 +08002340 if (irqchip && gpiochip->irq.parent_handler) {
2341 struct gpio_irq_chip *irq = &gpiochip->irq;
2342 unsigned int i;
2343
2344 for (i = 0; i < irq->num_parents; i++)
2345 irq_set_chained_handler_and_data(irq->parents[i],
2346 NULL, NULL);
Googleraf606d22022-10-26 21:40:12 -07002347 }
2348
2349 /* Remove all IRQ mappings and delete the domain */
Googler9398cc32022-12-02 17:21:52 +08002350 if (gpiochip->irq.domain) {
2351 unsigned int irq;
2352
Googleraf606d22022-10-26 21:40:12 -07002353 for (offset = 0; offset < gpiochip->ngpio; offset++) {
2354 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
2355 continue;
Googler9398cc32022-12-02 17:21:52 +08002356
2357 irq = irq_find_mapping(gpiochip->irq.domain, offset);
2358 irq_dispose_mapping(irq);
Googleraf606d22022-10-26 21:40:12 -07002359 }
Googler9398cc32022-12-02 17:21:52 +08002360
2361 irq_domain_remove(gpiochip->irq.domain);
Googleraf606d22022-10-26 21:40:12 -07002362 }
2363
Googler9398cc32022-12-02 17:21:52 +08002364 if (irqchip) {
2365 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
2366 irqchip->irq_request_resources = NULL;
2367 irqchip->irq_release_resources = NULL;
2368 }
2369 if (irqchip->irq_enable == gpiochip_irq_enable) {
2370 irqchip->irq_enable = gpiochip->irq.irq_enable;
2371 irqchip->irq_disable = gpiochip->irq.irq_disable;
2372 }
Googleraf606d22022-10-26 21:40:12 -07002373 }
Googler9398cc32022-12-02 17:21:52 +08002374 gpiochip->irq.irq_enable = NULL;
2375 gpiochip->irq.irq_disable = NULL;
2376 gpiochip->irq.chip = NULL;
Googleraf606d22022-10-26 21:40:12 -07002377
2378 gpiochip_irqchip_free_valid_mask(gpiochip);
2379}
2380
2381/**
Googler9398cc32022-12-02 17:21:52 +08002382 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Googleraf606d22022-10-26 21:40:12 -07002383 * @gpiochip: the gpiochip to add the irqchip to
2384 * @irqchip: the irqchip to add to the gpiochip
2385 * @first_irq: if not dynamically assigned, the base (first) IRQ to
2386 * allocate gpiochip irqs from
2387 * @handler: the irq handler to use (often a predefined irq core function)
2388 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
2389 * to have the core avoid setting up any default type in the hardware.
Googler9398cc32022-12-02 17:21:52 +08002390 * @threaded: whether this irqchip uses a nested thread handler
2391 * @lock_key: lockdep class for IRQ lock
2392 * @request_key: lockdep class for IRQ request
Googleraf606d22022-10-26 21:40:12 -07002393 *
2394 * This function closely associates a certain irqchip with a certain
2395 * gpiochip, providing an irq domain to translate the local IRQs to
2396 * global irqs in the gpiolib core, and making sure that the gpiochip
2397 * is passed as chip data to all related functions. Driver callbacks
2398 * need to use gpiochip_get_data() to get their local state containers back
2399 * from the gpiochip passed as chip data. An irqdomain will be stored
2400 * in the gpiochip that shall be used by the driver to handle IRQ number
2401 * translation. The gpiochip will need to be initialized and registered
2402 * before calling this function.
2403 *
2404 * This function will handle two cell:ed simple IRQs and assumes all
2405 * the pins on the gpiochip can generate a unique IRQ. Everything else
2406 * need to be open coded.
2407 */
Googler9398cc32022-12-02 17:21:52 +08002408int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
2409 struct irq_chip *irqchip,
2410 unsigned int first_irq,
2411 irq_flow_handler_t handler,
2412 unsigned int type,
2413 bool threaded,
2414 struct lock_class_key *lock_key,
2415 struct lock_class_key *request_key)
Googleraf606d22022-10-26 21:40:12 -07002416{
2417 struct device_node *of_node;
2418
2419 if (!gpiochip || !irqchip)
2420 return -EINVAL;
2421
2422 if (!gpiochip->parent) {
2423 pr_err("missing gpiochip .dev parent pointer\n");
2424 return -EINVAL;
2425 }
Googler9398cc32022-12-02 17:21:52 +08002426 gpiochip->irq.threaded = threaded;
Googleraf606d22022-10-26 21:40:12 -07002427 of_node = gpiochip->parent->of_node;
2428#ifdef CONFIG_OF_GPIO
2429 /*
2430 * If the gpiochip has an assigned OF node this takes precedence
2431 * FIXME: get rid of this and use gpiochip->parent->of_node
2432 * everywhere
2433 */
2434 if (gpiochip->of_node)
2435 of_node = gpiochip->of_node;
2436#endif
2437 /*
2438 * Specifying a default trigger is a terrible idea if DT or ACPI is
2439 * used to configure the interrupts, as you may end-up with
2440 * conflicting triggers. Tell the user, and reset to NONE.
2441 */
2442 if (WARN(of_node && type != IRQ_TYPE_NONE,
Googler9398cc32022-12-02 17:21:52 +08002443 "%pOF: Ignoring %d default trigger\n", of_node, type))
Googleraf606d22022-10-26 21:40:12 -07002444 type = IRQ_TYPE_NONE;
2445 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2446 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2447 "Ignoring %d default trigger\n", type);
2448 type = IRQ_TYPE_NONE;
2449 }
2450
Googler9398cc32022-12-02 17:21:52 +08002451 gpiochip->irq.chip = irqchip;
2452 gpiochip->irq.handler = handler;
2453 gpiochip->irq.default_type = type;
Googleraf606d22022-10-26 21:40:12 -07002454 gpiochip->to_irq = gpiochip_to_irq;
Googler9398cc32022-12-02 17:21:52 +08002455 gpiochip->irq.lock_key = lock_key;
2456 gpiochip->irq.request_key = request_key;
2457 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Googleraf606d22022-10-26 21:40:12 -07002458 gpiochip->ngpio, first_irq,
2459 &gpiochip_domain_ops, gpiochip);
Googler9398cc32022-12-02 17:21:52 +08002460 if (!gpiochip->irq.domain) {
2461 gpiochip->irq.chip = NULL;
Googleraf606d22022-10-26 21:40:12 -07002462 return -EINVAL;
2463 }
2464
Googler9398cc32022-12-02 17:21:52 +08002465 gpiochip_set_irq_hooks(gpiochip);
Googler9726be62022-12-14 05:53:31 +00002466
Googleraf606d22022-10-26 21:40:12 -07002467 acpi_gpiochip_request_interrupts(gpiochip);
2468
2469 return 0;
2470}
Googler9398cc32022-12-02 17:21:52 +08002471EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Googleraf606d22022-10-26 21:40:12 -07002472
2473#else /* CONFIG_GPIOLIB_IRQCHIP */
2474
Googler9398cc32022-12-02 17:21:52 +08002475static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
2476 struct lock_class_key *lock_key,
2477 struct lock_class_key *request_key)
2478{
2479 return 0;
2480}
Googleraf606d22022-10-26 21:40:12 -07002481static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Googler9398cc32022-12-02 17:21:52 +08002482
2483static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip)
2484{
2485 return 0;
2486}
2487
Googleraf606d22022-10-26 21:40:12 -07002488static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
2489{
2490 return 0;
2491}
2492static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
2493{ }
2494
2495#endif /* CONFIG_GPIOLIB_IRQCHIP */
2496
2497/**
2498 * gpiochip_generic_request() - request the gpio function for a pin
2499 * @chip: the gpiochip owning the GPIO
2500 * @offset: the offset of the GPIO to request for GPIO function
2501 */
2502int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
2503{
Googler9398cc32022-12-02 17:21:52 +08002504 return pinctrl_gpio_request(chip->gpiodev->base + offset);
Googleraf606d22022-10-26 21:40:12 -07002505}
2506EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2507
2508/**
2509 * gpiochip_generic_free() - free the gpio function from a pin
2510 * @chip: the gpiochip to request the gpio function for
2511 * @offset: the offset of the GPIO to free from GPIO function
2512 */
2513void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2514{
Googler9398cc32022-12-02 17:21:52 +08002515 pinctrl_gpio_free(chip->gpiodev->base + offset);
Googleraf606d22022-10-26 21:40:12 -07002516}
2517EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2518
Googler9398cc32022-12-02 17:21:52 +08002519/**
2520 * gpiochip_generic_config() - apply configuration for a pin
2521 * @chip: the gpiochip owning the GPIO
2522 * @offset: the offset of the GPIO to apply the configuration
2523 * @config: the configuration to be applied
2524 */
2525int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2526 unsigned long config)
2527{
2528 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2529}
2530EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2531
Googleraf606d22022-10-26 21:40:12 -07002532#ifdef CONFIG_PINCTRL
2533
2534/**
2535 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2536 * @chip: the gpiochip to add the range for
2537 * @pctldev: the pin controller to map to
2538 * @gpio_offset: the start offset in the current gpio_chip number space
2539 * @pin_group: name of the pin group inside the pin controller
Googler9398cc32022-12-02 17:21:52 +08002540 *
2541 * Calling this function directly from a DeviceTree-supported
2542 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2543 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2544 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Googleraf606d22022-10-26 21:40:12 -07002545 */
2546int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2547 struct pinctrl_dev *pctldev,
2548 unsigned int gpio_offset, const char *pin_group)
2549{
2550 struct gpio_pin_range *pin_range;
2551 struct gpio_device *gdev = chip->gpiodev;
2552 int ret;
2553
2554 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2555 if (!pin_range) {
2556 chip_err(chip, "failed to allocate pin ranges\n");
2557 return -ENOMEM;
2558 }
2559
2560 /* Use local offset as range ID */
2561 pin_range->range.id = gpio_offset;
2562 pin_range->range.gc = chip;
2563 pin_range->range.name = chip->label;
2564 pin_range->range.base = gdev->base + gpio_offset;
2565 pin_range->pctldev = pctldev;
2566
2567 ret = pinctrl_get_group_pins(pctldev, pin_group,
2568 &pin_range->range.pins,
2569 &pin_range->range.npins);
2570 if (ret < 0) {
2571 kfree(pin_range);
2572 return ret;
2573 }
2574
2575 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2576
2577 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2578 gpio_offset, gpio_offset + pin_range->range.npins - 1,
2579 pinctrl_dev_get_devname(pctldev), pin_group);
2580
2581 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2582
2583 return 0;
2584}
2585EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2586
2587/**
2588 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2589 * @chip: the gpiochip to add the range for
Googler9398cc32022-12-02 17:21:52 +08002590 * @pinctl_name: the dev_name() of the pin controller to map to
Googleraf606d22022-10-26 21:40:12 -07002591 * @gpio_offset: the start offset in the current gpio_chip number space
2592 * @pin_offset: the start offset in the pin controller number space
2593 * @npins: the number of pins from the offset of each pin space (GPIO and
2594 * pin controller) to accumulate in this range
Googler9398cc32022-12-02 17:21:52 +08002595 *
2596 * Returns:
2597 * 0 on success, or a negative error-code on failure.
2598 *
2599 * Calling this function directly from a DeviceTree-supported
2600 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2601 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2602 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Googleraf606d22022-10-26 21:40:12 -07002603 */
2604int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
2605 unsigned int gpio_offset, unsigned int pin_offset,
2606 unsigned int npins)
2607{
2608 struct gpio_pin_range *pin_range;
2609 struct gpio_device *gdev = chip->gpiodev;
2610 int ret;
2611
2612 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2613 if (!pin_range) {
2614 chip_err(chip, "failed to allocate pin ranges\n");
2615 return -ENOMEM;
2616 }
2617
2618 /* Use local offset as range ID */
2619 pin_range->range.id = gpio_offset;
2620 pin_range->range.gc = chip;
2621 pin_range->range.name = chip->label;
2622 pin_range->range.base = gdev->base + gpio_offset;
2623 pin_range->range.pin_base = pin_offset;
2624 pin_range->range.npins = npins;
2625 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
2626 &pin_range->range);
2627 if (IS_ERR(pin_range->pctldev)) {
2628 ret = PTR_ERR(pin_range->pctldev);
2629 chip_err(chip, "could not create pin range\n");
2630 kfree(pin_range);
2631 return ret;
2632 }
2633 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2634 gpio_offset, gpio_offset + npins - 1,
2635 pinctl_name,
2636 pin_offset, pin_offset + npins - 1);
2637
2638 list_add_tail(&pin_range->node, &gdev->pin_ranges);
2639
2640 return 0;
2641}
2642EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
2643
2644/**
2645 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2646 * @chip: the chip to remove all the mappings for
2647 */
2648void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2649{
2650 struct gpio_pin_range *pin_range, *tmp;
2651 struct gpio_device *gdev = chip->gpiodev;
2652
2653 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
2654 list_del(&pin_range->node);
2655 pinctrl_remove_gpio_range(pin_range->pctldev,
2656 &pin_range->range);
2657 kfree(pin_range);
2658 }
2659}
2660EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2661
2662#endif /* CONFIG_PINCTRL */
2663
2664/* These "optional" allocation calls help prevent drivers from stomping
2665 * on each other, and help provide better diagnostics in debugfs.
2666 * They're called even less than the "set direction" calls.
2667 */
Googler9398cc32022-12-02 17:21:52 +08002668static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
Googleraf606d22022-10-26 21:40:12 -07002669{
2670 struct gpio_chip *chip = desc->gdev->chip;
Googler9398cc32022-12-02 17:21:52 +08002671 int ret;
Googleraf606d22022-10-26 21:40:12 -07002672 unsigned long flags;
Googler9398cc32022-12-02 17:21:52 +08002673 unsigned offset;
2674
2675 if (label) {
2676 label = kstrdup_const(label, GFP_KERNEL);
2677 if (!label)
2678 return -ENOMEM;
2679 }
Googleraf606d22022-10-26 21:40:12 -07002680
2681 spin_lock_irqsave(&gpio_lock, flags);
2682
2683 /* NOTE: gpio_request() can be called in early boot,
2684 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
2685 */
2686
2687 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2688 desc_set_label(desc, label ? : "?");
Googler9398cc32022-12-02 17:21:52 +08002689 ret = 0;
Googleraf606d22022-10-26 21:40:12 -07002690 } else {
Googler9398cc32022-12-02 17:21:52 +08002691 kfree_const(label);
2692 ret = -EBUSY;
Googleraf606d22022-10-26 21:40:12 -07002693 goto done;
2694 }
2695
2696 if (chip->request) {
2697 /* chip->request may sleep */
2698 spin_unlock_irqrestore(&gpio_lock, flags);
Googler9398cc32022-12-02 17:21:52 +08002699 offset = gpio_chip_hwgpio(desc);
2700 if (gpiochip_line_is_valid(chip, offset))
2701 ret = chip->request(chip, offset);
2702 else
2703 ret = -EINVAL;
Googleraf606d22022-10-26 21:40:12 -07002704 spin_lock_irqsave(&gpio_lock, flags);
2705
Googler9398cc32022-12-02 17:21:52 +08002706 if (ret < 0) {
Googleraf606d22022-10-26 21:40:12 -07002707 desc_set_label(desc, NULL);
Googler9398cc32022-12-02 17:21:52 +08002708 kfree_const(label);
Googleraf606d22022-10-26 21:40:12 -07002709 clear_bit(FLAG_REQUESTED, &desc->flags);
2710 goto done;
2711 }
2712 }
2713 if (chip->get_direction) {
2714 /* chip->get_direction may sleep */
2715 spin_unlock_irqrestore(&gpio_lock, flags);
2716 gpiod_get_direction(desc);
2717 spin_lock_irqsave(&gpio_lock, flags);
2718 }
2719done:
2720 spin_unlock_irqrestore(&gpio_lock, flags);
Googler9398cc32022-12-02 17:21:52 +08002721 return ret;
Googleraf606d22022-10-26 21:40:12 -07002722}
2723
2724/*
2725 * This descriptor validation needs to be inserted verbatim into each
2726 * function taking a descriptor, so we need to use a preprocessor
2727 * macro to avoid endless duplication. If the desc is NULL it is an
2728 * optional GPIO and calls should just bail out.
2729 */
Googler9398cc32022-12-02 17:21:52 +08002730static int validate_desc(const struct gpio_desc *desc, const char *func)
2731{
2732 if (!desc)
2733 return 0;
2734 if (IS_ERR(desc)) {
2735 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2736 return PTR_ERR(desc);
2737 }
2738 if (!desc->gdev) {
2739 pr_warn("%s: invalid GPIO (no device)\n", func);
2740 return -EINVAL;
2741 }
2742 if (!desc->gdev->chip) {
2743 dev_warn(&desc->gdev->dev,
2744 "%s: backing chip is gone\n", func);
2745 return 0;
2746 }
2747 return 1;
2748}
2749
Googleraf606d22022-10-26 21:40:12 -07002750#define VALIDATE_DESC(desc) do { \
Googler9398cc32022-12-02 17:21:52 +08002751 int __valid = validate_desc(desc, __func__); \
2752 if (__valid <= 0) \
2753 return __valid; \
2754 } while (0)
Googleraf606d22022-10-26 21:40:12 -07002755
2756#define VALIDATE_DESC_VOID(desc) do { \
Googler9398cc32022-12-02 17:21:52 +08002757 int __valid = validate_desc(desc, __func__); \
2758 if (__valid <= 0) \
Googleraf606d22022-10-26 21:40:12 -07002759 return; \
Googler9398cc32022-12-02 17:21:52 +08002760 } while (0)
Googleraf606d22022-10-26 21:40:12 -07002761
2762int gpiod_request(struct gpio_desc *desc, const char *label)
2763{
Googler9398cc32022-12-02 17:21:52 +08002764 int ret = -EPROBE_DEFER;
Googleraf606d22022-10-26 21:40:12 -07002765 struct gpio_device *gdev;
2766
2767 VALIDATE_DESC(desc);
2768 gdev = desc->gdev;
2769
2770 if (try_module_get(gdev->owner)) {
Googler9398cc32022-12-02 17:21:52 +08002771 ret = gpiod_request_commit(desc, label);
2772 if (ret < 0)
Googleraf606d22022-10-26 21:40:12 -07002773 module_put(gdev->owner);
2774 else
2775 get_device(&gdev->dev);
2776 }
2777
Googler9398cc32022-12-02 17:21:52 +08002778 if (ret)
2779 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
Googleraf606d22022-10-26 21:40:12 -07002780
Googler9398cc32022-12-02 17:21:52 +08002781 return ret;
Googleraf606d22022-10-26 21:40:12 -07002782}
2783
Googler9398cc32022-12-02 17:21:52 +08002784static bool gpiod_free_commit(struct gpio_desc *desc)
Googleraf606d22022-10-26 21:40:12 -07002785{
2786 bool ret = false;
2787 unsigned long flags;
2788 struct gpio_chip *chip;
2789
2790 might_sleep();
2791
2792 gpiod_unexport(desc);
2793
2794 spin_lock_irqsave(&gpio_lock, flags);
2795
2796 chip = desc->gdev->chip;
2797 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2798 if (chip->free) {
2799 spin_unlock_irqrestore(&gpio_lock, flags);
2800 might_sleep_if(chip->can_sleep);
2801 chip->free(chip, gpio_chip_hwgpio(desc));
2802 spin_lock_irqsave(&gpio_lock, flags);
2803 }
Googler9398cc32022-12-02 17:21:52 +08002804 kfree_const(desc->label);
Googleraf606d22022-10-26 21:40:12 -07002805 desc_set_label(desc, NULL);
2806 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2807 clear_bit(FLAG_REQUESTED, &desc->flags);
2808 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2809 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2810 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2811 ret = true;
2812 }
2813
2814 spin_unlock_irqrestore(&gpio_lock, flags);
2815 return ret;
2816}
2817
2818void gpiod_free(struct gpio_desc *desc)
2819{
Googler9398cc32022-12-02 17:21:52 +08002820 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Googleraf606d22022-10-26 21:40:12 -07002821 module_put(desc->gdev->owner);
2822 put_device(&desc->gdev->dev);
2823 } else {
2824 WARN_ON(extra_checks);
2825 }
2826}
2827
2828/**
2829 * gpiochip_is_requested - return string iff signal was requested
2830 * @chip: controller managing the signal
2831 * @offset: of signal within controller's 0..(ngpio - 1) range
2832 *
2833 * Returns NULL if the GPIO is not currently requested, else a string.
2834 * The string returned is the label passed to gpio_request(); if none has been
2835 * passed it is a meaningless, non-NULL constant.
2836 *
2837 * This function is for use by GPIO controller drivers. The label can
2838 * help with diagnostics, and knowing that the signal is used as a GPIO
2839 * can help avoid accidentally multiplexing it to another controller.
2840 */
2841const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2842{
2843 struct gpio_desc *desc;
2844
2845 if (offset >= chip->ngpio)
2846 return NULL;
2847
2848 desc = &chip->gpiodev->descs[offset];
2849
2850 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2851 return NULL;
2852 return desc->label;
2853}
2854EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2855
2856/**
2857 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Googler9398cc32022-12-02 17:21:52 +08002858 * @chip: GPIO chip
2859 * @hwnum: hardware number of the GPIO for which to request the descriptor
Googleraf606d22022-10-26 21:40:12 -07002860 * @label: label for the GPIO
Googler9398cc32022-12-02 17:21:52 +08002861 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2862 * specify things like line inversion semantics with the machine flags
2863 * such as GPIO_OUT_LOW
2864 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2865 * can be used to specify consumer semantics such as open drain
Googleraf606d22022-10-26 21:40:12 -07002866 *
2867 * Function allows GPIO chip drivers to request and use their own GPIO
2868 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2869 * function will not increase reference count of the GPIO chip module. This
2870 * allows the GPIO chip module to be unloaded as needed (we assume that the
2871 * GPIO chip driver handles freeing the GPIOs it has requested).
Googler9398cc32022-12-02 17:21:52 +08002872 *
2873 * Returns:
2874 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2875 * code on failure.
Googleraf606d22022-10-26 21:40:12 -07002876 */
2877struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
Googler9398cc32022-12-02 17:21:52 +08002878 const char *label,
2879 enum gpio_lookup_flags lflags,
2880 enum gpiod_flags dflags)
Googleraf606d22022-10-26 21:40:12 -07002881{
2882 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
Googler9398cc32022-12-02 17:21:52 +08002883 int ret;
Googleraf606d22022-10-26 21:40:12 -07002884
2885 if (IS_ERR(desc)) {
2886 chip_err(chip, "failed to get GPIO descriptor\n");
2887 return desc;
2888 }
2889
Googler9398cc32022-12-02 17:21:52 +08002890 ret = gpiod_request_commit(desc, label);
2891 if (ret < 0)
2892 return ERR_PTR(ret);
2893
2894 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2895 if (ret) {
2896 chip_err(chip, "setup of own GPIO %s failed\n", label);
2897 gpiod_free_commit(desc);
2898 return ERR_PTR(ret);
2899 }
Googleraf606d22022-10-26 21:40:12 -07002900
2901 return desc;
2902}
2903EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2904
2905/**
2906 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2907 * @desc: GPIO descriptor to free
2908 *
2909 * Function frees the given GPIO requested previously with
2910 * gpiochip_request_own_desc().
2911 */
2912void gpiochip_free_own_desc(struct gpio_desc *desc)
2913{
2914 if (desc)
Googler9398cc32022-12-02 17:21:52 +08002915 gpiod_free_commit(desc);
Googleraf606d22022-10-26 21:40:12 -07002916}
2917EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2918
2919/*
2920 * Drivers MUST set GPIO direction before making get/set calls. In
2921 * some cases this is done in early boot, before IRQs are enabled.
2922 *
2923 * As a rule these aren't called more than once (except for drivers
2924 * using the open-drain emulation idiom) so these are natural places
2925 * to accumulate extra debugging checks. Note that we can't (yet)
2926 * rely on gpio_request() having been called beforehand.
2927 */
2928
Googler9398cc32022-12-02 17:21:52 +08002929static int gpio_set_config(struct gpio_chip *gc, unsigned offset,
2930 enum pin_config_param mode)
2931{
2932 unsigned long config;
2933 unsigned arg;
2934
2935 switch (mode) {
2936 case PIN_CONFIG_BIAS_PULL_DOWN:
2937 case PIN_CONFIG_BIAS_PULL_UP:
2938 arg = 1;
2939 break;
2940
2941 default:
2942 arg = 0;
2943 }
2944
2945 config = PIN_CONF_PACKED(mode, arg);
2946 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2947}
2948
Googleraf606d22022-10-26 21:40:12 -07002949/**
2950 * gpiod_direction_input - set the GPIO direction to input
2951 * @desc: GPIO to set to input
2952 *
2953 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2954 * be called safely on it.
2955 *
2956 * Return 0 in case of success, else an error code.
2957 */
2958int gpiod_direction_input(struct gpio_desc *desc)
2959{
2960 struct gpio_chip *chip;
Googler9398cc32022-12-02 17:21:52 +08002961 int ret = 0;
Googleraf606d22022-10-26 21:40:12 -07002962
2963 VALIDATE_DESC(desc);
2964 chip = desc->gdev->chip;
2965
Googler9398cc32022-12-02 17:21:52 +08002966 /*
2967 * It is legal to have no .get() and .direction_input() specified if
2968 * the chip is output-only, but you can't specify .direction_input()
2969 * and not support the .get() operation, that doesn't make sense.
2970 */
2971 if (!chip->get && chip->direction_input) {
Googleraf606d22022-10-26 21:40:12 -07002972 gpiod_warn(desc,
Googler9398cc32022-12-02 17:21:52 +08002973 "%s: missing get() but have direction_input()\n",
2974 __func__);
Googleraf606d22022-10-26 21:40:12 -07002975 return -EIO;
2976 }
2977
Googler9398cc32022-12-02 17:21:52 +08002978 /*
2979 * If we have a .direction_input() callback, things are simple,
2980 * just call it. Else we are some input-only chip so try to check the
2981 * direction (if .get_direction() is supported) else we silently
2982 * assume we are in input mode after this.
2983 */
2984 if (chip->direction_input) {
2985 ret = chip->direction_input(chip, gpio_chip_hwgpio(desc));
2986 } else if (chip->get_direction &&
2987 (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) {
2988 gpiod_warn(desc,
2989 "%s: missing direction_input() operation and line is output\n",
2990 __func__);
2991 return -EIO;
2992 }
2993 if (ret == 0)
Googleraf606d22022-10-26 21:40:12 -07002994 clear_bit(FLAG_IS_OUT, &desc->flags);
2995
Googler9398cc32022-12-02 17:21:52 +08002996 if (test_bit(FLAG_PULL_UP, &desc->flags))
2997 gpio_set_config(chip, gpio_chip_hwgpio(desc),
2998 PIN_CONFIG_BIAS_PULL_UP);
2999 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
3000 gpio_set_config(chip, gpio_chip_hwgpio(desc),
3001 PIN_CONFIG_BIAS_PULL_DOWN);
Googleraf606d22022-10-26 21:40:12 -07003002
Googler9398cc32022-12-02 17:21:52 +08003003 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
3004
3005 return ret;
Googleraf606d22022-10-26 21:40:12 -07003006}
3007EXPORT_SYMBOL_GPL(gpiod_direction_input);
3008
Googler9398cc32022-12-02 17:21:52 +08003009static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
Googleraf606d22022-10-26 21:40:12 -07003010{
3011 struct gpio_chip *gc = desc->gdev->chip;
Googler9398cc32022-12-02 17:21:52 +08003012 int val = !!value;
3013 int ret = 0;
Googleraf606d22022-10-26 21:40:12 -07003014
Googler9398cc32022-12-02 17:21:52 +08003015 /*
3016 * It's OK not to specify .direction_output() if the gpiochip is
3017 * output-only, but if there is then not even a .set() operation it
3018 * is pretty tricky to drive the output line.
3019 */
3020 if (!gc->set && !gc->direction_output) {
Googler012a81c2022-09-15 14:55:24 +08003021 gpiod_warn(desc,
Googler9398cc32022-12-02 17:21:52 +08003022 "%s: missing set() and direction_output() operations\n",
3023 __func__);
Googler012a81c2022-09-15 14:55:24 +08003024 return -EIO;
3025 }
3026
Googler9398cc32022-12-02 17:21:52 +08003027 if (gc->direction_output) {
3028 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
3029 } else {
3030 /* Check that we are in output mode if we can */
3031 if (gc->get_direction &&
3032 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
3033 gpiod_warn(desc,
3034 "%s: missing direction_output() operation\n",
3035 __func__);
3036 return -EIO;
3037 }
3038 /*
3039 * If we can't actively set the direction, we are some
3040 * output-only chip, so just drive the output as desired.
3041 */
3042 gc->set(gc, gpio_chip_hwgpio(desc), val);
3043 }
3044
Googleraf606d22022-10-26 21:40:12 -07003045 if (!ret)
3046 set_bit(FLAG_IS_OUT, &desc->flags);
Googler9398cc32022-12-02 17:21:52 +08003047 trace_gpio_value(desc_to_gpio(desc), 0, val);
Googleraf606d22022-10-26 21:40:12 -07003048 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
3049 return ret;
3050}
3051
3052/**
3053 * gpiod_direction_output_raw - set the GPIO direction to output
3054 * @desc: GPIO to set to output
3055 * @value: initial output value of the GPIO
3056 *
3057 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3058 * be called safely on it. The initial value of the output must be specified
3059 * as raw value on the physical line without regard for the ACTIVE_LOW status.
3060 *
3061 * Return 0 in case of success, else an error code.
3062 */
3063int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
3064{
3065 VALIDATE_DESC(desc);
Googler9398cc32022-12-02 17:21:52 +08003066 return gpiod_direction_output_raw_commit(desc, value);
Googleraf606d22022-10-26 21:40:12 -07003067}
3068EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
3069
3070/**
3071 * gpiod_direction_output - set the GPIO direction to output
3072 * @desc: GPIO to set to output
3073 * @value: initial output value of the GPIO
3074 *
3075 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3076 * be called safely on it. The initial value of the output must be specified
3077 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3078 * account.
3079 *
3080 * Return 0 in case of success, else an error code.
3081 */
3082int gpiod_direction_output(struct gpio_desc *desc, int value)
3083{
Googler9398cc32022-12-02 17:21:52 +08003084 struct gpio_chip *gc;
3085 int ret;
3086
Googleraf606d22022-10-26 21:40:12 -07003087 VALIDATE_DESC(desc);
3088 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3089 value = !value;
Googler9398cc32022-12-02 17:21:52 +08003090 else
3091 value = !!value;
3092
3093 /* GPIOs used for enabled IRQs shall not be set as output */
3094 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
3095 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
3096 gpiod_err(desc,
3097 "%s: tried to set a GPIO tied to an IRQ as output\n",
3098 __func__);
3099 return -EIO;
3100 }
3101
3102 gc = desc->gdev->chip;
3103 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3104 /* First see if we can enable open drain in hardware */
3105 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
3106 PIN_CONFIG_DRIVE_OPEN_DRAIN);
3107 if (!ret)
3108 goto set_output_value;
3109 /* Emulate open drain by not actively driving the line high */
3110 if (value) {
3111 ret = gpiod_direction_input(desc);
3112 goto set_output_flag;
3113 }
3114 }
3115 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
3116 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
3117 PIN_CONFIG_DRIVE_OPEN_SOURCE);
3118 if (!ret)
3119 goto set_output_value;
3120 /* Emulate open source by not actively driving the line low */
3121 if (!value) {
3122 ret = gpiod_direction_input(desc);
3123 goto set_output_flag;
3124 }
3125 } else {
3126 gpio_set_config(gc, gpio_chip_hwgpio(desc),
3127 PIN_CONFIG_DRIVE_PUSH_PULL);
3128 }
3129
3130set_output_value:
3131 return gpiod_direction_output_raw_commit(desc, value);
3132
3133set_output_flag:
3134 /*
3135 * When emulating open-source or open-drain functionalities by not
3136 * actively driving the line (setting mode to input) we still need to
3137 * set the IS_OUT flag or otherwise we won't be able to set the line
3138 * value anymore.
3139 */
3140 if (ret == 0)
3141 set_bit(FLAG_IS_OUT, &desc->flags);
3142 return ret;
Googleraf606d22022-10-26 21:40:12 -07003143}
3144EXPORT_SYMBOL_GPL(gpiod_direction_output);
3145
3146/**
Googler9398cc32022-12-02 17:21:52 +08003147 * gpiod_set_debounce - sets @debounce time for a GPIO
3148 * @desc: descriptor of the GPIO for which to set debounce time
3149 * @debounce: debounce time in microseconds
Googleraf606d22022-10-26 21:40:12 -07003150 *
Googler9398cc32022-12-02 17:21:52 +08003151 * Returns:
3152 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
3153 * debounce time.
Googleraf606d22022-10-26 21:40:12 -07003154 */
3155int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
3156{
3157 struct gpio_chip *chip;
Googler9398cc32022-12-02 17:21:52 +08003158 unsigned long config;
Googleraf606d22022-10-26 21:40:12 -07003159
3160 VALIDATE_DESC(desc);
3161 chip = desc->gdev->chip;
Googler9398cc32022-12-02 17:21:52 +08003162 if (!chip->set || !chip->set_config) {
Googleraf606d22022-10-26 21:40:12 -07003163 gpiod_dbg(desc,
Googler9398cc32022-12-02 17:21:52 +08003164 "%s: missing set() or set_config() operations\n",
Googleraf606d22022-10-26 21:40:12 -07003165 __func__);
3166 return -ENOTSUPP;
3167 }
3168
Googler9398cc32022-12-02 17:21:52 +08003169 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
3170 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Googleraf606d22022-10-26 21:40:12 -07003171}
3172EXPORT_SYMBOL_GPL(gpiod_set_debounce);
3173
3174/**
Googler9398cc32022-12-02 17:21:52 +08003175 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
3176 * @desc: descriptor of the GPIO for which to configure persistence
3177 * @transitory: True to lose state on suspend or reset, false for persistence
3178 *
3179 * Returns:
3180 * 0 on success, otherwise a negative error code.
3181 */
3182int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
3183{
3184 struct gpio_chip *chip;
3185 unsigned long packed;
3186 int gpio;
3187 int rc;
3188
3189 VALIDATE_DESC(desc);
3190 /*
3191 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
3192 * persistence state.
3193 */
3194 if (transitory)
3195 set_bit(FLAG_TRANSITORY, &desc->flags);
3196 else
3197 clear_bit(FLAG_TRANSITORY, &desc->flags);
3198
3199 /* If the driver supports it, set the persistence state now */
3200 chip = desc->gdev->chip;
3201 if (!chip->set_config)
3202 return 0;
3203
3204 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
3205 !transitory);
3206 gpio = gpio_chip_hwgpio(desc);
3207 rc = chip->set_config(chip, gpio, packed);
3208 if (rc == -ENOTSUPP) {
3209 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
3210 gpio);
3211 return 0;
3212 }
3213
3214 return rc;
3215}
3216EXPORT_SYMBOL_GPL(gpiod_set_transitory);
3217
Googler9398cc32022-12-02 17:21:52 +08003218/**
Googleraf606d22022-10-26 21:40:12 -07003219 * gpiod_is_active_low - test whether a GPIO is active-low or not
3220 * @desc: the gpio descriptor to test
3221 *
3222 * Returns 1 if the GPIO is active-low, 0 otherwise.
3223 */
3224int gpiod_is_active_low(const struct gpio_desc *desc)
3225{
3226 VALIDATE_DESC(desc);
3227 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
3228}
3229EXPORT_SYMBOL_GPL(gpiod_is_active_low);
3230
Googler9398cc32022-12-02 17:21:52 +08003231/**
3232 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
3233 * @desc: the gpio descriptor to change
3234 */
3235void gpiod_toggle_active_low(struct gpio_desc *desc)
3236{
3237 VALIDATE_DESC_VOID(desc);
3238 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
3239}
3240EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
3241
Googleraf606d22022-10-26 21:40:12 -07003242/* I/O calls are only valid after configuration completed; the relevant
3243 * "is this a valid GPIO" error checks should already have been done.
3244 *
3245 * "Get" operations are often inlinable as reading a pin value register,
3246 * and masking the relevant bit in that register.
3247 *
3248 * When "set" operations are inlinable, they involve writing that mask to
3249 * one register to set a low value, or a different register to set it high.
3250 * Otherwise locking is needed, so there may be little value to inlining.
3251 *
3252 *------------------------------------------------------------------------
3253 *
3254 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
3255 * have requested the GPIO. That can include implicit requesting by
3256 * a direction setting call. Marking a gpio as requested locks its chip
3257 * in memory, guaranteeing that these table lookups need no more locking
3258 * and that gpiochip_remove() will fail.
3259 *
3260 * REVISIT when debugging, consider adding some instrumentation to ensure
3261 * that the GPIO was actually requested.
3262 */
3263
Googler9398cc32022-12-02 17:21:52 +08003264static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
Googleraf606d22022-10-26 21:40:12 -07003265{
3266 struct gpio_chip *chip;
3267 int offset;
3268 int value;
3269
3270 chip = desc->gdev->chip;
3271 offset = gpio_chip_hwgpio(desc);
3272 value = chip->get ? chip->get(chip, offset) : -EIO;
3273 value = value < 0 ? value : !!value;
3274 trace_gpio_value(desc_to_gpio(desc), 1, value);
3275 return value;
3276}
3277
Googler9398cc32022-12-02 17:21:52 +08003278static int gpio_chip_get_multiple(struct gpio_chip *chip,
3279 unsigned long *mask, unsigned long *bits)
3280{
3281 if (chip->get_multiple) {
3282 return chip->get_multiple(chip, mask, bits);
3283 } else if (chip->get) {
3284 int i, value;
3285
3286 for_each_set_bit(i, mask, chip->ngpio) {
3287 value = chip->get(chip, i);
3288 if (value < 0)
3289 return value;
3290 __assign_bit(i, bits, value);
3291 }
3292 return 0;
3293 }
3294 return -EIO;
3295}
3296
3297int gpiod_get_array_value_complex(bool raw, bool can_sleep,
3298 unsigned int array_size,
3299 struct gpio_desc **desc_array,
3300 struct gpio_array *array_info,
3301 unsigned long *value_bitmap)
3302{
3303 int ret, i = 0;
3304
3305 /*
3306 * Validate array_info against desc_array and its size.
3307 * It should immediately follow desc_array if both
3308 * have been obtained from the same gpiod_get_array() call.
3309 */
3310 if (array_info && array_info->desc == desc_array &&
3311 array_size <= array_info->size &&
3312 (void *)array_info == desc_array + array_info->size) {
3313 if (!can_sleep)
3314 WARN_ON(array_info->chip->can_sleep);
3315
3316 ret = gpio_chip_get_multiple(array_info->chip,
3317 array_info->get_mask,
3318 value_bitmap);
3319 if (ret)
3320 return ret;
3321
3322 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3323 bitmap_xor(value_bitmap, value_bitmap,
3324 array_info->invert_mask, array_size);
3325
3326 if (bitmap_full(array_info->get_mask, array_size))
3327 return 0;
3328
3329 i = find_first_zero_bit(array_info->get_mask, array_size);
3330 } else {
3331 array_info = NULL;
3332 }
3333
3334 while (i < array_size) {
3335 struct gpio_chip *chip = desc_array[i]->gdev->chip;
3336 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3337 unsigned long *mask, *bits;
3338 int first, j, ret;
3339
3340 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3341 mask = fastpath;
3342 } else {
3343 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3344 sizeof(*mask),
3345 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3346 if (!mask)
3347 return -ENOMEM;
3348 }
3349
3350 bits = mask + BITS_TO_LONGS(chip->ngpio);
3351 bitmap_zero(mask, chip->ngpio);
3352
3353 if (!can_sleep)
3354 WARN_ON(chip->can_sleep);
3355
3356 /* collect all inputs belonging to the same chip */
3357 first = i;
3358 do {
3359 const struct gpio_desc *desc = desc_array[i];
3360 int hwgpio = gpio_chip_hwgpio(desc);
3361
3362 __set_bit(hwgpio, mask);
3363 i++;
3364
3365 if (array_info)
3366 i = find_next_zero_bit(array_info->get_mask,
3367 array_size, i);
3368 } while ((i < array_size) &&
3369 (desc_array[i]->gdev->chip == chip));
3370
3371 ret = gpio_chip_get_multiple(chip, mask, bits);
3372 if (ret) {
3373 if (mask != fastpath)
3374 kfree(mask);
3375 return ret;
3376 }
3377
3378 for (j = first; j < i; ) {
3379 const struct gpio_desc *desc = desc_array[j];
3380 int hwgpio = gpio_chip_hwgpio(desc);
3381 int value = test_bit(hwgpio, bits);
3382
3383 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3384 value = !value;
3385 __assign_bit(j, value_bitmap, value);
3386 trace_gpio_value(desc_to_gpio(desc), 1, value);
3387 j++;
3388
3389 if (array_info)
3390 j = find_next_zero_bit(array_info->get_mask, i,
3391 j);
3392 }
3393
3394 if (mask != fastpath)
3395 kfree(mask);
3396 }
3397 return 0;
3398}
3399
Googleraf606d22022-10-26 21:40:12 -07003400/**
3401 * gpiod_get_raw_value() - return a gpio's raw value
3402 * @desc: gpio whose value will be returned
3403 *
3404 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
3405 * its ACTIVE_LOW status, or negative errno on failure.
3406 *
Googler9398cc32022-12-02 17:21:52 +08003407 * This function can be called from contexts where we cannot sleep, and will
Googleraf606d22022-10-26 21:40:12 -07003408 * complain if the GPIO chip functions potentially sleep.
3409 */
3410int gpiod_get_raw_value(const struct gpio_desc *desc)
3411{
3412 VALIDATE_DESC(desc);
3413 /* Should be using gpiod_get_raw_value_cansleep() */
3414 WARN_ON(desc->gdev->chip->can_sleep);
Googler9398cc32022-12-02 17:21:52 +08003415 return gpiod_get_raw_value_commit(desc);
Googleraf606d22022-10-26 21:40:12 -07003416}
3417EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
3418
3419/**
3420 * gpiod_get_value() - return a gpio's value
3421 * @desc: gpio whose value will be returned
3422 *
3423 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
3424 * account, or negative errno on failure.
3425 *
Googler9398cc32022-12-02 17:21:52 +08003426 * This function can be called from contexts where we cannot sleep, and will
Googleraf606d22022-10-26 21:40:12 -07003427 * complain if the GPIO chip functions potentially sleep.
3428 */
3429int gpiod_get_value(const struct gpio_desc *desc)
3430{
3431 int value;
3432
3433 VALIDATE_DESC(desc);
3434 /* Should be using gpiod_get_value_cansleep() */
3435 WARN_ON(desc->gdev->chip->can_sleep);
3436
Googler9398cc32022-12-02 17:21:52 +08003437 value = gpiod_get_raw_value_commit(desc);
Googleraf606d22022-10-26 21:40:12 -07003438 if (value < 0)
3439 return value;
3440
3441 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3442 value = !value;
3443
3444 return value;
3445}
3446EXPORT_SYMBOL_GPL(gpiod_get_value);
3447
Googler9398cc32022-12-02 17:21:52 +08003448/**
3449 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
3450 * @array_size: number of elements in the descriptor array / value bitmap
3451 * @desc_array: array of GPIO descriptors whose values will be read
3452 * @array_info: information on applicability of fast bitmap processing path
3453 * @value_bitmap: bitmap to store the read values
3454 *
3455 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3456 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3457 * else an error code.
3458 *
3459 * This function can be called from contexts where we cannot sleep,
3460 * and it will complain if the GPIO chip functions potentially sleep.
3461 */
3462int gpiod_get_raw_array_value(unsigned int array_size,
3463 struct gpio_desc **desc_array,
3464 struct gpio_array *array_info,
3465 unsigned long *value_bitmap)
3466{
3467 if (!desc_array)
3468 return -EINVAL;
3469 return gpiod_get_array_value_complex(true, false, array_size,
3470 desc_array, array_info,
3471 value_bitmap);
3472}
3473EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
3474
3475/**
3476 * gpiod_get_array_value() - read values from an array of GPIOs
3477 * @array_size: number of elements in the descriptor array / value bitmap
3478 * @desc_array: array of GPIO descriptors whose values will be read
3479 * @array_info: information on applicability of fast bitmap processing path
3480 * @value_bitmap: bitmap to store the read values
3481 *
3482 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3483 * into account. Return 0 in case of success, else an error code.
3484 *
3485 * This function can be called from contexts where we cannot sleep,
3486 * and it will complain if the GPIO chip functions potentially sleep.
3487 */
3488int gpiod_get_array_value(unsigned int array_size,
3489 struct gpio_desc **desc_array,
3490 struct gpio_array *array_info,
3491 unsigned long *value_bitmap)
3492{
3493 if (!desc_array)
3494 return -EINVAL;
3495 return gpiod_get_array_value_complex(false, false, array_size,
3496 desc_array, array_info,
3497 value_bitmap);
3498}
3499EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3500
Googleraf606d22022-10-26 21:40:12 -07003501/*
Googler9398cc32022-12-02 17:21:52 +08003502 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Googleraf606d22022-10-26 21:40:12 -07003503 * @desc: gpio descriptor whose state need to be set.
3504 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3505 */
Googler9398cc32022-12-02 17:21:52 +08003506static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Googleraf606d22022-10-26 21:40:12 -07003507{
Googler9398cc32022-12-02 17:21:52 +08003508 int ret = 0;
Googleraf606d22022-10-26 21:40:12 -07003509 struct gpio_chip *chip = desc->gdev->chip;
3510 int offset = gpio_chip_hwgpio(desc);
3511
3512 if (value) {
Googler9398cc32022-12-02 17:21:52 +08003513 ret = chip->direction_input(chip, offset);
Googleraf606d22022-10-26 21:40:12 -07003514 } else {
Googler9398cc32022-12-02 17:21:52 +08003515 ret = chip->direction_output(chip, offset, 0);
3516 if (!ret)
Googleraf606d22022-10-26 21:40:12 -07003517 set_bit(FLAG_IS_OUT, &desc->flags);
3518 }
Googler9398cc32022-12-02 17:21:52 +08003519 trace_gpio_direction(desc_to_gpio(desc), value, ret);
3520 if (ret < 0)
Googleraf606d22022-10-26 21:40:12 -07003521 gpiod_err(desc,
3522 "%s: Error in set_value for open drain err %d\n",
Googler9398cc32022-12-02 17:21:52 +08003523 __func__, ret);
Googleraf606d22022-10-26 21:40:12 -07003524}
3525
3526/*
3527 * _gpio_set_open_source_value() - Set the open source gpio's value.
3528 * @desc: gpio descriptor whose state need to be set.
3529 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
3530 */
Googler9398cc32022-12-02 17:21:52 +08003531static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Googleraf606d22022-10-26 21:40:12 -07003532{
Googler9398cc32022-12-02 17:21:52 +08003533 int ret = 0;
Googleraf606d22022-10-26 21:40:12 -07003534 struct gpio_chip *chip = desc->gdev->chip;
3535 int offset = gpio_chip_hwgpio(desc);
3536
3537 if (value) {
Googler9398cc32022-12-02 17:21:52 +08003538 ret = chip->direction_output(chip, offset, 1);
3539 if (!ret)
Googleraf606d22022-10-26 21:40:12 -07003540 set_bit(FLAG_IS_OUT, &desc->flags);
3541 } else {
Googler9398cc32022-12-02 17:21:52 +08003542 ret = chip->direction_input(chip, offset);
Googleraf606d22022-10-26 21:40:12 -07003543 }
Googler9398cc32022-12-02 17:21:52 +08003544 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
3545 if (ret < 0)
Googleraf606d22022-10-26 21:40:12 -07003546 gpiod_err(desc,
3547 "%s: Error in set_value for open source err %d\n",
Googler9398cc32022-12-02 17:21:52 +08003548 __func__, ret);
Googleraf606d22022-10-26 21:40:12 -07003549}
3550
Googler9398cc32022-12-02 17:21:52 +08003551static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
Googleraf606d22022-10-26 21:40:12 -07003552{
3553 struct gpio_chip *chip;
3554
3555 chip = desc->gdev->chip;
3556 trace_gpio_value(desc_to_gpio(desc), 0, value);
Googler9398cc32022-12-02 17:21:52 +08003557 chip->set(chip, gpio_chip_hwgpio(desc), value);
Googleraf606d22022-10-26 21:40:12 -07003558}
3559
3560/*
3561 * set multiple outputs on the same chip;
3562 * use the chip's set_multiple function if available;
3563 * otherwise set the outputs sequentially;
3564 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3565 * defines which outputs are to be changed
3566 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3567 * defines the values the outputs specified by mask are to be set to
3568 */
3569static void gpio_chip_set_multiple(struct gpio_chip *chip,
3570 unsigned long *mask, unsigned long *bits)
3571{
3572 if (chip->set_multiple) {
3573 chip->set_multiple(chip, mask, bits);
3574 } else {
Googler9398cc32022-12-02 17:21:52 +08003575 unsigned int i;
3576
3577 /* set outputs if the corresponding mask bit is set */
3578 for_each_set_bit(i, mask, chip->ngpio)
3579 chip->set(chip, i, test_bit(i, bits));
Googleraf606d22022-10-26 21:40:12 -07003580 }
3581}
3582
Googler9398cc32022-12-02 17:21:52 +08003583int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3584 unsigned int array_size,
3585 struct gpio_desc **desc_array,
3586 struct gpio_array *array_info,
3587 unsigned long *value_bitmap)
Googleraf606d22022-10-26 21:40:12 -07003588{
3589 int i = 0;
3590
Googler9398cc32022-12-02 17:21:52 +08003591 /*
3592 * Validate array_info against desc_array and its size.
3593 * It should immediately follow desc_array if both
3594 * have been obtained from the same gpiod_get_array() call.
3595 */
3596 if (array_info && array_info->desc == desc_array &&
3597 array_size <= array_info->size &&
3598 (void *)array_info == desc_array + array_info->size) {
3599 if (!can_sleep)
3600 WARN_ON(array_info->chip->can_sleep);
3601
3602 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3603 bitmap_xor(value_bitmap, value_bitmap,
3604 array_info->invert_mask, array_size);
3605
3606 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3607 value_bitmap);
3608
3609 if (bitmap_full(array_info->set_mask, array_size))
3610 return 0;
3611
3612 i = find_first_zero_bit(array_info->set_mask, array_size);
3613 } else {
3614 array_info = NULL;
3615 }
3616
Googleraf606d22022-10-26 21:40:12 -07003617 while (i < array_size) {
3618 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Googler9398cc32022-12-02 17:21:52 +08003619 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3620 unsigned long *mask, *bits;
Googleraf606d22022-10-26 21:40:12 -07003621 int count = 0;
3622
Googler9398cc32022-12-02 17:21:52 +08003623 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3624 mask = fastpath;
3625 } else {
3626 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3627 sizeof(*mask),
3628 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3629 if (!mask)
3630 return -ENOMEM;
3631 }
3632
3633 bits = mask + BITS_TO_LONGS(chip->ngpio);
3634 bitmap_zero(mask, chip->ngpio);
3635
Googleraf606d22022-10-26 21:40:12 -07003636 if (!can_sleep)
3637 WARN_ON(chip->can_sleep);
3638
3639 do {
3640 struct gpio_desc *desc = desc_array[i];
3641 int hwgpio = gpio_chip_hwgpio(desc);
Googler9398cc32022-12-02 17:21:52 +08003642 int value = test_bit(i, value_bitmap);
Googleraf606d22022-10-26 21:40:12 -07003643
Googler9398cc32022-12-02 17:21:52 +08003644 /*
3645 * Pins applicable for fast input but not for
3646 * fast output processing may have been already
3647 * inverted inside the fast path, skip them.
3648 */
3649 if (!raw && !(array_info &&
3650 test_bit(i, array_info->invert_mask)) &&
3651 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Googleraf606d22022-10-26 21:40:12 -07003652 value = !value;
3653 trace_gpio_value(desc_to_gpio(desc), 0, value);
3654 /*
3655 * collect all normal outputs belonging to the same chip
3656 * open drain and open source outputs are set individually
3657 */
Googler9398cc32022-12-02 17:21:52 +08003658 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
3659 gpio_set_open_drain_value_commit(desc, value);
3660 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
3661 gpio_set_open_source_value_commit(desc, value);
Googleraf606d22022-10-26 21:40:12 -07003662 } else {
3663 __set_bit(hwgpio, mask);
3664 if (value)
3665 __set_bit(hwgpio, bits);
3666 else
3667 __clear_bit(hwgpio, bits);
3668 count++;
3669 }
3670 i++;
Googler9398cc32022-12-02 17:21:52 +08003671
3672 if (array_info)
3673 i = find_next_zero_bit(array_info->set_mask,
3674 array_size, i);
Googleraf606d22022-10-26 21:40:12 -07003675 } while ((i < array_size) &&
3676 (desc_array[i]->gdev->chip == chip));
3677 /* push collected bits to outputs */
3678 if (count != 0)
3679 gpio_chip_set_multiple(chip, mask, bits);
Googler9398cc32022-12-02 17:21:52 +08003680
3681 if (mask != fastpath)
3682 kfree(mask);
Googleraf606d22022-10-26 21:40:12 -07003683 }
Googler9398cc32022-12-02 17:21:52 +08003684 return 0;
Googleraf606d22022-10-26 21:40:12 -07003685}
3686
3687/**
3688 * gpiod_set_raw_value() - assign a gpio's raw value
3689 * @desc: gpio whose value will be assigned
3690 * @value: value to assign
3691 *
3692 * Set the raw value of the GPIO, i.e. the value of its physical line without
3693 * regard for its ACTIVE_LOW status.
3694 *
Googler9398cc32022-12-02 17:21:52 +08003695 * This function can be called from contexts where we cannot sleep, and will
Googleraf606d22022-10-26 21:40:12 -07003696 * complain if the GPIO chip functions potentially sleep.
3697 */
3698void gpiod_set_raw_value(struct gpio_desc *desc, int value)
3699{
3700 VALIDATE_DESC_VOID(desc);
3701 /* Should be using gpiod_set_raw_value_cansleep() */
3702 WARN_ON(desc->gdev->chip->can_sleep);
Googler9398cc32022-12-02 17:21:52 +08003703 gpiod_set_raw_value_commit(desc, value);
Googleraf606d22022-10-26 21:40:12 -07003704}
3705EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
3706
3707/**
Googler9398cc32022-12-02 17:21:52 +08003708 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3709 * @desc: the descriptor to set the value on
3710 * @value: value to set
3711 *
3712 * This sets the value of a GPIO line backing a descriptor, applying
3713 * different semantic quirks like active low and open drain/source
3714 * handling.
3715 */
3716static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3717{
3718 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3719 value = !value;
3720 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3721 gpio_set_open_drain_value_commit(desc, value);
3722 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3723 gpio_set_open_source_value_commit(desc, value);
3724 else
3725 gpiod_set_raw_value_commit(desc, value);
3726}
3727
3728/**
Googleraf606d22022-10-26 21:40:12 -07003729 * gpiod_set_value() - assign a gpio's value
3730 * @desc: gpio whose value will be assigned
3731 * @value: value to assign
3732 *
Googler9398cc32022-12-02 17:21:52 +08003733 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3734 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Googleraf606d22022-10-26 21:40:12 -07003735 *
Googler9398cc32022-12-02 17:21:52 +08003736 * This function can be called from contexts where we cannot sleep, and will
Googleraf606d22022-10-26 21:40:12 -07003737 * complain if the GPIO chip functions potentially sleep.
3738 */
3739void gpiod_set_value(struct gpio_desc *desc, int value)
3740{
3741 VALIDATE_DESC_VOID(desc);
3742 /* Should be using gpiod_set_value_cansleep() */
3743 WARN_ON(desc->gdev->chip->can_sleep);
Googler9398cc32022-12-02 17:21:52 +08003744 gpiod_set_value_nocheck(desc, value);
Googleraf606d22022-10-26 21:40:12 -07003745}
3746EXPORT_SYMBOL_GPL(gpiod_set_value);
3747
3748/**
3749 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Googler9398cc32022-12-02 17:21:52 +08003750 * @array_size: number of elements in the descriptor array / value bitmap
Googleraf606d22022-10-26 21:40:12 -07003751 * @desc_array: array of GPIO descriptors whose values will be assigned
Googler9398cc32022-12-02 17:21:52 +08003752 * @array_info: information on applicability of fast bitmap processing path
3753 * @value_bitmap: bitmap of values to assign
Googleraf606d22022-10-26 21:40:12 -07003754 *
3755 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3756 * without regard for their ACTIVE_LOW status.
3757 *
Googler9398cc32022-12-02 17:21:52 +08003758 * This function can be called from contexts where we cannot sleep, and will
Googleraf606d22022-10-26 21:40:12 -07003759 * complain if the GPIO chip functions potentially sleep.
3760 */
Googler9398cc32022-12-02 17:21:52 +08003761int gpiod_set_raw_array_value(unsigned int array_size,
3762 struct gpio_desc **desc_array,
3763 struct gpio_array *array_info,
3764 unsigned long *value_bitmap)
Googleraf606d22022-10-26 21:40:12 -07003765{
3766 if (!desc_array)
Googler9398cc32022-12-02 17:21:52 +08003767 return -EINVAL;
3768 return gpiod_set_array_value_complex(true, false, array_size,
3769 desc_array, array_info, value_bitmap);
Googleraf606d22022-10-26 21:40:12 -07003770}
3771EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
3772
3773/**
3774 * gpiod_set_array_value() - assign values to an array of GPIOs
Googler9398cc32022-12-02 17:21:52 +08003775 * @array_size: number of elements in the descriptor array / value bitmap
Googleraf606d22022-10-26 21:40:12 -07003776 * @desc_array: array of GPIO descriptors whose values will be assigned
Googler9398cc32022-12-02 17:21:52 +08003777 * @array_info: information on applicability of fast bitmap processing path
3778 * @value_bitmap: bitmap of values to assign
Googleraf606d22022-10-26 21:40:12 -07003779 *
3780 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3781 * into account.
3782 *
Googler9398cc32022-12-02 17:21:52 +08003783 * This function can be called from contexts where we cannot sleep, and will
Googleraf606d22022-10-26 21:40:12 -07003784 * complain if the GPIO chip functions potentially sleep.
3785 */
Googler9398cc32022-12-02 17:21:52 +08003786int gpiod_set_array_value(unsigned int array_size,
3787 struct gpio_desc **desc_array,
3788 struct gpio_array *array_info,
3789 unsigned long *value_bitmap)
Googleraf606d22022-10-26 21:40:12 -07003790{
3791 if (!desc_array)
Googler9398cc32022-12-02 17:21:52 +08003792 return -EINVAL;
3793 return gpiod_set_array_value_complex(false, false, array_size,
3794 desc_array, array_info,
3795 value_bitmap);
Googleraf606d22022-10-26 21:40:12 -07003796}
3797EXPORT_SYMBOL_GPL(gpiod_set_array_value);
3798
3799/**
3800 * gpiod_cansleep() - report whether gpio value access may sleep
3801 * @desc: gpio to check
3802 *
3803 */
3804int gpiod_cansleep(const struct gpio_desc *desc)
3805{
3806 VALIDATE_DESC(desc);
3807 return desc->gdev->chip->can_sleep;
3808}
3809EXPORT_SYMBOL_GPL(gpiod_cansleep);
3810
3811/**
Googler9398cc32022-12-02 17:21:52 +08003812 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3813 * @desc: gpio to set the consumer name on
3814 * @name: the new consumer name
3815 */
3816int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3817{
3818 VALIDATE_DESC(desc);
3819 if (name) {
3820 name = kstrdup_const(name, GFP_KERNEL);
3821 if (!name)
3822 return -ENOMEM;
3823 }
3824
3825 kfree_const(desc->label);
3826 desc_set_label(desc, name);
3827
3828 return 0;
3829}
3830EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3831
3832/**
Googleraf606d22022-10-26 21:40:12 -07003833 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3834 * @desc: gpio whose IRQ will be returned (already requested)
3835 *
3836 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3837 * error.
3838 */
3839int gpiod_to_irq(const struct gpio_desc *desc)
3840{
3841 struct gpio_chip *chip;
3842 int offset;
3843
3844 /*
3845 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3846 * requires this function to not return zero on an invalid descriptor
3847 * but rather a negative error number.
3848 */
3849 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
3850 return -EINVAL;
3851
3852 chip = desc->gdev->chip;
3853 offset = gpio_chip_hwgpio(desc);
3854 if (chip->to_irq) {
3855 int retirq = chip->to_irq(chip, offset);
3856
3857 /* Zero means NO_IRQ */
3858 if (!retirq)
3859 return -ENXIO;
3860
3861 return retirq;
3862 }
3863 return -ENXIO;
3864}
3865EXPORT_SYMBOL_GPL(gpiod_to_irq);
3866
3867/**
3868 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3869 * @chip: the chip the GPIO to lock belongs to
3870 * @offset: the offset of the GPIO to lock as IRQ
3871 *
3872 * This is used directly by GPIO drivers that want to lock down
3873 * a certain GPIO line to be used for IRQs.
3874 */
3875int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
3876{
3877 struct gpio_desc *desc;
3878
3879 desc = gpiochip_get_desc(chip, offset);
3880 if (IS_ERR(desc))
3881 return PTR_ERR(desc);
3882
3883 /*
3884 * If it's fast: flush the direction setting if something changed
3885 * behind our back
3886 */
3887 if (!chip->can_sleep && chip->get_direction) {
Googler9398cc32022-12-02 17:21:52 +08003888 int dir = gpiod_get_direction(desc);
Googleraf606d22022-10-26 21:40:12 -07003889
Googler9398cc32022-12-02 17:21:52 +08003890 if (dir < 0) {
3891 chip_err(chip, "%s: cannot get GPIO direction\n",
3892 __func__);
3893 return dir;
3894 }
Googleraf606d22022-10-26 21:40:12 -07003895 }
3896
Googler9398cc32022-12-02 17:21:52 +08003897 /* To be valid for IRQ the line needs to be input or open drain */
3898 if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3899 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Googleraf606d22022-10-26 21:40:12 -07003900 chip_err(chip,
Googler9398cc32022-12-02 17:21:52 +08003901 "%s: tried to flag a GPIO set as output for IRQ\n",
3902 __func__);
Googleraf606d22022-10-26 21:40:12 -07003903 return -EIO;
3904 }
3905
3906 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Googler9398cc32022-12-02 17:21:52 +08003907 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3908
3909 /*
3910 * If the consumer has not set up a label (such as when the
3911 * IRQ is referenced from .to_irq()) we set up a label here
3912 * so it is clear this is used as an interrupt.
3913 */
3914 if (!desc->label)
3915 desc_set_label(desc, "interrupt");
3916
Googleraf606d22022-10-26 21:40:12 -07003917 return 0;
3918}
3919EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
3920
3921/**
3922 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3923 * @chip: the chip the GPIO to lock belongs to
3924 * @offset: the offset of the GPIO to lock as IRQ
3925 *
3926 * This is used directly by GPIO drivers that want to indicate
3927 * that a certain GPIO is no longer used exclusively for IRQ.
3928 */
3929void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
3930{
Googler9398cc32022-12-02 17:21:52 +08003931 struct gpio_desc *desc;
3932
3933 desc = gpiochip_get_desc(chip, offset);
3934 if (IS_ERR(desc))
Googleraf606d22022-10-26 21:40:12 -07003935 return;
3936
Googler9398cc32022-12-02 17:21:52 +08003937 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3938 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3939
3940 /* If we only had this marking, erase it */
3941 if (desc->label && !strcmp(desc->label, "interrupt"))
3942 desc_set_label(desc, NULL);
Googleraf606d22022-10-26 21:40:12 -07003943}
3944EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
3945
Googler9398cc32022-12-02 17:21:52 +08003946void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset)
3947{
3948 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
3949
3950 if (!IS_ERR(desc) &&
3951 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3952 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3953}
3954EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3955
3956void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset)
3957{
3958 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
3959
3960 if (!IS_ERR(desc) &&
3961 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3962 /*
3963 * We must not be output when using IRQ UNLESS we are
3964 * open drain.
3965 */
3966 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3967 !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3968 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3969 }
3970}
3971EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3972
Googleraf606d22022-10-26 21:40:12 -07003973bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3974{
3975 if (offset >= chip->ngpio)
3976 return false;
3977
3978 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3979}
3980EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3981
Googler9398cc32022-12-02 17:21:52 +08003982int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset)
3983{
3984 int ret;
3985
3986 if (!try_module_get(chip->gpiodev->owner))
3987 return -ENODEV;
3988
3989 ret = gpiochip_lock_as_irq(chip, offset);
3990 if (ret) {
3991 chip_err(chip, "unable to lock HW IRQ %u for IRQ\n", offset);
3992 module_put(chip->gpiodev->owner);
3993 return ret;
3994 }
3995 return 0;
3996}
3997EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3998
3999void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset)
4000{
4001 gpiochip_unlock_as_irq(chip, offset);
4002 module_put(chip->gpiodev->owner);
4003}
4004EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
4005
Googleraf606d22022-10-26 21:40:12 -07004006bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
4007{
4008 if (offset >= chip->ngpio)
4009 return false;
4010
4011 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
4012}
4013EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
4014
4015bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
4016{
4017 if (offset >= chip->ngpio)
4018 return false;
4019
4020 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
4021}
4022EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
4023
Googler9398cc32022-12-02 17:21:52 +08004024bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
4025{
4026 if (offset >= chip->ngpio)
4027 return false;
4028
4029 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
4030}
4031EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
4032
Googleraf606d22022-10-26 21:40:12 -07004033/**
4034 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
4035 * @desc: gpio whose value will be returned
4036 *
4037 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
4038 * its ACTIVE_LOW status, or negative errno on failure.
4039 *
4040 * This function is to be called from contexts that can sleep.
4041 */
4042int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
4043{
4044 might_sleep_if(extra_checks);
4045 VALIDATE_DESC(desc);
Googler9398cc32022-12-02 17:21:52 +08004046 return gpiod_get_raw_value_commit(desc);
Googleraf606d22022-10-26 21:40:12 -07004047}
4048EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
4049
4050/**
4051 * gpiod_get_value_cansleep() - return a gpio's value
4052 * @desc: gpio whose value will be returned
4053 *
4054 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
4055 * account, or negative errno on failure.
4056 *
4057 * This function is to be called from contexts that can sleep.
4058 */
4059int gpiod_get_value_cansleep(const struct gpio_desc *desc)
4060{
4061 int value;
4062
4063 might_sleep_if(extra_checks);
4064 VALIDATE_DESC(desc);
Googler9398cc32022-12-02 17:21:52 +08004065 value = gpiod_get_raw_value_commit(desc);
Googleraf606d22022-10-26 21:40:12 -07004066 if (value < 0)
4067 return value;
4068
4069 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
4070 value = !value;
4071
4072 return value;
4073}
4074EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
4075
4076/**
Googler9398cc32022-12-02 17:21:52 +08004077 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
4078 * @array_size: number of elements in the descriptor array / value bitmap
4079 * @desc_array: array of GPIO descriptors whose values will be read
4080 * @array_info: information on applicability of fast bitmap processing path
4081 * @value_bitmap: bitmap to store the read values
4082 *
4083 * Read the raw values of the GPIOs, i.e. the values of the physical lines
4084 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
4085 * else an error code.
4086 *
4087 * This function is to be called from contexts that can sleep.
4088 */
4089int gpiod_get_raw_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(true, true, array_size,
4098 desc_array, array_info,
4099 value_bitmap);
4100}
4101EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
4102
4103/**
4104 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
4105 * @array_size: number of elements in the descriptor array / value bitmap
4106 * @desc_array: array of GPIO descriptors whose values will be read
4107 * @array_info: information on applicability of fast bitmap processing path
4108 * @value_bitmap: bitmap to store the read values
4109 *
4110 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4111 * into account. Return 0 in case of success, else an error code.
4112 *
4113 * This function is to be called from contexts that can sleep.
4114 */
4115int gpiod_get_array_value_cansleep(unsigned int array_size,
4116 struct gpio_desc **desc_array,
4117 struct gpio_array *array_info,
4118 unsigned long *value_bitmap)
4119{
4120 might_sleep_if(extra_checks);
4121 if (!desc_array)
4122 return -EINVAL;
4123 return gpiod_get_array_value_complex(false, true, array_size,
4124 desc_array, array_info,
4125 value_bitmap);
4126}
4127EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
4128
4129/**
Googleraf606d22022-10-26 21:40:12 -07004130 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
4131 * @desc: gpio whose value will be assigned
4132 * @value: value to assign
4133 *
4134 * Set the raw value of the GPIO, i.e. the value of its physical line without
4135 * regard for its ACTIVE_LOW status.
4136 *
4137 * This function is to be called from contexts that can sleep.
4138 */
4139void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
4140{
4141 might_sleep_if(extra_checks);
4142 VALIDATE_DESC_VOID(desc);
Googler9398cc32022-12-02 17:21:52 +08004143 gpiod_set_raw_value_commit(desc, value);
Googleraf606d22022-10-26 21:40:12 -07004144}
4145EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
4146
4147/**
4148 * gpiod_set_value_cansleep() - assign a gpio's value
4149 * @desc: gpio whose value will be assigned
4150 * @value: value to assign
4151 *
4152 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
4153 * account
4154 *
4155 * This function is to be called from contexts that can sleep.
4156 */
4157void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
4158{
4159 might_sleep_if(extra_checks);
4160 VALIDATE_DESC_VOID(desc);
Googler9398cc32022-12-02 17:21:52 +08004161 gpiod_set_value_nocheck(desc, value);
Googleraf606d22022-10-26 21:40:12 -07004162}
4163EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
4164
4165/**
4166 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Googler9398cc32022-12-02 17:21:52 +08004167 * @array_size: number of elements in the descriptor array / value bitmap
Googleraf606d22022-10-26 21:40:12 -07004168 * @desc_array: array of GPIO descriptors whose values will be assigned
Googler9398cc32022-12-02 17:21:52 +08004169 * @array_info: information on applicability of fast bitmap processing path
4170 * @value_bitmap: bitmap of values to assign
Googleraf606d22022-10-26 21:40:12 -07004171 *
4172 * Set the raw values of the GPIOs, i.e. the values of the physical lines
4173 * without regard for their ACTIVE_LOW status.
4174 *
4175 * This function is to be called from contexts that can sleep.
4176 */
Googler9398cc32022-12-02 17:21:52 +08004177int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
4178 struct gpio_desc **desc_array,
4179 struct gpio_array *array_info,
4180 unsigned long *value_bitmap)
Googleraf606d22022-10-26 21:40:12 -07004181{
4182 might_sleep_if(extra_checks);
4183 if (!desc_array)
Googler9398cc32022-12-02 17:21:52 +08004184 return -EINVAL;
4185 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
4186 array_info, value_bitmap);
Googleraf606d22022-10-26 21:40:12 -07004187}
4188EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
4189
4190/**
Googler9398cc32022-12-02 17:21:52 +08004191 * gpiod_add_lookup_tables() - register GPIO device consumers
4192 * @tables: list of tables of consumers to register
4193 * @n: number of tables in the list
4194 */
4195void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
4196{
4197 unsigned int i;
4198
4199 mutex_lock(&gpio_lookup_lock);
4200
4201 for (i = 0; i < n; i++)
4202 list_add_tail(&tables[i]->list, &gpio_lookup_list);
4203
4204 mutex_unlock(&gpio_lookup_lock);
4205}
4206
4207/**
Googleraf606d22022-10-26 21:40:12 -07004208 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Googler9398cc32022-12-02 17:21:52 +08004209 * @array_size: number of elements in the descriptor array / value bitmap
Googleraf606d22022-10-26 21:40:12 -07004210 * @desc_array: array of GPIO descriptors whose values will be assigned
Googler9398cc32022-12-02 17:21:52 +08004211 * @array_info: information on applicability of fast bitmap processing path
4212 * @value_bitmap: bitmap of values to assign
Googleraf606d22022-10-26 21:40:12 -07004213 *
4214 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4215 * into account.
4216 *
4217 * This function is to be called from contexts that can sleep.
4218 */
Googler9398cc32022-12-02 17:21:52 +08004219int gpiod_set_array_value_cansleep(unsigned int array_size,
4220 struct gpio_desc **desc_array,
4221 struct gpio_array *array_info,
4222 unsigned long *value_bitmap)
Googleraf606d22022-10-26 21:40:12 -07004223{
4224 might_sleep_if(extra_checks);
4225 if (!desc_array)
Googler9398cc32022-12-02 17:21:52 +08004226 return -EINVAL;
4227 return gpiod_set_array_value_complex(false, true, array_size,
4228 desc_array, array_info,
4229 value_bitmap);
Googleraf606d22022-10-26 21:40:12 -07004230}
4231EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
4232
4233/**
4234 * gpiod_add_lookup_table() - register GPIO device consumers
4235 * @table: table of consumers to register
4236 */
4237void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
4238{
4239 mutex_lock(&gpio_lookup_lock);
4240
4241 list_add_tail(&table->list, &gpio_lookup_list);
4242
4243 mutex_unlock(&gpio_lookup_lock);
4244}
Googler9398cc32022-12-02 17:21:52 +08004245EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
Googleraf606d22022-10-26 21:40:12 -07004246
4247/**
4248 * gpiod_remove_lookup_table() - unregister GPIO device consumers
4249 * @table: table of consumers to unregister
4250 */
4251void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
4252{
4253 mutex_lock(&gpio_lookup_lock);
4254
4255 list_del(&table->list);
4256
4257 mutex_unlock(&gpio_lookup_lock);
4258}
Googler9398cc32022-12-02 17:21:52 +08004259EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
4260
4261/**
4262 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
4263 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
4264 */
4265void gpiod_add_hogs(struct gpiod_hog *hogs)
4266{
4267 struct gpio_chip *chip;
4268 struct gpiod_hog *hog;
4269
4270 mutex_lock(&gpio_machine_hogs_mutex);
4271
4272 for (hog = &hogs[0]; hog->chip_label; hog++) {
4273 list_add_tail(&hog->list, &gpio_machine_hogs);
4274
4275 /*
4276 * The chip may have been registered earlier, so check if it
4277 * exists and, if so, try to hog the line now.
4278 */
4279 chip = find_chip_by_name(hog->chip_label);
4280 if (chip)
4281 gpiochip_machine_hog(chip, hog);
4282 }
4283
4284 mutex_unlock(&gpio_machine_hogs_mutex);
4285}
4286EXPORT_SYMBOL_GPL(gpiod_add_hogs);
Googleraf606d22022-10-26 21:40:12 -07004287
4288static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
4289{
4290 const char *dev_id = dev ? dev_name(dev) : NULL;
4291 struct gpiod_lookup_table *table;
4292
4293 mutex_lock(&gpio_lookup_lock);
4294
4295 list_for_each_entry(table, &gpio_lookup_list, list) {
4296 if (table->dev_id && dev_id) {
4297 /*
4298 * Valid strings on both ends, must be identical to have
4299 * a match
4300 */
4301 if (!strcmp(table->dev_id, dev_id))
4302 goto found;
4303 } else {
4304 /*
4305 * One of the pointers is NULL, so both must be to have
4306 * a match
4307 */
4308 if (dev_id == table->dev_id)
4309 goto found;
4310 }
4311 }
4312 table = NULL;
4313
4314found:
4315 mutex_unlock(&gpio_lookup_lock);
4316 return table;
4317}
4318
4319static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Googler9398cc32022-12-02 17:21:52 +08004320 unsigned int idx, unsigned long *flags)
Googleraf606d22022-10-26 21:40:12 -07004321{
4322 struct gpio_desc *desc = ERR_PTR(-ENOENT);
4323 struct gpiod_lookup_table *table;
4324 struct gpiod_lookup *p;
4325
4326 table = gpiod_find_lookup_table(dev);
4327 if (!table)
4328 return desc;
4329
4330 for (p = &table->table[0]; p->chip_label; p++) {
4331 struct gpio_chip *chip;
4332
4333 /* idx must always match exactly */
4334 if (p->idx != idx)
4335 continue;
4336
4337 /* If the lookup entry has a con_id, require exact match */
4338 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
4339 continue;
4340
4341 chip = find_chip_by_name(p->chip_label);
4342
4343 if (!chip) {
Googler9398cc32022-12-02 17:21:52 +08004344 /*
4345 * As the lookup table indicates a chip with
4346 * p->chip_label should exist, assume it may
4347 * still appear later and let the interested
4348 * consumer be probed again or let the Deferred
4349 * Probe infrastructure handle the error.
4350 */
4351 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
4352 p->chip_label);
4353 return ERR_PTR(-EPROBE_DEFER);
Googleraf606d22022-10-26 21:40:12 -07004354 }
4355
4356 if (chip->ngpio <= p->chip_hwnum) {
4357 dev_err(dev,
4358 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
4359 idx, p->chip_hwnum, chip->ngpio - 1,
4360 chip->label);
4361 return ERR_PTR(-EINVAL);
4362 }
4363
4364 desc = gpiochip_get_desc(chip, p->chip_hwnum);
4365 *flags = p->flags;
4366
4367 return desc;
4368 }
4369
4370 return desc;
4371}
4372
4373static int platform_gpio_count(struct device *dev, const char *con_id)
4374{
4375 struct gpiod_lookup_table *table;
4376 struct gpiod_lookup *p;
4377 unsigned int count = 0;
4378
4379 table = gpiod_find_lookup_table(dev);
4380 if (!table)
4381 return -ENOENT;
4382
4383 for (p = &table->table[0]; p->chip_label; p++) {
4384 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
4385 (!con_id && !p->con_id))
4386 count++;
4387 }
4388 if (!count)
4389 return -ENOENT;
4390
4391 return count;
4392}
4393
4394/**
4395 * gpiod_count - return the number of GPIOs associated with a device / function
4396 * or -ENOENT if no GPIO has been assigned to the requested function
4397 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4398 * @con_id: function within the GPIO consumer
4399 */
4400int gpiod_count(struct device *dev, const char *con_id)
4401{
4402 int count = -ENOENT;
4403
4404 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
Googler9398cc32022-12-02 17:21:52 +08004405 count = of_gpio_get_count(dev, con_id);
Googleraf606d22022-10-26 21:40:12 -07004406 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
4407 count = acpi_gpio_count(dev, con_id);
4408
4409 if (count < 0)
4410 count = platform_gpio_count(dev, con_id);
4411
4412 return count;
4413}
4414EXPORT_SYMBOL_GPL(gpiod_count);
4415
4416/**
4417 * gpiod_get - obtain a GPIO for a given GPIO function
4418 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4419 * @con_id: function within the GPIO consumer
4420 * @flags: optional GPIO initialization flags
4421 *
4422 * Return the GPIO descriptor corresponding to the function con_id of device
4423 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
4424 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
4425 */
4426struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
4427 enum gpiod_flags flags)
4428{
4429 return gpiod_get_index(dev, con_id, 0, flags);
4430}
4431EXPORT_SYMBOL_GPL(gpiod_get);
4432
4433/**
4434 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4435 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4436 * @con_id: function within the GPIO consumer
4437 * @flags: optional GPIO initialization flags
4438 *
4439 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4440 * the requested function it will return NULL. This is convenient for drivers
4441 * that need to handle optional GPIOs.
4442 */
4443struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
4444 const char *con_id,
4445 enum gpiod_flags flags)
4446{
4447 return gpiod_get_index_optional(dev, con_id, 0, flags);
4448}
4449EXPORT_SYMBOL_GPL(gpiod_get_optional);
4450
4451
4452/**
4453 * gpiod_configure_flags - helper function to configure a given GPIO
4454 * @desc: gpio whose value will be assigned
4455 * @con_id: function within the GPIO consumer
Googler9398cc32022-12-02 17:21:52 +08004456 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4457 * of_find_gpio() or of_get_gpio_hog()
Googleraf606d22022-10-26 21:40:12 -07004458 * @dflags: gpiod_flags - optional GPIO initialization flags
4459 *
4460 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4461 * requested function and/or index, or another IS_ERR() code if an error
4462 * occurred while trying to acquire the GPIO.
4463 */
Googler9398cc32022-12-02 17:21:52 +08004464int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Googleraf606d22022-10-26 21:40:12 -07004465 unsigned long lflags, enum gpiod_flags dflags)
4466{
Googler9398cc32022-12-02 17:21:52 +08004467 int ret;
Googleraf606d22022-10-26 21:40:12 -07004468
4469 if (lflags & GPIO_ACTIVE_LOW)
4470 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Googler9398cc32022-12-02 17:21:52 +08004471
Googleraf606d22022-10-26 21:40:12 -07004472 if (lflags & GPIO_OPEN_DRAIN)
4473 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Googler9398cc32022-12-02 17:21:52 +08004474 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4475 /*
4476 * This enforces open drain mode from the consumer side.
4477 * This is necessary for some busses like I2C, but the lookup
4478 * should *REALLY* have specified them as open drain in the
4479 * first place, so print a little warning here.
4480 */
4481 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4482 gpiod_warn(desc,
4483 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4484 }
4485
Googleraf606d22022-10-26 21:40:12 -07004486 if (lflags & GPIO_OPEN_SOURCE)
4487 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
4488
Googler9398cc32022-12-02 17:21:52 +08004489 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
4490 gpiod_err(desc,
4491 "both pull-up and pull-down enabled, invalid configuration\n");
4492 return -EINVAL;
4493 }
4494
4495 if (lflags & GPIO_PULL_UP)
4496 set_bit(FLAG_PULL_UP, &desc->flags);
4497 else if (lflags & GPIO_PULL_DOWN)
4498 set_bit(FLAG_PULL_DOWN, &desc->flags);
4499
4500 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4501 if (ret < 0)
4502 return ret;
4503
Googleraf606d22022-10-26 21:40:12 -07004504 /* No particular flag request, return here... */
4505 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4506 pr_debug("no flags found for %s\n", con_id);
4507 return 0;
4508 }
4509
4510 /* Process flags */
4511 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
Googler9398cc32022-12-02 17:21:52 +08004512 ret = gpiod_direction_output(desc,
4513 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Googleraf606d22022-10-26 21:40:12 -07004514 else
Googler9398cc32022-12-02 17:21:52 +08004515 ret = gpiod_direction_input(desc);
Googleraf606d22022-10-26 21:40:12 -07004516
Googler9398cc32022-12-02 17:21:52 +08004517 return ret;
Googleraf606d22022-10-26 21:40:12 -07004518}
4519
4520/**
4521 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
4522 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4523 * @con_id: function within the GPIO consumer
4524 * @idx: index of the GPIO to obtain in the consumer
4525 * @flags: optional GPIO initialization flags
4526 *
4527 * This variant of gpiod_get() allows to access GPIOs other than the first
4528 * defined one for functions that define several GPIOs.
4529 *
4530 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4531 * requested function and/or index, or another IS_ERR() code if an error
4532 * occurred while trying to acquire the GPIO.
4533 */
4534struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
4535 const char *con_id,
4536 unsigned int idx,
4537 enum gpiod_flags flags)
4538{
Googler9398cc32022-12-02 17:21:52 +08004539 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Googleraf606d22022-10-26 21:40:12 -07004540 struct gpio_desc *desc = NULL;
Googler9398cc32022-12-02 17:21:52 +08004541 int ret;
Googleraf606d22022-10-26 21:40:12 -07004542 /* Maybe we have a device name, maybe not */
4543 const char *devname = dev ? dev_name(dev) : "?";
4544
4545 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
4546
4547 if (dev) {
4548 /* Using device tree? */
4549 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
4550 dev_dbg(dev, "using device tree for GPIO lookup\n");
4551 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
4552 } else if (ACPI_COMPANION(dev)) {
4553 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Googler9398cc32022-12-02 17:21:52 +08004554 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Googleraf606d22022-10-26 21:40:12 -07004555 }
4556 }
4557
4558 /*
4559 * Either we are not using DT or ACPI, or their lookup did not return
4560 * a result. In that case, use platform lookup as a fallback.
4561 */
4562 if (!desc || desc == ERR_PTR(-ENOENT)) {
4563 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
4564 desc = gpiod_find(dev, con_id, idx, &lookupflags);
4565 }
4566
4567 if (IS_ERR(desc)) {
Googler9398cc32022-12-02 17:21:52 +08004568 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
Googleraf606d22022-10-26 21:40:12 -07004569 return desc;
4570 }
4571
4572 /*
4573 * If a connection label was passed use that, else attempt to use
4574 * the device name as label
4575 */
Googler9398cc32022-12-02 17:21:52 +08004576 ret = gpiod_request(desc, con_id ? con_id : devname);
4577 if (ret < 0) {
4578 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
4579 /*
4580 * This happens when there are several consumers for
4581 * the same GPIO line: we just return here without
4582 * further initialization. It is a bit if a hack.
4583 * This is necessary to support fixed regulators.
4584 *
4585 * FIXME: Make this more sane and safe.
4586 */
4587 dev_info(dev, "nonexclusive access to GPIO for %s\n",
4588 con_id ? con_id : devname);
4589 return desc;
4590 } else {
4591 return ERR_PTR(ret);
4592 }
4593 }
Googleraf606d22022-10-26 21:40:12 -07004594
Googler9398cc32022-12-02 17:21:52 +08004595 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4596 if (ret < 0) {
Googleraf606d22022-10-26 21:40:12 -07004597 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
4598 gpiod_put(desc);
Googler9398cc32022-12-02 17:21:52 +08004599 return ERR_PTR(ret);
Googleraf606d22022-10-26 21:40:12 -07004600 }
4601
4602 return desc;
4603}
4604EXPORT_SYMBOL_GPL(gpiod_get_index);
4605
4606/**
4607 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4608 * @fwnode: handle of the firmware node
4609 * @propname: name of the firmware property representing the GPIO
Googler9398cc32022-12-02 17:21:52 +08004610 * @index: index of the GPIO to obtain for the consumer
4611 * @dflags: GPIO initialization flags
4612 * @label: label to attach to the requested GPIO
Googleraf606d22022-10-26 21:40:12 -07004613 *
4614 * This function can be used for drivers that get their configuration
Googler9398cc32022-12-02 17:21:52 +08004615 * from opaque firmware.
Googleraf606d22022-10-26 21:40:12 -07004616 *
Googler9398cc32022-12-02 17:21:52 +08004617 * The function properly finds the corresponding GPIO using whatever is the
Googleraf606d22022-10-26 21:40:12 -07004618 * underlying firmware interface and then makes sure that the GPIO
4619 * descriptor is requested before it is returned to the caller.
4620 *
Googler9398cc32022-12-02 17:21:52 +08004621 * Returns:
4622 * On successful request the GPIO pin is configured in accordance with
4623 * provided @dflags.
4624 *
Googleraf606d22022-10-26 21:40:12 -07004625 * In case of error an ERR_PTR() is returned.
4626 */
4627struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Googler9398cc32022-12-02 17:21:52 +08004628 const char *propname, int index,
4629 enum gpiod_flags dflags,
4630 const char *label)
Googleraf606d22022-10-26 21:40:12 -07004631{
Googler9398cc32022-12-02 17:21:52 +08004632 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Googleraf606d22022-10-26 21:40:12 -07004633 struct gpio_desc *desc = ERR_PTR(-ENODEV);
4634 int ret;
4635
4636 if (!fwnode)
4637 return ERR_PTR(-EINVAL);
4638
4639 if (is_of_node(fwnode)) {
Googler9398cc32022-12-02 17:21:52 +08004640 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4641 propname, index,
4642 dflags,
4643 label);
4644 return desc;
Googleraf606d22022-10-26 21:40:12 -07004645 } else if (is_acpi_node(fwnode)) {
4646 struct acpi_gpio_info info;
4647
Googler9398cc32022-12-02 17:21:52 +08004648 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
4649 if (IS_ERR(desc))
4650 return desc;
4651
4652 acpi_gpio_update_gpiod_flags(&dflags, &info);
4653 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
Googleraf606d22022-10-26 21:40:12 -07004654 }
4655
Googler9398cc32022-12-02 17:21:52 +08004656 /* Currently only ACPI takes this path */
4657 ret = gpiod_request(desc, label);
Googleraf606d22022-10-26 21:40:12 -07004658 if (ret)
4659 return ERR_PTR(ret);
4660
Googler9398cc32022-12-02 17:21:52 +08004661 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4662 if (ret < 0) {
4663 gpiod_put(desc);
4664 return ERR_PTR(ret);
Googleraf606d22022-10-26 21:40:12 -07004665 }
4666
4667 return desc;
4668}
4669EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
4670
4671/**
4672 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4673 * function
4674 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4675 * @con_id: function within the GPIO consumer
4676 * @index: index of the GPIO to obtain in the consumer
4677 * @flags: optional GPIO initialization flags
4678 *
4679 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4680 * specified index was assigned to the requested function it will return NULL.
4681 * This is convenient for drivers that need to handle optional GPIOs.
4682 */
4683struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
4684 const char *con_id,
4685 unsigned int index,
4686 enum gpiod_flags flags)
4687{
4688 struct gpio_desc *desc;
4689
4690 desc = gpiod_get_index(dev, con_id, index, flags);
4691 if (IS_ERR(desc)) {
4692 if (PTR_ERR(desc) == -ENOENT)
4693 return NULL;
4694 }
4695
4696 return desc;
4697}
4698EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4699
4700/**
4701 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4702 * @desc: gpio whose value will be assigned
4703 * @name: gpio line name
Googler9398cc32022-12-02 17:21:52 +08004704 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4705 * of_find_gpio() or of_get_gpio_hog()
Googleraf606d22022-10-26 21:40:12 -07004706 * @dflags: gpiod_flags - optional GPIO initialization flags
4707 */
4708int gpiod_hog(struct gpio_desc *desc, const char *name,
4709 unsigned long lflags, enum gpiod_flags dflags)
4710{
4711 struct gpio_chip *chip;
4712 struct gpio_desc *local_desc;
4713 int hwnum;
Googler9398cc32022-12-02 17:21:52 +08004714 int ret;
Googleraf606d22022-10-26 21:40:12 -07004715
4716 chip = gpiod_to_chip(desc);
4717 hwnum = gpio_chip_hwgpio(desc);
4718
Googler9398cc32022-12-02 17:21:52 +08004719 local_desc = gpiochip_request_own_desc(chip, hwnum, name,
4720 lflags, dflags);
Googleraf606d22022-10-26 21:40:12 -07004721 if (IS_ERR(local_desc)) {
Googler9398cc32022-12-02 17:21:52 +08004722 ret = PTR_ERR(local_desc);
Googleraf606d22022-10-26 21:40:12 -07004723 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
Googler9398cc32022-12-02 17:21:52 +08004724 name, chip->label, hwnum, ret);
4725 return ret;
Googleraf606d22022-10-26 21:40:12 -07004726 }
4727
4728 /* Mark GPIO as hogged so it can be identified and removed later */
4729 set_bit(FLAG_IS_HOGGED, &desc->flags);
4730
4731 pr_info("GPIO line %d (%s) hogged as %s%s\n",
4732 desc_to_gpio(desc), name,
4733 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4734 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
4735 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
4736
4737 return 0;
4738}
4739
4740/**
4741 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4742 * @chip: gpio chip to act on
4743 */
4744static void gpiochip_free_hogs(struct gpio_chip *chip)
4745{
4746 int id;
4747
4748 for (id = 0; id < chip->ngpio; id++) {
4749 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
4750 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
4751 }
4752}
4753
4754/**
4755 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4756 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4757 * @con_id: function within the GPIO consumer
4758 * @flags: optional GPIO initialization flags
4759 *
4760 * This function acquires all the GPIOs defined under a given function.
4761 *
4762 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4763 * no GPIO has been assigned to the requested function, or another IS_ERR()
4764 * code if an error occurred while trying to acquire the GPIOs.
4765 */
4766struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4767 const char *con_id,
4768 enum gpiod_flags flags)
4769{
4770 struct gpio_desc *desc;
4771 struct gpio_descs *descs;
Googler9398cc32022-12-02 17:21:52 +08004772 struct gpio_array *array_info = NULL;
4773 struct gpio_chip *chip;
4774 int count, bitmap_size;
Googleraf606d22022-10-26 21:40:12 -07004775
4776 count = gpiod_count(dev, con_id);
4777 if (count < 0)
4778 return ERR_PTR(count);
4779
Googler9398cc32022-12-02 17:21:52 +08004780 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
Googleraf606d22022-10-26 21:40:12 -07004781 if (!descs)
4782 return ERR_PTR(-ENOMEM);
4783
4784 for (descs->ndescs = 0; descs->ndescs < count; ) {
4785 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4786 if (IS_ERR(desc)) {
4787 gpiod_put_array(descs);
4788 return ERR_CAST(desc);
4789 }
Googler9398cc32022-12-02 17:21:52 +08004790
Googleraf606d22022-10-26 21:40:12 -07004791 descs->desc[descs->ndescs] = desc;
Googler9398cc32022-12-02 17:21:52 +08004792
4793 chip = gpiod_to_chip(desc);
4794 /*
4795 * If pin hardware number of array member 0 is also 0, select
4796 * its chip as a candidate for fast bitmap processing path.
4797 */
4798 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4799 struct gpio_descs *array;
4800
4801 bitmap_size = BITS_TO_LONGS(chip->ngpio > count ?
4802 chip->ngpio : count);
4803
4804 array = kzalloc(struct_size(descs, desc, count) +
4805 struct_size(array_info, invert_mask,
4806 3 * bitmap_size), GFP_KERNEL);
4807 if (!array) {
4808 gpiod_put_array(descs);
4809 return ERR_PTR(-ENOMEM);
4810 }
4811
4812 memcpy(array, descs,
4813 struct_size(descs, desc, descs->ndescs + 1));
4814 kfree(descs);
4815
4816 descs = array;
4817 array_info = (void *)(descs->desc + count);
4818 array_info->get_mask = array_info->invert_mask +
4819 bitmap_size;
4820 array_info->set_mask = array_info->get_mask +
4821 bitmap_size;
4822
4823 array_info->desc = descs->desc;
4824 array_info->size = count;
4825 array_info->chip = chip;
4826 bitmap_set(array_info->get_mask, descs->ndescs,
4827 count - descs->ndescs);
4828 bitmap_set(array_info->set_mask, descs->ndescs,
4829 count - descs->ndescs);
4830 descs->info = array_info;
4831 }
4832 /* Unmark array members which don't belong to the 'fast' chip */
4833 if (array_info && array_info->chip != chip) {
4834 __clear_bit(descs->ndescs, array_info->get_mask);
4835 __clear_bit(descs->ndescs, array_info->set_mask);
4836 }
4837 /*
4838 * Detect array members which belong to the 'fast' chip
4839 * but their pins are not in hardware order.
4840 */
4841 else if (array_info &&
4842 gpio_chip_hwgpio(desc) != descs->ndescs) {
4843 /*
4844 * Don't use fast path if all array members processed so
4845 * far belong to the same chip as this one but its pin
4846 * hardware number is different from its array index.
4847 */
4848 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4849 array_info = NULL;
4850 } else {
4851 __clear_bit(descs->ndescs,
4852 array_info->get_mask);
4853 __clear_bit(descs->ndescs,
4854 array_info->set_mask);
4855 }
4856 } else if (array_info) {
4857 /* Exclude open drain or open source from fast output */
4858 if (gpiochip_line_is_open_drain(chip, descs->ndescs) ||
4859 gpiochip_line_is_open_source(chip, descs->ndescs))
4860 __clear_bit(descs->ndescs,
4861 array_info->set_mask);
4862 /* Identify 'fast' pins which require invertion */
4863 if (gpiod_is_active_low(desc))
4864 __set_bit(descs->ndescs,
4865 array_info->invert_mask);
4866 }
4867
Googleraf606d22022-10-26 21:40:12 -07004868 descs->ndescs++;
4869 }
Googler9398cc32022-12-02 17:21:52 +08004870 if (array_info)
4871 dev_dbg(dev,
4872 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4873 array_info->chip->label, array_info->size,
4874 *array_info->get_mask, *array_info->set_mask,
4875 *array_info->invert_mask);
Googleraf606d22022-10-26 21:40:12 -07004876 return descs;
4877}
4878EXPORT_SYMBOL_GPL(gpiod_get_array);
4879
4880/**
4881 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4882 * function
4883 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4884 * @con_id: function within the GPIO consumer
4885 * @flags: optional GPIO initialization flags
4886 *
4887 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4888 * assigned to the requested function it will return NULL.
4889 */
4890struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4891 const char *con_id,
4892 enum gpiod_flags flags)
4893{
4894 struct gpio_descs *descs;
4895
4896 descs = gpiod_get_array(dev, con_id, flags);
4897 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
4898 return NULL;
4899
4900 return descs;
4901}
4902EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4903
4904/**
4905 * gpiod_put - dispose of a GPIO descriptor
4906 * @desc: GPIO descriptor to dispose of
4907 *
4908 * No descriptor can be used after gpiod_put() has been called on it.
4909 */
4910void gpiod_put(struct gpio_desc *desc)
4911{
Googler9398cc32022-12-02 17:21:52 +08004912 if (desc)
4913 gpiod_free(desc);
Googleraf606d22022-10-26 21:40:12 -07004914}
4915EXPORT_SYMBOL_GPL(gpiod_put);
4916
4917/**
4918 * gpiod_put_array - dispose of multiple GPIO descriptors
4919 * @descs: struct gpio_descs containing an array of descriptors
4920 */
4921void gpiod_put_array(struct gpio_descs *descs)
4922{
4923 unsigned int i;
4924
4925 for (i = 0; i < descs->ndescs; i++)
4926 gpiod_put(descs->desc[i]);
4927
4928 kfree(descs);
4929}
4930EXPORT_SYMBOL_GPL(gpiod_put_array);
4931
4932static int __init gpiolib_dev_init(void)
4933{
4934 int ret;
4935
4936 /* Register GPIO sysfs bus */
Googler9398cc32022-12-02 17:21:52 +08004937 ret = bus_register(&gpio_bus_type);
Googleraf606d22022-10-26 21:40:12 -07004938 if (ret < 0) {
4939 pr_err("gpiolib: could not register GPIO bus type\n");
4940 return ret;
4941 }
4942
4943 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
4944 if (ret < 0) {
4945 pr_err("gpiolib: failed to allocate char dev region\n");
4946 bus_unregister(&gpio_bus_type);
4947 } else {
4948 gpiolib_initialized = true;
4949 gpiochip_setup_devs();
4950 }
4951 return ret;
4952}
4953core_initcall(gpiolib_dev_init);
4954
4955#ifdef CONFIG_DEBUG_FS
4956
4957static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
4958{
4959 unsigned i;
4960 struct gpio_chip *chip = gdev->chip;
4961 unsigned gpio = gdev->base;
4962 struct gpio_desc *gdesc = &gdev->descs[0];
Googler9398cc32022-12-02 17:21:52 +08004963 bool is_out;
4964 bool is_irq;
4965 bool active_low;
Googleraf606d22022-10-26 21:40:12 -07004966
4967 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
4968 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4969 if (gdesc->name) {
4970 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4971 gpio, gdesc->name);
4972 }
4973 continue;
4974 }
4975
4976 gpiod_get_direction(gdesc);
4977 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4978 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Googler9398cc32022-12-02 17:21:52 +08004979 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
4980 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
Googleraf606d22022-10-26 21:40:12 -07004981 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
4982 is_out ? "out" : "in ",
Googler9398cc32022-12-02 17:21:52 +08004983 chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
4984 is_irq ? "IRQ " : "",
4985 active_low ? "ACTIVE LOW" : "");
Googleraf606d22022-10-26 21:40:12 -07004986 seq_printf(s, "\n");
4987 }
4988}
4989
4990static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4991{
4992 unsigned long flags;
4993 struct gpio_device *gdev = NULL;
4994 loff_t index = *pos;
4995
4996 s->private = "";
4997
4998 spin_lock_irqsave(&gpio_lock, flags);
4999 list_for_each_entry(gdev, &gpio_devices, list)
5000 if (index-- == 0) {
5001 spin_unlock_irqrestore(&gpio_lock, flags);
5002 return gdev;
5003 }
5004 spin_unlock_irqrestore(&gpio_lock, flags);
5005
5006 return NULL;
5007}
5008
5009static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
5010{
5011 unsigned long flags;
5012 struct gpio_device *gdev = v;
5013 void *ret = NULL;
5014
5015 spin_lock_irqsave(&gpio_lock, flags);
5016 if (list_is_last(&gdev->list, &gpio_devices))
5017 ret = NULL;
5018 else
5019 ret = list_entry(gdev->list.next, struct gpio_device, list);
5020 spin_unlock_irqrestore(&gpio_lock, flags);
5021
5022 s->private = "\n";
5023 ++*pos;
5024
5025 return ret;
5026}
5027
5028static void gpiolib_seq_stop(struct seq_file *s, void *v)
5029{
5030}
5031
5032static int gpiolib_seq_show(struct seq_file *s, void *v)
5033{
5034 struct gpio_device *gdev = v;
5035 struct gpio_chip *chip = gdev->chip;
5036 struct device *parent;
5037
5038 if (!chip) {
5039 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
5040 dev_name(&gdev->dev));
5041 return 0;
5042 }
5043
5044 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
5045 dev_name(&gdev->dev),
5046 gdev->base, gdev->base + gdev->ngpio - 1);
5047 parent = chip->parent;
5048 if (parent)
5049 seq_printf(s, ", parent: %s/%s",
5050 parent->bus ? parent->bus->name : "no-bus",
5051 dev_name(parent));
5052 if (chip->label)
5053 seq_printf(s, ", %s", chip->label);
5054 if (chip->can_sleep)
5055 seq_printf(s, ", can sleep");
5056 seq_printf(s, ":\n");
5057
5058 if (chip->dbg_show)
5059 chip->dbg_show(s, chip);
5060 else
5061 gpiolib_dbg_show(s, gdev);
5062
5063 return 0;
5064}
5065
5066static const struct seq_operations gpiolib_seq_ops = {
5067 .start = gpiolib_seq_start,
5068 .next = gpiolib_seq_next,
5069 .stop = gpiolib_seq_stop,
5070 .show = gpiolib_seq_show,
5071};
5072
5073static int gpiolib_open(struct inode *inode, struct file *file)
5074{
5075 return seq_open(file, &gpiolib_seq_ops);
5076}
5077
5078static const struct file_operations gpiolib_operations = {
5079 .owner = THIS_MODULE,
5080 .open = gpiolib_open,
5081 .read = seq_read,
5082 .llseek = seq_lseek,
5083 .release = seq_release,
5084};
5085
5086static int __init gpiolib_debugfs_init(void)
5087{
5088 /* /sys/kernel/debug/gpio */
Googler9398cc32022-12-02 17:21:52 +08005089 debugfs_create_file("gpio", S_IFREG | S_IRUGO, NULL, NULL,
5090 &gpiolib_operations);
Googleraf606d22022-10-26 21:40:12 -07005091 return 0;
5092}
5093subsys_initcall(gpiolib_debugfs_init);
5094
5095#endif /* DEBUG_FS */