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