gpio: sysfs: remove FLAG_SYSFS_DIR
[deliverable/linux.git] / drivers / gpio / gpiolib-sysfs.c
CommitLineData
0eb4c6c2
AC
1#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>
c43960fb 9#include <linux/slab.h>
0eb4c6c2
AC
10
11#include "gpiolib.h"
12
c43960fb
JH
13struct gpiod_data {
14 struct gpio_desc *desc;
6ffcb797
JH
15
16 struct mutex mutex;
a08f5c21 17 struct kernfs_node *value_kn;
2ec74a95 18 int irq;
427fdeef
JH
19
20 bool direction_can_change;
c43960fb
JH
21};
22
6ffcb797
JH
23/*
24 * Lock to serialise gpiod export and unexport, and prevent re-export of
25 * gpiod whose chip is being unregistered.
0eb4c6c2
AC
26 */
27static DEFINE_MUTEX(sysfs_lock);
28
29/*
30 * /sys/class/gpio/gpioN... only for GPIOs that are exported
31 * /direction
32 * * MAY BE OMITTED if kernel won't allow direction changes
33 * * is read/write as "in" or "out"
34 * * may also be written as "high" or "low", initializing
35 * output value as specified ("out" implies "low")
36 * /value
37 * * always readable, subject to hardware behavior
38 * * may be writable, as zero/nonzero
39 * /edge
40 * * configures behavior of poll(2) on /value
41 * * available only if pin can generate IRQs on input
42 * * is read/write as "none", "falling", "rising", or "both"
43 * /active_low
44 * * configures polarity of /value
45 * * is read/write as zero/nonzero
46 * * also affects existing and subsequent "falling" and "rising"
47 * /edge configuration
48 */
49
6beac9d1 50static ssize_t direction_show(struct device *dev,
0eb4c6c2
AC
51 struct device_attribute *attr, char *buf)
52{
c43960fb
JH
53 struct gpiod_data *data = dev_get_drvdata(dev);
54 struct gpio_desc *desc = data->desc;
0eb4c6c2
AC
55 ssize_t status;
56
6ffcb797 57 mutex_lock(&data->mutex);
0eb4c6c2 58
f0b7866a
JH
59 gpiod_get_direction(desc);
60 status = sprintf(buf, "%s\n",
0eb4c6c2
AC
61 test_bit(FLAG_IS_OUT, &desc->flags)
62 ? "out" : "in");
0eb4c6c2 63
6ffcb797
JH
64 mutex_unlock(&data->mutex);
65
0eb4c6c2
AC
66 return status;
67}
68
6beac9d1 69static ssize_t direction_store(struct device *dev,
0eb4c6c2
AC
70 struct device_attribute *attr, const char *buf, size_t size)
71{
c43960fb
JH
72 struct gpiod_data *data = dev_get_drvdata(dev);
73 struct gpio_desc *desc = data->desc;
0eb4c6c2
AC
74 ssize_t status;
75
6ffcb797 76 mutex_lock(&data->mutex);
0eb4c6c2 77
f0b7866a 78 if (sysfs_streq(buf, "high"))
0eb4c6c2
AC
79 status = gpiod_direction_output_raw(desc, 1);
80 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
81 status = gpiod_direction_output_raw(desc, 0);
82 else if (sysfs_streq(buf, "in"))
83 status = gpiod_direction_input(desc);
84 else
85 status = -EINVAL;
86
6ffcb797
JH
87 mutex_unlock(&data->mutex);
88
0eb4c6c2
AC
89 return status ? : size;
90}
6beac9d1 91static DEVICE_ATTR_RW(direction);
0eb4c6c2 92
6beac9d1 93static ssize_t value_show(struct device *dev,
0eb4c6c2
AC
94 struct device_attribute *attr, char *buf)
95{
c43960fb
JH
96 struct gpiod_data *data = dev_get_drvdata(dev);
97 struct gpio_desc *desc = data->desc;
0eb4c6c2
AC
98 ssize_t status;
99
6ffcb797 100 mutex_lock(&data->mutex);
0eb4c6c2 101
f0b7866a 102 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
0eb4c6c2 103
6ffcb797
JH
104 mutex_unlock(&data->mutex);
105
0eb4c6c2
AC
106 return status;
107}
108
6beac9d1 109static ssize_t value_store(struct device *dev,
0eb4c6c2
AC
110 struct device_attribute *attr, const char *buf, size_t size)
111{
c43960fb
JH
112 struct gpiod_data *data = dev_get_drvdata(dev);
113 struct gpio_desc *desc = data->desc;
0eb4c6c2
AC
114 ssize_t status;
115
6ffcb797 116 mutex_lock(&data->mutex);
0eb4c6c2 117
f0b7866a 118 if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
0eb4c6c2 119 status = -EPERM;
f0b7866a 120 } else {
0eb4c6c2
AC
121 long value;
122
123 status = kstrtol(buf, 0, &value);
124 if (status == 0) {
125 gpiod_set_value_cansleep(desc, value);
126 status = size;
127 }
128 }
129
6ffcb797
JH
130 mutex_unlock(&data->mutex);
131
0eb4c6c2
AC
132 return status;
133}
6beac9d1 134static DEVICE_ATTR_RW(value);
0eb4c6c2
AC
135
136static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
137{
a08f5c21
JH
138 struct gpiod_data *data = priv;
139
140 sysfs_notify_dirent(data->value_kn);
0eb4c6c2 141
0eb4c6c2
AC
142 return IRQ_HANDLED;
143}
144
6ffcb797 145/* Caller holds gpiod-data mutex. */
2ec74a95 146static int gpio_sysfs_request_irq(struct device *dev, unsigned long gpio_flags)
0eb4c6c2 147{
0f628508
JH
148 struct gpiod_data *data = dev_get_drvdata(dev);
149 struct gpio_desc *desc = data->desc;
0eb4c6c2 150 unsigned long irq_flags;
2ec74a95 151 int ret;
0eb4c6c2 152
2ec74a95
JH
153 data->irq = gpiod_to_irq(desc);
154 if (data->irq < 0)
0eb4c6c2
AC
155 return -EIO;
156
2ec74a95
JH
157 data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
158 if (!data->value_kn)
159 return -ENODEV;
0eb4c6c2
AC
160
161 irq_flags = IRQF_SHARED;
162 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
163 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
164 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
165 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
166 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
167 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
168
52176d0d
JH
169 /*
170 * FIXME: This should be done in the irq_request_resources callback
171 * when the irq is requested, but a few drivers currently fail
172 * to do so.
173 *
174 * Remove this redundant call (along with the corresponding
175 * unlock) when those drivers have been fixed.
176 */
177 ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
0eb4c6c2 178 if (ret < 0)
2ec74a95 179 goto err_put_kn;
0eb4c6c2 180
2ec74a95 181 ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
a08f5c21 182 "gpiolib", data);
52176d0d
JH
183 if (ret < 0)
184 goto err_unlock;
0eb4c6c2
AC
185
186 desc->flags |= gpio_flags;
2ec74a95 187
0eb4c6c2
AC
188 return 0;
189
52176d0d
JH
190err_unlock:
191 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
2ec74a95
JH
192err_put_kn:
193 sysfs_put(data->value_kn);
194
0eb4c6c2
AC
195 return ret;
196}
197
6ffcb797
JH
198/*
199 * Caller holds gpiod-data mutex (unless called after class-device
200 * deregistration).
201 */
2ec74a95
JH
202static void gpio_sysfs_free_irq(struct device *dev)
203{
204 struct gpiod_data *data = dev_get_drvdata(dev);
205 struct gpio_desc *desc = data->desc;
206
207 desc->flags &= ~GPIO_TRIGGER_MASK;
208 free_irq(data->irq, data);
209 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
210 sysfs_put(data->value_kn);
211}
212
0eb4c6c2
AC
213static const struct {
214 const char *name;
215 unsigned long flags;
216} trigger_types[] = {
217 { "none", 0 },
218 { "falling", BIT(FLAG_TRIG_FALL) },
219 { "rising", BIT(FLAG_TRIG_RISE) },
220 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
221};
222
6beac9d1 223static ssize_t edge_show(struct device *dev,
0eb4c6c2
AC
224 struct device_attribute *attr, char *buf)
225{
c43960fb
JH
226 struct gpiod_data *data = dev_get_drvdata(dev);
227 struct gpio_desc *desc = data->desc;
f0b7866a
JH
228 unsigned long mask;
229 ssize_t status = 0;
230 int i;
0eb4c6c2 231
6ffcb797 232 mutex_lock(&data->mutex);
0eb4c6c2 233
f0b7866a
JH
234 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
235 mask = desc->flags & GPIO_TRIGGER_MASK;
236 if (mask == trigger_types[i].flags) {
237 status = sprintf(buf, "%s\n", trigger_types[i].name);
238 break;
239 }
0eb4c6c2
AC
240 }
241
6ffcb797
JH
242 mutex_unlock(&data->mutex);
243
0eb4c6c2
AC
244 return status;
245}
246
6beac9d1 247static ssize_t edge_store(struct device *dev,
0eb4c6c2
AC
248 struct device_attribute *attr, const char *buf, size_t size)
249{
b91e1807
JH
250 struct gpiod_data *data = dev_get_drvdata(dev);
251 struct gpio_desc *desc = data->desc;
252 unsigned long flags;
2ec74a95 253 ssize_t status = size;
e4339ce3 254 int i;
0eb4c6c2 255
e4339ce3 256 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
0eb4c6c2 257 if (sysfs_streq(trigger_types[i].name, buf))
e4339ce3
JH
258 break;
259 }
260
261 if (i == ARRAY_SIZE(trigger_types))
262 return -EINVAL;
0eb4c6c2 263
b91e1807
JH
264 flags = trigger_types[i].flags;
265
6ffcb797 266 mutex_lock(&data->mutex);
0eb4c6c2 267
b91e1807
JH
268 if ((desc->flags & GPIO_TRIGGER_MASK) == flags) {
269 status = size;
270 goto out_unlock;
271 }
272
2ec74a95
JH
273 if (desc->flags & GPIO_TRIGGER_MASK)
274 gpio_sysfs_free_irq(dev);
275
276 if (flags) {
277 status = gpio_sysfs_request_irq(dev, flags);
278 if (!status)
279 status = size;
280 }
0eb4c6c2 281
b91e1807 282out_unlock:
6ffcb797 283 mutex_unlock(&data->mutex);
0eb4c6c2
AC
284
285 return status;
286}
6beac9d1 287static DEVICE_ATTR_RW(edge);
0eb4c6c2 288
6ffcb797 289/* Caller holds gpiod-data mutex. */
2f323b85 290static int gpio_sysfs_set_active_low(struct device *dev, int value)
0eb4c6c2 291{
0f628508
JH
292 struct gpiod_data *data = dev_get_drvdata(dev);
293 struct gpio_desc *desc = data->desc;
0eb4c6c2
AC
294 int status = 0;
295
296 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
297 return 0;
298
299 if (value)
300 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
301 else
302 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
303
304 /* reconfigure poll(2) support if enabled on one edge only */
166a85e4
JH
305 if (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
306 !!test_bit(FLAG_TRIG_FALL, &desc->flags)) {
0eb4c6c2
AC
307 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
308
2ec74a95
JH
309 gpio_sysfs_free_irq(dev);
310 status = gpio_sysfs_request_irq(dev, trigger_flags);
0eb4c6c2
AC
311 }
312
313 return status;
314}
315
6beac9d1 316static ssize_t active_low_show(struct device *dev,
0eb4c6c2
AC
317 struct device_attribute *attr, char *buf)
318{
c43960fb
JH
319 struct gpiod_data *data = dev_get_drvdata(dev);
320 struct gpio_desc *desc = data->desc;
0eb4c6c2
AC
321 ssize_t status;
322
6ffcb797 323 mutex_lock(&data->mutex);
0eb4c6c2 324
f0b7866a 325 status = sprintf(buf, "%d\n",
0eb4c6c2
AC
326 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
327
6ffcb797 328 mutex_unlock(&data->mutex);
0eb4c6c2
AC
329
330 return status;
331}
332
6beac9d1 333static ssize_t active_low_store(struct device *dev,
0eb4c6c2
AC
334 struct device_attribute *attr, const char *buf, size_t size)
335{
6ffcb797 336 struct gpiod_data *data = dev_get_drvdata(dev);
0eb4c6c2 337 ssize_t status;
f0b7866a 338 long value;
0eb4c6c2 339
6ffcb797 340 mutex_lock(&data->mutex);
0eb4c6c2 341
f0b7866a
JH
342 status = kstrtol(buf, 0, &value);
343 if (status == 0)
2f323b85 344 status = gpio_sysfs_set_active_low(dev, value);
0eb4c6c2 345
6ffcb797 346 mutex_unlock(&data->mutex);
0eb4c6c2
AC
347
348 return status ? : size;
349}
6beac9d1 350static DEVICE_ATTR_RW(active_low);
0eb4c6c2 351
ebbeba12
JH
352static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
353 int n)
354{
355 struct device *dev = container_of(kobj, struct device, kobj);
c43960fb
JH
356 struct gpiod_data *data = dev_get_drvdata(dev);
357 struct gpio_desc *desc = data->desc;
ebbeba12 358 umode_t mode = attr->mode;
427fdeef 359 bool show_direction = data->direction_can_change;
ebbeba12
JH
360
361 if (attr == &dev_attr_direction.attr) {
362 if (!show_direction)
363 mode = 0;
364 } else if (attr == &dev_attr_edge.attr) {
365 if (gpiod_to_irq(desc) < 0)
366 mode = 0;
367 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
368 mode = 0;
369 }
370
371 return mode;
372}
373
0915e6fe 374static struct attribute *gpio_attrs[] = {
ebbeba12
JH
375 &dev_attr_direction.attr,
376 &dev_attr_edge.attr,
0eb4c6c2
AC
377 &dev_attr_value.attr,
378 &dev_attr_active_low.attr,
379 NULL,
380};
ebbeba12
JH
381
382static const struct attribute_group gpio_group = {
383 .attrs = gpio_attrs,
384 .is_visible = gpio_is_visible,
385};
386
387static const struct attribute_group *gpio_groups[] = {
388 &gpio_group,
389 NULL
390};
0eb4c6c2
AC
391
392/*
393 * /sys/class/gpio/gpiochipN/
394 * /base ... matching gpio_chip.base (N)
395 * /label ... matching gpio_chip.label
396 * /ngpio ... matching gpio_chip.ngpio
397 */
398
6beac9d1 399static ssize_t base_show(struct device *dev,
0eb4c6c2
AC
400 struct device_attribute *attr, char *buf)
401{
402 const struct gpio_chip *chip = dev_get_drvdata(dev);
403
404 return sprintf(buf, "%d\n", chip->base);
405}
6beac9d1 406static DEVICE_ATTR_RO(base);
0eb4c6c2 407
6beac9d1 408static ssize_t label_show(struct device *dev,
0eb4c6c2
AC
409 struct device_attribute *attr, char *buf)
410{
411 const struct gpio_chip *chip = dev_get_drvdata(dev);
412
413 return sprintf(buf, "%s\n", chip->label ? : "");
414}
6beac9d1 415static DEVICE_ATTR_RO(label);
0eb4c6c2 416
6beac9d1 417static ssize_t ngpio_show(struct device *dev,
0eb4c6c2
AC
418 struct device_attribute *attr, char *buf)
419{
420 const struct gpio_chip *chip = dev_get_drvdata(dev);
421
422 return sprintf(buf, "%u\n", chip->ngpio);
423}
6beac9d1 424static DEVICE_ATTR_RO(ngpio);
0eb4c6c2 425
121b6a79 426static struct attribute *gpiochip_attrs[] = {
0eb4c6c2
AC
427 &dev_attr_base.attr,
428 &dev_attr_label.attr,
429 &dev_attr_ngpio.attr,
430 NULL,
431};
121b6a79 432ATTRIBUTE_GROUPS(gpiochip);
0eb4c6c2
AC
433
434/*
435 * /sys/class/gpio/export ... write-only
436 * integer N ... number of GPIO to export (full access)
437 * /sys/class/gpio/unexport ... write-only
438 * integer N ... number of GPIO to unexport
439 */
440static ssize_t export_store(struct class *class,
441 struct class_attribute *attr,
442 const char *buf, size_t len)
443{
444 long gpio;
445 struct gpio_desc *desc;
446 int status;
447
448 status = kstrtol(buf, 0, &gpio);
449 if (status < 0)
450 goto done;
451
452 desc = gpio_to_desc(gpio);
453 /* reject invalid GPIOs */
454 if (!desc) {
455 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
456 return -EINVAL;
457 }
458
459 /* No extra locking here; FLAG_SYSFS just signifies that the
460 * request and export were done by on behalf of userspace, so
461 * they may be undone on its behalf too.
462 */
463
464 status = gpiod_request(desc, "sysfs");
465 if (status < 0) {
466 if (status == -EPROBE_DEFER)
467 status = -ENODEV;
468 goto done;
469 }
470 status = gpiod_export(desc, true);
471 if (status < 0)
472 gpiod_free(desc);
473 else
474 set_bit(FLAG_SYSFS, &desc->flags);
475
476done:
477 if (status)
478 pr_debug("%s: status %d\n", __func__, status);
479 return status ? : len;
480}
481
482static ssize_t unexport_store(struct class *class,
483 struct class_attribute *attr,
484 const char *buf, size_t len)
485{
486 long gpio;
487 struct gpio_desc *desc;
488 int status;
489
490 status = kstrtol(buf, 0, &gpio);
491 if (status < 0)
492 goto done;
493
494 desc = gpio_to_desc(gpio);
495 /* reject bogus commands (gpio_unexport ignores them) */
496 if (!desc) {
497 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
498 return -EINVAL;
499 }
500
501 status = -EINVAL;
502
503 /* No extra locking here; FLAG_SYSFS just signifies that the
504 * request and export were done by on behalf of userspace, so
505 * they may be undone on its behalf too.
506 */
507 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
508 status = 0;
509 gpiod_free(desc);
510 }
511done:
512 if (status)
513 pr_debug("%s: status %d\n", __func__, status);
514 return status ? : len;
515}
516
517static struct class_attribute gpio_class_attrs[] = {
518 __ATTR(export, 0200, NULL, export_store),
519 __ATTR(unexport, 0200, NULL, unexport_store),
520 __ATTR_NULL,
521};
522
523static struct class gpio_class = {
524 .name = "gpio",
525 .owner = THIS_MODULE,
526
527 .class_attrs = gpio_class_attrs,
528};
529
530
531/**
532 * gpiod_export - export a GPIO through sysfs
533 * @gpio: gpio to make available, already requested
534 * @direction_may_change: true if userspace may change gpio direction
535 * Context: arch_initcall or later
536 *
537 * When drivers want to make a GPIO accessible to userspace after they
538 * have requested it -- perhaps while debugging, or as part of their
539 * public interface -- they may use this routine. If the GPIO can
540 * change direction (some can't) and the caller allows it, userspace
541 * will see "direction" sysfs attribute which may be used to change
542 * the gpio's direction. A "value" attribute will always be provided.
543 *
544 * Returns zero on success, else an error.
545 */
546int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
547{
483d8211 548 struct gpio_chip *chip;
c43960fb 549 struct gpiod_data *data;
0eb4c6c2
AC
550 unsigned long flags;
551 int status;
552 const char *ioname = NULL;
553 struct device *dev;
554 int offset;
555
556 /* can't export until sysfs is available ... */
557 if (!gpio_class.p) {
558 pr_debug("%s: called too early!\n", __func__);
559 return -ENOENT;
560 }
561
562 if (!desc) {
563 pr_debug("%s: invalid gpio descriptor\n", __func__);
564 return -EINVAL;
565 }
566
483d8211
JH
567 chip = desc->chip;
568
0eb4c6c2
AC
569 mutex_lock(&sysfs_lock);
570
483d8211 571 /* check if chip is being removed */
6a4b6b0a 572 if (!chip || !chip->cdev) {
483d8211 573 status = -ENODEV;
c43960fb 574 goto err_unlock;
483d8211
JH
575 }
576
0eb4c6c2
AC
577 spin_lock_irqsave(&gpio_lock, flags);
578 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
579 test_bit(FLAG_EXPORT, &desc->flags)) {
580 spin_unlock_irqrestore(&gpio_lock, flags);
581 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
582 __func__,
583 test_bit(FLAG_REQUESTED, &desc->flags),
584 test_bit(FLAG_EXPORT, &desc->flags));
585 status = -EPERM;
c43960fb 586 goto err_unlock;
0eb4c6c2 587 }
0eb4c6c2
AC
588 spin_unlock_irqrestore(&gpio_lock, flags);
589
c43960fb
JH
590 data = kzalloc(sizeof(*data), GFP_KERNEL);
591 if (!data) {
592 status = -ENOMEM;
593 goto err_unlock;
594 }
595
596 data->desc = desc;
6ffcb797 597 mutex_init(&data->mutex);
427fdeef
JH
598 if (chip->direction_input && chip->direction_output)
599 data->direction_can_change = direction_may_change;
600 else
601 data->direction_can_change = false;
c43960fb 602
0eb4c6c2 603 offset = gpio_chip_hwgpio(desc);
cecf58ab
JH
604 if (chip->names && chip->names[offset])
605 ioname = chip->names[offset];
0eb4c6c2 606
cecf58ab 607 dev = device_create_with_groups(&gpio_class, chip->dev,
c43960fb 608 MKDEV(0, 0), data, gpio_groups,
0915e6fe
JH
609 ioname ? ioname : "gpio%u",
610 desc_to_gpio(desc));
0eb4c6c2
AC
611 if (IS_ERR(dev)) {
612 status = PTR_ERR(dev);
c43960fb 613 goto err_free_data;
0eb4c6c2
AC
614 }
615
0eb4c6c2
AC
616 set_bit(FLAG_EXPORT, &desc->flags);
617 mutex_unlock(&sysfs_lock);
618 return 0;
619
c43960fb
JH
620err_free_data:
621 kfree(data);
622err_unlock:
0eb4c6c2
AC
623 mutex_unlock(&sysfs_lock);
624 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
625 return status;
626}
627EXPORT_SYMBOL_GPL(gpiod_export);
628
c43960fb 629static int match_export(struct device *dev, const void *desc)
0eb4c6c2 630{
c43960fb
JH
631 struct gpiod_data *data = dev_get_drvdata(dev);
632
633 return data->desc == desc;
0eb4c6c2
AC
634}
635
636/**
637 * gpiod_export_link - create a sysfs link to an exported GPIO node
638 * @dev: device under which to create symlink
639 * @name: name of the symlink
640 * @gpio: gpio to create symlink to, already exported
641 *
642 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
643 * node. Caller is responsible for unlinking.
644 *
645 * Returns zero on success, else an error.
646 */
647int gpiod_export_link(struct device *dev, const char *name,
648 struct gpio_desc *desc)
649{
56d30ec1
JH
650 struct device *cdev;
651 int ret;
0eb4c6c2
AC
652
653 if (!desc) {
654 pr_warn("%s: invalid GPIO\n", __func__);
655 return -EINVAL;
656 }
657
56d30ec1
JH
658 cdev = class_find_device(&gpio_class, NULL, desc, match_export);
659 if (!cdev)
660 return -ENODEV;
0eb4c6c2 661
56d30ec1
JH
662 ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
663 put_device(cdev);
0eb4c6c2 664
56d30ec1 665 return ret;
0eb4c6c2
AC
666}
667EXPORT_SYMBOL_GPL(gpiod_export_link);
668
0eb4c6c2
AC
669/**
670 * gpiod_unexport - reverse effect of gpio_export()
671 * @gpio: gpio to make unavailable
672 *
673 * This is implicit on gpio_free().
674 */
675void gpiod_unexport(struct gpio_desc *desc)
676{
72eba6f6
JH
677 struct gpiod_data *data;
678 struct device *dev;
0eb4c6c2
AC
679
680 if (!desc) {
681 pr_warn("%s: invalid GPIO\n", __func__);
682 return;
683 }
684
685 mutex_lock(&sysfs_lock);
686
72eba6f6
JH
687 if (!test_bit(FLAG_EXPORT, &desc->flags))
688 goto err_unlock;
0eb4c6c2 689
72eba6f6
JH
690 dev = class_find_device(&gpio_class, NULL, desc, match_export);
691 if (!dev)
692 goto err_unlock;
693
694 data = dev_get_drvdata(dev);
695
72eba6f6
JH
696 clear_bit(FLAG_EXPORT, &desc->flags);
697
698 device_unregister(dev);
699
700 /*
701 * Release irq after deregistration to prevent race with edge_store.
702 */
703 if (desc->flags & GPIO_TRIGGER_MASK)
704 gpio_sysfs_free_irq(dev);
0eb4c6c2
AC
705
706 mutex_unlock(&sysfs_lock);
707
72eba6f6
JH
708 put_device(dev);
709 kfree(data);
0eb4c6c2 710
72eba6f6
JH
711 return;
712
713err_unlock:
714 mutex_unlock(&sysfs_lock);
0eb4c6c2
AC
715}
716EXPORT_SYMBOL_GPL(gpiod_unexport);
717
426577bd 718int gpiochip_sysfs_register(struct gpio_chip *chip)
0eb4c6c2 719{
0eb4c6c2
AC
720 struct device *dev;
721
426577bd
JH
722 /*
723 * Many systems add gpio chips for SOC support very early,
0eb4c6c2 724 * before driver model support is available. In those cases we
426577bd 725 * register later, in gpiolib_sysfs_init() ... here we just
0eb4c6c2
AC
726 * verify that _some_ field of gpio_class got initialized.
727 */
728 if (!gpio_class.p)
729 return 0;
730
731 /* use chip->base for the ID; it's already known to be unique */
121b6a79
JH
732 dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0),
733 chip, gpiochip_groups,
734 "gpiochip%d", chip->base);
735 if (IS_ERR(dev))
6a4b6b0a 736 return PTR_ERR(dev);
3ff74be5
JH
737
738 mutex_lock(&sysfs_lock);
6a4b6b0a 739 chip->cdev = dev;
0eb4c6c2
AC
740 mutex_unlock(&sysfs_lock);
741
6a4b6b0a 742 return 0;
0eb4c6c2
AC
743}
744
426577bd 745void gpiochip_sysfs_unregister(struct gpio_chip *chip)
0eb4c6c2 746{
483d8211
JH
747 struct gpio_desc *desc;
748 unsigned int i;
0eb4c6c2 749
6a4b6b0a
JH
750 if (!chip->cdev)
751 return;
0eb4c6c2 752
6a4b6b0a
JH
753 device_unregister(chip->cdev);
754
755 /* prevent further gpiod exports */
756 mutex_lock(&sysfs_lock);
757 chip->cdev = NULL;
758 mutex_unlock(&sysfs_lock);
483d8211
JH
759
760 /* unregister gpiod class devices owned by sysfs */
761 for (i = 0; i < chip->ngpio; i++) {
762 desc = &chip->desc[i];
763 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
764 gpiod_free(desc);
765 }
0eb4c6c2
AC
766}
767
768static int __init gpiolib_sysfs_init(void)
769{
770 int status;
771 unsigned long flags;
772 struct gpio_chip *chip;
773
774 status = class_register(&gpio_class);
775 if (status < 0)
776 return status;
777
778 /* Scan and register the gpio_chips which registered very
779 * early (e.g. before the class_register above was called).
780 *
781 * We run before arch_initcall() so chip->dev nodes can have
782 * registered, and so arch_initcall() can always gpio_export().
783 */
784 spin_lock_irqsave(&gpio_lock, flags);
785 list_for_each_entry(chip, &gpio_chips, list) {
6a4b6b0a 786 if (chip->cdev)
0eb4c6c2
AC
787 continue;
788
14141a93 789 /*
426577bd
JH
790 * TODO we yield gpio_lock here because
791 * gpiochip_sysfs_register() acquires a mutex. This is unsafe
792 * and needs to be fixed.
14141a93
AC
793 *
794 * Also it would be nice to use gpiochip_find() here so we
795 * can keep gpio_chips local to gpiolib.c, but the yield of
796 * gpio_lock prevents us from doing this.
797 */
0eb4c6c2 798 spin_unlock_irqrestore(&gpio_lock, flags);
426577bd 799 status = gpiochip_sysfs_register(chip);
0eb4c6c2
AC
800 spin_lock_irqsave(&gpio_lock, flags);
801 }
802 spin_unlock_irqrestore(&gpio_lock, flags);
803
804
805 return status;
806}
807postcore_initcall(gpiolib_sysfs_init);
This page took 0.135953 seconds and 5 git commands to generate.