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