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