blob: 30ae38681fef050ad560ff6c39a3192035e57340 [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 Brownelld8f388d2008-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>
Mika Westerberg81f59e92013-10-10 11:01:09 +030013#include <linux/acpi_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070014#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010016#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090017#include <linux/gpio/driver.h>
David Brownelld2876d02008-02-04 22:28:20 -080018
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060019#define CREATE_TRACE_POINTS
20#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080021
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070022/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080023 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070024 * The GPIO programming interface allows for inlining speed-critical
25 * get/set operations for common cases, so that access to SOC-integrated
26 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080027 */
28
29
30/* When debugging, extend minimal trust to callers and platform code.
31 * Also emit diagnostic messages that may help initial bringup, when
32 * board setup or driver bugs are most common.
33 *
34 * Otherwise, minimize overhead in what may be bitbanging codepaths.
35 */
36#ifdef DEBUG
37#define extra_checks 1
38#else
39#define extra_checks 0
40#endif
41
42/* gpio_lock prevents conflicts during gpio_desc[] table updates.
43 * While any GPIO is requested, its gpio_chip is not removable;
44 * each GPIO's "requested" flag serves as a lock and refcount.
45 */
46static DEFINE_SPINLOCK(gpio_lock);
47
48struct gpio_desc {
49 struct gpio_chip *chip;
50 unsigned long flags;
51/* flag symbols are bit numbers */
52#define FLAG_REQUESTED 0
53#define FLAG_IS_OUT 1
Alexandre Courbot710b40e2013-02-02 23:44:06 +090054#define FLAG_EXPORT 2 /* protected by sysfs_lock */
55#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
56#define FLAG_TRIG_FALL 4 /* trigger on falling edge */
57#define FLAG_TRIG_RISE 5 /* trigger on rising edge */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070058#define FLAG_ACTIVE_LOW 6 /* value has active low */
Alexandre Courbot710b40e2013-02-02 23:44:06 +090059#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
60#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
Linus Walleijd468bf92013-09-24 11:54:38 +020061#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070062
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070063#define ID_SHIFT 16 /* add new flags before this one */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070064
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070065#define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
Daniel Glöcknerff77c352009-09-22 16:46:38 -070066#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
David Brownelld2876d02008-02-04 22:28:20 -080067
68#ifdef CONFIG_DEBUG_FS
69 const char *label;
70#endif
71};
72static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
73
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +090074#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
75
Alexandre Courbotbae48da2013-10-17 10:21:38 -070076static DEFINE_MUTEX(gpio_lookup_lock);
77static LIST_HEAD(gpio_lookup_list);
Alexandre Courbot1a989d02013-02-03 01:29:24 +090078static LIST_HEAD(gpio_chips);
79
Daniel Glöcknerff77c352009-09-22 16:46:38 -070080#ifdef CONFIG_GPIO_SYSFS
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070081static DEFINE_IDR(dirent_idr);
Daniel Glöcknerff77c352009-09-22 16:46:38 -070082#endif
83
Alexandre Courbot372e7222013-02-03 01:29:29 +090084static int gpiod_request(struct gpio_desc *desc, const char *label);
85static void gpiod_free(struct gpio_desc *desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090086
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +020087/* With descriptor prefix */
88
Mark Brown7b17b592013-09-09 10:33:50 +010089#ifdef CONFIG_DEBUG_FS
Andy Shevchenko7589e592013-12-05 11:26:23 +020090#define gpiod_emerg(desc, fmt, ...) \
91 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
Mark Brown7b17b592013-09-09 10:33:50 +010092 ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +020093#define gpiod_crit(desc, fmt, ...) \
94 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
Mark Brown7b17b592013-09-09 10:33:50 +010095 ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +020096#define gpiod_err(desc, fmt, ...) \
97 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
Mark Brown7b17b592013-09-09 10:33:50 +010098 ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +020099#define gpiod_warn(desc, fmt, ...) \
100 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
Mark Brown7b17b592013-09-09 10:33:50 +0100101 ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200102#define gpiod_info(desc, fmt, ...) \
103 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
Mark Brown7b17b592013-09-09 10:33:50 +0100104 ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200105#define gpiod_dbg(desc, fmt, ...) \
106 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
Mark Brown7b17b592013-09-09 10:33:50 +0100107 ##__VA_ARGS__)
108#else
Andy Shevchenko7589e592013-12-05 11:26:23 +0200109#define gpiod_emerg(desc, fmt, ...) \
Mark Brown6424de52013-09-09 10:33:49 +0100110 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200111#define gpiod_crit(desc, fmt, ...) \
Mark Brown6424de52013-09-09 10:33:49 +0100112 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200113#define gpiod_err(desc, fmt, ...) \
Mark Brown6424de52013-09-09 10:33:49 +0100114 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200115#define gpiod_warn(desc, fmt, ...) \
Mark Brown6424de52013-09-09 10:33:49 +0100116 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200117#define gpiod_info(desc, fmt, ...) \
Mark Brown6424de52013-09-09 10:33:49 +0100118 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200119#define gpiod_dbg(desc, fmt, ...) \
Mark Brown6424de52013-09-09 10:33:49 +0100120 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Mark Brown7b17b592013-09-09 10:33:50 +0100121#endif
Alexandre Courbot372e7222013-02-03 01:29:29 +0900122
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200123/* With chip prefix */
124
125#define chip_emerg(chip, fmt, ...) \
126 pr_emerg("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
127#define chip_crit(chip, fmt, ...) \
128 pr_crit("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
129#define chip_err(chip, fmt, ...) \
130 pr_err("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
131#define chip_warn(chip, fmt, ...) \
132 pr_warn("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
133#define chip_info(chip, fmt, ...) \
134 pr_info("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
135#define chip_dbg(chip, fmt, ...) \
136 pr_debug("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
137
David Brownelld2876d02008-02-04 22:28:20 -0800138static inline void desc_set_label(struct gpio_desc *d, const char *label)
139{
140#ifdef CONFIG_DEBUG_FS
141 d->label = label;
142#endif
143}
144
Alexandre Courbot372e7222013-02-03 01:29:29 +0900145/*
146 * Return the GPIO number of the passed descriptor relative to its chip
147 */
148static int gpio_chip_hwgpio(const struct gpio_desc *desc)
149{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900150 return desc - &desc->chip->desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900151}
152
153/**
154 * Convert a GPIO number to its descriptor
155 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700156struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900157{
158 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
159 return NULL;
160 else
161 return &gpio_desc[gpio];
162}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700163EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900164
165/**
Linus Walleijd468bf92013-09-24 11:54:38 +0200166 * Convert an offset on a certain chip to a corresponding descriptor
167 */
168static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip,
169 unsigned int offset)
170{
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900171 if (offset >= chip->ngpio)
172 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200173
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900174 return &chip->desc[offset];
Linus Walleijd468bf92013-09-24 11:54:38 +0200175}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900176
177/**
178 * Convert a GPIO descriptor to the integer namespace.
179 * This should disappear in the future but is needed since we still
180 * use GPIO numbers for error messages and sysfs nodes
181 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700182int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900183{
Alexandre Courbot8c0fca82013-10-04 10:59:57 -0700184 return desc - &gpio_desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900185}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700186EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900187
188
David Brownelld2876d02008-02-04 22:28:20 -0800189/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
190 * when setting direction, and otherwise illegal. Until board setup code
191 * and drivers use explicit requests everywhere (which won't happen when
192 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -0700193 * message should motivate switching to explicit requests... so should
194 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -0700195 *
196 * NOTE: the autorequest mechanism is going away; at this point it's
197 * only "legal" in the sense that (old) code using it won't break yet,
198 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -0800199 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900200static int gpio_ensure_requested(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800201{
David Brownell8a0cecf2009-04-02 16:57:06 -0700202 const struct gpio_chip *chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900203 const int gpio = desc_to_gpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -0700204
David Brownell8a0cecf2009-04-02 16:57:06 -0700205 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
206 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700207 if (!try_module_get(chip->owner)) {
Andy Shevchenko7589e592013-12-05 11:26:23 +0200208 gpiod_err(desc, "%s: module can't be gotten\n",
209 __func__);
David Brownell35e8bb52008-10-15 22:03:16 -0700210 clear_bit(FLAG_REQUESTED, &desc->flags);
211 /* lose */
212 return -EIO;
213 }
David Brownelld2876d02008-02-04 22:28:20 -0800214 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700215 /* caller must chip->request() w/o spinlock */
216 if (chip->request)
217 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800218 }
David Brownell35e8bb52008-10-15 22:03:16 -0700219 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800220}
221
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700222/**
223 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
224 * @desc: descriptor to return the chip of
225 */
226struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900227{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900228 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900229}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700230EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800231
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700232/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
233static int gpiochip_find_base(int ngpio)
234{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900235 struct gpio_chip *chip;
236 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700237
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900238 list_for_each_entry_reverse(chip, &gpio_chips, list) {
239 /* found a free space? */
240 if (chip->base + chip->ngpio <= base)
241 break;
242 else
243 /* nope, check the space right before the chip */
244 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700245 }
246
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900247 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700248 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900249 return base;
250 } else {
251 pr_err("%s: cannot find free range\n", __func__);
252 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700253 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700254}
255
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700256/**
257 * gpiod_get_direction - return the current direction of a GPIO
258 * @desc: GPIO to get the direction of
259 *
260 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
261 *
262 * This function may sleep if gpiod_cansleep() is true.
263 */
264int gpiod_get_direction(const struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300265{
266 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900267 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300268 int status = -EINVAL;
269
Alexandre Courbot372e7222013-02-03 01:29:29 +0900270 chip = gpiod_to_chip(desc);
271 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300272
273 if (!chip->get_direction)
274 return status;
275
Alexandre Courbot372e7222013-02-03 01:29:29 +0900276 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300277 if (status > 0) {
278 /* GPIOF_DIR_IN, or other positive */
279 status = 1;
Alexandre Courbotdef63432013-02-15 14:46:15 +0900280 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
281 * so it does not affect constness per se */
282 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300283 }
284 if (status == 0) {
285 /* GPIOF_DIR_OUT */
Alexandre Courbotdef63432013-02-15 14:46:15 +0900286 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300287 }
288 return status;
289}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700290EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300291
David Brownelld8f388d2008-07-25 01:46:07 -0700292#ifdef CONFIG_GPIO_SYSFS
293
294/* lock protects against unexport_gpio() being called while
295 * sysfs files are active.
296 */
297static DEFINE_MUTEX(sysfs_lock);
298
299/*
300 * /sys/class/gpio/gpioN... only for GPIOs that are exported
301 * /direction
302 * * MAY BE OMITTED if kernel won't allow direction changes
303 * * is read/write as "in" or "out"
304 * * may also be written as "high" or "low", initializing
305 * output value as specified ("out" implies "low")
306 * /value
307 * * always readable, subject to hardware behavior
308 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700309 * /edge
310 * * configures behavior of poll(2) on /value
311 * * available only if pin can generate IRQs on input
312 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800313 * /active_low
314 * * configures polarity of /value
315 * * is read/write as zero/nonzero
316 * * also affects existing and subsequent "falling" and "rising"
317 * /edge configuration
David Brownelld8f388d2008-07-25 01:46:07 -0700318 */
319
320static ssize_t gpio_direction_show(struct device *dev,
321 struct device_attribute *attr, char *buf)
322{
Alexandre Courbotdef63432013-02-15 14:46:15 +0900323 const struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700324 ssize_t status;
325
326 mutex_lock(&sysfs_lock);
327
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900328 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700329 status = -EIO;
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900330 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +0900331 gpiod_get_direction(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700332 status = sprintf(buf, "%s\n",
333 test_bit(FLAG_IS_OUT, &desc->flags)
334 ? "out" : "in");
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900335 }
David Brownelld8f388d2008-07-25 01:46:07 -0700336
337 mutex_unlock(&sysfs_lock);
338 return status;
339}
340
341static ssize_t gpio_direction_store(struct device *dev,
342 struct device_attribute *attr, const char *buf, size_t size)
343{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900344 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700345 ssize_t status;
346
347 mutex_lock(&sysfs_lock);
348
349 if (!test_bit(FLAG_EXPORT, &desc->flags))
350 status = -EIO;
351 else if (sysfs_streq(buf, "high"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900352 status = gpiod_direction_output(desc, 1);
David Brownelld8f388d2008-07-25 01:46:07 -0700353 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900354 status = gpiod_direction_output(desc, 0);
David Brownelld8f388d2008-07-25 01:46:07 -0700355 else if (sysfs_streq(buf, "in"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900356 status = gpiod_direction_input(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700357 else
358 status = -EINVAL;
359
360 mutex_unlock(&sysfs_lock);
361 return status ? : size;
362}
363
Jani Nikula07697462009-12-15 16:46:20 -0800364static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d2008-07-25 01:46:07 -0700365 gpio_direction_show, gpio_direction_store);
366
367static ssize_t gpio_value_show(struct device *dev,
368 struct device_attribute *attr, char *buf)
369{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900370 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700371 ssize_t status;
372
373 mutex_lock(&sysfs_lock);
374
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700375 if (!test_bit(FLAG_EXPORT, &desc->flags))
David Brownelld8f388d2008-07-25 01:46:07 -0700376 status = -EIO;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700377 else
378 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
David Brownelld8f388d2008-07-25 01:46:07 -0700379
380 mutex_unlock(&sysfs_lock);
381 return status;
382}
383
384static ssize_t gpio_value_store(struct device *dev,
385 struct device_attribute *attr, const char *buf, size_t size)
386{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900387 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700388 ssize_t status;
389
390 mutex_lock(&sysfs_lock);
391
392 if (!test_bit(FLAG_EXPORT, &desc->flags))
393 status = -EIO;
394 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
395 status = -EPERM;
396 else {
397 long value;
398
Jingoo Hana3d88c92013-07-19 16:12:50 +0900399 status = kstrtol(buf, 0, &value);
David Brownelld8f388d2008-07-25 01:46:07 -0700400 if (status == 0) {
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700401 gpiod_set_value_cansleep(desc, value);
David Brownelld8f388d2008-07-25 01:46:07 -0700402 status = size;
403 }
404 }
405
406 mutex_unlock(&sysfs_lock);
407 return status;
408}
409
Jani Nikula07697462009-12-15 16:46:20 -0800410static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d2008-07-25 01:46:07 -0700411 gpio_value_show, gpio_value_store);
412
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700413static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
414{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700415 struct sysfs_dirent *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700416
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700417 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700418 return IRQ_HANDLED;
419}
420
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700421static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
422 unsigned long gpio_flags)
423{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700424 struct sysfs_dirent *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700425 unsigned long irq_flags;
426 int ret, irq, id;
427
428 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
429 return 0;
430
Alexandre Courbot372e7222013-02-03 01:29:29 +0900431 irq = gpiod_to_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700432 if (irq < 0)
433 return -EIO;
434
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700435 id = desc->flags >> ID_SHIFT;
436 value_sd = idr_find(&dirent_idr, id);
437 if (value_sd)
438 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700439
440 desc->flags &= ~GPIO_TRIGGER_MASK;
441
442 if (!gpio_flags) {
Linus Walleijd468bf92013-09-24 11:54:38 +0200443 gpiod_unlock_as_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700444 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700445 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700446 }
447
448 irq_flags = IRQF_SHARED;
449 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800450 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
451 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700452 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800453 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
454 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700455
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700456 if (!value_sd) {
Tejun Heo388975c2013-09-11 23:19:13 -0400457 value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700458 if (!value_sd) {
459 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700460 goto err_out;
461 }
462
Tejun Heo62f516b2013-02-27 17:04:06 -0800463 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
464 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700465 goto free_sd;
Tejun Heo62f516b2013-02-27 17:04:06 -0800466 id = ret;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700467
468 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700469 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700470
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700471 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700472 ret = -ERANGE;
473 goto free_id;
474 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700475 }
476
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700477 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700478 "gpiolib", value_sd);
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700479 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700480 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700481
Linus Walleijd468bf92013-09-24 11:54:38 +0200482 ret = gpiod_lock_as_irq(desc);
483 if (ret < 0) {
484 gpiod_warn(desc, "failed to flag the GPIO for IRQ\n");
485 goto free_id;
486 }
487
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700488 desc->flags |= gpio_flags;
489 return 0;
490
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700491free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700492 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700493 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700494free_sd:
495 if (value_sd)
496 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700497err_out:
498 return ret;
499}
500
501static const struct {
502 const char *name;
503 unsigned long flags;
504} trigger_types[] = {
505 { "none", 0 },
506 { "falling", BIT(FLAG_TRIG_FALL) },
507 { "rising", BIT(FLAG_TRIG_RISE) },
508 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
509};
510
511static ssize_t gpio_edge_show(struct device *dev,
512 struct device_attribute *attr, char *buf)
513{
514 const struct gpio_desc *desc = dev_get_drvdata(dev);
515 ssize_t status;
516
517 mutex_lock(&sysfs_lock);
518
519 if (!test_bit(FLAG_EXPORT, &desc->flags))
520 status = -EIO;
521 else {
522 int i;
523
524 status = 0;
525 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
526 if ((desc->flags & GPIO_TRIGGER_MASK)
527 == trigger_types[i].flags) {
528 status = sprintf(buf, "%s\n",
529 trigger_types[i].name);
530 break;
531 }
532 }
533
534 mutex_unlock(&sysfs_lock);
535 return status;
536}
537
538static ssize_t gpio_edge_store(struct device *dev,
539 struct device_attribute *attr, const char *buf, size_t size)
540{
541 struct gpio_desc *desc = dev_get_drvdata(dev);
542 ssize_t status;
543 int i;
544
545 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
546 if (sysfs_streq(trigger_types[i].name, buf))
547 goto found;
548 return -EINVAL;
549
550found:
551 mutex_lock(&sysfs_lock);
552
553 if (!test_bit(FLAG_EXPORT, &desc->flags))
554 status = -EIO;
555 else {
556 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
557 if (!status)
558 status = size;
559 }
560
561 mutex_unlock(&sysfs_lock);
562
563 return status;
564}
565
566static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
567
Jani Nikula07697462009-12-15 16:46:20 -0800568static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
569 int value)
570{
571 int status = 0;
572
573 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
574 return 0;
575
576 if (value)
577 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
578 else
579 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
580
581 /* reconfigure poll(2) support if enabled on one edge only */
582 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
583 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
584 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
585
586 gpio_setup_irq(desc, dev, 0);
587 status = gpio_setup_irq(desc, dev, trigger_flags);
588 }
589
590 return status;
591}
592
593static ssize_t gpio_active_low_show(struct device *dev,
594 struct device_attribute *attr, char *buf)
595{
596 const struct gpio_desc *desc = dev_get_drvdata(dev);
597 ssize_t status;
598
599 mutex_lock(&sysfs_lock);
600
601 if (!test_bit(FLAG_EXPORT, &desc->flags))
602 status = -EIO;
603 else
604 status = sprintf(buf, "%d\n",
605 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
606
607 mutex_unlock(&sysfs_lock);
608
609 return status;
610}
611
612static ssize_t gpio_active_low_store(struct device *dev,
613 struct device_attribute *attr, const char *buf, size_t size)
614{
615 struct gpio_desc *desc = dev_get_drvdata(dev);
616 ssize_t status;
617
618 mutex_lock(&sysfs_lock);
619
620 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
621 status = -EIO;
622 } else {
623 long value;
624
Jingoo Hana3d88c92013-07-19 16:12:50 +0900625 status = kstrtol(buf, 0, &value);
Jani Nikula07697462009-12-15 16:46:20 -0800626 if (status == 0)
627 status = sysfs_set_active_low(desc, dev, value != 0);
628 }
629
630 mutex_unlock(&sysfs_lock);
631
632 return status ? : size;
633}
634
635static const DEVICE_ATTR(active_low, 0644,
636 gpio_active_low_show, gpio_active_low_store);
637
David Brownelld8f388d2008-07-25 01:46:07 -0700638static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d2008-07-25 01:46:07 -0700639 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800640 &dev_attr_active_low.attr,
David Brownelld8f388d2008-07-25 01:46:07 -0700641 NULL,
642};
643
644static const struct attribute_group gpio_attr_group = {
645 .attrs = (struct attribute **) gpio_attrs,
646};
647
648/*
649 * /sys/class/gpio/gpiochipN/
650 * /base ... matching gpio_chip.base (N)
651 * /label ... matching gpio_chip.label
652 * /ngpio ... matching gpio_chip.ngpio
653 */
654
655static ssize_t chip_base_show(struct device *dev,
656 struct device_attribute *attr, char *buf)
657{
658 const struct gpio_chip *chip = dev_get_drvdata(dev);
659
660 return sprintf(buf, "%d\n", chip->base);
661}
662static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
663
664static ssize_t chip_label_show(struct device *dev,
665 struct device_attribute *attr, char *buf)
666{
667 const struct gpio_chip *chip = dev_get_drvdata(dev);
668
669 return sprintf(buf, "%s\n", chip->label ? : "");
670}
671static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
672
673static ssize_t chip_ngpio_show(struct device *dev,
674 struct device_attribute *attr, char *buf)
675{
676 const struct gpio_chip *chip = dev_get_drvdata(dev);
677
678 return sprintf(buf, "%u\n", chip->ngpio);
679}
680static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
681
682static const struct attribute *gpiochip_attrs[] = {
683 &dev_attr_base.attr,
684 &dev_attr_label.attr,
685 &dev_attr_ngpio.attr,
686 NULL,
687};
688
689static const struct attribute_group gpiochip_attr_group = {
690 .attrs = (struct attribute **) gpiochip_attrs,
691};
692
693/*
694 * /sys/class/gpio/export ... write-only
695 * integer N ... number of GPIO to export (full access)
696 * /sys/class/gpio/unexport ... write-only
697 * integer N ... number of GPIO to unexport
698 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100699static ssize_t export_store(struct class *class,
700 struct class_attribute *attr,
701 const char *buf, size_t len)
David Brownelld8f388d2008-07-25 01:46:07 -0700702{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900703 long gpio;
704 struct gpio_desc *desc;
705 int status;
David Brownelld8f388d2008-07-25 01:46:07 -0700706
Jingoo Hana3d88c92013-07-19 16:12:50 +0900707 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d2008-07-25 01:46:07 -0700708 if (status < 0)
709 goto done;
710
Alexandre Courbot372e7222013-02-03 01:29:29 +0900711 desc = gpio_to_desc(gpio);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900712 /* reject invalid GPIOs */
713 if (!desc) {
714 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
715 return -EINVAL;
716 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900717
David Brownelld8f388d2008-07-25 01:46:07 -0700718 /* No extra locking here; FLAG_SYSFS just signifies that the
719 * request and export were done by on behalf of userspace, so
720 * they may be undone on its behalf too.
721 */
722
Alexandre Courbot372e7222013-02-03 01:29:29 +0900723 status = gpiod_request(desc, "sysfs");
Mathias Nymanad2fab32012-10-25 14:03:03 +0300724 if (status < 0) {
725 if (status == -EPROBE_DEFER)
726 status = -ENODEV;
David Brownelld8f388d2008-07-25 01:46:07 -0700727 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +0300728 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900729 status = gpiod_export(desc, true);
David Brownelld8f388d2008-07-25 01:46:07 -0700730 if (status < 0)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900731 gpiod_free(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700732 else
Alexandre Courbot372e7222013-02-03 01:29:29 +0900733 set_bit(FLAG_SYSFS, &desc->flags);
David Brownelld8f388d2008-07-25 01:46:07 -0700734
735done:
736 if (status)
737 pr_debug("%s: status %d\n", __func__, status);
738 return status ? : len;
739}
740
Andi Kleen28812fe2010-01-05 12:48:07 +0100741static ssize_t unexport_store(struct class *class,
742 struct class_attribute *attr,
743 const char *buf, size_t len)
David Brownelld8f388d2008-07-25 01:46:07 -0700744{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900745 long gpio;
746 struct gpio_desc *desc;
747 int status;
David Brownelld8f388d2008-07-25 01:46:07 -0700748
Jingoo Hana3d88c92013-07-19 16:12:50 +0900749 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d2008-07-25 01:46:07 -0700750 if (status < 0)
751 goto done;
752
Alexandre Courbot372e7222013-02-03 01:29:29 +0900753 desc = gpio_to_desc(gpio);
David Brownelld8f388d2008-07-25 01:46:07 -0700754 /* reject bogus commands (gpio_unexport ignores them) */
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900755 if (!desc) {
756 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
757 return -EINVAL;
758 }
759
760 status = -EINVAL;
David Brownelld8f388d2008-07-25 01:46:07 -0700761
762 /* No extra locking here; FLAG_SYSFS just signifies that the
763 * request and export were done by on behalf of userspace, so
764 * they may be undone on its behalf too.
765 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900766 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700767 status = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900768 gpiod_free(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700769 }
770done:
771 if (status)
772 pr_debug("%s: status %d\n", __func__, status);
773 return status ? : len;
774}
775
776static struct class_attribute gpio_class_attrs[] = {
777 __ATTR(export, 0200, NULL, export_store),
778 __ATTR(unexport, 0200, NULL, unexport_store),
779 __ATTR_NULL,
780};
781
782static struct class gpio_class = {
783 .name = "gpio",
784 .owner = THIS_MODULE,
785
786 .class_attrs = gpio_class_attrs,
787};
788
789
790/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700791 * gpiod_export - export a GPIO through sysfs
David Brownelld8f388d2008-07-25 01:46:07 -0700792 * @gpio: gpio to make available, already requested
793 * @direction_may_change: true if userspace may change gpio direction
794 * Context: arch_initcall or later
795 *
796 * When drivers want to make a GPIO accessible to userspace after they
797 * have requested it -- perhaps while debugging, or as part of their
798 * public interface -- they may use this routine. If the GPIO can
799 * change direction (some can't) and the caller allows it, userspace
800 * will see "direction" sysfs attribute which may be used to change
801 * the gpio's direction. A "value" attribute will always be provided.
802 *
803 * Returns zero on success, else an error.
804 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700805int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
David Brownelld8f388d2008-07-25 01:46:07 -0700806{
807 unsigned long flags;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100808 int status;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700809 const char *ioname = NULL;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100810 struct device *dev;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900811 int offset;
David Brownelld8f388d2008-07-25 01:46:07 -0700812
813 /* can't export until sysfs is available ... */
814 if (!gpio_class.p) {
815 pr_debug("%s: called too early!\n", __func__);
816 return -ENOENT;
817 }
818
Alexandre Courbot372e7222013-02-03 01:29:29 +0900819 if (!desc) {
820 pr_debug("%s: invalid gpio descriptor\n", __func__);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100821 return -EINVAL;
822 }
David Brownelld8f388d2008-07-25 01:46:07 -0700823
824 mutex_lock(&sysfs_lock);
825
826 spin_lock_irqsave(&gpio_lock, flags);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100827 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
828 test_bit(FLAG_EXPORT, &desc->flags)) {
829 spin_unlock_irqrestore(&gpio_lock, flags);
Andy Shevchenko7589e592013-12-05 11:26:23 +0200830 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
831 __func__,
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100832 test_bit(FLAG_REQUESTED, &desc->flags),
833 test_bit(FLAG_EXPORT, &desc->flags));
Dan Carpenter529f2ad2012-10-26 09:59:43 +0300834 status = -EPERM;
835 goto fail_unlock;
David Brownelld8f388d2008-07-25 01:46:07 -0700836 }
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100837
838 if (!desc->chip->direction_input || !desc->chip->direction_output)
839 direction_may_change = false;
David Brownelld8f388d2008-07-25 01:46:07 -0700840 spin_unlock_irqrestore(&gpio_lock, flags);
841
Alexandre Courbot372e7222013-02-03 01:29:29 +0900842 offset = gpio_chip_hwgpio(desc);
843 if (desc->chip->names && desc->chip->names[offset])
844 ioname = desc->chip->names[offset];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700845
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100846 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Alexandre Courbot372e7222013-02-03 01:29:29 +0900847 desc, ioname ? ioname : "gpio%u",
848 desc_to_gpio(desc));
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100849 if (IS_ERR(dev)) {
850 status = PTR_ERR(dev);
851 goto fail_unlock;
David Brownelld8f388d2008-07-25 01:46:07 -0700852 }
853
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100854 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
David Brownelld8f388d2008-07-25 01:46:07 -0700855 if (status)
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100856 goto fail_unregister_device;
David Brownelld8f388d2008-07-25 01:46:07 -0700857
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100858 if (direction_may_change) {
859 status = device_create_file(dev, &dev_attr_direction);
860 if (status)
861 goto fail_unregister_device;
862 }
863
Alexandre Courbot372e7222013-02-03 01:29:29 +0900864 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100865 !test_bit(FLAG_IS_OUT, &desc->flags))) {
866 status = device_create_file(dev, &dev_attr_edge);
867 if (status)
868 goto fail_unregister_device;
869 }
870
871 set_bit(FLAG_EXPORT, &desc->flags);
872 mutex_unlock(&sysfs_lock);
873 return 0;
874
875fail_unregister_device:
876 device_unregister(dev);
877fail_unlock:
878 mutex_unlock(&sysfs_lock);
Andy Shevchenko7589e592013-12-05 11:26:23 +0200879 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
David Brownelld8f388d2008-07-25 01:46:07 -0700880 return status;
881}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700882EXPORT_SYMBOL_GPL(gpiod_export);
David Brownelld8f388d2008-07-25 01:46:07 -0700883
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100884static int match_export(struct device *dev, const void *data)
David Brownelld8f388d2008-07-25 01:46:07 -0700885{
886 return dev_get_drvdata(dev) == data;
887}
888
889/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700890 * gpiod_export_link - create a sysfs link to an exported GPIO node
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700891 * @dev: device under which to create symlink
892 * @name: name of the symlink
893 * @gpio: gpio to create symlink to, already exported
894 *
895 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
896 * node. Caller is responsible for unlinking.
897 *
898 * Returns zero on success, else an error.
899 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700900int gpiod_export_link(struct device *dev, const char *name,
901 struct gpio_desc *desc)
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700902{
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700903 int status = -EINVAL;
904
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900905 if (!desc) {
906 pr_warn("%s: invalid GPIO\n", __func__);
907 return -EINVAL;
908 }
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700909
910 mutex_lock(&sysfs_lock);
911
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700912 if (test_bit(FLAG_EXPORT, &desc->flags)) {
913 struct device *tdev;
914
915 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
916 if (tdev != NULL) {
917 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
918 name);
919 } else {
920 status = -ENODEV;
921 }
922 }
923
924 mutex_unlock(&sysfs_lock);
925
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700926 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200927 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700928
929 return status;
930}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700931EXPORT_SYMBOL_GPL(gpiod_export_link);
Jani Nikula07697462009-12-15 16:46:20 -0800932
933/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700934 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
Jani Nikula07697462009-12-15 16:46:20 -0800935 * @gpio: gpio to change
936 * @value: non-zero to use active low, i.e. inverted values
937 *
938 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
939 * The GPIO does not have to be exported yet. If poll(2) support has
940 * been enabled for either rising or falling edge, it will be
941 * reconfigured to follow the new polarity.
942 *
943 * Returns zero on success, else an error.
944 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700945int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
Jani Nikula07697462009-12-15 16:46:20 -0800946{
Jani Nikula07697462009-12-15 16:46:20 -0800947 struct device *dev = NULL;
948 int status = -EINVAL;
949
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900950 if (!desc) {
951 pr_warn("%s: invalid GPIO\n", __func__);
952 return -EINVAL;
953 }
Jani Nikula07697462009-12-15 16:46:20 -0800954
955 mutex_lock(&sysfs_lock);
956
Jani Nikula07697462009-12-15 16:46:20 -0800957 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800958 dev = class_find_device(&gpio_class, NULL, desc, match_export);
959 if (dev == NULL) {
960 status = -ENODEV;
961 goto unlock;
962 }
963 }
964
965 status = sysfs_set_active_low(desc, dev, value);
966
967unlock:
968 mutex_unlock(&sysfs_lock);
969
Jani Nikula07697462009-12-15 16:46:20 -0800970 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200971 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Jani Nikula07697462009-12-15 16:46:20 -0800972
973 return status;
974}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700975EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
Jani Nikula07697462009-12-15 16:46:20 -0800976
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700977/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700978 * gpiod_unexport - reverse effect of gpio_export()
David Brownelld8f388d2008-07-25 01:46:07 -0700979 * @gpio: gpio to make unavailable
980 *
981 * This is implicit on gpio_free().
982 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700983void gpiod_unexport(struct gpio_desc *desc)
David Brownelld8f388d2008-07-25 01:46:07 -0700984{
Jon Povey6a99ad42010-07-27 13:18:06 -0700985 int status = 0;
Ming Lei864533c2012-02-13 22:53:20 +0800986 struct device *dev = NULL;
David Brownelld8f388d2008-07-25 01:46:07 -0700987
Alexandre Courbot372e7222013-02-03 01:29:29 +0900988 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900989 pr_warn("%s: invalid GPIO\n", __func__);
990 return;
Jon Povey6a99ad42010-07-27 13:18:06 -0700991 }
David Brownelld8f388d2008-07-25 01:46:07 -0700992
993 mutex_lock(&sysfs_lock);
994
David Brownelld8f388d2008-07-25 01:46:07 -0700995 if (test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700996
997 dev = class_find_device(&gpio_class, NULL, desc, match_export);
998 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700999 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d2008-07-25 01:46:07 -07001000 clear_bit(FLAG_EXPORT, &desc->flags);
David Brownelld8f388d2008-07-25 01:46:07 -07001001 } else
1002 status = -ENODEV;
1003 }
1004
1005 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001006
Ming Lei864533c2012-02-13 22:53:20 +08001007 if (dev) {
1008 device_unregister(dev);
1009 put_device(dev);
1010 }
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001011
David Brownelld8f388d2008-07-25 01:46:07 -07001012 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001013 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001014}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001015EXPORT_SYMBOL_GPL(gpiod_unexport);
David Brownelld8f388d2008-07-25 01:46:07 -07001016
1017static int gpiochip_export(struct gpio_chip *chip)
1018{
1019 int status;
1020 struct device *dev;
1021
1022 /* Many systems register gpio chips for SOC support very early,
1023 * before driver model support is available. In those cases we
1024 * export this later, in gpiolib_sysfs_init() ... here we just
1025 * verify that _some_ field of gpio_class got initialized.
1026 */
1027 if (!gpio_class.p)
1028 return 0;
1029
1030 /* use chip->base for the ID; it's already known to be unique */
1031 mutex_lock(&sysfs_lock);
1032 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
1033 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001034 if (!IS_ERR(dev)) {
David Brownelld8f388d2008-07-25 01:46:07 -07001035 status = sysfs_create_group(&dev->kobj,
1036 &gpiochip_attr_group);
1037 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001038 status = PTR_ERR(dev);
David Brownelld8f388d2008-07-25 01:46:07 -07001039 chip->exported = (status == 0);
1040 mutex_unlock(&sysfs_lock);
1041
1042 if (status) {
1043 unsigned long flags;
1044 unsigned gpio;
1045
1046 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001047 gpio = 0;
1048 while (gpio < chip->ngpio)
1049 chip->desc[gpio++].chip = NULL;
David Brownelld8f388d2008-07-25 01:46:07 -07001050 spin_unlock_irqrestore(&gpio_lock, flags);
1051
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001052 chip_dbg(chip, "%s: status %d\n", __func__, status);
David Brownelld8f388d2008-07-25 01:46:07 -07001053 }
1054
1055 return status;
1056}
1057
1058static void gpiochip_unexport(struct gpio_chip *chip)
1059{
1060 int status;
1061 struct device *dev;
1062
1063 mutex_lock(&sysfs_lock);
1064 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1065 if (dev) {
1066 put_device(dev);
1067 device_unregister(dev);
Linus Walleij9fb1f392013-12-04 14:42:46 +01001068 chip->exported = false;
David Brownelld8f388d2008-07-25 01:46:07 -07001069 status = 0;
1070 } else
1071 status = -ENODEV;
1072 mutex_unlock(&sysfs_lock);
1073
1074 if (status)
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001075 chip_dbg(chip, "%s: status %d\n", __func__, status);
David Brownelld8f388d2008-07-25 01:46:07 -07001076}
1077
1078static int __init gpiolib_sysfs_init(void)
1079{
1080 int status;
1081 unsigned long flags;
Alexandre Courbot65493e32013-02-03 01:29:25 +09001082 struct gpio_chip *chip;
David Brownelld8f388d2008-07-25 01:46:07 -07001083
1084 status = class_register(&gpio_class);
1085 if (status < 0)
1086 return status;
1087
1088 /* Scan and register the gpio_chips which registered very
1089 * early (e.g. before the class_register above was called).
1090 *
1091 * We run before arch_initcall() so chip->dev nodes can have
1092 * registered, and so arch_initcall() can always gpio_export().
1093 */
1094 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot65493e32013-02-03 01:29:25 +09001095 list_for_each_entry(chip, &gpio_chips, list) {
David Brownelld8f388d2008-07-25 01:46:07 -07001096 if (!chip || chip->exported)
1097 continue;
1098
1099 spin_unlock_irqrestore(&gpio_lock, flags);
1100 status = gpiochip_export(chip);
1101 spin_lock_irqsave(&gpio_lock, flags);
1102 }
1103 spin_unlock_irqrestore(&gpio_lock, flags);
1104
1105
1106 return status;
1107}
1108postcore_initcall(gpiolib_sysfs_init);
1109
1110#else
1111static inline int gpiochip_export(struct gpio_chip *chip)
1112{
1113 return 0;
1114}
1115
1116static inline void gpiochip_unexport(struct gpio_chip *chip)
1117{
1118}
1119
1120#endif /* CONFIG_GPIO_SYSFS */
1121
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001122/*
1123 * Add a new chip to the global chips list, keeping the list of chips sorted
1124 * by base order.
1125 *
1126 * Return -EBUSY if the new chip overlaps with some other chip's integer
1127 * space.
1128 */
1129static int gpiochip_add_to_list(struct gpio_chip *chip)
1130{
1131 struct list_head *pos = &gpio_chips;
1132 struct gpio_chip *_chip;
1133 int err = 0;
1134
1135 /* find where to insert our chip */
1136 list_for_each(pos, &gpio_chips) {
1137 _chip = list_entry(pos, struct gpio_chip, list);
1138 /* shall we insert before _chip? */
1139 if (_chip->base >= chip->base + chip->ngpio)
1140 break;
1141 }
1142
1143 /* are we stepping on the chip right before? */
1144 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1145 _chip = list_entry(pos->prev, struct gpio_chip, list);
1146 if (_chip->base + _chip->ngpio > chip->base) {
1147 dev_err(chip->dev,
1148 "GPIO integer space overlap, cannot add chip\n");
1149 err = -EBUSY;
1150 }
1151 }
1152
1153 if (!err)
1154 list_add_tail(&chip->list, pos);
1155
1156 return err;
1157}
1158
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001159/**
David Brownelld2876d02008-02-04 22:28:20 -08001160 * gpiochip_add() - register a gpio_chip
1161 * @chip: the chip to register, with chip->base initialized
1162 * Context: potentially before irqs or kmalloc will work
1163 *
1164 * Returns a negative errno if the chip can't be registered, such as
1165 * because the chip->base is invalid or already associated with a
1166 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001167 *
David Brownelld8f388d2008-07-25 01:46:07 -07001168 * When gpiochip_add() is called very early during boot, so that GPIOs
1169 * can be freely used, the chip->dev device must be registered before
1170 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1171 * for GPIOs will fail rudely.
1172 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001173 * If chip->base is negative, this requests dynamic assignment of
1174 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001175 */
1176int gpiochip_add(struct gpio_chip *chip)
1177{
1178 unsigned long flags;
1179 int status = 0;
1180 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001181 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001182
Trent Piephobff5fda2008-05-23 13:04:44 -07001183 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001184 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001185 status = -EINVAL;
1186 goto fail;
1187 }
1188
1189 spin_lock_irqsave(&gpio_lock, flags);
1190
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001191 if (base < 0) {
1192 base = gpiochip_find_base(chip->ngpio);
1193 if (base < 0) {
1194 status = base;
David Brownelld8f388d2008-07-25 01:46:07 -07001195 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001196 }
1197 chip->base = base;
1198 }
1199
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001200 status = gpiochip_add_to_list(chip);
1201
David Brownelld2876d02008-02-04 22:28:20 -08001202 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001203 chip->desc = &gpio_desc[chip->base];
1204
1205 for (id = 0; id < chip->ngpio; id++) {
1206 struct gpio_desc *desc = &chip->desc[id];
1207 desc->chip = chip;
David Brownelld8f388d2008-07-25 01:46:07 -07001208
1209 /* REVISIT: most hardware initializes GPIOs as
1210 * inputs (often with pullups enabled) so power
1211 * usage is minimized. Linux code should set the
1212 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +03001213 * and in case chip->get_direction is not set,
David Brownelld8f388d2008-07-25 01:46:07 -07001214 * we may expose the wrong direction in sysfs.
1215 */
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001216 desc->flags = !chip->direction_input
David Brownelld8f388d2008-07-25 01:46:07 -07001217 ? (1 << FLAG_IS_OUT)
1218 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001219 }
1220 }
1221
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001222 spin_unlock_irqrestore(&gpio_lock, flags);
1223
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301224#ifdef CONFIG_PINCTRL
1225 INIT_LIST_HEAD(&chip->pin_ranges);
1226#endif
1227
Anton Vorontsov391c9702010-06-08 07:48:17 -06001228 of_gpiochip_add(chip);
1229
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001230 if (status)
1231 goto fail;
1232
1233 status = gpiochip_export(chip);
1234 if (status)
1235 goto fail;
1236
Andy Shevchenko7589e592013-12-05 11:26:23 +02001237 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -07001238 chip->base, chip->base + chip->ngpio - 1,
1239 chip->label ? : "generic");
1240
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001241 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001242
1243unlock:
1244 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld2876d02008-02-04 22:28:20 -08001245fail:
1246 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001247 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001248 chip->base, chip->base + chip->ngpio - 1,
1249 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001250 return status;
1251}
1252EXPORT_SYMBOL_GPL(gpiochip_add);
1253
1254/**
1255 * gpiochip_remove() - unregister a gpio_chip
1256 * @chip: the chip to unregister
1257 *
1258 * A gpio_chip with any GPIOs still requested may not be removed.
1259 */
1260int gpiochip_remove(struct gpio_chip *chip)
1261{
1262 unsigned long flags;
1263 int status = 0;
1264 unsigned id;
1265
1266 spin_lock_irqsave(&gpio_lock, flags);
1267
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001268 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001269 of_gpiochip_remove(chip);
1270
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001271 for (id = 0; id < chip->ngpio; id++) {
1272 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
David Brownelld2876d02008-02-04 22:28:20 -08001273 status = -EBUSY;
1274 break;
1275 }
1276 }
1277 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001278 for (id = 0; id < chip->ngpio; id++)
1279 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001280
1281 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -08001282 }
1283
1284 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d2008-07-25 01:46:07 -07001285
1286 if (status == 0)
1287 gpiochip_unexport(chip);
1288
David Brownelld2876d02008-02-04 22:28:20 -08001289 return status;
1290}
1291EXPORT_SYMBOL_GPL(gpiochip_remove);
1292
Grant Likely594fa262010-06-08 07:48:16 -06001293/**
1294 * gpiochip_find() - iterator for locating a specific gpio_chip
1295 * @data: data to pass to match function
1296 * @callback: Callback function to check gpio_chip
1297 *
1298 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1299 * determined by a user supplied @match callback. The callback should return
1300 * 0 if the device doesn't match and non-zero if it does. If the callback is
1301 * non-zero, this function will return to the caller and not iterate over any
1302 * more gpio_chips.
1303 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001304struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001305 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001306 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001307{
Alexandre Courbot125eef92013-02-03 01:29:26 +09001308 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001309 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001310
1311 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +09001312 list_for_each_entry(chip, &gpio_chips, list)
1313 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001314 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001315
1316 /* No match? */
1317 if (&chip->list == &gpio_chips)
1318 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001319 spin_unlock_irqrestore(&gpio_lock, flags);
1320
1321 return chip;
1322}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001323EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001324
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001325static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1326{
1327 const char *name = data;
1328
1329 return !strcmp(chip->label, name);
1330}
1331
1332static struct gpio_chip *find_chip_by_name(const char *name)
1333{
1334 return gpiochip_find((void *)name, gpiochip_match_name);
1335}
1336
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301337#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001338
Linus Walleij3f0f8672012-11-20 12:40:15 +01001339/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001340 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1341 * @chip: the gpiochip to add the range for
1342 * @pinctrl: the dev_name() of the pin controller to map to
1343 * @gpio_offset: the start offset in the current gpio_chip number space
1344 * @pin_group: name of the pin group inside the pin controller
1345 */
1346int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1347 struct pinctrl_dev *pctldev,
1348 unsigned int gpio_offset, const char *pin_group)
1349{
1350 struct gpio_pin_range *pin_range;
1351 int ret;
1352
1353 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1354 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001355 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001356 return -ENOMEM;
1357 }
1358
1359 /* Use local offset as range ID */
1360 pin_range->range.id = gpio_offset;
1361 pin_range->range.gc = chip;
1362 pin_range->range.name = chip->label;
1363 pin_range->range.base = chip->base + gpio_offset;
1364 pin_range->pctldev = pctldev;
1365
1366 ret = pinctrl_get_group_pins(pctldev, pin_group,
1367 &pin_range->range.pins,
1368 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001369 if (ret < 0) {
1370 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001371 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001372 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001373
1374 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1375
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001376 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1377 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001378 pinctrl_dev_get_devname(pctldev), pin_group);
1379
1380 list_add_tail(&pin_range->node, &chip->pin_ranges);
1381
1382 return 0;
1383}
1384EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1385
1386/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001387 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1388 * @chip: the gpiochip to add the range for
1389 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001390 * @gpio_offset: the start offset in the current gpio_chip number space
1391 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001392 * @npins: the number of pins from the offset of each pin space (GPIO and
1393 * pin controller) to accumulate in this range
1394 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001395int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001396 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001397 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301398{
1399 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001400 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301401
Linus Walleij3f0f8672012-11-20 12:40:15 +01001402 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301403 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001404 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001405 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301406 }
1407
Linus Walleij3f0f8672012-11-20 12:40:15 +01001408 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001409 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001410 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301411 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001412 pin_range->range.base = chip->base + gpio_offset;
1413 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301414 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001415 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301416 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001417 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001418 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001419 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001420 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001421 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001422 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001423 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1424 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001425 pinctl_name,
1426 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301427
1428 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001429
1430 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301431}
Linus Walleij165adc92012-11-06 14:49:39 +01001432EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301433
Linus Walleij3f0f8672012-11-20 12:40:15 +01001434/**
1435 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1436 * @chip: the chip to remove all the mappings for
1437 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301438void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1439{
1440 struct gpio_pin_range *pin_range, *tmp;
1441
1442 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1443 list_del(&pin_range->node);
1444 pinctrl_remove_gpio_range(pin_range->pctldev,
1445 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001446 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301447 }
1448}
Linus Walleij165adc92012-11-06 14:49:39 +01001449EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1450
1451#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301452
David Brownelld2876d02008-02-04 22:28:20 -08001453/* These "optional" allocation calls help prevent drivers from stomping
1454 * on each other, and help provide better diagnostics in debugfs.
1455 * They're called even less than the "set direction" calls.
1456 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001457static int gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001458{
David Brownell35e8bb52008-10-15 22:03:16 -07001459 struct gpio_chip *chip;
Mark Browne9354572012-07-09 12:22:56 +01001460 int status = -EPROBE_DEFER;
David Brownelld2876d02008-02-04 22:28:20 -08001461 unsigned long flags;
1462
Alexandre Courbot0204df42013-10-04 10:59:58 -07001463 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001464 pr_warn("%s: invalid GPIO\n", __func__);
1465 return -EINVAL;
1466 }
1467
David Brownelld2876d02008-02-04 22:28:20 -08001468 spin_lock_irqsave(&gpio_lock, flags);
1469
David Brownell35e8bb52008-10-15 22:03:16 -07001470 chip = desc->chip;
Alexandre Courbot0204df42013-10-04 10:59:58 -07001471 if (chip == NULL)
1472 goto done;
David Brownelld2876d02008-02-04 22:28:20 -08001473
David Brownell35e8bb52008-10-15 22:03:16 -07001474 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001475 goto done;
1476
David Brownelld2876d02008-02-04 22:28:20 -08001477 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001478 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001479 */
1480
1481 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1482 desc_set_label(desc, label ? : "?");
1483 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001484 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001485 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001486 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001487 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001488 }
1489
1490 if (chip->request) {
1491 /* chip->request may sleep */
1492 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001493 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001494 spin_lock_irqsave(&gpio_lock, flags);
1495
1496 if (status < 0) {
1497 desc_set_label(desc, NULL);
1498 module_put(chip->owner);
1499 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001500 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001501 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001502 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001503 if (chip->get_direction) {
1504 /* chip->get_direction may sleep */
1505 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001506 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001507 spin_lock_irqsave(&gpio_lock, flags);
1508 }
David Brownelld2876d02008-02-04 22:28:20 -08001509done:
1510 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001511 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001512 spin_unlock_irqrestore(&gpio_lock, flags);
1513 return status;
1514}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001515
1516int gpio_request(unsigned gpio, const char *label)
1517{
1518 return gpiod_request(gpio_to_desc(gpio), label);
1519}
David Brownelld2876d02008-02-04 22:28:20 -08001520EXPORT_SYMBOL_GPL(gpio_request);
1521
Alexandre Courbot372e7222013-02-03 01:29:29 +09001522static void gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001523{
1524 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001525 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001526
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001527 might_sleep();
1528
Alexandre Courbot372e7222013-02-03 01:29:29 +09001529 if (!desc) {
David Brownelld2876d02008-02-04 22:28:20 -08001530 WARN_ON(extra_checks);
1531 return;
1532 }
1533
Alexandre Courbot372e7222013-02-03 01:29:29 +09001534 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001535
David Brownelld2876d02008-02-04 22:28:20 -08001536 spin_lock_irqsave(&gpio_lock, flags);
1537
David Brownell35e8bb52008-10-15 22:03:16 -07001538 chip = desc->chip;
1539 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1540 if (chip->free) {
1541 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001542 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001543 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001544 spin_lock_irqsave(&gpio_lock, flags);
1545 }
David Brownelld2876d02008-02-04 22:28:20 -08001546 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001547 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001548 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001549 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301550 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301551 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001552 } else
David Brownelld2876d02008-02-04 22:28:20 -08001553 WARN_ON(extra_checks);
1554
1555 spin_unlock_irqrestore(&gpio_lock, flags);
1556}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001557
1558void gpio_free(unsigned gpio)
1559{
1560 gpiod_free(gpio_to_desc(gpio));
1561}
David Brownelld2876d02008-02-04 22:28:20 -08001562EXPORT_SYMBOL_GPL(gpio_free);
1563
Eric Miao3e45f1d2010-03-05 13:44:35 -08001564/**
1565 * gpio_request_one - request a single GPIO with initial configuration
1566 * @gpio: the GPIO number
1567 * @flags: GPIO configuration as specified by GPIOF_*
1568 * @label: a literal description string of this GPIO
1569 */
1570int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1571{
Alexandre Courbot372e7222013-02-03 01:29:29 +09001572 struct gpio_desc *desc;
Eric Miao3e45f1d2010-03-05 13:44:35 -08001573 int err;
1574
Alexandre Courbot372e7222013-02-03 01:29:29 +09001575 desc = gpio_to_desc(gpio);
1576
1577 err = gpiod_request(desc, label);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001578 if (err)
1579 return err;
1580
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301581 if (flags & GPIOF_OPEN_DRAIN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001582 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301583
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301584 if (flags & GPIOF_OPEN_SOURCE)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001585 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301586
Eric Miao3e45f1d2010-03-05 13:44:35 -08001587 if (flags & GPIOF_DIR_IN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001588 err = gpiod_direction_input(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001589 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001590 err = gpiod_direction_output(desc,
Eric Miao3e45f1d2010-03-05 13:44:35 -08001591 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1592
Aaro Koskinene2548112010-12-21 17:24:22 -08001593 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001594 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -08001595
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001596 if (flags & GPIOF_EXPORT) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001597 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001598 if (err)
1599 goto free_gpio;
1600 }
1601
1602 return 0;
1603
1604 free_gpio:
Alexandre Courbot372e7222013-02-03 01:29:29 +09001605 gpiod_free(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001606 return err;
1607}
1608EXPORT_SYMBOL_GPL(gpio_request_one);
1609
1610/**
1611 * gpio_request_array - request multiple GPIOs in a single call
1612 * @array: array of the 'struct gpio'
1613 * @num: how many GPIOs in the array
1614 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001615int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001616{
1617 int i, err;
1618
1619 for (i = 0; i < num; i++, array++) {
1620 err = gpio_request_one(array->gpio, array->flags, array->label);
1621 if (err)
1622 goto err_free;
1623 }
1624 return 0;
1625
1626err_free:
1627 while (i--)
1628 gpio_free((--array)->gpio);
1629 return err;
1630}
1631EXPORT_SYMBOL_GPL(gpio_request_array);
1632
1633/**
1634 * gpio_free_array - release multiple GPIOs in a single call
1635 * @array: array of the 'struct gpio'
1636 * @num: how many GPIOs in the array
1637 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001638void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001639{
1640 while (num--)
1641 gpio_free((array++)->gpio);
1642}
1643EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001644
1645/**
1646 * gpiochip_is_requested - return string iff signal was requested
1647 * @chip: controller managing the signal
1648 * @offset: of signal within controller's 0..(ngpio - 1) range
1649 *
1650 * Returns NULL if the GPIO is not currently requested, else a string.
1651 * If debugfs support is enabled, the string returned is the label passed
1652 * to gpio_request(); otherwise it is a meaningless constant.
1653 *
1654 * This function is for use by GPIO controller drivers. The label can
1655 * help with diagnostics, and knowing that the signal is used as a GPIO
1656 * can help avoid accidentally multiplexing it to another controller.
1657 */
1658const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1659{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001660 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001661
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001662 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -08001663 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001664
1665 desc = &chip->desc[offset];
1666
Alexandre Courbot372e7222013-02-03 01:29:29 +09001667 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001668 return NULL;
1669#ifdef CONFIG_DEBUG_FS
Alexandre Courbot372e7222013-02-03 01:29:29 +09001670 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001671#else
1672 return "?";
1673#endif
1674}
1675EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1676
1677
1678/* Drivers MUST set GPIO direction before making get/set calls. In
1679 * some cases this is done in early boot, before IRQs are enabled.
1680 *
1681 * As a rule these aren't called more than once (except for drivers
1682 * using the open-drain emulation idiom) so these are natural places
1683 * to accumulate extra debugging checks. Note that we can't (yet)
1684 * rely on gpio_request() having been called beforehand.
1685 */
1686
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001687/**
1688 * gpiod_direction_input - set the GPIO direction to input
1689 * @desc: GPIO to set to input
1690 *
1691 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1692 * be called safely on it.
1693 *
1694 * Return 0 in case of success, else an error code.
1695 */
1696int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001697{
1698 unsigned long flags;
1699 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001700 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001701 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001702
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001703 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001704 pr_warn("%s: invalid GPIO\n", __func__);
1705 return -EINVAL;
1706 }
1707
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001708 chip = desc->chip;
1709 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001710 gpiod_warn(desc,
1711 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001712 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001713 return -EIO;
1714 }
1715
David Brownelld2876d02008-02-04 22:28:20 -08001716 spin_lock_irqsave(&gpio_lock, flags);
1717
Alexandre Courbot372e7222013-02-03 01:29:29 +09001718 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001719 if (status < 0)
1720 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001721
1722 /* now we know the gpio is valid and chip won't vanish */
1723
1724 spin_unlock_irqrestore(&gpio_lock, flags);
1725
David Brownell9c4ba942010-08-10 18:02:24 -07001726 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001727
Alexandre Courbot372e7222013-02-03 01:29:29 +09001728 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001729 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001730 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001731 if (status < 0) {
Andy Shevchenko7589e592013-12-05 11:26:23 +02001732 gpiod_dbg(desc, "%s: chip request fail, %d\n",
1733 __func__, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001734 /* and it's not available to anyone else ...
1735 * gpio_request() is the fully clean solution.
1736 */
1737 goto lose;
1738 }
1739 }
1740
Alexandre Courbot372e7222013-02-03 01:29:29 +09001741 status = chip->direction_input(chip, offset);
David Brownelld2876d02008-02-04 22:28:20 -08001742 if (status == 0)
1743 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001744
Alexandre Courbot372e7222013-02-03 01:29:29 +09001745 trace_gpio_direction(desc_to_gpio(desc), 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001746lose:
David Brownelld2876d02008-02-04 22:28:20 -08001747 return status;
1748fail:
1749 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001750 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001751 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001752 return status;
1753}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001754EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001755
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001756/**
1757 * gpiod_direction_output - set the GPIO direction to input
1758 * @desc: GPIO to set to output
1759 * @value: initial output value of the GPIO
1760 *
1761 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1762 * be called safely on it. The initial value of the output must be specified.
1763 *
1764 * Return 0 in case of success, else an error code.
1765 */
1766int gpiod_direction_output(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001767{
1768 unsigned long flags;
1769 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001770 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001771 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001772
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001773 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001774 pr_warn("%s: invalid GPIO\n", __func__);
1775 return -EINVAL;
1776 }
1777
Linus Walleijd468bf92013-09-24 11:54:38 +02001778 /* GPIOs used for IRQs shall not be set as output */
1779 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1780 gpiod_err(desc,
1781 "%s: tried to set a GPIO tied to an IRQ as output\n",
1782 __func__);
1783 return -EIO;
1784 }
1785
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301786 /* Open drain pin should not be driven to 1 */
1787 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001788 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301789
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301790 /* Open source pin should not be driven to 0 */
1791 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001792 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301793
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001794 chip = desc->chip;
1795 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001796 gpiod_warn(desc,
1797 "%s: missing set() or direction_output() operations\n",
1798 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001799 return -EIO;
1800 }
1801
David Brownelld2876d02008-02-04 22:28:20 -08001802 spin_lock_irqsave(&gpio_lock, flags);
1803
Alexandre Courbot372e7222013-02-03 01:29:29 +09001804 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001805 if (status < 0)
1806 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001807
1808 /* now we know the gpio is valid and chip won't vanish */
1809
1810 spin_unlock_irqrestore(&gpio_lock, flags);
1811
David Brownell9c4ba942010-08-10 18:02:24 -07001812 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001813
Alexandre Courbot372e7222013-02-03 01:29:29 +09001814 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001815 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001816 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001817 if (status < 0) {
Andy Shevchenko7589e592013-12-05 11:26:23 +02001818 gpiod_dbg(desc, "%s: chip request fail, %d\n",
1819 __func__, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001820 /* and it's not available to anyone else ...
1821 * gpio_request() is the fully clean solution.
1822 */
1823 goto lose;
1824 }
1825 }
1826
Alexandre Courbot372e7222013-02-03 01:29:29 +09001827 status = chip->direction_output(chip, offset, value);
David Brownelld2876d02008-02-04 22:28:20 -08001828 if (status == 0)
1829 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001830 trace_gpio_value(desc_to_gpio(desc), 0, value);
1831 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001832lose:
David Brownelld2876d02008-02-04 22:28:20 -08001833 return status;
1834fail:
1835 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001836 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001837 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001838 return status;
1839}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001840EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001841
Felipe Balbic4b5be92010-05-26 14:42:23 -07001842/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001843 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001844 * @gpio: the gpio to set debounce time
1845 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001846 *
1847 * returns -ENOTSUPP if the controller does not support setting
1848 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001849 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001850int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001851{
1852 unsigned long flags;
1853 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001854 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001855 int offset;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001856
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001857 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001858 pr_warn("%s: invalid GPIO\n", __func__);
1859 return -EINVAL;
1860 }
1861
Felipe Balbic4b5be92010-05-26 14:42:23 -07001862 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001863 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001864 gpiod_dbg(desc,
1865 "%s: missing set() or set_debounce() operations\n",
1866 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001867 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001868 }
1869
1870 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001871
1872 status = gpio_ensure_requested(desc);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001873 if (status < 0)
1874 goto fail;
1875
1876 /* now we know the gpio is valid and chip won't vanish */
1877
1878 spin_unlock_irqrestore(&gpio_lock, flags);
1879
David Brownell9c4ba942010-08-10 18:02:24 -07001880 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001881
Alexandre Courbot372e7222013-02-03 01:29:29 +09001882 offset = gpio_chip_hwgpio(desc);
1883 return chip->set_debounce(chip, offset, debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001884
1885fail:
1886 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001887 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001888 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001889
1890 return status;
1891}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001892EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001893
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001894/**
1895 * gpiod_is_active_low - test whether a GPIO is active-low or not
1896 * @desc: the gpio descriptor to test
1897 *
1898 * Returns 1 if the GPIO is active-low, 0 otherwise.
1899 */
1900int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001901{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001902 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001903}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001904EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001905
1906/* I/O calls are only valid after configuration completed; the relevant
1907 * "is this a valid GPIO" error checks should already have been done.
1908 *
1909 * "Get" operations are often inlinable as reading a pin value register,
1910 * and masking the relevant bit in that register.
1911 *
1912 * When "set" operations are inlinable, they involve writing that mask to
1913 * one register to set a low value, or a different register to set it high.
1914 * Otherwise locking is needed, so there may be little value to inlining.
1915 *
1916 *------------------------------------------------------------------------
1917 *
1918 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1919 * have requested the GPIO. That can include implicit requesting by
1920 * a direction setting call. Marking a gpio as requested locks its chip
1921 * in memory, guaranteeing that these table lookups need no more locking
1922 * and that gpiochip_remove() will fail.
1923 *
1924 * REVISIT when debugging, consider adding some instrumentation to ensure
1925 * that the GPIO was actually requested.
1926 */
1927
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001928static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001929{
1930 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001931 int value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001932 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001933
Alexandre Courbot372e7222013-02-03 01:29:29 +09001934 chip = desc->chip;
1935 offset = gpio_chip_hwgpio(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001936 value = chip->get ? chip->get(chip, offset) : 0;
1937 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001938 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001939}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001940
David Brownelld2876d02008-02-04 22:28:20 -08001941/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001942 * gpiod_get_raw_value() - return a gpio's raw value
1943 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001944 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001945 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1946 * its ACTIVE_LOW status.
1947 *
1948 * This function should be called from contexts where we cannot sleep, and will
1949 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001950 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001951int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001952{
David Brownelld2876d02008-02-04 22:28:20 -08001953 if (!desc)
1954 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001955 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001956 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001957 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001958}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001959EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001960
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001961/**
1962 * gpiod_get_value() - return a gpio's value
1963 * @desc: gpio whose value will be returned
1964 *
1965 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1966 * account.
1967 *
1968 * This function should be called from contexts where we cannot sleep, and will
1969 * complain if the GPIO chip functions potentially sleep.
1970 */
1971int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001972{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001973 int value;
1974 if (!desc)
1975 return 0;
1976 /* Should be using gpio_get_value_cansleep() */
1977 WARN_ON(desc->chip->can_sleep);
1978
1979 value = _gpiod_get_raw_value(desc);
1980 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1981 value = !value;
1982
1983 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001984}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001985EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001986
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301987/*
1988 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001989 * @desc: gpio descriptor whose state need to be set.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301990 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1991 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001992static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301993{
1994 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001995 struct gpio_chip *chip = desc->chip;
1996 int offset = gpio_chip_hwgpio(desc);
1997
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301998 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001999 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302000 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002001 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302002 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002003 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302004 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002005 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302006 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002007 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302008 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002009 gpiod_err(desc,
2010 "%s: Error in set_value for open drain err %d\n",
2011 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302012}
2013
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302014/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002015 * _gpio_set_open_source_value() - Set the open source gpio's value.
2016 * @desc: gpio descriptor whose state need to be set.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302017 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
2018 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09002019static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302020{
2021 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002022 struct gpio_chip *chip = desc->chip;
2023 int offset = gpio_chip_hwgpio(desc);
2024
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302025 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002026 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302027 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002028 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302029 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002030 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302031 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002032 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302033 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002034 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302035 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002036 gpiod_err(desc,
2037 "%s: Error in set_value for open source err %d\n",
2038 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302039}
2040
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002041static void _gpiod_set_raw_value(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002042{
2043 struct gpio_chip *chip;
2044
Alexandre Courbot372e7222013-02-03 01:29:29 +09002045 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002046 trace_gpio_value(desc_to_gpio(desc), 0, value);
2047 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2048 _gpio_set_open_drain_value(desc, value);
2049 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2050 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302051 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002052 chip->set(chip, gpio_chip_hwgpio(desc), value);
2053}
2054
David Brownelld2876d02008-02-04 22:28:20 -08002055/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002056 * gpiod_set_raw_value() - assign a gpio's raw value
2057 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002058 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002059 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002060 * Set the raw value of the GPIO, i.e. the value of its physical line without
2061 * regard for its ACTIVE_LOW status.
2062 *
2063 * This function should be called from contexts where we cannot sleep, and will
2064 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002065 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002066void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002067{
David Brownelld2876d02008-02-04 22:28:20 -08002068 if (!desc)
2069 return;
David Brownelld2876d02008-02-04 22:28:20 -08002070 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09002071 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002072 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002073}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002074EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002075
2076/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002077 * gpiod_set_value() - assign a gpio's value
2078 * @desc: gpio whose value will be assigned
2079 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002080 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002081 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2082 * account
2083 *
2084 * This function should be called from contexts where we cannot sleep, and will
2085 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002086 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002087void gpiod_set_value(struct gpio_desc *desc, int value)
2088{
2089 if (!desc)
2090 return;
2091 /* Should be using gpio_set_value_cansleep() */
2092 WARN_ON(desc->chip->can_sleep);
2093 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2094 value = !value;
2095 _gpiod_set_raw_value(desc, value);
2096}
2097EXPORT_SYMBOL_GPL(gpiod_set_value);
2098
2099/**
2100 * gpiod_cansleep() - report whether gpio value access may sleep
2101 * @desc: gpio to check
2102 *
2103 */
2104int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002105{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002106 if (!desc)
2107 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002108 return desc->chip->can_sleep;
2109}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002110EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002111
David Brownell0f6d5042008-10-15 22:03:14 -07002112/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002113 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2114 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002115 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002116 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2117 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002118 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002119int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002120{
2121 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002122 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002123
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002124 if (!desc)
2125 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002126 chip = desc->chip;
2127 offset = gpio_chip_hwgpio(desc);
2128 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
2129}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002130EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002131
Linus Walleijd468bf92013-09-24 11:54:38 +02002132/**
2133 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
2134 * @gpio: the GPIO line to lock as used for IRQ
2135 *
2136 * This is used directly by GPIO drivers that want to lock down
2137 * a certain GPIO line to be used as IRQs, for example in the
2138 * .to_irq() callback of their gpio_chip, or in the .irq_enable()
2139 * of its irq_chip implementation if the GPIO is known from that
2140 * code.
David Brownelld2876d02008-02-04 22:28:20 -08002141 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002142int gpiod_lock_as_irq(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002143{
Linus Walleijd468bf92013-09-24 11:54:38 +02002144 if (!desc)
2145 return -EINVAL;
2146
2147 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
2148 gpiod_err(desc,
2149 "%s: tried to flag a GPIO set as output for IRQ\n",
2150 __func__);
2151 return -EIO;
2152 }
2153
2154 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
2155 return 0;
2156}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002157EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002158
2159int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
2160{
2161 return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset));
2162}
2163EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
2164
2165/**
2166 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
2167 * @gpio: the GPIO line to unlock from IRQ usage
2168 *
2169 * This is used directly by GPIO drivers that want to indicate
2170 * that a certain GPIO is no longer used exclusively for IRQ.
2171 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002172void gpiod_unlock_as_irq(struct gpio_desc *desc)
Linus Walleijd468bf92013-09-24 11:54:38 +02002173{
2174 if (!desc)
2175 return;
2176
2177 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2178}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002179EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002180
2181void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2182{
2183 return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset));
2184}
2185EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
David Brownelld2876d02008-02-04 22:28:20 -08002186
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002187/**
2188 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2189 * @desc: gpio whose value will be returned
2190 *
2191 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2192 * its ACTIVE_LOW status.
2193 *
2194 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002195 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002196int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002197{
David Brownelld2876d02008-02-04 22:28:20 -08002198 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002199 if (!desc)
2200 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002201 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002202}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002203EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002204
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002205/**
2206 * gpiod_get_value_cansleep() - return a gpio's value
2207 * @desc: gpio whose value will be returned
2208 *
2209 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2210 * account.
2211 *
2212 * This function is to be called from contexts that can sleep.
2213 */
2214int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002215{
David Brownelld2876d02008-02-04 22:28:20 -08002216 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002217
2218 might_sleep_if(extra_checks);
2219 if (!desc)
2220 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002221
2222 value = _gpiod_get_raw_value(desc);
2223 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2224 value = !value;
2225
David Brownelld2876d02008-02-04 22:28:20 -08002226 return value;
2227}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002228EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002229
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002230/**
2231 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2232 * @desc: gpio whose value will be assigned
2233 * @value: value to assign
2234 *
2235 * Set the raw value of the GPIO, i.e. the value of its physical line without
2236 * regard for its ACTIVE_LOW status.
2237 *
2238 * This function is to be called from contexts that can sleep.
2239 */
2240void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002241{
David Brownelld2876d02008-02-04 22:28:20 -08002242 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002243 if (!desc)
2244 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002245 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002246}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002247EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002248
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002249/**
2250 * gpiod_set_value_cansleep() - assign a gpio's value
2251 * @desc: gpio whose value will be assigned
2252 * @value: value to assign
2253 *
2254 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2255 * account
2256 *
2257 * This function is to be called from contexts that can sleep.
2258 */
2259void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002260{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002261 might_sleep_if(extra_checks);
2262 if (!desc)
2263 return;
2264
2265 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2266 value = !value;
2267 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002268}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002269EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002270
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002271/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002272 * gpiod_add_lookup_table() - register GPIO device consumers
2273 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002274 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002275void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002276{
2277 mutex_lock(&gpio_lookup_lock);
2278
Alexandre Courbotad824782013-12-03 12:20:11 +09002279 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002280
2281 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002282}
2283
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002284#ifdef CONFIG_OF
2285static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002286 unsigned int idx,
2287 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002288{
2289 char prop_name[32]; /* 32 is max size of property name */
2290 enum of_gpio_flags of_flags;
2291 struct gpio_desc *desc;
2292
2293 if (con_id)
2294 snprintf(prop_name, 32, "%s-gpios", con_id);
2295 else
2296 snprintf(prop_name, 32, "gpios");
2297
2298 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2299 &of_flags);
2300
2301 if (IS_ERR(desc))
2302 return desc;
2303
2304 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002305 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002306
2307 return desc;
2308}
2309#else
2310static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot209e64f2013-11-19 10:37:29 +09002311 unsigned int idx,
2312 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002313{
2314 return ERR_PTR(-ENODEV);
2315}
2316#endif
2317
Mika Westerberg81f59e92013-10-10 11:01:09 +03002318static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002319 unsigned int idx,
2320 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03002321{
Mika Westerberge01f4402013-10-10 11:01:10 +03002322 struct acpi_gpio_info info;
2323 struct gpio_desc *desc;
2324
2325 desc = acpi_get_gpiod_by_index(dev, idx, &info);
2326 if (IS_ERR(desc))
2327 return desc;
2328
2329 if (info.gpioint && info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002330 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002331
2332 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002333}
2334
Alexandre Courbotad824782013-12-03 12:20:11 +09002335static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2336{
2337 const char *dev_id = dev ? dev_name(dev) : NULL;
2338 struct gpiod_lookup_table *table;
2339
2340 mutex_lock(&gpio_lookup_lock);
2341
2342 list_for_each_entry(table, &gpio_lookup_list, list) {
2343 if (table->dev_id && dev_id) {
2344 /*
2345 * Valid strings on both ends, must be identical to have
2346 * a match
2347 */
2348 if (!strcmp(table->dev_id, dev_id))
2349 goto found;
2350 } else {
2351 /*
2352 * One of the pointers is NULL, so both must be to have
2353 * a match
2354 */
2355 if (dev_id == table->dev_id)
2356 goto found;
2357 }
2358 }
2359 table = NULL;
2360
2361found:
2362 mutex_unlock(&gpio_lookup_lock);
2363 return table;
2364}
2365
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002366static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002367 unsigned int idx,
2368 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002369{
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002370 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Alexandre Courbotad824782013-12-03 12:20:11 +09002371 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002372 struct gpiod_lookup *p;
2373
Alexandre Courbotad824782013-12-03 12:20:11 +09002374 table = gpiod_find_lookup_table(dev);
2375 if (!table)
2376 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002377
Alexandre Courbotad824782013-12-03 12:20:11 +09002378 for (p = &table->table[0]; p->chip_label; p++) {
2379 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002380
Alexandre Courbotad824782013-12-03 12:20:11 +09002381 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002382 if (p->idx != idx)
2383 continue;
2384
Alexandre Courbotad824782013-12-03 12:20:11 +09002385 /* If the lookup entry has a con_id, require exact match */
2386 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2387 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002388
Alexandre Courbotad824782013-12-03 12:20:11 +09002389 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002390
Alexandre Courbotad824782013-12-03 12:20:11 +09002391 if (!chip) {
2392 dev_warn(dev, "cannot find GPIO chip %s\n",
2393 p->chip_label);
2394 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002395 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002396
Alexandre Courbotad824782013-12-03 12:20:11 +09002397 if (chip->ngpio <= p->chip_hwnum) {
2398 dev_warn(dev, "GPIO chip %s has %d GPIOs\n",
2399 chip->label, chip->ngpio);
2400 continue;
2401 }
2402
2403 desc = gpiochip_offset_to_desc(chip, p->chip_hwnum);
2404 *flags = p->flags;
2405 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002406
2407 return desc;
2408}
2409
2410/**
2411 * gpio_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002412 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002413 * @con_id: function within the GPIO consumer
2414 *
2415 * Return the GPIO descriptor corresponding to the function con_id of device
2416 * dev, or an IS_ERR() condition if an error occured.
2417 */
2418struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id)
2419{
2420 return gpiod_get_index(dev, con_id, 0);
2421}
2422EXPORT_SYMBOL_GPL(gpiod_get);
2423
2424/**
2425 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2426 * @dev: GPIO consumer
2427 * @con_id: function within the GPIO consumer
2428 * @idx: index of the GPIO to obtain in the consumer
2429 *
2430 * This variant of gpiod_get() allows to access GPIOs other than the first
2431 * defined one for functions that define several GPIOs.
2432 *
2433 * Return a valid GPIO descriptor, or an IS_ERR() condition in case of error.
2434 */
2435struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2436 const char *con_id,
2437 unsigned int idx)
2438{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002439 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002440 int status;
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002441 enum gpio_lookup_flags flags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002442
2443 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2444
2445 /* Using device tree? */
2446 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
2447 dev_dbg(dev, "using device tree for GPIO lookup\n");
2448 desc = of_find_gpio(dev, con_id, idx, &flags);
Mika Westerberg81f59e92013-10-10 11:01:09 +03002449 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
2450 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2451 desc = acpi_find_gpio(dev, con_id, idx, &flags);
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002452 }
2453
2454 /*
2455 * Either we are not using DT or ACPI, or their lookup did not return
2456 * a result. In that case, use platform lookup as a fallback.
2457 */
2458 if (!desc || IS_ERR(desc)) {
2459 struct gpio_desc *pdesc;
Andy Shevchenko7589e592013-12-05 11:26:23 +02002460
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002461 dev_dbg(dev, "using lookup tables for GPIO lookup");
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002462 pdesc = gpiod_find(dev, con_id, idx, &flags);
Andy Shevchenko7589e592013-12-05 11:26:23 +02002463
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002464 /* If used as fallback, do not replace the previous error */
2465 if (!IS_ERR(pdesc) || !desc)
2466 desc = pdesc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002467 }
2468
2469 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002470 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002471 return desc;
2472 }
2473
2474 status = gpiod_request(desc, con_id);
2475
2476 if (status < 0)
2477 return ERR_PTR(status);
2478
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002479 if (flags & GPIO_ACTIVE_LOW)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002480 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002481 if (flags & GPIO_OPEN_DRAIN)
2482 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2483 if (flags & GPIO_OPEN_SOURCE)
2484 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002485
2486 return desc;
2487}
2488EXPORT_SYMBOL_GPL(gpiod_get_index);
2489
2490/**
2491 * gpiod_put - dispose of a GPIO descriptor
2492 * @desc: GPIO descriptor to dispose of
2493 *
2494 * No descriptor can be used after gpiod_put() has been called on it.
2495 */
2496void gpiod_put(struct gpio_desc *desc)
2497{
2498 gpiod_free(desc);
2499}
2500EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002501
David Brownelld2876d02008-02-04 22:28:20 -08002502#ifdef CONFIG_DEBUG_FS
2503
2504static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2505{
2506 unsigned i;
2507 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002508 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002509 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002510 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002511
2512 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2513 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2514 continue;
2515
Alexandre Courbot372e7222013-02-03 01:29:29 +09002516 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002517 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002518 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2519 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002520 gpio, gdesc->label,
2521 is_out ? "out" : "in ",
2522 chip->get
2523 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002524 : "? ",
2525 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002526 seq_printf(s, "\n");
2527 }
2528}
2529
Thierry Redingf9c4a312012-04-12 13:26:01 +02002530static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002531{
Grant Likely362432a2013-02-09 09:41:49 +00002532 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002533 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002534 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002535
2536 s->private = "";
2537
Grant Likely362432a2013-02-09 09:41:49 +00002538 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002539 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002540 if (index-- == 0) {
2541 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002542 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002543 }
2544 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002545
2546 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002547}
2548
2549static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2550{
Grant Likely362432a2013-02-09 09:41:49 +00002551 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002552 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002553 void *ret = NULL;
2554
Grant Likely362432a2013-02-09 09:41:49 +00002555 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002556 if (list_is_last(&chip->list, &gpio_chips))
2557 ret = NULL;
2558 else
2559 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002560 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002561
2562 s->private = "\n";
2563 ++*pos;
2564
2565 return ret;
2566}
2567
2568static void gpiolib_seq_stop(struct seq_file *s, void *v)
2569{
2570}
2571
2572static int gpiolib_seq_show(struct seq_file *s, void *v)
2573{
2574 struct gpio_chip *chip = v;
2575 struct device *dev;
2576
2577 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2578 chip->base, chip->base + chip->ngpio - 1);
2579 dev = chip->dev;
2580 if (dev)
2581 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2582 dev_name(dev));
2583 if (chip->label)
2584 seq_printf(s, ", %s", chip->label);
2585 if (chip->can_sleep)
2586 seq_printf(s, ", can sleep");
2587 seq_printf(s, ":\n");
2588
2589 if (chip->dbg_show)
2590 chip->dbg_show(s, chip);
2591 else
2592 gpiolib_dbg_show(s, chip);
2593
David Brownelld2876d02008-02-04 22:28:20 -08002594 return 0;
2595}
2596
Thierry Redingf9c4a312012-04-12 13:26:01 +02002597static const struct seq_operations gpiolib_seq_ops = {
2598 .start = gpiolib_seq_start,
2599 .next = gpiolib_seq_next,
2600 .stop = gpiolib_seq_stop,
2601 .show = gpiolib_seq_show,
2602};
2603
David Brownelld2876d02008-02-04 22:28:20 -08002604static int gpiolib_open(struct inode *inode, struct file *file)
2605{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002606 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002607}
2608
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002609static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002610 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002611 .open = gpiolib_open,
2612 .read = seq_read,
2613 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002614 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002615};
2616
2617static int __init gpiolib_debugfs_init(void)
2618{
2619 /* /sys/kernel/debug/gpio */
2620 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2621 NULL, NULL, &gpiolib_operations);
2622 return 0;
2623}
2624subsys_initcall(gpiolib_debugfs_init);
2625
2626#endif /* DEBUG_FS */