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