]>
Commit | Line | Data |
---|---|---|
0eb4c6c2 AC |
1 | #include <linux/idr.h> |
2 | #include <linux/mutex.h> | |
3 | #include <linux/device.h> | |
4 | #include <linux/sysfs.h> | |
5 | #include <linux/gpio/consumer.h> | |
6 | #include <linux/gpio/driver.h> | |
7 | #include <linux/interrupt.h> | |
8 | #include <linux/kdev_t.h> | |
9 | ||
10 | #include "gpiolib.h" | |
11 | ||
12 | static DEFINE_IDR(dirent_idr); | |
13 | ||
14 | ||
15 | /* lock protects against unexport_gpio() being called while | |
16 | * sysfs files are active. | |
17 | */ | |
18 | static DEFINE_MUTEX(sysfs_lock); | |
19 | ||
20 | /* | |
21 | * /sys/class/gpio/gpioN... only for GPIOs that are exported | |
22 | * /direction | |
23 | * * MAY BE OMITTED if kernel won't allow direction changes | |
24 | * * is read/write as "in" or "out" | |
25 | * * may also be written as "high" or "low", initializing | |
26 | * output value as specified ("out" implies "low") | |
27 | * /value | |
28 | * * always readable, subject to hardware behavior | |
29 | * * may be writable, as zero/nonzero | |
30 | * /edge | |
31 | * * configures behavior of poll(2) on /value | |
32 | * * available only if pin can generate IRQs on input | |
33 | * * is read/write as "none", "falling", "rising", or "both" | |
34 | * /active_low | |
35 | * * configures polarity of /value | |
36 | * * is read/write as zero/nonzero | |
37 | * * also affects existing and subsequent "falling" and "rising" | |
38 | * /edge configuration | |
39 | */ | |
40 | ||
41 | static ssize_t gpio_direction_show(struct device *dev, | |
42 | struct device_attribute *attr, char *buf) | |
43 | { | |
8e53b0f1 | 44 | struct gpio_desc *desc = dev_get_drvdata(dev); |
0eb4c6c2 AC |
45 | ssize_t status; |
46 | ||
47 | mutex_lock(&sysfs_lock); | |
48 | ||
49 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { | |
50 | status = -EIO; | |
51 | } else { | |
52 | gpiod_get_direction(desc); | |
53 | status = sprintf(buf, "%s\n", | |
54 | test_bit(FLAG_IS_OUT, &desc->flags) | |
55 | ? "out" : "in"); | |
56 | } | |
57 | ||
58 | mutex_unlock(&sysfs_lock); | |
59 | return status; | |
60 | } | |
61 | ||
62 | static ssize_t gpio_direction_store(struct device *dev, | |
63 | struct device_attribute *attr, const char *buf, size_t size) | |
64 | { | |
65 | struct gpio_desc *desc = dev_get_drvdata(dev); | |
66 | ssize_t status; | |
67 | ||
68 | mutex_lock(&sysfs_lock); | |
69 | ||
70 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
71 | status = -EIO; | |
72 | else if (sysfs_streq(buf, "high")) | |
73 | status = gpiod_direction_output_raw(desc, 1); | |
74 | else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) | |
75 | status = gpiod_direction_output_raw(desc, 0); | |
76 | else if (sysfs_streq(buf, "in")) | |
77 | status = gpiod_direction_input(desc); | |
78 | else | |
79 | status = -EINVAL; | |
80 | ||
81 | mutex_unlock(&sysfs_lock); | |
82 | return status ? : size; | |
83 | } | |
84 | ||
85 | static /* const */ DEVICE_ATTR(direction, 0644, | |
86 | gpio_direction_show, gpio_direction_store); | |
87 | ||
88 | static ssize_t gpio_value_show(struct device *dev, | |
89 | struct device_attribute *attr, char *buf) | |
90 | { | |
91 | struct gpio_desc *desc = dev_get_drvdata(dev); | |
92 | ssize_t status; | |
93 | ||
94 | mutex_lock(&sysfs_lock); | |
95 | ||
96 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
97 | status = -EIO; | |
98 | else | |
99 | status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc)); | |
100 | ||
101 | mutex_unlock(&sysfs_lock); | |
102 | return status; | |
103 | } | |
104 | ||
105 | static ssize_t gpio_value_store(struct device *dev, | |
106 | struct device_attribute *attr, const char *buf, size_t size) | |
107 | { | |
108 | struct gpio_desc *desc = dev_get_drvdata(dev); | |
109 | ssize_t status; | |
110 | ||
111 | mutex_lock(&sysfs_lock); | |
112 | ||
113 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
114 | status = -EIO; | |
115 | else if (!test_bit(FLAG_IS_OUT, &desc->flags)) | |
116 | status = -EPERM; | |
117 | else { | |
118 | long value; | |
119 | ||
120 | status = kstrtol(buf, 0, &value); | |
121 | if (status == 0) { | |
122 | gpiod_set_value_cansleep(desc, value); | |
123 | status = size; | |
124 | } | |
125 | } | |
126 | ||
127 | mutex_unlock(&sysfs_lock); | |
128 | return status; | |
129 | } | |
130 | ||
0915e6fe | 131 | static DEVICE_ATTR(value, 0644, |
0eb4c6c2 AC |
132 | gpio_value_show, gpio_value_store); |
133 | ||
134 | static irqreturn_t gpio_sysfs_irq(int irq, void *priv) | |
135 | { | |
136 | struct kernfs_node *value_sd = priv; | |
137 | ||
138 | sysfs_notify_dirent(value_sd); | |
139 | return IRQ_HANDLED; | |
140 | } | |
141 | ||
142 | static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, | |
143 | unsigned long gpio_flags) | |
144 | { | |
145 | struct kernfs_node *value_sd; | |
146 | unsigned long irq_flags; | |
147 | int ret, irq, id; | |
148 | ||
149 | if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags) | |
150 | return 0; | |
151 | ||
152 | irq = gpiod_to_irq(desc); | |
153 | if (irq < 0) | |
154 | return -EIO; | |
155 | ||
156 | id = desc->flags >> ID_SHIFT; | |
157 | value_sd = idr_find(&dirent_idr, id); | |
158 | if (value_sd) | |
159 | free_irq(irq, value_sd); | |
160 | ||
161 | desc->flags &= ~GPIO_TRIGGER_MASK; | |
162 | ||
163 | if (!gpio_flags) { | |
e3a2e878 | 164 | gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc)); |
0eb4c6c2 AC |
165 | ret = 0; |
166 | goto free_id; | |
167 | } | |
168 | ||
169 | irq_flags = IRQF_SHARED; | |
170 | if (test_bit(FLAG_TRIG_FALL, &gpio_flags)) | |
171 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? | |
172 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; | |
173 | if (test_bit(FLAG_TRIG_RISE, &gpio_flags)) | |
174 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? | |
175 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; | |
176 | ||
177 | if (!value_sd) { | |
178 | value_sd = sysfs_get_dirent(dev->kobj.sd, "value"); | |
179 | if (!value_sd) { | |
180 | ret = -ENODEV; | |
181 | goto err_out; | |
182 | } | |
183 | ||
184 | ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL); | |
185 | if (ret < 0) | |
186 | goto free_sd; | |
187 | id = ret; | |
188 | ||
189 | desc->flags &= GPIO_FLAGS_MASK; | |
190 | desc->flags |= (unsigned long)id << ID_SHIFT; | |
191 | ||
192 | if (desc->flags >> ID_SHIFT != id) { | |
193 | ret = -ERANGE; | |
194 | goto free_id; | |
195 | } | |
196 | } | |
197 | ||
52176d0d JH |
198 | /* |
199 | * FIXME: This should be done in the irq_request_resources callback | |
200 | * when the irq is requested, but a few drivers currently fail | |
201 | * to do so. | |
202 | * | |
203 | * Remove this redundant call (along with the corresponding | |
204 | * unlock) when those drivers have been fixed. | |
205 | */ | |
206 | ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc)); | |
0eb4c6c2 AC |
207 | if (ret < 0) |
208 | goto free_id; | |
209 | ||
52176d0d JH |
210 | ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags, |
211 | "gpiolib", value_sd); | |
212 | if (ret < 0) | |
213 | goto err_unlock; | |
0eb4c6c2 AC |
214 | |
215 | desc->flags |= gpio_flags; | |
216 | return 0; | |
217 | ||
52176d0d JH |
218 | err_unlock: |
219 | gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc)); | |
0eb4c6c2 AC |
220 | free_id: |
221 | idr_remove(&dirent_idr, id); | |
222 | desc->flags &= GPIO_FLAGS_MASK; | |
223 | free_sd: | |
224 | if (value_sd) | |
225 | sysfs_put(value_sd); | |
226 | err_out: | |
227 | return ret; | |
228 | } | |
229 | ||
230 | static const struct { | |
231 | const char *name; | |
232 | unsigned long flags; | |
233 | } trigger_types[] = { | |
234 | { "none", 0 }, | |
235 | { "falling", BIT(FLAG_TRIG_FALL) }, | |
236 | { "rising", BIT(FLAG_TRIG_RISE) }, | |
237 | { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) }, | |
238 | }; | |
239 | ||
240 | static ssize_t gpio_edge_show(struct device *dev, | |
241 | struct device_attribute *attr, char *buf) | |
242 | { | |
243 | const struct gpio_desc *desc = dev_get_drvdata(dev); | |
244 | ssize_t status; | |
245 | ||
246 | mutex_lock(&sysfs_lock); | |
247 | ||
248 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
249 | status = -EIO; | |
250 | else { | |
251 | int i; | |
252 | ||
253 | status = 0; | |
254 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) | |
255 | if ((desc->flags & GPIO_TRIGGER_MASK) | |
256 | == trigger_types[i].flags) { | |
257 | status = sprintf(buf, "%s\n", | |
258 | trigger_types[i].name); | |
259 | break; | |
260 | } | |
261 | } | |
262 | ||
263 | mutex_unlock(&sysfs_lock); | |
264 | return status; | |
265 | } | |
266 | ||
267 | static ssize_t gpio_edge_store(struct device *dev, | |
268 | struct device_attribute *attr, const char *buf, size_t size) | |
269 | { | |
270 | struct gpio_desc *desc = dev_get_drvdata(dev); | |
271 | ssize_t status; | |
272 | int i; | |
273 | ||
274 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) | |
275 | if (sysfs_streq(trigger_types[i].name, buf)) | |
276 | goto found; | |
277 | return -EINVAL; | |
278 | ||
279 | found: | |
280 | mutex_lock(&sysfs_lock); | |
281 | ||
282 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
283 | status = -EIO; | |
284 | else { | |
285 | status = gpio_setup_irq(desc, dev, trigger_types[i].flags); | |
286 | if (!status) | |
287 | status = size; | |
288 | } | |
289 | ||
290 | mutex_unlock(&sysfs_lock); | |
291 | ||
292 | return status; | |
293 | } | |
294 | ||
295 | static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store); | |
296 | ||
297 | static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev, | |
298 | int value) | |
299 | { | |
300 | int status = 0; | |
301 | ||
302 | if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) | |
303 | return 0; | |
304 | ||
305 | if (value) | |
306 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
307 | else | |
308 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
309 | ||
310 | /* reconfigure poll(2) support if enabled on one edge only */ | |
311 | if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^ | |
312 | !!test_bit(FLAG_TRIG_FALL, &desc->flags))) { | |
313 | unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK; | |
314 | ||
315 | gpio_setup_irq(desc, dev, 0); | |
316 | status = gpio_setup_irq(desc, dev, trigger_flags); | |
317 | } | |
318 | ||
319 | return status; | |
320 | } | |
321 | ||
322 | static ssize_t gpio_active_low_show(struct device *dev, | |
323 | struct device_attribute *attr, char *buf) | |
324 | { | |
325 | const struct gpio_desc *desc = dev_get_drvdata(dev); | |
326 | ssize_t status; | |
327 | ||
328 | mutex_lock(&sysfs_lock); | |
329 | ||
330 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
331 | status = -EIO; | |
332 | else | |
333 | status = sprintf(buf, "%d\n", | |
334 | !!test_bit(FLAG_ACTIVE_LOW, &desc->flags)); | |
335 | ||
336 | mutex_unlock(&sysfs_lock); | |
337 | ||
338 | return status; | |
339 | } | |
340 | ||
341 | static ssize_t gpio_active_low_store(struct device *dev, | |
342 | struct device_attribute *attr, const char *buf, size_t size) | |
343 | { | |
344 | struct gpio_desc *desc = dev_get_drvdata(dev); | |
345 | ssize_t status; | |
346 | ||
347 | mutex_lock(&sysfs_lock); | |
348 | ||
349 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { | |
350 | status = -EIO; | |
351 | } else { | |
352 | long value; | |
353 | ||
354 | status = kstrtol(buf, 0, &value); | |
355 | if (status == 0) | |
356 | status = sysfs_set_active_low(desc, dev, value != 0); | |
357 | } | |
358 | ||
359 | mutex_unlock(&sysfs_lock); | |
360 | ||
361 | return status ? : size; | |
362 | } | |
363 | ||
0915e6fe | 364 | static DEVICE_ATTR(active_low, 0644, |
0eb4c6c2 AC |
365 | gpio_active_low_show, gpio_active_low_store); |
366 | ||
ebbeba12 JH |
367 | static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr, |
368 | int n) | |
369 | { | |
370 | struct device *dev = container_of(kobj, struct device, kobj); | |
371 | struct gpio_desc *desc = dev_get_drvdata(dev); | |
372 | umode_t mode = attr->mode; | |
373 | bool show_direction = test_bit(FLAG_SYSFS_DIR, &desc->flags); | |
374 | ||
375 | if (attr == &dev_attr_direction.attr) { | |
376 | if (!show_direction) | |
377 | mode = 0; | |
378 | } else if (attr == &dev_attr_edge.attr) { | |
379 | if (gpiod_to_irq(desc) < 0) | |
380 | mode = 0; | |
381 | if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags)) | |
382 | mode = 0; | |
383 | } | |
384 | ||
385 | return mode; | |
386 | } | |
387 | ||
0915e6fe | 388 | static struct attribute *gpio_attrs[] = { |
ebbeba12 JH |
389 | &dev_attr_direction.attr, |
390 | &dev_attr_edge.attr, | |
0eb4c6c2 AC |
391 | &dev_attr_value.attr, |
392 | &dev_attr_active_low.attr, | |
393 | NULL, | |
394 | }; | |
ebbeba12 JH |
395 | |
396 | static const struct attribute_group gpio_group = { | |
397 | .attrs = gpio_attrs, | |
398 | .is_visible = gpio_is_visible, | |
399 | }; | |
400 | ||
401 | static const struct attribute_group *gpio_groups[] = { | |
402 | &gpio_group, | |
403 | NULL | |
404 | }; | |
0eb4c6c2 AC |
405 | |
406 | /* | |
407 | * /sys/class/gpio/gpiochipN/ | |
408 | * /base ... matching gpio_chip.base (N) | |
409 | * /label ... matching gpio_chip.label | |
410 | * /ngpio ... matching gpio_chip.ngpio | |
411 | */ | |
412 | ||
413 | static ssize_t chip_base_show(struct device *dev, | |
414 | struct device_attribute *attr, char *buf) | |
415 | { | |
416 | const struct gpio_chip *chip = dev_get_drvdata(dev); | |
417 | ||
418 | return sprintf(buf, "%d\n", chip->base); | |
419 | } | |
420 | static DEVICE_ATTR(base, 0444, chip_base_show, NULL); | |
421 | ||
422 | static ssize_t chip_label_show(struct device *dev, | |
423 | struct device_attribute *attr, char *buf) | |
424 | { | |
425 | const struct gpio_chip *chip = dev_get_drvdata(dev); | |
426 | ||
427 | return sprintf(buf, "%s\n", chip->label ? : ""); | |
428 | } | |
429 | static DEVICE_ATTR(label, 0444, chip_label_show, NULL); | |
430 | ||
431 | static ssize_t chip_ngpio_show(struct device *dev, | |
432 | struct device_attribute *attr, char *buf) | |
433 | { | |
434 | const struct gpio_chip *chip = dev_get_drvdata(dev); | |
435 | ||
436 | return sprintf(buf, "%u\n", chip->ngpio); | |
437 | } | |
438 | static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); | |
439 | ||
121b6a79 | 440 | static struct attribute *gpiochip_attrs[] = { |
0eb4c6c2 AC |
441 | &dev_attr_base.attr, |
442 | &dev_attr_label.attr, | |
443 | &dev_attr_ngpio.attr, | |
444 | NULL, | |
445 | }; | |
121b6a79 | 446 | ATTRIBUTE_GROUPS(gpiochip); |
0eb4c6c2 AC |
447 | |
448 | /* | |
449 | * /sys/class/gpio/export ... write-only | |
450 | * integer N ... number of GPIO to export (full access) | |
451 | * /sys/class/gpio/unexport ... write-only | |
452 | * integer N ... number of GPIO to unexport | |
453 | */ | |
454 | static ssize_t export_store(struct class *class, | |
455 | struct class_attribute *attr, | |
456 | const char *buf, size_t len) | |
457 | { | |
458 | long gpio; | |
459 | struct gpio_desc *desc; | |
460 | int status; | |
461 | ||
462 | status = kstrtol(buf, 0, &gpio); | |
463 | if (status < 0) | |
464 | goto done; | |
465 | ||
466 | desc = gpio_to_desc(gpio); | |
467 | /* reject invalid GPIOs */ | |
468 | if (!desc) { | |
469 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); | |
470 | return -EINVAL; | |
471 | } | |
472 | ||
473 | /* No extra locking here; FLAG_SYSFS just signifies that the | |
474 | * request and export were done by on behalf of userspace, so | |
475 | * they may be undone on its behalf too. | |
476 | */ | |
477 | ||
478 | status = gpiod_request(desc, "sysfs"); | |
479 | if (status < 0) { | |
480 | if (status == -EPROBE_DEFER) | |
481 | status = -ENODEV; | |
482 | goto done; | |
483 | } | |
484 | status = gpiod_export(desc, true); | |
485 | if (status < 0) | |
486 | gpiod_free(desc); | |
487 | else | |
488 | set_bit(FLAG_SYSFS, &desc->flags); | |
489 | ||
490 | done: | |
491 | if (status) | |
492 | pr_debug("%s: status %d\n", __func__, status); | |
493 | return status ? : len; | |
494 | } | |
495 | ||
496 | static ssize_t unexport_store(struct class *class, | |
497 | struct class_attribute *attr, | |
498 | const char *buf, size_t len) | |
499 | { | |
500 | long gpio; | |
501 | struct gpio_desc *desc; | |
502 | int status; | |
503 | ||
504 | status = kstrtol(buf, 0, &gpio); | |
505 | if (status < 0) | |
506 | goto done; | |
507 | ||
508 | desc = gpio_to_desc(gpio); | |
509 | /* reject bogus commands (gpio_unexport ignores them) */ | |
510 | if (!desc) { | |
511 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); | |
512 | return -EINVAL; | |
513 | } | |
514 | ||
515 | status = -EINVAL; | |
516 | ||
517 | /* No extra locking here; FLAG_SYSFS just signifies that the | |
518 | * request and export were done by on behalf of userspace, so | |
519 | * they may be undone on its behalf too. | |
520 | */ | |
521 | if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) { | |
522 | status = 0; | |
523 | gpiod_free(desc); | |
524 | } | |
525 | done: | |
526 | if (status) | |
527 | pr_debug("%s: status %d\n", __func__, status); | |
528 | return status ? : len; | |
529 | } | |
530 | ||
531 | static struct class_attribute gpio_class_attrs[] = { | |
532 | __ATTR(export, 0200, NULL, export_store), | |
533 | __ATTR(unexport, 0200, NULL, unexport_store), | |
534 | __ATTR_NULL, | |
535 | }; | |
536 | ||
537 | static struct class gpio_class = { | |
538 | .name = "gpio", | |
539 | .owner = THIS_MODULE, | |
540 | ||
541 | .class_attrs = gpio_class_attrs, | |
542 | }; | |
543 | ||
544 | ||
545 | /** | |
546 | * gpiod_export - export a GPIO through sysfs | |
547 | * @gpio: gpio to make available, already requested | |
548 | * @direction_may_change: true if userspace may change gpio direction | |
549 | * 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 | */ | |
560 | int gpiod_export(struct gpio_desc *desc, bool direction_may_change) | |
561 | { | |
483d8211 | 562 | struct gpio_chip *chip; |
0eb4c6c2 AC |
563 | unsigned long flags; |
564 | int status; | |
565 | const char *ioname = NULL; | |
566 | struct device *dev; | |
567 | int offset; | |
568 | ||
569 | /* can't export until sysfs is available ... */ | |
570 | if (!gpio_class.p) { | |
571 | pr_debug("%s: called too early!\n", __func__); | |
572 | return -ENOENT; | |
573 | } | |
574 | ||
575 | if (!desc) { | |
576 | pr_debug("%s: invalid gpio descriptor\n", __func__); | |
577 | return -EINVAL; | |
578 | } | |
579 | ||
483d8211 JH |
580 | chip = desc->chip; |
581 | ||
0eb4c6c2 AC |
582 | mutex_lock(&sysfs_lock); |
583 | ||
483d8211 JH |
584 | /* check if chip is being removed */ |
585 | if (!chip || !chip->exported) { | |
586 | status = -ENODEV; | |
587 | goto fail_unlock; | |
588 | } | |
589 | ||
0eb4c6c2 AC |
590 | spin_lock_irqsave(&gpio_lock, flags); |
591 | if (!test_bit(FLAG_REQUESTED, &desc->flags) || | |
592 | test_bit(FLAG_EXPORT, &desc->flags)) { | |
593 | spin_unlock_irqrestore(&gpio_lock, flags); | |
594 | gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n", | |
595 | __func__, | |
596 | test_bit(FLAG_REQUESTED, &desc->flags), | |
597 | test_bit(FLAG_EXPORT, &desc->flags)); | |
598 | status = -EPERM; | |
599 | goto fail_unlock; | |
600 | } | |
601 | ||
cecf58ab | 602 | if (chip->direction_input && chip->direction_output && |
ebbeba12 JH |
603 | direction_may_change) { |
604 | set_bit(FLAG_SYSFS_DIR, &desc->flags); | |
605 | } | |
606 | ||
0eb4c6c2 AC |
607 | spin_unlock_irqrestore(&gpio_lock, flags); |
608 | ||
609 | offset = gpio_chip_hwgpio(desc); | |
cecf58ab JH |
610 | if (chip->names && chip->names[offset]) |
611 | ioname = chip->names[offset]; | |
0eb4c6c2 | 612 | |
cecf58ab | 613 | dev = device_create_with_groups(&gpio_class, chip->dev, |
0915e6fe JH |
614 | MKDEV(0, 0), desc, gpio_groups, |
615 | ioname ? ioname : "gpio%u", | |
616 | desc_to_gpio(desc)); | |
0eb4c6c2 AC |
617 | if (IS_ERR(dev)) { |
618 | status = PTR_ERR(dev); | |
619 | goto fail_unlock; | |
620 | } | |
621 | ||
0eb4c6c2 AC |
622 | set_bit(FLAG_EXPORT, &desc->flags); |
623 | mutex_unlock(&sysfs_lock); | |
624 | return 0; | |
625 | ||
0eb4c6c2 AC |
626 | fail_unlock: |
627 | mutex_unlock(&sysfs_lock); | |
628 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); | |
629 | return status; | |
630 | } | |
631 | EXPORT_SYMBOL_GPL(gpiod_export); | |
632 | ||
633 | static int match_export(struct device *dev, const void *data) | |
634 | { | |
635 | return dev_get_drvdata(dev) == data; | |
636 | } | |
637 | ||
638 | /** | |
639 | * gpiod_export_link - create a sysfs link to an exported GPIO node | |
640 | * @dev: device under which to create symlink | |
641 | * @name: name of the symlink | |
642 | * @gpio: gpio to create symlink to, already exported | |
643 | * | |
644 | * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN | |
645 | * node. Caller is responsible for unlinking. | |
646 | * | |
647 | * Returns zero on success, else an error. | |
648 | */ | |
649 | int gpiod_export_link(struct device *dev, const char *name, | |
650 | struct gpio_desc *desc) | |
651 | { | |
652 | int status = -EINVAL; | |
653 | ||
654 | if (!desc) { | |
655 | pr_warn("%s: invalid GPIO\n", __func__); | |
656 | return -EINVAL; | |
657 | } | |
658 | ||
659 | mutex_lock(&sysfs_lock); | |
660 | ||
661 | if (test_bit(FLAG_EXPORT, &desc->flags)) { | |
662 | struct device *tdev; | |
663 | ||
664 | tdev = class_find_device(&gpio_class, NULL, desc, match_export); | |
665 | if (tdev != NULL) { | |
666 | status = sysfs_create_link(&dev->kobj, &tdev->kobj, | |
667 | name); | |
0f303db0 | 668 | put_device(tdev); |
0eb4c6c2 AC |
669 | } else { |
670 | status = -ENODEV; | |
671 | } | |
672 | } | |
673 | ||
674 | mutex_unlock(&sysfs_lock); | |
675 | ||
676 | if (status) | |
677 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); | |
678 | ||
679 | return status; | |
680 | } | |
681 | EXPORT_SYMBOL_GPL(gpiod_export_link); | |
682 | ||
683 | /** | |
684 | * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value | |
685 | * @gpio: gpio to change | |
686 | * @value: non-zero to use active low, i.e. inverted values | |
687 | * | |
688 | * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute. | |
689 | * The GPIO does not have to be exported yet. If poll(2) support has | |
690 | * been enabled for either rising or falling edge, it will be | |
691 | * reconfigured to follow the new polarity. | |
692 | * | |
693 | * Returns zero on success, else an error. | |
694 | */ | |
695 | int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) | |
696 | { | |
697 | struct device *dev = NULL; | |
698 | int status = -EINVAL; | |
699 | ||
700 | if (!desc) { | |
701 | pr_warn("%s: invalid GPIO\n", __func__); | |
702 | return -EINVAL; | |
703 | } | |
704 | ||
705 | mutex_lock(&sysfs_lock); | |
706 | ||
707 | if (test_bit(FLAG_EXPORT, &desc->flags)) { | |
708 | dev = class_find_device(&gpio_class, NULL, desc, match_export); | |
709 | if (dev == NULL) { | |
710 | status = -ENODEV; | |
711 | goto unlock; | |
712 | } | |
713 | } | |
714 | ||
715 | status = sysfs_set_active_low(desc, dev, value); | |
49d2ca84 | 716 | put_device(dev); |
0eb4c6c2 AC |
717 | unlock: |
718 | mutex_unlock(&sysfs_lock); | |
719 | ||
720 | if (status) | |
721 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); | |
722 | ||
723 | return status; | |
724 | } | |
725 | EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low); | |
726 | ||
727 | /** | |
728 | * gpiod_unexport - reverse effect of gpio_export() | |
729 | * @gpio: gpio to make unavailable | |
730 | * | |
731 | * This is implicit on gpio_free(). | |
732 | */ | |
733 | void gpiod_unexport(struct gpio_desc *desc) | |
734 | { | |
735 | int status = 0; | |
736 | struct device *dev = NULL; | |
737 | ||
738 | if (!desc) { | |
739 | pr_warn("%s: invalid GPIO\n", __func__); | |
740 | return; | |
741 | } | |
742 | ||
743 | mutex_lock(&sysfs_lock); | |
744 | ||
745 | if (test_bit(FLAG_EXPORT, &desc->flags)) { | |
746 | ||
747 | dev = class_find_device(&gpio_class, NULL, desc, match_export); | |
748 | if (dev) { | |
749 | gpio_setup_irq(desc, dev, 0); | |
ebbeba12 | 750 | clear_bit(FLAG_SYSFS_DIR, &desc->flags); |
0eb4c6c2 AC |
751 | clear_bit(FLAG_EXPORT, &desc->flags); |
752 | } else | |
753 | status = -ENODEV; | |
754 | } | |
755 | ||
756 | mutex_unlock(&sysfs_lock); | |
757 | ||
758 | if (dev) { | |
759 | device_unregister(dev); | |
760 | put_device(dev); | |
761 | } | |
762 | ||
763 | if (status) | |
764 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); | |
765 | } | |
766 | EXPORT_SYMBOL_GPL(gpiod_unexport); | |
767 | ||
768 | int gpiochip_export(struct gpio_chip *chip) | |
769 | { | |
770 | int status; | |
771 | struct device *dev; | |
772 | ||
773 | /* Many systems register gpio chips for SOC support very early, | |
774 | * before driver model support is available. In those cases we | |
775 | * export this later, in gpiolib_sysfs_init() ... here we just | |
776 | * verify that _some_ field of gpio_class got initialized. | |
777 | */ | |
778 | if (!gpio_class.p) | |
779 | return 0; | |
780 | ||
781 | /* use chip->base for the ID; it's already known to be unique */ | |
782 | mutex_lock(&sysfs_lock); | |
121b6a79 JH |
783 | dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0), |
784 | chip, gpiochip_groups, | |
785 | "gpiochip%d", chip->base); | |
786 | if (IS_ERR(dev)) | |
0eb4c6c2 | 787 | status = PTR_ERR(dev); |
121b6a79 JH |
788 | else |
789 | status = 0; | |
0eb4c6c2 AC |
790 | chip->exported = (status == 0); |
791 | mutex_unlock(&sysfs_lock); | |
792 | ||
14141a93 | 793 | if (status) |
0eb4c6c2 | 794 | chip_dbg(chip, "%s: status %d\n", __func__, status); |
0eb4c6c2 AC |
795 | |
796 | return status; | |
797 | } | |
798 | ||
799 | void gpiochip_unexport(struct gpio_chip *chip) | |
800 | { | |
801 | int status; | |
802 | struct device *dev; | |
483d8211 JH |
803 | struct gpio_desc *desc; |
804 | unsigned int i; | |
0eb4c6c2 AC |
805 | |
806 | mutex_lock(&sysfs_lock); | |
807 | dev = class_find_device(&gpio_class, NULL, chip, match_export); | |
808 | if (dev) { | |
809 | put_device(dev); | |
810 | device_unregister(dev); | |
483d8211 | 811 | /* prevent further gpiod exports */ |
0eb4c6c2 AC |
812 | chip->exported = false; |
813 | status = 0; | |
814 | } else | |
815 | status = -ENODEV; | |
816 | mutex_unlock(&sysfs_lock); | |
817 | ||
818 | if (status) | |
819 | chip_dbg(chip, "%s: status %d\n", __func__, status); | |
483d8211 JH |
820 | |
821 | /* unregister gpiod class devices owned by sysfs */ | |
822 | for (i = 0; i < chip->ngpio; i++) { | |
823 | desc = &chip->desc[i]; | |
824 | if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) | |
825 | gpiod_free(desc); | |
826 | } | |
0eb4c6c2 AC |
827 | } |
828 | ||
829 | static int __init gpiolib_sysfs_init(void) | |
830 | { | |
831 | int status; | |
832 | unsigned long flags; | |
833 | struct gpio_chip *chip; | |
834 | ||
835 | status = class_register(&gpio_class); | |
836 | if (status < 0) | |
837 | return status; | |
838 | ||
839 | /* Scan and register the gpio_chips which registered very | |
840 | * early (e.g. before the class_register above was called). | |
841 | * | |
842 | * We run before arch_initcall() so chip->dev nodes can have | |
843 | * registered, and so arch_initcall() can always gpio_export(). | |
844 | */ | |
845 | spin_lock_irqsave(&gpio_lock, flags); | |
846 | list_for_each_entry(chip, &gpio_chips, list) { | |
7fd834ad | 847 | if (chip->exported) |
0eb4c6c2 AC |
848 | continue; |
849 | ||
14141a93 AC |
850 | /* |
851 | * TODO we yield gpio_lock here because gpiochip_export() | |
852 | * acquires a mutex. This is unsafe and needs to be fixed. | |
853 | * | |
854 | * Also it would be nice to use gpiochip_find() here so we | |
855 | * can keep gpio_chips local to gpiolib.c, but the yield of | |
856 | * gpio_lock prevents us from doing this. | |
857 | */ | |
0eb4c6c2 AC |
858 | spin_unlock_irqrestore(&gpio_lock, flags); |
859 | status = gpiochip_export(chip); | |
860 | spin_lock_irqsave(&gpio_lock, flags); | |
861 | } | |
862 | spin_unlock_irqrestore(&gpio_lock, flags); | |
863 | ||
864 | ||
865 | return status; | |
866 | } | |
867 | postcore_initcall(gpiolib_sysfs_init); |