GPIO: gpiolib: correct description of gpiod_direction_output
[linux-block.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>
7b199811 15#include <linux/acpi.h>
53e7cac3 16#include <linux/gpio/driver.h>
d2876d08 17
664e3e5a
MW
18#include "gpiolib.h"
19
3f397c21
UKK
20#define CREATE_TRACE_POINTS
21#include <trace/events/gpio.h>
d2876d08 22
79a9becd 23/* Implementation infrastructure for GPIO interfaces.
d2876d08 24 *
79a9becd
AC
25 * The GPIO programming interface allows for inlining speed-critical
26 * get/set operations for common cases, so that access to SOC-integrated
27 * GPIOs can sometimes cost only an instruction or two per bit.
d2876d08
DB
28 */
29
30
31/* When debugging, extend minimal trust to callers and platform code.
32 * Also emit diagnostic messages that may help initial bringup, when
33 * board setup or driver bugs are most common.
34 *
35 * Otherwise, minimize overhead in what may be bitbanging codepaths.
36 */
37#ifdef DEBUG
38#define extra_checks 1
39#else
40#define extra_checks 0
41#endif
42
43/* gpio_lock prevents conflicts during gpio_desc[] table updates.
44 * While any GPIO is requested, its gpio_chip is not removable;
45 * each GPIO's "requested" flag serves as a lock and refcount.
46 */
47static DEFINE_SPINLOCK(gpio_lock);
48
49struct gpio_desc {
50 struct gpio_chip *chip;
51 unsigned long flags;
52/* flag symbols are bit numbers */
53#define FLAG_REQUESTED 0
54#define FLAG_IS_OUT 1
710b40ea
AC
55#define FLAG_EXPORT 2 /* protected by sysfs_lock */
56#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
57#define FLAG_TRIG_FALL 4 /* trigger on falling edge */
58#define FLAG_TRIG_RISE 5 /* trigger on rising edge */
79a9becd 59#define FLAG_ACTIVE_LOW 6 /* value has active low */
710b40ea
AC
60#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
61#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
d468bf9e 62#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
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
bae48da2
AC
77static DEFINE_MUTEX(gpio_lookup_lock);
78static LIST_HEAD(gpio_lookup_list);
1a989d0f
AC
79static LIST_HEAD(gpio_chips);
80
ff77c352 81#ifdef CONFIG_GPIO_SYSFS
5ba1821d 82static DEFINE_IDR(dirent_idr);
ff77c352
DG
83#endif
84
372e722e
AC
85static int gpiod_request(struct gpio_desc *desc, const char *label);
86static void gpiod_free(struct gpio_desc *desc);
372e722e 87
1a2a99c6
AS
88/* With descriptor prefix */
89
7b17b59f 90#ifdef CONFIG_DEBUG_FS
7589e59f
AS
91#define gpiod_emerg(desc, fmt, ...) \
92 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
7b17b59f 93 ##__VA_ARGS__)
7589e59f
AS
94#define gpiod_crit(desc, fmt, ...) \
95 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
7b17b59f 96 ##__VA_ARGS__)
7589e59f
AS
97#define gpiod_err(desc, fmt, ...) \
98 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
7b17b59f 99 ##__VA_ARGS__)
7589e59f
AS
100#define gpiod_warn(desc, fmt, ...) \
101 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
7b17b59f 102 ##__VA_ARGS__)
7589e59f
AS
103#define gpiod_info(desc, fmt, ...) \
104 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
7b17b59f 105 ##__VA_ARGS__)
7589e59f
AS
106#define gpiod_dbg(desc, fmt, ...) \
107 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
7b17b59f
MB
108 ##__VA_ARGS__)
109#else
7589e59f 110#define gpiod_emerg(desc, fmt, ...) \
6424de5a 111 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
7589e59f 112#define gpiod_crit(desc, fmt, ...) \
6424de5a 113 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
7589e59f 114#define gpiod_err(desc, fmt, ...) \
6424de5a 115 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
7589e59f 116#define gpiod_warn(desc, fmt, ...) \
6424de5a 117 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
7589e59f 118#define gpiod_info(desc, fmt, ...) \
6424de5a 119 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
7589e59f 120#define gpiod_dbg(desc, fmt, ...) \
6424de5a 121 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
7b17b59f 122#endif
372e722e 123
1a2a99c6
AS
124/* With chip prefix */
125
126#define chip_emerg(chip, fmt, ...) \
127 pr_emerg("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
128#define chip_crit(chip, fmt, ...) \
129 pr_crit("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
130#define chip_err(chip, fmt, ...) \
131 pr_err("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
132#define chip_warn(chip, fmt, ...) \
133 pr_warn("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
134#define chip_info(chip, fmt, ...) \
135 pr_info("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
136#define chip_dbg(chip, fmt, ...) \
137 pr_debug("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
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 */
79a9becd 157struct gpio_desc *gpio_to_desc(unsigned gpio)
372e722e
AC
158{
159 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
160 return NULL;
161 else
162 return &gpio_desc[gpio];
163}
79a9becd 164EXPORT_SYMBOL_GPL(gpio_to_desc);
372e722e 165
d468bf9e
LW
166/**
167 * Convert an offset on a certain chip to a corresponding descriptor
168 */
169static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip,
170 unsigned int offset)
171{
b7d0a28a
AC
172 if (offset >= chip->ngpio)
173 return ERR_PTR(-EINVAL);
d468bf9e 174
b7d0a28a 175 return &chip->desc[offset];
d468bf9e 176}
372e722e
AC
177
178/**
179 * Convert a GPIO descriptor to the integer namespace.
180 * This should disappear in the future but is needed since we still
181 * use GPIO numbers for error messages and sysfs nodes
182 */
79a9becd 183int desc_to_gpio(const struct gpio_desc *desc)
372e722e 184{
8c0fca81 185 return desc - &gpio_desc[0];
372e722e 186}
79a9becd 187EXPORT_SYMBOL_GPL(desc_to_gpio);
372e722e
AC
188
189
d2876d08
DB
190/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
191 * when setting direction, and otherwise illegal. Until board setup code
192 * and drivers use explicit requests everywhere (which won't happen when
193 * those calls have no teeth) we can't avoid autorequesting. This nag
35e8bb51
DB
194 * message should motivate switching to explicit requests... so should
195 * the weaker cleanup after faults, compared to gpio_request().
8a0cecff
DB
196 *
197 * NOTE: the autorequest mechanism is going away; at this point it's
198 * only "legal" in the sense that (old) code using it won't break yet,
199 * but instead only triggers a WARN() stack dump.
d2876d08 200 */
372e722e 201static int gpio_ensure_requested(struct gpio_desc *desc)
d2876d08 202{
8a0cecff 203 const struct gpio_chip *chip = desc->chip;
372e722e 204 const int gpio = desc_to_gpio(desc);
35e8bb51 205
8a0cecff
DB
206 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
207 "autorequest GPIO-%d\n", gpio)) {
35e8bb51 208 if (!try_module_get(chip->owner)) {
7589e59f
AS
209 gpiod_err(desc, "%s: module can't be gotten\n",
210 __func__);
35e8bb51
DB
211 clear_bit(FLAG_REQUESTED, &desc->flags);
212 /* lose */
213 return -EIO;
214 }
d2876d08 215 desc_set_label(desc, "[auto]");
35e8bb51
DB
216 /* caller must chip->request() w/o spinlock */
217 if (chip->request)
218 return 1;
d2876d08 219 }
35e8bb51 220 return 0;
d2876d08
DB
221}
222
79a9becd
AC
223/**
224 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
225 * @desc: descriptor to return the chip of
226 */
227struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
372e722e 228{
bcabdef1 229 return desc ? desc->chip : NULL;
372e722e 230}
79a9becd 231EXPORT_SYMBOL_GPL(gpiod_to_chip);
d2876d08 232
8d0aab2f
AV
233/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
234static int gpiochip_find_base(int ngpio)
235{
83cabe33
AC
236 struct gpio_chip *chip;
237 int base = ARCH_NR_GPIOS - ngpio;
8d0aab2f 238
83cabe33
AC
239 list_for_each_entry_reverse(chip, &gpio_chips, list) {
240 /* found a free space? */
241 if (chip->base + chip->ngpio <= base)
242 break;
243 else
244 /* nope, check the space right before the chip */
245 base = chip->base - ngpio;
8d0aab2f
AV
246 }
247
83cabe33 248 if (gpio_is_valid(base)) {
8d0aab2f 249 pr_debug("%s: found new base at %d\n", __func__, base);
83cabe33
AC
250 return base;
251 } else {
252 pr_err("%s: cannot find free range\n", __func__);
253 return -ENOSPC;
169b6a7a 254 }
169b6a7a
AV
255}
256
79a9becd
AC
257/**
258 * gpiod_get_direction - return the current direction of a GPIO
259 * @desc: GPIO to get the direction of
260 *
261 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
262 *
263 * This function may sleep if gpiod_cansleep() is true.
264 */
265int gpiod_get_direction(const struct gpio_desc *desc)
80b0a602
MN
266{
267 struct gpio_chip *chip;
372e722e 268 unsigned offset;
80b0a602
MN
269 int status = -EINVAL;
270
372e722e
AC
271 chip = gpiod_to_chip(desc);
272 offset = gpio_chip_hwgpio(desc);
80b0a602
MN
273
274 if (!chip->get_direction)
275 return status;
276
372e722e 277 status = chip->get_direction(chip, offset);
80b0a602
MN
278 if (status > 0) {
279 /* GPIOF_DIR_IN, or other positive */
280 status = 1;
def63433
AC
281 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
282 * so it does not affect constness per se */
283 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
80b0a602
MN
284 }
285 if (status == 0) {
286 /* GPIOF_DIR_OUT */
def63433 287 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
80b0a602
MN
288 }
289 return status;
290}
79a9becd 291EXPORT_SYMBOL_GPL(gpiod_get_direction);
80b0a602 292
d8f388d8
DB
293#ifdef CONFIG_GPIO_SYSFS
294
295/* lock protects against unexport_gpio() being called while
296 * sysfs files are active.
297 */
298static DEFINE_MUTEX(sysfs_lock);
299
300/*
301 * /sys/class/gpio/gpioN... only for GPIOs that are exported
302 * /direction
303 * * MAY BE OMITTED if kernel won't allow direction changes
304 * * is read/write as "in" or "out"
305 * * may also be written as "high" or "low", initializing
306 * output value as specified ("out" implies "low")
307 * /value
308 * * always readable, subject to hardware behavior
309 * * may be writable, as zero/nonzero
ff77c352
DG
310 * /edge
311 * * configures behavior of poll(2) on /value
312 * * available only if pin can generate IRQs on input
313 * * is read/write as "none", "falling", "rising", or "both"
07697461
JN
314 * /active_low
315 * * configures polarity of /value
316 * * is read/write as zero/nonzero
317 * * also affects existing and subsequent "falling" and "rising"
318 * /edge configuration
d8f388d8
DB
319 */
320
321static ssize_t gpio_direction_show(struct device *dev,
322 struct device_attribute *attr, char *buf)
323{
def63433 324 const struct gpio_desc *desc = dev_get_drvdata(dev);
d8f388d8
DB
325 ssize_t status;
326
327 mutex_lock(&sysfs_lock);
328
476171ce 329 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
d8f388d8 330 status = -EIO;
476171ce 331 } else {
372e722e 332 gpiod_get_direction(desc);
d8f388d8
DB
333 status = sprintf(buf, "%s\n",
334 test_bit(FLAG_IS_OUT, &desc->flags)
335 ? "out" : "in");
476171ce 336 }
d8f388d8
DB
337
338 mutex_unlock(&sysfs_lock);
339 return status;
340}
341
342static ssize_t gpio_direction_store(struct device *dev,
343 struct device_attribute *attr, const char *buf, size_t size)
344{
372e722e 345 struct gpio_desc *desc = dev_get_drvdata(dev);
d8f388d8
DB
346 ssize_t status;
347
348 mutex_lock(&sysfs_lock);
349
350 if (!test_bit(FLAG_EXPORT, &desc->flags))
351 status = -EIO;
352 else if (sysfs_streq(buf, "high"))
ef70bbe1 353 status = gpiod_direction_output_raw(desc, 1);
d8f388d8 354 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
ef70bbe1 355 status = gpiod_direction_output_raw(desc, 0);
d8f388d8 356 else if (sysfs_streq(buf, "in"))
372e722e 357 status = gpiod_direction_input(desc);
d8f388d8
DB
358 else
359 status = -EINVAL;
360
361 mutex_unlock(&sysfs_lock);
362 return status ? : size;
363}
364
07697461 365static /* const */ DEVICE_ATTR(direction, 0644,
d8f388d8
DB
366 gpio_direction_show, gpio_direction_store);
367
368static ssize_t gpio_value_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
370{
372e722e 371 struct gpio_desc *desc = dev_get_drvdata(dev);
d8f388d8
DB
372 ssize_t status;
373
374 mutex_lock(&sysfs_lock);
375
79a9becd 376 if (!test_bit(FLAG_EXPORT, &desc->flags))
d8f388d8 377 status = -EIO;
79a9becd
AC
378 else
379 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
d8f388d8
DB
380
381 mutex_unlock(&sysfs_lock);
382 return status;
383}
384
385static ssize_t gpio_value_store(struct device *dev,
386 struct device_attribute *attr, const char *buf, size_t size)
387{
372e722e 388 struct gpio_desc *desc = dev_get_drvdata(dev);
d8f388d8
DB
389 ssize_t status;
390
391 mutex_lock(&sysfs_lock);
392
393 if (!test_bit(FLAG_EXPORT, &desc->flags))
394 status = -EIO;
395 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
396 status = -EPERM;
397 else {
398 long value;
399
a3d88c92 400 status = kstrtol(buf, 0, &value);
d8f388d8 401 if (status == 0) {
79a9becd 402 gpiod_set_value_cansleep(desc, value);
d8f388d8
DB
403 status = size;
404 }
405 }
406
407 mutex_unlock(&sysfs_lock);
408 return status;
409}
410
07697461 411static const DEVICE_ATTR(value, 0644,
d8f388d8
DB
412 gpio_value_show, gpio_value_store);
413
ff77c352
DG
414static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
415{
324a56e1 416 struct kernfs_node *value_sd = priv;
ff77c352 417
5ba1821d 418 sysfs_notify_dirent(value_sd);
ff77c352
DG
419 return IRQ_HANDLED;
420}
421
ff77c352
DG
422static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
423 unsigned long gpio_flags)
424{
324a56e1 425 struct kernfs_node *value_sd;
ff77c352
DG
426 unsigned long irq_flags;
427 int ret, irq, id;
428
429 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
430 return 0;
431
372e722e 432 irq = gpiod_to_irq(desc);
ff77c352
DG
433 if (irq < 0)
434 return -EIO;
435
5ba1821d
DG
436 id = desc->flags >> ID_SHIFT;
437 value_sd = idr_find(&dirent_idr, id);
438 if (value_sd)
439 free_irq(irq, value_sd);
ff77c352
DG
440
441 desc->flags &= ~GPIO_TRIGGER_MASK;
442
443 if (!gpio_flags) {
d468bf9e 444 gpiod_unlock_as_irq(desc);
ff77c352 445 ret = 0;
5ba1821d 446 goto free_id;
ff77c352
DG
447 }
448
449 irq_flags = IRQF_SHARED;
450 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
07697461
JN
451 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
452 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
ff77c352 453 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
07697461
JN
454 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
455 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
ff77c352 456
5ba1821d 457 if (!value_sd) {
388975cc 458 value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
5ba1821d
DG
459 if (!value_sd) {
460 ret = -ENODEV;
ff77c352
DG
461 goto err_out;
462 }
463
62f516b8
TH
464 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
465 if (ret < 0)
5ba1821d 466 goto free_sd;
62f516b8 467 id = ret;
ff77c352
DG
468
469 desc->flags &= GPIO_FLAGS_MASK;
5ba1821d 470 desc->flags |= (unsigned long)id << ID_SHIFT;
ff77c352 471
5ba1821d 472 if (desc->flags >> ID_SHIFT != id) {
ff77c352
DG
473 ret = -ERANGE;
474 goto free_id;
475 }
ff77c352
DG
476 }
477
364fadb3 478 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
5ba1821d 479 "gpiolib", value_sd);
364fadb3 480 if (ret < 0)
5ba1821d 481 goto free_id;
ff77c352 482
d468bf9e
LW
483 ret = gpiod_lock_as_irq(desc);
484 if (ret < 0) {
485 gpiod_warn(desc, "failed to flag the GPIO for IRQ\n");
486 goto free_id;
487 }
488
ff77c352
DG
489 desc->flags |= gpio_flags;
490 return 0;
491
ff77c352 492free_id:
5ba1821d 493 idr_remove(&dirent_idr, id);
ff77c352 494 desc->flags &= GPIO_FLAGS_MASK;
5ba1821d
DG
495free_sd:
496 if (value_sd)
497 sysfs_put(value_sd);
ff77c352
DG
498err_out:
499 return ret;
500}
501
502static const struct {
503 const char *name;
504 unsigned long flags;
505} trigger_types[] = {
506 { "none", 0 },
507 { "falling", BIT(FLAG_TRIG_FALL) },
508 { "rising", BIT(FLAG_TRIG_RISE) },
509 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
510};
511
512static ssize_t gpio_edge_show(struct device *dev,
513 struct device_attribute *attr, char *buf)
514{
515 const struct gpio_desc *desc = dev_get_drvdata(dev);
516 ssize_t status;
517
518 mutex_lock(&sysfs_lock);
519
520 if (!test_bit(FLAG_EXPORT, &desc->flags))
521 status = -EIO;
522 else {
523 int i;
524
525 status = 0;
526 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
527 if ((desc->flags & GPIO_TRIGGER_MASK)
528 == trigger_types[i].flags) {
529 status = sprintf(buf, "%s\n",
530 trigger_types[i].name);
531 break;
532 }
533 }
534
535 mutex_unlock(&sysfs_lock);
536 return status;
537}
538
539static ssize_t gpio_edge_store(struct device *dev,
540 struct device_attribute *attr, const char *buf, size_t size)
541{
542 struct gpio_desc *desc = dev_get_drvdata(dev);
543 ssize_t status;
544 int i;
545
546 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
547 if (sysfs_streq(trigger_types[i].name, buf))
548 goto found;
549 return -EINVAL;
550
551found:
552 mutex_lock(&sysfs_lock);
553
554 if (!test_bit(FLAG_EXPORT, &desc->flags))
555 status = -EIO;
556 else {
557 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
558 if (!status)
559 status = size;
560 }
561
562 mutex_unlock(&sysfs_lock);
563
564 return status;
565}
566
567static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
568
07697461
JN
569static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
570 int value)
571{
572 int status = 0;
573
574 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
575 return 0;
576
577 if (value)
578 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
579 else
580 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
581
582 /* reconfigure poll(2) support if enabled on one edge only */
583 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
584 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
585 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
586
587 gpio_setup_irq(desc, dev, 0);
588 status = gpio_setup_irq(desc, dev, trigger_flags);
589 }
590
591 return status;
592}
593
594static ssize_t gpio_active_low_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
596{
597 const struct gpio_desc *desc = dev_get_drvdata(dev);
598 ssize_t status;
599
600 mutex_lock(&sysfs_lock);
601
602 if (!test_bit(FLAG_EXPORT, &desc->flags))
603 status = -EIO;
604 else
605 status = sprintf(buf, "%d\n",
606 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
607
608 mutex_unlock(&sysfs_lock);
609
610 return status;
611}
612
613static ssize_t gpio_active_low_store(struct device *dev,
614 struct device_attribute *attr, const char *buf, size_t size)
615{
616 struct gpio_desc *desc = dev_get_drvdata(dev);
617 ssize_t status;
618
619 mutex_lock(&sysfs_lock);
620
621 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
622 status = -EIO;
623 } else {
624 long value;
625
a3d88c92 626 status = kstrtol(buf, 0, &value);
07697461
JN
627 if (status == 0)
628 status = sysfs_set_active_low(desc, dev, value != 0);
629 }
630
631 mutex_unlock(&sysfs_lock);
632
633 return status ? : size;
634}
635
636static const DEVICE_ATTR(active_low, 0644,
637 gpio_active_low_show, gpio_active_low_store);
638
d8f388d8 639static const struct attribute *gpio_attrs[] = {
d8f388d8 640 &dev_attr_value.attr,
07697461 641 &dev_attr_active_low.attr,
d8f388d8
DB
642 NULL,
643};
644
645static const struct attribute_group gpio_attr_group = {
646 .attrs = (struct attribute **) gpio_attrs,
647};
648
649/*
650 * /sys/class/gpio/gpiochipN/
651 * /base ... matching gpio_chip.base (N)
652 * /label ... matching gpio_chip.label
653 * /ngpio ... matching gpio_chip.ngpio
654 */
655
656static ssize_t chip_base_show(struct device *dev,
657 struct device_attribute *attr, char *buf)
658{
659 const struct gpio_chip *chip = dev_get_drvdata(dev);
660
661 return sprintf(buf, "%d\n", chip->base);
662}
663static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
664
665static ssize_t chip_label_show(struct device *dev,
666 struct device_attribute *attr, char *buf)
667{
668 const struct gpio_chip *chip = dev_get_drvdata(dev);
669
670 return sprintf(buf, "%s\n", chip->label ? : "");
671}
672static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
673
674static ssize_t chip_ngpio_show(struct device *dev,
675 struct device_attribute *attr, char *buf)
676{
677 const struct gpio_chip *chip = dev_get_drvdata(dev);
678
679 return sprintf(buf, "%u\n", chip->ngpio);
680}
681static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
682
683static const struct attribute *gpiochip_attrs[] = {
684 &dev_attr_base.attr,
685 &dev_attr_label.attr,
686 &dev_attr_ngpio.attr,
687 NULL,
688};
689
690static const struct attribute_group gpiochip_attr_group = {
691 .attrs = (struct attribute **) gpiochip_attrs,
692};
693
694/*
695 * /sys/class/gpio/export ... write-only
696 * integer N ... number of GPIO to export (full access)
697 * /sys/class/gpio/unexport ... write-only
698 * integer N ... number of GPIO to unexport
699 */
28812fe1
AK
700static ssize_t export_store(struct class *class,
701 struct class_attribute *attr,
702 const char *buf, size_t len)
d8f388d8 703{
372e722e
AC
704 long gpio;
705 struct gpio_desc *desc;
706 int status;
d8f388d8 707
a3d88c92 708 status = kstrtol(buf, 0, &gpio);
d8f388d8
DB
709 if (status < 0)
710 goto done;
711
372e722e 712 desc = gpio_to_desc(gpio);
bcabdef1
AC
713 /* reject invalid GPIOs */
714 if (!desc) {
715 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
716 return -EINVAL;
717 }
372e722e 718
d8f388d8
DB
719 /* No extra locking here; FLAG_SYSFS just signifies that the
720 * request and export were done by on behalf of userspace, so
721 * they may be undone on its behalf too.
722 */
723
372e722e 724 status = gpiod_request(desc, "sysfs");
ad2fab36
MN
725 if (status < 0) {
726 if (status == -EPROBE_DEFER)
727 status = -ENODEV;
d8f388d8 728 goto done;
ad2fab36 729 }
372e722e 730 status = gpiod_export(desc, true);
d8f388d8 731 if (status < 0)
372e722e 732 gpiod_free(desc);
d8f388d8 733 else
372e722e 734 set_bit(FLAG_SYSFS, &desc->flags);
d8f388d8
DB
735
736done:
737 if (status)
738 pr_debug("%s: status %d\n", __func__, status);
739 return status ? : len;
740}
741
28812fe1
AK
742static ssize_t unexport_store(struct class *class,
743 struct class_attribute *attr,
744 const char *buf, size_t len)
d8f388d8 745{
372e722e
AC
746 long gpio;
747 struct gpio_desc *desc;
748 int status;
d8f388d8 749
a3d88c92 750 status = kstrtol(buf, 0, &gpio);
d8f388d8
DB
751 if (status < 0)
752 goto done;
753
372e722e 754 desc = gpio_to_desc(gpio);
d8f388d8 755 /* reject bogus commands (gpio_unexport ignores them) */
bcabdef1
AC
756 if (!desc) {
757 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
758 return -EINVAL;
759 }
760
761 status = -EINVAL;
d8f388d8
DB
762
763 /* No extra locking here; FLAG_SYSFS just signifies that the
764 * request and export were done by on behalf of userspace, so
765 * they may be undone on its behalf too.
766 */
372e722e 767 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
d8f388d8 768 status = 0;
372e722e 769 gpiod_free(desc);
d8f388d8
DB
770 }
771done:
772 if (status)
773 pr_debug("%s: status %d\n", __func__, status);
774 return status ? : len;
775}
776
777static struct class_attribute gpio_class_attrs[] = {
778 __ATTR(export, 0200, NULL, export_store),
779 __ATTR(unexport, 0200, NULL, unexport_store),
780 __ATTR_NULL,
781};
782
783static struct class gpio_class = {
784 .name = "gpio",
785 .owner = THIS_MODULE,
786
787 .class_attrs = gpio_class_attrs,
788};
789
790
791/**
79a9becd 792 * gpiod_export - export a GPIO through sysfs
d8f388d8
DB
793 * @gpio: gpio to make available, already requested
794 * @direction_may_change: true if userspace may change gpio direction
795 * Context: arch_initcall or later
796 *
797 * When drivers want to make a GPIO accessible to userspace after they
798 * have requested it -- perhaps while debugging, or as part of their
799 * public interface -- they may use this routine. If the GPIO can
800 * change direction (some can't) and the caller allows it, userspace
801 * will see "direction" sysfs attribute which may be used to change
802 * the gpio's direction. A "value" attribute will always be provided.
803 *
804 * Returns zero on success, else an error.
805 */
79a9becd 806int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
d8f388d8
DB
807{
808 unsigned long flags;
fc4e2514 809 int status;
62154991 810 const char *ioname = NULL;
fc4e2514 811 struct device *dev;
372e722e 812 int offset;
d8f388d8
DB
813
814 /* can't export until sysfs is available ... */
815 if (!gpio_class.p) {
816 pr_debug("%s: called too early!\n", __func__);
817 return -ENOENT;
818 }
819
372e722e
AC
820 if (!desc) {
821 pr_debug("%s: invalid gpio descriptor\n", __func__);
fc4e2514
RM
822 return -EINVAL;
823 }
d8f388d8
DB
824
825 mutex_lock(&sysfs_lock);
826
827 spin_lock_irqsave(&gpio_lock, flags);
fc4e2514
RM
828 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
829 test_bit(FLAG_EXPORT, &desc->flags)) {
830 spin_unlock_irqrestore(&gpio_lock, flags);
7589e59f
AS
831 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
832 __func__,
fc4e2514
RM
833 test_bit(FLAG_REQUESTED, &desc->flags),
834 test_bit(FLAG_EXPORT, &desc->flags));
529f2ad5
DC
835 status = -EPERM;
836 goto fail_unlock;
d8f388d8 837 }
fc4e2514
RM
838
839 if (!desc->chip->direction_input || !desc->chip->direction_output)
840 direction_may_change = false;
d8f388d8
DB
841 spin_unlock_irqrestore(&gpio_lock, flags);
842
372e722e
AC
843 offset = gpio_chip_hwgpio(desc);
844 if (desc->chip->names && desc->chip->names[offset])
845 ioname = desc->chip->names[offset];
926b663c 846
fc4e2514 847 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
372e722e
AC
848 desc, ioname ? ioname : "gpio%u",
849 desc_to_gpio(desc));
fc4e2514
RM
850 if (IS_ERR(dev)) {
851 status = PTR_ERR(dev);
852 goto fail_unlock;
d8f388d8
DB
853 }
854
fc4e2514 855 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
d8f388d8 856 if (status)
fc4e2514 857 goto fail_unregister_device;
d8f388d8 858
fc4e2514
RM
859 if (direction_may_change) {
860 status = device_create_file(dev, &dev_attr_direction);
861 if (status)
862 goto fail_unregister_device;
863 }
864
372e722e 865 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
fc4e2514
RM
866 !test_bit(FLAG_IS_OUT, &desc->flags))) {
867 status = device_create_file(dev, &dev_attr_edge);
868 if (status)
869 goto fail_unregister_device;
870 }
d8f388d8 871
fc4e2514
RM
872 set_bit(FLAG_EXPORT, &desc->flags);
873 mutex_unlock(&sysfs_lock);
874 return 0;
875
876fail_unregister_device:
877 device_unregister(dev);
878fail_unlock:
879 mutex_unlock(&sysfs_lock);
7589e59f 880 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
d8f388d8
DB
881 return status;
882}
79a9becd 883EXPORT_SYMBOL_GPL(gpiod_export);
d8f388d8 884
9f3b795a 885static int match_export(struct device *dev, const void *data)
d8f388d8
DB
886{
887 return dev_get_drvdata(dev) == data;
888}
889
a4177ee7 890/**
79a9becd 891 * gpiod_export_link - create a sysfs link to an exported GPIO node
a4177ee7
JN
892 * @dev: device under which to create symlink
893 * @name: name of the symlink
894 * @gpio: gpio to create symlink to, already exported
895 *
896 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
897 * node. Caller is responsible for unlinking.
898 *
899 * Returns zero on success, else an error.
900 */
79a9becd
AC
901int gpiod_export_link(struct device *dev, const char *name,
902 struct gpio_desc *desc)
a4177ee7 903{
a4177ee7
JN
904 int status = -EINVAL;
905
bcabdef1
AC
906 if (!desc) {
907 pr_warn("%s: invalid GPIO\n", __func__);
908 return -EINVAL;
909 }
a4177ee7
JN
910
911 mutex_lock(&sysfs_lock);
912
a4177ee7
JN
913 if (test_bit(FLAG_EXPORT, &desc->flags)) {
914 struct device *tdev;
915
916 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
917 if (tdev != NULL) {
918 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
919 name);
920 } else {
921 status = -ENODEV;
922 }
923 }
924
925 mutex_unlock(&sysfs_lock);
926
a4177ee7 927 if (status)
7589e59f 928 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
a4177ee7
JN
929
930 return status;
931}
79a9becd 932EXPORT_SYMBOL_GPL(gpiod_export_link);
07697461
JN
933
934/**
79a9becd 935 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
07697461
JN
936 * @gpio: gpio to change
937 * @value: non-zero to use active low, i.e. inverted values
938 *
939 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
940 * The GPIO does not have to be exported yet. If poll(2) support has
941 * been enabled for either rising or falling edge, it will be
942 * reconfigured to follow the new polarity.
943 *
944 * Returns zero on success, else an error.
945 */
79a9becd 946int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
07697461 947{
07697461
JN
948 struct device *dev = NULL;
949 int status = -EINVAL;
950
bcabdef1
AC
951 if (!desc) {
952 pr_warn("%s: invalid GPIO\n", __func__);
953 return -EINVAL;
954 }
07697461
JN
955
956 mutex_lock(&sysfs_lock);
957
07697461 958 if (test_bit(FLAG_EXPORT, &desc->flags)) {
07697461
JN
959 dev = class_find_device(&gpio_class, NULL, desc, match_export);
960 if (dev == NULL) {
961 status = -ENODEV;
962 goto unlock;
963 }
964 }
965
966 status = sysfs_set_active_low(desc, dev, value);
967
968unlock:
969 mutex_unlock(&sysfs_lock);
970
07697461 971 if (status)
7589e59f 972 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
07697461
JN
973
974 return status;
975}
79a9becd 976EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
07697461 977
d8f388d8 978/**
79a9becd 979 * gpiod_unexport - reverse effect of gpio_export()
d8f388d8
DB
980 * @gpio: gpio to make unavailable
981 *
982 * This is implicit on gpio_free().
983 */
79a9becd 984void gpiod_unexport(struct gpio_desc *desc)
d8f388d8 985{
6a99ad4a 986 int status = 0;
864533ce 987 struct device *dev = NULL;
d8f388d8 988
372e722e 989 if (!desc) {
bcabdef1
AC
990 pr_warn("%s: invalid GPIO\n", __func__);
991 return;
6a99ad4a 992 }
d8f388d8
DB
993
994 mutex_lock(&sysfs_lock);
995
d8f388d8 996 if (test_bit(FLAG_EXPORT, &desc->flags)) {
d8f388d8
DB
997
998 dev = class_find_device(&gpio_class, NULL, desc, match_export);
999 if (dev) {
ff77c352 1000 gpio_setup_irq(desc, dev, 0);
d8f388d8 1001 clear_bit(FLAG_EXPORT, &desc->flags);
d8f388d8
DB
1002 } else
1003 status = -ENODEV;
1004 }
1005
1006 mutex_unlock(&sysfs_lock);
372e722e 1007
864533ce
ML
1008 if (dev) {
1009 device_unregister(dev);
1010 put_device(dev);
1011 }
bcabdef1 1012
d8f388d8 1013 if (status)
7589e59f 1014 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
372e722e 1015}
79a9becd 1016EXPORT_SYMBOL_GPL(gpiod_unexport);
d8f388d8
DB
1017
1018static int gpiochip_export(struct gpio_chip *chip)
1019{
1020 int status;
1021 struct device *dev;
1022
1023 /* Many systems register gpio chips for SOC support very early,
1024 * before driver model support is available. In those cases we
1025 * export this later, in gpiolib_sysfs_init() ... here we just
1026 * verify that _some_ field of gpio_class got initialized.
1027 */
1028 if (!gpio_class.p)
1029 return 0;
1030
1031 /* use chip->base for the ID; it's already known to be unique */
1032 mutex_lock(&sysfs_lock);
1033 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
1034 "gpiochip%d", chip->base);
d62668e1 1035 if (!IS_ERR(dev)) {
d8f388d8
DB
1036 status = sysfs_create_group(&dev->kobj,
1037 &gpiochip_attr_group);
1038 } else
d62668e1 1039 status = PTR_ERR(dev);
d8f388d8
DB
1040 chip->exported = (status == 0);
1041 mutex_unlock(&sysfs_lock);
1042
1043 if (status) {
1044 unsigned long flags;
1045 unsigned gpio;
1046
1047 spin_lock_irqsave(&gpio_lock, flags);
6c0b4e6c
AC
1048 gpio = 0;
1049 while (gpio < chip->ngpio)
1050 chip->desc[gpio++].chip = NULL;
d8f388d8
DB
1051 spin_unlock_irqrestore(&gpio_lock, flags);
1052
1a2a99c6 1053 chip_dbg(chip, "%s: status %d\n", __func__, status);
d8f388d8
DB
1054 }
1055
1056 return status;
1057}
1058
1059static void gpiochip_unexport(struct gpio_chip *chip)
1060{
1061 int status;
1062 struct device *dev;
1063
1064 mutex_lock(&sysfs_lock);
1065 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1066 if (dev) {
1067 put_device(dev);
1068 device_unregister(dev);
9fb1f39e 1069 chip->exported = false;
d8f388d8
DB
1070 status = 0;
1071 } else
1072 status = -ENODEV;
1073 mutex_unlock(&sysfs_lock);
1074
1075 if (status)
1a2a99c6 1076 chip_dbg(chip, "%s: status %d\n", __func__, status);
d8f388d8
DB
1077}
1078
1079static int __init gpiolib_sysfs_init(void)
1080{
1081 int status;
1082 unsigned long flags;
65493e3a 1083 struct gpio_chip *chip;
d8f388d8
DB
1084
1085 status = class_register(&gpio_class);
1086 if (status < 0)
1087 return status;
1088
1089 /* Scan and register the gpio_chips which registered very
1090 * early (e.g. before the class_register above was called).
1091 *
1092 * We run before arch_initcall() so chip->dev nodes can have
1093 * registered, and so arch_initcall() can always gpio_export().
1094 */
1095 spin_lock_irqsave(&gpio_lock, flags);
65493e3a 1096 list_for_each_entry(chip, &gpio_chips, list) {
d8f388d8
DB
1097 if (!chip || chip->exported)
1098 continue;
1099
1100 spin_unlock_irqrestore(&gpio_lock, flags);
1101 status = gpiochip_export(chip);
1102 spin_lock_irqsave(&gpio_lock, flags);
1103 }
1104 spin_unlock_irqrestore(&gpio_lock, flags);
1105
1106
1107 return status;
1108}
1109postcore_initcall(gpiolib_sysfs_init);
1110
1111#else
1112static inline int gpiochip_export(struct gpio_chip *chip)
1113{
1114 return 0;
1115}
1116
1117static inline void gpiochip_unexport(struct gpio_chip *chip)
1118{
1119}
1120
1121#endif /* CONFIG_GPIO_SYSFS */
1122
1a989d0f
AC
1123/*
1124 * Add a new chip to the global chips list, keeping the list of chips sorted
1125 * by base order.
1126 *
1127 * Return -EBUSY if the new chip overlaps with some other chip's integer
1128 * space.
1129 */
1130static int gpiochip_add_to_list(struct gpio_chip *chip)
1131{
1132 struct list_head *pos = &gpio_chips;
1133 struct gpio_chip *_chip;
1134 int err = 0;
1135
1136 /* find where to insert our chip */
1137 list_for_each(pos, &gpio_chips) {
1138 _chip = list_entry(pos, struct gpio_chip, list);
1139 /* shall we insert before _chip? */
1140 if (_chip->base >= chip->base + chip->ngpio)
1141 break;
1142 }
1143
1144 /* are we stepping on the chip right before? */
1145 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1146 _chip = list_entry(pos->prev, struct gpio_chip, list);
1147 if (_chip->base + _chip->ngpio > chip->base) {
1148 dev_err(chip->dev,
1149 "GPIO integer space overlap, cannot add chip\n");
1150 err = -EBUSY;
1151 }
1152 }
1153
1154 if (!err)
1155 list_add_tail(&chip->list, pos);
1156
1157 return err;
1158}
1159
d2876d08
DB
1160/**
1161 * gpiochip_add() - register a gpio_chip
1162 * @chip: the chip to register, with chip->base initialized
1163 * Context: potentially before irqs or kmalloc will work
1164 *
1165 * Returns a negative errno if the chip can't be registered, such as
1166 * because the chip->base is invalid or already associated with a
1167 * different chip. Otherwise it returns zero as a success code.
8d0aab2f 1168 *
d8f388d8
DB
1169 * When gpiochip_add() is called very early during boot, so that GPIOs
1170 * can be freely used, the chip->dev device must be registered before
1171 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1172 * for GPIOs will fail rudely.
1173 *
8d0aab2f
AV
1174 * If chip->base is negative, this requests dynamic assignment of
1175 * a range of valid GPIOs.
d2876d08
DB
1176 */
1177int gpiochip_add(struct gpio_chip *chip)
1178{
1179 unsigned long flags;
1180 int status = 0;
1181 unsigned id;
8d0aab2f 1182 int base = chip->base;
d2876d08 1183
bff5fda9 1184 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
8d0aab2f 1185 && base >= 0) {
d2876d08
DB
1186 status = -EINVAL;
1187 goto fail;
1188 }
1189
1190 spin_lock_irqsave(&gpio_lock, flags);
1191
8d0aab2f
AV
1192 if (base < 0) {
1193 base = gpiochip_find_base(chip->ngpio);
1194 if (base < 0) {
1195 status = base;
d8f388d8 1196 goto unlock;
8d0aab2f
AV
1197 }
1198 chip->base = base;
1199 }
1200
1a989d0f
AC
1201 status = gpiochip_add_to_list(chip);
1202
d2876d08 1203 if (status == 0) {
6c0b4e6c
AC
1204 chip->desc = &gpio_desc[chip->base];
1205
1206 for (id = 0; id < chip->ngpio; id++) {
1207 struct gpio_desc *desc = &chip->desc[id];
1208 desc->chip = chip;
d8f388d8
DB
1209
1210 /* REVISIT: most hardware initializes GPIOs as
1211 * inputs (often with pullups enabled) so power
1212 * usage is minimized. Linux code should set the
1213 * gpio direction first thing; but until it does,
80b0a602 1214 * and in case chip->get_direction is not set,
d8f388d8
DB
1215 * we may expose the wrong direction in sysfs.
1216 */
6c0b4e6c 1217 desc->flags = !chip->direction_input
d8f388d8
DB
1218 ? (1 << FLAG_IS_OUT)
1219 : 0;
d2876d08
DB
1220 }
1221 }
1222
3bae4811
ZG
1223 spin_unlock_irqrestore(&gpio_lock, flags);
1224
f23f1516
SH
1225#ifdef CONFIG_PINCTRL
1226 INIT_LIST_HEAD(&chip->pin_ranges);
1227#endif
1228
391c970c 1229 of_gpiochip_add(chip);
664e3e5a 1230 acpi_gpiochip_add(chip);
391c970c 1231
cedb1881
AV
1232 if (status)
1233 goto fail;
1234
1235 status = gpiochip_export(chip);
1236 if (status)
1237 goto fail;
1238
7589e59f 1239 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
64842aad
GL
1240 chip->base, chip->base + chip->ngpio - 1,
1241 chip->label ? : "generic");
1242
cedb1881 1243 return 0;
3bae4811
ZG
1244
1245unlock:
1246 spin_unlock_irqrestore(&gpio_lock, flags);
d2876d08
DB
1247fail:
1248 /* failures here can mean systems won't boot... */
7589e59f 1249 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
cedb1881
AV
1250 chip->base, chip->base + chip->ngpio - 1,
1251 chip->label ? : "generic");
d2876d08
DB
1252 return status;
1253}
1254EXPORT_SYMBOL_GPL(gpiochip_add);
1255
1256/**
1257 * gpiochip_remove() - unregister a gpio_chip
1258 * @chip: the chip to unregister
1259 *
1260 * A gpio_chip with any GPIOs still requested may not be removed.
1261 */
1262int gpiochip_remove(struct gpio_chip *chip)
1263{
1264 unsigned long flags;
1265 int status = 0;
1266 unsigned id;
1267
1268 spin_lock_irqsave(&gpio_lock, flags);
1269
9ef0d6f7 1270 gpiochip_remove_pin_ranges(chip);
391c970c 1271 of_gpiochip_remove(chip);
664e3e5a 1272 acpi_gpiochip_remove(chip);
391c970c 1273
6c0b4e6c
AC
1274 for (id = 0; id < chip->ngpio; id++) {
1275 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
d2876d08
DB
1276 status = -EBUSY;
1277 break;
1278 }
1279 }
1280 if (status == 0) {
6c0b4e6c
AC
1281 for (id = 0; id < chip->ngpio; id++)
1282 chip->desc[id].chip = NULL;
1a989d0f
AC
1283
1284 list_del(&chip->list);
d2876d08
DB
1285 }
1286
1287 spin_unlock_irqrestore(&gpio_lock, flags);
d8f388d8
DB
1288
1289 if (status == 0)
1290 gpiochip_unexport(chip);
1291
d2876d08
DB
1292 return status;
1293}
1294EXPORT_SYMBOL_GPL(gpiochip_remove);
1295
594fa265
GL
1296/**
1297 * gpiochip_find() - iterator for locating a specific gpio_chip
1298 * @data: data to pass to match function
1299 * @callback: Callback function to check gpio_chip
1300 *
1301 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1302 * determined by a user supplied @match callback. The callback should return
1303 * 0 if the device doesn't match and non-zero if it does. If the callback is
1304 * non-zero, this function will return to the caller and not iterate over any
1305 * more gpio_chips.
1306 */
07ce8ec7 1307struct gpio_chip *gpiochip_find(void *data,
6e2cf651 1308 int (*match)(struct gpio_chip *chip,
3d0f7cf0 1309 void *data))
594fa265 1310{
125eef96 1311 struct gpio_chip *chip;
594fa265 1312 unsigned long flags;
594fa265
GL
1313
1314 spin_lock_irqsave(&gpio_lock, flags);
125eef96
AC
1315 list_for_each_entry(chip, &gpio_chips, list)
1316 if (match(chip, data))
594fa265 1317 break;
125eef96
AC
1318
1319 /* No match? */
1320 if (&chip->list == &gpio_chips)
1321 chip = NULL;
594fa265
GL
1322 spin_unlock_irqrestore(&gpio_lock, flags);
1323
1324 return chip;
1325}
8fa0c9bf 1326EXPORT_SYMBOL_GPL(gpiochip_find);
d2876d08 1327
79697ef9
AC
1328static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1329{
1330 const char *name = data;
1331
1332 return !strcmp(chip->label, name);
1333}
1334
1335static struct gpio_chip *find_chip_by_name(const char *name)
1336{
1337 return gpiochip_find((void *)name, gpiochip_match_name);
1338}
1339
f23f1516 1340#ifdef CONFIG_PINCTRL
165adc9c 1341
586a87e6
CR
1342/**
1343 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1344 * @chip: the gpiochip to add the range for
1345 * @pinctrl: the dev_name() of the pin controller to map to
1346 * @gpio_offset: the start offset in the current gpio_chip number space
1347 * @pin_group: name of the pin group inside the pin controller
1348 */
1349int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1350 struct pinctrl_dev *pctldev,
1351 unsigned int gpio_offset, const char *pin_group)
1352{
1353 struct gpio_pin_range *pin_range;
1354 int ret;
1355
1356 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1357 if (!pin_range) {
1a2a99c6 1358 chip_err(chip, "failed to allocate pin ranges\n");
586a87e6
CR
1359 return -ENOMEM;
1360 }
1361
1362 /* Use local offset as range ID */
1363 pin_range->range.id = gpio_offset;
1364 pin_range->range.gc = chip;
1365 pin_range->range.name = chip->label;
1366 pin_range->range.base = chip->base + gpio_offset;
1367 pin_range->pctldev = pctldev;
1368
1369 ret = pinctrl_get_group_pins(pctldev, pin_group,
1370 &pin_range->range.pins,
1371 &pin_range->range.npins);
61c6375d
MN
1372 if (ret < 0) {
1373 kfree(pin_range);
586a87e6 1374 return ret;
61c6375d 1375 }
586a87e6
CR
1376
1377 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1378
1a2a99c6
AS
1379 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1380 gpio_offset, gpio_offset + pin_range->range.npins - 1,
586a87e6
CR
1381 pinctrl_dev_get_devname(pctldev), pin_group);
1382
1383 list_add_tail(&pin_range->node, &chip->pin_ranges);
1384
1385 return 0;
1386}
1387EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1388
3f0f8670
LW
1389/**
1390 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1391 * @chip: the gpiochip to add the range for
1392 * @pinctrl_name: the dev_name() of the pin controller to map to
316511c0
LW
1393 * @gpio_offset: the start offset in the current gpio_chip number space
1394 * @pin_offset: the start offset in the pin controller number space
3f0f8670
LW
1395 * @npins: the number of pins from the offset of each pin space (GPIO and
1396 * pin controller) to accumulate in this range
1397 */
1e63d7b9 1398int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
316511c0 1399 unsigned int gpio_offset, unsigned int pin_offset,
3f0f8670 1400 unsigned int npins)
f23f1516
SH
1401{
1402 struct gpio_pin_range *pin_range;
b4d4b1f0 1403 int ret;
f23f1516 1404
3f0f8670 1405 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
f23f1516 1406 if (!pin_range) {
1a2a99c6 1407 chip_err(chip, "failed to allocate pin ranges\n");
1e63d7b9 1408 return -ENOMEM;
f23f1516
SH
1409 }
1410
3f0f8670 1411 /* Use local offset as range ID */
316511c0 1412 pin_range->range.id = gpio_offset;
3f0f8670 1413 pin_range->range.gc = chip;
f23f1516 1414 pin_range->range.name = chip->label;
316511c0
LW
1415 pin_range->range.base = chip->base + gpio_offset;
1416 pin_range->range.pin_base = pin_offset;
f23f1516 1417 pin_range->range.npins = npins;
192c369c 1418 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
f23f1516 1419 &pin_range->range);
8f23ca1a 1420 if (IS_ERR(pin_range->pctldev)) {
b4d4b1f0 1421 ret = PTR_ERR(pin_range->pctldev);
1a2a99c6 1422 chip_err(chip, "could not create pin range\n");
3f0f8670 1423 kfree(pin_range);
b4d4b1f0 1424 return ret;
3f0f8670 1425 }
1a2a99c6
AS
1426 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1427 gpio_offset, gpio_offset + npins - 1,
316511c0
LW
1428 pinctl_name,
1429 pin_offset, pin_offset + npins - 1);
f23f1516
SH
1430
1431 list_add_tail(&pin_range->node, &chip->pin_ranges);
1e63d7b9
LW
1432
1433 return 0;
f23f1516 1434}
165adc9c 1435EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
f23f1516 1436
3f0f8670
LW
1437/**
1438 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1439 * @chip: the chip to remove all the mappings for
1440 */
f23f1516
SH
1441void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1442{
1443 struct gpio_pin_range *pin_range, *tmp;
1444
1445 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1446 list_del(&pin_range->node);
1447 pinctrl_remove_gpio_range(pin_range->pctldev,
1448 &pin_range->range);
3f0f8670 1449 kfree(pin_range);
f23f1516
SH
1450 }
1451}
165adc9c
LW
1452EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1453
1454#endif /* CONFIG_PINCTRL */
f23f1516 1455
d2876d08
DB
1456/* These "optional" allocation calls help prevent drivers from stomping
1457 * on each other, and help provide better diagnostics in debugfs.
1458 * They're called even less than the "set direction" calls.
1459 */
372e722e 1460static int gpiod_request(struct gpio_desc *desc, const char *label)
d2876d08 1461{
35e8bb51 1462 struct gpio_chip *chip;
e9354576 1463 int status = -EPROBE_DEFER;
d2876d08
DB
1464 unsigned long flags;
1465
0204df47 1466 if (!desc) {
bcabdef1
AC
1467 pr_warn("%s: invalid GPIO\n", __func__);
1468 return -EINVAL;
ad2fab36 1469 }
bcabdef1
AC
1470
1471 spin_lock_irqsave(&gpio_lock, flags);
1472
35e8bb51 1473 chip = desc->chip;
0204df47
AC
1474 if (chip == NULL)
1475 goto done;
d2876d08 1476
35e8bb51 1477 if (!try_module_get(chip->owner))
438d8908
GL
1478 goto done;
1479
d2876d08 1480 /* NOTE: gpio_request() can be called in early boot,
35e8bb51 1481 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
d2876d08
DB
1482 */
1483
1484 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1485 desc_set_label(desc, label ? : "?");
1486 status = 0;
438d8908 1487 } else {
d2876d08 1488 status = -EBUSY;
35e8bb51 1489 module_put(chip->owner);
7460db56 1490 goto done;
35e8bb51
DB
1491 }
1492
1493 if (chip->request) {
1494 /* chip->request may sleep */
1495 spin_unlock_irqrestore(&gpio_lock, flags);
372e722e 1496 status = chip->request(chip, gpio_chip_hwgpio(desc));
35e8bb51
DB
1497 spin_lock_irqsave(&gpio_lock, flags);
1498
1499 if (status < 0) {
1500 desc_set_label(desc, NULL);
1501 module_put(chip->owner);
1502 clear_bit(FLAG_REQUESTED, &desc->flags);
80b0a602 1503 goto done;
35e8bb51 1504 }
438d8908 1505 }
80b0a602
MN
1506 if (chip->get_direction) {
1507 /* chip->get_direction may sleep */
1508 spin_unlock_irqrestore(&gpio_lock, flags);
372e722e 1509 gpiod_get_direction(desc);
80b0a602
MN
1510 spin_lock_irqsave(&gpio_lock, flags);
1511 }
d2876d08
DB
1512done:
1513 if (status)
7589e59f 1514 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
d2876d08
DB
1515 spin_unlock_irqrestore(&gpio_lock, flags);
1516 return status;
1517}
372e722e
AC
1518
1519int gpio_request(unsigned gpio, const char *label)
1520{
1521 return gpiod_request(gpio_to_desc(gpio), label);
1522}
d2876d08
DB
1523EXPORT_SYMBOL_GPL(gpio_request);
1524
372e722e 1525static void gpiod_free(struct gpio_desc *desc)
d2876d08
DB
1526{
1527 unsigned long flags;
35e8bb51 1528 struct gpio_chip *chip;
d2876d08 1529
3d599d1c
UKK
1530 might_sleep();
1531
372e722e 1532 if (!desc) {
d2876d08
DB
1533 WARN_ON(extra_checks);
1534 return;
1535 }
1536
372e722e 1537 gpiod_unexport(desc);
d8f388d8 1538
d2876d08
DB
1539 spin_lock_irqsave(&gpio_lock, flags);
1540
35e8bb51
DB
1541 chip = desc->chip;
1542 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1543 if (chip->free) {
1544 spin_unlock_irqrestore(&gpio_lock, flags);
9c4ba946 1545 might_sleep_if(chip->can_sleep);
372e722e 1546 chip->free(chip, gpio_chip_hwgpio(desc));
35e8bb51
DB
1547 spin_lock_irqsave(&gpio_lock, flags);
1548 }
d2876d08 1549 desc_set_label(desc, NULL);
438d8908 1550 module_put(desc->chip->owner);
07697461 1551 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
35e8bb51 1552 clear_bit(FLAG_REQUESTED, &desc->flags);
aca5ce14 1553 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
25553ff0 1554 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
438d8908 1555 } else
d2876d08
DB
1556 WARN_ON(extra_checks);
1557
1558 spin_unlock_irqrestore(&gpio_lock, flags);
1559}
372e722e
AC
1560
1561void gpio_free(unsigned gpio)
1562{
1563 gpiod_free(gpio_to_desc(gpio));
1564}
d2876d08
DB
1565EXPORT_SYMBOL_GPL(gpio_free);
1566
3e45f1d1
EM
1567/**
1568 * gpio_request_one - request a single GPIO with initial configuration
1569 * @gpio: the GPIO number
1570 * @flags: GPIO configuration as specified by GPIOF_*
1571 * @label: a literal description string of this GPIO
1572 */
1573int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1574{
372e722e 1575 struct gpio_desc *desc;
3e45f1d1
EM
1576 int err;
1577
372e722e
AC
1578 desc = gpio_to_desc(gpio);
1579
1580 err = gpiod_request(desc, label);
3e45f1d1
EM
1581 if (err)
1582 return err;
1583
aca5ce14 1584 if (flags & GPIOF_OPEN_DRAIN)
372e722e 1585 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
aca5ce14 1586
25553ff0 1587 if (flags & GPIOF_OPEN_SOURCE)
372e722e 1588 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
25553ff0 1589
3e45f1d1 1590 if (flags & GPIOF_DIR_IN)
372e722e 1591 err = gpiod_direction_input(desc);
3e45f1d1 1592 else
ef70bbe1 1593 err = gpiod_direction_output_raw(desc,
3e45f1d1
EM
1594 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1595
e254811c 1596 if (err)
fc3a1f04
WS
1597 goto free_gpio;
1598
1599 if (flags & GPIOF_EXPORT) {
372e722e 1600 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
fc3a1f04
WS
1601 if (err)
1602 goto free_gpio;
1603 }
1604
1605 return 0;
e254811c 1606
fc3a1f04 1607 free_gpio:
372e722e 1608 gpiod_free(desc);
3e45f1d1
EM
1609 return err;
1610}
1611EXPORT_SYMBOL_GPL(gpio_request_one);
1612
1613/**
1614 * gpio_request_array - request multiple GPIOs in a single call
1615 * @array: array of the 'struct gpio'
1616 * @num: how many GPIOs in the array
1617 */
7c295975 1618int gpio_request_array(const struct gpio *array, size_t num)
3e45f1d1
EM
1619{
1620 int i, err;
1621
1622 for (i = 0; i < num; i++, array++) {
1623 err = gpio_request_one(array->gpio, array->flags, array->label);
1624 if (err)
1625 goto err_free;
1626 }
1627 return 0;
1628
1629err_free:
1630 while (i--)
1631 gpio_free((--array)->gpio);
1632 return err;
1633}
1634EXPORT_SYMBOL_GPL(gpio_request_array);
1635
1636/**
1637 * gpio_free_array - release multiple GPIOs in a single call
1638 * @array: array of the 'struct gpio'
1639 * @num: how many GPIOs in the array
1640 */
7c295975 1641void gpio_free_array(const struct gpio *array, size_t num)
3e45f1d1
EM
1642{
1643 while (num--)
1644 gpio_free((array++)->gpio);
1645}
1646EXPORT_SYMBOL_GPL(gpio_free_array);
d2876d08
DB
1647
1648/**
1649 * gpiochip_is_requested - return string iff signal was requested
1650 * @chip: controller managing the signal
1651 * @offset: of signal within controller's 0..(ngpio - 1) range
1652 *
1653 * Returns NULL if the GPIO is not currently requested, else a string.
1654 * If debugfs support is enabled, the string returned is the label passed
1655 * to gpio_request(); otherwise it is a meaningless constant.
1656 *
1657 * This function is for use by GPIO controller drivers. The label can
1658 * help with diagnostics, and knowing that the signal is used as a GPIO
1659 * can help avoid accidentally multiplexing it to another controller.
1660 */
1661const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1662{
6c0b4e6c 1663 struct gpio_desc *desc;
d2876d08 1664
6c0b4e6c 1665 if (!GPIO_OFFSET_VALID(chip, offset))
d2876d08 1666 return NULL;
6c0b4e6c
AC
1667
1668 desc = &chip->desc[offset];
1669
372e722e 1670 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
d2876d08
DB
1671 return NULL;
1672#ifdef CONFIG_DEBUG_FS
372e722e 1673 return desc->label;
d2876d08
DB
1674#else
1675 return "?";
1676#endif
1677}
1678EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1679
1680
1681/* Drivers MUST set GPIO direction before making get/set calls. In
1682 * some cases this is done in early boot, before IRQs are enabled.
1683 *
1684 * As a rule these aren't called more than once (except for drivers
1685 * using the open-drain emulation idiom) so these are natural places
1686 * to accumulate extra debugging checks. Note that we can't (yet)
1687 * rely on gpio_request() having been called beforehand.
1688 */
1689
79a9becd
AC
1690/**
1691 * gpiod_direction_input - set the GPIO direction to input
1692 * @desc: GPIO to set to input
1693 *
1694 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1695 * be called safely on it.
1696 *
1697 * Return 0 in case of success, else an error code.
1698 */
1699int gpiod_direction_input(struct gpio_desc *desc)
d2876d08
DB
1700{
1701 unsigned long flags;
1702 struct gpio_chip *chip;
d2876d08 1703 int status = -EINVAL;
372e722e 1704 int offset;
d2876d08 1705
be1a4b13 1706 if (!desc || !desc->chip) {
bcabdef1
AC
1707 pr_warn("%s: invalid GPIO\n", __func__);
1708 return -EINVAL;
1709 }
1710
be1a4b13
LW
1711 chip = desc->chip;
1712 if (!chip->get || !chip->direction_input) {
6424de5a
MB
1713 gpiod_warn(desc,
1714 "%s: missing get() or direction_input() operations\n",
7589e59f 1715 __func__);
be1a4b13
LW
1716 return -EIO;
1717 }
1718
d2876d08
DB
1719 spin_lock_irqsave(&gpio_lock, flags);
1720
372e722e 1721 status = gpio_ensure_requested(desc);
35e8bb51
DB
1722 if (status < 0)
1723 goto fail;
d2876d08
DB
1724
1725 /* now we know the gpio is valid and chip won't vanish */
1726
1727 spin_unlock_irqrestore(&gpio_lock, flags);
1728
9c4ba946 1729 might_sleep_if(chip->can_sleep);
d2876d08 1730
372e722e 1731 offset = gpio_chip_hwgpio(desc);
35e8bb51 1732 if (status) {
372e722e 1733 status = chip->request(chip, offset);
35e8bb51 1734 if (status < 0) {
7589e59f
AS
1735 gpiod_dbg(desc, "%s: chip request fail, %d\n",
1736 __func__, status);
35e8bb51
DB
1737 /* and it's not available to anyone else ...
1738 * gpio_request() is the fully clean solution.
1739 */
1740 goto lose;
1741 }
1742 }
1743
372e722e 1744 status = chip->direction_input(chip, offset);
d2876d08
DB
1745 if (status == 0)
1746 clear_bit(FLAG_IS_OUT, &desc->flags);
3f397c21 1747
372e722e 1748 trace_gpio_direction(desc_to_gpio(desc), 1, status);
35e8bb51 1749lose:
d2876d08
DB
1750 return status;
1751fail:
1752 spin_unlock_irqrestore(&gpio_lock, flags);
bcabdef1 1753 if (status)
7589e59f 1754 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
d2876d08
DB
1755 return status;
1756}
79a9becd 1757EXPORT_SYMBOL_GPL(gpiod_direction_input);
372e722e 1758
ef70bbe1 1759static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
d2876d08
DB
1760{
1761 unsigned long flags;
1762 struct gpio_chip *chip;
d2876d08 1763 int status = -EINVAL;
372e722e 1764 int offset;
d2876d08 1765
d468bf9e
LW
1766 /* GPIOs used for IRQs shall not be set as output */
1767 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1768 gpiod_err(desc,
1769 "%s: tried to set a GPIO tied to an IRQ as output\n",
1770 __func__);
1771 return -EIO;
1772 }
1773
aca5ce14
LD
1774 /* Open drain pin should not be driven to 1 */
1775 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
372e722e 1776 return gpiod_direction_input(desc);
aca5ce14 1777
25553ff0
LD
1778 /* Open source pin should not be driven to 0 */
1779 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
372e722e 1780 return gpiod_direction_input(desc);
25553ff0 1781
be1a4b13
LW
1782 chip = desc->chip;
1783 if (!chip->set || !chip->direction_output) {
6424de5a
MB
1784 gpiod_warn(desc,
1785 "%s: missing set() or direction_output() operations\n",
1786 __func__);
be1a4b13
LW
1787 return -EIO;
1788 }
1789
d2876d08
DB
1790 spin_lock_irqsave(&gpio_lock, flags);
1791
372e722e 1792 status = gpio_ensure_requested(desc);
35e8bb51
DB
1793 if (status < 0)
1794 goto fail;
d2876d08
DB
1795
1796 /* now we know the gpio is valid and chip won't vanish */
1797
1798 spin_unlock_irqrestore(&gpio_lock, flags);
1799
9c4ba946 1800 might_sleep_if(chip->can_sleep);
d2876d08 1801
372e722e 1802 offset = gpio_chip_hwgpio(desc);
35e8bb51 1803 if (status) {
372e722e 1804 status = chip->request(chip, offset);
35e8bb51 1805 if (status < 0) {
7589e59f
AS
1806 gpiod_dbg(desc, "%s: chip request fail, %d\n",
1807 __func__, status);
35e8bb51
DB
1808 /* and it's not available to anyone else ...
1809 * gpio_request() is the fully clean solution.
1810 */
1811 goto lose;
1812 }
1813 }
1814
372e722e 1815 status = chip->direction_output(chip, offset, value);
d2876d08
DB
1816 if (status == 0)
1817 set_bit(FLAG_IS_OUT, &desc->flags);
372e722e
AC
1818 trace_gpio_value(desc_to_gpio(desc), 0, value);
1819 trace_gpio_direction(desc_to_gpio(desc), 0, status);
35e8bb51 1820lose:
d2876d08
DB
1821 return status;
1822fail:
1823 spin_unlock_irqrestore(&gpio_lock, flags);
bcabdef1 1824 if (status)
6424de5a 1825 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
d2876d08
DB
1826 return status;
1827}
ef70bbe1
PZ
1828
1829/**
1830 * gpiod_direction_output_raw - set the GPIO direction to output
1831 * @desc: GPIO to set to output
1832 * @value: initial output value of the GPIO
1833 *
1834 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1835 * be called safely on it. The initial value of the output must be specified
1836 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1837 *
1838 * Return 0 in case of success, else an error code.
1839 */
1840int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1841{
1842 if (!desc || !desc->chip) {
1843 pr_warn("%s: invalid GPIO\n", __func__);
1844 return -EINVAL;
1845 }
1846 return _gpiod_direction_output_raw(desc, value);
1847}
1848EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1849
1850/**
90df4fe0 1851 * gpiod_direction_output - set the GPIO direction to output
ef70bbe1
PZ
1852 * @desc: GPIO to set to output
1853 * @value: initial output value of the GPIO
1854 *
1855 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1856 * be called safely on it. The initial value of the output must be specified
1857 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1858 * account.
1859 *
1860 * Return 0 in case of success, else an error code.
1861 */
1862int gpiod_direction_output(struct gpio_desc *desc, int value)
1863{
1864 if (!desc || !desc->chip) {
1865 pr_warn("%s: invalid GPIO\n", __func__);
1866 return -EINVAL;
1867 }
1868 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1869 value = !value;
1870 return _gpiod_direction_output_raw(desc, value);
1871}
79a9becd 1872EXPORT_SYMBOL_GPL(gpiod_direction_output);
d2876d08 1873
c4b5be98 1874/**
79a9becd 1875 * gpiod_set_debounce - sets @debounce time for a @gpio
c4b5be98
FB
1876 * @gpio: the gpio to set debounce time
1877 * @debounce: debounce time is microseconds
65d87656
LW
1878 *
1879 * returns -ENOTSUPP if the controller does not support setting
1880 * debounce.
c4b5be98 1881 */
79a9becd 1882int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
c4b5be98
FB
1883{
1884 unsigned long flags;
1885 struct gpio_chip *chip;
c4b5be98 1886 int status = -EINVAL;
372e722e 1887 int offset;
c4b5be98 1888
be1a4b13 1889 if (!desc || !desc->chip) {
bcabdef1
AC
1890 pr_warn("%s: invalid GPIO\n", __func__);
1891 return -EINVAL;
1892 }
1893
c4b5be98 1894 chip = desc->chip;
be1a4b13 1895 if (!chip->set || !chip->set_debounce) {
6424de5a
MB
1896 gpiod_dbg(desc,
1897 "%s: missing set() or set_debounce() operations\n",
1898 __func__);
65d87656 1899 return -ENOTSUPP;
be1a4b13
LW
1900 }
1901
1902 spin_lock_irqsave(&gpio_lock, flags);
372e722e
AC
1903
1904 status = gpio_ensure_requested(desc);
c4b5be98
FB
1905 if (status < 0)
1906 goto fail;
1907
1908 /* now we know the gpio is valid and chip won't vanish */
1909
1910 spin_unlock_irqrestore(&gpio_lock, flags);
1911
9c4ba946 1912 might_sleep_if(chip->can_sleep);
c4b5be98 1913
372e722e
AC
1914 offset = gpio_chip_hwgpio(desc);
1915 return chip->set_debounce(chip, offset, debounce);
c4b5be98
FB
1916
1917fail:
1918 spin_unlock_irqrestore(&gpio_lock, flags);
bcabdef1 1919 if (status)
6424de5a 1920 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
c4b5be98
FB
1921
1922 return status;
1923}
79a9becd 1924EXPORT_SYMBOL_GPL(gpiod_set_debounce);
372e722e 1925
79a9becd
AC
1926/**
1927 * gpiod_is_active_low - test whether a GPIO is active-low or not
1928 * @desc: the gpio descriptor to test
1929 *
1930 * Returns 1 if the GPIO is active-low, 0 otherwise.
1931 */
1932int gpiod_is_active_low(const struct gpio_desc *desc)
372e722e 1933{
79a9becd 1934 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
372e722e 1935}
79a9becd 1936EXPORT_SYMBOL_GPL(gpiod_is_active_low);
d2876d08
DB
1937
1938/* I/O calls are only valid after configuration completed; the relevant
1939 * "is this a valid GPIO" error checks should already have been done.
1940 *
1941 * "Get" operations are often inlinable as reading a pin value register,
1942 * and masking the relevant bit in that register.
1943 *
1944 * When "set" operations are inlinable, they involve writing that mask to
1945 * one register to set a low value, or a different register to set it high.
1946 * Otherwise locking is needed, so there may be little value to inlining.
1947 *
1948 *------------------------------------------------------------------------
1949 *
1950 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1951 * have requested the GPIO. That can include implicit requesting by
1952 * a direction setting call. Marking a gpio as requested locks its chip
1953 * in memory, guaranteeing that these table lookups need no more locking
1954 * and that gpiochip_remove() will fail.
1955 *
1956 * REVISIT when debugging, consider adding some instrumentation to ensure
1957 * that the GPIO was actually requested.
1958 */
1959
79a9becd 1960static int _gpiod_get_raw_value(const struct gpio_desc *desc)
d2876d08
DB
1961{
1962 struct gpio_chip *chip;
3f397c21 1963 int value;
372e722e 1964 int offset;
d2876d08 1965
372e722e
AC
1966 chip = desc->chip;
1967 offset = gpio_chip_hwgpio(desc);
372e722e
AC
1968 value = chip->get ? chip->get(chip, offset) : 0;
1969 trace_gpio_value(desc_to_gpio(desc), 1, value);
3f397c21 1970 return value;
d2876d08 1971}
372e722e 1972
d2876d08 1973/**
79a9becd
AC
1974 * gpiod_get_raw_value() - return a gpio's raw value
1975 * @desc: gpio whose value will be returned
d2876d08 1976 *
79a9becd
AC
1977 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1978 * its ACTIVE_LOW status.
1979 *
1980 * This function should be called from contexts where we cannot sleep, and will
1981 * complain if the GPIO chip functions potentially sleep.
d2876d08 1982 */
79a9becd 1983int gpiod_get_raw_value(const struct gpio_desc *desc)
d2876d08 1984{
bcabdef1
AC
1985 if (!desc)
1986 return 0;
e4e449e8 1987 /* Should be using gpio_get_value_cansleep() */
d8e0ac08 1988 WARN_ON(desc->chip->can_sleep);
79a9becd 1989 return _gpiod_get_raw_value(desc);
d2876d08 1990}
79a9becd 1991EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
372e722e 1992
79a9becd
AC
1993/**
1994 * gpiod_get_value() - return a gpio's value
1995 * @desc: gpio whose value will be returned
1996 *
1997 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1998 * account.
1999 *
2000 * This function should be called from contexts where we cannot sleep, and will
2001 * complain if the GPIO chip functions potentially sleep.
2002 */
2003int gpiod_get_value(const struct gpio_desc *desc)
372e722e 2004{
79a9becd
AC
2005 int value;
2006 if (!desc)
2007 return 0;
2008 /* Should be using gpio_get_value_cansleep() */
2009 WARN_ON(desc->chip->can_sleep);
2010
2011 value = _gpiod_get_raw_value(desc);
2012 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2013 value = !value;
2014
2015 return value;
372e722e 2016}
79a9becd 2017EXPORT_SYMBOL_GPL(gpiod_get_value);
d2876d08 2018
aca5ce14
LD
2019/*
2020 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
79a9becd 2021 * @desc: gpio descriptor whose state need to be set.
aca5ce14
LD
2022 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
2023 */
372e722e 2024static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
aca5ce14
LD
2025{
2026 int err = 0;
372e722e
AC
2027 struct gpio_chip *chip = desc->chip;
2028 int offset = gpio_chip_hwgpio(desc);
2029
aca5ce14 2030 if (value) {
372e722e 2031 err = chip->direction_input(chip, offset);
aca5ce14 2032 if (!err)
372e722e 2033 clear_bit(FLAG_IS_OUT, &desc->flags);
aca5ce14 2034 } else {
372e722e 2035 err = chip->direction_output(chip, offset, 0);
aca5ce14 2036 if (!err)
372e722e 2037 set_bit(FLAG_IS_OUT, &desc->flags);
aca5ce14 2038 }
372e722e 2039 trace_gpio_direction(desc_to_gpio(desc), value, err);
aca5ce14 2040 if (err < 0)
6424de5a
MB
2041 gpiod_err(desc,
2042 "%s: Error in set_value for open drain err %d\n",
2043 __func__, err);
aca5ce14
LD
2044}
2045
25553ff0 2046/*
79a9becd
AC
2047 * _gpio_set_open_source_value() - Set the open source gpio's value.
2048 * @desc: gpio descriptor whose state need to be set.
25553ff0
LD
2049 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
2050 */
372e722e 2051static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
25553ff0
LD
2052{
2053 int err = 0;
372e722e
AC
2054 struct gpio_chip *chip = desc->chip;
2055 int offset = gpio_chip_hwgpio(desc);
2056
25553ff0 2057 if (value) {
372e722e 2058 err = chip->direction_output(chip, offset, 1);
25553ff0 2059 if (!err)
372e722e 2060 set_bit(FLAG_IS_OUT, &desc->flags);
25553ff0 2061 } else {
372e722e 2062 err = chip->direction_input(chip, offset);
25553ff0 2063 if (!err)
372e722e 2064 clear_bit(FLAG_IS_OUT, &desc->flags);
25553ff0 2065 }
372e722e 2066 trace_gpio_direction(desc_to_gpio(desc), !value, err);
25553ff0 2067 if (err < 0)
6424de5a
MB
2068 gpiod_err(desc,
2069 "%s: Error in set_value for open source err %d\n",
2070 __func__, err);
25553ff0
LD
2071}
2072
79a9becd 2073static void _gpiod_set_raw_value(struct gpio_desc *desc, int value)
d2876d08
DB
2074{
2075 struct gpio_chip *chip;
2076
372e722e 2077 chip = desc->chip;
372e722e
AC
2078 trace_gpio_value(desc_to_gpio(desc), 0, value);
2079 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2080 _gpio_set_open_drain_value(desc, value);
2081 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2082 _gpio_set_open_source_value(desc, value);
aca5ce14 2083 else
372e722e
AC
2084 chip->set(chip, gpio_chip_hwgpio(desc), value);
2085}
2086
d2876d08 2087/**
79a9becd
AC
2088 * gpiod_set_raw_value() - assign a gpio's raw value
2089 * @desc: gpio whose value will be assigned
d2876d08 2090 * @value: value to assign
d2876d08 2091 *
79a9becd
AC
2092 * Set the raw value of the GPIO, i.e. the value of its physical line without
2093 * regard for its ACTIVE_LOW status.
2094 *
2095 * This function should be called from contexts where we cannot sleep, and will
2096 * complain if the GPIO chip functions potentially sleep.
d2876d08 2097 */
79a9becd 2098void gpiod_set_raw_value(struct gpio_desc *desc, int value)
372e722e 2099{
bcabdef1
AC
2100 if (!desc)
2101 return;
e4e449e8 2102 /* Should be using gpio_set_value_cansleep() */
d8e0ac08 2103 WARN_ON(desc->chip->can_sleep);
79a9becd 2104 _gpiod_set_raw_value(desc, value);
d2876d08 2105}
79a9becd 2106EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
d2876d08
DB
2107
2108/**
79a9becd
AC
2109 * gpiod_set_value() - assign a gpio's value
2110 * @desc: gpio whose value will be assigned
2111 * @value: value to assign
2112 *
2113 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2114 * account
d2876d08 2115 *
79a9becd
AC
2116 * This function should be called from contexts where we cannot sleep, and will
2117 * complain if the GPIO chip functions potentially sleep.
d2876d08 2118 */
79a9becd 2119void gpiod_set_value(struct gpio_desc *desc, int value)
d2876d08 2120{
bcabdef1 2121 if (!desc)
79a9becd
AC
2122 return;
2123 /* Should be using gpio_set_value_cansleep() */
2124 WARN_ON(desc->chip->can_sleep);
2125 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2126 value = !value;
2127 _gpiod_set_raw_value(desc, value);
372e722e 2128}
79a9becd 2129EXPORT_SYMBOL_GPL(gpiod_set_value);
d2876d08 2130
d2876d08 2131/**
79a9becd
AC
2132 * gpiod_cansleep() - report whether gpio value access may sleep
2133 * @desc: gpio to check
d2876d08 2134 *
d2876d08 2135 */
79a9becd 2136int gpiod_cansleep(const struct gpio_desc *desc)
372e722e 2137{
bcabdef1
AC
2138 if (!desc)
2139 return 0;
372e722e 2140 return desc->chip->can_sleep;
d2876d08 2141}
79a9becd 2142EXPORT_SYMBOL_GPL(gpiod_cansleep);
d2876d08 2143
0f6d504e 2144/**
79a9becd
AC
2145 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2146 * @desc: gpio whose IRQ will be returned (already requested)
0f6d504e 2147 *
79a9becd
AC
2148 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2149 * error.
0f6d504e 2150 */
79a9becd 2151int gpiod_to_irq(const struct gpio_desc *desc)
0f6d504e
DB
2152{
2153 struct gpio_chip *chip;
372e722e 2154 int offset;
0f6d504e 2155
bcabdef1
AC
2156 if (!desc)
2157 return -EINVAL;
372e722e
AC
2158 chip = desc->chip;
2159 offset = gpio_chip_hwgpio(desc);
2160 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
0f6d504e 2161}
79a9becd 2162EXPORT_SYMBOL_GPL(gpiod_to_irq);
0f6d504e 2163
d468bf9e
LW
2164/**
2165 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
2166 * @gpio: the GPIO line to lock as used for IRQ
2167 *
2168 * This is used directly by GPIO drivers that want to lock down
2169 * a certain GPIO line to be used as IRQs, for example in the
2170 * .to_irq() callback of their gpio_chip, or in the .irq_enable()
2171 * of its irq_chip implementation if the GPIO is known from that
2172 * code.
2173 */
79a9becd 2174int gpiod_lock_as_irq(struct gpio_desc *desc)
372e722e 2175{
d468bf9e
LW
2176 if (!desc)
2177 return -EINVAL;
2178
2179 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
2180 gpiod_err(desc,
2181 "%s: tried to flag a GPIO set as output for IRQ\n",
2182 __func__);
2183 return -EIO;
2184 }
2185
2186 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
2187 return 0;
372e722e 2188}
79a9becd 2189EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);
d2876d08 2190
d468bf9e
LW
2191int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
2192{
2193 return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset));
2194}
2195EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
d2876d08 2196
d468bf9e
LW
2197/**
2198 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
2199 * @gpio: the GPIO line to unlock from IRQ usage
2200 *
2201 * This is used directly by GPIO drivers that want to indicate
2202 * that a certain GPIO is no longer used exclusively for IRQ.
d2876d08 2203 */
79a9becd 2204void gpiod_unlock_as_irq(struct gpio_desc *desc)
d468bf9e
LW
2205{
2206 if (!desc)
2207 return;
d2876d08 2208
d468bf9e
LW
2209 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2210}
79a9becd 2211EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq);
d468bf9e
LW
2212
2213void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2214{
2215 return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset));
2216}
2217EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
d2876d08 2218
79a9becd
AC
2219/**
2220 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2221 * @desc: gpio whose value will be returned
2222 *
2223 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2224 * its ACTIVE_LOW status.
2225 *
2226 * This function is to be called from contexts that can sleep.
d2876d08 2227 */
79a9becd 2228int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
d2876d08 2229{
d2876d08 2230 might_sleep_if(extra_checks);
bcabdef1
AC
2231 if (!desc)
2232 return 0;
79a9becd 2233 return _gpiod_get_raw_value(desc);
d2876d08 2234}
79a9becd 2235EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
372e722e 2236
79a9becd
AC
2237/**
2238 * gpiod_get_value_cansleep() - return a gpio's value
2239 * @desc: gpio whose value will be returned
2240 *
2241 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2242 * account.
2243 *
2244 * This function is to be called from contexts that can sleep.
2245 */
2246int gpiod_get_value_cansleep(const struct gpio_desc *desc)
d2876d08 2247{
3f397c21 2248 int value;
d2876d08
DB
2249
2250 might_sleep_if(extra_checks);
bcabdef1
AC
2251 if (!desc)
2252 return 0;
79a9becd
AC
2253
2254 value = _gpiod_get_raw_value(desc);
2255 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2256 value = !value;
2257
3f397c21 2258 return value;
d2876d08 2259}
79a9becd 2260EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
372e722e 2261
79a9becd
AC
2262/**
2263 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2264 * @desc: gpio whose value will be assigned
2265 * @value: value to assign
2266 *
2267 * Set the raw value of the GPIO, i.e. the value of its physical line without
2268 * regard for its ACTIVE_LOW status.
2269 *
2270 * This function is to be called from contexts that can sleep.
2271 */
2272void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
372e722e 2273{
d2876d08 2274 might_sleep_if(extra_checks);
bcabdef1
AC
2275 if (!desc)
2276 return;
79a9becd 2277 _gpiod_set_raw_value(desc, value);
372e722e 2278}
79a9becd 2279EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
d2876d08 2280
79a9becd
AC
2281/**
2282 * gpiod_set_value_cansleep() - assign a gpio's value
2283 * @desc: gpio whose value will be assigned
2284 * @value: value to assign
2285 *
2286 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2287 * account
2288 *
2289 * This function is to be called from contexts that can sleep.
2290 */
2291void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
d2876d08 2292{
d2876d08 2293 might_sleep_if(extra_checks);
bcabdef1
AC
2294 if (!desc)
2295 return;
79a9becd
AC
2296
2297 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2298 value = !value;
2299 _gpiod_set_raw_value(desc, value);
372e722e 2300}
79a9becd 2301EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
d2876d08 2302
bae48da2 2303/**
ad824783
AC
2304 * gpiod_add_lookup_table() - register GPIO device consumers
2305 * @table: table of consumers to register
bae48da2 2306 */
ad824783 2307void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
bae48da2
AC
2308{
2309 mutex_lock(&gpio_lookup_lock);
2310
ad824783 2311 list_add_tail(&table->list, &gpio_lookup_list);
bae48da2
AC
2312
2313 mutex_unlock(&gpio_lookup_lock);
2314}
2315
bae48da2
AC
2316#ifdef CONFIG_OF
2317static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
53e7cac3
AC
2318 unsigned int idx,
2319 enum gpio_lookup_flags *flags)
bae48da2
AC
2320{
2321 char prop_name[32]; /* 32 is max size of property name */
2322 enum of_gpio_flags of_flags;
2323 struct gpio_desc *desc;
2324
2325 if (con_id)
2326 snprintf(prop_name, 32, "%s-gpios", con_id);
aca5ce14 2327 else
bae48da2
AC
2328 snprintf(prop_name, 32, "gpios");
2329
2330 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2331 &of_flags);
2332
2333 if (IS_ERR(desc))
2334 return desc;
2335
2336 if (of_flags & OF_GPIO_ACTIVE_LOW)
53e7cac3 2337 *flags |= GPIO_ACTIVE_LOW;
bae48da2
AC
2338
2339 return desc;
2340}
2341#else
2342static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
209e64f9
AC
2343 unsigned int idx,
2344 enum gpio_lookup_flags *flags)
bae48da2
AC
2345{
2346 return ERR_PTR(-ENODEV);
d2876d08 2347}
bae48da2 2348#endif
d2876d08 2349
81f59e9d 2350static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
53e7cac3
AC
2351 unsigned int idx,
2352 enum gpio_lookup_flags *flags)
372e722e 2353{
e01f440a
MW
2354 struct acpi_gpio_info info;
2355 struct gpio_desc *desc;
2356
2357 desc = acpi_get_gpiod_by_index(dev, idx, &info);
2358 if (IS_ERR(desc))
2359 return desc;
2360
2361 if (info.gpioint && info.active_low)
53e7cac3 2362 *flags |= GPIO_ACTIVE_LOW;
e01f440a
MW
2363
2364 return desc;
81f59e9d
MW
2365}
2366
ad824783 2367static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
bae48da2
AC
2368{
2369 const char *dev_id = dev ? dev_name(dev) : NULL;
ad824783 2370 struct gpiod_lookup_table *table;
bae48da2
AC
2371
2372 mutex_lock(&gpio_lookup_lock);
2373
ad824783
AC
2374 list_for_each_entry(table, &gpio_lookup_list, list) {
2375 if (table->dev_id && dev_id) {
2376 /*
2377 * Valid strings on both ends, must be identical to have
2378 * a match
2379 */
2380 if (!strcmp(table->dev_id, dev_id))
2381 goto found;
2382 } else {
2383 /*
2384 * One of the pointers is NULL, so both must be to have
2385 * a match
2386 */
2387 if (dev_id == table->dev_id)
2388 goto found;
2389 }
2390 }
2391 table = NULL;
bae48da2 2392
ad824783
AC
2393found:
2394 mutex_unlock(&gpio_lookup_lock);
2395 return table;
2396}
bae48da2 2397
ad824783
AC
2398static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
2399 unsigned int idx,
2400 enum gpio_lookup_flags *flags)
2401{
2a3cf6a3 2402 struct gpio_desc *desc = ERR_PTR(-ENOENT);
ad824783
AC
2403 struct gpiod_lookup_table *table;
2404 struct gpiod_lookup *p;
bae48da2 2405
ad824783
AC
2406 table = gpiod_find_lookup_table(dev);
2407 if (!table)
2408 return desc;
bae48da2 2409
ad824783
AC
2410 for (p = &table->table[0]; p->chip_label; p++) {
2411 struct gpio_chip *chip;
bae48da2 2412
ad824783 2413 /* idx must always match exactly */
bae48da2
AC
2414 if (p->idx != idx)
2415 continue;
2416
ad824783
AC
2417 /* If the lookup entry has a con_id, require exact match */
2418 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2419 continue;
bae48da2 2420
ad824783 2421 chip = find_chip_by_name(p->chip_label);
bae48da2 2422
ad824783 2423 if (!chip) {
2a3cf6a3
AC
2424 dev_err(dev, "cannot find GPIO chip %s\n",
2425 p->chip_label);
2426 return ERR_PTR(-ENODEV);
ad824783 2427 }
bae48da2 2428
ad824783 2429 if (chip->ngpio <= p->chip_hwnum) {
2a3cf6a3
AC
2430 dev_err(dev,
2431 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2432 idx, chip->ngpio, chip->label);
2433 return ERR_PTR(-EINVAL);
bae48da2 2434 }
bae48da2 2435
ad824783
AC
2436 desc = gpiochip_offset_to_desc(chip, p->chip_hwnum);
2437 *flags = p->flags;
bae48da2 2438
2a3cf6a3 2439 return desc;
bae48da2
AC
2440 }
2441
bae48da2
AC
2442 return desc;
2443}
2444
2445/**
2446 * gpio_get - obtain a GPIO for a given GPIO function
ad824783 2447 * @dev: GPIO consumer, can be NULL for system-global GPIOs
bae48da2
AC
2448 * @con_id: function within the GPIO consumer
2449 *
2450 * Return the GPIO descriptor corresponding to the function con_id of device
2a3cf6a3
AC
2451 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
2452 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
bae48da2
AC
2453 */
2454struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id)
2455{
2456 return gpiod_get_index(dev, con_id, 0);
2457}
2458EXPORT_SYMBOL_GPL(gpiod_get);
2459
2460/**
2461 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
fdd6a5fe 2462 * @dev: GPIO consumer, can be NULL for system-global GPIOs
bae48da2
AC
2463 * @con_id: function within the GPIO consumer
2464 * @idx: index of the GPIO to obtain in the consumer
2465 *
2466 * This variant of gpiod_get() allows to access GPIOs other than the first
2467 * defined one for functions that define several GPIOs.
2468 *
2a3cf6a3
AC
2469 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2470 * requested function and/or index, or another IS_ERR() code if an error
2471 * occured while trying to acquire the GPIO.
bae48da2
AC
2472 */
2473struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2474 const char *con_id,
2475 unsigned int idx)
2476{
35c5d7fd 2477 struct gpio_desc *desc = NULL;
bae48da2 2478 int status;
53e7cac3 2479 enum gpio_lookup_flags flags = 0;
bae48da2
AC
2480
2481 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2482
2483 /* Using device tree? */
2484 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
2485 dev_dbg(dev, "using device tree for GPIO lookup\n");
2486 desc = of_find_gpio(dev, con_id, idx, &flags);
81f59e9d
MW
2487 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
2488 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2489 desc = acpi_find_gpio(dev, con_id, idx, &flags);
35c5d7fd
AC
2490 }
2491
2492 /*
2493 * Either we are not using DT or ACPI, or their lookup did not return
2494 * a result. In that case, use platform lookup as a fallback.
2495 */
2a3cf6a3 2496 if (!desc || desc == ERR_PTR(-ENOENT)) {
bae48da2 2497 dev_dbg(dev, "using lookup tables for GPIO lookup");
2a3cf6a3 2498 desc = gpiod_find(dev, con_id, idx, &flags);
bae48da2
AC
2499 }
2500
2501 if (IS_ERR(desc)) {
351cfe0f 2502 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
bae48da2
AC
2503 return desc;
2504 }
2505
2506 status = gpiod_request(desc, con_id);
2507
2508 if (status < 0)
2509 return ERR_PTR(status);
2510
53e7cac3 2511 if (flags & GPIO_ACTIVE_LOW)
bae48da2 2512 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
53e7cac3
AC
2513 if (flags & GPIO_OPEN_DRAIN)
2514 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2515 if (flags & GPIO_OPEN_SOURCE)
2516 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
bae48da2
AC
2517
2518 return desc;
2519}
2520EXPORT_SYMBOL_GPL(gpiod_get_index);
2521
2522/**
2523 * gpiod_put - dispose of a GPIO descriptor
2524 * @desc: GPIO descriptor to dispose of
2525 *
2526 * No descriptor can be used after gpiod_put() has been called on it.
2527 */
2528void gpiod_put(struct gpio_desc *desc)
2529{
2530 gpiod_free(desc);
372e722e 2531}
bae48da2 2532EXPORT_SYMBOL_GPL(gpiod_put);
d2876d08
DB
2533
2534#ifdef CONFIG_DEBUG_FS
2535
d2876d08
DB
2536static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2537{
2538 unsigned i;
2539 unsigned gpio = chip->base;
6c0b4e6c 2540 struct gpio_desc *gdesc = &chip->desc[0];
d2876d08 2541 int is_out;
d468bf9e 2542 int is_irq;
d2876d08
DB
2543
2544 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2545 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2546 continue;
2547
372e722e 2548 gpiod_get_direction(gdesc);
d2876d08 2549 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
d468bf9e
LW
2550 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2551 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
d2876d08
DB
2552 gpio, gdesc->label,
2553 is_out ? "out" : "in ",
2554 chip->get
2555 ? (chip->get(chip, i) ? "hi" : "lo")
d468bf9e
LW
2556 : "? ",
2557 is_irq ? "IRQ" : " ");
d2876d08
DB
2558 seq_printf(s, "\n");
2559 }
2560}
2561
f9c4a31f 2562static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
d2876d08 2563{
362432ae 2564 unsigned long flags;
f9c4a31f 2565 struct gpio_chip *chip = NULL;
cb1650d4 2566 loff_t index = *pos;
d2876d08 2567
f9c4a31f 2568 s->private = "";
d2876d08 2569
362432ae 2570 spin_lock_irqsave(&gpio_lock, flags);
cb1650d4 2571 list_for_each_entry(chip, &gpio_chips, list)
362432ae
GL
2572 if (index-- == 0) {
2573 spin_unlock_irqrestore(&gpio_lock, flags);
cb1650d4 2574 return chip;
f9c4a31f 2575 }
362432ae 2576 spin_unlock_irqrestore(&gpio_lock, flags);
f9c4a31f 2577
cb1650d4 2578 return NULL;
f9c4a31f
TR
2579}
2580
2581static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2582{
362432ae 2583 unsigned long flags;
f9c4a31f 2584 struct gpio_chip *chip = v;
f9c4a31f
TR
2585 void *ret = NULL;
2586
362432ae 2587 spin_lock_irqsave(&gpio_lock, flags);
cb1650d4
AC
2588 if (list_is_last(&chip->list, &gpio_chips))
2589 ret = NULL;
2590 else
2591 ret = list_entry(chip->list.next, struct gpio_chip, list);
362432ae 2592 spin_unlock_irqrestore(&gpio_lock, flags);
f9c4a31f
TR
2593
2594 s->private = "\n";
2595 ++*pos;
2596
2597 return ret;
2598}
2599
2600static void gpiolib_seq_stop(struct seq_file *s, void *v)
2601{
2602}
2603
2604static int gpiolib_seq_show(struct seq_file *s, void *v)
2605{
2606 struct gpio_chip *chip = v;
2607 struct device *dev;
2608
2609 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2610 chip->base, chip->base + chip->ngpio - 1);
2611 dev = chip->dev;
2612 if (dev)
2613 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2614 dev_name(dev));
2615 if (chip->label)
2616 seq_printf(s, ", %s", chip->label);
2617 if (chip->can_sleep)
2618 seq_printf(s, ", can sleep");
2619 seq_printf(s, ":\n");
2620
2621 if (chip->dbg_show)
2622 chip->dbg_show(s, chip);
2623 else
2624 gpiolib_dbg_show(s, chip);
2625
d2876d08
DB
2626 return 0;
2627}
2628
f9c4a31f
TR
2629static const struct seq_operations gpiolib_seq_ops = {
2630 .start = gpiolib_seq_start,
2631 .next = gpiolib_seq_next,
2632 .stop = gpiolib_seq_stop,
2633 .show = gpiolib_seq_show,
2634};
2635
d2876d08
DB
2636static int gpiolib_open(struct inode *inode, struct file *file)
2637{
f9c4a31f 2638 return seq_open(file, &gpiolib_seq_ops);
d2876d08
DB
2639}
2640
828c0950 2641static const struct file_operations gpiolib_operations = {
f9c4a31f 2642 .owner = THIS_MODULE,
d2876d08
DB
2643 .open = gpiolib_open,
2644 .read = seq_read,
2645 .llseek = seq_lseek,
f9c4a31f 2646 .release = seq_release,
d2876d08
DB
2647};
2648
2649static int __init gpiolib_debugfs_init(void)
2650{
2651 /* /sys/kernel/debug/gpio */
2652 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2653 NULL, NULL, &gpiolib_operations);
2654 return 0;
2655}
2656subsys_initcall(gpiolib_debugfs_init);
2657
2658#endif /* DEBUG_FS */