blob: 201b757ad0dbe387e6332e3f88b4c3a85ad1a5c5 [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 Hovold6beac9d12015-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 Hovold6beac9d12015-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 Hovold6beac9d12015-05-04 17:10:34 +020083static DEVICE_ATTR_RW(direction);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090084
Johan Hovold6beac9d12015-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 Hovold6beac9d12015-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 Hovold6beac9d12015-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
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900142 irq = gpiod_to_irq(desc);
143 if (irq < 0)
144 return -EIO;
145
Johan Hovolda08f5c22015-05-04 17:10:39 +0200146 if (data->value_kn)
147 free_irq(irq, data);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900148
149 desc->flags &= ~GPIO_TRIGGER_MASK;
150
151 if (!gpio_flags) {
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900152 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900153 ret = 0;
Johan Hovolda08f5c22015-05-04 17:10:39 +0200154 goto free_kn;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900155 }
156
157 irq_flags = IRQF_SHARED;
158 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
159 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
160 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
161 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
162 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
163 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
164
Johan Hovolda08f5c22015-05-04 17:10:39 +0200165 if (!data->value_kn) {
166 data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
167 if (!data->value_kn) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900168 ret = -ENODEV;
169 goto err_out;
170 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900171 }
172
Johan Hovold52176d02015-05-04 17:10:28 +0200173 /*
174 * FIXME: This should be done in the irq_request_resources callback
175 * when the irq is requested, but a few drivers currently fail
176 * to do so.
177 *
178 * Remove this redundant call (along with the corresponding
179 * unlock) when those drivers have been fixed.
180 */
181 ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900182 if (ret < 0)
Johan Hovolda08f5c22015-05-04 17:10:39 +0200183 goto free_kn;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900184
Johan Hovold52176d02015-05-04 17:10:28 +0200185 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Johan Hovolda08f5c22015-05-04 17:10:39 +0200186 "gpiolib", data);
Johan Hovold52176d02015-05-04 17:10:28 +0200187 if (ret < 0)
188 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900189
190 desc->flags |= gpio_flags;
191 return 0;
192
Johan Hovold52176d02015-05-04 17:10:28 +0200193err_unlock:
194 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Johan Hovolda08f5c22015-05-04 17:10:39 +0200195free_kn:
196 if (data->value_kn) {
197 sysfs_put(data->value_kn);
198 data->value_kn = NULL;
199 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900200err_out:
201 return ret;
202}
203
204static const struct {
205 const char *name;
206 unsigned long flags;
207} trigger_types[] = {
208 { "none", 0 },
209 { "falling", BIT(FLAG_TRIG_FALL) },
210 { "rising", BIT(FLAG_TRIG_RISE) },
211 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
212};
213
Johan Hovold6beac9d12015-05-04 17:10:34 +0200214static ssize_t edge_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900215 struct device_attribute *attr, char *buf)
216{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200217 struct gpiod_data *data = dev_get_drvdata(dev);
218 struct gpio_desc *desc = data->desc;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200219 unsigned long mask;
220 ssize_t status = 0;
221 int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900222
223 mutex_lock(&sysfs_lock);
224
Johan Hovoldf0b78662015-05-04 17:10:36 +0200225 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
226 mask = desc->flags & GPIO_TRIGGER_MASK;
227 if (mask == trigger_types[i].flags) {
228 status = sprintf(buf, "%s\n", trigger_types[i].name);
229 break;
230 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900231 }
232
233 mutex_unlock(&sysfs_lock);
234 return status;
235}
236
Johan Hovold6beac9d12015-05-04 17:10:34 +0200237static ssize_t edge_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900238 struct device_attribute *attr, const char *buf, size_t size)
239{
Johan Hovoldb91e1802015-05-04 17:10:40 +0200240 struct gpiod_data *data = dev_get_drvdata(dev);
241 struct gpio_desc *desc = data->desc;
242 unsigned long flags;
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:
Johan Hovoldb91e1802015-05-04 17:10:40 +0200252 flags = trigger_types[i].flags;
253
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900254 mutex_lock(&sysfs_lock);
255
Johan Hovoldb91e1802015-05-04 17:10:40 +0200256 if ((desc->flags & GPIO_TRIGGER_MASK) == flags) {
257 status = size;
258 goto out_unlock;
259 }
260
261 status = gpio_setup_irq(dev, flags);
Johan Hovoldf0b78662015-05-04 17:10:36 +0200262 if (!status)
263 status = size;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900264
Johan Hovoldb91e1802015-05-04 17:10:40 +0200265out_unlock:
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900266 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
Johan Hovold0f628502015-05-04 17:10:38 +0200272static int sysfs_set_active_low(struct device *dev, int value)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900273{
Johan Hovold0f628502015-05-04 17:10:38 +0200274 struct gpiod_data *data = dev_get_drvdata(dev);
275 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900276 int status = 0;
277
278 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
279 return 0;
280
281 if (value)
282 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
283 else
284 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
285
286 /* reconfigure poll(2) support if enabled on one edge only */
Johan Hovold166a85e2015-05-04 17:10:33 +0200287 if (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
288 !!test_bit(FLAG_TRIG_FALL, &desc->flags)) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900289 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
290
Johan Hovold0f628502015-05-04 17:10:38 +0200291 gpio_setup_irq(dev, 0);
292 status = gpio_setup_irq(dev, trigger_flags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900293 }
294
295 return status;
296}
297
Johan Hovold6beac9d12015-05-04 17:10:34 +0200298static ssize_t active_low_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900299 struct device_attribute *attr, char *buf)
300{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200301 struct gpiod_data *data = dev_get_drvdata(dev);
302 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900303 ssize_t status;
304
305 mutex_lock(&sysfs_lock);
306
Johan Hovoldf0b78662015-05-04 17:10:36 +0200307 status = sprintf(buf, "%d\n",
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900308 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
309
310 mutex_unlock(&sysfs_lock);
311
312 return status;
313}
314
Johan Hovold6beac9d12015-05-04 17:10:34 +0200315static ssize_t active_low_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900316 struct device_attribute *attr, const char *buf, size_t size)
317{
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900318 ssize_t status;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200319 long value;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900320
321 mutex_lock(&sysfs_lock);
322
Johan Hovoldf0b78662015-05-04 17:10:36 +0200323 status = kstrtol(buf, 0, &value);
324 if (status == 0)
Johan Hovold0f628502015-05-04 17:10:38 +0200325 status = sysfs_set_active_low(dev, value != 0);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900326
327 mutex_unlock(&sysfs_lock);
328
329 return status ? : size;
330}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200331static DEVICE_ATTR_RW(active_low);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900332
Johan Hovoldebbeba12015-01-13 13:00:06 +0100333static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
334 int n)
335{
336 struct device *dev = container_of(kobj, struct device, kobj);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200337 struct gpiod_data *data = dev_get_drvdata(dev);
338 struct gpio_desc *desc = data->desc;
Johan Hovoldebbeba12015-01-13 13:00:06 +0100339 umode_t mode = attr->mode;
340 bool show_direction = test_bit(FLAG_SYSFS_DIR, &desc->flags);
341
342 if (attr == &dev_attr_direction.attr) {
343 if (!show_direction)
344 mode = 0;
345 } else if (attr == &dev_attr_edge.attr) {
346 if (gpiod_to_irq(desc) < 0)
347 mode = 0;
348 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
349 mode = 0;
350 }
351
352 return mode;
353}
354
Johan Hovold0915e6f2015-01-13 13:00:05 +0100355static struct attribute *gpio_attrs[] = {
Johan Hovoldebbeba12015-01-13 13:00:06 +0100356 &dev_attr_direction.attr,
357 &dev_attr_edge.attr,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900358 &dev_attr_value.attr,
359 &dev_attr_active_low.attr,
360 NULL,
361};
Johan Hovoldebbeba12015-01-13 13:00:06 +0100362
363static const struct attribute_group gpio_group = {
364 .attrs = gpio_attrs,
365 .is_visible = gpio_is_visible,
366};
367
368static const struct attribute_group *gpio_groups[] = {
369 &gpio_group,
370 NULL
371};
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900372
373/*
374 * /sys/class/gpio/gpiochipN/
375 * /base ... matching gpio_chip.base (N)
376 * /label ... matching gpio_chip.label
377 * /ngpio ... matching gpio_chip.ngpio
378 */
379
Johan Hovold6beac9d12015-05-04 17:10:34 +0200380static ssize_t base_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900381 struct device_attribute *attr, char *buf)
382{
383 const struct gpio_chip *chip = dev_get_drvdata(dev);
384
385 return sprintf(buf, "%d\n", chip->base);
386}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200387static DEVICE_ATTR_RO(base);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900388
Johan Hovold6beac9d12015-05-04 17:10:34 +0200389static ssize_t label_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900390 struct device_attribute *attr, char *buf)
391{
392 const struct gpio_chip *chip = dev_get_drvdata(dev);
393
394 return sprintf(buf, "%s\n", chip->label ? : "");
395}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200396static DEVICE_ATTR_RO(label);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900397
Johan Hovold6beac9d12015-05-04 17:10:34 +0200398static ssize_t ngpio_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900399 struct device_attribute *attr, char *buf)
400{
401 const struct gpio_chip *chip = dev_get_drvdata(dev);
402
403 return sprintf(buf, "%u\n", chip->ngpio);
404}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200405static DEVICE_ATTR_RO(ngpio);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900406
Johan Hovold121b6a72015-01-13 13:00:04 +0100407static struct attribute *gpiochip_attrs[] = {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900408 &dev_attr_base.attr,
409 &dev_attr_label.attr,
410 &dev_attr_ngpio.attr,
411 NULL,
412};
Johan Hovold121b6a72015-01-13 13:00:04 +0100413ATTRIBUTE_GROUPS(gpiochip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900414
415/*
416 * /sys/class/gpio/export ... write-only
417 * integer N ... number of GPIO to export (full access)
418 * /sys/class/gpio/unexport ... write-only
419 * integer N ... number of GPIO to unexport
420 */
421static ssize_t export_store(struct class *class,
422 struct class_attribute *attr,
423 const char *buf, size_t len)
424{
425 long gpio;
426 struct gpio_desc *desc;
427 int status;
428
429 status = kstrtol(buf, 0, &gpio);
430 if (status < 0)
431 goto done;
432
433 desc = gpio_to_desc(gpio);
434 /* reject invalid GPIOs */
435 if (!desc) {
436 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
437 return -EINVAL;
438 }
439
440 /* No extra locking here; FLAG_SYSFS just signifies that the
441 * request and export were done by on behalf of userspace, so
442 * they may be undone on its behalf too.
443 */
444
445 status = gpiod_request(desc, "sysfs");
446 if (status < 0) {
447 if (status == -EPROBE_DEFER)
448 status = -ENODEV;
449 goto done;
450 }
451 status = gpiod_export(desc, true);
452 if (status < 0)
453 gpiod_free(desc);
454 else
455 set_bit(FLAG_SYSFS, &desc->flags);
456
457done:
458 if (status)
459 pr_debug("%s: status %d\n", __func__, status);
460 return status ? : len;
461}
462
463static ssize_t unexport_store(struct class *class,
464 struct class_attribute *attr,
465 const char *buf, size_t len)
466{
467 long gpio;
468 struct gpio_desc *desc;
469 int status;
470
471 status = kstrtol(buf, 0, &gpio);
472 if (status < 0)
473 goto done;
474
475 desc = gpio_to_desc(gpio);
476 /* reject bogus commands (gpio_unexport ignores them) */
477 if (!desc) {
478 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
479 return -EINVAL;
480 }
481
482 status = -EINVAL;
483
484 /* No extra locking here; FLAG_SYSFS just signifies that the
485 * request and export were done by on behalf of userspace, so
486 * they may be undone on its behalf too.
487 */
488 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
489 status = 0;
490 gpiod_free(desc);
491 }
492done:
493 if (status)
494 pr_debug("%s: status %d\n", __func__, status);
495 return status ? : len;
496}
497
498static struct class_attribute gpio_class_attrs[] = {
499 __ATTR(export, 0200, NULL, export_store),
500 __ATTR(unexport, 0200, NULL, unexport_store),
501 __ATTR_NULL,
502};
503
504static struct class gpio_class = {
505 .name = "gpio",
506 .owner = THIS_MODULE,
507
508 .class_attrs = gpio_class_attrs,
509};
510
511
512/**
513 * gpiod_export - export a GPIO through sysfs
514 * @gpio: gpio to make available, already requested
515 * @direction_may_change: true if userspace may change gpio direction
516 * Context: arch_initcall or later
517 *
518 * When drivers want to make a GPIO accessible to userspace after they
519 * have requested it -- perhaps while debugging, or as part of their
520 * public interface -- they may use this routine. If the GPIO can
521 * change direction (some can't) and the caller allows it, userspace
522 * will see "direction" sysfs attribute which may be used to change
523 * the gpio's direction. A "value" attribute will always be provided.
524 *
525 * Returns zero on success, else an error.
526 */
527int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
528{
Johan Hovold483d8212015-04-21 17:42:09 +0200529 struct gpio_chip *chip;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200530 struct gpiod_data *data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900531 unsigned long flags;
532 int status;
533 const char *ioname = NULL;
534 struct device *dev;
535 int offset;
536
537 /* can't export until sysfs is available ... */
538 if (!gpio_class.p) {
539 pr_debug("%s: called too early!\n", __func__);
540 return -ENOENT;
541 }
542
543 if (!desc) {
544 pr_debug("%s: invalid gpio descriptor\n", __func__);
545 return -EINVAL;
546 }
547
Johan Hovold483d8212015-04-21 17:42:09 +0200548 chip = desc->chip;
549
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900550 mutex_lock(&sysfs_lock);
551
Johan Hovold483d8212015-04-21 17:42:09 +0200552 /* check if chip is being removed */
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200553 if (!chip || !chip->cdev) {
Johan Hovold483d8212015-04-21 17:42:09 +0200554 status = -ENODEV;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200555 goto err_unlock;
Johan Hovold483d8212015-04-21 17:42:09 +0200556 }
557
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900558 spin_lock_irqsave(&gpio_lock, flags);
559 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
560 test_bit(FLAG_EXPORT, &desc->flags)) {
561 spin_unlock_irqrestore(&gpio_lock, flags);
562 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
563 __func__,
564 test_bit(FLAG_REQUESTED, &desc->flags),
565 test_bit(FLAG_EXPORT, &desc->flags));
566 status = -EPERM;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200567 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900568 }
569
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200570 if (chip->direction_input && chip->direction_output &&
Johan Hovoldebbeba12015-01-13 13:00:06 +0100571 direction_may_change) {
572 set_bit(FLAG_SYSFS_DIR, &desc->flags);
573 }
574
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900575 spin_unlock_irqrestore(&gpio_lock, flags);
576
Johan Hovoldc43960f2015-05-04 17:10:37 +0200577 data = kzalloc(sizeof(*data), GFP_KERNEL);
578 if (!data) {
579 status = -ENOMEM;
580 goto err_unlock;
581 }
582
583 data->desc = desc;
584
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900585 offset = gpio_chip_hwgpio(desc);
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200586 if (chip->names && chip->names[offset])
587 ioname = chip->names[offset];
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900588
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200589 dev = device_create_with_groups(&gpio_class, chip->dev,
Johan Hovoldc43960f2015-05-04 17:10:37 +0200590 MKDEV(0, 0), data, gpio_groups,
Johan Hovold0915e6f2015-01-13 13:00:05 +0100591 ioname ? ioname : "gpio%u",
592 desc_to_gpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900593 if (IS_ERR(dev)) {
594 status = PTR_ERR(dev);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200595 goto err_free_data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900596 }
597
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900598 set_bit(FLAG_EXPORT, &desc->flags);
599 mutex_unlock(&sysfs_lock);
600 return 0;
601
Johan Hovoldc43960f2015-05-04 17:10:37 +0200602err_free_data:
603 kfree(data);
604err_unlock:
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900605 mutex_unlock(&sysfs_lock);
606 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
607 return status;
608}
609EXPORT_SYMBOL_GPL(gpiod_export);
610
Johan Hovoldc43960f2015-05-04 17:10:37 +0200611static int match_export(struct device *dev, const void *desc)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900612{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200613 struct gpiod_data *data = dev_get_drvdata(dev);
614
615 return data->desc == desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900616}
617
618/**
619 * gpiod_export_link - create a sysfs link to an exported GPIO node
620 * @dev: device under which to create symlink
621 * @name: name of the symlink
622 * @gpio: gpio to create symlink to, already exported
623 *
624 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
625 * node. Caller is responsible for unlinking.
626 *
627 * Returns zero on success, else an error.
628 */
629int gpiod_export_link(struct device *dev, const char *name,
630 struct gpio_desc *desc)
631{
632 int status = -EINVAL;
633
634 if (!desc) {
635 pr_warn("%s: invalid GPIO\n", __func__);
636 return -EINVAL;
637 }
638
639 mutex_lock(&sysfs_lock);
640
641 if (test_bit(FLAG_EXPORT, &desc->flags)) {
642 struct device *tdev;
643
644 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
645 if (tdev != NULL) {
646 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
647 name);
Johan Hovold0f303db2015-01-26 12:02:45 +0100648 put_device(tdev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900649 } else {
650 status = -ENODEV;
651 }
652 }
653
654 mutex_unlock(&sysfs_lock);
655
656 if (status)
657 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
658
659 return status;
660}
661EXPORT_SYMBOL_GPL(gpiod_export_link);
662
663/**
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900664 * gpiod_unexport - reverse effect of gpio_export()
665 * @gpio: gpio to make unavailable
666 *
667 * This is implicit on gpio_free().
668 */
669void gpiod_unexport(struct gpio_desc *desc)
670{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200671 struct gpiod_data *data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900672 int status = 0;
673 struct device *dev = NULL;
674
675 if (!desc) {
676 pr_warn("%s: invalid GPIO\n", __func__);
677 return;
678 }
679
680 mutex_lock(&sysfs_lock);
681
682 if (test_bit(FLAG_EXPORT, &desc->flags)) {
683
684 dev = class_find_device(&gpio_class, NULL, desc, match_export);
685 if (dev) {
Johan Hovoldebbeba12015-01-13 13:00:06 +0100686 clear_bit(FLAG_SYSFS_DIR, &desc->flags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900687 clear_bit(FLAG_EXPORT, &desc->flags);
688 } else
689 status = -ENODEV;
690 }
691
692 mutex_unlock(&sysfs_lock);
693
694 if (dev) {
Johan Hovoldc43960f2015-05-04 17:10:37 +0200695 data = dev_get_drvdata(dev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900696 device_unregister(dev);
Johan Hovold54d9acd2015-05-04 17:10:35 +0200697 /*
698 * Release irq after deregistration to prevent race with
699 * edge_store.
700 */
Johan Hovoldb91e1802015-05-04 17:10:40 +0200701 if (desc->flags & GPIO_TRIGGER_MASK)
702 gpio_setup_irq(dev, 0);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900703 put_device(dev);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200704 kfree(data);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900705 }
706
707 if (status)
708 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
709}
710EXPORT_SYMBOL_GPL(gpiod_unexport);
711
Johan Hovold426577b2015-05-04 17:10:32 +0200712int gpiochip_sysfs_register(struct gpio_chip *chip)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900713{
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900714 struct device *dev;
715
Johan Hovold426577b2015-05-04 17:10:32 +0200716 /*
717 * Many systems add gpio chips for SOC support very early,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900718 * before driver model support is available. In those cases we
Johan Hovold426577b2015-05-04 17:10:32 +0200719 * register later, in gpiolib_sysfs_init() ... here we just
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900720 * verify that _some_ field of gpio_class got initialized.
721 */
722 if (!gpio_class.p)
723 return 0;
724
725 /* use chip->base for the ID; it's already known to be unique */
Johan Hovold121b6a72015-01-13 13:00:04 +0100726 dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0),
727 chip, gpiochip_groups,
728 "gpiochip%d", chip->base);
729 if (IS_ERR(dev))
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200730 return PTR_ERR(dev);
Johan Hovold3ff74be2015-05-04 17:10:30 +0200731
732 mutex_lock(&sysfs_lock);
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200733 chip->cdev = dev;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900734 mutex_unlock(&sysfs_lock);
735
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200736 return 0;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900737}
738
Johan Hovold426577b2015-05-04 17:10:32 +0200739void gpiochip_sysfs_unregister(struct gpio_chip *chip)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900740{
Johan Hovold483d8212015-04-21 17:42:09 +0200741 struct gpio_desc *desc;
742 unsigned int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900743
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200744 if (!chip->cdev)
745 return;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900746
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200747 device_unregister(chip->cdev);
748
749 /* prevent further gpiod exports */
750 mutex_lock(&sysfs_lock);
751 chip->cdev = NULL;
752 mutex_unlock(&sysfs_lock);
Johan Hovold483d8212015-04-21 17:42:09 +0200753
754 /* unregister gpiod class devices owned by sysfs */
755 for (i = 0; i < chip->ngpio; i++) {
756 desc = &chip->desc[i];
757 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
758 gpiod_free(desc);
759 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900760}
761
762static int __init gpiolib_sysfs_init(void)
763{
764 int status;
765 unsigned long flags;
766 struct gpio_chip *chip;
767
768 status = class_register(&gpio_class);
769 if (status < 0)
770 return status;
771
772 /* Scan and register the gpio_chips which registered very
773 * early (e.g. before the class_register above was called).
774 *
775 * We run before arch_initcall() so chip->dev nodes can have
776 * registered, and so arch_initcall() can always gpio_export().
777 */
778 spin_lock_irqsave(&gpio_lock, flags);
779 list_for_each_entry(chip, &gpio_chips, list) {
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200780 if (chip->cdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900781 continue;
782
Alexandre Courbot14141a92014-07-22 16:17:40 +0900783 /*
Johan Hovold426577b2015-05-04 17:10:32 +0200784 * TODO we yield gpio_lock here because
785 * gpiochip_sysfs_register() acquires a mutex. This is unsafe
786 * and needs to be fixed.
Alexandre Courbot14141a92014-07-22 16:17:40 +0900787 *
788 * Also it would be nice to use gpiochip_find() here so we
789 * can keep gpio_chips local to gpiolib.c, but the yield of
790 * gpio_lock prevents us from doing this.
791 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900792 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold426577b2015-05-04 17:10:32 +0200793 status = gpiochip_sysfs_register(chip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900794 spin_lock_irqsave(&gpio_lock, flags);
795 }
796 spin_unlock_irqrestore(&gpio_lock, flags);
797
798
799 return status;
800}
801postcore_initcall(gpiolib_sysfs_init);