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