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