blob: 767b79adb9a4786a778ce661db7a5cf336288aca [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
Johan Hovold52176d02015-05-04 17:10:28 +0200198 /*
199 * FIXME: This should be done in the irq_request_resources callback
200 * when the irq is requested, but a few drivers currently fail
201 * to do so.
202 *
203 * Remove this redundant call (along with the corresponding
204 * unlock) when those drivers have been fixed.
205 */
206 ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900207 if (ret < 0)
208 goto free_id;
209
Johan Hovold52176d02015-05-04 17:10:28 +0200210 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
211 "gpiolib", value_sd);
212 if (ret < 0)
213 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900214
215 desc->flags |= gpio_flags;
216 return 0;
217
Johan Hovold52176d02015-05-04 17:10:28 +0200218err_unlock:
219 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900220free_id:
221 idr_remove(&dirent_idr, id);
222 desc->flags &= GPIO_FLAGS_MASK;
223free_sd:
224 if (value_sd)
225 sysfs_put(value_sd);
226err_out:
227 return ret;
228}
229
230static const struct {
231 const char *name;
232 unsigned long flags;
233} trigger_types[] = {
234 { "none", 0 },
235 { "falling", BIT(FLAG_TRIG_FALL) },
236 { "rising", BIT(FLAG_TRIG_RISE) },
237 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
238};
239
240static ssize_t gpio_edge_show(struct device *dev,
241 struct device_attribute *attr, char *buf)
242{
243 const struct gpio_desc *desc = dev_get_drvdata(dev);
244 ssize_t status;
245
246 mutex_lock(&sysfs_lock);
247
248 if (!test_bit(FLAG_EXPORT, &desc->flags))
249 status = -EIO;
250 else {
251 int i;
252
253 status = 0;
254 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
255 if ((desc->flags & GPIO_TRIGGER_MASK)
256 == trigger_types[i].flags) {
257 status = sprintf(buf, "%s\n",
258 trigger_types[i].name);
259 break;
260 }
261 }
262
263 mutex_unlock(&sysfs_lock);
264 return status;
265}
266
267static ssize_t gpio_edge_store(struct device *dev,
268 struct device_attribute *attr, const char *buf, size_t size)
269{
270 struct gpio_desc *desc = dev_get_drvdata(dev);
271 ssize_t status;
272 int i;
273
274 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
275 if (sysfs_streq(trigger_types[i].name, buf))
276 goto found;
277 return -EINVAL;
278
279found:
280 mutex_lock(&sysfs_lock);
281
282 if (!test_bit(FLAG_EXPORT, &desc->flags))
283 status = -EIO;
284 else {
285 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
286 if (!status)
287 status = size;
288 }
289
290 mutex_unlock(&sysfs_lock);
291
292 return status;
293}
294
295static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
296
297static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
298 int value)
299{
300 int status = 0;
301
302 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
303 return 0;
304
305 if (value)
306 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
307 else
308 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
309
310 /* reconfigure poll(2) support if enabled on one edge only */
311 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
312 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
313 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
314
315 gpio_setup_irq(desc, dev, 0);
316 status = gpio_setup_irq(desc, dev, trigger_flags);
317 }
318
319 return status;
320}
321
322static ssize_t gpio_active_low_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
324{
325 const struct gpio_desc *desc = dev_get_drvdata(dev);
326 ssize_t status;
327
328 mutex_lock(&sysfs_lock);
329
330 if (!test_bit(FLAG_EXPORT, &desc->flags))
331 status = -EIO;
332 else
333 status = sprintf(buf, "%d\n",
334 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
335
336 mutex_unlock(&sysfs_lock);
337
338 return status;
339}
340
341static ssize_t gpio_active_low_store(struct device *dev,
342 struct device_attribute *attr, const char *buf, size_t size)
343{
344 struct gpio_desc *desc = dev_get_drvdata(dev);
345 ssize_t status;
346
347 mutex_lock(&sysfs_lock);
348
349 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
350 status = -EIO;
351 } else {
352 long value;
353
354 status = kstrtol(buf, 0, &value);
355 if (status == 0)
356 status = sysfs_set_active_low(desc, dev, value != 0);
357 }
358
359 mutex_unlock(&sysfs_lock);
360
361 return status ? : size;
362}
363
Johan Hovold0915e6f2015-01-13 13:00:05 +0100364static DEVICE_ATTR(active_low, 0644,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900365 gpio_active_low_show, gpio_active_low_store);
366
Johan Hovoldebbeba12015-01-13 13:00:06 +0100367static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
368 int n)
369{
370 struct device *dev = container_of(kobj, struct device, kobj);
371 struct gpio_desc *desc = dev_get_drvdata(dev);
372 umode_t mode = attr->mode;
373 bool show_direction = test_bit(FLAG_SYSFS_DIR, &desc->flags);
374
375 if (attr == &dev_attr_direction.attr) {
376 if (!show_direction)
377 mode = 0;
378 } else if (attr == &dev_attr_edge.attr) {
379 if (gpiod_to_irq(desc) < 0)
380 mode = 0;
381 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
382 mode = 0;
383 }
384
385 return mode;
386}
387
Johan Hovold0915e6f2015-01-13 13:00:05 +0100388static struct attribute *gpio_attrs[] = {
Johan Hovoldebbeba12015-01-13 13:00:06 +0100389 &dev_attr_direction.attr,
390 &dev_attr_edge.attr,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900391 &dev_attr_value.attr,
392 &dev_attr_active_low.attr,
393 NULL,
394};
Johan Hovoldebbeba12015-01-13 13:00:06 +0100395
396static const struct attribute_group gpio_group = {
397 .attrs = gpio_attrs,
398 .is_visible = gpio_is_visible,
399};
400
401static const struct attribute_group *gpio_groups[] = {
402 &gpio_group,
403 NULL
404};
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900405
406/*
407 * /sys/class/gpio/gpiochipN/
408 * /base ... matching gpio_chip.base (N)
409 * /label ... matching gpio_chip.label
410 * /ngpio ... matching gpio_chip.ngpio
411 */
412
413static ssize_t chip_base_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
415{
416 const struct gpio_chip *chip = dev_get_drvdata(dev);
417
418 return sprintf(buf, "%d\n", chip->base);
419}
420static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
421
422static ssize_t chip_label_show(struct device *dev,
423 struct device_attribute *attr, char *buf)
424{
425 const struct gpio_chip *chip = dev_get_drvdata(dev);
426
427 return sprintf(buf, "%s\n", chip->label ? : "");
428}
429static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
430
431static ssize_t chip_ngpio_show(struct device *dev,
432 struct device_attribute *attr, char *buf)
433{
434 const struct gpio_chip *chip = dev_get_drvdata(dev);
435
436 return sprintf(buf, "%u\n", chip->ngpio);
437}
438static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
439
Johan Hovold121b6a72015-01-13 13:00:04 +0100440static struct attribute *gpiochip_attrs[] = {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900441 &dev_attr_base.attr,
442 &dev_attr_label.attr,
443 &dev_attr_ngpio.attr,
444 NULL,
445};
Johan Hovold121b6a72015-01-13 13:00:04 +0100446ATTRIBUTE_GROUPS(gpiochip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900447
448/*
449 * /sys/class/gpio/export ... write-only
450 * integer N ... number of GPIO to export (full access)
451 * /sys/class/gpio/unexport ... write-only
452 * integer N ... number of GPIO to unexport
453 */
454static ssize_t export_store(struct class *class,
455 struct class_attribute *attr,
456 const char *buf, size_t len)
457{
458 long gpio;
459 struct gpio_desc *desc;
460 int status;
461
462 status = kstrtol(buf, 0, &gpio);
463 if (status < 0)
464 goto done;
465
466 desc = gpio_to_desc(gpio);
467 /* reject invalid GPIOs */
468 if (!desc) {
469 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
470 return -EINVAL;
471 }
472
473 /* No extra locking here; FLAG_SYSFS just signifies that the
474 * request and export were done by on behalf of userspace, so
475 * they may be undone on its behalf too.
476 */
477
478 status = gpiod_request(desc, "sysfs");
479 if (status < 0) {
480 if (status == -EPROBE_DEFER)
481 status = -ENODEV;
482 goto done;
483 }
484 status = gpiod_export(desc, true);
485 if (status < 0)
486 gpiod_free(desc);
487 else
488 set_bit(FLAG_SYSFS, &desc->flags);
489
490done:
491 if (status)
492 pr_debug("%s: status %d\n", __func__, status);
493 return status ? : len;
494}
495
496static ssize_t unexport_store(struct class *class,
497 struct class_attribute *attr,
498 const char *buf, size_t len)
499{
500 long gpio;
501 struct gpio_desc *desc;
502 int status;
503
504 status = kstrtol(buf, 0, &gpio);
505 if (status < 0)
506 goto done;
507
508 desc = gpio_to_desc(gpio);
509 /* reject bogus commands (gpio_unexport ignores them) */
510 if (!desc) {
511 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
512 return -EINVAL;
513 }
514
515 status = -EINVAL;
516
517 /* No extra locking here; FLAG_SYSFS just signifies that the
518 * request and export were done by on behalf of userspace, so
519 * they may be undone on its behalf too.
520 */
521 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
522 status = 0;
523 gpiod_free(desc);
524 }
525done:
526 if (status)
527 pr_debug("%s: status %d\n", __func__, status);
528 return status ? : len;
529}
530
531static struct class_attribute gpio_class_attrs[] = {
532 __ATTR(export, 0200, NULL, export_store),
533 __ATTR(unexport, 0200, NULL, unexport_store),
534 __ATTR_NULL,
535};
536
537static struct class gpio_class = {
538 .name = "gpio",
539 .owner = THIS_MODULE,
540
541 .class_attrs = gpio_class_attrs,
542};
543
544
545/**
546 * gpiod_export - export a GPIO through sysfs
547 * @gpio: gpio to make available, already requested
548 * @direction_may_change: true if userspace may change gpio direction
549 * Context: arch_initcall or later
550 *
551 * When drivers want to make a GPIO accessible to userspace after they
552 * have requested it -- perhaps while debugging, or as part of their
553 * public interface -- they may use this routine. If the GPIO can
554 * change direction (some can't) and the caller allows it, userspace
555 * will see "direction" sysfs attribute which may be used to change
556 * the gpio's direction. A "value" attribute will always be provided.
557 *
558 * Returns zero on success, else an error.
559 */
560int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
561{
Johan Hovold483d8212015-04-21 17:42:09 +0200562 struct gpio_chip *chip;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900563 unsigned long flags;
564 int status;
565 const char *ioname = NULL;
566 struct device *dev;
567 int offset;
568
569 /* can't export until sysfs is available ... */
570 if (!gpio_class.p) {
571 pr_debug("%s: called too early!\n", __func__);
572 return -ENOENT;
573 }
574
575 if (!desc) {
576 pr_debug("%s: invalid gpio descriptor\n", __func__);
577 return -EINVAL;
578 }
579
Johan Hovold483d8212015-04-21 17:42:09 +0200580 chip = desc->chip;
581
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900582 mutex_lock(&sysfs_lock);
583
Johan Hovold483d8212015-04-21 17:42:09 +0200584 /* check if chip is being removed */
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200585 if (!chip || !chip->cdev) {
Johan Hovold483d8212015-04-21 17:42:09 +0200586 status = -ENODEV;
587 goto fail_unlock;
588 }
589
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900590 spin_lock_irqsave(&gpio_lock, flags);
591 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
592 test_bit(FLAG_EXPORT, &desc->flags)) {
593 spin_unlock_irqrestore(&gpio_lock, flags);
594 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
595 __func__,
596 test_bit(FLAG_REQUESTED, &desc->flags),
597 test_bit(FLAG_EXPORT, &desc->flags));
598 status = -EPERM;
599 goto fail_unlock;
600 }
601
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200602 if (chip->direction_input && chip->direction_output &&
Johan Hovoldebbeba12015-01-13 13:00:06 +0100603 direction_may_change) {
604 set_bit(FLAG_SYSFS_DIR, &desc->flags);
605 }
606
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900607 spin_unlock_irqrestore(&gpio_lock, flags);
608
609 offset = gpio_chip_hwgpio(desc);
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200610 if (chip->names && chip->names[offset])
611 ioname = chip->names[offset];
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900612
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200613 dev = device_create_with_groups(&gpio_class, chip->dev,
Johan Hovold0915e6f2015-01-13 13:00:05 +0100614 MKDEV(0, 0), desc, gpio_groups,
615 ioname ? ioname : "gpio%u",
616 desc_to_gpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900617 if (IS_ERR(dev)) {
618 status = PTR_ERR(dev);
619 goto fail_unlock;
620 }
621
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900622 set_bit(FLAG_EXPORT, &desc->flags);
623 mutex_unlock(&sysfs_lock);
624 return 0;
625
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900626fail_unlock:
627 mutex_unlock(&sysfs_lock);
628 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
629 return status;
630}
631EXPORT_SYMBOL_GPL(gpiod_export);
632
633static int match_export(struct device *dev, const void *data)
634{
635 return dev_get_drvdata(dev) == data;
636}
637
638/**
639 * gpiod_export_link - create a sysfs link to an exported GPIO node
640 * @dev: device under which to create symlink
641 * @name: name of the symlink
642 * @gpio: gpio to create symlink to, already exported
643 *
644 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
645 * node. Caller is responsible for unlinking.
646 *
647 * Returns zero on success, else an error.
648 */
649int gpiod_export_link(struct device *dev, const char *name,
650 struct gpio_desc *desc)
651{
652 int status = -EINVAL;
653
654 if (!desc) {
655 pr_warn("%s: invalid GPIO\n", __func__);
656 return -EINVAL;
657 }
658
659 mutex_lock(&sysfs_lock);
660
661 if (test_bit(FLAG_EXPORT, &desc->flags)) {
662 struct device *tdev;
663
664 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
665 if (tdev != NULL) {
666 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
667 name);
Johan Hovold0f303db2015-01-26 12:02:45 +0100668 put_device(tdev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900669 } else {
670 status = -ENODEV;
671 }
672 }
673
674 mutex_unlock(&sysfs_lock);
675
676 if (status)
677 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
678
679 return status;
680}
681EXPORT_SYMBOL_GPL(gpiod_export_link);
682
683/**
684 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
685 * @gpio: gpio to change
686 * @value: non-zero to use active low, i.e. inverted values
687 *
688 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
689 * The GPIO does not have to be exported yet. If poll(2) support has
690 * been enabled for either rising or falling edge, it will be
691 * reconfigured to follow the new polarity.
692 *
693 * Returns zero on success, else an error.
694 */
695int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
696{
697 struct device *dev = NULL;
698 int status = -EINVAL;
699
700 if (!desc) {
701 pr_warn("%s: invalid GPIO\n", __func__);
702 return -EINVAL;
703 }
704
705 mutex_lock(&sysfs_lock);
706
707 if (test_bit(FLAG_EXPORT, &desc->flags)) {
708 dev = class_find_device(&gpio_class, NULL, desc, match_export);
709 if (dev == NULL) {
710 status = -ENODEV;
711 goto unlock;
712 }
713 }
714
715 status = sysfs_set_active_low(desc, dev, value);
Johan Hovold49d2ca82015-01-26 12:02:46 +0100716 put_device(dev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900717unlock:
718 mutex_unlock(&sysfs_lock);
719
720 if (status)
721 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
722
723 return status;
724}
725EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
726
727/**
728 * gpiod_unexport - reverse effect of gpio_export()
729 * @gpio: gpio to make unavailable
730 *
731 * This is implicit on gpio_free().
732 */
733void gpiod_unexport(struct gpio_desc *desc)
734{
735 int status = 0;
736 struct device *dev = NULL;
737
738 if (!desc) {
739 pr_warn("%s: invalid GPIO\n", __func__);
740 return;
741 }
742
743 mutex_lock(&sysfs_lock);
744
745 if (test_bit(FLAG_EXPORT, &desc->flags)) {
746
747 dev = class_find_device(&gpio_class, NULL, desc, match_export);
748 if (dev) {
749 gpio_setup_irq(desc, dev, 0);
Johan Hovoldebbeba12015-01-13 13:00:06 +0100750 clear_bit(FLAG_SYSFS_DIR, &desc->flags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900751 clear_bit(FLAG_EXPORT, &desc->flags);
752 } else
753 status = -ENODEV;
754 }
755
756 mutex_unlock(&sysfs_lock);
757
758 if (dev) {
759 device_unregister(dev);
760 put_device(dev);
761 }
762
763 if (status)
764 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
765}
766EXPORT_SYMBOL_GPL(gpiod_unexport);
767
768int gpiochip_export(struct gpio_chip *chip)
769{
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900770 struct device *dev;
771
772 /* Many systems register gpio chips for SOC support very early,
773 * before driver model support is available. In those cases we
774 * export this later, in gpiolib_sysfs_init() ... here we just
775 * verify that _some_ field of gpio_class got initialized.
776 */
777 if (!gpio_class.p)
778 return 0;
779
780 /* use chip->base for the ID; it's already known to be unique */
Johan Hovold121b6a72015-01-13 13:00:04 +0100781 dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0),
782 chip, gpiochip_groups,
783 "gpiochip%d", chip->base);
784 if (IS_ERR(dev))
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200785 return PTR_ERR(dev);
Johan Hovold3ff74be2015-05-04 17:10:30 +0200786
787 mutex_lock(&sysfs_lock);
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200788 chip->cdev = dev;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900789 mutex_unlock(&sysfs_lock);
790
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200791 return 0;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900792}
793
794void gpiochip_unexport(struct gpio_chip *chip)
795{
Johan Hovold483d8212015-04-21 17:42:09 +0200796 struct gpio_desc *desc;
797 unsigned int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900798
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200799 if (!chip->cdev)
800 return;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900801
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200802 device_unregister(chip->cdev);
803
804 /* prevent further gpiod exports */
805 mutex_lock(&sysfs_lock);
806 chip->cdev = NULL;
807 mutex_unlock(&sysfs_lock);
Johan Hovold483d8212015-04-21 17:42:09 +0200808
809 /* unregister gpiod class devices owned by sysfs */
810 for (i = 0; i < chip->ngpio; i++) {
811 desc = &chip->desc[i];
812 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
813 gpiod_free(desc);
814 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900815}
816
817static int __init gpiolib_sysfs_init(void)
818{
819 int status;
820 unsigned long flags;
821 struct gpio_chip *chip;
822
823 status = class_register(&gpio_class);
824 if (status < 0)
825 return status;
826
827 /* Scan and register the gpio_chips which registered very
828 * early (e.g. before the class_register above was called).
829 *
830 * We run before arch_initcall() so chip->dev nodes can have
831 * registered, and so arch_initcall() can always gpio_export().
832 */
833 spin_lock_irqsave(&gpio_lock, flags);
834 list_for_each_entry(chip, &gpio_chips, list) {
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200835 if (chip->cdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900836 continue;
837
Alexandre Courbot14141a92014-07-22 16:17:40 +0900838 /*
839 * TODO we yield gpio_lock here because gpiochip_export()
840 * acquires a mutex. This is unsafe and needs to be fixed.
841 *
842 * Also it would be nice to use gpiochip_find() here so we
843 * can keep gpio_chips local to gpiolib.c, but the yield of
844 * gpio_lock prevents us from doing this.
845 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900846 spin_unlock_irqrestore(&gpio_lock, flags);
847 status = gpiochip_export(chip);
848 spin_lock_irqsave(&gpio_lock, flags);
849 }
850 spin_unlock_irqrestore(&gpio_lock, flags);
851
852
853 return status;
854}
855postcore_initcall(gpiolib_sysfs_init);