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