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