blob: 9263c7b359b723ae08c4f1b5a37a2457e5489549 [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>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
David Brownelld2876d02008-02-04 22:28:20 -080015
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060016#define CREATE_TRACE_POINTS
17#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080018
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070019/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080020 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070021 * The GPIO programming interface allows for inlining speed-critical
22 * get/set operations for common cases, so that access to SOC-integrated
23 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080024 */
25
26
27/* When debugging, extend minimal trust to callers and platform code.
28 * Also emit diagnostic messages that may help initial bringup, when
29 * board setup or driver bugs are most common.
30 *
31 * Otherwise, minimize overhead in what may be bitbanging codepaths.
32 */
33#ifdef DEBUG
34#define extra_checks 1
35#else
36#define extra_checks 0
37#endif
38
39/* gpio_lock prevents conflicts during gpio_desc[] table updates.
40 * While any GPIO is requested, its gpio_chip is not removable;
41 * each GPIO's "requested" flag serves as a lock and refcount.
42 */
43static DEFINE_SPINLOCK(gpio_lock);
44
45struct gpio_desc {
46 struct gpio_chip *chip;
47 unsigned long flags;
48/* flag symbols are bit numbers */
49#define FLAG_REQUESTED 0
50#define FLAG_IS_OUT 1
Alexandre Courbot710b40e2013-02-02 23:44:06 +090051#define FLAG_EXPORT 2 /* protected by sysfs_lock */
52#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
53#define FLAG_TRIG_FALL 4 /* trigger on falling edge */
54#define FLAG_TRIG_RISE 5 /* trigger on rising edge */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070055#define FLAG_ACTIVE_LOW 6 /* value has active low */
Alexandre Courbot710b40e2013-02-02 23:44:06 +090056#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
57#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
Linus Walleijd468bf92013-09-24 11:54:38 +020058#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070059
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070060#define ID_SHIFT 16 /* add new flags before this one */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070061
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070062#define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
Daniel Glöcknerff77c352009-09-22 16:46:38 -070063#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
David Brownelld2876d02008-02-04 22:28:20 -080064
65#ifdef CONFIG_DEBUG_FS
66 const char *label;
67#endif
68};
69static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
70
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +090071#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
72
Alexandre Courbotbae48da2013-10-17 10:21:38 -070073static DEFINE_MUTEX(gpio_lookup_lock);
74static LIST_HEAD(gpio_lookup_list);
Alexandre Courbot1a989d02013-02-03 01:29:24 +090075static LIST_HEAD(gpio_chips);
76
Daniel Glöcknerff77c352009-09-22 16:46:38 -070077#ifdef CONFIG_GPIO_SYSFS
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070078static DEFINE_IDR(dirent_idr);
Daniel Glöcknerff77c352009-09-22 16:46:38 -070079#endif
80
Alexandre Courbot372e7222013-02-03 01:29:29 +090081static int gpiod_request(struct gpio_desc *desc, const char *label);
82static void gpiod_free(struct gpio_desc *desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090083
Mark Brown7b17b592013-09-09 10:33:50 +010084#ifdef CONFIG_DEBUG_FS
85#define gpiod_emerg(desc, fmt, ...) \
86 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
87 ##__VA_ARGS__)
88#define gpiod_crit(desc, fmt, ...) \
89 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
90 ##__VA_ARGS__)
91#define gpiod_err(desc, fmt, ...) \
92 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
93 ##__VA_ARGS__)
94#define gpiod_warn(desc, fmt, ...) \
95 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
96 ##__VA_ARGS__)
97#define gpiod_info(desc, fmt, ...) \
98 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
99 ##__VA_ARGS__)
100#define gpiod_dbg(desc, fmt, ...) \
101 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
102 ##__VA_ARGS__)
103#else
Mark Brown6424de52013-09-09 10:33:49 +0100104#define gpiod_emerg(desc, fmt, ...) \
105 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
106#define gpiod_crit(desc, fmt, ...) \
107 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
108#define gpiod_err(desc, fmt, ...) \
109 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
110#define gpiod_warn(desc, fmt, ...) \
111 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
112#define gpiod_info(desc, fmt, ...) \
113 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
114#define gpiod_dbg(desc, fmt, ...) \
115 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Mark Brown7b17b592013-09-09 10:33:50 +0100116#endif
Alexandre Courbot372e7222013-02-03 01:29:29 +0900117
David Brownelld2876d02008-02-04 22:28:20 -0800118static inline void desc_set_label(struct gpio_desc *d, const char *label)
119{
120#ifdef CONFIG_DEBUG_FS
121 d->label = label;
122#endif
123}
124
Alexandre Courbot372e7222013-02-03 01:29:29 +0900125/*
126 * Return the GPIO number of the passed descriptor relative to its chip
127 */
128static int gpio_chip_hwgpio(const struct gpio_desc *desc)
129{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900130 return desc - &desc->chip->desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900131}
132
133/**
134 * Convert a GPIO number to its descriptor
135 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700136struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900137{
138 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
139 return NULL;
140 else
141 return &gpio_desc[gpio];
142}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700143EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900144
145/**
Linus Walleijd468bf92013-09-24 11:54:38 +0200146 * Convert an offset on a certain chip to a corresponding descriptor
147 */
148static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip,
149 unsigned int offset)
150{
151 unsigned int gpio = chip->base + offset;
152
153 return gpio_to_desc(gpio);
154}
155
156/**
Alexandre Courbot372e7222013-02-03 01:29:29 +0900157 * Convert a GPIO descriptor to the integer namespace.
158 * This should disappear in the future but is needed since we still
159 * use GPIO numbers for error messages and sysfs nodes
160 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700161int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900162{
Alexandre Courbot8c0fca82013-10-04 10:59:57 -0700163 return desc - &gpio_desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900164}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700165EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900166
167
David Brownelld2876d02008-02-04 22:28:20 -0800168/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
169 * when setting direction, and otherwise illegal. Until board setup code
170 * and drivers use explicit requests everywhere (which won't happen when
171 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -0700172 * message should motivate switching to explicit requests... so should
173 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -0700174 *
175 * NOTE: the autorequest mechanism is going away; at this point it's
176 * only "legal" in the sense that (old) code using it won't break yet,
177 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -0800178 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900179static int gpio_ensure_requested(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800180{
David Brownell8a0cecf2009-04-02 16:57:06 -0700181 const struct gpio_chip *chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900182 const int gpio = desc_to_gpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -0700183
David Brownell8a0cecf2009-04-02 16:57:06 -0700184 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
185 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700186 if (!try_module_get(chip->owner)) {
187 pr_err("GPIO-%d: module can't be gotten \n", gpio);
188 clear_bit(FLAG_REQUESTED, &desc->flags);
189 /* lose */
190 return -EIO;
191 }
David Brownelld2876d02008-02-04 22:28:20 -0800192 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700193 /* caller must chip->request() w/o spinlock */
194 if (chip->request)
195 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800196 }
David Brownell35e8bb52008-10-15 22:03:16 -0700197 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800198}
199
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700200/**
201 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
202 * @desc: descriptor to return the chip of
203 */
204struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900205{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900206 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900207}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700208EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800209
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700210/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
211static int gpiochip_find_base(int ngpio)
212{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900213 struct gpio_chip *chip;
214 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700215
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900216 list_for_each_entry_reverse(chip, &gpio_chips, list) {
217 /* found a free space? */
218 if (chip->base + chip->ngpio <= base)
219 break;
220 else
221 /* nope, check the space right before the chip */
222 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700223 }
224
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900225 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700226 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900227 return base;
228 } else {
229 pr_err("%s: cannot find free range\n", __func__);
230 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700231 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700232}
233
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700234/**
235 * gpiod_get_direction - return the current direction of a GPIO
236 * @desc: GPIO to get the direction of
237 *
238 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
239 *
240 * This function may sleep if gpiod_cansleep() is true.
241 */
242int gpiod_get_direction(const struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300243{
244 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900245 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300246 int status = -EINVAL;
247
Alexandre Courbot372e7222013-02-03 01:29:29 +0900248 chip = gpiod_to_chip(desc);
249 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300250
251 if (!chip->get_direction)
252 return status;
253
Alexandre Courbot372e7222013-02-03 01:29:29 +0900254 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300255 if (status > 0) {
256 /* GPIOF_DIR_IN, or other positive */
257 status = 1;
Alexandre Courbotdef63432013-02-15 14:46:15 +0900258 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
259 * so it does not affect constness per se */
260 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300261 }
262 if (status == 0) {
263 /* GPIOF_DIR_OUT */
Alexandre Courbotdef63432013-02-15 14:46:15 +0900264 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300265 }
266 return status;
267}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700268EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300269
David Brownelld8f388d2008-07-25 01:46:07 -0700270#ifdef CONFIG_GPIO_SYSFS
271
272/* lock protects against unexport_gpio() being called while
273 * sysfs files are active.
274 */
275static DEFINE_MUTEX(sysfs_lock);
276
277/*
278 * /sys/class/gpio/gpioN... only for GPIOs that are exported
279 * /direction
280 * * MAY BE OMITTED if kernel won't allow direction changes
281 * * is read/write as "in" or "out"
282 * * may also be written as "high" or "low", initializing
283 * output value as specified ("out" implies "low")
284 * /value
285 * * always readable, subject to hardware behavior
286 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700287 * /edge
288 * * configures behavior of poll(2) on /value
289 * * available only if pin can generate IRQs on input
290 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800291 * /active_low
292 * * configures polarity of /value
293 * * is read/write as zero/nonzero
294 * * also affects existing and subsequent "falling" and "rising"
295 * /edge configuration
David Brownelld8f388d2008-07-25 01:46:07 -0700296 */
297
298static ssize_t gpio_direction_show(struct device *dev,
299 struct device_attribute *attr, char *buf)
300{
Alexandre Courbotdef63432013-02-15 14:46:15 +0900301 const struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700302 ssize_t status;
303
304 mutex_lock(&sysfs_lock);
305
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900306 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700307 status = -EIO;
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900308 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +0900309 gpiod_get_direction(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700310 status = sprintf(buf, "%s\n",
311 test_bit(FLAG_IS_OUT, &desc->flags)
312 ? "out" : "in");
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900313 }
David Brownelld8f388d2008-07-25 01:46:07 -0700314
315 mutex_unlock(&sysfs_lock);
316 return status;
317}
318
319static ssize_t gpio_direction_store(struct device *dev,
320 struct device_attribute *attr, const char *buf, size_t size)
321{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900322 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700323 ssize_t status;
324
325 mutex_lock(&sysfs_lock);
326
327 if (!test_bit(FLAG_EXPORT, &desc->flags))
328 status = -EIO;
329 else if (sysfs_streq(buf, "high"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900330 status = gpiod_direction_output(desc, 1);
David Brownelld8f388d2008-07-25 01:46:07 -0700331 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900332 status = gpiod_direction_output(desc, 0);
David Brownelld8f388d2008-07-25 01:46:07 -0700333 else if (sysfs_streq(buf, "in"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900334 status = gpiod_direction_input(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700335 else
336 status = -EINVAL;
337
338 mutex_unlock(&sysfs_lock);
339 return status ? : size;
340}
341
Jani Nikula07697462009-12-15 16:46:20 -0800342static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d2008-07-25 01:46:07 -0700343 gpio_direction_show, gpio_direction_store);
344
345static ssize_t gpio_value_show(struct device *dev,
346 struct device_attribute *attr, char *buf)
347{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900348 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700349 ssize_t status;
350
351 mutex_lock(&sysfs_lock);
352
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700353 if (!test_bit(FLAG_EXPORT, &desc->flags))
David Brownelld8f388d2008-07-25 01:46:07 -0700354 status = -EIO;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700355 else
356 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
David Brownelld8f388d2008-07-25 01:46:07 -0700357
358 mutex_unlock(&sysfs_lock);
359 return status;
360}
361
362static ssize_t gpio_value_store(struct device *dev,
363 struct device_attribute *attr, const char *buf, size_t size)
364{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900365 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700366 ssize_t status;
367
368 mutex_lock(&sysfs_lock);
369
370 if (!test_bit(FLAG_EXPORT, &desc->flags))
371 status = -EIO;
372 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
373 status = -EPERM;
374 else {
375 long value;
376
Jingoo Hana3d88c92013-07-19 16:12:50 +0900377 status = kstrtol(buf, 0, &value);
David Brownelld8f388d2008-07-25 01:46:07 -0700378 if (status == 0) {
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700379 gpiod_set_value_cansleep(desc, value);
David Brownelld8f388d2008-07-25 01:46:07 -0700380 status = size;
381 }
382 }
383
384 mutex_unlock(&sysfs_lock);
385 return status;
386}
387
Jani Nikula07697462009-12-15 16:46:20 -0800388static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d2008-07-25 01:46:07 -0700389 gpio_value_show, gpio_value_store);
390
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700391static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
392{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700393 struct sysfs_dirent *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700394
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700395 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700396 return IRQ_HANDLED;
397}
398
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700399static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
400 unsigned long gpio_flags)
401{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700402 struct sysfs_dirent *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700403 unsigned long irq_flags;
404 int ret, irq, id;
405
406 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
407 return 0;
408
Alexandre Courbot372e7222013-02-03 01:29:29 +0900409 irq = gpiod_to_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700410 if (irq < 0)
411 return -EIO;
412
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700413 id = desc->flags >> ID_SHIFT;
414 value_sd = idr_find(&dirent_idr, id);
415 if (value_sd)
416 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700417
418 desc->flags &= ~GPIO_TRIGGER_MASK;
419
420 if (!gpio_flags) {
Linus Walleijd468bf92013-09-24 11:54:38 +0200421 gpiod_unlock_as_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700422 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700423 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700424 }
425
426 irq_flags = IRQF_SHARED;
427 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800428 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
429 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700430 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800431 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
432 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700433
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700434 if (!value_sd) {
435 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
436 if (!value_sd) {
437 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700438 goto err_out;
439 }
440
Tejun Heo62f516b2013-02-27 17:04:06 -0800441 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
442 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700443 goto free_sd;
Tejun Heo62f516b2013-02-27 17:04:06 -0800444 id = ret;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700445
446 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700447 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700448
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700449 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700450 ret = -ERANGE;
451 goto free_id;
452 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700453 }
454
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700455 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700456 "gpiolib", value_sd);
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700457 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700458 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700459
Linus Walleijd468bf92013-09-24 11:54:38 +0200460 ret = gpiod_lock_as_irq(desc);
461 if (ret < 0) {
462 gpiod_warn(desc, "failed to flag the GPIO for IRQ\n");
463 goto free_id;
464 }
465
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700466 desc->flags |= gpio_flags;
467 return 0;
468
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700469free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700470 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700471 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700472free_sd:
473 if (value_sd)
474 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700475err_out:
476 return ret;
477}
478
479static const struct {
480 const char *name;
481 unsigned long flags;
482} trigger_types[] = {
483 { "none", 0 },
484 { "falling", BIT(FLAG_TRIG_FALL) },
485 { "rising", BIT(FLAG_TRIG_RISE) },
486 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
487};
488
489static ssize_t gpio_edge_show(struct device *dev,
490 struct device_attribute *attr, char *buf)
491{
492 const struct gpio_desc *desc = dev_get_drvdata(dev);
493 ssize_t status;
494
495 mutex_lock(&sysfs_lock);
496
497 if (!test_bit(FLAG_EXPORT, &desc->flags))
498 status = -EIO;
499 else {
500 int i;
501
502 status = 0;
503 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
504 if ((desc->flags & GPIO_TRIGGER_MASK)
505 == trigger_types[i].flags) {
506 status = sprintf(buf, "%s\n",
507 trigger_types[i].name);
508 break;
509 }
510 }
511
512 mutex_unlock(&sysfs_lock);
513 return status;
514}
515
516static ssize_t gpio_edge_store(struct device *dev,
517 struct device_attribute *attr, const char *buf, size_t size)
518{
519 struct gpio_desc *desc = dev_get_drvdata(dev);
520 ssize_t status;
521 int i;
522
523 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
524 if (sysfs_streq(trigger_types[i].name, buf))
525 goto found;
526 return -EINVAL;
527
528found:
529 mutex_lock(&sysfs_lock);
530
531 if (!test_bit(FLAG_EXPORT, &desc->flags))
532 status = -EIO;
533 else {
534 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
535 if (!status)
536 status = size;
537 }
538
539 mutex_unlock(&sysfs_lock);
540
541 return status;
542}
543
544static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
545
Jani Nikula07697462009-12-15 16:46:20 -0800546static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
547 int value)
548{
549 int status = 0;
550
551 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
552 return 0;
553
554 if (value)
555 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
556 else
557 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
558
559 /* reconfigure poll(2) support if enabled on one edge only */
560 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
561 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
562 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
563
564 gpio_setup_irq(desc, dev, 0);
565 status = gpio_setup_irq(desc, dev, trigger_flags);
566 }
567
568 return status;
569}
570
571static ssize_t gpio_active_low_show(struct device *dev,
572 struct device_attribute *attr, char *buf)
573{
574 const struct gpio_desc *desc = dev_get_drvdata(dev);
575 ssize_t status;
576
577 mutex_lock(&sysfs_lock);
578
579 if (!test_bit(FLAG_EXPORT, &desc->flags))
580 status = -EIO;
581 else
582 status = sprintf(buf, "%d\n",
583 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
584
585 mutex_unlock(&sysfs_lock);
586
587 return status;
588}
589
590static ssize_t gpio_active_low_store(struct device *dev,
591 struct device_attribute *attr, const char *buf, size_t size)
592{
593 struct gpio_desc *desc = dev_get_drvdata(dev);
594 ssize_t status;
595
596 mutex_lock(&sysfs_lock);
597
598 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
599 status = -EIO;
600 } else {
601 long value;
602
Jingoo Hana3d88c92013-07-19 16:12:50 +0900603 status = kstrtol(buf, 0, &value);
Jani Nikula07697462009-12-15 16:46:20 -0800604 if (status == 0)
605 status = sysfs_set_active_low(desc, dev, value != 0);
606 }
607
608 mutex_unlock(&sysfs_lock);
609
610 return status ? : size;
611}
612
613static const DEVICE_ATTR(active_low, 0644,
614 gpio_active_low_show, gpio_active_low_store);
615
David Brownelld8f388d2008-07-25 01:46:07 -0700616static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d2008-07-25 01:46:07 -0700617 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800618 &dev_attr_active_low.attr,
David Brownelld8f388d2008-07-25 01:46:07 -0700619 NULL,
620};
621
622static const struct attribute_group gpio_attr_group = {
623 .attrs = (struct attribute **) gpio_attrs,
624};
625
626/*
627 * /sys/class/gpio/gpiochipN/
628 * /base ... matching gpio_chip.base (N)
629 * /label ... matching gpio_chip.label
630 * /ngpio ... matching gpio_chip.ngpio
631 */
632
633static ssize_t chip_base_show(struct device *dev,
634 struct device_attribute *attr, char *buf)
635{
636 const struct gpio_chip *chip = dev_get_drvdata(dev);
637
638 return sprintf(buf, "%d\n", chip->base);
639}
640static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
641
642static ssize_t chip_label_show(struct device *dev,
643 struct device_attribute *attr, char *buf)
644{
645 const struct gpio_chip *chip = dev_get_drvdata(dev);
646
647 return sprintf(buf, "%s\n", chip->label ? : "");
648}
649static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
650
651static ssize_t chip_ngpio_show(struct device *dev,
652 struct device_attribute *attr, char *buf)
653{
654 const struct gpio_chip *chip = dev_get_drvdata(dev);
655
656 return sprintf(buf, "%u\n", chip->ngpio);
657}
658static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
659
660static const struct attribute *gpiochip_attrs[] = {
661 &dev_attr_base.attr,
662 &dev_attr_label.attr,
663 &dev_attr_ngpio.attr,
664 NULL,
665};
666
667static const struct attribute_group gpiochip_attr_group = {
668 .attrs = (struct attribute **) gpiochip_attrs,
669};
670
671/*
672 * /sys/class/gpio/export ... write-only
673 * integer N ... number of GPIO to export (full access)
674 * /sys/class/gpio/unexport ... write-only
675 * integer N ... number of GPIO to unexport
676 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100677static ssize_t export_store(struct class *class,
678 struct class_attribute *attr,
679 const char *buf, size_t len)
David Brownelld8f388d2008-07-25 01:46:07 -0700680{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900681 long gpio;
682 struct gpio_desc *desc;
683 int status;
David Brownelld8f388d2008-07-25 01:46:07 -0700684
Jingoo Hana3d88c92013-07-19 16:12:50 +0900685 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d2008-07-25 01:46:07 -0700686 if (status < 0)
687 goto done;
688
Alexandre Courbot372e7222013-02-03 01:29:29 +0900689 desc = gpio_to_desc(gpio);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900690 /* reject invalid GPIOs */
691 if (!desc) {
692 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
693 return -EINVAL;
694 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900695
David Brownelld8f388d2008-07-25 01:46:07 -0700696 /* No extra locking here; FLAG_SYSFS just signifies that the
697 * request and export were done by on behalf of userspace, so
698 * they may be undone on its behalf too.
699 */
700
Alexandre Courbot372e7222013-02-03 01:29:29 +0900701 status = gpiod_request(desc, "sysfs");
Mathias Nymanad2fab32012-10-25 14:03:03 +0300702 if (status < 0) {
703 if (status == -EPROBE_DEFER)
704 status = -ENODEV;
David Brownelld8f388d2008-07-25 01:46:07 -0700705 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +0300706 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900707 status = gpiod_export(desc, true);
David Brownelld8f388d2008-07-25 01:46:07 -0700708 if (status < 0)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900709 gpiod_free(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700710 else
Alexandre Courbot372e7222013-02-03 01:29:29 +0900711 set_bit(FLAG_SYSFS, &desc->flags);
David Brownelld8f388d2008-07-25 01:46:07 -0700712
713done:
714 if (status)
715 pr_debug("%s: status %d\n", __func__, status);
716 return status ? : len;
717}
718
Andi Kleen28812fe2010-01-05 12:48:07 +0100719static ssize_t unexport_store(struct class *class,
720 struct class_attribute *attr,
721 const char *buf, size_t len)
David Brownelld8f388d2008-07-25 01:46:07 -0700722{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900723 long gpio;
724 struct gpio_desc *desc;
725 int status;
David Brownelld8f388d2008-07-25 01:46:07 -0700726
Jingoo Hana3d88c92013-07-19 16:12:50 +0900727 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d2008-07-25 01:46:07 -0700728 if (status < 0)
729 goto done;
730
Alexandre Courbot372e7222013-02-03 01:29:29 +0900731 desc = gpio_to_desc(gpio);
David Brownelld8f388d2008-07-25 01:46:07 -0700732 /* reject bogus commands (gpio_unexport ignores them) */
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900733 if (!desc) {
734 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
735 return -EINVAL;
736 }
737
738 status = -EINVAL;
David Brownelld8f388d2008-07-25 01:46:07 -0700739
740 /* No extra locking here; FLAG_SYSFS just signifies that the
741 * request and export were done by on behalf of userspace, so
742 * they may be undone on its behalf too.
743 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900744 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700745 status = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900746 gpiod_free(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700747 }
748done:
749 if (status)
750 pr_debug("%s: status %d\n", __func__, status);
751 return status ? : len;
752}
753
754static struct class_attribute gpio_class_attrs[] = {
755 __ATTR(export, 0200, NULL, export_store),
756 __ATTR(unexport, 0200, NULL, unexport_store),
757 __ATTR_NULL,
758};
759
760static struct class gpio_class = {
761 .name = "gpio",
762 .owner = THIS_MODULE,
763
764 .class_attrs = gpio_class_attrs,
765};
766
767
768/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700769 * gpiod_export - export a GPIO through sysfs
David Brownelld8f388d2008-07-25 01:46:07 -0700770 * @gpio: gpio to make available, already requested
771 * @direction_may_change: true if userspace may change gpio direction
772 * Context: arch_initcall or later
773 *
774 * When drivers want to make a GPIO accessible to userspace after they
775 * have requested it -- perhaps while debugging, or as part of their
776 * public interface -- they may use this routine. If the GPIO can
777 * change direction (some can't) and the caller allows it, userspace
778 * will see "direction" sysfs attribute which may be used to change
779 * the gpio's direction. A "value" attribute will always be provided.
780 *
781 * Returns zero on success, else an error.
782 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700783int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
David Brownelld8f388d2008-07-25 01:46:07 -0700784{
785 unsigned long flags;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100786 int status;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700787 const char *ioname = NULL;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100788 struct device *dev;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900789 int offset;
David Brownelld8f388d2008-07-25 01:46:07 -0700790
791 /* can't export until sysfs is available ... */
792 if (!gpio_class.p) {
793 pr_debug("%s: called too early!\n", __func__);
794 return -ENOENT;
795 }
796
Alexandre Courbot372e7222013-02-03 01:29:29 +0900797 if (!desc) {
798 pr_debug("%s: invalid gpio descriptor\n", __func__);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100799 return -EINVAL;
800 }
David Brownelld8f388d2008-07-25 01:46:07 -0700801
802 mutex_lock(&sysfs_lock);
803
804 spin_lock_irqsave(&gpio_lock, flags);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100805 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
806 test_bit(FLAG_EXPORT, &desc->flags)) {
807 spin_unlock_irqrestore(&gpio_lock, flags);
808 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +0900809 __func__, desc_to_gpio(desc),
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100810 test_bit(FLAG_REQUESTED, &desc->flags),
811 test_bit(FLAG_EXPORT, &desc->flags));
Dan Carpenter529f2ad2012-10-26 09:59:43 +0300812 status = -EPERM;
813 goto fail_unlock;
David Brownelld8f388d2008-07-25 01:46:07 -0700814 }
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100815
816 if (!desc->chip->direction_input || !desc->chip->direction_output)
817 direction_may_change = false;
David Brownelld8f388d2008-07-25 01:46:07 -0700818 spin_unlock_irqrestore(&gpio_lock, flags);
819
Alexandre Courbot372e7222013-02-03 01:29:29 +0900820 offset = gpio_chip_hwgpio(desc);
821 if (desc->chip->names && desc->chip->names[offset])
822 ioname = desc->chip->names[offset];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700823
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100824 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Alexandre Courbot372e7222013-02-03 01:29:29 +0900825 desc, ioname ? ioname : "gpio%u",
826 desc_to_gpio(desc));
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100827 if (IS_ERR(dev)) {
828 status = PTR_ERR(dev);
829 goto fail_unlock;
David Brownelld8f388d2008-07-25 01:46:07 -0700830 }
831
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100832 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
David Brownelld8f388d2008-07-25 01:46:07 -0700833 if (status)
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100834 goto fail_unregister_device;
David Brownelld8f388d2008-07-25 01:46:07 -0700835
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100836 if (direction_may_change) {
837 status = device_create_file(dev, &dev_attr_direction);
838 if (status)
839 goto fail_unregister_device;
840 }
841
Alexandre Courbot372e7222013-02-03 01:29:29 +0900842 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100843 !test_bit(FLAG_IS_OUT, &desc->flags))) {
844 status = device_create_file(dev, &dev_attr_edge);
845 if (status)
846 goto fail_unregister_device;
847 }
848
849 set_bit(FLAG_EXPORT, &desc->flags);
850 mutex_unlock(&sysfs_lock);
851 return 0;
852
853fail_unregister_device:
854 device_unregister(dev);
855fail_unlock:
856 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900857 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
858 status);
David Brownelld8f388d2008-07-25 01:46:07 -0700859 return status;
860}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700861EXPORT_SYMBOL_GPL(gpiod_export);
David Brownelld8f388d2008-07-25 01:46:07 -0700862
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100863static int match_export(struct device *dev, const void *data)
David Brownelld8f388d2008-07-25 01:46:07 -0700864{
865 return dev_get_drvdata(dev) == data;
866}
867
868/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700869 * gpiod_export_link - create a sysfs link to an exported GPIO node
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700870 * @dev: device under which to create symlink
871 * @name: name of the symlink
872 * @gpio: gpio to create symlink to, already exported
873 *
874 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
875 * node. Caller is responsible for unlinking.
876 *
877 * Returns zero on success, else an error.
878 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700879int gpiod_export_link(struct device *dev, const char *name,
880 struct gpio_desc *desc)
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700881{
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700882 int status = -EINVAL;
883
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900884 if (!desc) {
885 pr_warn("%s: invalid GPIO\n", __func__);
886 return -EINVAL;
887 }
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700888
889 mutex_lock(&sysfs_lock);
890
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700891 if (test_bit(FLAG_EXPORT, &desc->flags)) {
892 struct device *tdev;
893
894 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
895 if (tdev != NULL) {
896 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
897 name);
898 } else {
899 status = -ENODEV;
900 }
901 }
902
903 mutex_unlock(&sysfs_lock);
904
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700905 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900906 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
907 status);
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700908
909 return status;
910}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700911EXPORT_SYMBOL_GPL(gpiod_export_link);
Jani Nikula07697462009-12-15 16:46:20 -0800912
913/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700914 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
Jani Nikula07697462009-12-15 16:46:20 -0800915 * @gpio: gpio to change
916 * @value: non-zero to use active low, i.e. inverted values
917 *
918 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
919 * The GPIO does not have to be exported yet. If poll(2) support has
920 * been enabled for either rising or falling edge, it will be
921 * reconfigured to follow the new polarity.
922 *
923 * Returns zero on success, else an error.
924 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700925int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
Jani Nikula07697462009-12-15 16:46:20 -0800926{
Jani Nikula07697462009-12-15 16:46:20 -0800927 struct device *dev = NULL;
928 int status = -EINVAL;
929
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900930 if (!desc) {
931 pr_warn("%s: invalid GPIO\n", __func__);
932 return -EINVAL;
933 }
Jani Nikula07697462009-12-15 16:46:20 -0800934
935 mutex_lock(&sysfs_lock);
936
Jani Nikula07697462009-12-15 16:46:20 -0800937 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800938 dev = class_find_device(&gpio_class, NULL, desc, match_export);
939 if (dev == NULL) {
940 status = -ENODEV;
941 goto unlock;
942 }
943 }
944
945 status = sysfs_set_active_low(desc, dev, value);
946
947unlock:
948 mutex_unlock(&sysfs_lock);
949
Jani Nikula07697462009-12-15 16:46:20 -0800950 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900951 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
952 status);
Jani Nikula07697462009-12-15 16:46:20 -0800953
954 return status;
955}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700956EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
Jani Nikula07697462009-12-15 16:46:20 -0800957
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700958/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700959 * gpiod_unexport - reverse effect of gpio_export()
David Brownelld8f388d2008-07-25 01:46:07 -0700960 * @gpio: gpio to make unavailable
961 *
962 * This is implicit on gpio_free().
963 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700964void gpiod_unexport(struct gpio_desc *desc)
David Brownelld8f388d2008-07-25 01:46:07 -0700965{
Jon Povey6a99ad42010-07-27 13:18:06 -0700966 int status = 0;
Ming Lei864533c2012-02-13 22:53:20 +0800967 struct device *dev = NULL;
David Brownelld8f388d2008-07-25 01:46:07 -0700968
Alexandre Courbot372e7222013-02-03 01:29:29 +0900969 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900970 pr_warn("%s: invalid GPIO\n", __func__);
971 return;
Jon Povey6a99ad42010-07-27 13:18:06 -0700972 }
David Brownelld8f388d2008-07-25 01:46:07 -0700973
974 mutex_lock(&sysfs_lock);
975
David Brownelld8f388d2008-07-25 01:46:07 -0700976 if (test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700977
978 dev = class_find_device(&gpio_class, NULL, desc, match_export);
979 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700980 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d2008-07-25 01:46:07 -0700981 clear_bit(FLAG_EXPORT, &desc->flags);
David Brownelld8f388d2008-07-25 01:46:07 -0700982 } else
983 status = -ENODEV;
984 }
985
986 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900987
Ming Lei864533c2012-02-13 22:53:20 +0800988 if (dev) {
989 device_unregister(dev);
990 put_device(dev);
991 }
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900992
David Brownelld8f388d2008-07-25 01:46:07 -0700993 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900994 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
995 status);
996}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700997EXPORT_SYMBOL_GPL(gpiod_unexport);
David Brownelld8f388d2008-07-25 01:46:07 -0700998
999static int gpiochip_export(struct gpio_chip *chip)
1000{
1001 int status;
1002 struct device *dev;
1003
1004 /* Many systems register gpio chips for SOC support very early,
1005 * before driver model support is available. In those cases we
1006 * export this later, in gpiolib_sysfs_init() ... here we just
1007 * verify that _some_ field of gpio_class got initialized.
1008 */
1009 if (!gpio_class.p)
1010 return 0;
1011
1012 /* use chip->base for the ID; it's already known to be unique */
1013 mutex_lock(&sysfs_lock);
1014 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
1015 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001016 if (!IS_ERR(dev)) {
David Brownelld8f388d2008-07-25 01:46:07 -07001017 status = sysfs_create_group(&dev->kobj,
1018 &gpiochip_attr_group);
1019 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001020 status = PTR_ERR(dev);
David Brownelld8f388d2008-07-25 01:46:07 -07001021 chip->exported = (status == 0);
1022 mutex_unlock(&sysfs_lock);
1023
1024 if (status) {
1025 unsigned long flags;
1026 unsigned gpio;
1027
1028 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001029 gpio = 0;
1030 while (gpio < chip->ngpio)
1031 chip->desc[gpio++].chip = NULL;
David Brownelld8f388d2008-07-25 01:46:07 -07001032 spin_unlock_irqrestore(&gpio_lock, flags);
1033
1034 pr_debug("%s: chip %s status %d\n", __func__,
1035 chip->label, status);
1036 }
1037
1038 return status;
1039}
1040
1041static void gpiochip_unexport(struct gpio_chip *chip)
1042{
1043 int status;
1044 struct device *dev;
1045
1046 mutex_lock(&sysfs_lock);
1047 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1048 if (dev) {
1049 put_device(dev);
1050 device_unregister(dev);
1051 chip->exported = 0;
1052 status = 0;
1053 } else
1054 status = -ENODEV;
1055 mutex_unlock(&sysfs_lock);
1056
1057 if (status)
1058 pr_debug("%s: chip %s status %d\n", __func__,
1059 chip->label, status);
1060}
1061
1062static int __init gpiolib_sysfs_init(void)
1063{
1064 int status;
1065 unsigned long flags;
Alexandre Courbot65493e32013-02-03 01:29:25 +09001066 struct gpio_chip *chip;
David Brownelld8f388d2008-07-25 01:46:07 -07001067
1068 status = class_register(&gpio_class);
1069 if (status < 0)
1070 return status;
1071
1072 /* Scan and register the gpio_chips which registered very
1073 * early (e.g. before the class_register above was called).
1074 *
1075 * We run before arch_initcall() so chip->dev nodes can have
1076 * registered, and so arch_initcall() can always gpio_export().
1077 */
1078 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot65493e32013-02-03 01:29:25 +09001079 list_for_each_entry(chip, &gpio_chips, list) {
David Brownelld8f388d2008-07-25 01:46:07 -07001080 if (!chip || chip->exported)
1081 continue;
1082
1083 spin_unlock_irqrestore(&gpio_lock, flags);
1084 status = gpiochip_export(chip);
1085 spin_lock_irqsave(&gpio_lock, flags);
1086 }
1087 spin_unlock_irqrestore(&gpio_lock, flags);
1088
1089
1090 return status;
1091}
1092postcore_initcall(gpiolib_sysfs_init);
1093
1094#else
1095static inline int gpiochip_export(struct gpio_chip *chip)
1096{
1097 return 0;
1098}
1099
1100static inline void gpiochip_unexport(struct gpio_chip *chip)
1101{
1102}
1103
1104#endif /* CONFIG_GPIO_SYSFS */
1105
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001106/*
1107 * Add a new chip to the global chips list, keeping the list of chips sorted
1108 * by base order.
1109 *
1110 * Return -EBUSY if the new chip overlaps with some other chip's integer
1111 * space.
1112 */
1113static int gpiochip_add_to_list(struct gpio_chip *chip)
1114{
1115 struct list_head *pos = &gpio_chips;
1116 struct gpio_chip *_chip;
1117 int err = 0;
1118
1119 /* find where to insert our chip */
1120 list_for_each(pos, &gpio_chips) {
1121 _chip = list_entry(pos, struct gpio_chip, list);
1122 /* shall we insert before _chip? */
1123 if (_chip->base >= chip->base + chip->ngpio)
1124 break;
1125 }
1126
1127 /* are we stepping on the chip right before? */
1128 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1129 _chip = list_entry(pos->prev, struct gpio_chip, list);
1130 if (_chip->base + _chip->ngpio > chip->base) {
1131 dev_err(chip->dev,
1132 "GPIO integer space overlap, cannot add chip\n");
1133 err = -EBUSY;
1134 }
1135 }
1136
1137 if (!err)
1138 list_add_tail(&chip->list, pos);
1139
1140 return err;
1141}
1142
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001143/**
David Brownelld2876d02008-02-04 22:28:20 -08001144 * gpiochip_add() - register a gpio_chip
1145 * @chip: the chip to register, with chip->base initialized
1146 * Context: potentially before irqs or kmalloc will work
1147 *
1148 * Returns a negative errno if the chip can't be registered, such as
1149 * because the chip->base is invalid or already associated with a
1150 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001151 *
David Brownelld8f388d2008-07-25 01:46:07 -07001152 * When gpiochip_add() is called very early during boot, so that GPIOs
1153 * can be freely used, the chip->dev device must be registered before
1154 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1155 * for GPIOs will fail rudely.
1156 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001157 * If chip->base is negative, this requests dynamic assignment of
1158 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001159 */
1160int gpiochip_add(struct gpio_chip *chip)
1161{
1162 unsigned long flags;
1163 int status = 0;
1164 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001165 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001166
Trent Piephobff5fda2008-05-23 13:04:44 -07001167 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001168 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001169 status = -EINVAL;
1170 goto fail;
1171 }
1172
1173 spin_lock_irqsave(&gpio_lock, flags);
1174
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001175 if (base < 0) {
1176 base = gpiochip_find_base(chip->ngpio);
1177 if (base < 0) {
1178 status = base;
David Brownelld8f388d2008-07-25 01:46:07 -07001179 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001180 }
1181 chip->base = base;
1182 }
1183
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001184 status = gpiochip_add_to_list(chip);
1185
David Brownelld2876d02008-02-04 22:28:20 -08001186 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001187 chip->desc = &gpio_desc[chip->base];
1188
1189 for (id = 0; id < chip->ngpio; id++) {
1190 struct gpio_desc *desc = &chip->desc[id];
1191 desc->chip = chip;
David Brownelld8f388d2008-07-25 01:46:07 -07001192
1193 /* REVISIT: most hardware initializes GPIOs as
1194 * inputs (often with pullups enabled) so power
1195 * usage is minimized. Linux code should set the
1196 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +03001197 * and in case chip->get_direction is not set,
David Brownelld8f388d2008-07-25 01:46:07 -07001198 * we may expose the wrong direction in sysfs.
1199 */
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001200 desc->flags = !chip->direction_input
David Brownelld8f388d2008-07-25 01:46:07 -07001201 ? (1 << FLAG_IS_OUT)
1202 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001203 }
1204 }
1205
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001206 spin_unlock_irqrestore(&gpio_lock, flags);
1207
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301208#ifdef CONFIG_PINCTRL
1209 INIT_LIST_HEAD(&chip->pin_ranges);
1210#endif
1211
Anton Vorontsov391c9702010-06-08 07:48:17 -06001212 of_gpiochip_add(chip);
1213
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001214 if (status)
1215 goto fail;
1216
1217 status = gpiochip_export(chip);
1218 if (status)
1219 goto fail;
1220
H Hartley Sweetenee1c1e72012-05-11 16:58:33 -07001221 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
Grant Likely64842aa2011-11-06 11:36:18 -07001222 chip->base, chip->base + chip->ngpio - 1,
1223 chip->label ? : "generic");
1224
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001225 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001226
1227unlock:
1228 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld2876d02008-02-04 22:28:20 -08001229fail:
1230 /* failures here can mean systems won't boot... */
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001231 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1232 chip->base, chip->base + chip->ngpio - 1,
1233 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001234 return status;
1235}
1236EXPORT_SYMBOL_GPL(gpiochip_add);
1237
1238/**
1239 * gpiochip_remove() - unregister a gpio_chip
1240 * @chip: the chip to unregister
1241 *
1242 * A gpio_chip with any GPIOs still requested may not be removed.
1243 */
1244int gpiochip_remove(struct gpio_chip *chip)
1245{
1246 unsigned long flags;
1247 int status = 0;
1248 unsigned id;
1249
1250 spin_lock_irqsave(&gpio_lock, flags);
1251
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001252 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001253 of_gpiochip_remove(chip);
1254
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001255 for (id = 0; id < chip->ngpio; id++) {
1256 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
David Brownelld2876d02008-02-04 22:28:20 -08001257 status = -EBUSY;
1258 break;
1259 }
1260 }
1261 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001262 for (id = 0; id < chip->ngpio; id++)
1263 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001264
1265 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -08001266 }
1267
1268 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d2008-07-25 01:46:07 -07001269
1270 if (status == 0)
1271 gpiochip_unexport(chip);
1272
David Brownelld2876d02008-02-04 22:28:20 -08001273 return status;
1274}
1275EXPORT_SYMBOL_GPL(gpiochip_remove);
1276
Grant Likely594fa262010-06-08 07:48:16 -06001277/**
1278 * gpiochip_find() - iterator for locating a specific gpio_chip
1279 * @data: data to pass to match function
1280 * @callback: Callback function to check gpio_chip
1281 *
1282 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1283 * determined by a user supplied @match callback. The callback should return
1284 * 0 if the device doesn't match and non-zero if it does. If the callback is
1285 * non-zero, this function will return to the caller and not iterate over any
1286 * more gpio_chips.
1287 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001288struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001289 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001290 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001291{
Alexandre Courbot125eef92013-02-03 01:29:26 +09001292 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001293 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001294
1295 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +09001296 list_for_each_entry(chip, &gpio_chips, list)
1297 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001298 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001299
1300 /* No match? */
1301 if (&chip->list == &gpio_chips)
1302 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001303 spin_unlock_irqrestore(&gpio_lock, flags);
1304
1305 return chip;
1306}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001307EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001308
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301309#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001310
Linus Walleij3f0f8672012-11-20 12:40:15 +01001311/**
1312 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1313 * @chip: the gpiochip to add the range for
1314 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001315 * @gpio_offset: the start offset in the current gpio_chip number space
1316 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001317 * @npins: the number of pins from the offset of each pin space (GPIO and
1318 * pin controller) to accumulate in this range
1319 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001320int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001321 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001322 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301323{
1324 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001325 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301326
Linus Walleij3f0f8672012-11-20 12:40:15 +01001327 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301328 if (!pin_range) {
1329 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1330 chip->label);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001331 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301332 }
1333
Linus Walleij3f0f8672012-11-20 12:40:15 +01001334 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001335 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001336 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301337 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001338 pin_range->range.base = chip->base + gpio_offset;
1339 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301340 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001341 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301342 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001343 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001344 ret = PTR_ERR(pin_range->pctldev);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001345 pr_err("%s: GPIO chip: could not create pin range\n",
1346 chip->label);
1347 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001348 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001349 }
Linus Walleij316511c2012-11-21 08:48:09 +01001350 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1351 chip->label, gpio_offset, gpio_offset + npins - 1,
1352 pinctl_name,
1353 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301354
1355 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001356
1357 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301358}
Linus Walleij165adc92012-11-06 14:49:39 +01001359EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301360
Linus Walleij3f0f8672012-11-20 12:40:15 +01001361/**
1362 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1363 * @chip: the chip to remove all the mappings for
1364 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301365void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1366{
1367 struct gpio_pin_range *pin_range, *tmp;
1368
1369 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1370 list_del(&pin_range->node);
1371 pinctrl_remove_gpio_range(pin_range->pctldev,
1372 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001373 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301374 }
1375}
Linus Walleij165adc92012-11-06 14:49:39 +01001376EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1377
1378#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301379
David Brownelld2876d02008-02-04 22:28:20 -08001380/* These "optional" allocation calls help prevent drivers from stomping
1381 * on each other, and help provide better diagnostics in debugfs.
1382 * They're called even less than the "set direction" calls.
1383 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001384static int gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001385{
David Brownell35e8bb52008-10-15 22:03:16 -07001386 struct gpio_chip *chip;
Mark Browne9354572012-07-09 12:22:56 +01001387 int status = -EPROBE_DEFER;
David Brownelld2876d02008-02-04 22:28:20 -08001388 unsigned long flags;
1389
Alexandre Courbot0204df42013-10-04 10:59:58 -07001390 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001391 pr_warn("%s: invalid GPIO\n", __func__);
1392 return -EINVAL;
1393 }
1394
David Brownelld2876d02008-02-04 22:28:20 -08001395 spin_lock_irqsave(&gpio_lock, flags);
1396
David Brownell35e8bb52008-10-15 22:03:16 -07001397 chip = desc->chip;
Alexandre Courbot0204df42013-10-04 10:59:58 -07001398 if (chip == NULL)
1399 goto done;
David Brownelld2876d02008-02-04 22:28:20 -08001400
David Brownell35e8bb52008-10-15 22:03:16 -07001401 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001402 goto done;
1403
David Brownelld2876d02008-02-04 22:28:20 -08001404 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001405 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001406 */
1407
1408 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1409 desc_set_label(desc, label ? : "?");
1410 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001411 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001412 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001413 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001414 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001415 }
1416
1417 if (chip->request) {
1418 /* chip->request may sleep */
1419 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001420 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001421 spin_lock_irqsave(&gpio_lock, flags);
1422
1423 if (status < 0) {
1424 desc_set_label(desc, NULL);
1425 module_put(chip->owner);
1426 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001427 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001428 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001429 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001430 if (chip->get_direction) {
1431 /* chip->get_direction may sleep */
1432 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001433 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001434 spin_lock_irqsave(&gpio_lock, flags);
1435 }
David Brownelld2876d02008-02-04 22:28:20 -08001436done:
1437 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001438 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001439 desc_to_gpio(desc), label ? : "?", status);
David Brownelld2876d02008-02-04 22:28:20 -08001440 spin_unlock_irqrestore(&gpio_lock, flags);
1441 return status;
1442}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001443
1444int gpio_request(unsigned gpio, const char *label)
1445{
1446 return gpiod_request(gpio_to_desc(gpio), label);
1447}
David Brownelld2876d02008-02-04 22:28:20 -08001448EXPORT_SYMBOL_GPL(gpio_request);
1449
Alexandre Courbot372e7222013-02-03 01:29:29 +09001450static void gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001451{
1452 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001453 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001454
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001455 might_sleep();
1456
Alexandre Courbot372e7222013-02-03 01:29:29 +09001457 if (!desc) {
David Brownelld2876d02008-02-04 22:28:20 -08001458 WARN_ON(extra_checks);
1459 return;
1460 }
1461
Alexandre Courbot372e7222013-02-03 01:29:29 +09001462 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001463
David Brownelld2876d02008-02-04 22:28:20 -08001464 spin_lock_irqsave(&gpio_lock, flags);
1465
David Brownell35e8bb52008-10-15 22:03:16 -07001466 chip = desc->chip;
1467 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1468 if (chip->free) {
1469 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001470 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001471 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001472 spin_lock_irqsave(&gpio_lock, flags);
1473 }
David Brownelld2876d02008-02-04 22:28:20 -08001474 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001475 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001476 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001477 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301478 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301479 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001480 } else
David Brownelld2876d02008-02-04 22:28:20 -08001481 WARN_ON(extra_checks);
1482
1483 spin_unlock_irqrestore(&gpio_lock, flags);
1484}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001485
1486void gpio_free(unsigned gpio)
1487{
1488 gpiod_free(gpio_to_desc(gpio));
1489}
David Brownelld2876d02008-02-04 22:28:20 -08001490EXPORT_SYMBOL_GPL(gpio_free);
1491
Eric Miao3e45f1d2010-03-05 13:44:35 -08001492/**
1493 * gpio_request_one - request a single GPIO with initial configuration
1494 * @gpio: the GPIO number
1495 * @flags: GPIO configuration as specified by GPIOF_*
1496 * @label: a literal description string of this GPIO
1497 */
1498int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1499{
Alexandre Courbot372e7222013-02-03 01:29:29 +09001500 struct gpio_desc *desc;
Eric Miao3e45f1d2010-03-05 13:44:35 -08001501 int err;
1502
Alexandre Courbot372e7222013-02-03 01:29:29 +09001503 desc = gpio_to_desc(gpio);
1504
1505 err = gpiod_request(desc, label);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001506 if (err)
1507 return err;
1508
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301509 if (flags & GPIOF_OPEN_DRAIN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001510 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301511
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301512 if (flags & GPIOF_OPEN_SOURCE)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001513 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301514
Eric Miao3e45f1d2010-03-05 13:44:35 -08001515 if (flags & GPIOF_DIR_IN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001516 err = gpiod_direction_input(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001517 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001518 err = gpiod_direction_output(desc,
Eric Miao3e45f1d2010-03-05 13:44:35 -08001519 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1520
Aaro Koskinene2548112010-12-21 17:24:22 -08001521 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001522 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -08001523
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001524 if (flags & GPIOF_EXPORT) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001525 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001526 if (err)
1527 goto free_gpio;
1528 }
1529
1530 return 0;
1531
1532 free_gpio:
Alexandre Courbot372e7222013-02-03 01:29:29 +09001533 gpiod_free(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001534 return err;
1535}
1536EXPORT_SYMBOL_GPL(gpio_request_one);
1537
1538/**
1539 * gpio_request_array - request multiple GPIOs in a single call
1540 * @array: array of the 'struct gpio'
1541 * @num: how many GPIOs in the array
1542 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001543int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001544{
1545 int i, err;
1546
1547 for (i = 0; i < num; i++, array++) {
1548 err = gpio_request_one(array->gpio, array->flags, array->label);
1549 if (err)
1550 goto err_free;
1551 }
1552 return 0;
1553
1554err_free:
1555 while (i--)
1556 gpio_free((--array)->gpio);
1557 return err;
1558}
1559EXPORT_SYMBOL_GPL(gpio_request_array);
1560
1561/**
1562 * gpio_free_array - release multiple GPIOs in a single call
1563 * @array: array of the 'struct gpio'
1564 * @num: how many GPIOs in the array
1565 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001566void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001567{
1568 while (num--)
1569 gpio_free((array++)->gpio);
1570}
1571EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001572
1573/**
1574 * gpiochip_is_requested - return string iff signal was requested
1575 * @chip: controller managing the signal
1576 * @offset: of signal within controller's 0..(ngpio - 1) range
1577 *
1578 * Returns NULL if the GPIO is not currently requested, else a string.
1579 * If debugfs support is enabled, the string returned is the label passed
1580 * to gpio_request(); otherwise it is a meaningless constant.
1581 *
1582 * This function is for use by GPIO controller drivers. The label can
1583 * help with diagnostics, and knowing that the signal is used as a GPIO
1584 * can help avoid accidentally multiplexing it to another controller.
1585 */
1586const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1587{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001588 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001589
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001590 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -08001591 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001592
1593 desc = &chip->desc[offset];
1594
Alexandre Courbot372e7222013-02-03 01:29:29 +09001595 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001596 return NULL;
1597#ifdef CONFIG_DEBUG_FS
Alexandre Courbot372e7222013-02-03 01:29:29 +09001598 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001599#else
1600 return "?";
1601#endif
1602}
1603EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1604
1605
1606/* Drivers MUST set GPIO direction before making get/set calls. In
1607 * some cases this is done in early boot, before IRQs are enabled.
1608 *
1609 * As a rule these aren't called more than once (except for drivers
1610 * using the open-drain emulation idiom) so these are natural places
1611 * to accumulate extra debugging checks. Note that we can't (yet)
1612 * rely on gpio_request() having been called beforehand.
1613 */
1614
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001615/**
1616 * gpiod_direction_input - set the GPIO direction to input
1617 * @desc: GPIO to set to input
1618 *
1619 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1620 * be called safely on it.
1621 *
1622 * Return 0 in case of success, else an error code.
1623 */
1624int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001625{
1626 unsigned long flags;
1627 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001628 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001629 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001630
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001631 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001632 pr_warn("%s: invalid GPIO\n", __func__);
1633 return -EINVAL;
1634 }
1635
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001636 chip = desc->chip;
1637 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001638 gpiod_warn(desc,
1639 "%s: missing get() or direction_input() operations\n",
1640 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001641 return -EIO;
1642 }
1643
David Brownelld2876d02008-02-04 22:28:20 -08001644 spin_lock_irqsave(&gpio_lock, flags);
1645
Alexandre Courbot372e7222013-02-03 01:29:29 +09001646 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001647 if (status < 0)
1648 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001649
1650 /* now we know the gpio is valid and chip won't vanish */
1651
1652 spin_unlock_irqrestore(&gpio_lock, flags);
1653
David Brownell9c4ba942010-08-10 18:02:24 -07001654 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001655
Alexandre Courbot372e7222013-02-03 01:29:29 +09001656 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001657 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001658 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001659 if (status < 0) {
Mark Brown6424de52013-09-09 10:33:49 +01001660 gpiod_dbg(desc, "chip request fail, %d\n", status);
David Brownell35e8bb52008-10-15 22:03:16 -07001661 /* and it's not available to anyone else ...
1662 * gpio_request() is the fully clean solution.
1663 */
1664 goto lose;
1665 }
1666 }
1667
Alexandre Courbot372e7222013-02-03 01:29:29 +09001668 status = chip->direction_input(chip, offset);
David Brownelld2876d02008-02-04 22:28:20 -08001669 if (status == 0)
1670 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001671
Alexandre Courbot372e7222013-02-03 01:29:29 +09001672 trace_gpio_direction(desc_to_gpio(desc), 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001673lose:
David Brownelld2876d02008-02-04 22:28:20 -08001674 return status;
1675fail:
1676 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001677 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001678 gpiod_dbg(desc, "%s status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001679 return status;
1680}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001681EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001682
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001683/**
1684 * gpiod_direction_output - set the GPIO direction to input
1685 * @desc: GPIO to set to output
1686 * @value: initial output value of the GPIO
1687 *
1688 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1689 * be called safely on it. The initial value of the output must be specified.
1690 *
1691 * Return 0 in case of success, else an error code.
1692 */
1693int gpiod_direction_output(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001694{
1695 unsigned long flags;
1696 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001697 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001698 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001699
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001700 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001701 pr_warn("%s: invalid GPIO\n", __func__);
1702 return -EINVAL;
1703 }
1704
Linus Walleijd468bf92013-09-24 11:54:38 +02001705 /* GPIOs used for IRQs shall not be set as output */
1706 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1707 gpiod_err(desc,
1708 "%s: tried to set a GPIO tied to an IRQ as output\n",
1709 __func__);
1710 return -EIO;
1711 }
1712
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301713 /* Open drain pin should not be driven to 1 */
1714 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001715 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301716
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301717 /* Open source pin should not be driven to 0 */
1718 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001719 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301720
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001721 chip = desc->chip;
1722 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001723 gpiod_warn(desc,
1724 "%s: missing set() or direction_output() operations\n",
1725 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001726 return -EIO;
1727 }
1728
David Brownelld2876d02008-02-04 22:28:20 -08001729 spin_lock_irqsave(&gpio_lock, flags);
1730
Alexandre Courbot372e7222013-02-03 01:29:29 +09001731 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001732 if (status < 0)
1733 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001734
1735 /* now we know the gpio is valid and chip won't vanish */
1736
1737 spin_unlock_irqrestore(&gpio_lock, flags);
1738
David Brownell9c4ba942010-08-10 18:02:24 -07001739 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001740
Alexandre Courbot372e7222013-02-03 01:29:29 +09001741 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001742 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001743 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001744 if (status < 0) {
Mark Brown6424de52013-09-09 10:33:49 +01001745 gpiod_dbg(desc, "chip request fail, %d\n", status);
David Brownell35e8bb52008-10-15 22:03:16 -07001746 /* and it's not available to anyone else ...
1747 * gpio_request() is the fully clean solution.
1748 */
1749 goto lose;
1750 }
1751 }
1752
Alexandre Courbot372e7222013-02-03 01:29:29 +09001753 status = chip->direction_output(chip, offset, value);
David Brownelld2876d02008-02-04 22:28:20 -08001754 if (status == 0)
1755 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001756 trace_gpio_value(desc_to_gpio(desc), 0, value);
1757 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001758lose:
David Brownelld2876d02008-02-04 22:28:20 -08001759 return status;
1760fail:
1761 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001762 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001763 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001764 return status;
1765}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001766EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001767
Felipe Balbic4b5be92010-05-26 14:42:23 -07001768/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001769 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001770 * @gpio: the gpio to set debounce time
1771 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001772 *
1773 * returns -ENOTSUPP if the controller does not support setting
1774 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001775 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001776int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001777{
1778 unsigned long flags;
1779 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001780 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001781 int offset;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001782
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001783 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001784 pr_warn("%s: invalid GPIO\n", __func__);
1785 return -EINVAL;
1786 }
1787
Felipe Balbic4b5be92010-05-26 14:42:23 -07001788 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001789 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001790 gpiod_dbg(desc,
1791 "%s: missing set() or set_debounce() operations\n",
1792 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001793 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001794 }
1795
1796 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001797
1798 status = gpio_ensure_requested(desc);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001799 if (status < 0)
1800 goto fail;
1801
1802 /* now we know the gpio is valid and chip won't vanish */
1803
1804 spin_unlock_irqrestore(&gpio_lock, flags);
1805
David Brownell9c4ba942010-08-10 18:02:24 -07001806 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001807
Alexandre Courbot372e7222013-02-03 01:29:29 +09001808 offset = gpio_chip_hwgpio(desc);
1809 return chip->set_debounce(chip, offset, debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001810
1811fail:
1812 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001813 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001814 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001815
1816 return status;
1817}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001818EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001819
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001820/**
1821 * gpiod_is_active_low - test whether a GPIO is active-low or not
1822 * @desc: the gpio descriptor to test
1823 *
1824 * Returns 1 if the GPIO is active-low, 0 otherwise.
1825 */
1826int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001827{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001828 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001829}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001830EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001831
1832/* I/O calls are only valid after configuration completed; the relevant
1833 * "is this a valid GPIO" error checks should already have been done.
1834 *
1835 * "Get" operations are often inlinable as reading a pin value register,
1836 * and masking the relevant bit in that register.
1837 *
1838 * When "set" operations are inlinable, they involve writing that mask to
1839 * one register to set a low value, or a different register to set it high.
1840 * Otherwise locking is needed, so there may be little value to inlining.
1841 *
1842 *------------------------------------------------------------------------
1843 *
1844 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1845 * have requested the GPIO. That can include implicit requesting by
1846 * a direction setting call. Marking a gpio as requested locks its chip
1847 * in memory, guaranteeing that these table lookups need no more locking
1848 * and that gpiochip_remove() will fail.
1849 *
1850 * REVISIT when debugging, consider adding some instrumentation to ensure
1851 * that the GPIO was actually requested.
1852 */
1853
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001854static int _gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001855{
1856 struct gpio_chip *chip;
1857 int value;
1858 int offset;
1859
1860 chip = desc->chip;
1861 offset = gpio_chip_hwgpio(desc);
1862 value = chip->get ? chip->get(chip, offset) : 0;
1863 trace_gpio_value(desc_to_gpio(desc), 1, value);
1864 return value;
1865}
1866
David Brownelld2876d02008-02-04 22:28:20 -08001867/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001868 * gpiod_get_raw_value() - return a gpio's raw value
1869 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001870 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001871 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1872 * its ACTIVE_LOW status.
1873 *
1874 * This function should be called from contexts where we cannot sleep, and will
1875 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001876 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001877int gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001878{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001879 if (!desc)
1880 return 0;
Mark Browne4e449e2012-02-17 10:46:00 -08001881 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001882 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001883 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001884}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001885EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001886
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001887/**
1888 * gpiod_get_value() - return a gpio's value
1889 * @desc: gpio whose value will be returned
1890 *
1891 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1892 * account.
1893 *
1894 * This function should be called from contexts where we cannot sleep, and will
1895 * complain if the GPIO chip functions potentially sleep.
1896 */
1897int gpiod_get_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001898{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001899 int value;
1900 if (!desc)
1901 return 0;
1902 /* Should be using gpio_get_value_cansleep() */
1903 WARN_ON(desc->chip->can_sleep);
1904
1905 value = _gpiod_get_raw_value(desc);
1906 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1907 value = !value;
1908
1909 return value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001910}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001911EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001912
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301913/*
1914 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001915 * @desc: gpio descriptor whose state need to be set.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301916 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1917 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001918static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301919{
1920 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001921 struct gpio_chip *chip = desc->chip;
1922 int offset = gpio_chip_hwgpio(desc);
1923
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301924 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001925 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301926 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001927 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301928 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001929 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301930 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001931 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301932 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001933 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301934 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001935 gpiod_err(desc,
1936 "%s: Error in set_value for open drain err %d\n",
1937 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301938}
1939
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301940/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001941 * _gpio_set_open_source_value() - Set the open source gpio's value.
1942 * @desc: gpio descriptor whose state need to be set.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301943 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1944 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001945static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301946{
1947 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001948 struct gpio_chip *chip = desc->chip;
1949 int offset = gpio_chip_hwgpio(desc);
1950
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301951 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001952 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301953 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001954 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301955 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001956 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301957 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001958 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301959 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001960 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301961 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001962 gpiod_err(desc,
1963 "%s: Error in set_value for open source err %d\n",
1964 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301965}
1966
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001967static void _gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001968{
1969 struct gpio_chip *chip;
1970
1971 chip = desc->chip;
1972 trace_gpio_value(desc_to_gpio(desc), 0, value);
1973 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1974 _gpio_set_open_drain_value(desc, value);
1975 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1976 _gpio_set_open_source_value(desc, value);
1977 else
1978 chip->set(chip, gpio_chip_hwgpio(desc), value);
1979}
1980
David Brownelld2876d02008-02-04 22:28:20 -08001981/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001982 * gpiod_set_raw_value() - assign a gpio's raw value
1983 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001984 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001985 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001986 * Set the raw value of the GPIO, i.e. the value of its physical line without
1987 * regard for its ACTIVE_LOW status.
1988 *
1989 * This function should be called from contexts where we cannot sleep, and will
1990 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001991 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001992void gpiod_set_raw_value(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001993{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001994 if (!desc)
1995 return;
Mark Browne4e449e2012-02-17 10:46:00 -08001996 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001997 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001998 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001999}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002000EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002001
2002/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002003 * gpiod_set_value() - assign a gpio's value
2004 * @desc: gpio whose value will be assigned
2005 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002006 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002007 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2008 * account
2009 *
2010 * This function should be called from contexts where we cannot sleep, and will
2011 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002012 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002013void gpiod_set_value(struct gpio_desc *desc, int value)
2014{
2015 if (!desc)
2016 return;
2017 /* Should be using gpio_set_value_cansleep() */
2018 WARN_ON(desc->chip->can_sleep);
2019 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2020 value = !value;
2021 _gpiod_set_raw_value(desc, value);
2022}
2023EXPORT_SYMBOL_GPL(gpiod_set_value);
2024
2025/**
2026 * gpiod_cansleep() - report whether gpio value access may sleep
2027 * @desc: gpio to check
2028 *
2029 */
2030int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002031{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002032 if (!desc)
2033 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002034 return desc->chip->can_sleep;
2035}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002036EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002037
David Brownell0f6d5042008-10-15 22:03:14 -07002038/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002039 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2040 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002041 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002042 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2043 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002044 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002045int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002046{
2047 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002048 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002049
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002050 if (!desc)
2051 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002052 chip = desc->chip;
2053 offset = gpio_chip_hwgpio(desc);
2054 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
2055}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002056EXPORT_SYMBOL_GPL(gpiod_to_irq);
David Brownell0f6d5042008-10-15 22:03:14 -07002057
Linus Walleijd468bf92013-09-24 11:54:38 +02002058/**
2059 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
2060 * @gpio: the GPIO line to lock as used for IRQ
2061 *
2062 * This is used directly by GPIO drivers that want to lock down
2063 * a certain GPIO line to be used as IRQs, for example in the
2064 * .to_irq() callback of their gpio_chip, or in the .irq_enable()
2065 * of its irq_chip implementation if the GPIO is known from that
2066 * code.
2067 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002068int gpiod_lock_as_irq(struct gpio_desc *desc)
Linus Walleijd468bf92013-09-24 11:54:38 +02002069{
2070 if (!desc)
2071 return -EINVAL;
2072
2073 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
2074 gpiod_err(desc,
2075 "%s: tried to flag a GPIO set as output for IRQ\n",
2076 __func__);
2077 return -EIO;
2078 }
2079
2080 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
2081 return 0;
2082}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002083EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002084
2085int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
2086{
2087 return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset));
2088}
2089EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
2090
2091/**
2092 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
2093 * @gpio: the GPIO line to unlock from IRQ usage
2094 *
2095 * This is used directly by GPIO drivers that want to indicate
2096 * that a certain GPIO is no longer used exclusively for IRQ.
2097 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002098void gpiod_unlock_as_irq(struct gpio_desc *desc)
Linus Walleijd468bf92013-09-24 11:54:38 +02002099{
2100 if (!desc)
2101 return;
2102
2103 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2104}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002105EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002106
2107void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2108{
2109 return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset));
2110}
2111EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
David Brownelld2876d02008-02-04 22:28:20 -08002112
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002113/**
2114 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2115 * @desc: gpio whose value will be returned
2116 *
2117 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2118 * its ACTIVE_LOW status.
2119 *
2120 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002121 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002122int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002123{
David Brownelld2876d02008-02-04 22:28:20 -08002124 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002125 if (!desc)
2126 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002127 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002128}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002129EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002130
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002131/**
2132 * gpiod_get_value_cansleep() - return a gpio's value
2133 * @desc: gpio whose value will be returned
2134 *
2135 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2136 * account.
2137 *
2138 * This function is to be called from contexts that can sleep.
2139 */
2140int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002141{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002142 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002143
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002144 might_sleep_if(extra_checks);
2145 if (!desc)
2146 return 0;
2147
2148 value = _gpiod_get_raw_value(desc);
2149 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2150 value = !value;
2151
2152 return value;
2153}
2154EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
2155
2156/**
2157 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2158 * @desc: gpio whose value will be assigned
2159 * @value: value to assign
2160 *
2161 * Set the raw value of the GPIO, i.e. the value of its physical line without
2162 * regard for its ACTIVE_LOW status.
2163 *
2164 * This function is to be called from contexts that can sleep.
2165 */
2166void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002167{
David Brownelld2876d02008-02-04 22:28:20 -08002168 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002169 if (!desc)
2170 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002171 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002172}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002173EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002174
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002175/**
2176 * gpiod_set_value_cansleep() - assign a gpio's value
2177 * @desc: gpio whose value will be assigned
2178 * @value: value to assign
2179 *
2180 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2181 * account
2182 *
2183 * This function is to be called from contexts that can sleep.
2184 */
2185void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002186{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002187 might_sleep_if(extra_checks);
2188 if (!desc)
2189 return;
2190
2191 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2192 value = !value;
2193 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002194}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002195EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002196
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002197/**
2198 * gpiod_add_table() - register GPIO device consumers
2199 * @table: array of consumers to register
2200 * @num: number of consumers in table
2201 */
2202void gpiod_add_table(struct gpiod_lookup *table, size_t size)
2203{
2204 mutex_lock(&gpio_lookup_lock);
2205
2206 while (size--) {
2207 list_add_tail(&table->list, &gpio_lookup_list);
2208 table++;
2209 }
2210
2211 mutex_unlock(&gpio_lookup_lock);
2212}
2213
2214/*
2215 * Caller must have a acquired gpio_lookup_lock
2216 */
2217static struct gpio_chip *find_chip_by_name(const char *name)
2218{
2219 struct gpio_chip *chip = NULL;
2220
2221 list_for_each_entry(chip, &gpio_lookup_list, list) {
2222 if (chip->label == NULL)
2223 continue;
2224 if (!strcmp(chip->label, name))
2225 break;
2226 }
2227
2228 return chip;
2229}
2230
2231#ifdef CONFIG_OF
2232static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
2233 unsigned int idx, unsigned long *flags)
2234{
2235 char prop_name[32]; /* 32 is max size of property name */
2236 enum of_gpio_flags of_flags;
2237 struct gpio_desc *desc;
2238
2239 if (con_id)
2240 snprintf(prop_name, 32, "%s-gpios", con_id);
2241 else
2242 snprintf(prop_name, 32, "gpios");
2243
2244 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2245 &of_flags);
2246
2247 if (IS_ERR(desc))
2248 return desc;
2249
2250 if (of_flags & OF_GPIO_ACTIVE_LOW)
2251 *flags |= GPIOF_ACTIVE_LOW;
2252
2253 return desc;
2254}
2255#else
2256static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
2257 unsigned int idx, unsigned long *flags)
2258{
2259 return ERR_PTR(-ENODEV);
2260}
2261#endif
2262
2263static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
2264 unsigned int idx, unsigned long *flags)
2265{
2266 const char *dev_id = dev ? dev_name(dev) : NULL;
2267 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2268 unsigned int match, best = 0;
2269 struct gpiod_lookup *p;
2270
2271 mutex_lock(&gpio_lookup_lock);
2272
2273 list_for_each_entry(p, &gpio_lookup_list, list) {
2274 match = 0;
2275
2276 if (p->dev_id) {
2277 if (!dev_id || strcmp(p->dev_id, dev_id))
2278 continue;
2279
2280 match += 2;
2281 }
2282
2283 if (p->con_id) {
2284 if (!con_id || strcmp(p->con_id, con_id))
2285 continue;
2286
2287 match += 1;
2288 }
2289
2290 if (p->idx != idx)
2291 continue;
2292
2293 if (match > best) {
2294 struct gpio_chip *chip;
2295
2296 chip = find_chip_by_name(p->chip_label);
2297
2298 if (!chip) {
2299 dev_warn(dev, "cannot find GPIO chip %s\n",
2300 p->chip_label);
2301 continue;
2302 }
2303
2304 if (chip->ngpio >= p->chip_hwnum) {
2305 dev_warn(dev, "GPIO chip %s has %d GPIOs\n",
2306 chip->label, chip->ngpio);
2307 continue;
2308 }
2309
2310 desc = gpio_to_desc(chip->base + p->chip_hwnum);
2311 *flags = p->flags;
2312
2313 if (match != 3)
2314 best = match;
2315 else
2316 break;
2317 }
2318 }
2319
2320 mutex_unlock(&gpio_lookup_lock);
2321
2322 return desc;
2323}
2324
2325/**
2326 * gpio_get - obtain a GPIO for a given GPIO function
2327 * @dev: GPIO consumer
2328 * @con_id: function within the GPIO consumer
2329 *
2330 * Return the GPIO descriptor corresponding to the function con_id of device
2331 * dev, or an IS_ERR() condition if an error occured.
2332 */
2333struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id)
2334{
2335 return gpiod_get_index(dev, con_id, 0);
2336}
2337EXPORT_SYMBOL_GPL(gpiod_get);
2338
2339/**
2340 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2341 * @dev: GPIO consumer
2342 * @con_id: function within the GPIO consumer
2343 * @idx: index of the GPIO to obtain in the consumer
2344 *
2345 * This variant of gpiod_get() allows to access GPIOs other than the first
2346 * defined one for functions that define several GPIOs.
2347 *
2348 * Return a valid GPIO descriptor, or an IS_ERR() condition in case of error.
2349 */
2350struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2351 const char *con_id,
2352 unsigned int idx)
2353{
2354 struct gpio_desc *desc;
2355 int status;
2356 unsigned long flags = 0;
2357
2358 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2359
2360 /* Using device tree? */
2361 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
2362 dev_dbg(dev, "using device tree for GPIO lookup\n");
2363 desc = of_find_gpio(dev, con_id, idx, &flags);
2364 } else {
2365 dev_dbg(dev, "using lookup tables for GPIO lookup");
2366 desc = gpiod_find(dev, con_id, idx, &flags);
2367 }
2368
2369 if (IS_ERR(desc)) {
2370 dev_warn(dev, "lookup for GPIO %s failed\n", con_id);
2371 return desc;
2372 }
2373
2374 status = gpiod_request(desc, con_id);
2375
2376 if (status < 0)
2377 return ERR_PTR(status);
2378
2379 if (flags & GPIOF_ACTIVE_LOW)
2380 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2381
2382 return desc;
2383}
2384EXPORT_SYMBOL_GPL(gpiod_get_index);
2385
2386/**
2387 * gpiod_put - dispose of a GPIO descriptor
2388 * @desc: GPIO descriptor to dispose of
2389 *
2390 * No descriptor can be used after gpiod_put() has been called on it.
2391 */
2392void gpiod_put(struct gpio_desc *desc)
2393{
2394 gpiod_free(desc);
2395}
2396EXPORT_SYMBOL_GPL(gpiod_put);
2397
David Brownelld2876d02008-02-04 22:28:20 -08002398#ifdef CONFIG_DEBUG_FS
2399
David Brownelld2876d02008-02-04 22:28:20 -08002400static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2401{
2402 unsigned i;
2403 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002404 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002405 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002406 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002407
2408 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2409 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2410 continue;
2411
Alexandre Courbot372e7222013-02-03 01:29:29 +09002412 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002413 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002414 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2415 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002416 gpio, gdesc->label,
2417 is_out ? "out" : "in ",
2418 chip->get
2419 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002420 : "? ",
2421 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002422 seq_printf(s, "\n");
2423 }
2424}
2425
Thierry Redingf9c4a312012-04-12 13:26:01 +02002426static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002427{
Grant Likely362432a2013-02-09 09:41:49 +00002428 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002429 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002430 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002431
2432 s->private = "";
2433
Grant Likely362432a2013-02-09 09:41:49 +00002434 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002435 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002436 if (index-- == 0) {
2437 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002438 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002439 }
2440 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002441
2442 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002443}
2444
2445static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2446{
Grant Likely362432a2013-02-09 09:41:49 +00002447 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002448 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002449 void *ret = NULL;
2450
Grant Likely362432a2013-02-09 09:41:49 +00002451 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002452 if (list_is_last(&chip->list, &gpio_chips))
2453 ret = NULL;
2454 else
2455 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002456 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002457
2458 s->private = "\n";
2459 ++*pos;
2460
2461 return ret;
2462}
2463
2464static void gpiolib_seq_stop(struct seq_file *s, void *v)
2465{
2466}
2467
2468static int gpiolib_seq_show(struct seq_file *s, void *v)
2469{
2470 struct gpio_chip *chip = v;
2471 struct device *dev;
2472
2473 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2474 chip->base, chip->base + chip->ngpio - 1);
2475 dev = chip->dev;
2476 if (dev)
2477 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2478 dev_name(dev));
2479 if (chip->label)
2480 seq_printf(s, ", %s", chip->label);
2481 if (chip->can_sleep)
2482 seq_printf(s, ", can sleep");
2483 seq_printf(s, ":\n");
2484
2485 if (chip->dbg_show)
2486 chip->dbg_show(s, chip);
2487 else
2488 gpiolib_dbg_show(s, chip);
2489
David Brownelld2876d02008-02-04 22:28:20 -08002490 return 0;
2491}
2492
Thierry Redingf9c4a312012-04-12 13:26:01 +02002493static const struct seq_operations gpiolib_seq_ops = {
2494 .start = gpiolib_seq_start,
2495 .next = gpiolib_seq_next,
2496 .stop = gpiolib_seq_stop,
2497 .show = gpiolib_seq_show,
2498};
2499
David Brownelld2876d02008-02-04 22:28:20 -08002500static int gpiolib_open(struct inode *inode, struct file *file)
2501{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002502 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002503}
2504
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002505static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002506 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002507 .open = gpiolib_open,
2508 .read = seq_read,
2509 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002510 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002511};
2512
2513static int __init gpiolib_debugfs_init(void)
2514{
2515 /* /sys/kernel/debug/gpio */
2516 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2517 NULL, NULL, &gpiolib_operations);
2518 return 0;
2519}
2520subsys_initcall(gpiolib_debugfs_init);
2521
2522#endif /* DEBUG_FS */