blob: 097e7e539c9d2a42f5a609428330b4f93404d72b [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>
Johan Hovoldc43960f2015-05-04 17:10:37 +02009#include <linux/slab.h>
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090010
11#include "gpiolib.h"
12
Johan Hovoldc43960f2015-05-04 17:10:37 +020013struct gpiod_data {
14 struct gpio_desc *desc;
15};
16
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090017static DEFINE_IDR(dirent_idr);
18
19
20/* lock protects against unexport_gpio() being called while
21 * sysfs files are active.
22 */
23static DEFINE_MUTEX(sysfs_lock);
24
25/*
26 * /sys/class/gpio/gpioN... only for GPIOs that are exported
27 * /direction
28 * * MAY BE OMITTED if kernel won't allow direction changes
29 * * is read/write as "in" or "out"
30 * * may also be written as "high" or "low", initializing
31 * output value as specified ("out" implies "low")
32 * /value
33 * * always readable, subject to hardware behavior
34 * * may be writable, as zero/nonzero
35 * /edge
36 * * configures behavior of poll(2) on /value
37 * * available only if pin can generate IRQs on input
38 * * is read/write as "none", "falling", "rising", or "both"
39 * /active_low
40 * * configures polarity of /value
41 * * is read/write as zero/nonzero
42 * * also affects existing and subsequent "falling" and "rising"
43 * /edge configuration
44 */
45
Johan Hovold6beac9d2015-05-04 17:10:34 +020046static ssize_t direction_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090047 struct device_attribute *attr, char *buf)
48{
Johan Hovoldc43960f2015-05-04 17:10:37 +020049 struct gpiod_data *data = dev_get_drvdata(dev);
50 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090051 ssize_t status;
52
53 mutex_lock(&sysfs_lock);
54
Johan Hovoldf0b78662015-05-04 17:10:36 +020055 gpiod_get_direction(desc);
56 status = sprintf(buf, "%s\n",
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090057 test_bit(FLAG_IS_OUT, &desc->flags)
58 ? "out" : "in");
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090059
60 mutex_unlock(&sysfs_lock);
61 return status;
62}
63
Johan Hovold6beac9d2015-05-04 17:10:34 +020064static ssize_t direction_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090065 struct device_attribute *attr, const char *buf, size_t size)
66{
Johan Hovoldc43960f2015-05-04 17:10:37 +020067 struct gpiod_data *data = dev_get_drvdata(dev);
68 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090069 ssize_t status;
70
71 mutex_lock(&sysfs_lock);
72
Johan Hovoldf0b78662015-05-04 17:10:36 +020073 if (sysfs_streq(buf, "high"))
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090074 status = gpiod_direction_output_raw(desc, 1);
75 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
76 status = gpiod_direction_output_raw(desc, 0);
77 else if (sysfs_streq(buf, "in"))
78 status = gpiod_direction_input(desc);
79 else
80 status = -EINVAL;
81
82 mutex_unlock(&sysfs_lock);
83 return status ? : size;
84}
Johan Hovold6beac9d2015-05-04 17:10:34 +020085static DEVICE_ATTR_RW(direction);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090086
Johan Hovold6beac9d2015-05-04 17:10:34 +020087static ssize_t value_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090088 struct device_attribute *attr, char *buf)
89{
Johan Hovoldc43960f2015-05-04 17:10:37 +020090 struct gpiod_data *data = dev_get_drvdata(dev);
91 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090092 ssize_t status;
93
94 mutex_lock(&sysfs_lock);
95
Johan Hovoldf0b78662015-05-04 17:10:36 +020096 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090097
98 mutex_unlock(&sysfs_lock);
99 return status;
100}
101
Johan Hovold6beac9d2015-05-04 17:10:34 +0200102static ssize_t value_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900103 struct device_attribute *attr, const char *buf, size_t size)
104{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200105 struct gpiod_data *data = dev_get_drvdata(dev);
106 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900107 ssize_t status;
108
109 mutex_lock(&sysfs_lock);
110
Johan Hovoldf0b78662015-05-04 17:10:36 +0200111 if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900112 status = -EPERM;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200113 } else {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900114 long value;
115
116 status = kstrtol(buf, 0, &value);
117 if (status == 0) {
118 gpiod_set_value_cansleep(desc, value);
119 status = size;
120 }
121 }
122
123 mutex_unlock(&sysfs_lock);
124 return status;
125}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200126static DEVICE_ATTR_RW(value);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900127
128static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
129{
130 struct kernfs_node *value_sd = priv;
131
132 sysfs_notify_dirent(value_sd);
133 return IRQ_HANDLED;
134}
135
136static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
137 unsigned long gpio_flags)
138{
139 struct kernfs_node *value_sd;
140 unsigned long irq_flags;
141 int ret, irq, id;
142
143 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
144 return 0;
145
146 irq = gpiod_to_irq(desc);
147 if (irq < 0)
148 return -EIO;
149
150 id = desc->flags >> ID_SHIFT;
151 value_sd = idr_find(&dirent_idr, id);
152 if (value_sd)
153 free_irq(irq, value_sd);
154
155 desc->flags &= ~GPIO_TRIGGER_MASK;
156
157 if (!gpio_flags) {
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900158 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900159 ret = 0;
160 goto free_id;
161 }
162
163 irq_flags = IRQF_SHARED;
164 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
165 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
166 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
167 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
168 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
169 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
170
171 if (!value_sd) {
172 value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
173 if (!value_sd) {
174 ret = -ENODEV;
175 goto err_out;
176 }
177
178 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
179 if (ret < 0)
180 goto free_sd;
181 id = ret;
182
183 desc->flags &= GPIO_FLAGS_MASK;
184 desc->flags |= (unsigned long)id << ID_SHIFT;
185
186 if (desc->flags >> ID_SHIFT != id) {
187 ret = -ERANGE;
188 goto free_id;
189 }
190 }
191
Johan Hovold52176d02015-05-04 17:10:28 +0200192 /*
193 * FIXME: This should be done in the irq_request_resources callback
194 * when the irq is requested, but a few drivers currently fail
195 * to do so.
196 *
197 * Remove this redundant call (along with the corresponding
198 * unlock) when those drivers have been fixed.
199 */
200 ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900201 if (ret < 0)
202 goto free_id;
203
Johan Hovold52176d02015-05-04 17:10:28 +0200204 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
205 "gpiolib", value_sd);
206 if (ret < 0)
207 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900208
209 desc->flags |= gpio_flags;
210 return 0;
211
Johan Hovold52176d02015-05-04 17:10:28 +0200212err_unlock:
213 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900214free_id:
215 idr_remove(&dirent_idr, id);
216 desc->flags &= GPIO_FLAGS_MASK;
217free_sd:
218 if (value_sd)
219 sysfs_put(value_sd);
220err_out:
221 return ret;
222}
223
224static const struct {
225 const char *name;
226 unsigned long flags;
227} trigger_types[] = {
228 { "none", 0 },
229 { "falling", BIT(FLAG_TRIG_FALL) },
230 { "rising", BIT(FLAG_TRIG_RISE) },
231 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
232};
233
Johan Hovold6beac9d2015-05-04 17:10:34 +0200234static ssize_t edge_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900235 struct device_attribute *attr, char *buf)
236{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200237 struct gpiod_data *data = dev_get_drvdata(dev);
238 struct gpio_desc *desc = data->desc;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200239 unsigned long mask;
240 ssize_t status = 0;
241 int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900242
243 mutex_lock(&sysfs_lock);
244
Johan Hovoldf0b78662015-05-04 17:10:36 +0200245 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
246 mask = desc->flags & GPIO_TRIGGER_MASK;
247 if (mask == trigger_types[i].flags) {
248 status = sprintf(buf, "%s\n", trigger_types[i].name);
249 break;
250 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900251 }
252
253 mutex_unlock(&sysfs_lock);
254 return status;
255}
256
Johan Hovold6beac9d2015-05-04 17:10:34 +0200257static ssize_t edge_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900258 struct device_attribute *attr, const char *buf, size_t size)
259{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200260 struct gpiod_data *data = dev_get_drvdata(dev);
261 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900262 ssize_t status;
263 int i;
264
265 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
266 if (sysfs_streq(trigger_types[i].name, buf))
267 goto found;
268 return -EINVAL;
269
270found:
271 mutex_lock(&sysfs_lock);
272
Johan Hovoldf0b78662015-05-04 17:10:36 +0200273 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
274 if (!status)
275 status = size;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900276
277 mutex_unlock(&sysfs_lock);
278
279 return status;
280}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200281static DEVICE_ATTR_RW(edge);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900282
283static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
284 int value)
285{
286 int status = 0;
287
288 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
289 return 0;
290
291 if (value)
292 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
293 else
294 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
295
296 /* reconfigure poll(2) support if enabled on one edge only */
Johan Hovold166a85e2015-05-04 17:10:33 +0200297 if (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
298 !!test_bit(FLAG_TRIG_FALL, &desc->flags)) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900299 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
300
301 gpio_setup_irq(desc, dev, 0);
302 status = gpio_setup_irq(desc, dev, trigger_flags);
303 }
304
305 return status;
306}
307
Johan Hovold6beac9d2015-05-04 17:10:34 +0200308static ssize_t active_low_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900309 struct device_attribute *attr, char *buf)
310{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200311 struct gpiod_data *data = dev_get_drvdata(dev);
312 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900313 ssize_t status;
314
315 mutex_lock(&sysfs_lock);
316
Johan Hovoldf0b78662015-05-04 17:10:36 +0200317 status = sprintf(buf, "%d\n",
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900318 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
319
320 mutex_unlock(&sysfs_lock);
321
322 return status;
323}
324
Johan Hovold6beac9d2015-05-04 17:10:34 +0200325static ssize_t active_low_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900326 struct device_attribute *attr, const char *buf, size_t size)
327{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200328 struct gpiod_data *data = dev_get_drvdata(dev);
329 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900330 ssize_t status;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200331 long value;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900332
333 mutex_lock(&sysfs_lock);
334
Johan Hovoldf0b78662015-05-04 17:10:36 +0200335 status = kstrtol(buf, 0, &value);
336 if (status == 0)
337 status = sysfs_set_active_low(desc, dev, value != 0);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900338
339 mutex_unlock(&sysfs_lock);
340
341 return status ? : size;
342}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200343static DEVICE_ATTR_RW(active_low);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900344
Johan Hovoldebbeba12015-01-13 13:00:06 +0100345static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
346 int n)
347{
348 struct device *dev = container_of(kobj, struct device, kobj);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200349 struct gpiod_data *data = dev_get_drvdata(dev);
350 struct gpio_desc *desc = data->desc;
Johan Hovoldebbeba12015-01-13 13:00:06 +0100351 umode_t mode = attr->mode;
352 bool show_direction = test_bit(FLAG_SYSFS_DIR, &desc->flags);
353
354 if (attr == &dev_attr_direction.attr) {
355 if (!show_direction)
356 mode = 0;
357 } else if (attr == &dev_attr_edge.attr) {
358 if (gpiod_to_irq(desc) < 0)
359 mode = 0;
360 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
361 mode = 0;
362 }
363
364 return mode;
365}
366
Johan Hovold0915e6f2015-01-13 13:00:05 +0100367static struct attribute *gpio_attrs[] = {
Johan Hovoldebbeba12015-01-13 13:00:06 +0100368 &dev_attr_direction.attr,
369 &dev_attr_edge.attr,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900370 &dev_attr_value.attr,
371 &dev_attr_active_low.attr,
372 NULL,
373};
Johan Hovoldebbeba12015-01-13 13:00:06 +0100374
375static const struct attribute_group gpio_group = {
376 .attrs = gpio_attrs,
377 .is_visible = gpio_is_visible,
378};
379
380static const struct attribute_group *gpio_groups[] = {
381 &gpio_group,
382 NULL
383};
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900384
385/*
386 * /sys/class/gpio/gpiochipN/
387 * /base ... matching gpio_chip.base (N)
388 * /label ... matching gpio_chip.label
389 * /ngpio ... matching gpio_chip.ngpio
390 */
391
Johan Hovold6beac9d2015-05-04 17:10:34 +0200392static ssize_t base_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900393 struct device_attribute *attr, char *buf)
394{
395 const struct gpio_chip *chip = dev_get_drvdata(dev);
396
397 return sprintf(buf, "%d\n", chip->base);
398}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200399static DEVICE_ATTR_RO(base);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900400
Johan Hovold6beac9d2015-05-04 17:10:34 +0200401static ssize_t label_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900402 struct device_attribute *attr, char *buf)
403{
404 const struct gpio_chip *chip = dev_get_drvdata(dev);
405
406 return sprintf(buf, "%s\n", chip->label ? : "");
407}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200408static DEVICE_ATTR_RO(label);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900409
Johan Hovold6beac9d2015-05-04 17:10:34 +0200410static ssize_t ngpio_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900411 struct device_attribute *attr, char *buf)
412{
413 const struct gpio_chip *chip = dev_get_drvdata(dev);
414
415 return sprintf(buf, "%u\n", chip->ngpio);
416}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200417static DEVICE_ATTR_RO(ngpio);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900418
Johan Hovold121b6a72015-01-13 13:00:04 +0100419static struct attribute *gpiochip_attrs[] = {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900420 &dev_attr_base.attr,
421 &dev_attr_label.attr,
422 &dev_attr_ngpio.attr,
423 NULL,
424};
Johan Hovold121b6a72015-01-13 13:00:04 +0100425ATTRIBUTE_GROUPS(gpiochip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900426
427/*
428 * /sys/class/gpio/export ... write-only
429 * integer N ... number of GPIO to export (full access)
430 * /sys/class/gpio/unexport ... write-only
431 * integer N ... number of GPIO to unexport
432 */
433static ssize_t export_store(struct class *class,
434 struct class_attribute *attr,
435 const char *buf, size_t len)
436{
437 long gpio;
438 struct gpio_desc *desc;
439 int status;
440
441 status = kstrtol(buf, 0, &gpio);
442 if (status < 0)
443 goto done;
444
445 desc = gpio_to_desc(gpio);
446 /* reject invalid GPIOs */
447 if (!desc) {
448 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
449 return -EINVAL;
450 }
451
452 /* No extra locking here; FLAG_SYSFS just signifies that the
453 * request and export were done by on behalf of userspace, so
454 * they may be undone on its behalf too.
455 */
456
457 status = gpiod_request(desc, "sysfs");
458 if (status < 0) {
459 if (status == -EPROBE_DEFER)
460 status = -ENODEV;
461 goto done;
462 }
463 status = gpiod_export(desc, true);
464 if (status < 0)
465 gpiod_free(desc);
466 else
467 set_bit(FLAG_SYSFS, &desc->flags);
468
469done:
470 if (status)
471 pr_debug("%s: status %d\n", __func__, status);
472 return status ? : len;
473}
474
475static ssize_t unexport_store(struct class *class,
476 struct class_attribute *attr,
477 const char *buf, size_t len)
478{
479 long gpio;
480 struct gpio_desc *desc;
481 int status;
482
483 status = kstrtol(buf, 0, &gpio);
484 if (status < 0)
485 goto done;
486
487 desc = gpio_to_desc(gpio);
488 /* reject bogus commands (gpio_unexport ignores them) */
489 if (!desc) {
490 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
491 return -EINVAL;
492 }
493
494 status = -EINVAL;
495
496 /* No extra locking here; FLAG_SYSFS just signifies that the
497 * request and export were done by on behalf of userspace, so
498 * they may be undone on its behalf too.
499 */
500 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
501 status = 0;
502 gpiod_free(desc);
503 }
504done:
505 if (status)
506 pr_debug("%s: status %d\n", __func__, status);
507 return status ? : len;
508}
509
510static struct class_attribute gpio_class_attrs[] = {
511 __ATTR(export, 0200, NULL, export_store),
512 __ATTR(unexport, 0200, NULL, unexport_store),
513 __ATTR_NULL,
514};
515
516static struct class gpio_class = {
517 .name = "gpio",
518 .owner = THIS_MODULE,
519
520 .class_attrs = gpio_class_attrs,
521};
522
523
524/**
525 * gpiod_export - export a GPIO through sysfs
526 * @gpio: gpio to make available, already requested
527 * @direction_may_change: true if userspace may change gpio direction
528 * Context: arch_initcall or later
529 *
530 * When drivers want to make a GPIO accessible to userspace after they
531 * have requested it -- perhaps while debugging, or as part of their
532 * public interface -- they may use this routine. If the GPIO can
533 * change direction (some can't) and the caller allows it, userspace
534 * will see "direction" sysfs attribute which may be used to change
535 * the gpio's direction. A "value" attribute will always be provided.
536 *
537 * Returns zero on success, else an error.
538 */
539int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
540{
Johan Hovold483d8212015-04-21 17:42:09 +0200541 struct gpio_chip *chip;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200542 struct gpiod_data *data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900543 unsigned long flags;
544 int status;
545 const char *ioname = NULL;
546 struct device *dev;
547 int offset;
548
549 /* can't export until sysfs is available ... */
550 if (!gpio_class.p) {
551 pr_debug("%s: called too early!\n", __func__);
552 return -ENOENT;
553 }
554
555 if (!desc) {
556 pr_debug("%s: invalid gpio descriptor\n", __func__);
557 return -EINVAL;
558 }
559
Johan Hovold483d8212015-04-21 17:42:09 +0200560 chip = desc->chip;
561
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900562 mutex_lock(&sysfs_lock);
563
Johan Hovold483d8212015-04-21 17:42:09 +0200564 /* check if chip is being removed */
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200565 if (!chip || !chip->cdev) {
Johan Hovold483d8212015-04-21 17:42:09 +0200566 status = -ENODEV;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200567 goto err_unlock;
Johan Hovold483d8212015-04-21 17:42:09 +0200568 }
569
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900570 spin_lock_irqsave(&gpio_lock, flags);
571 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
572 test_bit(FLAG_EXPORT, &desc->flags)) {
573 spin_unlock_irqrestore(&gpio_lock, flags);
574 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
575 __func__,
576 test_bit(FLAG_REQUESTED, &desc->flags),
577 test_bit(FLAG_EXPORT, &desc->flags));
578 status = -EPERM;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200579 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900580 }
581
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200582 if (chip->direction_input && chip->direction_output &&
Johan Hovoldebbeba12015-01-13 13:00:06 +0100583 direction_may_change) {
584 set_bit(FLAG_SYSFS_DIR, &desc->flags);
585 }
586
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900587 spin_unlock_irqrestore(&gpio_lock, flags);
588
Johan Hovoldc43960f2015-05-04 17:10:37 +0200589 data = kzalloc(sizeof(*data), GFP_KERNEL);
590 if (!data) {
591 status = -ENOMEM;
592 goto err_unlock;
593 }
594
595 data->desc = desc;
596
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900597 offset = gpio_chip_hwgpio(desc);
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200598 if (chip->names && chip->names[offset])
599 ioname = chip->names[offset];
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900600
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200601 dev = device_create_with_groups(&gpio_class, chip->dev,
Johan Hovoldc43960f2015-05-04 17:10:37 +0200602 MKDEV(0, 0), data, gpio_groups,
Johan Hovold0915e6f2015-01-13 13:00:05 +0100603 ioname ? ioname : "gpio%u",
604 desc_to_gpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900605 if (IS_ERR(dev)) {
606 status = PTR_ERR(dev);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200607 goto err_free_data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900608 }
609
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900610 set_bit(FLAG_EXPORT, &desc->flags);
611 mutex_unlock(&sysfs_lock);
612 return 0;
613
Johan Hovoldc43960f2015-05-04 17:10:37 +0200614err_free_data:
615 kfree(data);
616err_unlock:
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900617 mutex_unlock(&sysfs_lock);
618 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
619 return status;
620}
621EXPORT_SYMBOL_GPL(gpiod_export);
622
Johan Hovoldc43960f2015-05-04 17:10:37 +0200623static int match_export(struct device *dev, const void *desc)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900624{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200625 struct gpiod_data *data = dev_get_drvdata(dev);
626
627 return data->desc == desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900628}
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/**
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900676 * gpiod_unexport - reverse effect of gpio_export()
677 * @gpio: gpio to make unavailable
678 *
679 * This is implicit on gpio_free().
680 */
681void gpiod_unexport(struct gpio_desc *desc)
682{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200683 struct gpiod_data *data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900684 int status = 0;
685 struct device *dev = NULL;
686
687 if (!desc) {
688 pr_warn("%s: invalid GPIO\n", __func__);
689 return;
690 }
691
692 mutex_lock(&sysfs_lock);
693
694 if (test_bit(FLAG_EXPORT, &desc->flags)) {
695
696 dev = class_find_device(&gpio_class, NULL, desc, match_export);
697 if (dev) {
Johan Hovoldebbeba12015-01-13 13:00:06 +0100698 clear_bit(FLAG_SYSFS_DIR, &desc->flags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900699 clear_bit(FLAG_EXPORT, &desc->flags);
700 } else
701 status = -ENODEV;
702 }
703
704 mutex_unlock(&sysfs_lock);
705
706 if (dev) {
Johan Hovoldc43960f2015-05-04 17:10:37 +0200707 data = dev_get_drvdata(dev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900708 device_unregister(dev);
Johan Hovold54d9acd2015-05-04 17:10:35 +0200709 /*
710 * Release irq after deregistration to prevent race with
711 * edge_store.
712 */
713 gpio_setup_irq(desc, dev, 0);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900714 put_device(dev);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200715 kfree(data);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900716 }
717
718 if (status)
719 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
720}
721EXPORT_SYMBOL_GPL(gpiod_unexport);
722
Johan Hovold426577b2015-05-04 17:10:32 +0200723int gpiochip_sysfs_register(struct gpio_chip *chip)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900724{
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900725 struct device *dev;
726
Johan Hovold426577b2015-05-04 17:10:32 +0200727 /*
728 * Many systems add gpio chips for SOC support very early,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900729 * before driver model support is available. In those cases we
Johan Hovold426577b2015-05-04 17:10:32 +0200730 * register later, in gpiolib_sysfs_init() ... here we just
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900731 * verify that _some_ field of gpio_class got initialized.
732 */
733 if (!gpio_class.p)
734 return 0;
735
736 /* use chip->base for the ID; it's already known to be unique */
Johan Hovold121b6a72015-01-13 13:00:04 +0100737 dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0),
738 chip, gpiochip_groups,
739 "gpiochip%d", chip->base);
740 if (IS_ERR(dev))
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200741 return PTR_ERR(dev);
Johan Hovold3ff74be2015-05-04 17:10:30 +0200742
743 mutex_lock(&sysfs_lock);
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200744 chip->cdev = dev;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900745 mutex_unlock(&sysfs_lock);
746
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200747 return 0;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900748}
749
Johan Hovold426577b2015-05-04 17:10:32 +0200750void gpiochip_sysfs_unregister(struct gpio_chip *chip)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900751{
Johan Hovold483d8212015-04-21 17:42:09 +0200752 struct gpio_desc *desc;
753 unsigned int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900754
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200755 if (!chip->cdev)
756 return;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900757
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200758 device_unregister(chip->cdev);
759
760 /* prevent further gpiod exports */
761 mutex_lock(&sysfs_lock);
762 chip->cdev = NULL;
763 mutex_unlock(&sysfs_lock);
Johan Hovold483d8212015-04-21 17:42:09 +0200764
765 /* unregister gpiod class devices owned by sysfs */
766 for (i = 0; i < chip->ngpio; i++) {
767 desc = &chip->desc[i];
768 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
769 gpiod_free(desc);
770 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900771}
772
773static int __init gpiolib_sysfs_init(void)
774{
775 int status;
776 unsigned long flags;
777 struct gpio_chip *chip;
778
779 status = class_register(&gpio_class);
780 if (status < 0)
781 return status;
782
783 /* Scan and register the gpio_chips which registered very
784 * early (e.g. before the class_register above was called).
785 *
786 * We run before arch_initcall() so chip->dev nodes can have
787 * registered, and so arch_initcall() can always gpio_export().
788 */
789 spin_lock_irqsave(&gpio_lock, flags);
790 list_for_each_entry(chip, &gpio_chips, list) {
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200791 if (chip->cdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900792 continue;
793
Alexandre Courbot14141a92014-07-22 16:17:40 +0900794 /*
Johan Hovold426577b2015-05-04 17:10:32 +0200795 * TODO we yield gpio_lock here because
796 * gpiochip_sysfs_register() acquires a mutex. This is unsafe
797 * and needs to be fixed.
Alexandre Courbot14141a92014-07-22 16:17:40 +0900798 *
799 * Also it would be nice to use gpiochip_find() here so we
800 * can keep gpio_chips local to gpiolib.c, but the yield of
801 * gpio_lock prevents us from doing this.
802 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900803 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold426577b2015-05-04 17:10:32 +0200804 status = gpiochip_sysfs_register(chip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900805 spin_lock_irqsave(&gpio_lock, flags);
806 }
807 spin_unlock_irqrestore(&gpio_lock, flags);
808
809
810 return status;
811}
812postcore_initcall(gpiolib_sysfs_init);