Commit | Line | Data |
---|---|---|
dae5f0af | 1 | // SPDX-License-Identifier: GPL-2.0 |
923a654c | 2 | #include <linux/bitmap.h> |
d2876d08 DB |
3 | #include <linux/kernel.h> |
4 | #include <linux/module.h> | |
ff77c352 | 5 | #include <linux/interrupt.h> |
d2876d08 DB |
6 | #include <linux/irq.h> |
7 | #include <linux/spinlock.h> | |
1a989d0f | 8 | #include <linux/list.h> |
d8f388d8 DB |
9 | #include <linux/device.h> |
10 | #include <linux/err.h> | |
11 | #include <linux/debugfs.h> | |
12 | #include <linux/seq_file.h> | |
13 | #include <linux/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 | 31 | #include "gpiolib.h" |
f626d6df | 32 | #include "gpiolib-of.h" |
77cb907a | 33 | #include "gpiolib-acpi.h" |
664e3e5a | 34 | |
3f397c21 UKK |
35 | #define CREATE_TRACE_POINTS |
36 | #include <trace/events/gpio.h> | |
d2876d08 | 37 | |
79a9becd | 38 | /* Implementation infrastructure for GPIO interfaces. |
d2876d08 | 39 | * |
79a9becd AC |
40 | * The GPIO programming interface allows for inlining speed-critical |
41 | * get/set operations for common cases, so that access to SOC-integrated | |
42 | * GPIOs can sometimes cost only an instruction or two per bit. | |
d2876d08 DB |
43 | */ |
44 | ||
45 | ||
46 | /* When debugging, extend minimal trust to callers and platform code. | |
47 | * Also emit diagnostic messages that may help initial bringup, when | |
48 | * board setup or driver bugs are most common. | |
49 | * | |
50 | * Otherwise, minimize overhead in what may be bitbanging codepaths. | |
51 | */ | |
52 | #ifdef DEBUG | |
53 | #define extra_checks 1 | |
54 | #else | |
55 | #define extra_checks 0 | |
56 | #endif | |
57 | ||
ff2b1359 LW |
58 | /* Device and char device-related information */ |
59 | static DEFINE_IDA(gpio_ida); | |
3c702e99 LW |
60 | static dev_t gpio_devt; |
61 | #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */ | |
62 | static struct bus_type gpio_bus_type = { | |
63 | .name = "gpio", | |
64 | }; | |
ff2b1359 | 65 | |
3027743f LA |
66 | /* |
67 | * Number of GPIOs to use for the fast path in set array | |
68 | */ | |
69 | #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT | |
70 | ||
d2876d08 DB |
71 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. |
72 | * While any GPIO is requested, its gpio_chip is not removable; | |
73 | * each GPIO's "requested" flag serves as a lock and refcount. | |
74 | */ | |
0eb4c6c2 | 75 | DEFINE_SPINLOCK(gpio_lock); |
d2876d08 | 76 | |
bae48da2 AC |
77 | static DEFINE_MUTEX(gpio_lookup_lock); |
78 | static LIST_HEAD(gpio_lookup_list); | |
ff2b1359 | 79 | LIST_HEAD(gpio_devices); |
6d86750c | 80 | |
a411e81e BG |
81 | static DEFINE_MUTEX(gpio_machine_hogs_mutex); |
82 | static LIST_HEAD(gpio_machine_hogs); | |
83 | ||
6d86750c | 84 | static void gpiochip_free_hogs(struct gpio_chip *chip); |
959bc7b2 | 85 | static int gpiochip_add_irqchip(struct gpio_chip *gpiochip, |
39c3fd58 AL |
86 | struct lock_class_key *lock_key, |
87 | struct lock_class_key *request_key); | |
6d86750c | 88 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); |
9411e3aa | 89 | static int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip); |
79b804cb MW |
90 | static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip); |
91 | static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip); | |
6d86750c | 92 | |
159f3cd9 | 93 | static bool gpiolib_initialized; |
6d86750c | 94 | |
d2876d08 DB |
95 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
96 | { | |
d2876d08 | 97 | d->label = label; |
d2876d08 DB |
98 | } |
99 | ||
372e722e | 100 | /** |
950d55f5 TR |
101 | * gpio_to_desc - Convert a GPIO number to its descriptor |
102 | * @gpio: global GPIO number | |
103 | * | |
104 | * Returns: | |
105 | * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO | |
106 | * with the given number exists in the system. | |
372e722e | 107 | */ |
79a9becd | 108 | struct gpio_desc *gpio_to_desc(unsigned gpio) |
372e722e | 109 | { |
ff2b1359 | 110 | struct gpio_device *gdev; |
14e85c0e AC |
111 | unsigned long flags; |
112 | ||
113 | spin_lock_irqsave(&gpio_lock, flags); | |
114 | ||
ff2b1359 | 115 | list_for_each_entry(gdev, &gpio_devices, list) { |
fdeb8e15 LW |
116 | if (gdev->base <= gpio && |
117 | gdev->base + gdev->ngpio > gpio) { | |
14e85c0e | 118 | spin_unlock_irqrestore(&gpio_lock, flags); |
fdeb8e15 | 119 | return &gdev->descs[gpio - gdev->base]; |
14e85c0e AC |
120 | } |
121 | } | |
122 | ||
123 | spin_unlock_irqrestore(&gpio_lock, flags); | |
124 | ||
0e9a5edf AC |
125 | if (!gpio_is_valid(gpio)) |
126 | WARN(1, "invalid GPIO %d\n", gpio); | |
127 | ||
14e85c0e | 128 | return NULL; |
372e722e | 129 | } |
79a9becd | 130 | EXPORT_SYMBOL_GPL(gpio_to_desc); |
372e722e | 131 | |
d468bf9e | 132 | /** |
950d55f5 TR |
133 | * gpiochip_get_desc - get the GPIO descriptor corresponding to the given |
134 | * hardware number for this chip | |
135 | * @chip: GPIO chip | |
136 | * @hwnum: hardware number of the GPIO for this chip | |
137 | * | |
138 | * Returns: | |
35c6cfb4 | 139 | * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists |
950d55f5 | 140 | * in the given chip for the specified hardware number. |
d468bf9e | 141 | */ |
bb1e88cc | 142 | struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, |
06863620 | 143 | unsigned int hwnum) |
d468bf9e | 144 | { |
fdeb8e15 LW |
145 | struct gpio_device *gdev = chip->gpiodev; |
146 | ||
147 | if (hwnum >= gdev->ngpio) | |
b7d0a28a | 148 | return ERR_PTR(-EINVAL); |
d468bf9e | 149 | |
fdeb8e15 | 150 | return &gdev->descs[hwnum]; |
d468bf9e | 151 | } |
372e722e AC |
152 | |
153 | /** | |
950d55f5 TR |
154 | * desc_to_gpio - convert a GPIO descriptor to the integer namespace |
155 | * @desc: GPIO descriptor | |
156 | * | |
372e722e | 157 | * This should disappear in the future but is needed since we still |
950d55f5 TR |
158 | * use GPIO numbers for error messages and sysfs nodes. |
159 | * | |
160 | * Returns: | |
161 | * The global GPIO number for the GPIO specified by its descriptor. | |
372e722e | 162 | */ |
79a9becd | 163 | int desc_to_gpio(const struct gpio_desc *desc) |
372e722e | 164 | { |
fdeb8e15 | 165 | return desc->gdev->base + (desc - &desc->gdev->descs[0]); |
372e722e | 166 | } |
79a9becd | 167 | EXPORT_SYMBOL_GPL(desc_to_gpio); |
372e722e AC |
168 | |
169 | ||
79a9becd AC |
170 | /** |
171 | * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs | |
172 | * @desc: descriptor to return the chip of | |
173 | */ | |
174 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) | |
372e722e | 175 | { |
dd3b9a44 | 176 | if (!desc || !desc->gdev) |
fdeb8e15 LW |
177 | return NULL; |
178 | return desc->gdev->chip; | |
372e722e | 179 | } |
79a9becd | 180 | EXPORT_SYMBOL_GPL(gpiod_to_chip); |
d2876d08 | 181 | |
8d0aab2f AV |
182 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
183 | static int gpiochip_find_base(int ngpio) | |
184 | { | |
ff2b1359 | 185 | struct gpio_device *gdev; |
83cabe33 | 186 | int base = ARCH_NR_GPIOS - ngpio; |
8d0aab2f | 187 | |
ff2b1359 | 188 | list_for_each_entry_reverse(gdev, &gpio_devices, list) { |
83cabe33 | 189 | /* found a free space? */ |
fdeb8e15 | 190 | if (gdev->base + gdev->ngpio <= base) |
83cabe33 AC |
191 | break; |
192 | else | |
193 | /* nope, check the space right before the chip */ | |
fdeb8e15 | 194 | base = gdev->base - ngpio; |
8d0aab2f AV |
195 | } |
196 | ||
83cabe33 | 197 | if (gpio_is_valid(base)) { |
8d0aab2f | 198 | pr_debug("%s: found new base at %d\n", __func__, base); |
83cabe33 AC |
199 | return base; |
200 | } else { | |
201 | pr_err("%s: cannot find free range\n", __func__); | |
202 | return -ENOSPC; | |
169b6a7a | 203 | } |
169b6a7a AV |
204 | } |
205 | ||
79a9becd AC |
206 | /** |
207 | * gpiod_get_direction - return the current direction of a GPIO | |
208 | * @desc: GPIO to get the direction of | |
209 | * | |
94fc7309 | 210 | * Returns 0 for output, 1 for input, or an error code in case of error. |
79a9becd AC |
211 | * |
212 | * This function may sleep if gpiod_cansleep() is true. | |
213 | */ | |
8e53b0f1 | 214 | int gpiod_get_direction(struct gpio_desc *desc) |
80b0a602 | 215 | { |
d0121b85 WS |
216 | struct gpio_chip *chip; |
217 | unsigned offset; | |
d377f56f | 218 | int ret; |
80b0a602 | 219 | |
372e722e AC |
220 | chip = gpiod_to_chip(desc); |
221 | offset = gpio_chip_hwgpio(desc); | |
80b0a602 | 222 | |
256efaea RK |
223 | /* |
224 | * Open drain emulation using input mode may incorrectly report | |
225 | * input here, fix that up. | |
226 | */ | |
227 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && | |
228 | test_bit(FLAG_IS_OUT, &desc->flags)) | |
229 | return 0; | |
230 | ||
80b0a602 | 231 | if (!chip->get_direction) |
d0121b85 | 232 | return -ENOTSUPP; |
80b0a602 | 233 | |
d377f56f | 234 | ret = chip->get_direction(chip, offset); |
4fc5bfeb AS |
235 | if (ret < 0) |
236 | return ret; | |
237 | ||
238 | /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */ | |
239 | if (ret > 0) | |
d377f56f | 240 | ret = 1; |
4fc5bfeb AS |
241 | |
242 | assign_bit(FLAG_IS_OUT, &desc->flags, !ret); | |
243 | ||
d377f56f | 244 | return ret; |
80b0a602 | 245 | } |
79a9becd | 246 | EXPORT_SYMBOL_GPL(gpiod_get_direction); |
80b0a602 | 247 | |
1a989d0f AC |
248 | /* |
249 | * Add a new chip to the global chips list, keeping the list of chips sorted | |
ef7c7553 | 250 | * by range(means [base, base + ngpio - 1]) order. |
1a989d0f AC |
251 | * |
252 | * Return -EBUSY if the new chip overlaps with some other chip's integer | |
253 | * space. | |
254 | */ | |
ff2b1359 | 255 | static int gpiodev_add_to_list(struct gpio_device *gdev) |
1a989d0f | 256 | { |
a961f9b4 | 257 | struct gpio_device *prev, *next; |
1a989d0f | 258 | |
ff2b1359 | 259 | if (list_empty(&gpio_devices)) { |
a961f9b4 | 260 | /* initial entry in list */ |
ff2b1359 | 261 | list_add_tail(&gdev->list, &gpio_devices); |
e28ecca6 | 262 | return 0; |
1a989d0f AC |
263 | } |
264 | ||
a961f9b4 BJZ |
265 | next = list_entry(gpio_devices.next, struct gpio_device, list); |
266 | if (gdev->base + gdev->ngpio <= next->base) { | |
267 | /* add before first entry */ | |
268 | list_add(&gdev->list, &gpio_devices); | |
269 | return 0; | |
1a989d0f AC |
270 | } |
271 | ||
a961f9b4 BJZ |
272 | prev = list_entry(gpio_devices.prev, struct gpio_device, list); |
273 | if (prev->base + prev->ngpio <= gdev->base) { | |
274 | /* add behind last entry */ | |
275 | list_add_tail(&gdev->list, &gpio_devices); | |
96098df1 | 276 | return 0; |
1a989d0f AC |
277 | } |
278 | ||
a961f9b4 BJZ |
279 | list_for_each_entry_safe(prev, next, &gpio_devices, list) { |
280 | /* at the end of the list */ | |
281 | if (&next->list == &gpio_devices) | |
282 | break; | |
1a989d0f | 283 | |
a961f9b4 BJZ |
284 | /* add between prev and next */ |
285 | if (prev->base + prev->ngpio <= gdev->base | |
286 | && gdev->base + gdev->ngpio <= next->base) { | |
287 | list_add(&gdev->list, &prev->list); | |
288 | return 0; | |
289 | } | |
290 | } | |
291 | ||
292 | dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n"); | |
293 | return -EBUSY; | |
1a989d0f AC |
294 | } |
295 | ||
950d55f5 | 296 | /* |
f881bab0 LW |
297 | * Convert a GPIO name to its descriptor |
298 | */ | |
299 | static struct gpio_desc *gpio_name_to_desc(const char * const name) | |
300 | { | |
ff2b1359 | 301 | struct gpio_device *gdev; |
f881bab0 LW |
302 | unsigned long flags; |
303 | ||
ee203bbd MM |
304 | if (!name) |
305 | return NULL; | |
306 | ||
f881bab0 LW |
307 | spin_lock_irqsave(&gpio_lock, flags); |
308 | ||
ff2b1359 | 309 | list_for_each_entry(gdev, &gpio_devices, list) { |
f881bab0 LW |
310 | int i; |
311 | ||
fdeb8e15 LW |
312 | for (i = 0; i != gdev->ngpio; ++i) { |
313 | struct gpio_desc *desc = &gdev->descs[i]; | |
f881bab0 | 314 | |
ee203bbd | 315 | if (!desc->name) |
f881bab0 LW |
316 | continue; |
317 | ||
fdeb8e15 | 318 | if (!strcmp(desc->name, name)) { |
f881bab0 | 319 | spin_unlock_irqrestore(&gpio_lock, flags); |
fdeb8e15 | 320 | return desc; |
f881bab0 LW |
321 | } |
322 | } | |
323 | } | |
324 | ||
325 | spin_unlock_irqrestore(&gpio_lock, flags); | |
326 | ||
327 | return NULL; | |
328 | } | |
329 | ||
5f3ca732 MP |
330 | /* |
331 | * Takes the names from gc->names and checks if they are all unique. If they | |
332 | * are, they are assigned to their gpio descriptors. | |
333 | * | |
ed37915c | 334 | * Warning if one of the names is already used for a different GPIO. |
5f3ca732 MP |
335 | */ |
336 | static int gpiochip_set_desc_names(struct gpio_chip *gc) | |
337 | { | |
fdeb8e15 | 338 | struct gpio_device *gdev = gc->gpiodev; |
5f3ca732 MP |
339 | int i; |
340 | ||
341 | if (!gc->names) | |
342 | return 0; | |
343 | ||
344 | /* First check all names if they are unique */ | |
345 | for (i = 0; i != gc->ngpio; ++i) { | |
346 | struct gpio_desc *gpio; | |
347 | ||
348 | gpio = gpio_name_to_desc(gc->names[i]); | |
f881bab0 | 349 | if (gpio) |
fdeb8e15 | 350 | dev_warn(&gdev->dev, |
34ffd85d | 351 | "Detected name collision for GPIO name '%s'\n", |
f881bab0 | 352 | gc->names[i]); |
5f3ca732 MP |
353 | } |
354 | ||
355 | /* Then add all names to the GPIO descriptors */ | |
356 | for (i = 0; i != gc->ngpio; ++i) | |
fdeb8e15 | 357 | gdev->descs[i].name = gc->names[i]; |
5f3ca732 MP |
358 | |
359 | return 0; | |
360 | } | |
361 | ||
e4371f6e SB |
362 | static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip) |
363 | { | |
364 | unsigned long *p; | |
365 | ||
7bdbd1ec | 366 | p = bitmap_alloc(chip->ngpio, GFP_KERNEL); |
e4371f6e SB |
367 | if (!p) |
368 | return NULL; | |
369 | ||
370 | /* Assume by default all GPIOs are valid */ | |
371 | bitmap_fill(p, chip->ngpio); | |
372 | ||
373 | return p; | |
374 | } | |
375 | ||
f626d6df | 376 | static int gpiochip_alloc_valid_mask(struct gpio_chip *gc) |
726cb3ba | 377 | { |
eb1e8bd6 | 378 | if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask)) |
726cb3ba SB |
379 | return 0; |
380 | ||
f626d6df LW |
381 | gc->valid_mask = gpiochip_allocate_mask(gc); |
382 | if (!gc->valid_mask) | |
726cb3ba SB |
383 | return -ENOMEM; |
384 | ||
385 | return 0; | |
386 | } | |
387 | ||
c9fc5aff | 388 | static int gpiochip_init_valid_mask(struct gpio_chip *gc) |
f8ec92a9 | 389 | { |
c9fc5aff LW |
390 | if (gc->init_valid_mask) |
391 | return gc->init_valid_mask(gc, | |
392 | gc->valid_mask, | |
393 | gc->ngpio); | |
f8ec92a9 RRD |
394 | |
395 | return 0; | |
396 | } | |
397 | ||
726cb3ba SB |
398 | static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip) |
399 | { | |
7bdbd1ec | 400 | bitmap_free(gpiochip->valid_mask); |
726cb3ba SB |
401 | gpiochip->valid_mask = NULL; |
402 | } | |
403 | ||
b056ca1c AS |
404 | static int gpiochip_add_pin_ranges(struct gpio_chip *gc) |
405 | { | |
406 | if (gc->add_pin_ranges) | |
407 | return gc->add_pin_ranges(gc); | |
408 | ||
409 | return 0; | |
410 | } | |
411 | ||
726cb3ba SB |
412 | bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip, |
413 | unsigned int offset) | |
414 | { | |
415 | /* No mask means all valid */ | |
416 | if (likely(!gpiochip->valid_mask)) | |
417 | return true; | |
418 | return test_bit(offset, gpiochip->valid_mask); | |
419 | } | |
420 | EXPORT_SYMBOL_GPL(gpiochip_line_is_valid); | |
421 | ||
d7c51b47 LW |
422 | /* |
423 | * GPIO line handle management | |
424 | */ | |
425 | ||
426 | /** | |
427 | * struct linehandle_state - contains the state of a userspace handle | |
428 | * @gdev: the GPIO device the handle pertains to | |
429 | * @label: consumer label used to tag descriptors | |
430 | * @descs: the GPIO descriptors held by this handle | |
431 | * @numdescs: the number of descriptors held in the descs array | |
432 | */ | |
433 | struct linehandle_state { | |
434 | struct gpio_device *gdev; | |
435 | const char *label; | |
436 | struct gpio_desc *descs[GPIOHANDLES_MAX]; | |
437 | u32 numdescs; | |
438 | }; | |
439 | ||
e3e847c7 LPC |
440 | #define GPIOHANDLE_REQUEST_VALID_FLAGS \ |
441 | (GPIOHANDLE_REQUEST_INPUT | \ | |
442 | GPIOHANDLE_REQUEST_OUTPUT | \ | |
443 | GPIOHANDLE_REQUEST_ACTIVE_LOW | \ | |
9225d516 DF |
444 | GPIOHANDLE_REQUEST_BIAS_PULL_UP | \ |
445 | GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \ | |
2148ad77 | 446 | GPIOHANDLE_REQUEST_BIAS_DISABLE | \ |
e3e847c7 LPC |
447 | GPIOHANDLE_REQUEST_OPEN_DRAIN | \ |
448 | GPIOHANDLE_REQUEST_OPEN_SOURCE) | |
449 | ||
b043ed7e KG |
450 | static int linehandle_validate_flags(u32 flags) |
451 | { | |
452 | /* Return an error if an unknown flag is set */ | |
453 | if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) | |
454 | return -EINVAL; | |
455 | ||
456 | /* | |
457 | * Do not allow both INPUT & OUTPUT flags to be set as they are | |
458 | * contradictory. | |
459 | */ | |
460 | if ((flags & GPIOHANDLE_REQUEST_INPUT) && | |
461 | (flags & GPIOHANDLE_REQUEST_OUTPUT)) | |
462 | return -EINVAL; | |
463 | ||
464 | /* | |
465 | * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If | |
466 | * the hardware actually supports enabling both at the same time the | |
467 | * electrical result would be disastrous. | |
468 | */ | |
469 | if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) && | |
470 | (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) | |
471 | return -EINVAL; | |
472 | ||
473 | /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */ | |
474 | if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) && | |
475 | ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || | |
476 | (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))) | |
477 | return -EINVAL; | |
478 | ||
479 | /* Bias flags only allowed for input or output mode. */ | |
480 | if (!((flags & GPIOHANDLE_REQUEST_INPUT) || | |
481 | (flags & GPIOHANDLE_REQUEST_OUTPUT)) && | |
482 | ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) || | |
483 | (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) || | |
484 | (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN))) | |
485 | return -EINVAL; | |
486 | ||
487 | /* Only one bias flag can be set. */ | |
488 | if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && | |
489 | (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | | |
490 | GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || | |
491 | ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && | |
492 | (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) | |
493 | return -EINVAL; | |
494 | ||
495 | return 0; | |
496 | } | |
497 | ||
e588bb1e KG |
498 | static long linehandle_set_config(struct linehandle_state *lh, |
499 | void __user *ip) | |
500 | { | |
501 | struct gpiohandle_config gcnf; | |
502 | struct gpio_desc *desc; | |
503 | int i, ret; | |
504 | u32 lflags; | |
505 | unsigned long *flagsp; | |
506 | ||
507 | if (copy_from_user(&gcnf, ip, sizeof(gcnf))) | |
508 | return -EFAULT; | |
509 | ||
510 | lflags = gcnf.flags; | |
511 | ret = linehandle_validate_flags(lflags); | |
512 | if (ret) | |
513 | return ret; | |
514 | ||
515 | for (i = 0; i < lh->numdescs; i++) { | |
516 | desc = lh->descs[i]; | |
517 | flagsp = &desc->flags; | |
518 | ||
4fc5bfeb | 519 | assign_bit(FLAG_ACTIVE_LOW, flagsp, |
e588bb1e KG |
520 | lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW); |
521 | ||
4fc5bfeb | 522 | assign_bit(FLAG_OPEN_DRAIN, flagsp, |
e588bb1e KG |
523 | lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN); |
524 | ||
4fc5bfeb | 525 | assign_bit(FLAG_OPEN_SOURCE, flagsp, |
e588bb1e KG |
526 | lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE); |
527 | ||
4fc5bfeb | 528 | assign_bit(FLAG_PULL_UP, flagsp, |
e588bb1e KG |
529 | lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP); |
530 | ||
4fc5bfeb | 531 | assign_bit(FLAG_PULL_DOWN, flagsp, |
e588bb1e KG |
532 | lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN); |
533 | ||
4fc5bfeb | 534 | assign_bit(FLAG_BIAS_DISABLE, flagsp, |
e588bb1e KG |
535 | lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE); |
536 | ||
537 | /* | |
538 | * Lines have to be requested explicitly for input | |
539 | * or output, else the line will be treated "as is". | |
540 | */ | |
541 | if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { | |
542 | int val = !!gcnf.default_values[i]; | |
543 | ||
544 | ret = gpiod_direction_output(desc, val); | |
545 | if (ret) | |
546 | return ret; | |
547 | } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { | |
548 | ret = gpiod_direction_input(desc); | |
549 | if (ret) | |
550 | return ret; | |
551 | } | |
51c1064e BG |
552 | |
553 | atomic_notifier_call_chain(&desc->gdev->notifier, | |
554 | GPIOLINE_CHANGED_CONFIG, desc); | |
e588bb1e KG |
555 | } |
556 | return 0; | |
557 | } | |
558 | ||
d7c51b47 LW |
559 | static long linehandle_ioctl(struct file *filep, unsigned int cmd, |
560 | unsigned long arg) | |
561 | { | |
562 | struct linehandle_state *lh = filep->private_data; | |
563 | void __user *ip = (void __user *)arg; | |
564 | struct gpiohandle_data ghd; | |
b9762beb | 565 | DECLARE_BITMAP(vals, GPIOHANDLES_MAX); |
d7c51b47 LW |
566 | int i; |
567 | ||
568 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { | |
2b955b34 | 569 | /* NOTE: It's ok to read values of output lines. */ |
eec1d566 LW |
570 | int ret = gpiod_get_array_value_complex(false, |
571 | true, | |
572 | lh->numdescs, | |
573 | lh->descs, | |
77588c14 | 574 | NULL, |
eec1d566 LW |
575 | vals); |
576 | if (ret) | |
577 | return ret; | |
d7c51b47 | 578 | |
3eded5d8 | 579 | memset(&ghd, 0, sizeof(ghd)); |
eec1d566 | 580 | for (i = 0; i < lh->numdescs; i++) |
b9762beb | 581 | ghd.values[i] = test_bit(i, vals); |
d7c51b47 LW |
582 | |
583 | if (copy_to_user(ip, &ghd, sizeof(ghd))) | |
584 | return -EFAULT; | |
585 | ||
586 | return 0; | |
587 | } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) { | |
e5332d54 BG |
588 | /* |
589 | * All line descriptors were created at once with the same | |
590 | * flags so just check if the first one is really output. | |
591 | */ | |
592 | if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags)) | |
593 | return -EPERM; | |
594 | ||
d7c51b47 LW |
595 | if (copy_from_user(&ghd, ip, sizeof(ghd))) |
596 | return -EFAULT; | |
597 | ||
598 | /* Clamp all values to [0,1] */ | |
599 | for (i = 0; i < lh->numdescs; i++) | |
b9762beb | 600 | __assign_bit(i, vals, ghd.values[i]); |
d7c51b47 LW |
601 | |
602 | /* Reuse the array setting function */ | |
3027743f | 603 | return gpiod_set_array_value_complex(false, |
d7c51b47 LW |
604 | true, |
605 | lh->numdescs, | |
606 | lh->descs, | |
77588c14 | 607 | NULL, |
d7c51b47 | 608 | vals); |
e588bb1e KG |
609 | } else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) { |
610 | return linehandle_set_config(lh, ip); | |
d7c51b47 LW |
611 | } |
612 | return -EINVAL; | |
613 | } | |
614 | ||
615 | #ifdef CONFIG_COMPAT | |
616 | static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd, | |
617 | unsigned long arg) | |
618 | { | |
619 | return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg)); | |
620 | } | |
621 | #endif | |
622 | ||
623 | static int linehandle_release(struct inode *inode, struct file *filep) | |
624 | { | |
625 | struct linehandle_state *lh = filep->private_data; | |
626 | struct gpio_device *gdev = lh->gdev; | |
627 | int i; | |
628 | ||
629 | for (i = 0; i < lh->numdescs; i++) | |
630 | gpiod_free(lh->descs[i]); | |
631 | kfree(lh->label); | |
632 | kfree(lh); | |
633 | put_device(&gdev->dev); | |
634 | return 0; | |
635 | } | |
636 | ||
637 | static const struct file_operations linehandle_fileops = { | |
638 | .release = linehandle_release, | |
639 | .owner = THIS_MODULE, | |
640 | .llseek = noop_llseek, | |
641 | .unlocked_ioctl = linehandle_ioctl, | |
642 | #ifdef CONFIG_COMPAT | |
643 | .compat_ioctl = linehandle_ioctl_compat, | |
644 | #endif | |
645 | }; | |
646 | ||
647 | static int linehandle_create(struct gpio_device *gdev, void __user *ip) | |
648 | { | |
649 | struct gpiohandle_request handlereq; | |
650 | struct linehandle_state *lh; | |
953b956a | 651 | struct file *file; |
ab3dbcf7 | 652 | int fd, i, count = 0, ret; |
418ee8e9 | 653 | u32 lflags; |
d7c51b47 LW |
654 | |
655 | if (copy_from_user(&handlereq, ip, sizeof(handlereq))) | |
656 | return -EFAULT; | |
657 | if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) | |
658 | return -EINVAL; | |
659 | ||
418ee8e9 BG |
660 | lflags = handlereq.flags; |
661 | ||
b043ed7e KG |
662 | ret = linehandle_validate_flags(lflags); |
663 | if (ret) | |
664 | return ret; | |
2148ad77 | 665 | |
d7c51b47 LW |
666 | lh = kzalloc(sizeof(*lh), GFP_KERNEL); |
667 | if (!lh) | |
668 | return -ENOMEM; | |
669 | lh->gdev = gdev; | |
670 | get_device(&gdev->dev); | |
671 | ||
672 | /* Make sure this is terminated */ | |
673 | handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0'; | |
674 | if (strlen(handlereq.consumer_label)) { | |
675 | lh->label = kstrdup(handlereq.consumer_label, | |
676 | GFP_KERNEL); | |
677 | if (!lh->label) { | |
678 | ret = -ENOMEM; | |
679 | goto out_free_lh; | |
680 | } | |
681 | } | |
682 | ||
683 | /* Request each GPIO */ | |
684 | for (i = 0; i < handlereq.lines; i++) { | |
685 | u32 offset = handlereq.lineoffsets[i]; | |
0f41dabe | 686 | struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); |
d7c51b47 | 687 | |
0f41dabe BG |
688 | if (IS_ERR(desc)) { |
689 | ret = PTR_ERR(desc); | |
e405f9fc LPC |
690 | goto out_free_descs; |
691 | } | |
692 | ||
d7c51b47 LW |
693 | ret = gpiod_request(desc, lh->label); |
694 | if (ret) | |
695 | goto out_free_descs; | |
696 | lh->descs[i] = desc; | |
19a4fbff | 697 | count = i + 1; |
d7c51b47 LW |
698 | |
699 | if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW) | |
700 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
701 | if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) | |
702 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); | |
703 | if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE) | |
704 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); | |
2148ad77 KG |
705 | if (lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) |
706 | set_bit(FLAG_BIAS_DISABLE, &desc->flags); | |
9225d516 DF |
707 | if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) |
708 | set_bit(FLAG_PULL_DOWN, &desc->flags); | |
709 | if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) | |
710 | set_bit(FLAG_PULL_UP, &desc->flags); | |
d7c51b47 | 711 | |
e10f72bf AJ |
712 | ret = gpiod_set_transitory(desc, false); |
713 | if (ret < 0) | |
714 | goto out_free_descs; | |
715 | ||
d7c51b47 LW |
716 | /* |
717 | * Lines have to be requested explicitly for input | |
718 | * or output, else the line will be treated "as is". | |
719 | */ | |
720 | if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { | |
721 | int val = !!handlereq.default_values[i]; | |
722 | ||
723 | ret = gpiod_direction_output(desc, val); | |
724 | if (ret) | |
725 | goto out_free_descs; | |
726 | } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { | |
727 | ret = gpiod_direction_input(desc); | |
728 | if (ret) | |
729 | goto out_free_descs; | |
730 | } | |
731 | dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", | |
732 | offset); | |
733 | } | |
e2f608be LW |
734 | /* Let i point at the last handle */ |
735 | i--; | |
d7c51b47 LW |
736 | lh->numdescs = handlereq.lines; |
737 | ||
953b956a | 738 | fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); |
d7c51b47 LW |
739 | if (fd < 0) { |
740 | ret = fd; | |
741 | goto out_free_descs; | |
742 | } | |
743 | ||
953b956a LPC |
744 | file = anon_inode_getfile("gpio-linehandle", |
745 | &linehandle_fileops, | |
746 | lh, | |
747 | O_RDONLY | O_CLOEXEC); | |
748 | if (IS_ERR(file)) { | |
749 | ret = PTR_ERR(file); | |
750 | goto out_put_unused_fd; | |
751 | } | |
752 | ||
d7c51b47 | 753 | handlereq.fd = fd; |
d932cd49 | 754 | if (copy_to_user(ip, &handlereq, sizeof(handlereq))) { |
953b956a LPC |
755 | /* |
756 | * fput() will trigger the release() callback, so do not go onto | |
757 | * the regular error cleanup path here. | |
758 | */ | |
759 | fput(file); | |
760 | put_unused_fd(fd); | |
761 | return -EFAULT; | |
d932cd49 | 762 | } |
d7c51b47 | 763 | |
953b956a LPC |
764 | fd_install(fd, file); |
765 | ||
d7c51b47 LW |
766 | dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", |
767 | lh->numdescs); | |
768 | ||
769 | return 0; | |
770 | ||
953b956a LPC |
771 | out_put_unused_fd: |
772 | put_unused_fd(fd); | |
d7c51b47 | 773 | out_free_descs: |
ab3dbcf7 | 774 | for (i = 0; i < count; i++) |
d7c51b47 LW |
775 | gpiod_free(lh->descs[i]); |
776 | kfree(lh->label); | |
777 | out_free_lh: | |
778 | kfree(lh); | |
779 | put_device(&gdev->dev); | |
780 | return ret; | |
781 | } | |
782 | ||
61f922db LW |
783 | /* |
784 | * GPIO line event management | |
785 | */ | |
786 | ||
787 | /** | |
788 | * struct lineevent_state - contains the state of a userspace event | |
789 | * @gdev: the GPIO device the event pertains to | |
790 | * @label: consumer label used to tag descriptors | |
791 | * @desc: the GPIO descriptor held by this event | |
792 | * @eflags: the event flags this line was requested with | |
793 | * @irq: the interrupt that trigger in response to events on this GPIO | |
794 | * @wait: wait queue that handles blocking reads of events | |
795 | * @events: KFIFO for the GPIO events | |
d58f2bf2 LW |
796 | * @timestamp: cache for the timestamp storing it between hardirq |
797 | * and IRQ thread, used to bring the timestamp close to the actual | |
798 | * event | |
61f922db LW |
799 | */ |
800 | struct lineevent_state { | |
801 | struct gpio_device *gdev; | |
802 | const char *label; | |
803 | struct gpio_desc *desc; | |
804 | u32 eflags; | |
805 | int irq; | |
806 | wait_queue_head_t wait; | |
807 | DECLARE_KFIFO(events, struct gpioevent_data, 16); | |
d58f2bf2 | 808 | u64 timestamp; |
61f922db LW |
809 | }; |
810 | ||
ac7dbb99 LPC |
811 | #define GPIOEVENT_REQUEST_VALID_FLAGS \ |
812 | (GPIOEVENT_REQUEST_RISING_EDGE | \ | |
813 | GPIOEVENT_REQUEST_FALLING_EDGE) | |
814 | ||
afc9a42b | 815 | static __poll_t lineevent_poll(struct file *filep, |
61f922db LW |
816 | struct poll_table_struct *wait) |
817 | { | |
818 | struct lineevent_state *le = filep->private_data; | |
afc9a42b | 819 | __poll_t events = 0; |
61f922db LW |
820 | |
821 | poll_wait(filep, &le->wait, wait); | |
822 | ||
dea9c80e | 823 | if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock)) |
a9a08845 | 824 | events = EPOLLIN | EPOLLRDNORM; |
61f922db LW |
825 | |
826 | return events; | |
827 | } | |
828 | ||
829 | ||
830 | static ssize_t lineevent_read(struct file *filep, | |
831 | char __user *buf, | |
832 | size_t count, | |
833 | loff_t *f_ps) | |
834 | { | |
835 | struct lineevent_state *le = filep->private_data; | |
df2cd589 | 836 | struct gpioevent_data ge; |
dea9c80e | 837 | ssize_t bytes_read = 0; |
61f922db LW |
838 | int ret; |
839 | ||
df2cd589 | 840 | if (count < sizeof(ge)) |
61f922db LW |
841 | return -EINVAL; |
842 | ||
843 | do { | |
dea9c80e | 844 | spin_lock(&le->wait.lock); |
61f922db | 845 | if (kfifo_is_empty(&le->events)) { |
dea9c80e BG |
846 | if (bytes_read) { |
847 | spin_unlock(&le->wait.lock); | |
848 | return bytes_read; | |
849 | } | |
850 | ||
851 | if (filep->f_flags & O_NONBLOCK) { | |
852 | spin_unlock(&le->wait.lock); | |
61f922db | 853 | return -EAGAIN; |
dea9c80e | 854 | } |
61f922db | 855 | |
dea9c80e | 856 | ret = wait_event_interruptible_locked(le->wait, |
61f922db | 857 | !kfifo_is_empty(&le->events)); |
dea9c80e BG |
858 | if (ret) { |
859 | spin_unlock(&le->wait.lock); | |
61f922db | 860 | return ret; |
dea9c80e | 861 | } |
61f922db LW |
862 | } |
863 | ||
df2cd589 | 864 | ret = kfifo_out(&le->events, &ge, 1); |
dea9c80e BG |
865 | spin_unlock(&le->wait.lock); |
866 | if (ret != 1) { | |
867 | /* | |
868 | * This should never happen - we were holding the lock | |
869 | * from the moment we learned the fifo is no longer | |
870 | * empty until now. | |
871 | */ | |
872 | ret = -EIO; | |
873 | break; | |
874 | } | |
61f922db | 875 | |
df2cd589 | 876 | if (copy_to_user(buf + bytes_read, &ge, sizeof(ge))) |
dea9c80e | 877 | return -EFAULT; |
df2cd589 AS |
878 | bytes_read += sizeof(ge); |
879 | } while (count >= bytes_read + sizeof(ge)); | |
61f922db | 880 | |
dea9c80e | 881 | return bytes_read; |
61f922db LW |
882 | } |
883 | ||
884 | static int lineevent_release(struct inode *inode, struct file *filep) | |
885 | { | |
886 | struct lineevent_state *le = filep->private_data; | |
887 | struct gpio_device *gdev = le->gdev; | |
888 | ||
889 | free_irq(le->irq, le); | |
890 | gpiod_free(le->desc); | |
891 | kfree(le->label); | |
892 | kfree(le); | |
893 | put_device(&gdev->dev); | |
894 | return 0; | |
895 | } | |
896 | ||
897 | static long lineevent_ioctl(struct file *filep, unsigned int cmd, | |
898 | unsigned long arg) | |
899 | { | |
900 | struct lineevent_state *le = filep->private_data; | |
901 | void __user *ip = (void __user *)arg; | |
902 | struct gpiohandle_data ghd; | |
903 | ||
904 | /* | |
905 | * We can get the value for an event line but not set it, | |
906 | * because it is input by definition. | |
907 | */ | |
908 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { | |
909 | int val; | |
910 | ||
d82aa4a8 LPC |
911 | memset(&ghd, 0, sizeof(ghd)); |
912 | ||
61f922db LW |
913 | val = gpiod_get_value_cansleep(le->desc); |
914 | if (val < 0) | |
915 | return val; | |
916 | ghd.values[0] = val; | |
917 | ||
918 | if (copy_to_user(ip, &ghd, sizeof(ghd))) | |
919 | return -EFAULT; | |
920 | ||
921 | return 0; | |
922 | } | |
923 | return -EINVAL; | |
924 | } | |
925 | ||
926 | #ifdef CONFIG_COMPAT | |
927 | static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd, | |
928 | unsigned long arg) | |
929 | { | |
930 | return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg)); | |
931 | } | |
932 | #endif | |
933 | ||
934 | static const struct file_operations lineevent_fileops = { | |
935 | .release = lineevent_release, | |
936 | .read = lineevent_read, | |
937 | .poll = lineevent_poll, | |
938 | .owner = THIS_MODULE, | |
939 | .llseek = noop_llseek, | |
940 | .unlocked_ioctl = lineevent_ioctl, | |
941 | #ifdef CONFIG_COMPAT | |
942 | .compat_ioctl = lineevent_ioctl_compat, | |
943 | #endif | |
944 | }; | |
945 | ||
33265b17 | 946 | static irqreturn_t lineevent_irq_thread(int irq, void *p) |
61f922db LW |
947 | { |
948 | struct lineevent_state *le = p; | |
949 | struct gpioevent_data ge; | |
fa38869b | 950 | int ret; |
61f922db | 951 | |
24bd3efc LW |
952 | /* Do not leak kernel stack to userspace */ |
953 | memset(&ge, 0, sizeof(ge)); | |
954 | ||
1033be58 BG |
955 | /* |
956 | * We may be running from a nested threaded interrupt in which case | |
957 | * we didn't get the timestamp from lineevent_irq_handler(). | |
958 | */ | |
959 | if (!le->timestamp) | |
f8850206 | 960 | ge.timestamp = ktime_get_ns(); |
1033be58 BG |
961 | else |
962 | ge.timestamp = le->timestamp; | |
61f922db | 963 | |
ad537b82 BG |
964 | if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE |
965 | && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { | |
fa38869b | 966 | int level = gpiod_get_value_cansleep(le->desc); |
61f922db LW |
967 | if (level) |
968 | /* Emit low-to-high event */ | |
969 | ge.id = GPIOEVENT_EVENT_RISING_EDGE; | |
970 | else | |
971 | /* Emit high-to-low event */ | |
972 | ge.id = GPIOEVENT_EVENT_FALLING_EDGE; | |
fa38869b | 973 | } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) { |
61f922db LW |
974 | /* Emit low-to-high event */ |
975 | ge.id = GPIOEVENT_EVENT_RISING_EDGE; | |
fa38869b | 976 | } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { |
61f922db LW |
977 | /* Emit high-to-low event */ |
978 | ge.id = GPIOEVENT_EVENT_FALLING_EDGE; | |
bc0207a5 AB |
979 | } else { |
980 | return IRQ_NONE; | |
61f922db LW |
981 | } |
982 | ||
dea9c80e BG |
983 | ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge, |
984 | 1, &le->wait.lock); | |
2efc6bfa | 985 | if (ret) |
a9a08845 | 986 | wake_up_poll(&le->wait, EPOLLIN); |
248ae175 BG |
987 | else |
988 | pr_debug_ratelimited("event FIFO is full - event dropped\n"); | |
61f922db LW |
989 | |
990 | return IRQ_HANDLED; | |
991 | } | |
992 | ||
d58f2bf2 LW |
993 | static irqreturn_t lineevent_irq_handler(int irq, void *p) |
994 | { | |
995 | struct lineevent_state *le = p; | |
996 | ||
997 | /* | |
998 | * Just store the timestamp in hardirq context so we get it as | |
999 | * close in time as possible to the actual event. | |
1000 | */ | |
f8850206 | 1001 | le->timestamp = ktime_get_ns(); |
d58f2bf2 LW |
1002 | |
1003 | return IRQ_WAKE_THREAD; | |
1004 | } | |
1005 | ||
61f922db LW |
1006 | static int lineevent_create(struct gpio_device *gdev, void __user *ip) |
1007 | { | |
1008 | struct gpioevent_request eventreq; | |
1009 | struct lineevent_state *le; | |
1010 | struct gpio_desc *desc; | |
953b956a | 1011 | struct file *file; |
61f922db LW |
1012 | u32 offset; |
1013 | u32 lflags; | |
1014 | u32 eflags; | |
1015 | int fd; | |
1016 | int ret; | |
1017 | int irqflags = 0; | |
1018 | ||
1019 | if (copy_from_user(&eventreq, ip, sizeof(eventreq))) | |
1020 | return -EFAULT; | |
1021 | ||
bcc6d99a BG |
1022 | offset = eventreq.lineoffset; |
1023 | lflags = eventreq.handleflags; | |
1024 | eflags = eventreq.eventflags; | |
1025 | ||
45e23604 BG |
1026 | desc = gpiochip_get_desc(gdev->chip, offset); |
1027 | if (IS_ERR(desc)) | |
1028 | return PTR_ERR(desc); | |
bcc6d99a BG |
1029 | |
1030 | /* Return an error if a unknown flag is set */ | |
1031 | if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || | |
1032 | (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) | |
1033 | return -EINVAL; | |
1034 | ||
1035 | /* This is just wrong: we don't look for events on output lines */ | |
1036 | if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || | |
1037 | (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || | |
1038 | (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) | |
1039 | return -EINVAL; | |
1040 | ||
2148ad77 KG |
1041 | /* Only one bias flag can be set. */ |
1042 | if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && | |
1043 | (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | | |
1044 | GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || | |
1045 | ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && | |
1046 | (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) | |
1047 | return -EINVAL; | |
1048 | ||
61f922db LW |
1049 | le = kzalloc(sizeof(*le), GFP_KERNEL); |
1050 | if (!le) | |
1051 | return -ENOMEM; | |
1052 | le->gdev = gdev; | |
1053 | get_device(&gdev->dev); | |
1054 | ||
1055 | /* Make sure this is terminated */ | |
1056 | eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0'; | |
1057 | if (strlen(eventreq.consumer_label)) { | |
1058 | le->label = kstrdup(eventreq.consumer_label, | |
1059 | GFP_KERNEL); | |
1060 | if (!le->label) { | |
1061 | ret = -ENOMEM; | |
1062 | goto out_free_le; | |
1063 | } | |
1064 | } | |
1065 | ||
61f922db LW |
1066 | ret = gpiod_request(desc, le->label); |
1067 | if (ret) | |
f001cc35 | 1068 | goto out_free_label; |
61f922db LW |
1069 | le->desc = desc; |
1070 | le->eflags = eflags; | |
1071 | ||
1072 | if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW) | |
1073 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
2148ad77 KG |
1074 | if (lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) |
1075 | set_bit(FLAG_BIAS_DISABLE, &desc->flags); | |
7b479a84 KG |
1076 | if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) |
1077 | set_bit(FLAG_PULL_DOWN, &desc->flags); | |
1078 | if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) | |
1079 | set_bit(FLAG_PULL_UP, &desc->flags); | |
61f922db LW |
1080 | |
1081 | ret = gpiod_direction_input(desc); | |
1082 | if (ret) | |
1083 | goto out_free_desc; | |
1084 | ||
1085 | le->irq = gpiod_to_irq(desc); | |
1086 | if (le->irq <= 0) { | |
1087 | ret = -ENODEV; | |
1088 | goto out_free_desc; | |
1089 | } | |
1090 | ||
1091 | if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) | |
223ecaf1 MW |
1092 | irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
1093 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; | |
61f922db | 1094 | if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) |
223ecaf1 MW |
1095 | irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
1096 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; | |
61f922db | 1097 | irqflags |= IRQF_ONESHOT; |
61f922db LW |
1098 | |
1099 | INIT_KFIFO(le->events); | |
1100 | init_waitqueue_head(&le->wait); | |
61f922db LW |
1101 | |
1102 | /* Request a thread to read the events */ | |
1103 | ret = request_threaded_irq(le->irq, | |
d58f2bf2 | 1104 | lineevent_irq_handler, |
61f922db LW |
1105 | lineevent_irq_thread, |
1106 | irqflags, | |
1107 | le->label, | |
1108 | le); | |
1109 | if (ret) | |
1110 | goto out_free_desc; | |
1111 | ||
953b956a | 1112 | fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); |
61f922db LW |
1113 | if (fd < 0) { |
1114 | ret = fd; | |
1115 | goto out_free_irq; | |
1116 | } | |
1117 | ||
953b956a LPC |
1118 | file = anon_inode_getfile("gpio-event", |
1119 | &lineevent_fileops, | |
1120 | le, | |
1121 | O_RDONLY | O_CLOEXEC); | |
1122 | if (IS_ERR(file)) { | |
1123 | ret = PTR_ERR(file); | |
1124 | goto out_put_unused_fd; | |
1125 | } | |
1126 | ||
61f922db | 1127 | eventreq.fd = fd; |
d932cd49 | 1128 | if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { |
953b956a LPC |
1129 | /* |
1130 | * fput() will trigger the release() callback, so do not go onto | |
1131 | * the regular error cleanup path here. | |
1132 | */ | |
1133 | fput(file); | |
1134 | put_unused_fd(fd); | |
1135 | return -EFAULT; | |
d932cd49 | 1136 | } |
61f922db | 1137 | |
953b956a LPC |
1138 | fd_install(fd, file); |
1139 | ||
61f922db LW |
1140 | return 0; |
1141 | ||
953b956a LPC |
1142 | out_put_unused_fd: |
1143 | put_unused_fd(fd); | |
61f922db LW |
1144 | out_free_irq: |
1145 | free_irq(le->irq, le); | |
1146 | out_free_desc: | |
1147 | gpiod_free(le->desc); | |
1148 | out_free_label: | |
1149 | kfree(le->label); | |
1150 | out_free_le: | |
1151 | kfree(le); | |
1152 | put_device(&gdev->dev); | |
1153 | return ret; | |
1154 | } | |
1155 | ||
d2ac2579 BG |
1156 | static void gpio_desc_to_lineinfo(struct gpio_desc *desc, |
1157 | struct gpioline_info *info) | |
1158 | { | |
1159 | struct gpio_chip *chip = desc->gdev->chip; | |
1160 | unsigned long flags; | |
1161 | ||
1162 | spin_lock_irqsave(&gpio_lock, flags); | |
1163 | ||
1164 | if (desc->name) { | |
1165 | strncpy(info->name, desc->name, sizeof(info->name)); | |
1166 | info->name[sizeof(info->name) - 1] = '\0'; | |
1167 | } else { | |
1168 | info->name[0] = '\0'; | |
1169 | } | |
1170 | ||
1171 | if (desc->label) { | |
1172 | strncpy(info->consumer, desc->label, sizeof(info->consumer)); | |
1173 | info->consumer[sizeof(info->consumer) - 1] = '\0'; | |
1174 | } else { | |
1175 | info->consumer[0] = '\0'; | |
1176 | } | |
1177 | ||
1178 | /* | |
1179 | * Userspace only need to know that the kernel is using this GPIO so | |
1180 | * it can't use it. | |
1181 | */ | |
1182 | info->flags = 0; | |
1183 | if (test_bit(FLAG_REQUESTED, &desc->flags) || | |
1184 | test_bit(FLAG_IS_HOGGED, &desc->flags) || | |
1185 | test_bit(FLAG_USED_AS_IRQ, &desc->flags) || | |
1186 | test_bit(FLAG_EXPORT, &desc->flags) || | |
1187 | test_bit(FLAG_SYSFS, &desc->flags) || | |
1188 | !pinctrl_gpio_can_use_line(chip->base + info->line_offset)) | |
1189 | info->flags |= GPIOLINE_FLAG_KERNEL; | |
1190 | if (test_bit(FLAG_IS_OUT, &desc->flags)) | |
1191 | info->flags |= GPIOLINE_FLAG_IS_OUT; | |
1192 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
1193 | info->flags |= GPIOLINE_FLAG_ACTIVE_LOW; | |
1194 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | |
1195 | info->flags |= (GPIOLINE_FLAG_OPEN_DRAIN | | |
1196 | GPIOLINE_FLAG_IS_OUT); | |
1197 | if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | |
1198 | info->flags |= (GPIOLINE_FLAG_OPEN_SOURCE | | |
1199 | GPIOLINE_FLAG_IS_OUT); | |
1200 | if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) | |
1201 | info->flags |= GPIOLINE_FLAG_BIAS_DISABLE; | |
1202 | if (test_bit(FLAG_PULL_DOWN, &desc->flags)) | |
1203 | info->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; | |
1204 | if (test_bit(FLAG_PULL_UP, &desc->flags)) | |
1205 | info->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; | |
1206 | ||
1207 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1208 | } | |
1209 | ||
51c1064e BG |
1210 | struct gpio_chardev_data { |
1211 | struct gpio_device *gdev; | |
1212 | wait_queue_head_t wait; | |
1213 | DECLARE_KFIFO(events, struct gpioline_info_changed, 32); | |
1214 | struct notifier_block lineinfo_changed_nb; | |
1215 | unsigned long *watched_lines; | |
1216 | }; | |
1217 | ||
950d55f5 | 1218 | /* |
3c702e99 LW |
1219 | * gpio_ioctl() - ioctl handler for the GPIO chardev |
1220 | */ | |
1221 | static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) | |
1222 | { | |
51c1064e BG |
1223 | struct gpio_chardev_data *priv = filp->private_data; |
1224 | struct gpio_device *gdev = priv->gdev; | |
3c702e99 | 1225 | struct gpio_chip *chip = gdev->chip; |
8b92e17e | 1226 | void __user *ip = (void __user *)arg; |
51c1064e BG |
1227 | struct gpio_desc *desc; |
1228 | __u32 offset; | |
3c702e99 LW |
1229 | |
1230 | /* We fail any subsequent ioctl():s when the chip is gone */ | |
1231 | if (!chip) | |
1232 | return -ENODEV; | |
1233 | ||
521a2ad6 | 1234 | /* Fill in the struct and pass to userspace */ |
3c702e99 | 1235 | if (cmd == GPIO_GET_CHIPINFO_IOCTL) { |
521a2ad6 LW |
1236 | struct gpiochip_info chipinfo; |
1237 | ||
0f4bbb23 LPC |
1238 | memset(&chipinfo, 0, sizeof(chipinfo)); |
1239 | ||
3c702e99 LW |
1240 | strncpy(chipinfo.name, dev_name(&gdev->dev), |
1241 | sizeof(chipinfo.name)); | |
1242 | chipinfo.name[sizeof(chipinfo.name)-1] = '\0'; | |
df4878e9 LW |
1243 | strncpy(chipinfo.label, gdev->label, |
1244 | sizeof(chipinfo.label)); | |
1245 | chipinfo.label[sizeof(chipinfo.label)-1] = '\0'; | |
fdeb8e15 | 1246 | chipinfo.lines = gdev->ngpio; |
3c702e99 LW |
1247 | if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) |
1248 | return -EFAULT; | |
1249 | return 0; | |
51c1064e BG |
1250 | } else if (cmd == GPIO_GET_LINEINFO_IOCTL || |
1251 | cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) { | |
521a2ad6 | 1252 | struct gpioline_info lineinfo; |
521a2ad6 LW |
1253 | |
1254 | if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) | |
1255 | return -EFAULT; | |
521a2ad6 | 1256 | |
2a2cabd8 BG |
1257 | desc = gpiochip_get_desc(chip, lineinfo.line_offset); |
1258 | if (IS_ERR(desc)) | |
1259 | return PTR_ERR(desc); | |
1260 | ||
d2ac2579 | 1261 | gpio_desc_to_lineinfo(desc, &lineinfo); |
521a2ad6 LW |
1262 | |
1263 | if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) | |
1264 | return -EFAULT; | |
51c1064e BG |
1265 | |
1266 | if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) | |
19314797 | 1267 | set_bit(gpio_chip_hwgpio(desc), priv->watched_lines); |
51c1064e | 1268 | |
521a2ad6 | 1269 | return 0; |
d7c51b47 LW |
1270 | } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) { |
1271 | return linehandle_create(gdev, ip); | |
61f922db LW |
1272 | } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) { |
1273 | return lineevent_create(gdev, ip); | |
51c1064e BG |
1274 | } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) { |
1275 | if (copy_from_user(&offset, ip, sizeof(offset))) | |
1276 | return -EFAULT; | |
1277 | ||
1278 | desc = gpiochip_get_desc(chip, offset); | |
1279 | if (IS_ERR(desc)) | |
1280 | return PTR_ERR(desc); | |
1281 | ||
19314797 | 1282 | clear_bit(gpio_chip_hwgpio(desc), priv->watched_lines); |
51c1064e | 1283 | return 0; |
3c702e99 LW |
1284 | } |
1285 | return -EINVAL; | |
1286 | } | |
1287 | ||
8b92e17e LW |
1288 | #ifdef CONFIG_COMPAT |
1289 | static long gpio_ioctl_compat(struct file *filp, unsigned int cmd, | |
1290 | unsigned long arg) | |
1291 | { | |
1292 | return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); | |
1293 | } | |
1294 | #endif | |
1295 | ||
51c1064e BG |
1296 | static struct gpio_chardev_data * |
1297 | to_gpio_chardev_data(struct notifier_block *nb) | |
1298 | { | |
1299 | return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); | |
1300 | } | |
1301 | ||
1302 | static int lineinfo_changed_notify(struct notifier_block *nb, | |
1303 | unsigned long action, void *data) | |
1304 | { | |
1305 | struct gpio_chardev_data *priv = to_gpio_chardev_data(nb); | |
1306 | struct gpioline_info_changed chg; | |
1307 | struct gpio_desc *desc = data; | |
1308 | int ret; | |
1309 | ||
19314797 | 1310 | if (!test_bit(gpio_chip_hwgpio(desc), priv->watched_lines)) |
51c1064e BG |
1311 | return NOTIFY_DONE; |
1312 | ||
1313 | memset(&chg, 0, sizeof(chg)); | |
1314 | chg.info.line_offset = gpio_chip_hwgpio(desc); | |
1315 | chg.event_type = action; | |
1316 | chg.timestamp = ktime_get_ns(); | |
1317 | gpio_desc_to_lineinfo(desc, &chg.info); | |
1318 | ||
1319 | ret = kfifo_in_spinlocked(&priv->events, &chg, 1, &priv->wait.lock); | |
1320 | if (ret) | |
1321 | wake_up_poll(&priv->wait, EPOLLIN); | |
1322 | else | |
1323 | pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); | |
1324 | ||
1325 | return NOTIFY_OK; | |
1326 | } | |
1327 | ||
1328 | static __poll_t lineinfo_watch_poll(struct file *filep, | |
1329 | struct poll_table_struct *pollt) | |
1330 | { | |
1331 | struct gpio_chardev_data *priv = filep->private_data; | |
1332 | __poll_t events = 0; | |
1333 | ||
1334 | poll_wait(filep, &priv->wait, pollt); | |
1335 | ||
1336 | if (!kfifo_is_empty_spinlocked_noirqsave(&priv->events, | |
1337 | &priv->wait.lock)) | |
1338 | events = EPOLLIN | EPOLLRDNORM; | |
1339 | ||
1340 | return events; | |
1341 | } | |
1342 | ||
1343 | static ssize_t lineinfo_watch_read(struct file *filep, char __user *buf, | |
1344 | size_t count, loff_t *off) | |
1345 | { | |
1346 | struct gpio_chardev_data *priv = filep->private_data; | |
1347 | struct gpioline_info_changed event; | |
1348 | ssize_t bytes_read = 0; | |
1349 | int ret; | |
1350 | ||
1351 | if (count < sizeof(event)) | |
1352 | return -EINVAL; | |
1353 | ||
1354 | do { | |
1355 | spin_lock(&priv->wait.lock); | |
1356 | if (kfifo_is_empty(&priv->events)) { | |
1357 | if (bytes_read) { | |
1358 | spin_unlock(&priv->wait.lock); | |
1359 | return bytes_read; | |
1360 | } | |
1361 | ||
1362 | if (filep->f_flags & O_NONBLOCK) { | |
1363 | spin_unlock(&priv->wait.lock); | |
1364 | return -EAGAIN; | |
1365 | } | |
1366 | ||
1367 | ret = wait_event_interruptible_locked(priv->wait, | |
1368 | !kfifo_is_empty(&priv->events)); | |
1369 | if (ret) { | |
1370 | spin_unlock(&priv->wait.lock); | |
1371 | return ret; | |
1372 | } | |
1373 | } | |
1374 | ||
1375 | ret = kfifo_out(&priv->events, &event, 1); | |
1376 | spin_unlock(&priv->wait.lock); | |
1377 | if (ret != 1) { | |
1378 | ret = -EIO; | |
1379 | break; | |
1380 | /* We should never get here. See lineevent_read(). */ | |
1381 | } | |
1382 | ||
1383 | if (copy_to_user(buf + bytes_read, &event, sizeof(event))) | |
1384 | return -EFAULT; | |
1385 | bytes_read += sizeof(event); | |
1386 | } while (count >= bytes_read + sizeof(event)); | |
1387 | ||
1388 | return bytes_read; | |
1389 | } | |
1390 | ||
3c702e99 LW |
1391 | /** |
1392 | * gpio_chrdev_open() - open the chardev for ioctl operations | |
1393 | * @inode: inode for this chardev | |
1394 | * @filp: file struct for storing private data | |
1395 | * Returns 0 on success | |
1396 | */ | |
1397 | static int gpio_chrdev_open(struct inode *inode, struct file *filp) | |
1398 | { | |
1399 | struct gpio_device *gdev = container_of(inode->i_cdev, | |
1400 | struct gpio_device, chrdev); | |
51c1064e BG |
1401 | struct gpio_chardev_data *priv; |
1402 | int ret = -ENOMEM; | |
3c702e99 LW |
1403 | |
1404 | /* Fail on open if the backing gpiochip is gone */ | |
fb505747 | 1405 | if (!gdev->chip) |
3c702e99 | 1406 | return -ENODEV; |
51c1064e BG |
1407 | |
1408 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); | |
1409 | if (!priv) | |
1410 | return -ENOMEM; | |
1411 | ||
1412 | priv->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL); | |
1413 | if (!priv->watched_lines) | |
1414 | goto out_free_priv; | |
1415 | ||
1416 | init_waitqueue_head(&priv->wait); | |
1417 | INIT_KFIFO(priv->events); | |
1418 | priv->gdev = gdev; | |
1419 | ||
1420 | priv->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify; | |
1421 | ret = atomic_notifier_chain_register(&gdev->notifier, | |
1422 | &priv->lineinfo_changed_nb); | |
1423 | if (ret) | |
1424 | goto out_free_bitmap; | |
1425 | ||
3c702e99 | 1426 | get_device(&gdev->dev); |
51c1064e | 1427 | filp->private_data = priv; |
f4e81c52 | 1428 | |
51c1064e BG |
1429 | ret = nonseekable_open(inode, filp); |
1430 | if (ret) | |
1431 | goto out_unregister_notifier; | |
1432 | ||
1433 | return ret; | |
1434 | ||
1435 | out_unregister_notifier: | |
1436 | atomic_notifier_chain_unregister(&gdev->notifier, | |
1437 | &priv->lineinfo_changed_nb); | |
1438 | out_free_bitmap: | |
1439 | bitmap_free(priv->watched_lines); | |
1440 | out_free_priv: | |
1441 | kfree(priv); | |
1442 | return ret; | |
3c702e99 LW |
1443 | } |
1444 | ||
1445 | /** | |
1446 | * gpio_chrdev_release() - close chardev after ioctl operations | |
1447 | * @inode: inode for this chardev | |
1448 | * @filp: file struct for storing private data | |
1449 | * Returns 0 on success | |
1450 | */ | |
1451 | static int gpio_chrdev_release(struct inode *inode, struct file *filp) | |
1452 | { | |
51c1064e BG |
1453 | struct gpio_chardev_data *priv = filp->private_data; |
1454 | struct gpio_device *gdev = priv->gdev; | |
3c702e99 | 1455 | |
51c1064e BG |
1456 | bitmap_free(priv->watched_lines); |
1457 | atomic_notifier_chain_unregister(&gdev->notifier, | |
1458 | &priv->lineinfo_changed_nb); | |
3c702e99 | 1459 | put_device(&gdev->dev); |
51c1064e BG |
1460 | kfree(priv); |
1461 | ||
3c702e99 LW |
1462 | return 0; |
1463 | } | |
1464 | ||
3c702e99 LW |
1465 | static const struct file_operations gpio_fileops = { |
1466 | .release = gpio_chrdev_release, | |
1467 | .open = gpio_chrdev_open, | |
51c1064e BG |
1468 | .poll = lineinfo_watch_poll, |
1469 | .read = lineinfo_watch_read, | |
3c702e99 | 1470 | .owner = THIS_MODULE, |
f4e81c52 | 1471 | .llseek = no_llseek, |
3c702e99 | 1472 | .unlocked_ioctl = gpio_ioctl, |
8b92e17e LW |
1473 | #ifdef CONFIG_COMPAT |
1474 | .compat_ioctl = gpio_ioctl_compat, | |
1475 | #endif | |
3c702e99 LW |
1476 | }; |
1477 | ||
ff2b1359 LW |
1478 | static void gpiodevice_release(struct device *dev) |
1479 | { | |
1480 | struct gpio_device *gdev = dev_get_drvdata(dev); | |
1481 | ||
1482 | list_del(&gdev->list); | |
1483 | ida_simple_remove(&gpio_ida, gdev->id); | |
fcf273e5 | 1484 | kfree_const(gdev->label); |
476e2fc5 | 1485 | kfree(gdev->descs); |
9efd9e69 | 1486 | kfree(gdev); |
ff2b1359 LW |
1487 | } |
1488 | ||
159f3cd9 GR |
1489 | static int gpiochip_setup_dev(struct gpio_device *gdev) |
1490 | { | |
d377f56f | 1491 | int ret; |
159f3cd9 GR |
1492 | |
1493 | cdev_init(&gdev->chrdev, &gpio_fileops); | |
1494 | gdev->chrdev.owner = THIS_MODULE; | |
159f3cd9 | 1495 | gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id); |
111379dc | 1496 | |
d377f56f LW |
1497 | ret = cdev_device_add(&gdev->chrdev, &gdev->dev); |
1498 | if (ret) | |
1499 | return ret; | |
111379dc LG |
1500 | |
1501 | chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", | |
1502 | MAJOR(gpio_devt), gdev->id); | |
159f3cd9 | 1503 | |
d377f56f LW |
1504 | ret = gpiochip_sysfs_register(gdev); |
1505 | if (ret) | |
159f3cd9 GR |
1506 | goto err_remove_device; |
1507 | ||
1508 | /* From this point, the .release() function cleans up gpio_device */ | |
1509 | gdev->dev.release = gpiodevice_release; | |
159f3cd9 GR |
1510 | pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n", |
1511 | __func__, gdev->base, gdev->base + gdev->ngpio - 1, | |
1512 | dev_name(&gdev->dev), gdev->chip->label ? : "generic"); | |
1513 | ||
1514 | return 0; | |
1515 | ||
1516 | err_remove_device: | |
111379dc | 1517 | cdev_device_del(&gdev->chrdev, &gdev->dev); |
d377f56f | 1518 | return ret; |
159f3cd9 GR |
1519 | } |
1520 | ||
a411e81e BG |
1521 | static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog) |
1522 | { | |
1523 | struct gpio_desc *desc; | |
1524 | int rv; | |
1525 | ||
1526 | desc = gpiochip_get_desc(chip, hog->chip_hwnum); | |
1527 | if (IS_ERR(desc)) { | |
1528 | pr_err("%s: unable to get GPIO desc: %ld\n", | |
1529 | __func__, PTR_ERR(desc)); | |
1530 | return; | |
1531 | } | |
1532 | ||
ba3efdff | 1533 | if (test_bit(FLAG_IS_HOGGED, &desc->flags)) |
a411e81e BG |
1534 | return; |
1535 | ||
1536 | rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags); | |
1537 | if (rv) | |
1538 | pr_err("%s: unable to hog GPIO line (%s:%u): %d\n", | |
1539 | __func__, chip->label, hog->chip_hwnum, rv); | |
1540 | } | |
1541 | ||
1542 | static void machine_gpiochip_add(struct gpio_chip *chip) | |
1543 | { | |
1544 | struct gpiod_hog *hog; | |
1545 | ||
1546 | mutex_lock(&gpio_machine_hogs_mutex); | |
1547 | ||
1548 | list_for_each_entry(hog, &gpio_machine_hogs, list) { | |
1549 | if (!strcmp(chip->label, hog->chip_label)) | |
1550 | gpiochip_machine_hog(chip, hog); | |
1551 | } | |
1552 | ||
1553 | mutex_unlock(&gpio_machine_hogs_mutex); | |
1554 | } | |
1555 | ||
159f3cd9 GR |
1556 | static void gpiochip_setup_devs(void) |
1557 | { | |
1558 | struct gpio_device *gdev; | |
d377f56f | 1559 | int ret; |
159f3cd9 GR |
1560 | |
1561 | list_for_each_entry(gdev, &gpio_devices, list) { | |
d377f56f LW |
1562 | ret = gpiochip_setup_dev(gdev); |
1563 | if (ret) | |
159f3cd9 | 1564 | pr_err("%s: Failed to initialize gpio device (%d)\n", |
d377f56f | 1565 | dev_name(&gdev->dev), ret); |
159f3cd9 GR |
1566 | } |
1567 | } | |
1568 | ||
959bc7b2 | 1569 | int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data, |
39c3fd58 AL |
1570 | struct lock_class_key *lock_key, |
1571 | struct lock_class_key *request_key) | |
d2876d08 DB |
1572 | { |
1573 | unsigned long flags; | |
d377f56f | 1574 | int ret = 0; |
ff2b1359 | 1575 | unsigned i; |
8d0aab2f | 1576 | int base = chip->base; |
ff2b1359 | 1577 | struct gpio_device *gdev; |
d2876d08 | 1578 | |
ff2b1359 LW |
1579 | /* |
1580 | * First: allocate and populate the internal stat container, and | |
1581 | * set up the struct device. | |
1582 | */ | |
969f07b4 | 1583 | gdev = kzalloc(sizeof(*gdev), GFP_KERNEL); |
ff2b1359 | 1584 | if (!gdev) |
14e85c0e | 1585 | return -ENOMEM; |
3c702e99 | 1586 | gdev->dev.bus = &gpio_bus_type; |
ff2b1359 LW |
1587 | gdev->chip = chip; |
1588 | chip->gpiodev = gdev; | |
1589 | if (chip->parent) { | |
1590 | gdev->dev.parent = chip->parent; | |
1591 | gdev->dev.of_node = chip->parent->of_node; | |
acc6e331 TR |
1592 | } |
1593 | ||
ff2b1359 LW |
1594 | #ifdef CONFIG_OF_GPIO |
1595 | /* If the gpiochip has an assigned OF node this takes precedence */ | |
acc6e331 TR |
1596 | if (chip->of_node) |
1597 | gdev->dev.of_node = chip->of_node; | |
6ff04974 BD |
1598 | else |
1599 | chip->of_node = gdev->dev.of_node; | |
ff2b1359 | 1600 | #endif |
acc6e331 | 1601 | |
ff2b1359 LW |
1602 | gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL); |
1603 | if (gdev->id < 0) { | |
d377f56f | 1604 | ret = gdev->id; |
ff2b1359 LW |
1605 | goto err_free_gdev; |
1606 | } | |
ddd8891e | 1607 | dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id); |
ff2b1359 LW |
1608 | device_initialize(&gdev->dev); |
1609 | dev_set_drvdata(&gdev->dev, gdev); | |
1610 | if (chip->parent && chip->parent->driver) | |
1611 | gdev->owner = chip->parent->driver->owner; | |
1612 | else if (chip->owner) | |
1613 | /* TODO: remove chip->owner */ | |
1614 | gdev->owner = chip->owner; | |
1615 | else | |
1616 | gdev->owner = THIS_MODULE; | |
d2876d08 | 1617 | |
476e2fc5 | 1618 | gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL); |
1c3cdb18 | 1619 | if (!gdev->descs) { |
d377f56f | 1620 | ret = -ENOMEM; |
a05a1404 | 1621 | goto err_free_ida; |
ff2b1359 LW |
1622 | } |
1623 | ||
5ed41cc4 BJZ |
1624 | if (chip->ngpio == 0) { |
1625 | chip_err(chip, "tried to insert a GPIO chip with zero lines\n"); | |
d377f56f | 1626 | ret = -EINVAL; |
159f3cd9 | 1627 | goto err_free_descs; |
5ed41cc4 | 1628 | } |
df4878e9 | 1629 | |
3027743f LA |
1630 | if (chip->ngpio > FASTPATH_NGPIO) |
1631 | chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n", | |
2ddac5ae | 1632 | chip->ngpio, FASTPATH_NGPIO); |
3027743f | 1633 | |
fcf273e5 | 1634 | gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL); |
df4878e9 | 1635 | if (!gdev->label) { |
d377f56f | 1636 | ret = -ENOMEM; |
476e2fc5 | 1637 | goto err_free_descs; |
df4878e9 LW |
1638 | } |
1639 | ||
fdeb8e15 | 1640 | gdev->ngpio = chip->ngpio; |
43c54eca | 1641 | gdev->data = data; |
5ed41cc4 | 1642 | |
d2876d08 DB |
1643 | spin_lock_irqsave(&gpio_lock, flags); |
1644 | ||
fdeb8e15 LW |
1645 | /* |
1646 | * TODO: this allocates a Linux GPIO number base in the global | |
1647 | * GPIO numberspace for this chip. In the long run we want to | |
1648 | * get *rid* of this numberspace and use only descriptors, but | |
1649 | * it may be a pipe dream. It will not happen before we get rid | |
1650 | * of the sysfs interface anyways. | |
1651 | */ | |
8d0aab2f AV |
1652 | if (base < 0) { |
1653 | base = gpiochip_find_base(chip->ngpio); | |
1654 | if (base < 0) { | |
d377f56f | 1655 | ret = base; |
225fce83 | 1656 | spin_unlock_irqrestore(&gpio_lock, flags); |
476e2fc5 | 1657 | goto err_free_label; |
8d0aab2f | 1658 | } |
fdeb8e15 LW |
1659 | /* |
1660 | * TODO: it should not be necessary to reflect the assigned | |
1661 | * base outside of the GPIO subsystem. Go over drivers and | |
1662 | * see if anyone makes use of this, else drop this and assign | |
1663 | * a poison instead. | |
1664 | */ | |
8d0aab2f AV |
1665 | chip->base = base; |
1666 | } | |
fdeb8e15 | 1667 | gdev->base = base; |
8d0aab2f | 1668 | |
d377f56f LW |
1669 | ret = gpiodev_add_to_list(gdev); |
1670 | if (ret) { | |
05aa5203 | 1671 | spin_unlock_irqrestore(&gpio_lock, flags); |
476e2fc5 | 1672 | goto err_free_label; |
05aa5203 | 1673 | } |
1a989d0f | 1674 | |
767cd17a RRD |
1675 | for (i = 0; i < chip->ngpio; i++) |
1676 | gdev->descs[i].gdev = gdev; | |
14e85c0e | 1677 | |
207270dd DC |
1678 | spin_unlock_irqrestore(&gpio_lock, flags); |
1679 | ||
51c1064e BG |
1680 | ATOMIC_INIT_NOTIFIER_HEAD(&gdev->notifier); |
1681 | ||
f23f1516 | 1682 | #ifdef CONFIG_PINCTRL |
20ec3e39 | 1683 | INIT_LIST_HEAD(&gdev->pin_ranges); |
f23f1516 SH |
1684 | #endif |
1685 | ||
d377f56f LW |
1686 | ret = gpiochip_set_desc_names(chip); |
1687 | if (ret) | |
5f3ca732 MP |
1688 | goto err_remove_from_list; |
1689 | ||
d377f56f LW |
1690 | ret = gpiochip_alloc_valid_mask(chip); |
1691 | if (ret) | |
48057ed1 | 1692 | goto err_remove_from_list; |
e0d89728 | 1693 | |
d377f56f LW |
1694 | ret = of_gpiochip_add(chip); |
1695 | if (ret) | |
48057ed1 | 1696 | goto err_free_gpiochip_mask; |
28355f81 | 1697 | |
d377f56f LW |
1698 | ret = gpiochip_init_valid_mask(chip); |
1699 | if (ret) | |
35779890 | 1700 | goto err_remove_of_chip; |
f8ec92a9 | 1701 | |
3edfb7bd RRD |
1702 | for (i = 0; i < chip->ngpio; i++) { |
1703 | struct gpio_desc *desc = &gdev->descs[i]; | |
1704 | ||
d95da993 | 1705 | if (chip->get_direction && gpiochip_line_is_valid(chip, i)) { |
4fc5bfeb AS |
1706 | assign_bit(FLAG_IS_OUT, |
1707 | &desc->flags, !chip->get_direction(chip, i)); | |
d95da993 | 1708 | } else { |
4fc5bfeb AS |
1709 | assign_bit(FLAG_IS_OUT, |
1710 | &desc->flags, !chip->direction_input); | |
d95da993 | 1711 | } |
3edfb7bd RRD |
1712 | } |
1713 | ||
b056ca1c AS |
1714 | ret = gpiochip_add_pin_ranges(chip); |
1715 | if (ret) | |
1716 | goto err_remove_of_chip; | |
1717 | ||
664e3e5a | 1718 | acpi_gpiochip_add(chip); |
391c970c | 1719 | |
a411e81e BG |
1720 | machine_gpiochip_add(chip); |
1721 | ||
504369cd | 1722 | ret = gpiochip_irqchip_init_valid_mask(chip); |
9411e3aa AS |
1723 | if (ret) |
1724 | goto err_remove_acpi_chip; | |
1725 | ||
504369cd | 1726 | ret = gpiochip_irqchip_init_hw(chip); |
fbdf8d4b | 1727 | if (ret) |
48057ed1 LW |
1728 | goto err_remove_acpi_chip; |
1729 | ||
fbdf8d4b LW |
1730 | ret = gpiochip_add_irqchip(chip, lock_key, request_key); |
1731 | if (ret) | |
48057ed1 LW |
1732 | goto err_remove_irqchip_mask; |
1733 | ||
3c702e99 LW |
1734 | /* |
1735 | * By first adding the chardev, and then adding the device, | |
1736 | * we get a device node entry in sysfs under | |
1737 | * /sys/bus/gpio/devices/gpiochipN/dev that can be used for | |
1738 | * coldplug of device nodes and other udev business. | |
159f3cd9 GR |
1739 | * We can do this only if gpiolib has been initialized. |
1740 | * Otherwise, defer until later. | |
3c702e99 | 1741 | */ |
159f3cd9 | 1742 | if (gpiolib_initialized) { |
d377f56f LW |
1743 | ret = gpiochip_setup_dev(gdev); |
1744 | if (ret) | |
48057ed1 | 1745 | goto err_remove_irqchip; |
159f3cd9 | 1746 | } |
cedb1881 | 1747 | return 0; |
3bae4811 | 1748 | |
48057ed1 LW |
1749 | err_remove_irqchip: |
1750 | gpiochip_irqchip_remove(chip); | |
1751 | err_remove_irqchip_mask: | |
1752 | gpiochip_irqchip_free_valid_mask(chip); | |
35779890 | 1753 | err_remove_acpi_chip: |
225fce83 | 1754 | acpi_gpiochip_remove(chip); |
35779890 | 1755 | err_remove_of_chip: |
6d86750c | 1756 | gpiochip_free_hogs(chip); |
225fce83 | 1757 | of_gpiochip_remove(chip); |
35779890 | 1758 | err_free_gpiochip_mask: |
2f4133bb | 1759 | gpiochip_remove_pin_ranges(chip); |
726cb3ba | 1760 | gpiochip_free_valid_mask(chip); |
5f3ca732 | 1761 | err_remove_from_list: |
225fce83 | 1762 | spin_lock_irqsave(&gpio_lock, flags); |
ff2b1359 | 1763 | list_del(&gdev->list); |
3bae4811 | 1764 | spin_unlock_irqrestore(&gpio_lock, flags); |
476e2fc5 | 1765 | err_free_label: |
fcf273e5 | 1766 | kfree_const(gdev->label); |
476e2fc5 GR |
1767 | err_free_descs: |
1768 | kfree(gdev->descs); | |
a05a1404 | 1769 | err_free_ida: |
ff2b1359 | 1770 | ida_simple_remove(&gpio_ida, gdev->id); |
a05a1404 | 1771 | err_free_gdev: |
d2876d08 | 1772 | /* failures here can mean systems won't boot... */ |
1777fc97 | 1773 | pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__, |
fdeb8e15 | 1774 | gdev->base, gdev->base + gdev->ngpio - 1, |
d377f56f | 1775 | chip->label ? : "generic", ret); |
fdeb8e15 | 1776 | kfree(gdev); |
d377f56f | 1777 | return ret; |
d2876d08 | 1778 | } |
959bc7b2 | 1779 | EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key); |
d2876d08 | 1780 | |
43c54eca LW |
1781 | /** |
1782 | * gpiochip_get_data() - get per-subdriver data for the chip | |
950d55f5 TR |
1783 | * @chip: GPIO chip |
1784 | * | |
1785 | * Returns: | |
1786 | * The per-subdriver data for the chip. | |
43c54eca LW |
1787 | */ |
1788 | void *gpiochip_get_data(struct gpio_chip *chip) | |
1789 | { | |
1790 | return chip->gpiodev->data; | |
1791 | } | |
1792 | EXPORT_SYMBOL_GPL(gpiochip_get_data); | |
1793 | ||
d2876d08 DB |
1794 | /** |
1795 | * gpiochip_remove() - unregister a gpio_chip | |
1796 | * @chip: the chip to unregister | |
1797 | * | |
1798 | * A gpio_chip with any GPIOs still requested may not be removed. | |
1799 | */ | |
e1db1706 | 1800 | void gpiochip_remove(struct gpio_chip *chip) |
d2876d08 | 1801 | { |
ff2b1359 | 1802 | struct gpio_device *gdev = chip->gpiodev; |
d2876d08 | 1803 | unsigned long flags; |
869233f8 | 1804 | unsigned int i; |
d2876d08 | 1805 | |
ff2b1359 | 1806 | /* FIXME: should the legacy sysfs handling be moved to gpio_device? */ |
afbc4f31 | 1807 | gpiochip_sysfs_unregister(gdev); |
5018ada6 | 1808 | gpiochip_free_hogs(chip); |
bd203bd5 BJZ |
1809 | /* Numb the device, cancelling all outstanding operations */ |
1810 | gdev->chip = NULL; | |
00acc3dc | 1811 | gpiochip_irqchip_remove(chip); |
6072b9dc | 1812 | acpi_gpiochip_remove(chip); |
391c970c | 1813 | of_gpiochip_remove(chip); |
2f4133bb | 1814 | gpiochip_remove_pin_ranges(chip); |
726cb3ba | 1815 | gpiochip_free_valid_mask(chip); |
43c54eca LW |
1816 | /* |
1817 | * We accept no more calls into the driver from this point, so | |
1818 | * NULL the driver data pointer | |
1819 | */ | |
1820 | gdev->data = NULL; | |
391c970c | 1821 | |
6798acaa | 1822 | spin_lock_irqsave(&gpio_lock, flags); |
fdeb8e15 | 1823 | for (i = 0; i < gdev->ngpio; i++) { |
869233f8 AS |
1824 | if (gpiochip_is_requested(chip, i)) |
1825 | break; | |
d2876d08 | 1826 | } |
d2876d08 | 1827 | spin_unlock_irqrestore(&gpio_lock, flags); |
14e85c0e | 1828 | |
ca18a852 | 1829 | if (i != gdev->ngpio) |
fdeb8e15 | 1830 | dev_crit(&gdev->dev, |
58383c78 | 1831 | "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); |
fab28b89 | 1832 | |
ff2b1359 LW |
1833 | /* |
1834 | * The gpiochip side puts its use of the device to rest here: | |
1835 | * if there are no userspace clients, the chardev and device will | |
1836 | * be removed, else it will be dangling until the last user is | |
1837 | * gone. | |
1838 | */ | |
111379dc | 1839 | cdev_device_del(&gdev->chrdev, &gdev->dev); |
ff2b1359 | 1840 | put_device(&gdev->dev); |
d2876d08 DB |
1841 | } |
1842 | EXPORT_SYMBOL_GPL(gpiochip_remove); | |
1843 | ||
594fa265 GL |
1844 | /** |
1845 | * gpiochip_find() - iterator for locating a specific gpio_chip | |
1846 | * @data: data to pass to match function | |
950d55f5 | 1847 | * @match: Callback function to check gpio_chip |
594fa265 GL |
1848 | * |
1849 | * Similar to bus_find_device. It returns a reference to a gpio_chip as | |
1850 | * determined by a user supplied @match callback. The callback should return | |
1851 | * 0 if the device doesn't match and non-zero if it does. If the callback is | |
1852 | * non-zero, this function will return to the caller and not iterate over any | |
1853 | * more gpio_chips. | |
1854 | */ | |
07ce8ec7 | 1855 | struct gpio_chip *gpiochip_find(void *data, |
6e2cf651 | 1856 | int (*match)(struct gpio_chip *chip, |
3d0f7cf0 | 1857 | void *data)) |
594fa265 | 1858 | { |
ff2b1359 | 1859 | struct gpio_device *gdev; |
acf06ff7 | 1860 | struct gpio_chip *chip = NULL; |
594fa265 | 1861 | unsigned long flags; |
594fa265 GL |
1862 | |
1863 | spin_lock_irqsave(&gpio_lock, flags); | |
ff2b1359 | 1864 | list_for_each_entry(gdev, &gpio_devices, list) |
acf06ff7 MY |
1865 | if (gdev->chip && match(gdev->chip, data)) { |
1866 | chip = gdev->chip; | |
594fa265 | 1867 | break; |
acf06ff7 | 1868 | } |
ff2b1359 | 1869 | |
594fa265 GL |
1870 | spin_unlock_irqrestore(&gpio_lock, flags); |
1871 | ||
1872 | return chip; | |
1873 | } | |
8fa0c9bf | 1874 | EXPORT_SYMBOL_GPL(gpiochip_find); |
d2876d08 | 1875 | |
79697ef9 AC |
1876 | static int gpiochip_match_name(struct gpio_chip *chip, void *data) |
1877 | { | |
1878 | const char *name = data; | |
1879 | ||
1880 | return !strcmp(chip->label, name); | |
1881 | } | |
1882 | ||
1883 | static struct gpio_chip *find_chip_by_name(const char *name) | |
1884 | { | |
1885 | return gpiochip_find((void *)name, gpiochip_match_name); | |
1886 | } | |
1887 | ||
14250520 LW |
1888 | #ifdef CONFIG_GPIOLIB_IRQCHIP |
1889 | ||
1890 | /* | |
1891 | * The following is irqchip helper code for gpiochips. | |
1892 | */ | |
1893 | ||
9411e3aa AS |
1894 | static int gpiochip_irqchip_init_hw(struct gpio_chip *gc) |
1895 | { | |
1896 | struct gpio_irq_chip *girq = &gc->irq; | |
1897 | ||
1898 | if (!girq->init_hw) | |
1899 | return 0; | |
1900 | ||
1901 | return girq->init_hw(gc); | |
1902 | } | |
1903 | ||
5fbe5b58 | 1904 | static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc) |
79b804cb | 1905 | { |
5fbe5b58 LW |
1906 | struct gpio_irq_chip *girq = &gc->irq; |
1907 | ||
1908 | if (!girq->init_valid_mask) | |
79b804cb MW |
1909 | return 0; |
1910 | ||
5fbe5b58 LW |
1911 | girq->valid_mask = gpiochip_allocate_mask(gc); |
1912 | if (!girq->valid_mask) | |
79b804cb MW |
1913 | return -ENOMEM; |
1914 | ||
5fbe5b58 LW |
1915 | girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio); |
1916 | ||
79b804cb MW |
1917 | return 0; |
1918 | } | |
1919 | ||
1920 | static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip) | |
1921 | { | |
7bdbd1ec | 1922 | bitmap_free(gpiochip->irq.valid_mask); |
dc7b0387 | 1923 | gpiochip->irq.valid_mask = NULL; |
79b804cb MW |
1924 | } |
1925 | ||
64ff2c8e SB |
1926 | bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip, |
1927 | unsigned int offset) | |
79b804cb | 1928 | { |
726cb3ba SB |
1929 | if (!gpiochip_line_is_valid(gpiochip, offset)) |
1930 | return false; | |
79b804cb | 1931 | /* No mask means all valid */ |
dc7b0387 | 1932 | if (likely(!gpiochip->irq.valid_mask)) |
79b804cb | 1933 | return true; |
dc7b0387 | 1934 | return test_bit(offset, gpiochip->irq.valid_mask); |
79b804cb | 1935 | } |
64ff2c8e | 1936 | EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid); |
79b804cb | 1937 | |
14250520 | 1938 | /** |
d245b3f9 | 1939 | * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip |
4892d3a6 | 1940 | * @gc: the gpiochip to set the irqchip chain to |
14250520 | 1941 | * @parent_irq: the irq number corresponding to the parent IRQ for this |
72780ce5 | 1942 | * cascaded irqchip |
14250520 | 1943 | * @parent_handler: the parent interrupt handler for the accumulated IRQ |
3f97d5fc LW |
1944 | * coming out of the gpiochip. If the interrupt is nested rather than |
1945 | * cascaded, pass NULL in this handler argument | |
14250520 | 1946 | */ |
4892d3a6 | 1947 | static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc, |
6f79309a | 1948 | unsigned int parent_irq, |
d245b3f9 | 1949 | irq_flow_handler_t parent_handler) |
14250520 | 1950 | { |
4892d3a6 LW |
1951 | struct gpio_irq_chip *girq = &gc->irq; |
1952 | struct device *dev = &gc->gpiodev->dev; | |
1953 | ||
1954 | if (!girq->domain) { | |
1955 | chip_err(gc, "called %s before setting up irqchip\n", | |
83141a77 | 1956 | __func__); |
1c8732bb LW |
1957 | return; |
1958 | } | |
1959 | ||
3f97d5fc | 1960 | if (parent_handler) { |
4892d3a6 LW |
1961 | if (gc->can_sleep) { |
1962 | chip_err(gc, | |
b1911710 | 1963 | "you cannot have chained interrupts on a chip that may sleep\n"); |
3f97d5fc LW |
1964 | return; |
1965 | } | |
4892d3a6 LW |
1966 | girq->parents = devm_kcalloc(dev, 1, |
1967 | sizeof(*girq->parents), | |
1968 | GFP_KERNEL); | |
1969 | if (!girq->parents) { | |
1970 | chip_err(gc, "out of memory allocating parent IRQ\n"); | |
1971 | return; | |
1972 | } | |
1973 | girq->parents[0] = parent_irq; | |
1974 | girq->num_parents = 1; | |
3f97d5fc LW |
1975 | /* |
1976 | * The parent irqchip is already using the chip_data for this | |
1977 | * irqchip, so our callbacks simply use the handler_data. | |
1978 | */ | |
f7f87753 | 1979 | irq_set_chained_handler_and_data(parent_irq, parent_handler, |
4892d3a6 | 1980 | gc); |
3f97d5fc | 1981 | } |
14250520 | 1982 | } |
d245b3f9 | 1983 | |
d245b3f9 LW |
1984 | /** |
1985 | * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip | |
1986 | * @gpiochip: the gpiochip to set the irqchip nested handler to | |
1987 | * @irqchip: the irqchip to nest to the gpiochip | |
1988 | * @parent_irq: the irq number corresponding to the parent IRQ for this | |
1989 | * nested irqchip | |
1990 | */ | |
1991 | void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip, | |
1992 | struct irq_chip *irqchip, | |
6f79309a | 1993 | unsigned int parent_irq) |
d245b3f9 | 1994 | { |
3c1f6b2d | 1995 | gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, NULL); |
d245b3f9 LW |
1996 | } |
1997 | EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip); | |
1998 | ||
fdd61a01 LW |
1999 | #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY |
2000 | ||
2001 | /** | |
2002 | * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip | |
2003 | * to a gpiochip | |
2004 | * @gc: the gpiochip to set the irqchip hierarchical handler to | |
2005 | * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt | |
2006 | * will then percolate up to the parent | |
2007 | */ | |
2008 | static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc, | |
2009 | struct irq_chip *irqchip) | |
2010 | { | |
2011 | /* DT will deal with mapping each IRQ as we go along */ | |
2012 | if (is_of_node(gc->irq.fwnode)) | |
2013 | return; | |
2014 | ||
2015 | /* | |
2016 | * This is for legacy and boardfile "irqchip" fwnodes: allocate | |
2017 | * irqs upfront instead of dynamically since we don't have the | |
2018 | * dynamic type of allocation that hardware description languages | |
2019 | * provide. Once all GPIO drivers using board files are gone from | |
2020 | * the kernel we can delete this code, but for a transitional period | |
2021 | * it is necessary to keep this around. | |
2022 | */ | |
2023 | if (is_fwnode_irqchip(gc->irq.fwnode)) { | |
2024 | int i; | |
2025 | int ret; | |
2026 | ||
2027 | for (i = 0; i < gc->ngpio; i++) { | |
2028 | struct irq_fwspec fwspec; | |
2029 | unsigned int parent_hwirq; | |
2030 | unsigned int parent_type; | |
2031 | struct gpio_irq_chip *girq = &gc->irq; | |
2032 | ||
2033 | /* | |
2034 | * We call the child to parent translation function | |
2035 | * only to check if the child IRQ is valid or not. | |
2036 | * Just pick the rising edge type here as that is what | |
2037 | * we likely need to support. | |
2038 | */ | |
2039 | ret = girq->child_to_parent_hwirq(gc, i, | |
2040 | IRQ_TYPE_EDGE_RISING, | |
2041 | &parent_hwirq, | |
2042 | &parent_type); | |
2043 | if (ret) { | |
2044 | chip_err(gc, "skip set-up on hwirq %d\n", | |
2045 | i); | |
2046 | continue; | |
2047 | } | |
2048 | ||
2049 | fwspec.fwnode = gc->irq.fwnode; | |
2050 | /* This is the hwirq for the GPIO line side of things */ | |
2051 | fwspec.param[0] = girq->child_offset_to_irq(gc, i); | |
2052 | /* Just pick something */ | |
2053 | fwspec.param[1] = IRQ_TYPE_EDGE_RISING; | |
2054 | fwspec.param_count = 2; | |
2055 | ret = __irq_domain_alloc_irqs(gc->irq.domain, | |
2056 | /* just pick something */ | |
2057 | -1, | |
2058 | 1, | |
2059 | NUMA_NO_NODE, | |
2060 | &fwspec, | |
2061 | false, | |
2062 | NULL); | |
2063 | if (ret < 0) { | |
2064 | chip_err(gc, | |
2065 | "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n", | |
2066 | i, parent_hwirq, | |
2067 | ret); | |
2068 | } | |
2069 | } | |
2070 | } | |
2071 | ||
2072 | chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__); | |
2073 | ||
2074 | return; | |
2075 | } | |
2076 | ||
2077 | static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d, | |
2078 | struct irq_fwspec *fwspec, | |
2079 | unsigned long *hwirq, | |
2080 | unsigned int *type) | |
2081 | { | |
2082 | /* We support standard DT translation */ | |
2083 | if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) { | |
2084 | return irq_domain_translate_twocell(d, fwspec, hwirq, type); | |
2085 | } | |
2086 | ||
2087 | /* This is for board files and others not using DT */ | |
2088 | if (is_fwnode_irqchip(fwspec->fwnode)) { | |
2089 | int ret; | |
2090 | ||
2091 | ret = irq_domain_translate_twocell(d, fwspec, hwirq, type); | |
2092 | if (ret) | |
2093 | return ret; | |
2094 | WARN_ON(*type == IRQ_TYPE_NONE); | |
2095 | return 0; | |
2096 | } | |
2097 | return -EINVAL; | |
2098 | } | |
2099 | ||
2100 | static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d, | |
2101 | unsigned int irq, | |
2102 | unsigned int nr_irqs, | |
2103 | void *data) | |
2104 | { | |
2105 | struct gpio_chip *gc = d->host_data; | |
2106 | irq_hw_number_t hwirq; | |
2107 | unsigned int type = IRQ_TYPE_NONE; | |
2108 | struct irq_fwspec *fwspec = data; | |
24258761 | 2109 | void *parent_arg; |
fdd61a01 LW |
2110 | unsigned int parent_hwirq; |
2111 | unsigned int parent_type; | |
2112 | struct gpio_irq_chip *girq = &gc->irq; | |
2113 | int ret; | |
2114 | ||
2115 | /* | |
2116 | * The nr_irqs parameter is always one except for PCI multi-MSI | |
2117 | * so this should not happen. | |
2118 | */ | |
2119 | WARN_ON(nr_irqs != 1); | |
2120 | ||
2121 | ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type); | |
2122 | if (ret) | |
2123 | return ret; | |
2124 | ||
366950ee | 2125 | chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq); |
fdd61a01 LW |
2126 | |
2127 | ret = girq->child_to_parent_hwirq(gc, hwirq, type, | |
2128 | &parent_hwirq, &parent_type); | |
2129 | if (ret) { | |
2130 | chip_err(gc, "can't look up hwirq %lu\n", hwirq); | |
2131 | return ret; | |
2132 | } | |
366950ee | 2133 | chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq); |
fdd61a01 LW |
2134 | |
2135 | /* | |
2136 | * We set handle_bad_irq because the .set_type() should | |
2137 | * always be invoked and set the right type of handler. | |
2138 | */ | |
2139 | irq_domain_set_info(d, | |
2140 | irq, | |
2141 | hwirq, | |
2142 | gc->irq.chip, | |
2143 | gc, | |
2144 | girq->handler, | |
2145 | NULL, NULL); | |
2146 | irq_set_probe(irq); | |
2147 | ||
fdd61a01 | 2148 | /* This parent only handles asserted level IRQs */ |
24258761 KH |
2149 | parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type); |
2150 | if (!parent_arg) | |
2151 | return -ENOMEM; | |
2152 | ||
366950ee | 2153 | chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n", |
fdd61a01 | 2154 | irq, parent_hwirq); |
c34f6dc8 | 2155 | irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key); |
24258761 | 2156 | ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg); |
880b7cf2 KH |
2157 | /* |
2158 | * If the parent irqdomain is msi, the interrupts have already | |
2159 | * been allocated, so the EEXIST is good. | |
2160 | */ | |
2161 | if (irq_domain_is_msi(d->parent) && (ret == -EEXIST)) | |
2162 | ret = 0; | |
fdd61a01 LW |
2163 | if (ret) |
2164 | chip_err(gc, | |
2165 | "failed to allocate parent hwirq %d for hwirq %lu\n", | |
2166 | parent_hwirq, hwirq); | |
2167 | ||
24258761 | 2168 | kfree(parent_arg); |
fdd61a01 LW |
2169 | return ret; |
2170 | } | |
2171 | ||
2172 | static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *chip, | |
2173 | unsigned int offset) | |
2174 | { | |
2175 | return offset; | |
2176 | } | |
2177 | ||
2178 | static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops) | |
2179 | { | |
2180 | ops->activate = gpiochip_irq_domain_activate; | |
2181 | ops->deactivate = gpiochip_irq_domain_deactivate; | |
2182 | ops->alloc = gpiochip_hierarchy_irq_domain_alloc; | |
2183 | ops->free = irq_domain_free_irqs_common; | |
2184 | ||
2185 | /* | |
2186 | * We only allow overriding the translate() function for | |
2187 | * hierarchical chips, and this should only be done if the user | |
2188 | * really need something other than 1:1 translation. | |
2189 | */ | |
2190 | if (!ops->translate) | |
2191 | ops->translate = gpiochip_hierarchy_irq_domain_translate; | |
2192 | } | |
2193 | ||
2194 | static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc) | |
2195 | { | |
2196 | if (!gc->irq.child_to_parent_hwirq || | |
2197 | !gc->irq.fwnode) { | |
2198 | chip_err(gc, "missing irqdomain vital data\n"); | |
2199 | return -EINVAL; | |
2200 | } | |
2201 | ||
2202 | if (!gc->irq.child_offset_to_irq) | |
2203 | gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop; | |
2204 | ||
24258761 KH |
2205 | if (!gc->irq.populate_parent_alloc_arg) |
2206 | gc->irq.populate_parent_alloc_arg = | |
fdd61a01 LW |
2207 | gpiochip_populate_parent_fwspec_twocell; |
2208 | ||
2209 | gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops); | |
2210 | ||
2211 | gc->irq.domain = irq_domain_create_hierarchy( | |
2212 | gc->irq.parent_domain, | |
2213 | 0, | |
2214 | gc->ngpio, | |
2215 | gc->irq.fwnode, | |
2216 | &gc->irq.child_irq_domain_ops, | |
2217 | gc); | |
2218 | ||
2219 | if (!gc->irq.domain) | |
2220 | return -ENOMEM; | |
2221 | ||
2222 | gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip); | |
2223 | ||
2224 | return 0; | |
2225 | } | |
2226 | ||
2227 | static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc) | |
2228 | { | |
2229 | return !!gc->irq.parent_domain; | |
2230 | } | |
2231 | ||
24258761 | 2232 | void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *chip, |
fdd61a01 LW |
2233 | unsigned int parent_hwirq, |
2234 | unsigned int parent_type) | |
2235 | { | |
24258761 KH |
2236 | struct irq_fwspec *fwspec; |
2237 | ||
2238 | fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL); | |
2239 | if (!fwspec) | |
2240 | return NULL; | |
2241 | ||
2242 | fwspec->fwnode = chip->irq.parent_domain->fwnode; | |
fdd61a01 LW |
2243 | fwspec->param_count = 2; |
2244 | fwspec->param[0] = parent_hwirq; | |
2245 | fwspec->param[1] = parent_type; | |
24258761 KH |
2246 | |
2247 | return fwspec; | |
fdd61a01 LW |
2248 | } |
2249 | EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell); | |
2250 | ||
24258761 | 2251 | void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *chip, |
fdd61a01 LW |
2252 | unsigned int parent_hwirq, |
2253 | unsigned int parent_type) | |
2254 | { | |
24258761 KH |
2255 | struct irq_fwspec *fwspec; |
2256 | ||
2257 | fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL); | |
2258 | if (!fwspec) | |
2259 | return NULL; | |
2260 | ||
2261 | fwspec->fwnode = chip->irq.parent_domain->fwnode; | |
fdd61a01 LW |
2262 | fwspec->param_count = 4; |
2263 | fwspec->param[0] = 0; | |
2264 | fwspec->param[1] = parent_hwirq; | |
2265 | fwspec->param[2] = 0; | |
2266 | fwspec->param[3] = parent_type; | |
24258761 KH |
2267 | |
2268 | return fwspec; | |
fdd61a01 LW |
2269 | } |
2270 | EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell); | |
2271 | ||
2272 | #else | |
2273 | ||
2274 | static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc) | |
2275 | { | |
2276 | return -EINVAL; | |
2277 | } | |
2278 | ||
2279 | static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc) | |
2280 | { | |
2281 | return false; | |
2282 | } | |
2283 | ||
2284 | #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */ | |
2285 | ||
14250520 LW |
2286 | /** |
2287 | * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip | |
2288 | * @d: the irqdomain used by this irqchip | |
2289 | * @irq: the global irq number used by this GPIO irqchip irq | |
2290 | * @hwirq: the local IRQ/GPIO line offset on this gpiochip | |
2291 | * | |
2292 | * This function will set up the mapping for a certain IRQ line on a | |
2293 | * gpiochip by assigning the gpiochip as chip data, and using the irqchip | |
2294 | * stored inside the gpiochip. | |
2295 | */ | |
1b95b4eb TR |
2296 | int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, |
2297 | irq_hw_number_t hwirq) | |
14250520 LW |
2298 | { |
2299 | struct gpio_chip *chip = d->host_data; | |
d377f56f | 2300 | int ret = 0; |
14250520 | 2301 | |
dc749a09 GS |
2302 | if (!gpiochip_irqchip_irq_valid(chip, hwirq)) |
2303 | return -ENXIO; | |
2304 | ||
14250520 | 2305 | irq_set_chip_data(irq, chip); |
a0a8bcf4 GS |
2306 | /* |
2307 | * This lock class tells lockdep that GPIO irqs are in a different | |
2308 | * category than their parents, so it won't report false recursion. | |
2309 | */ | |
39c3fd58 | 2310 | irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key); |
c7a0aa59 | 2311 | irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler); |
d245b3f9 | 2312 | /* Chips that use nested thread handlers have them marked */ |
60ed54ca | 2313 | if (chip->irq.threaded) |
1c8732bb | 2314 | irq_set_nested_thread(irq, 1); |
14250520 | 2315 | irq_set_noprobe(irq); |
23393d49 | 2316 | |
e0d89728 | 2317 | if (chip->irq.num_parents == 1) |
d377f56f | 2318 | ret = irq_set_parent(irq, chip->irq.parents[0]); |
e0d89728 | 2319 | else if (chip->irq.map) |
d377f56f | 2320 | ret = irq_set_parent(irq, chip->irq.map[hwirq]); |
e0d89728 | 2321 | |
d377f56f LW |
2322 | if (ret < 0) |
2323 | return ret; | |
e0d89728 | 2324 | |
1333b90f LW |
2325 | /* |
2326 | * No set-up of the hardware will happen if IRQ_TYPE_NONE | |
2327 | * is passed as default type. | |
2328 | */ | |
3634eeb0 TR |
2329 | if (chip->irq.default_type != IRQ_TYPE_NONE) |
2330 | irq_set_irq_type(irq, chip->irq.default_type); | |
14250520 LW |
2331 | |
2332 | return 0; | |
2333 | } | |
1b95b4eb | 2334 | EXPORT_SYMBOL_GPL(gpiochip_irq_map); |
14250520 | 2335 | |
1b95b4eb | 2336 | void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq) |
c3626fde | 2337 | { |
1c8732bb LW |
2338 | struct gpio_chip *chip = d->host_data; |
2339 | ||
60ed54ca | 2340 | if (chip->irq.threaded) |
1c8732bb | 2341 | irq_set_nested_thread(irq, 0); |
c3626fde LW |
2342 | irq_set_chip_and_handler(irq, NULL, NULL); |
2343 | irq_set_chip_data(irq, NULL); | |
2344 | } | |
1b95b4eb | 2345 | EXPORT_SYMBOL_GPL(gpiochip_irq_unmap); |
c3626fde | 2346 | |
14250520 LW |
2347 | static const struct irq_domain_ops gpiochip_domain_ops = { |
2348 | .map = gpiochip_irq_map, | |
c3626fde | 2349 | .unmap = gpiochip_irq_unmap, |
14250520 LW |
2350 | /* Virtually all GPIO irqchips are twocell:ed */ |
2351 | .xlate = irq_domain_xlate_twocell, | |
2352 | }; | |
2353 | ||
fdd61a01 LW |
2354 | /* |
2355 | * TODO: move these activate/deactivate in under the hierarchicial | |
2356 | * irqchip implementation as static once SPMI and SSBI (all external | |
2357 | * users) are phased over. | |
2358 | */ | |
ef74f70e BM |
2359 | /** |
2360 | * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ | |
2361 | * @domain: The IRQ domain used by this IRQ chip | |
2362 | * @data: Outermost irq_data associated with the IRQ | |
2363 | * @reserve: If set, only reserve an interrupt vector instead of assigning one | |
2364 | * | |
2365 | * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be | |
2366 | * used as the activate function for the &struct irq_domain_ops. The host_data | |
2367 | * for the IRQ domain must be the &struct gpio_chip. | |
2368 | */ | |
2369 | int gpiochip_irq_domain_activate(struct irq_domain *domain, | |
2370 | struct irq_data *data, bool reserve) | |
2371 | { | |
2372 | struct gpio_chip *chip = domain->host_data; | |
2373 | ||
2374 | return gpiochip_lock_as_irq(chip, data->hwirq); | |
2375 | } | |
2376 | EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate); | |
2377 | ||
2378 | /** | |
2379 | * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ | |
2380 | * @domain: The IRQ domain used by this IRQ chip | |
2381 | * @data: Outermost irq_data associated with the IRQ | |
2382 | * | |
2383 | * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to | |
2384 | * be used as the deactivate function for the &struct irq_domain_ops. The | |
2385 | * host_data for the IRQ domain must be the &struct gpio_chip. | |
2386 | */ | |
2387 | void gpiochip_irq_domain_deactivate(struct irq_domain *domain, | |
2388 | struct irq_data *data) | |
2389 | { | |
2390 | struct gpio_chip *chip = domain->host_data; | |
2391 | ||
2392 | return gpiochip_unlock_as_irq(chip, data->hwirq); | |
2393 | } | |
2394 | EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate); | |
2395 | ||
4e6b8238 | 2396 | static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset) |
14250520 | 2397 | { |
fdd61a01 LW |
2398 | struct irq_domain *domain = chip->irq.domain; |
2399 | ||
4e6b8238 HV |
2400 | if (!gpiochip_irqchip_irq_valid(chip, offset)) |
2401 | return -ENXIO; | |
5b76e79c | 2402 | |
fdd61a01 LW |
2403 | #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY |
2404 | if (irq_domain_is_hierarchy(domain)) { | |
2405 | struct irq_fwspec spec; | |
2406 | ||
2407 | spec.fwnode = domain->fwnode; | |
2408 | spec.param_count = 2; | |
2409 | spec.param[0] = chip->irq.child_offset_to_irq(chip, offset); | |
2410 | spec.param[1] = IRQ_TYPE_NONE; | |
2411 | ||
2412 | return irq_create_fwspec_mapping(&spec); | |
2413 | } | |
2414 | #endif | |
2415 | ||
2416 | return irq_create_mapping(domain, offset); | |
14250520 LW |
2417 | } |
2418 | ||
14250520 LW |
2419 | static int gpiochip_irq_reqres(struct irq_data *d) |
2420 | { | |
2421 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); | |
5b76e79c | 2422 | |
4e6b8238 | 2423 | return gpiochip_reqres_irq(chip, d->hwirq); |
14250520 LW |
2424 | } |
2425 | ||
2426 | static void gpiochip_irq_relres(struct irq_data *d) | |
2427 | { | |
2428 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); | |
2429 | ||
4e6b8238 | 2430 | gpiochip_relres_irq(chip, d->hwirq); |
14250520 LW |
2431 | } |
2432 | ||
461c1a7d | 2433 | static void gpiochip_irq_enable(struct irq_data *d) |
14250520 | 2434 | { |
461c1a7d | 2435 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); |
e0d89728 | 2436 | |
461c1a7d HV |
2437 | gpiochip_enable_irq(chip, d->hwirq); |
2438 | if (chip->irq.irq_enable) | |
2439 | chip->irq.irq_enable(d); | |
2440 | else | |
2441 | chip->irq.chip->irq_unmask(d); | |
2442 | } | |
2443 | ||
2444 | static void gpiochip_irq_disable(struct irq_data *d) | |
2445 | { | |
2446 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); | |
2447 | ||
2448 | if (chip->irq.irq_disable) | |
2449 | chip->irq.irq_disable(d); | |
2450 | else | |
2451 | chip->irq.chip->irq_mask(d); | |
2452 | gpiochip_disable_irq(chip, d->hwirq); | |
2453 | } | |
2454 | ||
ca620f2d HV |
2455 | static void gpiochip_set_irq_hooks(struct gpio_chip *gpiochip) |
2456 | { | |
2457 | struct irq_chip *irqchip = gpiochip->irq.chip; | |
2458 | ||
2459 | if (!irqchip->irq_request_resources && | |
2460 | !irqchip->irq_release_resources) { | |
2461 | irqchip->irq_request_resources = gpiochip_irq_reqres; | |
2462 | irqchip->irq_release_resources = gpiochip_irq_relres; | |
2463 | } | |
461c1a7d HV |
2464 | if (WARN_ON(gpiochip->irq.irq_enable)) |
2465 | return; | |
171948ea HV |
2466 | /* Check if the irqchip already has this hook... */ |
2467 | if (irqchip->irq_enable == gpiochip_irq_enable) { | |
2468 | /* | |
2469 | * ...and if so, give a gentle warning that this is bad | |
2470 | * practice. | |
2471 | */ | |
2472 | chip_info(gpiochip, | |
2473 | "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n"); | |
2474 | return; | |
2475 | } | |
461c1a7d HV |
2476 | gpiochip->irq.irq_enable = irqchip->irq_enable; |
2477 | gpiochip->irq.irq_disable = irqchip->irq_disable; | |
2478 | irqchip->irq_enable = gpiochip_irq_enable; | |
2479 | irqchip->irq_disable = gpiochip_irq_disable; | |
14250520 LW |
2480 | } |
2481 | ||
e0d89728 TR |
2482 | /** |
2483 | * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip | |
2484 | * @gpiochip: the GPIO chip to add the IRQ chip to | |
39c3fd58 AL |
2485 | * @lock_key: lockdep class for IRQ lock |
2486 | * @request_key: lockdep class for IRQ request | |
e0d89728 | 2487 | */ |
959bc7b2 | 2488 | static int gpiochip_add_irqchip(struct gpio_chip *gpiochip, |
39c3fd58 AL |
2489 | struct lock_class_key *lock_key, |
2490 | struct lock_class_key *request_key) | |
e0d89728 TR |
2491 | { |
2492 | struct irq_chip *irqchip = gpiochip->irq.chip; | |
fdd61a01 | 2493 | const struct irq_domain_ops *ops = NULL; |
e0d89728 TR |
2494 | struct device_node *np; |
2495 | unsigned int type; | |
2496 | unsigned int i; | |
2497 | ||
2498 | if (!irqchip) | |
2499 | return 0; | |
2500 | ||
2501 | if (gpiochip->irq.parent_handler && gpiochip->can_sleep) { | |
b1911710 | 2502 | chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n"); |
e0d89728 TR |
2503 | return -EINVAL; |
2504 | } | |
2505 | ||
2506 | np = gpiochip->gpiodev->dev.of_node; | |
2507 | type = gpiochip->irq.default_type; | |
2508 | ||
2509 | /* | |
2510 | * Specifying a default trigger is a terrible idea if DT or ACPI is | |
2511 | * used to configure the interrupts, as you may end up with | |
2512 | * conflicting triggers. Tell the user, and reset to NONE. | |
2513 | */ | |
2514 | if (WARN(np && type != IRQ_TYPE_NONE, | |
2515 | "%s: Ignoring %u default trigger\n", np->full_name, type)) | |
2516 | type = IRQ_TYPE_NONE; | |
2517 | ||
2518 | if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) { | |
2519 | acpi_handle_warn(ACPI_HANDLE(gpiochip->parent), | |
2520 | "Ignoring %u default trigger\n", type); | |
2521 | type = IRQ_TYPE_NONE; | |
2522 | } | |
2523 | ||
2524 | gpiochip->to_irq = gpiochip_to_irq; | |
2525 | gpiochip->irq.default_type = type; | |
959bc7b2 | 2526 | gpiochip->irq.lock_key = lock_key; |
39c3fd58 | 2527 | gpiochip->irq.request_key = request_key; |
e0d89728 | 2528 | |
fdd61a01 LW |
2529 | /* If a parent irqdomain is provided, let's build a hierarchy */ |
2530 | if (gpiochip_hierarchy_is_hierarchical(gpiochip)) { | |
2531 | int ret = gpiochip_hierarchy_add_domain(gpiochip); | |
2532 | if (ret) | |
2533 | return ret; | |
2534 | } else { | |
2535 | /* Some drivers provide custom irqdomain ops */ | |
2536 | if (gpiochip->irq.domain_ops) | |
2537 | ops = gpiochip->irq.domain_ops; | |
2538 | ||
2539 | if (!ops) | |
2540 | ops = &gpiochip_domain_ops; | |
2541 | gpiochip->irq.domain = irq_domain_add_simple(np, | |
2542 | gpiochip->ngpio, | |
2543 | gpiochip->irq.first, | |
2544 | ops, gpiochip); | |
2545 | if (!gpiochip->irq.domain) | |
2546 | return -EINVAL; | |
2547 | } | |
e0d89728 | 2548 | |
e0d89728 TR |
2549 | if (gpiochip->irq.parent_handler) { |
2550 | void *data = gpiochip->irq.parent_handler_data ?: gpiochip; | |
2551 | ||
2552 | for (i = 0; i < gpiochip->irq.num_parents; i++) { | |
2553 | /* | |
2554 | * The parent IRQ chip is already using the chip_data | |
2555 | * for this IRQ chip, so our callbacks simply use the | |
2556 | * handler_data. | |
2557 | */ | |
2558 | irq_set_chained_handler_and_data(gpiochip->irq.parents[i], | |
2559 | gpiochip->irq.parent_handler, | |
2560 | data); | |
2561 | } | |
e0d89728 TR |
2562 | } |
2563 | ||
ca620f2d HV |
2564 | gpiochip_set_irq_hooks(gpiochip); |
2565 | ||
e0d89728 TR |
2566 | acpi_gpiochip_request_interrupts(gpiochip); |
2567 | ||
2568 | return 0; | |
2569 | } | |
2570 | ||
14250520 LW |
2571 | /** |
2572 | * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip | |
2573 | * @gpiochip: the gpiochip to remove the irqchip from | |
2574 | * | |
2575 | * This is called only from gpiochip_remove() | |
2576 | */ | |
2577 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) | |
2578 | { | |
ca620f2d | 2579 | struct irq_chip *irqchip = gpiochip->irq.chip; |
39e5f096 | 2580 | unsigned int offset; |
c3626fde | 2581 | |
afa82fab MW |
2582 | acpi_gpiochip_free_interrupts(gpiochip); |
2583 | ||
ca620f2d | 2584 | if (irqchip && gpiochip->irq.parent_handler) { |
39e5f096 TR |
2585 | struct gpio_irq_chip *irq = &gpiochip->irq; |
2586 | unsigned int i; | |
2587 | ||
2588 | for (i = 0; i < irq->num_parents; i++) | |
2589 | irq_set_chained_handler_and_data(irq->parents[i], | |
2590 | NULL, NULL); | |
25e4fe92 DES |
2591 | } |
2592 | ||
c3626fde | 2593 | /* Remove all IRQ mappings and delete the domain */ |
f0fbe7bc | 2594 | if (gpiochip->irq.domain) { |
39e5f096 TR |
2595 | unsigned int irq; |
2596 | ||
79b804cb MW |
2597 | for (offset = 0; offset < gpiochip->ngpio; offset++) { |
2598 | if (!gpiochip_irqchip_irq_valid(gpiochip, offset)) | |
2599 | continue; | |
f0fbe7bc TR |
2600 | |
2601 | irq = irq_find_mapping(gpiochip->irq.domain, offset); | |
2602 | irq_dispose_mapping(irq); | |
79b804cb | 2603 | } |
f0fbe7bc TR |
2604 | |
2605 | irq_domain_remove(gpiochip->irq.domain); | |
c3626fde | 2606 | } |
14250520 | 2607 | |
461c1a7d HV |
2608 | if (irqchip) { |
2609 | if (irqchip->irq_request_resources == gpiochip_irq_reqres) { | |
2610 | irqchip->irq_request_resources = NULL; | |
2611 | irqchip->irq_release_resources = NULL; | |
2612 | } | |
2613 | if (irqchip->irq_enable == gpiochip_irq_enable) { | |
2614 | irqchip->irq_enable = gpiochip->irq.irq_enable; | |
2615 | irqchip->irq_disable = gpiochip->irq.irq_disable; | |
2616 | } | |
14250520 | 2617 | } |
461c1a7d HV |
2618 | gpiochip->irq.irq_enable = NULL; |
2619 | gpiochip->irq.irq_disable = NULL; | |
ca620f2d | 2620 | gpiochip->irq.chip = NULL; |
79b804cb MW |
2621 | |
2622 | gpiochip_irqchip_free_valid_mask(gpiochip); | |
14250520 LW |
2623 | } |
2624 | ||
2625 | /** | |
739e6f59 | 2626 | * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip |
14250520 LW |
2627 | * @gpiochip: the gpiochip to add the irqchip to |
2628 | * @irqchip: the irqchip to add to the gpiochip | |
2629 | * @first_irq: if not dynamically assigned, the base (first) IRQ to | |
2630 | * allocate gpiochip irqs from | |
2631 | * @handler: the irq handler to use (often a predefined irq core function) | |
1333b90f LW |
2632 | * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE |
2633 | * to have the core avoid setting up any default type in the hardware. | |
60ed54ca | 2634 | * @threaded: whether this irqchip uses a nested thread handler |
39c3fd58 AL |
2635 | * @lock_key: lockdep class for IRQ lock |
2636 | * @request_key: lockdep class for IRQ request | |
14250520 LW |
2637 | * |
2638 | * This function closely associates a certain irqchip with a certain | |
2639 | * gpiochip, providing an irq domain to translate the local IRQs to | |
2640 | * global irqs in the gpiolib core, and making sure that the gpiochip | |
2641 | * is passed as chip data to all related functions. Driver callbacks | |
09dd5f9e | 2642 | * need to use gpiochip_get_data() to get their local state containers back |
14250520 LW |
2643 | * from the gpiochip passed as chip data. An irqdomain will be stored |
2644 | * in the gpiochip that shall be used by the driver to handle IRQ number | |
2645 | * translation. The gpiochip will need to be initialized and registered | |
2646 | * before calling this function. | |
2647 | * | |
c3626fde LW |
2648 | * This function will handle two cell:ed simple IRQs and assumes all |
2649 | * the pins on the gpiochip can generate a unique IRQ. Everything else | |
14250520 LW |
2650 | * need to be open coded. |
2651 | */ | |
739e6f59 LW |
2652 | int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip, |
2653 | struct irq_chip *irqchip, | |
2654 | unsigned int first_irq, | |
2655 | irq_flow_handler_t handler, | |
2656 | unsigned int type, | |
60ed54ca | 2657 | bool threaded, |
39c3fd58 AL |
2658 | struct lock_class_key *lock_key, |
2659 | struct lock_class_key *request_key) | |
14250520 LW |
2660 | { |
2661 | struct device_node *of_node; | |
14250520 LW |
2662 | |
2663 | if (!gpiochip || !irqchip) | |
2664 | return -EINVAL; | |
2665 | ||
58383c78 | 2666 | if (!gpiochip->parent) { |
14250520 LW |
2667 | pr_err("missing gpiochip .dev parent pointer\n"); |
2668 | return -EINVAL; | |
2669 | } | |
60ed54ca | 2670 | gpiochip->irq.threaded = threaded; |
58383c78 | 2671 | of_node = gpiochip->parent->of_node; |
14250520 LW |
2672 | #ifdef CONFIG_OF_GPIO |
2673 | /* | |
20a8a968 | 2674 | * If the gpiochip has an assigned OF node this takes precedence |
c88402c2 BJZ |
2675 | * FIXME: get rid of this and use gpiochip->parent->of_node |
2676 | * everywhere | |
14250520 LW |
2677 | */ |
2678 | if (gpiochip->of_node) | |
2679 | of_node = gpiochip->of_node; | |
2680 | #endif | |
332e99d5 | 2681 | /* |
0a1e0053 | 2682 | * Specifying a default trigger is a terrible idea if DT or ACPI is |
332e99d5 MZ |
2683 | * used to configure the interrupts, as you may end-up with |
2684 | * conflicting triggers. Tell the user, and reset to NONE. | |
2685 | */ | |
2686 | if (WARN(of_node && type != IRQ_TYPE_NONE, | |
7eb6ce2f | 2687 | "%pOF: Ignoring %d default trigger\n", of_node, type)) |
332e99d5 | 2688 | type = IRQ_TYPE_NONE; |
0a1e0053 MW |
2689 | if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) { |
2690 | acpi_handle_warn(ACPI_HANDLE(gpiochip->parent), | |
2691 | "Ignoring %d default trigger\n", type); | |
2692 | type = IRQ_TYPE_NONE; | |
2693 | } | |
332e99d5 | 2694 | |
da80ff81 | 2695 | gpiochip->irq.chip = irqchip; |
c7a0aa59 | 2696 | gpiochip->irq.handler = handler; |
3634eeb0 | 2697 | gpiochip->irq.default_type = type; |
14250520 | 2698 | gpiochip->to_irq = gpiochip_to_irq; |
ca9df053 | 2699 | gpiochip->irq.lock_key = lock_key; |
39c3fd58 | 2700 | gpiochip->irq.request_key = request_key; |
f0fbe7bc | 2701 | gpiochip->irq.domain = irq_domain_add_simple(of_node, |
14250520 LW |
2702 | gpiochip->ngpio, first_irq, |
2703 | &gpiochip_domain_ops, gpiochip); | |
f0fbe7bc | 2704 | if (!gpiochip->irq.domain) { |
da80ff81 | 2705 | gpiochip->irq.chip = NULL; |
14250520 LW |
2706 | return -EINVAL; |
2707 | } | |
8b67a1f0 | 2708 | |
ca620f2d | 2709 | gpiochip_set_irq_hooks(gpiochip); |
14250520 | 2710 | |
afa82fab MW |
2711 | acpi_gpiochip_request_interrupts(gpiochip); |
2712 | ||
14250520 LW |
2713 | return 0; |
2714 | } | |
739e6f59 | 2715 | EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key); |
14250520 LW |
2716 | |
2717 | #else /* CONFIG_GPIOLIB_IRQCHIP */ | |
2718 | ||
959bc7b2 | 2719 | static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip, |
39c3fd58 AL |
2720 | struct lock_class_key *lock_key, |
2721 | struct lock_class_key *request_key) | |
e0d89728 TR |
2722 | { |
2723 | return 0; | |
2724 | } | |
14250520 | 2725 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {} |
9411e3aa AS |
2726 | |
2727 | static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip) | |
2728 | { | |
2729 | return 0; | |
2730 | } | |
2731 | ||
79b804cb MW |
2732 | static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip) |
2733 | { | |
2734 | return 0; | |
2735 | } | |
2736 | static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip) | |
2737 | { } | |
14250520 LW |
2738 | |
2739 | #endif /* CONFIG_GPIOLIB_IRQCHIP */ | |
2740 | ||
c771c2f4 JG |
2741 | /** |
2742 | * gpiochip_generic_request() - request the gpio function for a pin | |
2743 | * @chip: the gpiochip owning the GPIO | |
2744 | * @offset: the offset of the GPIO to request for GPIO function | |
2745 | */ | |
2746 | int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset) | |
2747 | { | |
89ad556b TR |
2748 | #ifdef CONFIG_PINCTRL |
2749 | if (list_empty(&chip->gpiodev->pin_ranges)) | |
2750 | return 0; | |
2751 | #endif | |
2ab73c6d | 2752 | |
89ad556b | 2753 | return pinctrl_gpio_request(chip->gpiodev->base + offset); |
c771c2f4 JG |
2754 | } |
2755 | EXPORT_SYMBOL_GPL(gpiochip_generic_request); | |
2756 | ||
2757 | /** | |
2758 | * gpiochip_generic_free() - free the gpio function from a pin | |
2759 | * @chip: the gpiochip to request the gpio function for | |
2760 | * @offset: the offset of the GPIO to free from GPIO function | |
2761 | */ | |
2762 | void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset) | |
2763 | { | |
a9a1d2a7 | 2764 | pinctrl_gpio_free(chip->gpiodev->base + offset); |
c771c2f4 JG |
2765 | } |
2766 | EXPORT_SYMBOL_GPL(gpiochip_generic_free); | |
2767 | ||
2956b5d9 MW |
2768 | /** |
2769 | * gpiochip_generic_config() - apply configuration for a pin | |
2770 | * @chip: the gpiochip owning the GPIO | |
2771 | * @offset: the offset of the GPIO to apply the configuration | |
2772 | * @config: the configuration to be applied | |
2773 | */ | |
2774 | int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset, | |
2775 | unsigned long config) | |
2776 | { | |
2777 | return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config); | |
2778 | } | |
2779 | EXPORT_SYMBOL_GPL(gpiochip_generic_config); | |
2780 | ||
f23f1516 | 2781 | #ifdef CONFIG_PINCTRL |
165adc9c | 2782 | |
586a87e6 CR |
2783 | /** |
2784 | * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping | |
2785 | * @chip: the gpiochip to add the range for | |
d32651f6 | 2786 | * @pctldev: the pin controller to map to |
586a87e6 CR |
2787 | * @gpio_offset: the start offset in the current gpio_chip number space |
2788 | * @pin_group: name of the pin group inside the pin controller | |
973c1714 CL |
2789 | * |
2790 | * Calling this function directly from a DeviceTree-supported | |
2791 | * pinctrl driver is DEPRECATED. Please see Section 2.1 of | |
2792 | * Documentation/devicetree/bindings/gpio/gpio.txt on how to | |
2793 | * bind pinctrl and gpio drivers via the "gpio-ranges" property. | |
586a87e6 CR |
2794 | */ |
2795 | int gpiochip_add_pingroup_range(struct gpio_chip *chip, | |
2796 | struct pinctrl_dev *pctldev, | |
2797 | unsigned int gpio_offset, const char *pin_group) | |
2798 | { | |
2799 | struct gpio_pin_range *pin_range; | |
fdeb8e15 | 2800 | struct gpio_device *gdev = chip->gpiodev; |
586a87e6 CR |
2801 | int ret; |
2802 | ||
2803 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); | |
2804 | if (!pin_range) { | |
1a2a99c6 | 2805 | chip_err(chip, "failed to allocate pin ranges\n"); |
586a87e6 CR |
2806 | return -ENOMEM; |
2807 | } | |
2808 | ||
2809 | /* Use local offset as range ID */ | |
2810 | pin_range->range.id = gpio_offset; | |
2811 | pin_range->range.gc = chip; | |
2812 | pin_range->range.name = chip->label; | |
fdeb8e15 | 2813 | pin_range->range.base = gdev->base + gpio_offset; |
586a87e6 CR |
2814 | pin_range->pctldev = pctldev; |
2815 | ||
2816 | ret = pinctrl_get_group_pins(pctldev, pin_group, | |
2817 | &pin_range->range.pins, | |
2818 | &pin_range->range.npins); | |
61c6375d MN |
2819 | if (ret < 0) { |
2820 | kfree(pin_range); | |
586a87e6 | 2821 | return ret; |
61c6375d | 2822 | } |
586a87e6 CR |
2823 | |
2824 | pinctrl_add_gpio_range(pctldev, &pin_range->range); | |
2825 | ||
1a2a99c6 AS |
2826 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n", |
2827 | gpio_offset, gpio_offset + pin_range->range.npins - 1, | |
586a87e6 CR |
2828 | pinctrl_dev_get_devname(pctldev), pin_group); |
2829 | ||
20ec3e39 | 2830 | list_add_tail(&pin_range->node, &gdev->pin_ranges); |
586a87e6 CR |
2831 | |
2832 | return 0; | |
2833 | } | |
2834 | EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range); | |
2835 | ||
3f0f8670 LW |
2836 | /** |
2837 | * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping | |
2838 | * @chip: the gpiochip to add the range for | |
950d55f5 | 2839 | * @pinctl_name: the dev_name() of the pin controller to map to |
316511c0 LW |
2840 | * @gpio_offset: the start offset in the current gpio_chip number space |
2841 | * @pin_offset: the start offset in the pin controller number space | |
3f0f8670 LW |
2842 | * @npins: the number of pins from the offset of each pin space (GPIO and |
2843 | * pin controller) to accumulate in this range | |
950d55f5 TR |
2844 | * |
2845 | * Returns: | |
2846 | * 0 on success, or a negative error-code on failure. | |
973c1714 CL |
2847 | * |
2848 | * Calling this function directly from a DeviceTree-supported | |
2849 | * pinctrl driver is DEPRECATED. Please see Section 2.1 of | |
2850 | * Documentation/devicetree/bindings/gpio/gpio.txt on how to | |
2851 | * bind pinctrl and gpio drivers via the "gpio-ranges" property. | |
3f0f8670 | 2852 | */ |
1e63d7b9 | 2853 | int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
316511c0 | 2854 | unsigned int gpio_offset, unsigned int pin_offset, |
3f0f8670 | 2855 | unsigned int npins) |
f23f1516 SH |
2856 | { |
2857 | struct gpio_pin_range *pin_range; | |
fdeb8e15 | 2858 | struct gpio_device *gdev = chip->gpiodev; |
b4d4b1f0 | 2859 | int ret; |
f23f1516 | 2860 | |
3f0f8670 | 2861 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
f23f1516 | 2862 | if (!pin_range) { |
1a2a99c6 | 2863 | chip_err(chip, "failed to allocate pin ranges\n"); |
1e63d7b9 | 2864 | return -ENOMEM; |
f23f1516 SH |
2865 | } |
2866 | ||
3f0f8670 | 2867 | /* Use local offset as range ID */ |
316511c0 | 2868 | pin_range->range.id = gpio_offset; |
3f0f8670 | 2869 | pin_range->range.gc = chip; |
f23f1516 | 2870 | pin_range->range.name = chip->label; |
fdeb8e15 | 2871 | pin_range->range.base = gdev->base + gpio_offset; |
316511c0 | 2872 | pin_range->range.pin_base = pin_offset; |
f23f1516 | 2873 | pin_range->range.npins = npins; |
192c369c | 2874 | pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, |
f23f1516 | 2875 | &pin_range->range); |
8f23ca1a | 2876 | if (IS_ERR(pin_range->pctldev)) { |
b4d4b1f0 | 2877 | ret = PTR_ERR(pin_range->pctldev); |
1a2a99c6 | 2878 | chip_err(chip, "could not create pin range\n"); |
3f0f8670 | 2879 | kfree(pin_range); |
b4d4b1f0 | 2880 | return ret; |
3f0f8670 | 2881 | } |
1a2a99c6 AS |
2882 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n", |
2883 | gpio_offset, gpio_offset + npins - 1, | |
316511c0 LW |
2884 | pinctl_name, |
2885 | pin_offset, pin_offset + npins - 1); | |
f23f1516 | 2886 | |
20ec3e39 | 2887 | list_add_tail(&pin_range->node, &gdev->pin_ranges); |
1e63d7b9 LW |
2888 | |
2889 | return 0; | |
f23f1516 | 2890 | } |
165adc9c | 2891 | EXPORT_SYMBOL_GPL(gpiochip_add_pin_range); |
f23f1516 | 2892 | |
3f0f8670 LW |
2893 | /** |
2894 | * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings | |
2895 | * @chip: the chip to remove all the mappings for | |
2896 | */ | |
f23f1516 SH |
2897 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
2898 | { | |
2899 | struct gpio_pin_range *pin_range, *tmp; | |
20ec3e39 | 2900 | struct gpio_device *gdev = chip->gpiodev; |
f23f1516 | 2901 | |
20ec3e39 | 2902 | list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) { |
f23f1516 SH |
2903 | list_del(&pin_range->node); |
2904 | pinctrl_remove_gpio_range(pin_range->pctldev, | |
2905 | &pin_range->range); | |
3f0f8670 | 2906 | kfree(pin_range); |
f23f1516 SH |
2907 | } |
2908 | } | |
165adc9c LW |
2909 | EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); |
2910 | ||
2911 | #endif /* CONFIG_PINCTRL */ | |
f23f1516 | 2912 | |
d2876d08 DB |
2913 | /* These "optional" allocation calls help prevent drivers from stomping |
2914 | * on each other, and help provide better diagnostics in debugfs. | |
2915 | * They're called even less than the "set direction" calls. | |
2916 | */ | |
fac9d885 | 2917 | static int gpiod_request_commit(struct gpio_desc *desc, const char *label) |
d2876d08 | 2918 | { |
fdeb8e15 | 2919 | struct gpio_chip *chip = desc->gdev->chip; |
d377f56f | 2920 | int ret; |
d2876d08 | 2921 | unsigned long flags; |
3789f5ac | 2922 | unsigned offset; |
d2876d08 | 2923 | |
18534df4 MS |
2924 | if (label) { |
2925 | label = kstrdup_const(label, GFP_KERNEL); | |
2926 | if (!label) | |
2927 | return -ENOMEM; | |
2928 | } | |
2929 | ||
bcabdef1 AC |
2930 | spin_lock_irqsave(&gpio_lock, flags); |
2931 | ||
d2876d08 | 2932 | /* NOTE: gpio_request() can be called in early boot, |
35e8bb51 | 2933 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
d2876d08 DB |
2934 | */ |
2935 | ||
2936 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { | |
2937 | desc_set_label(desc, label ? : "?"); | |
d377f56f | 2938 | ret = 0; |
438d8908 | 2939 | } else { |
18534df4 | 2940 | kfree_const(label); |
d377f56f | 2941 | ret = -EBUSY; |
7460db56 | 2942 | goto done; |
35e8bb51 DB |
2943 | } |
2944 | ||
2945 | if (chip->request) { | |
2946 | /* chip->request may sleep */ | |
2947 | spin_unlock_irqrestore(&gpio_lock, flags); | |
3789f5ac BD |
2948 | offset = gpio_chip_hwgpio(desc); |
2949 | if (gpiochip_line_is_valid(chip, offset)) | |
d377f56f | 2950 | ret = chip->request(chip, offset); |
3789f5ac | 2951 | else |
d377f56f | 2952 | ret = -EINVAL; |
35e8bb51 DB |
2953 | spin_lock_irqsave(&gpio_lock, flags); |
2954 | ||
d377f56f | 2955 | if (ret < 0) { |
35e8bb51 | 2956 | desc_set_label(desc, NULL); |
18534df4 | 2957 | kfree_const(label); |
35e8bb51 | 2958 | clear_bit(FLAG_REQUESTED, &desc->flags); |
80b0a602 | 2959 | goto done; |
35e8bb51 | 2960 | } |
438d8908 | 2961 | } |
80b0a602 MN |
2962 | if (chip->get_direction) { |
2963 | /* chip->get_direction may sleep */ | |
2964 | spin_unlock_irqrestore(&gpio_lock, flags); | |
372e722e | 2965 | gpiod_get_direction(desc); |
80b0a602 MN |
2966 | spin_lock_irqsave(&gpio_lock, flags); |
2967 | } | |
77c2d792 MW |
2968 | done: |
2969 | spin_unlock_irqrestore(&gpio_lock, flags); | |
51c1064e BG |
2970 | atomic_notifier_call_chain(&desc->gdev->notifier, |
2971 | GPIOLINE_CHANGED_REQUESTED, desc); | |
d377f56f | 2972 | return ret; |
77c2d792 MW |
2973 | } |
2974 | ||
fdeb8e15 LW |
2975 | /* |
2976 | * This descriptor validation needs to be inserted verbatim into each | |
2977 | * function taking a descriptor, so we need to use a preprocessor | |
54d77198 LW |
2978 | * macro to avoid endless duplication. If the desc is NULL it is an |
2979 | * optional GPIO and calls should just bail out. | |
fdeb8e15 | 2980 | */ |
a746a232 RV |
2981 | static int validate_desc(const struct gpio_desc *desc, const char *func) |
2982 | { | |
2983 | if (!desc) | |
2984 | return 0; | |
2985 | if (IS_ERR(desc)) { | |
2986 | pr_warn("%s: invalid GPIO (errorpointer)\n", func); | |
2987 | return PTR_ERR(desc); | |
2988 | } | |
2989 | if (!desc->gdev) { | |
2990 | pr_warn("%s: invalid GPIO (no device)\n", func); | |
2991 | return -EINVAL; | |
2992 | } | |
2993 | if (!desc->gdev->chip) { | |
2994 | dev_warn(&desc->gdev->dev, | |
2995 | "%s: backing chip is gone\n", func); | |
2996 | return 0; | |
2997 | } | |
2998 | return 1; | |
2999 | } | |
3000 | ||
fdeb8e15 | 3001 | #define VALIDATE_DESC(desc) do { \ |
a746a232 RV |
3002 | int __valid = validate_desc(desc, __func__); \ |
3003 | if (__valid <= 0) \ | |
3004 | return __valid; \ | |
3005 | } while (0) | |
fdeb8e15 LW |
3006 | |
3007 | #define VALIDATE_DESC_VOID(desc) do { \ | |
a746a232 RV |
3008 | int __valid = validate_desc(desc, __func__); \ |
3009 | if (__valid <= 0) \ | |
fdeb8e15 | 3010 | return; \ |
a746a232 | 3011 | } while (0) |
fdeb8e15 | 3012 | |
0eb4c6c2 | 3013 | int gpiod_request(struct gpio_desc *desc, const char *label) |
77c2d792 | 3014 | { |
d377f56f | 3015 | int ret = -EPROBE_DEFER; |
fdeb8e15 | 3016 | struct gpio_device *gdev; |
77c2d792 | 3017 | |
fdeb8e15 LW |
3018 | VALIDATE_DESC(desc); |
3019 | gdev = desc->gdev; | |
77c2d792 | 3020 | |
fdeb8e15 | 3021 | if (try_module_get(gdev->owner)) { |
d377f56f LW |
3022 | ret = gpiod_request_commit(desc, label); |
3023 | if (ret < 0) | |
fdeb8e15 | 3024 | module_put(gdev->owner); |
33a68e86 LW |
3025 | else |
3026 | get_device(&gdev->dev); | |
77c2d792 MW |
3027 | } |
3028 | ||
d377f56f LW |
3029 | if (ret) |
3030 | gpiod_dbg(desc, "%s: status %d\n", __func__, ret); | |
77c2d792 | 3031 | |
d377f56f | 3032 | return ret; |
d2876d08 | 3033 | } |
372e722e | 3034 | |
fac9d885 | 3035 | static bool gpiod_free_commit(struct gpio_desc *desc) |
d2876d08 | 3036 | { |
77c2d792 | 3037 | bool ret = false; |
d2876d08 | 3038 | unsigned long flags; |
35e8bb51 | 3039 | struct gpio_chip *chip; |
d2876d08 | 3040 | |
3d599d1c UKK |
3041 | might_sleep(); |
3042 | ||
372e722e | 3043 | gpiod_unexport(desc); |
d8f388d8 | 3044 | |
d2876d08 DB |
3045 | spin_lock_irqsave(&gpio_lock, flags); |
3046 | ||
fdeb8e15 | 3047 | chip = desc->gdev->chip; |
35e8bb51 DB |
3048 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { |
3049 | if (chip->free) { | |
3050 | spin_unlock_irqrestore(&gpio_lock, flags); | |
9c4ba946 | 3051 | might_sleep_if(chip->can_sleep); |
372e722e | 3052 | chip->free(chip, gpio_chip_hwgpio(desc)); |
35e8bb51 DB |
3053 | spin_lock_irqsave(&gpio_lock, flags); |
3054 | } | |
18534df4 | 3055 | kfree_const(desc->label); |
d2876d08 | 3056 | desc_set_label(desc, NULL); |
07697461 | 3057 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
35e8bb51 | 3058 | clear_bit(FLAG_REQUESTED, &desc->flags); |
aca5ce14 | 3059 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
25553ff0 | 3060 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
9225d516 DF |
3061 | clear_bit(FLAG_PULL_UP, &desc->flags); |
3062 | clear_bit(FLAG_PULL_DOWN, &desc->flags); | |
2148ad77 | 3063 | clear_bit(FLAG_BIAS_DISABLE, &desc->flags); |
f625d460 | 3064 | clear_bit(FLAG_IS_HOGGED, &desc->flags); |
63636d95 GU |
3065 | #ifdef CONFIG_OF_DYNAMIC |
3066 | desc->hog = NULL; | |
3067 | #endif | |
77c2d792 MW |
3068 | ret = true; |
3069 | } | |
d2876d08 DB |
3070 | |
3071 | spin_unlock_irqrestore(&gpio_lock, flags); | |
51c1064e BG |
3072 | atomic_notifier_call_chain(&desc->gdev->notifier, |
3073 | GPIOLINE_CHANGED_RELEASED, desc); | |
3074 | ||
77c2d792 MW |
3075 | return ret; |
3076 | } | |
3077 | ||
0eb4c6c2 | 3078 | void gpiod_free(struct gpio_desc *desc) |
77c2d792 | 3079 | { |
fac9d885 | 3080 | if (desc && desc->gdev && gpiod_free_commit(desc)) { |
fdeb8e15 | 3081 | module_put(desc->gdev->owner); |
33a68e86 LW |
3082 | put_device(&desc->gdev->dev); |
3083 | } else { | |
77c2d792 | 3084 | WARN_ON(extra_checks); |
33a68e86 | 3085 | } |
d2876d08 | 3086 | } |
372e722e | 3087 | |
d2876d08 DB |
3088 | /** |
3089 | * gpiochip_is_requested - return string iff signal was requested | |
3090 | * @chip: controller managing the signal | |
3091 | * @offset: of signal within controller's 0..(ngpio - 1) range | |
3092 | * | |
3093 | * Returns NULL if the GPIO is not currently requested, else a string. | |
9c8318ff AC |
3094 | * The string returned is the label passed to gpio_request(); if none has been |
3095 | * passed it is a meaningless, non-NULL constant. | |
d2876d08 DB |
3096 | * |
3097 | * This function is for use by GPIO controller drivers. The label can | |
3098 | * help with diagnostics, and knowing that the signal is used as a GPIO | |
3099 | * can help avoid accidentally multiplexing it to another controller. | |
3100 | */ | |
3101 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) | |
3102 | { | |
6c0b4e6c | 3103 | struct gpio_desc *desc; |
d2876d08 | 3104 | |
48b5953e | 3105 | if (offset >= chip->ngpio) |
d2876d08 | 3106 | return NULL; |
6c0b4e6c | 3107 | |
1739a2d8 BG |
3108 | desc = gpiochip_get_desc(chip, offset); |
3109 | if (IS_ERR(desc)) | |
3110 | return NULL; | |
6c0b4e6c | 3111 | |
372e722e | 3112 | if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) |
d2876d08 | 3113 | return NULL; |
372e722e | 3114 | return desc->label; |
d2876d08 DB |
3115 | } |
3116 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); | |
3117 | ||
77c2d792 MW |
3118 | /** |
3119 | * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor | |
950d55f5 TR |
3120 | * @chip: GPIO chip |
3121 | * @hwnum: hardware number of the GPIO for which to request the descriptor | |
77c2d792 | 3122 | * @label: label for the GPIO |
5923ea6c LW |
3123 | * @lflags: lookup flags for this GPIO or 0 if default, this can be used to |
3124 | * specify things like line inversion semantics with the machine flags | |
3125 | * such as GPIO_OUT_LOW | |
3126 | * @dflags: descriptor request flags for this GPIO or 0 if default, this | |
3127 | * can be used to specify consumer semantics such as open drain | |
77c2d792 MW |
3128 | * |
3129 | * Function allows GPIO chip drivers to request and use their own GPIO | |
3130 | * descriptors via gpiolib API. Difference to gpiod_request() is that this | |
3131 | * function will not increase reference count of the GPIO chip module. This | |
3132 | * allows the GPIO chip module to be unloaded as needed (we assume that the | |
3133 | * GPIO chip driver handles freeing the GPIOs it has requested). | |
950d55f5 TR |
3134 | * |
3135 | * Returns: | |
3136 | * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error | |
3137 | * code on failure. | |
77c2d792 | 3138 | */ |
06863620 BG |
3139 | struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, |
3140 | unsigned int hwnum, | |
21abf103 | 3141 | const char *label, |
5923ea6c LW |
3142 | enum gpio_lookup_flags lflags, |
3143 | enum gpiod_flags dflags) | |
77c2d792 | 3144 | { |
abdc08a3 | 3145 | struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum); |
d377f56f | 3146 | int ret; |
77c2d792 | 3147 | |
abdc08a3 AC |
3148 | if (IS_ERR(desc)) { |
3149 | chip_err(chip, "failed to get GPIO descriptor\n"); | |
3150 | return desc; | |
3151 | } | |
3152 | ||
d377f56f LW |
3153 | ret = gpiod_request_commit(desc, label); |
3154 | if (ret < 0) | |
3155 | return ERR_PTR(ret); | |
77c2d792 | 3156 | |
d377f56f LW |
3157 | ret = gpiod_configure_flags(desc, label, lflags, dflags); |
3158 | if (ret) { | |
21abf103 LW |
3159 | chip_err(chip, "setup of own GPIO %s failed\n", label); |
3160 | gpiod_free_commit(desc); | |
d377f56f | 3161 | return ERR_PTR(ret); |
21abf103 LW |
3162 | } |
3163 | ||
abdc08a3 | 3164 | return desc; |
77c2d792 | 3165 | } |
f7d4ad98 | 3166 | EXPORT_SYMBOL_GPL(gpiochip_request_own_desc); |
77c2d792 MW |
3167 | |
3168 | /** | |
3169 | * gpiochip_free_own_desc - Free GPIO requested by the chip driver | |
3170 | * @desc: GPIO descriptor to free | |
3171 | * | |
3172 | * Function frees the given GPIO requested previously with | |
3173 | * gpiochip_request_own_desc(). | |
3174 | */ | |
3175 | void gpiochip_free_own_desc(struct gpio_desc *desc) | |
3176 | { | |
3177 | if (desc) | |
fac9d885 | 3178 | gpiod_free_commit(desc); |
77c2d792 | 3179 | } |
f7d4ad98 | 3180 | EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); |
d2876d08 | 3181 | |
fdeb8e15 LW |
3182 | /* |
3183 | * Drivers MUST set GPIO direction before making get/set calls. In | |
d2876d08 DB |
3184 | * some cases this is done in early boot, before IRQs are enabled. |
3185 | * | |
3186 | * As a rule these aren't called more than once (except for drivers | |
3187 | * using the open-drain emulation idiom) so these are natural places | |
3188 | * to accumulate extra debugging checks. Note that we can't (yet) | |
3189 | * rely on gpio_request() having been called beforehand. | |
3190 | */ | |
3191 | ||
d99f8876 | 3192 | static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset, |
62adc6f3 | 3193 | unsigned long config) |
71479789 | 3194 | { |
d90f3685 BG |
3195 | if (!gc->set_config) |
3196 | return -ENOTSUPP; | |
542f3615 | 3197 | |
62adc6f3 | 3198 | return gc->set_config(gc, offset, config); |
71479789 TP |
3199 | } |
3200 | ||
83522358 | 3201 | static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode) |
d99f8876 | 3202 | { |
83522358 | 3203 | struct gpio_chip *chip = desc->gdev->chip; |
91b4ea5f | 3204 | unsigned long config; |
d99f8876 BG |
3205 | unsigned arg; |
3206 | ||
3207 | switch (mode) { | |
d99f8876 BG |
3208 | case PIN_CONFIG_BIAS_PULL_DOWN: |
3209 | case PIN_CONFIG_BIAS_PULL_UP: | |
3210 | arg = 1; | |
3211 | break; | |
3212 | ||
3213 | default: | |
3214 | arg = 0; | |
3215 | } | |
3216 | ||
91b4ea5f | 3217 | config = PIN_CONF_PACKED(mode, arg); |
83522358 | 3218 | return gpio_do_set_config(chip, gpio_chip_hwgpio(desc), config); |
71479789 TP |
3219 | } |
3220 | ||
5f4bf171 | 3221 | static int gpio_set_bias(struct gpio_desc *desc) |
2148ad77 KG |
3222 | { |
3223 | int bias = 0; | |
3224 | int ret = 0; | |
3225 | ||
3226 | if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) | |
3227 | bias = PIN_CONFIG_BIAS_DISABLE; | |
3228 | else if (test_bit(FLAG_PULL_UP, &desc->flags)) | |
3229 | bias = PIN_CONFIG_BIAS_PULL_UP; | |
3230 | else if (test_bit(FLAG_PULL_DOWN, &desc->flags)) | |
3231 | bias = PIN_CONFIG_BIAS_PULL_DOWN; | |
3232 | ||
3233 | if (bias) { | |
83522358 | 3234 | ret = gpio_set_config(desc, bias); |
2148ad77 KG |
3235 | if (ret != -ENOTSUPP) |
3236 | return ret; | |
3237 | } | |
3238 | return 0; | |
3239 | } | |
3240 | ||
79a9becd AC |
3241 | /** |
3242 | * gpiod_direction_input - set the GPIO direction to input | |
3243 | * @desc: GPIO to set to input | |
3244 | * | |
3245 | * Set the direction of the passed GPIO to input, such as gpiod_get_value() can | |
3246 | * be called safely on it. | |
3247 | * | |
3248 | * Return 0 in case of success, else an error code. | |
3249 | */ | |
3250 | int gpiod_direction_input(struct gpio_desc *desc) | |
d2876d08 | 3251 | { |
d2876d08 | 3252 | struct gpio_chip *chip; |
d377f56f | 3253 | int ret = 0; |
d2876d08 | 3254 | |
fdeb8e15 LW |
3255 | VALIDATE_DESC(desc); |
3256 | chip = desc->gdev->chip; | |
bcabdef1 | 3257 | |
e48d194d LW |
3258 | /* |
3259 | * It is legal to have no .get() and .direction_input() specified if | |
3260 | * the chip is output-only, but you can't specify .direction_input() | |
3261 | * and not support the .get() operation, that doesn't make sense. | |
3262 | */ | |
ae9847f4 | 3263 | if (!chip->get && chip->direction_input) { |
6424de5a | 3264 | gpiod_warn(desc, |
e48d194d LW |
3265 | "%s: missing get() but have direction_input()\n", |
3266 | __func__); | |
be1a4b13 LW |
3267 | return -EIO; |
3268 | } | |
3269 | ||
e48d194d LW |
3270 | /* |
3271 | * If we have a .direction_input() callback, things are simple, | |
3272 | * just call it. Else we are some input-only chip so try to check the | |
3273 | * direction (if .get_direction() is supported) else we silently | |
3274 | * assume we are in input mode after this. | |
3275 | */ | |
ae9847f4 | 3276 | if (chip->direction_input) { |
d377f56f | 3277 | ret = chip->direction_input(chip, gpio_chip_hwgpio(desc)); |
ae9847f4 RRD |
3278 | } else if (chip->get_direction && |
3279 | (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) { | |
3280 | gpiod_warn(desc, | |
e48d194d LW |
3281 | "%s: missing direction_input() operation and line is output\n", |
3282 | __func__); | |
ae9847f4 RRD |
3283 | return -EIO; |
3284 | } | |
2148ad77 | 3285 | if (ret == 0) { |
d2876d08 | 3286 | clear_bit(FLAG_IS_OUT, &desc->flags); |
5f4bf171 | 3287 | ret = gpio_set_bias(desc); |
2148ad77 | 3288 | } |
d449991c | 3289 | |
d377f56f | 3290 | trace_gpio_direction(desc_to_gpio(desc), 1, ret); |
d82da797 | 3291 | |
d377f56f | 3292 | return ret; |
d2876d08 | 3293 | } |
79a9becd | 3294 | EXPORT_SYMBOL_GPL(gpiod_direction_input); |
372e722e | 3295 | |
fac9d885 | 3296 | static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) |
d2876d08 | 3297 | { |
c663e5f5 | 3298 | struct gpio_chip *gc = desc->gdev->chip; |
ad17731d | 3299 | int val = !!value; |
ae9847f4 | 3300 | int ret = 0; |
d2876d08 | 3301 | |
e48d194d LW |
3302 | /* |
3303 | * It's OK not to specify .direction_output() if the gpiochip is | |
3304 | * output-only, but if there is then not even a .set() operation it | |
3305 | * is pretty tricky to drive the output line. | |
3306 | */ | |
ae9847f4 | 3307 | if (!gc->set && !gc->direction_output) { |
6424de5a | 3308 | gpiod_warn(desc, |
e48d194d LW |
3309 | "%s: missing set() and direction_output() operations\n", |
3310 | __func__); | |
be1a4b13 LW |
3311 | return -EIO; |
3312 | } | |
3313 | ||
ae9847f4 RRD |
3314 | if (gc->direction_output) { |
3315 | ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val); | |
3316 | } else { | |
e48d194d | 3317 | /* Check that we are in output mode if we can */ |
ae9847f4 RRD |
3318 | if (gc->get_direction && |
3319 | gc->get_direction(gc, gpio_chip_hwgpio(desc))) { | |
3320 | gpiod_warn(desc, | |
3321 | "%s: missing direction_output() operation\n", | |
3322 | __func__); | |
3323 | return -EIO; | |
3324 | } | |
e48d194d LW |
3325 | /* |
3326 | * If we can't actively set the direction, we are some | |
3327 | * output-only chip, so just drive the output as desired. | |
3328 | */ | |
ae9847f4 RRD |
3329 | gc->set(gc, gpio_chip_hwgpio(desc), val); |
3330 | } | |
3331 | ||
c663e5f5 | 3332 | if (!ret) |
d2876d08 | 3333 | set_bit(FLAG_IS_OUT, &desc->flags); |
ad17731d | 3334 | trace_gpio_value(desc_to_gpio(desc), 0, val); |
c663e5f5 LW |
3335 | trace_gpio_direction(desc_to_gpio(desc), 0, ret); |
3336 | return ret; | |
d2876d08 | 3337 | } |
ef70bbe1 PZ |
3338 | |
3339 | /** | |
3340 | * gpiod_direction_output_raw - set the GPIO direction to output | |
3341 | * @desc: GPIO to set to output | |
3342 | * @value: initial output value of the GPIO | |
3343 | * | |
3344 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can | |
3345 | * be called safely on it. The initial value of the output must be specified | |
3346 | * as raw value on the physical line without regard for the ACTIVE_LOW status. | |
3347 | * | |
3348 | * Return 0 in case of success, else an error code. | |
3349 | */ | |
3350 | int gpiod_direction_output_raw(struct gpio_desc *desc, int value) | |
3351 | { | |
fdeb8e15 | 3352 | VALIDATE_DESC(desc); |
fac9d885 | 3353 | return gpiod_direction_output_raw_commit(desc, value); |
ef70bbe1 PZ |
3354 | } |
3355 | EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); | |
3356 | ||
3357 | /** | |
90df4fe0 | 3358 | * gpiod_direction_output - set the GPIO direction to output |
ef70bbe1 PZ |
3359 | * @desc: GPIO to set to output |
3360 | * @value: initial output value of the GPIO | |
3361 | * | |
3362 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can | |
3363 | * be called safely on it. The initial value of the output must be specified | |
3364 | * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | |
3365 | * account. | |
3366 | * | |
3367 | * Return 0 in case of success, else an error code. | |
3368 | */ | |
3369 | int gpiod_direction_output(struct gpio_desc *desc, int value) | |
3370 | { | |
02e47980 LW |
3371 | int ret; |
3372 | ||
fdeb8e15 | 3373 | VALIDATE_DESC(desc); |
ef70bbe1 PZ |
3374 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
3375 | value = !value; | |
ad17731d LW |
3376 | else |
3377 | value = !!value; | |
02e47980 | 3378 | |
4e9439dd HV |
3379 | /* GPIOs used for enabled IRQs shall not be set as output */ |
3380 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) && | |
3381 | test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) { | |
02e47980 LW |
3382 | gpiod_err(desc, |
3383 | "%s: tried to set a GPIO tied to an IRQ as output\n", | |
3384 | __func__); | |
3385 | return -EIO; | |
3386 | } | |
3387 | ||
3388 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { | |
3389 | /* First see if we can enable open drain in hardware */ | |
83522358 | 3390 | ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN); |
02e47980 LW |
3391 | if (!ret) |
3392 | goto set_output_value; | |
3393 | /* Emulate open drain by not actively driving the line high */ | |
e735244e BG |
3394 | if (value) { |
3395 | ret = gpiod_direction_input(desc); | |
3396 | goto set_output_flag; | |
3397 | } | |
02e47980 LW |
3398 | } |
3399 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { | |
83522358 | 3400 | ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE); |
02e47980 LW |
3401 | if (!ret) |
3402 | goto set_output_value; | |
3403 | /* Emulate open source by not actively driving the line low */ | |
e735244e BG |
3404 | if (!value) { |
3405 | ret = gpiod_direction_input(desc); | |
3406 | goto set_output_flag; | |
3407 | } | |
02e47980 | 3408 | } else { |
83522358 | 3409 | gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL); |
02e47980 LW |
3410 | } |
3411 | ||
3412 | set_output_value: | |
5f4bf171 | 3413 | ret = gpio_set_bias(desc); |
2821ae5f KG |
3414 | if (ret) |
3415 | return ret; | |
fac9d885 | 3416 | return gpiod_direction_output_raw_commit(desc, value); |
e735244e BG |
3417 | |
3418 | set_output_flag: | |
3419 | /* | |
3420 | * When emulating open-source or open-drain functionalities by not | |
3421 | * actively driving the line (setting mode to input) we still need to | |
3422 | * set the IS_OUT flag or otherwise we won't be able to set the line | |
3423 | * value anymore. | |
3424 | */ | |
3425 | if (ret == 0) | |
3426 | set_bit(FLAG_IS_OUT, &desc->flags); | |
3427 | return ret; | |
ef70bbe1 | 3428 | } |
79a9becd | 3429 | EXPORT_SYMBOL_GPL(gpiod_direction_output); |
d2876d08 | 3430 | |
8ced32ff GU |
3431 | /** |
3432 | * gpiod_set_config - sets @config for a GPIO | |
3433 | * @desc: descriptor of the GPIO for which to set the configuration | |
3434 | * @config: Same packed config format as generic pinconf | |
3435 | * | |
3436 | * Returns: | |
3437 | * 0 on success, %-ENOTSUPP if the controller doesn't support setting the | |
3438 | * configuration. | |
3439 | */ | |
3440 | int gpiod_set_config(struct gpio_desc *desc, unsigned long config) | |
3441 | { | |
3442 | struct gpio_chip *chip; | |
3443 | ||
3444 | VALIDATE_DESC(desc); | |
3445 | chip = desc->gdev->chip; | |
3446 | ||
3447 | return gpio_do_set_config(chip, gpio_chip_hwgpio(desc), config); | |
3448 | } | |
3449 | EXPORT_SYMBOL_GPL(gpiod_set_config); | |
3450 | ||
c4b5be98 | 3451 | /** |
950d55f5 TR |
3452 | * gpiod_set_debounce - sets @debounce time for a GPIO |
3453 | * @desc: descriptor of the GPIO for which to set debounce time | |
3454 | * @debounce: debounce time in microseconds | |
65d87656 | 3455 | * |
950d55f5 TR |
3456 | * Returns: |
3457 | * 0 on success, %-ENOTSUPP if the controller doesn't support setting the | |
3458 | * debounce time. | |
c4b5be98 | 3459 | */ |
79a9becd | 3460 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
c4b5be98 | 3461 | { |
8ced32ff | 3462 | unsigned long config; |
be1a4b13 | 3463 | |
2956b5d9 | 3464 | config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce); |
8ced32ff | 3465 | return gpiod_set_config(desc, config); |
c4b5be98 | 3466 | } |
79a9becd | 3467 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); |
372e722e | 3468 | |
e10f72bf AJ |
3469 | /** |
3470 | * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset | |
3471 | * @desc: descriptor of the GPIO for which to configure persistence | |
3472 | * @transitory: True to lose state on suspend or reset, false for persistence | |
3473 | * | |
3474 | * Returns: | |
3475 | * 0 on success, otherwise a negative error code. | |
3476 | */ | |
3477 | int gpiod_set_transitory(struct gpio_desc *desc, bool transitory) | |
3478 | { | |
3479 | struct gpio_chip *chip; | |
3480 | unsigned long packed; | |
3481 | int gpio; | |
3482 | int rc; | |
3483 | ||
156dd392 | 3484 | VALIDATE_DESC(desc); |
e10f72bf AJ |
3485 | /* |
3486 | * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for | |
3487 | * persistence state. | |
3488 | */ | |
4fc5bfeb | 3489 | assign_bit(FLAG_TRANSITORY, &desc->flags, transitory); |
e10f72bf AJ |
3490 | |
3491 | /* If the driver supports it, set the persistence state now */ | |
3492 | chip = desc->gdev->chip; | |
3493 | if (!chip->set_config) | |
3494 | return 0; | |
3495 | ||
3496 | packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE, | |
3497 | !transitory); | |
3498 | gpio = gpio_chip_hwgpio(desc); | |
d99f8876 | 3499 | rc = gpio_do_set_config(chip, gpio, packed); |
e10f72bf AJ |
3500 | if (rc == -ENOTSUPP) { |
3501 | dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n", | |
3502 | gpio); | |
3503 | return 0; | |
3504 | } | |
3505 | ||
3506 | return rc; | |
3507 | } | |
3508 | EXPORT_SYMBOL_GPL(gpiod_set_transitory); | |
3509 | ||
79a9becd AC |
3510 | /** |
3511 | * gpiod_is_active_low - test whether a GPIO is active-low or not | |
3512 | * @desc: the gpio descriptor to test | |
3513 | * | |
3514 | * Returns 1 if the GPIO is active-low, 0 otherwise. | |
3515 | */ | |
3516 | int gpiod_is_active_low(const struct gpio_desc *desc) | |
372e722e | 3517 | { |
fdeb8e15 | 3518 | VALIDATE_DESC(desc); |
79a9becd | 3519 | return test_bit(FLAG_ACTIVE_LOW, &desc->flags); |
372e722e | 3520 | } |
79a9becd | 3521 | EXPORT_SYMBOL_GPL(gpiod_is_active_low); |
d2876d08 | 3522 | |
d3a5bcb4 MM |
3523 | /** |
3524 | * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not | |
3525 | * @desc: the gpio descriptor to change | |
3526 | */ | |
3527 | void gpiod_toggle_active_low(struct gpio_desc *desc) | |
3528 | { | |
3529 | VALIDATE_DESC_VOID(desc); | |
3530 | change_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
3531 | } | |
3532 | EXPORT_SYMBOL_GPL(gpiod_toggle_active_low); | |
3533 | ||
d2876d08 DB |
3534 | /* I/O calls are only valid after configuration completed; the relevant |
3535 | * "is this a valid GPIO" error checks should already have been done. | |
3536 | * | |
3537 | * "Get" operations are often inlinable as reading a pin value register, | |
3538 | * and masking the relevant bit in that register. | |
3539 | * | |
3540 | * When "set" operations are inlinable, they involve writing that mask to | |
3541 | * one register to set a low value, or a different register to set it high. | |
3542 | * Otherwise locking is needed, so there may be little value to inlining. | |
3543 | * | |
3544 | *------------------------------------------------------------------------ | |
3545 | * | |
3546 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers | |
3547 | * have requested the GPIO. That can include implicit requesting by | |
3548 | * a direction setting call. Marking a gpio as requested locks its chip | |
3549 | * in memory, guaranteeing that these table lookups need no more locking | |
3550 | * and that gpiochip_remove() will fail. | |
3551 | * | |
3552 | * REVISIT when debugging, consider adding some instrumentation to ensure | |
3553 | * that the GPIO was actually requested. | |
3554 | */ | |
3555 | ||
fac9d885 | 3556 | static int gpiod_get_raw_value_commit(const struct gpio_desc *desc) |
d2876d08 DB |
3557 | { |
3558 | struct gpio_chip *chip; | |
372e722e | 3559 | int offset; |
e20538b8 | 3560 | int value; |
d2876d08 | 3561 | |
fdeb8e15 | 3562 | chip = desc->gdev->chip; |
372e722e | 3563 | offset = gpio_chip_hwgpio(desc); |
e20538b8 | 3564 | value = chip->get ? chip->get(chip, offset) : -EIO; |
723a6303 | 3565 | value = value < 0 ? value : !!value; |
372e722e | 3566 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
3f397c21 | 3567 | return value; |
d2876d08 | 3568 | } |
372e722e | 3569 | |
eec1d566 LW |
3570 | static int gpio_chip_get_multiple(struct gpio_chip *chip, |
3571 | unsigned long *mask, unsigned long *bits) | |
3572 | { | |
3573 | if (chip->get_multiple) { | |
3574 | return chip->get_multiple(chip, mask, bits); | |
3575 | } else if (chip->get) { | |
3576 | int i, value; | |
3577 | ||
3578 | for_each_set_bit(i, mask, chip->ngpio) { | |
3579 | value = chip->get(chip, i); | |
3580 | if (value < 0) | |
3581 | return value; | |
3582 | __assign_bit(i, bits, value); | |
3583 | } | |
3584 | return 0; | |
3585 | } | |
3586 | return -EIO; | |
3587 | } | |
3588 | ||
3589 | int gpiod_get_array_value_complex(bool raw, bool can_sleep, | |
3590 | unsigned int array_size, | |
3591 | struct gpio_desc **desc_array, | |
77588c14 | 3592 | struct gpio_array *array_info, |
b9762beb | 3593 | unsigned long *value_bitmap) |
eec1d566 | 3594 | { |
d377f56f | 3595 | int ret, i = 0; |
b17566a6 JK |
3596 | |
3597 | /* | |
3598 | * Validate array_info against desc_array and its size. | |
3599 | * It should immediately follow desc_array if both | |
3600 | * have been obtained from the same gpiod_get_array() call. | |
3601 | */ | |
3602 | if (array_info && array_info->desc == desc_array && | |
3603 | array_size <= array_info->size && | |
3604 | (void *)array_info == desc_array + array_info->size) { | |
3605 | if (!can_sleep) | |
3606 | WARN_ON(array_info->chip->can_sleep); | |
3607 | ||
d377f56f | 3608 | ret = gpio_chip_get_multiple(array_info->chip, |
b17566a6 JK |
3609 | array_info->get_mask, |
3610 | value_bitmap); | |
d377f56f LW |
3611 | if (ret) |
3612 | return ret; | |
b17566a6 JK |
3613 | |
3614 | if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) | |
3615 | bitmap_xor(value_bitmap, value_bitmap, | |
3616 | array_info->invert_mask, array_size); | |
3617 | ||
3618 | if (bitmap_full(array_info->get_mask, array_size)) | |
3619 | return 0; | |
3620 | ||
3621 | i = find_first_zero_bit(array_info->get_mask, array_size); | |
3622 | } else { | |
3623 | array_info = NULL; | |
3624 | } | |
eec1d566 LW |
3625 | |
3626 | while (i < array_size) { | |
3627 | struct gpio_chip *chip = desc_array[i]->gdev->chip; | |
3027743f LA |
3628 | unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)]; |
3629 | unsigned long *mask, *bits; | |
eec1d566 LW |
3630 | int first, j, ret; |
3631 | ||
3027743f LA |
3632 | if (likely(chip->ngpio <= FASTPATH_NGPIO)) { |
3633 | mask = fastpath; | |
3634 | } else { | |
3635 | mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio), | |
3636 | sizeof(*mask), | |
3637 | can_sleep ? GFP_KERNEL : GFP_ATOMIC); | |
3638 | if (!mask) | |
3639 | return -ENOMEM; | |
3640 | } | |
3641 | ||
3642 | bits = mask + BITS_TO_LONGS(chip->ngpio); | |
3643 | bitmap_zero(mask, chip->ngpio); | |
3644 | ||
eec1d566 LW |
3645 | if (!can_sleep) |
3646 | WARN_ON(chip->can_sleep); | |
3647 | ||
3648 | /* collect all inputs belonging to the same chip */ | |
3649 | first = i; | |
eec1d566 LW |
3650 | do { |
3651 | const struct gpio_desc *desc = desc_array[i]; | |
3652 | int hwgpio = gpio_chip_hwgpio(desc); | |
3653 | ||
3654 | __set_bit(hwgpio, mask); | |
3655 | i++; | |
b17566a6 JK |
3656 | |
3657 | if (array_info) | |
35ae7f96 JK |
3658 | i = find_next_zero_bit(array_info->get_mask, |
3659 | array_size, i); | |
eec1d566 LW |
3660 | } while ((i < array_size) && |
3661 | (desc_array[i]->gdev->chip == chip)); | |
3662 | ||
3663 | ret = gpio_chip_get_multiple(chip, mask, bits); | |
3027743f LA |
3664 | if (ret) { |
3665 | if (mask != fastpath) | |
3666 | kfree(mask); | |
eec1d566 | 3667 | return ret; |
3027743f | 3668 | } |
eec1d566 | 3669 | |
b17566a6 | 3670 | for (j = first; j < i; ) { |
eec1d566 LW |
3671 | const struct gpio_desc *desc = desc_array[j]; |
3672 | int hwgpio = gpio_chip_hwgpio(desc); | |
3673 | int value = test_bit(hwgpio, bits); | |
3674 | ||
3675 | if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
3676 | value = !value; | |
b9762beb | 3677 | __assign_bit(j, value_bitmap, value); |
eec1d566 | 3678 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
799d5eb4 | 3679 | j++; |
b17566a6 JK |
3680 | |
3681 | if (array_info) | |
35ae7f96 JK |
3682 | j = find_next_zero_bit(array_info->get_mask, i, |
3683 | j); | |
eec1d566 | 3684 | } |
3027743f LA |
3685 | |
3686 | if (mask != fastpath) | |
3687 | kfree(mask); | |
eec1d566 LW |
3688 | } |
3689 | return 0; | |
3690 | } | |
3691 | ||
d2876d08 | 3692 | /** |
79a9becd AC |
3693 | * gpiod_get_raw_value() - return a gpio's raw value |
3694 | * @desc: gpio whose value will be returned | |
d2876d08 | 3695 | * |
79a9becd | 3696 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding |
e20538b8 | 3697 | * its ACTIVE_LOW status, or negative errno on failure. |
79a9becd | 3698 | * |
827a9b8b | 3699 | * This function can be called from contexts where we cannot sleep, and will |
79a9becd | 3700 | * complain if the GPIO chip functions potentially sleep. |
d2876d08 | 3701 | */ |
79a9becd | 3702 | int gpiod_get_raw_value(const struct gpio_desc *desc) |
d2876d08 | 3703 | { |
fdeb8e15 | 3704 | VALIDATE_DESC(desc); |
3285170f | 3705 | /* Should be using gpiod_get_raw_value_cansleep() */ |
fdeb8e15 | 3706 | WARN_ON(desc->gdev->chip->can_sleep); |
fac9d885 | 3707 | return gpiod_get_raw_value_commit(desc); |
d2876d08 | 3708 | } |
79a9becd | 3709 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value); |
372e722e | 3710 | |
79a9becd AC |
3711 | /** |
3712 | * gpiod_get_value() - return a gpio's value | |
3713 | * @desc: gpio whose value will be returned | |
3714 | * | |
3715 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into | |
e20538b8 | 3716 | * account, or negative errno on failure. |
79a9becd | 3717 | * |
827a9b8b | 3718 | * This function can be called from contexts where we cannot sleep, and will |
79a9becd AC |
3719 | * complain if the GPIO chip functions potentially sleep. |
3720 | */ | |
3721 | int gpiod_get_value(const struct gpio_desc *desc) | |
372e722e | 3722 | { |
79a9becd | 3723 | int value; |
fdeb8e15 LW |
3724 | |
3725 | VALIDATE_DESC(desc); | |
3285170f | 3726 | /* Should be using gpiod_get_value_cansleep() */ |
fdeb8e15 | 3727 | WARN_ON(desc->gdev->chip->can_sleep); |
79a9becd | 3728 | |
fac9d885 | 3729 | value = gpiod_get_raw_value_commit(desc); |
e20538b8 BA |
3730 | if (value < 0) |
3731 | return value; | |
3732 | ||
79a9becd AC |
3733 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
3734 | value = !value; | |
3735 | ||
3736 | return value; | |
372e722e | 3737 | } |
79a9becd | 3738 | EXPORT_SYMBOL_GPL(gpiod_get_value); |
d2876d08 | 3739 | |
eec1d566 LW |
3740 | /** |
3741 | * gpiod_get_raw_array_value() - read raw values from an array of GPIOs | |
b9762beb | 3742 | * @array_size: number of elements in the descriptor array / value bitmap |
eec1d566 | 3743 | * @desc_array: array of GPIO descriptors whose values will be read |
77588c14 | 3744 | * @array_info: information on applicability of fast bitmap processing path |
b9762beb | 3745 | * @value_bitmap: bitmap to store the read values |
eec1d566 LW |
3746 | * |
3747 | * Read the raw values of the GPIOs, i.e. the values of the physical lines | |
3748 | * without regard for their ACTIVE_LOW status. Return 0 in case of success, | |
3749 | * else an error code. | |
3750 | * | |
827a9b8b | 3751 | * This function can be called from contexts where we cannot sleep, |
eec1d566 LW |
3752 | * and it will complain if the GPIO chip functions potentially sleep. |
3753 | */ | |
3754 | int gpiod_get_raw_array_value(unsigned int array_size, | |
b9762beb | 3755 | struct gpio_desc **desc_array, |
77588c14 | 3756 | struct gpio_array *array_info, |
b9762beb | 3757 | unsigned long *value_bitmap) |
eec1d566 LW |
3758 | { |
3759 | if (!desc_array) | |
3760 | return -EINVAL; | |
3761 | return gpiod_get_array_value_complex(true, false, array_size, | |
77588c14 JK |
3762 | desc_array, array_info, |
3763 | value_bitmap); | |
eec1d566 LW |
3764 | } |
3765 | EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value); | |
3766 | ||
3767 | /** | |
3768 | * gpiod_get_array_value() - read values from an array of GPIOs | |
b9762beb | 3769 | * @array_size: number of elements in the descriptor array / value bitmap |
eec1d566 | 3770 | * @desc_array: array of GPIO descriptors whose values will be read |
77588c14 | 3771 | * @array_info: information on applicability of fast bitmap processing path |
b9762beb | 3772 | * @value_bitmap: bitmap to store the read values |
eec1d566 LW |
3773 | * |
3774 | * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status | |
3775 | * into account. Return 0 in case of success, else an error code. | |
3776 | * | |
827a9b8b | 3777 | * This function can be called from contexts where we cannot sleep, |
eec1d566 LW |
3778 | * and it will complain if the GPIO chip functions potentially sleep. |
3779 | */ | |
3780 | int gpiod_get_array_value(unsigned int array_size, | |
b9762beb | 3781 | struct gpio_desc **desc_array, |
77588c14 | 3782 | struct gpio_array *array_info, |
b9762beb | 3783 | unsigned long *value_bitmap) |
eec1d566 LW |
3784 | { |
3785 | if (!desc_array) | |
3786 | return -EINVAL; | |
3787 | return gpiod_get_array_value_complex(false, false, array_size, | |
77588c14 JK |
3788 | desc_array, array_info, |
3789 | value_bitmap); | |
eec1d566 LW |
3790 | } |
3791 | EXPORT_SYMBOL_GPL(gpiod_get_array_value); | |
3792 | ||
aca5ce14 | 3793 | /* |
fac9d885 | 3794 | * gpio_set_open_drain_value_commit() - Set the open drain gpio's value. |
79a9becd | 3795 | * @desc: gpio descriptor whose state need to be set. |
20a8a968 | 3796 | * @value: Non-zero for setting it HIGH otherwise it will set to LOW. |
aca5ce14 | 3797 | */ |
fac9d885 | 3798 | static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value) |
aca5ce14 | 3799 | { |
d377f56f | 3800 | int ret = 0; |
fdeb8e15 | 3801 | struct gpio_chip *chip = desc->gdev->chip; |
372e722e AC |
3802 | int offset = gpio_chip_hwgpio(desc); |
3803 | ||
aca5ce14 | 3804 | if (value) { |
d377f56f | 3805 | ret = chip->direction_input(chip, offset); |
aca5ce14 | 3806 | } else { |
d377f56f LW |
3807 | ret = chip->direction_output(chip, offset, 0); |
3808 | if (!ret) | |
372e722e | 3809 | set_bit(FLAG_IS_OUT, &desc->flags); |
aca5ce14 | 3810 | } |
d377f56f LW |
3811 | trace_gpio_direction(desc_to_gpio(desc), value, ret); |
3812 | if (ret < 0) | |
6424de5a MB |
3813 | gpiod_err(desc, |
3814 | "%s: Error in set_value for open drain err %d\n", | |
d377f56f | 3815 | __func__, ret); |
aca5ce14 LD |
3816 | } |
3817 | ||
25553ff0 | 3818 | /* |
79a9becd AC |
3819 | * _gpio_set_open_source_value() - Set the open source gpio's value. |
3820 | * @desc: gpio descriptor whose state need to be set. | |
20a8a968 | 3821 | * @value: Non-zero for setting it HIGH otherwise it will set to LOW. |
25553ff0 | 3822 | */ |
fac9d885 | 3823 | static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value) |
25553ff0 | 3824 | { |
d377f56f | 3825 | int ret = 0; |
fdeb8e15 | 3826 | struct gpio_chip *chip = desc->gdev->chip; |
372e722e AC |
3827 | int offset = gpio_chip_hwgpio(desc); |
3828 | ||
25553ff0 | 3829 | if (value) { |
d377f56f LW |
3830 | ret = chip->direction_output(chip, offset, 1); |
3831 | if (!ret) | |
372e722e | 3832 | set_bit(FLAG_IS_OUT, &desc->flags); |
25553ff0 | 3833 | } else { |
d377f56f | 3834 | ret = chip->direction_input(chip, offset); |
25553ff0 | 3835 | } |
d377f56f LW |
3836 | trace_gpio_direction(desc_to_gpio(desc), !value, ret); |
3837 | if (ret < 0) | |
6424de5a MB |
3838 | gpiod_err(desc, |
3839 | "%s: Error in set_value for open source err %d\n", | |
d377f56f | 3840 | __func__, ret); |
25553ff0 LD |
3841 | } |
3842 | ||
fac9d885 | 3843 | static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value) |
d2876d08 DB |
3844 | { |
3845 | struct gpio_chip *chip; | |
3846 | ||
fdeb8e15 | 3847 | chip = desc->gdev->chip; |
372e722e | 3848 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
02e47980 | 3849 | chip->set(chip, gpio_chip_hwgpio(desc), value); |
372e722e AC |
3850 | } |
3851 | ||
5f424243 RI |
3852 | /* |
3853 | * set multiple outputs on the same chip; | |
3854 | * use the chip's set_multiple function if available; | |
3855 | * otherwise set the outputs sequentially; | |
3856 | * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word | |
3857 | * defines which outputs are to be changed | |
3858 | * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word | |
3859 | * defines the values the outputs specified by mask are to be set to | |
3860 | */ | |
3861 | static void gpio_chip_set_multiple(struct gpio_chip *chip, | |
3862 | unsigned long *mask, unsigned long *bits) | |
3863 | { | |
3864 | if (chip->set_multiple) { | |
3865 | chip->set_multiple(chip, mask, bits); | |
3866 | } else { | |
5e4e6fb3 AS |
3867 | unsigned int i; |
3868 | ||
3869 | /* set outputs if the corresponding mask bit is set */ | |
3870 | for_each_set_bit(i, mask, chip->ngpio) | |
3871 | chip->set(chip, i, test_bit(i, bits)); | |
5f424243 RI |
3872 | } |
3873 | } | |
3874 | ||
3027743f | 3875 | int gpiod_set_array_value_complex(bool raw, bool can_sleep, |
3c940660 GU |
3876 | unsigned int array_size, |
3877 | struct gpio_desc **desc_array, | |
3878 | struct gpio_array *array_info, | |
3879 | unsigned long *value_bitmap) | |
5f424243 RI |
3880 | { |
3881 | int i = 0; | |
3882 | ||
b17566a6 JK |
3883 | /* |
3884 | * Validate array_info against desc_array and its size. | |
3885 | * It should immediately follow desc_array if both | |
3886 | * have been obtained from the same gpiod_get_array() call. | |
3887 | */ | |
3888 | if (array_info && array_info->desc == desc_array && | |
3889 | array_size <= array_info->size && | |
3890 | (void *)array_info == desc_array + array_info->size) { | |
3891 | if (!can_sleep) | |
3892 | WARN_ON(array_info->chip->can_sleep); | |
3893 | ||
3894 | if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) | |
3895 | bitmap_xor(value_bitmap, value_bitmap, | |
3896 | array_info->invert_mask, array_size); | |
3897 | ||
3898 | gpio_chip_set_multiple(array_info->chip, array_info->set_mask, | |
3899 | value_bitmap); | |
3900 | ||
3901 | if (bitmap_full(array_info->set_mask, array_size)) | |
3902 | return 0; | |
3903 | ||
3904 | i = find_first_zero_bit(array_info->set_mask, array_size); | |
3905 | } else { | |
3906 | array_info = NULL; | |
3907 | } | |
3908 | ||
5f424243 | 3909 | while (i < array_size) { |
fdeb8e15 | 3910 | struct gpio_chip *chip = desc_array[i]->gdev->chip; |
3027743f LA |
3911 | unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)]; |
3912 | unsigned long *mask, *bits; | |
5f424243 RI |
3913 | int count = 0; |
3914 | ||
3027743f LA |
3915 | if (likely(chip->ngpio <= FASTPATH_NGPIO)) { |
3916 | mask = fastpath; | |
3917 | } else { | |
3918 | mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio), | |
3919 | sizeof(*mask), | |
3920 | can_sleep ? GFP_KERNEL : GFP_ATOMIC); | |
3921 | if (!mask) | |
3922 | return -ENOMEM; | |
3923 | } | |
3924 | ||
3925 | bits = mask + BITS_TO_LONGS(chip->ngpio); | |
3926 | bitmap_zero(mask, chip->ngpio); | |
3927 | ||
38e003f4 | 3928 | if (!can_sleep) |
5f424243 | 3929 | WARN_ON(chip->can_sleep); |
38e003f4 | 3930 | |
5f424243 RI |
3931 | do { |
3932 | struct gpio_desc *desc = desc_array[i]; | |
3933 | int hwgpio = gpio_chip_hwgpio(desc); | |
b9762beb | 3934 | int value = test_bit(i, value_bitmap); |
5f424243 | 3935 | |
b17566a6 JK |
3936 | /* |
3937 | * Pins applicable for fast input but not for | |
3938 | * fast output processing may have been already | |
3939 | * inverted inside the fast path, skip them. | |
3940 | */ | |
3941 | if (!raw && !(array_info && | |
3942 | test_bit(i, array_info->invert_mask)) && | |
3943 | test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
5f424243 RI |
3944 | value = !value; |
3945 | trace_gpio_value(desc_to_gpio(desc), 0, value); | |
3946 | /* | |
3947 | * collect all normal outputs belonging to the same chip | |
3948 | * open drain and open source outputs are set individually | |
3949 | */ | |
02e47980 | 3950 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) { |
fac9d885 | 3951 | gpio_set_open_drain_value_commit(desc, value); |
02e47980 | 3952 | } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) { |
fac9d885 | 3953 | gpio_set_open_source_value_commit(desc, value); |
5f424243 RI |
3954 | } else { |
3955 | __set_bit(hwgpio, mask); | |
4fc5bfeb | 3956 | __assign_bit(hwgpio, bits, value); |
5f424243 RI |
3957 | count++; |
3958 | } | |
3959 | i++; | |
b17566a6 JK |
3960 | |
3961 | if (array_info) | |
35ae7f96 JK |
3962 | i = find_next_zero_bit(array_info->set_mask, |
3963 | array_size, i); | |
fdeb8e15 LW |
3964 | } while ((i < array_size) && |
3965 | (desc_array[i]->gdev->chip == chip)); | |
5f424243 | 3966 | /* push collected bits to outputs */ |
38e003f4 | 3967 | if (count != 0) |
5f424243 | 3968 | gpio_chip_set_multiple(chip, mask, bits); |
3027743f LA |
3969 | |
3970 | if (mask != fastpath) | |
3971 | kfree(mask); | |
5f424243 | 3972 | } |
3027743f | 3973 | return 0; |
5f424243 RI |
3974 | } |
3975 | ||
d2876d08 | 3976 | /** |
79a9becd AC |
3977 | * gpiod_set_raw_value() - assign a gpio's raw value |
3978 | * @desc: gpio whose value will be assigned | |
d2876d08 | 3979 | * @value: value to assign |
d2876d08 | 3980 | * |
79a9becd AC |
3981 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
3982 | * regard for its ACTIVE_LOW status. | |
3983 | * | |
827a9b8b | 3984 | * This function can be called from contexts where we cannot sleep, and will |
79a9becd | 3985 | * complain if the GPIO chip functions potentially sleep. |
d2876d08 | 3986 | */ |
79a9becd | 3987 | void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
372e722e | 3988 | { |
fdeb8e15 | 3989 | VALIDATE_DESC_VOID(desc); |
3285170f | 3990 | /* Should be using gpiod_set_raw_value_cansleep() */ |
fdeb8e15 | 3991 | WARN_ON(desc->gdev->chip->can_sleep); |
fac9d885 | 3992 | gpiod_set_raw_value_commit(desc, value); |
d2876d08 | 3993 | } |
79a9becd | 3994 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value); |
d2876d08 | 3995 | |
1e77fc82 GU |
3996 | /** |
3997 | * gpiod_set_value_nocheck() - set a GPIO line value without checking | |
3998 | * @desc: the descriptor to set the value on | |
3999 | * @value: value to set | |
4000 | * | |
4001 | * This sets the value of a GPIO line backing a descriptor, applying | |
4002 | * different semantic quirks like active low and open drain/source | |
4003 | * handling. | |
4004 | */ | |
4005 | static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value) | |
4006 | { | |
4007 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
4008 | value = !value; | |
4009 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | |
4010 | gpio_set_open_drain_value_commit(desc, value); | |
4011 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | |
4012 | gpio_set_open_source_value_commit(desc, value); | |
4013 | else | |
4014 | gpiod_set_raw_value_commit(desc, value); | |
4015 | } | |
4016 | ||
d2876d08 | 4017 | /** |
79a9becd AC |
4018 | * gpiod_set_value() - assign a gpio's value |
4019 | * @desc: gpio whose value will be assigned | |
4020 | * @value: value to assign | |
4021 | * | |
02e47980 LW |
4022 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW, |
4023 | * OPEN_DRAIN and OPEN_SOURCE flags into account. | |
d2876d08 | 4024 | * |
827a9b8b | 4025 | * This function can be called from contexts where we cannot sleep, and will |
79a9becd | 4026 | * complain if the GPIO chip functions potentially sleep. |
d2876d08 | 4027 | */ |
79a9becd | 4028 | void gpiod_set_value(struct gpio_desc *desc, int value) |
d2876d08 | 4029 | { |
fdeb8e15 | 4030 | VALIDATE_DESC_VOID(desc); |
3285170f | 4031 | /* Should be using gpiod_set_value_cansleep() */ |
fdeb8e15 | 4032 | WARN_ON(desc->gdev->chip->can_sleep); |
1e77fc82 | 4033 | gpiod_set_value_nocheck(desc, value); |
372e722e | 4034 | } |
79a9becd | 4035 | EXPORT_SYMBOL_GPL(gpiod_set_value); |
d2876d08 | 4036 | |
5f424243 | 4037 | /** |
3fff99bc | 4038 | * gpiod_set_raw_array_value() - assign values to an array of GPIOs |
b9762beb | 4039 | * @array_size: number of elements in the descriptor array / value bitmap |
5f424243 | 4040 | * @desc_array: array of GPIO descriptors whose values will be assigned |
77588c14 | 4041 | * @array_info: information on applicability of fast bitmap processing path |
b9762beb | 4042 | * @value_bitmap: bitmap of values to assign |
5f424243 RI |
4043 | * |
4044 | * Set the raw values of the GPIOs, i.e. the values of the physical lines | |
4045 | * without regard for their ACTIVE_LOW status. | |
4046 | * | |
827a9b8b | 4047 | * This function can be called from contexts where we cannot sleep, and will |
5f424243 RI |
4048 | * complain if the GPIO chip functions potentially sleep. |
4049 | */ | |
3027743f | 4050 | int gpiod_set_raw_array_value(unsigned int array_size, |
3c940660 GU |
4051 | struct gpio_desc **desc_array, |
4052 | struct gpio_array *array_info, | |
4053 | unsigned long *value_bitmap) | |
5f424243 RI |
4054 | { |
4055 | if (!desc_array) | |
3027743f LA |
4056 | return -EINVAL; |
4057 | return gpiod_set_array_value_complex(true, false, array_size, | |
77588c14 | 4058 | desc_array, array_info, value_bitmap); |
5f424243 | 4059 | } |
3fff99bc | 4060 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value); |
5f424243 RI |
4061 | |
4062 | /** | |
3fff99bc | 4063 | * gpiod_set_array_value() - assign values to an array of GPIOs |
b9762beb | 4064 | * @array_size: number of elements in the descriptor array / value bitmap |
5f424243 | 4065 | * @desc_array: array of GPIO descriptors whose values will be assigned |
77588c14 | 4066 | * @array_info: information on applicability of fast bitmap processing path |
b9762beb | 4067 | * @value_bitmap: bitmap of values to assign |
5f424243 RI |
4068 | * |
4069 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status | |
4070 | * into account. | |
4071 | * | |
827a9b8b | 4072 | * This function can be called from contexts where we cannot sleep, and will |
5f424243 RI |
4073 | * complain if the GPIO chip functions potentially sleep. |
4074 | */ | |
cf9af0d5 GU |
4075 | int gpiod_set_array_value(unsigned int array_size, |
4076 | struct gpio_desc **desc_array, | |
4077 | struct gpio_array *array_info, | |
4078 | unsigned long *value_bitmap) | |
5f424243 RI |
4079 | { |
4080 | if (!desc_array) | |
cf9af0d5 GU |
4081 | return -EINVAL; |
4082 | return gpiod_set_array_value_complex(false, false, array_size, | |
4083 | desc_array, array_info, | |
4084 | value_bitmap); | |
5f424243 | 4085 | } |
3fff99bc | 4086 | EXPORT_SYMBOL_GPL(gpiod_set_array_value); |
5f424243 | 4087 | |
d2876d08 | 4088 | /** |
79a9becd AC |
4089 | * gpiod_cansleep() - report whether gpio value access may sleep |
4090 | * @desc: gpio to check | |
d2876d08 | 4091 | * |
d2876d08 | 4092 | */ |
79a9becd | 4093 | int gpiod_cansleep(const struct gpio_desc *desc) |
372e722e | 4094 | { |
fdeb8e15 LW |
4095 | VALIDATE_DESC(desc); |
4096 | return desc->gdev->chip->can_sleep; | |
d2876d08 | 4097 | } |
79a9becd | 4098 | EXPORT_SYMBOL_GPL(gpiod_cansleep); |
d2876d08 | 4099 | |
90b39402 LW |
4100 | /** |
4101 | * gpiod_set_consumer_name() - set the consumer name for the descriptor | |
4102 | * @desc: gpio to set the consumer name on | |
4103 | * @name: the new consumer name | |
4104 | */ | |
18534df4 | 4105 | int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name) |
90b39402 | 4106 | { |
18534df4 MS |
4107 | VALIDATE_DESC(desc); |
4108 | if (name) { | |
4109 | name = kstrdup_const(name, GFP_KERNEL); | |
4110 | if (!name) | |
4111 | return -ENOMEM; | |
4112 | } | |
4113 | ||
4114 | kfree_const(desc->label); | |
4115 | desc_set_label(desc, name); | |
4116 | ||
4117 | return 0; | |
90b39402 LW |
4118 | } |
4119 | EXPORT_SYMBOL_GPL(gpiod_set_consumer_name); | |
4120 | ||
0f6d504e | 4121 | /** |
79a9becd AC |
4122 | * gpiod_to_irq() - return the IRQ corresponding to a GPIO |
4123 | * @desc: gpio whose IRQ will be returned (already requested) | |
0f6d504e | 4124 | * |
79a9becd AC |
4125 | * Return the IRQ corresponding to the passed GPIO, or an error code in case of |
4126 | * error. | |
0f6d504e | 4127 | */ |
79a9becd | 4128 | int gpiod_to_irq(const struct gpio_desc *desc) |
0f6d504e | 4129 | { |
4c37ce86 LW |
4130 | struct gpio_chip *chip; |
4131 | int offset; | |
0f6d504e | 4132 | |
79bb71bd LW |
4133 | /* |
4134 | * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics | |
4135 | * requires this function to not return zero on an invalid descriptor | |
4136 | * but rather a negative error number. | |
4137 | */ | |
bfbbe44d | 4138 | if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip) |
79bb71bd LW |
4139 | return -EINVAL; |
4140 | ||
fdeb8e15 | 4141 | chip = desc->gdev->chip; |
372e722e | 4142 | offset = gpio_chip_hwgpio(desc); |
4c37ce86 LW |
4143 | if (chip->to_irq) { |
4144 | int retirq = chip->to_irq(chip, offset); | |
4145 | ||
4146 | /* Zero means NO_IRQ */ | |
4147 | if (!retirq) | |
4148 | return -ENXIO; | |
4149 | ||
4150 | return retirq; | |
4151 | } | |
4152 | return -ENXIO; | |
0f6d504e | 4153 | } |
79a9becd | 4154 | EXPORT_SYMBOL_GPL(gpiod_to_irq); |
0f6d504e | 4155 | |
d468bf9e | 4156 | /** |
e3a2e878 | 4157 | * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ |
d74be6df AC |
4158 | * @chip: the chip the GPIO to lock belongs to |
4159 | * @offset: the offset of the GPIO to lock as IRQ | |
d468bf9e LW |
4160 | * |
4161 | * This is used directly by GPIO drivers that want to lock down | |
f438acdf | 4162 | * a certain GPIO line to be used for IRQs. |
d468bf9e | 4163 | */ |
e3a2e878 | 4164 | int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset) |
372e722e | 4165 | { |
9c10280d LW |
4166 | struct gpio_desc *desc; |
4167 | ||
4168 | desc = gpiochip_get_desc(chip, offset); | |
4169 | if (IS_ERR(desc)) | |
4170 | return PTR_ERR(desc); | |
4171 | ||
60f8339e LW |
4172 | /* |
4173 | * If it's fast: flush the direction setting if something changed | |
4174 | * behind our back | |
4175 | */ | |
4176 | if (!chip->can_sleep && chip->get_direction) { | |
80956790 | 4177 | int dir = gpiod_get_direction(desc); |
9c10280d | 4178 | |
36b31279 AS |
4179 | if (dir < 0) { |
4180 | chip_err(chip, "%s: cannot get GPIO direction\n", | |
4181 | __func__); | |
4182 | return dir; | |
4183 | } | |
9c10280d | 4184 | } |
d468bf9e | 4185 | |
9c10280d | 4186 | if (test_bit(FLAG_IS_OUT, &desc->flags)) { |
d74be6df | 4187 | chip_err(chip, |
b1911710 AS |
4188 | "%s: tried to flag a GPIO set as output for IRQ\n", |
4189 | __func__); | |
d468bf9e LW |
4190 | return -EIO; |
4191 | } | |
4192 | ||
9c10280d | 4193 | set_bit(FLAG_USED_AS_IRQ, &desc->flags); |
4e9439dd | 4194 | set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); |
3940c34a LW |
4195 | |
4196 | /* | |
4197 | * If the consumer has not set up a label (such as when the | |
4198 | * IRQ is referenced from .to_irq()) we set up a label here | |
4199 | * so it is clear this is used as an interrupt. | |
4200 | */ | |
4201 | if (!desc->label) | |
4202 | desc_set_label(desc, "interrupt"); | |
4203 | ||
d468bf9e | 4204 | return 0; |
372e722e | 4205 | } |
e3a2e878 | 4206 | EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq); |
d2876d08 | 4207 | |
d468bf9e | 4208 | /** |
e3a2e878 | 4209 | * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ |
d74be6df AC |
4210 | * @chip: the chip the GPIO to lock belongs to |
4211 | * @offset: the offset of the GPIO to lock as IRQ | |
d468bf9e LW |
4212 | * |
4213 | * This is used directly by GPIO drivers that want to indicate | |
4214 | * that a certain GPIO is no longer used exclusively for IRQ. | |
d2876d08 | 4215 | */ |
e3a2e878 | 4216 | void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) |
d468bf9e | 4217 | { |
3940c34a LW |
4218 | struct gpio_desc *desc; |
4219 | ||
4220 | desc = gpiochip_get_desc(chip, offset); | |
4221 | if (IS_ERR(desc)) | |
d468bf9e | 4222 | return; |
d2876d08 | 4223 | |
3940c34a | 4224 | clear_bit(FLAG_USED_AS_IRQ, &desc->flags); |
4e9439dd | 4225 | clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); |
3940c34a LW |
4226 | |
4227 | /* If we only had this marking, erase it */ | |
4228 | if (desc->label && !strcmp(desc->label, "interrupt")) | |
4229 | desc_set_label(desc, NULL); | |
d468bf9e | 4230 | } |
e3a2e878 | 4231 | EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq); |
d468bf9e | 4232 | |
4e9439dd HV |
4233 | void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset) |
4234 | { | |
4235 | struct gpio_desc *desc = gpiochip_get_desc(chip, offset); | |
4236 | ||
4237 | if (!IS_ERR(desc) && | |
4238 | !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) | |
4239 | clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); | |
4240 | } | |
4241 | EXPORT_SYMBOL_GPL(gpiochip_disable_irq); | |
4242 | ||
4243 | void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset) | |
4244 | { | |
4245 | struct gpio_desc *desc = gpiochip_get_desc(chip, offset); | |
4246 | ||
4247 | if (!IS_ERR(desc) && | |
4248 | !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) { | |
4249 | WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags)); | |
4250 | set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); | |
4251 | } | |
4252 | } | |
4253 | EXPORT_SYMBOL_GPL(gpiochip_enable_irq); | |
4254 | ||
6cee3821 LW |
4255 | bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset) |
4256 | { | |
4257 | if (offset >= chip->ngpio) | |
4258 | return false; | |
4259 | ||
4260 | return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags); | |
4261 | } | |
4262 | EXPORT_SYMBOL_GPL(gpiochip_line_is_irq); | |
4263 | ||
4e6b8238 HV |
4264 | int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset) |
4265 | { | |
4266 | int ret; | |
4267 | ||
4268 | if (!try_module_get(chip->gpiodev->owner)) | |
4269 | return -ENODEV; | |
4270 | ||
4271 | ret = gpiochip_lock_as_irq(chip, offset); | |
4272 | if (ret) { | |
4273 | chip_err(chip, "unable to lock HW IRQ %u for IRQ\n", offset); | |
4274 | module_put(chip->gpiodev->owner); | |
4275 | return ret; | |
4276 | } | |
4277 | return 0; | |
4278 | } | |
4279 | EXPORT_SYMBOL_GPL(gpiochip_reqres_irq); | |
4280 | ||
4281 | void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset) | |
4282 | { | |
4283 | gpiochip_unlock_as_irq(chip, offset); | |
4284 | module_put(chip->gpiodev->owner); | |
4285 | } | |
4286 | EXPORT_SYMBOL_GPL(gpiochip_relres_irq); | |
4287 | ||
143b65d6 LW |
4288 | bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset) |
4289 | { | |
4290 | if (offset >= chip->ngpio) | |
4291 | return false; | |
4292 | ||
4293 | return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags); | |
4294 | } | |
4295 | EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain); | |
4296 | ||
4297 | bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset) | |
4298 | { | |
4299 | if (offset >= chip->ngpio) | |
4300 | return false; | |
4301 | ||
4302 | return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags); | |
4303 | } | |
4304 | EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source); | |
4305 | ||
05f479bf CK |
4306 | bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset) |
4307 | { | |
4308 | if (offset >= chip->ngpio) | |
4309 | return false; | |
4310 | ||
e10f72bf | 4311 | return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags); |
05f479bf CK |
4312 | } |
4313 | EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent); | |
4314 | ||
79a9becd AC |
4315 | /** |
4316 | * gpiod_get_raw_value_cansleep() - return a gpio's raw value | |
4317 | * @desc: gpio whose value will be returned | |
4318 | * | |
4319 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding | |
e20538b8 | 4320 | * its ACTIVE_LOW status, or negative errno on failure. |
79a9becd AC |
4321 | * |
4322 | * This function is to be called from contexts that can sleep. | |
d2876d08 | 4323 | */ |
79a9becd | 4324 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) |
d2876d08 | 4325 | { |
d2876d08 | 4326 | might_sleep_if(extra_checks); |
fdeb8e15 | 4327 | VALIDATE_DESC(desc); |
fac9d885 | 4328 | return gpiod_get_raw_value_commit(desc); |
d2876d08 | 4329 | } |
79a9becd | 4330 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); |
372e722e | 4331 | |
79a9becd AC |
4332 | /** |
4333 | * gpiod_get_value_cansleep() - return a gpio's value | |
4334 | * @desc: gpio whose value will be returned | |
4335 | * | |
4336 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into | |
e20538b8 | 4337 | * account, or negative errno on failure. |
79a9becd AC |
4338 | * |
4339 | * This function is to be called from contexts that can sleep. | |
4340 | */ | |
4341 | int gpiod_get_value_cansleep(const struct gpio_desc *desc) | |
d2876d08 | 4342 | { |
3f397c21 | 4343 | int value; |
d2876d08 DB |
4344 | |
4345 | might_sleep_if(extra_checks); | |
fdeb8e15 | 4346 | VALIDATE_DESC(desc); |
fac9d885 | 4347 | value = gpiod_get_raw_value_commit(desc); |
e20538b8 BA |
4348 | if (value < 0) |
4349 | return value; | |
4350 | ||
79a9becd AC |
4351 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
4352 | value = !value; | |
4353 | ||
3f397c21 | 4354 | return value; |
d2876d08 | 4355 | } |
79a9becd | 4356 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); |
372e722e | 4357 | |
eec1d566 LW |
4358 | /** |
4359 | * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs | |
b9762beb | 4360 | * @array_size: number of elements in the descriptor array / value bitmap |
eec1d566 | 4361 | * @desc_array: array of GPIO descriptors whose values will be read |
77588c14 | 4362 | * @array_info: information on applicability of fast bitmap processing path |
b9762beb | 4363 | * @value_bitmap: bitmap to store the read values |
eec1d566 LW |
4364 | * |
4365 | * Read the raw values of the GPIOs, i.e. the values of the physical lines | |
4366 | * without regard for their ACTIVE_LOW status. Return 0 in case of success, | |
4367 | * else an error code. | |
4368 | * | |
4369 | * This function is to be called from contexts that can sleep. | |
4370 | */ | |
4371 | int gpiod_get_raw_array_value_cansleep(unsigned int array_size, | |
4372 | struct gpio_desc **desc_array, | |
77588c14 | 4373 | struct gpio_array *array_info, |
b9762beb | 4374 | unsigned long *value_bitmap) |
eec1d566 LW |
4375 | { |
4376 | might_sleep_if(extra_checks); | |
4377 | if (!desc_array) | |
4378 | return -EINVAL; | |
4379 | return gpiod_get_array_value_complex(true, true, array_size, | |
77588c14 JK |
4380 | desc_array, array_info, |
4381 | value_bitmap); | |
eec1d566 LW |
4382 | } |
4383 | EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep); | |
4384 | ||
4385 | /** | |
4386 | * gpiod_get_array_value_cansleep() - read values from an array of GPIOs | |
b9762beb | 4387 | * @array_size: number of elements in the descriptor array / value bitmap |
eec1d566 | 4388 | * @desc_array: array of GPIO descriptors whose values will be read |
77588c14 | 4389 | * @array_info: information on applicability of fast bitmap processing path |
b9762beb | 4390 | * @value_bitmap: bitmap to store the read values |
eec1d566 LW |
4391 | * |
4392 | * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status | |
4393 | * into account. Return 0 in case of success, else an error code. | |
4394 | * | |
4395 | * This function is to be called from contexts that can sleep. | |
4396 | */ | |
4397 | int gpiod_get_array_value_cansleep(unsigned int array_size, | |
4398 | struct gpio_desc **desc_array, | |
77588c14 | 4399 | struct gpio_array *array_info, |
b9762beb | 4400 | unsigned long *value_bitmap) |
eec1d566 LW |
4401 | { |
4402 | might_sleep_if(extra_checks); | |
4403 | if (!desc_array) | |
4404 | return -EINVAL; | |
4405 | return gpiod_get_array_value_complex(false, true, array_size, | |
77588c14 JK |
4406 | desc_array, array_info, |
4407 | value_bitmap); | |
eec1d566 LW |
4408 | } |
4409 | EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep); | |
4410 | ||
79a9becd AC |
4411 | /** |
4412 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value | |
4413 | * @desc: gpio whose value will be assigned | |
4414 | * @value: value to assign | |
4415 | * | |
4416 | * Set the raw value of the GPIO, i.e. the value of its physical line without | |
4417 | * regard for its ACTIVE_LOW status. | |
4418 | * | |
4419 | * This function is to be called from contexts that can sleep. | |
4420 | */ | |
4421 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) | |
372e722e | 4422 | { |
d2876d08 | 4423 | might_sleep_if(extra_checks); |
fdeb8e15 | 4424 | VALIDATE_DESC_VOID(desc); |
fac9d885 | 4425 | gpiod_set_raw_value_commit(desc, value); |
372e722e | 4426 | } |
79a9becd | 4427 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); |
d2876d08 | 4428 | |
79a9becd AC |
4429 | /** |
4430 | * gpiod_set_value_cansleep() - assign a gpio's value | |
4431 | * @desc: gpio whose value will be assigned | |
4432 | * @value: value to assign | |
4433 | * | |
4434 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | |
4435 | * account | |
4436 | * | |
4437 | * This function is to be called from contexts that can sleep. | |
4438 | */ | |
4439 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) | |
d2876d08 | 4440 | { |
d2876d08 | 4441 | might_sleep_if(extra_checks); |
fdeb8e15 | 4442 | VALIDATE_DESC_VOID(desc); |
1e77fc82 | 4443 | gpiod_set_value_nocheck(desc, value); |
372e722e | 4444 | } |
79a9becd | 4445 | EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); |
d2876d08 | 4446 | |
5f424243 | 4447 | /** |
3fff99bc | 4448 | * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs |
b9762beb | 4449 | * @array_size: number of elements in the descriptor array / value bitmap |
5f424243 | 4450 | * @desc_array: array of GPIO descriptors whose values will be assigned |
77588c14 | 4451 | * @array_info: information on applicability of fast bitmap processing path |
b9762beb | 4452 | * @value_bitmap: bitmap of values to assign |
5f424243 RI |
4453 | * |
4454 | * Set the raw values of the GPIOs, i.e. the values of the physical lines | |
4455 | * without regard for their ACTIVE_LOW status. | |
4456 | * | |
4457 | * This function is to be called from contexts that can sleep. | |
4458 | */ | |
3027743f | 4459 | int gpiod_set_raw_array_value_cansleep(unsigned int array_size, |
3c940660 GU |
4460 | struct gpio_desc **desc_array, |
4461 | struct gpio_array *array_info, | |
4462 | unsigned long *value_bitmap) | |
5f424243 RI |
4463 | { |
4464 | might_sleep_if(extra_checks); | |
4465 | if (!desc_array) | |
3027743f LA |
4466 | return -EINVAL; |
4467 | return gpiod_set_array_value_complex(true, true, array_size, desc_array, | |
77588c14 | 4468 | array_info, value_bitmap); |
5f424243 | 4469 | } |
3fff99bc | 4470 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep); |
5f424243 | 4471 | |
3946d187 DT |
4472 | /** |
4473 | * gpiod_add_lookup_tables() - register GPIO device consumers | |
4474 | * @tables: list of tables of consumers to register | |
4475 | * @n: number of tables in the list | |
4476 | */ | |
4477 | void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n) | |
4478 | { | |
4479 | unsigned int i; | |
4480 | ||
4481 | mutex_lock(&gpio_lookup_lock); | |
4482 | ||
4483 | for (i = 0; i < n; i++) | |
4484 | list_add_tail(&tables[i]->list, &gpio_lookup_list); | |
4485 | ||
4486 | mutex_unlock(&gpio_lookup_lock); | |
4487 | } | |
4488 | ||
5f424243 | 4489 | /** |
3fff99bc | 4490 | * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs |
b9762beb | 4491 | * @array_size: number of elements in the descriptor array / value bitmap |
5f424243 | 4492 | * @desc_array: array of GPIO descriptors whose values will be assigned |
77588c14 | 4493 | * @array_info: information on applicability of fast bitmap processing path |
b9762beb | 4494 | * @value_bitmap: bitmap of values to assign |
5f424243 RI |
4495 | * |
4496 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status | |
4497 | * into account. | |
4498 | * | |
4499 | * This function is to be called from contexts that can sleep. | |
4500 | */ | |
cf9af0d5 GU |
4501 | int gpiod_set_array_value_cansleep(unsigned int array_size, |
4502 | struct gpio_desc **desc_array, | |
4503 | struct gpio_array *array_info, | |
4504 | unsigned long *value_bitmap) | |
5f424243 RI |
4505 | { |
4506 | might_sleep_if(extra_checks); | |
4507 | if (!desc_array) | |
cf9af0d5 GU |
4508 | return -EINVAL; |
4509 | return gpiod_set_array_value_complex(false, true, array_size, | |
4510 | desc_array, array_info, | |
4511 | value_bitmap); | |
5f424243 | 4512 | } |
3fff99bc | 4513 | EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep); |
5f424243 | 4514 | |
bae48da2 | 4515 | /** |
ad824783 AC |
4516 | * gpiod_add_lookup_table() - register GPIO device consumers |
4517 | * @table: table of consumers to register | |
bae48da2 | 4518 | */ |
ad824783 | 4519 | void gpiod_add_lookup_table(struct gpiod_lookup_table *table) |
bae48da2 AC |
4520 | { |
4521 | mutex_lock(&gpio_lookup_lock); | |
4522 | ||
ad824783 | 4523 | list_add_tail(&table->list, &gpio_lookup_list); |
bae48da2 AC |
4524 | |
4525 | mutex_unlock(&gpio_lookup_lock); | |
4526 | } | |
226b2242 | 4527 | EXPORT_SYMBOL_GPL(gpiod_add_lookup_table); |
bae48da2 | 4528 | |
be9015ab SK |
4529 | /** |
4530 | * gpiod_remove_lookup_table() - unregister GPIO device consumers | |
4531 | * @table: table of consumers to unregister | |
4532 | */ | |
4533 | void gpiod_remove_lookup_table(struct gpiod_lookup_table *table) | |
4534 | { | |
4535 | mutex_lock(&gpio_lookup_lock); | |
4536 | ||
4537 | list_del(&table->list); | |
4538 | ||
4539 | mutex_unlock(&gpio_lookup_lock); | |
4540 | } | |
226b2242 | 4541 | EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table); |
be9015ab | 4542 | |
a411e81e BG |
4543 | /** |
4544 | * gpiod_add_hogs() - register a set of GPIO hogs from machine code | |
4545 | * @hogs: table of gpio hog entries with a zeroed sentinel at the end | |
4546 | */ | |
4547 | void gpiod_add_hogs(struct gpiod_hog *hogs) | |
4548 | { | |
4549 | struct gpio_chip *chip; | |
4550 | struct gpiod_hog *hog; | |
4551 | ||
4552 | mutex_lock(&gpio_machine_hogs_mutex); | |
4553 | ||
4554 | for (hog = &hogs[0]; hog->chip_label; hog++) { | |
4555 | list_add_tail(&hog->list, &gpio_machine_hogs); | |
4556 | ||
4557 | /* | |
4558 | * The chip may have been registered earlier, so check if it | |
4559 | * exists and, if so, try to hog the line now. | |
4560 | */ | |
4561 | chip = find_chip_by_name(hog->chip_label); | |
4562 | if (chip) | |
4563 | gpiochip_machine_hog(chip, hog); | |
4564 | } | |
4565 | ||
4566 | mutex_unlock(&gpio_machine_hogs_mutex); | |
4567 | } | |
4568 | EXPORT_SYMBOL_GPL(gpiod_add_hogs); | |
4569 | ||
ad824783 | 4570 | static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) |
bae48da2 AC |
4571 | { |
4572 | const char *dev_id = dev ? dev_name(dev) : NULL; | |
ad824783 | 4573 | struct gpiod_lookup_table *table; |
bae48da2 AC |
4574 | |
4575 | mutex_lock(&gpio_lookup_lock); | |
4576 | ||
ad824783 AC |
4577 | list_for_each_entry(table, &gpio_lookup_list, list) { |
4578 | if (table->dev_id && dev_id) { | |
4579 | /* | |
4580 | * Valid strings on both ends, must be identical to have | |
4581 | * a match | |
4582 | */ | |
4583 | if (!strcmp(table->dev_id, dev_id)) | |
4584 | goto found; | |
4585 | } else { | |
4586 | /* | |
4587 | * One of the pointers is NULL, so both must be to have | |
4588 | * a match | |
4589 | */ | |
4590 | if (dev_id == table->dev_id) | |
4591 | goto found; | |
4592 | } | |
4593 | } | |
4594 | table = NULL; | |
bae48da2 | 4595 | |
ad824783 AC |
4596 | found: |
4597 | mutex_unlock(&gpio_lookup_lock); | |
4598 | return table; | |
4599 | } | |
bae48da2 | 4600 | |
ad824783 | 4601 | static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, |
fed7026a | 4602 | unsigned int idx, unsigned long *flags) |
ad824783 | 4603 | { |
2a3cf6a3 | 4604 | struct gpio_desc *desc = ERR_PTR(-ENOENT); |
ad824783 AC |
4605 | struct gpiod_lookup_table *table; |
4606 | struct gpiod_lookup *p; | |
bae48da2 | 4607 | |
ad824783 AC |
4608 | table = gpiod_find_lookup_table(dev); |
4609 | if (!table) | |
4610 | return desc; | |
bae48da2 | 4611 | |
ad824783 AC |
4612 | for (p = &table->table[0]; p->chip_label; p++) { |
4613 | struct gpio_chip *chip; | |
bae48da2 | 4614 | |
ad824783 | 4615 | /* idx must always match exactly */ |
bae48da2 AC |
4616 | if (p->idx != idx) |
4617 | continue; | |
4618 | ||
ad824783 AC |
4619 | /* If the lookup entry has a con_id, require exact match */ |
4620 | if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) | |
4621 | continue; | |
bae48da2 | 4622 | |
ad824783 | 4623 | chip = find_chip_by_name(p->chip_label); |
bae48da2 | 4624 | |
ad824783 | 4625 | if (!chip) { |
8853daf3 JK |
4626 | /* |
4627 | * As the lookup table indicates a chip with | |
4628 | * p->chip_label should exist, assume it may | |
4629 | * still appear later and let the interested | |
4630 | * consumer be probed again or let the Deferred | |
4631 | * Probe infrastructure handle the error. | |
4632 | */ | |
4633 | dev_warn(dev, "cannot find GPIO chip %s, deferring\n", | |
4634 | p->chip_label); | |
4635 | return ERR_PTR(-EPROBE_DEFER); | |
ad824783 | 4636 | } |
bae48da2 | 4637 | |
ad824783 | 4638 | if (chip->ngpio <= p->chip_hwnum) { |
2a3cf6a3 | 4639 | dev_err(dev, |
d935bd50 GU |
4640 | "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n", |
4641 | idx, p->chip_hwnum, chip->ngpio - 1, | |
4642 | chip->label); | |
2a3cf6a3 | 4643 | return ERR_PTR(-EINVAL); |
bae48da2 | 4644 | } |
bae48da2 | 4645 | |
bb1e88cc | 4646 | desc = gpiochip_get_desc(chip, p->chip_hwnum); |
ad824783 | 4647 | *flags = p->flags; |
bae48da2 | 4648 | |
2a3cf6a3 | 4649 | return desc; |
bae48da2 AC |
4650 | } |
4651 | ||
bae48da2 AC |
4652 | return desc; |
4653 | } | |
4654 | ||
66858527 RI |
4655 | static int platform_gpio_count(struct device *dev, const char *con_id) |
4656 | { | |
4657 | struct gpiod_lookup_table *table; | |
4658 | struct gpiod_lookup *p; | |
4659 | unsigned int count = 0; | |
4660 | ||
4661 | table = gpiod_find_lookup_table(dev); | |
4662 | if (!table) | |
4663 | return -ENOENT; | |
4664 | ||
4665 | for (p = &table->table[0]; p->chip_label; p++) { | |
4666 | if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || | |
4667 | (!con_id && !p->con_id)) | |
4668 | count++; | |
4669 | } | |
4670 | if (!count) | |
4671 | return -ENOENT; | |
4672 | ||
4673 | return count; | |
4674 | } | |
4675 | ||
13949fa9 DT |
4676 | /** |
4677 | * fwnode_gpiod_get_index - obtain a GPIO from firmware node | |
4678 | * @fwnode: handle of the firmware node | |
4679 | * @con_id: function within the GPIO consumer | |
4680 | * @index: index of the GPIO to obtain for the consumer | |
4681 | * @flags: GPIO initialization flags | |
4682 | * @label: label to attach to the requested GPIO | |
4683 | * | |
4684 | * This function can be used for drivers that get their configuration | |
4685 | * from opaque firmware. | |
4686 | * | |
4687 | * The function properly finds the corresponding GPIO using whatever is the | |
4688 | * underlying firmware interface and then makes sure that the GPIO | |
4689 | * descriptor is requested before it is returned to the caller. | |
4690 | * | |
4691 | * Returns: | |
4692 | * On successful request the GPIO pin is configured in accordance with | |
4693 | * provided @flags. | |
4694 | * | |
4695 | * In case of error an ERR_PTR() is returned. | |
4696 | */ | |
4697 | struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, | |
4698 | const char *con_id, int index, | |
4699 | enum gpiod_flags flags, | |
4700 | const char *label) | |
4701 | { | |
4702 | struct gpio_desc *desc; | |
4703 | char prop_name[32]; /* 32 is max size of property name */ | |
4704 | unsigned int i; | |
4705 | ||
4706 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { | |
4707 | if (con_id) | |
4708 | snprintf(prop_name, sizeof(prop_name), "%s-%s", | |
4709 | con_id, gpio_suffixes[i]); | |
4710 | else | |
4711 | snprintf(prop_name, sizeof(prop_name), "%s", | |
4712 | gpio_suffixes[i]); | |
4713 | ||
4714 | desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags, | |
4715 | label); | |
4716 | if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT)) | |
4717 | break; | |
4718 | } | |
4719 | ||
4720 | return desc; | |
4721 | } | |
4722 | EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index); | |
4723 | ||
66858527 RI |
4724 | /** |
4725 | * gpiod_count - return the number of GPIOs associated with a device / function | |
4726 | * or -ENOENT if no GPIO has been assigned to the requested function | |
4727 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
4728 | * @con_id: function within the GPIO consumer | |
4729 | */ | |
4730 | int gpiod_count(struct device *dev, const char *con_id) | |
4731 | { | |
4732 | int count = -ENOENT; | |
4733 | ||
4734 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) | |
f626d6df | 4735 | count = of_gpio_get_count(dev, con_id); |
66858527 RI |
4736 | else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) |
4737 | count = acpi_gpio_count(dev, con_id); | |
4738 | ||
4739 | if (count < 0) | |
4740 | count = platform_gpio_count(dev, con_id); | |
4741 | ||
4742 | return count; | |
4743 | } | |
4744 | EXPORT_SYMBOL_GPL(gpiod_count); | |
4745 | ||
bae48da2 | 4746 | /** |
0879162f | 4747 | * gpiod_get - obtain a GPIO for a given GPIO function |
ad824783 | 4748 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
bae48da2 | 4749 | * @con_id: function within the GPIO consumer |
39b2bbe3 | 4750 | * @flags: optional GPIO initialization flags |
bae48da2 AC |
4751 | * |
4752 | * Return the GPIO descriptor corresponding to the function con_id of device | |
2a3cf6a3 | 4753 | * dev, -ENOENT if no GPIO has been assigned to the requested function, or |
20a8a968 | 4754 | * another IS_ERR() code if an error occurred while trying to acquire the GPIO. |
bae48da2 | 4755 | */ |
b17d1bf1 | 4756 | struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id, |
39b2bbe3 | 4757 | enum gpiod_flags flags) |
bae48da2 | 4758 | { |
39b2bbe3 | 4759 | return gpiod_get_index(dev, con_id, 0, flags); |
bae48da2 | 4760 | } |
b17d1bf1 | 4761 | EXPORT_SYMBOL_GPL(gpiod_get); |
bae48da2 | 4762 | |
29a1f233 TR |
4763 | /** |
4764 | * gpiod_get_optional - obtain an optional GPIO for a given GPIO function | |
4765 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
4766 | * @con_id: function within the GPIO consumer | |
39b2bbe3 | 4767 | * @flags: optional GPIO initialization flags |
29a1f233 TR |
4768 | * |
4769 | * This is equivalent to gpiod_get(), except that when no GPIO was assigned to | |
4770 | * the requested function it will return NULL. This is convenient for drivers | |
4771 | * that need to handle optional GPIOs. | |
4772 | */ | |
b17d1bf1 | 4773 | struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, |
39b2bbe3 AC |
4774 | const char *con_id, |
4775 | enum gpiod_flags flags) | |
29a1f233 | 4776 | { |
39b2bbe3 | 4777 | return gpiod_get_index_optional(dev, con_id, 0, flags); |
29a1f233 | 4778 | } |
b17d1bf1 | 4779 | EXPORT_SYMBOL_GPL(gpiod_get_optional); |
29a1f233 | 4780 | |
f625d460 BP |
4781 | |
4782 | /** | |
4783 | * gpiod_configure_flags - helper function to configure a given GPIO | |
4784 | * @desc: gpio whose value will be assigned | |
4785 | * @con_id: function within the GPIO consumer | |
fed7026a AS |
4786 | * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from |
4787 | * of_find_gpio() or of_get_gpio_hog() | |
f625d460 BP |
4788 | * @dflags: gpiod_flags - optional GPIO initialization flags |
4789 | * | |
4790 | * Return 0 on success, -ENOENT if no GPIO has been assigned to the | |
4791 | * requested function and/or index, or another IS_ERR() code if an error | |
4792 | * occurred while trying to acquire the GPIO. | |
4793 | */ | |
c29fd9eb | 4794 | int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, |
85b03b30 | 4795 | unsigned long lflags, enum gpiod_flags dflags) |
f625d460 | 4796 | { |
d377f56f | 4797 | int ret; |
f625d460 | 4798 | |
85b03b30 JH |
4799 | if (lflags & GPIO_ACTIVE_LOW) |
4800 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
f926dfc1 | 4801 | |
85b03b30 JH |
4802 | if (lflags & GPIO_OPEN_DRAIN) |
4803 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); | |
f926dfc1 LW |
4804 | else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) { |
4805 | /* | |
4806 | * This enforces open drain mode from the consumer side. | |
4807 | * This is necessary for some busses like I2C, but the lookup | |
4808 | * should *REALLY* have specified them as open drain in the | |
4809 | * first place, so print a little warning here. | |
4810 | */ | |
4811 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); | |
4812 | gpiod_warn(desc, | |
4813 | "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n"); | |
4814 | } | |
4815 | ||
85b03b30 JH |
4816 | if (lflags & GPIO_OPEN_SOURCE) |
4817 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); | |
e10f72bf | 4818 | |
d449991c TP |
4819 | if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) { |
4820 | gpiod_err(desc, | |
4821 | "both pull-up and pull-down enabled, invalid configuration\n"); | |
4822 | return -EINVAL; | |
4823 | } | |
4824 | ||
4825 | if (lflags & GPIO_PULL_UP) | |
4826 | set_bit(FLAG_PULL_UP, &desc->flags); | |
4827 | else if (lflags & GPIO_PULL_DOWN) | |
4828 | set_bit(FLAG_PULL_DOWN, &desc->flags); | |
4829 | ||
d377f56f LW |
4830 | ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY)); |
4831 | if (ret < 0) | |
4832 | return ret; | |
85b03b30 | 4833 | |
f625d460 BP |
4834 | /* No particular flag request, return here... */ |
4835 | if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) { | |
4836 | pr_debug("no flags found for %s\n", con_id); | |
4837 | return 0; | |
4838 | } | |
4839 | ||
4840 | /* Process flags */ | |
4841 | if (dflags & GPIOD_FLAGS_BIT_DIR_OUT) | |
d377f56f | 4842 | ret = gpiod_direction_output(desc, |
ad17731d | 4843 | !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL)); |
f625d460 | 4844 | else |
d377f56f | 4845 | ret = gpiod_direction_input(desc); |
f625d460 | 4846 | |
d377f56f | 4847 | return ret; |
f625d460 BP |
4848 | } |
4849 | ||
bae48da2 AC |
4850 | /** |
4851 | * gpiod_get_index - obtain a GPIO from a multi-index GPIO function | |
fdd6a5fe | 4852 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
bae48da2 AC |
4853 | * @con_id: function within the GPIO consumer |
4854 | * @idx: index of the GPIO to obtain in the consumer | |
39b2bbe3 | 4855 | * @flags: optional GPIO initialization flags |
bae48da2 AC |
4856 | * |
4857 | * This variant of gpiod_get() allows to access GPIOs other than the first | |
4858 | * defined one for functions that define several GPIOs. | |
4859 | * | |
2a3cf6a3 AC |
4860 | * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the |
4861 | * requested function and/or index, or another IS_ERR() code if an error | |
20a8a968 | 4862 | * occurred while trying to acquire the GPIO. |
bae48da2 | 4863 | */ |
b17d1bf1 | 4864 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
bae48da2 | 4865 | const char *con_id, |
39b2bbe3 AC |
4866 | unsigned int idx, |
4867 | enum gpiod_flags flags) | |
bae48da2 | 4868 | { |
2d6c06f5 | 4869 | unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT; |
35c5d7fd | 4870 | struct gpio_desc *desc = NULL; |
d377f56f | 4871 | int ret; |
7d18f0a1 LW |
4872 | /* Maybe we have a device name, maybe not */ |
4873 | const char *devname = dev ? dev_name(dev) : "?"; | |
bae48da2 AC |
4874 | |
4875 | dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); | |
4876 | ||
4d8440b9 RW |
4877 | if (dev) { |
4878 | /* Using device tree? */ | |
4879 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) { | |
4880 | dev_dbg(dev, "using device tree for GPIO lookup\n"); | |
4881 | desc = of_find_gpio(dev, con_id, idx, &lookupflags); | |
4882 | } else if (ACPI_COMPANION(dev)) { | |
4883 | dev_dbg(dev, "using ACPI for GPIO lookup\n"); | |
a31f5c3a | 4884 | desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags); |
4d8440b9 | 4885 | } |
35c5d7fd AC |
4886 | } |
4887 | ||
4888 | /* | |
4889 | * Either we are not using DT or ACPI, or their lookup did not return | |
4890 | * a result. In that case, use platform lookup as a fallback. | |
4891 | */ | |
2a3cf6a3 | 4892 | if (!desc || desc == ERR_PTR(-ENOENT)) { |
43a8785a | 4893 | dev_dbg(dev, "using lookup tables for GPIO lookup\n"); |
39b2bbe3 | 4894 | desc = gpiod_find(dev, con_id, idx, &lookupflags); |
bae48da2 AC |
4895 | } |
4896 | ||
4897 | if (IS_ERR(desc)) { | |
9d5a1f2c | 4898 | dev_dbg(dev, "No GPIO consumer %s found\n", con_id); |
bae48da2 AC |
4899 | return desc; |
4900 | } | |
4901 | ||
7d18f0a1 LW |
4902 | /* |
4903 | * If a connection label was passed use that, else attempt to use | |
4904 | * the device name as label | |
4905 | */ | |
d377f56f LW |
4906 | ret = gpiod_request(desc, con_id ? con_id : devname); |
4907 | if (ret < 0) { | |
4908 | if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) { | |
b0ce7b29 LW |
4909 | /* |
4910 | * This happens when there are several consumers for | |
4911 | * the same GPIO line: we just return here without | |
4912 | * further initialization. It is a bit if a hack. | |
4913 | * This is necessary to support fixed regulators. | |
4914 | * | |
4915 | * FIXME: Make this more sane and safe. | |
4916 | */ | |
4917 | dev_info(dev, "nonexclusive access to GPIO for %s\n", | |
4918 | con_id ? con_id : devname); | |
4919 | return desc; | |
4920 | } else { | |
d377f56f | 4921 | return ERR_PTR(ret); |
b0ce7b29 LW |
4922 | } |
4923 | } | |
bae48da2 | 4924 | |
d377f56f | 4925 | ret = gpiod_configure_flags(desc, con_id, lookupflags, flags); |
6392cca4 | 4926 | if (ret < 0) { |
39b2bbe3 | 4927 | dev_dbg(dev, "setup of GPIO %s failed\n", con_id); |
6392cca4 LW |
4928 | gpiod_put(desc); |
4929 | return ERR_PTR(ret); | |
4930 | } | |
4931 | ||
4932 | return desc; | |
4933 | } | |
b17d1bf1 | 4934 | EXPORT_SYMBOL_GPL(gpiod_get_index); |
6392cca4 | 4935 | |
40b73183 MW |
4936 | /** |
4937 | * fwnode_get_named_gpiod - obtain a GPIO from firmware node | |
4938 | * @fwnode: handle of the firmware node | |
4939 | * @propname: name of the firmware property representing the GPIO | |
6392cca4 | 4940 | * @index: index of the GPIO to obtain for the consumer |
a264d10f | 4941 | * @dflags: GPIO initialization flags |
950d55f5 | 4942 | * @label: label to attach to the requested GPIO |
40b73183 MW |
4943 | * |
4944 | * This function can be used for drivers that get their configuration | |
6392cca4 | 4945 | * from opaque firmware. |
40b73183 | 4946 | * |
6392cca4 | 4947 | * The function properly finds the corresponding GPIO using whatever is the |
40b73183 MW |
4948 | * underlying firmware interface and then makes sure that the GPIO |
4949 | * descriptor is requested before it is returned to the caller. | |
4950 | * | |
950d55f5 | 4951 | * Returns: |
ff21378a | 4952 | * On successful request the GPIO pin is configured in accordance with |
a264d10f AS |
4953 | * provided @dflags. |
4954 | * | |
40b73183 MW |
4955 | * In case of error an ERR_PTR() is returned. |
4956 | */ | |
4957 | struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, | |
537b94da | 4958 | const char *propname, int index, |
b2987d74 AS |
4959 | enum gpiod_flags dflags, |
4960 | const char *label) | |
40b73183 | 4961 | { |
2d6c06f5 | 4962 | unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT; |
40b73183 | 4963 | struct gpio_desc *desc = ERR_PTR(-ENODEV); |
40b73183 MW |
4964 | int ret; |
4965 | ||
4966 | if (!fwnode) | |
4967 | return ERR_PTR(-EINVAL); | |
4968 | ||
4969 | if (is_of_node(fwnode)) { | |
6392cca4 LW |
4970 | desc = gpiod_get_from_of_node(to_of_node(fwnode), |
4971 | propname, index, | |
4972 | dflags, | |
4973 | label); | |
4974 | return desc; | |
40b73183 MW |
4975 | } else if (is_acpi_node(fwnode)) { |
4976 | struct acpi_gpio_info info; | |
4977 | ||
537b94da | 4978 | desc = acpi_node_get_gpiod(fwnode, propname, index, &info); |
6392cca4 LW |
4979 | if (IS_ERR(desc)) |
4980 | return desc; | |
40b73183 | 4981 | |
6392cca4 | 4982 | acpi_gpio_update_gpiod_flags(&dflags, &info); |
606be344 | 4983 | acpi_gpio_update_gpiod_lookup_flags(&lflags, &info); |
6392cca4 | 4984 | } |
40b73183 | 4985 | |
6392cca4 | 4986 | /* Currently only ACPI takes this path */ |
b2987d74 | 4987 | ret = gpiod_request(desc, label); |
85b03b30 JH |
4988 | if (ret) |
4989 | return ERR_PTR(ret); | |
4990 | ||
a264d10f AS |
4991 | ret = gpiod_configure_flags(desc, propname, lflags, dflags); |
4992 | if (ret < 0) { | |
4993 | gpiod_put(desc); | |
4994 | return ERR_PTR(ret); | |
90b665f6 LP |
4995 | } |
4996 | ||
40b73183 MW |
4997 | return desc; |
4998 | } | |
4999 | EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod); | |
5000 | ||
29a1f233 TR |
5001 | /** |
5002 | * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO | |
5003 | * function | |
5004 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
5005 | * @con_id: function within the GPIO consumer | |
5006 | * @index: index of the GPIO to obtain in the consumer | |
39b2bbe3 | 5007 | * @flags: optional GPIO initialization flags |
29a1f233 TR |
5008 | * |
5009 | * This is equivalent to gpiod_get_index(), except that when no GPIO with the | |
5010 | * specified index was assigned to the requested function it will return NULL. | |
5011 | * This is convenient for drivers that need to handle optional GPIOs. | |
5012 | */ | |
b17d1bf1 | 5013 | struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, |
29a1f233 | 5014 | const char *con_id, |
39b2bbe3 AC |
5015 | unsigned int index, |
5016 | enum gpiod_flags flags) | |
29a1f233 TR |
5017 | { |
5018 | struct gpio_desc *desc; | |
5019 | ||
39b2bbe3 | 5020 | desc = gpiod_get_index(dev, con_id, index, flags); |
29a1f233 TR |
5021 | if (IS_ERR(desc)) { |
5022 | if (PTR_ERR(desc) == -ENOENT) | |
5023 | return NULL; | |
5024 | } | |
5025 | ||
5026 | return desc; | |
5027 | } | |
b17d1bf1 | 5028 | EXPORT_SYMBOL_GPL(gpiod_get_index_optional); |
29a1f233 | 5029 | |
f625d460 BP |
5030 | /** |
5031 | * gpiod_hog - Hog the specified GPIO desc given the provided flags | |
5032 | * @desc: gpio whose value will be assigned | |
5033 | * @name: gpio line name | |
fed7026a AS |
5034 | * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from |
5035 | * of_find_gpio() or of_get_gpio_hog() | |
f625d460 BP |
5036 | * @dflags: gpiod_flags - optional GPIO initialization flags |
5037 | */ | |
5038 | int gpiod_hog(struct gpio_desc *desc, const char *name, | |
5039 | unsigned long lflags, enum gpiod_flags dflags) | |
5040 | { | |
5041 | struct gpio_chip *chip; | |
5042 | struct gpio_desc *local_desc; | |
5043 | int hwnum; | |
d377f56f | 5044 | int ret; |
f625d460 BP |
5045 | |
5046 | chip = gpiod_to_chip(desc); | |
5047 | hwnum = gpio_chip_hwgpio(desc); | |
5048 | ||
5923ea6c LW |
5049 | local_desc = gpiochip_request_own_desc(chip, hwnum, name, |
5050 | lflags, dflags); | |
f625d460 | 5051 | if (IS_ERR(local_desc)) { |
d377f56f | 5052 | ret = PTR_ERR(local_desc); |
c31a571d | 5053 | pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n", |
d377f56f LW |
5054 | name, chip->label, hwnum, ret); |
5055 | return ret; | |
f625d460 BP |
5056 | } |
5057 | ||
f625d460 BP |
5058 | /* Mark GPIO as hogged so it can be identified and removed later */ |
5059 | set_bit(FLAG_IS_HOGGED, &desc->flags); | |
5060 | ||
5061 | pr_info("GPIO line %d (%s) hogged as %s%s\n", | |
5062 | desc_to_gpio(desc), name, | |
b27f300f BG |
5063 | (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input", |
5064 | (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? | |
5065 | (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : ""); | |
f625d460 BP |
5066 | |
5067 | return 0; | |
5068 | } | |
5069 | ||
5070 | /** | |
5071 | * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog | |
5072 | * @chip: gpio chip to act on | |
f625d460 BP |
5073 | */ |
5074 | static void gpiochip_free_hogs(struct gpio_chip *chip) | |
5075 | { | |
5076 | int id; | |
5077 | ||
5078 | for (id = 0; id < chip->ngpio; id++) { | |
1c3cdb18 LW |
5079 | if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags)) |
5080 | gpiochip_free_own_desc(&chip->gpiodev->descs[id]); | |
f625d460 BP |
5081 | } |
5082 | } | |
5083 | ||
66858527 RI |
5084 | /** |
5085 | * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function | |
5086 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
5087 | * @con_id: function within the GPIO consumer | |
5088 | * @flags: optional GPIO initialization flags | |
5089 | * | |
5090 | * This function acquires all the GPIOs defined under a given function. | |
5091 | * | |
5092 | * Return a struct gpio_descs containing an array of descriptors, -ENOENT if | |
5093 | * no GPIO has been assigned to the requested function, or another IS_ERR() | |
5094 | * code if an error occurred while trying to acquire the GPIOs. | |
5095 | */ | |
5096 | struct gpio_descs *__must_check gpiod_get_array(struct device *dev, | |
5097 | const char *con_id, | |
5098 | enum gpiod_flags flags) | |
5099 | { | |
5100 | struct gpio_desc *desc; | |
5101 | struct gpio_descs *descs; | |
bf9346f5 JK |
5102 | struct gpio_array *array_info = NULL; |
5103 | struct gpio_chip *chip; | |
5104 | int count, bitmap_size; | |
66858527 RI |
5105 | |
5106 | count = gpiod_count(dev, con_id); | |
5107 | if (count < 0) | |
5108 | return ERR_PTR(count); | |
5109 | ||
acafe7e3 | 5110 | descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL); |
66858527 RI |
5111 | if (!descs) |
5112 | return ERR_PTR(-ENOMEM); | |
5113 | ||
5114 | for (descs->ndescs = 0; descs->ndescs < count; ) { | |
5115 | desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); | |
5116 | if (IS_ERR(desc)) { | |
5117 | gpiod_put_array(descs); | |
5118 | return ERR_CAST(desc); | |
5119 | } | |
bf9346f5 | 5120 | |
66858527 | 5121 | descs->desc[descs->ndescs] = desc; |
bf9346f5 JK |
5122 | |
5123 | chip = gpiod_to_chip(desc); | |
5124 | /* | |
c4c958aa JK |
5125 | * If pin hardware number of array member 0 is also 0, select |
5126 | * its chip as a candidate for fast bitmap processing path. | |
bf9346f5 | 5127 | */ |
c4c958aa | 5128 | if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) { |
bf9346f5 JK |
5129 | struct gpio_descs *array; |
5130 | ||
5131 | bitmap_size = BITS_TO_LONGS(chip->ngpio > count ? | |
5132 | chip->ngpio : count); | |
5133 | ||
5134 | array = kzalloc(struct_size(descs, desc, count) + | |
5135 | struct_size(array_info, invert_mask, | |
5136 | 3 * bitmap_size), GFP_KERNEL); | |
5137 | if (!array) { | |
5138 | gpiod_put_array(descs); | |
5139 | return ERR_PTR(-ENOMEM); | |
5140 | } | |
5141 | ||
5142 | memcpy(array, descs, | |
5143 | struct_size(descs, desc, descs->ndescs + 1)); | |
5144 | kfree(descs); | |
5145 | ||
5146 | descs = array; | |
5147 | array_info = (void *)(descs->desc + count); | |
5148 | array_info->get_mask = array_info->invert_mask + | |
5149 | bitmap_size; | |
5150 | array_info->set_mask = array_info->get_mask + | |
5151 | bitmap_size; | |
5152 | ||
5153 | array_info->desc = descs->desc; | |
5154 | array_info->size = count; | |
5155 | array_info->chip = chip; | |
5156 | bitmap_set(array_info->get_mask, descs->ndescs, | |
5157 | count - descs->ndescs); | |
5158 | bitmap_set(array_info->set_mask, descs->ndescs, | |
5159 | count - descs->ndescs); | |
5160 | descs->info = array_info; | |
5161 | } | |
c4c958aa JK |
5162 | /* Unmark array members which don't belong to the 'fast' chip */ |
5163 | if (array_info && array_info->chip != chip) { | |
bf9346f5 JK |
5164 | __clear_bit(descs->ndescs, array_info->get_mask); |
5165 | __clear_bit(descs->ndescs, array_info->set_mask); | |
c4c958aa JK |
5166 | } |
5167 | /* | |
5168 | * Detect array members which belong to the 'fast' chip | |
5169 | * but their pins are not in hardware order. | |
5170 | */ | |
5171 | else if (array_info && | |
5172 | gpio_chip_hwgpio(desc) != descs->ndescs) { | |
5173 | /* | |
5174 | * Don't use fast path if all array members processed so | |
5175 | * far belong to the same chip as this one but its pin | |
5176 | * hardware number is different from its array index. | |
5177 | */ | |
5178 | if (bitmap_full(array_info->get_mask, descs->ndescs)) { | |
5179 | array_info = NULL; | |
5180 | } else { | |
5181 | __clear_bit(descs->ndescs, | |
5182 | array_info->get_mask); | |
5183 | __clear_bit(descs->ndescs, | |
5184 | array_info->set_mask); | |
5185 | } | |
bf9346f5 JK |
5186 | } else if (array_info) { |
5187 | /* Exclude open drain or open source from fast output */ | |
5188 | if (gpiochip_line_is_open_drain(chip, descs->ndescs) || | |
5189 | gpiochip_line_is_open_source(chip, descs->ndescs)) | |
5190 | __clear_bit(descs->ndescs, | |
5191 | array_info->set_mask); | |
5192 | /* Identify 'fast' pins which require invertion */ | |
5193 | if (gpiod_is_active_low(desc)) | |
5194 | __set_bit(descs->ndescs, | |
5195 | array_info->invert_mask); | |
5196 | } | |
5197 | ||
66858527 RI |
5198 | descs->ndescs++; |
5199 | } | |
bf9346f5 JK |
5200 | if (array_info) |
5201 | dev_dbg(dev, | |
5202 | "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n", | |
5203 | array_info->chip->label, array_info->size, | |
5204 | *array_info->get_mask, *array_info->set_mask, | |
5205 | *array_info->invert_mask); | |
66858527 RI |
5206 | return descs; |
5207 | } | |
5208 | EXPORT_SYMBOL_GPL(gpiod_get_array); | |
5209 | ||
5210 | /** | |
5211 | * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO | |
5212 | * function | |
5213 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
5214 | * @con_id: function within the GPIO consumer | |
5215 | * @flags: optional GPIO initialization flags | |
5216 | * | |
5217 | * This is equivalent to gpiod_get_array(), except that when no GPIO was | |
5218 | * assigned to the requested function it will return NULL. | |
5219 | */ | |
5220 | struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, | |
5221 | const char *con_id, | |
5222 | enum gpiod_flags flags) | |
5223 | { | |
5224 | struct gpio_descs *descs; | |
5225 | ||
5226 | descs = gpiod_get_array(dev, con_id, flags); | |
45586c70 | 5227 | if (PTR_ERR(descs) == -ENOENT) |
66858527 RI |
5228 | return NULL; |
5229 | ||
5230 | return descs; | |
5231 | } | |
5232 | EXPORT_SYMBOL_GPL(gpiod_get_array_optional); | |
5233 | ||
bae48da2 AC |
5234 | /** |
5235 | * gpiod_put - dispose of a GPIO descriptor | |
5236 | * @desc: GPIO descriptor to dispose of | |
5237 | * | |
5238 | * No descriptor can be used after gpiod_put() has been called on it. | |
5239 | */ | |
5240 | void gpiod_put(struct gpio_desc *desc) | |
5241 | { | |
1d7765ba AS |
5242 | if (desc) |
5243 | gpiod_free(desc); | |
372e722e | 5244 | } |
bae48da2 | 5245 | EXPORT_SYMBOL_GPL(gpiod_put); |
d2876d08 | 5246 | |
66858527 RI |
5247 | /** |
5248 | * gpiod_put_array - dispose of multiple GPIO descriptors | |
5249 | * @descs: struct gpio_descs containing an array of descriptors | |
5250 | */ | |
5251 | void gpiod_put_array(struct gpio_descs *descs) | |
5252 | { | |
5253 | unsigned int i; | |
5254 | ||
5255 | for (i = 0; i < descs->ndescs; i++) | |
5256 | gpiod_put(descs->desc[i]); | |
5257 | ||
5258 | kfree(descs); | |
5259 | } | |
5260 | EXPORT_SYMBOL_GPL(gpiod_put_array); | |
5261 | ||
3c702e99 LW |
5262 | static int __init gpiolib_dev_init(void) |
5263 | { | |
5264 | int ret; | |
5265 | ||
5266 | /* Register GPIO sysfs bus */ | |
b1911710 | 5267 | ret = bus_register(&gpio_bus_type); |
3c702e99 LW |
5268 | if (ret < 0) { |
5269 | pr_err("gpiolib: could not register GPIO bus type\n"); | |
5270 | return ret; | |
5271 | } | |
5272 | ||
ddd8891e | 5273 | ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME); |
3c702e99 LW |
5274 | if (ret < 0) { |
5275 | pr_err("gpiolib: failed to allocate char dev region\n"); | |
5276 | bus_unregister(&gpio_bus_type); | |
63636d95 | 5277 | return ret; |
3c702e99 | 5278 | } |
63636d95 GU |
5279 | |
5280 | gpiolib_initialized = true; | |
5281 | gpiochip_setup_devs(); | |
5282 | ||
5283 | if (IS_ENABLED(CONFIG_OF_DYNAMIC)) | |
5284 | WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier)); | |
5285 | ||
3c702e99 LW |
5286 | return ret; |
5287 | } | |
5288 | core_initcall(gpiolib_dev_init); | |
5289 | ||
d2876d08 DB |
5290 | #ifdef CONFIG_DEBUG_FS |
5291 | ||
fdeb8e15 | 5292 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev) |
d2876d08 DB |
5293 | { |
5294 | unsigned i; | |
fdeb8e15 LW |
5295 | struct gpio_chip *chip = gdev->chip; |
5296 | unsigned gpio = gdev->base; | |
5297 | struct gpio_desc *gdesc = &gdev->descs[0]; | |
90fd2270 LW |
5298 | bool is_out; |
5299 | bool is_irq; | |
5300 | bool active_low; | |
d2876d08 | 5301 | |
fdeb8e15 | 5302 | for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) { |
ced433e2 MP |
5303 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) { |
5304 | if (gdesc->name) { | |
5305 | seq_printf(s, " gpio-%-3d (%-20.20s)\n", | |
5306 | gpio, gdesc->name); | |
5307 | } | |
d2876d08 | 5308 | continue; |
ced433e2 | 5309 | } |
d2876d08 | 5310 | |
372e722e | 5311 | gpiod_get_direction(gdesc); |
d2876d08 | 5312 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
d468bf9e | 5313 | is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); |
90fd2270 LW |
5314 | active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags); |
5315 | seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s", | |
ced433e2 | 5316 | gpio, gdesc->name ? gdesc->name : "", gdesc->label, |
d2876d08 | 5317 | is_out ? "out" : "in ", |
1c22a252 | 5318 | chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ", |
90fd2270 LW |
5319 | is_irq ? "IRQ " : "", |
5320 | active_low ? "ACTIVE LOW" : ""); | |
d2876d08 DB |
5321 | seq_printf(s, "\n"); |
5322 | } | |
5323 | } | |
5324 | ||
f9c4a31f | 5325 | static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) |
d2876d08 | 5326 | { |
362432ae | 5327 | unsigned long flags; |
ff2b1359 | 5328 | struct gpio_device *gdev = NULL; |
cb1650d4 | 5329 | loff_t index = *pos; |
d2876d08 | 5330 | |
f9c4a31f | 5331 | s->private = ""; |
d2876d08 | 5332 | |
362432ae | 5333 | spin_lock_irqsave(&gpio_lock, flags); |
ff2b1359 | 5334 | list_for_each_entry(gdev, &gpio_devices, list) |
362432ae GL |
5335 | if (index-- == 0) { |
5336 | spin_unlock_irqrestore(&gpio_lock, flags); | |
ff2b1359 | 5337 | return gdev; |
f9c4a31f | 5338 | } |
362432ae | 5339 | spin_unlock_irqrestore(&gpio_lock, flags); |
f9c4a31f | 5340 | |
cb1650d4 | 5341 | return NULL; |
f9c4a31f TR |
5342 | } |
5343 | ||
5344 | static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
5345 | { | |
362432ae | 5346 | unsigned long flags; |
ff2b1359 | 5347 | struct gpio_device *gdev = v; |
f9c4a31f TR |
5348 | void *ret = NULL; |
5349 | ||
362432ae | 5350 | spin_lock_irqsave(&gpio_lock, flags); |
ff2b1359 | 5351 | if (list_is_last(&gdev->list, &gpio_devices)) |
cb1650d4 AC |
5352 | ret = NULL; |
5353 | else | |
ff2b1359 | 5354 | ret = list_entry(gdev->list.next, struct gpio_device, list); |
362432ae | 5355 | spin_unlock_irqrestore(&gpio_lock, flags); |
f9c4a31f TR |
5356 | |
5357 | s->private = "\n"; | |
5358 | ++*pos; | |
5359 | ||
5360 | return ret; | |
5361 | } | |
5362 | ||
5363 | static void gpiolib_seq_stop(struct seq_file *s, void *v) | |
5364 | { | |
5365 | } | |
5366 | ||
5367 | static int gpiolib_seq_show(struct seq_file *s, void *v) | |
5368 | { | |
ff2b1359 LW |
5369 | struct gpio_device *gdev = v; |
5370 | struct gpio_chip *chip = gdev->chip; | |
5371 | struct device *parent; | |
5372 | ||
5373 | if (!chip) { | |
5374 | seq_printf(s, "%s%s: (dangling chip)", (char *)s->private, | |
5375 | dev_name(&gdev->dev)); | |
5376 | return 0; | |
5377 | } | |
f9c4a31f | 5378 | |
ff2b1359 LW |
5379 | seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private, |
5380 | dev_name(&gdev->dev), | |
fdeb8e15 | 5381 | gdev->base, gdev->base + gdev->ngpio - 1); |
ff2b1359 LW |
5382 | parent = chip->parent; |
5383 | if (parent) | |
5384 | seq_printf(s, ", parent: %s/%s", | |
5385 | parent->bus ? parent->bus->name : "no-bus", | |
5386 | dev_name(parent)); | |
f9c4a31f TR |
5387 | if (chip->label) |
5388 | seq_printf(s, ", %s", chip->label); | |
5389 | if (chip->can_sleep) | |
5390 | seq_printf(s, ", can sleep"); | |
5391 | seq_printf(s, ":\n"); | |
5392 | ||
5393 | if (chip->dbg_show) | |
5394 | chip->dbg_show(s, chip); | |
5395 | else | |
fdeb8e15 | 5396 | gpiolib_dbg_show(s, gdev); |
f9c4a31f | 5397 | |
d2876d08 DB |
5398 | return 0; |
5399 | } | |
5400 | ||
f9c4a31f TR |
5401 | static const struct seq_operations gpiolib_seq_ops = { |
5402 | .start = gpiolib_seq_start, | |
5403 | .next = gpiolib_seq_next, | |
5404 | .stop = gpiolib_seq_stop, | |
5405 | .show = gpiolib_seq_show, | |
5406 | }; | |
5407 | ||
d2876d08 DB |
5408 | static int gpiolib_open(struct inode *inode, struct file *file) |
5409 | { | |
f9c4a31f | 5410 | return seq_open(file, &gpiolib_seq_ops); |
d2876d08 DB |
5411 | } |
5412 | ||
828c0950 | 5413 | static const struct file_operations gpiolib_operations = { |
f9c4a31f | 5414 | .owner = THIS_MODULE, |
d2876d08 DB |
5415 | .open = gpiolib_open, |
5416 | .read = seq_read, | |
5417 | .llseek = seq_lseek, | |
f9c4a31f | 5418 | .release = seq_release, |
d2876d08 DB |
5419 | }; |
5420 | ||
5421 | static int __init gpiolib_debugfs_init(void) | |
5422 | { | |
5423 | /* /sys/kernel/debug/gpio */ | |
acc68b0e GKH |
5424 | debugfs_create_file("gpio", S_IFREG | S_IRUGO, NULL, NULL, |
5425 | &gpiolib_operations); | |
d2876d08 DB |
5426 | return 0; |
5427 | } | |
5428 | subsys_initcall(gpiolib_debugfs_init); | |
5429 | ||
5430 | #endif /* DEBUG_FS */ |