Commit | Line | Data |
---|---|---|
dae5f0af | 1 | // SPDX-License-Identifier: GPL-2.0 |
380c7ba3 AS |
2 | |
3 | #include <linux/bitops.h> | |
59cba4a0 | 4 | #include <linux/cleanup.h> |
380c7ba3 | 5 | #include <linux/device.h> |
0eb4c6c2 | 6 | #include <linux/idr.h> |
380c7ba3 AS |
7 | #include <linux/init.h> |
8 | #include <linux/interrupt.h> | |
9 | #include <linux/kdev_t.h> | |
10 | #include <linux/kstrtox.h> | |
11 | #include <linux/list.h> | |
0eb4c6c2 | 12 | #include <linux/mutex.h> |
380c7ba3 AS |
13 | #include <linux/printk.h> |
14 | #include <linux/slab.h> | |
15 | #include <linux/spinlock.h> | |
16 | #include <linux/string.h> | |
d82b9e08 | 17 | #include <linux/srcu.h> |
0eb4c6c2 | 18 | #include <linux/sysfs.h> |
380c7ba3 AS |
19 | #include <linux/types.h> |
20 | ||
0eb4c6c2 AC |
21 | #include <linux/gpio/consumer.h> |
22 | #include <linux/gpio/driver.h> | |
0eb4c6c2 AC |
23 | |
24 | #include "gpiolib.h" | |
ef087d8e | 25 | #include "gpiolib-sysfs.h" |
0eb4c6c2 | 26 | |
380c7ba3 AS |
27 | struct kernfs_node; |
28 | ||
667630ed | 29 | #define GPIO_IRQF_TRIGGER_NONE 0 |
cef1717b JH |
30 | #define GPIO_IRQF_TRIGGER_FALLING BIT(0) |
31 | #define GPIO_IRQF_TRIGGER_RISING BIT(1) | |
32 | #define GPIO_IRQF_TRIGGER_BOTH (GPIO_IRQF_TRIGGER_FALLING | \ | |
33 | GPIO_IRQF_TRIGGER_RISING) | |
34 | ||
c43960fb JH |
35 | struct gpiod_data { |
36 | struct gpio_desc *desc; | |
6ffcb797 JH |
37 | |
38 | struct mutex mutex; | |
a08f5c21 | 39 | struct kernfs_node *value_kn; |
2ec74a95 | 40 | int irq; |
cef1717b | 41 | unsigned char irq_flags; |
427fdeef JH |
42 | |
43 | bool direction_can_change; | |
c43960fb JH |
44 | }; |
45 | ||
6ffcb797 JH |
46 | /* |
47 | * Lock to serialise gpiod export and unexport, and prevent re-export of | |
48 | * gpiod whose chip is being unregistered. | |
0eb4c6c2 AC |
49 | */ |
50 | static DEFINE_MUTEX(sysfs_lock); | |
51 | ||
52 | /* | |
53 | * /sys/class/gpio/gpioN... only for GPIOs that are exported | |
54 | * /direction | |
55 | * * MAY BE OMITTED if kernel won't allow direction changes | |
56 | * * is read/write as "in" or "out" | |
57 | * * may also be written as "high" or "low", initializing | |
58 | * output value as specified ("out" implies "low") | |
59 | * /value | |
60 | * * always readable, subject to hardware behavior | |
61 | * * may be writable, as zero/nonzero | |
62 | * /edge | |
63 | * * configures behavior of poll(2) on /value | |
64 | * * available only if pin can generate IRQs on input | |
65 | * * is read/write as "none", "falling", "rising", or "both" | |
66 | * /active_low | |
67 | * * configures polarity of /value | |
68 | * * is read/write as zero/nonzero | |
69 | * * also affects existing and subsequent "falling" and "rising" | |
70 | * /edge configuration | |
71 | */ | |
72 | ||
6beac9d1 | 73 | static ssize_t direction_show(struct device *dev, |
0eb4c6c2 AC |
74 | struct device_attribute *attr, char *buf) |
75 | { | |
c43960fb JH |
76 | struct gpiod_data *data = dev_get_drvdata(dev); |
77 | struct gpio_desc *desc = data->desc; | |
e28747da | 78 | int value; |
0eb4c6c2 | 79 | |
6ffcb797 | 80 | mutex_lock(&data->mutex); |
0eb4c6c2 | 81 | |
f0b7866a | 82 | gpiod_get_direction(desc); |
e28747da | 83 | value = !!test_bit(FLAG_IS_OUT, &desc->flags); |
0eb4c6c2 | 84 | |
6ffcb797 JH |
85 | mutex_unlock(&data->mutex); |
86 | ||
e28747da | 87 | return sysfs_emit(buf, "%s\n", value ? "out" : "in"); |
0eb4c6c2 AC |
88 | } |
89 | ||
6beac9d1 | 90 | static ssize_t direction_store(struct device *dev, |
0eb4c6c2 AC |
91 | struct device_attribute *attr, const char *buf, size_t size) |
92 | { | |
c43960fb JH |
93 | struct gpiod_data *data = dev_get_drvdata(dev); |
94 | struct gpio_desc *desc = data->desc; | |
0eb4c6c2 AC |
95 | ssize_t status; |
96 | ||
6ffcb797 | 97 | mutex_lock(&data->mutex); |
0eb4c6c2 | 98 | |
f0b7866a | 99 | if (sysfs_streq(buf, "high")) |
0eb4c6c2 AC |
100 | status = gpiod_direction_output_raw(desc, 1); |
101 | else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) | |
102 | status = gpiod_direction_output_raw(desc, 0); | |
103 | else if (sysfs_streq(buf, "in")) | |
104 | status = gpiod_direction_input(desc); | |
105 | else | |
106 | status = -EINVAL; | |
107 | ||
6ffcb797 JH |
108 | mutex_unlock(&data->mutex); |
109 | ||
0eb4c6c2 AC |
110 | return status ? : size; |
111 | } | |
6beac9d1 | 112 | static DEVICE_ATTR_RW(direction); |
0eb4c6c2 | 113 | |
6beac9d1 | 114 | static ssize_t value_show(struct device *dev, |
0eb4c6c2 AC |
115 | struct device_attribute *attr, char *buf) |
116 | { | |
c43960fb JH |
117 | struct gpiod_data *data = dev_get_drvdata(dev); |
118 | struct gpio_desc *desc = data->desc; | |
0eb4c6c2 AC |
119 | ssize_t status; |
120 | ||
6ffcb797 | 121 | mutex_lock(&data->mutex); |
0eb4c6c2 | 122 | |
9295c012 | 123 | status = gpiod_get_value_cansleep(desc); |
0eb4c6c2 | 124 | |
6ffcb797 JH |
125 | mutex_unlock(&data->mutex); |
126 | ||
e28747da AS |
127 | if (status < 0) |
128 | return status; | |
129 | ||
130 | return sysfs_emit(buf, "%zd\n", status); | |
0eb4c6c2 AC |
131 | } |
132 | ||
6beac9d1 | 133 | static ssize_t value_store(struct device *dev, |
0eb4c6c2 AC |
134 | struct device_attribute *attr, const char *buf, size_t size) |
135 | { | |
c43960fb JH |
136 | struct gpiod_data *data = dev_get_drvdata(dev); |
137 | struct gpio_desc *desc = data->desc; | |
6b3c1791 AS |
138 | ssize_t status; |
139 | long value; | |
140 | ||
141 | status = kstrtol(buf, 0, &value); | |
0eb4c6c2 | 142 | |
6ffcb797 | 143 | mutex_lock(&data->mutex); |
0eb4c6c2 | 144 | |
f0b7866a | 145 | if (!test_bit(FLAG_IS_OUT, &desc->flags)) { |
0eb4c6c2 | 146 | status = -EPERM; |
6b3c1791 AS |
147 | } else if (status == 0) { |
148 | gpiod_set_value_cansleep(desc, value); | |
149 | status = size; | |
0eb4c6c2 AC |
150 | } |
151 | ||
6ffcb797 JH |
152 | mutex_unlock(&data->mutex); |
153 | ||
0eb4c6c2 AC |
154 | return status; |
155 | } | |
7fda9100 | 156 | static DEVICE_ATTR_PREALLOC(value, S_IWUSR | S_IRUGO, value_show, value_store); |
0eb4c6c2 AC |
157 | |
158 | static irqreturn_t gpio_sysfs_irq(int irq, void *priv) | |
159 | { | |
a08f5c21 JH |
160 | struct gpiod_data *data = priv; |
161 | ||
162 | sysfs_notify_dirent(data->value_kn); | |
0eb4c6c2 | 163 | |
0eb4c6c2 AC |
164 | return IRQ_HANDLED; |
165 | } | |
166 | ||
6ffcb797 | 167 | /* Caller holds gpiod-data mutex. */ |
cef1717b | 168 | static int gpio_sysfs_request_irq(struct device *dev, unsigned char flags) |
0eb4c6c2 | 169 | { |
513246a3 BG |
170 | struct gpiod_data *data = dev_get_drvdata(dev); |
171 | struct gpio_desc *desc = data->desc; | |
172 | unsigned long irq_flags; | |
173 | int ret; | |
0eb4c6c2 | 174 | |
d83cee3d BG |
175 | CLASS(gpio_chip_guard, guard)(desc); |
176 | if (!guard.gc) | |
177 | return -ENODEV; | |
178 | ||
2ec74a95 JH |
179 | data->irq = gpiod_to_irq(desc); |
180 | if (data->irq < 0) | |
0eb4c6c2 AC |
181 | return -EIO; |
182 | ||
2ec74a95 JH |
183 | data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value"); |
184 | if (!data->value_kn) | |
185 | return -ENODEV; | |
0eb4c6c2 AC |
186 | |
187 | irq_flags = IRQF_SHARED; | |
cef1717b | 188 | if (flags & GPIO_IRQF_TRIGGER_FALLING) |
0eb4c6c2 AC |
189 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
190 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; | |
cef1717b | 191 | if (flags & GPIO_IRQF_TRIGGER_RISING) |
0eb4c6c2 AC |
192 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
193 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; | |
194 | ||
52176d0d JH |
195 | /* |
196 | * FIXME: This should be done in the irq_request_resources callback | |
197 | * when the irq is requested, but a few drivers currently fail | |
198 | * to do so. | |
199 | * | |
200 | * Remove this redundant call (along with the corresponding | |
201 | * unlock) when those drivers have been fixed. | |
202 | */ | |
d83cee3d | 203 | ret = gpiochip_lock_as_irq(guard.gc, gpio_chip_hwgpio(desc)); |
0eb4c6c2 | 204 | if (ret < 0) |
2ec74a95 | 205 | goto err_put_kn; |
0eb4c6c2 | 206 | |
2ec74a95 | 207 | ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags, |
a08f5c21 | 208 | "gpiolib", data); |
52176d0d JH |
209 | if (ret < 0) |
210 | goto err_unlock; | |
0eb4c6c2 | 211 | |
cef1717b | 212 | data->irq_flags = flags; |
2ec74a95 | 213 | |
0eb4c6c2 AC |
214 | return 0; |
215 | ||
52176d0d | 216 | err_unlock: |
d83cee3d | 217 | gpiochip_unlock_as_irq(guard.gc, gpio_chip_hwgpio(desc)); |
2ec74a95 JH |
218 | err_put_kn: |
219 | sysfs_put(data->value_kn); | |
220 | ||
0eb4c6c2 AC |
221 | return ret; |
222 | } | |
223 | ||
6ffcb797 JH |
224 | /* |
225 | * Caller holds gpiod-data mutex (unless called after class-device | |
226 | * deregistration). | |
227 | */ | |
2ec74a95 JH |
228 | static void gpio_sysfs_free_irq(struct device *dev) |
229 | { | |
230 | struct gpiod_data *data = dev_get_drvdata(dev); | |
231 | struct gpio_desc *desc = data->desc; | |
232 | ||
d83cee3d BG |
233 | CLASS(gpio_chip_guard, guard)(desc); |
234 | if (!guard.gc) | |
235 | return; | |
236 | ||
cef1717b | 237 | data->irq_flags = 0; |
2ec74a95 | 238 | free_irq(data->irq, data); |
d83cee3d | 239 | gpiochip_unlock_as_irq(guard.gc, gpio_chip_hwgpio(desc)); |
2ec74a95 JH |
240 | sysfs_put(data->value_kn); |
241 | } | |
242 | ||
667630ed AS |
243 | static const char * const trigger_names[] = { |
244 | [GPIO_IRQF_TRIGGER_NONE] = "none", | |
245 | [GPIO_IRQF_TRIGGER_FALLING] = "falling", | |
246 | [GPIO_IRQF_TRIGGER_RISING] = "rising", | |
247 | [GPIO_IRQF_TRIGGER_BOTH] = "both", | |
0eb4c6c2 AC |
248 | }; |
249 | ||
6beac9d1 | 250 | static ssize_t edge_show(struct device *dev, |
0eb4c6c2 AC |
251 | struct device_attribute *attr, char *buf) |
252 | { | |
c43960fb | 253 | struct gpiod_data *data = dev_get_drvdata(dev); |
667630ed | 254 | int flags; |
0eb4c6c2 | 255 | |
6ffcb797 | 256 | mutex_lock(&data->mutex); |
0eb4c6c2 | 257 | |
667630ed | 258 | flags = data->irq_flags; |
0eb4c6c2 | 259 | |
6ffcb797 JH |
260 | mutex_unlock(&data->mutex); |
261 | ||
667630ed | 262 | if (flags >= ARRAY_SIZE(trigger_names)) |
e28747da AS |
263 | return 0; |
264 | ||
667630ed | 265 | return sysfs_emit(buf, "%s\n", trigger_names[flags]); |
0eb4c6c2 AC |
266 | } |
267 | ||
6beac9d1 | 268 | static ssize_t edge_store(struct device *dev, |
0eb4c6c2 AC |
269 | struct device_attribute *attr, const char *buf, size_t size) |
270 | { | |
b91e1807 | 271 | struct gpiod_data *data = dev_get_drvdata(dev); |
513246a3 | 272 | ssize_t status = size; |
667630ed | 273 | int flags; |
e4339ce3 | 274 | |
667630ed AS |
275 | flags = sysfs_match_string(trigger_names, buf); |
276 | if (flags < 0) | |
277 | return flags; | |
b91e1807 | 278 | |
6ffcb797 | 279 | mutex_lock(&data->mutex); |
0eb4c6c2 | 280 | |
cef1717b | 281 | if (flags == data->irq_flags) { |
b91e1807 JH |
282 | status = size; |
283 | goto out_unlock; | |
284 | } | |
285 | ||
cef1717b | 286 | if (data->irq_flags) |
2ec74a95 JH |
287 | gpio_sysfs_free_irq(dev); |
288 | ||
289 | if (flags) { | |
290 | status = gpio_sysfs_request_irq(dev, flags); | |
291 | if (!status) | |
292 | status = size; | |
293 | } | |
0eb4c6c2 | 294 | |
b91e1807 | 295 | out_unlock: |
6ffcb797 | 296 | mutex_unlock(&data->mutex); |
0eb4c6c2 AC |
297 | |
298 | return status; | |
299 | } | |
6beac9d1 | 300 | static DEVICE_ATTR_RW(edge); |
0eb4c6c2 | 301 | |
6ffcb797 | 302 | /* Caller holds gpiod-data mutex. */ |
2f323b85 | 303 | static int gpio_sysfs_set_active_low(struct device *dev, int value) |
0eb4c6c2 | 304 | { |
513246a3 BG |
305 | struct gpiod_data *data = dev_get_drvdata(dev); |
306 | unsigned int flags = data->irq_flags; | |
307 | struct gpio_desc *desc = data->desc; | |
308 | int status = 0; | |
309 | ||
0eb4c6c2 AC |
310 | |
311 | if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) | |
312 | return 0; | |
313 | ||
fd80b8ba | 314 | assign_bit(FLAG_ACTIVE_LOW, &desc->flags, value); |
0eb4c6c2 AC |
315 | |
316 | /* reconfigure poll(2) support if enabled on one edge only */ | |
cef1717b JH |
317 | if (flags == GPIO_IRQF_TRIGGER_FALLING || |
318 | flags == GPIO_IRQF_TRIGGER_RISING) { | |
2ec74a95 | 319 | gpio_sysfs_free_irq(dev); |
cef1717b | 320 | status = gpio_sysfs_request_irq(dev, flags); |
0eb4c6c2 AC |
321 | } |
322 | ||
323 | return status; | |
324 | } | |
325 | ||
6beac9d1 | 326 | static ssize_t active_low_show(struct device *dev, |
0eb4c6c2 AC |
327 | struct device_attribute *attr, char *buf) |
328 | { | |
c43960fb JH |
329 | struct gpiod_data *data = dev_get_drvdata(dev); |
330 | struct gpio_desc *desc = data->desc; | |
e28747da | 331 | int value; |
0eb4c6c2 | 332 | |
6ffcb797 | 333 | mutex_lock(&data->mutex); |
0eb4c6c2 | 334 | |
e28747da | 335 | value = !!test_bit(FLAG_ACTIVE_LOW, &desc->flags); |
0eb4c6c2 | 336 | |
6ffcb797 | 337 | mutex_unlock(&data->mutex); |
0eb4c6c2 | 338 | |
e28747da | 339 | return sysfs_emit(buf, "%d\n", value); |
0eb4c6c2 AC |
340 | } |
341 | ||
6beac9d1 | 342 | static ssize_t active_low_store(struct device *dev, |
0eb4c6c2 AC |
343 | struct device_attribute *attr, const char *buf, size_t size) |
344 | { | |
513246a3 BG |
345 | struct gpiod_data *data = dev_get_drvdata(dev); |
346 | ssize_t status; | |
347 | long value; | |
0eb4c6c2 | 348 | |
6b3c1791 AS |
349 | status = kstrtol(buf, 0, &value); |
350 | if (status) | |
351 | return status; | |
352 | ||
6ffcb797 | 353 | mutex_lock(&data->mutex); |
0eb4c6c2 | 354 | |
6b3c1791 | 355 | status = gpio_sysfs_set_active_low(dev, value); |
0eb4c6c2 | 356 | |
6ffcb797 | 357 | mutex_unlock(&data->mutex); |
0eb4c6c2 AC |
358 | |
359 | return status ? : size; | |
360 | } | |
6beac9d1 | 361 | static DEVICE_ATTR_RW(active_low); |
0eb4c6c2 | 362 | |
ebbeba12 JH |
363 | static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr, |
364 | int n) | |
365 | { | |
97cd738c | 366 | struct device *dev = kobj_to_dev(kobj); |
c43960fb JH |
367 | struct gpiod_data *data = dev_get_drvdata(dev); |
368 | struct gpio_desc *desc = data->desc; | |
ebbeba12 | 369 | umode_t mode = attr->mode; |
427fdeef | 370 | bool show_direction = data->direction_can_change; |
ebbeba12 JH |
371 | |
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 | ||
0915e6fe | 385 | static struct attribute *gpio_attrs[] = { |
ebbeba12 JH |
386 | &dev_attr_direction.attr, |
387 | &dev_attr_edge.attr, | |
0eb4c6c2 AC |
388 | &dev_attr_value.attr, |
389 | &dev_attr_active_low.attr, | |
390 | NULL, | |
391 | }; | |
ebbeba12 JH |
392 | |
393 | static const struct attribute_group gpio_group = { | |
394 | .attrs = gpio_attrs, | |
395 | .is_visible = gpio_is_visible, | |
396 | }; | |
397 | ||
398 | static const struct attribute_group *gpio_groups[] = { | |
399 | &gpio_group, | |
400 | NULL | |
401 | }; | |
0eb4c6c2 AC |
402 | |
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 | ||
6beac9d1 | 410 | static ssize_t base_show(struct device *dev, |
0eb4c6c2 AC |
411 | struct device_attribute *attr, char *buf) |
412 | { | |
b93bca4b | 413 | const struct gpio_device *gdev = dev_get_drvdata(dev); |
0eb4c6c2 | 414 | |
8a7a6103 | 415 | return sysfs_emit(buf, "%u\n", gdev->base); |
0eb4c6c2 | 416 | } |
6beac9d1 | 417 | static DEVICE_ATTR_RO(base); |
0eb4c6c2 | 418 | |
6beac9d1 | 419 | static ssize_t label_show(struct device *dev, |
0eb4c6c2 AC |
420 | struct device_attribute *attr, char *buf) |
421 | { | |
b93bca4b | 422 | const struct gpio_device *gdev = dev_get_drvdata(dev); |
0eb4c6c2 | 423 | |
5694f274 | 424 | return sysfs_emit(buf, "%s\n", gdev->label); |
0eb4c6c2 | 425 | } |
6beac9d1 | 426 | static DEVICE_ATTR_RO(label); |
0eb4c6c2 | 427 | |
6beac9d1 | 428 | static ssize_t ngpio_show(struct device *dev, |
0eb4c6c2 AC |
429 | struct device_attribute *attr, char *buf) |
430 | { | |
b93bca4b | 431 | const struct gpio_device *gdev = dev_get_drvdata(dev); |
0eb4c6c2 | 432 | |
5694f274 | 433 | return sysfs_emit(buf, "%u\n", gdev->ngpio); |
0eb4c6c2 | 434 | } |
6beac9d1 | 435 | static DEVICE_ATTR_RO(ngpio); |
0eb4c6c2 | 436 | |
121b6a79 | 437 | static struct attribute *gpiochip_attrs[] = { |
0eb4c6c2 AC |
438 | &dev_attr_base.attr, |
439 | &dev_attr_label.attr, | |
440 | &dev_attr_ngpio.attr, | |
441 | NULL, | |
442 | }; | |
121b6a79 | 443 | ATTRIBUTE_GROUPS(gpiochip); |
0eb4c6c2 AC |
444 | |
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 | */ | |
75a2d422 GKH |
451 | static ssize_t export_store(const struct class *class, |
452 | const struct class_attribute *attr, | |
0eb4c6c2 AC |
453 | const char *buf, size_t len) |
454 | { | |
513246a3 | 455 | struct gpio_desc *desc; |
513246a3 BG |
456 | int status, offset; |
457 | long gpio; | |
0eb4c6c2 AC |
458 | |
459 | status = kstrtol(buf, 0, &gpio); | |
d83cee3d BG |
460 | if (status) |
461 | return status; | |
0eb4c6c2 | 462 | |
f13a0b0b | 463 | desc = gpio_to_desc(gpio); |
0eb4c6c2 AC |
464 | /* reject invalid GPIOs */ |
465 | if (!desc) { | |
466 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); | |
467 | return -EINVAL; | |
468 | } | |
d83cee3d BG |
469 | |
470 | CLASS(gpio_chip_guard, guard)(desc); | |
471 | if (!guard.gc) | |
472 | return -ENODEV; | |
473 | ||
23cf00dd | 474 | offset = gpio_chip_hwgpio(desc); |
d83cee3d | 475 | if (!gpiochip_line_is_valid(guard.gc, offset)) { |
23cf00dd MV |
476 | pr_warn("%s: GPIO %ld masked\n", __func__, gpio); |
477 | return -EINVAL; | |
478 | } | |
0eb4c6c2 AC |
479 | |
480 | /* No extra locking here; FLAG_SYSFS just signifies that the | |
481 | * request and export were done by on behalf of userspace, so | |
482 | * they may be undone on its behalf too. | |
483 | */ | |
484 | ||
95a4eed7 AS |
485 | status = gpiod_request_user(desc, "sysfs"); |
486 | if (status) | |
0eb4c6c2 | 487 | goto done; |
e10f72bf AJ |
488 | |
489 | status = gpiod_set_transitory(desc, false); | |
95dd1e34 BS |
490 | if (status) { |
491 | gpiod_free(desc); | |
492 | goto done; | |
e10f72bf | 493 | } |
0eb4c6c2 | 494 | |
95dd1e34 BS |
495 | status = gpiod_export(desc, true); |
496 | if (status < 0) | |
497 | gpiod_free(desc); | |
498 | else | |
499 | set_bit(FLAG_SYSFS, &desc->flags); | |
500 | ||
0eb4c6c2 AC |
501 | done: |
502 | if (status) | |
503 | pr_debug("%s: status %d\n", __func__, status); | |
504 | return status ? : len; | |
505 | } | |
d83bb159 | 506 | static CLASS_ATTR_WO(export); |
0eb4c6c2 | 507 | |
75a2d422 GKH |
508 | static ssize_t unexport_store(const struct class *class, |
509 | const struct class_attribute *attr, | |
0eb4c6c2 AC |
510 | const char *buf, size_t len) |
511 | { | |
513246a3 BG |
512 | struct gpio_desc *desc; |
513 | int status; | |
514 | long gpio; | |
0eb4c6c2 AC |
515 | |
516 | status = kstrtol(buf, 0, &gpio); | |
517 | if (status < 0) | |
518 | goto done; | |
519 | ||
f13a0b0b | 520 | desc = gpio_to_desc(gpio); |
d74e3166 | 521 | /* reject bogus commands (gpiod_unexport() ignores them) */ |
0eb4c6c2 AC |
522 | if (!desc) { |
523 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); | |
524 | return -EINVAL; | |
525 | } | |
526 | ||
527 | status = -EINVAL; | |
528 | ||
529 | /* No extra locking here; FLAG_SYSFS just signifies that the | |
530 | * request and export were done by on behalf of userspace, so | |
531 | * they may be undone on its behalf too. | |
532 | */ | |
533 | if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) { | |
20d9b3b5 | 534 | gpiod_unexport(desc); |
0eb4c6c2 | 535 | gpiod_free(desc); |
20d9b3b5 | 536 | status = 0; |
0eb4c6c2 AC |
537 | } |
538 | done: | |
539 | if (status) | |
540 | pr_debug("%s: status %d\n", __func__, status); | |
541 | return status ? : len; | |
542 | } | |
d83bb159 | 543 | static CLASS_ATTR_WO(unexport); |
0eb4c6c2 | 544 | |
d83bb159 GKH |
545 | static struct attribute *gpio_class_attrs[] = { |
546 | &class_attr_export.attr, | |
547 | &class_attr_unexport.attr, | |
548 | NULL, | |
0eb4c6c2 | 549 | }; |
d83bb159 | 550 | ATTRIBUTE_GROUPS(gpio_class); |
0eb4c6c2 AC |
551 | |
552 | static struct class gpio_class = { | |
553 | .name = "gpio", | |
d83bb159 | 554 | .class_groups = gpio_class_groups, |
0eb4c6c2 AC |
555 | }; |
556 | ||
557 | ||
558 | /** | |
559 | * gpiod_export - export a GPIO through sysfs | |
2d9d0519 TR |
560 | * @desc: GPIO to make available, already requested |
561 | * @direction_may_change: true if userspace may change GPIO direction | |
0eb4c6c2 AC |
562 | * Context: arch_initcall or later |
563 | * | |
564 | * When drivers want to make a GPIO accessible to userspace after they | |
565 | * have requested it -- perhaps while debugging, or as part of their | |
566 | * public interface -- they may use this routine. If the GPIO can | |
567 | * change direction (some can't) and the caller allows it, userspace | |
568 | * will see "direction" sysfs attribute which may be used to change | |
569 | * the gpio's direction. A "value" attribute will always be provided. | |
570 | * | |
94bd9ce1 AS |
571 | * Returns: |
572 | * 0 on success, or negative errno on failure. | |
0eb4c6c2 AC |
573 | */ |
574 | int gpiod_export(struct gpio_desc *desc, bool direction_may_change) | |
575 | { | |
513246a3 BG |
576 | const char *ioname = NULL; |
577 | struct gpio_device *gdev; | |
578 | struct gpiod_data *data; | |
513246a3 BG |
579 | struct device *dev; |
580 | int status, offset; | |
0eb4c6c2 AC |
581 | |
582 | /* can't export until sysfs is available ... */ | |
6f14c022 | 583 | if (!class_is_registered(&gpio_class)) { |
0eb4c6c2 AC |
584 | pr_debug("%s: called too early!\n", __func__); |
585 | return -ENOENT; | |
586 | } | |
587 | ||
588 | if (!desc) { | |
589 | pr_debug("%s: invalid gpio descriptor\n", __func__); | |
590 | return -EINVAL; | |
591 | } | |
592 | ||
d83cee3d BG |
593 | CLASS(gpio_chip_guard, guard)(desc); |
594 | if (!guard.gc) | |
595 | return -ENODEV; | |
596 | ||
8636f19c | 597 | if (test_and_set_bit(FLAG_EXPORT, &desc->flags)) |
35b54533 BG |
598 | return -EPERM; |
599 | ||
fdeb8e15 | 600 | gdev = desc->gdev; |
483d8211 | 601 | |
0eb4c6c2 AC |
602 | mutex_lock(&sysfs_lock); |
603 | ||
483d8211 | 604 | /* check if chip is being removed */ |
d83cee3d | 605 | if (!gdev->mockdev) { |
483d8211 | 606 | status = -ENODEV; |
c43960fb | 607 | goto err_unlock; |
483d8211 JH |
608 | } |
609 | ||
35b54533 BG |
610 | if (!test_bit(FLAG_REQUESTED, &desc->flags)) { |
611 | gpiod_dbg(desc, "%s: unavailable (not requested)\n", __func__); | |
0eb4c6c2 | 612 | status = -EPERM; |
c43960fb | 613 | goto err_unlock; |
0eb4c6c2 | 614 | } |
0eb4c6c2 | 615 | |
c43960fb JH |
616 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
617 | if (!data) { | |
618 | status = -ENOMEM; | |
619 | goto err_unlock; | |
620 | } | |
621 | ||
622 | data->desc = desc; | |
6ffcb797 | 623 | mutex_init(&data->mutex); |
d83cee3d | 624 | if (guard.gc->direction_input && guard.gc->direction_output) |
427fdeef JH |
625 | data->direction_can_change = direction_may_change; |
626 | else | |
627 | data->direction_can_change = false; | |
c43960fb | 628 | |
0eb4c6c2 | 629 | offset = gpio_chip_hwgpio(desc); |
d83cee3d BG |
630 | if (guard.gc->names && guard.gc->names[offset]) |
631 | ioname = guard.gc->names[offset]; | |
0eb4c6c2 | 632 | |
ff2b1359 | 633 | dev = device_create_with_groups(&gpio_class, &gdev->dev, |
c43960fb | 634 | MKDEV(0, 0), data, gpio_groups, |
0915e6fe JH |
635 | ioname ? ioname : "gpio%u", |
636 | desc_to_gpio(desc)); | |
0eb4c6c2 AC |
637 | if (IS_ERR(dev)) { |
638 | status = PTR_ERR(dev); | |
c43960fb | 639 | goto err_free_data; |
0eb4c6c2 AC |
640 | } |
641 | ||
0eb4c6c2 AC |
642 | mutex_unlock(&sysfs_lock); |
643 | return 0; | |
644 | ||
c43960fb JH |
645 | err_free_data: |
646 | kfree(data); | |
647 | err_unlock: | |
0eb4c6c2 | 648 | mutex_unlock(&sysfs_lock); |
35b54533 | 649 | clear_bit(FLAG_EXPORT, &desc->flags); |
0eb4c6c2 AC |
650 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
651 | return status; | |
652 | } | |
653 | EXPORT_SYMBOL_GPL(gpiod_export); | |
654 | ||
c43960fb | 655 | static int match_export(struct device *dev, const void *desc) |
0eb4c6c2 | 656 | { |
c43960fb JH |
657 | struct gpiod_data *data = dev_get_drvdata(dev); |
658 | ||
659 | return data->desc == desc; | |
0eb4c6c2 AC |
660 | } |
661 | ||
662 | /** | |
663 | * gpiod_export_link - create a sysfs link to an exported GPIO node | |
664 | * @dev: device under which to create symlink | |
665 | * @name: name of the symlink | |
2d9d0519 | 666 | * @desc: GPIO to create symlink to, already exported |
0eb4c6c2 AC |
667 | * |
668 | * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN | |
669 | * node. Caller is responsible for unlinking. | |
670 | * | |
94bd9ce1 AS |
671 | * Returns: |
672 | * 0 on success, or negative errno on failure. | |
0eb4c6c2 AC |
673 | */ |
674 | int gpiod_export_link(struct device *dev, const char *name, | |
675 | struct gpio_desc *desc) | |
676 | { | |
56d30ec1 JH |
677 | struct device *cdev; |
678 | int ret; | |
0eb4c6c2 AC |
679 | |
680 | if (!desc) { | |
681 | pr_warn("%s: invalid GPIO\n", __func__); | |
682 | return -EINVAL; | |
683 | } | |
684 | ||
56d30ec1 JH |
685 | cdev = class_find_device(&gpio_class, NULL, desc, match_export); |
686 | if (!cdev) | |
687 | return -ENODEV; | |
0eb4c6c2 | 688 | |
56d30ec1 JH |
689 | ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name); |
690 | put_device(cdev); | |
0eb4c6c2 | 691 | |
56d30ec1 | 692 | return ret; |
0eb4c6c2 AC |
693 | } |
694 | EXPORT_SYMBOL_GPL(gpiod_export_link); | |
695 | ||
0eb4c6c2 | 696 | /** |
31963eb0 | 697 | * gpiod_unexport - reverse effect of gpiod_export() |
2d9d0519 | 698 | * @desc: GPIO to make unavailable |
0eb4c6c2 | 699 | * |
31963eb0 | 700 | * This is implicit on gpiod_free(). |
0eb4c6c2 AC |
701 | */ |
702 | void gpiod_unexport(struct gpio_desc *desc) | |
703 | { | |
72eba6f6 JH |
704 | struct gpiod_data *data; |
705 | struct device *dev; | |
0eb4c6c2 AC |
706 | |
707 | if (!desc) { | |
708 | pr_warn("%s: invalid GPIO\n", __func__); | |
709 | return; | |
710 | } | |
711 | ||
712 | mutex_lock(&sysfs_lock); | |
713 | ||
72eba6f6 JH |
714 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
715 | goto err_unlock; | |
0eb4c6c2 | 716 | |
72eba6f6 JH |
717 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
718 | if (!dev) | |
719 | goto err_unlock; | |
720 | ||
721 | data = dev_get_drvdata(dev); | |
722 | ||
72eba6f6 JH |
723 | clear_bit(FLAG_EXPORT, &desc->flags); |
724 | ||
725 | device_unregister(dev); | |
726 | ||
727 | /* | |
728 | * Release irq after deregistration to prevent race with edge_store. | |
729 | */ | |
cef1717b | 730 | if (data->irq_flags) |
72eba6f6 | 731 | gpio_sysfs_free_irq(dev); |
0eb4c6c2 AC |
732 | |
733 | mutex_unlock(&sysfs_lock); | |
734 | ||
72eba6f6 JH |
735 | put_device(dev); |
736 | kfree(data); | |
0eb4c6c2 | 737 | |
72eba6f6 JH |
738 | return; |
739 | ||
740 | err_unlock: | |
741 | mutex_unlock(&sysfs_lock); | |
0eb4c6c2 AC |
742 | } |
743 | EXPORT_SYMBOL_GPL(gpiod_unexport); | |
744 | ||
afbc4f31 | 745 | int gpiochip_sysfs_register(struct gpio_device *gdev) |
0eb4c6c2 | 746 | { |
d83cee3d | 747 | struct gpio_chip *chip; |
513246a3 BG |
748 | struct device *parent; |
749 | struct device *dev; | |
0eb4c6c2 | 750 | |
426577bd JH |
751 | /* |
752 | * Many systems add gpio chips for SOC support very early, | |
0eb4c6c2 | 753 | * before driver model support is available. In those cases we |
426577bd | 754 | * register later, in gpiolib_sysfs_init() ... here we just |
0eb4c6c2 AC |
755 | * verify that _some_ field of gpio_class got initialized. |
756 | */ | |
6f14c022 | 757 | if (!class_is_registered(&gpio_class)) |
0eb4c6c2 AC |
758 | return 0; |
759 | ||
d83cee3d BG |
760 | guard(srcu)(&gdev->srcu); |
761 | ||
d82b9e08 | 762 | chip = srcu_dereference(gdev->chip, &gdev->srcu); |
d83cee3d BG |
763 | if (!chip) |
764 | return -ENODEV; | |
765 | ||
d27c1728 BJZ |
766 | /* |
767 | * For sysfs backward compatibility we need to preserve this | |
768 | * preferred parenting to the gpio_chip parent field, if set. | |
769 | */ | |
770 | if (chip->parent) | |
771 | parent = chip->parent; | |
772 | else | |
773 | parent = &gdev->dev; | |
774 | ||
0eb4c6c2 | 775 | /* use chip->base for the ID; it's already known to be unique */ |
b93bca4b | 776 | dev = device_create_with_groups(&gpio_class, parent, MKDEV(0, 0), gdev, |
ddd8891e GU |
777 | gpiochip_groups, GPIOCHIP_NAME "%d", |
778 | chip->base); | |
121b6a79 | 779 | if (IS_ERR(dev)) |
6a4b6b0a | 780 | return PTR_ERR(dev); |
3ff74be5 JH |
781 | |
782 | mutex_lock(&sysfs_lock); | |
afbc4f31 | 783 | gdev->mockdev = dev; |
0eb4c6c2 AC |
784 | mutex_unlock(&sysfs_lock); |
785 | ||
6a4b6b0a | 786 | return 0; |
0eb4c6c2 AC |
787 | } |
788 | ||
afbc4f31 | 789 | void gpiochip_sysfs_unregister(struct gpio_device *gdev) |
0eb4c6c2 | 790 | { |
483d8211 | 791 | struct gpio_desc *desc; |
d83cee3d | 792 | struct gpio_chip *chip; |
0eb4c6c2 | 793 | |
59cba4a0 BG |
794 | scoped_guard(mutex, &sysfs_lock) { |
795 | if (!gdev->mockdev) | |
796 | return; | |
0eb4c6c2 | 797 | |
59cba4a0 | 798 | device_unregister(gdev->mockdev); |
6a4b6b0a | 799 | |
59cba4a0 BG |
800 | /* prevent further gpiod exports */ |
801 | gdev->mockdev = NULL; | |
802 | } | |
483d8211 | 803 | |
d83cee3d BG |
804 | guard(srcu)(&gdev->srcu); |
805 | ||
d82b9e08 | 806 | chip = srcu_dereference(gdev->chip, &gdev->srcu); |
be91c19e | 807 | if (!chip) |
d83cee3d BG |
808 | return; |
809 | ||
483d8211 | 810 | /* unregister gpiod class devices owned by sysfs */ |
20d9b3b5 AS |
811 | for_each_gpio_desc_with_flag(chip, desc, FLAG_SYSFS) { |
812 | gpiod_unexport(desc); | |
80c78fbe | 813 | gpiod_free(desc); |
20d9b3b5 | 814 | } |
0eb4c6c2 AC |
815 | } |
816 | ||
2a9101e8 BG |
817 | /* |
818 | * We're not really looking for a device - we just want to iterate over the | |
819 | * list and call this callback for each GPIO device. This is why this function | |
820 | * always returns 0. | |
821 | */ | |
822 | static int gpiofind_sysfs_register(struct gpio_chip *gc, const void *data) | |
823 | { | |
824 | struct gpio_device *gdev = gc->gpiodev; | |
825 | int ret; | |
826 | ||
827 | if (gdev->mockdev) | |
828 | return 0; | |
829 | ||
830 | ret = gpiochip_sysfs_register(gdev); | |
831 | if (ret) | |
832 | chip_err(gc, "failed to register the sysfs entry: %d\n", ret); | |
833 | ||
834 | return 0; | |
835 | } | |
836 | ||
0eb4c6c2 AC |
837 | static int __init gpiolib_sysfs_init(void) |
838 | { | |
2a9101e8 | 839 | int status; |
0eb4c6c2 AC |
840 | |
841 | status = class_register(&gpio_class); | |
842 | if (status < 0) | |
843 | return status; | |
844 | ||
845 | /* Scan and register the gpio_chips which registered very | |
846 | * early (e.g. before the class_register above was called). | |
847 | * | |
848 | * We run before arch_initcall() so chip->dev nodes can have | |
d74e3166 | 849 | * registered, and so arch_initcall() can always gpiod_export(). |
0eb4c6c2 | 850 | */ |
2a9101e8 | 851 | (void)gpio_device_find(NULL, gpiofind_sysfs_register); |
efb8235b | 852 | |
2a9101e8 | 853 | return 0; |
0eb4c6c2 AC |
854 | } |
855 | postcore_initcall(gpiolib_sysfs_init); |