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