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