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