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