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