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