blob: af3bc7a8033bdcbaa2e93602bb107fbe12968d35 [file] [log] [blame]
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001#include <linux/idr.h>
2#include <linux/mutex.h>
3#include <linux/device.h>
4#include <linux/sysfs.h>
5#include <linux/gpio/consumer.h>
6#include <linux/gpio/driver.h>
7#include <linux/interrupt.h>
8#include <linux/kdev_t.h>
9
10#include "gpiolib.h"
11
12static DEFINE_IDR(dirent_idr);
13
14
15/* lock protects against unexport_gpio() being called while
16 * sysfs files are active.
17 */
18static DEFINE_MUTEX(sysfs_lock);
19
20/*
21 * /sys/class/gpio/gpioN... only for GPIOs that are exported
22 * /direction
23 * * MAY BE OMITTED if kernel won't allow direction changes
24 * * is read/write as "in" or "out"
25 * * may also be written as "high" or "low", initializing
26 * output value as specified ("out" implies "low")
27 * /value
28 * * always readable, subject to hardware behavior
29 * * may be writable, as zero/nonzero
30 * /edge
31 * * configures behavior of poll(2) on /value
32 * * available only if pin can generate IRQs on input
33 * * is read/write as "none", "falling", "rising", or "both"
34 * /active_low
35 * * configures polarity of /value
36 * * is read/write as zero/nonzero
37 * * also affects existing and subsequent "falling" and "rising"
38 * /edge configuration
39 */
40
41static ssize_t gpio_direction_show(struct device *dev,
42 struct device_attribute *attr, char *buf)
43{
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +090044 struct gpio_desc *desc = dev_get_drvdata(dev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090045 ssize_t status;
46
47 mutex_lock(&sysfs_lock);
48
49 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
50 status = -EIO;
51 } else {
52 gpiod_get_direction(desc);
53 status = sprintf(buf, "%s\n",
54 test_bit(FLAG_IS_OUT, &desc->flags)
55 ? "out" : "in");
56 }
57
58 mutex_unlock(&sysfs_lock);
59 return status;
60}
61
62static ssize_t gpio_direction_store(struct device *dev,
63 struct device_attribute *attr, const char *buf, size_t size)
64{
65 struct gpio_desc *desc = dev_get_drvdata(dev);
66 ssize_t status;
67
68 mutex_lock(&sysfs_lock);
69
70 if (!test_bit(FLAG_EXPORT, &desc->flags))
71 status = -EIO;
72 else if (sysfs_streq(buf, "high"))
73 status = gpiod_direction_output_raw(desc, 1);
74 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
75 status = gpiod_direction_output_raw(desc, 0);
76 else if (sysfs_streq(buf, "in"))
77 status = gpiod_direction_input(desc);
78 else
79 status = -EINVAL;
80
81 mutex_unlock(&sysfs_lock);
82 return status ? : size;
83}
84
85static /* const */ DEVICE_ATTR(direction, 0644,
86 gpio_direction_show, gpio_direction_store);
87
88static ssize_t gpio_value_show(struct device *dev,
89 struct device_attribute *attr, char *buf)
90{
91 struct gpio_desc *desc = dev_get_drvdata(dev);
92 ssize_t status;
93
94 mutex_lock(&sysfs_lock);
95
96 if (!test_bit(FLAG_EXPORT, &desc->flags))
97 status = -EIO;
98 else
99 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
100
101 mutex_unlock(&sysfs_lock);
102 return status;
103}
104
105static ssize_t gpio_value_store(struct device *dev,
106 struct device_attribute *attr, const char *buf, size_t size)
107{
108 struct gpio_desc *desc = dev_get_drvdata(dev);
109 ssize_t status;
110
111 mutex_lock(&sysfs_lock);
112
113 if (!test_bit(FLAG_EXPORT, &desc->flags))
114 status = -EIO;
115 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
116 status = -EPERM;
117 else {
118 long value;
119
120 status = kstrtol(buf, 0, &value);
121 if (status == 0) {
122 gpiod_set_value_cansleep(desc, value);
123 status = size;
124 }
125 }
126
127 mutex_unlock(&sysfs_lock);
128 return status;
129}
130
Johan Hovold0915e6f2015-01-13 13:00:05 +0100131static DEVICE_ATTR(value, 0644,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900132 gpio_value_show, gpio_value_store);
133
134static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
135{
136 struct kernfs_node *value_sd = priv;
137
138 sysfs_notify_dirent(value_sd);
139 return IRQ_HANDLED;
140}
141
142static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
143 unsigned long gpio_flags)
144{
145 struct kernfs_node *value_sd;
146 unsigned long irq_flags;
147 int ret, irq, id;
148
149 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
150 return 0;
151
152 irq = gpiod_to_irq(desc);
153 if (irq < 0)
154 return -EIO;
155
156 id = desc->flags >> ID_SHIFT;
157 value_sd = idr_find(&dirent_idr, id);
158 if (value_sd)
159 free_irq(irq, value_sd);
160
161 desc->flags &= ~GPIO_TRIGGER_MASK;
162
163 if (!gpio_flags) {
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900164 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900165 ret = 0;
166 goto free_id;
167 }
168
169 irq_flags = IRQF_SHARED;
170 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
171 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
172 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
173 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
174 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
175 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
176
177 if (!value_sd) {
178 value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
179 if (!value_sd) {
180 ret = -ENODEV;
181 goto err_out;
182 }
183
184 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
185 if (ret < 0)
186 goto free_sd;
187 id = ret;
188
189 desc->flags &= GPIO_FLAGS_MASK;
190 desc->flags |= (unsigned long)id << ID_SHIFT;
191
192 if (desc->flags >> ID_SHIFT != id) {
193 ret = -ERANGE;
194 goto free_id;
195 }
196 }
197
198 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
199 "gpiolib", value_sd);
200 if (ret < 0)
201 goto free_id;
202
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900203 ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900204 if (ret < 0) {
205 gpiod_warn(desc, "failed to flag the GPIO for IRQ\n");
206 goto free_id;
207 }
208
209 desc->flags |= gpio_flags;
210 return 0;
211
212free_id:
213 idr_remove(&dirent_idr, id);
214 desc->flags &= GPIO_FLAGS_MASK;
215free_sd:
216 if (value_sd)
217 sysfs_put(value_sd);
218err_out:
219 return ret;
220}
221
222static const struct {
223 const char *name;
224 unsigned long flags;
225} trigger_types[] = {
226 { "none", 0 },
227 { "falling", BIT(FLAG_TRIG_FALL) },
228 { "rising", BIT(FLAG_TRIG_RISE) },
229 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
230};
231
232static ssize_t gpio_edge_show(struct device *dev,
233 struct device_attribute *attr, char *buf)
234{
235 const struct gpio_desc *desc = dev_get_drvdata(dev);
236 ssize_t status;
237
238 mutex_lock(&sysfs_lock);
239
240 if (!test_bit(FLAG_EXPORT, &desc->flags))
241 status = -EIO;
242 else {
243 int i;
244
245 status = 0;
246 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
247 if ((desc->flags & GPIO_TRIGGER_MASK)
248 == trigger_types[i].flags) {
249 status = sprintf(buf, "%s\n",
250 trigger_types[i].name);
251 break;
252 }
253 }
254
255 mutex_unlock(&sysfs_lock);
256 return status;
257}
258
259static ssize_t gpio_edge_store(struct device *dev,
260 struct device_attribute *attr, const char *buf, size_t size)
261{
262 struct gpio_desc *desc = dev_get_drvdata(dev);
263 ssize_t status;
264 int i;
265
266 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
267 if (sysfs_streq(trigger_types[i].name, buf))
268 goto found;
269 return -EINVAL;
270
271found:
272 mutex_lock(&sysfs_lock);
273
274 if (!test_bit(FLAG_EXPORT, &desc->flags))
275 status = -EIO;
276 else {
277 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
278 if (!status)
279 status = size;
280 }
281
282 mutex_unlock(&sysfs_lock);
283
284 return status;
285}
286
287static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
288
289static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
290 int value)
291{
292 int status = 0;
293
294 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
295 return 0;
296
297 if (value)
298 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
299 else
300 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
301
302 /* reconfigure poll(2) support if enabled on one edge only */
303 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
304 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
305 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
306
307 gpio_setup_irq(desc, dev, 0);
308 status = gpio_setup_irq(desc, dev, trigger_flags);
309 }
310
311 return status;
312}
313
314static ssize_t gpio_active_low_show(struct device *dev,
315 struct device_attribute *attr, char *buf)
316{
317 const struct gpio_desc *desc = dev_get_drvdata(dev);
318 ssize_t status;
319
320 mutex_lock(&sysfs_lock);
321
322 if (!test_bit(FLAG_EXPORT, &desc->flags))
323 status = -EIO;
324 else
325 status = sprintf(buf, "%d\n",
326 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
327
328 mutex_unlock(&sysfs_lock);
329
330 return status;
331}
332
333static ssize_t gpio_active_low_store(struct device *dev,
334 struct device_attribute *attr, const char *buf, size_t size)
335{
336 struct gpio_desc *desc = dev_get_drvdata(dev);
337 ssize_t status;
338
339 mutex_lock(&sysfs_lock);
340
341 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
342 status = -EIO;
343 } else {
344 long value;
345
346 status = kstrtol(buf, 0, &value);
347 if (status == 0)
348 status = sysfs_set_active_low(desc, dev, value != 0);
349 }
350
351 mutex_unlock(&sysfs_lock);
352
353 return status ? : size;
354}
355
Johan Hovold0915e6f2015-01-13 13:00:05 +0100356static DEVICE_ATTR(active_low, 0644,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900357 gpio_active_low_show, gpio_active_low_store);
358
Johan Hovoldebbeba12015-01-13 13:00:06 +0100359static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
360 int n)
361{
362 struct device *dev = container_of(kobj, struct device, kobj);
363 struct gpio_desc *desc = dev_get_drvdata(dev);
364 umode_t mode = attr->mode;
365 bool show_direction = test_bit(FLAG_SYSFS_DIR, &desc->flags);
366
367 if (attr == &dev_attr_direction.attr) {
368 if (!show_direction)
369 mode = 0;
370 } else if (attr == &dev_attr_edge.attr) {
371 if (gpiod_to_irq(desc) < 0)
372 mode = 0;
373 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
374 mode = 0;
375 }
376
377 return mode;
378}
379
Johan Hovold0915e6f2015-01-13 13:00:05 +0100380static struct attribute *gpio_attrs[] = {
Johan Hovoldebbeba12015-01-13 13:00:06 +0100381 &dev_attr_direction.attr,
382 &dev_attr_edge.attr,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900383 &dev_attr_value.attr,
384 &dev_attr_active_low.attr,
385 NULL,
386};
Johan Hovoldebbeba12015-01-13 13:00:06 +0100387
388static const struct attribute_group gpio_group = {
389 .attrs = gpio_attrs,
390 .is_visible = gpio_is_visible,
391};
392
393static const struct attribute_group *gpio_groups[] = {
394 &gpio_group,
395 NULL
396};
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900397
398/*
399 * /sys/class/gpio/gpiochipN/
400 * /base ... matching gpio_chip.base (N)
401 * /label ... matching gpio_chip.label
402 * /ngpio ... matching gpio_chip.ngpio
403 */
404
405static ssize_t chip_base_show(struct device *dev,
406 struct device_attribute *attr, char *buf)
407{
408 const struct gpio_chip *chip = dev_get_drvdata(dev);
409
410 return sprintf(buf, "%d\n", chip->base);
411}
412static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
413
414static ssize_t chip_label_show(struct device *dev,
415 struct device_attribute *attr, char *buf)
416{
417 const struct gpio_chip *chip = dev_get_drvdata(dev);
418
419 return sprintf(buf, "%s\n", chip->label ? : "");
420}
421static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
422
423static ssize_t chip_ngpio_show(struct device *dev,
424 struct device_attribute *attr, char *buf)
425{
426 const struct gpio_chip *chip = dev_get_drvdata(dev);
427
428 return sprintf(buf, "%u\n", chip->ngpio);
429}
430static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
431
Johan Hovold121b6a72015-01-13 13:00:04 +0100432static struct attribute *gpiochip_attrs[] = {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900433 &dev_attr_base.attr,
434 &dev_attr_label.attr,
435 &dev_attr_ngpio.attr,
436 NULL,
437};
Johan Hovold121b6a72015-01-13 13:00:04 +0100438ATTRIBUTE_GROUPS(gpiochip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900439
440/*
441 * /sys/class/gpio/export ... write-only
442 * integer N ... number of GPIO to export (full access)
443 * /sys/class/gpio/unexport ... write-only
444 * integer N ... number of GPIO to unexport
445 */
446static ssize_t export_store(struct class *class,
447 struct class_attribute *attr,
448 const char *buf, size_t len)
449{
450 long gpio;
451 struct gpio_desc *desc;
452 int status;
453
454 status = kstrtol(buf, 0, &gpio);
455 if (status < 0)
456 goto done;
457
458 desc = gpio_to_desc(gpio);
459 /* reject invalid GPIOs */
460 if (!desc) {
461 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
462 return -EINVAL;
463 }
464
465 /* No extra locking here; FLAG_SYSFS just signifies that the
466 * request and export were done by on behalf of userspace, so
467 * they may be undone on its behalf too.
468 */
469
470 status = gpiod_request(desc, "sysfs");
471 if (status < 0) {
472 if (status == -EPROBE_DEFER)
473 status = -ENODEV;
474 goto done;
475 }
476 status = gpiod_export(desc, true);
477 if (status < 0)
478 gpiod_free(desc);
479 else
480 set_bit(FLAG_SYSFS, &desc->flags);
481
482done:
483 if (status)
484 pr_debug("%s: status %d\n", __func__, status);
485 return status ? : len;
486}
487
488static ssize_t unexport_store(struct class *class,
489 struct class_attribute *attr,
490 const char *buf, size_t len)
491{
492 long gpio;
493 struct gpio_desc *desc;
494 int status;
495
496 status = kstrtol(buf, 0, &gpio);
497 if (status < 0)
498 goto done;
499
500 desc = gpio_to_desc(gpio);
501 /* reject bogus commands (gpio_unexport ignores them) */
502 if (!desc) {
503 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
504 return -EINVAL;
505 }
506
507 status = -EINVAL;
508
509 /* No extra locking here; FLAG_SYSFS just signifies that the
510 * request and export were done by on behalf of userspace, so
511 * they may be undone on its behalf too.
512 */
513 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
514 status = 0;
515 gpiod_free(desc);
516 }
517done:
518 if (status)
519 pr_debug("%s: status %d\n", __func__, status);
520 return status ? : len;
521}
522
523static struct class_attribute gpio_class_attrs[] = {
524 __ATTR(export, 0200, NULL, export_store),
525 __ATTR(unexport, 0200, NULL, unexport_store),
526 __ATTR_NULL,
527};
528
529static struct class gpio_class = {
530 .name = "gpio",
531 .owner = THIS_MODULE,
532
533 .class_attrs = gpio_class_attrs,
534};
535
536
537/**
538 * gpiod_export - export a GPIO through sysfs
539 * @gpio: gpio to make available, already requested
540 * @direction_may_change: true if userspace may change gpio direction
541 * Context: arch_initcall or later
542 *
543 * When drivers want to make a GPIO accessible to userspace after they
544 * have requested it -- perhaps while debugging, or as part of their
545 * public interface -- they may use this routine. If the GPIO can
546 * change direction (some can't) and the caller allows it, userspace
547 * will see "direction" sysfs attribute which may be used to change
548 * the gpio's direction. A "value" attribute will always be provided.
549 *
550 * Returns zero on success, else an error.
551 */
552int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
553{
Johan Hovold483d8212015-04-21 17:42:09 +0200554 struct gpio_chip *chip;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900555 unsigned long flags;
556 int status;
557 const char *ioname = NULL;
558 struct device *dev;
559 int offset;
560
561 /* can't export until sysfs is available ... */
562 if (!gpio_class.p) {
563 pr_debug("%s: called too early!\n", __func__);
564 return -ENOENT;
565 }
566
567 if (!desc) {
568 pr_debug("%s: invalid gpio descriptor\n", __func__);
569 return -EINVAL;
570 }
571
Johan Hovold483d8212015-04-21 17:42:09 +0200572 chip = desc->chip;
573
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900574 mutex_lock(&sysfs_lock);
575
Johan Hovold483d8212015-04-21 17:42:09 +0200576 /* check if chip is being removed */
577 if (!chip || !chip->exported) {
578 status = -ENODEV;
579 goto fail_unlock;
580 }
581
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900582 spin_lock_irqsave(&gpio_lock, flags);
583 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
584 test_bit(FLAG_EXPORT, &desc->flags)) {
585 spin_unlock_irqrestore(&gpio_lock, flags);
586 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
587 __func__,
588 test_bit(FLAG_REQUESTED, &desc->flags),
589 test_bit(FLAG_EXPORT, &desc->flags));
590 status = -EPERM;
591 goto fail_unlock;
592 }
593
Johan Hovoldebbeba12015-01-13 13:00:06 +0100594 if (desc->chip->direction_input && desc->chip->direction_output &&
595 direction_may_change) {
596 set_bit(FLAG_SYSFS_DIR, &desc->flags);
597 }
598
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900599 spin_unlock_irqrestore(&gpio_lock, flags);
600
601 offset = gpio_chip_hwgpio(desc);
602 if (desc->chip->names && desc->chip->names[offset])
603 ioname = desc->chip->names[offset];
604
Johan Hovold0915e6f2015-01-13 13:00:05 +0100605 dev = device_create_with_groups(&gpio_class, desc->chip->dev,
606 MKDEV(0, 0), desc, gpio_groups,
607 ioname ? ioname : "gpio%u",
608 desc_to_gpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900609 if (IS_ERR(dev)) {
610 status = PTR_ERR(dev);
611 goto fail_unlock;
612 }
613
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900614 set_bit(FLAG_EXPORT, &desc->flags);
615 mutex_unlock(&sysfs_lock);
616 return 0;
617
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900618fail_unlock:
619 mutex_unlock(&sysfs_lock);
620 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
621 return status;
622}
623EXPORT_SYMBOL_GPL(gpiod_export);
624
625static int match_export(struct device *dev, const void *data)
626{
627 return dev_get_drvdata(dev) == data;
628}
629
630/**
631 * gpiod_export_link - create a sysfs link to an exported GPIO node
632 * @dev: device under which to create symlink
633 * @name: name of the symlink
634 * @gpio: gpio to create symlink to, already exported
635 *
636 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
637 * node. Caller is responsible for unlinking.
638 *
639 * Returns zero on success, else an error.
640 */
641int gpiod_export_link(struct device *dev, const char *name,
642 struct gpio_desc *desc)
643{
644 int status = -EINVAL;
645
646 if (!desc) {
647 pr_warn("%s: invalid GPIO\n", __func__);
648 return -EINVAL;
649 }
650
651 mutex_lock(&sysfs_lock);
652
653 if (test_bit(FLAG_EXPORT, &desc->flags)) {
654 struct device *tdev;
655
656 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
657 if (tdev != NULL) {
658 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
659 name);
Johan Hovold0f303db2015-01-26 12:02:45 +0100660 put_device(tdev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900661 } else {
662 status = -ENODEV;
663 }
664 }
665
666 mutex_unlock(&sysfs_lock);
667
668 if (status)
669 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
670
671 return status;
672}
673EXPORT_SYMBOL_GPL(gpiod_export_link);
674
675/**
676 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
677 * @gpio: gpio to change
678 * @value: non-zero to use active low, i.e. inverted values
679 *
680 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
681 * The GPIO does not have to be exported yet. If poll(2) support has
682 * been enabled for either rising or falling edge, it will be
683 * reconfigured to follow the new polarity.
684 *
685 * Returns zero on success, else an error.
686 */
687int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
688{
689 struct device *dev = NULL;
690 int status = -EINVAL;
691
692 if (!desc) {
693 pr_warn("%s: invalid GPIO\n", __func__);
694 return -EINVAL;
695 }
696
697 mutex_lock(&sysfs_lock);
698
699 if (test_bit(FLAG_EXPORT, &desc->flags)) {
700 dev = class_find_device(&gpio_class, NULL, desc, match_export);
701 if (dev == NULL) {
702 status = -ENODEV;
703 goto unlock;
704 }
705 }
706
707 status = sysfs_set_active_low(desc, dev, value);
Johan Hovold49d2ca82015-01-26 12:02:46 +0100708 put_device(dev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900709unlock:
710 mutex_unlock(&sysfs_lock);
711
712 if (status)
713 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
714
715 return status;
716}
717EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
718
719/**
720 * gpiod_unexport - reverse effect of gpio_export()
721 * @gpio: gpio to make unavailable
722 *
723 * This is implicit on gpio_free().
724 */
725void gpiod_unexport(struct gpio_desc *desc)
726{
727 int status = 0;
728 struct device *dev = NULL;
729
730 if (!desc) {
731 pr_warn("%s: invalid GPIO\n", __func__);
732 return;
733 }
734
735 mutex_lock(&sysfs_lock);
736
737 if (test_bit(FLAG_EXPORT, &desc->flags)) {
738
739 dev = class_find_device(&gpio_class, NULL, desc, match_export);
740 if (dev) {
741 gpio_setup_irq(desc, dev, 0);
Johan Hovoldebbeba12015-01-13 13:00:06 +0100742 clear_bit(FLAG_SYSFS_DIR, &desc->flags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900743 clear_bit(FLAG_EXPORT, &desc->flags);
744 } else
745 status = -ENODEV;
746 }
747
748 mutex_unlock(&sysfs_lock);
749
750 if (dev) {
751 device_unregister(dev);
752 put_device(dev);
753 }
754
755 if (status)
756 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
757}
758EXPORT_SYMBOL_GPL(gpiod_unexport);
759
760int gpiochip_export(struct gpio_chip *chip)
761{
762 int status;
763 struct device *dev;
764
765 /* Many systems register gpio chips for SOC support very early,
766 * before driver model support is available. In those cases we
767 * export this later, in gpiolib_sysfs_init() ... here we just
768 * verify that _some_ field of gpio_class got initialized.
769 */
770 if (!gpio_class.p)
771 return 0;
772
773 /* use chip->base for the ID; it's already known to be unique */
774 mutex_lock(&sysfs_lock);
Johan Hovold121b6a72015-01-13 13:00:04 +0100775 dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0),
776 chip, gpiochip_groups,
777 "gpiochip%d", chip->base);
778 if (IS_ERR(dev))
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900779 status = PTR_ERR(dev);
Johan Hovold121b6a72015-01-13 13:00:04 +0100780 else
781 status = 0;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900782 chip->exported = (status == 0);
783 mutex_unlock(&sysfs_lock);
784
Alexandre Courbot14141a92014-07-22 16:17:40 +0900785 if (status)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900786 chip_dbg(chip, "%s: status %d\n", __func__, status);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900787
788 return status;
789}
790
791void gpiochip_unexport(struct gpio_chip *chip)
792{
793 int status;
794 struct device *dev;
Johan Hovold483d8212015-04-21 17:42:09 +0200795 struct gpio_desc *desc;
796 unsigned int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900797
798 mutex_lock(&sysfs_lock);
799 dev = class_find_device(&gpio_class, NULL, chip, match_export);
800 if (dev) {
801 put_device(dev);
802 device_unregister(dev);
Johan Hovold483d8212015-04-21 17:42:09 +0200803 /* prevent further gpiod exports */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900804 chip->exported = false;
805 status = 0;
806 } else
807 status = -ENODEV;
808 mutex_unlock(&sysfs_lock);
809
810 if (status)
811 chip_dbg(chip, "%s: status %d\n", __func__, status);
Johan Hovold483d8212015-04-21 17:42:09 +0200812
813 /* unregister gpiod class devices owned by sysfs */
814 for (i = 0; i < chip->ngpio; i++) {
815 desc = &chip->desc[i];
816 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
817 gpiod_free(desc);
818 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900819}
820
821static int __init gpiolib_sysfs_init(void)
822{
823 int status;
824 unsigned long flags;
825 struct gpio_chip *chip;
826
827 status = class_register(&gpio_class);
828 if (status < 0)
829 return status;
830
831 /* Scan and register the gpio_chips which registered very
832 * early (e.g. before the class_register above was called).
833 *
834 * We run before arch_initcall() so chip->dev nodes can have
835 * registered, and so arch_initcall() can always gpio_export().
836 */
837 spin_lock_irqsave(&gpio_lock, flags);
838 list_for_each_entry(chip, &gpio_chips, list) {
Alexandre Courbot7fd834a2014-07-23 23:56:53 +0900839 if (chip->exported)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900840 continue;
841
Alexandre Courbot14141a92014-07-22 16:17:40 +0900842 /*
843 * TODO we yield gpio_lock here because gpiochip_export()
844 * acquires a mutex. This is unsafe and needs to be fixed.
845 *
846 * Also it would be nice to use gpiochip_find() here so we
847 * can keep gpio_chips local to gpiolib.c, but the yield of
848 * gpio_lock prevents us from doing this.
849 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900850 spin_unlock_irqrestore(&gpio_lock, flags);
851 status = gpiochip_export(chip);
852 spin_lock_irqsave(&gpio_lock, flags);
853 }
854 spin_unlock_irqrestore(&gpio_lock, flags);
855
856
857 return status;
858}
859postcore_initcall(gpiolib_sysfs_init);