blob: 1161a46618dd3d4958cd22422747218652923e92 [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 Hovold6beac9d2015-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 Hovold6beac9d2015-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 Hovold6beac9d2015-05-04 17:10:34 +020084static DEVICE_ATTR_RW(direction);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090085
Johan Hovold6beac9d2015-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 Hovold6beac9d2015-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 Hovold6beac9d2015-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 Hovold6beac9d2015-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 Hovold6beac9d2015-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;
Johan Hovolde4339ce2015-05-04 17:10:42 +0200239 int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900240
Johan Hovolde4339ce2015-05-04 17:10:42 +0200241 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900242 if (sysfs_streq(trigger_types[i].name, buf))
Johan Hovolde4339ce2015-05-04 17:10:42 +0200243 break;
244 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900245
Johan Hovolde4339ce2015-05-04 17:10:42 +0200246 if (i == ARRAY_SIZE(trigger_types))
247 return -EINVAL;
248
Johan Hovoldb91e1802015-05-04 17:10:40 +0200249 flags = trigger_types[i].flags;
250
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900251 mutex_lock(&sysfs_lock);
252
Johan Hovoldb91e1802015-05-04 17:10:40 +0200253 if ((desc->flags & GPIO_TRIGGER_MASK) == flags) {
254 status = size;
255 goto out_unlock;
256 }
257
Johan Hovold2ec74a92015-05-04 17:10:41 +0200258 if (desc->flags & GPIO_TRIGGER_MASK)
259 gpio_sysfs_free_irq(dev);
260
261 if (flags) {
262 status = gpio_sysfs_request_irq(dev, flags);
263 if (!status)
264 status = size;
265 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900266
Johan Hovoldb91e1802015-05-04 17:10:40 +0200267out_unlock:
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900268 mutex_unlock(&sysfs_lock);
269
270 return status;
271}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200272static DEVICE_ATTR_RW(edge);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900273
Johan Hovold0f628502015-05-04 17:10:38 +0200274static int sysfs_set_active_low(struct device *dev, int value)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900275{
Johan Hovold0f628502015-05-04 17:10:38 +0200276 struct gpiod_data *data = dev_get_drvdata(dev);
277 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900278 int status = 0;
279
280 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
281 return 0;
282
283 if (value)
284 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
285 else
286 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
287
288 /* reconfigure poll(2) support if enabled on one edge only */
Johan Hovold166a85e2015-05-04 17:10:33 +0200289 if (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
290 !!test_bit(FLAG_TRIG_FALL, &desc->flags)) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900291 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
292
Johan Hovold2ec74a92015-05-04 17:10:41 +0200293 gpio_sysfs_free_irq(dev);
294 status = gpio_sysfs_request_irq(dev, trigger_flags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900295 }
296
297 return status;
298}
299
Johan Hovold6beac9d2015-05-04 17:10:34 +0200300static ssize_t active_low_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900301 struct device_attribute *attr, char *buf)
302{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200303 struct gpiod_data *data = dev_get_drvdata(dev);
304 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900305 ssize_t status;
306
307 mutex_lock(&sysfs_lock);
308
Johan Hovoldf0b78662015-05-04 17:10:36 +0200309 status = sprintf(buf, "%d\n",
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900310 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
311
312 mutex_unlock(&sysfs_lock);
313
314 return status;
315}
316
Johan Hovold6beac9d2015-05-04 17:10:34 +0200317static ssize_t active_low_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900318 struct device_attribute *attr, const char *buf, size_t size)
319{
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900320 ssize_t status;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200321 long value;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900322
323 mutex_lock(&sysfs_lock);
324
Johan Hovoldf0b78662015-05-04 17:10:36 +0200325 status = kstrtol(buf, 0, &value);
326 if (status == 0)
Johan Hovold0f628502015-05-04 17:10:38 +0200327 status = sysfs_set_active_low(dev, value != 0);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900328
329 mutex_unlock(&sysfs_lock);
330
331 return status ? : size;
332}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200333static DEVICE_ATTR_RW(active_low);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900334
Johan Hovoldebbeba12015-01-13 13:00:06 +0100335static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
336 int n)
337{
338 struct device *dev = container_of(kobj, struct device, kobj);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200339 struct gpiod_data *data = dev_get_drvdata(dev);
340 struct gpio_desc *desc = data->desc;
Johan Hovoldebbeba12015-01-13 13:00:06 +0100341 umode_t mode = attr->mode;
342 bool show_direction = test_bit(FLAG_SYSFS_DIR, &desc->flags);
343
344 if (attr == &dev_attr_direction.attr) {
345 if (!show_direction)
346 mode = 0;
347 } else if (attr == &dev_attr_edge.attr) {
348 if (gpiod_to_irq(desc) < 0)
349 mode = 0;
350 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
351 mode = 0;
352 }
353
354 return mode;
355}
356
Johan Hovold0915e6f2015-01-13 13:00:05 +0100357static struct attribute *gpio_attrs[] = {
Johan Hovoldebbeba12015-01-13 13:00:06 +0100358 &dev_attr_direction.attr,
359 &dev_attr_edge.attr,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900360 &dev_attr_value.attr,
361 &dev_attr_active_low.attr,
362 NULL,
363};
Johan Hovoldebbeba12015-01-13 13:00:06 +0100364
365static const struct attribute_group gpio_group = {
366 .attrs = gpio_attrs,
367 .is_visible = gpio_is_visible,
368};
369
370static const struct attribute_group *gpio_groups[] = {
371 &gpio_group,
372 NULL
373};
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900374
375/*
376 * /sys/class/gpio/gpiochipN/
377 * /base ... matching gpio_chip.base (N)
378 * /label ... matching gpio_chip.label
379 * /ngpio ... matching gpio_chip.ngpio
380 */
381
Johan Hovold6beac9d2015-05-04 17:10:34 +0200382static ssize_t base_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900383 struct device_attribute *attr, char *buf)
384{
385 const struct gpio_chip *chip = dev_get_drvdata(dev);
386
387 return sprintf(buf, "%d\n", chip->base);
388}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200389static DEVICE_ATTR_RO(base);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900390
Johan Hovold6beac9d2015-05-04 17:10:34 +0200391static ssize_t label_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900392 struct device_attribute *attr, char *buf)
393{
394 const struct gpio_chip *chip = dev_get_drvdata(dev);
395
396 return sprintf(buf, "%s\n", chip->label ? : "");
397}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200398static DEVICE_ATTR_RO(label);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900399
Johan Hovold6beac9d2015-05-04 17:10:34 +0200400static ssize_t ngpio_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900401 struct device_attribute *attr, char *buf)
402{
403 const struct gpio_chip *chip = dev_get_drvdata(dev);
404
405 return sprintf(buf, "%u\n", chip->ngpio);
406}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200407static DEVICE_ATTR_RO(ngpio);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900408
Johan Hovold121b6a72015-01-13 13:00:04 +0100409static struct attribute *gpiochip_attrs[] = {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900410 &dev_attr_base.attr,
411 &dev_attr_label.attr,
412 &dev_attr_ngpio.attr,
413 NULL,
414};
Johan Hovold121b6a72015-01-13 13:00:04 +0100415ATTRIBUTE_GROUPS(gpiochip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900416
417/*
418 * /sys/class/gpio/export ... write-only
419 * integer N ... number of GPIO to export (full access)
420 * /sys/class/gpio/unexport ... write-only
421 * integer N ... number of GPIO to unexport
422 */
423static ssize_t export_store(struct class *class,
424 struct class_attribute *attr,
425 const char *buf, size_t len)
426{
427 long gpio;
428 struct gpio_desc *desc;
429 int status;
430
431 status = kstrtol(buf, 0, &gpio);
432 if (status < 0)
433 goto done;
434
435 desc = gpio_to_desc(gpio);
436 /* reject invalid GPIOs */
437 if (!desc) {
438 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
439 return -EINVAL;
440 }
441
442 /* No extra locking here; FLAG_SYSFS just signifies that the
443 * request and export were done by on behalf of userspace, so
444 * they may be undone on its behalf too.
445 */
446
447 status = gpiod_request(desc, "sysfs");
448 if (status < 0) {
449 if (status == -EPROBE_DEFER)
450 status = -ENODEV;
451 goto done;
452 }
453 status = gpiod_export(desc, true);
454 if (status < 0)
455 gpiod_free(desc);
456 else
457 set_bit(FLAG_SYSFS, &desc->flags);
458
459done:
460 if (status)
461 pr_debug("%s: status %d\n", __func__, status);
462 return status ? : len;
463}
464
465static ssize_t unexport_store(struct class *class,
466 struct class_attribute *attr,
467 const char *buf, size_t len)
468{
469 long gpio;
470 struct gpio_desc *desc;
471 int status;
472
473 status = kstrtol(buf, 0, &gpio);
474 if (status < 0)
475 goto done;
476
477 desc = gpio_to_desc(gpio);
478 /* reject bogus commands (gpio_unexport ignores them) */
479 if (!desc) {
480 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
481 return -EINVAL;
482 }
483
484 status = -EINVAL;
485
486 /* No extra locking here; FLAG_SYSFS just signifies that the
487 * request and export were done by on behalf of userspace, so
488 * they may be undone on its behalf too.
489 */
490 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
491 status = 0;
492 gpiod_free(desc);
493 }
494done:
495 if (status)
496 pr_debug("%s: status %d\n", __func__, status);
497 return status ? : len;
498}
499
500static struct class_attribute gpio_class_attrs[] = {
501 __ATTR(export, 0200, NULL, export_store),
502 __ATTR(unexport, 0200, NULL, unexport_store),
503 __ATTR_NULL,
504};
505
506static struct class gpio_class = {
507 .name = "gpio",
508 .owner = THIS_MODULE,
509
510 .class_attrs = gpio_class_attrs,
511};
512
513
514/**
515 * gpiod_export - export a GPIO through sysfs
516 * @gpio: gpio to make available, already requested
517 * @direction_may_change: true if userspace may change gpio direction
518 * Context: arch_initcall or later
519 *
520 * When drivers want to make a GPIO accessible to userspace after they
521 * have requested it -- perhaps while debugging, or as part of their
522 * public interface -- they may use this routine. If the GPIO can
523 * change direction (some can't) and the caller allows it, userspace
524 * will see "direction" sysfs attribute which may be used to change
525 * the gpio's direction. A "value" attribute will always be provided.
526 *
527 * Returns zero on success, else an error.
528 */
529int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
530{
Johan Hovold483d8212015-04-21 17:42:09 +0200531 struct gpio_chip *chip;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200532 struct gpiod_data *data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900533 unsigned long flags;
534 int status;
535 const char *ioname = NULL;
536 struct device *dev;
537 int offset;
538
539 /* can't export until sysfs is available ... */
540 if (!gpio_class.p) {
541 pr_debug("%s: called too early!\n", __func__);
542 return -ENOENT;
543 }
544
545 if (!desc) {
546 pr_debug("%s: invalid gpio descriptor\n", __func__);
547 return -EINVAL;
548 }
549
Johan Hovold483d8212015-04-21 17:42:09 +0200550 chip = desc->chip;
551
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900552 mutex_lock(&sysfs_lock);
553
Johan Hovold483d8212015-04-21 17:42:09 +0200554 /* check if chip is being removed */
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200555 if (!chip || !chip->cdev) {
Johan Hovold483d8212015-04-21 17:42:09 +0200556 status = -ENODEV;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200557 goto err_unlock;
Johan Hovold483d8212015-04-21 17:42:09 +0200558 }
559
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900560 spin_lock_irqsave(&gpio_lock, flags);
561 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
562 test_bit(FLAG_EXPORT, &desc->flags)) {
563 spin_unlock_irqrestore(&gpio_lock, flags);
564 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
565 __func__,
566 test_bit(FLAG_REQUESTED, &desc->flags),
567 test_bit(FLAG_EXPORT, &desc->flags));
568 status = -EPERM;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200569 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900570 }
571
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200572 if (chip->direction_input && chip->direction_output &&
Johan Hovoldebbeba12015-01-13 13:00:06 +0100573 direction_may_change) {
574 set_bit(FLAG_SYSFS_DIR, &desc->flags);
575 }
576
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900577 spin_unlock_irqrestore(&gpio_lock, flags);
578
Johan Hovoldc43960f2015-05-04 17:10:37 +0200579 data = kzalloc(sizeof(*data), GFP_KERNEL);
580 if (!data) {
581 status = -ENOMEM;
582 goto err_unlock;
583 }
584
585 data->desc = desc;
586
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900587 offset = gpio_chip_hwgpio(desc);
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200588 if (chip->names && chip->names[offset])
589 ioname = chip->names[offset];
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900590
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200591 dev = device_create_with_groups(&gpio_class, chip->dev,
Johan Hovoldc43960f2015-05-04 17:10:37 +0200592 MKDEV(0, 0), data, gpio_groups,
Johan Hovold0915e6f2015-01-13 13:00:05 +0100593 ioname ? ioname : "gpio%u",
594 desc_to_gpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900595 if (IS_ERR(dev)) {
596 status = PTR_ERR(dev);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200597 goto err_free_data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900598 }
599
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900600 set_bit(FLAG_EXPORT, &desc->flags);
601 mutex_unlock(&sysfs_lock);
602 return 0;
603
Johan Hovoldc43960f2015-05-04 17:10:37 +0200604err_free_data:
605 kfree(data);
606err_unlock:
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900607 mutex_unlock(&sysfs_lock);
608 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
609 return status;
610}
611EXPORT_SYMBOL_GPL(gpiod_export);
612
Johan Hovoldc43960f2015-05-04 17:10:37 +0200613static int match_export(struct device *dev, const void *desc)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900614{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200615 struct gpiod_data *data = dev_get_drvdata(dev);
616
617 return data->desc == desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900618}
619
620/**
621 * gpiod_export_link - create a sysfs link to an exported GPIO node
622 * @dev: device under which to create symlink
623 * @name: name of the symlink
624 * @gpio: gpio to create symlink to, already exported
625 *
626 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
627 * node. Caller is responsible for unlinking.
628 *
629 * Returns zero on success, else an error.
630 */
631int gpiod_export_link(struct device *dev, const char *name,
632 struct gpio_desc *desc)
633{
634 int status = -EINVAL;
635
636 if (!desc) {
637 pr_warn("%s: invalid GPIO\n", __func__);
638 return -EINVAL;
639 }
640
641 mutex_lock(&sysfs_lock);
642
643 if (test_bit(FLAG_EXPORT, &desc->flags)) {
644 struct device *tdev;
645
646 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
647 if (tdev != NULL) {
648 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
649 name);
Johan Hovold0f303db2015-01-26 12:02:45 +0100650 put_device(tdev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900651 } else {
652 status = -ENODEV;
653 }
654 }
655
656 mutex_unlock(&sysfs_lock);
657
658 if (status)
659 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
660
661 return status;
662}
663EXPORT_SYMBOL_GPL(gpiod_export_link);
664
665/**
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900666 * gpiod_unexport - reverse effect of gpio_export()
667 * @gpio: gpio to make unavailable
668 *
669 * This is implicit on gpio_free().
670 */
671void gpiod_unexport(struct gpio_desc *desc)
672{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200673 struct gpiod_data *data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900674 int status = 0;
675 struct device *dev = NULL;
676
677 if (!desc) {
678 pr_warn("%s: invalid GPIO\n", __func__);
679 return;
680 }
681
682 mutex_lock(&sysfs_lock);
683
684 if (test_bit(FLAG_EXPORT, &desc->flags)) {
685
686 dev = class_find_device(&gpio_class, NULL, desc, match_export);
687 if (dev) {
Johan Hovoldebbeba12015-01-13 13:00:06 +0100688 clear_bit(FLAG_SYSFS_DIR, &desc->flags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900689 clear_bit(FLAG_EXPORT, &desc->flags);
690 } else
691 status = -ENODEV;
692 }
693
694 mutex_unlock(&sysfs_lock);
695
696 if (dev) {
Johan Hovoldc43960f2015-05-04 17:10:37 +0200697 data = dev_get_drvdata(dev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900698 device_unregister(dev);
Johan Hovold54d9acd2015-05-04 17:10:35 +0200699 /*
700 * Release irq after deregistration to prevent race with
701 * edge_store.
702 */
Johan Hovoldb91e1802015-05-04 17:10:40 +0200703 if (desc->flags & GPIO_TRIGGER_MASK)
Johan Hovold2ec74a92015-05-04 17:10:41 +0200704 gpio_sysfs_free_irq(dev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900705 put_device(dev);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200706 kfree(data);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900707 }
708
709 if (status)
710 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
711}
712EXPORT_SYMBOL_GPL(gpiod_unexport);
713
Johan Hovold426577b2015-05-04 17:10:32 +0200714int gpiochip_sysfs_register(struct gpio_chip *chip)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900715{
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900716 struct device *dev;
717
Johan Hovold426577b2015-05-04 17:10:32 +0200718 /*
719 * Many systems add gpio chips for SOC support very early,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900720 * before driver model support is available. In those cases we
Johan Hovold426577b2015-05-04 17:10:32 +0200721 * register later, in gpiolib_sysfs_init() ... here we just
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900722 * verify that _some_ field of gpio_class got initialized.
723 */
724 if (!gpio_class.p)
725 return 0;
726
727 /* use chip->base for the ID; it's already known to be unique */
Johan Hovold121b6a72015-01-13 13:00:04 +0100728 dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0),
729 chip, gpiochip_groups,
730 "gpiochip%d", chip->base);
731 if (IS_ERR(dev))
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200732 return PTR_ERR(dev);
Johan Hovold3ff74be2015-05-04 17:10:30 +0200733
734 mutex_lock(&sysfs_lock);
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200735 chip->cdev = dev;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900736 mutex_unlock(&sysfs_lock);
737
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200738 return 0;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900739}
740
Johan Hovold426577b2015-05-04 17:10:32 +0200741void gpiochip_sysfs_unregister(struct gpio_chip *chip)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900742{
Johan Hovold483d8212015-04-21 17:42:09 +0200743 struct gpio_desc *desc;
744 unsigned int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900745
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200746 if (!chip->cdev)
747 return;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900748
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200749 device_unregister(chip->cdev);
750
751 /* prevent further gpiod exports */
752 mutex_lock(&sysfs_lock);
753 chip->cdev = NULL;
754 mutex_unlock(&sysfs_lock);
Johan Hovold483d8212015-04-21 17:42:09 +0200755
756 /* unregister gpiod class devices owned by sysfs */
757 for (i = 0; i < chip->ngpio; i++) {
758 desc = &chip->desc[i];
759 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
760 gpiod_free(desc);
761 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900762}
763
764static int __init gpiolib_sysfs_init(void)
765{
766 int status;
767 unsigned long flags;
768 struct gpio_chip *chip;
769
770 status = class_register(&gpio_class);
771 if (status < 0)
772 return status;
773
774 /* Scan and register the gpio_chips which registered very
775 * early (e.g. before the class_register above was called).
776 *
777 * We run before arch_initcall() so chip->dev nodes can have
778 * registered, and so arch_initcall() can always gpio_export().
779 */
780 spin_lock_irqsave(&gpio_lock, flags);
781 list_for_each_entry(chip, &gpio_chips, list) {
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200782 if (chip->cdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900783 continue;
784
Alexandre Courbot14141a92014-07-22 16:17:40 +0900785 /*
Johan Hovold426577b2015-05-04 17:10:32 +0200786 * TODO we yield gpio_lock here because
787 * gpiochip_sysfs_register() acquires a mutex. This is unsafe
788 * and needs to be fixed.
Alexandre Courbot14141a92014-07-22 16:17:40 +0900789 *
790 * Also it would be nice to use gpiochip_find() here so we
791 * can keep gpio_chips local to gpiolib.c, but the yield of
792 * gpio_lock prevents us from doing this.
793 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900794 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold426577b2015-05-04 17:10:32 +0200795 status = gpiochip_sysfs_register(chip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900796 spin_lock_irqsave(&gpio_lock, flags);
797 }
798 spin_unlock_irqrestore(&gpio_lock, flags);
799
800
801 return status;
802}
803postcore_initcall(gpiolib_sysfs_init);