blob: d9b3faa01feeca2b6d21ce330cc52646ecf910d5 [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 Hovold2ec74a92015-05-04 17:10:41 +020016 int irq;
Johan Hovoldc43960f2015-05-04 17:10:37 +020017};
18
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090019/* lock protects against unexport_gpio() being called while
20 * sysfs files are active.
21 */
22static DEFINE_MUTEX(sysfs_lock);
23
24/*
25 * /sys/class/gpio/gpioN... only for GPIOs that are exported
26 * /direction
27 * * MAY BE OMITTED if kernel won't allow direction changes
28 * * is read/write as "in" or "out"
29 * * may also be written as "high" or "low", initializing
30 * output value as specified ("out" implies "low")
31 * /value
32 * * always readable, subject to hardware behavior
33 * * may be writable, as zero/nonzero
34 * /edge
35 * * configures behavior of poll(2) on /value
36 * * available only if pin can generate IRQs on input
37 * * is read/write as "none", "falling", "rising", or "both"
38 * /active_low
39 * * configures polarity of /value
40 * * is read/write as zero/nonzero
41 * * also affects existing and subsequent "falling" and "rising"
42 * /edge configuration
43 */
44
Johan Hovold6beac9d12015-05-04 17:10:34 +020045static ssize_t direction_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090046 struct device_attribute *attr, char *buf)
47{
Johan Hovoldc43960f2015-05-04 17:10:37 +020048 struct gpiod_data *data = dev_get_drvdata(dev);
49 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090050 ssize_t status;
51
52 mutex_lock(&sysfs_lock);
53
Johan Hovoldf0b78662015-05-04 17:10:36 +020054 gpiod_get_direction(desc);
55 status = sprintf(buf, "%s\n",
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090056 test_bit(FLAG_IS_OUT, &desc->flags)
57 ? "out" : "in");
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090058
59 mutex_unlock(&sysfs_lock);
60 return status;
61}
62
Johan Hovold6beac9d12015-05-04 17:10:34 +020063static ssize_t direction_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090064 struct device_attribute *attr, const char *buf, size_t size)
65{
Johan Hovoldc43960f2015-05-04 17:10:37 +020066 struct gpiod_data *data = dev_get_drvdata(dev);
67 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090068 ssize_t status;
69
70 mutex_lock(&sysfs_lock);
71
Johan Hovoldf0b78662015-05-04 17:10:36 +020072 if (sysfs_streq(buf, "high"))
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090073 status = gpiod_direction_output_raw(desc, 1);
74 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
75 status = gpiod_direction_output_raw(desc, 0);
76 else if (sysfs_streq(buf, "in"))
77 status = gpiod_direction_input(desc);
78 else
79 status = -EINVAL;
80
81 mutex_unlock(&sysfs_lock);
82 return status ? : size;
83}
Johan Hovold6beac9d12015-05-04 17:10:34 +020084static DEVICE_ATTR_RW(direction);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090085
Johan Hovold6beac9d12015-05-04 17:10:34 +020086static ssize_t value_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090087 struct device_attribute *attr, char *buf)
88{
Johan Hovoldc43960f2015-05-04 17:10:37 +020089 struct gpiod_data *data = dev_get_drvdata(dev);
90 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090091 ssize_t status;
92
93 mutex_lock(&sysfs_lock);
94
Johan Hovoldf0b78662015-05-04 17:10:36 +020095 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090096
97 mutex_unlock(&sysfs_lock);
98 return status;
99}
100
Johan Hovold6beac9d12015-05-04 17:10:34 +0200101static ssize_t value_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900102 struct device_attribute *attr, const char *buf, size_t size)
103{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200104 struct gpiod_data *data = dev_get_drvdata(dev);
105 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900106 ssize_t status;
107
108 mutex_lock(&sysfs_lock);
109
Johan Hovoldf0b78662015-05-04 17:10:36 +0200110 if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900111 status = -EPERM;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200112 } else {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900113 long value;
114
115 status = kstrtol(buf, 0, &value);
116 if (status == 0) {
117 gpiod_set_value_cansleep(desc, value);
118 status = size;
119 }
120 }
121
122 mutex_unlock(&sysfs_lock);
123 return status;
124}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200125static DEVICE_ATTR_RW(value);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900126
127static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
128{
Johan Hovolda08f5c22015-05-04 17:10:39 +0200129 struct gpiod_data *data = priv;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900130
Johan Hovolda08f5c22015-05-04 17:10:39 +0200131 sysfs_notify_dirent(data->value_kn);
132
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900133 return IRQ_HANDLED;
134}
135
Johan Hovold2ec74a92015-05-04 17:10:41 +0200136static int gpio_sysfs_request_irq(struct device *dev, unsigned long gpio_flags)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900137{
Johan Hovold0f628502015-05-04 17:10:38 +0200138 struct gpiod_data *data = dev_get_drvdata(dev);
139 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900140 unsigned long irq_flags;
Johan Hovold2ec74a92015-05-04 17:10:41 +0200141 int ret;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900142
Johan Hovold2ec74a92015-05-04 17:10:41 +0200143 data->irq = gpiod_to_irq(desc);
144 if (data->irq < 0)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900145 return -EIO;
146
Johan Hovold2ec74a92015-05-04 17:10:41 +0200147 data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
148 if (!data->value_kn)
149 return -ENODEV;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900150
151 irq_flags = IRQF_SHARED;
152 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
153 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
154 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
155 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
156 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
157 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
158
Johan Hovold52176d02015-05-04 17:10:28 +0200159 /*
160 * FIXME: This should be done in the irq_request_resources callback
161 * when the irq is requested, but a few drivers currently fail
162 * to do so.
163 *
164 * Remove this redundant call (along with the corresponding
165 * unlock) when those drivers have been fixed.
166 */
167 ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900168 if (ret < 0)
Johan Hovold2ec74a92015-05-04 17:10:41 +0200169 goto err_put_kn;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900170
Johan Hovold2ec74a92015-05-04 17:10:41 +0200171 ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
Johan Hovolda08f5c22015-05-04 17:10:39 +0200172 "gpiolib", data);
Johan Hovold52176d02015-05-04 17:10:28 +0200173 if (ret < 0)
174 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900175
176 desc->flags |= gpio_flags;
Johan Hovold2ec74a92015-05-04 17:10:41 +0200177
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900178 return 0;
179
Johan Hovold52176d02015-05-04 17:10:28 +0200180err_unlock:
181 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Johan Hovold2ec74a92015-05-04 17:10:41 +0200182err_put_kn:
183 sysfs_put(data->value_kn);
184
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900185 return ret;
186}
187
Johan Hovold2ec74a92015-05-04 17:10:41 +0200188static void gpio_sysfs_free_irq(struct device *dev)
189{
190 struct gpiod_data *data = dev_get_drvdata(dev);
191 struct gpio_desc *desc = data->desc;
192
193 desc->flags &= ~GPIO_TRIGGER_MASK;
194 free_irq(data->irq, data);
195 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
196 sysfs_put(data->value_kn);
197}
198
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900199static const struct {
200 const char *name;
201 unsigned long flags;
202} trigger_types[] = {
203 { "none", 0 },
204 { "falling", BIT(FLAG_TRIG_FALL) },
205 { "rising", BIT(FLAG_TRIG_RISE) },
206 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
207};
208
Johan Hovold6beac9d12015-05-04 17:10:34 +0200209static ssize_t edge_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900210 struct device_attribute *attr, char *buf)
211{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200212 struct gpiod_data *data = dev_get_drvdata(dev);
213 struct gpio_desc *desc = data->desc;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200214 unsigned long mask;
215 ssize_t status = 0;
216 int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900217
218 mutex_lock(&sysfs_lock);
219
Johan Hovoldf0b78662015-05-04 17:10:36 +0200220 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
221 mask = desc->flags & GPIO_TRIGGER_MASK;
222 if (mask == trigger_types[i].flags) {
223 status = sprintf(buf, "%s\n", trigger_types[i].name);
224 break;
225 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900226 }
227
228 mutex_unlock(&sysfs_lock);
229 return status;
230}
231
Johan Hovold6beac9d12015-05-04 17:10:34 +0200232static ssize_t edge_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900233 struct device_attribute *attr, const char *buf, size_t size)
234{
Johan Hovoldb91e1802015-05-04 17:10:40 +0200235 struct gpiod_data *data = dev_get_drvdata(dev);
236 struct gpio_desc *desc = data->desc;
237 unsigned long flags;
Johan Hovold2ec74a92015-05-04 17:10:41 +0200238 ssize_t status = size;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900239 int i;
240
241 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
242 if (sysfs_streq(trigger_types[i].name, buf))
243 goto found;
244 return -EINVAL;
245
246found:
Johan Hovoldb91e1802015-05-04 17:10:40 +0200247 flags = trigger_types[i].flags;
248
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900249 mutex_lock(&sysfs_lock);
250
Johan Hovoldb91e1802015-05-04 17:10:40 +0200251 if ((desc->flags & GPIO_TRIGGER_MASK) == flags) {
252 status = size;
253 goto out_unlock;
254 }
255
Johan Hovold2ec74a92015-05-04 17:10:41 +0200256 if (desc->flags & GPIO_TRIGGER_MASK)
257 gpio_sysfs_free_irq(dev);
258
259 if (flags) {
260 status = gpio_sysfs_request_irq(dev, flags);
261 if (!status)
262 status = size;
263 }
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 Hovold2ec74a92015-05-04 17:10:41 +0200291 gpio_sysfs_free_irq(dev);
292 status = gpio_sysfs_request_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)
Johan Hovold2ec74a92015-05-04 17:10:41 +0200702 gpio_sysfs_free_irq(dev);
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);