blob: 50c4922fe53abf8f51cf1d613d59a9d5207bc002 [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07007#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>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
David Brownelld2876d02008-02-04 22:28:20 -080017
Mika Westerberg664e3e52014-01-08 12:40:54 +020018#include "gpiolib.h"
19
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060020#define CREATE_TRACE_POINTS
21#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080022
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070023/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080024 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070025 * 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.
David Brownelld2876d02008-02-04 22:28:20 -080028 */
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
Alexandre Courbot710b40e2013-02-02 23:44:06 +090055#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 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070059#define FLAG_ACTIVE_LOW 6 /* value has active low */
Alexandre Courbot710b40e2013-02-02 23:44:06 +090060#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
61#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
Linus Walleijd468bf92013-09-24 11:54:38 +020062#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070063
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070064#define ID_SHIFT 16 /* add new flags before this one */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070065
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070066#define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
Daniel Glöcknerff77c352009-09-22 16:46:38 -070067#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
David Brownelld2876d02008-02-04 22:28:20 -080068
69#ifdef CONFIG_DEBUG_FS
70 const char *label;
71#endif
72};
73static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
74
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +090075#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
76
Alexandre Courbotbae48da2013-10-17 10:21:38 -070077static DEFINE_MUTEX(gpio_lookup_lock);
78static LIST_HEAD(gpio_lookup_list);
Alexandre Courbot1a989d02013-02-03 01:29:24 +090079static LIST_HEAD(gpio_chips);
80
Daniel Glöcknerff77c352009-09-22 16:46:38 -070081#ifdef CONFIG_GPIO_SYSFS
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070082static DEFINE_IDR(dirent_idr);
Daniel Glöcknerff77c352009-09-22 16:46:38 -070083#endif
84
Alexandre Courbot372e7222013-02-03 01:29:29 +090085static int gpiod_request(struct gpio_desc *desc, const char *label);
86static void gpiod_free(struct gpio_desc *desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090087
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +020088/* With descriptor prefix */
89
Mark Brown7b17b592013-09-09 10:33:50 +010090#ifdef CONFIG_DEBUG_FS
Andy Shevchenko7589e592013-12-05 11:26:23 +020091#define gpiod_emerg(desc, fmt, ...) \
92 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
Mark Brown7b17b592013-09-09 10:33:50 +010093 ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +020094#define gpiod_crit(desc, fmt, ...) \
95 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
Mark Brown7b17b592013-09-09 10:33:50 +010096 ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +020097#define gpiod_err(desc, fmt, ...) \
98 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
Mark Brown7b17b592013-09-09 10:33:50 +010099 ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200100#define gpiod_warn(desc, fmt, ...) \
101 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
Mark Brown7b17b592013-09-09 10:33:50 +0100102 ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200103#define gpiod_info(desc, fmt, ...) \
104 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
Mark Brown7b17b592013-09-09 10:33:50 +0100105 ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200106#define gpiod_dbg(desc, fmt, ...) \
107 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
Mark Brown7b17b592013-09-09 10:33:50 +0100108 ##__VA_ARGS__)
109#else
Andy Shevchenko7589e592013-12-05 11:26:23 +0200110#define gpiod_emerg(desc, fmt, ...) \
Mark Brown6424de52013-09-09 10:33:49 +0100111 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200112#define gpiod_crit(desc, fmt, ...) \
Mark Brown6424de52013-09-09 10:33:49 +0100113 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200114#define gpiod_err(desc, fmt, ...) \
Mark Brown6424de52013-09-09 10:33:49 +0100115 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200116#define gpiod_warn(desc, fmt, ...) \
Mark Brown6424de52013-09-09 10:33:49 +0100117 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200118#define gpiod_info(desc, fmt, ...) \
Mark Brown6424de52013-09-09 10:33:49 +0100119 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200120#define gpiod_dbg(desc, fmt, ...) \
Mark Brown6424de52013-09-09 10:33:49 +0100121 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Mark Brown7b17b592013-09-09 10:33:50 +0100122#endif
Alexandre Courbot372e7222013-02-03 01:29:29 +0900123
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200124/* 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
David Brownelld2876d02008-02-04 22:28:20 -0800139static 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
Alexandre Courbot372e7222013-02-03 01:29:29 +0900146/*
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{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900151 return desc - &desc->chip->desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900152}
153
154/**
155 * Convert a GPIO number to its descriptor
156 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700157struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900158{
159 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
160 return NULL;
161 else
162 return &gpio_desc[gpio];
163}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700164EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900165
166/**
Linus Walleijd468bf92013-09-24 11:54:38 +0200167 * 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{
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900172 if (offset >= chip->ngpio)
173 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200174
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900175 return &chip->desc[offset];
Linus Walleijd468bf92013-09-24 11:54:38 +0200176}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900177
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 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700183int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900184{
Alexandre Courbot8c0fca82013-10-04 10:59:57 -0700185 return desc - &gpio_desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900186}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700187EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900188
189
David Brownelld2876d02008-02-04 22:28:20 -0800190/* 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
David Brownell35e8bb52008-10-15 22:03:16 -0700194 * message should motivate switching to explicit requests... so should
195 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -0700196 *
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.
David Brownelld2876d02008-02-04 22:28:20 -0800200 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900201static int gpio_ensure_requested(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800202{
David Brownell8a0cecf2009-04-02 16:57:06 -0700203 const struct gpio_chip *chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900204 const int gpio = desc_to_gpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -0700205
David Brownell8a0cecf2009-04-02 16:57:06 -0700206 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
207 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700208 if (!try_module_get(chip->owner)) {
Andy Shevchenko7589e592013-12-05 11:26:23 +0200209 gpiod_err(desc, "%s: module can't be gotten\n",
210 __func__);
David Brownell35e8bb52008-10-15 22:03:16 -0700211 clear_bit(FLAG_REQUESTED, &desc->flags);
212 /* lose */
213 return -EIO;
214 }
David Brownelld2876d02008-02-04 22:28:20 -0800215 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700216 /* caller must chip->request() w/o spinlock */
217 if (chip->request)
218 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800219 }
David Brownell35e8bb52008-10-15 22:03:16 -0700220 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800221}
222
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700223/**
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)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900228{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900229 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900230}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700231EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800232
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700233/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
234static int gpiochip_find_base(int ngpio)
235{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900236 struct gpio_chip *chip;
237 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700238
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900239 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;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700246 }
247
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900248 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700249 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900250 return base;
251 } else {
252 pr_err("%s: cannot find free range\n", __func__);
253 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700254 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700255}
256
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700257/**
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)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300266{
267 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900268 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300269 int status = -EINVAL;
270
Alexandre Courbot372e7222013-02-03 01:29:29 +0900271 chip = gpiod_to_chip(desc);
272 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300273
274 if (!chip->get_direction)
275 return status;
276
Alexandre Courbot372e7222013-02-03 01:29:29 +0900277 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300278 if (status > 0) {
279 /* GPIOF_DIR_IN, or other positive */
280 status = 1;
Alexandre Courbotdef63432013-02-15 14:46:15 +0900281 /* 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);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300284 }
285 if (status == 0) {
286 /* GPIOF_DIR_OUT */
Alexandre Courbotdef63432013-02-15 14:46:15 +0900287 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300288 }
289 return status;
290}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700291EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300292
David Brownelld8f388d82008-07-25 01:46:07 -0700293#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
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700310 * /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"
Jani Nikula07697462009-12-15 16:46:20 -0800314 * /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
David Brownelld8f388d82008-07-25 01:46:07 -0700319 */
320
321static ssize_t gpio_direction_show(struct device *dev,
322 struct device_attribute *attr, char *buf)
323{
Alexandre Courbotdef63432013-02-15 14:46:15 +0900324 const struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700325 ssize_t status;
326
327 mutex_lock(&sysfs_lock);
328
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900329 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700330 status = -EIO;
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900331 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +0900332 gpiod_get_direction(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700333 status = sprintf(buf, "%s\n",
334 test_bit(FLAG_IS_OUT, &desc->flags)
335 ? "out" : "in");
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900336 }
David Brownelld8f388d82008-07-25 01:46:07 -0700337
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{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900345 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700346 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"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900353 status = gpiod_direction_output(desc, 1);
David Brownelld8f388d82008-07-25 01:46:07 -0700354 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900355 status = gpiod_direction_output(desc, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700356 else if (sysfs_streq(buf, "in"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900357 status = gpiod_direction_input(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700358 else
359 status = -EINVAL;
360
361 mutex_unlock(&sysfs_lock);
362 return status ? : size;
363}
364
Jani Nikula07697462009-12-15 16:46:20 -0800365static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700366 gpio_direction_show, gpio_direction_store);
367
368static ssize_t gpio_value_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
370{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900371 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700372 ssize_t status;
373
374 mutex_lock(&sysfs_lock);
375
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700376 if (!test_bit(FLAG_EXPORT, &desc->flags))
David Brownelld8f388d82008-07-25 01:46:07 -0700377 status = -EIO;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700378 else
379 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
David Brownelld8f388d82008-07-25 01:46:07 -0700380
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{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900388 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700389 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
Jingoo Hana3d88c92013-07-19 16:12:50 +0900400 status = kstrtol(buf, 0, &value);
David Brownelld8f388d82008-07-25 01:46:07 -0700401 if (status == 0) {
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700402 gpiod_set_value_cansleep(desc, value);
David Brownelld8f388d82008-07-25 01:46:07 -0700403 status = size;
404 }
405 }
406
407 mutex_unlock(&sysfs_lock);
408 return status;
409}
410
Jani Nikula07697462009-12-15 16:46:20 -0800411static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700412 gpio_value_show, gpio_value_store);
413
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700414static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
415{
Tejun Heo324a56e2013-12-11 14:11:53 -0500416 struct kernfs_node *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700417
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700418 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700419 return IRQ_HANDLED;
420}
421
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700422static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
423 unsigned long gpio_flags)
424{
Tejun Heo324a56e2013-12-11 14:11:53 -0500425 struct kernfs_node *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700426 unsigned long irq_flags;
427 int ret, irq, id;
428
429 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
430 return 0;
431
Alexandre Courbot372e7222013-02-03 01:29:29 +0900432 irq = gpiod_to_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700433 if (irq < 0)
434 return -EIO;
435
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700436 id = desc->flags >> ID_SHIFT;
437 value_sd = idr_find(&dirent_idr, id);
438 if (value_sd)
439 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700440
441 desc->flags &= ~GPIO_TRIGGER_MASK;
442
443 if (!gpio_flags) {
Linus Walleijd468bf92013-09-24 11:54:38 +0200444 gpiod_unlock_as_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700445 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700446 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700447 }
448
449 irq_flags = IRQF_SHARED;
450 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800451 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
452 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700453 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800454 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
455 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700456
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700457 if (!value_sd) {
Tejun Heo388975c2013-09-11 23:19:13 -0400458 value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700459 if (!value_sd) {
460 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700461 goto err_out;
462 }
463
Tejun Heo62f516b2013-02-27 17:04:06 -0800464 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
465 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700466 goto free_sd;
Tejun Heo62f516b2013-02-27 17:04:06 -0800467 id = ret;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700468
469 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700470 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700471
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700472 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700473 ret = -ERANGE;
474 goto free_id;
475 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700476 }
477
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700478 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700479 "gpiolib", value_sd);
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700480 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700481 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700482
Linus Walleijd468bf92013-09-24 11:54:38 +0200483 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
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700489 desc->flags |= gpio_flags;
490 return 0;
491
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700492free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700493 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700494 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700495free_sd:
496 if (value_sd)
497 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700498err_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
Jani Nikula07697462009-12-15 16:46:20 -0800569static 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
Jingoo Hana3d88c92013-07-19 16:12:50 +0900626 status = kstrtol(buf, 0, &value);
Jani Nikula07697462009-12-15 16:46:20 -0800627 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
David Brownelld8f388d82008-07-25 01:46:07 -0700639static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d82008-07-25 01:46:07 -0700640 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800641 &dev_attr_active_low.attr,
David Brownelld8f388d82008-07-25 01:46:07 -0700642 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 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100700static ssize_t export_store(struct class *class,
701 struct class_attribute *attr,
702 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700703{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900704 long gpio;
705 struct gpio_desc *desc;
706 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700707
Jingoo Hana3d88c92013-07-19 16:12:50 +0900708 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700709 if (status < 0)
710 goto done;
711
Alexandre Courbot372e7222013-02-03 01:29:29 +0900712 desc = gpio_to_desc(gpio);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900713 /* reject invalid GPIOs */
714 if (!desc) {
715 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
716 return -EINVAL;
717 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900718
David Brownelld8f388d82008-07-25 01:46:07 -0700719 /* 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
Alexandre Courbot372e7222013-02-03 01:29:29 +0900724 status = gpiod_request(desc, "sysfs");
Mathias Nymanad2fab32012-10-25 14:03:03 +0300725 if (status < 0) {
726 if (status == -EPROBE_DEFER)
727 status = -ENODEV;
David Brownelld8f388d82008-07-25 01:46:07 -0700728 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +0300729 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900730 status = gpiod_export(desc, true);
David Brownelld8f388d82008-07-25 01:46:07 -0700731 if (status < 0)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900732 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700733 else
Alexandre Courbot372e7222013-02-03 01:29:29 +0900734 set_bit(FLAG_SYSFS, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700735
736done:
737 if (status)
738 pr_debug("%s: status %d\n", __func__, status);
739 return status ? : len;
740}
741
Andi Kleen28812fe2010-01-05 12:48:07 +0100742static ssize_t unexport_store(struct class *class,
743 struct class_attribute *attr,
744 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700745{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900746 long gpio;
747 struct gpio_desc *desc;
748 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700749
Jingoo Hana3d88c92013-07-19 16:12:50 +0900750 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700751 if (status < 0)
752 goto done;
753
Alexandre Courbot372e7222013-02-03 01:29:29 +0900754 desc = gpio_to_desc(gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700755 /* reject bogus commands (gpio_unexport ignores them) */
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900756 if (!desc) {
757 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
758 return -EINVAL;
759 }
760
761 status = -EINVAL;
David Brownelld8f388d82008-07-25 01:46:07 -0700762
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 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900767 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700768 status = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900769 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700770 }
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/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700792 * gpiod_export - export a GPIO through sysfs
David Brownelld8f388d82008-07-25 01:46:07 -0700793 * @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 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700806int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
David Brownelld8f388d82008-07-25 01:46:07 -0700807{
808 unsigned long flags;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100809 int status;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700810 const char *ioname = NULL;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100811 struct device *dev;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900812 int offset;
David Brownelld8f388d82008-07-25 01:46:07 -0700813
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
Alexandre Courbot372e7222013-02-03 01:29:29 +0900820 if (!desc) {
821 pr_debug("%s: invalid gpio descriptor\n", __func__);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100822 return -EINVAL;
823 }
David Brownelld8f388d82008-07-25 01:46:07 -0700824
825 mutex_lock(&sysfs_lock);
826
827 spin_lock_irqsave(&gpio_lock, flags);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100828 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
829 test_bit(FLAG_EXPORT, &desc->flags)) {
830 spin_unlock_irqrestore(&gpio_lock, flags);
Andy Shevchenko7589e592013-12-05 11:26:23 +0200831 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
832 __func__,
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100833 test_bit(FLAG_REQUESTED, &desc->flags),
834 test_bit(FLAG_EXPORT, &desc->flags));
Dan Carpenter529f2ad2012-10-26 09:59:43 +0300835 status = -EPERM;
836 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700837 }
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100838
839 if (!desc->chip->direction_input || !desc->chip->direction_output)
840 direction_may_change = false;
David Brownelld8f388d82008-07-25 01:46:07 -0700841 spin_unlock_irqrestore(&gpio_lock, flags);
842
Alexandre Courbot372e7222013-02-03 01:29:29 +0900843 offset = gpio_chip_hwgpio(desc);
844 if (desc->chip->names && desc->chip->names[offset])
845 ioname = desc->chip->names[offset];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700846
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100847 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Alexandre Courbot372e7222013-02-03 01:29:29 +0900848 desc, ioname ? ioname : "gpio%u",
849 desc_to_gpio(desc));
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100850 if (IS_ERR(dev)) {
851 status = PTR_ERR(dev);
852 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700853 }
854
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100855 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
David Brownelld8f388d82008-07-25 01:46:07 -0700856 if (status)
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100857 goto fail_unregister_device;
David Brownelld8f388d82008-07-25 01:46:07 -0700858
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100859 if (direction_may_change) {
860 status = device_create_file(dev, &dev_attr_direction);
861 if (status)
862 goto fail_unregister_device;
863 }
864
Alexandre Courbot372e7222013-02-03 01:29:29 +0900865 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100866 !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 }
871
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);
Andy Shevchenko7589e592013-12-05 11:26:23 +0200880 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
David Brownelld8f388d82008-07-25 01:46:07 -0700881 return status;
882}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700883EXPORT_SYMBOL_GPL(gpiod_export);
David Brownelld8f388d82008-07-25 01:46:07 -0700884
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100885static int match_export(struct device *dev, const void *data)
David Brownelld8f388d82008-07-25 01:46:07 -0700886{
887 return dev_get_drvdata(dev) == data;
888}
889
890/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700891 * gpiod_export_link - create a sysfs link to an exported GPIO node
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700892 * @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 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700901int gpiod_export_link(struct device *dev, const char *name,
902 struct gpio_desc *desc)
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700903{
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700904 int status = -EINVAL;
905
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900906 if (!desc) {
907 pr_warn("%s: invalid GPIO\n", __func__);
908 return -EINVAL;
909 }
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700910
911 mutex_lock(&sysfs_lock);
912
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700913 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
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700927 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200928 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700929
930 return status;
931}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700932EXPORT_SYMBOL_GPL(gpiod_export_link);
Jani Nikula07697462009-12-15 16:46:20 -0800933
934/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700935 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
Jani Nikula07697462009-12-15 16:46:20 -0800936 * @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 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700946int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
Jani Nikula07697462009-12-15 16:46:20 -0800947{
Jani Nikula07697462009-12-15 16:46:20 -0800948 struct device *dev = NULL;
949 int status = -EINVAL;
950
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900951 if (!desc) {
952 pr_warn("%s: invalid GPIO\n", __func__);
953 return -EINVAL;
954 }
Jani Nikula07697462009-12-15 16:46:20 -0800955
956 mutex_lock(&sysfs_lock);
957
Jani Nikula07697462009-12-15 16:46:20 -0800958 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800959 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
Jani Nikula07697462009-12-15 16:46:20 -0800971 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200972 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Jani Nikula07697462009-12-15 16:46:20 -0800973
974 return status;
975}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700976EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
Jani Nikula07697462009-12-15 16:46:20 -0800977
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700978/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700979 * gpiod_unexport - reverse effect of gpio_export()
David Brownelld8f388d82008-07-25 01:46:07 -0700980 * @gpio: gpio to make unavailable
981 *
982 * This is implicit on gpio_free().
983 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700984void gpiod_unexport(struct gpio_desc *desc)
David Brownelld8f388d82008-07-25 01:46:07 -0700985{
Jon Povey6a99ad42010-07-27 13:18:06 -0700986 int status = 0;
Ming Lei864533c2012-02-13 22:53:20 +0800987 struct device *dev = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -0700988
Alexandre Courbot372e7222013-02-03 01:29:29 +0900989 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900990 pr_warn("%s: invalid GPIO\n", __func__);
991 return;
Jon Povey6a99ad42010-07-27 13:18:06 -0700992 }
David Brownelld8f388d82008-07-25 01:46:07 -0700993
994 mutex_lock(&sysfs_lock);
995
David Brownelld8f388d82008-07-25 01:46:07 -0700996 if (test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700997
998 dev = class_find_device(&gpio_class, NULL, desc, match_export);
999 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -07001000 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d82008-07-25 01:46:07 -07001001 clear_bit(FLAG_EXPORT, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -07001002 } else
1003 status = -ENODEV;
1004 }
1005
1006 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001007
Ming Lei864533c2012-02-13 22:53:20 +08001008 if (dev) {
1009 device_unregister(dev);
1010 put_device(dev);
1011 }
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001012
David Brownelld8f388d82008-07-25 01:46:07 -07001013 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001014 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001015}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001016EXPORT_SYMBOL_GPL(gpiod_unexport);
David Brownelld8f388d82008-07-25 01:46:07 -07001017
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);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001035 if (!IS_ERR(dev)) {
David Brownelld8f388d82008-07-25 01:46:07 -07001036 status = sysfs_create_group(&dev->kobj,
1037 &gpiochip_attr_group);
1038 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001039 status = PTR_ERR(dev);
David Brownelld8f388d82008-07-25 01:46:07 -07001040 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);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001048 gpio = 0;
1049 while (gpio < chip->ngpio)
1050 chip->desc[gpio++].chip = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -07001051 spin_unlock_irqrestore(&gpio_lock, flags);
1052
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001053 chip_dbg(chip, "%s: status %d\n", __func__, status);
David Brownelld8f388d82008-07-25 01:46:07 -07001054 }
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);
Linus Walleij9fb1f392013-12-04 14:42:46 +01001069 chip->exported = false;
David Brownelld8f388d82008-07-25 01:46:07 -07001070 status = 0;
1071 } else
1072 status = -ENODEV;
1073 mutex_unlock(&sysfs_lock);
1074
1075 if (status)
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001076 chip_dbg(chip, "%s: status %d\n", __func__, status);
David Brownelld8f388d82008-07-25 01:46:07 -07001077}
1078
1079static int __init gpiolib_sysfs_init(void)
1080{
1081 int status;
1082 unsigned long flags;
Alexandre Courbot65493e32013-02-03 01:29:25 +09001083 struct gpio_chip *chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001084
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);
Alexandre Courbot65493e32013-02-03 01:29:25 +09001096 list_for_each_entry(chip, &gpio_chips, list) {
David Brownelld8f388d82008-07-25 01:46:07 -07001097 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
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001123/*
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
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001160/**
David Brownelld2876d02008-02-04 22:28:20 -08001161 * 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.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001168 *
David Brownelld8f388d82008-07-25 01:46:07 -07001169 * 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 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001174 * If chip->base is negative, this requests dynamic assignment of
1175 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001176 */
1177int gpiochip_add(struct gpio_chip *chip)
1178{
1179 unsigned long flags;
1180 int status = 0;
1181 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001182 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001183
Trent Piephobff5fda2008-05-23 13:04:44 -07001184 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001185 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001186 status = -EINVAL;
1187 goto fail;
1188 }
1189
1190 spin_lock_irqsave(&gpio_lock, flags);
1191
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001192 if (base < 0) {
1193 base = gpiochip_find_base(chip->ngpio);
1194 if (base < 0) {
1195 status = base;
David Brownelld8f388d82008-07-25 01:46:07 -07001196 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001197 }
1198 chip->base = base;
1199 }
1200
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001201 status = gpiochip_add_to_list(chip);
1202
David Brownelld2876d02008-02-04 22:28:20 -08001203 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001204 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;
David Brownelld8f388d82008-07-25 01:46:07 -07001209
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,
Mathias Nyman80b0a602012-10-24 17:25:27 +03001214 * and in case chip->get_direction is not set,
David Brownelld8f388d82008-07-25 01:46:07 -07001215 * we may expose the wrong direction in sysfs.
1216 */
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001217 desc->flags = !chip->direction_input
David Brownelld8f388d82008-07-25 01:46:07 -07001218 ? (1 << FLAG_IS_OUT)
1219 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001220 }
1221 }
1222
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001223 spin_unlock_irqrestore(&gpio_lock, flags);
1224
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301225#ifdef CONFIG_PINCTRL
1226 INIT_LIST_HEAD(&chip->pin_ranges);
1227#endif
1228
Anton Vorontsov391c9702010-06-08 07:48:17 -06001229 of_gpiochip_add(chip);
Mika Westerberg664e3e52014-01-08 12:40:54 +02001230 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001231
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001232 if (status)
1233 goto fail;
1234
1235 status = gpiochip_export(chip);
1236 if (status)
1237 goto fail;
1238
Andy Shevchenko7589e592013-12-05 11:26:23 +02001239 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -07001240 chip->base, chip->base + chip->ngpio - 1,
1241 chip->label ? : "generic");
1242
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001243 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001244
1245unlock:
1246 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld2876d02008-02-04 22:28:20 -08001247fail:
1248 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001249 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001250 chip->base, chip->base + chip->ngpio - 1,
1251 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001252 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
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001270 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001271 of_gpiochip_remove(chip);
Mika Westerberg664e3e52014-01-08 12:40:54 +02001272 acpi_gpiochip_remove(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001273
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001274 for (id = 0; id < chip->ngpio; id++) {
1275 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
David Brownelld2876d02008-02-04 22:28:20 -08001276 status = -EBUSY;
1277 break;
1278 }
1279 }
1280 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001281 for (id = 0; id < chip->ngpio; id++)
1282 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001283
1284 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -08001285 }
1286
1287 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d82008-07-25 01:46:07 -07001288
1289 if (status == 0)
1290 gpiochip_unexport(chip);
1291
David Brownelld2876d02008-02-04 22:28:20 -08001292 return status;
1293}
1294EXPORT_SYMBOL_GPL(gpiochip_remove);
1295
Grant Likely594fa262010-06-08 07:48:16 -06001296/**
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 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001307struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001308 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001309 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001310{
Alexandre Courbot125eef92013-02-03 01:29:26 +09001311 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001312 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001313
1314 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +09001315 list_for_each_entry(chip, &gpio_chips, list)
1316 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001317 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001318
1319 /* No match? */
1320 if (&chip->list == &gpio_chips)
1321 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001322 spin_unlock_irqrestore(&gpio_lock, flags);
1323
1324 return chip;
1325}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001326EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001327
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001328static 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
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301340#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001341
Linus Walleij3f0f8672012-11-20 12:40:15 +01001342/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001343 * 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) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001358 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001359 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);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001372 if (ret < 0) {
1373 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001374 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001375 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001376
1377 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1378
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001379 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1380 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001381 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
1389/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001390 * 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
Linus Walleij316511c2012-11-21 08:48:09 +01001393 * @gpio_offset: the start offset in the current gpio_chip number space
1394 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001395 * @npins: the number of pins from the offset of each pin space (GPIO and
1396 * pin controller) to accumulate in this range
1397 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001398int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001399 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001400 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301401{
1402 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001403 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301404
Linus Walleij3f0f8672012-11-20 12:40:15 +01001405 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301406 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001407 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001408 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301409 }
1410
Linus Walleij3f0f8672012-11-20 12:40:15 +01001411 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001412 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001413 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301414 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001415 pin_range->range.base = chip->base + gpio_offset;
1416 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301417 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001418 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301419 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001420 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001421 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001422 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001423 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001424 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001425 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001426 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1427 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001428 pinctl_name,
1429 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301430
1431 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001432
1433 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301434}
Linus Walleij165adc92012-11-06 14:49:39 +01001435EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301436
Linus Walleij3f0f8672012-11-20 12:40:15 +01001437/**
1438 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1439 * @chip: the chip to remove all the mappings for
1440 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301441void 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);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001449 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301450 }
1451}
Linus Walleij165adc92012-11-06 14:49:39 +01001452EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1453
1454#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301455
David Brownelld2876d02008-02-04 22:28:20 -08001456/* 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 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001460static int gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001461{
David Brownell35e8bb52008-10-15 22:03:16 -07001462 struct gpio_chip *chip;
Mark Browne9354572012-07-09 12:22:56 +01001463 int status = -EPROBE_DEFER;
David Brownelld2876d02008-02-04 22:28:20 -08001464 unsigned long flags;
1465
Alexandre Courbot0204df42013-10-04 10:59:58 -07001466 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001467 pr_warn("%s: invalid GPIO\n", __func__);
1468 return -EINVAL;
1469 }
1470
David Brownelld2876d02008-02-04 22:28:20 -08001471 spin_lock_irqsave(&gpio_lock, flags);
1472
David Brownell35e8bb52008-10-15 22:03:16 -07001473 chip = desc->chip;
Alexandre Courbot0204df42013-10-04 10:59:58 -07001474 if (chip == NULL)
1475 goto done;
David Brownelld2876d02008-02-04 22:28:20 -08001476
David Brownell35e8bb52008-10-15 22:03:16 -07001477 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001478 goto done;
1479
David Brownelld2876d02008-02-04 22:28:20 -08001480 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001481 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001482 */
1483
1484 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1485 desc_set_label(desc, label ? : "?");
1486 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001487 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001488 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001489 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001490 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001491 }
1492
1493 if (chip->request) {
1494 /* chip->request may sleep */
1495 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001496 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001497 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);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001503 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001504 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001505 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001506 if (chip->get_direction) {
1507 /* chip->get_direction may sleep */
1508 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001509 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001510 spin_lock_irqsave(&gpio_lock, flags);
1511 }
David Brownelld2876d02008-02-04 22:28:20 -08001512done:
1513 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001514 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001515 spin_unlock_irqrestore(&gpio_lock, flags);
1516 return status;
1517}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001518
1519int gpio_request(unsigned gpio, const char *label)
1520{
1521 return gpiod_request(gpio_to_desc(gpio), label);
1522}
David Brownelld2876d02008-02-04 22:28:20 -08001523EXPORT_SYMBOL_GPL(gpio_request);
1524
Alexandre Courbot372e7222013-02-03 01:29:29 +09001525static void gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001526{
1527 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001528 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001529
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001530 might_sleep();
1531
Alexandre Courbot372e7222013-02-03 01:29:29 +09001532 if (!desc) {
David Brownelld2876d02008-02-04 22:28:20 -08001533 WARN_ON(extra_checks);
1534 return;
1535 }
1536
Alexandre Courbot372e7222013-02-03 01:29:29 +09001537 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07001538
David Brownelld2876d02008-02-04 22:28:20 -08001539 spin_lock_irqsave(&gpio_lock, flags);
1540
David Brownell35e8bb52008-10-15 22:03:16 -07001541 chip = desc->chip;
1542 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1543 if (chip->free) {
1544 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001545 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001546 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001547 spin_lock_irqsave(&gpio_lock, flags);
1548 }
David Brownelld2876d02008-02-04 22:28:20 -08001549 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001550 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001551 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001552 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301553 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301554 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001555 } else
David Brownelld2876d02008-02-04 22:28:20 -08001556 WARN_ON(extra_checks);
1557
1558 spin_unlock_irqrestore(&gpio_lock, flags);
1559}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001560
1561void gpio_free(unsigned gpio)
1562{
1563 gpiod_free(gpio_to_desc(gpio));
1564}
David Brownelld2876d02008-02-04 22:28:20 -08001565EXPORT_SYMBOL_GPL(gpio_free);
1566
Eric Miao3e45f1d2010-03-05 13:44:35 -08001567/**
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{
Alexandre Courbot372e7222013-02-03 01:29:29 +09001575 struct gpio_desc *desc;
Eric Miao3e45f1d2010-03-05 13:44:35 -08001576 int err;
1577
Alexandre Courbot372e7222013-02-03 01:29:29 +09001578 desc = gpio_to_desc(gpio);
1579
1580 err = gpiod_request(desc, label);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001581 if (err)
1582 return err;
1583
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301584 if (flags & GPIOF_OPEN_DRAIN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001585 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301586
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301587 if (flags & GPIOF_OPEN_SOURCE)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001588 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301589
Eric Miao3e45f1d2010-03-05 13:44:35 -08001590 if (flags & GPIOF_DIR_IN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001591 err = gpiod_direction_input(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001592 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001593 err = gpiod_direction_output(desc,
Eric Miao3e45f1d2010-03-05 13:44:35 -08001594 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1595
Aaro Koskinene2548112010-12-21 17:24:22 -08001596 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001597 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -08001598
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001599 if (flags & GPIOF_EXPORT) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001600 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001601 if (err)
1602 goto free_gpio;
1603 }
1604
1605 return 0;
1606
1607 free_gpio:
Alexandre Courbot372e7222013-02-03 01:29:29 +09001608 gpiod_free(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001609 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 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001618int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001619{
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 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001641void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001642{
1643 while (num--)
1644 gpio_free((array++)->gpio);
1645}
1646EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001647
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{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001663 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001664
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001665 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -08001666 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001667
1668 desc = &chip->desc[offset];
1669
Alexandre Courbot372e7222013-02-03 01:29:29 +09001670 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001671 return NULL;
1672#ifdef CONFIG_DEBUG_FS
Alexandre Courbot372e7222013-02-03 01:29:29 +09001673 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001674#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
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001690/**
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)
David Brownelld2876d02008-02-04 22:28:20 -08001700{
1701 unsigned long flags;
1702 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001703 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001704 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001705
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001706 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001707 pr_warn("%s: invalid GPIO\n", __func__);
1708 return -EINVAL;
1709 }
1710
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001711 chip = desc->chip;
1712 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001713 gpiod_warn(desc,
1714 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001715 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001716 return -EIO;
1717 }
1718
David Brownelld2876d02008-02-04 22:28:20 -08001719 spin_lock_irqsave(&gpio_lock, flags);
1720
Alexandre Courbot372e7222013-02-03 01:29:29 +09001721 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001722 if (status < 0)
1723 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001724
1725 /* now we know the gpio is valid and chip won't vanish */
1726
1727 spin_unlock_irqrestore(&gpio_lock, flags);
1728
David Brownell9c4ba942010-08-10 18:02:24 -07001729 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001730
Alexandre Courbot372e7222013-02-03 01:29:29 +09001731 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001732 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001733 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001734 if (status < 0) {
Andy Shevchenko7589e592013-12-05 11:26:23 +02001735 gpiod_dbg(desc, "%s: chip request fail, %d\n",
1736 __func__, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001737 /* and it's not available to anyone else ...
1738 * gpio_request() is the fully clean solution.
1739 */
1740 goto lose;
1741 }
1742 }
1743
Alexandre Courbot372e7222013-02-03 01:29:29 +09001744 status = chip->direction_input(chip, offset);
David Brownelld2876d02008-02-04 22:28:20 -08001745 if (status == 0)
1746 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001747
Alexandre Courbot372e7222013-02-03 01:29:29 +09001748 trace_gpio_direction(desc_to_gpio(desc), 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001749lose:
David Brownelld2876d02008-02-04 22:28:20 -08001750 return status;
1751fail:
1752 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001753 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001754 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001755 return status;
1756}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001757EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001758
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001759/**
1760 * gpiod_direction_output - set the GPIO direction to input
1761 * @desc: GPIO to set to output
1762 * @value: initial output value of the GPIO
1763 *
1764 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1765 * be called safely on it. The initial value of the output must be specified.
1766 *
1767 * Return 0 in case of success, else an error code.
1768 */
1769int gpiod_direction_output(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001770{
1771 unsigned long flags;
1772 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001773 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001774 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001775
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001776 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001777 pr_warn("%s: invalid GPIO\n", __func__);
1778 return -EINVAL;
1779 }
1780
Linus Walleijd468bf92013-09-24 11:54:38 +02001781 /* GPIOs used for IRQs shall not be set as output */
1782 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1783 gpiod_err(desc,
1784 "%s: tried to set a GPIO tied to an IRQ as output\n",
1785 __func__);
1786 return -EIO;
1787 }
1788
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301789 /* Open drain pin should not be driven to 1 */
1790 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001791 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301792
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301793 /* Open source pin should not be driven to 0 */
1794 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001795 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301796
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001797 chip = desc->chip;
1798 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001799 gpiod_warn(desc,
1800 "%s: missing set() or direction_output() operations\n",
1801 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001802 return -EIO;
1803 }
1804
David Brownelld2876d02008-02-04 22:28:20 -08001805 spin_lock_irqsave(&gpio_lock, flags);
1806
Alexandre Courbot372e7222013-02-03 01:29:29 +09001807 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001808 if (status < 0)
1809 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001810
1811 /* now we know the gpio is valid and chip won't vanish */
1812
1813 spin_unlock_irqrestore(&gpio_lock, flags);
1814
David Brownell9c4ba942010-08-10 18:02:24 -07001815 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001816
Alexandre Courbot372e7222013-02-03 01:29:29 +09001817 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001818 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001819 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001820 if (status < 0) {
Andy Shevchenko7589e592013-12-05 11:26:23 +02001821 gpiod_dbg(desc, "%s: chip request fail, %d\n",
1822 __func__, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001823 /* and it's not available to anyone else ...
1824 * gpio_request() is the fully clean solution.
1825 */
1826 goto lose;
1827 }
1828 }
1829
Alexandre Courbot372e7222013-02-03 01:29:29 +09001830 status = chip->direction_output(chip, offset, value);
David Brownelld2876d02008-02-04 22:28:20 -08001831 if (status == 0)
1832 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001833 trace_gpio_value(desc_to_gpio(desc), 0, value);
1834 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001835lose:
David Brownelld2876d02008-02-04 22:28:20 -08001836 return status;
1837fail:
1838 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001839 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001840 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001841 return status;
1842}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001843EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001844
Felipe Balbic4b5be92010-05-26 14:42:23 -07001845/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001846 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001847 * @gpio: the gpio to set debounce time
1848 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001849 *
1850 * returns -ENOTSUPP if the controller does not support setting
1851 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001852 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001853int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001854{
1855 unsigned long flags;
1856 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001857 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001858 int offset;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001859
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001860 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001861 pr_warn("%s: invalid GPIO\n", __func__);
1862 return -EINVAL;
1863 }
1864
Felipe Balbic4b5be92010-05-26 14:42:23 -07001865 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001866 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001867 gpiod_dbg(desc,
1868 "%s: missing set() or set_debounce() operations\n",
1869 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001870 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001871 }
1872
1873 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001874
1875 status = gpio_ensure_requested(desc);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001876 if (status < 0)
1877 goto fail;
1878
1879 /* now we know the gpio is valid and chip won't vanish */
1880
1881 spin_unlock_irqrestore(&gpio_lock, flags);
1882
David Brownell9c4ba942010-08-10 18:02:24 -07001883 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001884
Alexandre Courbot372e7222013-02-03 01:29:29 +09001885 offset = gpio_chip_hwgpio(desc);
1886 return chip->set_debounce(chip, offset, debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001887
1888fail:
1889 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001890 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001891 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001892
1893 return status;
1894}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001895EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001896
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001897/**
1898 * gpiod_is_active_low - test whether a GPIO is active-low or not
1899 * @desc: the gpio descriptor to test
1900 *
1901 * Returns 1 if the GPIO is active-low, 0 otherwise.
1902 */
1903int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001904{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001905 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001906}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001907EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001908
1909/* I/O calls are only valid after configuration completed; the relevant
1910 * "is this a valid GPIO" error checks should already have been done.
1911 *
1912 * "Get" operations are often inlinable as reading a pin value register,
1913 * and masking the relevant bit in that register.
1914 *
1915 * When "set" operations are inlinable, they involve writing that mask to
1916 * one register to set a low value, or a different register to set it high.
1917 * Otherwise locking is needed, so there may be little value to inlining.
1918 *
1919 *------------------------------------------------------------------------
1920 *
1921 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1922 * have requested the GPIO. That can include implicit requesting by
1923 * a direction setting call. Marking a gpio as requested locks its chip
1924 * in memory, guaranteeing that these table lookups need no more locking
1925 * and that gpiochip_remove() will fail.
1926 *
1927 * REVISIT when debugging, consider adding some instrumentation to ensure
1928 * that the GPIO was actually requested.
1929 */
1930
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001931static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001932{
1933 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001934 int value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001935 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001936
Alexandre Courbot372e7222013-02-03 01:29:29 +09001937 chip = desc->chip;
1938 offset = gpio_chip_hwgpio(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001939 value = chip->get ? chip->get(chip, offset) : 0;
1940 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001941 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001942}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001943
David Brownelld2876d02008-02-04 22:28:20 -08001944/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001945 * gpiod_get_raw_value() - return a gpio's raw value
1946 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001947 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001948 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1949 * its ACTIVE_LOW status.
1950 *
1951 * This function should be called from contexts where we cannot sleep, and will
1952 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001953 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001954int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001955{
David Brownelld2876d02008-02-04 22:28:20 -08001956 if (!desc)
1957 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001958 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001959 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001960 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001961}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001962EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001963
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001964/**
1965 * gpiod_get_value() - return a gpio's value
1966 * @desc: gpio whose value will be returned
1967 *
1968 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1969 * account.
1970 *
1971 * This function should be called from contexts where we cannot sleep, and will
1972 * complain if the GPIO chip functions potentially sleep.
1973 */
1974int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001975{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001976 int value;
1977 if (!desc)
1978 return 0;
1979 /* Should be using gpio_get_value_cansleep() */
1980 WARN_ON(desc->chip->can_sleep);
1981
1982 value = _gpiod_get_raw_value(desc);
1983 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1984 value = !value;
1985
1986 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001987}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001988EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001989
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301990/*
1991 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001992 * @desc: gpio descriptor whose state need to be set.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301993 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1994 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001995static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301996{
1997 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001998 struct gpio_chip *chip = desc->chip;
1999 int offset = gpio_chip_hwgpio(desc);
2000
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302001 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002002 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302003 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002004 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302005 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002006 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302007 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002008 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302009 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002010 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302011 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002012 gpiod_err(desc,
2013 "%s: Error in set_value for open drain err %d\n",
2014 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302015}
2016
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302017/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002018 * _gpio_set_open_source_value() - Set the open source gpio's value.
2019 * @desc: gpio descriptor whose state need to be set.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302020 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
2021 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09002022static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302023{
2024 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002025 struct gpio_chip *chip = desc->chip;
2026 int offset = gpio_chip_hwgpio(desc);
2027
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302028 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002029 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302030 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002031 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302032 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002033 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302034 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002035 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302036 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002037 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302038 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002039 gpiod_err(desc,
2040 "%s: Error in set_value for open source err %d\n",
2041 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302042}
2043
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002044static void _gpiod_set_raw_value(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002045{
2046 struct gpio_chip *chip;
2047
Alexandre Courbot372e7222013-02-03 01:29:29 +09002048 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002049 trace_gpio_value(desc_to_gpio(desc), 0, value);
2050 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2051 _gpio_set_open_drain_value(desc, value);
2052 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2053 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302054 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002055 chip->set(chip, gpio_chip_hwgpio(desc), value);
2056}
2057
David Brownelld2876d02008-02-04 22:28:20 -08002058/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002059 * gpiod_set_raw_value() - assign a gpio's raw value
2060 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002061 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002062 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002063 * Set the raw value of the GPIO, i.e. the value of its physical line without
2064 * regard for its ACTIVE_LOW status.
2065 *
2066 * This function should be called from contexts where we cannot sleep, and will
2067 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002068 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002069void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002070{
David Brownelld2876d02008-02-04 22:28:20 -08002071 if (!desc)
2072 return;
David Brownelld2876d02008-02-04 22:28:20 -08002073 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09002074 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002075 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002076}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002077EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002078
2079/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002080 * gpiod_set_value() - assign a gpio's value
2081 * @desc: gpio whose value will be assigned
2082 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002083 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002084 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2085 * account
2086 *
2087 * This function should be called from contexts where we cannot sleep, and will
2088 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002089 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002090void gpiod_set_value(struct gpio_desc *desc, int value)
2091{
2092 if (!desc)
2093 return;
2094 /* Should be using gpio_set_value_cansleep() */
2095 WARN_ON(desc->chip->can_sleep);
2096 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2097 value = !value;
2098 _gpiod_set_raw_value(desc, value);
2099}
2100EXPORT_SYMBOL_GPL(gpiod_set_value);
2101
2102/**
2103 * gpiod_cansleep() - report whether gpio value access may sleep
2104 * @desc: gpio to check
2105 *
2106 */
2107int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002108{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002109 if (!desc)
2110 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002111 return desc->chip->can_sleep;
2112}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002113EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002114
David Brownell0f6d5042008-10-15 22:03:14 -07002115/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002116 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2117 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002118 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002119 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2120 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002121 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002122int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002123{
2124 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002125 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002126
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002127 if (!desc)
2128 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002129 chip = desc->chip;
2130 offset = gpio_chip_hwgpio(desc);
2131 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
2132}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002133EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002134
Linus Walleijd468bf92013-09-24 11:54:38 +02002135/**
2136 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
2137 * @gpio: the GPIO line to lock as used for IRQ
2138 *
2139 * This is used directly by GPIO drivers that want to lock down
2140 * a certain GPIO line to be used as IRQs, for example in the
2141 * .to_irq() callback of their gpio_chip, or in the .irq_enable()
2142 * of its irq_chip implementation if the GPIO is known from that
2143 * code.
David Brownelld2876d02008-02-04 22:28:20 -08002144 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002145int gpiod_lock_as_irq(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002146{
Linus Walleijd468bf92013-09-24 11:54:38 +02002147 if (!desc)
2148 return -EINVAL;
2149
2150 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
2151 gpiod_err(desc,
2152 "%s: tried to flag a GPIO set as output for IRQ\n",
2153 __func__);
2154 return -EIO;
2155 }
2156
2157 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
2158 return 0;
2159}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002160EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002161
2162int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
2163{
2164 return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset));
2165}
2166EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
2167
2168/**
2169 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
2170 * @gpio: the GPIO line to unlock from IRQ usage
2171 *
2172 * This is used directly by GPIO drivers that want to indicate
2173 * that a certain GPIO is no longer used exclusively for IRQ.
2174 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002175void gpiod_unlock_as_irq(struct gpio_desc *desc)
Linus Walleijd468bf92013-09-24 11:54:38 +02002176{
2177 if (!desc)
2178 return;
2179
2180 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2181}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002182EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002183
2184void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2185{
2186 return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset));
2187}
2188EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
David Brownelld2876d02008-02-04 22:28:20 -08002189
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002190/**
2191 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2192 * @desc: gpio whose value will be returned
2193 *
2194 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2195 * its ACTIVE_LOW status.
2196 *
2197 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002198 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002199int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002200{
David Brownelld2876d02008-02-04 22:28:20 -08002201 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002202 if (!desc)
2203 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002204 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002205}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002206EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002207
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002208/**
2209 * gpiod_get_value_cansleep() - return a gpio's value
2210 * @desc: gpio whose value will be returned
2211 *
2212 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2213 * account.
2214 *
2215 * This function is to be called from contexts that can sleep.
2216 */
2217int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002218{
David Brownelld2876d02008-02-04 22:28:20 -08002219 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002220
2221 might_sleep_if(extra_checks);
2222 if (!desc)
2223 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002224
2225 value = _gpiod_get_raw_value(desc);
2226 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2227 value = !value;
2228
David Brownelld2876d02008-02-04 22:28:20 -08002229 return value;
2230}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002231EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002232
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002233/**
2234 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2235 * @desc: gpio whose value will be assigned
2236 * @value: value to assign
2237 *
2238 * Set the raw value of the GPIO, i.e. the value of its physical line without
2239 * regard for its ACTIVE_LOW status.
2240 *
2241 * This function is to be called from contexts that can sleep.
2242 */
2243void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002244{
David Brownelld2876d02008-02-04 22:28:20 -08002245 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002246 if (!desc)
2247 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002248 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002249}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002250EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002251
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002252/**
2253 * gpiod_set_value_cansleep() - assign a gpio's value
2254 * @desc: gpio whose value will be assigned
2255 * @value: value to assign
2256 *
2257 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2258 * account
2259 *
2260 * This function is to be called from contexts that can sleep.
2261 */
2262void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002263{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002264 might_sleep_if(extra_checks);
2265 if (!desc)
2266 return;
2267
2268 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2269 value = !value;
2270 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002271}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002272EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002273
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002274/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002275 * gpiod_add_lookup_table() - register GPIO device consumers
2276 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002277 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002278void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002279{
2280 mutex_lock(&gpio_lookup_lock);
2281
Alexandre Courbotad824782013-12-03 12:20:11 +09002282 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002283
2284 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002285}
2286
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002287#ifdef CONFIG_OF
2288static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002289 unsigned int idx,
2290 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002291{
2292 char prop_name[32]; /* 32 is max size of property name */
2293 enum of_gpio_flags of_flags;
2294 struct gpio_desc *desc;
2295
2296 if (con_id)
2297 snprintf(prop_name, 32, "%s-gpios", con_id);
2298 else
2299 snprintf(prop_name, 32, "gpios");
2300
2301 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2302 &of_flags);
2303
2304 if (IS_ERR(desc))
2305 return desc;
2306
2307 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002308 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002309
2310 return desc;
2311}
2312#else
2313static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot209e64f2013-11-19 10:37:29 +09002314 unsigned int idx,
2315 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002316{
2317 return ERR_PTR(-ENODEV);
2318}
2319#endif
2320
Mika Westerberg81f59e92013-10-10 11:01:09 +03002321static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002322 unsigned int idx,
2323 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03002324{
Mika Westerberge01f4402013-10-10 11:01:10 +03002325 struct acpi_gpio_info info;
2326 struct gpio_desc *desc;
2327
2328 desc = acpi_get_gpiod_by_index(dev, idx, &info);
2329 if (IS_ERR(desc))
2330 return desc;
2331
2332 if (info.gpioint && info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002333 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002334
2335 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002336}
2337
Alexandre Courbotad824782013-12-03 12:20:11 +09002338static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2339{
2340 const char *dev_id = dev ? dev_name(dev) : NULL;
2341 struct gpiod_lookup_table *table;
2342
2343 mutex_lock(&gpio_lookup_lock);
2344
2345 list_for_each_entry(table, &gpio_lookup_list, list) {
2346 if (table->dev_id && dev_id) {
2347 /*
2348 * Valid strings on both ends, must be identical to have
2349 * a match
2350 */
2351 if (!strcmp(table->dev_id, dev_id))
2352 goto found;
2353 } else {
2354 /*
2355 * One of the pointers is NULL, so both must be to have
2356 * a match
2357 */
2358 if (dev_id == table->dev_id)
2359 goto found;
2360 }
2361 }
2362 table = NULL;
2363
2364found:
2365 mutex_unlock(&gpio_lookup_lock);
2366 return table;
2367}
2368
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002369static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002370 unsigned int idx,
2371 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002372{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002373 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002374 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002375 struct gpiod_lookup *p;
2376
Alexandre Courbotad824782013-12-03 12:20:11 +09002377 table = gpiod_find_lookup_table(dev);
2378 if (!table)
2379 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002380
Alexandre Courbotad824782013-12-03 12:20:11 +09002381 for (p = &table->table[0]; p->chip_label; p++) {
2382 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002383
Alexandre Courbotad824782013-12-03 12:20:11 +09002384 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002385 if (p->idx != idx)
2386 continue;
2387
Alexandre Courbotad824782013-12-03 12:20:11 +09002388 /* If the lookup entry has a con_id, require exact match */
2389 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2390 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002391
Alexandre Courbotad824782013-12-03 12:20:11 +09002392 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002393
Alexandre Courbotad824782013-12-03 12:20:11 +09002394 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002395 dev_err(dev, "cannot find GPIO chip %s\n",
2396 p->chip_label);
2397 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002398 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002399
Alexandre Courbotad824782013-12-03 12:20:11 +09002400 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002401 dev_err(dev,
2402 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2403 idx, chip->ngpio, chip->label);
2404 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09002405 }
2406
2407 desc = gpiochip_offset_to_desc(chip, p->chip_hwnum);
2408 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002409
2410 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09002411 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002412
2413 return desc;
2414}
2415
2416/**
2417 * gpio_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002418 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002419 * @con_id: function within the GPIO consumer
2420 *
2421 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002422 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
2423 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002424 */
2425struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id)
2426{
2427 return gpiod_get_index(dev, con_id, 0);
2428}
2429EXPORT_SYMBOL_GPL(gpiod_get);
2430
2431/**
2432 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002433 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002434 * @con_id: function within the GPIO consumer
2435 * @idx: index of the GPIO to obtain in the consumer
2436 *
2437 * This variant of gpiod_get() allows to access GPIOs other than the first
2438 * defined one for functions that define several GPIOs.
2439 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002440 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2441 * requested function and/or index, or another IS_ERR() code if an error
2442 * occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002443 */
2444struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2445 const char *con_id,
2446 unsigned int idx)
2447{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002448 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002449 int status;
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002450 enum gpio_lookup_flags flags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002451
2452 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2453
2454 /* Using device tree? */
2455 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
2456 dev_dbg(dev, "using device tree for GPIO lookup\n");
2457 desc = of_find_gpio(dev, con_id, idx, &flags);
Mika Westerberg81f59e92013-10-10 11:01:09 +03002458 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
2459 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2460 desc = acpi_find_gpio(dev, con_id, idx, &flags);
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002461 }
2462
2463 /*
2464 * Either we are not using DT or ACPI, or their lookup did not return
2465 * a result. In that case, use platform lookup as a fallback.
2466 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002467 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002468 dev_dbg(dev, "using lookup tables for GPIO lookup");
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002469 desc = gpiod_find(dev, con_id, idx, &flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002470 }
2471
2472 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002473 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002474 return desc;
2475 }
2476
2477 status = gpiod_request(desc, con_id);
2478
2479 if (status < 0)
2480 return ERR_PTR(status);
2481
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002482 if (flags & GPIO_ACTIVE_LOW)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002483 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002484 if (flags & GPIO_OPEN_DRAIN)
2485 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2486 if (flags & GPIO_OPEN_SOURCE)
2487 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002488
2489 return desc;
2490}
2491EXPORT_SYMBOL_GPL(gpiod_get_index);
2492
2493/**
2494 * gpiod_put - dispose of a GPIO descriptor
2495 * @desc: GPIO descriptor to dispose of
2496 *
2497 * No descriptor can be used after gpiod_put() has been called on it.
2498 */
2499void gpiod_put(struct gpio_desc *desc)
2500{
2501 gpiod_free(desc);
2502}
2503EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002504
David Brownelld2876d02008-02-04 22:28:20 -08002505#ifdef CONFIG_DEBUG_FS
2506
2507static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2508{
2509 unsigned i;
2510 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002511 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002512 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002513 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002514
2515 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2516 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2517 continue;
2518
Alexandre Courbot372e7222013-02-03 01:29:29 +09002519 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002520 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002521 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2522 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002523 gpio, gdesc->label,
2524 is_out ? "out" : "in ",
2525 chip->get
2526 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002527 : "? ",
2528 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002529 seq_printf(s, "\n");
2530 }
2531}
2532
Thierry Redingf9c4a312012-04-12 13:26:01 +02002533static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002534{
Grant Likely362432a2013-02-09 09:41:49 +00002535 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002536 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002537 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002538
2539 s->private = "";
2540
Grant Likely362432a2013-02-09 09:41:49 +00002541 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002542 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002543 if (index-- == 0) {
2544 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002545 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002546 }
2547 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002548
2549 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002550}
2551
2552static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2553{
Grant Likely362432a2013-02-09 09:41:49 +00002554 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002555 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002556 void *ret = NULL;
2557
Grant Likely362432a2013-02-09 09:41:49 +00002558 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002559 if (list_is_last(&chip->list, &gpio_chips))
2560 ret = NULL;
2561 else
2562 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002563 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002564
2565 s->private = "\n";
2566 ++*pos;
2567
2568 return ret;
2569}
2570
2571static void gpiolib_seq_stop(struct seq_file *s, void *v)
2572{
2573}
2574
2575static int gpiolib_seq_show(struct seq_file *s, void *v)
2576{
2577 struct gpio_chip *chip = v;
2578 struct device *dev;
2579
2580 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2581 chip->base, chip->base + chip->ngpio - 1);
2582 dev = chip->dev;
2583 if (dev)
2584 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2585 dev_name(dev));
2586 if (chip->label)
2587 seq_printf(s, ", %s", chip->label);
2588 if (chip->can_sleep)
2589 seq_printf(s, ", can sleep");
2590 seq_printf(s, ":\n");
2591
2592 if (chip->dbg_show)
2593 chip->dbg_show(s, chip);
2594 else
2595 gpiolib_dbg_show(s, chip);
2596
David Brownelld2876d02008-02-04 22:28:20 -08002597 return 0;
2598}
2599
Thierry Redingf9c4a312012-04-12 13:26:01 +02002600static const struct seq_operations gpiolib_seq_ops = {
2601 .start = gpiolib_seq_start,
2602 .next = gpiolib_seq_next,
2603 .stop = gpiolib_seq_stop,
2604 .show = gpiolib_seq_show,
2605};
2606
David Brownelld2876d02008-02-04 22:28:20 -08002607static int gpiolib_open(struct inode *inode, struct file *file)
2608{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002609 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002610}
2611
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002612static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002613 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002614 .open = gpiolib_open,
2615 .read = seq_read,
2616 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002617 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002618};
2619
2620static int __init gpiolib_debugfs_init(void)
2621{
2622 /* /sys/kernel/debug/gpio */
2623 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2624 NULL, NULL, &gpiolib_operations);
2625 return 0;
2626}
2627subsys_initcall(gpiolib_debugfs_init);
2628
2629#endif /* DEBUG_FS */