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