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