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