]>
Commit | Line | Data |
---|---|---|
d2876d08 DB |
1 | #include <linux/kernel.h> |
2 | #include <linux/module.h> | |
ff77c352 | 3 | #include <linux/interrupt.h> |
d2876d08 DB |
4 | #include <linux/irq.h> |
5 | #include <linux/spinlock.h> | |
1a989d0f | 6 | #include <linux/list.h> |
d8f388d8 DB |
7 | #include <linux/device.h> |
8 | #include <linux/err.h> | |
9 | #include <linux/debugfs.h> | |
10 | #include <linux/seq_file.h> | |
11 | #include <linux/gpio.h> | |
391c970c | 12 | #include <linux/of_gpio.h> |
ff77c352 | 13 | #include <linux/idr.h> |
5a0e3ad6 | 14 | #include <linux/slab.h> |
7b199811 | 15 | #include <linux/acpi.h> |
53e7cac3 | 16 | #include <linux/gpio/driver.h> |
0a6d3158 | 17 | #include <linux/gpio/machine.h> |
d2876d08 | 18 | |
664e3e5a MW |
19 | #include "gpiolib.h" |
20 | ||
3f397c21 UKK |
21 | #define CREATE_TRACE_POINTS |
22 | #include <trace/events/gpio.h> | |
d2876d08 | 23 | |
79a9becd | 24 | /* Implementation infrastructure for GPIO interfaces. |
d2876d08 | 25 | * |
79a9becd AC |
26 | * The GPIO programming interface allows for inlining speed-critical |
27 | * get/set operations for common cases, so that access to SOC-integrated | |
28 | * GPIOs can sometimes cost only an instruction or two per bit. | |
d2876d08 DB |
29 | */ |
30 | ||
31 | ||
32 | /* When debugging, extend minimal trust to callers and platform code. | |
33 | * Also emit diagnostic messages that may help initial bringup, when | |
34 | * board setup or driver bugs are most common. | |
35 | * | |
36 | * Otherwise, minimize overhead in what may be bitbanging codepaths. | |
37 | */ | |
38 | #ifdef DEBUG | |
39 | #define extra_checks 1 | |
40 | #else | |
41 | #define extra_checks 0 | |
42 | #endif | |
43 | ||
44 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. | |
45 | * While any GPIO is requested, its gpio_chip is not removable; | |
46 | * each GPIO's "requested" flag serves as a lock and refcount. | |
47 | */ | |
0eb4c6c2 | 48 | DEFINE_SPINLOCK(gpio_lock); |
d2876d08 | 49 | |
6c0b4e6c AC |
50 | #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio) |
51 | ||
bae48da2 AC |
52 | static DEFINE_MUTEX(gpio_lookup_lock); |
53 | static LIST_HEAD(gpio_lookup_list); | |
0eb4c6c2 | 54 | LIST_HEAD(gpio_chips); |
1a2a99c6 | 55 | |
6d86750c JH |
56 | |
57 | static void gpiochip_free_hogs(struct gpio_chip *chip); | |
58 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); | |
59 | ||
60 | ||
d2876d08 DB |
61 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
62 | { | |
d2876d08 | 63 | d->label = label; |
d2876d08 DB |
64 | } |
65 | ||
372e722e AC |
66 | /** |
67 | * Convert a GPIO number to its descriptor | |
68 | */ | |
79a9becd | 69 | struct gpio_desc *gpio_to_desc(unsigned gpio) |
372e722e | 70 | { |
14e85c0e AC |
71 | struct gpio_chip *chip; |
72 | unsigned long flags; | |
73 | ||
74 | spin_lock_irqsave(&gpio_lock, flags); | |
75 | ||
76 | list_for_each_entry(chip, &gpio_chips, list) { | |
77 | if (chip->base <= gpio && chip->base + chip->ngpio > gpio) { | |
78 | spin_unlock_irqrestore(&gpio_lock, flags); | |
79 | return &chip->desc[gpio - chip->base]; | |
80 | } | |
81 | } | |
82 | ||
83 | spin_unlock_irqrestore(&gpio_lock, flags); | |
84 | ||
0e9a5edf AC |
85 | if (!gpio_is_valid(gpio)) |
86 | WARN(1, "invalid GPIO %d\n", gpio); | |
87 | ||
14e85c0e | 88 | return NULL; |
372e722e | 89 | } |
79a9becd | 90 | EXPORT_SYMBOL_GPL(gpio_to_desc); |
372e722e | 91 | |
c0017ed7 MP |
92 | /** |
93 | * Convert a GPIO name to its descriptor | |
94 | */ | |
95 | struct gpio_desc *gpio_name_to_desc(const char * const name) | |
96 | { | |
97 | struct gpio_chip *chip; | |
98 | unsigned long flags; | |
99 | ||
100 | spin_lock_irqsave(&gpio_lock, flags); | |
101 | ||
102 | list_for_each_entry(chip, &gpio_chips, list) { | |
103 | int i; | |
104 | ||
105 | for (i = 0; i != chip->ngpio; ++i) { | |
106 | struct gpio_desc *gpio = &chip->desc[i]; | |
107 | ||
108 | if (!gpio->name) | |
109 | continue; | |
110 | ||
111 | if (!strcmp(gpio->name, name)) { | |
112 | spin_unlock_irqrestore(&gpio_lock, flags); | |
113 | return gpio; | |
114 | } | |
115 | } | |
116 | } | |
117 | ||
118 | spin_unlock_irqrestore(&gpio_lock, flags); | |
119 | ||
120 | return NULL; | |
121 | } | |
122 | EXPORT_SYMBOL_GPL(gpio_name_to_desc); | |
123 | ||
d468bf9e | 124 | /** |
bb1e88cc | 125 | * Get the GPIO descriptor corresponding to the given hw number for this chip. |
d468bf9e | 126 | */ |
bb1e88cc AC |
127 | struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, |
128 | u16 hwnum) | |
d468bf9e | 129 | { |
bb1e88cc | 130 | if (hwnum >= chip->ngpio) |
b7d0a28a | 131 | return ERR_PTR(-EINVAL); |
d468bf9e | 132 | |
bb1e88cc | 133 | return &chip->desc[hwnum]; |
d468bf9e | 134 | } |
372e722e AC |
135 | |
136 | /** | |
137 | * Convert a GPIO descriptor to the integer namespace. | |
138 | * This should disappear in the future but is needed since we still | |
139 | * use GPIO numbers for error messages and sysfs nodes | |
140 | */ | |
79a9becd | 141 | int desc_to_gpio(const struct gpio_desc *desc) |
372e722e | 142 | { |
14e85c0e | 143 | return desc->chip->base + (desc - &desc->chip->desc[0]); |
372e722e | 144 | } |
79a9becd | 145 | EXPORT_SYMBOL_GPL(desc_to_gpio); |
372e722e AC |
146 | |
147 | ||
79a9becd AC |
148 | /** |
149 | * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs | |
150 | * @desc: descriptor to return the chip of | |
151 | */ | |
152 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) | |
372e722e | 153 | { |
bcabdef1 | 154 | return desc ? desc->chip : NULL; |
372e722e | 155 | } |
79a9becd | 156 | EXPORT_SYMBOL_GPL(gpiod_to_chip); |
d2876d08 | 157 | |
8d0aab2f AV |
158 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
159 | static int gpiochip_find_base(int ngpio) | |
160 | { | |
83cabe33 AC |
161 | struct gpio_chip *chip; |
162 | int base = ARCH_NR_GPIOS - ngpio; | |
8d0aab2f | 163 | |
83cabe33 AC |
164 | list_for_each_entry_reverse(chip, &gpio_chips, list) { |
165 | /* found a free space? */ | |
166 | if (chip->base + chip->ngpio <= base) | |
167 | break; | |
168 | else | |
169 | /* nope, check the space right before the chip */ | |
170 | base = chip->base - ngpio; | |
8d0aab2f AV |
171 | } |
172 | ||
83cabe33 | 173 | if (gpio_is_valid(base)) { |
8d0aab2f | 174 | pr_debug("%s: found new base at %d\n", __func__, base); |
83cabe33 AC |
175 | return base; |
176 | } else { | |
177 | pr_err("%s: cannot find free range\n", __func__); | |
178 | return -ENOSPC; | |
169b6a7a | 179 | } |
169b6a7a AV |
180 | } |
181 | ||
79a9becd AC |
182 | /** |
183 | * gpiod_get_direction - return the current direction of a GPIO | |
184 | * @desc: GPIO to get the direction of | |
185 | * | |
186 | * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error. | |
187 | * | |
188 | * This function may sleep if gpiod_cansleep() is true. | |
189 | */ | |
8e53b0f1 | 190 | int gpiod_get_direction(struct gpio_desc *desc) |
80b0a602 MN |
191 | { |
192 | struct gpio_chip *chip; | |
372e722e | 193 | unsigned offset; |
80b0a602 MN |
194 | int status = -EINVAL; |
195 | ||
372e722e AC |
196 | chip = gpiod_to_chip(desc); |
197 | offset = gpio_chip_hwgpio(desc); | |
80b0a602 MN |
198 | |
199 | if (!chip->get_direction) | |
200 | return status; | |
201 | ||
372e722e | 202 | status = chip->get_direction(chip, offset); |
80b0a602 MN |
203 | if (status > 0) { |
204 | /* GPIOF_DIR_IN, or other positive */ | |
205 | status = 1; | |
8e53b0f1 | 206 | clear_bit(FLAG_IS_OUT, &desc->flags); |
80b0a602 MN |
207 | } |
208 | if (status == 0) { | |
209 | /* GPIOF_DIR_OUT */ | |
8e53b0f1 | 210 | set_bit(FLAG_IS_OUT, &desc->flags); |
80b0a602 MN |
211 | } |
212 | return status; | |
213 | } | |
79a9becd | 214 | EXPORT_SYMBOL_GPL(gpiod_get_direction); |
80b0a602 | 215 | |
1a989d0f AC |
216 | /* |
217 | * Add a new chip to the global chips list, keeping the list of chips sorted | |
218 | * by base order. | |
219 | * | |
220 | * Return -EBUSY if the new chip overlaps with some other chip's integer | |
221 | * space. | |
222 | */ | |
223 | static int gpiochip_add_to_list(struct gpio_chip *chip) | |
224 | { | |
d1aceb80 | 225 | struct list_head *pos; |
1a989d0f AC |
226 | struct gpio_chip *_chip; |
227 | int err = 0; | |
228 | ||
229 | /* find where to insert our chip */ | |
230 | list_for_each(pos, &gpio_chips) { | |
231 | _chip = list_entry(pos, struct gpio_chip, list); | |
232 | /* shall we insert before _chip? */ | |
233 | if (_chip->base >= chip->base + chip->ngpio) | |
234 | break; | |
235 | } | |
236 | ||
237 | /* are we stepping on the chip right before? */ | |
238 | if (pos != &gpio_chips && pos->prev != &gpio_chips) { | |
239 | _chip = list_entry(pos->prev, struct gpio_chip, list); | |
240 | if (_chip->base + _chip->ngpio > chip->base) { | |
241 | dev_err(chip->dev, | |
242 | "GPIO integer space overlap, cannot add chip\n"); | |
243 | err = -EBUSY; | |
244 | } | |
245 | } | |
246 | ||
247 | if (!err) | |
248 | list_add_tail(&chip->list, pos); | |
249 | ||
250 | return err; | |
251 | } | |
252 | ||
d2876d08 DB |
253 | /** |
254 | * gpiochip_add() - register a gpio_chip | |
255 | * @chip: the chip to register, with chip->base initialized | |
14e85c0e | 256 | * Context: potentially before irqs will work |
d2876d08 DB |
257 | * |
258 | * Returns a negative errno if the chip can't be registered, such as | |
259 | * because the chip->base is invalid or already associated with a | |
260 | * different chip. Otherwise it returns zero as a success code. | |
8d0aab2f | 261 | * |
d8f388d8 DB |
262 | * When gpiochip_add() is called very early during boot, so that GPIOs |
263 | * can be freely used, the chip->dev device must be registered before | |
264 | * the gpio framework's arch_initcall(). Otherwise sysfs initialization | |
265 | * for GPIOs will fail rudely. | |
266 | * | |
8d0aab2f AV |
267 | * If chip->base is negative, this requests dynamic assignment of |
268 | * a range of valid GPIOs. | |
d2876d08 DB |
269 | */ |
270 | int gpiochip_add(struct gpio_chip *chip) | |
271 | { | |
272 | unsigned long flags; | |
273 | int status = 0; | |
274 | unsigned id; | |
8d0aab2f | 275 | int base = chip->base; |
14e85c0e | 276 | struct gpio_desc *descs; |
d2876d08 | 277 | |
14e85c0e AC |
278 | descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL); |
279 | if (!descs) | |
280 | return -ENOMEM; | |
d2876d08 DB |
281 | |
282 | spin_lock_irqsave(&gpio_lock, flags); | |
283 | ||
8d0aab2f AV |
284 | if (base < 0) { |
285 | base = gpiochip_find_base(chip->ngpio); | |
286 | if (base < 0) { | |
287 | status = base; | |
225fce83 JH |
288 | spin_unlock_irqrestore(&gpio_lock, flags); |
289 | goto err_free_descs; | |
8d0aab2f AV |
290 | } |
291 | chip->base = base; | |
292 | } | |
293 | ||
1a989d0f | 294 | status = gpiochip_add_to_list(chip); |
05aa5203 JH |
295 | if (status) { |
296 | spin_unlock_irqrestore(&gpio_lock, flags); | |
297 | goto err_free_descs; | |
298 | } | |
1a989d0f | 299 | |
05aa5203 JH |
300 | for (id = 0; id < chip->ngpio; id++) { |
301 | struct gpio_desc *desc = &descs[id]; | |
302 | ||
303 | desc->chip = chip; | |
304 | ||
305 | /* REVISIT: most hardware initializes GPIOs as inputs (often | |
306 | * with pullups enabled) so power usage is minimized. Linux | |
307 | * code should set the gpio direction first thing; but until | |
308 | * it does, and in case chip->get_direction is not set, we may | |
309 | * expose the wrong direction in sysfs. | |
310 | */ | |
311 | desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0; | |
d2876d08 DB |
312 | } |
313 | ||
14e85c0e AC |
314 | chip->desc = descs; |
315 | ||
3bae4811 ZG |
316 | spin_unlock_irqrestore(&gpio_lock, flags); |
317 | ||
f23f1516 SH |
318 | #ifdef CONFIG_PINCTRL |
319 | INIT_LIST_HEAD(&chip->pin_ranges); | |
320 | #endif | |
321 | ||
3726960e GS |
322 | if (!chip->owner && chip->dev && chip->dev->driver) |
323 | chip->owner = chip->dev->driver->owner; | |
324 | ||
28355f81 TV |
325 | status = of_gpiochip_add(chip); |
326 | if (status) | |
327 | goto err_remove_chip; | |
328 | ||
664e3e5a | 329 | acpi_gpiochip_add(chip); |
391c970c | 330 | |
426577bd | 331 | status = gpiochip_sysfs_register(chip); |
225fce83 JH |
332 | if (status) |
333 | goto err_remove_chip; | |
cedb1881 | 334 | |
7589e59f | 335 | pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__, |
64842aad GL |
336 | chip->base, chip->base + chip->ngpio - 1, |
337 | chip->label ? : "generic"); | |
338 | ||
cedb1881 | 339 | return 0; |
3bae4811 | 340 | |
225fce83 JH |
341 | err_remove_chip: |
342 | acpi_gpiochip_remove(chip); | |
6d86750c | 343 | gpiochip_free_hogs(chip); |
225fce83 JH |
344 | of_gpiochip_remove(chip); |
345 | spin_lock_irqsave(&gpio_lock, flags); | |
346 | list_del(&chip->list); | |
3bae4811 | 347 | spin_unlock_irqrestore(&gpio_lock, flags); |
05aa5203 | 348 | chip->desc = NULL; |
225fce83 | 349 | err_free_descs: |
14e85c0e | 350 | kfree(descs); |
14e85c0e | 351 | |
d2876d08 | 352 | /* failures here can mean systems won't boot... */ |
7589e59f | 353 | pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__, |
cedb1881 AV |
354 | chip->base, chip->base + chip->ngpio - 1, |
355 | chip->label ? : "generic"); | |
d2876d08 DB |
356 | return status; |
357 | } | |
358 | EXPORT_SYMBOL_GPL(gpiochip_add); | |
359 | ||
360 | /** | |
361 | * gpiochip_remove() - unregister a gpio_chip | |
362 | * @chip: the chip to unregister | |
363 | * | |
364 | * A gpio_chip with any GPIOs still requested may not be removed. | |
365 | */ | |
e1db1706 | 366 | void gpiochip_remove(struct gpio_chip *chip) |
d2876d08 | 367 | { |
fab28b89 | 368 | struct gpio_desc *desc; |
d2876d08 | 369 | unsigned long flags; |
d2876d08 | 370 | unsigned id; |
fab28b89 | 371 | bool requested = false; |
d2876d08 | 372 | |
426577bd | 373 | gpiochip_sysfs_unregister(chip); |
01cca93a | 374 | |
00acc3dc JH |
375 | gpiochip_irqchip_remove(chip); |
376 | ||
6072b9dc | 377 | acpi_gpiochip_remove(chip); |
9ef0d6f7 | 378 | gpiochip_remove_pin_ranges(chip); |
f625d460 | 379 | gpiochip_free_hogs(chip); |
391c970c AV |
380 | of_gpiochip_remove(chip); |
381 | ||
6798acaa | 382 | spin_lock_irqsave(&gpio_lock, flags); |
6c0b4e6c | 383 | for (id = 0; id < chip->ngpio; id++) { |
fab28b89 JH |
384 | desc = &chip->desc[id]; |
385 | desc->chip = NULL; | |
386 | if (test_bit(FLAG_REQUESTED, &desc->flags)) | |
387 | requested = true; | |
d2876d08 | 388 | } |
e1db1706 | 389 | list_del(&chip->list); |
d2876d08 | 390 | spin_unlock_irqrestore(&gpio_lock, flags); |
14e85c0e | 391 | |
fab28b89 JH |
392 | if (requested) |
393 | dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); | |
394 | ||
14e85c0e AC |
395 | kfree(chip->desc); |
396 | chip->desc = NULL; | |
d2876d08 DB |
397 | } |
398 | EXPORT_SYMBOL_GPL(gpiochip_remove); | |
399 | ||
594fa265 GL |
400 | /** |
401 | * gpiochip_find() - iterator for locating a specific gpio_chip | |
402 | * @data: data to pass to match function | |
403 | * @callback: Callback function to check gpio_chip | |
404 | * | |
405 | * Similar to bus_find_device. It returns a reference to a gpio_chip as | |
406 | * determined by a user supplied @match callback. The callback should return | |
407 | * 0 if the device doesn't match and non-zero if it does. If the callback is | |
408 | * non-zero, this function will return to the caller and not iterate over any | |
409 | * more gpio_chips. | |
410 | */ | |
07ce8ec7 | 411 | struct gpio_chip *gpiochip_find(void *data, |
6e2cf651 | 412 | int (*match)(struct gpio_chip *chip, |
3d0f7cf0 | 413 | void *data)) |
594fa265 | 414 | { |
125eef96 | 415 | struct gpio_chip *chip; |
594fa265 | 416 | unsigned long flags; |
594fa265 GL |
417 | |
418 | spin_lock_irqsave(&gpio_lock, flags); | |
125eef96 AC |
419 | list_for_each_entry(chip, &gpio_chips, list) |
420 | if (match(chip, data)) | |
594fa265 | 421 | break; |
125eef96 AC |
422 | |
423 | /* No match? */ | |
424 | if (&chip->list == &gpio_chips) | |
425 | chip = NULL; | |
594fa265 GL |
426 | spin_unlock_irqrestore(&gpio_lock, flags); |
427 | ||
428 | return chip; | |
429 | } | |
8fa0c9bf | 430 | EXPORT_SYMBOL_GPL(gpiochip_find); |
d2876d08 | 431 | |
79697ef9 AC |
432 | static int gpiochip_match_name(struct gpio_chip *chip, void *data) |
433 | { | |
434 | const char *name = data; | |
435 | ||
436 | return !strcmp(chip->label, name); | |
437 | } | |
438 | ||
439 | static struct gpio_chip *find_chip_by_name(const char *name) | |
440 | { | |
441 | return gpiochip_find((void *)name, gpiochip_match_name); | |
442 | } | |
443 | ||
14250520 LW |
444 | #ifdef CONFIG_GPIOLIB_IRQCHIP |
445 | ||
446 | /* | |
447 | * The following is irqchip helper code for gpiochips. | |
448 | */ | |
449 | ||
450 | /** | |
3f97d5fc LW |
451 | * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip |
452 | * @gpiochip: the gpiochip to set the irqchip chain to | |
453 | * @irqchip: the irqchip to chain to the gpiochip | |
14250520 LW |
454 | * @parent_irq: the irq number corresponding to the parent IRQ for this |
455 | * chained irqchip | |
456 | * @parent_handler: the parent interrupt handler for the accumulated IRQ | |
3f97d5fc LW |
457 | * coming out of the gpiochip. If the interrupt is nested rather than |
458 | * cascaded, pass NULL in this handler argument | |
14250520 LW |
459 | */ |
460 | void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip, | |
461 | struct irq_chip *irqchip, | |
462 | int parent_irq, | |
463 | irq_flow_handler_t parent_handler) | |
464 | { | |
83141a77 LW |
465 | unsigned int offset; |
466 | ||
83141a77 LW |
467 | if (!gpiochip->irqdomain) { |
468 | chip_err(gpiochip, "called %s before setting up irqchip\n", | |
469 | __func__); | |
1c8732bb LW |
470 | return; |
471 | } | |
472 | ||
3f97d5fc LW |
473 | if (parent_handler) { |
474 | if (gpiochip->can_sleep) { | |
475 | chip_err(gpiochip, | |
476 | "you cannot have chained interrupts on a " | |
477 | "chip that may sleep\n"); | |
478 | return; | |
479 | } | |
3f97d5fc LW |
480 | /* |
481 | * The parent irqchip is already using the chip_data for this | |
482 | * irqchip, so our callbacks simply use the handler_data. | |
483 | */ | |
f7f87753 TG |
484 | irq_set_chained_handler_and_data(parent_irq, parent_handler, |
485 | gpiochip); | |
25e4fe92 DES |
486 | |
487 | gpiochip->irq_parent = parent_irq; | |
3f97d5fc | 488 | } |
83141a77 LW |
489 | |
490 | /* Set the parent IRQ for all affected IRQs */ | |
491 | for (offset = 0; offset < gpiochip->ngpio; offset++) | |
492 | irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset), | |
493 | parent_irq); | |
14250520 LW |
494 | } |
495 | EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip); | |
496 | ||
497 | /** | |
498 | * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip | |
499 | * @d: the irqdomain used by this irqchip | |
500 | * @irq: the global irq number used by this GPIO irqchip irq | |
501 | * @hwirq: the local IRQ/GPIO line offset on this gpiochip | |
502 | * | |
503 | * This function will set up the mapping for a certain IRQ line on a | |
504 | * gpiochip by assigning the gpiochip as chip data, and using the irqchip | |
505 | * stored inside the gpiochip. | |
506 | */ | |
507 | static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, | |
508 | irq_hw_number_t hwirq) | |
509 | { | |
510 | struct gpio_chip *chip = d->host_data; | |
511 | ||
14250520 | 512 | irq_set_chip_data(irq, chip); |
a0a8bcf4 GS |
513 | /* |
514 | * This lock class tells lockdep that GPIO irqs are in a different | |
515 | * category than their parents, so it won't report false recursion. | |
516 | */ | |
517 | irq_set_lockdep_class(irq, chip->lock_key); | |
7633fb95 | 518 | irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler); |
1c8732bb | 519 | /* Chips that can sleep need nested thread handlers */ |
295494af | 520 | if (chip->can_sleep && !chip->irq_not_threaded) |
1c8732bb | 521 | irq_set_nested_thread(irq, 1); |
14250520 | 522 | irq_set_noprobe(irq); |
23393d49 | 523 | |
1333b90f LW |
524 | /* |
525 | * No set-up of the hardware will happen if IRQ_TYPE_NONE | |
526 | * is passed as default type. | |
527 | */ | |
528 | if (chip->irq_default_type != IRQ_TYPE_NONE) | |
529 | irq_set_irq_type(irq, chip->irq_default_type); | |
14250520 LW |
530 | |
531 | return 0; | |
532 | } | |
533 | ||
c3626fde LW |
534 | static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq) |
535 | { | |
1c8732bb LW |
536 | struct gpio_chip *chip = d->host_data; |
537 | ||
1c8732bb LW |
538 | if (chip->can_sleep) |
539 | irq_set_nested_thread(irq, 0); | |
c3626fde LW |
540 | irq_set_chip_and_handler(irq, NULL, NULL); |
541 | irq_set_chip_data(irq, NULL); | |
542 | } | |
543 | ||
14250520 LW |
544 | static const struct irq_domain_ops gpiochip_domain_ops = { |
545 | .map = gpiochip_irq_map, | |
c3626fde | 546 | .unmap = gpiochip_irq_unmap, |
14250520 LW |
547 | /* Virtually all GPIO irqchips are twocell:ed */ |
548 | .xlate = irq_domain_xlate_twocell, | |
549 | }; | |
550 | ||
551 | static int gpiochip_irq_reqres(struct irq_data *d) | |
552 | { | |
553 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); | |
554 | ||
5b76e79c GS |
555 | if (!try_module_get(chip->owner)) |
556 | return -ENODEV; | |
557 | ||
e3a2e878 | 558 | if (gpiochip_lock_as_irq(chip, d->hwirq)) { |
14250520 LW |
559 | chip_err(chip, |
560 | "unable to lock HW IRQ %lu for IRQ\n", | |
561 | d->hwirq); | |
5b76e79c | 562 | module_put(chip->owner); |
14250520 LW |
563 | return -EINVAL; |
564 | } | |
565 | return 0; | |
566 | } | |
567 | ||
568 | static void gpiochip_irq_relres(struct irq_data *d) | |
569 | { | |
570 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); | |
571 | ||
e3a2e878 | 572 | gpiochip_unlock_as_irq(chip, d->hwirq); |
5b76e79c | 573 | module_put(chip->owner); |
14250520 LW |
574 | } |
575 | ||
576 | static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset) | |
577 | { | |
578 | return irq_find_mapping(chip->irqdomain, offset); | |
579 | } | |
580 | ||
581 | /** | |
582 | * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip | |
583 | * @gpiochip: the gpiochip to remove the irqchip from | |
584 | * | |
585 | * This is called only from gpiochip_remove() | |
586 | */ | |
587 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) | |
588 | { | |
c3626fde LW |
589 | unsigned int offset; |
590 | ||
afa82fab MW |
591 | acpi_gpiochip_free_interrupts(gpiochip); |
592 | ||
25e4fe92 DES |
593 | if (gpiochip->irq_parent) { |
594 | irq_set_chained_handler(gpiochip->irq_parent, NULL); | |
595 | irq_set_handler_data(gpiochip->irq_parent, NULL); | |
596 | } | |
597 | ||
c3626fde LW |
598 | /* Remove all IRQ mappings and delete the domain */ |
599 | if (gpiochip->irqdomain) { | |
600 | for (offset = 0; offset < gpiochip->ngpio; offset++) | |
e3893386 GS |
601 | irq_dispose_mapping( |
602 | irq_find_mapping(gpiochip->irqdomain, offset)); | |
14250520 | 603 | irq_domain_remove(gpiochip->irqdomain); |
c3626fde | 604 | } |
14250520 LW |
605 | |
606 | if (gpiochip->irqchip) { | |
607 | gpiochip->irqchip->irq_request_resources = NULL; | |
608 | gpiochip->irqchip->irq_release_resources = NULL; | |
609 | gpiochip->irqchip = NULL; | |
610 | } | |
611 | } | |
612 | ||
613 | /** | |
614 | * gpiochip_irqchip_add() - adds an irqchip to a gpiochip | |
615 | * @gpiochip: the gpiochip to add the irqchip to | |
616 | * @irqchip: the irqchip to add to the gpiochip | |
617 | * @first_irq: if not dynamically assigned, the base (first) IRQ to | |
618 | * allocate gpiochip irqs from | |
619 | * @handler: the irq handler to use (often a predefined irq core function) | |
1333b90f LW |
620 | * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE |
621 | * to have the core avoid setting up any default type in the hardware. | |
a0a8bcf4 | 622 | * @lock_key: lockdep class |
14250520 LW |
623 | * |
624 | * This function closely associates a certain irqchip with a certain | |
625 | * gpiochip, providing an irq domain to translate the local IRQs to | |
626 | * global irqs in the gpiolib core, and making sure that the gpiochip | |
627 | * is passed as chip data to all related functions. Driver callbacks | |
628 | * need to use container_of() to get their local state containers back | |
629 | * from the gpiochip passed as chip data. An irqdomain will be stored | |
630 | * in the gpiochip that shall be used by the driver to handle IRQ number | |
631 | * translation. The gpiochip will need to be initialized and registered | |
632 | * before calling this function. | |
633 | * | |
c3626fde LW |
634 | * This function will handle two cell:ed simple IRQs and assumes all |
635 | * the pins on the gpiochip can generate a unique IRQ. Everything else | |
14250520 LW |
636 | * need to be open coded. |
637 | */ | |
a0a8bcf4 GS |
638 | int _gpiochip_irqchip_add(struct gpio_chip *gpiochip, |
639 | struct irq_chip *irqchip, | |
640 | unsigned int first_irq, | |
641 | irq_flow_handler_t handler, | |
642 | unsigned int type, | |
643 | struct lock_class_key *lock_key) | |
14250520 LW |
644 | { |
645 | struct device_node *of_node; | |
646 | unsigned int offset; | |
c3626fde | 647 | unsigned irq_base = 0; |
14250520 LW |
648 | |
649 | if (!gpiochip || !irqchip) | |
650 | return -EINVAL; | |
651 | ||
652 | if (!gpiochip->dev) { | |
653 | pr_err("missing gpiochip .dev parent pointer\n"); | |
654 | return -EINVAL; | |
655 | } | |
656 | of_node = gpiochip->dev->of_node; | |
657 | #ifdef CONFIG_OF_GPIO | |
658 | /* | |
20a8a968 | 659 | * If the gpiochip has an assigned OF node this takes precedence |
14250520 LW |
660 | * FIXME: get rid of this and use gpiochip->dev->of_node everywhere |
661 | */ | |
662 | if (gpiochip->of_node) | |
663 | of_node = gpiochip->of_node; | |
664 | #endif | |
665 | gpiochip->irqchip = irqchip; | |
666 | gpiochip->irq_handler = handler; | |
667 | gpiochip->irq_default_type = type; | |
668 | gpiochip->to_irq = gpiochip_to_irq; | |
a0a8bcf4 | 669 | gpiochip->lock_key = lock_key; |
14250520 LW |
670 | gpiochip->irqdomain = irq_domain_add_simple(of_node, |
671 | gpiochip->ngpio, first_irq, | |
672 | &gpiochip_domain_ops, gpiochip); | |
673 | if (!gpiochip->irqdomain) { | |
674 | gpiochip->irqchip = NULL; | |
675 | return -EINVAL; | |
676 | } | |
8b67a1f0 RV |
677 | |
678 | /* | |
679 | * It is possible for a driver to override this, but only if the | |
680 | * alternative functions are both implemented. | |
681 | */ | |
682 | if (!irqchip->irq_request_resources && | |
683 | !irqchip->irq_release_resources) { | |
684 | irqchip->irq_request_resources = gpiochip_irq_reqres; | |
685 | irqchip->irq_release_resources = gpiochip_irq_relres; | |
686 | } | |
14250520 LW |
687 | |
688 | /* | |
689 | * Prepare the mapping since the irqchip shall be orthogonal to | |
690 | * any gpiochip calls. If the first_irq was zero, this is | |
691 | * necessary to allocate descriptors for all IRQs. | |
692 | */ | |
c3626fde LW |
693 | for (offset = 0; offset < gpiochip->ngpio; offset++) { |
694 | irq_base = irq_create_mapping(gpiochip->irqdomain, offset); | |
695 | if (offset == 0) | |
696 | /* | |
697 | * Store the base into the gpiochip to be used when | |
698 | * unmapping the irqs. | |
699 | */ | |
700 | gpiochip->irq_base = irq_base; | |
701 | } | |
14250520 | 702 | |
afa82fab MW |
703 | acpi_gpiochip_request_interrupts(gpiochip); |
704 | ||
14250520 LW |
705 | return 0; |
706 | } | |
a0a8bcf4 | 707 | EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add); |
14250520 LW |
708 | |
709 | #else /* CONFIG_GPIOLIB_IRQCHIP */ | |
710 | ||
711 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {} | |
712 | ||
713 | #endif /* CONFIG_GPIOLIB_IRQCHIP */ | |
714 | ||
f23f1516 | 715 | #ifdef CONFIG_PINCTRL |
165adc9c | 716 | |
586a87e6 CR |
717 | /** |
718 | * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping | |
719 | * @chip: the gpiochip to add the range for | |
d32651f6 | 720 | * @pctldev: the pin controller to map to |
586a87e6 CR |
721 | * @gpio_offset: the start offset in the current gpio_chip number space |
722 | * @pin_group: name of the pin group inside the pin controller | |
723 | */ | |
724 | int gpiochip_add_pingroup_range(struct gpio_chip *chip, | |
725 | struct pinctrl_dev *pctldev, | |
726 | unsigned int gpio_offset, const char *pin_group) | |
727 | { | |
728 | struct gpio_pin_range *pin_range; | |
729 | int ret; | |
730 | ||
731 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); | |
732 | if (!pin_range) { | |
1a2a99c6 | 733 | chip_err(chip, "failed to allocate pin ranges\n"); |
586a87e6 CR |
734 | return -ENOMEM; |
735 | } | |
736 | ||
737 | /* Use local offset as range ID */ | |
738 | pin_range->range.id = gpio_offset; | |
739 | pin_range->range.gc = chip; | |
740 | pin_range->range.name = chip->label; | |
741 | pin_range->range.base = chip->base + gpio_offset; | |
742 | pin_range->pctldev = pctldev; | |
743 | ||
744 | ret = pinctrl_get_group_pins(pctldev, pin_group, | |
745 | &pin_range->range.pins, | |
746 | &pin_range->range.npins); | |
61c6375d MN |
747 | if (ret < 0) { |
748 | kfree(pin_range); | |
586a87e6 | 749 | return ret; |
61c6375d | 750 | } |
586a87e6 CR |
751 | |
752 | pinctrl_add_gpio_range(pctldev, &pin_range->range); | |
753 | ||
1a2a99c6 AS |
754 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n", |
755 | gpio_offset, gpio_offset + pin_range->range.npins - 1, | |
586a87e6 CR |
756 | pinctrl_dev_get_devname(pctldev), pin_group); |
757 | ||
758 | list_add_tail(&pin_range->node, &chip->pin_ranges); | |
759 | ||
760 | return 0; | |
761 | } | |
762 | EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range); | |
763 | ||
3f0f8670 LW |
764 | /** |
765 | * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping | |
766 | * @chip: the gpiochip to add the range for | |
767 | * @pinctrl_name: the dev_name() of the pin controller to map to | |
316511c0 LW |
768 | * @gpio_offset: the start offset in the current gpio_chip number space |
769 | * @pin_offset: the start offset in the pin controller number space | |
3f0f8670 LW |
770 | * @npins: the number of pins from the offset of each pin space (GPIO and |
771 | * pin controller) to accumulate in this range | |
772 | */ | |
1e63d7b9 | 773 | int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
316511c0 | 774 | unsigned int gpio_offset, unsigned int pin_offset, |
3f0f8670 | 775 | unsigned int npins) |
f23f1516 SH |
776 | { |
777 | struct gpio_pin_range *pin_range; | |
b4d4b1f0 | 778 | int ret; |
f23f1516 | 779 | |
3f0f8670 | 780 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
f23f1516 | 781 | if (!pin_range) { |
1a2a99c6 | 782 | chip_err(chip, "failed to allocate pin ranges\n"); |
1e63d7b9 | 783 | return -ENOMEM; |
f23f1516 SH |
784 | } |
785 | ||
3f0f8670 | 786 | /* Use local offset as range ID */ |
316511c0 | 787 | pin_range->range.id = gpio_offset; |
3f0f8670 | 788 | pin_range->range.gc = chip; |
f23f1516 | 789 | pin_range->range.name = chip->label; |
316511c0 LW |
790 | pin_range->range.base = chip->base + gpio_offset; |
791 | pin_range->range.pin_base = pin_offset; | |
f23f1516 | 792 | pin_range->range.npins = npins; |
192c369c | 793 | pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, |
f23f1516 | 794 | &pin_range->range); |
8f23ca1a | 795 | if (IS_ERR(pin_range->pctldev)) { |
b4d4b1f0 | 796 | ret = PTR_ERR(pin_range->pctldev); |
1a2a99c6 | 797 | chip_err(chip, "could not create pin range\n"); |
3f0f8670 | 798 | kfree(pin_range); |
b4d4b1f0 | 799 | return ret; |
3f0f8670 | 800 | } |
1a2a99c6 AS |
801 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n", |
802 | gpio_offset, gpio_offset + npins - 1, | |
316511c0 LW |
803 | pinctl_name, |
804 | pin_offset, pin_offset + npins - 1); | |
f23f1516 SH |
805 | |
806 | list_add_tail(&pin_range->node, &chip->pin_ranges); | |
1e63d7b9 LW |
807 | |
808 | return 0; | |
f23f1516 | 809 | } |
165adc9c | 810 | EXPORT_SYMBOL_GPL(gpiochip_add_pin_range); |
f23f1516 | 811 | |
3f0f8670 LW |
812 | /** |
813 | * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings | |
814 | * @chip: the chip to remove all the mappings for | |
815 | */ | |
f23f1516 SH |
816 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
817 | { | |
818 | struct gpio_pin_range *pin_range, *tmp; | |
819 | ||
820 | list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) { | |
821 | list_del(&pin_range->node); | |
822 | pinctrl_remove_gpio_range(pin_range->pctldev, | |
823 | &pin_range->range); | |
3f0f8670 | 824 | kfree(pin_range); |
f23f1516 SH |
825 | } |
826 | } | |
165adc9c LW |
827 | EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); |
828 | ||
829 | #endif /* CONFIG_PINCTRL */ | |
f23f1516 | 830 | |
d2876d08 DB |
831 | /* These "optional" allocation calls help prevent drivers from stomping |
832 | * on each other, and help provide better diagnostics in debugfs. | |
833 | * They're called even less than the "set direction" calls. | |
834 | */ | |
77c2d792 | 835 | static int __gpiod_request(struct gpio_desc *desc, const char *label) |
d2876d08 | 836 | { |
77c2d792 MW |
837 | struct gpio_chip *chip = desc->chip; |
838 | int status; | |
d2876d08 DB |
839 | unsigned long flags; |
840 | ||
bcabdef1 AC |
841 | spin_lock_irqsave(&gpio_lock, flags); |
842 | ||
d2876d08 | 843 | /* NOTE: gpio_request() can be called in early boot, |
35e8bb51 | 844 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
d2876d08 DB |
845 | */ |
846 | ||
847 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { | |
848 | desc_set_label(desc, label ? : "?"); | |
849 | status = 0; | |
438d8908 | 850 | } else { |
d2876d08 | 851 | status = -EBUSY; |
7460db56 | 852 | goto done; |
35e8bb51 DB |
853 | } |
854 | ||
855 | if (chip->request) { | |
856 | /* chip->request may sleep */ | |
857 | spin_unlock_irqrestore(&gpio_lock, flags); | |
372e722e | 858 | status = chip->request(chip, gpio_chip_hwgpio(desc)); |
35e8bb51 DB |
859 | spin_lock_irqsave(&gpio_lock, flags); |
860 | ||
861 | if (status < 0) { | |
862 | desc_set_label(desc, NULL); | |
35e8bb51 | 863 | clear_bit(FLAG_REQUESTED, &desc->flags); |
80b0a602 | 864 | goto done; |
35e8bb51 | 865 | } |
438d8908 | 866 | } |
80b0a602 MN |
867 | if (chip->get_direction) { |
868 | /* chip->get_direction may sleep */ | |
869 | spin_unlock_irqrestore(&gpio_lock, flags); | |
372e722e | 870 | gpiod_get_direction(desc); |
80b0a602 MN |
871 | spin_lock_irqsave(&gpio_lock, flags); |
872 | } | |
77c2d792 MW |
873 | done: |
874 | spin_unlock_irqrestore(&gpio_lock, flags); | |
875 | return status; | |
876 | } | |
877 | ||
0eb4c6c2 | 878 | int gpiod_request(struct gpio_desc *desc, const char *label) |
77c2d792 MW |
879 | { |
880 | int status = -EPROBE_DEFER; | |
881 | struct gpio_chip *chip; | |
882 | ||
883 | if (!desc) { | |
884 | pr_warn("%s: invalid GPIO\n", __func__); | |
885 | return -EINVAL; | |
886 | } | |
887 | ||
888 | chip = desc->chip; | |
889 | if (!chip) | |
890 | goto done; | |
891 | ||
892 | if (try_module_get(chip->owner)) { | |
893 | status = __gpiod_request(desc, label); | |
894 | if (status < 0) | |
895 | module_put(chip->owner); | |
896 | } | |
897 | ||
d2876d08 DB |
898 | done: |
899 | if (status) | |
7589e59f | 900 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
77c2d792 | 901 | |
d2876d08 DB |
902 | return status; |
903 | } | |
372e722e | 904 | |
77c2d792 | 905 | static bool __gpiod_free(struct gpio_desc *desc) |
d2876d08 | 906 | { |
77c2d792 | 907 | bool ret = false; |
d2876d08 | 908 | unsigned long flags; |
35e8bb51 | 909 | struct gpio_chip *chip; |
d2876d08 | 910 | |
3d599d1c UKK |
911 | might_sleep(); |
912 | ||
372e722e | 913 | gpiod_unexport(desc); |
d8f388d8 | 914 | |
d2876d08 DB |
915 | spin_lock_irqsave(&gpio_lock, flags); |
916 | ||
35e8bb51 DB |
917 | chip = desc->chip; |
918 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { | |
919 | if (chip->free) { | |
920 | spin_unlock_irqrestore(&gpio_lock, flags); | |
9c4ba946 | 921 | might_sleep_if(chip->can_sleep); |
372e722e | 922 | chip->free(chip, gpio_chip_hwgpio(desc)); |
35e8bb51 DB |
923 | spin_lock_irqsave(&gpio_lock, flags); |
924 | } | |
d2876d08 | 925 | desc_set_label(desc, NULL); |
07697461 | 926 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
35e8bb51 | 927 | clear_bit(FLAG_REQUESTED, &desc->flags); |
aca5ce14 | 928 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
25553ff0 | 929 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
f625d460 | 930 | clear_bit(FLAG_IS_HOGGED, &desc->flags); |
77c2d792 MW |
931 | ret = true; |
932 | } | |
d2876d08 DB |
933 | |
934 | spin_unlock_irqrestore(&gpio_lock, flags); | |
77c2d792 MW |
935 | return ret; |
936 | } | |
937 | ||
0eb4c6c2 | 938 | void gpiod_free(struct gpio_desc *desc) |
77c2d792 MW |
939 | { |
940 | if (desc && __gpiod_free(desc)) | |
941 | module_put(desc->chip->owner); | |
942 | else | |
943 | WARN_ON(extra_checks); | |
d2876d08 | 944 | } |
372e722e | 945 | |
d2876d08 DB |
946 | /** |
947 | * gpiochip_is_requested - return string iff signal was requested | |
948 | * @chip: controller managing the signal | |
949 | * @offset: of signal within controller's 0..(ngpio - 1) range | |
950 | * | |
951 | * Returns NULL if the GPIO is not currently requested, else a string. | |
9c8318ff AC |
952 | * The string returned is the label passed to gpio_request(); if none has been |
953 | * passed it is a meaningless, non-NULL constant. | |
d2876d08 DB |
954 | * |
955 | * This function is for use by GPIO controller drivers. The label can | |
956 | * help with diagnostics, and knowing that the signal is used as a GPIO | |
957 | * can help avoid accidentally multiplexing it to another controller. | |
958 | */ | |
959 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) | |
960 | { | |
6c0b4e6c | 961 | struct gpio_desc *desc; |
d2876d08 | 962 | |
6c0b4e6c | 963 | if (!GPIO_OFFSET_VALID(chip, offset)) |
d2876d08 | 964 | return NULL; |
6c0b4e6c AC |
965 | |
966 | desc = &chip->desc[offset]; | |
967 | ||
372e722e | 968 | if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) |
d2876d08 | 969 | return NULL; |
372e722e | 970 | return desc->label; |
d2876d08 DB |
971 | } |
972 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); | |
973 | ||
77c2d792 MW |
974 | /** |
975 | * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor | |
976 | * @desc: GPIO descriptor to request | |
977 | * @label: label for the GPIO | |
978 | * | |
979 | * Function allows GPIO chip drivers to request and use their own GPIO | |
980 | * descriptors via gpiolib API. Difference to gpiod_request() is that this | |
981 | * function will not increase reference count of the GPIO chip module. This | |
982 | * allows the GPIO chip module to be unloaded as needed (we assume that the | |
983 | * GPIO chip driver handles freeing the GPIOs it has requested). | |
984 | */ | |
abdc08a3 AC |
985 | struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum, |
986 | const char *label) | |
77c2d792 | 987 | { |
abdc08a3 AC |
988 | struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum); |
989 | int err; | |
77c2d792 | 990 | |
abdc08a3 AC |
991 | if (IS_ERR(desc)) { |
992 | chip_err(chip, "failed to get GPIO descriptor\n"); | |
993 | return desc; | |
994 | } | |
995 | ||
996 | err = __gpiod_request(desc, label); | |
997 | if (err < 0) | |
998 | return ERR_PTR(err); | |
77c2d792 | 999 | |
abdc08a3 | 1000 | return desc; |
77c2d792 | 1001 | } |
f7d4ad98 | 1002 | EXPORT_SYMBOL_GPL(gpiochip_request_own_desc); |
77c2d792 MW |
1003 | |
1004 | /** | |
1005 | * gpiochip_free_own_desc - Free GPIO requested by the chip driver | |
1006 | * @desc: GPIO descriptor to free | |
1007 | * | |
1008 | * Function frees the given GPIO requested previously with | |
1009 | * gpiochip_request_own_desc(). | |
1010 | */ | |
1011 | void gpiochip_free_own_desc(struct gpio_desc *desc) | |
1012 | { | |
1013 | if (desc) | |
1014 | __gpiod_free(desc); | |
1015 | } | |
f7d4ad98 | 1016 | EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); |
d2876d08 DB |
1017 | |
1018 | /* Drivers MUST set GPIO direction before making get/set calls. In | |
1019 | * some cases this is done in early boot, before IRQs are enabled. | |
1020 | * | |
1021 | * As a rule these aren't called more than once (except for drivers | |
1022 | * using the open-drain emulation idiom) so these are natural places | |
1023 | * to accumulate extra debugging checks. Note that we can't (yet) | |
1024 | * rely on gpio_request() having been called beforehand. | |
1025 | */ | |
1026 | ||
79a9becd AC |
1027 | /** |
1028 | * gpiod_direction_input - set the GPIO direction to input | |
1029 | * @desc: GPIO to set to input | |
1030 | * | |
1031 | * Set the direction of the passed GPIO to input, such as gpiod_get_value() can | |
1032 | * be called safely on it. | |
1033 | * | |
1034 | * Return 0 in case of success, else an error code. | |
1035 | */ | |
1036 | int gpiod_direction_input(struct gpio_desc *desc) | |
d2876d08 | 1037 | { |
d2876d08 | 1038 | struct gpio_chip *chip; |
d2876d08 DB |
1039 | int status = -EINVAL; |
1040 | ||
be1a4b13 | 1041 | if (!desc || !desc->chip) { |
bcabdef1 AC |
1042 | pr_warn("%s: invalid GPIO\n", __func__); |
1043 | return -EINVAL; | |
1044 | } | |
1045 | ||
be1a4b13 LW |
1046 | chip = desc->chip; |
1047 | if (!chip->get || !chip->direction_input) { | |
6424de5a MB |
1048 | gpiod_warn(desc, |
1049 | "%s: missing get() or direction_input() operations\n", | |
7589e59f | 1050 | __func__); |
be1a4b13 LW |
1051 | return -EIO; |
1052 | } | |
1053 | ||
d82da797 | 1054 | status = chip->direction_input(chip, gpio_chip_hwgpio(desc)); |
d2876d08 DB |
1055 | if (status == 0) |
1056 | clear_bit(FLAG_IS_OUT, &desc->flags); | |
3f397c21 | 1057 | |
372e722e | 1058 | trace_gpio_direction(desc_to_gpio(desc), 1, status); |
d82da797 | 1059 | |
d2876d08 DB |
1060 | return status; |
1061 | } | |
79a9becd | 1062 | EXPORT_SYMBOL_GPL(gpiod_direction_input); |
372e722e | 1063 | |
ef70bbe1 | 1064 | static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value) |
d2876d08 | 1065 | { |
d2876d08 | 1066 | struct gpio_chip *chip; |
d2876d08 DB |
1067 | int status = -EINVAL; |
1068 | ||
d468bf9e LW |
1069 | /* GPIOs used for IRQs shall not be set as output */ |
1070 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { | |
1071 | gpiod_err(desc, | |
1072 | "%s: tried to set a GPIO tied to an IRQ as output\n", | |
1073 | __func__); | |
1074 | return -EIO; | |
1075 | } | |
1076 | ||
aca5ce14 LD |
1077 | /* Open drain pin should not be driven to 1 */ |
1078 | if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | |
372e722e | 1079 | return gpiod_direction_input(desc); |
aca5ce14 | 1080 | |
25553ff0 LD |
1081 | /* Open source pin should not be driven to 0 */ |
1082 | if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | |
372e722e | 1083 | return gpiod_direction_input(desc); |
25553ff0 | 1084 | |
be1a4b13 LW |
1085 | chip = desc->chip; |
1086 | if (!chip->set || !chip->direction_output) { | |
6424de5a MB |
1087 | gpiod_warn(desc, |
1088 | "%s: missing set() or direction_output() operations\n", | |
1089 | __func__); | |
be1a4b13 LW |
1090 | return -EIO; |
1091 | } | |
1092 | ||
d82da797 | 1093 | status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value); |
d2876d08 DB |
1094 | if (status == 0) |
1095 | set_bit(FLAG_IS_OUT, &desc->flags); | |
372e722e AC |
1096 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
1097 | trace_gpio_direction(desc_to_gpio(desc), 0, status); | |
d2876d08 DB |
1098 | return status; |
1099 | } | |
ef70bbe1 PZ |
1100 | |
1101 | /** | |
1102 | * gpiod_direction_output_raw - set the GPIO direction to output | |
1103 | * @desc: GPIO to set to output | |
1104 | * @value: initial output value of the GPIO | |
1105 | * | |
1106 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can | |
1107 | * be called safely on it. The initial value of the output must be specified | |
1108 | * as raw value on the physical line without regard for the ACTIVE_LOW status. | |
1109 | * | |
1110 | * Return 0 in case of success, else an error code. | |
1111 | */ | |
1112 | int gpiod_direction_output_raw(struct gpio_desc *desc, int value) | |
1113 | { | |
1114 | if (!desc || !desc->chip) { | |
1115 | pr_warn("%s: invalid GPIO\n", __func__); | |
1116 | return -EINVAL; | |
1117 | } | |
1118 | return _gpiod_direction_output_raw(desc, value); | |
1119 | } | |
1120 | EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); | |
1121 | ||
1122 | /** | |
90df4fe0 | 1123 | * gpiod_direction_output - set the GPIO direction to output |
ef70bbe1 PZ |
1124 | * @desc: GPIO to set to output |
1125 | * @value: initial output value of the GPIO | |
1126 | * | |
1127 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can | |
1128 | * be called safely on it. The initial value of the output must be specified | |
1129 | * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | |
1130 | * account. | |
1131 | * | |
1132 | * Return 0 in case of success, else an error code. | |
1133 | */ | |
1134 | int gpiod_direction_output(struct gpio_desc *desc, int value) | |
1135 | { | |
1136 | if (!desc || !desc->chip) { | |
1137 | pr_warn("%s: invalid GPIO\n", __func__); | |
1138 | return -EINVAL; | |
1139 | } | |
1140 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
1141 | value = !value; | |
1142 | return _gpiod_direction_output_raw(desc, value); | |
1143 | } | |
79a9becd | 1144 | EXPORT_SYMBOL_GPL(gpiod_direction_output); |
d2876d08 | 1145 | |
c4b5be98 | 1146 | /** |
79a9becd | 1147 | * gpiod_set_debounce - sets @debounce time for a @gpio |
c4b5be98 FB |
1148 | * @gpio: the gpio to set debounce time |
1149 | * @debounce: debounce time is microseconds | |
65d87656 LW |
1150 | * |
1151 | * returns -ENOTSUPP if the controller does not support setting | |
1152 | * debounce. | |
c4b5be98 | 1153 | */ |
79a9becd | 1154 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
c4b5be98 | 1155 | { |
c4b5be98 | 1156 | struct gpio_chip *chip; |
c4b5be98 | 1157 | |
be1a4b13 | 1158 | if (!desc || !desc->chip) { |
bcabdef1 AC |
1159 | pr_warn("%s: invalid GPIO\n", __func__); |
1160 | return -EINVAL; | |
1161 | } | |
1162 | ||
c4b5be98 | 1163 | chip = desc->chip; |
be1a4b13 | 1164 | if (!chip->set || !chip->set_debounce) { |
6424de5a MB |
1165 | gpiod_dbg(desc, |
1166 | "%s: missing set() or set_debounce() operations\n", | |
1167 | __func__); | |
65d87656 | 1168 | return -ENOTSUPP; |
be1a4b13 LW |
1169 | } |
1170 | ||
d82da797 | 1171 | return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce); |
c4b5be98 | 1172 | } |
79a9becd | 1173 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); |
372e722e | 1174 | |
79a9becd AC |
1175 | /** |
1176 | * gpiod_is_active_low - test whether a GPIO is active-low or not | |
1177 | * @desc: the gpio descriptor to test | |
1178 | * | |
1179 | * Returns 1 if the GPIO is active-low, 0 otherwise. | |
1180 | */ | |
1181 | int gpiod_is_active_low(const struct gpio_desc *desc) | |
372e722e | 1182 | { |
79a9becd | 1183 | return test_bit(FLAG_ACTIVE_LOW, &desc->flags); |
372e722e | 1184 | } |
79a9becd | 1185 | EXPORT_SYMBOL_GPL(gpiod_is_active_low); |
d2876d08 DB |
1186 | |
1187 | /* I/O calls are only valid after configuration completed; the relevant | |
1188 | * "is this a valid GPIO" error checks should already have been done. | |
1189 | * | |
1190 | * "Get" operations are often inlinable as reading a pin value register, | |
1191 | * and masking the relevant bit in that register. | |
1192 | * | |
1193 | * When "set" operations are inlinable, they involve writing that mask to | |
1194 | * one register to set a low value, or a different register to set it high. | |
1195 | * Otherwise locking is needed, so there may be little value to inlining. | |
1196 | * | |
1197 | *------------------------------------------------------------------------ | |
1198 | * | |
1199 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers | |
1200 | * have requested the GPIO. That can include implicit requesting by | |
1201 | * a direction setting call. Marking a gpio as requested locks its chip | |
1202 | * in memory, guaranteeing that these table lookups need no more locking | |
1203 | * and that gpiochip_remove() will fail. | |
1204 | * | |
1205 | * REVISIT when debugging, consider adding some instrumentation to ensure | |
1206 | * that the GPIO was actually requested. | |
1207 | */ | |
1208 | ||
e20538b8 | 1209 | static int _gpiod_get_raw_value(const struct gpio_desc *desc) |
d2876d08 DB |
1210 | { |
1211 | struct gpio_chip *chip; | |
372e722e | 1212 | int offset; |
e20538b8 | 1213 | int value; |
d2876d08 | 1214 | |
372e722e AC |
1215 | chip = desc->chip; |
1216 | offset = gpio_chip_hwgpio(desc); | |
e20538b8 BA |
1217 | value = chip->get ? chip->get(chip, offset) : -EIO; |
1218 | value = value < 0 ? value : !!value; | |
372e722e | 1219 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
3f397c21 | 1220 | return value; |
d2876d08 | 1221 | } |
372e722e | 1222 | |
d2876d08 | 1223 | /** |
79a9becd AC |
1224 | * gpiod_get_raw_value() - return a gpio's raw value |
1225 | * @desc: gpio whose value will be returned | |
d2876d08 | 1226 | * |
79a9becd | 1227 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding |
e20538b8 | 1228 | * its ACTIVE_LOW status, or negative errno on failure. |
79a9becd AC |
1229 | * |
1230 | * This function should be called from contexts where we cannot sleep, and will | |
1231 | * complain if the GPIO chip functions potentially sleep. | |
d2876d08 | 1232 | */ |
79a9becd | 1233 | int gpiod_get_raw_value(const struct gpio_desc *desc) |
d2876d08 | 1234 | { |
bcabdef1 AC |
1235 | if (!desc) |
1236 | return 0; | |
e4e449e8 | 1237 | /* Should be using gpio_get_value_cansleep() */ |
d8e0ac08 | 1238 | WARN_ON(desc->chip->can_sleep); |
79a9becd | 1239 | return _gpiod_get_raw_value(desc); |
d2876d08 | 1240 | } |
79a9becd | 1241 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value); |
372e722e | 1242 | |
79a9becd AC |
1243 | /** |
1244 | * gpiod_get_value() - return a gpio's value | |
1245 | * @desc: gpio whose value will be returned | |
1246 | * | |
1247 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into | |
e20538b8 | 1248 | * account, or negative errno on failure. |
79a9becd AC |
1249 | * |
1250 | * This function should be called from contexts where we cannot sleep, and will | |
1251 | * complain if the GPIO chip functions potentially sleep. | |
1252 | */ | |
1253 | int gpiod_get_value(const struct gpio_desc *desc) | |
372e722e | 1254 | { |
79a9becd AC |
1255 | int value; |
1256 | if (!desc) | |
1257 | return 0; | |
1258 | /* Should be using gpio_get_value_cansleep() */ | |
1259 | WARN_ON(desc->chip->can_sleep); | |
1260 | ||
1261 | value = _gpiod_get_raw_value(desc); | |
e20538b8 BA |
1262 | if (value < 0) |
1263 | return value; | |
1264 | ||
79a9becd AC |
1265 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
1266 | value = !value; | |
1267 | ||
1268 | return value; | |
372e722e | 1269 | } |
79a9becd | 1270 | EXPORT_SYMBOL_GPL(gpiod_get_value); |
d2876d08 | 1271 | |
aca5ce14 LD |
1272 | /* |
1273 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. | |
79a9becd | 1274 | * @desc: gpio descriptor whose state need to be set. |
20a8a968 | 1275 | * @value: Non-zero for setting it HIGH otherwise it will set to LOW. |
aca5ce14 | 1276 | */ |
23600969 | 1277 | static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value) |
aca5ce14 LD |
1278 | { |
1279 | int err = 0; | |
372e722e AC |
1280 | struct gpio_chip *chip = desc->chip; |
1281 | int offset = gpio_chip_hwgpio(desc); | |
1282 | ||
aca5ce14 | 1283 | if (value) { |
372e722e | 1284 | err = chip->direction_input(chip, offset); |
aca5ce14 | 1285 | if (!err) |
372e722e | 1286 | clear_bit(FLAG_IS_OUT, &desc->flags); |
aca5ce14 | 1287 | } else { |
372e722e | 1288 | err = chip->direction_output(chip, offset, 0); |
aca5ce14 | 1289 | if (!err) |
372e722e | 1290 | set_bit(FLAG_IS_OUT, &desc->flags); |
aca5ce14 | 1291 | } |
372e722e | 1292 | trace_gpio_direction(desc_to_gpio(desc), value, err); |
aca5ce14 | 1293 | if (err < 0) |
6424de5a MB |
1294 | gpiod_err(desc, |
1295 | "%s: Error in set_value for open drain err %d\n", | |
1296 | __func__, err); | |
aca5ce14 LD |
1297 | } |
1298 | ||
25553ff0 | 1299 | /* |
79a9becd AC |
1300 | * _gpio_set_open_source_value() - Set the open source gpio's value. |
1301 | * @desc: gpio descriptor whose state need to be set. | |
20a8a968 | 1302 | * @value: Non-zero for setting it HIGH otherwise it will set to LOW. |
25553ff0 | 1303 | */ |
23600969 | 1304 | static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value) |
25553ff0 LD |
1305 | { |
1306 | int err = 0; | |
372e722e AC |
1307 | struct gpio_chip *chip = desc->chip; |
1308 | int offset = gpio_chip_hwgpio(desc); | |
1309 | ||
25553ff0 | 1310 | if (value) { |
372e722e | 1311 | err = chip->direction_output(chip, offset, 1); |
25553ff0 | 1312 | if (!err) |
372e722e | 1313 | set_bit(FLAG_IS_OUT, &desc->flags); |
25553ff0 | 1314 | } else { |
372e722e | 1315 | err = chip->direction_input(chip, offset); |
25553ff0 | 1316 | if (!err) |
372e722e | 1317 | clear_bit(FLAG_IS_OUT, &desc->flags); |
25553ff0 | 1318 | } |
372e722e | 1319 | trace_gpio_direction(desc_to_gpio(desc), !value, err); |
25553ff0 | 1320 | if (err < 0) |
6424de5a MB |
1321 | gpiod_err(desc, |
1322 | "%s: Error in set_value for open source err %d\n", | |
1323 | __func__, err); | |
25553ff0 LD |
1324 | } |
1325 | ||
23600969 | 1326 | static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value) |
d2876d08 DB |
1327 | { |
1328 | struct gpio_chip *chip; | |
1329 | ||
372e722e | 1330 | chip = desc->chip; |
372e722e AC |
1331 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
1332 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | |
1333 | _gpio_set_open_drain_value(desc, value); | |
1334 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | |
1335 | _gpio_set_open_source_value(desc, value); | |
aca5ce14 | 1336 | else |
372e722e AC |
1337 | chip->set(chip, gpio_chip_hwgpio(desc), value); |
1338 | } | |
1339 | ||
5f424243 RI |
1340 | /* |
1341 | * set multiple outputs on the same chip; | |
1342 | * use the chip's set_multiple function if available; | |
1343 | * otherwise set the outputs sequentially; | |
1344 | * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word | |
1345 | * defines which outputs are to be changed | |
1346 | * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word | |
1347 | * defines the values the outputs specified by mask are to be set to | |
1348 | */ | |
1349 | static void gpio_chip_set_multiple(struct gpio_chip *chip, | |
1350 | unsigned long *mask, unsigned long *bits) | |
1351 | { | |
1352 | if (chip->set_multiple) { | |
1353 | chip->set_multiple(chip, mask, bits); | |
1354 | } else { | |
1355 | int i; | |
1356 | for (i = 0; i < chip->ngpio; i++) { | |
1357 | if (mask[BIT_WORD(i)] == 0) { | |
1358 | /* no more set bits in this mask word; | |
1359 | * skip ahead to the next word */ | |
1360 | i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1; | |
1361 | continue; | |
1362 | } | |
1363 | /* set outputs if the corresponding mask bit is set */ | |
38e003f4 | 1364 | if (__test_and_clear_bit(i, mask)) |
5f424243 | 1365 | chip->set(chip, i, test_bit(i, bits)); |
5f424243 RI |
1366 | } |
1367 | } | |
1368 | } | |
1369 | ||
3fff99bc RI |
1370 | static void gpiod_set_array_value_priv(bool raw, bool can_sleep, |
1371 | unsigned int array_size, | |
1372 | struct gpio_desc **desc_array, | |
1373 | int *value_array) | |
5f424243 RI |
1374 | { |
1375 | int i = 0; | |
1376 | ||
1377 | while (i < array_size) { | |
1378 | struct gpio_chip *chip = desc_array[i]->chip; | |
1379 | unsigned long mask[BITS_TO_LONGS(chip->ngpio)]; | |
1380 | unsigned long bits[BITS_TO_LONGS(chip->ngpio)]; | |
1381 | int count = 0; | |
1382 | ||
38e003f4 | 1383 | if (!can_sleep) |
5f424243 | 1384 | WARN_ON(chip->can_sleep); |
38e003f4 | 1385 | |
5f424243 RI |
1386 | memset(mask, 0, sizeof(mask)); |
1387 | do { | |
1388 | struct gpio_desc *desc = desc_array[i]; | |
1389 | int hwgpio = gpio_chip_hwgpio(desc); | |
1390 | int value = value_array[i]; | |
1391 | ||
1392 | if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
1393 | value = !value; | |
1394 | trace_gpio_value(desc_to_gpio(desc), 0, value); | |
1395 | /* | |
1396 | * collect all normal outputs belonging to the same chip | |
1397 | * open drain and open source outputs are set individually | |
1398 | */ | |
1399 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { | |
38e003f4 | 1400 | _gpio_set_open_drain_value(desc, value); |
5f424243 RI |
1401 | } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { |
1402 | _gpio_set_open_source_value(desc, value); | |
1403 | } else { | |
1404 | __set_bit(hwgpio, mask); | |
38e003f4 | 1405 | if (value) |
5f424243 | 1406 | __set_bit(hwgpio, bits); |
38e003f4 | 1407 | else |
5f424243 | 1408 | __clear_bit(hwgpio, bits); |
5f424243 RI |
1409 | count++; |
1410 | } | |
1411 | i++; | |
1412 | } while ((i < array_size) && (desc_array[i]->chip == chip)); | |
1413 | /* push collected bits to outputs */ | |
38e003f4 | 1414 | if (count != 0) |
5f424243 | 1415 | gpio_chip_set_multiple(chip, mask, bits); |
5f424243 RI |
1416 | } |
1417 | } | |
1418 | ||
d2876d08 | 1419 | /** |
79a9becd AC |
1420 | * gpiod_set_raw_value() - assign a gpio's raw value |
1421 | * @desc: gpio whose value will be assigned | |
d2876d08 | 1422 | * @value: value to assign |
d2876d08 | 1423 | * |
79a9becd AC |
1424 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
1425 | * regard for its ACTIVE_LOW status. | |
1426 | * | |
1427 | * This function should be called from contexts where we cannot sleep, and will | |
1428 | * complain if the GPIO chip functions potentially sleep. | |
d2876d08 | 1429 | */ |
79a9becd | 1430 | void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
372e722e | 1431 | { |
bcabdef1 AC |
1432 | if (!desc) |
1433 | return; | |
e4e449e8 | 1434 | /* Should be using gpio_set_value_cansleep() */ |
d8e0ac08 | 1435 | WARN_ON(desc->chip->can_sleep); |
79a9becd | 1436 | _gpiod_set_raw_value(desc, value); |
d2876d08 | 1437 | } |
79a9becd | 1438 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value); |
d2876d08 DB |
1439 | |
1440 | /** | |
79a9becd AC |
1441 | * gpiod_set_value() - assign a gpio's value |
1442 | * @desc: gpio whose value will be assigned | |
1443 | * @value: value to assign | |
1444 | * | |
1445 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | |
1446 | * account | |
d2876d08 | 1447 | * |
79a9becd AC |
1448 | * This function should be called from contexts where we cannot sleep, and will |
1449 | * complain if the GPIO chip functions potentially sleep. | |
d2876d08 | 1450 | */ |
79a9becd | 1451 | void gpiod_set_value(struct gpio_desc *desc, int value) |
d2876d08 | 1452 | { |
bcabdef1 | 1453 | if (!desc) |
79a9becd AC |
1454 | return; |
1455 | /* Should be using gpio_set_value_cansleep() */ | |
1456 | WARN_ON(desc->chip->can_sleep); | |
1457 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
1458 | value = !value; | |
1459 | _gpiod_set_raw_value(desc, value); | |
372e722e | 1460 | } |
79a9becd | 1461 | EXPORT_SYMBOL_GPL(gpiod_set_value); |
d2876d08 | 1462 | |
5f424243 | 1463 | /** |
3fff99bc | 1464 | * gpiod_set_raw_array_value() - assign values to an array of GPIOs |
5f424243 RI |
1465 | * @array_size: number of elements in the descriptor / value arrays |
1466 | * @desc_array: array of GPIO descriptors whose values will be assigned | |
1467 | * @value_array: array of values to assign | |
1468 | * | |
1469 | * Set the raw values of the GPIOs, i.e. the values of the physical lines | |
1470 | * without regard for their ACTIVE_LOW status. | |
1471 | * | |
1472 | * This function should be called from contexts where we cannot sleep, and will | |
1473 | * complain if the GPIO chip functions potentially sleep. | |
1474 | */ | |
3fff99bc | 1475 | void gpiod_set_raw_array_value(unsigned int array_size, |
5f424243 RI |
1476 | struct gpio_desc **desc_array, int *value_array) |
1477 | { | |
1478 | if (!desc_array) | |
1479 | return; | |
3fff99bc RI |
1480 | gpiod_set_array_value_priv(true, false, array_size, desc_array, |
1481 | value_array); | |
5f424243 | 1482 | } |
3fff99bc | 1483 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value); |
5f424243 RI |
1484 | |
1485 | /** | |
3fff99bc | 1486 | * gpiod_set_array_value() - assign values to an array of GPIOs |
5f424243 RI |
1487 | * @array_size: number of elements in the descriptor / value arrays |
1488 | * @desc_array: array of GPIO descriptors whose values will be assigned | |
1489 | * @value_array: array of values to assign | |
1490 | * | |
1491 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status | |
1492 | * into account. | |
1493 | * | |
1494 | * This function should be called from contexts where we cannot sleep, and will | |
1495 | * complain if the GPIO chip functions potentially sleep. | |
1496 | */ | |
3fff99bc RI |
1497 | void gpiod_set_array_value(unsigned int array_size, |
1498 | struct gpio_desc **desc_array, int *value_array) | |
5f424243 RI |
1499 | { |
1500 | if (!desc_array) | |
1501 | return; | |
3fff99bc RI |
1502 | gpiod_set_array_value_priv(false, false, array_size, desc_array, |
1503 | value_array); | |
5f424243 | 1504 | } |
3fff99bc | 1505 | EXPORT_SYMBOL_GPL(gpiod_set_array_value); |
5f424243 | 1506 | |
d2876d08 | 1507 | /** |
79a9becd AC |
1508 | * gpiod_cansleep() - report whether gpio value access may sleep |
1509 | * @desc: gpio to check | |
d2876d08 | 1510 | * |
d2876d08 | 1511 | */ |
79a9becd | 1512 | int gpiod_cansleep(const struct gpio_desc *desc) |
372e722e | 1513 | { |
bcabdef1 AC |
1514 | if (!desc) |
1515 | return 0; | |
372e722e | 1516 | return desc->chip->can_sleep; |
d2876d08 | 1517 | } |
79a9becd | 1518 | EXPORT_SYMBOL_GPL(gpiod_cansleep); |
d2876d08 | 1519 | |
0f6d504e | 1520 | /** |
79a9becd AC |
1521 | * gpiod_to_irq() - return the IRQ corresponding to a GPIO |
1522 | * @desc: gpio whose IRQ will be returned (already requested) | |
0f6d504e | 1523 | * |
79a9becd AC |
1524 | * Return the IRQ corresponding to the passed GPIO, or an error code in case of |
1525 | * error. | |
0f6d504e | 1526 | */ |
79a9becd | 1527 | int gpiod_to_irq(const struct gpio_desc *desc) |
0f6d504e DB |
1528 | { |
1529 | struct gpio_chip *chip; | |
372e722e | 1530 | int offset; |
0f6d504e | 1531 | |
bcabdef1 AC |
1532 | if (!desc) |
1533 | return -EINVAL; | |
372e722e AC |
1534 | chip = desc->chip; |
1535 | offset = gpio_chip_hwgpio(desc); | |
1536 | return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; | |
0f6d504e | 1537 | } |
79a9becd | 1538 | EXPORT_SYMBOL_GPL(gpiod_to_irq); |
0f6d504e | 1539 | |
d468bf9e | 1540 | /** |
e3a2e878 | 1541 | * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ |
d74be6df AC |
1542 | * @chip: the chip the GPIO to lock belongs to |
1543 | * @offset: the offset of the GPIO to lock as IRQ | |
d468bf9e LW |
1544 | * |
1545 | * This is used directly by GPIO drivers that want to lock down | |
f438acdf | 1546 | * a certain GPIO line to be used for IRQs. |
d468bf9e | 1547 | */ |
e3a2e878 | 1548 | int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset) |
372e722e | 1549 | { |
d74be6df | 1550 | if (offset >= chip->ngpio) |
d468bf9e LW |
1551 | return -EINVAL; |
1552 | ||
d74be6df AC |
1553 | if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) { |
1554 | chip_err(chip, | |
d468bf9e LW |
1555 | "%s: tried to flag a GPIO set as output for IRQ\n", |
1556 | __func__); | |
1557 | return -EIO; | |
1558 | } | |
1559 | ||
d74be6df | 1560 | set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags); |
d468bf9e | 1561 | return 0; |
372e722e | 1562 | } |
e3a2e878 | 1563 | EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq); |
d2876d08 | 1564 | |
d468bf9e | 1565 | /** |
e3a2e878 | 1566 | * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ |
d74be6df AC |
1567 | * @chip: the chip the GPIO to lock belongs to |
1568 | * @offset: the offset of the GPIO to lock as IRQ | |
d468bf9e LW |
1569 | * |
1570 | * This is used directly by GPIO drivers that want to indicate | |
1571 | * that a certain GPIO is no longer used exclusively for IRQ. | |
d2876d08 | 1572 | */ |
e3a2e878 | 1573 | void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) |
d468bf9e | 1574 | { |
d74be6df | 1575 | if (offset >= chip->ngpio) |
d468bf9e | 1576 | return; |
d2876d08 | 1577 | |
d74be6df | 1578 | clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags); |
d468bf9e | 1579 | } |
e3a2e878 | 1580 | EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq); |
d468bf9e | 1581 | |
79a9becd AC |
1582 | /** |
1583 | * gpiod_get_raw_value_cansleep() - return a gpio's raw value | |
1584 | * @desc: gpio whose value will be returned | |
1585 | * | |
1586 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding | |
e20538b8 | 1587 | * its ACTIVE_LOW status, or negative errno on failure. |
79a9becd AC |
1588 | * |
1589 | * This function is to be called from contexts that can sleep. | |
d2876d08 | 1590 | */ |
79a9becd | 1591 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) |
d2876d08 | 1592 | { |
d2876d08 | 1593 | might_sleep_if(extra_checks); |
bcabdef1 AC |
1594 | if (!desc) |
1595 | return 0; | |
79a9becd | 1596 | return _gpiod_get_raw_value(desc); |
d2876d08 | 1597 | } |
79a9becd | 1598 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); |
372e722e | 1599 | |
79a9becd AC |
1600 | /** |
1601 | * gpiod_get_value_cansleep() - return a gpio's value | |
1602 | * @desc: gpio whose value will be returned | |
1603 | * | |
1604 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into | |
e20538b8 | 1605 | * account, or negative errno on failure. |
79a9becd AC |
1606 | * |
1607 | * This function is to be called from contexts that can sleep. | |
1608 | */ | |
1609 | int gpiod_get_value_cansleep(const struct gpio_desc *desc) | |
d2876d08 | 1610 | { |
3f397c21 | 1611 | int value; |
d2876d08 DB |
1612 | |
1613 | might_sleep_if(extra_checks); | |
bcabdef1 AC |
1614 | if (!desc) |
1615 | return 0; | |
79a9becd AC |
1616 | |
1617 | value = _gpiod_get_raw_value(desc); | |
e20538b8 BA |
1618 | if (value < 0) |
1619 | return value; | |
1620 | ||
79a9becd AC |
1621 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
1622 | value = !value; | |
1623 | ||
3f397c21 | 1624 | return value; |
d2876d08 | 1625 | } |
79a9becd | 1626 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); |
372e722e | 1627 | |
79a9becd AC |
1628 | /** |
1629 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value | |
1630 | * @desc: gpio whose value will be assigned | |
1631 | * @value: value to assign | |
1632 | * | |
1633 | * Set the raw value of the GPIO, i.e. the value of its physical line without | |
1634 | * regard for its ACTIVE_LOW status. | |
1635 | * | |
1636 | * This function is to be called from contexts that can sleep. | |
1637 | */ | |
1638 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) | |
372e722e | 1639 | { |
d2876d08 | 1640 | might_sleep_if(extra_checks); |
bcabdef1 AC |
1641 | if (!desc) |
1642 | return; | |
79a9becd | 1643 | _gpiod_set_raw_value(desc, value); |
372e722e | 1644 | } |
79a9becd | 1645 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); |
d2876d08 | 1646 | |
79a9becd AC |
1647 | /** |
1648 | * gpiod_set_value_cansleep() - assign a gpio's value | |
1649 | * @desc: gpio whose value will be assigned | |
1650 | * @value: value to assign | |
1651 | * | |
1652 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | |
1653 | * account | |
1654 | * | |
1655 | * This function is to be called from contexts that can sleep. | |
1656 | */ | |
1657 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) | |
d2876d08 | 1658 | { |
d2876d08 | 1659 | might_sleep_if(extra_checks); |
bcabdef1 AC |
1660 | if (!desc) |
1661 | return; | |
79a9becd AC |
1662 | |
1663 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
1664 | value = !value; | |
1665 | _gpiod_set_raw_value(desc, value); | |
372e722e | 1666 | } |
79a9becd | 1667 | EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); |
d2876d08 | 1668 | |
5f424243 | 1669 | /** |
3fff99bc | 1670 | * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs |
5f424243 RI |
1671 | * @array_size: number of elements in the descriptor / value arrays |
1672 | * @desc_array: array of GPIO descriptors whose values will be assigned | |
1673 | * @value_array: array of values to assign | |
1674 | * | |
1675 | * Set the raw values of the GPIOs, i.e. the values of the physical lines | |
1676 | * without regard for their ACTIVE_LOW status. | |
1677 | * | |
1678 | * This function is to be called from contexts that can sleep. | |
1679 | */ | |
3fff99bc RI |
1680 | void gpiod_set_raw_array_value_cansleep(unsigned int array_size, |
1681 | struct gpio_desc **desc_array, | |
1682 | int *value_array) | |
5f424243 RI |
1683 | { |
1684 | might_sleep_if(extra_checks); | |
1685 | if (!desc_array) | |
1686 | return; | |
3fff99bc RI |
1687 | gpiod_set_array_value_priv(true, true, array_size, desc_array, |
1688 | value_array); | |
5f424243 | 1689 | } |
3fff99bc | 1690 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep); |
5f424243 RI |
1691 | |
1692 | /** | |
3fff99bc | 1693 | * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs |
5f424243 RI |
1694 | * @array_size: number of elements in the descriptor / value arrays |
1695 | * @desc_array: array of GPIO descriptors whose values will be assigned | |
1696 | * @value_array: array of values to assign | |
1697 | * | |
1698 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status | |
1699 | * into account. | |
1700 | * | |
1701 | * This function is to be called from contexts that can sleep. | |
1702 | */ | |
3fff99bc RI |
1703 | void gpiod_set_array_value_cansleep(unsigned int array_size, |
1704 | struct gpio_desc **desc_array, | |
1705 | int *value_array) | |
5f424243 RI |
1706 | { |
1707 | might_sleep_if(extra_checks); | |
1708 | if (!desc_array) | |
1709 | return; | |
3fff99bc RI |
1710 | gpiod_set_array_value_priv(false, true, array_size, desc_array, |
1711 | value_array); | |
5f424243 | 1712 | } |
3fff99bc | 1713 | EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep); |
5f424243 | 1714 | |
bae48da2 | 1715 | /** |
ad824783 AC |
1716 | * gpiod_add_lookup_table() - register GPIO device consumers |
1717 | * @table: table of consumers to register | |
bae48da2 | 1718 | */ |
ad824783 | 1719 | void gpiod_add_lookup_table(struct gpiod_lookup_table *table) |
bae48da2 AC |
1720 | { |
1721 | mutex_lock(&gpio_lookup_lock); | |
1722 | ||
ad824783 | 1723 | list_add_tail(&table->list, &gpio_lookup_list); |
bae48da2 AC |
1724 | |
1725 | mutex_unlock(&gpio_lookup_lock); | |
1726 | } | |
1727 | ||
be9015ab SK |
1728 | /** |
1729 | * gpiod_remove_lookup_table() - unregister GPIO device consumers | |
1730 | * @table: table of consumers to unregister | |
1731 | */ | |
1732 | void gpiod_remove_lookup_table(struct gpiod_lookup_table *table) | |
1733 | { | |
1734 | mutex_lock(&gpio_lookup_lock); | |
1735 | ||
1736 | list_del(&table->list); | |
1737 | ||
1738 | mutex_unlock(&gpio_lookup_lock); | |
1739 | } | |
1740 | ||
bae48da2 | 1741 | static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, |
53e7cac3 AC |
1742 | unsigned int idx, |
1743 | enum gpio_lookup_flags *flags) | |
bae48da2 AC |
1744 | { |
1745 | char prop_name[32]; /* 32 is max size of property name */ | |
1746 | enum of_gpio_flags of_flags; | |
1747 | struct gpio_desc *desc; | |
dd34c37a | 1748 | unsigned int i; |
bae48da2 | 1749 | |
7f2e553a | 1750 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
dd34c37a | 1751 | if (con_id) |
9e089246 | 1752 | snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, |
7f2e553a | 1753 | gpio_suffixes[i]); |
dd34c37a | 1754 | else |
9e089246 | 1755 | snprintf(prop_name, sizeof(prop_name), "%s", |
7f2e553a | 1756 | gpio_suffixes[i]); |
bae48da2 | 1757 | |
dd34c37a TR |
1758 | desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, |
1759 | &of_flags); | |
06fc3b70 | 1760 | if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER)) |
dd34c37a TR |
1761 | break; |
1762 | } | |
bae48da2 AC |
1763 | |
1764 | if (IS_ERR(desc)) | |
1765 | return desc; | |
1766 | ||
1767 | if (of_flags & OF_GPIO_ACTIVE_LOW) | |
53e7cac3 | 1768 | *flags |= GPIO_ACTIVE_LOW; |
bae48da2 AC |
1769 | |
1770 | return desc; | |
1771 | } | |
d2876d08 | 1772 | |
81f59e9d | 1773 | static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, |
53e7cac3 AC |
1774 | unsigned int idx, |
1775 | enum gpio_lookup_flags *flags) | |
372e722e | 1776 | { |
0d9a693c | 1777 | struct acpi_device *adev = ACPI_COMPANION(dev); |
e01f440a MW |
1778 | struct acpi_gpio_info info; |
1779 | struct gpio_desc *desc; | |
0d9a693c MW |
1780 | char propname[32]; |
1781 | int i; | |
e01f440a | 1782 | |
0d9a693c | 1783 | /* Try first from _DSD */ |
7f2e553a | 1784 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
0d9a693c MW |
1785 | if (con_id && strcmp(con_id, "gpios")) { |
1786 | snprintf(propname, sizeof(propname), "%s-%s", | |
7f2e553a | 1787 | con_id, gpio_suffixes[i]); |
0d9a693c MW |
1788 | } else { |
1789 | snprintf(propname, sizeof(propname), "%s", | |
7f2e553a | 1790 | gpio_suffixes[i]); |
0d9a693c MW |
1791 | } |
1792 | ||
1793 | desc = acpi_get_gpiod_by_index(adev, propname, idx, &info); | |
1794 | if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER)) | |
1795 | break; | |
1796 | } | |
1797 | ||
1798 | /* Then from plain _CRS GPIOs */ | |
1799 | if (IS_ERR(desc)) { | |
1800 | desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info); | |
1801 | if (IS_ERR(desc)) | |
1802 | return desc; | |
1803 | } | |
e01f440a | 1804 | |
0d9a693c | 1805 | if (info.active_low) |
53e7cac3 | 1806 | *flags |= GPIO_ACTIVE_LOW; |
e01f440a MW |
1807 | |
1808 | return desc; | |
81f59e9d MW |
1809 | } |
1810 | ||
ad824783 | 1811 | static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) |
bae48da2 AC |
1812 | { |
1813 | const char *dev_id = dev ? dev_name(dev) : NULL; | |
ad824783 | 1814 | struct gpiod_lookup_table *table; |
bae48da2 AC |
1815 | |
1816 | mutex_lock(&gpio_lookup_lock); | |
1817 | ||
ad824783 AC |
1818 | list_for_each_entry(table, &gpio_lookup_list, list) { |
1819 | if (table->dev_id && dev_id) { | |
1820 | /* | |
1821 | * Valid strings on both ends, must be identical to have | |
1822 | * a match | |
1823 | */ | |
1824 | if (!strcmp(table->dev_id, dev_id)) | |
1825 | goto found; | |
1826 | } else { | |
1827 | /* | |
1828 | * One of the pointers is NULL, so both must be to have | |
1829 | * a match | |
1830 | */ | |
1831 | if (dev_id == table->dev_id) | |
1832 | goto found; | |
1833 | } | |
1834 | } | |
1835 | table = NULL; | |
bae48da2 | 1836 | |
ad824783 AC |
1837 | found: |
1838 | mutex_unlock(&gpio_lookup_lock); | |
1839 | return table; | |
1840 | } | |
bae48da2 | 1841 | |
ad824783 AC |
1842 | static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, |
1843 | unsigned int idx, | |
1844 | enum gpio_lookup_flags *flags) | |
1845 | { | |
2a3cf6a3 | 1846 | struct gpio_desc *desc = ERR_PTR(-ENOENT); |
ad824783 AC |
1847 | struct gpiod_lookup_table *table; |
1848 | struct gpiod_lookup *p; | |
bae48da2 | 1849 | |
ad824783 AC |
1850 | table = gpiod_find_lookup_table(dev); |
1851 | if (!table) | |
1852 | return desc; | |
bae48da2 | 1853 | |
ad824783 AC |
1854 | for (p = &table->table[0]; p->chip_label; p++) { |
1855 | struct gpio_chip *chip; | |
bae48da2 | 1856 | |
ad824783 | 1857 | /* idx must always match exactly */ |
bae48da2 AC |
1858 | if (p->idx != idx) |
1859 | continue; | |
1860 | ||
ad824783 AC |
1861 | /* If the lookup entry has a con_id, require exact match */ |
1862 | if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) | |
1863 | continue; | |
bae48da2 | 1864 | |
ad824783 | 1865 | chip = find_chip_by_name(p->chip_label); |
bae48da2 | 1866 | |
ad824783 | 1867 | if (!chip) { |
2a3cf6a3 AC |
1868 | dev_err(dev, "cannot find GPIO chip %s\n", |
1869 | p->chip_label); | |
1870 | return ERR_PTR(-ENODEV); | |
ad824783 | 1871 | } |
bae48da2 | 1872 | |
ad824783 | 1873 | if (chip->ngpio <= p->chip_hwnum) { |
2a3cf6a3 AC |
1874 | dev_err(dev, |
1875 | "requested GPIO %d is out of range [0..%d] for chip %s\n", | |
1876 | idx, chip->ngpio, chip->label); | |
1877 | return ERR_PTR(-EINVAL); | |
bae48da2 | 1878 | } |
bae48da2 | 1879 | |
bb1e88cc | 1880 | desc = gpiochip_get_desc(chip, p->chip_hwnum); |
ad824783 | 1881 | *flags = p->flags; |
bae48da2 | 1882 | |
2a3cf6a3 | 1883 | return desc; |
bae48da2 AC |
1884 | } |
1885 | ||
bae48da2 AC |
1886 | return desc; |
1887 | } | |
1888 | ||
66858527 RI |
1889 | static int dt_gpio_count(struct device *dev, const char *con_id) |
1890 | { | |
1891 | int ret; | |
1892 | char propname[32]; | |
1893 | unsigned int i; | |
1894 | ||
1895 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { | |
1896 | if (con_id) | |
1897 | snprintf(propname, sizeof(propname), "%s-%s", | |
1898 | con_id, gpio_suffixes[i]); | |
1899 | else | |
1900 | snprintf(propname, sizeof(propname), "%s", | |
1901 | gpio_suffixes[i]); | |
1902 | ||
1903 | ret = of_gpio_named_count(dev->of_node, propname); | |
1904 | if (ret >= 0) | |
1905 | break; | |
1906 | } | |
1907 | return ret; | |
1908 | } | |
1909 | ||
1910 | static int platform_gpio_count(struct device *dev, const char *con_id) | |
1911 | { | |
1912 | struct gpiod_lookup_table *table; | |
1913 | struct gpiod_lookup *p; | |
1914 | unsigned int count = 0; | |
1915 | ||
1916 | table = gpiod_find_lookup_table(dev); | |
1917 | if (!table) | |
1918 | return -ENOENT; | |
1919 | ||
1920 | for (p = &table->table[0]; p->chip_label; p++) { | |
1921 | if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || | |
1922 | (!con_id && !p->con_id)) | |
1923 | count++; | |
1924 | } | |
1925 | if (!count) | |
1926 | return -ENOENT; | |
1927 | ||
1928 | return count; | |
1929 | } | |
1930 | ||
1931 | /** | |
1932 | * gpiod_count - return the number of GPIOs associated with a device / function | |
1933 | * or -ENOENT if no GPIO has been assigned to the requested function | |
1934 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
1935 | * @con_id: function within the GPIO consumer | |
1936 | */ | |
1937 | int gpiod_count(struct device *dev, const char *con_id) | |
1938 | { | |
1939 | int count = -ENOENT; | |
1940 | ||
1941 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) | |
1942 | count = dt_gpio_count(dev, con_id); | |
1943 | else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) | |
1944 | count = acpi_gpio_count(dev, con_id); | |
1945 | ||
1946 | if (count < 0) | |
1947 | count = platform_gpio_count(dev, con_id); | |
1948 | ||
1949 | return count; | |
1950 | } | |
1951 | EXPORT_SYMBOL_GPL(gpiod_count); | |
1952 | ||
bae48da2 | 1953 | /** |
0879162f | 1954 | * gpiod_get - obtain a GPIO for a given GPIO function |
ad824783 | 1955 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
bae48da2 | 1956 | * @con_id: function within the GPIO consumer |
39b2bbe3 | 1957 | * @flags: optional GPIO initialization flags |
bae48da2 AC |
1958 | * |
1959 | * Return the GPIO descriptor corresponding to the function con_id of device | |
2a3cf6a3 | 1960 | * dev, -ENOENT if no GPIO has been assigned to the requested function, or |
20a8a968 | 1961 | * another IS_ERR() code if an error occurred while trying to acquire the GPIO. |
bae48da2 | 1962 | */ |
b17d1bf1 | 1963 | struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id, |
39b2bbe3 | 1964 | enum gpiod_flags flags) |
bae48da2 | 1965 | { |
39b2bbe3 | 1966 | return gpiod_get_index(dev, con_id, 0, flags); |
bae48da2 | 1967 | } |
b17d1bf1 | 1968 | EXPORT_SYMBOL_GPL(gpiod_get); |
bae48da2 | 1969 | |
29a1f233 TR |
1970 | /** |
1971 | * gpiod_get_optional - obtain an optional GPIO for a given GPIO function | |
1972 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
1973 | * @con_id: function within the GPIO consumer | |
39b2bbe3 | 1974 | * @flags: optional GPIO initialization flags |
29a1f233 TR |
1975 | * |
1976 | * This is equivalent to gpiod_get(), except that when no GPIO was assigned to | |
1977 | * the requested function it will return NULL. This is convenient for drivers | |
1978 | * that need to handle optional GPIOs. | |
1979 | */ | |
b17d1bf1 | 1980 | struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, |
39b2bbe3 AC |
1981 | const char *con_id, |
1982 | enum gpiod_flags flags) | |
29a1f233 | 1983 | { |
39b2bbe3 | 1984 | return gpiod_get_index_optional(dev, con_id, 0, flags); |
29a1f233 | 1985 | } |
b17d1bf1 | 1986 | EXPORT_SYMBOL_GPL(gpiod_get_optional); |
29a1f233 | 1987 | |
f625d460 BP |
1988 | |
1989 | /** | |
1990 | * gpiod_configure_flags - helper function to configure a given GPIO | |
1991 | * @desc: gpio whose value will be assigned | |
1992 | * @con_id: function within the GPIO consumer | |
1993 | * @lflags: gpio_lookup_flags - returned from of_find_gpio() or | |
1994 | * of_get_gpio_hog() | |
1995 | * @dflags: gpiod_flags - optional GPIO initialization flags | |
1996 | * | |
1997 | * Return 0 on success, -ENOENT if no GPIO has been assigned to the | |
1998 | * requested function and/or index, or another IS_ERR() code if an error | |
1999 | * occurred while trying to acquire the GPIO. | |
2000 | */ | |
2001 | static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, | |
2002 | unsigned long lflags, enum gpiod_flags dflags) | |
2003 | { | |
2004 | int status; | |
2005 | ||
2006 | if (lflags & GPIO_ACTIVE_LOW) | |
2007 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
2008 | if (lflags & GPIO_OPEN_DRAIN) | |
2009 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); | |
2010 | if (lflags & GPIO_OPEN_SOURCE) | |
2011 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); | |
2012 | ||
2013 | /* No particular flag request, return here... */ | |
2014 | if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) { | |
2015 | pr_debug("no flags found for %s\n", con_id); | |
2016 | return 0; | |
2017 | } | |
2018 | ||
2019 | /* Process flags */ | |
2020 | if (dflags & GPIOD_FLAGS_BIT_DIR_OUT) | |
2021 | status = gpiod_direction_output(desc, | |
2022 | dflags & GPIOD_FLAGS_BIT_DIR_VAL); | |
2023 | else | |
2024 | status = gpiod_direction_input(desc); | |
2025 | ||
2026 | return status; | |
2027 | } | |
2028 | ||
bae48da2 AC |
2029 | /** |
2030 | * gpiod_get_index - obtain a GPIO from a multi-index GPIO function | |
fdd6a5fe | 2031 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
bae48da2 AC |
2032 | * @con_id: function within the GPIO consumer |
2033 | * @idx: index of the GPIO to obtain in the consumer | |
39b2bbe3 | 2034 | * @flags: optional GPIO initialization flags |
bae48da2 AC |
2035 | * |
2036 | * This variant of gpiod_get() allows to access GPIOs other than the first | |
2037 | * defined one for functions that define several GPIOs. | |
2038 | * | |
2a3cf6a3 AC |
2039 | * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the |
2040 | * requested function and/or index, or another IS_ERR() code if an error | |
20a8a968 | 2041 | * occurred while trying to acquire the GPIO. |
bae48da2 | 2042 | */ |
b17d1bf1 | 2043 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
bae48da2 | 2044 | const char *con_id, |
39b2bbe3 AC |
2045 | unsigned int idx, |
2046 | enum gpiod_flags flags) | |
bae48da2 | 2047 | { |
35c5d7fd | 2048 | struct gpio_desc *desc = NULL; |
bae48da2 | 2049 | int status; |
39b2bbe3 | 2050 | enum gpio_lookup_flags lookupflags = 0; |
bae48da2 AC |
2051 | |
2052 | dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); | |
2053 | ||
4d8440b9 RW |
2054 | if (dev) { |
2055 | /* Using device tree? */ | |
2056 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) { | |
2057 | dev_dbg(dev, "using device tree for GPIO lookup\n"); | |
2058 | desc = of_find_gpio(dev, con_id, idx, &lookupflags); | |
2059 | } else if (ACPI_COMPANION(dev)) { | |
2060 | dev_dbg(dev, "using ACPI for GPIO lookup\n"); | |
2061 | desc = acpi_find_gpio(dev, con_id, idx, &lookupflags); | |
2062 | } | |
35c5d7fd AC |
2063 | } |
2064 | ||
2065 | /* | |
2066 | * Either we are not using DT or ACPI, or their lookup did not return | |
2067 | * a result. In that case, use platform lookup as a fallback. | |
2068 | */ | |
2a3cf6a3 | 2069 | if (!desc || desc == ERR_PTR(-ENOENT)) { |
43a8785a | 2070 | dev_dbg(dev, "using lookup tables for GPIO lookup\n"); |
39b2bbe3 | 2071 | desc = gpiod_find(dev, con_id, idx, &lookupflags); |
bae48da2 AC |
2072 | } |
2073 | ||
2074 | if (IS_ERR(desc)) { | |
351cfe0f | 2075 | dev_dbg(dev, "lookup for GPIO %s failed\n", con_id); |
bae48da2 AC |
2076 | return desc; |
2077 | } | |
2078 | ||
2079 | status = gpiod_request(desc, con_id); | |
bae48da2 AC |
2080 | if (status < 0) |
2081 | return ERR_PTR(status); | |
2082 | ||
f625d460 | 2083 | status = gpiod_configure_flags(desc, con_id, lookupflags, flags); |
39b2bbe3 AC |
2084 | if (status < 0) { |
2085 | dev_dbg(dev, "setup of GPIO %s failed\n", con_id); | |
2086 | gpiod_put(desc); | |
2087 | return ERR_PTR(status); | |
2088 | } | |
2089 | ||
bae48da2 AC |
2090 | return desc; |
2091 | } | |
b17d1bf1 | 2092 | EXPORT_SYMBOL_GPL(gpiod_get_index); |
bae48da2 | 2093 | |
40b73183 MW |
2094 | /** |
2095 | * fwnode_get_named_gpiod - obtain a GPIO from firmware node | |
2096 | * @fwnode: handle of the firmware node | |
2097 | * @propname: name of the firmware property representing the GPIO | |
2098 | * | |
2099 | * This function can be used for drivers that get their configuration | |
2100 | * from firmware. | |
2101 | * | |
2102 | * Function properly finds the corresponding GPIO using whatever is the | |
2103 | * underlying firmware interface and then makes sure that the GPIO | |
2104 | * descriptor is requested before it is returned to the caller. | |
2105 | * | |
2106 | * In case of error an ERR_PTR() is returned. | |
2107 | */ | |
2108 | struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, | |
2109 | const char *propname) | |
2110 | { | |
2111 | struct gpio_desc *desc = ERR_PTR(-ENODEV); | |
2112 | bool active_low = false; | |
2113 | int ret; | |
2114 | ||
2115 | if (!fwnode) | |
2116 | return ERR_PTR(-EINVAL); | |
2117 | ||
2118 | if (is_of_node(fwnode)) { | |
2119 | enum of_gpio_flags flags; | |
2120 | ||
c181fb3e | 2121 | desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0, |
40b73183 MW |
2122 | &flags); |
2123 | if (!IS_ERR(desc)) | |
2124 | active_low = flags & OF_GPIO_ACTIVE_LOW; | |
2125 | } else if (is_acpi_node(fwnode)) { | |
2126 | struct acpi_gpio_info info; | |
2127 | ||
c181fb3e | 2128 | desc = acpi_get_gpiod_by_index(to_acpi_node(fwnode), propname, 0, |
40b73183 MW |
2129 | &info); |
2130 | if (!IS_ERR(desc)) | |
2131 | active_low = info.active_low; | |
2132 | } | |
2133 | ||
2134 | if (IS_ERR(desc)) | |
2135 | return desc; | |
2136 | ||
2137 | ret = gpiod_request(desc, NULL); | |
2138 | if (ret) | |
2139 | return ERR_PTR(ret); | |
2140 | ||
2141 | /* Only value flag can be set from both DT and ACPI is active_low */ | |
2142 | if (active_low) | |
2143 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
2144 | ||
2145 | return desc; | |
2146 | } | |
2147 | EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod); | |
2148 | ||
29a1f233 TR |
2149 | /** |
2150 | * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO | |
2151 | * function | |
2152 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
2153 | * @con_id: function within the GPIO consumer | |
2154 | * @index: index of the GPIO to obtain in the consumer | |
39b2bbe3 | 2155 | * @flags: optional GPIO initialization flags |
29a1f233 TR |
2156 | * |
2157 | * This is equivalent to gpiod_get_index(), except that when no GPIO with the | |
2158 | * specified index was assigned to the requested function it will return NULL. | |
2159 | * This is convenient for drivers that need to handle optional GPIOs. | |
2160 | */ | |
b17d1bf1 | 2161 | struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, |
29a1f233 | 2162 | const char *con_id, |
39b2bbe3 AC |
2163 | unsigned int index, |
2164 | enum gpiod_flags flags) | |
29a1f233 TR |
2165 | { |
2166 | struct gpio_desc *desc; | |
2167 | ||
39b2bbe3 | 2168 | desc = gpiod_get_index(dev, con_id, index, flags); |
29a1f233 TR |
2169 | if (IS_ERR(desc)) { |
2170 | if (PTR_ERR(desc) == -ENOENT) | |
2171 | return NULL; | |
2172 | } | |
2173 | ||
2174 | return desc; | |
2175 | } | |
b17d1bf1 | 2176 | EXPORT_SYMBOL_GPL(gpiod_get_index_optional); |
29a1f233 | 2177 | |
f625d460 BP |
2178 | /** |
2179 | * gpiod_hog - Hog the specified GPIO desc given the provided flags | |
2180 | * @desc: gpio whose value will be assigned | |
2181 | * @name: gpio line name | |
2182 | * @lflags: gpio_lookup_flags - returned from of_find_gpio() or | |
2183 | * of_get_gpio_hog() | |
2184 | * @dflags: gpiod_flags - optional GPIO initialization flags | |
2185 | */ | |
2186 | int gpiod_hog(struct gpio_desc *desc, const char *name, | |
2187 | unsigned long lflags, enum gpiod_flags dflags) | |
2188 | { | |
2189 | struct gpio_chip *chip; | |
2190 | struct gpio_desc *local_desc; | |
2191 | int hwnum; | |
2192 | int status; | |
2193 | ||
2194 | chip = gpiod_to_chip(desc); | |
2195 | hwnum = gpio_chip_hwgpio(desc); | |
2196 | ||
2197 | local_desc = gpiochip_request_own_desc(chip, hwnum, name); | |
2198 | if (IS_ERR(local_desc)) { | |
a713890d LW |
2199 | pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n", |
2200 | name, chip->label, hwnum); | |
f625d460 BP |
2201 | return PTR_ERR(local_desc); |
2202 | } | |
2203 | ||
2204 | status = gpiod_configure_flags(desc, name, lflags, dflags); | |
2205 | if (status < 0) { | |
a713890d LW |
2206 | pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n", |
2207 | name, chip->label, hwnum); | |
f625d460 BP |
2208 | gpiochip_free_own_desc(desc); |
2209 | return status; | |
2210 | } | |
2211 | ||
2212 | /* Mark GPIO as hogged so it can be identified and removed later */ | |
2213 | set_bit(FLAG_IS_HOGGED, &desc->flags); | |
2214 | ||
2215 | pr_info("GPIO line %d (%s) hogged as %s%s\n", | |
2216 | desc_to_gpio(desc), name, | |
2217 | (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input", | |
2218 | (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? | |
2219 | (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":""); | |
2220 | ||
2221 | return 0; | |
2222 | } | |
2223 | ||
2224 | /** | |
2225 | * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog | |
2226 | * @chip: gpio chip to act on | |
2227 | * | |
2228 | * This is only used by of_gpiochip_remove to free hogged gpios | |
2229 | */ | |
2230 | static void gpiochip_free_hogs(struct gpio_chip *chip) | |
2231 | { | |
2232 | int id; | |
2233 | ||
2234 | for (id = 0; id < chip->ngpio; id++) { | |
2235 | if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags)) | |
2236 | gpiochip_free_own_desc(&chip->desc[id]); | |
2237 | } | |
2238 | } | |
2239 | ||
66858527 RI |
2240 | /** |
2241 | * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function | |
2242 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
2243 | * @con_id: function within the GPIO consumer | |
2244 | * @flags: optional GPIO initialization flags | |
2245 | * | |
2246 | * This function acquires all the GPIOs defined under a given function. | |
2247 | * | |
2248 | * Return a struct gpio_descs containing an array of descriptors, -ENOENT if | |
2249 | * no GPIO has been assigned to the requested function, or another IS_ERR() | |
2250 | * code if an error occurred while trying to acquire the GPIOs. | |
2251 | */ | |
2252 | struct gpio_descs *__must_check gpiod_get_array(struct device *dev, | |
2253 | const char *con_id, | |
2254 | enum gpiod_flags flags) | |
2255 | { | |
2256 | struct gpio_desc *desc; | |
2257 | struct gpio_descs *descs; | |
2258 | int count; | |
2259 | ||
2260 | count = gpiod_count(dev, con_id); | |
2261 | if (count < 0) | |
2262 | return ERR_PTR(count); | |
2263 | ||
2264 | descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count, | |
2265 | GFP_KERNEL); | |
2266 | if (!descs) | |
2267 | return ERR_PTR(-ENOMEM); | |
2268 | ||
2269 | for (descs->ndescs = 0; descs->ndescs < count; ) { | |
2270 | desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); | |
2271 | if (IS_ERR(desc)) { | |
2272 | gpiod_put_array(descs); | |
2273 | return ERR_CAST(desc); | |
2274 | } | |
2275 | descs->desc[descs->ndescs] = desc; | |
2276 | descs->ndescs++; | |
2277 | } | |
2278 | return descs; | |
2279 | } | |
2280 | EXPORT_SYMBOL_GPL(gpiod_get_array); | |
2281 | ||
2282 | /** | |
2283 | * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO | |
2284 | * function | |
2285 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
2286 | * @con_id: function within the GPIO consumer | |
2287 | * @flags: optional GPIO initialization flags | |
2288 | * | |
2289 | * This is equivalent to gpiod_get_array(), except that when no GPIO was | |
2290 | * assigned to the requested function it will return NULL. | |
2291 | */ | |
2292 | struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, | |
2293 | const char *con_id, | |
2294 | enum gpiod_flags flags) | |
2295 | { | |
2296 | struct gpio_descs *descs; | |
2297 | ||
2298 | descs = gpiod_get_array(dev, con_id, flags); | |
2299 | if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT)) | |
2300 | return NULL; | |
2301 | ||
2302 | return descs; | |
2303 | } | |
2304 | EXPORT_SYMBOL_GPL(gpiod_get_array_optional); | |
2305 | ||
bae48da2 AC |
2306 | /** |
2307 | * gpiod_put - dispose of a GPIO descriptor | |
2308 | * @desc: GPIO descriptor to dispose of | |
2309 | * | |
2310 | * No descriptor can be used after gpiod_put() has been called on it. | |
2311 | */ | |
2312 | void gpiod_put(struct gpio_desc *desc) | |
2313 | { | |
2314 | gpiod_free(desc); | |
372e722e | 2315 | } |
bae48da2 | 2316 | EXPORT_SYMBOL_GPL(gpiod_put); |
d2876d08 | 2317 | |
66858527 RI |
2318 | /** |
2319 | * gpiod_put_array - dispose of multiple GPIO descriptors | |
2320 | * @descs: struct gpio_descs containing an array of descriptors | |
2321 | */ | |
2322 | void gpiod_put_array(struct gpio_descs *descs) | |
2323 | { | |
2324 | unsigned int i; | |
2325 | ||
2326 | for (i = 0; i < descs->ndescs; i++) | |
2327 | gpiod_put(descs->desc[i]); | |
2328 | ||
2329 | kfree(descs); | |
2330 | } | |
2331 | EXPORT_SYMBOL_GPL(gpiod_put_array); | |
2332 | ||
d2876d08 DB |
2333 | #ifdef CONFIG_DEBUG_FS |
2334 | ||
d2876d08 DB |
2335 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
2336 | { | |
2337 | unsigned i; | |
2338 | unsigned gpio = chip->base; | |
6c0b4e6c | 2339 | struct gpio_desc *gdesc = &chip->desc[0]; |
d2876d08 | 2340 | int is_out; |
d468bf9e | 2341 | int is_irq; |
d2876d08 DB |
2342 | |
2343 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { | |
2344 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) | |
2345 | continue; | |
2346 | ||
372e722e | 2347 | gpiod_get_direction(gdesc); |
d2876d08 | 2348 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
d468bf9e LW |
2349 | is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); |
2350 | seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s", | |
d2876d08 DB |
2351 | gpio, gdesc->label, |
2352 | is_out ? "out" : "in ", | |
2353 | chip->get | |
2354 | ? (chip->get(chip, i) ? "hi" : "lo") | |
d468bf9e LW |
2355 | : "? ", |
2356 | is_irq ? "IRQ" : " "); | |
d2876d08 DB |
2357 | seq_printf(s, "\n"); |
2358 | } | |
2359 | } | |
2360 | ||
f9c4a31f | 2361 | static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) |
d2876d08 | 2362 | { |
362432ae | 2363 | unsigned long flags; |
f9c4a31f | 2364 | struct gpio_chip *chip = NULL; |
cb1650d4 | 2365 | loff_t index = *pos; |
d2876d08 | 2366 | |
f9c4a31f | 2367 | s->private = ""; |
d2876d08 | 2368 | |
362432ae | 2369 | spin_lock_irqsave(&gpio_lock, flags); |
cb1650d4 | 2370 | list_for_each_entry(chip, &gpio_chips, list) |
362432ae GL |
2371 | if (index-- == 0) { |
2372 | spin_unlock_irqrestore(&gpio_lock, flags); | |
cb1650d4 | 2373 | return chip; |
f9c4a31f | 2374 | } |
362432ae | 2375 | spin_unlock_irqrestore(&gpio_lock, flags); |
f9c4a31f | 2376 | |
cb1650d4 | 2377 | return NULL; |
f9c4a31f TR |
2378 | } |
2379 | ||
2380 | static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
2381 | { | |
362432ae | 2382 | unsigned long flags; |
f9c4a31f | 2383 | struct gpio_chip *chip = v; |
f9c4a31f TR |
2384 | void *ret = NULL; |
2385 | ||
362432ae | 2386 | spin_lock_irqsave(&gpio_lock, flags); |
cb1650d4 AC |
2387 | if (list_is_last(&chip->list, &gpio_chips)) |
2388 | ret = NULL; | |
2389 | else | |
2390 | ret = list_entry(chip->list.next, struct gpio_chip, list); | |
362432ae | 2391 | spin_unlock_irqrestore(&gpio_lock, flags); |
f9c4a31f TR |
2392 | |
2393 | s->private = "\n"; | |
2394 | ++*pos; | |
2395 | ||
2396 | return ret; | |
2397 | } | |
2398 | ||
2399 | static void gpiolib_seq_stop(struct seq_file *s, void *v) | |
2400 | { | |
2401 | } | |
2402 | ||
2403 | static int gpiolib_seq_show(struct seq_file *s, void *v) | |
2404 | { | |
2405 | struct gpio_chip *chip = v; | |
2406 | struct device *dev; | |
2407 | ||
2408 | seq_printf(s, "%sGPIOs %d-%d", (char *)s->private, | |
2409 | chip->base, chip->base + chip->ngpio - 1); | |
2410 | dev = chip->dev; | |
2411 | if (dev) | |
2412 | seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus", | |
2413 | dev_name(dev)); | |
2414 | if (chip->label) | |
2415 | seq_printf(s, ", %s", chip->label); | |
2416 | if (chip->can_sleep) | |
2417 | seq_printf(s, ", can sleep"); | |
2418 | seq_printf(s, ":\n"); | |
2419 | ||
2420 | if (chip->dbg_show) | |
2421 | chip->dbg_show(s, chip); | |
2422 | else | |
2423 | gpiolib_dbg_show(s, chip); | |
2424 | ||
d2876d08 DB |
2425 | return 0; |
2426 | } | |
2427 | ||
f9c4a31f TR |
2428 | static const struct seq_operations gpiolib_seq_ops = { |
2429 | .start = gpiolib_seq_start, | |
2430 | .next = gpiolib_seq_next, | |
2431 | .stop = gpiolib_seq_stop, | |
2432 | .show = gpiolib_seq_show, | |
2433 | }; | |
2434 | ||
d2876d08 DB |
2435 | static int gpiolib_open(struct inode *inode, struct file *file) |
2436 | { | |
f9c4a31f | 2437 | return seq_open(file, &gpiolib_seq_ops); |
d2876d08 DB |
2438 | } |
2439 | ||
828c0950 | 2440 | static const struct file_operations gpiolib_operations = { |
f9c4a31f | 2441 | .owner = THIS_MODULE, |
d2876d08 DB |
2442 | .open = gpiolib_open, |
2443 | .read = seq_read, | |
2444 | .llseek = seq_lseek, | |
f9c4a31f | 2445 | .release = seq_release, |
d2876d08 DB |
2446 | }; |
2447 | ||
2448 | static int __init gpiolib_debugfs_init(void) | |
2449 | { | |
2450 | /* /sys/kernel/debug/gpio */ | |
2451 | (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, | |
2452 | NULL, NULL, &gpiolib_operations); | |
2453 | return 0; | |
2454 | } | |
2455 | subsys_initcall(gpiolib_debugfs_init); | |
2456 | ||
2457 | #endif /* DEBUG_FS */ |