blob: 9047b2d556a0306bf60754bc11e1f8a76a62f2f8 [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 Hovold427fdee2015-05-04 17:10:47 +020019
20 bool direction_can_change;
Johan Hovoldc43960f2015-05-04 17:10:37 +020021};
22
Johan Hovold6ffcb792015-05-04 17:10:44 +020023/*
24 * Lock to serialise gpiod export and unexport, and prevent re-export of
25 * gpiod whose chip is being unregistered.
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090026 */
27static DEFINE_MUTEX(sysfs_lock);
28
29/*
30 * /sys/class/gpio/gpioN... only for GPIOs that are exported
31 * /direction
32 * * MAY BE OMITTED if kernel won't allow direction changes
33 * * is read/write as "in" or "out"
34 * * may also be written as "high" or "low", initializing
35 * output value as specified ("out" implies "low")
36 * /value
37 * * always readable, subject to hardware behavior
38 * * may be writable, as zero/nonzero
39 * /edge
40 * * configures behavior of poll(2) on /value
41 * * available only if pin can generate IRQs on input
42 * * is read/write as "none", "falling", "rising", or "both"
43 * /active_low
44 * * configures polarity of /value
45 * * is read/write as zero/nonzero
46 * * also affects existing and subsequent "falling" and "rising"
47 * /edge configuration
48 */
49
Johan Hovold6beac9d2015-05-04 17:10:34 +020050static ssize_t direction_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090051 struct device_attribute *attr, char *buf)
52{
Johan Hovoldc43960f2015-05-04 17:10:37 +020053 struct gpiod_data *data = dev_get_drvdata(dev);
54 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090055 ssize_t status;
56
Johan Hovold6ffcb792015-05-04 17:10:44 +020057 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090058
Johan Hovoldf0b78662015-05-04 17:10:36 +020059 gpiod_get_direction(desc);
60 status = sprintf(buf, "%s\n",
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090061 test_bit(FLAG_IS_OUT, &desc->flags)
62 ? "out" : "in");
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090063
Johan Hovold6ffcb792015-05-04 17:10:44 +020064 mutex_unlock(&data->mutex);
65
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090066 return status;
67}
68
Johan Hovold6beac9d2015-05-04 17:10:34 +020069static ssize_t direction_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090070 struct device_attribute *attr, const char *buf, size_t size)
71{
Johan Hovoldc43960f2015-05-04 17:10:37 +020072 struct gpiod_data *data = dev_get_drvdata(dev);
73 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090074 ssize_t status;
75
Johan Hovold6ffcb792015-05-04 17:10:44 +020076 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090077
Johan Hovoldf0b78662015-05-04 17:10:36 +020078 if (sysfs_streq(buf, "high"))
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090079 status = gpiod_direction_output_raw(desc, 1);
80 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
81 status = gpiod_direction_output_raw(desc, 0);
82 else if (sysfs_streq(buf, "in"))
83 status = gpiod_direction_input(desc);
84 else
85 status = -EINVAL;
86
Johan Hovold6ffcb792015-05-04 17:10:44 +020087 mutex_unlock(&data->mutex);
88
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090089 return status ? : size;
90}
Johan Hovold6beac9d2015-05-04 17:10:34 +020091static DEVICE_ATTR_RW(direction);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090092
Johan Hovold6beac9d2015-05-04 17:10:34 +020093static ssize_t value_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090094 struct device_attribute *attr, char *buf)
95{
Johan Hovoldc43960f2015-05-04 17:10:37 +020096 struct gpiod_data *data = dev_get_drvdata(dev);
97 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090098 ssize_t status;
99
Johan Hovold6ffcb792015-05-04 17:10:44 +0200100 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900101
Johan Hovoldf0b78662015-05-04 17:10:36 +0200102 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900103
Johan Hovold6ffcb792015-05-04 17:10:44 +0200104 mutex_unlock(&data->mutex);
105
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900106 return status;
107}
108
Johan Hovold6beac9d2015-05-04 17:10:34 +0200109static ssize_t value_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900110 struct device_attribute *attr, const char *buf, size_t size)
111{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200112 struct gpiod_data *data = dev_get_drvdata(dev);
113 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900114 ssize_t status;
115
Johan Hovold6ffcb792015-05-04 17:10:44 +0200116 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900117
Johan Hovoldf0b78662015-05-04 17:10:36 +0200118 if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900119 status = -EPERM;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200120 } else {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900121 long value;
122
123 status = kstrtol(buf, 0, &value);
124 if (status == 0) {
125 gpiod_set_value_cansleep(desc, value);
126 status = size;
127 }
128 }
129
Johan Hovold6ffcb792015-05-04 17:10:44 +0200130 mutex_unlock(&data->mutex);
131
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900132 return status;
133}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200134static DEVICE_ATTR_RW(value);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900135
136static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
137{
Johan Hovolda08f5c22015-05-04 17:10:39 +0200138 struct gpiod_data *data = priv;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900139
Johan Hovolda08f5c22015-05-04 17:10:39 +0200140 sysfs_notify_dirent(data->value_kn);
141
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900142 return IRQ_HANDLED;
143}
144
Johan Hovold6ffcb792015-05-04 17:10:44 +0200145/* Caller holds gpiod-data mutex. */
Johan Hovold2ec74a92015-05-04 17:10:41 +0200146static int gpio_sysfs_request_irq(struct device *dev, unsigned long gpio_flags)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900147{
Johan Hovold0f628502015-05-04 17:10:38 +0200148 struct gpiod_data *data = dev_get_drvdata(dev);
149 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900150 unsigned long irq_flags;
Johan Hovold2ec74a92015-05-04 17:10:41 +0200151 int ret;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900152
Johan Hovold2ec74a92015-05-04 17:10:41 +0200153 data->irq = gpiod_to_irq(desc);
154 if (data->irq < 0)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900155 return -EIO;
156
Johan Hovold2ec74a92015-05-04 17:10:41 +0200157 data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
158 if (!data->value_kn)
159 return -ENODEV;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900160
161 irq_flags = IRQF_SHARED;
162 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
163 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
164 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
165 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
166 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
167 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
168
Johan Hovold52176d02015-05-04 17:10:28 +0200169 /*
170 * FIXME: This should be done in the irq_request_resources callback
171 * when the irq is requested, but a few drivers currently fail
172 * to do so.
173 *
174 * Remove this redundant call (along with the corresponding
175 * unlock) when those drivers have been fixed.
176 */
177 ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900178 if (ret < 0)
Johan Hovold2ec74a92015-05-04 17:10:41 +0200179 goto err_put_kn;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900180
Johan Hovold2ec74a92015-05-04 17:10:41 +0200181 ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
Johan Hovolda08f5c22015-05-04 17:10:39 +0200182 "gpiolib", data);
Johan Hovold52176d02015-05-04 17:10:28 +0200183 if (ret < 0)
184 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900185
186 desc->flags |= gpio_flags;
Johan Hovold2ec74a92015-05-04 17:10:41 +0200187
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900188 return 0;
189
Johan Hovold52176d02015-05-04 17:10:28 +0200190err_unlock:
191 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
Johan Hovold2ec74a92015-05-04 17:10:41 +0200192err_put_kn:
193 sysfs_put(data->value_kn);
194
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900195 return ret;
196}
197
Johan Hovold6ffcb792015-05-04 17:10:44 +0200198/*
199 * Caller holds gpiod-data mutex (unless called after class-device
200 * deregistration).
201 */
Johan Hovold2ec74a92015-05-04 17:10:41 +0200202static void gpio_sysfs_free_irq(struct device *dev)
203{
204 struct gpiod_data *data = dev_get_drvdata(dev);
205 struct gpio_desc *desc = data->desc;
206
207 desc->flags &= ~GPIO_TRIGGER_MASK;
208 free_irq(data->irq, data);
209 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
210 sysfs_put(data->value_kn);
211}
212
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900213static const struct {
214 const char *name;
215 unsigned long flags;
216} trigger_types[] = {
217 { "none", 0 },
218 { "falling", BIT(FLAG_TRIG_FALL) },
219 { "rising", BIT(FLAG_TRIG_RISE) },
220 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
221};
222
Johan Hovold6beac9d2015-05-04 17:10:34 +0200223static ssize_t edge_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900224 struct device_attribute *attr, char *buf)
225{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200226 struct gpiod_data *data = dev_get_drvdata(dev);
227 struct gpio_desc *desc = data->desc;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200228 unsigned long mask;
229 ssize_t status = 0;
230 int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900231
Johan Hovold6ffcb792015-05-04 17:10:44 +0200232 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900233
Johan Hovoldf0b78662015-05-04 17:10:36 +0200234 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
235 mask = desc->flags & GPIO_TRIGGER_MASK;
236 if (mask == trigger_types[i].flags) {
237 status = sprintf(buf, "%s\n", trigger_types[i].name);
238 break;
239 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900240 }
241
Johan Hovold6ffcb792015-05-04 17:10:44 +0200242 mutex_unlock(&data->mutex);
243
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900244 return status;
245}
246
Johan Hovold6beac9d2015-05-04 17:10:34 +0200247static ssize_t edge_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900248 struct device_attribute *attr, const char *buf, size_t size)
249{
Johan Hovoldb91e1802015-05-04 17:10:40 +0200250 struct gpiod_data *data = dev_get_drvdata(dev);
251 struct gpio_desc *desc = data->desc;
252 unsigned long flags;
Johan Hovold2ec74a92015-05-04 17:10:41 +0200253 ssize_t status = size;
Johan Hovolde4339ce2015-05-04 17:10:42 +0200254 int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900255
Johan Hovolde4339ce2015-05-04 17:10:42 +0200256 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900257 if (sysfs_streq(trigger_types[i].name, buf))
Johan Hovolde4339ce2015-05-04 17:10:42 +0200258 break;
259 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900260
Johan Hovolde4339ce2015-05-04 17:10:42 +0200261 if (i == ARRAY_SIZE(trigger_types))
262 return -EINVAL;
263
Johan Hovoldb91e1802015-05-04 17:10:40 +0200264 flags = trigger_types[i].flags;
265
Johan Hovold6ffcb792015-05-04 17:10:44 +0200266 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900267
Johan Hovoldb91e1802015-05-04 17:10:40 +0200268 if ((desc->flags & GPIO_TRIGGER_MASK) == flags) {
269 status = size;
270 goto out_unlock;
271 }
272
Johan Hovold2ec74a92015-05-04 17:10:41 +0200273 if (desc->flags & GPIO_TRIGGER_MASK)
274 gpio_sysfs_free_irq(dev);
275
276 if (flags) {
277 status = gpio_sysfs_request_irq(dev, flags);
278 if (!status)
279 status = size;
280 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900281
Johan Hovoldb91e1802015-05-04 17:10:40 +0200282out_unlock:
Johan Hovold6ffcb792015-05-04 17:10:44 +0200283 mutex_unlock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900284
285 return status;
286}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200287static DEVICE_ATTR_RW(edge);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900288
Johan Hovold6ffcb792015-05-04 17:10:44 +0200289/* Caller holds gpiod-data mutex. */
Johan Hovold2f323b82015-05-04 17:10:46 +0200290static int gpio_sysfs_set_active_low(struct device *dev, int value)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900291{
Johan Hovold0f628502015-05-04 17:10:38 +0200292 struct gpiod_data *data = dev_get_drvdata(dev);
293 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900294 int status = 0;
295
296 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
297 return 0;
298
299 if (value)
300 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
301 else
302 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
303
304 /* reconfigure poll(2) support if enabled on one edge only */
Johan Hovold166a85e2015-05-04 17:10:33 +0200305 if (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
306 !!test_bit(FLAG_TRIG_FALL, &desc->flags)) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900307 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
308
Johan Hovold2ec74a92015-05-04 17:10:41 +0200309 gpio_sysfs_free_irq(dev);
310 status = gpio_sysfs_request_irq(dev, trigger_flags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900311 }
312
313 return status;
314}
315
Johan Hovold6beac9d2015-05-04 17:10:34 +0200316static ssize_t active_low_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900317 struct device_attribute *attr, char *buf)
318{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200319 struct gpiod_data *data = dev_get_drvdata(dev);
320 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900321 ssize_t status;
322
Johan Hovold6ffcb792015-05-04 17:10:44 +0200323 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900324
Johan Hovoldf0b78662015-05-04 17:10:36 +0200325 status = sprintf(buf, "%d\n",
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900326 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
327
Johan Hovold6ffcb792015-05-04 17:10:44 +0200328 mutex_unlock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900329
330 return status;
331}
332
Johan Hovold6beac9d2015-05-04 17:10:34 +0200333static ssize_t active_low_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900334 struct device_attribute *attr, const char *buf, size_t size)
335{
Johan Hovold6ffcb792015-05-04 17:10:44 +0200336 struct gpiod_data *data = dev_get_drvdata(dev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900337 ssize_t status;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200338 long value;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900339
Johan Hovold6ffcb792015-05-04 17:10:44 +0200340 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900341
Johan Hovoldf0b78662015-05-04 17:10:36 +0200342 status = kstrtol(buf, 0, &value);
343 if (status == 0)
Johan Hovold2f323b82015-05-04 17:10:46 +0200344 status = gpio_sysfs_set_active_low(dev, value);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900345
Johan Hovold6ffcb792015-05-04 17:10:44 +0200346 mutex_unlock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900347
348 return status ? : size;
349}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200350static DEVICE_ATTR_RW(active_low);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900351
Johan Hovoldebbeba12015-01-13 13:00:06 +0100352static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
353 int n)
354{
355 struct device *dev = container_of(kobj, struct device, kobj);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200356 struct gpiod_data *data = dev_get_drvdata(dev);
357 struct gpio_desc *desc = data->desc;
Johan Hovoldebbeba12015-01-13 13:00:06 +0100358 umode_t mode = attr->mode;
Johan Hovold427fdee2015-05-04 17:10:47 +0200359 bool show_direction = data->direction_can_change;
Johan Hovoldebbeba12015-01-13 13:00:06 +0100360
361 if (attr == &dev_attr_direction.attr) {
362 if (!show_direction)
363 mode = 0;
364 } else if (attr == &dev_attr_edge.attr) {
365 if (gpiod_to_irq(desc) < 0)
366 mode = 0;
367 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
368 mode = 0;
369 }
370
371 return mode;
372}
373
Johan Hovold0915e6f2015-01-13 13:00:05 +0100374static struct attribute *gpio_attrs[] = {
Johan Hovoldebbeba12015-01-13 13:00:06 +0100375 &dev_attr_direction.attr,
376 &dev_attr_edge.attr,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900377 &dev_attr_value.attr,
378 &dev_attr_active_low.attr,
379 NULL,
380};
Johan Hovoldebbeba12015-01-13 13:00:06 +0100381
382static const struct attribute_group gpio_group = {
383 .attrs = gpio_attrs,
384 .is_visible = gpio_is_visible,
385};
386
387static const struct attribute_group *gpio_groups[] = {
388 &gpio_group,
389 NULL
390};
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900391
392/*
393 * /sys/class/gpio/gpiochipN/
394 * /base ... matching gpio_chip.base (N)
395 * /label ... matching gpio_chip.label
396 * /ngpio ... matching gpio_chip.ngpio
397 */
398
Johan Hovold6beac9d2015-05-04 17:10:34 +0200399static ssize_t base_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900400 struct device_attribute *attr, char *buf)
401{
402 const struct gpio_chip *chip = dev_get_drvdata(dev);
403
404 return sprintf(buf, "%d\n", chip->base);
405}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200406static DEVICE_ATTR_RO(base);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900407
Johan Hovold6beac9d2015-05-04 17:10:34 +0200408static ssize_t label_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900409 struct device_attribute *attr, char *buf)
410{
411 const struct gpio_chip *chip = dev_get_drvdata(dev);
412
413 return sprintf(buf, "%s\n", chip->label ? : "");
414}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200415static DEVICE_ATTR_RO(label);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900416
Johan Hovold6beac9d2015-05-04 17:10:34 +0200417static ssize_t ngpio_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900418 struct device_attribute *attr, char *buf)
419{
420 const struct gpio_chip *chip = dev_get_drvdata(dev);
421
422 return sprintf(buf, "%u\n", chip->ngpio);
423}
Johan Hovold6beac9d2015-05-04 17:10:34 +0200424static DEVICE_ATTR_RO(ngpio);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900425
Johan Hovold121b6a72015-01-13 13:00:04 +0100426static struct attribute *gpiochip_attrs[] = {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900427 &dev_attr_base.attr,
428 &dev_attr_label.attr,
429 &dev_attr_ngpio.attr,
430 NULL,
431};
Johan Hovold121b6a72015-01-13 13:00:04 +0100432ATTRIBUTE_GROUPS(gpiochip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900433
434/*
435 * /sys/class/gpio/export ... write-only
436 * integer N ... number of GPIO to export (full access)
437 * /sys/class/gpio/unexport ... write-only
438 * integer N ... number of GPIO to unexport
439 */
440static ssize_t export_store(struct class *class,
441 struct class_attribute *attr,
442 const char *buf, size_t len)
443{
444 long gpio;
445 struct gpio_desc *desc;
446 int status;
447
448 status = kstrtol(buf, 0, &gpio);
449 if (status < 0)
450 goto done;
451
452 desc = gpio_to_desc(gpio);
453 /* reject invalid GPIOs */
454 if (!desc) {
455 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
456 return -EINVAL;
457 }
458
459 /* No extra locking here; FLAG_SYSFS just signifies that the
460 * request and export were done by on behalf of userspace, so
461 * they may be undone on its behalf too.
462 */
463
464 status = gpiod_request(desc, "sysfs");
465 if (status < 0) {
466 if (status == -EPROBE_DEFER)
467 status = -ENODEV;
468 goto done;
469 }
470 status = gpiod_export(desc, true);
471 if (status < 0)
472 gpiod_free(desc);
473 else
474 set_bit(FLAG_SYSFS, &desc->flags);
475
476done:
477 if (status)
478 pr_debug("%s: status %d\n", __func__, status);
479 return status ? : len;
480}
481
482static ssize_t unexport_store(struct class *class,
483 struct class_attribute *attr,
484 const char *buf, size_t len)
485{
486 long gpio;
487 struct gpio_desc *desc;
488 int status;
489
490 status = kstrtol(buf, 0, &gpio);
491 if (status < 0)
492 goto done;
493
494 desc = gpio_to_desc(gpio);
495 /* reject bogus commands (gpio_unexport ignores them) */
496 if (!desc) {
497 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
498 return -EINVAL;
499 }
500
501 status = -EINVAL;
502
503 /* No extra locking here; FLAG_SYSFS just signifies that the
504 * request and export were done by on behalf of userspace, so
505 * they may be undone on its behalf too.
506 */
507 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
508 status = 0;
509 gpiod_free(desc);
510 }
511done:
512 if (status)
513 pr_debug("%s: status %d\n", __func__, status);
514 return status ? : len;
515}
516
517static struct class_attribute gpio_class_attrs[] = {
518 __ATTR(export, 0200, NULL, export_store),
519 __ATTR(unexport, 0200, NULL, unexport_store),
520 __ATTR_NULL,
521};
522
523static struct class gpio_class = {
524 .name = "gpio",
525 .owner = THIS_MODULE,
526
527 .class_attrs = gpio_class_attrs,
528};
529
530
531/**
532 * gpiod_export - export a GPIO through sysfs
533 * @gpio: gpio to make available, already requested
534 * @direction_may_change: true if userspace may change gpio direction
535 * Context: arch_initcall or later
536 *
537 * When drivers want to make a GPIO accessible to userspace after they
538 * have requested it -- perhaps while debugging, or as part of their
539 * public interface -- they may use this routine. If the GPIO can
540 * change direction (some can't) and the caller allows it, userspace
541 * will see "direction" sysfs attribute which may be used to change
542 * the gpio's direction. A "value" attribute will always be provided.
543 *
544 * Returns zero on success, else an error.
545 */
546int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
547{
Johan Hovold483d8212015-04-21 17:42:09 +0200548 struct gpio_chip *chip;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200549 struct gpiod_data *data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900550 unsigned long flags;
551 int status;
552 const char *ioname = NULL;
553 struct device *dev;
554 int offset;
555
556 /* can't export until sysfs is available ... */
557 if (!gpio_class.p) {
558 pr_debug("%s: called too early!\n", __func__);
559 return -ENOENT;
560 }
561
562 if (!desc) {
563 pr_debug("%s: invalid gpio descriptor\n", __func__);
564 return -EINVAL;
565 }
566
Johan Hovold483d8212015-04-21 17:42:09 +0200567 chip = desc->chip;
568
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900569 mutex_lock(&sysfs_lock);
570
Johan Hovold483d8212015-04-21 17:42:09 +0200571 /* check if chip is being removed */
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200572 if (!chip || !chip->cdev) {
Johan Hovold483d8212015-04-21 17:42:09 +0200573 status = -ENODEV;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200574 goto err_unlock;
Johan Hovold483d8212015-04-21 17:42:09 +0200575 }
576
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900577 spin_lock_irqsave(&gpio_lock, flags);
578 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
579 test_bit(FLAG_EXPORT, &desc->flags)) {
580 spin_unlock_irqrestore(&gpio_lock, flags);
581 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
582 __func__,
583 test_bit(FLAG_REQUESTED, &desc->flags),
584 test_bit(FLAG_EXPORT, &desc->flags));
585 status = -EPERM;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200586 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900587 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900588 spin_unlock_irqrestore(&gpio_lock, flags);
589
Johan Hovoldc43960f2015-05-04 17:10:37 +0200590 data = kzalloc(sizeof(*data), GFP_KERNEL);
591 if (!data) {
592 status = -ENOMEM;
593 goto err_unlock;
594 }
595
596 data->desc = desc;
Johan Hovold6ffcb792015-05-04 17:10:44 +0200597 mutex_init(&data->mutex);
Johan Hovold427fdee2015-05-04 17:10:47 +0200598 if (chip->direction_input && chip->direction_output)
599 data->direction_can_change = direction_may_change;
600 else
601 data->direction_can_change = false;
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 Hovold72eba6f2015-05-04 17:10:45 +0200677 struct gpiod_data *data;
678 struct device *dev;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900679
680 if (!desc) {
681 pr_warn("%s: invalid GPIO\n", __func__);
682 return;
683 }
684
685 mutex_lock(&sysfs_lock);
686
Johan Hovold72eba6f2015-05-04 17:10:45 +0200687 if (!test_bit(FLAG_EXPORT, &desc->flags))
688 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900689
Johan Hovold72eba6f2015-05-04 17:10:45 +0200690 dev = class_find_device(&gpio_class, NULL, desc, match_export);
691 if (!dev)
692 goto err_unlock;
693
694 data = dev_get_drvdata(dev);
695
Johan Hovold72eba6f2015-05-04 17:10:45 +0200696 clear_bit(FLAG_EXPORT, &desc->flags);
697
698 device_unregister(dev);
699
700 /*
701 * Release irq after deregistration to prevent race with edge_store.
702 */
703 if (desc->flags & GPIO_TRIGGER_MASK)
704 gpio_sysfs_free_irq(dev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900705
706 mutex_unlock(&sysfs_lock);
707
Johan Hovold72eba6f2015-05-04 17:10:45 +0200708 put_device(dev);
709 kfree(data);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900710
Johan Hovold72eba6f2015-05-04 17:10:45 +0200711 return;
712
713err_unlock:
714 mutex_unlock(&sysfs_lock);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900715}
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);