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