]>
Commit | Line | Data |
---|---|---|
923a654c | 1 | #include <linux/bitmap.h> |
d2876d08 DB |
2 | #include <linux/kernel.h> |
3 | #include <linux/module.h> | |
ff77c352 | 4 | #include <linux/interrupt.h> |
d2876d08 DB |
5 | #include <linux/irq.h> |
6 | #include <linux/spinlock.h> | |
1a989d0f | 7 | #include <linux/list.h> |
d8f388d8 DB |
8 | #include <linux/device.h> |
9 | #include <linux/err.h> | |
10 | #include <linux/debugfs.h> | |
11 | #include <linux/seq_file.h> | |
12 | #include <linux/gpio.h> | |
391c970c | 13 | #include <linux/of_gpio.h> |
ff77c352 | 14 | #include <linux/idr.h> |
5a0e3ad6 | 15 | #include <linux/slab.h> |
7b199811 | 16 | #include <linux/acpi.h> |
53e7cac3 | 17 | #include <linux/gpio/driver.h> |
0a6d3158 | 18 | #include <linux/gpio/machine.h> |
c771c2f4 | 19 | #include <linux/pinctrl/consumer.h> |
3c702e99 LW |
20 | #include <linux/cdev.h> |
21 | #include <linux/fs.h> | |
22 | #include <linux/uaccess.h> | |
8b92e17e | 23 | #include <linux/compat.h> |
d7c51b47 | 24 | #include <linux/anon_inodes.h> |
953b956a | 25 | #include <linux/file.h> |
61f922db LW |
26 | #include <linux/kfifo.h> |
27 | #include <linux/poll.h> | |
28 | #include <linux/timekeeping.h> | |
3c702e99 | 29 | #include <uapi/linux/gpio.h> |
d2876d08 | 30 | |
664e3e5a MW |
31 | #include "gpiolib.h" |
32 | ||
3f397c21 UKK |
33 | #define CREATE_TRACE_POINTS |
34 | #include <trace/events/gpio.h> | |
d2876d08 | 35 | |
79a9becd | 36 | /* Implementation infrastructure for GPIO interfaces. |
d2876d08 | 37 | * |
79a9becd AC |
38 | * The GPIO programming interface allows for inlining speed-critical |
39 | * get/set operations for common cases, so that access to SOC-integrated | |
40 | * GPIOs can sometimes cost only an instruction or two per bit. | |
d2876d08 DB |
41 | */ |
42 | ||
43 | ||
44 | /* When debugging, extend minimal trust to callers and platform code. | |
45 | * Also emit diagnostic messages that may help initial bringup, when | |
46 | * board setup or driver bugs are most common. | |
47 | * | |
48 | * Otherwise, minimize overhead in what may be bitbanging codepaths. | |
49 | */ | |
50 | #ifdef DEBUG | |
51 | #define extra_checks 1 | |
52 | #else | |
53 | #define extra_checks 0 | |
54 | #endif | |
55 | ||
ff2b1359 LW |
56 | /* Device and char device-related information */ |
57 | static DEFINE_IDA(gpio_ida); | |
3c702e99 LW |
58 | static dev_t gpio_devt; |
59 | #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */ | |
60 | static struct bus_type gpio_bus_type = { | |
61 | .name = "gpio", | |
62 | }; | |
ff2b1359 | 63 | |
d2876d08 DB |
64 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. |
65 | * While any GPIO is requested, its gpio_chip is not removable; | |
66 | * each GPIO's "requested" flag serves as a lock and refcount. | |
67 | */ | |
0eb4c6c2 | 68 | DEFINE_SPINLOCK(gpio_lock); |
d2876d08 | 69 | |
bae48da2 AC |
70 | static DEFINE_MUTEX(gpio_lookup_lock); |
71 | static LIST_HEAD(gpio_lookup_list); | |
ff2b1359 | 72 | LIST_HEAD(gpio_devices); |
6d86750c JH |
73 | |
74 | static void gpiochip_free_hogs(struct gpio_chip *chip); | |
959bc7b2 | 75 | static int gpiochip_add_irqchip(struct gpio_chip *gpiochip, |
39c3fd58 AL |
76 | struct lock_class_key *lock_key, |
77 | struct lock_class_key *request_key); | |
6d86750c | 78 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); |
79b804cb MW |
79 | static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip); |
80 | static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip); | |
6d86750c | 81 | |
159f3cd9 | 82 | static bool gpiolib_initialized; |
6d86750c | 83 | |
d2876d08 DB |
84 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
85 | { | |
d2876d08 | 86 | d->label = label; |
d2876d08 DB |
87 | } |
88 | ||
372e722e | 89 | /** |
950d55f5 TR |
90 | * gpio_to_desc - Convert a GPIO number to its descriptor |
91 | * @gpio: global GPIO number | |
92 | * | |
93 | * Returns: | |
94 | * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO | |
95 | * with the given number exists in the system. | |
372e722e | 96 | */ |
79a9becd | 97 | struct gpio_desc *gpio_to_desc(unsigned gpio) |
372e722e | 98 | { |
ff2b1359 | 99 | struct gpio_device *gdev; |
14e85c0e AC |
100 | unsigned long flags; |
101 | ||
102 | spin_lock_irqsave(&gpio_lock, flags); | |
103 | ||
ff2b1359 | 104 | list_for_each_entry(gdev, &gpio_devices, list) { |
fdeb8e15 LW |
105 | if (gdev->base <= gpio && |
106 | gdev->base + gdev->ngpio > gpio) { | |
14e85c0e | 107 | spin_unlock_irqrestore(&gpio_lock, flags); |
fdeb8e15 | 108 | return &gdev->descs[gpio - gdev->base]; |
14e85c0e AC |
109 | } |
110 | } | |
111 | ||
112 | spin_unlock_irqrestore(&gpio_lock, flags); | |
113 | ||
0e9a5edf AC |
114 | if (!gpio_is_valid(gpio)) |
115 | WARN(1, "invalid GPIO %d\n", gpio); | |
116 | ||
14e85c0e | 117 | return NULL; |
372e722e | 118 | } |
79a9becd | 119 | EXPORT_SYMBOL_GPL(gpio_to_desc); |
372e722e | 120 | |
d468bf9e | 121 | /** |
950d55f5 TR |
122 | * gpiochip_get_desc - get the GPIO descriptor corresponding to the given |
123 | * hardware number for this chip | |
124 | * @chip: GPIO chip | |
125 | * @hwnum: hardware number of the GPIO for this chip | |
126 | * | |
127 | * Returns: | |
128 | * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists | |
129 | * in the given chip for the specified hardware number. | |
d468bf9e | 130 | */ |
bb1e88cc AC |
131 | struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, |
132 | u16 hwnum) | |
d468bf9e | 133 | { |
fdeb8e15 LW |
134 | struct gpio_device *gdev = chip->gpiodev; |
135 | ||
136 | if (hwnum >= gdev->ngpio) | |
b7d0a28a | 137 | return ERR_PTR(-EINVAL); |
d468bf9e | 138 | |
fdeb8e15 | 139 | return &gdev->descs[hwnum]; |
d468bf9e | 140 | } |
372e722e AC |
141 | |
142 | /** | |
950d55f5 TR |
143 | * desc_to_gpio - convert a GPIO descriptor to the integer namespace |
144 | * @desc: GPIO descriptor | |
145 | * | |
372e722e | 146 | * This should disappear in the future but is needed since we still |
950d55f5 TR |
147 | * use GPIO numbers for error messages and sysfs nodes. |
148 | * | |
149 | * Returns: | |
150 | * The global GPIO number for the GPIO specified by its descriptor. | |
372e722e | 151 | */ |
79a9becd | 152 | int desc_to_gpio(const struct gpio_desc *desc) |
372e722e | 153 | { |
fdeb8e15 | 154 | return desc->gdev->base + (desc - &desc->gdev->descs[0]); |
372e722e | 155 | } |
79a9becd | 156 | EXPORT_SYMBOL_GPL(desc_to_gpio); |
372e722e AC |
157 | |
158 | ||
79a9becd AC |
159 | /** |
160 | * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs | |
161 | * @desc: descriptor to return the chip of | |
162 | */ | |
163 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) | |
372e722e | 164 | { |
fdeb8e15 LW |
165 | if (!desc || !desc->gdev || !desc->gdev->chip) |
166 | return NULL; | |
167 | return desc->gdev->chip; | |
372e722e | 168 | } |
79a9becd | 169 | EXPORT_SYMBOL_GPL(gpiod_to_chip); |
d2876d08 | 170 | |
8d0aab2f AV |
171 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
172 | static int gpiochip_find_base(int ngpio) | |
173 | { | |
ff2b1359 | 174 | struct gpio_device *gdev; |
83cabe33 | 175 | int base = ARCH_NR_GPIOS - ngpio; |
8d0aab2f | 176 | |
ff2b1359 | 177 | list_for_each_entry_reverse(gdev, &gpio_devices, list) { |
83cabe33 | 178 | /* found a free space? */ |
fdeb8e15 | 179 | if (gdev->base + gdev->ngpio <= base) |
83cabe33 AC |
180 | break; |
181 | else | |
182 | /* nope, check the space right before the chip */ | |
fdeb8e15 | 183 | base = gdev->base - ngpio; |
8d0aab2f AV |
184 | } |
185 | ||
83cabe33 | 186 | if (gpio_is_valid(base)) { |
8d0aab2f | 187 | pr_debug("%s: found new base at %d\n", __func__, base); |
83cabe33 AC |
188 | return base; |
189 | } else { | |
190 | pr_err("%s: cannot find free range\n", __func__); | |
191 | return -ENOSPC; | |
169b6a7a | 192 | } |
169b6a7a AV |
193 | } |
194 | ||
79a9becd AC |
195 | /** |
196 | * gpiod_get_direction - return the current direction of a GPIO | |
197 | * @desc: GPIO to get the direction of | |
198 | * | |
199 | * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error. | |
200 | * | |
201 | * This function may sleep if gpiod_cansleep() is true. | |
202 | */ | |
8e53b0f1 | 203 | int gpiod_get_direction(struct gpio_desc *desc) |
80b0a602 MN |
204 | { |
205 | struct gpio_chip *chip; | |
372e722e | 206 | unsigned offset; |
80b0a602 MN |
207 | int status = -EINVAL; |
208 | ||
372e722e AC |
209 | chip = gpiod_to_chip(desc); |
210 | offset = gpio_chip_hwgpio(desc); | |
80b0a602 MN |
211 | |
212 | if (!chip->get_direction) | |
213 | return status; | |
214 | ||
372e722e | 215 | status = chip->get_direction(chip, offset); |
80b0a602 MN |
216 | if (status > 0) { |
217 | /* GPIOF_DIR_IN, or other positive */ | |
218 | status = 1; | |
8e53b0f1 | 219 | clear_bit(FLAG_IS_OUT, &desc->flags); |
80b0a602 MN |
220 | } |
221 | if (status == 0) { | |
222 | /* GPIOF_DIR_OUT */ | |
8e53b0f1 | 223 | set_bit(FLAG_IS_OUT, &desc->flags); |
80b0a602 MN |
224 | } |
225 | return status; | |
226 | } | |
79a9becd | 227 | EXPORT_SYMBOL_GPL(gpiod_get_direction); |
80b0a602 | 228 | |
1a989d0f AC |
229 | /* |
230 | * Add a new chip to the global chips list, keeping the list of chips sorted | |
ef7c7553 | 231 | * by range(means [base, base + ngpio - 1]) order. |
1a989d0f AC |
232 | * |
233 | * Return -EBUSY if the new chip overlaps with some other chip's integer | |
234 | * space. | |
235 | */ | |
ff2b1359 | 236 | static int gpiodev_add_to_list(struct gpio_device *gdev) |
1a989d0f | 237 | { |
a961f9b4 | 238 | struct gpio_device *prev, *next; |
1a989d0f | 239 | |
ff2b1359 | 240 | if (list_empty(&gpio_devices)) { |
a961f9b4 | 241 | /* initial entry in list */ |
ff2b1359 | 242 | list_add_tail(&gdev->list, &gpio_devices); |
e28ecca6 | 243 | return 0; |
1a989d0f AC |
244 | } |
245 | ||
a961f9b4 BJZ |
246 | next = list_entry(gpio_devices.next, struct gpio_device, list); |
247 | if (gdev->base + gdev->ngpio <= next->base) { | |
248 | /* add before first entry */ | |
249 | list_add(&gdev->list, &gpio_devices); | |
250 | return 0; | |
1a989d0f AC |
251 | } |
252 | ||
a961f9b4 BJZ |
253 | prev = list_entry(gpio_devices.prev, struct gpio_device, list); |
254 | if (prev->base + prev->ngpio <= gdev->base) { | |
255 | /* add behind last entry */ | |
256 | list_add_tail(&gdev->list, &gpio_devices); | |
96098df1 | 257 | return 0; |
1a989d0f AC |
258 | } |
259 | ||
a961f9b4 BJZ |
260 | list_for_each_entry_safe(prev, next, &gpio_devices, list) { |
261 | /* at the end of the list */ | |
262 | if (&next->list == &gpio_devices) | |
263 | break; | |
1a989d0f | 264 | |
a961f9b4 BJZ |
265 | /* add between prev and next */ |
266 | if (prev->base + prev->ngpio <= gdev->base | |
267 | && gdev->base + gdev->ngpio <= next->base) { | |
268 | list_add(&gdev->list, &prev->list); | |
269 | return 0; | |
270 | } | |
271 | } | |
272 | ||
273 | dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n"); | |
274 | return -EBUSY; | |
1a989d0f AC |
275 | } |
276 | ||
950d55f5 | 277 | /* |
f881bab0 LW |
278 | * Convert a GPIO name to its descriptor |
279 | */ | |
280 | static struct gpio_desc *gpio_name_to_desc(const char * const name) | |
281 | { | |
ff2b1359 | 282 | struct gpio_device *gdev; |
f881bab0 LW |
283 | unsigned long flags; |
284 | ||
285 | spin_lock_irqsave(&gpio_lock, flags); | |
286 | ||
ff2b1359 | 287 | list_for_each_entry(gdev, &gpio_devices, list) { |
f881bab0 LW |
288 | int i; |
289 | ||
fdeb8e15 LW |
290 | for (i = 0; i != gdev->ngpio; ++i) { |
291 | struct gpio_desc *desc = &gdev->descs[i]; | |
f881bab0 | 292 | |
fdeb8e15 | 293 | if (!desc->name || !name) |
f881bab0 LW |
294 | continue; |
295 | ||
fdeb8e15 | 296 | if (!strcmp(desc->name, name)) { |
f881bab0 | 297 | spin_unlock_irqrestore(&gpio_lock, flags); |
fdeb8e15 | 298 | return desc; |
f881bab0 LW |
299 | } |
300 | } | |
301 | } | |
302 | ||
303 | spin_unlock_irqrestore(&gpio_lock, flags); | |
304 | ||
305 | return NULL; | |
306 | } | |
307 | ||
5f3ca732 MP |
308 | /* |
309 | * Takes the names from gc->names and checks if they are all unique. If they | |
310 | * are, they are assigned to their gpio descriptors. | |
311 | * | |
ed37915c | 312 | * Warning if one of the names is already used for a different GPIO. |
5f3ca732 MP |
313 | */ |
314 | static int gpiochip_set_desc_names(struct gpio_chip *gc) | |
315 | { | |
fdeb8e15 | 316 | struct gpio_device *gdev = gc->gpiodev; |
5f3ca732 MP |
317 | int i; |
318 | ||
319 | if (!gc->names) | |
320 | return 0; | |
321 | ||
322 | /* First check all names if they are unique */ | |
323 | for (i = 0; i != gc->ngpio; ++i) { | |
324 | struct gpio_desc *gpio; | |
325 | ||
326 | gpio = gpio_name_to_desc(gc->names[i]); | |
f881bab0 | 327 | if (gpio) |
fdeb8e15 | 328 | dev_warn(&gdev->dev, |
34ffd85d | 329 | "Detected name collision for GPIO name '%s'\n", |
f881bab0 | 330 | gc->names[i]); |
5f3ca732 MP |
331 | } |
332 | ||
333 | /* Then add all names to the GPIO descriptors */ | |
334 | for (i = 0; i != gc->ngpio; ++i) | |
fdeb8e15 | 335 | gdev->descs[i].name = gc->names[i]; |
5f3ca732 MP |
336 | |
337 | return 0; | |
338 | } | |
339 | ||
d7c51b47 LW |
340 | /* |
341 | * GPIO line handle management | |
342 | */ | |
343 | ||
344 | /** | |
345 | * struct linehandle_state - contains the state of a userspace handle | |
346 | * @gdev: the GPIO device the handle pertains to | |
347 | * @label: consumer label used to tag descriptors | |
348 | * @descs: the GPIO descriptors held by this handle | |
349 | * @numdescs: the number of descriptors held in the descs array | |
350 | */ | |
351 | struct linehandle_state { | |
352 | struct gpio_device *gdev; | |
353 | const char *label; | |
354 | struct gpio_desc *descs[GPIOHANDLES_MAX]; | |
355 | u32 numdescs; | |
356 | }; | |
357 | ||
e3e847c7 LPC |
358 | #define GPIOHANDLE_REQUEST_VALID_FLAGS \ |
359 | (GPIOHANDLE_REQUEST_INPUT | \ | |
360 | GPIOHANDLE_REQUEST_OUTPUT | \ | |
361 | GPIOHANDLE_REQUEST_ACTIVE_LOW | \ | |
362 | GPIOHANDLE_REQUEST_OPEN_DRAIN | \ | |
363 | GPIOHANDLE_REQUEST_OPEN_SOURCE) | |
364 | ||
d7c51b47 LW |
365 | static long linehandle_ioctl(struct file *filep, unsigned int cmd, |
366 | unsigned long arg) | |
367 | { | |
368 | struct linehandle_state *lh = filep->private_data; | |
369 | void __user *ip = (void __user *)arg; | |
370 | struct gpiohandle_data ghd; | |
eec1d566 | 371 | int vals[GPIOHANDLES_MAX]; |
d7c51b47 LW |
372 | int i; |
373 | ||
374 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { | |
eec1d566 LW |
375 | /* TODO: check if descriptors are really input */ |
376 | int ret = gpiod_get_array_value_complex(false, | |
377 | true, | |
378 | lh->numdescs, | |
379 | lh->descs, | |
380 | vals); | |
381 | if (ret) | |
382 | return ret; | |
d7c51b47 | 383 | |
3eded5d8 | 384 | memset(&ghd, 0, sizeof(ghd)); |
eec1d566 LW |
385 | for (i = 0; i < lh->numdescs; i++) |
386 | ghd.values[i] = vals[i]; | |
d7c51b47 LW |
387 | |
388 | if (copy_to_user(ip, &ghd, sizeof(ghd))) | |
389 | return -EFAULT; | |
390 | ||
391 | return 0; | |
392 | } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) { | |
d7c51b47 LW |
393 | /* TODO: check if descriptors are really output */ |
394 | if (copy_from_user(&ghd, ip, sizeof(ghd))) | |
395 | return -EFAULT; | |
396 | ||
397 | /* Clamp all values to [0,1] */ | |
398 | for (i = 0; i < lh->numdescs; i++) | |
399 | vals[i] = !!ghd.values[i]; | |
400 | ||
401 | /* Reuse the array setting function */ | |
402 | gpiod_set_array_value_complex(false, | |
403 | true, | |
404 | lh->numdescs, | |
405 | lh->descs, | |
406 | vals); | |
407 | return 0; | |
408 | } | |
409 | return -EINVAL; | |
410 | } | |
411 | ||
412 | #ifdef CONFIG_COMPAT | |
413 | static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd, | |
414 | unsigned long arg) | |
415 | { | |
416 | return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg)); | |
417 | } | |
418 | #endif | |
419 | ||
420 | static int linehandle_release(struct inode *inode, struct file *filep) | |
421 | { | |
422 | struct linehandle_state *lh = filep->private_data; | |
423 | struct gpio_device *gdev = lh->gdev; | |
424 | int i; | |
425 | ||
426 | for (i = 0; i < lh->numdescs; i++) | |
427 | gpiod_free(lh->descs[i]); | |
428 | kfree(lh->label); | |
429 | kfree(lh); | |
430 | put_device(&gdev->dev); | |
431 | return 0; | |
432 | } | |
433 | ||
434 | static const struct file_operations linehandle_fileops = { | |
435 | .release = linehandle_release, | |
436 | .owner = THIS_MODULE, | |
437 | .llseek = noop_llseek, | |
438 | .unlocked_ioctl = linehandle_ioctl, | |
439 | #ifdef CONFIG_COMPAT | |
440 | .compat_ioctl = linehandle_ioctl_compat, | |
441 | #endif | |
442 | }; | |
443 | ||
444 | static int linehandle_create(struct gpio_device *gdev, void __user *ip) | |
445 | { | |
446 | struct gpiohandle_request handlereq; | |
447 | struct linehandle_state *lh; | |
953b956a | 448 | struct file *file; |
d7c51b47 | 449 | int fd, i, ret; |
418ee8e9 | 450 | u32 lflags; |
d7c51b47 LW |
451 | |
452 | if (copy_from_user(&handlereq, ip, sizeof(handlereq))) | |
453 | return -EFAULT; | |
454 | if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) | |
455 | return -EINVAL; | |
456 | ||
418ee8e9 BG |
457 | lflags = handlereq.flags; |
458 | ||
459 | /* Return an error if an unknown flag is set */ | |
460 | if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) | |
461 | return -EINVAL; | |
462 | ||
609aaf6a BG |
463 | /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */ |
464 | if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) && | |
465 | ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || | |
466 | (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))) | |
467 | return -EINVAL; | |
468 | ||
d7c51b47 LW |
469 | lh = kzalloc(sizeof(*lh), GFP_KERNEL); |
470 | if (!lh) | |
471 | return -ENOMEM; | |
472 | lh->gdev = gdev; | |
473 | get_device(&gdev->dev); | |
474 | ||
475 | /* Make sure this is terminated */ | |
476 | handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0'; | |
477 | if (strlen(handlereq.consumer_label)) { | |
478 | lh->label = kstrdup(handlereq.consumer_label, | |
479 | GFP_KERNEL); | |
480 | if (!lh->label) { | |
481 | ret = -ENOMEM; | |
482 | goto out_free_lh; | |
483 | } | |
484 | } | |
485 | ||
486 | /* Request each GPIO */ | |
487 | for (i = 0; i < handlereq.lines; i++) { | |
488 | u32 offset = handlereq.lineoffsets[i]; | |
d7c51b47 LW |
489 | struct gpio_desc *desc; |
490 | ||
e405f9fc LPC |
491 | if (offset >= gdev->ngpio) { |
492 | ret = -EINVAL; | |
493 | goto out_free_descs; | |
494 | } | |
495 | ||
d7c51b47 LW |
496 | desc = &gdev->descs[offset]; |
497 | ret = gpiod_request(desc, lh->label); | |
498 | if (ret) | |
499 | goto out_free_descs; | |
500 | lh->descs[i] = desc; | |
501 | ||
502 | if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW) | |
503 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
504 | if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) | |
505 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); | |
506 | if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE) | |
507 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); | |
508 | ||
509 | /* | |
510 | * Lines have to be requested explicitly for input | |
511 | * or output, else the line will be treated "as is". | |
512 | */ | |
513 | if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { | |
514 | int val = !!handlereq.default_values[i]; | |
515 | ||
516 | ret = gpiod_direction_output(desc, val); | |
517 | if (ret) | |
518 | goto out_free_descs; | |
519 | } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { | |
520 | ret = gpiod_direction_input(desc); | |
521 | if (ret) | |
522 | goto out_free_descs; | |
523 | } | |
524 | dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", | |
525 | offset); | |
526 | } | |
e2f608be LW |
527 | /* Let i point at the last handle */ |
528 | i--; | |
d7c51b47 LW |
529 | lh->numdescs = handlereq.lines; |
530 | ||
953b956a | 531 | fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); |
d7c51b47 LW |
532 | if (fd < 0) { |
533 | ret = fd; | |
534 | goto out_free_descs; | |
535 | } | |
536 | ||
953b956a LPC |
537 | file = anon_inode_getfile("gpio-linehandle", |
538 | &linehandle_fileops, | |
539 | lh, | |
540 | O_RDONLY | O_CLOEXEC); | |
541 | if (IS_ERR(file)) { | |
542 | ret = PTR_ERR(file); | |
543 | goto out_put_unused_fd; | |
544 | } | |
545 | ||
d7c51b47 | 546 | handlereq.fd = fd; |
d932cd49 | 547 | if (copy_to_user(ip, &handlereq, sizeof(handlereq))) { |
953b956a LPC |
548 | /* |
549 | * fput() will trigger the release() callback, so do not go onto | |
550 | * the regular error cleanup path here. | |
551 | */ | |
552 | fput(file); | |
553 | put_unused_fd(fd); | |
554 | return -EFAULT; | |
d932cd49 | 555 | } |
d7c51b47 | 556 | |
953b956a LPC |
557 | fd_install(fd, file); |
558 | ||
d7c51b47 LW |
559 | dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", |
560 | lh->numdescs); | |
561 | ||
562 | return 0; | |
563 | ||
953b956a LPC |
564 | out_put_unused_fd: |
565 | put_unused_fd(fd); | |
d7c51b47 LW |
566 | out_free_descs: |
567 | for (; i >= 0; i--) | |
568 | gpiod_free(lh->descs[i]); | |
569 | kfree(lh->label); | |
570 | out_free_lh: | |
571 | kfree(lh); | |
572 | put_device(&gdev->dev); | |
573 | return ret; | |
574 | } | |
575 | ||
61f922db LW |
576 | /* |
577 | * GPIO line event management | |
578 | */ | |
579 | ||
580 | /** | |
581 | * struct lineevent_state - contains the state of a userspace event | |
582 | * @gdev: the GPIO device the event pertains to | |
583 | * @label: consumer label used to tag descriptors | |
584 | * @desc: the GPIO descriptor held by this event | |
585 | * @eflags: the event flags this line was requested with | |
586 | * @irq: the interrupt that trigger in response to events on this GPIO | |
587 | * @wait: wait queue that handles blocking reads of events | |
588 | * @events: KFIFO for the GPIO events | |
589 | * @read_lock: mutex lock to protect reads from colliding with adding | |
590 | * new events to the FIFO | |
591 | */ | |
592 | struct lineevent_state { | |
593 | struct gpio_device *gdev; | |
594 | const char *label; | |
595 | struct gpio_desc *desc; | |
596 | u32 eflags; | |
597 | int irq; | |
598 | wait_queue_head_t wait; | |
599 | DECLARE_KFIFO(events, struct gpioevent_data, 16); | |
600 | struct mutex read_lock; | |
601 | }; | |
602 | ||
ac7dbb99 LPC |
603 | #define GPIOEVENT_REQUEST_VALID_FLAGS \ |
604 | (GPIOEVENT_REQUEST_RISING_EDGE | \ | |
605 | GPIOEVENT_REQUEST_FALLING_EDGE) | |
606 | ||
61f922db LW |
607 | static unsigned int lineevent_poll(struct file *filep, |
608 | struct poll_table_struct *wait) | |
609 | { | |
610 | struct lineevent_state *le = filep->private_data; | |
611 | unsigned int events = 0; | |
612 | ||
613 | poll_wait(filep, &le->wait, wait); | |
614 | ||
615 | if (!kfifo_is_empty(&le->events)) | |
616 | events = POLLIN | POLLRDNORM; | |
617 | ||
618 | return events; | |
619 | } | |
620 | ||
621 | ||
622 | static ssize_t lineevent_read(struct file *filep, | |
623 | char __user *buf, | |
624 | size_t count, | |
625 | loff_t *f_ps) | |
626 | { | |
627 | struct lineevent_state *le = filep->private_data; | |
628 | unsigned int copied; | |
629 | int ret; | |
630 | ||
631 | if (count < sizeof(struct gpioevent_data)) | |
632 | return -EINVAL; | |
633 | ||
634 | do { | |
635 | if (kfifo_is_empty(&le->events)) { | |
636 | if (filep->f_flags & O_NONBLOCK) | |
637 | return -EAGAIN; | |
638 | ||
639 | ret = wait_event_interruptible(le->wait, | |
640 | !kfifo_is_empty(&le->events)); | |
641 | if (ret) | |
642 | return ret; | |
643 | } | |
644 | ||
645 | if (mutex_lock_interruptible(&le->read_lock)) | |
646 | return -ERESTARTSYS; | |
647 | ret = kfifo_to_user(&le->events, buf, count, &copied); | |
648 | mutex_unlock(&le->read_lock); | |
649 | ||
650 | if (ret) | |
651 | return ret; | |
652 | ||
653 | /* | |
654 | * If we couldn't read anything from the fifo (a different | |
655 | * thread might have been faster) we either return -EAGAIN if | |
656 | * the file descriptor is non-blocking, otherwise we go back to | |
657 | * sleep and wait for more data to arrive. | |
658 | */ | |
659 | if (copied == 0 && (filep->f_flags & O_NONBLOCK)) | |
660 | return -EAGAIN; | |
661 | ||
662 | } while (copied == 0); | |
663 | ||
664 | return copied; | |
665 | } | |
666 | ||
667 | static int lineevent_release(struct inode *inode, struct file *filep) | |
668 | { | |
669 | struct lineevent_state *le = filep->private_data; | |
670 | struct gpio_device *gdev = le->gdev; | |
671 | ||
672 | free_irq(le->irq, le); | |
673 | gpiod_free(le->desc); | |
674 | kfree(le->label); | |
675 | kfree(le); | |
676 | put_device(&gdev->dev); | |
677 | return 0; | |
678 | } | |
679 | ||
680 | static long lineevent_ioctl(struct file *filep, unsigned int cmd, | |
681 | unsigned long arg) | |
682 | { | |
683 | struct lineevent_state *le = filep->private_data; | |
684 | void __user *ip = (void __user *)arg; | |
685 | struct gpiohandle_data ghd; | |
686 | ||
687 | /* | |
688 | * We can get the value for an event line but not set it, | |
689 | * because it is input by definition. | |
690 | */ | |
691 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { | |
692 | int val; | |
693 | ||
d82aa4a8 LPC |
694 | memset(&ghd, 0, sizeof(ghd)); |
695 | ||
61f922db LW |
696 | val = gpiod_get_value_cansleep(le->desc); |
697 | if (val < 0) | |
698 | return val; | |
699 | ghd.values[0] = val; | |
700 | ||
701 | if (copy_to_user(ip, &ghd, sizeof(ghd))) | |
702 | return -EFAULT; | |
703 | ||
704 | return 0; | |
705 | } | |
706 | return -EINVAL; | |
707 | } | |
708 | ||
709 | #ifdef CONFIG_COMPAT | |
710 | static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd, | |
711 | unsigned long arg) | |
712 | { | |
713 | return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg)); | |
714 | } | |
715 | #endif | |
716 | ||
717 | static const struct file_operations lineevent_fileops = { | |
718 | .release = lineevent_release, | |
719 | .read = lineevent_read, | |
720 | .poll = lineevent_poll, | |
721 | .owner = THIS_MODULE, | |
722 | .llseek = noop_llseek, | |
723 | .unlocked_ioctl = lineevent_ioctl, | |
724 | #ifdef CONFIG_COMPAT | |
725 | .compat_ioctl = lineevent_ioctl_compat, | |
726 | #endif | |
727 | }; | |
728 | ||
33265b17 | 729 | static irqreturn_t lineevent_irq_thread(int irq, void *p) |
61f922db LW |
730 | { |
731 | struct lineevent_state *le = p; | |
732 | struct gpioevent_data ge; | |
df1e76f2 | 733 | int ret, level; |
61f922db LW |
734 | |
735 | ge.timestamp = ktime_get_real_ns(); | |
df1e76f2 | 736 | level = gpiod_get_value_cansleep(le->desc); |
61f922db | 737 | |
ad537b82 BG |
738 | if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE |
739 | && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { | |
61f922db LW |
740 | if (level) |
741 | /* Emit low-to-high event */ | |
742 | ge.id = GPIOEVENT_EVENT_RISING_EDGE; | |
743 | else | |
744 | /* Emit high-to-low event */ | |
745 | ge.id = GPIOEVENT_EVENT_FALLING_EDGE; | |
df1e76f2 | 746 | } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) { |
61f922db LW |
747 | /* Emit low-to-high event */ |
748 | ge.id = GPIOEVENT_EVENT_RISING_EDGE; | |
df1e76f2 | 749 | } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) { |
61f922db LW |
750 | /* Emit high-to-low event */ |
751 | ge.id = GPIOEVENT_EVENT_FALLING_EDGE; | |
bc0207a5 AB |
752 | } else { |
753 | return IRQ_NONE; | |
61f922db LW |
754 | } |
755 | ||
756 | ret = kfifo_put(&le->events, ge); | |
757 | if (ret != 0) | |
758 | wake_up_poll(&le->wait, POLLIN); | |
759 | ||
760 | return IRQ_HANDLED; | |
761 | } | |
762 | ||
763 | static int lineevent_create(struct gpio_device *gdev, void __user *ip) | |
764 | { | |
765 | struct gpioevent_request eventreq; | |
766 | struct lineevent_state *le; | |
767 | struct gpio_desc *desc; | |
953b956a | 768 | struct file *file; |
61f922db LW |
769 | u32 offset; |
770 | u32 lflags; | |
771 | u32 eflags; | |
772 | int fd; | |
773 | int ret; | |
774 | int irqflags = 0; | |
775 | ||
776 | if (copy_from_user(&eventreq, ip, sizeof(eventreq))) | |
777 | return -EFAULT; | |
778 | ||
779 | le = kzalloc(sizeof(*le), GFP_KERNEL); | |
780 | if (!le) | |
781 | return -ENOMEM; | |
782 | le->gdev = gdev; | |
783 | get_device(&gdev->dev); | |
784 | ||
785 | /* Make sure this is terminated */ | |
786 | eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0'; | |
787 | if (strlen(eventreq.consumer_label)) { | |
788 | le->label = kstrdup(eventreq.consumer_label, | |
789 | GFP_KERNEL); | |
790 | if (!le->label) { | |
791 | ret = -ENOMEM; | |
792 | goto out_free_le; | |
793 | } | |
794 | } | |
795 | ||
796 | offset = eventreq.lineoffset; | |
797 | lflags = eventreq.handleflags; | |
798 | eflags = eventreq.eventflags; | |
799 | ||
b8b0e3d3 LPC |
800 | if (offset >= gdev->ngpio) { |
801 | ret = -EINVAL; | |
802 | goto out_free_label; | |
803 | } | |
804 | ||
ac7dbb99 LPC |
805 | /* Return an error if a unknown flag is set */ |
806 | if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || | |
807 | (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) { | |
808 | ret = -EINVAL; | |
809 | goto out_free_label; | |
810 | } | |
811 | ||
61f922db LW |
812 | /* This is just wrong: we don't look for events on output lines */ |
813 | if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { | |
814 | ret = -EINVAL; | |
815 | goto out_free_label; | |
816 | } | |
817 | ||
818 | desc = &gdev->descs[offset]; | |
819 | ret = gpiod_request(desc, le->label); | |
820 | if (ret) | |
821 | goto out_free_desc; | |
822 | le->desc = desc; | |
823 | le->eflags = eflags; | |
824 | ||
825 | if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW) | |
826 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
827 | if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) | |
828 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); | |
829 | if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE) | |
830 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); | |
831 | ||
832 | ret = gpiod_direction_input(desc); | |
833 | if (ret) | |
834 | goto out_free_desc; | |
835 | ||
836 | le->irq = gpiod_to_irq(desc); | |
837 | if (le->irq <= 0) { | |
838 | ret = -ENODEV; | |
839 | goto out_free_desc; | |
840 | } | |
841 | ||
842 | if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) | |
843 | irqflags |= IRQF_TRIGGER_RISING; | |
844 | if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) | |
845 | irqflags |= IRQF_TRIGGER_FALLING; | |
846 | irqflags |= IRQF_ONESHOT; | |
847 | irqflags |= IRQF_SHARED; | |
848 | ||
849 | INIT_KFIFO(le->events); | |
850 | init_waitqueue_head(&le->wait); | |
851 | mutex_init(&le->read_lock); | |
852 | ||
853 | /* Request a thread to read the events */ | |
854 | ret = request_threaded_irq(le->irq, | |
855 | NULL, | |
856 | lineevent_irq_thread, | |
857 | irqflags, | |
858 | le->label, | |
859 | le); | |
860 | if (ret) | |
861 | goto out_free_desc; | |
862 | ||
953b956a | 863 | fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); |
61f922db LW |
864 | if (fd < 0) { |
865 | ret = fd; | |
866 | goto out_free_irq; | |
867 | } | |
868 | ||
953b956a LPC |
869 | file = anon_inode_getfile("gpio-event", |
870 | &lineevent_fileops, | |
871 | le, | |
872 | O_RDONLY | O_CLOEXEC); | |
873 | if (IS_ERR(file)) { | |
874 | ret = PTR_ERR(file); | |
875 | goto out_put_unused_fd; | |
876 | } | |
877 | ||
61f922db | 878 | eventreq.fd = fd; |
d932cd49 | 879 | if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { |
953b956a LPC |
880 | /* |
881 | * fput() will trigger the release() callback, so do not go onto | |
882 | * the regular error cleanup path here. | |
883 | */ | |
884 | fput(file); | |
885 | put_unused_fd(fd); | |
886 | return -EFAULT; | |
d932cd49 | 887 | } |
61f922db | 888 | |
953b956a LPC |
889 | fd_install(fd, file); |
890 | ||
61f922db LW |
891 | return 0; |
892 | ||
953b956a LPC |
893 | out_put_unused_fd: |
894 | put_unused_fd(fd); | |
61f922db LW |
895 | out_free_irq: |
896 | free_irq(le->irq, le); | |
897 | out_free_desc: | |
898 | gpiod_free(le->desc); | |
899 | out_free_label: | |
900 | kfree(le->label); | |
901 | out_free_le: | |
902 | kfree(le); | |
903 | put_device(&gdev->dev); | |
904 | return ret; | |
905 | } | |
906 | ||
950d55f5 | 907 | /* |
3c702e99 LW |
908 | * gpio_ioctl() - ioctl handler for the GPIO chardev |
909 | */ | |
910 | static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) | |
911 | { | |
912 | struct gpio_device *gdev = filp->private_data; | |
913 | struct gpio_chip *chip = gdev->chip; | |
8b92e17e | 914 | void __user *ip = (void __user *)arg; |
3c702e99 LW |
915 | |
916 | /* We fail any subsequent ioctl():s when the chip is gone */ | |
917 | if (!chip) | |
918 | return -ENODEV; | |
919 | ||
521a2ad6 | 920 | /* Fill in the struct and pass to userspace */ |
3c702e99 | 921 | if (cmd == GPIO_GET_CHIPINFO_IOCTL) { |
521a2ad6 LW |
922 | struct gpiochip_info chipinfo; |
923 | ||
0f4bbb23 LPC |
924 | memset(&chipinfo, 0, sizeof(chipinfo)); |
925 | ||
3c702e99 LW |
926 | strncpy(chipinfo.name, dev_name(&gdev->dev), |
927 | sizeof(chipinfo.name)); | |
928 | chipinfo.name[sizeof(chipinfo.name)-1] = '\0'; | |
df4878e9 LW |
929 | strncpy(chipinfo.label, gdev->label, |
930 | sizeof(chipinfo.label)); | |
931 | chipinfo.label[sizeof(chipinfo.label)-1] = '\0'; | |
fdeb8e15 | 932 | chipinfo.lines = gdev->ngpio; |
3c702e99 LW |
933 | if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) |
934 | return -EFAULT; | |
935 | return 0; | |
521a2ad6 LW |
936 | } else if (cmd == GPIO_GET_LINEINFO_IOCTL) { |
937 | struct gpioline_info lineinfo; | |
938 | struct gpio_desc *desc; | |
939 | ||
940 | if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) | |
941 | return -EFAULT; | |
1f1cc456 | 942 | if (lineinfo.line_offset >= gdev->ngpio) |
521a2ad6 LW |
943 | return -EINVAL; |
944 | ||
945 | desc = &gdev->descs[lineinfo.line_offset]; | |
946 | if (desc->name) { | |
947 | strncpy(lineinfo.name, desc->name, | |
948 | sizeof(lineinfo.name)); | |
949 | lineinfo.name[sizeof(lineinfo.name)-1] = '\0'; | |
950 | } else { | |
951 | lineinfo.name[0] = '\0'; | |
952 | } | |
953 | if (desc->label) { | |
214338e3 LW |
954 | strncpy(lineinfo.consumer, desc->label, |
955 | sizeof(lineinfo.consumer)); | |
956 | lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0'; | |
521a2ad6 | 957 | } else { |
214338e3 | 958 | lineinfo.consumer[0] = '\0'; |
521a2ad6 LW |
959 | } |
960 | ||
961 | /* | |
962 | * Userspace only need to know that the kernel is using | |
963 | * this GPIO so it can't use it. | |
964 | */ | |
965 | lineinfo.flags = 0; | |
9d8cc89c LW |
966 | if (test_bit(FLAG_REQUESTED, &desc->flags) || |
967 | test_bit(FLAG_IS_HOGGED, &desc->flags) || | |
968 | test_bit(FLAG_USED_AS_IRQ, &desc->flags) || | |
969 | test_bit(FLAG_EXPORT, &desc->flags) || | |
970 | test_bit(FLAG_SYSFS, &desc->flags)) | |
521a2ad6 | 971 | lineinfo.flags |= GPIOLINE_FLAG_KERNEL; |
9d8cc89c | 972 | if (test_bit(FLAG_IS_OUT, &desc->flags)) |
521a2ad6 | 973 | lineinfo.flags |= GPIOLINE_FLAG_IS_OUT; |
9d8cc89c | 974 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
521a2ad6 | 975 | lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW; |
9d8cc89c | 976 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
521a2ad6 | 977 | lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN; |
9d8cc89c | 978 | if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
521a2ad6 LW |
979 | lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE; |
980 | ||
981 | if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) | |
982 | return -EFAULT; | |
983 | return 0; | |
d7c51b47 LW |
984 | } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) { |
985 | return linehandle_create(gdev, ip); | |
61f922db LW |
986 | } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) { |
987 | return lineevent_create(gdev, ip); | |
3c702e99 LW |
988 | } |
989 | return -EINVAL; | |
990 | } | |
991 | ||
8b92e17e LW |
992 | #ifdef CONFIG_COMPAT |
993 | static long gpio_ioctl_compat(struct file *filp, unsigned int cmd, | |
994 | unsigned long arg) | |
995 | { | |
996 | return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); | |
997 | } | |
998 | #endif | |
999 | ||
3c702e99 LW |
1000 | /** |
1001 | * gpio_chrdev_open() - open the chardev for ioctl operations | |
1002 | * @inode: inode for this chardev | |
1003 | * @filp: file struct for storing private data | |
1004 | * Returns 0 on success | |
1005 | */ | |
1006 | static int gpio_chrdev_open(struct inode *inode, struct file *filp) | |
1007 | { | |
1008 | struct gpio_device *gdev = container_of(inode->i_cdev, | |
1009 | struct gpio_device, chrdev); | |
1010 | ||
1011 | /* Fail on open if the backing gpiochip is gone */ | |
fb505747 | 1012 | if (!gdev->chip) |
3c702e99 LW |
1013 | return -ENODEV; |
1014 | get_device(&gdev->dev); | |
1015 | filp->private_data = gdev; | |
f4e81c52 LPC |
1016 | |
1017 | return nonseekable_open(inode, filp); | |
3c702e99 LW |
1018 | } |
1019 | ||
1020 | /** | |
1021 | * gpio_chrdev_release() - close chardev after ioctl operations | |
1022 | * @inode: inode for this chardev | |
1023 | * @filp: file struct for storing private data | |
1024 | * Returns 0 on success | |
1025 | */ | |
1026 | static int gpio_chrdev_release(struct inode *inode, struct file *filp) | |
1027 | { | |
1028 | struct gpio_device *gdev = container_of(inode->i_cdev, | |
1029 | struct gpio_device, chrdev); | |
1030 | ||
3c702e99 LW |
1031 | put_device(&gdev->dev); |
1032 | return 0; | |
1033 | } | |
1034 | ||
1035 | ||
1036 | static const struct file_operations gpio_fileops = { | |
1037 | .release = gpio_chrdev_release, | |
1038 | .open = gpio_chrdev_open, | |
1039 | .owner = THIS_MODULE, | |
f4e81c52 | 1040 | .llseek = no_llseek, |
3c702e99 | 1041 | .unlocked_ioctl = gpio_ioctl, |
8b92e17e LW |
1042 | #ifdef CONFIG_COMPAT |
1043 | .compat_ioctl = gpio_ioctl_compat, | |
1044 | #endif | |
3c702e99 LW |
1045 | }; |
1046 | ||
ff2b1359 LW |
1047 | static void gpiodevice_release(struct device *dev) |
1048 | { | |
1049 | struct gpio_device *gdev = dev_get_drvdata(dev); | |
1050 | ||
1051 | list_del(&gdev->list); | |
1052 | ida_simple_remove(&gpio_ida, gdev->id); | |
476e2fc5 GR |
1053 | kfree(gdev->label); |
1054 | kfree(gdev->descs); | |
9efd9e69 | 1055 | kfree(gdev); |
ff2b1359 LW |
1056 | } |
1057 | ||
159f3cd9 GR |
1058 | static int gpiochip_setup_dev(struct gpio_device *gdev) |
1059 | { | |
1060 | int status; | |
1061 | ||
1062 | cdev_init(&gdev->chrdev, &gpio_fileops); | |
1063 | gdev->chrdev.owner = THIS_MODULE; | |
159f3cd9 | 1064 | gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id); |
111379dc LG |
1065 | |
1066 | status = cdev_device_add(&gdev->chrdev, &gdev->dev); | |
159f3cd9 | 1067 | if (status) |
111379dc LG |
1068 | return status; |
1069 | ||
1070 | chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", | |
1071 | MAJOR(gpio_devt), gdev->id); | |
159f3cd9 GR |
1072 | |
1073 | status = gpiochip_sysfs_register(gdev); | |
1074 | if (status) | |
1075 | goto err_remove_device; | |
1076 | ||
1077 | /* From this point, the .release() function cleans up gpio_device */ | |
1078 | gdev->dev.release = gpiodevice_release; | |
159f3cd9 GR |
1079 | pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n", |
1080 | __func__, gdev->base, gdev->base + gdev->ngpio - 1, | |
1081 | dev_name(&gdev->dev), gdev->chip->label ? : "generic"); | |
1082 | ||
1083 | return 0; | |
1084 | ||
1085 | err_remove_device: | |
111379dc | 1086 | cdev_device_del(&gdev->chrdev, &gdev->dev); |
159f3cd9 GR |
1087 | return status; |
1088 | } | |
1089 | ||
1090 | static void gpiochip_setup_devs(void) | |
1091 | { | |
1092 | struct gpio_device *gdev; | |
1093 | int err; | |
1094 | ||
1095 | list_for_each_entry(gdev, &gpio_devices, list) { | |
1096 | err = gpiochip_setup_dev(gdev); | |
1097 | if (err) | |
1098 | pr_err("%s: Failed to initialize gpio device (%d)\n", | |
1099 | dev_name(&gdev->dev), err); | |
1100 | } | |
1101 | } | |
1102 | ||
959bc7b2 | 1103 | int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data, |
39c3fd58 AL |
1104 | struct lock_class_key *lock_key, |
1105 | struct lock_class_key *request_key) | |
d2876d08 DB |
1106 | { |
1107 | unsigned long flags; | |
1108 | int status = 0; | |
ff2b1359 | 1109 | unsigned i; |
8d0aab2f | 1110 | int base = chip->base; |
ff2b1359 | 1111 | struct gpio_device *gdev; |
d2876d08 | 1112 | |
ff2b1359 LW |
1113 | /* |
1114 | * First: allocate and populate the internal stat container, and | |
1115 | * set up the struct device. | |
1116 | */ | |
969f07b4 | 1117 | gdev = kzalloc(sizeof(*gdev), GFP_KERNEL); |
ff2b1359 | 1118 | if (!gdev) |
14e85c0e | 1119 | return -ENOMEM; |
3c702e99 | 1120 | gdev->dev.bus = &gpio_bus_type; |
ff2b1359 LW |
1121 | gdev->chip = chip; |
1122 | chip->gpiodev = gdev; | |
1123 | if (chip->parent) { | |
1124 | gdev->dev.parent = chip->parent; | |
1125 | gdev->dev.of_node = chip->parent->of_node; | |
acc6e331 TR |
1126 | } |
1127 | ||
ff2b1359 LW |
1128 | #ifdef CONFIG_OF_GPIO |
1129 | /* If the gpiochip has an assigned OF node this takes precedence */ | |
acc6e331 TR |
1130 | if (chip->of_node) |
1131 | gdev->dev.of_node = chip->of_node; | |
ff2b1359 | 1132 | #endif |
acc6e331 | 1133 | |
ff2b1359 LW |
1134 | gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL); |
1135 | if (gdev->id < 0) { | |
1136 | status = gdev->id; | |
1137 | goto err_free_gdev; | |
1138 | } | |
1139 | dev_set_name(&gdev->dev, "gpiochip%d", gdev->id); | |
1140 | device_initialize(&gdev->dev); | |
1141 | dev_set_drvdata(&gdev->dev, gdev); | |
1142 | if (chip->parent && chip->parent->driver) | |
1143 | gdev->owner = chip->parent->driver->owner; | |
1144 | else if (chip->owner) | |
1145 | /* TODO: remove chip->owner */ | |
1146 | gdev->owner = chip->owner; | |
1147 | else | |
1148 | gdev->owner = THIS_MODULE; | |
d2876d08 | 1149 | |
476e2fc5 | 1150 | gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL); |
1c3cdb18 | 1151 | if (!gdev->descs) { |
ff2b1359 LW |
1152 | status = -ENOMEM; |
1153 | goto err_free_gdev; | |
1154 | } | |
1155 | ||
5ed41cc4 BJZ |
1156 | if (chip->ngpio == 0) { |
1157 | chip_err(chip, "tried to insert a GPIO chip with zero lines\n"); | |
ff2b1359 | 1158 | status = -EINVAL; |
159f3cd9 | 1159 | goto err_free_descs; |
5ed41cc4 | 1160 | } |
df4878e9 LW |
1161 | |
1162 | if (chip->label) | |
476e2fc5 | 1163 | gdev->label = kstrdup(chip->label, GFP_KERNEL); |
df4878e9 | 1164 | else |
476e2fc5 | 1165 | gdev->label = kstrdup("unknown", GFP_KERNEL); |
df4878e9 LW |
1166 | if (!gdev->label) { |
1167 | status = -ENOMEM; | |
476e2fc5 | 1168 | goto err_free_descs; |
df4878e9 LW |
1169 | } |
1170 | ||
fdeb8e15 | 1171 | gdev->ngpio = chip->ngpio; |
43c54eca | 1172 | gdev->data = data; |
5ed41cc4 | 1173 | |
d2876d08 DB |
1174 | spin_lock_irqsave(&gpio_lock, flags); |
1175 | ||
fdeb8e15 LW |
1176 | /* |
1177 | * TODO: this allocates a Linux GPIO number base in the global | |
1178 | * GPIO numberspace for this chip. In the long run we want to | |
1179 | * get *rid* of this numberspace and use only descriptors, but | |
1180 | * it may be a pipe dream. It will not happen before we get rid | |
1181 | * of the sysfs interface anyways. | |
1182 | */ | |
8d0aab2f AV |
1183 | if (base < 0) { |
1184 | base = gpiochip_find_base(chip->ngpio); | |
1185 | if (base < 0) { | |
1186 | status = base; | |
225fce83 | 1187 | spin_unlock_irqrestore(&gpio_lock, flags); |
476e2fc5 | 1188 | goto err_free_label; |
8d0aab2f | 1189 | } |
fdeb8e15 LW |
1190 | /* |
1191 | * TODO: it should not be necessary to reflect the assigned | |
1192 | * base outside of the GPIO subsystem. Go over drivers and | |
1193 | * see if anyone makes use of this, else drop this and assign | |
1194 | * a poison instead. | |
1195 | */ | |
8d0aab2f AV |
1196 | chip->base = base; |
1197 | } | |
fdeb8e15 | 1198 | gdev->base = base; |
8d0aab2f | 1199 | |
ff2b1359 | 1200 | status = gpiodev_add_to_list(gdev); |
05aa5203 JH |
1201 | if (status) { |
1202 | spin_unlock_irqrestore(&gpio_lock, flags); | |
476e2fc5 | 1203 | goto err_free_label; |
05aa5203 | 1204 | } |
1a989d0f | 1205 | |
545ebd9a LW |
1206 | spin_unlock_irqrestore(&gpio_lock, flags); |
1207 | ||
ff2b1359 | 1208 | for (i = 0; i < chip->ngpio; i++) { |
1c3cdb18 | 1209 | struct gpio_desc *desc = &gdev->descs[i]; |
05aa5203 | 1210 | |
fdeb8e15 | 1211 | desc->gdev = gdev; |
72d32000 LW |
1212 | /* |
1213 | * REVISIT: most hardware initializes GPIOs as inputs | |
1214 | * (often with pullups enabled) so power usage is | |
1215 | * minimized. Linux code should set the gpio direction | |
1216 | * first thing; but until it does, and in case | |
1217 | * chip->get_direction is not set, we may expose the | |
1218 | * wrong direction in sysfs. | |
05aa5203 | 1219 | */ |
72d32000 LW |
1220 | |
1221 | if (chip->get_direction) { | |
1222 | /* | |
1223 | * If we have .get_direction, set up the initial | |
1224 | * direction flag from the hardware. | |
1225 | */ | |
1226 | int dir = chip->get_direction(chip, i); | |
1227 | ||
1228 | if (!dir) | |
1229 | set_bit(FLAG_IS_OUT, &desc->flags); | |
1230 | } else if (!chip->direction_input) { | |
1231 | /* | |
1232 | * If the chip lacks the .direction_input callback | |
1233 | * we logically assume all lines are outputs. | |
1234 | */ | |
1235 | set_bit(FLAG_IS_OUT, &desc->flags); | |
1236 | } | |
d2876d08 | 1237 | } |
14e85c0e | 1238 | |
f23f1516 | 1239 | #ifdef CONFIG_PINCTRL |
20ec3e39 | 1240 | INIT_LIST_HEAD(&gdev->pin_ranges); |
f23f1516 SH |
1241 | #endif |
1242 | ||
5f3ca732 MP |
1243 | status = gpiochip_set_desc_names(chip); |
1244 | if (status) | |
1245 | goto err_remove_from_list; | |
1246 | ||
79b804cb MW |
1247 | status = gpiochip_irqchip_init_valid_mask(chip); |
1248 | if (status) | |
1249 | goto err_remove_from_list; | |
1250 | ||
39c3fd58 | 1251 | status = gpiochip_add_irqchip(chip, lock_key, request_key); |
e0d89728 TR |
1252 | if (status) |
1253 | goto err_remove_chip; | |
1254 | ||
28355f81 TV |
1255 | status = of_gpiochip_add(chip); |
1256 | if (status) | |
1257 | goto err_remove_chip; | |
1258 | ||
664e3e5a | 1259 | acpi_gpiochip_add(chip); |
391c970c | 1260 | |
3c702e99 LW |
1261 | /* |
1262 | * By first adding the chardev, and then adding the device, | |
1263 | * we get a device node entry in sysfs under | |
1264 | * /sys/bus/gpio/devices/gpiochipN/dev that can be used for | |
1265 | * coldplug of device nodes and other udev business. | |
159f3cd9 GR |
1266 | * We can do this only if gpiolib has been initialized. |
1267 | * Otherwise, defer until later. | |
3c702e99 | 1268 | */ |
159f3cd9 GR |
1269 | if (gpiolib_initialized) { |
1270 | status = gpiochip_setup_dev(gdev); | |
1271 | if (status) | |
1272 | goto err_remove_chip; | |
1273 | } | |
cedb1881 | 1274 | return 0; |
3bae4811 | 1275 | |
225fce83 JH |
1276 | err_remove_chip: |
1277 | acpi_gpiochip_remove(chip); | |
6d86750c | 1278 | gpiochip_free_hogs(chip); |
225fce83 | 1279 | of_gpiochip_remove(chip); |
79b804cb | 1280 | gpiochip_irqchip_free_valid_mask(chip); |
5f3ca732 | 1281 | err_remove_from_list: |
225fce83 | 1282 | spin_lock_irqsave(&gpio_lock, flags); |
ff2b1359 | 1283 | list_del(&gdev->list); |
3bae4811 | 1284 | spin_unlock_irqrestore(&gpio_lock, flags); |
476e2fc5 GR |
1285 | err_free_label: |
1286 | kfree(gdev->label); | |
1287 | err_free_descs: | |
1288 | kfree(gdev->descs); | |
ff2b1359 LW |
1289 | err_free_gdev: |
1290 | ida_simple_remove(&gpio_ida, gdev->id); | |
d2876d08 | 1291 | /* failures here can mean systems won't boot... */ |
7589e59f | 1292 | pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__, |
fdeb8e15 LW |
1293 | gdev->base, gdev->base + gdev->ngpio - 1, |
1294 | chip->label ? : "generic"); | |
1295 | kfree(gdev); | |
d2876d08 DB |
1296 | return status; |
1297 | } | |
959bc7b2 | 1298 | EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key); |
d2876d08 | 1299 | |
43c54eca LW |
1300 | /** |
1301 | * gpiochip_get_data() - get per-subdriver data for the chip | |
950d55f5 TR |
1302 | * @chip: GPIO chip |
1303 | * | |
1304 | * Returns: | |
1305 | * The per-subdriver data for the chip. | |
43c54eca LW |
1306 | */ |
1307 | void *gpiochip_get_data(struct gpio_chip *chip) | |
1308 | { | |
1309 | return chip->gpiodev->data; | |
1310 | } | |
1311 | EXPORT_SYMBOL_GPL(gpiochip_get_data); | |
1312 | ||
d2876d08 DB |
1313 | /** |
1314 | * gpiochip_remove() - unregister a gpio_chip | |
1315 | * @chip: the chip to unregister | |
1316 | * | |
1317 | * A gpio_chip with any GPIOs still requested may not be removed. | |
1318 | */ | |
e1db1706 | 1319 | void gpiochip_remove(struct gpio_chip *chip) |
d2876d08 | 1320 | { |
ff2b1359 | 1321 | struct gpio_device *gdev = chip->gpiodev; |
fab28b89 | 1322 | struct gpio_desc *desc; |
d2876d08 | 1323 | unsigned long flags; |
1c3cdb18 | 1324 | unsigned i; |
fab28b89 | 1325 | bool requested = false; |
d2876d08 | 1326 | |
ff2b1359 | 1327 | /* FIXME: should the legacy sysfs handling be moved to gpio_device? */ |
afbc4f31 | 1328 | gpiochip_sysfs_unregister(gdev); |
5018ada6 | 1329 | gpiochip_free_hogs(chip); |
bd203bd5 BJZ |
1330 | /* Numb the device, cancelling all outstanding operations */ |
1331 | gdev->chip = NULL; | |
00acc3dc | 1332 | gpiochip_irqchip_remove(chip); |
6072b9dc | 1333 | acpi_gpiochip_remove(chip); |
9ef0d6f7 | 1334 | gpiochip_remove_pin_ranges(chip); |
391c970c | 1335 | of_gpiochip_remove(chip); |
43c54eca LW |
1336 | /* |
1337 | * We accept no more calls into the driver from this point, so | |
1338 | * NULL the driver data pointer | |
1339 | */ | |
1340 | gdev->data = NULL; | |
391c970c | 1341 | |
6798acaa | 1342 | spin_lock_irqsave(&gpio_lock, flags); |
fdeb8e15 | 1343 | for (i = 0; i < gdev->ngpio; i++) { |
1c3cdb18 | 1344 | desc = &gdev->descs[i]; |
fab28b89 JH |
1345 | if (test_bit(FLAG_REQUESTED, &desc->flags)) |
1346 | requested = true; | |
d2876d08 | 1347 | } |
d2876d08 | 1348 | spin_unlock_irqrestore(&gpio_lock, flags); |
14e85c0e | 1349 | |
fab28b89 | 1350 | if (requested) |
fdeb8e15 | 1351 | dev_crit(&gdev->dev, |
58383c78 | 1352 | "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); |
fab28b89 | 1353 | |
ff2b1359 LW |
1354 | /* |
1355 | * The gpiochip side puts its use of the device to rest here: | |
1356 | * if there are no userspace clients, the chardev and device will | |
1357 | * be removed, else it will be dangling until the last user is | |
1358 | * gone. | |
1359 | */ | |
111379dc | 1360 | cdev_device_del(&gdev->chrdev, &gdev->dev); |
ff2b1359 | 1361 | put_device(&gdev->dev); |
d2876d08 DB |
1362 | } |
1363 | EXPORT_SYMBOL_GPL(gpiochip_remove); | |
1364 | ||
0cf3292c LD |
1365 | static void devm_gpio_chip_release(struct device *dev, void *res) |
1366 | { | |
1367 | struct gpio_chip *chip = *(struct gpio_chip **)res; | |
1368 | ||
1369 | gpiochip_remove(chip); | |
1370 | } | |
1371 | ||
1372 | static int devm_gpio_chip_match(struct device *dev, void *res, void *data) | |
1373 | ||
1374 | { | |
1375 | struct gpio_chip **r = res; | |
1376 | ||
1377 | if (!r || !*r) { | |
1378 | WARN_ON(!r || !*r); | |
1379 | return 0; | |
1380 | } | |
1381 | ||
1382 | return *r == data; | |
1383 | } | |
1384 | ||
1385 | /** | |
1386 | * devm_gpiochip_add_data() - Resource manager piochip_add_data() | |
1387 | * @dev: the device pointer on which irq_chip belongs to. | |
1388 | * @chip: the chip to register, with chip->base initialized | |
950d55f5 | 1389 | * @data: driver-private data associated with this chip |
0cf3292c | 1390 | * |
950d55f5 | 1391 | * Context: potentially before irqs will work |
0cf3292c LD |
1392 | * |
1393 | * The gpio chip automatically be released when the device is unbound. | |
950d55f5 TR |
1394 | * |
1395 | * Returns: | |
1396 | * A negative errno if the chip can't be registered, such as because the | |
1397 | * chip->base is invalid or already associated with a different chip. | |
1398 | * Otherwise it returns zero as a success code. | |
0cf3292c LD |
1399 | */ |
1400 | int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip, | |
1401 | void *data) | |
1402 | { | |
1403 | struct gpio_chip **ptr; | |
1404 | int ret; | |
1405 | ||
1406 | ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr), | |
1407 | GFP_KERNEL); | |
1408 | if (!ptr) | |
1409 | return -ENOMEM; | |
1410 | ||
1411 | ret = gpiochip_add_data(chip, data); | |
1412 | if (ret < 0) { | |
1413 | devres_free(ptr); | |
1414 | return ret; | |
1415 | } | |
1416 | ||
1417 | *ptr = chip; | |
1418 | devres_add(dev, ptr); | |
1419 | ||
1420 | return 0; | |
1421 | } | |
1422 | EXPORT_SYMBOL_GPL(devm_gpiochip_add_data); | |
1423 | ||
1424 | /** | |
1425 | * devm_gpiochip_remove() - Resource manager of gpiochip_remove() | |
1426 | * @dev: device for which which resource was allocated | |
1427 | * @chip: the chip to remove | |
1428 | * | |
1429 | * A gpio_chip with any GPIOs still requested may not be removed. | |
1430 | */ | |
1431 | void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip) | |
1432 | { | |
1433 | int ret; | |
1434 | ||
1435 | ret = devres_release(dev, devm_gpio_chip_release, | |
1436 | devm_gpio_chip_match, chip); | |
3988d663 | 1437 | WARN_ON(ret); |
0cf3292c LD |
1438 | } |
1439 | EXPORT_SYMBOL_GPL(devm_gpiochip_remove); | |
1440 | ||
594fa265 GL |
1441 | /** |
1442 | * gpiochip_find() - iterator for locating a specific gpio_chip | |
1443 | * @data: data to pass to match function | |
950d55f5 | 1444 | * @match: Callback function to check gpio_chip |
594fa265 GL |
1445 | * |
1446 | * Similar to bus_find_device. It returns a reference to a gpio_chip as | |
1447 | * determined by a user supplied @match callback. The callback should return | |
1448 | * 0 if the device doesn't match and non-zero if it does. If the callback is | |
1449 | * non-zero, this function will return to the caller and not iterate over any | |
1450 | * more gpio_chips. | |
1451 | */ | |
07ce8ec7 | 1452 | struct gpio_chip *gpiochip_find(void *data, |
6e2cf651 | 1453 | int (*match)(struct gpio_chip *chip, |
3d0f7cf0 | 1454 | void *data)) |
594fa265 | 1455 | { |
ff2b1359 | 1456 | struct gpio_device *gdev; |
acf06ff7 | 1457 | struct gpio_chip *chip = NULL; |
594fa265 | 1458 | unsigned long flags; |
594fa265 GL |
1459 | |
1460 | spin_lock_irqsave(&gpio_lock, flags); | |
ff2b1359 | 1461 | list_for_each_entry(gdev, &gpio_devices, list) |
acf06ff7 MY |
1462 | if (gdev->chip && match(gdev->chip, data)) { |
1463 | chip = gdev->chip; | |
594fa265 | 1464 | break; |
acf06ff7 | 1465 | } |
ff2b1359 | 1466 | |
594fa265 GL |
1467 | spin_unlock_irqrestore(&gpio_lock, flags); |
1468 | ||
1469 | return chip; | |
1470 | } | |
8fa0c9bf | 1471 | EXPORT_SYMBOL_GPL(gpiochip_find); |
d2876d08 | 1472 | |
79697ef9 AC |
1473 | static int gpiochip_match_name(struct gpio_chip *chip, void *data) |
1474 | { | |
1475 | const char *name = data; | |
1476 | ||
1477 | return !strcmp(chip->label, name); | |
1478 | } | |
1479 | ||
1480 | static struct gpio_chip *find_chip_by_name(const char *name) | |
1481 | { | |
1482 | return gpiochip_find((void *)name, gpiochip_match_name); | |
1483 | } | |
1484 | ||
14250520 LW |
1485 | #ifdef CONFIG_GPIOLIB_IRQCHIP |
1486 | ||
1487 | /* | |
1488 | * The following is irqchip helper code for gpiochips. | |
1489 | */ | |
1490 | ||
79b804cb MW |
1491 | static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip) |
1492 | { | |
dc7b0387 | 1493 | if (!gpiochip->irq.need_valid_mask) |
79b804cb MW |
1494 | return 0; |
1495 | ||
dc7b0387 | 1496 | gpiochip->irq.valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio), |
79b804cb | 1497 | sizeof(long), GFP_KERNEL); |
dc7b0387 | 1498 | if (!gpiochip->irq.valid_mask) |
79b804cb MW |
1499 | return -ENOMEM; |
1500 | ||
1501 | /* Assume by default all GPIOs are valid */ | |
dc7b0387 | 1502 | bitmap_fill(gpiochip->irq.valid_mask, gpiochip->ngpio); |
79b804cb MW |
1503 | |
1504 | return 0; | |
1505 | } | |
1506 | ||
1507 | static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip) | |
1508 | { | |
dc7b0387 TR |
1509 | kfree(gpiochip->irq.valid_mask); |
1510 | gpiochip->irq.valid_mask = NULL; | |
79b804cb MW |
1511 | } |
1512 | ||
1513 | static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip, | |
1514 | unsigned int offset) | |
1515 | { | |
1516 | /* No mask means all valid */ | |
dc7b0387 | 1517 | if (likely(!gpiochip->irq.valid_mask)) |
79b804cb | 1518 | return true; |
dc7b0387 | 1519 | return test_bit(offset, gpiochip->irq.valid_mask); |
79b804cb MW |
1520 | } |
1521 | ||
14250520 | 1522 | /** |
d245b3f9 | 1523 | * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip |
3f97d5fc LW |
1524 | * @gpiochip: the gpiochip to set the irqchip chain to |
1525 | * @irqchip: the irqchip to chain to the gpiochip | |
14250520 LW |
1526 | * @parent_irq: the irq number corresponding to the parent IRQ for this |
1527 | * chained irqchip | |
1528 | * @parent_handler: the parent interrupt handler for the accumulated IRQ | |
3f97d5fc LW |
1529 | * coming out of the gpiochip. If the interrupt is nested rather than |
1530 | * cascaded, pass NULL in this handler argument | |
14250520 | 1531 | */ |
d245b3f9 LW |
1532 | static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip, |
1533 | struct irq_chip *irqchip, | |
6f79309a | 1534 | unsigned int parent_irq, |
d245b3f9 | 1535 | irq_flow_handler_t parent_handler) |
14250520 | 1536 | { |
83141a77 LW |
1537 | unsigned int offset; |
1538 | ||
f0fbe7bc | 1539 | if (!gpiochip->irq.domain) { |
83141a77 LW |
1540 | chip_err(gpiochip, "called %s before setting up irqchip\n", |
1541 | __func__); | |
1c8732bb LW |
1542 | return; |
1543 | } | |
1544 | ||
3f97d5fc LW |
1545 | if (parent_handler) { |
1546 | if (gpiochip->can_sleep) { | |
1547 | chip_err(gpiochip, | |
1548 | "you cannot have chained interrupts on a " | |
1549 | "chip that may sleep\n"); | |
1550 | return; | |
1551 | } | |
3f97d5fc LW |
1552 | /* |
1553 | * The parent irqchip is already using the chip_data for this | |
1554 | * irqchip, so our callbacks simply use the handler_data. | |
1555 | */ | |
f7f87753 TG |
1556 | irq_set_chained_handler_and_data(parent_irq, parent_handler, |
1557 | gpiochip); | |
25e4fe92 | 1558 | |
39e5f096 TR |
1559 | gpiochip->irq.parents = &parent_irq; |
1560 | gpiochip->irq.num_parents = 1; | |
3f97d5fc | 1561 | } |
83141a77 LW |
1562 | |
1563 | /* Set the parent IRQ for all affected IRQs */ | |
79b804cb MW |
1564 | for (offset = 0; offset < gpiochip->ngpio; offset++) { |
1565 | if (!gpiochip_irqchip_irq_valid(gpiochip, offset)) | |
1566 | continue; | |
f0fbe7bc | 1567 | irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset), |
83141a77 | 1568 | parent_irq); |
79b804cb | 1569 | } |
14250520 | 1570 | } |
d245b3f9 LW |
1571 | |
1572 | /** | |
1573 | * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip | |
1574 | * @gpiochip: the gpiochip to set the irqchip chain to | |
1575 | * @irqchip: the irqchip to chain to the gpiochip | |
1576 | * @parent_irq: the irq number corresponding to the parent IRQ for this | |
1577 | * chained irqchip | |
1578 | * @parent_handler: the parent interrupt handler for the accumulated IRQ | |
1579 | * coming out of the gpiochip. If the interrupt is nested rather than | |
1580 | * cascaded, pass NULL in this handler argument | |
1581 | */ | |
1582 | void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip, | |
1583 | struct irq_chip *irqchip, | |
6f79309a | 1584 | unsigned int parent_irq, |
d245b3f9 LW |
1585 | irq_flow_handler_t parent_handler) |
1586 | { | |
60ed54ca TR |
1587 | if (gpiochip->irq.threaded) { |
1588 | chip_err(gpiochip, "tried to chain a threaded gpiochip\n"); | |
1589 | return; | |
1590 | } | |
1591 | ||
d245b3f9 LW |
1592 | gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq, |
1593 | parent_handler); | |
1594 | } | |
14250520 LW |
1595 | EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip); |
1596 | ||
d245b3f9 LW |
1597 | /** |
1598 | * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip | |
1599 | * @gpiochip: the gpiochip to set the irqchip nested handler to | |
1600 | * @irqchip: the irqchip to nest to the gpiochip | |
1601 | * @parent_irq: the irq number corresponding to the parent IRQ for this | |
1602 | * nested irqchip | |
1603 | */ | |
1604 | void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip, | |
1605 | struct irq_chip *irqchip, | |
6f79309a | 1606 | unsigned int parent_irq) |
d245b3f9 | 1607 | { |
d245b3f9 LW |
1608 | gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq, |
1609 | NULL); | |
1610 | } | |
1611 | EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip); | |
1612 | ||
14250520 LW |
1613 | /** |
1614 | * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip | |
1615 | * @d: the irqdomain used by this irqchip | |
1616 | * @irq: the global irq number used by this GPIO irqchip irq | |
1617 | * @hwirq: the local IRQ/GPIO line offset on this gpiochip | |
1618 | * | |
1619 | * This function will set up the mapping for a certain IRQ line on a | |
1620 | * gpiochip by assigning the gpiochip as chip data, and using the irqchip | |
1621 | * stored inside the gpiochip. | |
1622 | */ | |
1b95b4eb TR |
1623 | int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, |
1624 | irq_hw_number_t hwirq) | |
14250520 LW |
1625 | { |
1626 | struct gpio_chip *chip = d->host_data; | |
e0d89728 | 1627 | int err = 0; |
14250520 | 1628 | |
dc749a09 GS |
1629 | if (!gpiochip_irqchip_irq_valid(chip, hwirq)) |
1630 | return -ENXIO; | |
1631 | ||
14250520 | 1632 | irq_set_chip_data(irq, chip); |
a0a8bcf4 GS |
1633 | /* |
1634 | * This lock class tells lockdep that GPIO irqs are in a different | |
1635 | * category than their parents, so it won't report false recursion. | |
1636 | */ | |
39c3fd58 | 1637 | irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key); |
c7a0aa59 | 1638 | irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler); |
d245b3f9 | 1639 | /* Chips that use nested thread handlers have them marked */ |
60ed54ca | 1640 | if (chip->irq.threaded) |
1c8732bb | 1641 | irq_set_nested_thread(irq, 1); |
14250520 | 1642 | irq_set_noprobe(irq); |
23393d49 | 1643 | |
e0d89728 TR |
1644 | if (chip->irq.num_parents == 1) |
1645 | err = irq_set_parent(irq, chip->irq.parents[0]); | |
1646 | else if (chip->irq.map) | |
1647 | err = irq_set_parent(irq, chip->irq.map[hwirq]); | |
1648 | ||
1649 | if (err < 0) | |
1650 | return err; | |
1651 | ||
1333b90f LW |
1652 | /* |
1653 | * No set-up of the hardware will happen if IRQ_TYPE_NONE | |
1654 | * is passed as default type. | |
1655 | */ | |
3634eeb0 TR |
1656 | if (chip->irq.default_type != IRQ_TYPE_NONE) |
1657 | irq_set_irq_type(irq, chip->irq.default_type); | |
14250520 LW |
1658 | |
1659 | return 0; | |
1660 | } | |
1b95b4eb | 1661 | EXPORT_SYMBOL_GPL(gpiochip_irq_map); |
14250520 | 1662 | |
1b95b4eb | 1663 | void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq) |
c3626fde | 1664 | { |
1c8732bb LW |
1665 | struct gpio_chip *chip = d->host_data; |
1666 | ||
60ed54ca | 1667 | if (chip->irq.threaded) |
1c8732bb | 1668 | irq_set_nested_thread(irq, 0); |
c3626fde LW |
1669 | irq_set_chip_and_handler(irq, NULL, NULL); |
1670 | irq_set_chip_data(irq, NULL); | |
1671 | } | |
1b95b4eb | 1672 | EXPORT_SYMBOL_GPL(gpiochip_irq_unmap); |
c3626fde | 1673 | |
14250520 LW |
1674 | static const struct irq_domain_ops gpiochip_domain_ops = { |
1675 | .map = gpiochip_irq_map, | |
c3626fde | 1676 | .unmap = gpiochip_irq_unmap, |
14250520 LW |
1677 | /* Virtually all GPIO irqchips are twocell:ed */ |
1678 | .xlate = irq_domain_xlate_twocell, | |
1679 | }; | |
1680 | ||
1681 | static int gpiochip_irq_reqres(struct irq_data *d) | |
1682 | { | |
1683 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); | |
1684 | ||
ff2b1359 | 1685 | if (!try_module_get(chip->gpiodev->owner)) |
5b76e79c GS |
1686 | return -ENODEV; |
1687 | ||
e3a2e878 | 1688 | if (gpiochip_lock_as_irq(chip, d->hwirq)) { |
14250520 LW |
1689 | chip_err(chip, |
1690 | "unable to lock HW IRQ %lu for IRQ\n", | |
1691 | d->hwirq); | |
ff2b1359 | 1692 | module_put(chip->gpiodev->owner); |
14250520 LW |
1693 | return -EINVAL; |
1694 | } | |
1695 | return 0; | |
1696 | } | |
1697 | ||
1698 | static void gpiochip_irq_relres(struct irq_data *d) | |
1699 | { | |
1700 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); | |
1701 | ||
e3a2e878 | 1702 | gpiochip_unlock_as_irq(chip, d->hwirq); |
ff2b1359 | 1703 | module_put(chip->gpiodev->owner); |
14250520 LW |
1704 | } |
1705 | ||
1706 | static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset) | |
1707 | { | |
dc749a09 GS |
1708 | if (!gpiochip_irqchip_irq_valid(chip, offset)) |
1709 | return -ENXIO; | |
e0d89728 | 1710 | |
f0fbe7bc | 1711 | return irq_create_mapping(chip->irq.domain, offset); |
14250520 LW |
1712 | } |
1713 | ||
e0d89728 TR |
1714 | /** |
1715 | * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip | |
1716 | * @gpiochip: the GPIO chip to add the IRQ chip to | |
39c3fd58 AL |
1717 | * @lock_key: lockdep class for IRQ lock |
1718 | * @request_key: lockdep class for IRQ request | |
e0d89728 | 1719 | */ |
959bc7b2 | 1720 | static int gpiochip_add_irqchip(struct gpio_chip *gpiochip, |
39c3fd58 AL |
1721 | struct lock_class_key *lock_key, |
1722 | struct lock_class_key *request_key) | |
e0d89728 TR |
1723 | { |
1724 | struct irq_chip *irqchip = gpiochip->irq.chip; | |
1725 | const struct irq_domain_ops *ops; | |
1726 | struct device_node *np; | |
1727 | unsigned int type; | |
1728 | unsigned int i; | |
1729 | ||
1730 | if (!irqchip) | |
1731 | return 0; | |
1732 | ||
1733 | if (gpiochip->irq.parent_handler && gpiochip->can_sleep) { | |
1734 | chip_err(gpiochip, "you cannot have chained interrupts on a " | |
1735 | "chip that may sleep\n"); | |
1736 | return -EINVAL; | |
1737 | } | |
1738 | ||
1739 | np = gpiochip->gpiodev->dev.of_node; | |
1740 | type = gpiochip->irq.default_type; | |
1741 | ||
1742 | /* | |
1743 | * Specifying a default trigger is a terrible idea if DT or ACPI is | |
1744 | * used to configure the interrupts, as you may end up with | |
1745 | * conflicting triggers. Tell the user, and reset to NONE. | |
1746 | */ | |
1747 | if (WARN(np && type != IRQ_TYPE_NONE, | |
1748 | "%s: Ignoring %u default trigger\n", np->full_name, type)) | |
1749 | type = IRQ_TYPE_NONE; | |
1750 | ||
1751 | if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) { | |
1752 | acpi_handle_warn(ACPI_HANDLE(gpiochip->parent), | |
1753 | "Ignoring %u default trigger\n", type); | |
1754 | type = IRQ_TYPE_NONE; | |
1755 | } | |
1756 | ||
1757 | gpiochip->to_irq = gpiochip_to_irq; | |
1758 | gpiochip->irq.default_type = type; | |
959bc7b2 | 1759 | gpiochip->irq.lock_key = lock_key; |
39c3fd58 | 1760 | gpiochip->irq.request_key = request_key; |
e0d89728 TR |
1761 | |
1762 | if (gpiochip->irq.domain_ops) | |
1763 | ops = gpiochip->irq.domain_ops; | |
1764 | else | |
1765 | ops = &gpiochip_domain_ops; | |
1766 | ||
1767 | gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio, | |
8302cf58 TR |
1768 | gpiochip->irq.first, |
1769 | ops, gpiochip); | |
e0d89728 TR |
1770 | if (!gpiochip->irq.domain) |
1771 | return -EINVAL; | |
1772 | ||
1773 | /* | |
1774 | * It is possible for a driver to override this, but only if the | |
1775 | * alternative functions are both implemented. | |
1776 | */ | |
1777 | if (!irqchip->irq_request_resources && | |
1778 | !irqchip->irq_release_resources) { | |
1779 | irqchip->irq_request_resources = gpiochip_irq_reqres; | |
1780 | irqchip->irq_release_resources = gpiochip_irq_relres; | |
1781 | } | |
1782 | ||
1783 | if (gpiochip->irq.parent_handler) { | |
1784 | void *data = gpiochip->irq.parent_handler_data ?: gpiochip; | |
1785 | ||
1786 | for (i = 0; i < gpiochip->irq.num_parents; i++) { | |
1787 | /* | |
1788 | * The parent IRQ chip is already using the chip_data | |
1789 | * for this IRQ chip, so our callbacks simply use the | |
1790 | * handler_data. | |
1791 | */ | |
1792 | irq_set_chained_handler_and_data(gpiochip->irq.parents[i], | |
1793 | gpiochip->irq.parent_handler, | |
1794 | data); | |
1795 | } | |
e0d89728 TR |
1796 | } |
1797 | ||
1798 | acpi_gpiochip_request_interrupts(gpiochip); | |
1799 | ||
1800 | return 0; | |
1801 | } | |
1802 | ||
14250520 LW |
1803 | /** |
1804 | * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip | |
1805 | * @gpiochip: the gpiochip to remove the irqchip from | |
1806 | * | |
1807 | * This is called only from gpiochip_remove() | |
1808 | */ | |
1809 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) | |
1810 | { | |
39e5f096 | 1811 | unsigned int offset; |
c3626fde | 1812 | |
afa82fab MW |
1813 | acpi_gpiochip_free_interrupts(gpiochip); |
1814 | ||
e0d89728 | 1815 | if (gpiochip->irq.chip && gpiochip->irq.parent_handler) { |
39e5f096 TR |
1816 | struct gpio_irq_chip *irq = &gpiochip->irq; |
1817 | unsigned int i; | |
1818 | ||
1819 | for (i = 0; i < irq->num_parents; i++) | |
1820 | irq_set_chained_handler_and_data(irq->parents[i], | |
1821 | NULL, NULL); | |
25e4fe92 DB |
1822 | } |
1823 | ||
c3626fde | 1824 | /* Remove all IRQ mappings and delete the domain */ |
f0fbe7bc | 1825 | if (gpiochip->irq.domain) { |
39e5f096 TR |
1826 | unsigned int irq; |
1827 | ||
79b804cb MW |
1828 | for (offset = 0; offset < gpiochip->ngpio; offset++) { |
1829 | if (!gpiochip_irqchip_irq_valid(gpiochip, offset)) | |
1830 | continue; | |
f0fbe7bc TR |
1831 | |
1832 | irq = irq_find_mapping(gpiochip->irq.domain, offset); | |
1833 | irq_dispose_mapping(irq); | |
79b804cb | 1834 | } |
f0fbe7bc TR |
1835 | |
1836 | irq_domain_remove(gpiochip->irq.domain); | |
c3626fde | 1837 | } |
14250520 | 1838 | |
da80ff81 TR |
1839 | if (gpiochip->irq.chip) { |
1840 | gpiochip->irq.chip->irq_request_resources = NULL; | |
1841 | gpiochip->irq.chip->irq_release_resources = NULL; | |
1842 | gpiochip->irq.chip = NULL; | |
14250520 | 1843 | } |
79b804cb MW |
1844 | |
1845 | gpiochip_irqchip_free_valid_mask(gpiochip); | |
14250520 LW |
1846 | } |
1847 | ||
1848 | /** | |
739e6f59 | 1849 | * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip |
14250520 LW |
1850 | * @gpiochip: the gpiochip to add the irqchip to |
1851 | * @irqchip: the irqchip to add to the gpiochip | |
1852 | * @first_irq: if not dynamically assigned, the base (first) IRQ to | |
1853 | * allocate gpiochip irqs from | |
1854 | * @handler: the irq handler to use (often a predefined irq core function) | |
1333b90f LW |
1855 | * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE |
1856 | * to have the core avoid setting up any default type in the hardware. | |
60ed54ca | 1857 | * @threaded: whether this irqchip uses a nested thread handler |
39c3fd58 AL |
1858 | * @lock_key: lockdep class for IRQ lock |
1859 | * @request_key: lockdep class for IRQ request | |
14250520 LW |
1860 | * |
1861 | * This function closely associates a certain irqchip with a certain | |
1862 | * gpiochip, providing an irq domain to translate the local IRQs to | |
1863 | * global irqs in the gpiolib core, and making sure that the gpiochip | |
1864 | * is passed as chip data to all related functions. Driver callbacks | |
09dd5f9e | 1865 | * need to use gpiochip_get_data() to get their local state containers back |
14250520 LW |
1866 | * from the gpiochip passed as chip data. An irqdomain will be stored |
1867 | * in the gpiochip that shall be used by the driver to handle IRQ number | |
1868 | * translation. The gpiochip will need to be initialized and registered | |
1869 | * before calling this function. | |
1870 | * | |
c3626fde LW |
1871 | * This function will handle two cell:ed simple IRQs and assumes all |
1872 | * the pins on the gpiochip can generate a unique IRQ. Everything else | |
14250520 LW |
1873 | * need to be open coded. |
1874 | */ | |
739e6f59 LW |
1875 | int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip, |
1876 | struct irq_chip *irqchip, | |
1877 | unsigned int first_irq, | |
1878 | irq_flow_handler_t handler, | |
1879 | unsigned int type, | |
60ed54ca | 1880 | bool threaded, |
39c3fd58 AL |
1881 | struct lock_class_key *lock_key, |
1882 | struct lock_class_key *request_key) | |
14250520 LW |
1883 | { |
1884 | struct device_node *of_node; | |
14250520 LW |
1885 | |
1886 | if (!gpiochip || !irqchip) | |
1887 | return -EINVAL; | |
1888 | ||
58383c78 | 1889 | if (!gpiochip->parent) { |
14250520 LW |
1890 | pr_err("missing gpiochip .dev parent pointer\n"); |
1891 | return -EINVAL; | |
1892 | } | |
60ed54ca | 1893 | gpiochip->irq.threaded = threaded; |
58383c78 | 1894 | of_node = gpiochip->parent->of_node; |
14250520 LW |
1895 | #ifdef CONFIG_OF_GPIO |
1896 | /* | |
20a8a968 | 1897 | * If the gpiochip has an assigned OF node this takes precedence |
c88402c2 BJZ |
1898 | * FIXME: get rid of this and use gpiochip->parent->of_node |
1899 | * everywhere | |
14250520 LW |
1900 | */ |
1901 | if (gpiochip->of_node) | |
1902 | of_node = gpiochip->of_node; | |
1903 | #endif | |
332e99d5 | 1904 | /* |
0a1e0053 | 1905 | * Specifying a default trigger is a terrible idea if DT or ACPI is |
332e99d5 MZ |
1906 | * used to configure the interrupts, as you may end-up with |
1907 | * conflicting triggers. Tell the user, and reset to NONE. | |
1908 | */ | |
1909 | if (WARN(of_node && type != IRQ_TYPE_NONE, | |
7eb6ce2f | 1910 | "%pOF: Ignoring %d default trigger\n", of_node, type)) |
332e99d5 | 1911 | type = IRQ_TYPE_NONE; |
0a1e0053 MW |
1912 | if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) { |
1913 | acpi_handle_warn(ACPI_HANDLE(gpiochip->parent), | |
1914 | "Ignoring %d default trigger\n", type); | |
1915 | type = IRQ_TYPE_NONE; | |
1916 | } | |
332e99d5 | 1917 | |
da80ff81 | 1918 | gpiochip->irq.chip = irqchip; |
c7a0aa59 | 1919 | gpiochip->irq.handler = handler; |
3634eeb0 | 1920 | gpiochip->irq.default_type = type; |
14250520 | 1921 | gpiochip->to_irq = gpiochip_to_irq; |
ca9df053 | 1922 | gpiochip->irq.lock_key = lock_key; |
39c3fd58 | 1923 | gpiochip->irq.request_key = request_key; |
f0fbe7bc | 1924 | gpiochip->irq.domain = irq_domain_add_simple(of_node, |
14250520 LW |
1925 | gpiochip->ngpio, first_irq, |
1926 | &gpiochip_domain_ops, gpiochip); | |
f0fbe7bc | 1927 | if (!gpiochip->irq.domain) { |
da80ff81 | 1928 | gpiochip->irq.chip = NULL; |
14250520 LW |
1929 | return -EINVAL; |
1930 | } | |
8b67a1f0 RV |
1931 | |
1932 | /* | |
1933 | * It is possible for a driver to override this, but only if the | |
1934 | * alternative functions are both implemented. | |
1935 | */ | |
1936 | if (!irqchip->irq_request_resources && | |
1937 | !irqchip->irq_release_resources) { | |
1938 | irqchip->irq_request_resources = gpiochip_irq_reqres; | |
1939 | irqchip->irq_release_resources = gpiochip_irq_relres; | |
1940 | } | |
14250520 | 1941 | |
afa82fab MW |
1942 | acpi_gpiochip_request_interrupts(gpiochip); |
1943 | ||
14250520 LW |
1944 | return 0; |
1945 | } | |
739e6f59 | 1946 | EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key); |
14250520 LW |
1947 | |
1948 | #else /* CONFIG_GPIOLIB_IRQCHIP */ | |
1949 | ||
959bc7b2 | 1950 | static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip, |
39c3fd58 AL |
1951 | struct lock_class_key *lock_key, |
1952 | struct lock_class_key *request_key) | |
e0d89728 TR |
1953 | { |
1954 | return 0; | |
1955 | } | |
1956 | ||
14250520 | 1957 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {} |
79b804cb MW |
1958 | static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip) |
1959 | { | |
1960 | return 0; | |
1961 | } | |
1962 | static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip) | |
1963 | { } | |
14250520 LW |
1964 | |
1965 | #endif /* CONFIG_GPIOLIB_IRQCHIP */ | |
1966 | ||
c771c2f4 JG |
1967 | /** |
1968 | * gpiochip_generic_request() - request the gpio function for a pin | |
1969 | * @chip: the gpiochip owning the GPIO | |
1970 | * @offset: the offset of the GPIO to request for GPIO function | |
1971 | */ | |
1972 | int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset) | |
1973 | { | |
a9a1d2a7 | 1974 | return pinctrl_gpio_request(chip->gpiodev->base + offset); |
c771c2f4 JG |
1975 | } |
1976 | EXPORT_SYMBOL_GPL(gpiochip_generic_request); | |
1977 | ||
1978 | /** | |
1979 | * gpiochip_generic_free() - free the gpio function from a pin | |
1980 | * @chip: the gpiochip to request the gpio function for | |
1981 | * @offset: the offset of the GPIO to free from GPIO function | |
1982 | */ | |
1983 | void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset) | |
1984 | { | |
a9a1d2a7 | 1985 | pinctrl_gpio_free(chip->gpiodev->base + offset); |
c771c2f4 JG |
1986 | } |
1987 | EXPORT_SYMBOL_GPL(gpiochip_generic_free); | |
1988 | ||
2956b5d9 MW |
1989 | /** |
1990 | * gpiochip_generic_config() - apply configuration for a pin | |
1991 | * @chip: the gpiochip owning the GPIO | |
1992 | * @offset: the offset of the GPIO to apply the configuration | |
1993 | * @config: the configuration to be applied | |
1994 | */ | |
1995 | int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset, | |
1996 | unsigned long config) | |
1997 | { | |
1998 | return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config); | |
1999 | } | |
2000 | EXPORT_SYMBOL_GPL(gpiochip_generic_config); | |
2001 | ||
f23f1516 | 2002 | #ifdef CONFIG_PINCTRL |
165adc9c | 2003 | |
586a87e6 CR |
2004 | /** |
2005 | * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping | |
2006 | * @chip: the gpiochip to add the range for | |
d32651f6 | 2007 | * @pctldev: the pin controller to map to |
586a87e6 CR |
2008 | * @gpio_offset: the start offset in the current gpio_chip number space |
2009 | * @pin_group: name of the pin group inside the pin controller | |
2010 | */ | |
2011 | int gpiochip_add_pingroup_range(struct gpio_chip *chip, | |
2012 | struct pinctrl_dev *pctldev, | |
2013 | unsigned int gpio_offset, const char *pin_group) | |
2014 | { | |
2015 | struct gpio_pin_range *pin_range; | |
fdeb8e15 | 2016 | struct gpio_device *gdev = chip->gpiodev; |
586a87e6 CR |
2017 | int ret; |
2018 | ||
2019 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); | |
2020 | if (!pin_range) { | |
1a2a99c6 | 2021 | chip_err(chip, "failed to allocate pin ranges\n"); |
586a87e6 CR |
2022 | return -ENOMEM; |
2023 | } | |
2024 | ||
2025 | /* Use local offset as range ID */ | |
2026 | pin_range->range.id = gpio_offset; | |
2027 | pin_range->range.gc = chip; | |
2028 | pin_range->range.name = chip->label; | |
fdeb8e15 | 2029 | pin_range->range.base = gdev->base + gpio_offset; |
586a87e6 CR |
2030 | pin_range->pctldev = pctldev; |
2031 | ||
2032 | ret = pinctrl_get_group_pins(pctldev, pin_group, | |
2033 | &pin_range->range.pins, | |
2034 | &pin_range->range.npins); | |
61c6375d MN |
2035 | if (ret < 0) { |
2036 | kfree(pin_range); | |
586a87e6 | 2037 | return ret; |
61c6375d | 2038 | } |
586a87e6 CR |
2039 | |
2040 | pinctrl_add_gpio_range(pctldev, &pin_range->range); | |
2041 | ||
1a2a99c6 AS |
2042 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n", |
2043 | gpio_offset, gpio_offset + pin_range->range.npins - 1, | |
586a87e6 CR |
2044 | pinctrl_dev_get_devname(pctldev), pin_group); |
2045 | ||
20ec3e39 | 2046 | list_add_tail(&pin_range->node, &gdev->pin_ranges); |
586a87e6 CR |
2047 | |
2048 | return 0; | |
2049 | } | |
2050 | EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range); | |
2051 | ||
3f0f8670 LW |
2052 | /** |
2053 | * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping | |
2054 | * @chip: the gpiochip to add the range for | |
950d55f5 | 2055 | * @pinctl_name: the dev_name() of the pin controller to map to |
316511c0 LW |
2056 | * @gpio_offset: the start offset in the current gpio_chip number space |
2057 | * @pin_offset: the start offset in the pin controller number space | |
3f0f8670 LW |
2058 | * @npins: the number of pins from the offset of each pin space (GPIO and |
2059 | * pin controller) to accumulate in this range | |
950d55f5 TR |
2060 | * |
2061 | * Returns: | |
2062 | * 0 on success, or a negative error-code on failure. | |
3f0f8670 | 2063 | */ |
1e63d7b9 | 2064 | int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
316511c0 | 2065 | unsigned int gpio_offset, unsigned int pin_offset, |
3f0f8670 | 2066 | unsigned int npins) |
f23f1516 SH |
2067 | { |
2068 | struct gpio_pin_range *pin_range; | |
fdeb8e15 | 2069 | struct gpio_device *gdev = chip->gpiodev; |
b4d4b1f0 | 2070 | int ret; |
f23f1516 | 2071 | |
3f0f8670 | 2072 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
f23f1516 | 2073 | if (!pin_range) { |
1a2a99c6 | 2074 | chip_err(chip, "failed to allocate pin ranges\n"); |
1e63d7b9 | 2075 | return -ENOMEM; |
f23f1516 SH |
2076 | } |
2077 | ||
3f0f8670 | 2078 | /* Use local offset as range ID */ |
316511c0 | 2079 | pin_range->range.id = gpio_offset; |
3f0f8670 | 2080 | pin_range->range.gc = chip; |
f23f1516 | 2081 | pin_range->range.name = chip->label; |
fdeb8e15 | 2082 | pin_range->range.base = gdev->base + gpio_offset; |
316511c0 | 2083 | pin_range->range.pin_base = pin_offset; |
f23f1516 | 2084 | pin_range->range.npins = npins; |
192c369c | 2085 | pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, |
f23f1516 | 2086 | &pin_range->range); |
8f23ca1a | 2087 | if (IS_ERR(pin_range->pctldev)) { |
b4d4b1f0 | 2088 | ret = PTR_ERR(pin_range->pctldev); |
1a2a99c6 | 2089 | chip_err(chip, "could not create pin range\n"); |
3f0f8670 | 2090 | kfree(pin_range); |
b4d4b1f0 | 2091 | return ret; |
3f0f8670 | 2092 | } |
1a2a99c6 AS |
2093 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n", |
2094 | gpio_offset, gpio_offset + npins - 1, | |
316511c0 LW |
2095 | pinctl_name, |
2096 | pin_offset, pin_offset + npins - 1); | |
f23f1516 | 2097 | |
20ec3e39 | 2098 | list_add_tail(&pin_range->node, &gdev->pin_ranges); |
1e63d7b9 LW |
2099 | |
2100 | return 0; | |
f23f1516 | 2101 | } |
165adc9c | 2102 | EXPORT_SYMBOL_GPL(gpiochip_add_pin_range); |
f23f1516 | 2103 | |
3f0f8670 LW |
2104 | /** |
2105 | * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings | |
2106 | * @chip: the chip to remove all the mappings for | |
2107 | */ | |
f23f1516 SH |
2108 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
2109 | { | |
2110 | struct gpio_pin_range *pin_range, *tmp; | |
20ec3e39 | 2111 | struct gpio_device *gdev = chip->gpiodev; |
f23f1516 | 2112 | |
20ec3e39 | 2113 | list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) { |
f23f1516 SH |
2114 | list_del(&pin_range->node); |
2115 | pinctrl_remove_gpio_range(pin_range->pctldev, | |
2116 | &pin_range->range); | |
3f0f8670 | 2117 | kfree(pin_range); |
f23f1516 SH |
2118 | } |
2119 | } | |
165adc9c LW |
2120 | EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); |
2121 | ||
2122 | #endif /* CONFIG_PINCTRL */ | |
f23f1516 | 2123 | |
d2876d08 DB |
2124 | /* These "optional" allocation calls help prevent drivers from stomping |
2125 | * on each other, and help provide better diagnostics in debugfs. | |
2126 | * They're called even less than the "set direction" calls. | |
2127 | */ | |
fac9d885 | 2128 | static int gpiod_request_commit(struct gpio_desc *desc, const char *label) |
d2876d08 | 2129 | { |
fdeb8e15 | 2130 | struct gpio_chip *chip = desc->gdev->chip; |
77c2d792 | 2131 | int status; |
d2876d08 DB |
2132 | unsigned long flags; |
2133 | ||
bcabdef1 AC |
2134 | spin_lock_irqsave(&gpio_lock, flags); |
2135 | ||
d2876d08 | 2136 | /* NOTE: gpio_request() can be called in early boot, |
35e8bb51 | 2137 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
d2876d08 DB |
2138 | */ |
2139 | ||
2140 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { | |
2141 | desc_set_label(desc, label ? : "?"); | |
2142 | status = 0; | |
438d8908 | 2143 | } else { |
d2876d08 | 2144 | status = -EBUSY; |
7460db56 | 2145 | goto done; |
35e8bb51 DB |
2146 | } |
2147 | ||
2148 | if (chip->request) { | |
2149 | /* chip->request may sleep */ | |
2150 | spin_unlock_irqrestore(&gpio_lock, flags); | |
372e722e | 2151 | status = chip->request(chip, gpio_chip_hwgpio(desc)); |
35e8bb51 DB |
2152 | spin_lock_irqsave(&gpio_lock, flags); |
2153 | ||
2154 | if (status < 0) { | |
2155 | desc_set_label(desc, NULL); | |
35e8bb51 | 2156 | clear_bit(FLAG_REQUESTED, &desc->flags); |
80b0a602 | 2157 | goto done; |
35e8bb51 | 2158 | } |
438d8908 | 2159 | } |
80b0a602 MN |
2160 | if (chip->get_direction) { |
2161 | /* chip->get_direction may sleep */ | |
2162 | spin_unlock_irqrestore(&gpio_lock, flags); | |
372e722e | 2163 | gpiod_get_direction(desc); |
80b0a602 MN |
2164 | spin_lock_irqsave(&gpio_lock, flags); |
2165 | } | |
77c2d792 MW |
2166 | done: |
2167 | spin_unlock_irqrestore(&gpio_lock, flags); | |
2168 | return status; | |
2169 | } | |
2170 | ||
fdeb8e15 LW |
2171 | /* |
2172 | * This descriptor validation needs to be inserted verbatim into each | |
2173 | * function taking a descriptor, so we need to use a preprocessor | |
54d77198 LW |
2174 | * macro to avoid endless duplication. If the desc is NULL it is an |
2175 | * optional GPIO and calls should just bail out. | |
fdeb8e15 LW |
2176 | */ |
2177 | #define VALIDATE_DESC(desc) do { \ | |
54d77198 LW |
2178 | if (!desc) \ |
2179 | return 0; \ | |
bfbbe44d LW |
2180 | if (IS_ERR(desc)) { \ |
2181 | pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \ | |
2182 | return PTR_ERR(desc); \ | |
2183 | } \ | |
54d77198 | 2184 | if (!desc->gdev) { \ |
bfbbe44d | 2185 | pr_warn("%s: invalid GPIO (no device)\n", __func__); \ |
fdeb8e15 LW |
2186 | return -EINVAL; \ |
2187 | } \ | |
2188 | if ( !desc->gdev->chip ) { \ | |
2189 | dev_warn(&desc->gdev->dev, \ | |
2190 | "%s: backing chip is gone\n", __func__); \ | |
2191 | return 0; \ | |
2192 | } } while (0) | |
2193 | ||
2194 | #define VALIDATE_DESC_VOID(desc) do { \ | |
54d77198 LW |
2195 | if (!desc) \ |
2196 | return; \ | |
bfbbe44d LW |
2197 | if (IS_ERR(desc)) { \ |
2198 | pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \ | |
2199 | return; \ | |
2200 | } \ | |
54d77198 | 2201 | if (!desc->gdev) { \ |
bfbbe44d | 2202 | pr_warn("%s: invalid GPIO (no device)\n", __func__); \ |
fdeb8e15 LW |
2203 | return; \ |
2204 | } \ | |
2205 | if (!desc->gdev->chip) { \ | |
2206 | dev_warn(&desc->gdev->dev, \ | |
2207 | "%s: backing chip is gone\n", __func__); \ | |
2208 | return; \ | |
2209 | } } while (0) | |
2210 | ||
2211 | ||
0eb4c6c2 | 2212 | int gpiod_request(struct gpio_desc *desc, const char *label) |
77c2d792 MW |
2213 | { |
2214 | int status = -EPROBE_DEFER; | |
fdeb8e15 | 2215 | struct gpio_device *gdev; |
77c2d792 | 2216 | |
fdeb8e15 LW |
2217 | VALIDATE_DESC(desc); |
2218 | gdev = desc->gdev; | |
77c2d792 | 2219 | |
fdeb8e15 | 2220 | if (try_module_get(gdev->owner)) { |
fac9d885 | 2221 | status = gpiod_request_commit(desc, label); |
77c2d792 | 2222 | if (status < 0) |
fdeb8e15 | 2223 | module_put(gdev->owner); |
33a68e86 LW |
2224 | else |
2225 | get_device(&gdev->dev); | |
77c2d792 MW |
2226 | } |
2227 | ||
d2876d08 | 2228 | if (status) |
7589e59f | 2229 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
77c2d792 | 2230 | |
d2876d08 DB |
2231 | return status; |
2232 | } | |
372e722e | 2233 | |
fac9d885 | 2234 | static bool gpiod_free_commit(struct gpio_desc *desc) |
d2876d08 | 2235 | { |
77c2d792 | 2236 | bool ret = false; |
d2876d08 | 2237 | unsigned long flags; |
35e8bb51 | 2238 | struct gpio_chip *chip; |
d2876d08 | 2239 | |
3d599d1c UKK |
2240 | might_sleep(); |
2241 | ||
372e722e | 2242 | gpiod_unexport(desc); |
d8f388d8 | 2243 | |
d2876d08 DB |
2244 | spin_lock_irqsave(&gpio_lock, flags); |
2245 | ||
fdeb8e15 | 2246 | chip = desc->gdev->chip; |
35e8bb51 DB |
2247 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { |
2248 | if (chip->free) { | |
2249 | spin_unlock_irqrestore(&gpio_lock, flags); | |
9c4ba946 | 2250 | might_sleep_if(chip->can_sleep); |
372e722e | 2251 | chip->free(chip, gpio_chip_hwgpio(desc)); |
35e8bb51 DB |
2252 | spin_lock_irqsave(&gpio_lock, flags); |
2253 | } | |
d2876d08 | 2254 | desc_set_label(desc, NULL); |
07697461 | 2255 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
35e8bb51 | 2256 | clear_bit(FLAG_REQUESTED, &desc->flags); |
aca5ce14 | 2257 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
25553ff0 | 2258 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
f625d460 | 2259 | clear_bit(FLAG_IS_HOGGED, &desc->flags); |
77c2d792 MW |
2260 | ret = true; |
2261 | } | |
d2876d08 DB |
2262 | |
2263 | spin_unlock_irqrestore(&gpio_lock, flags); | |
77c2d792 MW |
2264 | return ret; |
2265 | } | |
2266 | ||
0eb4c6c2 | 2267 | void gpiod_free(struct gpio_desc *desc) |
77c2d792 | 2268 | { |
fac9d885 | 2269 | if (desc && desc->gdev && gpiod_free_commit(desc)) { |
fdeb8e15 | 2270 | module_put(desc->gdev->owner); |
33a68e86 LW |
2271 | put_device(&desc->gdev->dev); |
2272 | } else { | |
77c2d792 | 2273 | WARN_ON(extra_checks); |
33a68e86 | 2274 | } |
d2876d08 | 2275 | } |
372e722e | 2276 | |
d2876d08 DB |
2277 | /** |
2278 | * gpiochip_is_requested - return string iff signal was requested | |
2279 | * @chip: controller managing the signal | |
2280 | * @offset: of signal within controller's 0..(ngpio - 1) range | |
2281 | * | |
2282 | * Returns NULL if the GPIO is not currently requested, else a string. | |
9c8318ff AC |
2283 | * The string returned is the label passed to gpio_request(); if none has been |
2284 | * passed it is a meaningless, non-NULL constant. | |
d2876d08 DB |
2285 | * |
2286 | * This function is for use by GPIO controller drivers. The label can | |
2287 | * help with diagnostics, and knowing that the signal is used as a GPIO | |
2288 | * can help avoid accidentally multiplexing it to another controller. | |
2289 | */ | |
2290 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) | |
2291 | { | |
6c0b4e6c | 2292 | struct gpio_desc *desc; |
d2876d08 | 2293 | |
48b5953e | 2294 | if (offset >= chip->ngpio) |
d2876d08 | 2295 | return NULL; |
6c0b4e6c | 2296 | |
1c3cdb18 | 2297 | desc = &chip->gpiodev->descs[offset]; |
6c0b4e6c | 2298 | |
372e722e | 2299 | if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) |
d2876d08 | 2300 | return NULL; |
372e722e | 2301 | return desc->label; |
d2876d08 DB |
2302 | } |
2303 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); | |
2304 | ||
77c2d792 MW |
2305 | /** |
2306 | * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor | |
950d55f5 TR |
2307 | * @chip: GPIO chip |
2308 | * @hwnum: hardware number of the GPIO for which to request the descriptor | |
77c2d792 MW |
2309 | * @label: label for the GPIO |
2310 | * | |
2311 | * Function allows GPIO chip drivers to request and use their own GPIO | |
2312 | * descriptors via gpiolib API. Difference to gpiod_request() is that this | |
2313 | * function will not increase reference count of the GPIO chip module. This | |
2314 | * allows the GPIO chip module to be unloaded as needed (we assume that the | |
2315 | * GPIO chip driver handles freeing the GPIOs it has requested). | |
950d55f5 TR |
2316 | * |
2317 | * Returns: | |
2318 | * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error | |
2319 | * code on failure. | |
77c2d792 | 2320 | */ |
abdc08a3 AC |
2321 | struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum, |
2322 | const char *label) | |
77c2d792 | 2323 | { |
abdc08a3 AC |
2324 | struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum); |
2325 | int err; | |
77c2d792 | 2326 | |
abdc08a3 AC |
2327 | if (IS_ERR(desc)) { |
2328 | chip_err(chip, "failed to get GPIO descriptor\n"); | |
2329 | return desc; | |
2330 | } | |
2331 | ||
fac9d885 | 2332 | err = gpiod_request_commit(desc, label); |
abdc08a3 AC |
2333 | if (err < 0) |
2334 | return ERR_PTR(err); | |
77c2d792 | 2335 | |
abdc08a3 | 2336 | return desc; |
77c2d792 | 2337 | } |
f7d4ad98 | 2338 | EXPORT_SYMBOL_GPL(gpiochip_request_own_desc); |
77c2d792 MW |
2339 | |
2340 | /** | |
2341 | * gpiochip_free_own_desc - Free GPIO requested by the chip driver | |
2342 | * @desc: GPIO descriptor to free | |
2343 | * | |
2344 | * Function frees the given GPIO requested previously with | |
2345 | * gpiochip_request_own_desc(). | |
2346 | */ | |
2347 | void gpiochip_free_own_desc(struct gpio_desc *desc) | |
2348 | { | |
2349 | if (desc) | |
fac9d885 | 2350 | gpiod_free_commit(desc); |
77c2d792 | 2351 | } |
f7d4ad98 | 2352 | EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); |
d2876d08 | 2353 | |
fdeb8e15 LW |
2354 | /* |
2355 | * Drivers MUST set GPIO direction before making get/set calls. In | |
d2876d08 DB |
2356 | * some cases this is done in early boot, before IRQs are enabled. |
2357 | * | |
2358 | * As a rule these aren't called more than once (except for drivers | |
2359 | * using the open-drain emulation idiom) so these are natural places | |
2360 | * to accumulate extra debugging checks. Note that we can't (yet) | |
2361 | * rely on gpio_request() having been called beforehand. | |
2362 | */ | |
2363 | ||
79a9becd AC |
2364 | /** |
2365 | * gpiod_direction_input - set the GPIO direction to input | |
2366 | * @desc: GPIO to set to input | |
2367 | * | |
2368 | * Set the direction of the passed GPIO to input, such as gpiod_get_value() can | |
2369 | * be called safely on it. | |
2370 | * | |
2371 | * Return 0 in case of success, else an error code. | |
2372 | */ | |
2373 | int gpiod_direction_input(struct gpio_desc *desc) | |
d2876d08 | 2374 | { |
d2876d08 | 2375 | struct gpio_chip *chip; |
d2876d08 DB |
2376 | int status = -EINVAL; |
2377 | ||
fdeb8e15 LW |
2378 | VALIDATE_DESC(desc); |
2379 | chip = desc->gdev->chip; | |
bcabdef1 | 2380 | |
be1a4b13 | 2381 | if (!chip->get || !chip->direction_input) { |
6424de5a MB |
2382 | gpiod_warn(desc, |
2383 | "%s: missing get() or direction_input() operations\n", | |
7589e59f | 2384 | __func__); |
be1a4b13 LW |
2385 | return -EIO; |
2386 | } | |
2387 | ||
d82da797 | 2388 | status = chip->direction_input(chip, gpio_chip_hwgpio(desc)); |
d2876d08 DB |
2389 | if (status == 0) |
2390 | clear_bit(FLAG_IS_OUT, &desc->flags); | |
3f397c21 | 2391 | |
372e722e | 2392 | trace_gpio_direction(desc_to_gpio(desc), 1, status); |
d82da797 | 2393 | |
d2876d08 DB |
2394 | return status; |
2395 | } | |
79a9becd | 2396 | EXPORT_SYMBOL_GPL(gpiod_direction_input); |
372e722e | 2397 | |
2956b5d9 MW |
2398 | static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset, |
2399 | enum pin_config_param mode) | |
2400 | { | |
2401 | unsigned long config = { PIN_CONF_PACKED(mode, 0) }; | |
2402 | ||
2403 | return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP; | |
2404 | } | |
2405 | ||
fac9d885 | 2406 | static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) |
d2876d08 | 2407 | { |
c663e5f5 | 2408 | struct gpio_chip *gc = desc->gdev->chip; |
ad17731d | 2409 | int val = !!value; |
c663e5f5 | 2410 | int ret; |
d2876d08 | 2411 | |
c663e5f5 | 2412 | if (!gc->set || !gc->direction_output) { |
6424de5a MB |
2413 | gpiod_warn(desc, |
2414 | "%s: missing set() or direction_output() operations\n", | |
2415 | __func__); | |
be1a4b13 LW |
2416 | return -EIO; |
2417 | } | |
2418 | ||
ad17731d | 2419 | ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val); |
c663e5f5 | 2420 | if (!ret) |
d2876d08 | 2421 | set_bit(FLAG_IS_OUT, &desc->flags); |
ad17731d | 2422 | trace_gpio_value(desc_to_gpio(desc), 0, val); |
c663e5f5 LW |
2423 | trace_gpio_direction(desc_to_gpio(desc), 0, ret); |
2424 | return ret; | |
d2876d08 | 2425 | } |
ef70bbe1 PZ |
2426 | |
2427 | /** | |
2428 | * gpiod_direction_output_raw - set the GPIO direction to output | |
2429 | * @desc: GPIO to set to output | |
2430 | * @value: initial output value of the GPIO | |
2431 | * | |
2432 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can | |
2433 | * be called safely on it. The initial value of the output must be specified | |
2434 | * as raw value on the physical line without regard for the ACTIVE_LOW status. | |
2435 | * | |
2436 | * Return 0 in case of success, else an error code. | |
2437 | */ | |
2438 | int gpiod_direction_output_raw(struct gpio_desc *desc, int value) | |
2439 | { | |
fdeb8e15 | 2440 | VALIDATE_DESC(desc); |
fac9d885 | 2441 | return gpiod_direction_output_raw_commit(desc, value); |
ef70bbe1 PZ |
2442 | } |
2443 | EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); | |
2444 | ||
2445 | /** | |
90df4fe0 | 2446 | * gpiod_direction_output - set the GPIO direction to output |
ef70bbe1 PZ |
2447 | * @desc: GPIO to set to output |
2448 | * @value: initial output value of the GPIO | |
2449 | * | |
2450 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can | |
2451 | * be called safely on it. The initial value of the output must be specified | |
2452 | * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | |
2453 | * account. | |
2454 | * | |
2455 | * Return 0 in case of success, else an error code. | |
2456 | */ | |
2457 | int gpiod_direction_output(struct gpio_desc *desc, int value) | |
2458 | { | |
02e47980 LW |
2459 | struct gpio_chip *gc = desc->gdev->chip; |
2460 | int ret; | |
2461 | ||
fdeb8e15 | 2462 | VALIDATE_DESC(desc); |
ef70bbe1 PZ |
2463 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
2464 | value = !value; | |
ad17731d LW |
2465 | else |
2466 | value = !!value; | |
02e47980 LW |
2467 | |
2468 | /* GPIOs used for IRQs shall not be set as output */ | |
2469 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { | |
2470 | gpiod_err(desc, | |
2471 | "%s: tried to set a GPIO tied to an IRQ as output\n", | |
2472 | __func__); | |
2473 | return -EIO; | |
2474 | } | |
2475 | ||
2476 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { | |
2477 | /* First see if we can enable open drain in hardware */ | |
2478 | ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc), | |
2479 | PIN_CONFIG_DRIVE_OPEN_DRAIN); | |
2480 | if (!ret) | |
2481 | goto set_output_value; | |
2482 | /* Emulate open drain by not actively driving the line high */ | |
2483 | if (value) | |
2484 | return gpiod_direction_input(desc); | |
2485 | } | |
2486 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { | |
2487 | ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc), | |
2488 | PIN_CONFIG_DRIVE_OPEN_SOURCE); | |
2489 | if (!ret) | |
2490 | goto set_output_value; | |
2491 | /* Emulate open source by not actively driving the line low */ | |
2492 | if (!value) | |
2493 | return gpiod_direction_input(desc); | |
2494 | } else { | |
2495 | gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc), | |
2496 | PIN_CONFIG_DRIVE_PUSH_PULL); | |
2497 | } | |
2498 | ||
2499 | set_output_value: | |
fac9d885 | 2500 | return gpiod_direction_output_raw_commit(desc, value); |
ef70bbe1 | 2501 | } |
79a9becd | 2502 | EXPORT_SYMBOL_GPL(gpiod_direction_output); |
d2876d08 | 2503 | |
c4b5be98 | 2504 | /** |
950d55f5 TR |
2505 | * gpiod_set_debounce - sets @debounce time for a GPIO |
2506 | * @desc: descriptor of the GPIO for which to set debounce time | |
2507 | * @debounce: debounce time in microseconds | |
65d87656 | 2508 | * |
950d55f5 TR |
2509 | * Returns: |
2510 | * 0 on success, %-ENOTSUPP if the controller doesn't support setting the | |
2511 | * debounce time. | |
c4b5be98 | 2512 | */ |
79a9becd | 2513 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
c4b5be98 | 2514 | { |
c4b5be98 | 2515 | struct gpio_chip *chip; |
2956b5d9 | 2516 | unsigned long config; |
c4b5be98 | 2517 | |
fdeb8e15 LW |
2518 | VALIDATE_DESC(desc); |
2519 | chip = desc->gdev->chip; | |
2956b5d9 | 2520 | if (!chip->set || !chip->set_config) { |
6424de5a | 2521 | gpiod_dbg(desc, |
2956b5d9 | 2522 | "%s: missing set() or set_config() operations\n", |
6424de5a | 2523 | __func__); |
65d87656 | 2524 | return -ENOTSUPP; |
be1a4b13 LW |
2525 | } |
2526 | ||
2956b5d9 MW |
2527 | config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce); |
2528 | return chip->set_config(chip, gpio_chip_hwgpio(desc), config); | |
c4b5be98 | 2529 | } |
79a9becd | 2530 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); |
372e722e | 2531 | |
79a9becd AC |
2532 | /** |
2533 | * gpiod_is_active_low - test whether a GPIO is active-low or not | |
2534 | * @desc: the gpio descriptor to test | |
2535 | * | |
2536 | * Returns 1 if the GPIO is active-low, 0 otherwise. | |
2537 | */ | |
2538 | int gpiod_is_active_low(const struct gpio_desc *desc) | |
372e722e | 2539 | { |
fdeb8e15 | 2540 | VALIDATE_DESC(desc); |
79a9becd | 2541 | return test_bit(FLAG_ACTIVE_LOW, &desc->flags); |
372e722e | 2542 | } |
79a9becd | 2543 | EXPORT_SYMBOL_GPL(gpiod_is_active_low); |
d2876d08 DB |
2544 | |
2545 | /* I/O calls are only valid after configuration completed; the relevant | |
2546 | * "is this a valid GPIO" error checks should already have been done. | |
2547 | * | |
2548 | * "Get" operations are often inlinable as reading a pin value register, | |
2549 | * and masking the relevant bit in that register. | |
2550 | * | |
2551 | * When "set" operations are inlinable, they involve writing that mask to | |
2552 | * one register to set a low value, or a different register to set it high. | |
2553 | * Otherwise locking is needed, so there may be little value to inlining. | |
2554 | * | |
2555 | *------------------------------------------------------------------------ | |
2556 | * | |
2557 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers | |
2558 | * have requested the GPIO. That can include implicit requesting by | |
2559 | * a direction setting call. Marking a gpio as requested locks its chip | |
2560 | * in memory, guaranteeing that these table lookups need no more locking | |
2561 | * and that gpiochip_remove() will fail. | |
2562 | * | |
2563 | * REVISIT when debugging, consider adding some instrumentation to ensure | |
2564 | * that the GPIO was actually requested. | |
2565 | */ | |
2566 | ||
fac9d885 | 2567 | static int gpiod_get_raw_value_commit(const struct gpio_desc *desc) |
d2876d08 DB |
2568 | { |
2569 | struct gpio_chip *chip; | |
372e722e | 2570 | int offset; |
e20538b8 | 2571 | int value; |
d2876d08 | 2572 | |
fdeb8e15 | 2573 | chip = desc->gdev->chip; |
372e722e | 2574 | offset = gpio_chip_hwgpio(desc); |
e20538b8 | 2575 | value = chip->get ? chip->get(chip, offset) : -EIO; |
723a6303 | 2576 | value = value < 0 ? value : !!value; |
372e722e | 2577 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
3f397c21 | 2578 | return value; |
d2876d08 | 2579 | } |
372e722e | 2580 | |
eec1d566 LW |
2581 | static int gpio_chip_get_multiple(struct gpio_chip *chip, |
2582 | unsigned long *mask, unsigned long *bits) | |
2583 | { | |
2584 | if (chip->get_multiple) { | |
2585 | return chip->get_multiple(chip, mask, bits); | |
2586 | } else if (chip->get) { | |
2587 | int i, value; | |
2588 | ||
2589 | for_each_set_bit(i, mask, chip->ngpio) { | |
2590 | value = chip->get(chip, i); | |
2591 | if (value < 0) | |
2592 | return value; | |
2593 | __assign_bit(i, bits, value); | |
2594 | } | |
2595 | return 0; | |
2596 | } | |
2597 | return -EIO; | |
2598 | } | |
2599 | ||
2600 | int gpiod_get_array_value_complex(bool raw, bool can_sleep, | |
2601 | unsigned int array_size, | |
2602 | struct gpio_desc **desc_array, | |
2603 | int *value_array) | |
2604 | { | |
2605 | int i = 0; | |
2606 | ||
2607 | while (i < array_size) { | |
2608 | struct gpio_chip *chip = desc_array[i]->gdev->chip; | |
2609 | unsigned long mask[BITS_TO_LONGS(chip->ngpio)]; | |
2610 | unsigned long bits[BITS_TO_LONGS(chip->ngpio)]; | |
2611 | int first, j, ret; | |
2612 | ||
2613 | if (!can_sleep) | |
2614 | WARN_ON(chip->can_sleep); | |
2615 | ||
2616 | /* collect all inputs belonging to the same chip */ | |
2617 | first = i; | |
2618 | memset(mask, 0, sizeof(mask)); | |
2619 | do { | |
2620 | const struct gpio_desc *desc = desc_array[i]; | |
2621 | int hwgpio = gpio_chip_hwgpio(desc); | |
2622 | ||
2623 | __set_bit(hwgpio, mask); | |
2624 | i++; | |
2625 | } while ((i < array_size) && | |
2626 | (desc_array[i]->gdev->chip == chip)); | |
2627 | ||
2628 | ret = gpio_chip_get_multiple(chip, mask, bits); | |
2629 | if (ret) | |
2630 | return ret; | |
2631 | ||
2632 | for (j = first; j < i; j++) { | |
2633 | const struct gpio_desc *desc = desc_array[j]; | |
2634 | int hwgpio = gpio_chip_hwgpio(desc); | |
2635 | int value = test_bit(hwgpio, bits); | |
2636 | ||
2637 | if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
2638 | value = !value; | |
2639 | value_array[j] = value; | |
2640 | trace_gpio_value(desc_to_gpio(desc), 1, value); | |
2641 | } | |
2642 | } | |
2643 | return 0; | |
2644 | } | |
2645 | ||
d2876d08 | 2646 | /** |
79a9becd AC |
2647 | * gpiod_get_raw_value() - return a gpio's raw value |
2648 | * @desc: gpio whose value will be returned | |
d2876d08 | 2649 | * |
79a9becd | 2650 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding |
e20538b8 | 2651 | * its ACTIVE_LOW status, or negative errno on failure. |
79a9becd AC |
2652 | * |
2653 | * This function should be called from contexts where we cannot sleep, and will | |
2654 | * complain if the GPIO chip functions potentially sleep. | |
d2876d08 | 2655 | */ |
79a9becd | 2656 | int gpiod_get_raw_value(const struct gpio_desc *desc) |
d2876d08 | 2657 | { |
fdeb8e15 | 2658 | VALIDATE_DESC(desc); |
e4e449e8 | 2659 | /* Should be using gpio_get_value_cansleep() */ |
fdeb8e15 | 2660 | WARN_ON(desc->gdev->chip->can_sleep); |
fac9d885 | 2661 | return gpiod_get_raw_value_commit(desc); |
d2876d08 | 2662 | } |
79a9becd | 2663 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value); |
372e722e | 2664 | |
79a9becd AC |
2665 | /** |
2666 | * gpiod_get_value() - return a gpio's value | |
2667 | * @desc: gpio whose value will be returned | |
2668 | * | |
2669 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into | |
e20538b8 | 2670 | * account, or negative errno on failure. |
79a9becd AC |
2671 | * |
2672 | * This function should be called from contexts where we cannot sleep, and will | |
2673 | * complain if the GPIO chip functions potentially sleep. | |
2674 | */ | |
2675 | int gpiod_get_value(const struct gpio_desc *desc) | |
372e722e | 2676 | { |
79a9becd | 2677 | int value; |
fdeb8e15 LW |
2678 | |
2679 | VALIDATE_DESC(desc); | |
79a9becd | 2680 | /* Should be using gpio_get_value_cansleep() */ |
fdeb8e15 | 2681 | WARN_ON(desc->gdev->chip->can_sleep); |
79a9becd | 2682 | |
fac9d885 | 2683 | value = gpiod_get_raw_value_commit(desc); |
e20538b8 BA |
2684 | if (value < 0) |
2685 | return value; | |
2686 | ||
79a9becd AC |
2687 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
2688 | value = !value; | |
2689 | ||
2690 | return value; | |
372e722e | 2691 | } |
79a9becd | 2692 | EXPORT_SYMBOL_GPL(gpiod_get_value); |
d2876d08 | 2693 | |
eec1d566 LW |
2694 | /** |
2695 | * gpiod_get_raw_array_value() - read raw values from an array of GPIOs | |
2696 | * @array_size: number of elements in the descriptor / value arrays | |
2697 | * @desc_array: array of GPIO descriptors whose values will be read | |
2698 | * @value_array: array to store the read values | |
2699 | * | |
2700 | * Read the raw values of the GPIOs, i.e. the values of the physical lines | |
2701 | * without regard for their ACTIVE_LOW status. Return 0 in case of success, | |
2702 | * else an error code. | |
2703 | * | |
2704 | * This function should be called from contexts where we cannot sleep, | |
2705 | * and it will complain if the GPIO chip functions potentially sleep. | |
2706 | */ | |
2707 | int gpiod_get_raw_array_value(unsigned int array_size, | |
2708 | struct gpio_desc **desc_array, int *value_array) | |
2709 | { | |
2710 | if (!desc_array) | |
2711 | return -EINVAL; | |
2712 | return gpiod_get_array_value_complex(true, false, array_size, | |
2713 | desc_array, value_array); | |
2714 | } | |
2715 | EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value); | |
2716 | ||
2717 | /** | |
2718 | * gpiod_get_array_value() - read values from an array of GPIOs | |
2719 | * @array_size: number of elements in the descriptor / value arrays | |
2720 | * @desc_array: array of GPIO descriptors whose values will be read | |
2721 | * @value_array: array to store the read values | |
2722 | * | |
2723 | * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status | |
2724 | * into account. Return 0 in case of success, else an error code. | |
2725 | * | |
2726 | * This function should be called from contexts where we cannot sleep, | |
2727 | * and it will complain if the GPIO chip functions potentially sleep. | |
2728 | */ | |
2729 | int gpiod_get_array_value(unsigned int array_size, | |
2730 | struct gpio_desc **desc_array, int *value_array) | |
2731 | { | |
2732 | if (!desc_array) | |
2733 | return -EINVAL; | |
2734 | return gpiod_get_array_value_complex(false, false, array_size, | |
2735 | desc_array, value_array); | |
2736 | } | |
2737 | EXPORT_SYMBOL_GPL(gpiod_get_array_value); | |
2738 | ||
aca5ce14 | 2739 | /* |
fac9d885 | 2740 | * gpio_set_open_drain_value_commit() - Set the open drain gpio's value. |
79a9becd | 2741 | * @desc: gpio descriptor whose state need to be set. |
20a8a968 | 2742 | * @value: Non-zero for setting it HIGH otherwise it will set to LOW. |
aca5ce14 | 2743 | */ |
fac9d885 | 2744 | static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value) |
aca5ce14 LD |
2745 | { |
2746 | int err = 0; | |
fdeb8e15 | 2747 | struct gpio_chip *chip = desc->gdev->chip; |
372e722e AC |
2748 | int offset = gpio_chip_hwgpio(desc); |
2749 | ||
aca5ce14 | 2750 | if (value) { |
372e722e | 2751 | err = chip->direction_input(chip, offset); |
aca5ce14 | 2752 | if (!err) |
372e722e | 2753 | clear_bit(FLAG_IS_OUT, &desc->flags); |
aca5ce14 | 2754 | } else { |
372e722e | 2755 | err = chip->direction_output(chip, offset, 0); |
aca5ce14 | 2756 | if (!err) |
372e722e | 2757 | set_bit(FLAG_IS_OUT, &desc->flags); |
aca5ce14 | 2758 | } |
372e722e | 2759 | trace_gpio_direction(desc_to_gpio(desc), value, err); |
aca5ce14 | 2760 | if (err < 0) |
6424de5a MB |
2761 | gpiod_err(desc, |
2762 | "%s: Error in set_value for open drain err %d\n", | |
2763 | __func__, err); | |
aca5ce14 LD |
2764 | } |
2765 | ||
25553ff0 | 2766 | /* |
79a9becd AC |
2767 | * _gpio_set_open_source_value() - Set the open source gpio's value. |
2768 | * @desc: gpio descriptor whose state need to be set. | |
20a8a968 | 2769 | * @value: Non-zero for setting it HIGH otherwise it will set to LOW. |
25553ff0 | 2770 | */ |
fac9d885 | 2771 | static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value) |
25553ff0 LD |
2772 | { |
2773 | int err = 0; | |
fdeb8e15 | 2774 | struct gpio_chip *chip = desc->gdev->chip; |
372e722e AC |
2775 | int offset = gpio_chip_hwgpio(desc); |
2776 | ||
25553ff0 | 2777 | if (value) { |
372e722e | 2778 | err = chip->direction_output(chip, offset, 1); |
25553ff0 | 2779 | if (!err) |
372e722e | 2780 | set_bit(FLAG_IS_OUT, &desc->flags); |
25553ff0 | 2781 | } else { |
372e722e | 2782 | err = chip->direction_input(chip, offset); |
25553ff0 | 2783 | if (!err) |
372e722e | 2784 | clear_bit(FLAG_IS_OUT, &desc->flags); |
25553ff0 | 2785 | } |
372e722e | 2786 | trace_gpio_direction(desc_to_gpio(desc), !value, err); |
25553ff0 | 2787 | if (err < 0) |
6424de5a MB |
2788 | gpiod_err(desc, |
2789 | "%s: Error in set_value for open source err %d\n", | |
2790 | __func__, err); | |
25553ff0 LD |
2791 | } |
2792 | ||
fac9d885 | 2793 | static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value) |
d2876d08 DB |
2794 | { |
2795 | struct gpio_chip *chip; | |
2796 | ||
fdeb8e15 | 2797 | chip = desc->gdev->chip; |
372e722e | 2798 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
02e47980 | 2799 | chip->set(chip, gpio_chip_hwgpio(desc), value); |
372e722e AC |
2800 | } |
2801 | ||
5f424243 RI |
2802 | /* |
2803 | * set multiple outputs on the same chip; | |
2804 | * use the chip's set_multiple function if available; | |
2805 | * otherwise set the outputs sequentially; | |
2806 | * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word | |
2807 | * defines which outputs are to be changed | |
2808 | * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word | |
2809 | * defines the values the outputs specified by mask are to be set to | |
2810 | */ | |
2811 | static void gpio_chip_set_multiple(struct gpio_chip *chip, | |
2812 | unsigned long *mask, unsigned long *bits) | |
2813 | { | |
2814 | if (chip->set_multiple) { | |
2815 | chip->set_multiple(chip, mask, bits); | |
2816 | } else { | |
5e4e6fb3 AS |
2817 | unsigned int i; |
2818 | ||
2819 | /* set outputs if the corresponding mask bit is set */ | |
2820 | for_each_set_bit(i, mask, chip->ngpio) | |
2821 | chip->set(chip, i, test_bit(i, bits)); | |
5f424243 RI |
2822 | } |
2823 | } | |
2824 | ||
44c7288f LW |
2825 | void gpiod_set_array_value_complex(bool raw, bool can_sleep, |
2826 | unsigned int array_size, | |
2827 | struct gpio_desc **desc_array, | |
2828 | int *value_array) | |
5f424243 RI |
2829 | { |
2830 | int i = 0; | |
2831 | ||
2832 | while (i < array_size) { | |
fdeb8e15 | 2833 | struct gpio_chip *chip = desc_array[i]->gdev->chip; |
5f424243 RI |
2834 | unsigned long mask[BITS_TO_LONGS(chip->ngpio)]; |
2835 | unsigned long bits[BITS_TO_LONGS(chip->ngpio)]; | |
2836 | int count = 0; | |
2837 | ||
38e003f4 | 2838 | if (!can_sleep) |
5f424243 | 2839 | WARN_ON(chip->can_sleep); |
38e003f4 | 2840 | |
5f424243 RI |
2841 | memset(mask, 0, sizeof(mask)); |
2842 | do { | |
2843 | struct gpio_desc *desc = desc_array[i]; | |
2844 | int hwgpio = gpio_chip_hwgpio(desc); | |
2845 | int value = value_array[i]; | |
2846 | ||
2847 | if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
2848 | value = !value; | |
2849 | trace_gpio_value(desc_to_gpio(desc), 0, value); | |
2850 | /* | |
2851 | * collect all normal outputs belonging to the same chip | |
2852 | * open drain and open source outputs are set individually | |
2853 | */ | |
02e47980 | 2854 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) { |
fac9d885 | 2855 | gpio_set_open_drain_value_commit(desc, value); |
02e47980 | 2856 | } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) { |
fac9d885 | 2857 | gpio_set_open_source_value_commit(desc, value); |
5f424243 RI |
2858 | } else { |
2859 | __set_bit(hwgpio, mask); | |
38e003f4 | 2860 | if (value) |
5f424243 | 2861 | __set_bit(hwgpio, bits); |
38e003f4 | 2862 | else |
5f424243 | 2863 | __clear_bit(hwgpio, bits); |
5f424243 RI |
2864 | count++; |
2865 | } | |
2866 | i++; | |
fdeb8e15 LW |
2867 | } while ((i < array_size) && |
2868 | (desc_array[i]->gdev->chip == chip)); | |
5f424243 | 2869 | /* push collected bits to outputs */ |
38e003f4 | 2870 | if (count != 0) |
5f424243 | 2871 | gpio_chip_set_multiple(chip, mask, bits); |
5f424243 RI |
2872 | } |
2873 | } | |
2874 | ||
d2876d08 | 2875 | /** |
79a9becd AC |
2876 | * gpiod_set_raw_value() - assign a gpio's raw value |
2877 | * @desc: gpio whose value will be assigned | |
d2876d08 | 2878 | * @value: value to assign |
d2876d08 | 2879 | * |
79a9becd AC |
2880 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
2881 | * regard for its ACTIVE_LOW status. | |
2882 | * | |
2883 | * This function should be called from contexts where we cannot sleep, and will | |
2884 | * complain if the GPIO chip functions potentially sleep. | |
d2876d08 | 2885 | */ |
79a9becd | 2886 | void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
372e722e | 2887 | { |
fdeb8e15 | 2888 | VALIDATE_DESC_VOID(desc); |
1cfab8f8 | 2889 | /* Should be using gpiod_set_value_cansleep() */ |
fdeb8e15 | 2890 | WARN_ON(desc->gdev->chip->can_sleep); |
fac9d885 | 2891 | gpiod_set_raw_value_commit(desc, value); |
d2876d08 | 2892 | } |
79a9becd | 2893 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value); |
d2876d08 | 2894 | |
1e77fc82 GU |
2895 | /** |
2896 | * gpiod_set_value_nocheck() - set a GPIO line value without checking | |
2897 | * @desc: the descriptor to set the value on | |
2898 | * @value: value to set | |
2899 | * | |
2900 | * This sets the value of a GPIO line backing a descriptor, applying | |
2901 | * different semantic quirks like active low and open drain/source | |
2902 | * handling. | |
2903 | */ | |
2904 | static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value) | |
2905 | { | |
2906 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
2907 | value = !value; | |
2908 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | |
2909 | gpio_set_open_drain_value_commit(desc, value); | |
2910 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | |
2911 | gpio_set_open_source_value_commit(desc, value); | |
2912 | else | |
2913 | gpiod_set_raw_value_commit(desc, value); | |
2914 | } | |
2915 | ||
d2876d08 | 2916 | /** |
79a9becd AC |
2917 | * gpiod_set_value() - assign a gpio's value |
2918 | * @desc: gpio whose value will be assigned | |
2919 | * @value: value to assign | |
2920 | * | |
02e47980 LW |
2921 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW, |
2922 | * OPEN_DRAIN and OPEN_SOURCE flags into account. | |
d2876d08 | 2923 | * |
79a9becd AC |
2924 | * This function should be called from contexts where we cannot sleep, and will |
2925 | * complain if the GPIO chip functions potentially sleep. | |
d2876d08 | 2926 | */ |
79a9becd | 2927 | void gpiod_set_value(struct gpio_desc *desc, int value) |
d2876d08 | 2928 | { |
fdeb8e15 | 2929 | VALIDATE_DESC_VOID(desc); |
fdeb8e15 | 2930 | WARN_ON(desc->gdev->chip->can_sleep); |
1e77fc82 | 2931 | gpiod_set_value_nocheck(desc, value); |
372e722e | 2932 | } |
79a9becd | 2933 | EXPORT_SYMBOL_GPL(gpiod_set_value); |
d2876d08 | 2934 | |
5f424243 | 2935 | /** |
3fff99bc | 2936 | * gpiod_set_raw_array_value() - assign values to an array of GPIOs |
5f424243 RI |
2937 | * @array_size: number of elements in the descriptor / value arrays |
2938 | * @desc_array: array of GPIO descriptors whose values will be assigned | |
2939 | * @value_array: array of values to assign | |
2940 | * | |
2941 | * Set the raw values of the GPIOs, i.e. the values of the physical lines | |
2942 | * without regard for their ACTIVE_LOW status. | |
2943 | * | |
2944 | * This function should be called from contexts where we cannot sleep, and will | |
2945 | * complain if the GPIO chip functions potentially sleep. | |
2946 | */ | |
3fff99bc | 2947 | void gpiod_set_raw_array_value(unsigned int array_size, |
5f424243 RI |
2948 | struct gpio_desc **desc_array, int *value_array) |
2949 | { | |
2950 | if (!desc_array) | |
2951 | return; | |
44c7288f LW |
2952 | gpiod_set_array_value_complex(true, false, array_size, desc_array, |
2953 | value_array); | |
5f424243 | 2954 | } |
3fff99bc | 2955 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value); |
5f424243 RI |
2956 | |
2957 | /** | |
3fff99bc | 2958 | * gpiod_set_array_value() - assign values to an array of GPIOs |
5f424243 RI |
2959 | * @array_size: number of elements in the descriptor / value arrays |
2960 | * @desc_array: array of GPIO descriptors whose values will be assigned | |
2961 | * @value_array: array of values to assign | |
2962 | * | |
2963 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status | |
2964 | * into account. | |
2965 | * | |
2966 | * This function should be called from contexts where we cannot sleep, and will | |
2967 | * complain if the GPIO chip functions potentially sleep. | |
2968 | */ | |
3fff99bc RI |
2969 | void gpiod_set_array_value(unsigned int array_size, |
2970 | struct gpio_desc **desc_array, int *value_array) | |
5f424243 RI |
2971 | { |
2972 | if (!desc_array) | |
2973 | return; | |
44c7288f LW |
2974 | gpiod_set_array_value_complex(false, false, array_size, desc_array, |
2975 | value_array); | |
5f424243 | 2976 | } |
3fff99bc | 2977 | EXPORT_SYMBOL_GPL(gpiod_set_array_value); |
5f424243 | 2978 | |
d2876d08 | 2979 | /** |
79a9becd AC |
2980 | * gpiod_cansleep() - report whether gpio value access may sleep |
2981 | * @desc: gpio to check | |
d2876d08 | 2982 | * |
d2876d08 | 2983 | */ |
79a9becd | 2984 | int gpiod_cansleep(const struct gpio_desc *desc) |
372e722e | 2985 | { |
fdeb8e15 LW |
2986 | VALIDATE_DESC(desc); |
2987 | return desc->gdev->chip->can_sleep; | |
d2876d08 | 2988 | } |
79a9becd | 2989 | EXPORT_SYMBOL_GPL(gpiod_cansleep); |
d2876d08 | 2990 | |
0f6d504e | 2991 | /** |
79a9becd AC |
2992 | * gpiod_to_irq() - return the IRQ corresponding to a GPIO |
2993 | * @desc: gpio whose IRQ will be returned (already requested) | |
0f6d504e | 2994 | * |
79a9becd AC |
2995 | * Return the IRQ corresponding to the passed GPIO, or an error code in case of |
2996 | * error. | |
0f6d504e | 2997 | */ |
79a9becd | 2998 | int gpiod_to_irq(const struct gpio_desc *desc) |
0f6d504e | 2999 | { |
4c37ce86 LW |
3000 | struct gpio_chip *chip; |
3001 | int offset; | |
0f6d504e | 3002 | |
79bb71bd LW |
3003 | /* |
3004 | * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics | |
3005 | * requires this function to not return zero on an invalid descriptor | |
3006 | * but rather a negative error number. | |
3007 | */ | |
bfbbe44d | 3008 | if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip) |
79bb71bd LW |
3009 | return -EINVAL; |
3010 | ||
fdeb8e15 | 3011 | chip = desc->gdev->chip; |
372e722e | 3012 | offset = gpio_chip_hwgpio(desc); |
4c37ce86 LW |
3013 | if (chip->to_irq) { |
3014 | int retirq = chip->to_irq(chip, offset); | |
3015 | ||
3016 | /* Zero means NO_IRQ */ | |
3017 | if (!retirq) | |
3018 | return -ENXIO; | |
3019 | ||
3020 | return retirq; | |
3021 | } | |
3022 | return -ENXIO; | |
0f6d504e | 3023 | } |
79a9becd | 3024 | EXPORT_SYMBOL_GPL(gpiod_to_irq); |
0f6d504e | 3025 | |
d468bf9e | 3026 | /** |
e3a2e878 | 3027 | * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ |
d74be6df AC |
3028 | * @chip: the chip the GPIO to lock belongs to |
3029 | * @offset: the offset of the GPIO to lock as IRQ | |
d468bf9e LW |
3030 | * |
3031 | * This is used directly by GPIO drivers that want to lock down | |
f438acdf | 3032 | * a certain GPIO line to be used for IRQs. |
d468bf9e | 3033 | */ |
e3a2e878 | 3034 | int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset) |
372e722e | 3035 | { |
9c10280d LW |
3036 | struct gpio_desc *desc; |
3037 | ||
3038 | desc = gpiochip_get_desc(chip, offset); | |
3039 | if (IS_ERR(desc)) | |
3040 | return PTR_ERR(desc); | |
3041 | ||
60f8339e LW |
3042 | /* |
3043 | * If it's fast: flush the direction setting if something changed | |
3044 | * behind our back | |
3045 | */ | |
3046 | if (!chip->can_sleep && chip->get_direction) { | |
9c10280d LW |
3047 | int dir = chip->get_direction(chip, offset); |
3048 | ||
3049 | if (dir) | |
3050 | clear_bit(FLAG_IS_OUT, &desc->flags); | |
3051 | else | |
3052 | set_bit(FLAG_IS_OUT, &desc->flags); | |
3053 | } | |
d468bf9e | 3054 | |
9c10280d | 3055 | if (test_bit(FLAG_IS_OUT, &desc->flags)) { |
d74be6df | 3056 | chip_err(chip, |
d468bf9e LW |
3057 | "%s: tried to flag a GPIO set as output for IRQ\n", |
3058 | __func__); | |
3059 | return -EIO; | |
3060 | } | |
3061 | ||
9c10280d | 3062 | set_bit(FLAG_USED_AS_IRQ, &desc->flags); |
3940c34a LW |
3063 | |
3064 | /* | |
3065 | * If the consumer has not set up a label (such as when the | |
3066 | * IRQ is referenced from .to_irq()) we set up a label here | |
3067 | * so it is clear this is used as an interrupt. | |
3068 | */ | |
3069 | if (!desc->label) | |
3070 | desc_set_label(desc, "interrupt"); | |
3071 | ||
d468bf9e | 3072 | return 0; |
372e722e | 3073 | } |
e3a2e878 | 3074 | EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq); |
d2876d08 | 3075 | |
d468bf9e | 3076 | /** |
e3a2e878 | 3077 | * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ |
d74be6df AC |
3078 | * @chip: the chip the GPIO to lock belongs to |
3079 | * @offset: the offset of the GPIO to lock as IRQ | |
d468bf9e LW |
3080 | * |
3081 | * This is used directly by GPIO drivers that want to indicate | |
3082 | * that a certain GPIO is no longer used exclusively for IRQ. | |
d2876d08 | 3083 | */ |
e3a2e878 | 3084 | void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) |
d468bf9e | 3085 | { |
3940c34a LW |
3086 | struct gpio_desc *desc; |
3087 | ||
3088 | desc = gpiochip_get_desc(chip, offset); | |
3089 | if (IS_ERR(desc)) | |
d468bf9e | 3090 | return; |
d2876d08 | 3091 | |
3940c34a LW |
3092 | clear_bit(FLAG_USED_AS_IRQ, &desc->flags); |
3093 | ||
3094 | /* If we only had this marking, erase it */ | |
3095 | if (desc->label && !strcmp(desc->label, "interrupt")) | |
3096 | desc_set_label(desc, NULL); | |
d468bf9e | 3097 | } |
e3a2e878 | 3098 | EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq); |
d468bf9e | 3099 | |
6cee3821 LW |
3100 | bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset) |
3101 | { | |
3102 | if (offset >= chip->ngpio) | |
3103 | return false; | |
3104 | ||
3105 | return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags); | |
3106 | } | |
3107 | EXPORT_SYMBOL_GPL(gpiochip_line_is_irq); | |
3108 | ||
143b65d6 LW |
3109 | bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset) |
3110 | { | |
3111 | if (offset >= chip->ngpio) | |
3112 | return false; | |
3113 | ||
3114 | return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags); | |
3115 | } | |
3116 | EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain); | |
3117 | ||
3118 | bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset) | |
3119 | { | |
3120 | if (offset >= chip->ngpio) | |
3121 | return false; | |
3122 | ||
3123 | return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags); | |
3124 | } | |
3125 | EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source); | |
3126 | ||
05f479bf CK |
3127 | bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset) |
3128 | { | |
3129 | if (offset >= chip->ngpio) | |
3130 | return false; | |
3131 | ||
2cbfca66 | 3132 | return !test_bit(FLAG_SLEEP_MAY_LOSE_VALUE, |
05f479bf CK |
3133 | &chip->gpiodev->descs[offset].flags); |
3134 | } | |
3135 | EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent); | |
3136 | ||
79a9becd AC |
3137 | /** |
3138 | * gpiod_get_raw_value_cansleep() - return a gpio's raw value | |
3139 | * @desc: gpio whose value will be returned | |
3140 | * | |
3141 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding | |
e20538b8 | 3142 | * its ACTIVE_LOW status, or negative errno on failure. |
79a9becd AC |
3143 | * |
3144 | * This function is to be called from contexts that can sleep. | |
d2876d08 | 3145 | */ |
79a9becd | 3146 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) |
d2876d08 | 3147 | { |
d2876d08 | 3148 | might_sleep_if(extra_checks); |
fdeb8e15 | 3149 | VALIDATE_DESC(desc); |
fac9d885 | 3150 | return gpiod_get_raw_value_commit(desc); |
d2876d08 | 3151 | } |
79a9becd | 3152 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); |
372e722e | 3153 | |
79a9becd AC |
3154 | /** |
3155 | * gpiod_get_value_cansleep() - return a gpio's value | |
3156 | * @desc: gpio whose value will be returned | |
3157 | * | |
3158 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into | |
e20538b8 | 3159 | * account, or negative errno on failure. |
79a9becd AC |
3160 | * |
3161 | * This function is to be called from contexts that can sleep. | |
3162 | */ | |
3163 | int gpiod_get_value_cansleep(const struct gpio_desc *desc) | |
d2876d08 | 3164 | { |
3f397c21 | 3165 | int value; |
d2876d08 DB |
3166 | |
3167 | might_sleep_if(extra_checks); | |
fdeb8e15 | 3168 | VALIDATE_DESC(desc); |
fac9d885 | 3169 | value = gpiod_get_raw_value_commit(desc); |
e20538b8 BA |
3170 | if (value < 0) |
3171 | return value; | |
3172 | ||
79a9becd AC |
3173 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
3174 | value = !value; | |
3175 | ||
3f397c21 | 3176 | return value; |
d2876d08 | 3177 | } |
79a9becd | 3178 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); |
372e722e | 3179 | |
eec1d566 LW |
3180 | /** |
3181 | * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs | |
3182 | * @array_size: number of elements in the descriptor / value arrays | |
3183 | * @desc_array: array of GPIO descriptors whose values will be read | |
3184 | * @value_array: array to store the read values | |
3185 | * | |
3186 | * Read the raw values of the GPIOs, i.e. the values of the physical lines | |
3187 | * without regard for their ACTIVE_LOW status. Return 0 in case of success, | |
3188 | * else an error code. | |
3189 | * | |
3190 | * This function is to be called from contexts that can sleep. | |
3191 | */ | |
3192 | int gpiod_get_raw_array_value_cansleep(unsigned int array_size, | |
3193 | struct gpio_desc **desc_array, | |
3194 | int *value_array) | |
3195 | { | |
3196 | might_sleep_if(extra_checks); | |
3197 | if (!desc_array) | |
3198 | return -EINVAL; | |
3199 | return gpiod_get_array_value_complex(true, true, array_size, | |
3200 | desc_array, value_array); | |
3201 | } | |
3202 | EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep); | |
3203 | ||
3204 | /** | |
3205 | * gpiod_get_array_value_cansleep() - read values from an array of GPIOs | |
3206 | * @array_size: number of elements in the descriptor / value arrays | |
3207 | * @desc_array: array of GPIO descriptors whose values will be read | |
3208 | * @value_array: array to store the read values | |
3209 | * | |
3210 | * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status | |
3211 | * into account. Return 0 in case of success, else an error code. | |
3212 | * | |
3213 | * This function is to be called from contexts that can sleep. | |
3214 | */ | |
3215 | int gpiod_get_array_value_cansleep(unsigned int array_size, | |
3216 | struct gpio_desc **desc_array, | |
3217 | int *value_array) | |
3218 | { | |
3219 | might_sleep_if(extra_checks); | |
3220 | if (!desc_array) | |
3221 | return -EINVAL; | |
3222 | return gpiod_get_array_value_complex(false, true, array_size, | |
3223 | desc_array, value_array); | |
3224 | } | |
3225 | EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep); | |
3226 | ||
79a9becd AC |
3227 | /** |
3228 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value | |
3229 | * @desc: gpio whose value will be assigned | |
3230 | * @value: value to assign | |
3231 | * | |
3232 | * Set the raw value of the GPIO, i.e. the value of its physical line without | |
3233 | * regard for its ACTIVE_LOW status. | |
3234 | * | |
3235 | * This function is to be called from contexts that can sleep. | |
3236 | */ | |
3237 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) | |
372e722e | 3238 | { |
d2876d08 | 3239 | might_sleep_if(extra_checks); |
fdeb8e15 | 3240 | VALIDATE_DESC_VOID(desc); |
fac9d885 | 3241 | gpiod_set_raw_value_commit(desc, value); |
372e722e | 3242 | } |
79a9becd | 3243 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); |
d2876d08 | 3244 | |
79a9becd AC |
3245 | /** |
3246 | * gpiod_set_value_cansleep() - assign a gpio's value | |
3247 | * @desc: gpio whose value will be assigned | |
3248 | * @value: value to assign | |
3249 | * | |
3250 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | |
3251 | * account | |
3252 | * | |
3253 | * This function is to be called from contexts that can sleep. | |
3254 | */ | |
3255 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) | |
d2876d08 | 3256 | { |
d2876d08 | 3257 | might_sleep_if(extra_checks); |
fdeb8e15 | 3258 | VALIDATE_DESC_VOID(desc); |
1e77fc82 | 3259 | gpiod_set_value_nocheck(desc, value); |
372e722e | 3260 | } |
79a9becd | 3261 | EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); |
d2876d08 | 3262 | |
5f424243 | 3263 | /** |
3fff99bc | 3264 | * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs |
5f424243 RI |
3265 | * @array_size: number of elements in the descriptor / value arrays |
3266 | * @desc_array: array of GPIO descriptors whose values will be assigned | |
3267 | * @value_array: array of values to assign | |
3268 | * | |
3269 | * Set the raw values of the GPIOs, i.e. the values of the physical lines | |
3270 | * without regard for their ACTIVE_LOW status. | |
3271 | * | |
3272 | * This function is to be called from contexts that can sleep. | |
3273 | */ | |
3fff99bc RI |
3274 | void gpiod_set_raw_array_value_cansleep(unsigned int array_size, |
3275 | struct gpio_desc **desc_array, | |
3276 | int *value_array) | |
5f424243 RI |
3277 | { |
3278 | might_sleep_if(extra_checks); | |
3279 | if (!desc_array) | |
3280 | return; | |
44c7288f LW |
3281 | gpiod_set_array_value_complex(true, true, array_size, desc_array, |
3282 | value_array); | |
5f424243 | 3283 | } |
3fff99bc | 3284 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep); |
5f424243 | 3285 | |
3946d187 DT |
3286 | /** |
3287 | * gpiod_add_lookup_tables() - register GPIO device consumers | |
3288 | * @tables: list of tables of consumers to register | |
3289 | * @n: number of tables in the list | |
3290 | */ | |
3291 | void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n) | |
3292 | { | |
3293 | unsigned int i; | |
3294 | ||
3295 | mutex_lock(&gpio_lookup_lock); | |
3296 | ||
3297 | for (i = 0; i < n; i++) | |
3298 | list_add_tail(&tables[i]->list, &gpio_lookup_list); | |
3299 | ||
3300 | mutex_unlock(&gpio_lookup_lock); | |
3301 | } | |
3302 | ||
5f424243 | 3303 | /** |
3fff99bc | 3304 | * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs |
5f424243 RI |
3305 | * @array_size: number of elements in the descriptor / value arrays |
3306 | * @desc_array: array of GPIO descriptors whose values will be assigned | |
3307 | * @value_array: array of values to assign | |
3308 | * | |
3309 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status | |
3310 | * into account. | |
3311 | * | |
3312 | * This function is to be called from contexts that can sleep. | |
3313 | */ | |
3fff99bc RI |
3314 | void gpiod_set_array_value_cansleep(unsigned int array_size, |
3315 | struct gpio_desc **desc_array, | |
3316 | int *value_array) | |
5f424243 RI |
3317 | { |
3318 | might_sleep_if(extra_checks); | |
3319 | if (!desc_array) | |
3320 | return; | |
44c7288f LW |
3321 | gpiod_set_array_value_complex(false, true, array_size, desc_array, |
3322 | value_array); | |
5f424243 | 3323 | } |
3fff99bc | 3324 | EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep); |
5f424243 | 3325 | |
bae48da2 | 3326 | /** |
ad824783 AC |
3327 | * gpiod_add_lookup_table() - register GPIO device consumers |
3328 | * @table: table of consumers to register | |
bae48da2 | 3329 | */ |
ad824783 | 3330 | void gpiod_add_lookup_table(struct gpiod_lookup_table *table) |
bae48da2 AC |
3331 | { |
3332 | mutex_lock(&gpio_lookup_lock); | |
3333 | ||
ad824783 | 3334 | list_add_tail(&table->list, &gpio_lookup_list); |
bae48da2 AC |
3335 | |
3336 | mutex_unlock(&gpio_lookup_lock); | |
3337 | } | |
226b2242 | 3338 | EXPORT_SYMBOL_GPL(gpiod_add_lookup_table); |
bae48da2 | 3339 | |
be9015ab SK |
3340 | /** |
3341 | * gpiod_remove_lookup_table() - unregister GPIO device consumers | |
3342 | * @table: table of consumers to unregister | |
3343 | */ | |
3344 | void gpiod_remove_lookup_table(struct gpiod_lookup_table *table) | |
3345 | { | |
3346 | mutex_lock(&gpio_lookup_lock); | |
3347 | ||
3348 | list_del(&table->list); | |
3349 | ||
3350 | mutex_unlock(&gpio_lookup_lock); | |
3351 | } | |
226b2242 | 3352 | EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table); |
be9015ab | 3353 | |
ad824783 | 3354 | static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) |
bae48da2 AC |
3355 | { |
3356 | const char *dev_id = dev ? dev_name(dev) : NULL; | |
ad824783 | 3357 | struct gpiod_lookup_table *table; |
bae48da2 AC |
3358 | |
3359 | mutex_lock(&gpio_lookup_lock); | |
3360 | ||
ad824783 AC |
3361 | list_for_each_entry(table, &gpio_lookup_list, list) { |
3362 | if (table->dev_id && dev_id) { | |
3363 | /* | |
3364 | * Valid strings on both ends, must be identical to have | |
3365 | * a match | |
3366 | */ | |
3367 | if (!strcmp(table->dev_id, dev_id)) | |
3368 | goto found; | |
3369 | } else { | |
3370 | /* | |
3371 | * One of the pointers is NULL, so both must be to have | |
3372 | * a match | |
3373 | */ | |
3374 | if (dev_id == table->dev_id) | |
3375 | goto found; | |
3376 | } | |
3377 | } | |
3378 | table = NULL; | |
bae48da2 | 3379 | |
ad824783 AC |
3380 | found: |
3381 | mutex_unlock(&gpio_lookup_lock); | |
3382 | return table; | |
3383 | } | |
bae48da2 | 3384 | |
ad824783 AC |
3385 | static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, |
3386 | unsigned int idx, | |
3387 | enum gpio_lookup_flags *flags) | |
3388 | { | |
2a3cf6a3 | 3389 | struct gpio_desc *desc = ERR_PTR(-ENOENT); |
ad824783 AC |
3390 | struct gpiod_lookup_table *table; |
3391 | struct gpiod_lookup *p; | |
bae48da2 | 3392 | |
ad824783 AC |
3393 | table = gpiod_find_lookup_table(dev); |
3394 | if (!table) | |
3395 | return desc; | |
bae48da2 | 3396 | |
ad824783 AC |
3397 | for (p = &table->table[0]; p->chip_label; p++) { |
3398 | struct gpio_chip *chip; | |
bae48da2 | 3399 | |
ad824783 | 3400 | /* idx must always match exactly */ |
bae48da2 AC |
3401 | if (p->idx != idx) |
3402 | continue; | |
3403 | ||
ad824783 AC |
3404 | /* If the lookup entry has a con_id, require exact match */ |
3405 | if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) | |
3406 | continue; | |
bae48da2 | 3407 | |
ad824783 | 3408 | chip = find_chip_by_name(p->chip_label); |
bae48da2 | 3409 | |
ad824783 | 3410 | if (!chip) { |
2a3cf6a3 AC |
3411 | dev_err(dev, "cannot find GPIO chip %s\n", |
3412 | p->chip_label); | |
3413 | return ERR_PTR(-ENODEV); | |
ad824783 | 3414 | } |
bae48da2 | 3415 | |
ad824783 | 3416 | if (chip->ngpio <= p->chip_hwnum) { |
2a3cf6a3 AC |
3417 | dev_err(dev, |
3418 | "requested GPIO %d is out of range [0..%d] for chip %s\n", | |
3419 | idx, chip->ngpio, chip->label); | |
3420 | return ERR_PTR(-EINVAL); | |
bae48da2 | 3421 | } |
bae48da2 | 3422 | |
bb1e88cc | 3423 | desc = gpiochip_get_desc(chip, p->chip_hwnum); |
ad824783 | 3424 | *flags = p->flags; |
bae48da2 | 3425 | |
2a3cf6a3 | 3426 | return desc; |
bae48da2 AC |
3427 | } |
3428 | ||
bae48da2 AC |
3429 | return desc; |
3430 | } | |
3431 | ||
66858527 RI |
3432 | static int dt_gpio_count(struct device *dev, const char *con_id) |
3433 | { | |
3434 | int ret; | |
3435 | char propname[32]; | |
3436 | unsigned int i; | |
3437 | ||
3438 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { | |
3439 | if (con_id) | |
3440 | snprintf(propname, sizeof(propname), "%s-%s", | |
3441 | con_id, gpio_suffixes[i]); | |
3442 | else | |
3443 | snprintf(propname, sizeof(propname), "%s", | |
3444 | gpio_suffixes[i]); | |
3445 | ||
3446 | ret = of_gpio_named_count(dev->of_node, propname); | |
4033d4a4 | 3447 | if (ret > 0) |
66858527 RI |
3448 | break; |
3449 | } | |
4033d4a4 | 3450 | return ret ? ret : -ENOENT; |
66858527 RI |
3451 | } |
3452 | ||
3453 | static int platform_gpio_count(struct device *dev, const char *con_id) | |
3454 | { | |
3455 | struct gpiod_lookup_table *table; | |
3456 | struct gpiod_lookup *p; | |
3457 | unsigned int count = 0; | |
3458 | ||
3459 | table = gpiod_find_lookup_table(dev); | |
3460 | if (!table) | |
3461 | return -ENOENT; | |
3462 | ||
3463 | for (p = &table->table[0]; p->chip_label; p++) { | |
3464 | if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || | |
3465 | (!con_id && !p->con_id)) | |
3466 | count++; | |
3467 | } | |
3468 | if (!count) | |
3469 | return -ENOENT; | |
3470 | ||
3471 | return count; | |
3472 | } | |
3473 | ||
3474 | /** | |
3475 | * gpiod_count - return the number of GPIOs associated with a device / function | |
3476 | * or -ENOENT if no GPIO has been assigned to the requested function | |
3477 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
3478 | * @con_id: function within the GPIO consumer | |
3479 | */ | |
3480 | int gpiod_count(struct device *dev, const char *con_id) | |
3481 | { | |
3482 | int count = -ENOENT; | |
3483 | ||
3484 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) | |
3485 | count = dt_gpio_count(dev, con_id); | |
3486 | else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) | |
3487 | count = acpi_gpio_count(dev, con_id); | |
3488 | ||
3489 | if (count < 0) | |
3490 | count = platform_gpio_count(dev, con_id); | |
3491 | ||
3492 | return count; | |
3493 | } | |
3494 | EXPORT_SYMBOL_GPL(gpiod_count); | |
3495 | ||
bae48da2 | 3496 | /** |
0879162f | 3497 | * gpiod_get - obtain a GPIO for a given GPIO function |
ad824783 | 3498 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
bae48da2 | 3499 | * @con_id: function within the GPIO consumer |
39b2bbe3 | 3500 | * @flags: optional GPIO initialization flags |
bae48da2 AC |
3501 | * |
3502 | * Return the GPIO descriptor corresponding to the function con_id of device | |
2a3cf6a3 | 3503 | * dev, -ENOENT if no GPIO has been assigned to the requested function, or |
20a8a968 | 3504 | * another IS_ERR() code if an error occurred while trying to acquire the GPIO. |
bae48da2 | 3505 | */ |
b17d1bf1 | 3506 | struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id, |
39b2bbe3 | 3507 | enum gpiod_flags flags) |
bae48da2 | 3508 | { |
39b2bbe3 | 3509 | return gpiod_get_index(dev, con_id, 0, flags); |
bae48da2 | 3510 | } |
b17d1bf1 | 3511 | EXPORT_SYMBOL_GPL(gpiod_get); |
bae48da2 | 3512 | |
29a1f233 TR |
3513 | /** |
3514 | * gpiod_get_optional - obtain an optional GPIO for a given GPIO function | |
3515 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
3516 | * @con_id: function within the GPIO consumer | |
39b2bbe3 | 3517 | * @flags: optional GPIO initialization flags |
29a1f233 TR |
3518 | * |
3519 | * This is equivalent to gpiod_get(), except that when no GPIO was assigned to | |
3520 | * the requested function it will return NULL. This is convenient for drivers | |
3521 | * that need to handle optional GPIOs. | |
3522 | */ | |
b17d1bf1 | 3523 | struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, |
39b2bbe3 AC |
3524 | const char *con_id, |
3525 | enum gpiod_flags flags) | |
29a1f233 | 3526 | { |
39b2bbe3 | 3527 | return gpiod_get_index_optional(dev, con_id, 0, flags); |
29a1f233 | 3528 | } |
b17d1bf1 | 3529 | EXPORT_SYMBOL_GPL(gpiod_get_optional); |
29a1f233 | 3530 | |
f625d460 BP |
3531 | |
3532 | /** | |
3533 | * gpiod_configure_flags - helper function to configure a given GPIO | |
3534 | * @desc: gpio whose value will be assigned | |
3535 | * @con_id: function within the GPIO consumer | |
85b03b30 JH |
3536 | * @lflags: gpio_lookup_flags - returned from of_find_gpio() or |
3537 | * of_get_gpio_hog() | |
f625d460 BP |
3538 | * @dflags: gpiod_flags - optional GPIO initialization flags |
3539 | * | |
3540 | * Return 0 on success, -ENOENT if no GPIO has been assigned to the | |
3541 | * requested function and/or index, or another IS_ERR() code if an error | |
3542 | * occurred while trying to acquire the GPIO. | |
3543 | */ | |
c29fd9eb | 3544 | int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, |
85b03b30 | 3545 | unsigned long lflags, enum gpiod_flags dflags) |
f625d460 BP |
3546 | { |
3547 | int status; | |
3548 | ||
85b03b30 JH |
3549 | if (lflags & GPIO_ACTIVE_LOW) |
3550 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
f926dfc1 | 3551 | |
85b03b30 JH |
3552 | if (lflags & GPIO_OPEN_DRAIN) |
3553 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); | |
f926dfc1 LW |
3554 | else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) { |
3555 | /* | |
3556 | * This enforces open drain mode from the consumer side. | |
3557 | * This is necessary for some busses like I2C, but the lookup | |
3558 | * should *REALLY* have specified them as open drain in the | |
3559 | * first place, so print a little warning here. | |
3560 | */ | |
3561 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); | |
3562 | gpiod_warn(desc, | |
3563 | "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n"); | |
3564 | } | |
3565 | ||
85b03b30 JH |
3566 | if (lflags & GPIO_OPEN_SOURCE) |
3567 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); | |
2cbfca66 AJ |
3568 | if (lflags & GPIO_SLEEP_MAY_LOSE_VALUE) |
3569 | set_bit(FLAG_SLEEP_MAY_LOSE_VALUE, &desc->flags); | |
85b03b30 | 3570 | |
f625d460 BP |
3571 | /* No particular flag request, return here... */ |
3572 | if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) { | |
3573 | pr_debug("no flags found for %s\n", con_id); | |
3574 | return 0; | |
3575 | } | |
3576 | ||
3577 | /* Process flags */ | |
3578 | if (dflags & GPIOD_FLAGS_BIT_DIR_OUT) | |
3579 | status = gpiod_direction_output(desc, | |
ad17731d | 3580 | !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL)); |
f625d460 BP |
3581 | else |
3582 | status = gpiod_direction_input(desc); | |
3583 | ||
3584 | return status; | |
3585 | } | |
3586 | ||
bae48da2 AC |
3587 | /** |
3588 | * gpiod_get_index - obtain a GPIO from a multi-index GPIO function | |
fdd6a5fe | 3589 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
bae48da2 AC |
3590 | * @con_id: function within the GPIO consumer |
3591 | * @idx: index of the GPIO to obtain in the consumer | |
39b2bbe3 | 3592 | * @flags: optional GPIO initialization flags |
bae48da2 AC |
3593 | * |
3594 | * This variant of gpiod_get() allows to access GPIOs other than the first | |
3595 | * defined one for functions that define several GPIOs. | |
3596 | * | |
2a3cf6a3 AC |
3597 | * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the |
3598 | * requested function and/or index, or another IS_ERR() code if an error | |
20a8a968 | 3599 | * occurred while trying to acquire the GPIO. |
bae48da2 | 3600 | */ |
b17d1bf1 | 3601 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
bae48da2 | 3602 | const char *con_id, |
39b2bbe3 AC |
3603 | unsigned int idx, |
3604 | enum gpiod_flags flags) | |
bae48da2 | 3605 | { |
35c5d7fd | 3606 | struct gpio_desc *desc = NULL; |
bae48da2 | 3607 | int status; |
39b2bbe3 | 3608 | enum gpio_lookup_flags lookupflags = 0; |
bae48da2 AC |
3609 | |
3610 | dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); | |
3611 | ||
4d8440b9 RW |
3612 | if (dev) { |
3613 | /* Using device tree? */ | |
3614 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) { | |
3615 | dev_dbg(dev, "using device tree for GPIO lookup\n"); | |
3616 | desc = of_find_gpio(dev, con_id, idx, &lookupflags); | |
3617 | } else if (ACPI_COMPANION(dev)) { | |
3618 | dev_dbg(dev, "using ACPI for GPIO lookup\n"); | |
a31f5c3a | 3619 | desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags); |
4d8440b9 | 3620 | } |
35c5d7fd AC |
3621 | } |
3622 | ||
3623 | /* | |
3624 | * Either we are not using DT or ACPI, or their lookup did not return | |
3625 | * a result. In that case, use platform lookup as a fallback. | |
3626 | */ | |
2a3cf6a3 | 3627 | if (!desc || desc == ERR_PTR(-ENOENT)) { |
43a8785a | 3628 | dev_dbg(dev, "using lookup tables for GPIO lookup\n"); |
39b2bbe3 | 3629 | desc = gpiod_find(dev, con_id, idx, &lookupflags); |
bae48da2 AC |
3630 | } |
3631 | ||
3632 | if (IS_ERR(desc)) { | |
351cfe0f | 3633 | dev_dbg(dev, "lookup for GPIO %s failed\n", con_id); |
bae48da2 AC |
3634 | return desc; |
3635 | } | |
3636 | ||
3637 | status = gpiod_request(desc, con_id); | |
bae48da2 AC |
3638 | if (status < 0) |
3639 | return ERR_PTR(status); | |
3640 | ||
85b03b30 | 3641 | status = gpiod_configure_flags(desc, con_id, lookupflags, flags); |
39b2bbe3 AC |
3642 | if (status < 0) { |
3643 | dev_dbg(dev, "setup of GPIO %s failed\n", con_id); | |
3644 | gpiod_put(desc); | |
3645 | return ERR_PTR(status); | |
3646 | } | |
3647 | ||
bae48da2 AC |
3648 | return desc; |
3649 | } | |
b17d1bf1 | 3650 | EXPORT_SYMBOL_GPL(gpiod_get_index); |
bae48da2 | 3651 | |
40b73183 MW |
3652 | /** |
3653 | * fwnode_get_named_gpiod - obtain a GPIO from firmware node | |
3654 | * @fwnode: handle of the firmware node | |
3655 | * @propname: name of the firmware property representing the GPIO | |
537b94da | 3656 | * @index: index of the GPIO to obtain in the consumer |
a264d10f | 3657 | * @dflags: GPIO initialization flags |
950d55f5 | 3658 | * @label: label to attach to the requested GPIO |
40b73183 MW |
3659 | * |
3660 | * This function can be used for drivers that get their configuration | |
3661 | * from firmware. | |
3662 | * | |
3663 | * Function properly finds the corresponding GPIO using whatever is the | |
3664 | * underlying firmware interface and then makes sure that the GPIO | |
3665 | * descriptor is requested before it is returned to the caller. | |
3666 | * | |
950d55f5 | 3667 | * Returns: |
ff21378a | 3668 | * On successful request the GPIO pin is configured in accordance with |
a264d10f AS |
3669 | * provided @dflags. |
3670 | * | |
40b73183 MW |
3671 | * In case of error an ERR_PTR() is returned. |
3672 | */ | |
3673 | struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, | |
537b94da | 3674 | const char *propname, int index, |
b2987d74 AS |
3675 | enum gpiod_flags dflags, |
3676 | const char *label) | |
40b73183 MW |
3677 | { |
3678 | struct gpio_desc *desc = ERR_PTR(-ENODEV); | |
a264d10f | 3679 | unsigned long lflags = 0; |
40b73183 | 3680 | bool active_low = false; |
90b665f6 | 3681 | bool single_ended = false; |
4c0facdd | 3682 | bool open_drain = false; |
40b73183 MW |
3683 | int ret; |
3684 | ||
3685 | if (!fwnode) | |
3686 | return ERR_PTR(-EINVAL); | |
3687 | ||
3688 | if (is_of_node(fwnode)) { | |
3689 | enum of_gpio_flags flags; | |
3690 | ||
537b94da BB |
3691 | desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, |
3692 | index, &flags); | |
90b665f6 | 3693 | if (!IS_ERR(desc)) { |
40b73183 | 3694 | active_low = flags & OF_GPIO_ACTIVE_LOW; |
90b665f6 | 3695 | single_ended = flags & OF_GPIO_SINGLE_ENDED; |
4c0facdd | 3696 | open_drain = flags & OF_GPIO_OPEN_DRAIN; |
90b665f6 | 3697 | } |
40b73183 MW |
3698 | } else if (is_acpi_node(fwnode)) { |
3699 | struct acpi_gpio_info info; | |
3700 | ||
537b94da | 3701 | desc = acpi_node_get_gpiod(fwnode, propname, index, &info); |
a31f5c3a | 3702 | if (!IS_ERR(desc)) { |
52044723 | 3703 | active_low = info.polarity == GPIO_ACTIVE_LOW; |
a31f5c3a AS |
3704 | ret = acpi_gpio_update_gpiod_flags(&dflags, info.flags); |
3705 | if (ret) | |
3706 | pr_debug("Override GPIO initialization flags\n"); | |
3707 | } | |
40b73183 MW |
3708 | } |
3709 | ||
3710 | if (IS_ERR(desc)) | |
3711 | return desc; | |
3712 | ||
b2987d74 | 3713 | ret = gpiod_request(desc, label); |
85b03b30 JH |
3714 | if (ret) |
3715 | return ERR_PTR(ret); | |
3716 | ||
40b73183 | 3717 | if (active_low) |
a264d10f | 3718 | lflags |= GPIO_ACTIVE_LOW; |
40b73183 | 3719 | |
90b665f6 | 3720 | if (single_ended) { |
4c0facdd | 3721 | if (open_drain) |
a264d10f | 3722 | lflags |= GPIO_OPEN_DRAIN; |
90b665f6 | 3723 | else |
a264d10f AS |
3724 | lflags |= GPIO_OPEN_SOURCE; |
3725 | } | |
3726 | ||
3727 | ret = gpiod_configure_flags(desc, propname, lflags, dflags); | |
3728 | if (ret < 0) { | |
3729 | gpiod_put(desc); | |
3730 | return ERR_PTR(ret); | |
90b665f6 LP |
3731 | } |
3732 | ||
40b73183 MW |
3733 | return desc; |
3734 | } | |
3735 | EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod); | |
3736 | ||
29a1f233 TR |
3737 | /** |
3738 | * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO | |
3739 | * function | |
3740 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
3741 | * @con_id: function within the GPIO consumer | |
3742 | * @index: index of the GPIO to obtain in the consumer | |
39b2bbe3 | 3743 | * @flags: optional GPIO initialization flags |
29a1f233 TR |
3744 | * |
3745 | * This is equivalent to gpiod_get_index(), except that when no GPIO with the | |
3746 | * specified index was assigned to the requested function it will return NULL. | |
3747 | * This is convenient for drivers that need to handle optional GPIOs. | |
3748 | */ | |
b17d1bf1 | 3749 | struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, |
29a1f233 | 3750 | const char *con_id, |
39b2bbe3 AC |
3751 | unsigned int index, |
3752 | enum gpiod_flags flags) | |
29a1f233 TR |
3753 | { |
3754 | struct gpio_desc *desc; | |
3755 | ||
39b2bbe3 | 3756 | desc = gpiod_get_index(dev, con_id, index, flags); |
29a1f233 TR |
3757 | if (IS_ERR(desc)) { |
3758 | if (PTR_ERR(desc) == -ENOENT) | |
3759 | return NULL; | |
3760 | } | |
3761 | ||
3762 | return desc; | |
3763 | } | |
b17d1bf1 | 3764 | EXPORT_SYMBOL_GPL(gpiod_get_index_optional); |
29a1f233 | 3765 | |
f625d460 BP |
3766 | /** |
3767 | * gpiod_hog - Hog the specified GPIO desc given the provided flags | |
3768 | * @desc: gpio whose value will be assigned | |
3769 | * @name: gpio line name | |
3770 | * @lflags: gpio_lookup_flags - returned from of_find_gpio() or | |
3771 | * of_get_gpio_hog() | |
3772 | * @dflags: gpiod_flags - optional GPIO initialization flags | |
3773 | */ | |
3774 | int gpiod_hog(struct gpio_desc *desc, const char *name, | |
3775 | unsigned long lflags, enum gpiod_flags dflags) | |
3776 | { | |
3777 | struct gpio_chip *chip; | |
3778 | struct gpio_desc *local_desc; | |
3779 | int hwnum; | |
3780 | int status; | |
3781 | ||
3782 | chip = gpiod_to_chip(desc); | |
3783 | hwnum = gpio_chip_hwgpio(desc); | |
3784 | ||
3785 | local_desc = gpiochip_request_own_desc(chip, hwnum, name); | |
3786 | if (IS_ERR(local_desc)) { | |
c31a571d LD |
3787 | status = PTR_ERR(local_desc); |
3788 | pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n", | |
3789 | name, chip->label, hwnum, status); | |
3790 | return status; | |
f625d460 BP |
3791 | } |
3792 | ||
85b03b30 | 3793 | status = gpiod_configure_flags(desc, name, lflags, dflags); |
f625d460 | 3794 | if (status < 0) { |
c31a571d LD |
3795 | pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n", |
3796 | name, chip->label, hwnum, status); | |
f625d460 BP |
3797 | gpiochip_free_own_desc(desc); |
3798 | return status; | |
3799 | } | |
3800 | ||
3801 | /* Mark GPIO as hogged so it can be identified and removed later */ | |
3802 | set_bit(FLAG_IS_HOGGED, &desc->flags); | |
3803 | ||
3804 | pr_info("GPIO line %d (%s) hogged as %s%s\n", | |
3805 | desc_to_gpio(desc), name, | |
3806 | (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input", | |
3807 | (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? | |
3808 | (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":""); | |
3809 | ||
3810 | return 0; | |
3811 | } | |
3812 | ||
3813 | /** | |
3814 | * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog | |
3815 | * @chip: gpio chip to act on | |
3816 | * | |
3817 | * This is only used by of_gpiochip_remove to free hogged gpios | |
3818 | */ | |
3819 | static void gpiochip_free_hogs(struct gpio_chip *chip) | |
3820 | { | |
3821 | int id; | |
3822 | ||
3823 | for (id = 0; id < chip->ngpio; id++) { | |
1c3cdb18 LW |
3824 | if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags)) |
3825 | gpiochip_free_own_desc(&chip->gpiodev->descs[id]); | |
f625d460 BP |
3826 | } |
3827 | } | |
3828 | ||
66858527 RI |
3829 | /** |
3830 | * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function | |
3831 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
3832 | * @con_id: function within the GPIO consumer | |
3833 | * @flags: optional GPIO initialization flags | |
3834 | * | |
3835 | * This function acquires all the GPIOs defined under a given function. | |
3836 | * | |
3837 | * Return a struct gpio_descs containing an array of descriptors, -ENOENT if | |
3838 | * no GPIO has been assigned to the requested function, or another IS_ERR() | |
3839 | * code if an error occurred while trying to acquire the GPIOs. | |
3840 | */ | |
3841 | struct gpio_descs *__must_check gpiod_get_array(struct device *dev, | |
3842 | const char *con_id, | |
3843 | enum gpiod_flags flags) | |
3844 | { | |
3845 | struct gpio_desc *desc; | |
3846 | struct gpio_descs *descs; | |
3847 | int count; | |
3848 | ||
3849 | count = gpiod_count(dev, con_id); | |
3850 | if (count < 0) | |
3851 | return ERR_PTR(count); | |
3852 | ||
3853 | descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count, | |
3854 | GFP_KERNEL); | |
3855 | if (!descs) | |
3856 | return ERR_PTR(-ENOMEM); | |
3857 | ||
3858 | for (descs->ndescs = 0; descs->ndescs < count; ) { | |
3859 | desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); | |
3860 | if (IS_ERR(desc)) { | |
3861 | gpiod_put_array(descs); | |
3862 | return ERR_CAST(desc); | |
3863 | } | |
3864 | descs->desc[descs->ndescs] = desc; | |
3865 | descs->ndescs++; | |
3866 | } | |
3867 | return descs; | |
3868 | } | |
3869 | EXPORT_SYMBOL_GPL(gpiod_get_array); | |
3870 | ||
3871 | /** | |
3872 | * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO | |
3873 | * function | |
3874 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
3875 | * @con_id: function within the GPIO consumer | |
3876 | * @flags: optional GPIO initialization flags | |
3877 | * | |
3878 | * This is equivalent to gpiod_get_array(), except that when no GPIO was | |
3879 | * assigned to the requested function it will return NULL. | |
3880 | */ | |
3881 | struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, | |
3882 | const char *con_id, | |
3883 | enum gpiod_flags flags) | |
3884 | { | |
3885 | struct gpio_descs *descs; | |
3886 | ||
3887 | descs = gpiod_get_array(dev, con_id, flags); | |
3888 | if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT)) | |
3889 | return NULL; | |
3890 | ||
3891 | return descs; | |
3892 | } | |
3893 | EXPORT_SYMBOL_GPL(gpiod_get_array_optional); | |
3894 | ||
bae48da2 AC |
3895 | /** |
3896 | * gpiod_put - dispose of a GPIO descriptor | |
3897 | * @desc: GPIO descriptor to dispose of | |
3898 | * | |
3899 | * No descriptor can be used after gpiod_put() has been called on it. | |
3900 | */ | |
3901 | void gpiod_put(struct gpio_desc *desc) | |
3902 | { | |
3903 | gpiod_free(desc); | |
372e722e | 3904 | } |
bae48da2 | 3905 | EXPORT_SYMBOL_GPL(gpiod_put); |
d2876d08 | 3906 | |
66858527 RI |
3907 | /** |
3908 | * gpiod_put_array - dispose of multiple GPIO descriptors | |
3909 | * @descs: struct gpio_descs containing an array of descriptors | |
3910 | */ | |
3911 | void gpiod_put_array(struct gpio_descs *descs) | |
3912 | { | |
3913 | unsigned int i; | |
3914 | ||
3915 | for (i = 0; i < descs->ndescs; i++) | |
3916 | gpiod_put(descs->desc[i]); | |
3917 | ||
3918 | kfree(descs); | |
3919 | } | |
3920 | EXPORT_SYMBOL_GPL(gpiod_put_array); | |
3921 | ||
3c702e99 LW |
3922 | static int __init gpiolib_dev_init(void) |
3923 | { | |
3924 | int ret; | |
3925 | ||
3926 | /* Register GPIO sysfs bus */ | |
3927 | ret = bus_register(&gpio_bus_type); | |
3928 | if (ret < 0) { | |
3929 | pr_err("gpiolib: could not register GPIO bus type\n"); | |
3930 | return ret; | |
3931 | } | |
3932 | ||
3933 | ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip"); | |
3934 | if (ret < 0) { | |
3935 | pr_err("gpiolib: failed to allocate char dev region\n"); | |
3936 | bus_unregister(&gpio_bus_type); | |
159f3cd9 GR |
3937 | } else { |
3938 | gpiolib_initialized = true; | |
3939 | gpiochip_setup_devs(); | |
3c702e99 LW |
3940 | } |
3941 | return ret; | |
3942 | } | |
3943 | core_initcall(gpiolib_dev_init); | |
3944 | ||
d2876d08 DB |
3945 | #ifdef CONFIG_DEBUG_FS |
3946 | ||
fdeb8e15 | 3947 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev) |
d2876d08 DB |
3948 | { |
3949 | unsigned i; | |
fdeb8e15 LW |
3950 | struct gpio_chip *chip = gdev->chip; |
3951 | unsigned gpio = gdev->base; | |
3952 | struct gpio_desc *gdesc = &gdev->descs[0]; | |
d2876d08 | 3953 | int is_out; |
d468bf9e | 3954 | int is_irq; |
d2876d08 | 3955 | |
fdeb8e15 | 3956 | for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) { |
ced433e2 MP |
3957 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) { |
3958 | if (gdesc->name) { | |
3959 | seq_printf(s, " gpio-%-3d (%-20.20s)\n", | |
3960 | gpio, gdesc->name); | |
3961 | } | |
d2876d08 | 3962 | continue; |
ced433e2 | 3963 | } |
d2876d08 | 3964 | |
372e722e | 3965 | gpiod_get_direction(gdesc); |
d2876d08 | 3966 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
d468bf9e | 3967 | is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); |
ced433e2 MP |
3968 | seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s", |
3969 | gpio, gdesc->name ? gdesc->name : "", gdesc->label, | |
d2876d08 DB |
3970 | is_out ? "out" : "in ", |
3971 | chip->get | |
3972 | ? (chip->get(chip, i) ? "hi" : "lo") | |
d468bf9e LW |
3973 | : "? ", |
3974 | is_irq ? "IRQ" : " "); | |
d2876d08 DB |
3975 | seq_printf(s, "\n"); |
3976 | } | |
3977 | } | |
3978 | ||
f9c4a31f | 3979 | static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) |
d2876d08 | 3980 | { |
362432ae | 3981 | unsigned long flags; |
ff2b1359 | 3982 | struct gpio_device *gdev = NULL; |
cb1650d4 | 3983 | loff_t index = *pos; |
d2876d08 | 3984 | |
f9c4a31f | 3985 | s->private = ""; |
d2876d08 | 3986 | |
362432ae | 3987 | spin_lock_irqsave(&gpio_lock, flags); |
ff2b1359 | 3988 | list_for_each_entry(gdev, &gpio_devices, list) |
362432ae GL |
3989 | if (index-- == 0) { |
3990 | spin_unlock_irqrestore(&gpio_lock, flags); | |
ff2b1359 | 3991 | return gdev; |
f9c4a31f | 3992 | } |
362432ae | 3993 | spin_unlock_irqrestore(&gpio_lock, flags); |
f9c4a31f | 3994 | |
cb1650d4 | 3995 | return NULL; |
f9c4a31f TR |
3996 | } |
3997 | ||
3998 | static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
3999 | { | |
362432ae | 4000 | unsigned long flags; |
ff2b1359 | 4001 | struct gpio_device *gdev = v; |
f9c4a31f TR |
4002 | void *ret = NULL; |
4003 | ||
362432ae | 4004 | spin_lock_irqsave(&gpio_lock, flags); |
ff2b1359 | 4005 | if (list_is_last(&gdev->list, &gpio_devices)) |
cb1650d4 AC |
4006 | ret = NULL; |
4007 | else | |
ff2b1359 | 4008 | ret = list_entry(gdev->list.next, struct gpio_device, list); |
362432ae | 4009 | spin_unlock_irqrestore(&gpio_lock, flags); |
f9c4a31f TR |
4010 | |
4011 | s->private = "\n"; | |
4012 | ++*pos; | |
4013 | ||
4014 | return ret; | |
4015 | } | |
4016 | ||
4017 | static void gpiolib_seq_stop(struct seq_file *s, void *v) | |
4018 | { | |
4019 | } | |
4020 | ||
4021 | static int gpiolib_seq_show(struct seq_file *s, void *v) | |
4022 | { | |
ff2b1359 LW |
4023 | struct gpio_device *gdev = v; |
4024 | struct gpio_chip *chip = gdev->chip; | |
4025 | struct device *parent; | |
4026 | ||
4027 | if (!chip) { | |
4028 | seq_printf(s, "%s%s: (dangling chip)", (char *)s->private, | |
4029 | dev_name(&gdev->dev)); | |
4030 | return 0; | |
4031 | } | |
f9c4a31f | 4032 | |
ff2b1359 LW |
4033 | seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private, |
4034 | dev_name(&gdev->dev), | |
fdeb8e15 | 4035 | gdev->base, gdev->base + gdev->ngpio - 1); |
ff2b1359 LW |
4036 | parent = chip->parent; |
4037 | if (parent) | |
4038 | seq_printf(s, ", parent: %s/%s", | |
4039 | parent->bus ? parent->bus->name : "no-bus", | |
4040 | dev_name(parent)); | |
f9c4a31f TR |
4041 | if (chip->label) |
4042 | seq_printf(s, ", %s", chip->label); | |
4043 | if (chip->can_sleep) | |
4044 | seq_printf(s, ", can sleep"); | |
4045 | seq_printf(s, ":\n"); | |
4046 | ||
4047 | if (chip->dbg_show) | |
4048 | chip->dbg_show(s, chip); | |
4049 | else | |
fdeb8e15 | 4050 | gpiolib_dbg_show(s, gdev); |
f9c4a31f | 4051 | |
d2876d08 DB |
4052 | return 0; |
4053 | } | |
4054 | ||
f9c4a31f TR |
4055 | static const struct seq_operations gpiolib_seq_ops = { |
4056 | .start = gpiolib_seq_start, | |
4057 | .next = gpiolib_seq_next, | |
4058 | .stop = gpiolib_seq_stop, | |
4059 | .show = gpiolib_seq_show, | |
4060 | }; | |
4061 | ||
d2876d08 DB |
4062 | static int gpiolib_open(struct inode *inode, struct file *file) |
4063 | { | |
f9c4a31f | 4064 | return seq_open(file, &gpiolib_seq_ops); |
d2876d08 DB |
4065 | } |
4066 | ||
828c0950 | 4067 | static const struct file_operations gpiolib_operations = { |
f9c4a31f | 4068 | .owner = THIS_MODULE, |
d2876d08 DB |
4069 | .open = gpiolib_open, |
4070 | .read = seq_read, | |
4071 | .llseek = seq_lseek, | |
f9c4a31f | 4072 | .release = seq_release, |
d2876d08 DB |
4073 | }; |
4074 | ||
4075 | static int __init gpiolib_debugfs_init(void) | |
4076 | { | |
4077 | /* /sys/kernel/debug/gpio */ | |
4078 | (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, | |
4079 | NULL, NULL, &gpiolib_operations); | |
4080 | return 0; | |
4081 | } | |
4082 | subsys_initcall(gpiolib_debugfs_init); | |
4083 | ||
4084 | #endif /* DEBUG_FS */ |