blob: 1bb05aa33a8485022ae3dba6776d0630e012b31b [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 Hovold6ffcb792015-05-04 17:10:44 +020015
16 struct mutex mutex;
Johan Hovolda08f5c22015-05-04 17:10:39 +020017 struct kernfs_node *value_kn;
Johan Hovold2ec74a92015-05-04 17:10:41 +020018 int irq;
Johan Hovoldc43960f2015-05-04 17:10:37 +020019};
20
Johan Hovold6ffcb792015-05-04 17:10:44 +020021/*
22 * Lock to serialise gpiod export and unexport, and prevent re-export of
23 * gpiod whose chip is being unregistered.
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090024 */
25static DEFINE_MUTEX(sysfs_lock);
26
27/*
28 * /sys/class/gpio/gpioN... only for GPIOs that are exported
29 * /direction
30 * * MAY BE OMITTED if kernel won't allow direction changes
31 * * is read/write as "in" or "out"
32 * * may also be written as "high" or "low", initializing
33 * output value as specified ("out" implies "low")
34 * /value
35 * * always readable, subject to hardware behavior
36 * * may be writable, as zero/nonzero
37 * /edge
38 * * configures behavior of poll(2) on /value
39 * * available only if pin can generate IRQs on input
40 * * is read/write as "none", "falling", "rising", or "both"
41 * /active_low
42 * * configures polarity of /value
43 * * is read/write as zero/nonzero
44 * * also affects existing and subsequent "falling" and "rising"
45 * /edge configuration
46 */
47
Johan Hovold6beac9d12015-05-04 17:10:34 +020048static ssize_t direction_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090049 struct device_attribute *attr, char *buf)
50{
Johan Hovoldc43960f2015-05-04 17:10:37 +020051 struct gpiod_data *data = dev_get_drvdata(dev);
52 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090053 ssize_t status;
54
Johan Hovold6ffcb792015-05-04 17:10:44 +020055 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090056
Johan Hovoldf0b78662015-05-04 17:10:36 +020057 gpiod_get_direction(desc);
58 status = sprintf(buf, "%s\n",
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090059 test_bit(FLAG_IS_OUT, &desc->flags)
60 ? "out" : "in");
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090061
Johan Hovold6ffcb792015-05-04 17:10:44 +020062 mutex_unlock(&data->mutex);
63
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090064 return status;
65}
66
Johan Hovold6beac9d12015-05-04 17:10:34 +020067static ssize_t direction_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090068 struct device_attribute *attr, const char *buf, size_t size)
69{
Johan Hovoldc43960f2015-05-04 17:10:37 +020070 struct gpiod_data *data = dev_get_drvdata(dev);
71 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090072 ssize_t status;
73
Johan Hovold6ffcb792015-05-04 17:10:44 +020074 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090075
Johan Hovoldf0b78662015-05-04 17:10:36 +020076 if (sysfs_streq(buf, "high"))
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090077 status = gpiod_direction_output_raw(desc, 1);
78 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
79 status = gpiod_direction_output_raw(desc, 0);
80 else if (sysfs_streq(buf, "in"))
81 status = gpiod_direction_input(desc);
82 else
83 status = -EINVAL;
84
Johan Hovold6ffcb792015-05-04 17:10:44 +020085 mutex_unlock(&data->mutex);
86
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090087 return status ? : size;
88}
Johan Hovold6beac9d12015-05-04 17:10:34 +020089static DEVICE_ATTR_RW(direction);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090090
Johan Hovold6beac9d12015-05-04 17:10:34 +020091static ssize_t value_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090092 struct device_attribute *attr, char *buf)
93{
Johan Hovoldc43960f2015-05-04 17:10:37 +020094 struct gpiod_data *data = dev_get_drvdata(dev);
95 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090096 ssize_t status;
97
Johan Hovold6ffcb792015-05-04 17:10:44 +020098 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090099
Johan Hovoldf0b78662015-05-04 17:10:36 +0200100 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900101
Johan Hovold6ffcb792015-05-04 17:10:44 +0200102 mutex_unlock(&data->mutex);
103
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900104 return status;
105}
106
Johan Hovold6beac9d12015-05-04 17:10:34 +0200107static ssize_t value_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900108 struct device_attribute *attr, const char *buf, size_t size)
109{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200110 struct gpiod_data *data = dev_get_drvdata(dev);
111 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900112 ssize_t status;
113
Johan Hovold6ffcb792015-05-04 17:10:44 +0200114 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900115
Johan Hovoldf0b78662015-05-04 17:10:36 +0200116 if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900117 status = -EPERM;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200118 } else {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900119 long value;
120
121 status = kstrtol(buf, 0, &value);
122 if (status == 0) {
123 gpiod_set_value_cansleep(desc, value);
124 status = size;
125 }
126 }
127
Johan Hovold6ffcb792015-05-04 17:10:44 +0200128 mutex_unlock(&data->mutex);
129
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900130 return status;
131}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200132static DEVICE_ATTR_RW(value);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900133
134static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
135{
Johan Hovolda08f5c22015-05-04 17:10:39 +0200136 struct gpiod_data *data = priv;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900137
Johan Hovolda08f5c22015-05-04 17:10:39 +0200138 sysfs_notify_dirent(data->value_kn);
139
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900140 return IRQ_HANDLED;
141}
142
Johan Hovold6ffcb792015-05-04 17:10:44 +0200143/* Caller holds gpiod-data mutex. */
Johan Hovold2ec74a92015-05-04 17:10:41 +0200144static int gpio_sysfs_request_irq(struct device *dev, unsigned long gpio_flags)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900145{
Johan Hovold0f628502015-05-04 17:10:38 +0200146 struct gpiod_data *data = dev_get_drvdata(dev);
147 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900148 unsigned long irq_flags;
Johan Hovold2ec74a92015-05-04 17:10:41 +0200149 int ret;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900150
Johan Hovold2ec74a92015-05-04 17:10:41 +0200151 data->irq = gpiod_to_irq(desc);
152 if (data->irq < 0)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900153 return -EIO;
154
Johan Hovold2ec74a92015-05-04 17:10:41 +0200155 data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
156 if (!data->value_kn)
157 return -ENODEV;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900158
159 irq_flags = IRQF_SHARED;
160 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
161 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
162 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
163 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
164 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
165 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
166
Johan Hovold52176d02015-05-04 17:10:28 +0200167 /*
168 * FIXME: This should be done in the irq_request_resources callback
169 * when the irq is requested, but a few drivers currently fail
170 * to do so.
171 *
172 * Remove this redundant call (along with the corresponding
173 * unlock) when those drivers have been fixed.
174 */
175 ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900176 if (ret < 0)
Johan Hovold2ec74a92015-05-04 17:10:41 +0200177 goto err_put_kn;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900178
Johan Hovold2ec74a92015-05-04 17:10:41 +0200179 ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
Johan Hovolda08f5c22015-05-04 17:10:39 +0200180 "gpiolib", data);
Johan Hovold52176d02015-05-04 17:10:28 +0200181 if (ret < 0)
182 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900183
184 desc->flags |= gpio_flags;
Johan Hovold2ec74a92015-05-04 17:10:41 +0200185
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900186 return 0;
187
Johan Hovold52176d02015-05-04 17:10:28 +0200188err_unlock:
189 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Johan Hovold2ec74a92015-05-04 17:10:41 +0200190err_put_kn:
191 sysfs_put(data->value_kn);
192
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900193 return ret;
194}
195
Johan Hovold6ffcb792015-05-04 17:10:44 +0200196/*
197 * Caller holds gpiod-data mutex (unless called after class-device
198 * deregistration).
199 */
Johan Hovold2ec74a92015-05-04 17:10:41 +0200200static void gpio_sysfs_free_irq(struct device *dev)
201{
202 struct gpiod_data *data = dev_get_drvdata(dev);
203 struct gpio_desc *desc = data->desc;
204
205 desc->flags &= ~GPIO_TRIGGER_MASK;
206 free_irq(data->irq, data);
207 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
208 sysfs_put(data->value_kn);
209}
210
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900211static const struct {
212 const char *name;
213 unsigned long flags;
214} trigger_types[] = {
215 { "none", 0 },
216 { "falling", BIT(FLAG_TRIG_FALL) },
217 { "rising", BIT(FLAG_TRIG_RISE) },
218 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
219};
220
Johan Hovold6beac9d12015-05-04 17:10:34 +0200221static ssize_t edge_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900222 struct device_attribute *attr, char *buf)
223{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200224 struct gpiod_data *data = dev_get_drvdata(dev);
225 struct gpio_desc *desc = data->desc;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200226 unsigned long mask;
227 ssize_t status = 0;
228 int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900229
Johan Hovold6ffcb792015-05-04 17:10:44 +0200230 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900231
Johan Hovoldf0b78662015-05-04 17:10:36 +0200232 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
233 mask = desc->flags & GPIO_TRIGGER_MASK;
234 if (mask == trigger_types[i].flags) {
235 status = sprintf(buf, "%s\n", trigger_types[i].name);
236 break;
237 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900238 }
239
Johan Hovold6ffcb792015-05-04 17:10:44 +0200240 mutex_unlock(&data->mutex);
241
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900242 return status;
243}
244
Johan Hovold6beac9d12015-05-04 17:10:34 +0200245static ssize_t edge_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900246 struct device_attribute *attr, const char *buf, size_t size)
247{
Johan Hovoldb91e1802015-05-04 17:10:40 +0200248 struct gpiod_data *data = dev_get_drvdata(dev);
249 struct gpio_desc *desc = data->desc;
250 unsigned long flags;
Johan Hovold2ec74a92015-05-04 17:10:41 +0200251 ssize_t status = size;
Johan Hovolde4339ce2015-05-04 17:10:42 +0200252 int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900253
Johan Hovolde4339ce2015-05-04 17:10:42 +0200254 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900255 if (sysfs_streq(trigger_types[i].name, buf))
Johan Hovolde4339ce2015-05-04 17:10:42 +0200256 break;
257 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900258
Johan Hovolde4339ce2015-05-04 17:10:42 +0200259 if (i == ARRAY_SIZE(trigger_types))
260 return -EINVAL;
261
Johan Hovoldb91e1802015-05-04 17:10:40 +0200262 flags = trigger_types[i].flags;
263
Johan Hovold6ffcb792015-05-04 17:10:44 +0200264 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900265
Johan Hovoldb91e1802015-05-04 17:10:40 +0200266 if ((desc->flags & GPIO_TRIGGER_MASK) == flags) {
267 status = size;
268 goto out_unlock;
269 }
270
Johan Hovold2ec74a92015-05-04 17:10:41 +0200271 if (desc->flags & GPIO_TRIGGER_MASK)
272 gpio_sysfs_free_irq(dev);
273
274 if (flags) {
275 status = gpio_sysfs_request_irq(dev, flags);
276 if (!status)
277 status = size;
278 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900279
Johan Hovoldb91e1802015-05-04 17:10:40 +0200280out_unlock:
Johan Hovold6ffcb792015-05-04 17:10:44 +0200281 mutex_unlock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900282
283 return status;
284}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200285static DEVICE_ATTR_RW(edge);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900286
Johan Hovold6ffcb792015-05-04 17:10:44 +0200287/* Caller holds gpiod-data mutex. */
Johan Hovold0f628502015-05-04 17:10:38 +0200288static int sysfs_set_active_low(struct device *dev, int value)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900289{
Johan Hovold0f628502015-05-04 17:10:38 +0200290 struct gpiod_data *data = dev_get_drvdata(dev);
291 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900292 int status = 0;
293
294 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
295 return 0;
296
297 if (value)
298 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
299 else
300 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
301
302 /* reconfigure poll(2) support if enabled on one edge only */
Johan Hovold166a85e2015-05-04 17:10:33 +0200303 if (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
304 !!test_bit(FLAG_TRIG_FALL, &desc->flags)) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900305 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
306
Johan Hovold2ec74a92015-05-04 17:10:41 +0200307 gpio_sysfs_free_irq(dev);
308 status = gpio_sysfs_request_irq(dev, trigger_flags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900309 }
310
311 return status;
312}
313
Johan Hovold6beac9d12015-05-04 17:10:34 +0200314static ssize_t active_low_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900315 struct device_attribute *attr, char *buf)
316{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200317 struct gpiod_data *data = dev_get_drvdata(dev);
318 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900319 ssize_t status;
320
Johan Hovold6ffcb792015-05-04 17:10:44 +0200321 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900322
Johan Hovoldf0b78662015-05-04 17:10:36 +0200323 status = sprintf(buf, "%d\n",
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900324 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
325
Johan Hovold6ffcb792015-05-04 17:10:44 +0200326 mutex_unlock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900327
328 return status;
329}
330
Johan Hovold6beac9d12015-05-04 17:10:34 +0200331static ssize_t active_low_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900332 struct device_attribute *attr, const char *buf, size_t size)
333{
Johan Hovold6ffcb792015-05-04 17:10:44 +0200334 struct gpiod_data *data = dev_get_drvdata(dev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900335 ssize_t status;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200336 long value;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900337
Johan Hovold6ffcb792015-05-04 17:10:44 +0200338 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900339
Johan Hovoldf0b78662015-05-04 17:10:36 +0200340 status = kstrtol(buf, 0, &value);
341 if (status == 0)
Johan Hovold0f628502015-05-04 17:10:38 +0200342 status = sysfs_set_active_low(dev, value != 0);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900343
Johan Hovold6ffcb792015-05-04 17:10:44 +0200344 mutex_unlock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900345
346 return status ? : size;
347}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200348static DEVICE_ATTR_RW(active_low);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900349
Johan Hovoldebbeba12015-01-13 13:00:06 +0100350static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
351 int n)
352{
353 struct device *dev = container_of(kobj, struct device, kobj);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200354 struct gpiod_data *data = dev_get_drvdata(dev);
355 struct gpio_desc *desc = data->desc;
Johan Hovoldebbeba12015-01-13 13:00:06 +0100356 umode_t mode = attr->mode;
357 bool show_direction = test_bit(FLAG_SYSFS_DIR, &desc->flags);
358
359 if (attr == &dev_attr_direction.attr) {
360 if (!show_direction)
361 mode = 0;
362 } else if (attr == &dev_attr_edge.attr) {
363 if (gpiod_to_irq(desc) < 0)
364 mode = 0;
365 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
366 mode = 0;
367 }
368
369 return mode;
370}
371
Johan Hovold0915e6f2015-01-13 13:00:05 +0100372static struct attribute *gpio_attrs[] = {
Johan Hovoldebbeba12015-01-13 13:00:06 +0100373 &dev_attr_direction.attr,
374 &dev_attr_edge.attr,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900375 &dev_attr_value.attr,
376 &dev_attr_active_low.attr,
377 NULL,
378};
Johan Hovoldebbeba12015-01-13 13:00:06 +0100379
380static const struct attribute_group gpio_group = {
381 .attrs = gpio_attrs,
382 .is_visible = gpio_is_visible,
383};
384
385static const struct attribute_group *gpio_groups[] = {
386 &gpio_group,
387 NULL
388};
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900389
390/*
391 * /sys/class/gpio/gpiochipN/
392 * /base ... matching gpio_chip.base (N)
393 * /label ... matching gpio_chip.label
394 * /ngpio ... matching gpio_chip.ngpio
395 */
396
Johan Hovold6beac9d12015-05-04 17:10:34 +0200397static ssize_t base_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900398 struct device_attribute *attr, char *buf)
399{
400 const struct gpio_chip *chip = dev_get_drvdata(dev);
401
402 return sprintf(buf, "%d\n", chip->base);
403}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200404static DEVICE_ATTR_RO(base);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900405
Johan Hovold6beac9d12015-05-04 17:10:34 +0200406static ssize_t label_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900407 struct device_attribute *attr, char *buf)
408{
409 const struct gpio_chip *chip = dev_get_drvdata(dev);
410
411 return sprintf(buf, "%s\n", chip->label ? : "");
412}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200413static DEVICE_ATTR_RO(label);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900414
Johan Hovold6beac9d12015-05-04 17:10:34 +0200415static ssize_t ngpio_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900416 struct device_attribute *attr, char *buf)
417{
418 const struct gpio_chip *chip = dev_get_drvdata(dev);
419
420 return sprintf(buf, "%u\n", chip->ngpio);
421}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200422static DEVICE_ATTR_RO(ngpio);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900423
Johan Hovold121b6a72015-01-13 13:00:04 +0100424static struct attribute *gpiochip_attrs[] = {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900425 &dev_attr_base.attr,
426 &dev_attr_label.attr,
427 &dev_attr_ngpio.attr,
428 NULL,
429};
Johan Hovold121b6a72015-01-13 13:00:04 +0100430ATTRIBUTE_GROUPS(gpiochip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900431
432/*
433 * /sys/class/gpio/export ... write-only
434 * integer N ... number of GPIO to export (full access)
435 * /sys/class/gpio/unexport ... write-only
436 * integer N ... number of GPIO to unexport
437 */
438static ssize_t export_store(struct class *class,
439 struct class_attribute *attr,
440 const char *buf, size_t len)
441{
442 long gpio;
443 struct gpio_desc *desc;
444 int status;
445
446 status = kstrtol(buf, 0, &gpio);
447 if (status < 0)
448 goto done;
449
450 desc = gpio_to_desc(gpio);
451 /* reject invalid GPIOs */
452 if (!desc) {
453 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
454 return -EINVAL;
455 }
456
457 /* No extra locking here; FLAG_SYSFS just signifies that the
458 * request and export were done by on behalf of userspace, so
459 * they may be undone on its behalf too.
460 */
461
462 status = gpiod_request(desc, "sysfs");
463 if (status < 0) {
464 if (status == -EPROBE_DEFER)
465 status = -ENODEV;
466 goto done;
467 }
468 status = gpiod_export(desc, true);
469 if (status < 0)
470 gpiod_free(desc);
471 else
472 set_bit(FLAG_SYSFS, &desc->flags);
473
474done:
475 if (status)
476 pr_debug("%s: status %d\n", __func__, status);
477 return status ? : len;
478}
479
480static ssize_t unexport_store(struct class *class,
481 struct class_attribute *attr,
482 const char *buf, size_t len)
483{
484 long gpio;
485 struct gpio_desc *desc;
486 int status;
487
488 status = kstrtol(buf, 0, &gpio);
489 if (status < 0)
490 goto done;
491
492 desc = gpio_to_desc(gpio);
493 /* reject bogus commands (gpio_unexport ignores them) */
494 if (!desc) {
495 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
496 return -EINVAL;
497 }
498
499 status = -EINVAL;
500
501 /* No extra locking here; FLAG_SYSFS just signifies that the
502 * request and export were done by on behalf of userspace, so
503 * they may be undone on its behalf too.
504 */
505 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
506 status = 0;
507 gpiod_free(desc);
508 }
509done:
510 if (status)
511 pr_debug("%s: status %d\n", __func__, status);
512 return status ? : len;
513}
514
515static struct class_attribute gpio_class_attrs[] = {
516 __ATTR(export, 0200, NULL, export_store),
517 __ATTR(unexport, 0200, NULL, unexport_store),
518 __ATTR_NULL,
519};
520
521static struct class gpio_class = {
522 .name = "gpio",
523 .owner = THIS_MODULE,
524
525 .class_attrs = gpio_class_attrs,
526};
527
528
529/**
530 * gpiod_export - export a GPIO through sysfs
531 * @gpio: gpio to make available, already requested
532 * @direction_may_change: true if userspace may change gpio direction
533 * Context: arch_initcall or later
534 *
535 * When drivers want to make a GPIO accessible to userspace after they
536 * have requested it -- perhaps while debugging, or as part of their
537 * public interface -- they may use this routine. If the GPIO can
538 * change direction (some can't) and the caller allows it, userspace
539 * will see "direction" sysfs attribute which may be used to change
540 * the gpio's direction. A "value" attribute will always be provided.
541 *
542 * Returns zero on success, else an error.
543 */
544int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
545{
Johan Hovold483d8212015-04-21 17:42:09 +0200546 struct gpio_chip *chip;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200547 struct gpiod_data *data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900548 unsigned long flags;
549 int status;
550 const char *ioname = NULL;
551 struct device *dev;
552 int offset;
553
554 /* can't export until sysfs is available ... */
555 if (!gpio_class.p) {
556 pr_debug("%s: called too early!\n", __func__);
557 return -ENOENT;
558 }
559
560 if (!desc) {
561 pr_debug("%s: invalid gpio descriptor\n", __func__);
562 return -EINVAL;
563 }
564
Johan Hovold483d8212015-04-21 17:42:09 +0200565 chip = desc->chip;
566
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900567 mutex_lock(&sysfs_lock);
568
Johan Hovold483d8212015-04-21 17:42:09 +0200569 /* check if chip is being removed */
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200570 if (!chip || !chip->cdev) {
Johan Hovold483d8212015-04-21 17:42:09 +0200571 status = -ENODEV;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200572 goto err_unlock;
Johan Hovold483d8212015-04-21 17:42:09 +0200573 }
574
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900575 spin_lock_irqsave(&gpio_lock, flags);
576 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
577 test_bit(FLAG_EXPORT, &desc->flags)) {
578 spin_unlock_irqrestore(&gpio_lock, flags);
579 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
580 __func__,
581 test_bit(FLAG_REQUESTED, &desc->flags),
582 test_bit(FLAG_EXPORT, &desc->flags));
583 status = -EPERM;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200584 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900585 }
586
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200587 if (chip->direction_input && chip->direction_output &&
Johan Hovoldebbeba12015-01-13 13:00:06 +0100588 direction_may_change) {
589 set_bit(FLAG_SYSFS_DIR, &desc->flags);
590 }
591
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900592 spin_unlock_irqrestore(&gpio_lock, flags);
593
Johan Hovoldc43960f2015-05-04 17:10:37 +0200594 data = kzalloc(sizeof(*data), GFP_KERNEL);
595 if (!data) {
596 status = -ENOMEM;
597 goto err_unlock;
598 }
599
600 data->desc = desc;
Johan Hovold6ffcb792015-05-04 17:10:44 +0200601 mutex_init(&data->mutex);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200602
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900603 offset = gpio_chip_hwgpio(desc);
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200604 if (chip->names && chip->names[offset])
605 ioname = chip->names[offset];
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900606
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200607 dev = device_create_with_groups(&gpio_class, chip->dev,
Johan Hovoldc43960f2015-05-04 17:10:37 +0200608 MKDEV(0, 0), data, gpio_groups,
Johan Hovold0915e6f2015-01-13 13:00:05 +0100609 ioname ? ioname : "gpio%u",
610 desc_to_gpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900611 if (IS_ERR(dev)) {
612 status = PTR_ERR(dev);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200613 goto err_free_data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900614 }
615
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900616 set_bit(FLAG_EXPORT, &desc->flags);
617 mutex_unlock(&sysfs_lock);
618 return 0;
619
Johan Hovoldc43960f2015-05-04 17:10:37 +0200620err_free_data:
621 kfree(data);
622err_unlock:
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900623 mutex_unlock(&sysfs_lock);
624 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
625 return status;
626}
627EXPORT_SYMBOL_GPL(gpiod_export);
628
Johan Hovoldc43960f2015-05-04 17:10:37 +0200629static int match_export(struct device *dev, const void *desc)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900630{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200631 struct gpiod_data *data = dev_get_drvdata(dev);
632
633 return data->desc == desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900634}
635
636/**
637 * gpiod_export_link - create a sysfs link to an exported GPIO node
638 * @dev: device under which to create symlink
639 * @name: name of the symlink
640 * @gpio: gpio to create symlink to, already exported
641 *
642 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
643 * node. Caller is responsible for unlinking.
644 *
645 * Returns zero on success, else an error.
646 */
647int gpiod_export_link(struct device *dev, const char *name,
648 struct gpio_desc *desc)
649{
Johan Hovold56d30ec2015-05-04 17:10:43 +0200650 struct device *cdev;
651 int ret;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900652
653 if (!desc) {
654 pr_warn("%s: invalid GPIO\n", __func__);
655 return -EINVAL;
656 }
657
Johan Hovold56d30ec2015-05-04 17:10:43 +0200658 cdev = class_find_device(&gpio_class, NULL, desc, match_export);
659 if (!cdev)
660 return -ENODEV;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900661
Johan Hovold56d30ec2015-05-04 17:10:43 +0200662 ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
663 put_device(cdev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900664
Johan Hovold56d30ec2015-05-04 17:10:43 +0200665 return ret;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900666}
667EXPORT_SYMBOL_GPL(gpiod_export_link);
668
669/**
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900670 * gpiod_unexport - reverse effect of gpio_export()
671 * @gpio: gpio to make unavailable
672 *
673 * This is implicit on gpio_free().
674 */
675void gpiod_unexport(struct gpio_desc *desc)
676{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200677 struct gpiod_data *data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900678 int status = 0;
679 struct device *dev = NULL;
680
681 if (!desc) {
682 pr_warn("%s: invalid GPIO\n", __func__);
683 return;
684 }
685
686 mutex_lock(&sysfs_lock);
687
688 if (test_bit(FLAG_EXPORT, &desc->flags)) {
689
690 dev = class_find_device(&gpio_class, NULL, desc, match_export);
691 if (dev) {
Johan Hovoldebbeba12015-01-13 13:00:06 +0100692 clear_bit(FLAG_SYSFS_DIR, &desc->flags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900693 clear_bit(FLAG_EXPORT, &desc->flags);
694 } else
695 status = -ENODEV;
696 }
697
698 mutex_unlock(&sysfs_lock);
699
700 if (dev) {
Johan Hovoldc43960f2015-05-04 17:10:37 +0200701 data = dev_get_drvdata(dev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900702 device_unregister(dev);
Johan Hovold54d9acd2015-05-04 17:10:35 +0200703 /*
704 * Release irq after deregistration to prevent race with
705 * edge_store.
706 */
Johan Hovoldb91e1802015-05-04 17:10:40 +0200707 if (desc->flags & GPIO_TRIGGER_MASK)
Johan Hovold2ec74a92015-05-04 17:10:41 +0200708 gpio_sysfs_free_irq(dev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900709 put_device(dev);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200710 kfree(data);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900711 }
712
713 if (status)
714 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
715}
716EXPORT_SYMBOL_GPL(gpiod_unexport);
717
Johan Hovold426577b2015-05-04 17:10:32 +0200718int gpiochip_sysfs_register(struct gpio_chip *chip)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900719{
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900720 struct device *dev;
721
Johan Hovold426577b2015-05-04 17:10:32 +0200722 /*
723 * Many systems add gpio chips for SOC support very early,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900724 * before driver model support is available. In those cases we
Johan Hovold426577b2015-05-04 17:10:32 +0200725 * register later, in gpiolib_sysfs_init() ... here we just
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900726 * verify that _some_ field of gpio_class got initialized.
727 */
728 if (!gpio_class.p)
729 return 0;
730
731 /* use chip->base for the ID; it's already known to be unique */
Johan Hovold121b6a72015-01-13 13:00:04 +0100732 dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0),
733 chip, gpiochip_groups,
734 "gpiochip%d", chip->base);
735 if (IS_ERR(dev))
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200736 return PTR_ERR(dev);
Johan Hovold3ff74be2015-05-04 17:10:30 +0200737
738 mutex_lock(&sysfs_lock);
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200739 chip->cdev = dev;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900740 mutex_unlock(&sysfs_lock);
741
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200742 return 0;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900743}
744
Johan Hovold426577b2015-05-04 17:10:32 +0200745void gpiochip_sysfs_unregister(struct gpio_chip *chip)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900746{
Johan Hovold483d8212015-04-21 17:42:09 +0200747 struct gpio_desc *desc;
748 unsigned int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900749
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200750 if (!chip->cdev)
751 return;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900752
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200753 device_unregister(chip->cdev);
754
755 /* prevent further gpiod exports */
756 mutex_lock(&sysfs_lock);
757 chip->cdev = NULL;
758 mutex_unlock(&sysfs_lock);
Johan Hovold483d8212015-04-21 17:42:09 +0200759
760 /* unregister gpiod class devices owned by sysfs */
761 for (i = 0; i < chip->ngpio; i++) {
762 desc = &chip->desc[i];
763 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
764 gpiod_free(desc);
765 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900766}
767
768static int __init gpiolib_sysfs_init(void)
769{
770 int status;
771 unsigned long flags;
772 struct gpio_chip *chip;
773
774 status = class_register(&gpio_class);
775 if (status < 0)
776 return status;
777
778 /* Scan and register the gpio_chips which registered very
779 * early (e.g. before the class_register above was called).
780 *
781 * We run before arch_initcall() so chip->dev nodes can have
782 * registered, and so arch_initcall() can always gpio_export().
783 */
784 spin_lock_irqsave(&gpio_lock, flags);
785 list_for_each_entry(chip, &gpio_chips, list) {
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200786 if (chip->cdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900787 continue;
788
Alexandre Courbot14141a92014-07-22 16:17:40 +0900789 /*
Johan Hovold426577b2015-05-04 17:10:32 +0200790 * TODO we yield gpio_lock here because
791 * gpiochip_sysfs_register() acquires a mutex. This is unsafe
792 * and needs to be fixed.
Alexandre Courbot14141a92014-07-22 16:17:40 +0900793 *
794 * Also it would be nice to use gpiochip_find() here so we
795 * can keep gpio_chips local to gpiolib.c, but the yield of
796 * gpio_lock prevents us from doing this.
797 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900798 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold426577b2015-05-04 17:10:32 +0200799 status = gpiochip_sysfs_register(chip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900800 spin_lock_irqsave(&gpio_lock, flags);
801 }
802 spin_unlock_irqrestore(&gpio_lock, flags);
803
804
805 return status;
806}
807postcore_initcall(gpiolib_sysfs_init);