blob: 10baf31efe744ee2f8aacebf8bbb5270e73564e1 [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
Johan Hovold6beac9d12015-05-04 17:10:34 +020041static ssize_t direction_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090042 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
Johan Hovoldf0b78662015-05-04 17:10:36 +020049 gpiod_get_direction(desc);
50 status = sprintf(buf, "%s\n",
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090051 test_bit(FLAG_IS_OUT, &desc->flags)
52 ? "out" : "in");
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090053
54 mutex_unlock(&sysfs_lock);
55 return status;
56}
57
Johan Hovold6beac9d12015-05-04 17:10:34 +020058static ssize_t direction_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090059 struct device_attribute *attr, const char *buf, size_t size)
60{
61 struct gpio_desc *desc = dev_get_drvdata(dev);
62 ssize_t status;
63
64 mutex_lock(&sysfs_lock);
65
Johan Hovoldf0b78662015-05-04 17:10:36 +020066 if (sysfs_streq(buf, "high"))
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090067 status = gpiod_direction_output_raw(desc, 1);
68 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
69 status = gpiod_direction_output_raw(desc, 0);
70 else if (sysfs_streq(buf, "in"))
71 status = gpiod_direction_input(desc);
72 else
73 status = -EINVAL;
74
75 mutex_unlock(&sysfs_lock);
76 return status ? : size;
77}
Johan Hovold6beac9d12015-05-04 17:10:34 +020078static DEVICE_ATTR_RW(direction);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090079
Johan Hovold6beac9d12015-05-04 17:10:34 +020080static ssize_t value_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090081 struct device_attribute *attr, char *buf)
82{
83 struct gpio_desc *desc = dev_get_drvdata(dev);
84 ssize_t status;
85
86 mutex_lock(&sysfs_lock);
87
Johan Hovoldf0b78662015-05-04 17:10:36 +020088 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090089
90 mutex_unlock(&sysfs_lock);
91 return status;
92}
93
Johan Hovold6beac9d12015-05-04 17:10:34 +020094static ssize_t value_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090095 struct device_attribute *attr, const char *buf, size_t size)
96{
97 struct gpio_desc *desc = dev_get_drvdata(dev);
98 ssize_t status;
99
100 mutex_lock(&sysfs_lock);
101
Johan Hovoldf0b78662015-05-04 17:10:36 +0200102 if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900103 status = -EPERM;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200104 } else {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900105 long value;
106
107 status = kstrtol(buf, 0, &value);
108 if (status == 0) {
109 gpiod_set_value_cansleep(desc, value);
110 status = size;
111 }
112 }
113
114 mutex_unlock(&sysfs_lock);
115 return status;
116}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200117static DEVICE_ATTR_RW(value);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900118
119static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
120{
121 struct kernfs_node *value_sd = priv;
122
123 sysfs_notify_dirent(value_sd);
124 return IRQ_HANDLED;
125}
126
127static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
128 unsigned long gpio_flags)
129{
130 struct kernfs_node *value_sd;
131 unsigned long irq_flags;
132 int ret, irq, id;
133
134 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
135 return 0;
136
137 irq = gpiod_to_irq(desc);
138 if (irq < 0)
139 return -EIO;
140
141 id = desc->flags >> ID_SHIFT;
142 value_sd = idr_find(&dirent_idr, id);
143 if (value_sd)
144 free_irq(irq, value_sd);
145
146 desc->flags &= ~GPIO_TRIGGER_MASK;
147
148 if (!gpio_flags) {
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900149 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900150 ret = 0;
151 goto free_id;
152 }
153
154 irq_flags = IRQF_SHARED;
155 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
156 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
157 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
158 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
159 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
160 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
161
162 if (!value_sd) {
163 value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
164 if (!value_sd) {
165 ret = -ENODEV;
166 goto err_out;
167 }
168
169 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
170 if (ret < 0)
171 goto free_sd;
172 id = ret;
173
174 desc->flags &= GPIO_FLAGS_MASK;
175 desc->flags |= (unsigned long)id << ID_SHIFT;
176
177 if (desc->flags >> ID_SHIFT != id) {
178 ret = -ERANGE;
179 goto free_id;
180 }
181 }
182
Johan Hovold52176d02015-05-04 17:10:28 +0200183 /*
184 * FIXME: This should be done in the irq_request_resources callback
185 * when the irq is requested, but a few drivers currently fail
186 * to do so.
187 *
188 * Remove this redundant call (along with the corresponding
189 * unlock) when those drivers have been fixed.
190 */
191 ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900192 if (ret < 0)
193 goto free_id;
194
Johan Hovold52176d02015-05-04 17:10:28 +0200195 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
196 "gpiolib", value_sd);
197 if (ret < 0)
198 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900199
200 desc->flags |= gpio_flags;
201 return 0;
202
Johan Hovold52176d02015-05-04 17:10:28 +0200203err_unlock:
204 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900205free_id:
206 idr_remove(&dirent_idr, id);
207 desc->flags &= GPIO_FLAGS_MASK;
208free_sd:
209 if (value_sd)
210 sysfs_put(value_sd);
211err_out:
212 return ret;
213}
214
215static const struct {
216 const char *name;
217 unsigned long flags;
218} trigger_types[] = {
219 { "none", 0 },
220 { "falling", BIT(FLAG_TRIG_FALL) },
221 { "rising", BIT(FLAG_TRIG_RISE) },
222 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
223};
224
Johan Hovold6beac9d12015-05-04 17:10:34 +0200225static ssize_t edge_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900226 struct device_attribute *attr, char *buf)
227{
228 const struct gpio_desc *desc = dev_get_drvdata(dev);
Johan Hovoldf0b78662015-05-04 17:10:36 +0200229 unsigned long mask;
230 ssize_t status = 0;
231 int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900232
233 mutex_lock(&sysfs_lock);
234
Johan Hovoldf0b78662015-05-04 17:10:36 +0200235 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
236 mask = desc->flags & GPIO_TRIGGER_MASK;
237 if (mask == trigger_types[i].flags) {
238 status = sprintf(buf, "%s\n", trigger_types[i].name);
239 break;
240 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900241 }
242
243 mutex_unlock(&sysfs_lock);
244 return status;
245}
246
Johan Hovold6beac9d12015-05-04 17:10:34 +0200247static ssize_t edge_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900248 struct device_attribute *attr, const char *buf, size_t size)
249{
250 struct gpio_desc *desc = dev_get_drvdata(dev);
251 ssize_t status;
252 int i;
253
254 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
255 if (sysfs_streq(trigger_types[i].name, buf))
256 goto found;
257 return -EINVAL;
258
259found:
260 mutex_lock(&sysfs_lock);
261
Johan Hovoldf0b78662015-05-04 17:10:36 +0200262 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
263 if (!status)
264 status = size;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900265
266 mutex_unlock(&sysfs_lock);
267
268 return status;
269}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200270static DEVICE_ATTR_RW(edge);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900271
272static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
273 int value)
274{
275 int status = 0;
276
277 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
278 return 0;
279
280 if (value)
281 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
282 else
283 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
284
285 /* reconfigure poll(2) support if enabled on one edge only */
Johan Hovold166a85e2015-05-04 17:10:33 +0200286 if (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
287 !!test_bit(FLAG_TRIG_FALL, &desc->flags)) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900288 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
289
290 gpio_setup_irq(desc, dev, 0);
291 status = gpio_setup_irq(desc, dev, trigger_flags);
292 }
293
294 return status;
295}
296
Johan Hovold6beac9d12015-05-04 17:10:34 +0200297static ssize_t active_low_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900298 struct device_attribute *attr, char *buf)
299{
300 const struct gpio_desc *desc = dev_get_drvdata(dev);
301 ssize_t status;
302
303 mutex_lock(&sysfs_lock);
304
Johan Hovoldf0b78662015-05-04 17:10:36 +0200305 status = sprintf(buf, "%d\n",
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900306 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
307
308 mutex_unlock(&sysfs_lock);
309
310 return status;
311}
312
Johan Hovold6beac9d12015-05-04 17:10:34 +0200313static ssize_t active_low_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900314 struct device_attribute *attr, const char *buf, size_t size)
315{
316 struct gpio_desc *desc = dev_get_drvdata(dev);
317 ssize_t status;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200318 long value;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900319
320 mutex_lock(&sysfs_lock);
321
Johan Hovoldf0b78662015-05-04 17:10:36 +0200322 status = kstrtol(buf, 0, &value);
323 if (status == 0)
324 status = sysfs_set_active_low(desc, dev, value != 0);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900325
326 mutex_unlock(&sysfs_lock);
327
328 return status ? : size;
329}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200330static DEVICE_ATTR_RW(active_low);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900331
Johan Hovoldebbeba12015-01-13 13:00:06 +0100332static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
333 int n)
334{
335 struct device *dev = container_of(kobj, struct device, kobj);
336 struct gpio_desc *desc = dev_get_drvdata(dev);
337 umode_t mode = attr->mode;
338 bool show_direction = test_bit(FLAG_SYSFS_DIR, &desc->flags);
339
340 if (attr == &dev_attr_direction.attr) {
341 if (!show_direction)
342 mode = 0;
343 } else if (attr == &dev_attr_edge.attr) {
344 if (gpiod_to_irq(desc) < 0)
345 mode = 0;
346 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
347 mode = 0;
348 }
349
350 return mode;
351}
352
Johan Hovold0915e6f2015-01-13 13:00:05 +0100353static struct attribute *gpio_attrs[] = {
Johan Hovoldebbeba12015-01-13 13:00:06 +0100354 &dev_attr_direction.attr,
355 &dev_attr_edge.attr,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900356 &dev_attr_value.attr,
357 &dev_attr_active_low.attr,
358 NULL,
359};
Johan Hovoldebbeba12015-01-13 13:00:06 +0100360
361static const struct attribute_group gpio_group = {
362 .attrs = gpio_attrs,
363 .is_visible = gpio_is_visible,
364};
365
366static const struct attribute_group *gpio_groups[] = {
367 &gpio_group,
368 NULL
369};
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900370
371/*
372 * /sys/class/gpio/gpiochipN/
373 * /base ... matching gpio_chip.base (N)
374 * /label ... matching gpio_chip.label
375 * /ngpio ... matching gpio_chip.ngpio
376 */
377
Johan Hovold6beac9d12015-05-04 17:10:34 +0200378static ssize_t base_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900379 struct device_attribute *attr, char *buf)
380{
381 const struct gpio_chip *chip = dev_get_drvdata(dev);
382
383 return sprintf(buf, "%d\n", chip->base);
384}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200385static DEVICE_ATTR_RO(base);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900386
Johan Hovold6beac9d12015-05-04 17:10:34 +0200387static ssize_t label_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900388 struct device_attribute *attr, char *buf)
389{
390 const struct gpio_chip *chip = dev_get_drvdata(dev);
391
392 return sprintf(buf, "%s\n", chip->label ? : "");
393}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200394static DEVICE_ATTR_RO(label);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900395
Johan Hovold6beac9d12015-05-04 17:10:34 +0200396static ssize_t ngpio_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900397 struct device_attribute *attr, char *buf)
398{
399 const struct gpio_chip *chip = dev_get_drvdata(dev);
400
401 return sprintf(buf, "%u\n", chip->ngpio);
402}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200403static DEVICE_ATTR_RO(ngpio);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900404
Johan Hovold121b6a72015-01-13 13:00:04 +0100405static struct attribute *gpiochip_attrs[] = {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900406 &dev_attr_base.attr,
407 &dev_attr_label.attr,
408 &dev_attr_ngpio.attr,
409 NULL,
410};
Johan Hovold121b6a72015-01-13 13:00:04 +0100411ATTRIBUTE_GROUPS(gpiochip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900412
413/*
414 * /sys/class/gpio/export ... write-only
415 * integer N ... number of GPIO to export (full access)
416 * /sys/class/gpio/unexport ... write-only
417 * integer N ... number of GPIO to unexport
418 */
419static ssize_t export_store(struct class *class,
420 struct class_attribute *attr,
421 const char *buf, size_t len)
422{
423 long gpio;
424 struct gpio_desc *desc;
425 int status;
426
427 status = kstrtol(buf, 0, &gpio);
428 if (status < 0)
429 goto done;
430
431 desc = gpio_to_desc(gpio);
432 /* reject invalid GPIOs */
433 if (!desc) {
434 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
435 return -EINVAL;
436 }
437
438 /* No extra locking here; FLAG_SYSFS just signifies that the
439 * request and export were done by on behalf of userspace, so
440 * they may be undone on its behalf too.
441 */
442
443 status = gpiod_request(desc, "sysfs");
444 if (status < 0) {
445 if (status == -EPROBE_DEFER)
446 status = -ENODEV;
447 goto done;
448 }
449 status = gpiod_export(desc, true);
450 if (status < 0)
451 gpiod_free(desc);
452 else
453 set_bit(FLAG_SYSFS, &desc->flags);
454
455done:
456 if (status)
457 pr_debug("%s: status %d\n", __func__, status);
458 return status ? : len;
459}
460
461static ssize_t unexport_store(struct class *class,
462 struct class_attribute *attr,
463 const char *buf, size_t len)
464{
465 long gpio;
466 struct gpio_desc *desc;
467 int status;
468
469 status = kstrtol(buf, 0, &gpio);
470 if (status < 0)
471 goto done;
472
473 desc = gpio_to_desc(gpio);
474 /* reject bogus commands (gpio_unexport ignores them) */
475 if (!desc) {
476 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
477 return -EINVAL;
478 }
479
480 status = -EINVAL;
481
482 /* No extra locking here; FLAG_SYSFS just signifies that the
483 * request and export were done by on behalf of userspace, so
484 * they may be undone on its behalf too.
485 */
486 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
487 status = 0;
488 gpiod_free(desc);
489 }
490done:
491 if (status)
492 pr_debug("%s: status %d\n", __func__, status);
493 return status ? : len;
494}
495
496static struct class_attribute gpio_class_attrs[] = {
497 __ATTR(export, 0200, NULL, export_store),
498 __ATTR(unexport, 0200, NULL, unexport_store),
499 __ATTR_NULL,
500};
501
502static struct class gpio_class = {
503 .name = "gpio",
504 .owner = THIS_MODULE,
505
506 .class_attrs = gpio_class_attrs,
507};
508
509
510/**
511 * gpiod_export - export a GPIO through sysfs
512 * @gpio: gpio to make available, already requested
513 * @direction_may_change: true if userspace may change gpio direction
514 * Context: arch_initcall or later
515 *
516 * When drivers want to make a GPIO accessible to userspace after they
517 * have requested it -- perhaps while debugging, or as part of their
518 * public interface -- they may use this routine. If the GPIO can
519 * change direction (some can't) and the caller allows it, userspace
520 * will see "direction" sysfs attribute which may be used to change
521 * the gpio's direction. A "value" attribute will always be provided.
522 *
523 * Returns zero on success, else an error.
524 */
525int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
526{
Johan Hovold483d8212015-04-21 17:42:09 +0200527 struct gpio_chip *chip;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900528 unsigned long flags;
529 int status;
530 const char *ioname = NULL;
531 struct device *dev;
532 int offset;
533
534 /* can't export until sysfs is available ... */
535 if (!gpio_class.p) {
536 pr_debug("%s: called too early!\n", __func__);
537 return -ENOENT;
538 }
539
540 if (!desc) {
541 pr_debug("%s: invalid gpio descriptor\n", __func__);
542 return -EINVAL;
543 }
544
Johan Hovold483d8212015-04-21 17:42:09 +0200545 chip = desc->chip;
546
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900547 mutex_lock(&sysfs_lock);
548
Johan Hovold483d8212015-04-21 17:42:09 +0200549 /* check if chip is being removed */
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200550 if (!chip || !chip->cdev) {
Johan Hovold483d8212015-04-21 17:42:09 +0200551 status = -ENODEV;
552 goto fail_unlock;
553 }
554
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900555 spin_lock_irqsave(&gpio_lock, flags);
556 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
557 test_bit(FLAG_EXPORT, &desc->flags)) {
558 spin_unlock_irqrestore(&gpio_lock, flags);
559 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
560 __func__,
561 test_bit(FLAG_REQUESTED, &desc->flags),
562 test_bit(FLAG_EXPORT, &desc->flags));
563 status = -EPERM;
564 goto fail_unlock;
565 }
566
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200567 if (chip->direction_input && chip->direction_output &&
Johan Hovoldebbeba12015-01-13 13:00:06 +0100568 direction_may_change) {
569 set_bit(FLAG_SYSFS_DIR, &desc->flags);
570 }
571
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900572 spin_unlock_irqrestore(&gpio_lock, flags);
573
574 offset = gpio_chip_hwgpio(desc);
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200575 if (chip->names && chip->names[offset])
576 ioname = chip->names[offset];
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900577
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200578 dev = device_create_with_groups(&gpio_class, chip->dev,
Johan Hovold0915e6f2015-01-13 13:00:05 +0100579 MKDEV(0, 0), desc, gpio_groups,
580 ioname ? ioname : "gpio%u",
581 desc_to_gpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900582 if (IS_ERR(dev)) {
583 status = PTR_ERR(dev);
584 goto fail_unlock;
585 }
586
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900587 set_bit(FLAG_EXPORT, &desc->flags);
588 mutex_unlock(&sysfs_lock);
589 return 0;
590
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900591fail_unlock:
592 mutex_unlock(&sysfs_lock);
593 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
594 return status;
595}
596EXPORT_SYMBOL_GPL(gpiod_export);
597
598static int match_export(struct device *dev, const void *data)
599{
600 return dev_get_drvdata(dev) == data;
601}
602
603/**
604 * gpiod_export_link - create a sysfs link to an exported GPIO node
605 * @dev: device under which to create symlink
606 * @name: name of the symlink
607 * @gpio: gpio to create symlink to, already exported
608 *
609 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
610 * node. Caller is responsible for unlinking.
611 *
612 * Returns zero on success, else an error.
613 */
614int gpiod_export_link(struct device *dev, const char *name,
615 struct gpio_desc *desc)
616{
617 int status = -EINVAL;
618
619 if (!desc) {
620 pr_warn("%s: invalid GPIO\n", __func__);
621 return -EINVAL;
622 }
623
624 mutex_lock(&sysfs_lock);
625
626 if (test_bit(FLAG_EXPORT, &desc->flags)) {
627 struct device *tdev;
628
629 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
630 if (tdev != NULL) {
631 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
632 name);
Johan Hovold0f303db2015-01-26 12:02:45 +0100633 put_device(tdev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900634 } else {
635 status = -ENODEV;
636 }
637 }
638
639 mutex_unlock(&sysfs_lock);
640
641 if (status)
642 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
643
644 return status;
645}
646EXPORT_SYMBOL_GPL(gpiod_export_link);
647
648/**
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900649 * gpiod_unexport - reverse effect of gpio_export()
650 * @gpio: gpio to make unavailable
651 *
652 * This is implicit on gpio_free().
653 */
654void gpiod_unexport(struct gpio_desc *desc)
655{
656 int status = 0;
657 struct device *dev = NULL;
658
659 if (!desc) {
660 pr_warn("%s: invalid GPIO\n", __func__);
661 return;
662 }
663
664 mutex_lock(&sysfs_lock);
665
666 if (test_bit(FLAG_EXPORT, &desc->flags)) {
667
668 dev = class_find_device(&gpio_class, NULL, desc, match_export);
669 if (dev) {
Johan Hovoldebbeba12015-01-13 13:00:06 +0100670 clear_bit(FLAG_SYSFS_DIR, &desc->flags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900671 clear_bit(FLAG_EXPORT, &desc->flags);
672 } else
673 status = -ENODEV;
674 }
675
676 mutex_unlock(&sysfs_lock);
677
678 if (dev) {
679 device_unregister(dev);
Johan Hovold54d9acd2015-05-04 17:10:35 +0200680 /*
681 * Release irq after deregistration to prevent race with
682 * edge_store.
683 */
684 gpio_setup_irq(desc, dev, 0);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900685 put_device(dev);
686 }
687
688 if (status)
689 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
690}
691EXPORT_SYMBOL_GPL(gpiod_unexport);
692
Johan Hovold426577b2015-05-04 17:10:32 +0200693int gpiochip_sysfs_register(struct gpio_chip *chip)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900694{
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900695 struct device *dev;
696
Johan Hovold426577b2015-05-04 17:10:32 +0200697 /*
698 * Many systems add gpio chips for SOC support very early,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900699 * before driver model support is available. In those cases we
Johan Hovold426577b2015-05-04 17:10:32 +0200700 * register later, in gpiolib_sysfs_init() ... here we just
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900701 * verify that _some_ field of gpio_class got initialized.
702 */
703 if (!gpio_class.p)
704 return 0;
705
706 /* use chip->base for the ID; it's already known to be unique */
Johan Hovold121b6a72015-01-13 13:00:04 +0100707 dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0),
708 chip, gpiochip_groups,
709 "gpiochip%d", chip->base);
710 if (IS_ERR(dev))
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200711 return PTR_ERR(dev);
Johan Hovold3ff74be2015-05-04 17:10:30 +0200712
713 mutex_lock(&sysfs_lock);
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200714 chip->cdev = dev;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900715 mutex_unlock(&sysfs_lock);
716
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200717 return 0;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900718}
719
Johan Hovold426577b2015-05-04 17:10:32 +0200720void gpiochip_sysfs_unregister(struct gpio_chip *chip)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900721{
Johan Hovold483d8212015-04-21 17:42:09 +0200722 struct gpio_desc *desc;
723 unsigned int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900724
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200725 if (!chip->cdev)
726 return;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900727
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200728 device_unregister(chip->cdev);
729
730 /* prevent further gpiod exports */
731 mutex_lock(&sysfs_lock);
732 chip->cdev = NULL;
733 mutex_unlock(&sysfs_lock);
Johan Hovold483d8212015-04-21 17:42:09 +0200734
735 /* unregister gpiod class devices owned by sysfs */
736 for (i = 0; i < chip->ngpio; i++) {
737 desc = &chip->desc[i];
738 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
739 gpiod_free(desc);
740 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900741}
742
743static int __init gpiolib_sysfs_init(void)
744{
745 int status;
746 unsigned long flags;
747 struct gpio_chip *chip;
748
749 status = class_register(&gpio_class);
750 if (status < 0)
751 return status;
752
753 /* Scan and register the gpio_chips which registered very
754 * early (e.g. before the class_register above was called).
755 *
756 * We run before arch_initcall() so chip->dev nodes can have
757 * registered, and so arch_initcall() can always gpio_export().
758 */
759 spin_lock_irqsave(&gpio_lock, flags);
760 list_for_each_entry(chip, &gpio_chips, list) {
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200761 if (chip->cdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900762 continue;
763
Alexandre Courbot14141a92014-07-22 16:17:40 +0900764 /*
Johan Hovold426577b2015-05-04 17:10:32 +0200765 * TODO we yield gpio_lock here because
766 * gpiochip_sysfs_register() acquires a mutex. This is unsafe
767 * and needs to be fixed.
Alexandre Courbot14141a92014-07-22 16:17:40 +0900768 *
769 * Also it would be nice to use gpiochip_find() here so we
770 * can keep gpio_chips local to gpiolib.c, but the yield of
771 * gpio_lock prevents us from doing this.
772 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900773 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold426577b2015-05-04 17:10:32 +0200774 status = gpiochip_sysfs_register(chip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900775 spin_lock_irqsave(&gpio_lock, flags);
776 }
777 spin_unlock_irqrestore(&gpio_lock, flags);
778
779
780 return status;
781}
782postcore_initcall(gpiolib_sysfs_init);