blob: ae7acd6a4f2845de825db5600acef607d255581b [file] [log] [blame]
Linus Walleijdae5f0a2018-09-25 09:08:48 +02001// SPDX-License-Identifier: GPL-2.0
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002#include <linux/idr.h>
3#include <linux/mutex.h>
4#include <linux/device.h>
5#include <linux/sysfs.h>
6#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>
Christophe Leroy1efba35a2017-12-18 11:08:35 +010011#include <linux/ctype.h>
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090012
13#include "gpiolib.h"
Kent Gibsonef087d82020-07-08 12:15:44 +080014#include "gpiolib-sysfs.h"
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090015
Johan Hovoldcef17172015-05-04 17:10:48 +020016#define GPIO_IRQF_TRIGGER_FALLING BIT(0)
17#define GPIO_IRQF_TRIGGER_RISING BIT(1)
18#define GPIO_IRQF_TRIGGER_BOTH (GPIO_IRQF_TRIGGER_FALLING | \
19 GPIO_IRQF_TRIGGER_RISING)
20
Johan Hovoldc43960f2015-05-04 17:10:37 +020021struct gpiod_data {
22 struct gpio_desc *desc;
Johan Hovold6ffcb792015-05-04 17:10:44 +020023
24 struct mutex mutex;
Johan Hovolda08f5c22015-05-04 17:10:39 +020025 struct kernfs_node *value_kn;
Johan Hovold2ec74a92015-05-04 17:10:41 +020026 int irq;
Johan Hovoldcef17172015-05-04 17:10:48 +020027 unsigned char irq_flags;
Johan Hovold427fdee2015-05-04 17:10:47 +020028
29 bool direction_can_change;
Johan Hovoldc43960f2015-05-04 17:10:37 +020030};
31
Johan Hovold6ffcb792015-05-04 17:10:44 +020032/*
33 * Lock to serialise gpiod export and unexport, and prevent re-export of
34 * gpiod whose chip is being unregistered.
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090035 */
36static DEFINE_MUTEX(sysfs_lock);
37
38/*
39 * /sys/class/gpio/gpioN... only for GPIOs that are exported
40 * /direction
41 * * MAY BE OMITTED if kernel won't allow direction changes
42 * * is read/write as "in" or "out"
43 * * may also be written as "high" or "low", initializing
44 * output value as specified ("out" implies "low")
45 * /value
46 * * always readable, subject to hardware behavior
47 * * may be writable, as zero/nonzero
48 * /edge
49 * * configures behavior of poll(2) on /value
50 * * available only if pin can generate IRQs on input
51 * * is read/write as "none", "falling", "rising", or "both"
52 * /active_low
53 * * configures polarity of /value
54 * * is read/write as zero/nonzero
55 * * also affects existing and subsequent "falling" and "rising"
56 * /edge configuration
57 */
58
Johan Hovold6beac9d12015-05-04 17:10:34 +020059static ssize_t direction_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090060 struct device_attribute *attr, char *buf)
61{
Johan Hovoldc43960f2015-05-04 17:10:37 +020062 struct gpiod_data *data = dev_get_drvdata(dev);
63 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090064 ssize_t status;
65
Johan Hovold6ffcb792015-05-04 17:10:44 +020066 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090067
Johan Hovoldf0b78662015-05-04 17:10:36 +020068 gpiod_get_direction(desc);
69 status = sprintf(buf, "%s\n",
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090070 test_bit(FLAG_IS_OUT, &desc->flags)
71 ? "out" : "in");
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090072
Johan Hovold6ffcb792015-05-04 17:10:44 +020073 mutex_unlock(&data->mutex);
74
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090075 return status;
76}
77
Johan Hovold6beac9d12015-05-04 17:10:34 +020078static ssize_t direction_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090079 struct device_attribute *attr, const char *buf, size_t size)
80{
Johan Hovoldc43960f2015-05-04 17:10:37 +020081 struct gpiod_data *data = dev_get_drvdata(dev);
82 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090083 ssize_t status;
84
Johan Hovold6ffcb792015-05-04 17:10:44 +020085 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090086
Johan Hovoldf0b78662015-05-04 17:10:36 +020087 if (sysfs_streq(buf, "high"))
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090088 status = gpiod_direction_output_raw(desc, 1);
89 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
90 status = gpiod_direction_output_raw(desc, 0);
91 else if (sysfs_streq(buf, "in"))
92 status = gpiod_direction_input(desc);
93 else
94 status = -EINVAL;
95
Johan Hovold6ffcb792015-05-04 17:10:44 +020096 mutex_unlock(&data->mutex);
97
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090098 return status ? : size;
99}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200100static DEVICE_ATTR_RW(direction);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900101
Johan Hovold6beac9d12015-05-04 17:10:34 +0200102static ssize_t value_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900103 struct device_attribute *attr, char *buf)
104{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200105 struct gpiod_data *data = dev_get_drvdata(dev);
106 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900107 ssize_t status;
108
Johan Hovold6ffcb792015-05-04 17:10:44 +0200109 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900110
Christophe Leroy9295c012017-12-18 11:08:31 +0100111 status = gpiod_get_value_cansleep(desc);
112 if (status < 0)
113 goto err;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900114
Christophe Leroy7a94b882017-12-18 11:08:33 +0100115 buf[0] = '0' + status;
116 buf[1] = '\n';
117 status = 2;
Christophe Leroy9295c012017-12-18 11:08:31 +0100118err:
Johan Hovold6ffcb792015-05-04 17:10:44 +0200119 mutex_unlock(&data->mutex);
120
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900121 return status;
122}
123
Johan Hovold6beac9d12015-05-04 17:10:34 +0200124static ssize_t value_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900125 struct device_attribute *attr, const char *buf, size_t size)
126{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200127 struct gpiod_data *data = dev_get_drvdata(dev);
128 struct gpio_desc *desc = data->desc;
Christophe Leroy1efba35a2017-12-18 11:08:35 +0100129 ssize_t status = 0;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900130
Johan Hovold6ffcb792015-05-04 17:10:44 +0200131 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900132
Johan Hovoldf0b78662015-05-04 17:10:36 +0200133 if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900134 status = -EPERM;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200135 } else {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900136 long value;
137
Christophe Leroy1efba35a2017-12-18 11:08:35 +0100138 if (size <= 2 && isdigit(buf[0]) &&
139 (size == 1 || buf[1] == '\n'))
140 value = buf[0] - '0';
141 else
142 status = kstrtol(buf, 0, &value);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900143 if (status == 0) {
144 gpiod_set_value_cansleep(desc, value);
145 status = size;
146 }
147 }
148
Johan Hovold6ffcb792015-05-04 17:10:44 +0200149 mutex_unlock(&data->mutex);
150
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900151 return status;
152}
Christophe Leroy7fda9102017-12-18 11:08:29 +0100153static DEVICE_ATTR_PREALLOC(value, S_IWUSR | S_IRUGO, value_show, value_store);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900154
155static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
156{
Johan Hovolda08f5c22015-05-04 17:10:39 +0200157 struct gpiod_data *data = priv;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900158
Johan Hovolda08f5c22015-05-04 17:10:39 +0200159 sysfs_notify_dirent(data->value_kn);
160
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900161 return IRQ_HANDLED;
162}
163
Johan Hovold6ffcb792015-05-04 17:10:44 +0200164/* Caller holds gpiod-data mutex. */
Johan Hovoldcef17172015-05-04 17:10:48 +0200165static int gpio_sysfs_request_irq(struct device *dev, unsigned char flags)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900166{
Johan Hovold0f628502015-05-04 17:10:38 +0200167 struct gpiod_data *data = dev_get_drvdata(dev);
168 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900169 unsigned long irq_flags;
Johan Hovold2ec74a92015-05-04 17:10:41 +0200170 int ret;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900171
Johan Hovold2ec74a92015-05-04 17:10:41 +0200172 data->irq = gpiod_to_irq(desc);
173 if (data->irq < 0)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900174 return -EIO;
175
Johan Hovold2ec74a92015-05-04 17:10:41 +0200176 data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
177 if (!data->value_kn)
178 return -ENODEV;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900179
180 irq_flags = IRQF_SHARED;
Johan Hovoldcef17172015-05-04 17:10:48 +0200181 if (flags & GPIO_IRQF_TRIGGER_FALLING)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900182 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
183 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Johan Hovoldcef17172015-05-04 17:10:48 +0200184 if (flags & GPIO_IRQF_TRIGGER_RISING)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900185 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
186 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
187
Johan Hovold52176d02015-05-04 17:10:28 +0200188 /*
189 * FIXME: This should be done in the irq_request_resources callback
190 * when the irq is requested, but a few drivers currently fail
191 * to do so.
192 *
193 * Remove this redundant call (along with the corresponding
194 * unlock) when those drivers have been fixed.
195 */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100196 ret = gpiochip_lock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900197 if (ret < 0)
Johan Hovold2ec74a92015-05-04 17:10:41 +0200198 goto err_put_kn;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900199
Johan Hovold2ec74a92015-05-04 17:10:41 +0200200 ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
Johan Hovolda08f5c22015-05-04 17:10:39 +0200201 "gpiolib", data);
Johan Hovold52176d02015-05-04 17:10:28 +0200202 if (ret < 0)
203 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900204
Johan Hovoldcef17172015-05-04 17:10:48 +0200205 data->irq_flags = flags;
Johan Hovold2ec74a92015-05-04 17:10:41 +0200206
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900207 return 0;
208
Johan Hovold52176d02015-05-04 17:10:28 +0200209err_unlock:
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100210 gpiochip_unlock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
Johan Hovold2ec74a92015-05-04 17:10:41 +0200211err_put_kn:
212 sysfs_put(data->value_kn);
213
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900214 return ret;
215}
216
Johan Hovold6ffcb792015-05-04 17:10:44 +0200217/*
218 * Caller holds gpiod-data mutex (unless called after class-device
219 * deregistration).
220 */
Johan Hovold2ec74a92015-05-04 17:10:41 +0200221static void gpio_sysfs_free_irq(struct device *dev)
222{
223 struct gpiod_data *data = dev_get_drvdata(dev);
224 struct gpio_desc *desc = data->desc;
225
Johan Hovoldcef17172015-05-04 17:10:48 +0200226 data->irq_flags = 0;
Johan Hovold2ec74a92015-05-04 17:10:41 +0200227 free_irq(data->irq, data);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100228 gpiochip_unlock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
Johan Hovold2ec74a92015-05-04 17:10:41 +0200229 sysfs_put(data->value_kn);
230}
231
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900232static const struct {
233 const char *name;
Johan Hovoldcef17172015-05-04 17:10:48 +0200234 unsigned char flags;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900235} trigger_types[] = {
236 { "none", 0 },
Johan Hovoldcef17172015-05-04 17:10:48 +0200237 { "falling", GPIO_IRQF_TRIGGER_FALLING },
238 { "rising", GPIO_IRQF_TRIGGER_RISING },
239 { "both", GPIO_IRQF_TRIGGER_BOTH },
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900240};
241
Johan Hovold6beac9d12015-05-04 17:10:34 +0200242static ssize_t edge_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900243 struct device_attribute *attr, char *buf)
244{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200245 struct gpiod_data *data = dev_get_drvdata(dev);
Johan Hovoldf0b78662015-05-04 17:10:36 +0200246 ssize_t status = 0;
247 int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900248
Johan Hovold6ffcb792015-05-04 17:10:44 +0200249 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900250
Johan Hovoldf0b78662015-05-04 17:10:36 +0200251 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
Johan Hovoldcef17172015-05-04 17:10:48 +0200252 if (data->irq_flags == trigger_types[i].flags) {
Johan Hovoldf0b78662015-05-04 17:10:36 +0200253 status = sprintf(buf, "%s\n", trigger_types[i].name);
254 break;
255 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900256 }
257
Johan Hovold6ffcb792015-05-04 17:10:44 +0200258 mutex_unlock(&data->mutex);
259
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900260 return status;
261}
262
Johan Hovold6beac9d12015-05-04 17:10:34 +0200263static ssize_t edge_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900264 struct device_attribute *attr, const char *buf, size_t size)
265{
Johan Hovoldb91e1802015-05-04 17:10:40 +0200266 struct gpiod_data *data = dev_get_drvdata(dev);
Johan Hovoldcef17172015-05-04 17:10:48 +0200267 unsigned char flags;
Johan Hovold2ec74a92015-05-04 17:10:41 +0200268 ssize_t status = size;
Johan Hovolde4339ce2015-05-04 17:10:42 +0200269 int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900270
Johan Hovolde4339ce2015-05-04 17:10:42 +0200271 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900272 if (sysfs_streq(trigger_types[i].name, buf))
Johan Hovolde4339ce2015-05-04 17:10:42 +0200273 break;
274 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900275
Johan Hovolde4339ce2015-05-04 17:10:42 +0200276 if (i == ARRAY_SIZE(trigger_types))
277 return -EINVAL;
278
Johan Hovoldb91e1802015-05-04 17:10:40 +0200279 flags = trigger_types[i].flags;
280
Johan Hovold6ffcb792015-05-04 17:10:44 +0200281 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900282
Johan Hovoldcef17172015-05-04 17:10:48 +0200283 if (flags == data->irq_flags) {
Johan Hovoldb91e1802015-05-04 17:10:40 +0200284 status = size;
285 goto out_unlock;
286 }
287
Johan Hovoldcef17172015-05-04 17:10:48 +0200288 if (data->irq_flags)
Johan Hovold2ec74a92015-05-04 17:10:41 +0200289 gpio_sysfs_free_irq(dev);
290
291 if (flags) {
292 status = gpio_sysfs_request_irq(dev, flags);
293 if (!status)
294 status = size;
295 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900296
Johan Hovoldb91e1802015-05-04 17:10:40 +0200297out_unlock:
Johan Hovold6ffcb792015-05-04 17:10:44 +0200298 mutex_unlock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900299
300 return status;
301}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200302static DEVICE_ATTR_RW(edge);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900303
Johan Hovold6ffcb792015-05-04 17:10:44 +0200304/* Caller holds gpiod-data mutex. */
Johan Hovold2f323b82015-05-04 17:10:46 +0200305static int gpio_sysfs_set_active_low(struct device *dev, int value)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900306{
Johan Hovold0f628502015-05-04 17:10:38 +0200307 struct gpiod_data *data = dev_get_drvdata(dev);
308 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900309 int status = 0;
Johan Hovoldcef17172015-05-04 17:10:48 +0200310 unsigned int flags = data->irq_flags;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900311
312 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
313 return 0;
314
Andy Shevchenkofd80b8b2021-05-18 11:46:19 +0300315 assign_bit(FLAG_ACTIVE_LOW, &desc->flags, value);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900316
317 /* reconfigure poll(2) support if enabled on one edge only */
Johan Hovoldcef17172015-05-04 17:10:48 +0200318 if (flags == GPIO_IRQF_TRIGGER_FALLING ||
319 flags == GPIO_IRQF_TRIGGER_RISING) {
Johan Hovold2ec74a92015-05-04 17:10:41 +0200320 gpio_sysfs_free_irq(dev);
Johan Hovoldcef17172015-05-04 17:10:48 +0200321 status = gpio_sysfs_request_irq(dev, flags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900322 }
323
324 return status;
325}
326
Johan Hovold6beac9d12015-05-04 17:10:34 +0200327static ssize_t active_low_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900328 struct device_attribute *attr, char *buf)
329{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200330 struct gpiod_data *data = dev_get_drvdata(dev);
331 struct gpio_desc *desc = data->desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900332 ssize_t status;
333
Johan Hovold6ffcb792015-05-04 17:10:44 +0200334 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900335
Johan Hovoldf0b78662015-05-04 17:10:36 +0200336 status = sprintf(buf, "%d\n",
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900337 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
338
Johan Hovold6ffcb792015-05-04 17:10:44 +0200339 mutex_unlock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900340
341 return status;
342}
343
Johan Hovold6beac9d12015-05-04 17:10:34 +0200344static ssize_t active_low_store(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900345 struct device_attribute *attr, const char *buf, size_t size)
346{
Johan Hovold6ffcb792015-05-04 17:10:44 +0200347 struct gpiod_data *data = dev_get_drvdata(dev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900348 ssize_t status;
Johan Hovoldf0b78662015-05-04 17:10:36 +0200349 long value;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900350
Johan Hovold6ffcb792015-05-04 17:10:44 +0200351 mutex_lock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900352
Johan Hovoldf0b78662015-05-04 17:10:36 +0200353 status = kstrtol(buf, 0, &value);
354 if (status == 0)
Johan Hovold2f323b82015-05-04 17:10:46 +0200355 status = gpio_sysfs_set_active_low(dev, value);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900356
Johan Hovold6ffcb792015-05-04 17:10:44 +0200357 mutex_unlock(&data->mutex);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900358
359 return status ? : size;
360}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200361static DEVICE_ATTR_RW(active_low);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900362
Johan Hovoldebbeba12015-01-13 13:00:06 +0100363static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
364 int n)
365{
Wang Qing97cd7382020-06-12 16:05:45 +0800366 struct device *dev = kobj_to_dev(kobj);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200367 struct gpiod_data *data = dev_get_drvdata(dev);
368 struct gpio_desc *desc = data->desc;
Johan Hovoldebbeba12015-01-13 13:00:06 +0100369 umode_t mode = attr->mode;
Johan Hovold427fdee2015-05-04 17:10:47 +0200370 bool show_direction = data->direction_can_change;
Johan Hovoldebbeba12015-01-13 13:00:06 +0100371
372 if (attr == &dev_attr_direction.attr) {
373 if (!show_direction)
374 mode = 0;
375 } else if (attr == &dev_attr_edge.attr) {
376 if (gpiod_to_irq(desc) < 0)
377 mode = 0;
378 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
379 mode = 0;
380 }
381
382 return mode;
383}
384
Johan Hovold0915e6f2015-01-13 13:00:05 +0100385static struct attribute *gpio_attrs[] = {
Johan Hovoldebbeba12015-01-13 13:00:06 +0100386 &dev_attr_direction.attr,
387 &dev_attr_edge.attr,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900388 &dev_attr_value.attr,
389 &dev_attr_active_low.attr,
390 NULL,
391};
Johan Hovoldebbeba12015-01-13 13:00:06 +0100392
393static const struct attribute_group gpio_group = {
394 .attrs = gpio_attrs,
395 .is_visible = gpio_is_visible,
396};
397
398static const struct attribute_group *gpio_groups[] = {
399 &gpio_group,
400 NULL
401};
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900402
403/*
404 * /sys/class/gpio/gpiochipN/
405 * /base ... matching gpio_chip.base (N)
406 * /label ... matching gpio_chip.label
407 * /ngpio ... matching gpio_chip.ngpio
408 */
409
Johan Hovold6beac9d12015-05-04 17:10:34 +0200410static ssize_t base_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, "%d\n", chip->base);
416}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200417static DEVICE_ATTR_RO(base);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900418
Johan Hovold6beac9d12015-05-04 17:10:34 +0200419static ssize_t label_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, "%s\n", chip->label ? : "");
425}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200426static DEVICE_ATTR_RO(label);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900427
Johan Hovold6beac9d12015-05-04 17:10:34 +0200428static ssize_t ngpio_show(struct device *dev,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900429 struct device_attribute *attr, char *buf)
430{
431 const struct gpio_chip *chip = dev_get_drvdata(dev);
432
433 return sprintf(buf, "%u\n", chip->ngpio);
434}
Johan Hovold6beac9d12015-05-04 17:10:34 +0200435static DEVICE_ATTR_RO(ngpio);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900436
Johan Hovold121b6a72015-01-13 13:00:04 +0100437static struct attribute *gpiochip_attrs[] = {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900438 &dev_attr_base.attr,
439 &dev_attr_label.attr,
440 &dev_attr_ngpio.attr,
441 NULL,
442};
Johan Hovold121b6a72015-01-13 13:00:04 +0100443ATTRIBUTE_GROUPS(gpiochip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900444
445/*
446 * /sys/class/gpio/export ... write-only
447 * integer N ... number of GPIO to export (full access)
448 * /sys/class/gpio/unexport ... write-only
449 * integer N ... number of GPIO to unexport
450 */
451static ssize_t export_store(struct class *class,
452 struct class_attribute *attr,
453 const char *buf, size_t len)
454{
455 long gpio;
456 struct gpio_desc *desc;
457 int status;
Matti Vaittinen23cf00d2021-03-29 14:41:12 +0300458 struct gpio_chip *gc;
459 int offset;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900460
461 status = kstrtol(buf, 0, &gpio);
462 if (status < 0)
463 goto done;
464
Linus Walleijf13a0b02018-09-13 14:05:26 +0200465 desc = gpio_to_desc(gpio);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900466 /* reject invalid GPIOs */
467 if (!desc) {
468 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
469 return -EINVAL;
470 }
Matti Vaittinen23cf00d2021-03-29 14:41:12 +0300471 gc = desc->gdev->chip;
472 offset = gpio_chip_hwgpio(desc);
473 if (!gpiochip_line_is_valid(gc, offset)) {
474 pr_warn("%s: GPIO %ld masked\n", __func__, gpio);
475 return -EINVAL;
476 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900477
478 /* No extra locking here; FLAG_SYSFS just signifies that the
479 * request and export were done by on behalf of userspace, so
480 * they may be undone on its behalf too.
481 */
482
483 status = gpiod_request(desc, "sysfs");
Andy Shevchenko8bbff392020-10-21 14:25:36 +0300484 if (status) {
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900485 if (status == -EPROBE_DEFER)
486 status = -ENODEV;
487 goto done;
488 }
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030489
490 status = gpiod_set_transitory(desc, false);
491 if (!status) {
492 status = gpiod_export(desc, true);
493 if (status < 0)
494 gpiod_free(desc);
495 else
496 set_bit(FLAG_SYSFS, &desc->flags);
497 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900498
499done:
500 if (status)
501 pr_debug("%s: status %d\n", __func__, status);
502 return status ? : len;
503}
Greg Kroah-Hartmand83bb152017-06-08 10:12:40 +0200504static CLASS_ATTR_WO(export);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900505
506static ssize_t unexport_store(struct class *class,
507 struct class_attribute *attr,
508 const char *buf, size_t len)
509{
510 long gpio;
511 struct gpio_desc *desc;
512 int status;
513
514 status = kstrtol(buf, 0, &gpio);
515 if (status < 0)
516 goto done;
517
Linus Walleijf13a0b02018-09-13 14:05:26 +0200518 desc = gpio_to_desc(gpio);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900519 /* reject bogus commands (gpio_unexport ignores them) */
520 if (!desc) {
521 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
522 return -EINVAL;
523 }
524
525 status = -EINVAL;
526
527 /* No extra locking here; FLAG_SYSFS just signifies that the
528 * request and export were done by on behalf of userspace, so
529 * they may be undone on its behalf too.
530 */
531 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
532 status = 0;
533 gpiod_free(desc);
534 }
535done:
536 if (status)
537 pr_debug("%s: status %d\n", __func__, status);
538 return status ? : len;
539}
Greg Kroah-Hartmand83bb152017-06-08 10:12:40 +0200540static CLASS_ATTR_WO(unexport);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900541
Greg Kroah-Hartmand83bb152017-06-08 10:12:40 +0200542static struct attribute *gpio_class_attrs[] = {
543 &class_attr_export.attr,
544 &class_attr_unexport.attr,
545 NULL,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900546};
Greg Kroah-Hartmand83bb152017-06-08 10:12:40 +0200547ATTRIBUTE_GROUPS(gpio_class);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900548
549static struct class gpio_class = {
550 .name = "gpio",
551 .owner = THIS_MODULE,
552
Greg Kroah-Hartmand83bb152017-06-08 10:12:40 +0200553 .class_groups = gpio_class_groups,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900554};
555
556
557/**
558 * gpiod_export - export a GPIO through sysfs
Thierry Reding2d9d0512017-07-24 16:57:26 +0200559 * @desc: GPIO to make available, already requested
560 * @direction_may_change: true if userspace may change GPIO direction
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900561 * Context: arch_initcall or later
562 *
563 * When drivers want to make a GPIO accessible to userspace after they
564 * have requested it -- perhaps while debugging, or as part of their
565 * public interface -- they may use this routine. If the GPIO can
566 * change direction (some can't) and the caller allows it, userspace
567 * will see "direction" sysfs attribute which may be used to change
568 * the gpio's direction. A "value" attribute will always be provided.
569 *
570 * Returns zero on success, else an error.
571 */
572int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
573{
Johan Hovold483d8212015-04-21 17:42:09 +0200574 struct gpio_chip *chip;
Linus Walleijff2b1352015-10-20 11:10:38 +0200575 struct gpio_device *gdev;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200576 struct gpiod_data *data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900577 unsigned long flags;
578 int status;
579 const char *ioname = NULL;
580 struct device *dev;
581 int offset;
582
583 /* can't export until sysfs is available ... */
584 if (!gpio_class.p) {
585 pr_debug("%s: called too early!\n", __func__);
586 return -ENOENT;
587 }
588
589 if (!desc) {
590 pr_debug("%s: invalid gpio descriptor\n", __func__);
591 return -EINVAL;
592 }
593
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100594 gdev = desc->gdev;
595 chip = gdev->chip;
Johan Hovold483d8212015-04-21 17:42:09 +0200596
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900597 mutex_lock(&sysfs_lock);
598
Johan Hovold483d8212015-04-21 17:42:09 +0200599 /* check if chip is being removed */
Linus Walleijafbc4f32016-02-09 13:21:06 +0100600 if (!chip || !gdev->mockdev) {
Johan Hovold483d8212015-04-21 17:42:09 +0200601 status = -ENODEV;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200602 goto err_unlock;
Johan Hovold483d8212015-04-21 17:42:09 +0200603 }
604
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900605 spin_lock_irqsave(&gpio_lock, flags);
606 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
607 test_bit(FLAG_EXPORT, &desc->flags)) {
608 spin_unlock_irqrestore(&gpio_lock, flags);
609 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
610 __func__,
611 test_bit(FLAG_REQUESTED, &desc->flags),
612 test_bit(FLAG_EXPORT, &desc->flags));
613 status = -EPERM;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200614 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900615 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900616 spin_unlock_irqrestore(&gpio_lock, flags);
617
Johan Hovoldc43960f2015-05-04 17:10:37 +0200618 data = kzalloc(sizeof(*data), GFP_KERNEL);
619 if (!data) {
620 status = -ENOMEM;
621 goto err_unlock;
622 }
623
624 data->desc = desc;
Johan Hovold6ffcb792015-05-04 17:10:44 +0200625 mutex_init(&data->mutex);
Johan Hovold427fdee2015-05-04 17:10:47 +0200626 if (chip->direction_input && chip->direction_output)
627 data->direction_can_change = direction_may_change;
628 else
629 data->direction_can_change = false;
Johan Hovoldc43960f2015-05-04 17:10:37 +0200630
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900631 offset = gpio_chip_hwgpio(desc);
Johan Hovoldcecf58a2015-05-04 17:10:29 +0200632 if (chip->names && chip->names[offset])
633 ioname = chip->names[offset];
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900634
Linus Walleijff2b1352015-10-20 11:10:38 +0200635 dev = device_create_with_groups(&gpio_class, &gdev->dev,
Johan Hovoldc43960f2015-05-04 17:10:37 +0200636 MKDEV(0, 0), data, gpio_groups,
Johan Hovold0915e6f2015-01-13 13:00:05 +0100637 ioname ? ioname : "gpio%u",
638 desc_to_gpio(desc));
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900639 if (IS_ERR(dev)) {
640 status = PTR_ERR(dev);
Johan Hovoldc43960f2015-05-04 17:10:37 +0200641 goto err_free_data;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900642 }
643
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900644 set_bit(FLAG_EXPORT, &desc->flags);
645 mutex_unlock(&sysfs_lock);
646 return 0;
647
Johan Hovoldc43960f2015-05-04 17:10:37 +0200648err_free_data:
649 kfree(data);
650err_unlock:
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900651 mutex_unlock(&sysfs_lock);
652 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
653 return status;
654}
655EXPORT_SYMBOL_GPL(gpiod_export);
656
Johan Hovoldc43960f2015-05-04 17:10:37 +0200657static int match_export(struct device *dev, const void *desc)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900658{
Johan Hovoldc43960f2015-05-04 17:10:37 +0200659 struct gpiod_data *data = dev_get_drvdata(dev);
660
661 return data->desc == desc;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900662}
663
664/**
665 * gpiod_export_link - create a sysfs link to an exported GPIO node
666 * @dev: device under which to create symlink
667 * @name: name of the symlink
Thierry Reding2d9d0512017-07-24 16:57:26 +0200668 * @desc: GPIO to create symlink to, already exported
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900669 *
670 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
671 * node. Caller is responsible for unlinking.
672 *
673 * Returns zero on success, else an error.
674 */
675int gpiod_export_link(struct device *dev, const char *name,
676 struct gpio_desc *desc)
677{
Johan Hovold56d30ec2015-05-04 17:10:43 +0200678 struct device *cdev;
679 int ret;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900680
681 if (!desc) {
682 pr_warn("%s: invalid GPIO\n", __func__);
683 return -EINVAL;
684 }
685
Johan Hovold56d30ec2015-05-04 17:10:43 +0200686 cdev = class_find_device(&gpio_class, NULL, desc, match_export);
687 if (!cdev)
688 return -ENODEV;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900689
Johan Hovold56d30ec2015-05-04 17:10:43 +0200690 ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
691 put_device(cdev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900692
Johan Hovold56d30ec2015-05-04 17:10:43 +0200693 return ret;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900694}
695EXPORT_SYMBOL_GPL(gpiod_export_link);
696
697/**
Amitesh Singh31963eb2016-09-08 17:11:20 +0530698 * gpiod_unexport - reverse effect of gpiod_export()
Thierry Reding2d9d0512017-07-24 16:57:26 +0200699 * @desc: GPIO to make unavailable
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900700 *
Amitesh Singh31963eb2016-09-08 17:11:20 +0530701 * This is implicit on gpiod_free().
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900702 */
703void gpiod_unexport(struct gpio_desc *desc)
704{
Johan Hovold72eba6f2015-05-04 17:10:45 +0200705 struct gpiod_data *data;
706 struct device *dev;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900707
708 if (!desc) {
709 pr_warn("%s: invalid GPIO\n", __func__);
710 return;
711 }
712
713 mutex_lock(&sysfs_lock);
714
Johan Hovold72eba6f2015-05-04 17:10:45 +0200715 if (!test_bit(FLAG_EXPORT, &desc->flags))
716 goto err_unlock;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900717
Johan Hovold72eba6f2015-05-04 17:10:45 +0200718 dev = class_find_device(&gpio_class, NULL, desc, match_export);
719 if (!dev)
720 goto err_unlock;
721
722 data = dev_get_drvdata(dev);
723
Johan Hovold72eba6f2015-05-04 17:10:45 +0200724 clear_bit(FLAG_EXPORT, &desc->flags);
725
726 device_unregister(dev);
727
728 /*
729 * Release irq after deregistration to prevent race with edge_store.
730 */
Johan Hovoldcef17172015-05-04 17:10:48 +0200731 if (data->irq_flags)
Johan Hovold72eba6f2015-05-04 17:10:45 +0200732 gpio_sysfs_free_irq(dev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900733
734 mutex_unlock(&sysfs_lock);
735
Johan Hovold72eba6f2015-05-04 17:10:45 +0200736 put_device(dev);
737 kfree(data);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900738
Johan Hovold72eba6f2015-05-04 17:10:45 +0200739 return;
740
741err_unlock:
742 mutex_unlock(&sysfs_lock);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900743}
744EXPORT_SYMBOL_GPL(gpiod_unexport);
745
Linus Walleijafbc4f32016-02-09 13:21:06 +0100746int gpiochip_sysfs_register(struct gpio_device *gdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900747{
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900748 struct device *dev;
Bamvor Jian Zhangd27c1722016-02-24 22:17:19 +0800749 struct device *parent;
Linus Walleijafbc4f32016-02-09 13:21:06 +0100750 struct gpio_chip *chip = gdev->chip;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900751
Johan Hovold426577b2015-05-04 17:10:32 +0200752 /*
753 * Many systems add gpio chips for SOC support very early,
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900754 * before driver model support is available. In those cases we
Johan Hovold426577b2015-05-04 17:10:32 +0200755 * register later, in gpiolib_sysfs_init() ... here we just
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900756 * verify that _some_ field of gpio_class got initialized.
757 */
758 if (!gpio_class.p)
759 return 0;
760
Bamvor Jian Zhangd27c1722016-02-24 22:17:19 +0800761 /*
762 * For sysfs backward compatibility we need to preserve this
763 * preferred parenting to the gpio_chip parent field, if set.
764 */
765 if (chip->parent)
766 parent = chip->parent;
767 else
768 parent = &gdev->dev;
769
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900770 /* use chip->base for the ID; it's already known to be unique */
Geert Uytterhoevenddd88912019-11-27 09:42:47 +0100771 dev = device_create_with_groups(&gpio_class, parent, MKDEV(0, 0), chip,
772 gpiochip_groups, GPIOCHIP_NAME "%d",
773 chip->base);
Johan Hovold121b6a72015-01-13 13:00:04 +0100774 if (IS_ERR(dev))
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200775 return PTR_ERR(dev);
Johan Hovold3ff74be2015-05-04 17:10:30 +0200776
777 mutex_lock(&sysfs_lock);
Linus Walleijafbc4f32016-02-09 13:21:06 +0100778 gdev->mockdev = dev;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900779 mutex_unlock(&sysfs_lock);
780
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200781 return 0;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900782}
783
Linus Walleijafbc4f32016-02-09 13:21:06 +0100784void gpiochip_sysfs_unregister(struct gpio_device *gdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900785{
Johan Hovold483d8212015-04-21 17:42:09 +0200786 struct gpio_desc *desc;
Linus Walleijafbc4f32016-02-09 13:21:06 +0100787 struct gpio_chip *chip = gdev->chip;
Johan Hovold483d8212015-04-21 17:42:09 +0200788 unsigned int i;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900789
Linus Walleijafbc4f32016-02-09 13:21:06 +0100790 if (!gdev->mockdev)
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200791 return;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900792
Linus Walleijafbc4f32016-02-09 13:21:06 +0100793 device_unregister(gdev->mockdev);
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200794
795 /* prevent further gpiod exports */
796 mutex_lock(&sysfs_lock);
Linus Walleijafbc4f32016-02-09 13:21:06 +0100797 gdev->mockdev = NULL;
Johan Hovold6a4b6b02015-05-04 17:10:31 +0200798 mutex_unlock(&sysfs_lock);
Johan Hovold483d8212015-04-21 17:42:09 +0200799
800 /* unregister gpiod class devices owned by sysfs */
801 for (i = 0; i < chip->ngpio; i++) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100802 desc = &gdev->descs[i];
Johan Hovold483d8212015-04-21 17:42:09 +0200803 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
804 gpiod_free(desc);
805 }
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900806}
807
808static int __init gpiolib_sysfs_init(void)
809{
810 int status;
811 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +0200812 struct gpio_device *gdev;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900813
814 status = class_register(&gpio_class);
815 if (status < 0)
816 return status;
817
818 /* Scan and register the gpio_chips which registered very
819 * early (e.g. before the class_register above was called).
820 *
821 * We run before arch_initcall() so chip->dev nodes can have
822 * registered, and so arch_initcall() can always gpio_export().
823 */
824 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200825 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijafbc4f32016-02-09 13:21:06 +0100826 if (gdev->mockdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900827 continue;
828
Alexandre Courbot14141a92014-07-22 16:17:40 +0900829 /*
Johan Hovold426577b2015-05-04 17:10:32 +0200830 * TODO we yield gpio_lock here because
831 * gpiochip_sysfs_register() acquires a mutex. This is unsafe
832 * and needs to be fixed.
Alexandre Courbot14141a92014-07-22 16:17:40 +0900833 *
834 * Also it would be nice to use gpiochip_find() here so we
835 * can keep gpio_chips local to gpiolib.c, but the yield of
836 * gpio_lock prevents us from doing this.
837 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900838 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijafbc4f32016-02-09 13:21:06 +0100839 status = gpiochip_sysfs_register(gdev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900840 spin_lock_irqsave(&gpio_lock, flags);
841 }
842 spin_unlock_irqrestore(&gpio_lock, flags);
843
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900844 return status;
845}
846postcore_initcall(gpiolib_sysfs_init);