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