blob: bccba406fc22c87ce924c9d54855e7314ea6eec2 [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;
Johan Hovolda08f5c22015-05-04 17:10:39 +020015 struct kernfs_node *value_kn;
Johan Hovoldc43960f2015-05-04 17:10:37 +020016};
17
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090018/* lock protects against unexport_gpio() being called while
19 * sysfs files are active.
20 */
21static DEFINE_MUTEX(sysfs_lock);
22
23/*
24 * /sys/class/gpio/gpioN... only for GPIOs that are exported
25 * /direction
26 * * MAY BE OMITTED if kernel won't allow direction changes
27 * * is read/write as "in" or "out"
28 * * may also be written as "high" or "low", initializing
29 * output value as specified ("out" implies "low")
30 * /value
31 * * always readable, subject to hardware behavior
32 * * may be writable, as zero/nonzero
33 * /edge
34 * * configures behavior of poll(2) on /value
35 * * available only if pin can generate IRQs on input
36 * * is read/write as "none", "falling", "rising", or "both"
37 * /active_low
38 * * configures polarity of /value
39 * * is read/write as zero/nonzero
40 * * also affects existing and subsequent "falling" and "rising"
41 * /edge configuration
42 */
43
Johan Hovold6beac9d2015-05-04 17:10:34 +020044static ssize_t direction_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090045 struct device_attribute *attr, char *buf)
46{
Johan Hovoldc43960f2015-05-04 17:10:37 +020047 struct gpiod_data *data = dev_get_drvdata(dev);
48 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090049 ssize_t status;
50
51 mutex_lock(&sysfs_lock);
52
Johan Hovoldf0b78662015-05-04 17:10:36 +020053 gpiod_get_direction(desc);
54 status = sprintf(buf, "%s\n",
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090055 test_bit(FLAG_IS_OUT, &desc->flags)
56 ? "out" : "in");
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090057
58 mutex_unlock(&sysfs_lock);
59 return status;
60}
61
Johan Hovold6beac9d2015-05-04 17:10:34 +020062static ssize_t direction_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090063 struct device_attribute *attr, const char *buf, size_t size)
64{
Johan Hovoldc43960f2015-05-04 17:10:37 +020065 struct gpiod_data *data = dev_get_drvdata(dev);
66 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090067 ssize_t status;
68
69 mutex_lock(&sysfs_lock);
70
Johan Hovoldf0b78662015-05-04 17:10:36 +020071 if (sysfs_streq(buf, "high"))
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090072 status = gpiod_direction_output_raw(desc, 1);
73 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
74 status = gpiod_direction_output_raw(desc, 0);
75 else if (sysfs_streq(buf, "in"))
76 status = gpiod_direction_input(desc);
77 else
78 status = -EINVAL;
79
80 mutex_unlock(&sysfs_lock);
81 return status ? : size;
82}
Johan Hovold6beac9d2015-05-04 17:10:34 +020083static DEVICE_ATTR_RW(direction);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090084
Johan Hovold6beac9d2015-05-04 17:10:34 +020085static ssize_t value_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090086 struct device_attribute *attr, char *buf)
87{
Johan Hovoldc43960f2015-05-04 17:10:37 +020088 struct gpiod_data *data = dev_get_drvdata(dev);
89 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090090 ssize_t status;
91
92 mutex_lock(&sysfs_lock);
93
Johan Hovoldf0b78662015-05-04 17:10:36 +020094 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090095
96 mutex_unlock(&sysfs_lock);
97 return status;
98}
99
Johan Hovold6beac9d2015-05-04 17:10:34 +0200100static ssize_t value_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900101 struct device_attribute *attr, const char *buf, size_t size)
102{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200103 struct gpiod_data *data = dev_get_drvdata(dev);
104 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900105 ssize_t status;
106
107 mutex_lock(&sysfs_lock);
108
Johan Hovoldf0b78662015-05-04 17:10:36 +0200109 if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900110 status = -EPERM;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200111 } else {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900112 long value;
113
114 status = kstrtol(buf, 0, &value);
115 if (status == 0) {
116 gpiod_set_value_cansleep(desc, value);
117 status = size;
118 }
119 }
120
121 mutex_unlock(&sysfs_lock);
122 return status;
123}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200124static DEVICE_ATTR_RW(value);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900125
126static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
127{
Johan Hovolda08f5c22015-05-04 17:10:39 +0200128 struct gpiod_data *data = priv;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900129
Johan Hovolda08f5c22015-05-04 17:10:39 +0200130 sysfs_notify_dirent(data->value_kn);
131
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900132 return IRQ_HANDLED;
133}
134
Johan Hovold0f628502015-05-04 17:10:38 +0200135static int gpio_setup_irq(struct device *dev, unsigned long gpio_flags)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900136{
Johan Hovold0f628502015-05-04 17:10:38 +0200137 struct gpiod_data *data = dev_get_drvdata(dev);
138 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900139 unsigned long irq_flags;
Johan Hovolda08f5c22015-05-04 17:10:39 +0200140 int ret, irq;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900141
142 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
143 return 0;
144
145 irq = gpiod_to_irq(desc);
146 if (irq < 0)
147 return -EIO;
148
Johan Hovolda08f5c22015-05-04 17:10:39 +0200149 if (data->value_kn)
150 free_irq(irq, data);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900151
152 desc->flags &= ~GPIO_TRIGGER_MASK;
153
154 if (!gpio_flags) {
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900155 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900156 ret = 0;
Johan Hovolda08f5c22015-05-04 17:10:39 +0200157 goto free_kn;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900158 }
159
160 irq_flags = IRQF_SHARED;
161 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
162 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
163 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
164 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
165 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
166 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
167
Johan Hovolda08f5c22015-05-04 17:10:39 +0200168 if (!data->value_kn) {
169 data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
170 if (!data->value_kn) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900171 ret = -ENODEV;
172 goto err_out;
173 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900174 }
175
Johan Hovold52176d02015-05-04 17:10:28 +0200176 /*
177 * FIXME: This should be done in the irq_request_resources callback
178 * when the irq is requested, but a few drivers currently fail
179 * to do so.
180 *
181 * Remove this redundant call (along with the corresponding
182 * unlock) when those drivers have been fixed.
183 */
184 ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900185 if (ret < 0)
Johan Hovolda08f5c22015-05-04 17:10:39 +0200186 goto free_kn;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900187
Johan Hovold52176d02015-05-04 17:10:28 +0200188 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Johan Hovolda08f5c22015-05-04 17:10:39 +0200189 "gpiolib", data);
Johan Hovold52176d02015-05-04 17:10:28 +0200190 if (ret < 0)
191 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900192
193 desc->flags |= gpio_flags;
194 return 0;
195
Johan Hovold52176d02015-05-04 17:10:28 +0200196err_unlock:
197 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Johan Hovolda08f5c22015-05-04 17:10:39 +0200198free_kn:
199 if (data->value_kn) {
200 sysfs_put(data->value_kn);
201 data->value_kn = NULL;
202 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900203err_out:
204 return ret;
205}
206
207static const struct {
208 const char *name;
209 unsigned long flags;
210} trigger_types[] = {
211 { "none", 0 },
212 { "falling", BIT(FLAG_TRIG_FALL) },
213 { "rising", BIT(FLAG_TRIG_RISE) },
214 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
215};
216
Johan Hovold6beac9d2015-05-04 17:10:34 +0200217static ssize_t edge_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900218 struct device_attribute *attr, char *buf)
219{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200220 struct gpiod_data *data = dev_get_drvdata(dev);
221 struct gpio_desc *desc = data->desc;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200222 unsigned long mask;
223 ssize_t status = 0;
224 int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900225
226 mutex_lock(&sysfs_lock);
227
Johan Hovoldf0b78662015-05-04 17:10:36 +0200228 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
229 mask = desc->flags & GPIO_TRIGGER_MASK;
230 if (mask == trigger_types[i].flags) {
231 status = sprintf(buf, "%s\n", trigger_types[i].name);
232 break;
233 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900234 }
235
236 mutex_unlock(&sysfs_lock);
237 return status;
238}
239
Johan Hovold6beac9d2015-05-04 17:10:34 +0200240static ssize_t edge_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900241 struct device_attribute *attr, const char *buf, size_t size)
242{
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900243 ssize_t status;
244 int i;
245
246 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
247 if (sysfs_streq(trigger_types[i].name, buf))
248 goto found;
249 return -EINVAL;
250
251found:
252 mutex_lock(&sysfs_lock);
253
Johan Hovold0f628502015-05-04 17:10:38 +0200254 status = gpio_setup_irq(dev, trigger_types[i].flags);
Johan Hovoldf0b78662015-05-04 17:10:36 +0200255 if (!status)
256 status = size;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900257
258 mutex_unlock(&sysfs_lock);
259
260 return status;
261}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200262static DEVICE_ATTR_RW(edge);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900263
Johan Hovold0f628502015-05-04 17:10:38 +0200264static int sysfs_set_active_low(struct device *dev, int value)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900265{
Johan Hovold0f628502015-05-04 17:10:38 +0200266 struct gpiod_data *data = dev_get_drvdata(dev);
267 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900268 int status = 0;
269
270 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
271 return 0;
272
273 if (value)
274 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
275 else
276 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
277
278 /* reconfigure poll(2) support if enabled on one edge only */
Johan Hovold166a85e2015-05-04 17:10:33 +0200279 if (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
280 !!test_bit(FLAG_TRIG_FALL, &desc->flags)) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900281 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
282
Johan Hovold0f628502015-05-04 17:10:38 +0200283 gpio_setup_irq(dev, 0);
284 status = gpio_setup_irq(dev, trigger_flags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900285 }
286
287 return status;
288}
289
Johan Hovold6beac9d2015-05-04 17:10:34 +0200290static ssize_t active_low_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900291 struct device_attribute *attr, char *buf)
292{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200293 struct gpiod_data *data = dev_get_drvdata(dev);
294 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900295 ssize_t status;
296
297 mutex_lock(&sysfs_lock);
298
Johan Hovoldf0b78662015-05-04 17:10:36 +0200299 status = sprintf(buf, "%d\n",
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900300 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
301
302 mutex_unlock(&sysfs_lock);
303
304 return status;
305}
306
Johan Hovold6beac9d2015-05-04 17:10:34 +0200307static ssize_t active_low_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900308 struct device_attribute *attr, const char *buf, size_t size)
309{
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900310 ssize_t status;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200311 long value;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900312
313 mutex_lock(&sysfs_lock);
314
Johan Hovoldf0b78662015-05-04 17:10:36 +0200315 status = kstrtol(buf, 0, &value);
316 if (status == 0)
Johan Hovold0f628502015-05-04 17:10:38 +0200317 status = sysfs_set_active_low(dev, value != 0);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900318
319 mutex_unlock(&sysfs_lock);
320
321 return status ? : size;
322}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200323static DEVICE_ATTR_RW(active_low);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900324
Johan Hovoldebbeba12015-01-13 13:00:06 +0100325static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
326 int n)
327{
328 struct device *dev = container_of(kobj, struct device, kobj);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200329 struct gpiod_data *data = dev_get_drvdata(dev);
330 struct gpio_desc *desc = data->desc;
Johan Hovoldebbeba12015-01-13 13:00:06 +0100331 umode_t mode = attr->mode;
332 bool show_direction = test_bit(FLAG_SYSFS_DIR, &desc->flags);
333
334 if (attr == &dev_attr_direction.attr) {
335 if (!show_direction)
336 mode = 0;
337 } else if (attr == &dev_attr_edge.attr) {
338 if (gpiod_to_irq(desc) < 0)
339 mode = 0;
340 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
341 mode = 0;
342 }
343
344 return mode;
345}
346
Johan Hovold0915e6f2015-01-13 13:00:05 +0100347static struct attribute *gpio_attrs[] = {
Johan Hovoldebbeba12015-01-13 13:00:06 +0100348 &dev_attr_direction.attr,
349 &dev_attr_edge.attr,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900350 &dev_attr_value.attr,
351 &dev_attr_active_low.attr,
352 NULL,
353};
Johan Hovoldebbeba12015-01-13 13:00:06 +0100354
355static const struct attribute_group gpio_group = {
356 .attrs = gpio_attrs,
357 .is_visible = gpio_is_visible,
358};
359
360static const struct attribute_group *gpio_groups[] = {
361 &gpio_group,
362 NULL
363};
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900364
365/*
366 * /sys/class/gpio/gpiochipN/
367 * /base ... matching gpio_chip.base (N)
368 * /label ... matching gpio_chip.label
369 * /ngpio ... matching gpio_chip.ngpio
370 */
371
Johan Hovold6beac9d2015-05-04 17:10:34 +0200372static ssize_t base_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900373 struct device_attribute *attr, char *buf)
374{
375 const struct gpio_chip *chip = dev_get_drvdata(dev);
376
377 return sprintf(buf, "%d\n", chip->base);
378}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200379static DEVICE_ATTR_RO(base);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900380
Johan Hovold6beac9d2015-05-04 17:10:34 +0200381static ssize_t label_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900382 struct device_attribute *attr, char *buf)
383{
384 const struct gpio_chip *chip = dev_get_drvdata(dev);
385
386 return sprintf(buf, "%s\n", chip->label ? : "");
387}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200388static DEVICE_ATTR_RO(label);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900389
Johan Hovold6beac9d2015-05-04 17:10:34 +0200390static ssize_t ngpio_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900391 struct device_attribute *attr, char *buf)
392{
393 const struct gpio_chip *chip = dev_get_drvdata(dev);
394
395 return sprintf(buf, "%u\n", chip->ngpio);
396}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200397static DEVICE_ATTR_RO(ngpio);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900398
Johan Hovold121b6a72015-01-13 13:00:04 +0100399static struct attribute *gpiochip_attrs[] = {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900400 &dev_attr_base.attr,
401 &dev_attr_label.attr,
402 &dev_attr_ngpio.attr,
403 NULL,
404};
Johan Hovold121b6a72015-01-13 13:00:04 +0100405ATTRIBUTE_GROUPS(gpiochip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900406
407/*
408 * /sys/class/gpio/export ... write-only
409 * integer N ... number of GPIO to export (full access)
410 * /sys/class/gpio/unexport ... write-only
411 * integer N ... number of GPIO to unexport
412 */
413static ssize_t export_store(struct class *class,
414 struct class_attribute *attr,
415 const char *buf, size_t len)
416{
417 long gpio;
418 struct gpio_desc *desc;
419 int status;
420
421 status = kstrtol(buf, 0, &gpio);
422 if (status < 0)
423 goto done;
424
425 desc = gpio_to_desc(gpio);
426 /* reject invalid GPIOs */
427 if (!desc) {
428 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
429 return -EINVAL;
430 }
431
432 /* No extra locking here; FLAG_SYSFS just signifies that the
433 * request and export were done by on behalf of userspace, so
434 * they may be undone on its behalf too.
435 */
436
437 status = gpiod_request(desc, "sysfs");
438 if (status < 0) {
439 if (status == -EPROBE_DEFER)
440 status = -ENODEV;
441 goto done;
442 }
443 status = gpiod_export(desc, true);
444 if (status < 0)
445 gpiod_free(desc);
446 else
447 set_bit(FLAG_SYSFS, &desc->flags);
448
449done:
450 if (status)
451 pr_debug("%s: status %d\n", __func__, status);
452 return status ? : len;
453}
454
455static ssize_t unexport_store(struct class *class,
456 struct class_attribute *attr,
457 const char *buf, size_t len)
458{
459 long gpio;
460 struct gpio_desc *desc;
461 int status;
462
463 status = kstrtol(buf, 0, &gpio);
464 if (status < 0)
465 goto done;
466
467 desc = gpio_to_desc(gpio);
468 /* reject bogus commands (gpio_unexport ignores them) */
469 if (!desc) {
470 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
471 return -EINVAL;
472 }
473
474 status = -EINVAL;
475
476 /* No extra locking here; FLAG_SYSFS just signifies that the
477 * request and export were done by on behalf of userspace, so
478 * they may be undone on its behalf too.
479 */
480 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
481 status = 0;
482 gpiod_free(desc);
483 }
484done:
485 if (status)
486 pr_debug("%s: status %d\n", __func__, status);
487 return status ? : len;
488}
489
490static struct class_attribute gpio_class_attrs[] = {
491 __ATTR(export, 0200, NULL, export_store),
492 __ATTR(unexport, 0200, NULL, unexport_store),
493 __ATTR_NULL,
494};
495
496static struct class gpio_class = {
497 .name = "gpio",
498 .owner = THIS_MODULE,
499
500 .class_attrs = gpio_class_attrs,
501};
502
503
504/**
505 * gpiod_export - export a GPIO through sysfs
506 * @gpio: gpio to make available, already requested
507 * @direction_may_change: true if userspace may change gpio direction
508 * Context: arch_initcall or later
509 *
510 * When drivers want to make a GPIO accessible to userspace after they
511 * have requested it -- perhaps while debugging, or as part of their
512 * public interface -- they may use this routine. If the GPIO can
513 * change direction (some can't) and the caller allows it, userspace
514 * will see "direction" sysfs attribute which may be used to change
515 * the gpio's direction. A "value" attribute will always be provided.
516 *
517 * Returns zero on success, else an error.
518 */
519int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
520{
Johan Hovold483d8212015-04-21 17:42:09 +0200521 struct gpio_chip *chip;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200522 struct gpiod_data *data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900523 unsigned long flags;
524 int status;
525 const char *ioname = NULL;
526 struct device *dev;
527 int offset;
528
529 /* can't export until sysfs is available ... */
530 if (!gpio_class.p) {
531 pr_debug("%s: called too early!\n", __func__);
532 return -ENOENT;
533 }
534
535 if (!desc) {
536 pr_debug("%s: invalid gpio descriptor\n", __func__);
537 return -EINVAL;
538 }
539
Johan Hovold483d8212015-04-21 17:42:09 +0200540 chip = desc->chip;
541
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900542 mutex_lock(&sysfs_lock);
543
Johan Hovold483d8212015-04-21 17:42:09 +0200544 /* check if chip is being removed */
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200545 if (!chip || !chip->cdev) {
Johan Hovold483d8212015-04-21 17:42:09 +0200546 status = -ENODEV;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200547 goto err_unlock;
Johan Hovold483d8212015-04-21 17:42:09 +0200548 }
549
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900550 spin_lock_irqsave(&gpio_lock, flags);
551 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
552 test_bit(FLAG_EXPORT, &desc->flags)) {
553 spin_unlock_irqrestore(&gpio_lock, flags);
554 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
555 __func__,
556 test_bit(FLAG_REQUESTED, &desc->flags),
557 test_bit(FLAG_EXPORT, &desc->flags));
558 status = -EPERM;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200559 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900560 }
561
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200562 if (chip->direction_input && chip->direction_output &&
Johan Hovoldebbeba12015-01-13 13:00:06 +0100563 direction_may_change) {
564 set_bit(FLAG_SYSFS_DIR, &desc->flags);
565 }
566
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900567 spin_unlock_irqrestore(&gpio_lock, flags);
568
Johan Hovoldc43960f2015-05-04 17:10:37 +0200569 data = kzalloc(sizeof(*data), GFP_KERNEL);
570 if (!data) {
571 status = -ENOMEM;
572 goto err_unlock;
573 }
574
575 data->desc = desc;
576
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900577 offset = gpio_chip_hwgpio(desc);
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200578 if (chip->names && chip->names[offset])
579 ioname = chip->names[offset];
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900580
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200581 dev = device_create_with_groups(&gpio_class, chip->dev,
Johan Hovoldc43960f2015-05-04 17:10:37 +0200582 MKDEV(0, 0), data, gpio_groups,
Johan Hovold0915e6f2015-01-13 13:00:05 +0100583 ioname ? ioname : "gpio%u",
584 desc_to_gpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900585 if (IS_ERR(dev)) {
586 status = PTR_ERR(dev);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200587 goto err_free_data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900588 }
589
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900590 set_bit(FLAG_EXPORT, &desc->flags);
591 mutex_unlock(&sysfs_lock);
592 return 0;
593
Johan Hovoldc43960f2015-05-04 17:10:37 +0200594err_free_data:
595 kfree(data);
596err_unlock:
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900597 mutex_unlock(&sysfs_lock);
598 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
599 return status;
600}
601EXPORT_SYMBOL_GPL(gpiod_export);
602
Johan Hovoldc43960f2015-05-04 17:10:37 +0200603static int match_export(struct device *dev, const void *desc)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900604{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200605 struct gpiod_data *data = dev_get_drvdata(dev);
606
607 return data->desc == desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900608}
609
610/**
611 * gpiod_export_link - create a sysfs link to an exported GPIO node
612 * @dev: device under which to create symlink
613 * @name: name of the symlink
614 * @gpio: gpio to create symlink to, already exported
615 *
616 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
617 * node. Caller is responsible for unlinking.
618 *
619 * Returns zero on success, else an error.
620 */
621int gpiod_export_link(struct device *dev, const char *name,
622 struct gpio_desc *desc)
623{
624 int status = -EINVAL;
625
626 if (!desc) {
627 pr_warn("%s: invalid GPIO\n", __func__);
628 return -EINVAL;
629 }
630
631 mutex_lock(&sysfs_lock);
632
633 if (test_bit(FLAG_EXPORT, &desc->flags)) {
634 struct device *tdev;
635
636 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
637 if (tdev != NULL) {
638 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
639 name);
Johan Hovold0f303db2015-01-26 12:02:45 +0100640 put_device(tdev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900641 } else {
642 status = -ENODEV;
643 }
644 }
645
646 mutex_unlock(&sysfs_lock);
647
648 if (status)
649 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
650
651 return status;
652}
653EXPORT_SYMBOL_GPL(gpiod_export_link);
654
655/**
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900656 * gpiod_unexport - reverse effect of gpio_export()
657 * @gpio: gpio to make unavailable
658 *
659 * This is implicit on gpio_free().
660 */
661void gpiod_unexport(struct gpio_desc *desc)
662{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200663 struct gpiod_data *data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900664 int status = 0;
665 struct device *dev = NULL;
666
667 if (!desc) {
668 pr_warn("%s: invalid GPIO\n", __func__);
669 return;
670 }
671
672 mutex_lock(&sysfs_lock);
673
674 if (test_bit(FLAG_EXPORT, &desc->flags)) {
675
676 dev = class_find_device(&gpio_class, NULL, desc, match_export);
677 if (dev) {
Johan Hovoldebbeba12015-01-13 13:00:06 +0100678 clear_bit(FLAG_SYSFS_DIR, &desc->flags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900679 clear_bit(FLAG_EXPORT, &desc->flags);
680 } else
681 status = -ENODEV;
682 }
683
684 mutex_unlock(&sysfs_lock);
685
686 if (dev) {
Johan Hovoldc43960f2015-05-04 17:10:37 +0200687 data = dev_get_drvdata(dev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900688 device_unregister(dev);
Johan Hovold54d9acd2015-05-04 17:10:35 +0200689 /*
690 * Release irq after deregistration to prevent race with
691 * edge_store.
692 */
Johan Hovold0f628502015-05-04 17:10:38 +0200693 gpio_setup_irq(dev, 0);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900694 put_device(dev);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200695 kfree(data);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900696 }
697
698 if (status)
699 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
700}
701EXPORT_SYMBOL_GPL(gpiod_unexport);
702
Johan Hovold426577b2015-05-04 17:10:32 +0200703int gpiochip_sysfs_register(struct gpio_chip *chip)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900704{
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900705 struct device *dev;
706
Johan Hovold426577b2015-05-04 17:10:32 +0200707 /*
708 * Many systems add gpio chips for SOC support very early,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900709 * before driver model support is available. In those cases we
Johan Hovold426577b2015-05-04 17:10:32 +0200710 * register later, in gpiolib_sysfs_init() ... here we just
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900711 * verify that _some_ field of gpio_class got initialized.
712 */
713 if (!gpio_class.p)
714 return 0;
715
716 /* use chip->base for the ID; it's already known to be unique */
Johan Hovold121b6a72015-01-13 13:00:04 +0100717 dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0),
718 chip, gpiochip_groups,
719 "gpiochip%d", chip->base);
720 if (IS_ERR(dev))
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200721 return PTR_ERR(dev);
Johan Hovold3ff74be2015-05-04 17:10:30 +0200722
723 mutex_lock(&sysfs_lock);
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200724 chip->cdev = dev;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900725 mutex_unlock(&sysfs_lock);
726
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200727 return 0;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900728}
729
Johan Hovold426577b2015-05-04 17:10:32 +0200730void gpiochip_sysfs_unregister(struct gpio_chip *chip)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900731{
Johan Hovold483d8212015-04-21 17:42:09 +0200732 struct gpio_desc *desc;
733 unsigned int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900734
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200735 if (!chip->cdev)
736 return;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900737
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200738 device_unregister(chip->cdev);
739
740 /* prevent further gpiod exports */
741 mutex_lock(&sysfs_lock);
742 chip->cdev = NULL;
743 mutex_unlock(&sysfs_lock);
Johan Hovold483d8212015-04-21 17:42:09 +0200744
745 /* unregister gpiod class devices owned by sysfs */
746 for (i = 0; i < chip->ngpio; i++) {
747 desc = &chip->desc[i];
748 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
749 gpiod_free(desc);
750 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900751}
752
753static int __init gpiolib_sysfs_init(void)
754{
755 int status;
756 unsigned long flags;
757 struct gpio_chip *chip;
758
759 status = class_register(&gpio_class);
760 if (status < 0)
761 return status;
762
763 /* Scan and register the gpio_chips which registered very
764 * early (e.g. before the class_register above was called).
765 *
766 * We run before arch_initcall() so chip->dev nodes can have
767 * registered, and so arch_initcall() can always gpio_export().
768 */
769 spin_lock_irqsave(&gpio_lock, flags);
770 list_for_each_entry(chip, &gpio_chips, list) {
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200771 if (chip->cdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900772 continue;
773
Alexandre Courbot14141a92014-07-22 16:17:40 +0900774 /*
Johan Hovold426577b2015-05-04 17:10:32 +0200775 * TODO we yield gpio_lock here because
776 * gpiochip_sysfs_register() acquires a mutex. This is unsafe
777 * and needs to be fixed.
Alexandre Courbot14141a92014-07-22 16:17:40 +0900778 *
779 * Also it would be nice to use gpiochip_find() here so we
780 * can keep gpio_chips local to gpiolib.c, but the yield of
781 * gpio_lock prevents us from doing this.
782 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900783 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold426577b2015-05-04 17:10:32 +0200784 status = gpiochip_sysfs_register(chip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900785 spin_lock_irqsave(&gpio_lock, flags);
786 }
787 spin_unlock_irqrestore(&gpio_lock, flags);
788
789
790 return status;
791}
792postcore_initcall(gpiolib_sysfs_init);