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