blob: 0bd472ffb0720df058bec4ac9ee243e7658aa998 [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>
Masami Hiramatsu90b05b02017-08-02 13:47:42 +09005#include <linux/gpio.h>
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09006#include <linux/gpio/consumer.h>
7#include <linux/gpio/driver.h>
8#include <linux/interrupt.h>
9#include <linux/kdev_t.h>
Johan Hovoldc43960f2015-05-04 17:10:37 +020010#include <linux/slab.h>
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090011
12#include "gpiolib.h"
13
Johan Hovoldcef17172015-05-04 17:10:48 +020014#define GPIO_IRQF_TRIGGER_FALLING BIT(0)
15#define GPIO_IRQF_TRIGGER_RISING BIT(1)
16#define GPIO_IRQF_TRIGGER_BOTH (GPIO_IRQF_TRIGGER_FALLING | \
17 GPIO_IRQF_TRIGGER_RISING)
18
Johan Hovoldc43960f2015-05-04 17:10:37 +020019struct gpiod_data {
20 struct gpio_desc *desc;
Johan Hovold6ffcb792015-05-04 17:10:44 +020021
22 struct mutex mutex;
Johan Hovolda08f5c22015-05-04 17:10:39 +020023 struct kernfs_node *value_kn;
Johan Hovold2ec74a92015-05-04 17:10:41 +020024 int irq;
Johan Hovoldcef17172015-05-04 17:10:48 +020025 unsigned char irq_flags;
Johan Hovold427fdee2015-05-04 17:10:47 +020026
27 bool direction_can_change;
Johan Hovoldc43960f2015-05-04 17:10:37 +020028};
29
Johan Hovold6ffcb792015-05-04 17:10:44 +020030/*
31 * Lock to serialise gpiod export and unexport, and prevent re-export of
32 * gpiod whose chip is being unregistered.
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090033 */
34static DEFINE_MUTEX(sysfs_lock);
35
36/*
37 * /sys/class/gpio/gpioN... only for GPIOs that are exported
38 * /direction
39 * * MAY BE OMITTED if kernel won't allow direction changes
40 * * is read/write as "in" or "out"
41 * * may also be written as "high" or "low", initializing
42 * output value as specified ("out" implies "low")
43 * /value
44 * * always readable, subject to hardware behavior
45 * * may be writable, as zero/nonzero
46 * /edge
47 * * configures behavior of poll(2) on /value
48 * * available only if pin can generate IRQs on input
49 * * is read/write as "none", "falling", "rising", or "both"
50 * /active_low
51 * * configures polarity of /value
52 * * is read/write as zero/nonzero
53 * * also affects existing and subsequent "falling" and "rising"
54 * /edge configuration
55 */
56
Johan Hovold6beac9d12015-05-04 17:10:34 +020057static ssize_t direction_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090058 struct device_attribute *attr, char *buf)
59{
Johan Hovoldc43960f2015-05-04 17:10:37 +020060 struct gpiod_data *data = dev_get_drvdata(dev);
61 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090062 ssize_t status;
63
Johan Hovold6ffcb792015-05-04 17:10:44 +020064 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090065
Johan Hovoldf0b78662015-05-04 17:10:36 +020066 gpiod_get_direction(desc);
67 status = sprintf(buf, "%s\n",
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090068 test_bit(FLAG_IS_OUT, &desc->flags)
69 ? "out" : "in");
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090070
Johan Hovold6ffcb792015-05-04 17:10:44 +020071 mutex_unlock(&data->mutex);
72
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090073 return status;
74}
75
Johan Hovold6beac9d12015-05-04 17:10:34 +020076static ssize_t direction_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090077 struct device_attribute *attr, const char *buf, size_t size)
78{
Johan Hovoldc43960f2015-05-04 17:10:37 +020079 struct gpiod_data *data = dev_get_drvdata(dev);
80 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090081 ssize_t status;
82
Johan Hovold6ffcb792015-05-04 17:10:44 +020083 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090084
Johan Hovoldf0b78662015-05-04 17:10:36 +020085 if (sysfs_streq(buf, "high"))
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090086 status = gpiod_direction_output_raw(desc, 1);
87 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
88 status = gpiod_direction_output_raw(desc, 0);
89 else if (sysfs_streq(buf, "in"))
90 status = gpiod_direction_input(desc);
91 else
92 status = -EINVAL;
93
Johan Hovold6ffcb792015-05-04 17:10:44 +020094 mutex_unlock(&data->mutex);
95
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090096 return status ? : size;
97}
Johan Hovold6beac9d12015-05-04 17:10:34 +020098static DEVICE_ATTR_RW(direction);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090099
Johan Hovold6beac9d12015-05-04 17:10:34 +0200100static ssize_t value_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900101 struct device_attribute *attr, char *buf)
102{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200103 struct gpiod_data *data = dev_get_drvdata(dev);
104 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900105 ssize_t status;
106
Johan Hovold6ffcb792015-05-04 17:10:44 +0200107 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900108
Johan Hovoldf0b78662015-05-04 17:10:36 +0200109 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900110
Johan Hovold6ffcb792015-05-04 17:10:44 +0200111 mutex_unlock(&data->mutex);
112
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900113 return status;
114}
115
Johan Hovold6beac9d12015-05-04 17:10:34 +0200116static ssize_t value_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900117 struct device_attribute *attr, const char *buf, size_t size)
118{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200119 struct gpiod_data *data = dev_get_drvdata(dev);
120 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900121 ssize_t status;
122
Johan Hovold6ffcb792015-05-04 17:10:44 +0200123 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900124
Johan Hovoldf0b78662015-05-04 17:10:36 +0200125 if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900126 status = -EPERM;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200127 } else {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900128 long value;
129
130 status = kstrtol(buf, 0, &value);
131 if (status == 0) {
132 gpiod_set_value_cansleep(desc, value);
133 status = size;
134 }
135 }
136
Johan Hovold6ffcb792015-05-04 17:10:44 +0200137 mutex_unlock(&data->mutex);
138
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900139 return status;
140}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200141static DEVICE_ATTR_RW(value);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900142
143static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
144{
Johan Hovolda08f5c22015-05-04 17:10:39 +0200145 struct gpiod_data *data = priv;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900146
Johan Hovolda08f5c22015-05-04 17:10:39 +0200147 sysfs_notify_dirent(data->value_kn);
148
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900149 return IRQ_HANDLED;
150}
151
Johan Hovold6ffcb792015-05-04 17:10:44 +0200152/* Caller holds gpiod-data mutex. */
Johan Hovoldcef17172015-05-04 17:10:48 +0200153static int gpio_sysfs_request_irq(struct device *dev, unsigned char flags)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900154{
Johan Hovold0f628502015-05-04 17:10:38 +0200155 struct gpiod_data *data = dev_get_drvdata(dev);
156 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900157 unsigned long irq_flags;
Johan Hovold2ec74a92015-05-04 17:10:41 +0200158 int ret;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900159
Johan Hovold2ec74a92015-05-04 17:10:41 +0200160 data->irq = gpiod_to_irq(desc);
161 if (data->irq < 0)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900162 return -EIO;
163
Johan Hovold2ec74a92015-05-04 17:10:41 +0200164 data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
165 if (!data->value_kn)
166 return -ENODEV;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900167
168 irq_flags = IRQF_SHARED;
Johan Hovoldcef17172015-05-04 17:10:48 +0200169 if (flags & GPIO_IRQF_TRIGGER_FALLING)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900170 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
171 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Johan Hovoldcef17172015-05-04 17:10:48 +0200172 if (flags & GPIO_IRQF_TRIGGER_RISING)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900173 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
174 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
175
Johan Hovold52176d02015-05-04 17:10:28 +0200176 /*
177 * FIXME: This should be done in the irq_request_resources callback
178 * when the irq is requested, but a few drivers currently fail
179 * to do so.
180 *
181 * Remove this redundant call (along with the corresponding
182 * unlock) when those drivers have been fixed.
183 */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100184 ret = gpiochip_lock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900185 if (ret < 0)
Johan Hovold2ec74a92015-05-04 17:10:41 +0200186 goto err_put_kn;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900187
Johan Hovold2ec74a92015-05-04 17:10:41 +0200188 ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
Johan Hovolda08f5c22015-05-04 17:10:39 +0200189 "gpiolib", data);
Johan Hovold52176d02015-05-04 17:10:28 +0200190 if (ret < 0)
191 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900192
Johan Hovoldcef17172015-05-04 17:10:48 +0200193 data->irq_flags = flags;
Johan Hovold2ec74a92015-05-04 17:10:41 +0200194
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900195 return 0;
196
Johan Hovold52176d02015-05-04 17:10:28 +0200197err_unlock:
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100198 gpiochip_unlock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
Johan Hovold2ec74a92015-05-04 17:10:41 +0200199err_put_kn:
200 sysfs_put(data->value_kn);
201
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900202 return ret;
203}
204
Johan Hovold6ffcb792015-05-04 17:10:44 +0200205/*
206 * Caller holds gpiod-data mutex (unless called after class-device
207 * deregistration).
208 */
Johan Hovold2ec74a92015-05-04 17:10:41 +0200209static void gpio_sysfs_free_irq(struct device *dev)
210{
211 struct gpiod_data *data = dev_get_drvdata(dev);
212 struct gpio_desc *desc = data->desc;
213
Johan Hovoldcef17172015-05-04 17:10:48 +0200214 data->irq_flags = 0;
Johan Hovold2ec74a92015-05-04 17:10:41 +0200215 free_irq(data->irq, data);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100216 gpiochip_unlock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
Johan Hovold2ec74a92015-05-04 17:10:41 +0200217 sysfs_put(data->value_kn);
218}
219
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900220static const struct {
221 const char *name;
Johan Hovoldcef17172015-05-04 17:10:48 +0200222 unsigned char flags;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900223} trigger_types[] = {
224 { "none", 0 },
Johan Hovoldcef17172015-05-04 17:10:48 +0200225 { "falling", GPIO_IRQF_TRIGGER_FALLING },
226 { "rising", GPIO_IRQF_TRIGGER_RISING },
227 { "both", GPIO_IRQF_TRIGGER_BOTH },
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900228};
229
Johan Hovold6beac9d12015-05-04 17:10:34 +0200230static ssize_t edge_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900231 struct device_attribute *attr, char *buf)
232{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200233 struct gpiod_data *data = dev_get_drvdata(dev);
Johan Hovoldf0b78662015-05-04 17:10:36 +0200234 ssize_t status = 0;
235 int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900236
Johan Hovold6ffcb792015-05-04 17:10:44 +0200237 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900238
Johan Hovoldf0b78662015-05-04 17:10:36 +0200239 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
Johan Hovoldcef17172015-05-04 17:10:48 +0200240 if (data->irq_flags == trigger_types[i].flags) {
Johan Hovoldf0b78662015-05-04 17:10:36 +0200241 status = sprintf(buf, "%s\n", trigger_types[i].name);
242 break;
243 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900244 }
245
Johan Hovold6ffcb792015-05-04 17:10:44 +0200246 mutex_unlock(&data->mutex);
247
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900248 return status;
249}
250
Johan Hovold6beac9d12015-05-04 17:10:34 +0200251static ssize_t edge_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900252 struct device_attribute *attr, const char *buf, size_t size)
253{
Johan Hovoldb91e1802015-05-04 17:10:40 +0200254 struct gpiod_data *data = dev_get_drvdata(dev);
Johan Hovoldcef17172015-05-04 17:10:48 +0200255 unsigned char flags;
Johan Hovold2ec74a92015-05-04 17:10:41 +0200256 ssize_t status = size;
Johan Hovolde4339ce2015-05-04 17:10:42 +0200257 int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900258
Johan Hovolde4339ce2015-05-04 17:10:42 +0200259 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900260 if (sysfs_streq(trigger_types[i].name, buf))
Johan Hovolde4339ce2015-05-04 17:10:42 +0200261 break;
262 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900263
Johan Hovolde4339ce2015-05-04 17:10:42 +0200264 if (i == ARRAY_SIZE(trigger_types))
265 return -EINVAL;
266
Johan Hovoldb91e1802015-05-04 17:10:40 +0200267 flags = trigger_types[i].flags;
268
Johan Hovold6ffcb792015-05-04 17:10:44 +0200269 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900270
Johan Hovoldcef17172015-05-04 17:10:48 +0200271 if (flags == data->irq_flags) {
Johan Hovoldb91e1802015-05-04 17:10:40 +0200272 status = size;
273 goto out_unlock;
274 }
275
Johan Hovoldcef17172015-05-04 17:10:48 +0200276 if (data->irq_flags)
Johan Hovold2ec74a92015-05-04 17:10:41 +0200277 gpio_sysfs_free_irq(dev);
278
279 if (flags) {
280 status = gpio_sysfs_request_irq(dev, flags);
281 if (!status)
282 status = size;
283 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900284
Johan Hovoldb91e1802015-05-04 17:10:40 +0200285out_unlock:
Johan Hovold6ffcb792015-05-04 17:10:44 +0200286 mutex_unlock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900287
288 return status;
289}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200290static DEVICE_ATTR_RW(edge);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900291
Johan Hovold6ffcb792015-05-04 17:10:44 +0200292/* Caller holds gpiod-data mutex. */
Johan Hovold2f323b82015-05-04 17:10:46 +0200293static int gpio_sysfs_set_active_low(struct device *dev, int value)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900294{
Johan Hovold0f628502015-05-04 17:10:38 +0200295 struct gpiod_data *data = dev_get_drvdata(dev);
296 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900297 int status = 0;
Johan Hovoldcef17172015-05-04 17:10:48 +0200298 unsigned int flags = data->irq_flags;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900299
300 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
301 return 0;
302
303 if (value)
304 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
305 else
306 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
307
308 /* reconfigure poll(2) support if enabled on one edge only */
Johan Hovoldcef17172015-05-04 17:10:48 +0200309 if (flags == GPIO_IRQF_TRIGGER_FALLING ||
310 flags == GPIO_IRQF_TRIGGER_RISING) {
Johan Hovold2ec74a92015-05-04 17:10:41 +0200311 gpio_sysfs_free_irq(dev);
Johan Hovoldcef17172015-05-04 17:10:48 +0200312 status = gpio_sysfs_request_irq(dev, flags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900313 }
314
315 return status;
316}
317
Johan Hovold6beac9d12015-05-04 17:10:34 +0200318static ssize_t active_low_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900319 struct device_attribute *attr, char *buf)
320{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200321 struct gpiod_data *data = dev_get_drvdata(dev);
322 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900323 ssize_t status;
324
Johan Hovold6ffcb792015-05-04 17:10:44 +0200325 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900326
Johan Hovoldf0b78662015-05-04 17:10:36 +0200327 status = sprintf(buf, "%d\n",
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900328 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
329
Johan Hovold6ffcb792015-05-04 17:10:44 +0200330 mutex_unlock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900331
332 return status;
333}
334
Johan Hovold6beac9d12015-05-04 17:10:34 +0200335static ssize_t active_low_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900336 struct device_attribute *attr, const char *buf, size_t size)
337{
Johan Hovold6ffcb792015-05-04 17:10:44 +0200338 struct gpiod_data *data = dev_get_drvdata(dev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900339 ssize_t status;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200340 long value;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900341
Johan Hovold6ffcb792015-05-04 17:10:44 +0200342 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900343
Johan Hovoldf0b78662015-05-04 17:10:36 +0200344 status = kstrtol(buf, 0, &value);
345 if (status == 0)
Johan Hovold2f323b82015-05-04 17:10:46 +0200346 status = gpio_sysfs_set_active_low(dev, value);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900347
Johan Hovold6ffcb792015-05-04 17:10:44 +0200348 mutex_unlock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900349
350 return status ? : size;
351}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200352static DEVICE_ATTR_RW(active_low);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900353
Johan Hovoldebbeba12015-01-13 13:00:06 +0100354static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
355 int n)
356{
357 struct device *dev = container_of(kobj, struct device, kobj);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200358 struct gpiod_data *data = dev_get_drvdata(dev);
359 struct gpio_desc *desc = data->desc;
Johan Hovoldebbeba12015-01-13 13:00:06 +0100360 umode_t mode = attr->mode;
Johan Hovold427fdee2015-05-04 17:10:47 +0200361 bool show_direction = data->direction_can_change;
Johan Hovoldebbeba12015-01-13 13:00:06 +0100362
363 if (attr == &dev_attr_direction.attr) {
364 if (!show_direction)
365 mode = 0;
366 } else if (attr == &dev_attr_edge.attr) {
367 if (gpiod_to_irq(desc) < 0)
368 mode = 0;
369 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
370 mode = 0;
371 }
372
373 return mode;
374}
375
Johan Hovold0915e6f2015-01-13 13:00:05 +0100376static struct attribute *gpio_attrs[] = {
Johan Hovoldebbeba12015-01-13 13:00:06 +0100377 &dev_attr_direction.attr,
378 &dev_attr_edge.attr,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900379 &dev_attr_value.attr,
380 &dev_attr_active_low.attr,
381 NULL,
382};
Johan Hovoldebbeba12015-01-13 13:00:06 +0100383
384static const struct attribute_group gpio_group = {
385 .attrs = gpio_attrs,
386 .is_visible = gpio_is_visible,
387};
388
389static const struct attribute_group *gpio_groups[] = {
390 &gpio_group,
391 NULL
392};
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900393
394/*
395 * /sys/class/gpio/gpiochipN/
396 * /base ... matching gpio_chip.base (N)
397 * /label ... matching gpio_chip.label
398 * /ngpio ... matching gpio_chip.ngpio
399 */
400
Johan Hovold6beac9d12015-05-04 17:10:34 +0200401static ssize_t base_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900402 struct device_attribute *attr, char *buf)
403{
404 const struct gpio_chip *chip = dev_get_drvdata(dev);
405
406 return sprintf(buf, "%d\n", chip->base);
407}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200408static DEVICE_ATTR_RO(base);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900409
Johan Hovold6beac9d12015-05-04 17:10:34 +0200410static ssize_t label_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900411 struct device_attribute *attr, char *buf)
412{
413 const struct gpio_chip *chip = dev_get_drvdata(dev);
414
415 return sprintf(buf, "%s\n", chip->label ? : "");
416}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200417static DEVICE_ATTR_RO(label);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900418
Johan Hovold6beac9d12015-05-04 17:10:34 +0200419static ssize_t ngpio_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900420 struct device_attribute *attr, char *buf)
421{
422 const struct gpio_chip *chip = dev_get_drvdata(dev);
423
424 return sprintf(buf, "%u\n", chip->ngpio);
425}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200426static DEVICE_ATTR_RO(ngpio);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900427
Johan Hovold121b6a72015-01-13 13:00:04 +0100428static struct attribute *gpiochip_attrs[] = {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900429 &dev_attr_base.attr,
430 &dev_attr_label.attr,
431 &dev_attr_ngpio.attr,
432 NULL,
433};
Johan Hovold121b6a72015-01-13 13:00:04 +0100434ATTRIBUTE_GROUPS(gpiochip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900435
Masami Hiramatsu90b05b02017-08-02 13:47:42 +0900436static struct gpio_desc *gpio_to_valid_desc(int gpio)
437{
438 return gpio_is_valid(gpio) ? gpio_to_desc(gpio) : NULL;
439}
440
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900441/*
442 * /sys/class/gpio/export ... write-only
443 * integer N ... number of GPIO to export (full access)
444 * /sys/class/gpio/unexport ... write-only
445 * integer N ... number of GPIO to unexport
446 */
447static ssize_t export_store(struct class *class,
448 struct class_attribute *attr,
449 const char *buf, size_t len)
450{
451 long gpio;
452 struct gpio_desc *desc;
453 int status;
454
455 status = kstrtol(buf, 0, &gpio);
456 if (status < 0)
457 goto done;
458
Masami Hiramatsu90b05b02017-08-02 13:47:42 +0900459 desc = gpio_to_valid_desc(gpio);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900460 /* reject invalid GPIOs */
461 if (!desc) {
462 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
463 return -EINVAL;
464 }
465
466 /* No extra locking here; FLAG_SYSFS just signifies that the
467 * request and export were done by on behalf of userspace, so
468 * they may be undone on its behalf too.
469 */
470
471 status = gpiod_request(desc, "sysfs");
472 if (status < 0) {
473 if (status == -EPROBE_DEFER)
474 status = -ENODEV;
475 goto done;
476 }
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030477
478 status = gpiod_set_transitory(desc, false);
479 if (!status) {
480 status = gpiod_export(desc, true);
481 if (status < 0)
482 gpiod_free(desc);
483 else
484 set_bit(FLAG_SYSFS, &desc->flags);
485 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900486
487done:
488 if (status)
489 pr_debug("%s: status %d\n", __func__, status);
490 return status ? : len;
491}
Greg Kroah-Hartmand83bb152017-06-08 10:12:40 +0200492static CLASS_ATTR_WO(export);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900493
494static ssize_t unexport_store(struct class *class,
495 struct class_attribute *attr,
496 const char *buf, size_t len)
497{
498 long gpio;
499 struct gpio_desc *desc;
500 int status;
501
502 status = kstrtol(buf, 0, &gpio);
503 if (status < 0)
504 goto done;
505
Masami Hiramatsu90b05b02017-08-02 13:47:42 +0900506 desc = gpio_to_valid_desc(gpio);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900507 /* reject bogus commands (gpio_unexport ignores them) */
508 if (!desc) {
509 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
510 return -EINVAL;
511 }
512
513 status = -EINVAL;
514
515 /* No extra locking here; FLAG_SYSFS just signifies that the
516 * request and export were done by on behalf of userspace, so
517 * they may be undone on its behalf too.
518 */
519 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
520 status = 0;
521 gpiod_free(desc);
522 }
523done:
524 if (status)
525 pr_debug("%s: status %d\n", __func__, status);
526 return status ? : len;
527}
Greg Kroah-Hartmand83bb152017-06-08 10:12:40 +0200528static CLASS_ATTR_WO(unexport);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900529
Greg Kroah-Hartmand83bb152017-06-08 10:12:40 +0200530static struct attribute *gpio_class_attrs[] = {
531 &class_attr_export.attr,
532 &class_attr_unexport.attr,
533 NULL,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900534};
Greg Kroah-Hartmand83bb152017-06-08 10:12:40 +0200535ATTRIBUTE_GROUPS(gpio_class);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900536
537static struct class gpio_class = {
538 .name = "gpio",
539 .owner = THIS_MODULE,
540
Greg Kroah-Hartmand83bb152017-06-08 10:12:40 +0200541 .class_groups = gpio_class_groups,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900542};
543
544
545/**
546 * gpiod_export - export a GPIO through sysfs
Thierry Reding2d9d0512017-07-24 16:57:26 +0200547 * @desc: GPIO to make available, already requested
548 * @direction_may_change: true if userspace may change GPIO direction
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900549 * Context: arch_initcall or later
550 *
551 * When drivers want to make a GPIO accessible to userspace after they
552 * have requested it -- perhaps while debugging, or as part of their
553 * public interface -- they may use this routine. If the GPIO can
554 * change direction (some can't) and the caller allows it, userspace
555 * will see "direction" sysfs attribute which may be used to change
556 * the gpio's direction. A "value" attribute will always be provided.
557 *
558 * Returns zero on success, else an error.
559 */
560int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
561{
Johan Hovold483d8212015-04-21 17:42:09 +0200562 struct gpio_chip *chip;
Linus Walleijff2b1352015-10-20 11:10:38 +0200563 struct gpio_device *gdev;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200564 struct gpiod_data *data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900565 unsigned long flags;
566 int status;
567 const char *ioname = NULL;
568 struct device *dev;
569 int offset;
570
571 /* can't export until sysfs is available ... */
572 if (!gpio_class.p) {
573 pr_debug("%s: called too early!\n", __func__);
574 return -ENOENT;
575 }
576
577 if (!desc) {
578 pr_debug("%s: invalid gpio descriptor\n", __func__);
579 return -EINVAL;
580 }
581
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100582 gdev = desc->gdev;
583 chip = gdev->chip;
Johan Hovold483d8212015-04-21 17:42:09 +0200584
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900585 mutex_lock(&sysfs_lock);
586
Johan Hovold483d8212015-04-21 17:42:09 +0200587 /* check if chip is being removed */
Linus Walleijafbc4f32016-02-09 13:21:06 +0100588 if (!chip || !gdev->mockdev) {
Johan Hovold483d8212015-04-21 17:42:09 +0200589 status = -ENODEV;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200590 goto err_unlock;
Johan Hovold483d8212015-04-21 17:42:09 +0200591 }
592
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900593 spin_lock_irqsave(&gpio_lock, flags);
594 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
595 test_bit(FLAG_EXPORT, &desc->flags)) {
596 spin_unlock_irqrestore(&gpio_lock, flags);
597 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
598 __func__,
599 test_bit(FLAG_REQUESTED, &desc->flags),
600 test_bit(FLAG_EXPORT, &desc->flags));
601 status = -EPERM;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200602 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900603 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900604 spin_unlock_irqrestore(&gpio_lock, flags);
605
Johan Hovoldc43960f2015-05-04 17:10:37 +0200606 data = kzalloc(sizeof(*data), GFP_KERNEL);
607 if (!data) {
608 status = -ENOMEM;
609 goto err_unlock;
610 }
611
612 data->desc = desc;
Johan Hovold6ffcb792015-05-04 17:10:44 +0200613 mutex_init(&data->mutex);
Johan Hovold427fdee2015-05-04 17:10:47 +0200614 if (chip->direction_input && chip->direction_output)
615 data->direction_can_change = direction_may_change;
616 else
617 data->direction_can_change = false;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200618
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900619 offset = gpio_chip_hwgpio(desc);
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200620 if (chip->names && chip->names[offset])
621 ioname = chip->names[offset];
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900622
Linus Walleijff2b1352015-10-20 11:10:38 +0200623 dev = device_create_with_groups(&gpio_class, &gdev->dev,
Johan Hovoldc43960f2015-05-04 17:10:37 +0200624 MKDEV(0, 0), data, gpio_groups,
Johan Hovold0915e6f2015-01-13 13:00:05 +0100625 ioname ? ioname : "gpio%u",
626 desc_to_gpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900627 if (IS_ERR(dev)) {
628 status = PTR_ERR(dev);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200629 goto err_free_data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900630 }
631
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900632 set_bit(FLAG_EXPORT, &desc->flags);
633 mutex_unlock(&sysfs_lock);
634 return 0;
635
Johan Hovoldc43960f2015-05-04 17:10:37 +0200636err_free_data:
637 kfree(data);
638err_unlock:
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900639 mutex_unlock(&sysfs_lock);
640 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
641 return status;
642}
643EXPORT_SYMBOL_GPL(gpiod_export);
644
Johan Hovoldc43960f2015-05-04 17:10:37 +0200645static int match_export(struct device *dev, const void *desc)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900646{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200647 struct gpiod_data *data = dev_get_drvdata(dev);
648
649 return data->desc == desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900650}
651
652/**
653 * gpiod_export_link - create a sysfs link to an exported GPIO node
654 * @dev: device under which to create symlink
655 * @name: name of the symlink
Thierry Reding2d9d0512017-07-24 16:57:26 +0200656 * @desc: GPIO to create symlink to, already exported
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900657 *
658 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
659 * node. Caller is responsible for unlinking.
660 *
661 * Returns zero on success, else an error.
662 */
663int gpiod_export_link(struct device *dev, const char *name,
664 struct gpio_desc *desc)
665{
Johan Hovold56d30ec2015-05-04 17:10:43 +0200666 struct device *cdev;
667 int ret;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900668
669 if (!desc) {
670 pr_warn("%s: invalid GPIO\n", __func__);
671 return -EINVAL;
672 }
673
Johan Hovold56d30ec2015-05-04 17:10:43 +0200674 cdev = class_find_device(&gpio_class, NULL, desc, match_export);
675 if (!cdev)
676 return -ENODEV;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900677
Johan Hovold56d30ec2015-05-04 17:10:43 +0200678 ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
679 put_device(cdev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900680
Johan Hovold56d30ec2015-05-04 17:10:43 +0200681 return ret;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900682}
683EXPORT_SYMBOL_GPL(gpiod_export_link);
684
685/**
Amitesh Singh31963eb2016-09-08 17:11:20 +0530686 * gpiod_unexport - reverse effect of gpiod_export()
Thierry Reding2d9d0512017-07-24 16:57:26 +0200687 * @desc: GPIO to make unavailable
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900688 *
Amitesh Singh31963eb2016-09-08 17:11:20 +0530689 * This is implicit on gpiod_free().
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900690 */
691void gpiod_unexport(struct gpio_desc *desc)
692{
Johan Hovold72eba6f2015-05-04 17:10:45 +0200693 struct gpiod_data *data;
694 struct device *dev;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900695
696 if (!desc) {
697 pr_warn("%s: invalid GPIO\n", __func__);
698 return;
699 }
700
701 mutex_lock(&sysfs_lock);
702
Johan Hovold72eba6f2015-05-04 17:10:45 +0200703 if (!test_bit(FLAG_EXPORT, &desc->flags))
704 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900705
Johan Hovold72eba6f2015-05-04 17:10:45 +0200706 dev = class_find_device(&gpio_class, NULL, desc, match_export);
707 if (!dev)
708 goto err_unlock;
709
710 data = dev_get_drvdata(dev);
711
Johan Hovold72eba6f2015-05-04 17:10:45 +0200712 clear_bit(FLAG_EXPORT, &desc->flags);
713
714 device_unregister(dev);
715
716 /*
717 * Release irq after deregistration to prevent race with edge_store.
718 */
Johan Hovoldcef17172015-05-04 17:10:48 +0200719 if (data->irq_flags)
Johan Hovold72eba6f2015-05-04 17:10:45 +0200720 gpio_sysfs_free_irq(dev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900721
722 mutex_unlock(&sysfs_lock);
723
Johan Hovold72eba6f2015-05-04 17:10:45 +0200724 put_device(dev);
725 kfree(data);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900726
Johan Hovold72eba6f2015-05-04 17:10:45 +0200727 return;
728
729err_unlock:
730 mutex_unlock(&sysfs_lock);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900731}
732EXPORT_SYMBOL_GPL(gpiod_unexport);
733
Linus Walleijafbc4f32016-02-09 13:21:06 +0100734int gpiochip_sysfs_register(struct gpio_device *gdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900735{
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900736 struct device *dev;
Bamvor Jian Zhangd27c1722016-02-24 22:17:19 +0800737 struct device *parent;
Linus Walleijafbc4f32016-02-09 13:21:06 +0100738 struct gpio_chip *chip = gdev->chip;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900739
Johan Hovold426577b2015-05-04 17:10:32 +0200740 /*
741 * Many systems add gpio chips for SOC support very early,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900742 * before driver model support is available. In those cases we
Johan Hovold426577b2015-05-04 17:10:32 +0200743 * register later, in gpiolib_sysfs_init() ... here we just
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900744 * verify that _some_ field of gpio_class got initialized.
745 */
746 if (!gpio_class.p)
747 return 0;
748
Bamvor Jian Zhangd27c1722016-02-24 22:17:19 +0800749 /*
750 * For sysfs backward compatibility we need to preserve this
751 * preferred parenting to the gpio_chip parent field, if set.
752 */
753 if (chip->parent)
754 parent = chip->parent;
755 else
756 parent = &gdev->dev;
757
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900758 /* use chip->base for the ID; it's already known to be unique */
Bamvor Jian Zhangd27c1722016-02-24 22:17:19 +0800759 dev = device_create_with_groups(&gpio_class, parent,
Linus Walleij58383c782015-11-04 09:56:26 +0100760 MKDEV(0, 0),
Johan Hovold121b6a72015-01-13 13:00:04 +0100761 chip, gpiochip_groups,
762 "gpiochip%d", chip->base);
763 if (IS_ERR(dev))
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200764 return PTR_ERR(dev);
Johan Hovold3ff74be2015-05-04 17:10:30 +0200765
766 mutex_lock(&sysfs_lock);
Linus Walleijafbc4f32016-02-09 13:21:06 +0100767 gdev->mockdev = dev;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900768 mutex_unlock(&sysfs_lock);
769
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200770 return 0;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900771}
772
Linus Walleijafbc4f32016-02-09 13:21:06 +0100773void gpiochip_sysfs_unregister(struct gpio_device *gdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900774{
Johan Hovold483d8212015-04-21 17:42:09 +0200775 struct gpio_desc *desc;
Linus Walleijafbc4f32016-02-09 13:21:06 +0100776 struct gpio_chip *chip = gdev->chip;
Johan Hovold483d8212015-04-21 17:42:09 +0200777 unsigned int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900778
Linus Walleijafbc4f32016-02-09 13:21:06 +0100779 if (!gdev->mockdev)
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200780 return;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900781
Linus Walleijafbc4f32016-02-09 13:21:06 +0100782 device_unregister(gdev->mockdev);
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200783
784 /* prevent further gpiod exports */
785 mutex_lock(&sysfs_lock);
Linus Walleijafbc4f32016-02-09 13:21:06 +0100786 gdev->mockdev = NULL;
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200787 mutex_unlock(&sysfs_lock);
Johan Hovold483d8212015-04-21 17:42:09 +0200788
789 /* unregister gpiod class devices owned by sysfs */
790 for (i = 0; i < chip->ngpio; i++) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100791 desc = &gdev->descs[i];
Johan Hovold483d8212015-04-21 17:42:09 +0200792 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
793 gpiod_free(desc);
794 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900795}
796
797static int __init gpiolib_sysfs_init(void)
798{
799 int status;
800 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +0200801 struct gpio_device *gdev;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900802
803 status = class_register(&gpio_class);
804 if (status < 0)
805 return status;
806
807 /* Scan and register the gpio_chips which registered very
808 * early (e.g. before the class_register above was called).
809 *
810 * We run before arch_initcall() so chip->dev nodes can have
811 * registered, and so arch_initcall() can always gpio_export().
812 */
813 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200814 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijafbc4f32016-02-09 13:21:06 +0100815 if (gdev->mockdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900816 continue;
817
Alexandre Courbot14141a92014-07-22 16:17:40 +0900818 /*
Johan Hovold426577b2015-05-04 17:10:32 +0200819 * TODO we yield gpio_lock here because
820 * gpiochip_sysfs_register() acquires a mutex. This is unsafe
821 * and needs to be fixed.
Alexandre Courbot14141a92014-07-22 16:17:40 +0900822 *
823 * Also it would be nice to use gpiochip_find() here so we
824 * can keep gpio_chips local to gpiolib.c, but the yield of
825 * gpio_lock prevents us from doing this.
826 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900827 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijafbc4f32016-02-09 13:21:06 +0100828 status = gpiochip_sysfs_register(gdev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900829 spin_lock_irqsave(&gpio_lock, flags);
830 }
831 spin_unlock_irqrestore(&gpio_lock, flags);
832
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900833 return status;
834}
835postcore_initcall(gpiolib_sysfs_init);