]>
Commit | Line | Data |
---|---|---|
d2876d08 DB |
1 | #include <linux/kernel.h> |
2 | #include <linux/module.h> | |
ff77c352 | 3 | #include <linux/interrupt.h> |
d2876d08 DB |
4 | #include <linux/irq.h> |
5 | #include <linux/spinlock.h> | |
d8f388d8 DB |
6 | #include <linux/device.h> |
7 | #include <linux/err.h> | |
8 | #include <linux/debugfs.h> | |
9 | #include <linux/seq_file.h> | |
10 | #include <linux/gpio.h> | |
391c970c | 11 | #include <linux/of_gpio.h> |
ff77c352 | 12 | #include <linux/idr.h> |
5a0e3ad6 | 13 | #include <linux/slab.h> |
d2876d08 | 14 | |
3f397c21 UKK |
15 | #define CREATE_TRACE_POINTS |
16 | #include <trace/events/gpio.h> | |
d2876d08 DB |
17 | |
18 | /* Optional implementation infrastructure for GPIO interfaces. | |
19 | * | |
20 | * Platforms may want to use this if they tend to use very many GPIOs | |
21 | * that aren't part of a System-On-Chip core; or across I2C/SPI/etc. | |
22 | * | |
23 | * When kernel footprint or instruction count is an issue, simpler | |
24 | * implementations may be preferred. The GPIO programming interface | |
25 | * allows for inlining speed-critical get/set operations for common | |
26 | * cases, so that access to SOC-integrated GPIOs can sometimes cost | |
27 | * only an instruction or two per bit. | |
28 | */ | |
29 | ||
30 | ||
31 | /* When debugging, extend minimal trust to callers and platform code. | |
32 | * Also emit diagnostic messages that may help initial bringup, when | |
33 | * board setup or driver bugs are most common. | |
34 | * | |
35 | * Otherwise, minimize overhead in what may be bitbanging codepaths. | |
36 | */ | |
37 | #ifdef DEBUG | |
38 | #define extra_checks 1 | |
39 | #else | |
40 | #define extra_checks 0 | |
41 | #endif | |
42 | ||
43 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. | |
44 | * While any GPIO is requested, its gpio_chip is not removable; | |
45 | * each GPIO's "requested" flag serves as a lock and refcount. | |
46 | */ | |
47 | static DEFINE_SPINLOCK(gpio_lock); | |
48 | ||
49 | struct gpio_desc { | |
50 | struct gpio_chip *chip; | |
51 | unsigned long flags; | |
52 | /* flag symbols are bit numbers */ | |
53 | #define FLAG_REQUESTED 0 | |
54 | #define FLAG_IS_OUT 1 | |
169b6a7a | 55 | #define FLAG_RESERVED 2 |
d8f388d8 DB |
56 | #define FLAG_EXPORT 3 /* protected by sysfs_lock */ |
57 | #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */ | |
ff77c352 DG |
58 | #define FLAG_TRIG_FALL 5 /* trigger on falling edge */ |
59 | #define FLAG_TRIG_RISE 6 /* trigger on rising edge */ | |
07697461 | 60 | #define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */ |
ff77c352 | 61 | |
5ba1821d | 62 | #define ID_SHIFT 16 /* add new flags before this one */ |
ff77c352 | 63 | |
5ba1821d | 64 | #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1) |
ff77c352 | 65 | #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE)) |
d2876d08 DB |
66 | |
67 | #ifdef CONFIG_DEBUG_FS | |
68 | const char *label; | |
69 | #endif | |
70 | }; | |
71 | static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; | |
72 | ||
ff77c352 | 73 | #ifdef CONFIG_GPIO_SYSFS |
5ba1821d | 74 | static DEFINE_IDR(dirent_idr); |
ff77c352 DG |
75 | #endif |
76 | ||
d2876d08 DB |
77 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
78 | { | |
79 | #ifdef CONFIG_DEBUG_FS | |
80 | d->label = label; | |
81 | #endif | |
82 | } | |
83 | ||
84 | /* Warn when drivers omit gpio_request() calls -- legal but ill-advised | |
85 | * when setting direction, and otherwise illegal. Until board setup code | |
86 | * and drivers use explicit requests everywhere (which won't happen when | |
87 | * those calls have no teeth) we can't avoid autorequesting. This nag | |
35e8bb51 DB |
88 | * message should motivate switching to explicit requests... so should |
89 | * the weaker cleanup after faults, compared to gpio_request(). | |
8a0cecff DB |
90 | * |
91 | * NOTE: the autorequest mechanism is going away; at this point it's | |
92 | * only "legal" in the sense that (old) code using it won't break yet, | |
93 | * but instead only triggers a WARN() stack dump. | |
d2876d08 | 94 | */ |
35e8bb51 | 95 | static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset) |
d2876d08 | 96 | { |
8a0cecff DB |
97 | const struct gpio_chip *chip = desc->chip; |
98 | const int gpio = chip->base + offset; | |
35e8bb51 | 99 | |
8a0cecff DB |
100 | if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0, |
101 | "autorequest GPIO-%d\n", gpio)) { | |
35e8bb51 DB |
102 | if (!try_module_get(chip->owner)) { |
103 | pr_err("GPIO-%d: module can't be gotten \n", gpio); | |
104 | clear_bit(FLAG_REQUESTED, &desc->flags); | |
105 | /* lose */ | |
106 | return -EIO; | |
107 | } | |
d2876d08 | 108 | desc_set_label(desc, "[auto]"); |
35e8bb51 DB |
109 | /* caller must chip->request() w/o spinlock */ |
110 | if (chip->request) | |
111 | return 1; | |
d2876d08 | 112 | } |
35e8bb51 | 113 | return 0; |
d2876d08 DB |
114 | } |
115 | ||
116 | /* caller holds gpio_lock *OR* gpio is marked as requested */ | |
1a2d397a | 117 | struct gpio_chip *gpio_to_chip(unsigned gpio) |
d2876d08 DB |
118 | { |
119 | return gpio_desc[gpio].chip; | |
120 | } | |
121 | ||
8d0aab2f AV |
122 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
123 | static int gpiochip_find_base(int ngpio) | |
124 | { | |
125 | int i; | |
126 | int spare = 0; | |
127 | int base = -ENOSPC; | |
128 | ||
129 | for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) { | |
169b6a7a AV |
130 | struct gpio_desc *desc = &gpio_desc[i]; |
131 | struct gpio_chip *chip = desc->chip; | |
8d0aab2f | 132 | |
169b6a7a | 133 | if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) { |
8d0aab2f AV |
134 | spare++; |
135 | if (spare == ngpio) { | |
136 | base = i; | |
137 | break; | |
138 | } | |
139 | } else { | |
140 | spare = 0; | |
169b6a7a AV |
141 | if (chip) |
142 | i -= chip->ngpio - 1; | |
8d0aab2f AV |
143 | } |
144 | } | |
145 | ||
146 | if (gpio_is_valid(base)) | |
147 | pr_debug("%s: found new base at %d\n", __func__, base); | |
148 | return base; | |
149 | } | |
150 | ||
169b6a7a AV |
151 | /** |
152 | * gpiochip_reserve() - reserve range of gpios to use with platform code only | |
153 | * @start: starting gpio number | |
154 | * @ngpio: number of gpios to reserve | |
155 | * Context: platform init, potentially before irqs or kmalloc will work | |
156 | * | |
157 | * Returns a negative errno if any gpio within the range is already reserved | |
158 | * or registered, else returns zero as a success code. Use this function | |
159 | * to mark a range of gpios as unavailable for dynamic gpio number allocation, | |
160 | * for example because its driver support is not yet loaded. | |
161 | */ | |
162 | int __init gpiochip_reserve(int start, int ngpio) | |
163 | { | |
164 | int ret = 0; | |
165 | unsigned long flags; | |
166 | int i; | |
167 | ||
bff5fda9 | 168 | if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1)) |
169b6a7a AV |
169 | return -EINVAL; |
170 | ||
171 | spin_lock_irqsave(&gpio_lock, flags); | |
172 | ||
173 | for (i = start; i < start + ngpio; i++) { | |
174 | struct gpio_desc *desc = &gpio_desc[i]; | |
175 | ||
176 | if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) { | |
177 | ret = -EBUSY; | |
178 | goto err; | |
179 | } | |
180 | ||
181 | set_bit(FLAG_RESERVED, &desc->flags); | |
182 | } | |
183 | ||
184 | pr_debug("%s: reserved gpios from %d to %d\n", | |
185 | __func__, start, start + ngpio - 1); | |
186 | err: | |
187 | spin_unlock_irqrestore(&gpio_lock, flags); | |
188 | ||
189 | return ret; | |
190 | } | |
191 | ||
d8f388d8 DB |
192 | #ifdef CONFIG_GPIO_SYSFS |
193 | ||
194 | /* lock protects against unexport_gpio() being called while | |
195 | * sysfs files are active. | |
196 | */ | |
197 | static DEFINE_MUTEX(sysfs_lock); | |
198 | ||
199 | /* | |
200 | * /sys/class/gpio/gpioN... only for GPIOs that are exported | |
201 | * /direction | |
202 | * * MAY BE OMITTED if kernel won't allow direction changes | |
203 | * * is read/write as "in" or "out" | |
204 | * * may also be written as "high" or "low", initializing | |
205 | * output value as specified ("out" implies "low") | |
206 | * /value | |
207 | * * always readable, subject to hardware behavior | |
208 | * * may be writable, as zero/nonzero | |
ff77c352 DG |
209 | * /edge |
210 | * * configures behavior of poll(2) on /value | |
211 | * * available only if pin can generate IRQs on input | |
212 | * * is read/write as "none", "falling", "rising", or "both" | |
07697461 JN |
213 | * /active_low |
214 | * * configures polarity of /value | |
215 | * * is read/write as zero/nonzero | |
216 | * * also affects existing and subsequent "falling" and "rising" | |
217 | * /edge configuration | |
d8f388d8 DB |
218 | */ |
219 | ||
220 | static ssize_t gpio_direction_show(struct device *dev, | |
221 | struct device_attribute *attr, char *buf) | |
222 | { | |
223 | const struct gpio_desc *desc = dev_get_drvdata(dev); | |
224 | ssize_t status; | |
225 | ||
226 | mutex_lock(&sysfs_lock); | |
227 | ||
228 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
229 | status = -EIO; | |
230 | else | |
231 | status = sprintf(buf, "%s\n", | |
232 | test_bit(FLAG_IS_OUT, &desc->flags) | |
233 | ? "out" : "in"); | |
234 | ||
235 | mutex_unlock(&sysfs_lock); | |
236 | return status; | |
237 | } | |
238 | ||
239 | static ssize_t gpio_direction_store(struct device *dev, | |
240 | struct device_attribute *attr, const char *buf, size_t size) | |
241 | { | |
242 | const struct gpio_desc *desc = dev_get_drvdata(dev); | |
243 | unsigned gpio = desc - gpio_desc; | |
244 | ssize_t status; | |
245 | ||
246 | mutex_lock(&sysfs_lock); | |
247 | ||
248 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
249 | status = -EIO; | |
250 | else if (sysfs_streq(buf, "high")) | |
251 | status = gpio_direction_output(gpio, 1); | |
252 | else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) | |
253 | status = gpio_direction_output(gpio, 0); | |
254 | else if (sysfs_streq(buf, "in")) | |
255 | status = gpio_direction_input(gpio); | |
256 | else | |
257 | status = -EINVAL; | |
258 | ||
259 | mutex_unlock(&sysfs_lock); | |
260 | return status ? : size; | |
261 | } | |
262 | ||
07697461 | 263 | static /* const */ DEVICE_ATTR(direction, 0644, |
d8f388d8 DB |
264 | gpio_direction_show, gpio_direction_store); |
265 | ||
266 | static ssize_t gpio_value_show(struct device *dev, | |
267 | struct device_attribute *attr, char *buf) | |
268 | { | |
269 | const struct gpio_desc *desc = dev_get_drvdata(dev); | |
270 | unsigned gpio = desc - gpio_desc; | |
271 | ssize_t status; | |
272 | ||
273 | mutex_lock(&sysfs_lock); | |
274 | ||
07697461 | 275 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { |
d8f388d8 | 276 | status = -EIO; |
07697461 JN |
277 | } else { |
278 | int value; | |
279 | ||
280 | value = !!gpio_get_value_cansleep(gpio); | |
281 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
282 | value = !value; | |
283 | ||
284 | status = sprintf(buf, "%d\n", value); | |
285 | } | |
d8f388d8 DB |
286 | |
287 | mutex_unlock(&sysfs_lock); | |
288 | return status; | |
289 | } | |
290 | ||
291 | static ssize_t gpio_value_store(struct device *dev, | |
292 | struct device_attribute *attr, const char *buf, size_t size) | |
293 | { | |
294 | const struct gpio_desc *desc = dev_get_drvdata(dev); | |
295 | unsigned gpio = desc - gpio_desc; | |
296 | ssize_t status; | |
297 | ||
298 | mutex_lock(&sysfs_lock); | |
299 | ||
300 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
301 | status = -EIO; | |
302 | else if (!test_bit(FLAG_IS_OUT, &desc->flags)) | |
303 | status = -EPERM; | |
304 | else { | |
305 | long value; | |
306 | ||
307 | status = strict_strtol(buf, 0, &value); | |
308 | if (status == 0) { | |
07697461 JN |
309 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
310 | value = !value; | |
d8f388d8 DB |
311 | gpio_set_value_cansleep(gpio, value != 0); |
312 | status = size; | |
313 | } | |
314 | } | |
315 | ||
316 | mutex_unlock(&sysfs_lock); | |
317 | return status; | |
318 | } | |
319 | ||
07697461 | 320 | static const DEVICE_ATTR(value, 0644, |
d8f388d8 DB |
321 | gpio_value_show, gpio_value_store); |
322 | ||
ff77c352 DG |
323 | static irqreturn_t gpio_sysfs_irq(int irq, void *priv) |
324 | { | |
5ba1821d | 325 | struct sysfs_dirent *value_sd = priv; |
ff77c352 | 326 | |
5ba1821d | 327 | sysfs_notify_dirent(value_sd); |
ff77c352 DG |
328 | return IRQ_HANDLED; |
329 | } | |
330 | ||
ff77c352 DG |
331 | static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, |
332 | unsigned long gpio_flags) | |
333 | { | |
5ba1821d | 334 | struct sysfs_dirent *value_sd; |
ff77c352 DG |
335 | unsigned long irq_flags; |
336 | int ret, irq, id; | |
337 | ||
338 | if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags) | |
339 | return 0; | |
340 | ||
341 | irq = gpio_to_irq(desc - gpio_desc); | |
342 | if (irq < 0) | |
343 | return -EIO; | |
344 | ||
5ba1821d DG |
345 | id = desc->flags >> ID_SHIFT; |
346 | value_sd = idr_find(&dirent_idr, id); | |
347 | if (value_sd) | |
348 | free_irq(irq, value_sd); | |
ff77c352 DG |
349 | |
350 | desc->flags &= ~GPIO_TRIGGER_MASK; | |
351 | ||
352 | if (!gpio_flags) { | |
353 | ret = 0; | |
5ba1821d | 354 | goto free_id; |
ff77c352 DG |
355 | } |
356 | ||
357 | irq_flags = IRQF_SHARED; | |
358 | if (test_bit(FLAG_TRIG_FALL, &gpio_flags)) | |
07697461 JN |
359 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
360 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; | |
ff77c352 | 361 | if (test_bit(FLAG_TRIG_RISE, &gpio_flags)) |
07697461 JN |
362 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
363 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; | |
ff77c352 | 364 | |
5ba1821d DG |
365 | if (!value_sd) { |
366 | value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value"); | |
367 | if (!value_sd) { | |
368 | ret = -ENODEV; | |
ff77c352 DG |
369 | goto err_out; |
370 | } | |
371 | ||
372 | do { | |
373 | ret = -ENOMEM; | |
5ba1821d DG |
374 | if (idr_pre_get(&dirent_idr, GFP_KERNEL)) |
375 | ret = idr_get_new_above(&dirent_idr, value_sd, | |
376 | 1, &id); | |
ff77c352 DG |
377 | } while (ret == -EAGAIN); |
378 | ||
379 | if (ret) | |
5ba1821d | 380 | goto free_sd; |
ff77c352 DG |
381 | |
382 | desc->flags &= GPIO_FLAGS_MASK; | |
5ba1821d | 383 | desc->flags |= (unsigned long)id << ID_SHIFT; |
ff77c352 | 384 | |
5ba1821d | 385 | if (desc->flags >> ID_SHIFT != id) { |
ff77c352 DG |
386 | ret = -ERANGE; |
387 | goto free_id; | |
388 | } | |
ff77c352 DG |
389 | } |
390 | ||
364fadb3 | 391 | ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags, |
5ba1821d | 392 | "gpiolib", value_sd); |
364fadb3 | 393 | if (ret < 0) |
5ba1821d | 394 | goto free_id; |
ff77c352 DG |
395 | |
396 | desc->flags |= gpio_flags; | |
397 | return 0; | |
398 | ||
ff77c352 | 399 | free_id: |
5ba1821d | 400 | idr_remove(&dirent_idr, id); |
ff77c352 | 401 | desc->flags &= GPIO_FLAGS_MASK; |
5ba1821d DG |
402 | free_sd: |
403 | if (value_sd) | |
404 | sysfs_put(value_sd); | |
ff77c352 DG |
405 | err_out: |
406 | return ret; | |
407 | } | |
408 | ||
409 | static const struct { | |
410 | const char *name; | |
411 | unsigned long flags; | |
412 | } trigger_types[] = { | |
413 | { "none", 0 }, | |
414 | { "falling", BIT(FLAG_TRIG_FALL) }, | |
415 | { "rising", BIT(FLAG_TRIG_RISE) }, | |
416 | { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) }, | |
417 | }; | |
418 | ||
419 | static ssize_t gpio_edge_show(struct device *dev, | |
420 | struct device_attribute *attr, char *buf) | |
421 | { | |
422 | const struct gpio_desc *desc = dev_get_drvdata(dev); | |
423 | ssize_t status; | |
424 | ||
425 | mutex_lock(&sysfs_lock); | |
426 | ||
427 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
428 | status = -EIO; | |
429 | else { | |
430 | int i; | |
431 | ||
432 | status = 0; | |
433 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) | |
434 | if ((desc->flags & GPIO_TRIGGER_MASK) | |
435 | == trigger_types[i].flags) { | |
436 | status = sprintf(buf, "%s\n", | |
437 | trigger_types[i].name); | |
438 | break; | |
439 | } | |
440 | } | |
441 | ||
442 | mutex_unlock(&sysfs_lock); | |
443 | return status; | |
444 | } | |
445 | ||
446 | static ssize_t gpio_edge_store(struct device *dev, | |
447 | struct device_attribute *attr, const char *buf, size_t size) | |
448 | { | |
449 | struct gpio_desc *desc = dev_get_drvdata(dev); | |
450 | ssize_t status; | |
451 | int i; | |
452 | ||
453 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) | |
454 | if (sysfs_streq(trigger_types[i].name, buf)) | |
455 | goto found; | |
456 | return -EINVAL; | |
457 | ||
458 | found: | |
459 | mutex_lock(&sysfs_lock); | |
460 | ||
461 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
462 | status = -EIO; | |
463 | else { | |
464 | status = gpio_setup_irq(desc, dev, trigger_types[i].flags); | |
465 | if (!status) | |
466 | status = size; | |
467 | } | |
468 | ||
469 | mutex_unlock(&sysfs_lock); | |
470 | ||
471 | return status; | |
472 | } | |
473 | ||
474 | static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store); | |
475 | ||
07697461 JN |
476 | static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev, |
477 | int value) | |
478 | { | |
479 | int status = 0; | |
480 | ||
481 | if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) | |
482 | return 0; | |
483 | ||
484 | if (value) | |
485 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
486 | else | |
487 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
488 | ||
489 | /* reconfigure poll(2) support if enabled on one edge only */ | |
490 | if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^ | |
491 | !!test_bit(FLAG_TRIG_FALL, &desc->flags))) { | |
492 | unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK; | |
493 | ||
494 | gpio_setup_irq(desc, dev, 0); | |
495 | status = gpio_setup_irq(desc, dev, trigger_flags); | |
496 | } | |
497 | ||
498 | return status; | |
499 | } | |
500 | ||
501 | static ssize_t gpio_active_low_show(struct device *dev, | |
502 | struct device_attribute *attr, char *buf) | |
503 | { | |
504 | const struct gpio_desc *desc = dev_get_drvdata(dev); | |
505 | ssize_t status; | |
506 | ||
507 | mutex_lock(&sysfs_lock); | |
508 | ||
509 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
510 | status = -EIO; | |
511 | else | |
512 | status = sprintf(buf, "%d\n", | |
513 | !!test_bit(FLAG_ACTIVE_LOW, &desc->flags)); | |
514 | ||
515 | mutex_unlock(&sysfs_lock); | |
516 | ||
517 | return status; | |
518 | } | |
519 | ||
520 | static ssize_t gpio_active_low_store(struct device *dev, | |
521 | struct device_attribute *attr, const char *buf, size_t size) | |
522 | { | |
523 | struct gpio_desc *desc = dev_get_drvdata(dev); | |
524 | ssize_t status; | |
525 | ||
526 | mutex_lock(&sysfs_lock); | |
527 | ||
528 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { | |
529 | status = -EIO; | |
530 | } else { | |
531 | long value; | |
532 | ||
533 | status = strict_strtol(buf, 0, &value); | |
534 | if (status == 0) | |
535 | status = sysfs_set_active_low(desc, dev, value != 0); | |
536 | } | |
537 | ||
538 | mutex_unlock(&sysfs_lock); | |
539 | ||
540 | return status ? : size; | |
541 | } | |
542 | ||
543 | static const DEVICE_ATTR(active_low, 0644, | |
544 | gpio_active_low_show, gpio_active_low_store); | |
545 | ||
d8f388d8 | 546 | static const struct attribute *gpio_attrs[] = { |
d8f388d8 | 547 | &dev_attr_value.attr, |
07697461 | 548 | &dev_attr_active_low.attr, |
d8f388d8 DB |
549 | NULL, |
550 | }; | |
551 | ||
552 | static const struct attribute_group gpio_attr_group = { | |
553 | .attrs = (struct attribute **) gpio_attrs, | |
554 | }; | |
555 | ||
556 | /* | |
557 | * /sys/class/gpio/gpiochipN/ | |
558 | * /base ... matching gpio_chip.base (N) | |
559 | * /label ... matching gpio_chip.label | |
560 | * /ngpio ... matching gpio_chip.ngpio | |
561 | */ | |
562 | ||
563 | static ssize_t chip_base_show(struct device *dev, | |
564 | struct device_attribute *attr, char *buf) | |
565 | { | |
566 | const struct gpio_chip *chip = dev_get_drvdata(dev); | |
567 | ||
568 | return sprintf(buf, "%d\n", chip->base); | |
569 | } | |
570 | static DEVICE_ATTR(base, 0444, chip_base_show, NULL); | |
571 | ||
572 | static ssize_t chip_label_show(struct device *dev, | |
573 | struct device_attribute *attr, char *buf) | |
574 | { | |
575 | const struct gpio_chip *chip = dev_get_drvdata(dev); | |
576 | ||
577 | return sprintf(buf, "%s\n", chip->label ? : ""); | |
578 | } | |
579 | static DEVICE_ATTR(label, 0444, chip_label_show, NULL); | |
580 | ||
581 | static ssize_t chip_ngpio_show(struct device *dev, | |
582 | struct device_attribute *attr, char *buf) | |
583 | { | |
584 | const struct gpio_chip *chip = dev_get_drvdata(dev); | |
585 | ||
586 | return sprintf(buf, "%u\n", chip->ngpio); | |
587 | } | |
588 | static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); | |
589 | ||
590 | static const struct attribute *gpiochip_attrs[] = { | |
591 | &dev_attr_base.attr, | |
592 | &dev_attr_label.attr, | |
593 | &dev_attr_ngpio.attr, | |
594 | NULL, | |
595 | }; | |
596 | ||
597 | static const struct attribute_group gpiochip_attr_group = { | |
598 | .attrs = (struct attribute **) gpiochip_attrs, | |
599 | }; | |
600 | ||
601 | /* | |
602 | * /sys/class/gpio/export ... write-only | |
603 | * integer N ... number of GPIO to export (full access) | |
604 | * /sys/class/gpio/unexport ... write-only | |
605 | * integer N ... number of GPIO to unexport | |
606 | */ | |
28812fe1 AK |
607 | static ssize_t export_store(struct class *class, |
608 | struct class_attribute *attr, | |
609 | const char *buf, size_t len) | |
d8f388d8 DB |
610 | { |
611 | long gpio; | |
612 | int status; | |
613 | ||
614 | status = strict_strtol(buf, 0, &gpio); | |
615 | if (status < 0) | |
616 | goto done; | |
617 | ||
618 | /* No extra locking here; FLAG_SYSFS just signifies that the | |
619 | * request and export were done by on behalf of userspace, so | |
620 | * they may be undone on its behalf too. | |
621 | */ | |
622 | ||
623 | status = gpio_request(gpio, "sysfs"); | |
624 | if (status < 0) | |
625 | goto done; | |
626 | ||
627 | status = gpio_export(gpio, true); | |
628 | if (status < 0) | |
629 | gpio_free(gpio); | |
630 | else | |
631 | set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags); | |
632 | ||
633 | done: | |
634 | if (status) | |
635 | pr_debug("%s: status %d\n", __func__, status); | |
636 | return status ? : len; | |
637 | } | |
638 | ||
28812fe1 AK |
639 | static ssize_t unexport_store(struct class *class, |
640 | struct class_attribute *attr, | |
641 | const char *buf, size_t len) | |
d8f388d8 DB |
642 | { |
643 | long gpio; | |
644 | int status; | |
645 | ||
646 | status = strict_strtol(buf, 0, &gpio); | |
647 | if (status < 0) | |
648 | goto done; | |
649 | ||
650 | status = -EINVAL; | |
651 | ||
652 | /* reject bogus commands (gpio_unexport ignores them) */ | |
653 | if (!gpio_is_valid(gpio)) | |
654 | goto done; | |
655 | ||
656 | /* No extra locking here; FLAG_SYSFS just signifies that the | |
657 | * request and export were done by on behalf of userspace, so | |
658 | * they may be undone on its behalf too. | |
659 | */ | |
660 | if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) { | |
661 | status = 0; | |
662 | gpio_free(gpio); | |
663 | } | |
664 | done: | |
665 | if (status) | |
666 | pr_debug("%s: status %d\n", __func__, status); | |
667 | return status ? : len; | |
668 | } | |
669 | ||
670 | static struct class_attribute gpio_class_attrs[] = { | |
671 | __ATTR(export, 0200, NULL, export_store), | |
672 | __ATTR(unexport, 0200, NULL, unexport_store), | |
673 | __ATTR_NULL, | |
674 | }; | |
675 | ||
676 | static struct class gpio_class = { | |
677 | .name = "gpio", | |
678 | .owner = THIS_MODULE, | |
679 | ||
680 | .class_attrs = gpio_class_attrs, | |
681 | }; | |
682 | ||
683 | ||
684 | /** | |
685 | * gpio_export - export a GPIO through sysfs | |
686 | * @gpio: gpio to make available, already requested | |
687 | * @direction_may_change: true if userspace may change gpio direction | |
688 | * Context: arch_initcall or later | |
689 | * | |
690 | * When drivers want to make a GPIO accessible to userspace after they | |
691 | * have requested it -- perhaps while debugging, or as part of their | |
692 | * public interface -- they may use this routine. If the GPIO can | |
693 | * change direction (some can't) and the caller allows it, userspace | |
694 | * will see "direction" sysfs attribute which may be used to change | |
695 | * the gpio's direction. A "value" attribute will always be provided. | |
696 | * | |
697 | * Returns zero on success, else an error. | |
698 | */ | |
699 | int gpio_export(unsigned gpio, bool direction_may_change) | |
700 | { | |
701 | unsigned long flags; | |
702 | struct gpio_desc *desc; | |
703 | int status = -EINVAL; | |
62154991 | 704 | const char *ioname = NULL; |
d8f388d8 DB |
705 | |
706 | /* can't export until sysfs is available ... */ | |
707 | if (!gpio_class.p) { | |
708 | pr_debug("%s: called too early!\n", __func__); | |
709 | return -ENOENT; | |
710 | } | |
711 | ||
712 | if (!gpio_is_valid(gpio)) | |
713 | goto done; | |
714 | ||
715 | mutex_lock(&sysfs_lock); | |
716 | ||
717 | spin_lock_irqsave(&gpio_lock, flags); | |
718 | desc = &gpio_desc[gpio]; | |
719 | if (test_bit(FLAG_REQUESTED, &desc->flags) | |
720 | && !test_bit(FLAG_EXPORT, &desc->flags)) { | |
721 | status = 0; | |
722 | if (!desc->chip->direction_input | |
723 | || !desc->chip->direction_output) | |
724 | direction_may_change = false; | |
725 | } | |
726 | spin_unlock_irqrestore(&gpio_lock, flags); | |
727 | ||
926b663c DS |
728 | if (desc->chip->names && desc->chip->names[gpio - desc->chip->base]) |
729 | ioname = desc->chip->names[gpio - desc->chip->base]; | |
730 | ||
d8f388d8 DB |
731 | if (status == 0) { |
732 | struct device *dev; | |
733 | ||
734 | dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), | |
7cfe1395 | 735 | desc, ioname ? ioname : "gpio%u", gpio); |
d62668e1 | 736 | if (!IS_ERR(dev)) { |
07697461 | 737 | status = sysfs_create_group(&dev->kobj, |
d8f388d8 | 738 | &gpio_attr_group); |
07697461 JN |
739 | |
740 | if (!status && direction_may_change) | |
d8f388d8 | 741 | status = device_create_file(dev, |
07697461 | 742 | &dev_attr_direction); |
ff77c352 DG |
743 | |
744 | if (!status && gpio_to_irq(gpio) >= 0 | |
745 | && (direction_may_change | |
746 | || !test_bit(FLAG_IS_OUT, | |
747 | &desc->flags))) | |
748 | status = device_create_file(dev, | |
749 | &dev_attr_edge); | |
750 | ||
d8f388d8 DB |
751 | if (status != 0) |
752 | device_unregister(dev); | |
753 | } else | |
d62668e1 | 754 | status = PTR_ERR(dev); |
d8f388d8 DB |
755 | if (status == 0) |
756 | set_bit(FLAG_EXPORT, &desc->flags); | |
757 | } | |
758 | ||
759 | mutex_unlock(&sysfs_lock); | |
760 | ||
761 | done: | |
762 | if (status) | |
763 | pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); | |
764 | ||
765 | return status; | |
766 | } | |
767 | EXPORT_SYMBOL_GPL(gpio_export); | |
768 | ||
769 | static int match_export(struct device *dev, void *data) | |
770 | { | |
771 | return dev_get_drvdata(dev) == data; | |
772 | } | |
773 | ||
a4177ee7 JN |
774 | /** |
775 | * gpio_export_link - create a sysfs link to an exported GPIO node | |
776 | * @dev: device under which to create symlink | |
777 | * @name: name of the symlink | |
778 | * @gpio: gpio to create symlink to, already exported | |
779 | * | |
780 | * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN | |
781 | * node. Caller is responsible for unlinking. | |
782 | * | |
783 | * Returns zero on success, else an error. | |
784 | */ | |
785 | int gpio_export_link(struct device *dev, const char *name, unsigned gpio) | |
786 | { | |
787 | struct gpio_desc *desc; | |
788 | int status = -EINVAL; | |
789 | ||
790 | if (!gpio_is_valid(gpio)) | |
791 | goto done; | |
792 | ||
793 | mutex_lock(&sysfs_lock); | |
794 | ||
795 | desc = &gpio_desc[gpio]; | |
796 | ||
797 | if (test_bit(FLAG_EXPORT, &desc->flags)) { | |
798 | struct device *tdev; | |
799 | ||
800 | tdev = class_find_device(&gpio_class, NULL, desc, match_export); | |
801 | if (tdev != NULL) { | |
802 | status = sysfs_create_link(&dev->kobj, &tdev->kobj, | |
803 | name); | |
804 | } else { | |
805 | status = -ENODEV; | |
806 | } | |
807 | } | |
808 | ||
809 | mutex_unlock(&sysfs_lock); | |
810 | ||
811 | done: | |
812 | if (status) | |
813 | pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); | |
814 | ||
815 | return status; | |
816 | } | |
817 | EXPORT_SYMBOL_GPL(gpio_export_link); | |
818 | ||
07697461 JN |
819 | |
820 | /** | |
821 | * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value | |
822 | * @gpio: gpio to change | |
823 | * @value: non-zero to use active low, i.e. inverted values | |
824 | * | |
825 | * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute. | |
826 | * The GPIO does not have to be exported yet. If poll(2) support has | |
827 | * been enabled for either rising or falling edge, it will be | |
828 | * reconfigured to follow the new polarity. | |
829 | * | |
830 | * Returns zero on success, else an error. | |
831 | */ | |
832 | int gpio_sysfs_set_active_low(unsigned gpio, int value) | |
833 | { | |
834 | struct gpio_desc *desc; | |
835 | struct device *dev = NULL; | |
836 | int status = -EINVAL; | |
837 | ||
838 | if (!gpio_is_valid(gpio)) | |
839 | goto done; | |
840 | ||
841 | mutex_lock(&sysfs_lock); | |
842 | ||
843 | desc = &gpio_desc[gpio]; | |
844 | ||
845 | if (test_bit(FLAG_EXPORT, &desc->flags)) { | |
07697461 JN |
846 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
847 | if (dev == NULL) { | |
848 | status = -ENODEV; | |
849 | goto unlock; | |
850 | } | |
851 | } | |
852 | ||
853 | status = sysfs_set_active_low(desc, dev, value); | |
854 | ||
855 | unlock: | |
856 | mutex_unlock(&sysfs_lock); | |
857 | ||
858 | done: | |
859 | if (status) | |
860 | pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); | |
861 | ||
862 | return status; | |
863 | } | |
864 | EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low); | |
865 | ||
d8f388d8 DB |
866 | /** |
867 | * gpio_unexport - reverse effect of gpio_export() | |
868 | * @gpio: gpio to make unavailable | |
869 | * | |
870 | * This is implicit on gpio_free(). | |
871 | */ | |
872 | void gpio_unexport(unsigned gpio) | |
873 | { | |
874 | struct gpio_desc *desc; | |
6a99ad4a | 875 | int status = 0; |
864533ce | 876 | struct device *dev = NULL; |
d8f388d8 | 877 | |
6a99ad4a JP |
878 | if (!gpio_is_valid(gpio)) { |
879 | status = -EINVAL; | |
d8f388d8 | 880 | goto done; |
6a99ad4a | 881 | } |
d8f388d8 DB |
882 | |
883 | mutex_lock(&sysfs_lock); | |
884 | ||
885 | desc = &gpio_desc[gpio]; | |
926b663c | 886 | |
d8f388d8 | 887 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
d8f388d8 DB |
888 | |
889 | dev = class_find_device(&gpio_class, NULL, desc, match_export); | |
890 | if (dev) { | |
ff77c352 | 891 | gpio_setup_irq(desc, dev, 0); |
d8f388d8 | 892 | clear_bit(FLAG_EXPORT, &desc->flags); |
d8f388d8 DB |
893 | } else |
894 | status = -ENODEV; | |
895 | } | |
896 | ||
897 | mutex_unlock(&sysfs_lock); | |
864533ce ML |
898 | if (dev) { |
899 | device_unregister(dev); | |
900 | put_device(dev); | |
901 | } | |
d8f388d8 DB |
902 | done: |
903 | if (status) | |
904 | pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); | |
905 | } | |
906 | EXPORT_SYMBOL_GPL(gpio_unexport); | |
907 | ||
908 | static int gpiochip_export(struct gpio_chip *chip) | |
909 | { | |
910 | int status; | |
911 | struct device *dev; | |
912 | ||
913 | /* Many systems register gpio chips for SOC support very early, | |
914 | * before driver model support is available. In those cases we | |
915 | * export this later, in gpiolib_sysfs_init() ... here we just | |
916 | * verify that _some_ field of gpio_class got initialized. | |
917 | */ | |
918 | if (!gpio_class.p) | |
919 | return 0; | |
920 | ||
921 | /* use chip->base for the ID; it's already known to be unique */ | |
922 | mutex_lock(&sysfs_lock); | |
923 | dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip, | |
924 | "gpiochip%d", chip->base); | |
d62668e1 | 925 | if (!IS_ERR(dev)) { |
d8f388d8 DB |
926 | status = sysfs_create_group(&dev->kobj, |
927 | &gpiochip_attr_group); | |
928 | } else | |
d62668e1 | 929 | status = PTR_ERR(dev); |
d8f388d8 DB |
930 | chip->exported = (status == 0); |
931 | mutex_unlock(&sysfs_lock); | |
932 | ||
933 | if (status) { | |
934 | unsigned long flags; | |
935 | unsigned gpio; | |
936 | ||
937 | spin_lock_irqsave(&gpio_lock, flags); | |
938 | gpio = chip->base; | |
939 | while (gpio_desc[gpio].chip == chip) | |
940 | gpio_desc[gpio++].chip = NULL; | |
941 | spin_unlock_irqrestore(&gpio_lock, flags); | |
942 | ||
943 | pr_debug("%s: chip %s status %d\n", __func__, | |
944 | chip->label, status); | |
945 | } | |
946 | ||
947 | return status; | |
948 | } | |
949 | ||
950 | static void gpiochip_unexport(struct gpio_chip *chip) | |
951 | { | |
952 | int status; | |
953 | struct device *dev; | |
954 | ||
955 | mutex_lock(&sysfs_lock); | |
956 | dev = class_find_device(&gpio_class, NULL, chip, match_export); | |
957 | if (dev) { | |
958 | put_device(dev); | |
959 | device_unregister(dev); | |
960 | chip->exported = 0; | |
961 | status = 0; | |
962 | } else | |
963 | status = -ENODEV; | |
964 | mutex_unlock(&sysfs_lock); | |
965 | ||
966 | if (status) | |
967 | pr_debug("%s: chip %s status %d\n", __func__, | |
968 | chip->label, status); | |
969 | } | |
970 | ||
971 | static int __init gpiolib_sysfs_init(void) | |
972 | { | |
973 | int status; | |
974 | unsigned long flags; | |
975 | unsigned gpio; | |
976 | ||
977 | status = class_register(&gpio_class); | |
978 | if (status < 0) | |
979 | return status; | |
980 | ||
981 | /* Scan and register the gpio_chips which registered very | |
982 | * early (e.g. before the class_register above was called). | |
983 | * | |
984 | * We run before arch_initcall() so chip->dev nodes can have | |
985 | * registered, and so arch_initcall() can always gpio_export(). | |
986 | */ | |
987 | spin_lock_irqsave(&gpio_lock, flags); | |
988 | for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) { | |
989 | struct gpio_chip *chip; | |
990 | ||
991 | chip = gpio_desc[gpio].chip; | |
992 | if (!chip || chip->exported) | |
993 | continue; | |
994 | ||
995 | spin_unlock_irqrestore(&gpio_lock, flags); | |
996 | status = gpiochip_export(chip); | |
997 | spin_lock_irqsave(&gpio_lock, flags); | |
998 | } | |
999 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1000 | ||
1001 | ||
1002 | return status; | |
1003 | } | |
1004 | postcore_initcall(gpiolib_sysfs_init); | |
1005 | ||
1006 | #else | |
1007 | static inline int gpiochip_export(struct gpio_chip *chip) | |
1008 | { | |
1009 | return 0; | |
1010 | } | |
1011 | ||
1012 | static inline void gpiochip_unexport(struct gpio_chip *chip) | |
1013 | { | |
1014 | } | |
1015 | ||
1016 | #endif /* CONFIG_GPIO_SYSFS */ | |
1017 | ||
d2876d08 DB |
1018 | /** |
1019 | * gpiochip_add() - register a gpio_chip | |
1020 | * @chip: the chip to register, with chip->base initialized | |
1021 | * Context: potentially before irqs or kmalloc will work | |
1022 | * | |
1023 | * Returns a negative errno if the chip can't be registered, such as | |
1024 | * because the chip->base is invalid or already associated with a | |
1025 | * different chip. Otherwise it returns zero as a success code. | |
8d0aab2f | 1026 | * |
d8f388d8 DB |
1027 | * When gpiochip_add() is called very early during boot, so that GPIOs |
1028 | * can be freely used, the chip->dev device must be registered before | |
1029 | * the gpio framework's arch_initcall(). Otherwise sysfs initialization | |
1030 | * for GPIOs will fail rudely. | |
1031 | * | |
8d0aab2f AV |
1032 | * If chip->base is negative, this requests dynamic assignment of |
1033 | * a range of valid GPIOs. | |
d2876d08 DB |
1034 | */ |
1035 | int gpiochip_add(struct gpio_chip *chip) | |
1036 | { | |
1037 | unsigned long flags; | |
1038 | int status = 0; | |
1039 | unsigned id; | |
8d0aab2f | 1040 | int base = chip->base; |
d2876d08 | 1041 | |
bff5fda9 | 1042 | if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1)) |
8d0aab2f | 1043 | && base >= 0) { |
d2876d08 DB |
1044 | status = -EINVAL; |
1045 | goto fail; | |
1046 | } | |
1047 | ||
1048 | spin_lock_irqsave(&gpio_lock, flags); | |
1049 | ||
8d0aab2f AV |
1050 | if (base < 0) { |
1051 | base = gpiochip_find_base(chip->ngpio); | |
1052 | if (base < 0) { | |
1053 | status = base; | |
d8f388d8 | 1054 | goto unlock; |
8d0aab2f AV |
1055 | } |
1056 | chip->base = base; | |
1057 | } | |
1058 | ||
d2876d08 | 1059 | /* these GPIO numbers must not be managed by another gpio_chip */ |
8d0aab2f | 1060 | for (id = base; id < base + chip->ngpio; id++) { |
d2876d08 DB |
1061 | if (gpio_desc[id].chip != NULL) { |
1062 | status = -EBUSY; | |
1063 | break; | |
1064 | } | |
1065 | } | |
1066 | if (status == 0) { | |
8d0aab2f | 1067 | for (id = base; id < base + chip->ngpio; id++) { |
d2876d08 | 1068 | gpio_desc[id].chip = chip; |
d8f388d8 DB |
1069 | |
1070 | /* REVISIT: most hardware initializes GPIOs as | |
1071 | * inputs (often with pullups enabled) so power | |
1072 | * usage is minimized. Linux code should set the | |
1073 | * gpio direction first thing; but until it does, | |
1074 | * we may expose the wrong direction in sysfs. | |
1075 | */ | |
1076 | gpio_desc[id].flags = !chip->direction_input | |
1077 | ? (1 << FLAG_IS_OUT) | |
1078 | : 0; | |
d2876d08 DB |
1079 | } |
1080 | } | |
1081 | ||
391c970c AV |
1082 | of_gpiochip_add(chip); |
1083 | ||
d8f388d8 | 1084 | unlock: |
d2876d08 | 1085 | spin_unlock_irqrestore(&gpio_lock, flags); |
cedb1881 AV |
1086 | |
1087 | if (status) | |
1088 | goto fail; | |
1089 | ||
1090 | status = gpiochip_export(chip); | |
1091 | if (status) | |
1092 | goto fail; | |
1093 | ||
64842aad GL |
1094 | pr_info("gpiochip_add: registered GPIOs %d to %d on device: %s\n", |
1095 | chip->base, chip->base + chip->ngpio - 1, | |
1096 | chip->label ? : "generic"); | |
1097 | ||
cedb1881 | 1098 | return 0; |
d2876d08 DB |
1099 | fail: |
1100 | /* failures here can mean systems won't boot... */ | |
cedb1881 AV |
1101 | pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n", |
1102 | chip->base, chip->base + chip->ngpio - 1, | |
1103 | chip->label ? : "generic"); | |
d2876d08 DB |
1104 | return status; |
1105 | } | |
1106 | EXPORT_SYMBOL_GPL(gpiochip_add); | |
1107 | ||
1108 | /** | |
1109 | * gpiochip_remove() - unregister a gpio_chip | |
1110 | * @chip: the chip to unregister | |
1111 | * | |
1112 | * A gpio_chip with any GPIOs still requested may not be removed. | |
1113 | */ | |
1114 | int gpiochip_remove(struct gpio_chip *chip) | |
1115 | { | |
1116 | unsigned long flags; | |
1117 | int status = 0; | |
1118 | unsigned id; | |
1119 | ||
1120 | spin_lock_irqsave(&gpio_lock, flags); | |
1121 | ||
391c970c AV |
1122 | of_gpiochip_remove(chip); |
1123 | ||
d2876d08 DB |
1124 | for (id = chip->base; id < chip->base + chip->ngpio; id++) { |
1125 | if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) { | |
1126 | status = -EBUSY; | |
1127 | break; | |
1128 | } | |
1129 | } | |
1130 | if (status == 0) { | |
1131 | for (id = chip->base; id < chip->base + chip->ngpio; id++) | |
1132 | gpio_desc[id].chip = NULL; | |
1133 | } | |
1134 | ||
1135 | spin_unlock_irqrestore(&gpio_lock, flags); | |
d8f388d8 DB |
1136 | |
1137 | if (status == 0) | |
1138 | gpiochip_unexport(chip); | |
1139 | ||
d2876d08 DB |
1140 | return status; |
1141 | } | |
1142 | EXPORT_SYMBOL_GPL(gpiochip_remove); | |
1143 | ||
594fa265 GL |
1144 | /** |
1145 | * gpiochip_find() - iterator for locating a specific gpio_chip | |
1146 | * @data: data to pass to match function | |
1147 | * @callback: Callback function to check gpio_chip | |
1148 | * | |
1149 | * Similar to bus_find_device. It returns a reference to a gpio_chip as | |
1150 | * determined by a user supplied @match callback. The callback should return | |
1151 | * 0 if the device doesn't match and non-zero if it does. If the callback is | |
1152 | * non-zero, this function will return to the caller and not iterate over any | |
1153 | * more gpio_chips. | |
1154 | */ | |
1155 | struct gpio_chip *gpiochip_find(void *data, | |
1156 | int (*match)(struct gpio_chip *chip, void *data)) | |
1157 | { | |
1158 | struct gpio_chip *chip = NULL; | |
1159 | unsigned long flags; | |
1160 | int i; | |
1161 | ||
1162 | spin_lock_irqsave(&gpio_lock, flags); | |
1163 | for (i = 0; i < ARCH_NR_GPIOS; i++) { | |
1164 | if (!gpio_desc[i].chip) | |
1165 | continue; | |
1166 | ||
1167 | if (match(gpio_desc[i].chip, data)) { | |
1168 | chip = gpio_desc[i].chip; | |
1169 | break; | |
1170 | } | |
1171 | } | |
1172 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1173 | ||
1174 | return chip; | |
1175 | } | |
8fa0c9bf | 1176 | EXPORT_SYMBOL_GPL(gpiochip_find); |
d2876d08 DB |
1177 | |
1178 | /* These "optional" allocation calls help prevent drivers from stomping | |
1179 | * on each other, and help provide better diagnostics in debugfs. | |
1180 | * They're called even less than the "set direction" calls. | |
1181 | */ | |
1182 | int gpio_request(unsigned gpio, const char *label) | |
1183 | { | |
1184 | struct gpio_desc *desc; | |
35e8bb51 | 1185 | struct gpio_chip *chip; |
d2876d08 DB |
1186 | int status = -EINVAL; |
1187 | unsigned long flags; | |
1188 | ||
1189 | spin_lock_irqsave(&gpio_lock, flags); | |
1190 | ||
e6de1808 | 1191 | if (!gpio_is_valid(gpio)) |
d2876d08 DB |
1192 | goto done; |
1193 | desc = &gpio_desc[gpio]; | |
35e8bb51 DB |
1194 | chip = desc->chip; |
1195 | if (chip == NULL) | |
d2876d08 DB |
1196 | goto done; |
1197 | ||
35e8bb51 | 1198 | if (!try_module_get(chip->owner)) |
438d8908 GL |
1199 | goto done; |
1200 | ||
d2876d08 | 1201 | /* NOTE: gpio_request() can be called in early boot, |
35e8bb51 | 1202 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
d2876d08 DB |
1203 | */ |
1204 | ||
1205 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { | |
1206 | desc_set_label(desc, label ? : "?"); | |
1207 | status = 0; | |
438d8908 | 1208 | } else { |
d2876d08 | 1209 | status = -EBUSY; |
35e8bb51 | 1210 | module_put(chip->owner); |
7460db56 | 1211 | goto done; |
35e8bb51 DB |
1212 | } |
1213 | ||
1214 | if (chip->request) { | |
1215 | /* chip->request may sleep */ | |
1216 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1217 | status = chip->request(chip, gpio - chip->base); | |
1218 | spin_lock_irqsave(&gpio_lock, flags); | |
1219 | ||
1220 | if (status < 0) { | |
1221 | desc_set_label(desc, NULL); | |
1222 | module_put(chip->owner); | |
1223 | clear_bit(FLAG_REQUESTED, &desc->flags); | |
1224 | } | |
438d8908 | 1225 | } |
d2876d08 DB |
1226 | |
1227 | done: | |
1228 | if (status) | |
1229 | pr_debug("gpio_request: gpio-%d (%s) status %d\n", | |
1230 | gpio, label ? : "?", status); | |
1231 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1232 | return status; | |
1233 | } | |
1234 | EXPORT_SYMBOL_GPL(gpio_request); | |
1235 | ||
1236 | void gpio_free(unsigned gpio) | |
1237 | { | |
1238 | unsigned long flags; | |
1239 | struct gpio_desc *desc; | |
35e8bb51 | 1240 | struct gpio_chip *chip; |
d2876d08 | 1241 | |
3d599d1c UKK |
1242 | might_sleep(); |
1243 | ||
e6de1808 | 1244 | if (!gpio_is_valid(gpio)) { |
d2876d08 DB |
1245 | WARN_ON(extra_checks); |
1246 | return; | |
1247 | } | |
1248 | ||
d8f388d8 DB |
1249 | gpio_unexport(gpio); |
1250 | ||
d2876d08 DB |
1251 | spin_lock_irqsave(&gpio_lock, flags); |
1252 | ||
1253 | desc = &gpio_desc[gpio]; | |
35e8bb51 DB |
1254 | chip = desc->chip; |
1255 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { | |
1256 | if (chip->free) { | |
1257 | spin_unlock_irqrestore(&gpio_lock, flags); | |
9c4ba946 | 1258 | might_sleep_if(chip->can_sleep); |
35e8bb51 DB |
1259 | chip->free(chip, gpio - chip->base); |
1260 | spin_lock_irqsave(&gpio_lock, flags); | |
1261 | } | |
d2876d08 | 1262 | desc_set_label(desc, NULL); |
438d8908 | 1263 | module_put(desc->chip->owner); |
07697461 | 1264 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
35e8bb51 | 1265 | clear_bit(FLAG_REQUESTED, &desc->flags); |
438d8908 | 1266 | } else |
d2876d08 DB |
1267 | WARN_ON(extra_checks); |
1268 | ||
1269 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1270 | } | |
1271 | EXPORT_SYMBOL_GPL(gpio_free); | |
1272 | ||
3e45f1d1 EM |
1273 | /** |
1274 | * gpio_request_one - request a single GPIO with initial configuration | |
1275 | * @gpio: the GPIO number | |
1276 | * @flags: GPIO configuration as specified by GPIOF_* | |
1277 | * @label: a literal description string of this GPIO | |
1278 | */ | |
1279 | int gpio_request_one(unsigned gpio, unsigned long flags, const char *label) | |
1280 | { | |
1281 | int err; | |
1282 | ||
1283 | err = gpio_request(gpio, label); | |
1284 | if (err) | |
1285 | return err; | |
1286 | ||
1287 | if (flags & GPIOF_DIR_IN) | |
1288 | err = gpio_direction_input(gpio); | |
1289 | else | |
1290 | err = gpio_direction_output(gpio, | |
1291 | (flags & GPIOF_INIT_HIGH) ? 1 : 0); | |
1292 | ||
e254811c AK |
1293 | if (err) |
1294 | gpio_free(gpio); | |
1295 | ||
3e45f1d1 EM |
1296 | return err; |
1297 | } | |
1298 | EXPORT_SYMBOL_GPL(gpio_request_one); | |
1299 | ||
1300 | /** | |
1301 | * gpio_request_array - request multiple GPIOs in a single call | |
1302 | * @array: array of the 'struct gpio' | |
1303 | * @num: how many GPIOs in the array | |
1304 | */ | |
7c295975 | 1305 | int gpio_request_array(const struct gpio *array, size_t num) |
3e45f1d1 EM |
1306 | { |
1307 | int i, err; | |
1308 | ||
1309 | for (i = 0; i < num; i++, array++) { | |
1310 | err = gpio_request_one(array->gpio, array->flags, array->label); | |
1311 | if (err) | |
1312 | goto err_free; | |
1313 | } | |
1314 | return 0; | |
1315 | ||
1316 | err_free: | |
1317 | while (i--) | |
1318 | gpio_free((--array)->gpio); | |
1319 | return err; | |
1320 | } | |
1321 | EXPORT_SYMBOL_GPL(gpio_request_array); | |
1322 | ||
1323 | /** | |
1324 | * gpio_free_array - release multiple GPIOs in a single call | |
1325 | * @array: array of the 'struct gpio' | |
1326 | * @num: how many GPIOs in the array | |
1327 | */ | |
7c295975 | 1328 | void gpio_free_array(const struct gpio *array, size_t num) |
3e45f1d1 EM |
1329 | { |
1330 | while (num--) | |
1331 | gpio_free((array++)->gpio); | |
1332 | } | |
1333 | EXPORT_SYMBOL_GPL(gpio_free_array); | |
d2876d08 DB |
1334 | |
1335 | /** | |
1336 | * gpiochip_is_requested - return string iff signal was requested | |
1337 | * @chip: controller managing the signal | |
1338 | * @offset: of signal within controller's 0..(ngpio - 1) range | |
1339 | * | |
1340 | * Returns NULL if the GPIO is not currently requested, else a string. | |
1341 | * If debugfs support is enabled, the string returned is the label passed | |
1342 | * to gpio_request(); otherwise it is a meaningless constant. | |
1343 | * | |
1344 | * This function is for use by GPIO controller drivers. The label can | |
1345 | * help with diagnostics, and knowing that the signal is used as a GPIO | |
1346 | * can help avoid accidentally multiplexing it to another controller. | |
1347 | */ | |
1348 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) | |
1349 | { | |
1350 | unsigned gpio = chip->base + offset; | |
1351 | ||
e6de1808 | 1352 | if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip) |
d2876d08 DB |
1353 | return NULL; |
1354 | if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0) | |
1355 | return NULL; | |
1356 | #ifdef CONFIG_DEBUG_FS | |
1357 | return gpio_desc[gpio].label; | |
1358 | #else | |
1359 | return "?"; | |
1360 | #endif | |
1361 | } | |
1362 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); | |
1363 | ||
1364 | ||
1365 | /* Drivers MUST set GPIO direction before making get/set calls. In | |
1366 | * some cases this is done in early boot, before IRQs are enabled. | |
1367 | * | |
1368 | * As a rule these aren't called more than once (except for drivers | |
1369 | * using the open-drain emulation idiom) so these are natural places | |
1370 | * to accumulate extra debugging checks. Note that we can't (yet) | |
1371 | * rely on gpio_request() having been called beforehand. | |
1372 | */ | |
1373 | ||
1374 | int gpio_direction_input(unsigned gpio) | |
1375 | { | |
1376 | unsigned long flags; | |
1377 | struct gpio_chip *chip; | |
1378 | struct gpio_desc *desc = &gpio_desc[gpio]; | |
1379 | int status = -EINVAL; | |
1380 | ||
1381 | spin_lock_irqsave(&gpio_lock, flags); | |
1382 | ||
e6de1808 | 1383 | if (!gpio_is_valid(gpio)) |
d2876d08 DB |
1384 | goto fail; |
1385 | chip = desc->chip; | |
1386 | if (!chip || !chip->get || !chip->direction_input) | |
1387 | goto fail; | |
1388 | gpio -= chip->base; | |
1389 | if (gpio >= chip->ngpio) | |
1390 | goto fail; | |
35e8bb51 DB |
1391 | status = gpio_ensure_requested(desc, gpio); |
1392 | if (status < 0) | |
1393 | goto fail; | |
d2876d08 DB |
1394 | |
1395 | /* now we know the gpio is valid and chip won't vanish */ | |
1396 | ||
1397 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1398 | ||
9c4ba946 | 1399 | might_sleep_if(chip->can_sleep); |
d2876d08 | 1400 | |
35e8bb51 DB |
1401 | if (status) { |
1402 | status = chip->request(chip, gpio); | |
1403 | if (status < 0) { | |
1404 | pr_debug("GPIO-%d: chip request fail, %d\n", | |
1405 | chip->base + gpio, status); | |
1406 | /* and it's not available to anyone else ... | |
1407 | * gpio_request() is the fully clean solution. | |
1408 | */ | |
1409 | goto lose; | |
1410 | } | |
1411 | } | |
1412 | ||
d2876d08 DB |
1413 | status = chip->direction_input(chip, gpio); |
1414 | if (status == 0) | |
1415 | clear_bit(FLAG_IS_OUT, &desc->flags); | |
3f397c21 UKK |
1416 | |
1417 | trace_gpio_direction(chip->base + gpio, 1, status); | |
35e8bb51 | 1418 | lose: |
d2876d08 DB |
1419 | return status; |
1420 | fail: | |
1421 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1422 | if (status) | |
1423 | pr_debug("%s: gpio-%d status %d\n", | |
145980a0 | 1424 | __func__, gpio, status); |
d2876d08 DB |
1425 | return status; |
1426 | } | |
1427 | EXPORT_SYMBOL_GPL(gpio_direction_input); | |
1428 | ||
1429 | int gpio_direction_output(unsigned gpio, int value) | |
1430 | { | |
1431 | unsigned long flags; | |
1432 | struct gpio_chip *chip; | |
1433 | struct gpio_desc *desc = &gpio_desc[gpio]; | |
1434 | int status = -EINVAL; | |
1435 | ||
1436 | spin_lock_irqsave(&gpio_lock, flags); | |
1437 | ||
e6de1808 | 1438 | if (!gpio_is_valid(gpio)) |
d2876d08 DB |
1439 | goto fail; |
1440 | chip = desc->chip; | |
1441 | if (!chip || !chip->set || !chip->direction_output) | |
1442 | goto fail; | |
1443 | gpio -= chip->base; | |
1444 | if (gpio >= chip->ngpio) | |
1445 | goto fail; | |
35e8bb51 DB |
1446 | status = gpio_ensure_requested(desc, gpio); |
1447 | if (status < 0) | |
1448 | goto fail; | |
d2876d08 DB |
1449 | |
1450 | /* now we know the gpio is valid and chip won't vanish */ | |
1451 | ||
1452 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1453 | ||
9c4ba946 | 1454 | might_sleep_if(chip->can_sleep); |
d2876d08 | 1455 | |
35e8bb51 DB |
1456 | if (status) { |
1457 | status = chip->request(chip, gpio); | |
1458 | if (status < 0) { | |
1459 | pr_debug("GPIO-%d: chip request fail, %d\n", | |
1460 | chip->base + gpio, status); | |
1461 | /* and it's not available to anyone else ... | |
1462 | * gpio_request() is the fully clean solution. | |
1463 | */ | |
1464 | goto lose; | |
1465 | } | |
1466 | } | |
1467 | ||
d2876d08 DB |
1468 | status = chip->direction_output(chip, gpio, value); |
1469 | if (status == 0) | |
1470 | set_bit(FLAG_IS_OUT, &desc->flags); | |
3f397c21 UKK |
1471 | trace_gpio_value(chip->base + gpio, 0, value); |
1472 | trace_gpio_direction(chip->base + gpio, 0, status); | |
35e8bb51 | 1473 | lose: |
d2876d08 DB |
1474 | return status; |
1475 | fail: | |
1476 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1477 | if (status) | |
1478 | pr_debug("%s: gpio-%d status %d\n", | |
145980a0 | 1479 | __func__, gpio, status); |
d2876d08 DB |
1480 | return status; |
1481 | } | |
1482 | EXPORT_SYMBOL_GPL(gpio_direction_output); | |
1483 | ||
c4b5be98 FB |
1484 | /** |
1485 | * gpio_set_debounce - sets @debounce time for a @gpio | |
1486 | * @gpio: the gpio to set debounce time | |
1487 | * @debounce: debounce time is microseconds | |
1488 | */ | |
1489 | int gpio_set_debounce(unsigned gpio, unsigned debounce) | |
1490 | { | |
1491 | unsigned long flags; | |
1492 | struct gpio_chip *chip; | |
1493 | struct gpio_desc *desc = &gpio_desc[gpio]; | |
1494 | int status = -EINVAL; | |
1495 | ||
1496 | spin_lock_irqsave(&gpio_lock, flags); | |
1497 | ||
1498 | if (!gpio_is_valid(gpio)) | |
1499 | goto fail; | |
1500 | chip = desc->chip; | |
1501 | if (!chip || !chip->set || !chip->set_debounce) | |
1502 | goto fail; | |
1503 | gpio -= chip->base; | |
1504 | if (gpio >= chip->ngpio) | |
1505 | goto fail; | |
1506 | status = gpio_ensure_requested(desc, gpio); | |
1507 | if (status < 0) | |
1508 | goto fail; | |
1509 | ||
1510 | /* now we know the gpio is valid and chip won't vanish */ | |
1511 | ||
1512 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1513 | ||
9c4ba946 | 1514 | might_sleep_if(chip->can_sleep); |
c4b5be98 FB |
1515 | |
1516 | return chip->set_debounce(chip, gpio, debounce); | |
1517 | ||
1518 | fail: | |
1519 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1520 | if (status) | |
1521 | pr_debug("%s: gpio-%d status %d\n", | |
1522 | __func__, gpio, status); | |
1523 | ||
1524 | return status; | |
1525 | } | |
1526 | EXPORT_SYMBOL_GPL(gpio_set_debounce); | |
d2876d08 DB |
1527 | |
1528 | /* I/O calls are only valid after configuration completed; the relevant | |
1529 | * "is this a valid GPIO" error checks should already have been done. | |
1530 | * | |
1531 | * "Get" operations are often inlinable as reading a pin value register, | |
1532 | * and masking the relevant bit in that register. | |
1533 | * | |
1534 | * When "set" operations are inlinable, they involve writing that mask to | |
1535 | * one register to set a low value, or a different register to set it high. | |
1536 | * Otherwise locking is needed, so there may be little value to inlining. | |
1537 | * | |
1538 | *------------------------------------------------------------------------ | |
1539 | * | |
1540 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers | |
1541 | * have requested the GPIO. That can include implicit requesting by | |
1542 | * a direction setting call. Marking a gpio as requested locks its chip | |
1543 | * in memory, guaranteeing that these table lookups need no more locking | |
1544 | * and that gpiochip_remove() will fail. | |
1545 | * | |
1546 | * REVISIT when debugging, consider adding some instrumentation to ensure | |
1547 | * that the GPIO was actually requested. | |
1548 | */ | |
1549 | ||
1550 | /** | |
1551 | * __gpio_get_value() - return a gpio's value | |
1552 | * @gpio: gpio whose value will be returned | |
1553 | * Context: any | |
1554 | * | |
1555 | * This is used directly or indirectly to implement gpio_get_value(). | |
1556 | * It returns the zero or nonzero value provided by the associated | |
1557 | * gpio_chip.get() method; or zero if no such method is provided. | |
1558 | */ | |
1559 | int __gpio_get_value(unsigned gpio) | |
1560 | { | |
1561 | struct gpio_chip *chip; | |
3f397c21 | 1562 | int value; |
d2876d08 DB |
1563 | |
1564 | chip = gpio_to_chip(gpio); | |
9c4ba946 | 1565 | WARN_ON(chip->can_sleep); |
3f397c21 UKK |
1566 | value = chip->get ? chip->get(chip, gpio - chip->base) : 0; |
1567 | trace_gpio_value(gpio, 1, value); | |
1568 | return value; | |
d2876d08 DB |
1569 | } |
1570 | EXPORT_SYMBOL_GPL(__gpio_get_value); | |
1571 | ||
1572 | /** | |
1573 | * __gpio_set_value() - assign a gpio's value | |
1574 | * @gpio: gpio whose value will be assigned | |
1575 | * @value: value to assign | |
1576 | * Context: any | |
1577 | * | |
1578 | * This is used directly or indirectly to implement gpio_set_value(). | |
1579 | * It invokes the associated gpio_chip.set() method. | |
1580 | */ | |
1581 | void __gpio_set_value(unsigned gpio, int value) | |
1582 | { | |
1583 | struct gpio_chip *chip; | |
1584 | ||
1585 | chip = gpio_to_chip(gpio); | |
9c4ba946 | 1586 | WARN_ON(chip->can_sleep); |
3f397c21 | 1587 | trace_gpio_value(gpio, 0, value); |
d2876d08 DB |
1588 | chip->set(chip, gpio - chip->base, value); |
1589 | } | |
1590 | EXPORT_SYMBOL_GPL(__gpio_set_value); | |
1591 | ||
1592 | /** | |
1593 | * __gpio_cansleep() - report whether gpio value access will sleep | |
1594 | * @gpio: gpio in question | |
1595 | * Context: any | |
1596 | * | |
1597 | * This is used directly or indirectly to implement gpio_cansleep(). It | |
1598 | * returns nonzero if access reading or writing the GPIO value can sleep. | |
1599 | */ | |
1600 | int __gpio_cansleep(unsigned gpio) | |
1601 | { | |
1602 | struct gpio_chip *chip; | |
1603 | ||
1604 | /* only call this on GPIOs that are valid! */ | |
1605 | chip = gpio_to_chip(gpio); | |
1606 | ||
1607 | return chip->can_sleep; | |
1608 | } | |
1609 | EXPORT_SYMBOL_GPL(__gpio_cansleep); | |
1610 | ||
0f6d504e DB |
1611 | /** |
1612 | * __gpio_to_irq() - return the IRQ corresponding to a GPIO | |
1613 | * @gpio: gpio whose IRQ will be returned (already requested) | |
1614 | * Context: any | |
1615 | * | |
1616 | * This is used directly or indirectly to implement gpio_to_irq(). | |
1617 | * It returns the number of the IRQ signaled by this (input) GPIO, | |
1618 | * or a negative errno. | |
1619 | */ | |
1620 | int __gpio_to_irq(unsigned gpio) | |
1621 | { | |
1622 | struct gpio_chip *chip; | |
1623 | ||
1624 | chip = gpio_to_chip(gpio); | |
1625 | return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO; | |
1626 | } | |
1627 | EXPORT_SYMBOL_GPL(__gpio_to_irq); | |
1628 | ||
d2876d08 DB |
1629 | |
1630 | ||
1631 | /* There's no value in making it easy to inline GPIO calls that may sleep. | |
1632 | * Common examples include ones connected to I2C or SPI chips. | |
1633 | */ | |
1634 | ||
1635 | int gpio_get_value_cansleep(unsigned gpio) | |
1636 | { | |
1637 | struct gpio_chip *chip; | |
3f397c21 | 1638 | int value; |
d2876d08 DB |
1639 | |
1640 | might_sleep_if(extra_checks); | |
1641 | chip = gpio_to_chip(gpio); | |
3f397c21 UKK |
1642 | value = chip->get ? chip->get(chip, gpio - chip->base) : 0; |
1643 | trace_gpio_value(gpio, 1, value); | |
1644 | return value; | |
d2876d08 DB |
1645 | } |
1646 | EXPORT_SYMBOL_GPL(gpio_get_value_cansleep); | |
1647 | ||
1648 | void gpio_set_value_cansleep(unsigned gpio, int value) | |
1649 | { | |
1650 | struct gpio_chip *chip; | |
1651 | ||
1652 | might_sleep_if(extra_checks); | |
1653 | chip = gpio_to_chip(gpio); | |
3f397c21 | 1654 | trace_gpio_value(gpio, 0, value); |
d2876d08 DB |
1655 | chip->set(chip, gpio - chip->base, value); |
1656 | } | |
1657 | EXPORT_SYMBOL_GPL(gpio_set_value_cansleep); | |
1658 | ||
1659 | ||
1660 | #ifdef CONFIG_DEBUG_FS | |
1661 | ||
d2876d08 DB |
1662 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
1663 | { | |
1664 | unsigned i; | |
1665 | unsigned gpio = chip->base; | |
1666 | struct gpio_desc *gdesc = &gpio_desc[gpio]; | |
1667 | int is_out; | |
1668 | ||
1669 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { | |
1670 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) | |
1671 | continue; | |
1672 | ||
1673 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); | |
6e8ba729 | 1674 | seq_printf(s, " gpio-%-3d (%-20.20s) %s %s", |
d2876d08 DB |
1675 | gpio, gdesc->label, |
1676 | is_out ? "out" : "in ", | |
1677 | chip->get | |
1678 | ? (chip->get(chip, i) ? "hi" : "lo") | |
1679 | : "? "); | |
d2876d08 DB |
1680 | seq_printf(s, "\n"); |
1681 | } | |
1682 | } | |
1683 | ||
1684 | static int gpiolib_show(struct seq_file *s, void *unused) | |
1685 | { | |
1686 | struct gpio_chip *chip = NULL; | |
1687 | unsigned gpio; | |
1688 | int started = 0; | |
1689 | ||
1690 | /* REVISIT this isn't locked against gpio_chip removal ... */ | |
1691 | ||
e6de1808 | 1692 | for (gpio = 0; gpio_is_valid(gpio); gpio++) { |
d8f388d8 DB |
1693 | struct device *dev; |
1694 | ||
d2876d08 DB |
1695 | if (chip == gpio_desc[gpio].chip) |
1696 | continue; | |
1697 | chip = gpio_desc[gpio].chip; | |
1698 | if (!chip) | |
1699 | continue; | |
1700 | ||
d8f388d8 | 1701 | seq_printf(s, "%sGPIOs %d-%d", |
d2876d08 | 1702 | started ? "\n" : "", |
d8f388d8 DB |
1703 | chip->base, chip->base + chip->ngpio - 1); |
1704 | dev = chip->dev; | |
1705 | if (dev) | |
1706 | seq_printf(s, ", %s/%s", | |
1707 | dev->bus ? dev->bus->name : "no-bus", | |
14ab309d | 1708 | dev_name(dev)); |
d8f388d8 DB |
1709 | if (chip->label) |
1710 | seq_printf(s, ", %s", chip->label); | |
1711 | if (chip->can_sleep) | |
1712 | seq_printf(s, ", can sleep"); | |
1713 | seq_printf(s, ":\n"); | |
1714 | ||
d2876d08 DB |
1715 | started = 1; |
1716 | if (chip->dbg_show) | |
1717 | chip->dbg_show(s, chip); | |
1718 | else | |
1719 | gpiolib_dbg_show(s, chip); | |
1720 | } | |
1721 | return 0; | |
1722 | } | |
1723 | ||
1724 | static int gpiolib_open(struct inode *inode, struct file *file) | |
1725 | { | |
1726 | return single_open(file, gpiolib_show, NULL); | |
1727 | } | |
1728 | ||
828c0950 | 1729 | static const struct file_operations gpiolib_operations = { |
d2876d08 DB |
1730 | .open = gpiolib_open, |
1731 | .read = seq_read, | |
1732 | .llseek = seq_lseek, | |
1733 | .release = single_release, | |
1734 | }; | |
1735 | ||
1736 | static int __init gpiolib_debugfs_init(void) | |
1737 | { | |
1738 | /* /sys/kernel/debug/gpio */ | |
1739 | (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, | |
1740 | NULL, NULL, &gpiolib_operations); | |
1741 | return 0; | |
1742 | } | |
1743 | subsys_initcall(gpiolib_debugfs_init); | |
1744 | ||
1745 | #endif /* DEBUG_FS */ |