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