gpiolib: fix find_chip_by_name()
[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>
81f59e9d 13#include <linux/acpi_gpio.h>
ff77c352 14#include <linux/idr.h>
5a0e3ad6 15#include <linux/slab.h>
7b199811 16#include <linux/acpi.h>
d2876d08 17
3f397c21
UKK
18#define CREATE_TRACE_POINTS
19#include <trace/events/gpio.h>
d2876d08 20
79a9becd 21/* Implementation infrastructure for GPIO interfaces.
d2876d08 22 *
79a9becd
AC
23 * The GPIO programming interface allows for inlining speed-critical
24 * get/set operations for common cases, so that access to SOC-integrated
25 * GPIOs can sometimes cost only an instruction or two per bit.
d2876d08
DB
26 */
27
28
29/* When debugging, extend minimal trust to callers and platform code.
30 * Also emit diagnostic messages that may help initial bringup, when
31 * board setup or driver bugs are most common.
32 *
33 * Otherwise, minimize overhead in what may be bitbanging codepaths.
34 */
35#ifdef DEBUG
36#define extra_checks 1
37#else
38#define extra_checks 0
39#endif
40
41/* gpio_lock prevents conflicts during gpio_desc[] table updates.
42 * While any GPIO is requested, its gpio_chip is not removable;
43 * each GPIO's "requested" flag serves as a lock and refcount.
44 */
45static DEFINE_SPINLOCK(gpio_lock);
46
47struct gpio_desc {
48 struct gpio_chip *chip;
49 unsigned long flags;
50/* flag symbols are bit numbers */
51#define FLAG_REQUESTED 0
52#define FLAG_IS_OUT 1
710b40ea
AC
53#define FLAG_EXPORT 2 /* protected by sysfs_lock */
54#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
55#define FLAG_TRIG_FALL 4 /* trigger on falling edge */
56#define FLAG_TRIG_RISE 5 /* trigger on rising edge */
79a9becd 57#define FLAG_ACTIVE_LOW 6 /* value has active low */
710b40ea
AC
58#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
59#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
d468bf9e 60#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
ff77c352 61
5ba1821d 62#define ID_SHIFT 16 /* add new flags before this one */
ff77c352 63
5ba1821d 64#define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
ff77c352 65#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
d2876d08
DB
66
67#ifdef CONFIG_DEBUG_FS
68 const char *label;
69#endif
70};
71static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
72
6c0b4e6c
AC
73#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
74
bae48da2
AC
75static DEFINE_MUTEX(gpio_lookup_lock);
76static LIST_HEAD(gpio_lookup_list);
1a989d0f
AC
77static LIST_HEAD(gpio_chips);
78
ff77c352 79#ifdef CONFIG_GPIO_SYSFS
5ba1821d 80static DEFINE_IDR(dirent_idr);
ff77c352
DG
81#endif
82
372e722e
AC
83static int gpiod_request(struct gpio_desc *desc, const char *label);
84static void gpiod_free(struct gpio_desc *desc);
372e722e 85
7b17b59f
MB
86#ifdef CONFIG_DEBUG_FS
87#define gpiod_emerg(desc, fmt, ...) \
88 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
89 ##__VA_ARGS__)
90#define gpiod_crit(desc, fmt, ...) \
91 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
92 ##__VA_ARGS__)
93#define gpiod_err(desc, fmt, ...) \
94 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
95 ##__VA_ARGS__)
96#define gpiod_warn(desc, fmt, ...) \
97 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
98 ##__VA_ARGS__)
99#define gpiod_info(desc, fmt, ...) \
100 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
101 ##__VA_ARGS__)
102#define gpiod_dbg(desc, fmt, ...) \
103 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
104 ##__VA_ARGS__)
105#else
6424de5a
MB
106#define gpiod_emerg(desc, fmt, ...) \
107 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
108#define gpiod_crit(desc, fmt, ...) \
109 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
110#define gpiod_err(desc, fmt, ...) \
111 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
112#define gpiod_warn(desc, fmt, ...) \
113 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
114#define gpiod_info(desc, fmt, ...) \
115 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
116#define gpiod_dbg(desc, fmt, ...) \
117 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
7b17b59f 118#endif
372e722e 119
d2876d08
DB
120static inline void desc_set_label(struct gpio_desc *d, const char *label)
121{
122#ifdef CONFIG_DEBUG_FS
123 d->label = label;
124#endif
125}
126
372e722e
AC
127/*
128 * Return the GPIO number of the passed descriptor relative to its chip
129 */
130static int gpio_chip_hwgpio(const struct gpio_desc *desc)
131{
6c0b4e6c 132 return desc - &desc->chip->desc[0];
372e722e
AC
133}
134
135/**
136 * Convert a GPIO number to its descriptor
137 */
79a9becd 138struct gpio_desc *gpio_to_desc(unsigned gpio)
372e722e
AC
139{
140 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
141 return NULL;
142 else
143 return &gpio_desc[gpio];
144}
79a9becd 145EXPORT_SYMBOL_GPL(gpio_to_desc);
372e722e 146
d468bf9e
LW
147/**
148 * Convert an offset on a certain chip to a corresponding descriptor
149 */
150static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip,
151 unsigned int offset)
152{
153 unsigned int gpio = chip->base + offset;
154
155 return gpio_to_desc(gpio);
156}
372e722e
AC
157
158/**
159 * Convert a GPIO descriptor to the integer namespace.
160 * This should disappear in the future but is needed since we still
161 * use GPIO numbers for error messages and sysfs nodes
162 */
79a9becd 163int desc_to_gpio(const struct gpio_desc *desc)
372e722e 164{
8c0fca81 165 return desc - &gpio_desc[0];
372e722e 166}
79a9becd 167EXPORT_SYMBOL_GPL(desc_to_gpio);
372e722e
AC
168
169
d2876d08
DB
170/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
171 * when setting direction, and otherwise illegal. Until board setup code
172 * and drivers use explicit requests everywhere (which won't happen when
173 * those calls have no teeth) we can't avoid autorequesting. This nag
35e8bb51
DB
174 * message should motivate switching to explicit requests... so should
175 * the weaker cleanup after faults, compared to gpio_request().
8a0cecff
DB
176 *
177 * NOTE: the autorequest mechanism is going away; at this point it's
178 * only "legal" in the sense that (old) code using it won't break yet,
179 * but instead only triggers a WARN() stack dump.
d2876d08 180 */
372e722e 181static int gpio_ensure_requested(struct gpio_desc *desc)
d2876d08 182{
8a0cecff 183 const struct gpio_chip *chip = desc->chip;
372e722e 184 const int gpio = desc_to_gpio(desc);
35e8bb51 185
8a0cecff
DB
186 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
187 "autorequest GPIO-%d\n", gpio)) {
35e8bb51
DB
188 if (!try_module_get(chip->owner)) {
189 pr_err("GPIO-%d: module can't be gotten \n", gpio);
190 clear_bit(FLAG_REQUESTED, &desc->flags);
191 /* lose */
192 return -EIO;
193 }
d2876d08 194 desc_set_label(desc, "[auto]");
35e8bb51
DB
195 /* caller must chip->request() w/o spinlock */
196 if (chip->request)
197 return 1;
d2876d08 198 }
35e8bb51 199 return 0;
d2876d08
DB
200}
201
79a9becd
AC
202/**
203 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
204 * @desc: descriptor to return the chip of
205 */
206struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
372e722e 207{
bcabdef1 208 return desc ? desc->chip : NULL;
372e722e 209}
79a9becd 210EXPORT_SYMBOL_GPL(gpiod_to_chip);
d2876d08 211
8d0aab2f
AV
212/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
213static int gpiochip_find_base(int ngpio)
214{
83cabe33
AC
215 struct gpio_chip *chip;
216 int base = ARCH_NR_GPIOS - ngpio;
8d0aab2f 217
83cabe33
AC
218 list_for_each_entry_reverse(chip, &gpio_chips, list) {
219 /* found a free space? */
220 if (chip->base + chip->ngpio <= base)
221 break;
222 else
223 /* nope, check the space right before the chip */
224 base = chip->base - ngpio;
8d0aab2f
AV
225 }
226
83cabe33 227 if (gpio_is_valid(base)) {
8d0aab2f 228 pr_debug("%s: found new base at %d\n", __func__, base);
83cabe33
AC
229 return base;
230 } else {
231 pr_err("%s: cannot find free range\n", __func__);
232 return -ENOSPC;
169b6a7a 233 }
169b6a7a
AV
234}
235
79a9becd
AC
236/**
237 * gpiod_get_direction - return the current direction of a GPIO
238 * @desc: GPIO to get the direction of
239 *
240 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
241 *
242 * This function may sleep if gpiod_cansleep() is true.
243 */
244int gpiod_get_direction(const struct gpio_desc *desc)
80b0a602
MN
245{
246 struct gpio_chip *chip;
372e722e 247 unsigned offset;
80b0a602
MN
248 int status = -EINVAL;
249
372e722e
AC
250 chip = gpiod_to_chip(desc);
251 offset = gpio_chip_hwgpio(desc);
80b0a602
MN
252
253 if (!chip->get_direction)
254 return status;
255
372e722e 256 status = chip->get_direction(chip, offset);
80b0a602
MN
257 if (status > 0) {
258 /* GPIOF_DIR_IN, or other positive */
259 status = 1;
def63433
AC
260 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
261 * so it does not affect constness per se */
262 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
80b0a602
MN
263 }
264 if (status == 0) {
265 /* GPIOF_DIR_OUT */
def63433 266 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
80b0a602
MN
267 }
268 return status;
269}
79a9becd 270EXPORT_SYMBOL_GPL(gpiod_get_direction);
80b0a602 271
d8f388d8
DB
272#ifdef CONFIG_GPIO_SYSFS
273
274/* lock protects against unexport_gpio() being called while
275 * sysfs files are active.
276 */
277static DEFINE_MUTEX(sysfs_lock);
278
279/*
280 * /sys/class/gpio/gpioN... only for GPIOs that are exported
281 * /direction
282 * * MAY BE OMITTED if kernel won't allow direction changes
283 * * is read/write as "in" or "out"
284 * * may also be written as "high" or "low", initializing
285 * output value as specified ("out" implies "low")
286 * /value
287 * * always readable, subject to hardware behavior
288 * * may be writable, as zero/nonzero
ff77c352
DG
289 * /edge
290 * * configures behavior of poll(2) on /value
291 * * available only if pin can generate IRQs on input
292 * * is read/write as "none", "falling", "rising", or "both"
07697461
JN
293 * /active_low
294 * * configures polarity of /value
295 * * is read/write as zero/nonzero
296 * * also affects existing and subsequent "falling" and "rising"
297 * /edge configuration
d8f388d8
DB
298 */
299
300static ssize_t gpio_direction_show(struct device *dev,
301 struct device_attribute *attr, char *buf)
302{
def63433 303 const struct gpio_desc *desc = dev_get_drvdata(dev);
d8f388d8
DB
304 ssize_t status;
305
306 mutex_lock(&sysfs_lock);
307
476171ce 308 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
d8f388d8 309 status = -EIO;
476171ce 310 } else {
372e722e 311 gpiod_get_direction(desc);
d8f388d8
DB
312 status = sprintf(buf, "%s\n",
313 test_bit(FLAG_IS_OUT, &desc->flags)
314 ? "out" : "in");
476171ce 315 }
d8f388d8
DB
316
317 mutex_unlock(&sysfs_lock);
318 return status;
319}
320
321static ssize_t gpio_direction_store(struct device *dev,
322 struct device_attribute *attr, const char *buf, size_t size)
323{
372e722e 324 struct gpio_desc *desc = dev_get_drvdata(dev);
d8f388d8
DB
325 ssize_t status;
326
327 mutex_lock(&sysfs_lock);
328
329 if (!test_bit(FLAG_EXPORT, &desc->flags))
330 status = -EIO;
331 else if (sysfs_streq(buf, "high"))
372e722e 332 status = gpiod_direction_output(desc, 1);
d8f388d8 333 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
372e722e 334 status = gpiod_direction_output(desc, 0);
d8f388d8 335 else if (sysfs_streq(buf, "in"))
372e722e 336 status = gpiod_direction_input(desc);
d8f388d8
DB
337 else
338 status = -EINVAL;
339
340 mutex_unlock(&sysfs_lock);
341 return status ? : size;
342}
343
07697461 344static /* const */ DEVICE_ATTR(direction, 0644,
d8f388d8
DB
345 gpio_direction_show, gpio_direction_store);
346
347static ssize_t gpio_value_show(struct device *dev,
348 struct device_attribute *attr, char *buf)
349{
372e722e 350 struct gpio_desc *desc = dev_get_drvdata(dev);
d8f388d8
DB
351 ssize_t status;
352
353 mutex_lock(&sysfs_lock);
354
79a9becd 355 if (!test_bit(FLAG_EXPORT, &desc->flags))
d8f388d8 356 status = -EIO;
79a9becd
AC
357 else
358 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
d8f388d8
DB
359
360 mutex_unlock(&sysfs_lock);
361 return status;
362}
363
364static ssize_t gpio_value_store(struct device *dev,
365 struct device_attribute *attr, const char *buf, size_t size)
366{
372e722e 367 struct gpio_desc *desc = dev_get_drvdata(dev);
d8f388d8
DB
368 ssize_t status;
369
370 mutex_lock(&sysfs_lock);
371
372 if (!test_bit(FLAG_EXPORT, &desc->flags))
373 status = -EIO;
374 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
375 status = -EPERM;
376 else {
377 long value;
378
a3d88c92 379 status = kstrtol(buf, 0, &value);
d8f388d8 380 if (status == 0) {
79a9becd 381 gpiod_set_value_cansleep(desc, value);
d8f388d8
DB
382 status = size;
383 }
384 }
385
386 mutex_unlock(&sysfs_lock);
387 return status;
388}
389
07697461 390static const DEVICE_ATTR(value, 0644,
d8f388d8
DB
391 gpio_value_show, gpio_value_store);
392
ff77c352
DG
393static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
394{
5ba1821d 395 struct sysfs_dirent *value_sd = priv;
ff77c352 396
5ba1821d 397 sysfs_notify_dirent(value_sd);
ff77c352
DG
398 return IRQ_HANDLED;
399}
400
ff77c352
DG
401static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
402 unsigned long gpio_flags)
403{
5ba1821d 404 struct sysfs_dirent *value_sd;
ff77c352
DG
405 unsigned long irq_flags;
406 int ret, irq, id;
407
408 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
409 return 0;
410
372e722e 411 irq = gpiod_to_irq(desc);
ff77c352
DG
412 if (irq < 0)
413 return -EIO;
414
5ba1821d
DG
415 id = desc->flags >> ID_SHIFT;
416 value_sd = idr_find(&dirent_idr, id);
417 if (value_sd)
418 free_irq(irq, value_sd);
ff77c352
DG
419
420 desc->flags &= ~GPIO_TRIGGER_MASK;
421
422 if (!gpio_flags) {
d468bf9e 423 gpiod_unlock_as_irq(desc);
ff77c352 424 ret = 0;
5ba1821d 425 goto free_id;
ff77c352
DG
426 }
427
428 irq_flags = IRQF_SHARED;
429 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
07697461
JN
430 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
431 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
ff77c352 432 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
07697461
JN
433 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
434 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
ff77c352 435
5ba1821d 436 if (!value_sd) {
388975cc 437 value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
5ba1821d
DG
438 if (!value_sd) {
439 ret = -ENODEV;
ff77c352
DG
440 goto err_out;
441 }
442
62f516b8
TH
443 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
444 if (ret < 0)
5ba1821d 445 goto free_sd;
62f516b8 446 id = ret;
ff77c352
DG
447
448 desc->flags &= GPIO_FLAGS_MASK;
5ba1821d 449 desc->flags |= (unsigned long)id << ID_SHIFT;
ff77c352 450
5ba1821d 451 if (desc->flags >> ID_SHIFT != id) {
ff77c352
DG
452 ret = -ERANGE;
453 goto free_id;
454 }
ff77c352
DG
455 }
456
364fadb3 457 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
5ba1821d 458 "gpiolib", value_sd);
364fadb3 459 if (ret < 0)
5ba1821d 460 goto free_id;
ff77c352 461
d468bf9e
LW
462 ret = gpiod_lock_as_irq(desc);
463 if (ret < 0) {
464 gpiod_warn(desc, "failed to flag the GPIO for IRQ\n");
465 goto free_id;
466 }
467
ff77c352
DG
468 desc->flags |= gpio_flags;
469 return 0;
470
ff77c352 471free_id:
5ba1821d 472 idr_remove(&dirent_idr, id);
ff77c352 473 desc->flags &= GPIO_FLAGS_MASK;
5ba1821d
DG
474free_sd:
475 if (value_sd)
476 sysfs_put(value_sd);
ff77c352
DG
477err_out:
478 return ret;
479}
480
481static const struct {
482 const char *name;
483 unsigned long flags;
484} trigger_types[] = {
485 { "none", 0 },
486 { "falling", BIT(FLAG_TRIG_FALL) },
487 { "rising", BIT(FLAG_TRIG_RISE) },
488 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
489};
490
491static ssize_t gpio_edge_show(struct device *dev,
492 struct device_attribute *attr, char *buf)
493{
494 const struct gpio_desc *desc = dev_get_drvdata(dev);
495 ssize_t status;
496
497 mutex_lock(&sysfs_lock);
498
499 if (!test_bit(FLAG_EXPORT, &desc->flags))
500 status = -EIO;
501 else {
502 int i;
503
504 status = 0;
505 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
506 if ((desc->flags & GPIO_TRIGGER_MASK)
507 == trigger_types[i].flags) {
508 status = sprintf(buf, "%s\n",
509 trigger_types[i].name);
510 break;
511 }
512 }
513
514 mutex_unlock(&sysfs_lock);
515 return status;
516}
517
518static ssize_t gpio_edge_store(struct device *dev,
519 struct device_attribute *attr, const char *buf, size_t size)
520{
521 struct gpio_desc *desc = dev_get_drvdata(dev);
522 ssize_t status;
523 int i;
524
525 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
526 if (sysfs_streq(trigger_types[i].name, buf))
527 goto found;
528 return -EINVAL;
529
530found:
531 mutex_lock(&sysfs_lock);
532
533 if (!test_bit(FLAG_EXPORT, &desc->flags))
534 status = -EIO;
535 else {
536 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
537 if (!status)
538 status = size;
539 }
540
541 mutex_unlock(&sysfs_lock);
542
543 return status;
544}
545
546static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
547
07697461
JN
548static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
549 int value)
550{
551 int status = 0;
552
553 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
554 return 0;
555
556 if (value)
557 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
558 else
559 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
560
561 /* reconfigure poll(2) support if enabled on one edge only */
562 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
563 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
564 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
565
566 gpio_setup_irq(desc, dev, 0);
567 status = gpio_setup_irq(desc, dev, trigger_flags);
568 }
569
570 return status;
571}
572
573static ssize_t gpio_active_low_show(struct device *dev,
574 struct device_attribute *attr, char *buf)
575{
576 const struct gpio_desc *desc = dev_get_drvdata(dev);
577 ssize_t status;
578
579 mutex_lock(&sysfs_lock);
580
581 if (!test_bit(FLAG_EXPORT, &desc->flags))
582 status = -EIO;
583 else
584 status = sprintf(buf, "%d\n",
585 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
586
587 mutex_unlock(&sysfs_lock);
588
589 return status;
590}
591
592static ssize_t gpio_active_low_store(struct device *dev,
593 struct device_attribute *attr, const char *buf, size_t size)
594{
595 struct gpio_desc *desc = dev_get_drvdata(dev);
596 ssize_t status;
597
598 mutex_lock(&sysfs_lock);
599
600 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
601 status = -EIO;
602 } else {
603 long value;
604
a3d88c92 605 status = kstrtol(buf, 0, &value);
07697461
JN
606 if (status == 0)
607 status = sysfs_set_active_low(desc, dev, value != 0);
608 }
609
610 mutex_unlock(&sysfs_lock);
611
612 return status ? : size;
613}
614
615static const DEVICE_ATTR(active_low, 0644,
616 gpio_active_low_show, gpio_active_low_store);
617
d8f388d8 618static const struct attribute *gpio_attrs[] = {
d8f388d8 619 &dev_attr_value.attr,
07697461 620 &dev_attr_active_low.attr,
d8f388d8
DB
621 NULL,
622};
623
624static const struct attribute_group gpio_attr_group = {
625 .attrs = (struct attribute **) gpio_attrs,
626};
627
628/*
629 * /sys/class/gpio/gpiochipN/
630 * /base ... matching gpio_chip.base (N)
631 * /label ... matching gpio_chip.label
632 * /ngpio ... matching gpio_chip.ngpio
633 */
634
635static ssize_t chip_base_show(struct device *dev,
636 struct device_attribute *attr, char *buf)
637{
638 const struct gpio_chip *chip = dev_get_drvdata(dev);
639
640 return sprintf(buf, "%d\n", chip->base);
641}
642static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
643
644static ssize_t chip_label_show(struct device *dev,
645 struct device_attribute *attr, char *buf)
646{
647 const struct gpio_chip *chip = dev_get_drvdata(dev);
648
649 return sprintf(buf, "%s\n", chip->label ? : "");
650}
651static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
652
653static ssize_t chip_ngpio_show(struct device *dev,
654 struct device_attribute *attr, char *buf)
655{
656 const struct gpio_chip *chip = dev_get_drvdata(dev);
657
658 return sprintf(buf, "%u\n", chip->ngpio);
659}
660static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
661
662static const struct attribute *gpiochip_attrs[] = {
663 &dev_attr_base.attr,
664 &dev_attr_label.attr,
665 &dev_attr_ngpio.attr,
666 NULL,
667};
668
669static const struct attribute_group gpiochip_attr_group = {
670 .attrs = (struct attribute **) gpiochip_attrs,
671};
672
673/*
674 * /sys/class/gpio/export ... write-only
675 * integer N ... number of GPIO to export (full access)
676 * /sys/class/gpio/unexport ... write-only
677 * integer N ... number of GPIO to unexport
678 */
28812fe1
AK
679static ssize_t export_store(struct class *class,
680 struct class_attribute *attr,
681 const char *buf, size_t len)
d8f388d8 682{
372e722e
AC
683 long gpio;
684 struct gpio_desc *desc;
685 int status;
d8f388d8 686
a3d88c92 687 status = kstrtol(buf, 0, &gpio);
d8f388d8
DB
688 if (status < 0)
689 goto done;
690
372e722e 691 desc = gpio_to_desc(gpio);
bcabdef1
AC
692 /* reject invalid GPIOs */
693 if (!desc) {
694 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
695 return -EINVAL;
696 }
372e722e 697
d8f388d8
DB
698 /* No extra locking here; FLAG_SYSFS just signifies that the
699 * request and export were done by on behalf of userspace, so
700 * they may be undone on its behalf too.
701 */
702
372e722e 703 status = gpiod_request(desc, "sysfs");
ad2fab36
MN
704 if (status < 0) {
705 if (status == -EPROBE_DEFER)
706 status = -ENODEV;
d8f388d8 707 goto done;
ad2fab36 708 }
372e722e 709 status = gpiod_export(desc, true);
d8f388d8 710 if (status < 0)
372e722e 711 gpiod_free(desc);
d8f388d8 712 else
372e722e 713 set_bit(FLAG_SYSFS, &desc->flags);
d8f388d8
DB
714
715done:
716 if (status)
717 pr_debug("%s: status %d\n", __func__, status);
718 return status ? : len;
719}
720
28812fe1
AK
721static ssize_t unexport_store(struct class *class,
722 struct class_attribute *attr,
723 const char *buf, size_t len)
d8f388d8 724{
372e722e
AC
725 long gpio;
726 struct gpio_desc *desc;
727 int status;
d8f388d8 728
a3d88c92 729 status = kstrtol(buf, 0, &gpio);
d8f388d8
DB
730 if (status < 0)
731 goto done;
732
372e722e 733 desc = gpio_to_desc(gpio);
d8f388d8 734 /* reject bogus commands (gpio_unexport ignores them) */
bcabdef1
AC
735 if (!desc) {
736 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
737 return -EINVAL;
738 }
739
740 status = -EINVAL;
d8f388d8
DB
741
742 /* No extra locking here; FLAG_SYSFS just signifies that the
743 * request and export were done by on behalf of userspace, so
744 * they may be undone on its behalf too.
745 */
372e722e 746 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
d8f388d8 747 status = 0;
372e722e 748 gpiod_free(desc);
d8f388d8
DB
749 }
750done:
751 if (status)
752 pr_debug("%s: status %d\n", __func__, status);
753 return status ? : len;
754}
755
756static struct class_attribute gpio_class_attrs[] = {
757 __ATTR(export, 0200, NULL, export_store),
758 __ATTR(unexport, 0200, NULL, unexport_store),
759 __ATTR_NULL,
760};
761
762static struct class gpio_class = {
763 .name = "gpio",
764 .owner = THIS_MODULE,
765
766 .class_attrs = gpio_class_attrs,
767};
768
769
770/**
79a9becd 771 * gpiod_export - export a GPIO through sysfs
d8f388d8
DB
772 * @gpio: gpio to make available, already requested
773 * @direction_may_change: true if userspace may change gpio direction
774 * Context: arch_initcall or later
775 *
776 * When drivers want to make a GPIO accessible to userspace after they
777 * have requested it -- perhaps while debugging, or as part of their
778 * public interface -- they may use this routine. If the GPIO can
779 * change direction (some can't) and the caller allows it, userspace
780 * will see "direction" sysfs attribute which may be used to change
781 * the gpio's direction. A "value" attribute will always be provided.
782 *
783 * Returns zero on success, else an error.
784 */
79a9becd 785int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
d8f388d8
DB
786{
787 unsigned long flags;
fc4e2514 788 int status;
62154991 789 const char *ioname = NULL;
fc4e2514 790 struct device *dev;
372e722e 791 int offset;
d8f388d8
DB
792
793 /* can't export until sysfs is available ... */
794 if (!gpio_class.p) {
795 pr_debug("%s: called too early!\n", __func__);
796 return -ENOENT;
797 }
798
372e722e
AC
799 if (!desc) {
800 pr_debug("%s: invalid gpio descriptor\n", __func__);
fc4e2514
RM
801 return -EINVAL;
802 }
d8f388d8
DB
803
804 mutex_lock(&sysfs_lock);
805
806 spin_lock_irqsave(&gpio_lock, flags);
fc4e2514
RM
807 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
808 test_bit(FLAG_EXPORT, &desc->flags)) {
809 spin_unlock_irqrestore(&gpio_lock, flags);
810 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
372e722e 811 __func__, desc_to_gpio(desc),
fc4e2514
RM
812 test_bit(FLAG_REQUESTED, &desc->flags),
813 test_bit(FLAG_EXPORT, &desc->flags));
529f2ad5
DC
814 status = -EPERM;
815 goto fail_unlock;
d8f388d8 816 }
fc4e2514
RM
817
818 if (!desc->chip->direction_input || !desc->chip->direction_output)
819 direction_may_change = false;
d8f388d8
DB
820 spin_unlock_irqrestore(&gpio_lock, flags);
821
372e722e
AC
822 offset = gpio_chip_hwgpio(desc);
823 if (desc->chip->names && desc->chip->names[offset])
824 ioname = desc->chip->names[offset];
926b663c 825
fc4e2514 826 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
372e722e
AC
827 desc, ioname ? ioname : "gpio%u",
828 desc_to_gpio(desc));
fc4e2514
RM
829 if (IS_ERR(dev)) {
830 status = PTR_ERR(dev);
831 goto fail_unlock;
d8f388d8
DB
832 }
833
fc4e2514 834 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
d8f388d8 835 if (status)
fc4e2514 836 goto fail_unregister_device;
d8f388d8 837
fc4e2514
RM
838 if (direction_may_change) {
839 status = device_create_file(dev, &dev_attr_direction);
840 if (status)
841 goto fail_unregister_device;
842 }
843
372e722e 844 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
fc4e2514
RM
845 !test_bit(FLAG_IS_OUT, &desc->flags))) {
846 status = device_create_file(dev, &dev_attr_edge);
847 if (status)
848 goto fail_unregister_device;
849 }
d8f388d8 850
fc4e2514
RM
851 set_bit(FLAG_EXPORT, &desc->flags);
852 mutex_unlock(&sysfs_lock);
853 return 0;
854
855fail_unregister_device:
856 device_unregister(dev);
857fail_unlock:
858 mutex_unlock(&sysfs_lock);
372e722e
AC
859 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
860 status);
d8f388d8
DB
861 return status;
862}
79a9becd 863EXPORT_SYMBOL_GPL(gpiod_export);
d8f388d8 864
9f3b795a 865static int match_export(struct device *dev, const void *data)
d8f388d8
DB
866{
867 return dev_get_drvdata(dev) == data;
868}
869
a4177ee7 870/**
79a9becd 871 * gpiod_export_link - create a sysfs link to an exported GPIO node
a4177ee7
JN
872 * @dev: device under which to create symlink
873 * @name: name of the symlink
874 * @gpio: gpio to create symlink to, already exported
875 *
876 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
877 * node. Caller is responsible for unlinking.
878 *
879 * Returns zero on success, else an error.
880 */
79a9becd
AC
881int gpiod_export_link(struct device *dev, const char *name,
882 struct gpio_desc *desc)
a4177ee7 883{
a4177ee7
JN
884 int status = -EINVAL;
885
bcabdef1
AC
886 if (!desc) {
887 pr_warn("%s: invalid GPIO\n", __func__);
888 return -EINVAL;
889 }
a4177ee7
JN
890
891 mutex_lock(&sysfs_lock);
892
a4177ee7
JN
893 if (test_bit(FLAG_EXPORT, &desc->flags)) {
894 struct device *tdev;
895
896 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
897 if (tdev != NULL) {
898 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
899 name);
900 } else {
901 status = -ENODEV;
902 }
903 }
904
905 mutex_unlock(&sysfs_lock);
906
a4177ee7 907 if (status)
372e722e
AC
908 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
909 status);
a4177ee7
JN
910
911 return status;
912}
79a9becd 913EXPORT_SYMBOL_GPL(gpiod_export_link);
07697461
JN
914
915/**
79a9becd 916 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
07697461
JN
917 * @gpio: gpio to change
918 * @value: non-zero to use active low, i.e. inverted values
919 *
920 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
921 * The GPIO does not have to be exported yet. If poll(2) support has
922 * been enabled for either rising or falling edge, it will be
923 * reconfigured to follow the new polarity.
924 *
925 * Returns zero on success, else an error.
926 */
79a9becd 927int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
07697461 928{
07697461
JN
929 struct device *dev = NULL;
930 int status = -EINVAL;
931
bcabdef1
AC
932 if (!desc) {
933 pr_warn("%s: invalid GPIO\n", __func__);
934 return -EINVAL;
935 }
07697461
JN
936
937 mutex_lock(&sysfs_lock);
938
07697461 939 if (test_bit(FLAG_EXPORT, &desc->flags)) {
07697461
JN
940 dev = class_find_device(&gpio_class, NULL, desc, match_export);
941 if (dev == NULL) {
942 status = -ENODEV;
943 goto unlock;
944 }
945 }
946
947 status = sysfs_set_active_low(desc, dev, value);
948
949unlock:
950 mutex_unlock(&sysfs_lock);
951
07697461 952 if (status)
372e722e
AC
953 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
954 status);
07697461
JN
955
956 return status;
957}
79a9becd 958EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
07697461 959
d8f388d8 960/**
79a9becd 961 * gpiod_unexport - reverse effect of gpio_export()
d8f388d8
DB
962 * @gpio: gpio to make unavailable
963 *
964 * This is implicit on gpio_free().
965 */
79a9becd 966void gpiod_unexport(struct gpio_desc *desc)
d8f388d8 967{
6a99ad4a 968 int status = 0;
864533ce 969 struct device *dev = NULL;
d8f388d8 970
372e722e 971 if (!desc) {
bcabdef1
AC
972 pr_warn("%s: invalid GPIO\n", __func__);
973 return;
6a99ad4a 974 }
d8f388d8
DB
975
976 mutex_lock(&sysfs_lock);
977
d8f388d8 978 if (test_bit(FLAG_EXPORT, &desc->flags)) {
d8f388d8
DB
979
980 dev = class_find_device(&gpio_class, NULL, desc, match_export);
981 if (dev) {
ff77c352 982 gpio_setup_irq(desc, dev, 0);
d8f388d8 983 clear_bit(FLAG_EXPORT, &desc->flags);
d8f388d8
DB
984 } else
985 status = -ENODEV;
986 }
987
988 mutex_unlock(&sysfs_lock);
372e722e 989
864533ce
ML
990 if (dev) {
991 device_unregister(dev);
992 put_device(dev);
993 }
bcabdef1 994
d8f388d8 995 if (status)
372e722e
AC
996 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
997 status);
998}
79a9becd 999EXPORT_SYMBOL_GPL(gpiod_unexport);
d8f388d8
DB
1000
1001static int gpiochip_export(struct gpio_chip *chip)
1002{
1003 int status;
1004 struct device *dev;
1005
1006 /* Many systems register gpio chips for SOC support very early,
1007 * before driver model support is available. In those cases we
1008 * export this later, in gpiolib_sysfs_init() ... here we just
1009 * verify that _some_ field of gpio_class got initialized.
1010 */
1011 if (!gpio_class.p)
1012 return 0;
1013
1014 /* use chip->base for the ID; it's already known to be unique */
1015 mutex_lock(&sysfs_lock);
1016 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
1017 "gpiochip%d", chip->base);
d62668e1 1018 if (!IS_ERR(dev)) {
d8f388d8
DB
1019 status = sysfs_create_group(&dev->kobj,
1020 &gpiochip_attr_group);
1021 } else
d62668e1 1022 status = PTR_ERR(dev);
d8f388d8
DB
1023 chip->exported = (status == 0);
1024 mutex_unlock(&sysfs_lock);
1025
1026 if (status) {
1027 unsigned long flags;
1028 unsigned gpio;
1029
1030 spin_lock_irqsave(&gpio_lock, flags);
6c0b4e6c
AC
1031 gpio = 0;
1032 while (gpio < chip->ngpio)
1033 chip->desc[gpio++].chip = NULL;
d8f388d8
DB
1034 spin_unlock_irqrestore(&gpio_lock, flags);
1035
1036 pr_debug("%s: chip %s status %d\n", __func__,
1037 chip->label, status);
1038 }
1039
1040 return status;
1041}
1042
1043static void gpiochip_unexport(struct gpio_chip *chip)
1044{
1045 int status;
1046 struct device *dev;
1047
1048 mutex_lock(&sysfs_lock);
1049 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1050 if (dev) {
1051 put_device(dev);
1052 device_unregister(dev);
1053 chip->exported = 0;
1054 status = 0;
1055 } else
1056 status = -ENODEV;
1057 mutex_unlock(&sysfs_lock);
1058
1059 if (status)
1060 pr_debug("%s: chip %s status %d\n", __func__,
1061 chip->label, status);
1062}
1063
1064static int __init gpiolib_sysfs_init(void)
1065{
1066 int status;
1067 unsigned long flags;
65493e3a 1068 struct gpio_chip *chip;
d8f388d8
DB
1069
1070 status = class_register(&gpio_class);
1071 if (status < 0)
1072 return status;
1073
1074 /* Scan and register the gpio_chips which registered very
1075 * early (e.g. before the class_register above was called).
1076 *
1077 * We run before arch_initcall() so chip->dev nodes can have
1078 * registered, and so arch_initcall() can always gpio_export().
1079 */
1080 spin_lock_irqsave(&gpio_lock, flags);
65493e3a 1081 list_for_each_entry(chip, &gpio_chips, list) {
d8f388d8
DB
1082 if (!chip || chip->exported)
1083 continue;
1084
1085 spin_unlock_irqrestore(&gpio_lock, flags);
1086 status = gpiochip_export(chip);
1087 spin_lock_irqsave(&gpio_lock, flags);
1088 }
1089 spin_unlock_irqrestore(&gpio_lock, flags);
1090
1091
1092 return status;
1093}
1094postcore_initcall(gpiolib_sysfs_init);
1095
1096#else
1097static inline int gpiochip_export(struct gpio_chip *chip)
1098{
1099 return 0;
1100}
1101
1102static inline void gpiochip_unexport(struct gpio_chip *chip)
1103{
1104}
1105
1106#endif /* CONFIG_GPIO_SYSFS */
1107
1a989d0f
AC
1108/*
1109 * Add a new chip to the global chips list, keeping the list of chips sorted
1110 * by base order.
1111 *
1112 * Return -EBUSY if the new chip overlaps with some other chip's integer
1113 * space.
1114 */
1115static int gpiochip_add_to_list(struct gpio_chip *chip)
1116{
1117 struct list_head *pos = &gpio_chips;
1118 struct gpio_chip *_chip;
1119 int err = 0;
1120
1121 /* find where to insert our chip */
1122 list_for_each(pos, &gpio_chips) {
1123 _chip = list_entry(pos, struct gpio_chip, list);
1124 /* shall we insert before _chip? */
1125 if (_chip->base >= chip->base + chip->ngpio)
1126 break;
1127 }
1128
1129 /* are we stepping on the chip right before? */
1130 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1131 _chip = list_entry(pos->prev, struct gpio_chip, list);
1132 if (_chip->base + _chip->ngpio > chip->base) {
1133 dev_err(chip->dev,
1134 "GPIO integer space overlap, cannot add chip\n");
1135 err = -EBUSY;
1136 }
1137 }
1138
1139 if (!err)
1140 list_add_tail(&chip->list, pos);
1141
1142 return err;
1143}
1144
d2876d08
DB
1145/**
1146 * gpiochip_add() - register a gpio_chip
1147 * @chip: the chip to register, with chip->base initialized
1148 * Context: potentially before irqs or kmalloc will work
1149 *
1150 * Returns a negative errno if the chip can't be registered, such as
1151 * because the chip->base is invalid or already associated with a
1152 * different chip. Otherwise it returns zero as a success code.
8d0aab2f 1153 *
d8f388d8
DB
1154 * When gpiochip_add() is called very early during boot, so that GPIOs
1155 * can be freely used, the chip->dev device must be registered before
1156 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1157 * for GPIOs will fail rudely.
1158 *
8d0aab2f
AV
1159 * If chip->base is negative, this requests dynamic assignment of
1160 * a range of valid GPIOs.
d2876d08
DB
1161 */
1162int gpiochip_add(struct gpio_chip *chip)
1163{
1164 unsigned long flags;
1165 int status = 0;
1166 unsigned id;
8d0aab2f 1167 int base = chip->base;
d2876d08 1168
bff5fda9 1169 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
8d0aab2f 1170 && base >= 0) {
d2876d08
DB
1171 status = -EINVAL;
1172 goto fail;
1173 }
1174
1175 spin_lock_irqsave(&gpio_lock, flags);
1176
8d0aab2f
AV
1177 if (base < 0) {
1178 base = gpiochip_find_base(chip->ngpio);
1179 if (base < 0) {
1180 status = base;
d8f388d8 1181 goto unlock;
8d0aab2f
AV
1182 }
1183 chip->base = base;
1184 }
1185
1a989d0f
AC
1186 status = gpiochip_add_to_list(chip);
1187
d2876d08 1188 if (status == 0) {
6c0b4e6c
AC
1189 chip->desc = &gpio_desc[chip->base];
1190
1191 for (id = 0; id < chip->ngpio; id++) {
1192 struct gpio_desc *desc = &chip->desc[id];
1193 desc->chip = chip;
d8f388d8
DB
1194
1195 /* REVISIT: most hardware initializes GPIOs as
1196 * inputs (often with pullups enabled) so power
1197 * usage is minimized. Linux code should set the
1198 * gpio direction first thing; but until it does,
80b0a602 1199 * and in case chip->get_direction is not set,
d8f388d8
DB
1200 * we may expose the wrong direction in sysfs.
1201 */
6c0b4e6c 1202 desc->flags = !chip->direction_input
d8f388d8
DB
1203 ? (1 << FLAG_IS_OUT)
1204 : 0;
d2876d08
DB
1205 }
1206 }
1207
3bae4811
ZG
1208 spin_unlock_irqrestore(&gpio_lock, flags);
1209
f23f1516
SH
1210#ifdef CONFIG_PINCTRL
1211 INIT_LIST_HEAD(&chip->pin_ranges);
1212#endif
1213
391c970c
AV
1214 of_gpiochip_add(chip);
1215
cedb1881
AV
1216 if (status)
1217 goto fail;
1218
1219 status = gpiochip_export(chip);
1220 if (status)
1221 goto fail;
1222
ee1c1e7d 1223 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
64842aad
GL
1224 chip->base, chip->base + chip->ngpio - 1,
1225 chip->label ? : "generic");
1226
cedb1881 1227 return 0;
3bae4811
ZG
1228
1229unlock:
1230 spin_unlock_irqrestore(&gpio_lock, flags);
d2876d08
DB
1231fail:
1232 /* failures here can mean systems won't boot... */
cedb1881
AV
1233 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1234 chip->base, chip->base + chip->ngpio - 1,
1235 chip->label ? : "generic");
d2876d08
DB
1236 return status;
1237}
1238EXPORT_SYMBOL_GPL(gpiochip_add);
1239
1240/**
1241 * gpiochip_remove() - unregister a gpio_chip
1242 * @chip: the chip to unregister
1243 *
1244 * A gpio_chip with any GPIOs still requested may not be removed.
1245 */
1246int gpiochip_remove(struct gpio_chip *chip)
1247{
1248 unsigned long flags;
1249 int status = 0;
1250 unsigned id;
1251
1252 spin_lock_irqsave(&gpio_lock, flags);
1253
9ef0d6f7 1254 gpiochip_remove_pin_ranges(chip);
391c970c
AV
1255 of_gpiochip_remove(chip);
1256
6c0b4e6c
AC
1257 for (id = 0; id < chip->ngpio; id++) {
1258 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
d2876d08
DB
1259 status = -EBUSY;
1260 break;
1261 }
1262 }
1263 if (status == 0) {
6c0b4e6c
AC
1264 for (id = 0; id < chip->ngpio; id++)
1265 chip->desc[id].chip = NULL;
1a989d0f
AC
1266
1267 list_del(&chip->list);
d2876d08
DB
1268 }
1269
1270 spin_unlock_irqrestore(&gpio_lock, flags);
d8f388d8
DB
1271
1272 if (status == 0)
1273 gpiochip_unexport(chip);
1274
d2876d08
DB
1275 return status;
1276}
1277EXPORT_SYMBOL_GPL(gpiochip_remove);
1278
594fa265
GL
1279/**
1280 * gpiochip_find() - iterator for locating a specific gpio_chip
1281 * @data: data to pass to match function
1282 * @callback: Callback function to check gpio_chip
1283 *
1284 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1285 * determined by a user supplied @match callback. The callback should return
1286 * 0 if the device doesn't match and non-zero if it does. If the callback is
1287 * non-zero, this function will return to the caller and not iterate over any
1288 * more gpio_chips.
1289 */
07ce8ec7 1290struct gpio_chip *gpiochip_find(void *data,
6e2cf651 1291 int (*match)(struct gpio_chip *chip,
3d0f7cf0 1292 void *data))
594fa265 1293{
125eef96 1294 struct gpio_chip *chip;
594fa265 1295 unsigned long flags;
594fa265
GL
1296
1297 spin_lock_irqsave(&gpio_lock, flags);
125eef96
AC
1298 list_for_each_entry(chip, &gpio_chips, list)
1299 if (match(chip, data))
594fa265 1300 break;
125eef96
AC
1301
1302 /* No match? */
1303 if (&chip->list == &gpio_chips)
1304 chip = NULL;
594fa265
GL
1305 spin_unlock_irqrestore(&gpio_lock, flags);
1306
1307 return chip;
1308}
8fa0c9bf 1309EXPORT_SYMBOL_GPL(gpiochip_find);
d2876d08 1310
79697ef9
AC
1311static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1312{
1313 const char *name = data;
1314
1315 return !strcmp(chip->label, name);
1316}
1317
1318static struct gpio_chip *find_chip_by_name(const char *name)
1319{
1320 return gpiochip_find((void *)name, gpiochip_match_name);
1321}
1322
f23f1516 1323#ifdef CONFIG_PINCTRL
165adc9c 1324
586a87e6
CR
1325/**
1326 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1327 * @chip: the gpiochip to add the range for
1328 * @pinctrl: the dev_name() of the pin controller to map to
1329 * @gpio_offset: the start offset in the current gpio_chip number space
1330 * @pin_group: name of the pin group inside the pin controller
1331 */
1332int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1333 struct pinctrl_dev *pctldev,
1334 unsigned int gpio_offset, const char *pin_group)
1335{
1336 struct gpio_pin_range *pin_range;
1337 int ret;
1338
1339 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1340 if (!pin_range) {
1341 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1342 chip->label);
1343 return -ENOMEM;
1344 }
1345
1346 /* Use local offset as range ID */
1347 pin_range->range.id = gpio_offset;
1348 pin_range->range.gc = chip;
1349 pin_range->range.name = chip->label;
1350 pin_range->range.base = chip->base + gpio_offset;
1351 pin_range->pctldev = pctldev;
1352
1353 ret = pinctrl_get_group_pins(pctldev, pin_group,
1354 &pin_range->range.pins,
1355 &pin_range->range.npins);
1356 if (ret < 0)
1357 return ret;
1358
1359 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1360
1361 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PINGRP %s\n",
1362 chip->label, gpio_offset,
1363 gpio_offset + pin_range->range.npins - 1,
1364 pinctrl_dev_get_devname(pctldev), pin_group);
1365
1366 list_add_tail(&pin_range->node, &chip->pin_ranges);
1367
1368 return 0;
1369}
1370EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1371
3f0f8670
LW
1372/**
1373 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1374 * @chip: the gpiochip to add the range for
1375 * @pinctrl_name: the dev_name() of the pin controller to map to
316511c0
LW
1376 * @gpio_offset: the start offset in the current gpio_chip number space
1377 * @pin_offset: the start offset in the pin controller number space
3f0f8670
LW
1378 * @npins: the number of pins from the offset of each pin space (GPIO and
1379 * pin controller) to accumulate in this range
1380 */
1e63d7b9 1381int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
316511c0 1382 unsigned int gpio_offset, unsigned int pin_offset,
3f0f8670 1383 unsigned int npins)
f23f1516
SH
1384{
1385 struct gpio_pin_range *pin_range;
b4d4b1f0 1386 int ret;
f23f1516 1387
3f0f8670 1388 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
f23f1516
SH
1389 if (!pin_range) {
1390 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1391 chip->label);
1e63d7b9 1392 return -ENOMEM;
f23f1516
SH
1393 }
1394
3f0f8670 1395 /* Use local offset as range ID */
316511c0 1396 pin_range->range.id = gpio_offset;
3f0f8670 1397 pin_range->range.gc = chip;
f23f1516 1398 pin_range->range.name = chip->label;
316511c0
LW
1399 pin_range->range.base = chip->base + gpio_offset;
1400 pin_range->range.pin_base = pin_offset;
f23f1516 1401 pin_range->range.npins = npins;
192c369c 1402 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
f23f1516 1403 &pin_range->range);
8f23ca1a 1404 if (IS_ERR(pin_range->pctldev)) {
b4d4b1f0 1405 ret = PTR_ERR(pin_range->pctldev);
3f0f8670
LW
1406 pr_err("%s: GPIO chip: could not create pin range\n",
1407 chip->label);
1408 kfree(pin_range);
b4d4b1f0 1409 return ret;
3f0f8670 1410 }
316511c0
LW
1411 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1412 chip->label, gpio_offset, gpio_offset + npins - 1,
1413 pinctl_name,
1414 pin_offset, pin_offset + npins - 1);
f23f1516
SH
1415
1416 list_add_tail(&pin_range->node, &chip->pin_ranges);
1e63d7b9
LW
1417
1418 return 0;
f23f1516 1419}
165adc9c 1420EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
f23f1516 1421
3f0f8670
LW
1422/**
1423 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1424 * @chip: the chip to remove all the mappings for
1425 */
f23f1516
SH
1426void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1427{
1428 struct gpio_pin_range *pin_range, *tmp;
1429
1430 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1431 list_del(&pin_range->node);
1432 pinctrl_remove_gpio_range(pin_range->pctldev,
1433 &pin_range->range);
3f0f8670 1434 kfree(pin_range);
f23f1516
SH
1435 }
1436}
165adc9c
LW
1437EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1438
1439#endif /* CONFIG_PINCTRL */
f23f1516 1440
d2876d08
DB
1441/* These "optional" allocation calls help prevent drivers from stomping
1442 * on each other, and help provide better diagnostics in debugfs.
1443 * They're called even less than the "set direction" calls.
1444 */
372e722e 1445static int gpiod_request(struct gpio_desc *desc, const char *label)
d2876d08 1446{
35e8bb51 1447 struct gpio_chip *chip;
e9354576 1448 int status = -EPROBE_DEFER;
d2876d08
DB
1449 unsigned long flags;
1450
0204df47 1451 if (!desc) {
bcabdef1
AC
1452 pr_warn("%s: invalid GPIO\n", __func__);
1453 return -EINVAL;
ad2fab36 1454 }
bcabdef1
AC
1455
1456 spin_lock_irqsave(&gpio_lock, flags);
1457
35e8bb51 1458 chip = desc->chip;
0204df47
AC
1459 if (chip == NULL)
1460 goto done;
d2876d08 1461
35e8bb51 1462 if (!try_module_get(chip->owner))
438d8908
GL
1463 goto done;
1464
d2876d08 1465 /* NOTE: gpio_request() can be called in early boot,
35e8bb51 1466 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
d2876d08
DB
1467 */
1468
1469 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1470 desc_set_label(desc, label ? : "?");
1471 status = 0;
438d8908 1472 } else {
d2876d08 1473 status = -EBUSY;
35e8bb51 1474 module_put(chip->owner);
7460db56 1475 goto done;
35e8bb51
DB
1476 }
1477
1478 if (chip->request) {
1479 /* chip->request may sleep */
1480 spin_unlock_irqrestore(&gpio_lock, flags);
372e722e 1481 status = chip->request(chip, gpio_chip_hwgpio(desc));
35e8bb51
DB
1482 spin_lock_irqsave(&gpio_lock, flags);
1483
1484 if (status < 0) {
1485 desc_set_label(desc, NULL);
1486 module_put(chip->owner);
1487 clear_bit(FLAG_REQUESTED, &desc->flags);
80b0a602 1488 goto done;
35e8bb51 1489 }
438d8908 1490 }
80b0a602
MN
1491 if (chip->get_direction) {
1492 /* chip->get_direction may sleep */
1493 spin_unlock_irqrestore(&gpio_lock, flags);
372e722e 1494 gpiod_get_direction(desc);
80b0a602
MN
1495 spin_lock_irqsave(&gpio_lock, flags);
1496 }
d2876d08
DB
1497done:
1498 if (status)
372e722e 1499 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
bcabdef1 1500 desc_to_gpio(desc), label ? : "?", status);
d2876d08
DB
1501 spin_unlock_irqrestore(&gpio_lock, flags);
1502 return status;
1503}
372e722e
AC
1504
1505int gpio_request(unsigned gpio, const char *label)
1506{
1507 return gpiod_request(gpio_to_desc(gpio), label);
1508}
d2876d08
DB
1509EXPORT_SYMBOL_GPL(gpio_request);
1510
372e722e 1511static void gpiod_free(struct gpio_desc *desc)
d2876d08
DB
1512{
1513 unsigned long flags;
35e8bb51 1514 struct gpio_chip *chip;
d2876d08 1515
3d599d1c
UKK
1516 might_sleep();
1517
372e722e 1518 if (!desc) {
d2876d08
DB
1519 WARN_ON(extra_checks);
1520 return;
1521 }
1522
372e722e 1523 gpiod_unexport(desc);
d8f388d8 1524
d2876d08
DB
1525 spin_lock_irqsave(&gpio_lock, flags);
1526
35e8bb51
DB
1527 chip = desc->chip;
1528 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1529 if (chip->free) {
1530 spin_unlock_irqrestore(&gpio_lock, flags);
9c4ba946 1531 might_sleep_if(chip->can_sleep);
372e722e 1532 chip->free(chip, gpio_chip_hwgpio(desc));
35e8bb51
DB
1533 spin_lock_irqsave(&gpio_lock, flags);
1534 }
d2876d08 1535 desc_set_label(desc, NULL);
438d8908 1536 module_put(desc->chip->owner);
07697461 1537 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
35e8bb51 1538 clear_bit(FLAG_REQUESTED, &desc->flags);
aca5ce14 1539 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
25553ff0 1540 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
438d8908 1541 } else
d2876d08
DB
1542 WARN_ON(extra_checks);
1543
1544 spin_unlock_irqrestore(&gpio_lock, flags);
1545}
372e722e
AC
1546
1547void gpio_free(unsigned gpio)
1548{
1549 gpiod_free(gpio_to_desc(gpio));
1550}
d2876d08
DB
1551EXPORT_SYMBOL_GPL(gpio_free);
1552
3e45f1d1
EM
1553/**
1554 * gpio_request_one - request a single GPIO with initial configuration
1555 * @gpio: the GPIO number
1556 * @flags: GPIO configuration as specified by GPIOF_*
1557 * @label: a literal description string of this GPIO
1558 */
1559int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1560{
372e722e 1561 struct gpio_desc *desc;
3e45f1d1
EM
1562 int err;
1563
372e722e
AC
1564 desc = gpio_to_desc(gpio);
1565
1566 err = gpiod_request(desc, label);
3e45f1d1
EM
1567 if (err)
1568 return err;
1569
aca5ce14 1570 if (flags & GPIOF_OPEN_DRAIN)
372e722e 1571 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
aca5ce14 1572
25553ff0 1573 if (flags & GPIOF_OPEN_SOURCE)
372e722e 1574 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
25553ff0 1575
3e45f1d1 1576 if (flags & GPIOF_DIR_IN)
372e722e 1577 err = gpiod_direction_input(desc);
3e45f1d1 1578 else
372e722e 1579 err = gpiod_direction_output(desc,
3e45f1d1
EM
1580 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1581
e254811c 1582 if (err)
fc3a1f04
WS
1583 goto free_gpio;
1584
1585 if (flags & GPIOF_EXPORT) {
372e722e 1586 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
fc3a1f04
WS
1587 if (err)
1588 goto free_gpio;
1589 }
1590
1591 return 0;
e254811c 1592
fc3a1f04 1593 free_gpio:
372e722e 1594 gpiod_free(desc);
3e45f1d1
EM
1595 return err;
1596}
1597EXPORT_SYMBOL_GPL(gpio_request_one);
1598
1599/**
1600 * gpio_request_array - request multiple GPIOs in a single call
1601 * @array: array of the 'struct gpio'
1602 * @num: how many GPIOs in the array
1603 */
7c295975 1604int gpio_request_array(const struct gpio *array, size_t num)
3e45f1d1
EM
1605{
1606 int i, err;
1607
1608 for (i = 0; i < num; i++, array++) {
1609 err = gpio_request_one(array->gpio, array->flags, array->label);
1610 if (err)
1611 goto err_free;
1612 }
1613 return 0;
1614
1615err_free:
1616 while (i--)
1617 gpio_free((--array)->gpio);
1618 return err;
1619}
1620EXPORT_SYMBOL_GPL(gpio_request_array);
1621
1622/**
1623 * gpio_free_array - release multiple GPIOs in a single call
1624 * @array: array of the 'struct gpio'
1625 * @num: how many GPIOs in the array
1626 */
7c295975 1627void gpio_free_array(const struct gpio *array, size_t num)
3e45f1d1
EM
1628{
1629 while (num--)
1630 gpio_free((array++)->gpio);
1631}
1632EXPORT_SYMBOL_GPL(gpio_free_array);
d2876d08
DB
1633
1634/**
1635 * gpiochip_is_requested - return string iff signal was requested
1636 * @chip: controller managing the signal
1637 * @offset: of signal within controller's 0..(ngpio - 1) range
1638 *
1639 * Returns NULL if the GPIO is not currently requested, else a string.
1640 * If debugfs support is enabled, the string returned is the label passed
1641 * to gpio_request(); otherwise it is a meaningless constant.
1642 *
1643 * This function is for use by GPIO controller drivers. The label can
1644 * help with diagnostics, and knowing that the signal is used as a GPIO
1645 * can help avoid accidentally multiplexing it to another controller.
1646 */
1647const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1648{
6c0b4e6c 1649 struct gpio_desc *desc;
d2876d08 1650
6c0b4e6c 1651 if (!GPIO_OFFSET_VALID(chip, offset))
d2876d08 1652 return NULL;
6c0b4e6c
AC
1653
1654 desc = &chip->desc[offset];
1655
372e722e 1656 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
d2876d08
DB
1657 return NULL;
1658#ifdef CONFIG_DEBUG_FS
372e722e 1659 return desc->label;
d2876d08
DB
1660#else
1661 return "?";
1662#endif
1663}
1664EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1665
1666
1667/* Drivers MUST set GPIO direction before making get/set calls. In
1668 * some cases this is done in early boot, before IRQs are enabled.
1669 *
1670 * As a rule these aren't called more than once (except for drivers
1671 * using the open-drain emulation idiom) so these are natural places
1672 * to accumulate extra debugging checks. Note that we can't (yet)
1673 * rely on gpio_request() having been called beforehand.
1674 */
1675
79a9becd
AC
1676/**
1677 * gpiod_direction_input - set the GPIO direction to input
1678 * @desc: GPIO to set to input
1679 *
1680 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1681 * be called safely on it.
1682 *
1683 * Return 0 in case of success, else an error code.
1684 */
1685int gpiod_direction_input(struct gpio_desc *desc)
d2876d08
DB
1686{
1687 unsigned long flags;
1688 struct gpio_chip *chip;
d2876d08 1689 int status = -EINVAL;
372e722e 1690 int offset;
d2876d08 1691
be1a4b13 1692 if (!desc || !desc->chip) {
bcabdef1
AC
1693 pr_warn("%s: invalid GPIO\n", __func__);
1694 return -EINVAL;
1695 }
1696
be1a4b13
LW
1697 chip = desc->chip;
1698 if (!chip->get || !chip->direction_input) {
6424de5a
MB
1699 gpiod_warn(desc,
1700 "%s: missing get() or direction_input() operations\n",
1701 __func__);
be1a4b13
LW
1702 return -EIO;
1703 }
1704
d2876d08
DB
1705 spin_lock_irqsave(&gpio_lock, flags);
1706
372e722e 1707 status = gpio_ensure_requested(desc);
35e8bb51
DB
1708 if (status < 0)
1709 goto fail;
d2876d08
DB
1710
1711 /* now we know the gpio is valid and chip won't vanish */
1712
1713 spin_unlock_irqrestore(&gpio_lock, flags);
1714
9c4ba946 1715 might_sleep_if(chip->can_sleep);
d2876d08 1716
372e722e 1717 offset = gpio_chip_hwgpio(desc);
35e8bb51 1718 if (status) {
372e722e 1719 status = chip->request(chip, offset);
35e8bb51 1720 if (status < 0) {
6424de5a 1721 gpiod_dbg(desc, "chip request fail, %d\n", status);
35e8bb51
DB
1722 /* and it's not available to anyone else ...
1723 * gpio_request() is the fully clean solution.
1724 */
1725 goto lose;
1726 }
1727 }
1728
372e722e 1729 status = chip->direction_input(chip, offset);
d2876d08
DB
1730 if (status == 0)
1731 clear_bit(FLAG_IS_OUT, &desc->flags);
3f397c21 1732
372e722e 1733 trace_gpio_direction(desc_to_gpio(desc), 1, status);
35e8bb51 1734lose:
d2876d08
DB
1735 return status;
1736fail:
1737 spin_unlock_irqrestore(&gpio_lock, flags);
bcabdef1 1738 if (status)
6424de5a 1739 gpiod_dbg(desc, "%s status %d\n", __func__, status);
d2876d08
DB
1740 return status;
1741}
79a9becd 1742EXPORT_SYMBOL_GPL(gpiod_direction_input);
372e722e 1743
79a9becd
AC
1744/**
1745 * gpiod_direction_output - set the GPIO direction to input
1746 * @desc: GPIO to set to output
1747 * @value: initial output value of the GPIO
1748 *
1749 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1750 * be called safely on it. The initial value of the output must be specified.
1751 *
1752 * Return 0 in case of success, else an error code.
1753 */
1754int gpiod_direction_output(struct gpio_desc *desc, int value)
d2876d08
DB
1755{
1756 unsigned long flags;
1757 struct gpio_chip *chip;
d2876d08 1758 int status = -EINVAL;
372e722e 1759 int offset;
d2876d08 1760
be1a4b13 1761 if (!desc || !desc->chip) {
bcabdef1
AC
1762 pr_warn("%s: invalid GPIO\n", __func__);
1763 return -EINVAL;
1764 }
1765
d468bf9e
LW
1766 /* GPIOs used for IRQs shall not be set as output */
1767 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1768 gpiod_err(desc,
1769 "%s: tried to set a GPIO tied to an IRQ as output\n",
1770 __func__);
1771 return -EIO;
1772 }
1773
aca5ce14
LD
1774 /* Open drain pin should not be driven to 1 */
1775 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
372e722e 1776 return gpiod_direction_input(desc);
aca5ce14 1777
25553ff0
LD
1778 /* Open source pin should not be driven to 0 */
1779 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
372e722e 1780 return gpiod_direction_input(desc);
25553ff0 1781
be1a4b13
LW
1782 chip = desc->chip;
1783 if (!chip->set || !chip->direction_output) {
6424de5a
MB
1784 gpiod_warn(desc,
1785 "%s: missing set() or direction_output() operations\n",
1786 __func__);
be1a4b13
LW
1787 return -EIO;
1788 }
1789
d2876d08
DB
1790 spin_lock_irqsave(&gpio_lock, flags);
1791
372e722e 1792 status = gpio_ensure_requested(desc);
35e8bb51
DB
1793 if (status < 0)
1794 goto fail;
d2876d08
DB
1795
1796 /* now we know the gpio is valid and chip won't vanish */
1797
1798 spin_unlock_irqrestore(&gpio_lock, flags);
1799
9c4ba946 1800 might_sleep_if(chip->can_sleep);
d2876d08 1801
372e722e 1802 offset = gpio_chip_hwgpio(desc);
35e8bb51 1803 if (status) {
372e722e 1804 status = chip->request(chip, offset);
35e8bb51 1805 if (status < 0) {
6424de5a 1806 gpiod_dbg(desc, "chip request fail, %d\n", status);
35e8bb51
DB
1807 /* and it's not available to anyone else ...
1808 * gpio_request() is the fully clean solution.
1809 */
1810 goto lose;
1811 }
1812 }
1813
372e722e 1814 status = chip->direction_output(chip, offset, value);
d2876d08
DB
1815 if (status == 0)
1816 set_bit(FLAG_IS_OUT, &desc->flags);
372e722e
AC
1817 trace_gpio_value(desc_to_gpio(desc), 0, value);
1818 trace_gpio_direction(desc_to_gpio(desc), 0, status);
35e8bb51 1819lose:
d2876d08
DB
1820 return status;
1821fail:
1822 spin_unlock_irqrestore(&gpio_lock, flags);
bcabdef1 1823 if (status)
6424de5a 1824 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
d2876d08
DB
1825 return status;
1826}
79a9becd 1827EXPORT_SYMBOL_GPL(gpiod_direction_output);
d2876d08 1828
c4b5be98 1829/**
79a9becd 1830 * gpiod_set_debounce - sets @debounce time for a @gpio
c4b5be98
FB
1831 * @gpio: the gpio to set debounce time
1832 * @debounce: debounce time is microseconds
65d87656
LW
1833 *
1834 * returns -ENOTSUPP if the controller does not support setting
1835 * debounce.
c4b5be98 1836 */
79a9becd 1837int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
c4b5be98
FB
1838{
1839 unsigned long flags;
1840 struct gpio_chip *chip;
c4b5be98 1841 int status = -EINVAL;
372e722e 1842 int offset;
c4b5be98 1843
be1a4b13 1844 if (!desc || !desc->chip) {
bcabdef1
AC
1845 pr_warn("%s: invalid GPIO\n", __func__);
1846 return -EINVAL;
1847 }
1848
c4b5be98 1849 chip = desc->chip;
be1a4b13 1850 if (!chip->set || !chip->set_debounce) {
6424de5a
MB
1851 gpiod_dbg(desc,
1852 "%s: missing set() or set_debounce() operations\n",
1853 __func__);
65d87656 1854 return -ENOTSUPP;
be1a4b13
LW
1855 }
1856
1857 spin_lock_irqsave(&gpio_lock, flags);
372e722e
AC
1858
1859 status = gpio_ensure_requested(desc);
c4b5be98
FB
1860 if (status < 0)
1861 goto fail;
1862
1863 /* now we know the gpio is valid and chip won't vanish */
1864
1865 spin_unlock_irqrestore(&gpio_lock, flags);
1866
9c4ba946 1867 might_sleep_if(chip->can_sleep);
c4b5be98 1868
372e722e
AC
1869 offset = gpio_chip_hwgpio(desc);
1870 return chip->set_debounce(chip, offset, debounce);
c4b5be98
FB
1871
1872fail:
1873 spin_unlock_irqrestore(&gpio_lock, flags);
bcabdef1 1874 if (status)
6424de5a 1875 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
c4b5be98
FB
1876
1877 return status;
1878}
79a9becd 1879EXPORT_SYMBOL_GPL(gpiod_set_debounce);
372e722e 1880
79a9becd
AC
1881/**
1882 * gpiod_is_active_low - test whether a GPIO is active-low or not
1883 * @desc: the gpio descriptor to test
1884 *
1885 * Returns 1 if the GPIO is active-low, 0 otherwise.
1886 */
1887int gpiod_is_active_low(const struct gpio_desc *desc)
372e722e 1888{
79a9becd 1889 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
372e722e 1890}
79a9becd 1891EXPORT_SYMBOL_GPL(gpiod_is_active_low);
d2876d08
DB
1892
1893/* I/O calls are only valid after configuration completed; the relevant
1894 * "is this a valid GPIO" error checks should already have been done.
1895 *
1896 * "Get" operations are often inlinable as reading a pin value register,
1897 * and masking the relevant bit in that register.
1898 *
1899 * When "set" operations are inlinable, they involve writing that mask to
1900 * one register to set a low value, or a different register to set it high.
1901 * Otherwise locking is needed, so there may be little value to inlining.
1902 *
1903 *------------------------------------------------------------------------
1904 *
1905 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1906 * have requested the GPIO. That can include implicit requesting by
1907 * a direction setting call. Marking a gpio as requested locks its chip
1908 * in memory, guaranteeing that these table lookups need no more locking
1909 * and that gpiochip_remove() will fail.
1910 *
1911 * REVISIT when debugging, consider adding some instrumentation to ensure
1912 * that the GPIO was actually requested.
1913 */
1914
79a9becd 1915static int _gpiod_get_raw_value(const struct gpio_desc *desc)
d2876d08
DB
1916{
1917 struct gpio_chip *chip;
3f397c21 1918 int value;
372e722e 1919 int offset;
d2876d08 1920
372e722e
AC
1921 chip = desc->chip;
1922 offset = gpio_chip_hwgpio(desc);
372e722e
AC
1923 value = chip->get ? chip->get(chip, offset) : 0;
1924 trace_gpio_value(desc_to_gpio(desc), 1, value);
3f397c21 1925 return value;
d2876d08 1926}
372e722e 1927
d2876d08 1928/**
79a9becd
AC
1929 * gpiod_get_raw_value() - return a gpio's raw value
1930 * @desc: gpio whose value will be returned
d2876d08 1931 *
79a9becd
AC
1932 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1933 * its ACTIVE_LOW status.
1934 *
1935 * This function should be called from contexts where we cannot sleep, and will
1936 * complain if the GPIO chip functions potentially sleep.
d2876d08 1937 */
79a9becd 1938int gpiod_get_raw_value(const struct gpio_desc *desc)
d2876d08 1939{
bcabdef1
AC
1940 if (!desc)
1941 return 0;
e4e449e8 1942 /* Should be using gpio_get_value_cansleep() */
d8e0ac08 1943 WARN_ON(desc->chip->can_sleep);
79a9becd 1944 return _gpiod_get_raw_value(desc);
d2876d08 1945}
79a9becd 1946EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
372e722e 1947
79a9becd
AC
1948/**
1949 * gpiod_get_value() - return a gpio's value
1950 * @desc: gpio whose value will be returned
1951 *
1952 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1953 * account.
1954 *
1955 * This function should be called from contexts where we cannot sleep, and will
1956 * complain if the GPIO chip functions potentially sleep.
1957 */
1958int gpiod_get_value(const struct gpio_desc *desc)
372e722e 1959{
79a9becd
AC
1960 int value;
1961 if (!desc)
1962 return 0;
1963 /* Should be using gpio_get_value_cansleep() */
1964 WARN_ON(desc->chip->can_sleep);
1965
1966 value = _gpiod_get_raw_value(desc);
1967 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1968 value = !value;
1969
1970 return value;
372e722e 1971}
79a9becd 1972EXPORT_SYMBOL_GPL(gpiod_get_value);
d2876d08 1973
aca5ce14
LD
1974/*
1975 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
79a9becd 1976 * @desc: gpio descriptor whose state need to be set.
aca5ce14
LD
1977 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1978 */
372e722e 1979static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
aca5ce14
LD
1980{
1981 int err = 0;
372e722e
AC
1982 struct gpio_chip *chip = desc->chip;
1983 int offset = gpio_chip_hwgpio(desc);
1984
aca5ce14 1985 if (value) {
372e722e 1986 err = chip->direction_input(chip, offset);
aca5ce14 1987 if (!err)
372e722e 1988 clear_bit(FLAG_IS_OUT, &desc->flags);
aca5ce14 1989 } else {
372e722e 1990 err = chip->direction_output(chip, offset, 0);
aca5ce14 1991 if (!err)
372e722e 1992 set_bit(FLAG_IS_OUT, &desc->flags);
aca5ce14 1993 }
372e722e 1994 trace_gpio_direction(desc_to_gpio(desc), value, err);
aca5ce14 1995 if (err < 0)
6424de5a
MB
1996 gpiod_err(desc,
1997 "%s: Error in set_value for open drain err %d\n",
1998 __func__, err);
aca5ce14
LD
1999}
2000
25553ff0 2001/*
79a9becd
AC
2002 * _gpio_set_open_source_value() - Set the open source gpio's value.
2003 * @desc: gpio descriptor whose state need to be set.
25553ff0
LD
2004 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
2005 */
372e722e 2006static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
25553ff0
LD
2007{
2008 int err = 0;
372e722e
AC
2009 struct gpio_chip *chip = desc->chip;
2010 int offset = gpio_chip_hwgpio(desc);
2011
25553ff0 2012 if (value) {
372e722e 2013 err = chip->direction_output(chip, offset, 1);
25553ff0 2014 if (!err)
372e722e 2015 set_bit(FLAG_IS_OUT, &desc->flags);
25553ff0 2016 } else {
372e722e 2017 err = chip->direction_input(chip, offset);
25553ff0 2018 if (!err)
372e722e 2019 clear_bit(FLAG_IS_OUT, &desc->flags);
25553ff0 2020 }
372e722e 2021 trace_gpio_direction(desc_to_gpio(desc), !value, err);
25553ff0 2022 if (err < 0)
6424de5a
MB
2023 gpiod_err(desc,
2024 "%s: Error in set_value for open source err %d\n",
2025 __func__, err);
25553ff0
LD
2026}
2027
79a9becd 2028static void _gpiod_set_raw_value(struct gpio_desc *desc, int value)
d2876d08
DB
2029{
2030 struct gpio_chip *chip;
2031
372e722e 2032 chip = desc->chip;
372e722e
AC
2033 trace_gpio_value(desc_to_gpio(desc), 0, value);
2034 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2035 _gpio_set_open_drain_value(desc, value);
2036 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2037 _gpio_set_open_source_value(desc, value);
aca5ce14 2038 else
372e722e
AC
2039 chip->set(chip, gpio_chip_hwgpio(desc), value);
2040}
2041
d2876d08 2042/**
79a9becd
AC
2043 * gpiod_set_raw_value() - assign a gpio's raw value
2044 * @desc: gpio whose value will be assigned
d2876d08 2045 * @value: value to assign
d2876d08 2046 *
79a9becd
AC
2047 * Set the raw value of the GPIO, i.e. the value of its physical line without
2048 * regard for its ACTIVE_LOW status.
2049 *
2050 * This function should be called from contexts where we cannot sleep, and will
2051 * complain if the GPIO chip functions potentially sleep.
d2876d08 2052 */
79a9becd 2053void gpiod_set_raw_value(struct gpio_desc *desc, int value)
372e722e 2054{
bcabdef1
AC
2055 if (!desc)
2056 return;
e4e449e8 2057 /* Should be using gpio_set_value_cansleep() */
d8e0ac08 2058 WARN_ON(desc->chip->can_sleep);
79a9becd 2059 _gpiod_set_raw_value(desc, value);
d2876d08 2060}
79a9becd 2061EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
d2876d08
DB
2062
2063/**
79a9becd
AC
2064 * gpiod_set_value() - assign a gpio's value
2065 * @desc: gpio whose value will be assigned
2066 * @value: value to assign
2067 *
2068 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2069 * account
d2876d08 2070 *
79a9becd
AC
2071 * This function should be called from contexts where we cannot sleep, and will
2072 * complain if the GPIO chip functions potentially sleep.
d2876d08 2073 */
79a9becd 2074void gpiod_set_value(struct gpio_desc *desc, int value)
d2876d08 2075{
bcabdef1 2076 if (!desc)
79a9becd
AC
2077 return;
2078 /* Should be using gpio_set_value_cansleep() */
2079 WARN_ON(desc->chip->can_sleep);
2080 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2081 value = !value;
2082 _gpiod_set_raw_value(desc, value);
372e722e 2083}
79a9becd 2084EXPORT_SYMBOL_GPL(gpiod_set_value);
d2876d08 2085
d2876d08 2086/**
79a9becd
AC
2087 * gpiod_cansleep() - report whether gpio value access may sleep
2088 * @desc: gpio to check
d2876d08 2089 *
d2876d08 2090 */
79a9becd 2091int gpiod_cansleep(const struct gpio_desc *desc)
372e722e 2092{
bcabdef1
AC
2093 if (!desc)
2094 return 0;
372e722e 2095 return desc->chip->can_sleep;
d2876d08 2096}
79a9becd 2097EXPORT_SYMBOL_GPL(gpiod_cansleep);
d2876d08 2098
0f6d504e 2099/**
79a9becd
AC
2100 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2101 * @desc: gpio whose IRQ will be returned (already requested)
0f6d504e 2102 *
79a9becd
AC
2103 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2104 * error.
0f6d504e 2105 */
79a9becd 2106int gpiod_to_irq(const struct gpio_desc *desc)
0f6d504e
DB
2107{
2108 struct gpio_chip *chip;
372e722e 2109 int offset;
0f6d504e 2110
bcabdef1
AC
2111 if (!desc)
2112 return -EINVAL;
372e722e
AC
2113 chip = desc->chip;
2114 offset = gpio_chip_hwgpio(desc);
2115 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
0f6d504e 2116}
79a9becd 2117EXPORT_SYMBOL_GPL(gpiod_to_irq);
0f6d504e 2118
d468bf9e
LW
2119/**
2120 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
2121 * @gpio: the GPIO line to lock as used for IRQ
2122 *
2123 * This is used directly by GPIO drivers that want to lock down
2124 * a certain GPIO line to be used as IRQs, for example in the
2125 * .to_irq() callback of their gpio_chip, or in the .irq_enable()
2126 * of its irq_chip implementation if the GPIO is known from that
2127 * code.
2128 */
79a9becd 2129int gpiod_lock_as_irq(struct gpio_desc *desc)
372e722e 2130{
d468bf9e
LW
2131 if (!desc)
2132 return -EINVAL;
2133
2134 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
2135 gpiod_err(desc,
2136 "%s: tried to flag a GPIO set as output for IRQ\n",
2137 __func__);
2138 return -EIO;
2139 }
2140
2141 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
2142 return 0;
372e722e 2143}
79a9becd 2144EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);
d2876d08 2145
d468bf9e
LW
2146int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
2147{
2148 return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset));
2149}
2150EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
d2876d08 2151
d468bf9e
LW
2152/**
2153 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
2154 * @gpio: the GPIO line to unlock from IRQ usage
2155 *
2156 * This is used directly by GPIO drivers that want to indicate
2157 * that a certain GPIO is no longer used exclusively for IRQ.
d2876d08 2158 */
79a9becd 2159void gpiod_unlock_as_irq(struct gpio_desc *desc)
d468bf9e
LW
2160{
2161 if (!desc)
2162 return;
d2876d08 2163
d468bf9e
LW
2164 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2165}
79a9becd 2166EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq);
d468bf9e
LW
2167
2168void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2169{
2170 return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset));
2171}
2172EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
d2876d08 2173
79a9becd
AC
2174/**
2175 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2176 * @desc: gpio whose value will be returned
2177 *
2178 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2179 * its ACTIVE_LOW status.
2180 *
2181 * This function is to be called from contexts that can sleep.
d2876d08 2182 */
79a9becd 2183int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
d2876d08 2184{
d2876d08 2185 might_sleep_if(extra_checks);
bcabdef1
AC
2186 if (!desc)
2187 return 0;
79a9becd 2188 return _gpiod_get_raw_value(desc);
d2876d08 2189}
79a9becd 2190EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
372e722e 2191
79a9becd
AC
2192/**
2193 * gpiod_get_value_cansleep() - return a gpio's value
2194 * @desc: gpio whose value will be returned
2195 *
2196 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2197 * account.
2198 *
2199 * This function is to be called from contexts that can sleep.
2200 */
2201int gpiod_get_value_cansleep(const struct gpio_desc *desc)
d2876d08 2202{
3f397c21 2203 int value;
d2876d08
DB
2204
2205 might_sleep_if(extra_checks);
bcabdef1
AC
2206 if (!desc)
2207 return 0;
79a9becd
AC
2208
2209 value = _gpiod_get_raw_value(desc);
2210 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2211 value = !value;
2212
3f397c21 2213 return value;
d2876d08 2214}
79a9becd 2215EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
372e722e 2216
79a9becd
AC
2217/**
2218 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2219 * @desc: gpio whose value will be assigned
2220 * @value: value to assign
2221 *
2222 * Set the raw value of the GPIO, i.e. the value of its physical line without
2223 * regard for its ACTIVE_LOW status.
2224 *
2225 * This function is to be called from contexts that can sleep.
2226 */
2227void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
372e722e 2228{
d2876d08 2229 might_sleep_if(extra_checks);
bcabdef1
AC
2230 if (!desc)
2231 return;
79a9becd 2232 _gpiod_set_raw_value(desc, value);
372e722e 2233}
79a9becd 2234EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
d2876d08 2235
79a9becd
AC
2236/**
2237 * gpiod_set_value_cansleep() - assign a gpio's value
2238 * @desc: gpio whose value will be assigned
2239 * @value: value to assign
2240 *
2241 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2242 * account
2243 *
2244 * This function is to be called from contexts that can sleep.
2245 */
2246void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
d2876d08 2247{
d2876d08 2248 might_sleep_if(extra_checks);
bcabdef1
AC
2249 if (!desc)
2250 return;
79a9becd
AC
2251
2252 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2253 value = !value;
2254 _gpiod_set_raw_value(desc, value);
372e722e 2255}
79a9becd 2256EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
d2876d08 2257
bae48da2
AC
2258/**
2259 * gpiod_add_table() - register GPIO device consumers
2260 * @table: array of consumers to register
2261 * @num: number of consumers in table
2262 */
2263void gpiod_add_table(struct gpiod_lookup *table, size_t size)
2264{
2265 mutex_lock(&gpio_lookup_lock);
2266
2267 while (size--) {
2268 list_add_tail(&table->list, &gpio_lookup_list);
2269 table++;
2270 }
2271
2272 mutex_unlock(&gpio_lookup_lock);
2273}
2274
bae48da2
AC
2275#ifdef CONFIG_OF
2276static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
2277 unsigned int idx, unsigned long *flags)
2278{
2279 char prop_name[32]; /* 32 is max size of property name */
2280 enum of_gpio_flags of_flags;
2281 struct gpio_desc *desc;
2282
2283 if (con_id)
2284 snprintf(prop_name, 32, "%s-gpios", con_id);
aca5ce14 2285 else
bae48da2
AC
2286 snprintf(prop_name, 32, "gpios");
2287
2288 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2289 &of_flags);
2290
2291 if (IS_ERR(desc))
2292 return desc;
2293
2294 if (of_flags & OF_GPIO_ACTIVE_LOW)
2295 *flags |= GPIOF_ACTIVE_LOW;
2296
2297 return desc;
2298}
2299#else
2300static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
2301 unsigned int idx, unsigned long *flags)
2302{
2303 return ERR_PTR(-ENODEV);
d2876d08 2304}
bae48da2 2305#endif
d2876d08 2306
81f59e9d
MW
2307static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
2308 unsigned int idx, unsigned long *flags)
372e722e 2309{
e01f440a
MW
2310 struct acpi_gpio_info info;
2311 struct gpio_desc *desc;
2312
2313 desc = acpi_get_gpiod_by_index(dev, idx, &info);
2314 if (IS_ERR(desc))
2315 return desc;
2316
2317 if (info.gpioint && info.active_low)
2318 *flags |= GPIOF_ACTIVE_LOW;
2319
2320 return desc;
81f59e9d
MW
2321}
2322
bae48da2
AC
2323static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
2324 unsigned int idx, unsigned long *flags)
2325{
2326 const char *dev_id = dev ? dev_name(dev) : NULL;
2327 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2328 unsigned int match, best = 0;
2329 struct gpiod_lookup *p;
2330
2331 mutex_lock(&gpio_lookup_lock);
2332
2333 list_for_each_entry(p, &gpio_lookup_list, list) {
2334 match = 0;
2335
2336 if (p->dev_id) {
2337 if (!dev_id || strcmp(p->dev_id, dev_id))
2338 continue;
2339
2340 match += 2;
2341 }
2342
2343 if (p->con_id) {
2344 if (!con_id || strcmp(p->con_id, con_id))
2345 continue;
2346
2347 match += 1;
2348 }
2349
2350 if (p->idx != idx)
2351 continue;
2352
2353 if (match > best) {
2354 struct gpio_chip *chip;
2355
2356 chip = find_chip_by_name(p->chip_label);
2357
2358 if (!chip) {
2359 dev_warn(dev, "cannot find GPIO chip %s\n",
2360 p->chip_label);
2361 continue;
2362 }
2363
2364 if (chip->ngpio >= p->chip_hwnum) {
2365 dev_warn(dev, "GPIO chip %s has %d GPIOs\n",
2366 chip->label, chip->ngpio);
2367 continue;
2368 }
2369
2370 desc = gpio_to_desc(chip->base + p->chip_hwnum);
2371 *flags = p->flags;
2372
2373 if (match != 3)
2374 best = match;
2375 else
2376 break;
2377 }
2378 }
2379
2380 mutex_unlock(&gpio_lookup_lock);
2381
2382 return desc;
2383}
2384
2385/**
2386 * gpio_get - obtain a GPIO for a given GPIO function
2387 * @dev: GPIO consumer
2388 * @con_id: function within the GPIO consumer
2389 *
2390 * Return the GPIO descriptor corresponding to the function con_id of device
2391 * dev, or an IS_ERR() condition if an error occured.
2392 */
2393struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id)
2394{
2395 return gpiod_get_index(dev, con_id, 0);
2396}
2397EXPORT_SYMBOL_GPL(gpiod_get);
2398
2399/**
2400 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2401 * @dev: GPIO consumer
2402 * @con_id: function within the GPIO consumer
2403 * @idx: index of the GPIO to obtain in the consumer
2404 *
2405 * This variant of gpiod_get() allows to access GPIOs other than the first
2406 * defined one for functions that define several GPIOs.
2407 *
2408 * Return a valid GPIO descriptor, or an IS_ERR() condition in case of error.
2409 */
2410struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2411 const char *con_id,
2412 unsigned int idx)
2413{
2414 struct gpio_desc *desc;
2415 int status;
2416 unsigned long flags = 0;
2417
2418 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2419
2420 /* Using device tree? */
2421 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
2422 dev_dbg(dev, "using device tree for GPIO lookup\n");
2423 desc = of_find_gpio(dev, con_id, idx, &flags);
81f59e9d
MW
2424 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
2425 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2426 desc = acpi_find_gpio(dev, con_id, idx, &flags);
bae48da2
AC
2427 } else {
2428 dev_dbg(dev, "using lookup tables for GPIO lookup");
2429 desc = gpiod_find(dev, con_id, idx, &flags);
2430 }
2431
2432 if (IS_ERR(desc)) {
2433 dev_warn(dev, "lookup for GPIO %s failed\n", con_id);
2434 return desc;
2435 }
2436
2437 status = gpiod_request(desc, con_id);
2438
2439 if (status < 0)
2440 return ERR_PTR(status);
2441
2442 if (flags & GPIOF_ACTIVE_LOW)
2443 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2444
2445 return desc;
2446}
2447EXPORT_SYMBOL_GPL(gpiod_get_index);
2448
2449/**
2450 * gpiod_put - dispose of a GPIO descriptor
2451 * @desc: GPIO descriptor to dispose of
2452 *
2453 * No descriptor can be used after gpiod_put() has been called on it.
2454 */
2455void gpiod_put(struct gpio_desc *desc)
2456{
2457 gpiod_free(desc);
372e722e 2458}
bae48da2 2459EXPORT_SYMBOL_GPL(gpiod_put);
d2876d08
DB
2460
2461#ifdef CONFIG_DEBUG_FS
2462
d2876d08
DB
2463static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2464{
2465 unsigned i;
2466 unsigned gpio = chip->base;
6c0b4e6c 2467 struct gpio_desc *gdesc = &chip->desc[0];
d2876d08 2468 int is_out;
d468bf9e 2469 int is_irq;
d2876d08
DB
2470
2471 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2472 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2473 continue;
2474
372e722e 2475 gpiod_get_direction(gdesc);
d2876d08 2476 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
d468bf9e
LW
2477 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2478 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
d2876d08
DB
2479 gpio, gdesc->label,
2480 is_out ? "out" : "in ",
2481 chip->get
2482 ? (chip->get(chip, i) ? "hi" : "lo")
d468bf9e
LW
2483 : "? ",
2484 is_irq ? "IRQ" : " ");
d2876d08
DB
2485 seq_printf(s, "\n");
2486 }
2487}
2488
f9c4a31f 2489static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
d2876d08 2490{
362432ae 2491 unsigned long flags;
f9c4a31f 2492 struct gpio_chip *chip = NULL;
cb1650d4 2493 loff_t index = *pos;
d2876d08 2494
f9c4a31f 2495 s->private = "";
d2876d08 2496
362432ae 2497 spin_lock_irqsave(&gpio_lock, flags);
cb1650d4 2498 list_for_each_entry(chip, &gpio_chips, list)
362432ae
GL
2499 if (index-- == 0) {
2500 spin_unlock_irqrestore(&gpio_lock, flags);
cb1650d4 2501 return chip;
f9c4a31f 2502 }
362432ae 2503 spin_unlock_irqrestore(&gpio_lock, flags);
f9c4a31f 2504
cb1650d4 2505 return NULL;
f9c4a31f
TR
2506}
2507
2508static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2509{
362432ae 2510 unsigned long flags;
f9c4a31f 2511 struct gpio_chip *chip = v;
f9c4a31f
TR
2512 void *ret = NULL;
2513
362432ae 2514 spin_lock_irqsave(&gpio_lock, flags);
cb1650d4
AC
2515 if (list_is_last(&chip->list, &gpio_chips))
2516 ret = NULL;
2517 else
2518 ret = list_entry(chip->list.next, struct gpio_chip, list);
362432ae 2519 spin_unlock_irqrestore(&gpio_lock, flags);
f9c4a31f
TR
2520
2521 s->private = "\n";
2522 ++*pos;
2523
2524 return ret;
2525}
2526
2527static void gpiolib_seq_stop(struct seq_file *s, void *v)
2528{
2529}
2530
2531static int gpiolib_seq_show(struct seq_file *s, void *v)
2532{
2533 struct gpio_chip *chip = v;
2534 struct device *dev;
2535
2536 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2537 chip->base, chip->base + chip->ngpio - 1);
2538 dev = chip->dev;
2539 if (dev)
2540 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2541 dev_name(dev));
2542 if (chip->label)
2543 seq_printf(s, ", %s", chip->label);
2544 if (chip->can_sleep)
2545 seq_printf(s, ", can sleep");
2546 seq_printf(s, ":\n");
2547
2548 if (chip->dbg_show)
2549 chip->dbg_show(s, chip);
2550 else
2551 gpiolib_dbg_show(s, chip);
2552
d2876d08
DB
2553 return 0;
2554}
2555
f9c4a31f
TR
2556static const struct seq_operations gpiolib_seq_ops = {
2557 .start = gpiolib_seq_start,
2558 .next = gpiolib_seq_next,
2559 .stop = gpiolib_seq_stop,
2560 .show = gpiolib_seq_show,
2561};
2562
d2876d08
DB
2563static int gpiolib_open(struct inode *inode, struct file *file)
2564{
f9c4a31f 2565 return seq_open(file, &gpiolib_seq_ops);
d2876d08
DB
2566}
2567
828c0950 2568static const struct file_operations gpiolib_operations = {
f9c4a31f 2569 .owner = THIS_MODULE,
d2876d08
DB
2570 .open = gpiolib_open,
2571 .read = seq_read,
2572 .llseek = seq_lseek,
f9c4a31f 2573 .release = seq_release,
d2876d08
DB
2574};
2575
2576static int __init gpiolib_debugfs_init(void)
2577{
2578 /* /sys/kernel/debug/gpio */
2579 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2580 NULL, NULL, &gpiolib_operations);
2581 return 0;
2582}
2583subsys_initcall(gpiolib_debugfs_init);
2584
2585#endif /* DEBUG_FS */
This page took 0.614354 seconds and 5 git commands to generate.