blob: 224abdc4b09507082ca7f9899f065a3076d6b085 [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
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 Courbot1a989d02013-02-03 01:29:24 +090073static LIST_HEAD(gpio_chips);
74
Daniel Glöcknerff77c352009-09-22 16:46:38 -070075#ifdef CONFIG_GPIO_SYSFS
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070076static DEFINE_IDR(dirent_idr);
Daniel Glöcknerff77c352009-09-22 16:46:38 -070077#endif
78
Alexandre Courbot372e7222013-02-03 01:29:29 +090079static int gpiod_request(struct gpio_desc *desc, const char *label);
80static void gpiod_free(struct gpio_desc *desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090081
Mark Brown7b17b592013-09-09 10:33:50 +010082#ifdef CONFIG_DEBUG_FS
83#define gpiod_emerg(desc, fmt, ...) \
84 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
85 ##__VA_ARGS__)
86#define gpiod_crit(desc, fmt, ...) \
87 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
88 ##__VA_ARGS__)
89#define gpiod_err(desc, fmt, ...) \
90 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
91 ##__VA_ARGS__)
92#define gpiod_warn(desc, fmt, ...) \
93 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
94 ##__VA_ARGS__)
95#define gpiod_info(desc, fmt, ...) \
96 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
97 ##__VA_ARGS__)
98#define gpiod_dbg(desc, fmt, ...) \
99 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
100 ##__VA_ARGS__)
101#else
Mark Brown6424de52013-09-09 10:33:49 +0100102#define gpiod_emerg(desc, fmt, ...) \
103 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
104#define gpiod_crit(desc, fmt, ...) \
105 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
106#define gpiod_err(desc, fmt, ...) \
107 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
108#define gpiod_warn(desc, fmt, ...) \
109 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
110#define gpiod_info(desc, fmt, ...) \
111 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
112#define gpiod_dbg(desc, fmt, ...) \
113 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Mark Brown7b17b592013-09-09 10:33:50 +0100114#endif
Alexandre Courbot372e7222013-02-03 01:29:29 +0900115
David Brownelld2876d02008-02-04 22:28:20 -0800116static inline void desc_set_label(struct gpio_desc *d, const char *label)
117{
118#ifdef CONFIG_DEBUG_FS
119 d->label = label;
120#endif
121}
122
Alexandre Courbot372e7222013-02-03 01:29:29 +0900123/*
124 * Return the GPIO number of the passed descriptor relative to its chip
125 */
126static int gpio_chip_hwgpio(const struct gpio_desc *desc)
127{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900128 return desc - &desc->chip->desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900129}
130
131/**
132 * Convert a GPIO number to its descriptor
133 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700134struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900135{
136 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
137 return NULL;
138 else
139 return &gpio_desc[gpio];
140}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700141EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900142
143/**
Linus Walleijd468bf92013-09-24 11:54:38 +0200144 * Convert an offset on a certain chip to a corresponding descriptor
145 */
146static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip,
147 unsigned int offset)
148{
149 unsigned int gpio = chip->base + offset;
150
151 return gpio_to_desc(gpio);
152}
153
154/**
Alexandre Courbot372e7222013-02-03 01:29:29 +0900155 * Convert a GPIO descriptor to the integer namespace.
156 * This should disappear in the future but is needed since we still
157 * use GPIO numbers for error messages and sysfs nodes
158 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700159int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900160{
Alexandre Courbot8c0fca82013-10-04 10:59:57 -0700161 return desc - &gpio_desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900162}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700163EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900164
165
David Brownelld2876d02008-02-04 22:28:20 -0800166/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
167 * when setting direction, and otherwise illegal. Until board setup code
168 * and drivers use explicit requests everywhere (which won't happen when
169 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -0700170 * message should motivate switching to explicit requests... so should
171 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -0700172 *
173 * NOTE: the autorequest mechanism is going away; at this point it's
174 * only "legal" in the sense that (old) code using it won't break yet,
175 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -0800176 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900177static int gpio_ensure_requested(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800178{
David Brownell8a0cecf2009-04-02 16:57:06 -0700179 const struct gpio_chip *chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900180 const int gpio = desc_to_gpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -0700181
David Brownell8a0cecf2009-04-02 16:57:06 -0700182 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
183 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700184 if (!try_module_get(chip->owner)) {
185 pr_err("GPIO-%d: module can't be gotten \n", gpio);
186 clear_bit(FLAG_REQUESTED, &desc->flags);
187 /* lose */
188 return -EIO;
189 }
David Brownelld2876d02008-02-04 22:28:20 -0800190 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700191 /* caller must chip->request() w/o spinlock */
192 if (chip->request)
193 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800194 }
David Brownell35e8bb52008-10-15 22:03:16 -0700195 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800196}
197
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700198/**
199 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
200 * @desc: descriptor to return the chip of
201 */
202struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900203{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900204 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900205}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700206EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800207
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700208/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
209static int gpiochip_find_base(int ngpio)
210{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900211 struct gpio_chip *chip;
212 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700213
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900214 list_for_each_entry_reverse(chip, &gpio_chips, list) {
215 /* found a free space? */
216 if (chip->base + chip->ngpio <= base)
217 break;
218 else
219 /* nope, check the space right before the chip */
220 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700221 }
222
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900223 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700224 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900225 return base;
226 } else {
227 pr_err("%s: cannot find free range\n", __func__);
228 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700229 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700230}
231
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700232/**
233 * gpiod_get_direction - return the current direction of a GPIO
234 * @desc: GPIO to get the direction of
235 *
236 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
237 *
238 * This function may sleep if gpiod_cansleep() is true.
239 */
240int gpiod_get_direction(const struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300241{
242 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900243 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300244 int status = -EINVAL;
245
Alexandre Courbot372e7222013-02-03 01:29:29 +0900246 chip = gpiod_to_chip(desc);
247 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300248
249 if (!chip->get_direction)
250 return status;
251
Alexandre Courbot372e7222013-02-03 01:29:29 +0900252 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300253 if (status > 0) {
254 /* GPIOF_DIR_IN, or other positive */
255 status = 1;
Alexandre Courbotdef63432013-02-15 14:46:15 +0900256 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
257 * so it does not affect constness per se */
258 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300259 }
260 if (status == 0) {
261 /* GPIOF_DIR_OUT */
Alexandre Courbotdef63432013-02-15 14:46:15 +0900262 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300263 }
264 return status;
265}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700266EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300267
David Brownelld8f388d82008-07-25 01:46:07 -0700268#ifdef CONFIG_GPIO_SYSFS
269
270/* lock protects against unexport_gpio() being called while
271 * sysfs files are active.
272 */
273static DEFINE_MUTEX(sysfs_lock);
274
275/*
276 * /sys/class/gpio/gpioN... only for GPIOs that are exported
277 * /direction
278 * * MAY BE OMITTED if kernel won't allow direction changes
279 * * is read/write as "in" or "out"
280 * * may also be written as "high" or "low", initializing
281 * output value as specified ("out" implies "low")
282 * /value
283 * * always readable, subject to hardware behavior
284 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700285 * /edge
286 * * configures behavior of poll(2) on /value
287 * * available only if pin can generate IRQs on input
288 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800289 * /active_low
290 * * configures polarity of /value
291 * * is read/write as zero/nonzero
292 * * also affects existing and subsequent "falling" and "rising"
293 * /edge configuration
David Brownelld8f388d82008-07-25 01:46:07 -0700294 */
295
296static ssize_t gpio_direction_show(struct device *dev,
297 struct device_attribute *attr, char *buf)
298{
Alexandre Courbotdef63432013-02-15 14:46:15 +0900299 const struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700300 ssize_t status;
301
302 mutex_lock(&sysfs_lock);
303
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900304 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700305 status = -EIO;
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900306 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +0900307 gpiod_get_direction(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700308 status = sprintf(buf, "%s\n",
309 test_bit(FLAG_IS_OUT, &desc->flags)
310 ? "out" : "in");
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900311 }
David Brownelld8f388d82008-07-25 01:46:07 -0700312
313 mutex_unlock(&sysfs_lock);
314 return status;
315}
316
317static ssize_t gpio_direction_store(struct device *dev,
318 struct device_attribute *attr, const char *buf, size_t size)
319{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900320 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700321 ssize_t status;
322
323 mutex_lock(&sysfs_lock);
324
325 if (!test_bit(FLAG_EXPORT, &desc->flags))
326 status = -EIO;
327 else if (sysfs_streq(buf, "high"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900328 status = gpiod_direction_output(desc, 1);
David Brownelld8f388d82008-07-25 01:46:07 -0700329 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900330 status = gpiod_direction_output(desc, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700331 else if (sysfs_streq(buf, "in"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900332 status = gpiod_direction_input(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700333 else
334 status = -EINVAL;
335
336 mutex_unlock(&sysfs_lock);
337 return status ? : size;
338}
339
Jani Nikula07697462009-12-15 16:46:20 -0800340static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700341 gpio_direction_show, gpio_direction_store);
342
343static ssize_t gpio_value_show(struct device *dev,
344 struct device_attribute *attr, char *buf)
345{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900346 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700347 ssize_t status;
348
349 mutex_lock(&sysfs_lock);
350
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700351 if (!test_bit(FLAG_EXPORT, &desc->flags))
David Brownelld8f388d82008-07-25 01:46:07 -0700352 status = -EIO;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700353 else
354 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
David Brownelld8f388d82008-07-25 01:46:07 -0700355
356 mutex_unlock(&sysfs_lock);
357 return status;
358}
359
360static ssize_t gpio_value_store(struct device *dev,
361 struct device_attribute *attr, const char *buf, size_t size)
362{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900363 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700364 ssize_t status;
365
366 mutex_lock(&sysfs_lock);
367
368 if (!test_bit(FLAG_EXPORT, &desc->flags))
369 status = -EIO;
370 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
371 status = -EPERM;
372 else {
373 long value;
374
Jingoo Hana3d88c92013-07-19 16:12:50 +0900375 status = kstrtol(buf, 0, &value);
David Brownelld8f388d82008-07-25 01:46:07 -0700376 if (status == 0) {
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700377 gpiod_set_value_cansleep(desc, value);
David Brownelld8f388d82008-07-25 01:46:07 -0700378 status = size;
379 }
380 }
381
382 mutex_unlock(&sysfs_lock);
383 return status;
384}
385
Jani Nikula07697462009-12-15 16:46:20 -0800386static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700387 gpio_value_show, gpio_value_store);
388
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700389static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
390{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700391 struct sysfs_dirent *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700392
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700393 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700394 return IRQ_HANDLED;
395}
396
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700397static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
398 unsigned long gpio_flags)
399{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700400 struct sysfs_dirent *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700401 unsigned long irq_flags;
402 int ret, irq, id;
403
404 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
405 return 0;
406
Alexandre Courbot372e7222013-02-03 01:29:29 +0900407 irq = gpiod_to_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700408 if (irq < 0)
409 return -EIO;
410
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700411 id = desc->flags >> ID_SHIFT;
412 value_sd = idr_find(&dirent_idr, id);
413 if (value_sd)
414 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700415
416 desc->flags &= ~GPIO_TRIGGER_MASK;
417
418 if (!gpio_flags) {
Linus Walleijd468bf92013-09-24 11:54:38 +0200419 gpiod_unlock_as_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700420 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700421 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700422 }
423
424 irq_flags = IRQF_SHARED;
425 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800426 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
427 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700428 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800429 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
430 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700431
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700432 if (!value_sd) {
433 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
434 if (!value_sd) {
435 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700436 goto err_out;
437 }
438
Tejun Heo62f516b2013-02-27 17:04:06 -0800439 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
440 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700441 goto free_sd;
Tejun Heo62f516b2013-02-27 17:04:06 -0800442 id = ret;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700443
444 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700445 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700446
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700447 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700448 ret = -ERANGE;
449 goto free_id;
450 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700451 }
452
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700453 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700454 "gpiolib", value_sd);
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700455 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700456 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700457
Linus Walleijd468bf92013-09-24 11:54:38 +0200458 ret = gpiod_lock_as_irq(desc);
459 if (ret < 0) {
460 gpiod_warn(desc, "failed to flag the GPIO for IRQ\n");
461 goto free_id;
462 }
463
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700464 desc->flags |= gpio_flags;
465 return 0;
466
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700467free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700468 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700469 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700470free_sd:
471 if (value_sd)
472 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700473err_out:
474 return ret;
475}
476
477static const struct {
478 const char *name;
479 unsigned long flags;
480} trigger_types[] = {
481 { "none", 0 },
482 { "falling", BIT(FLAG_TRIG_FALL) },
483 { "rising", BIT(FLAG_TRIG_RISE) },
484 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
485};
486
487static ssize_t gpio_edge_show(struct device *dev,
488 struct device_attribute *attr, char *buf)
489{
490 const struct gpio_desc *desc = dev_get_drvdata(dev);
491 ssize_t status;
492
493 mutex_lock(&sysfs_lock);
494
495 if (!test_bit(FLAG_EXPORT, &desc->flags))
496 status = -EIO;
497 else {
498 int i;
499
500 status = 0;
501 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
502 if ((desc->flags & GPIO_TRIGGER_MASK)
503 == trigger_types[i].flags) {
504 status = sprintf(buf, "%s\n",
505 trigger_types[i].name);
506 break;
507 }
508 }
509
510 mutex_unlock(&sysfs_lock);
511 return status;
512}
513
514static ssize_t gpio_edge_store(struct device *dev,
515 struct device_attribute *attr, const char *buf, size_t size)
516{
517 struct gpio_desc *desc = dev_get_drvdata(dev);
518 ssize_t status;
519 int i;
520
521 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
522 if (sysfs_streq(trigger_types[i].name, buf))
523 goto found;
524 return -EINVAL;
525
526found:
527 mutex_lock(&sysfs_lock);
528
529 if (!test_bit(FLAG_EXPORT, &desc->flags))
530 status = -EIO;
531 else {
532 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
533 if (!status)
534 status = size;
535 }
536
537 mutex_unlock(&sysfs_lock);
538
539 return status;
540}
541
542static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
543
Jani Nikula07697462009-12-15 16:46:20 -0800544static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
545 int value)
546{
547 int status = 0;
548
549 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
550 return 0;
551
552 if (value)
553 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
554 else
555 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
556
557 /* reconfigure poll(2) support if enabled on one edge only */
558 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
559 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
560 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
561
562 gpio_setup_irq(desc, dev, 0);
563 status = gpio_setup_irq(desc, dev, trigger_flags);
564 }
565
566 return status;
567}
568
569static ssize_t gpio_active_low_show(struct device *dev,
570 struct device_attribute *attr, char *buf)
571{
572 const struct gpio_desc *desc = dev_get_drvdata(dev);
573 ssize_t status;
574
575 mutex_lock(&sysfs_lock);
576
577 if (!test_bit(FLAG_EXPORT, &desc->flags))
578 status = -EIO;
579 else
580 status = sprintf(buf, "%d\n",
581 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
582
583 mutex_unlock(&sysfs_lock);
584
585 return status;
586}
587
588static ssize_t gpio_active_low_store(struct device *dev,
589 struct device_attribute *attr, const char *buf, size_t size)
590{
591 struct gpio_desc *desc = dev_get_drvdata(dev);
592 ssize_t status;
593
594 mutex_lock(&sysfs_lock);
595
596 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
597 status = -EIO;
598 } else {
599 long value;
600
Jingoo Hana3d88c92013-07-19 16:12:50 +0900601 status = kstrtol(buf, 0, &value);
Jani Nikula07697462009-12-15 16:46:20 -0800602 if (status == 0)
603 status = sysfs_set_active_low(desc, dev, value != 0);
604 }
605
606 mutex_unlock(&sysfs_lock);
607
608 return status ? : size;
609}
610
611static const DEVICE_ATTR(active_low, 0644,
612 gpio_active_low_show, gpio_active_low_store);
613
David Brownelld8f388d82008-07-25 01:46:07 -0700614static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d82008-07-25 01:46:07 -0700615 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800616 &dev_attr_active_low.attr,
David Brownelld8f388d82008-07-25 01:46:07 -0700617 NULL,
618};
619
620static const struct attribute_group gpio_attr_group = {
621 .attrs = (struct attribute **) gpio_attrs,
622};
623
624/*
625 * /sys/class/gpio/gpiochipN/
626 * /base ... matching gpio_chip.base (N)
627 * /label ... matching gpio_chip.label
628 * /ngpio ... matching gpio_chip.ngpio
629 */
630
631static ssize_t chip_base_show(struct device *dev,
632 struct device_attribute *attr, char *buf)
633{
634 const struct gpio_chip *chip = dev_get_drvdata(dev);
635
636 return sprintf(buf, "%d\n", chip->base);
637}
638static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
639
640static ssize_t chip_label_show(struct device *dev,
641 struct device_attribute *attr, char *buf)
642{
643 const struct gpio_chip *chip = dev_get_drvdata(dev);
644
645 return sprintf(buf, "%s\n", chip->label ? : "");
646}
647static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
648
649static ssize_t chip_ngpio_show(struct device *dev,
650 struct device_attribute *attr, char *buf)
651{
652 const struct gpio_chip *chip = dev_get_drvdata(dev);
653
654 return sprintf(buf, "%u\n", chip->ngpio);
655}
656static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
657
658static const struct attribute *gpiochip_attrs[] = {
659 &dev_attr_base.attr,
660 &dev_attr_label.attr,
661 &dev_attr_ngpio.attr,
662 NULL,
663};
664
665static const struct attribute_group gpiochip_attr_group = {
666 .attrs = (struct attribute **) gpiochip_attrs,
667};
668
669/*
670 * /sys/class/gpio/export ... write-only
671 * integer N ... number of GPIO to export (full access)
672 * /sys/class/gpio/unexport ... write-only
673 * integer N ... number of GPIO to unexport
674 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100675static ssize_t export_store(struct class *class,
676 struct class_attribute *attr,
677 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700678{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900679 long gpio;
680 struct gpio_desc *desc;
681 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700682
Jingoo Hana3d88c92013-07-19 16:12:50 +0900683 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700684 if (status < 0)
685 goto done;
686
Alexandre Courbot372e7222013-02-03 01:29:29 +0900687 desc = gpio_to_desc(gpio);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900688 /* reject invalid GPIOs */
689 if (!desc) {
690 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
691 return -EINVAL;
692 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900693
David Brownelld8f388d82008-07-25 01:46:07 -0700694 /* No extra locking here; FLAG_SYSFS just signifies that the
695 * request and export were done by on behalf of userspace, so
696 * they may be undone on its behalf too.
697 */
698
Alexandre Courbot372e7222013-02-03 01:29:29 +0900699 status = gpiod_request(desc, "sysfs");
Mathias Nymanad2fab32012-10-25 14:03:03 +0300700 if (status < 0) {
701 if (status == -EPROBE_DEFER)
702 status = -ENODEV;
David Brownelld8f388d82008-07-25 01:46:07 -0700703 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +0300704 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900705 status = gpiod_export(desc, true);
David Brownelld8f388d82008-07-25 01:46:07 -0700706 if (status < 0)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900707 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700708 else
Alexandre Courbot372e7222013-02-03 01:29:29 +0900709 set_bit(FLAG_SYSFS, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700710
711done:
712 if (status)
713 pr_debug("%s: status %d\n", __func__, status);
714 return status ? : len;
715}
716
Andi Kleen28812fe2010-01-05 12:48:07 +0100717static ssize_t unexport_store(struct class *class,
718 struct class_attribute *attr,
719 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700720{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900721 long gpio;
722 struct gpio_desc *desc;
723 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700724
Jingoo Hana3d88c92013-07-19 16:12:50 +0900725 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700726 if (status < 0)
727 goto done;
728
Alexandre Courbot372e7222013-02-03 01:29:29 +0900729 desc = gpio_to_desc(gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700730 /* reject bogus commands (gpio_unexport ignores them) */
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900731 if (!desc) {
732 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
733 return -EINVAL;
734 }
735
736 status = -EINVAL;
David Brownelld8f388d82008-07-25 01:46:07 -0700737
738 /* No extra locking here; FLAG_SYSFS just signifies that the
739 * request and export were done by on behalf of userspace, so
740 * they may be undone on its behalf too.
741 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900742 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700743 status = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900744 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700745 }
746done:
747 if (status)
748 pr_debug("%s: status %d\n", __func__, status);
749 return status ? : len;
750}
751
752static struct class_attribute gpio_class_attrs[] = {
753 __ATTR(export, 0200, NULL, export_store),
754 __ATTR(unexport, 0200, NULL, unexport_store),
755 __ATTR_NULL,
756};
757
758static struct class gpio_class = {
759 .name = "gpio",
760 .owner = THIS_MODULE,
761
762 .class_attrs = gpio_class_attrs,
763};
764
765
766/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700767 * gpiod_export - export a GPIO through sysfs
David Brownelld8f388d82008-07-25 01:46:07 -0700768 * @gpio: gpio to make available, already requested
769 * @direction_may_change: true if userspace may change gpio direction
770 * Context: arch_initcall or later
771 *
772 * When drivers want to make a GPIO accessible to userspace after they
773 * have requested it -- perhaps while debugging, or as part of their
774 * public interface -- they may use this routine. If the GPIO can
775 * change direction (some can't) and the caller allows it, userspace
776 * will see "direction" sysfs attribute which may be used to change
777 * the gpio's direction. A "value" attribute will always be provided.
778 *
779 * Returns zero on success, else an error.
780 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700781int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
David Brownelld8f388d82008-07-25 01:46:07 -0700782{
783 unsigned long flags;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100784 int status;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700785 const char *ioname = NULL;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100786 struct device *dev;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900787 int offset;
David Brownelld8f388d82008-07-25 01:46:07 -0700788
789 /* can't export until sysfs is available ... */
790 if (!gpio_class.p) {
791 pr_debug("%s: called too early!\n", __func__);
792 return -ENOENT;
793 }
794
Alexandre Courbot372e7222013-02-03 01:29:29 +0900795 if (!desc) {
796 pr_debug("%s: invalid gpio descriptor\n", __func__);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100797 return -EINVAL;
798 }
David Brownelld8f388d82008-07-25 01:46:07 -0700799
800 mutex_lock(&sysfs_lock);
801
802 spin_lock_irqsave(&gpio_lock, flags);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100803 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
804 test_bit(FLAG_EXPORT, &desc->flags)) {
805 spin_unlock_irqrestore(&gpio_lock, flags);
806 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +0900807 __func__, desc_to_gpio(desc),
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100808 test_bit(FLAG_REQUESTED, &desc->flags),
809 test_bit(FLAG_EXPORT, &desc->flags));
Dan Carpenter529f2ad2012-10-26 09:59:43 +0300810 status = -EPERM;
811 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700812 }
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100813
814 if (!desc->chip->direction_input || !desc->chip->direction_output)
815 direction_may_change = false;
David Brownelld8f388d82008-07-25 01:46:07 -0700816 spin_unlock_irqrestore(&gpio_lock, flags);
817
Alexandre Courbot372e7222013-02-03 01:29:29 +0900818 offset = gpio_chip_hwgpio(desc);
819 if (desc->chip->names && desc->chip->names[offset])
820 ioname = desc->chip->names[offset];
Daniel Silverstone926b663c2009-04-02 16:57:05 -0700821
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100822 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Alexandre Courbot372e7222013-02-03 01:29:29 +0900823 desc, ioname ? ioname : "gpio%u",
824 desc_to_gpio(desc));
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100825 if (IS_ERR(dev)) {
826 status = PTR_ERR(dev);
827 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700828 }
829
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100830 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
David Brownelld8f388d82008-07-25 01:46:07 -0700831 if (status)
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100832 goto fail_unregister_device;
David Brownelld8f388d82008-07-25 01:46:07 -0700833
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100834 if (direction_may_change) {
835 status = device_create_file(dev, &dev_attr_direction);
836 if (status)
837 goto fail_unregister_device;
838 }
839
Alexandre Courbot372e7222013-02-03 01:29:29 +0900840 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100841 !test_bit(FLAG_IS_OUT, &desc->flags))) {
842 status = device_create_file(dev, &dev_attr_edge);
843 if (status)
844 goto fail_unregister_device;
845 }
846
847 set_bit(FLAG_EXPORT, &desc->flags);
848 mutex_unlock(&sysfs_lock);
849 return 0;
850
851fail_unregister_device:
852 device_unregister(dev);
853fail_unlock:
854 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900855 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
856 status);
David Brownelld8f388d82008-07-25 01:46:07 -0700857 return status;
858}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700859EXPORT_SYMBOL_GPL(gpiod_export);
David Brownelld8f388d82008-07-25 01:46:07 -0700860
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100861static int match_export(struct device *dev, const void *data)
David Brownelld8f388d82008-07-25 01:46:07 -0700862{
863 return dev_get_drvdata(dev) == data;
864}
865
866/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700867 * gpiod_export_link - create a sysfs link to an exported GPIO node
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700868 * @dev: device under which to create symlink
869 * @name: name of the symlink
870 * @gpio: gpio to create symlink to, already exported
871 *
872 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
873 * node. Caller is responsible for unlinking.
874 *
875 * Returns zero on success, else an error.
876 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700877int gpiod_export_link(struct device *dev, const char *name,
878 struct gpio_desc *desc)
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700879{
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700880 int status = -EINVAL;
881
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900882 if (!desc) {
883 pr_warn("%s: invalid GPIO\n", __func__);
884 return -EINVAL;
885 }
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700886
887 mutex_lock(&sysfs_lock);
888
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700889 if (test_bit(FLAG_EXPORT, &desc->flags)) {
890 struct device *tdev;
891
892 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
893 if (tdev != NULL) {
894 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
895 name);
896 } else {
897 status = -ENODEV;
898 }
899 }
900
901 mutex_unlock(&sysfs_lock);
902
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700903 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900904 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
905 status);
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700906
907 return status;
908}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700909EXPORT_SYMBOL_GPL(gpiod_export_link);
Jani Nikula07697462009-12-15 16:46:20 -0800910
911/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700912 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
Jani Nikula07697462009-12-15 16:46:20 -0800913 * @gpio: gpio to change
914 * @value: non-zero to use active low, i.e. inverted values
915 *
916 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
917 * The GPIO does not have to be exported yet. If poll(2) support has
918 * been enabled for either rising or falling edge, it will be
919 * reconfigured to follow the new polarity.
920 *
921 * Returns zero on success, else an error.
922 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700923int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
Jani Nikula07697462009-12-15 16:46:20 -0800924{
Jani Nikula07697462009-12-15 16:46:20 -0800925 struct device *dev = NULL;
926 int status = -EINVAL;
927
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900928 if (!desc) {
929 pr_warn("%s: invalid GPIO\n", __func__);
930 return -EINVAL;
931 }
Jani Nikula07697462009-12-15 16:46:20 -0800932
933 mutex_lock(&sysfs_lock);
934
Jani Nikula07697462009-12-15 16:46:20 -0800935 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800936 dev = class_find_device(&gpio_class, NULL, desc, match_export);
937 if (dev == NULL) {
938 status = -ENODEV;
939 goto unlock;
940 }
941 }
942
943 status = sysfs_set_active_low(desc, dev, value);
944
945unlock:
946 mutex_unlock(&sysfs_lock);
947
Jani Nikula07697462009-12-15 16:46:20 -0800948 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900949 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
950 status);
Jani Nikula07697462009-12-15 16:46:20 -0800951
952 return status;
953}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700954EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
Jani Nikula07697462009-12-15 16:46:20 -0800955
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700956/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700957 * gpiod_unexport - reverse effect of gpio_export()
David Brownelld8f388d82008-07-25 01:46:07 -0700958 * @gpio: gpio to make unavailable
959 *
960 * This is implicit on gpio_free().
961 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700962void gpiod_unexport(struct gpio_desc *desc)
David Brownelld8f388d82008-07-25 01:46:07 -0700963{
Jon Povey6a99ad42010-07-27 13:18:06 -0700964 int status = 0;
Ming Lei864533c2012-02-13 22:53:20 +0800965 struct device *dev = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -0700966
Alexandre Courbot372e7222013-02-03 01:29:29 +0900967 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900968 pr_warn("%s: invalid GPIO\n", __func__);
969 return;
Jon Povey6a99ad42010-07-27 13:18:06 -0700970 }
David Brownelld8f388d82008-07-25 01:46:07 -0700971
972 mutex_lock(&sysfs_lock);
973
David Brownelld8f388d82008-07-25 01:46:07 -0700974 if (test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700975
976 dev = class_find_device(&gpio_class, NULL, desc, match_export);
977 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700978 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700979 clear_bit(FLAG_EXPORT, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700980 } else
981 status = -ENODEV;
982 }
983
984 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900985
Ming Lei864533c2012-02-13 22:53:20 +0800986 if (dev) {
987 device_unregister(dev);
988 put_device(dev);
989 }
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900990
David Brownelld8f388d82008-07-25 01:46:07 -0700991 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900992 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
993 status);
994}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700995EXPORT_SYMBOL_GPL(gpiod_unexport);
David Brownelld8f388d82008-07-25 01:46:07 -0700996
997static int gpiochip_export(struct gpio_chip *chip)
998{
999 int status;
1000 struct device *dev;
1001
1002 /* Many systems register gpio chips for SOC support very early,
1003 * before driver model support is available. In those cases we
1004 * export this later, in gpiolib_sysfs_init() ... here we just
1005 * verify that _some_ field of gpio_class got initialized.
1006 */
1007 if (!gpio_class.p)
1008 return 0;
1009
1010 /* use chip->base for the ID; it's already known to be unique */
1011 mutex_lock(&sysfs_lock);
1012 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
1013 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001014 if (!IS_ERR(dev)) {
David Brownelld8f388d82008-07-25 01:46:07 -07001015 status = sysfs_create_group(&dev->kobj,
1016 &gpiochip_attr_group);
1017 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001018 status = PTR_ERR(dev);
David Brownelld8f388d82008-07-25 01:46:07 -07001019 chip->exported = (status == 0);
1020 mutex_unlock(&sysfs_lock);
1021
1022 if (status) {
1023 unsigned long flags;
1024 unsigned gpio;
1025
1026 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001027 gpio = 0;
1028 while (gpio < chip->ngpio)
1029 chip->desc[gpio++].chip = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -07001030 spin_unlock_irqrestore(&gpio_lock, flags);
1031
1032 pr_debug("%s: chip %s status %d\n", __func__,
1033 chip->label, status);
1034 }
1035
1036 return status;
1037}
1038
1039static void gpiochip_unexport(struct gpio_chip *chip)
1040{
1041 int status;
1042 struct device *dev;
1043
1044 mutex_lock(&sysfs_lock);
1045 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1046 if (dev) {
1047 put_device(dev);
1048 device_unregister(dev);
1049 chip->exported = 0;
1050 status = 0;
1051 } else
1052 status = -ENODEV;
1053 mutex_unlock(&sysfs_lock);
1054
1055 if (status)
1056 pr_debug("%s: chip %s status %d\n", __func__,
1057 chip->label, status);
1058}
1059
1060static int __init gpiolib_sysfs_init(void)
1061{
1062 int status;
1063 unsigned long flags;
Alexandre Courbot65493e32013-02-03 01:29:25 +09001064 struct gpio_chip *chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001065
1066 status = class_register(&gpio_class);
1067 if (status < 0)
1068 return status;
1069
1070 /* Scan and register the gpio_chips which registered very
1071 * early (e.g. before the class_register above was called).
1072 *
1073 * We run before arch_initcall() so chip->dev nodes can have
1074 * registered, and so arch_initcall() can always gpio_export().
1075 */
1076 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot65493e32013-02-03 01:29:25 +09001077 list_for_each_entry(chip, &gpio_chips, list) {
David Brownelld8f388d82008-07-25 01:46:07 -07001078 if (!chip || chip->exported)
1079 continue;
1080
1081 spin_unlock_irqrestore(&gpio_lock, flags);
1082 status = gpiochip_export(chip);
1083 spin_lock_irqsave(&gpio_lock, flags);
1084 }
1085 spin_unlock_irqrestore(&gpio_lock, flags);
1086
1087
1088 return status;
1089}
1090postcore_initcall(gpiolib_sysfs_init);
1091
1092#else
1093static inline int gpiochip_export(struct gpio_chip *chip)
1094{
1095 return 0;
1096}
1097
1098static inline void gpiochip_unexport(struct gpio_chip *chip)
1099{
1100}
1101
1102#endif /* CONFIG_GPIO_SYSFS */
1103
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001104/*
1105 * Add a new chip to the global chips list, keeping the list of chips sorted
1106 * by base order.
1107 *
1108 * Return -EBUSY if the new chip overlaps with some other chip's integer
1109 * space.
1110 */
1111static int gpiochip_add_to_list(struct gpio_chip *chip)
1112{
1113 struct list_head *pos = &gpio_chips;
1114 struct gpio_chip *_chip;
1115 int err = 0;
1116
1117 /* find where to insert our chip */
1118 list_for_each(pos, &gpio_chips) {
1119 _chip = list_entry(pos, struct gpio_chip, list);
1120 /* shall we insert before _chip? */
1121 if (_chip->base >= chip->base + chip->ngpio)
1122 break;
1123 }
1124
1125 /* are we stepping on the chip right before? */
1126 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1127 _chip = list_entry(pos->prev, struct gpio_chip, list);
1128 if (_chip->base + _chip->ngpio > chip->base) {
1129 dev_err(chip->dev,
1130 "GPIO integer space overlap, cannot add chip\n");
1131 err = -EBUSY;
1132 }
1133 }
1134
1135 if (!err)
1136 list_add_tail(&chip->list, pos);
1137
1138 return err;
1139}
1140
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001141/**
David Brownelld2876d02008-02-04 22:28:20 -08001142 * gpiochip_add() - register a gpio_chip
1143 * @chip: the chip to register, with chip->base initialized
1144 * Context: potentially before irqs or kmalloc will work
1145 *
1146 * Returns a negative errno if the chip can't be registered, such as
1147 * because the chip->base is invalid or already associated with a
1148 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001149 *
David Brownelld8f388d82008-07-25 01:46:07 -07001150 * When gpiochip_add() is called very early during boot, so that GPIOs
1151 * can be freely used, the chip->dev device must be registered before
1152 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1153 * for GPIOs will fail rudely.
1154 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001155 * If chip->base is negative, this requests dynamic assignment of
1156 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001157 */
1158int gpiochip_add(struct gpio_chip *chip)
1159{
1160 unsigned long flags;
1161 int status = 0;
1162 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001163 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001164
Trent Piephobff5fda2008-05-23 13:04:44 -07001165 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001166 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001167 status = -EINVAL;
1168 goto fail;
1169 }
1170
1171 spin_lock_irqsave(&gpio_lock, flags);
1172
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001173 if (base < 0) {
1174 base = gpiochip_find_base(chip->ngpio);
1175 if (base < 0) {
1176 status = base;
David Brownelld8f388d82008-07-25 01:46:07 -07001177 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001178 }
1179 chip->base = base;
1180 }
1181
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001182 status = gpiochip_add_to_list(chip);
1183
David Brownelld2876d02008-02-04 22:28:20 -08001184 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001185 chip->desc = &gpio_desc[chip->base];
1186
1187 for (id = 0; id < chip->ngpio; id++) {
1188 struct gpio_desc *desc = &chip->desc[id];
1189 desc->chip = chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001190
1191 /* REVISIT: most hardware initializes GPIOs as
1192 * inputs (often with pullups enabled) so power
1193 * usage is minimized. Linux code should set the
1194 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +03001195 * and in case chip->get_direction is not set,
David Brownelld8f388d82008-07-25 01:46:07 -07001196 * we may expose the wrong direction in sysfs.
1197 */
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001198 desc->flags = !chip->direction_input
David Brownelld8f388d82008-07-25 01:46:07 -07001199 ? (1 << FLAG_IS_OUT)
1200 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001201 }
1202 }
1203
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001204 spin_unlock_irqrestore(&gpio_lock, flags);
1205
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301206#ifdef CONFIG_PINCTRL
1207 INIT_LIST_HEAD(&chip->pin_ranges);
1208#endif
1209
Anton Vorontsov391c9702010-06-08 07:48:17 -06001210 of_gpiochip_add(chip);
1211
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001212 if (status)
1213 goto fail;
1214
1215 status = gpiochip_export(chip);
1216 if (status)
1217 goto fail;
1218
H Hartley Sweetenee1c1e72012-05-11 16:58:33 -07001219 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
Grant Likely64842aa2011-11-06 11:36:18 -07001220 chip->base, chip->base + chip->ngpio - 1,
1221 chip->label ? : "generic");
1222
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001223 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001224
1225unlock:
1226 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld2876d02008-02-04 22:28:20 -08001227fail:
1228 /* failures here can mean systems won't boot... */
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001229 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1230 chip->base, chip->base + chip->ngpio - 1,
1231 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001232 return status;
1233}
1234EXPORT_SYMBOL_GPL(gpiochip_add);
1235
1236/**
1237 * gpiochip_remove() - unregister a gpio_chip
1238 * @chip: the chip to unregister
1239 *
1240 * A gpio_chip with any GPIOs still requested may not be removed.
1241 */
1242int gpiochip_remove(struct gpio_chip *chip)
1243{
1244 unsigned long flags;
1245 int status = 0;
1246 unsigned id;
1247
1248 spin_lock_irqsave(&gpio_lock, flags);
1249
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001250 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001251 of_gpiochip_remove(chip);
1252
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001253 for (id = 0; id < chip->ngpio; id++) {
1254 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
David Brownelld2876d02008-02-04 22:28:20 -08001255 status = -EBUSY;
1256 break;
1257 }
1258 }
1259 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001260 for (id = 0; id < chip->ngpio; id++)
1261 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001262
1263 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -08001264 }
1265
1266 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d82008-07-25 01:46:07 -07001267
1268 if (status == 0)
1269 gpiochip_unexport(chip);
1270
David Brownelld2876d02008-02-04 22:28:20 -08001271 return status;
1272}
1273EXPORT_SYMBOL_GPL(gpiochip_remove);
1274
Grant Likely594fa262010-06-08 07:48:16 -06001275/**
1276 * gpiochip_find() - iterator for locating a specific gpio_chip
1277 * @data: data to pass to match function
1278 * @callback: Callback function to check gpio_chip
1279 *
1280 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1281 * determined by a user supplied @match callback. The callback should return
1282 * 0 if the device doesn't match and non-zero if it does. If the callback is
1283 * non-zero, this function will return to the caller and not iterate over any
1284 * more gpio_chips.
1285 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001286struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001287 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001288 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001289{
Alexandre Courbot125eef92013-02-03 01:29:26 +09001290 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001291 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001292
1293 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +09001294 list_for_each_entry(chip, &gpio_chips, list)
1295 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001296 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001297
1298 /* No match? */
1299 if (&chip->list == &gpio_chips)
1300 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001301 spin_unlock_irqrestore(&gpio_lock, flags);
1302
1303 return chip;
1304}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001305EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001306
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301307#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001308
Linus Walleij3f0f8672012-11-20 12:40:15 +01001309/**
1310 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1311 * @chip: the gpiochip to add the range for
1312 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001313 * @gpio_offset: the start offset in the current gpio_chip number space
1314 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001315 * @npins: the number of pins from the offset of each pin space (GPIO and
1316 * pin controller) to accumulate in this range
1317 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001318int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001319 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001320 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301321{
1322 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001323 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301324
Linus Walleij3f0f8672012-11-20 12:40:15 +01001325 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301326 if (!pin_range) {
1327 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1328 chip->label);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001329 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301330 }
1331
Linus Walleij3f0f8672012-11-20 12:40:15 +01001332 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001333 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001334 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301335 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001336 pin_range->range.base = chip->base + gpio_offset;
1337 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301338 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001339 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301340 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001341 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001342 ret = PTR_ERR(pin_range->pctldev);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001343 pr_err("%s: GPIO chip: could not create pin range\n",
1344 chip->label);
1345 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001346 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001347 }
Linus Walleij316511c2012-11-21 08:48:09 +01001348 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1349 chip->label, gpio_offset, gpio_offset + npins - 1,
1350 pinctl_name,
1351 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301352
1353 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001354
1355 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301356}
Linus Walleij165adc92012-11-06 14:49:39 +01001357EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301358
Linus Walleij3f0f8672012-11-20 12:40:15 +01001359/**
1360 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1361 * @chip: the chip to remove all the mappings for
1362 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301363void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1364{
1365 struct gpio_pin_range *pin_range, *tmp;
1366
1367 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1368 list_del(&pin_range->node);
1369 pinctrl_remove_gpio_range(pin_range->pctldev,
1370 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001371 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301372 }
1373}
Linus Walleij165adc92012-11-06 14:49:39 +01001374EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1375
1376#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301377
David Brownelld2876d02008-02-04 22:28:20 -08001378/* These "optional" allocation calls help prevent drivers from stomping
1379 * on each other, and help provide better diagnostics in debugfs.
1380 * They're called even less than the "set direction" calls.
1381 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001382static int gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001383{
David Brownell35e8bb52008-10-15 22:03:16 -07001384 struct gpio_chip *chip;
Mark Browne9354572012-07-09 12:22:56 +01001385 int status = -EPROBE_DEFER;
David Brownelld2876d02008-02-04 22:28:20 -08001386 unsigned long flags;
1387
Alexandre Courbot0204df42013-10-04 10:59:58 -07001388 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001389 pr_warn("%s: invalid GPIO\n", __func__);
1390 return -EINVAL;
1391 }
1392
David Brownelld2876d02008-02-04 22:28:20 -08001393 spin_lock_irqsave(&gpio_lock, flags);
1394
David Brownell35e8bb52008-10-15 22:03:16 -07001395 chip = desc->chip;
Alexandre Courbot0204df42013-10-04 10:59:58 -07001396 if (chip == NULL)
1397 goto done;
David Brownelld2876d02008-02-04 22:28:20 -08001398
David Brownell35e8bb52008-10-15 22:03:16 -07001399 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001400 goto done;
1401
David Brownelld2876d02008-02-04 22:28:20 -08001402 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001403 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001404 */
1405
1406 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1407 desc_set_label(desc, label ? : "?");
1408 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001409 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001410 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001411 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001412 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001413 }
1414
1415 if (chip->request) {
1416 /* chip->request may sleep */
1417 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001418 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001419 spin_lock_irqsave(&gpio_lock, flags);
1420
1421 if (status < 0) {
1422 desc_set_label(desc, NULL);
1423 module_put(chip->owner);
1424 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001425 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001426 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001427 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001428 if (chip->get_direction) {
1429 /* chip->get_direction may sleep */
1430 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001431 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001432 spin_lock_irqsave(&gpio_lock, flags);
1433 }
David Brownelld2876d02008-02-04 22:28:20 -08001434done:
1435 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001436 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001437 desc_to_gpio(desc), label ? : "?", status);
David Brownelld2876d02008-02-04 22:28:20 -08001438 spin_unlock_irqrestore(&gpio_lock, flags);
1439 return status;
1440}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001441
1442int gpio_request(unsigned gpio, const char *label)
1443{
1444 return gpiod_request(gpio_to_desc(gpio), label);
1445}
David Brownelld2876d02008-02-04 22:28:20 -08001446EXPORT_SYMBOL_GPL(gpio_request);
1447
Alexandre Courbot372e7222013-02-03 01:29:29 +09001448static void gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001449{
1450 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001451 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001452
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001453 might_sleep();
1454
Alexandre Courbot372e7222013-02-03 01:29:29 +09001455 if (!desc) {
David Brownelld2876d02008-02-04 22:28:20 -08001456 WARN_ON(extra_checks);
1457 return;
1458 }
1459
Alexandre Courbot372e7222013-02-03 01:29:29 +09001460 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07001461
David Brownelld2876d02008-02-04 22:28:20 -08001462 spin_lock_irqsave(&gpio_lock, flags);
1463
David Brownell35e8bb52008-10-15 22:03:16 -07001464 chip = desc->chip;
1465 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1466 if (chip->free) {
1467 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001468 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001469 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001470 spin_lock_irqsave(&gpio_lock, flags);
1471 }
David Brownelld2876d02008-02-04 22:28:20 -08001472 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001473 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001474 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001475 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301476 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301477 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001478 } else
David Brownelld2876d02008-02-04 22:28:20 -08001479 WARN_ON(extra_checks);
1480
1481 spin_unlock_irqrestore(&gpio_lock, flags);
1482}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001483
1484void gpio_free(unsigned gpio)
1485{
1486 gpiod_free(gpio_to_desc(gpio));
1487}
David Brownelld2876d02008-02-04 22:28:20 -08001488EXPORT_SYMBOL_GPL(gpio_free);
1489
Eric Miao3e45f1d2010-03-05 13:44:35 -08001490/**
1491 * gpio_request_one - request a single GPIO with initial configuration
1492 * @gpio: the GPIO number
1493 * @flags: GPIO configuration as specified by GPIOF_*
1494 * @label: a literal description string of this GPIO
1495 */
1496int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1497{
Alexandre Courbot372e7222013-02-03 01:29:29 +09001498 struct gpio_desc *desc;
Eric Miao3e45f1d2010-03-05 13:44:35 -08001499 int err;
1500
Alexandre Courbot372e7222013-02-03 01:29:29 +09001501 desc = gpio_to_desc(gpio);
1502
1503 err = gpiod_request(desc, label);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001504 if (err)
1505 return err;
1506
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301507 if (flags & GPIOF_OPEN_DRAIN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001508 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301509
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301510 if (flags & GPIOF_OPEN_SOURCE)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001511 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301512
Eric Miao3e45f1d2010-03-05 13:44:35 -08001513 if (flags & GPIOF_DIR_IN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001514 err = gpiod_direction_input(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001515 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001516 err = gpiod_direction_output(desc,
Eric Miao3e45f1d2010-03-05 13:44:35 -08001517 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1518
Aaro Koskinene2548112010-12-21 17:24:22 -08001519 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001520 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -08001521
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001522 if (flags & GPIOF_EXPORT) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001523 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001524 if (err)
1525 goto free_gpio;
1526 }
1527
1528 return 0;
1529
1530 free_gpio:
Alexandre Courbot372e7222013-02-03 01:29:29 +09001531 gpiod_free(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001532 return err;
1533}
1534EXPORT_SYMBOL_GPL(gpio_request_one);
1535
1536/**
1537 * gpio_request_array - request multiple GPIOs in a single call
1538 * @array: array of the 'struct gpio'
1539 * @num: how many GPIOs in the array
1540 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001541int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001542{
1543 int i, err;
1544
1545 for (i = 0; i < num; i++, array++) {
1546 err = gpio_request_one(array->gpio, array->flags, array->label);
1547 if (err)
1548 goto err_free;
1549 }
1550 return 0;
1551
1552err_free:
1553 while (i--)
1554 gpio_free((--array)->gpio);
1555 return err;
1556}
1557EXPORT_SYMBOL_GPL(gpio_request_array);
1558
1559/**
1560 * gpio_free_array - release multiple GPIOs in a single call
1561 * @array: array of the 'struct gpio'
1562 * @num: how many GPIOs in the array
1563 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001564void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001565{
1566 while (num--)
1567 gpio_free((array++)->gpio);
1568}
1569EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001570
1571/**
1572 * gpiochip_is_requested - return string iff signal was requested
1573 * @chip: controller managing the signal
1574 * @offset: of signal within controller's 0..(ngpio - 1) range
1575 *
1576 * Returns NULL if the GPIO is not currently requested, else a string.
1577 * If debugfs support is enabled, the string returned is the label passed
1578 * to gpio_request(); otherwise it is a meaningless constant.
1579 *
1580 * This function is for use by GPIO controller drivers. The label can
1581 * help with diagnostics, and knowing that the signal is used as a GPIO
1582 * can help avoid accidentally multiplexing it to another controller.
1583 */
1584const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1585{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001586 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001587
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001588 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -08001589 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001590
1591 desc = &chip->desc[offset];
1592
Alexandre Courbot372e7222013-02-03 01:29:29 +09001593 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001594 return NULL;
1595#ifdef CONFIG_DEBUG_FS
Alexandre Courbot372e7222013-02-03 01:29:29 +09001596 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001597#else
1598 return "?";
1599#endif
1600}
1601EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1602
1603
1604/* Drivers MUST set GPIO direction before making get/set calls. In
1605 * some cases this is done in early boot, before IRQs are enabled.
1606 *
1607 * As a rule these aren't called more than once (except for drivers
1608 * using the open-drain emulation idiom) so these are natural places
1609 * to accumulate extra debugging checks. Note that we can't (yet)
1610 * rely on gpio_request() having been called beforehand.
1611 */
1612
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001613/**
1614 * gpiod_direction_input - set the GPIO direction to input
1615 * @desc: GPIO to set to input
1616 *
1617 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1618 * be called safely on it.
1619 *
1620 * Return 0 in case of success, else an error code.
1621 */
1622int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001623{
1624 unsigned long flags;
1625 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001626 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001627 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001628
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001629 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001630 pr_warn("%s: invalid GPIO\n", __func__);
1631 return -EINVAL;
1632 }
1633
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001634 chip = desc->chip;
1635 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001636 gpiod_warn(desc,
1637 "%s: missing get() or direction_input() operations\n",
1638 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001639 return -EIO;
1640 }
1641
David Brownelld2876d02008-02-04 22:28:20 -08001642 spin_lock_irqsave(&gpio_lock, flags);
1643
Alexandre Courbot372e7222013-02-03 01:29:29 +09001644 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001645 if (status < 0)
1646 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001647
1648 /* now we know the gpio is valid and chip won't vanish */
1649
1650 spin_unlock_irqrestore(&gpio_lock, flags);
1651
David Brownell9c4ba942010-08-10 18:02:24 -07001652 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001653
Alexandre Courbot372e7222013-02-03 01:29:29 +09001654 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001655 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001656 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001657 if (status < 0) {
Mark Brown6424de52013-09-09 10:33:49 +01001658 gpiod_dbg(desc, "chip request fail, %d\n", status);
David Brownell35e8bb52008-10-15 22:03:16 -07001659 /* and it's not available to anyone else ...
1660 * gpio_request() is the fully clean solution.
1661 */
1662 goto lose;
1663 }
1664 }
1665
Alexandre Courbot372e7222013-02-03 01:29:29 +09001666 status = chip->direction_input(chip, offset);
David Brownelld2876d02008-02-04 22:28:20 -08001667 if (status == 0)
1668 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001669
Alexandre Courbot372e7222013-02-03 01:29:29 +09001670 trace_gpio_direction(desc_to_gpio(desc), 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001671lose:
David Brownelld2876d02008-02-04 22:28:20 -08001672 return status;
1673fail:
1674 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001675 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001676 gpiod_dbg(desc, "%s status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001677 return status;
1678}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001679EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001680
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001681/**
1682 * gpiod_direction_output - set the GPIO direction to input
1683 * @desc: GPIO to set to output
1684 * @value: initial output value of the GPIO
1685 *
1686 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1687 * be called safely on it. The initial value of the output must be specified.
1688 *
1689 * Return 0 in case of success, else an error code.
1690 */
1691int gpiod_direction_output(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001692{
1693 unsigned long flags;
1694 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001695 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001696 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001697
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001698 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001699 pr_warn("%s: invalid GPIO\n", __func__);
1700 return -EINVAL;
1701 }
1702
Linus Walleijd468bf92013-09-24 11:54:38 +02001703 /* GPIOs used for IRQs shall not be set as output */
1704 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1705 gpiod_err(desc,
1706 "%s: tried to set a GPIO tied to an IRQ as output\n",
1707 __func__);
1708 return -EIO;
1709 }
1710
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301711 /* Open drain pin should not be driven to 1 */
1712 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001713 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301714
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301715 /* Open source pin should not be driven to 0 */
1716 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001717 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301718
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001719 chip = desc->chip;
1720 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001721 gpiod_warn(desc,
1722 "%s: missing set() or direction_output() operations\n",
1723 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001724 return -EIO;
1725 }
1726
David Brownelld2876d02008-02-04 22:28:20 -08001727 spin_lock_irqsave(&gpio_lock, flags);
1728
Alexandre Courbot372e7222013-02-03 01:29:29 +09001729 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001730 if (status < 0)
1731 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001732
1733 /* now we know the gpio is valid and chip won't vanish */
1734
1735 spin_unlock_irqrestore(&gpio_lock, flags);
1736
David Brownell9c4ba942010-08-10 18:02:24 -07001737 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001738
Alexandre Courbot372e7222013-02-03 01:29:29 +09001739 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001740 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001741 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001742 if (status < 0) {
Mark Brown6424de52013-09-09 10:33:49 +01001743 gpiod_dbg(desc, "chip request fail, %d\n", status);
David Brownell35e8bb52008-10-15 22:03:16 -07001744 /* and it's not available to anyone else ...
1745 * gpio_request() is the fully clean solution.
1746 */
1747 goto lose;
1748 }
1749 }
1750
Alexandre Courbot372e7222013-02-03 01:29:29 +09001751 status = chip->direction_output(chip, offset, value);
David Brownelld2876d02008-02-04 22:28:20 -08001752 if (status == 0)
1753 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001754 trace_gpio_value(desc_to_gpio(desc), 0, value);
1755 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001756lose:
David Brownelld2876d02008-02-04 22:28:20 -08001757 return status;
1758fail:
1759 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001760 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001761 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001762 return status;
1763}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001764EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001765
Felipe Balbic4b5be92010-05-26 14:42:23 -07001766/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001767 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001768 * @gpio: the gpio to set debounce time
1769 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001770 *
1771 * returns -ENOTSUPP if the controller does not support setting
1772 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001773 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001774int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001775{
1776 unsigned long flags;
1777 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001778 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001779 int offset;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001780
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001781 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001782 pr_warn("%s: invalid GPIO\n", __func__);
1783 return -EINVAL;
1784 }
1785
Felipe Balbic4b5be92010-05-26 14:42:23 -07001786 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001787 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001788 gpiod_dbg(desc,
1789 "%s: missing set() or set_debounce() operations\n",
1790 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001791 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001792 }
1793
1794 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001795
1796 status = gpio_ensure_requested(desc);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001797 if (status < 0)
1798 goto fail;
1799
1800 /* now we know the gpio is valid and chip won't vanish */
1801
1802 spin_unlock_irqrestore(&gpio_lock, flags);
1803
David Brownell9c4ba942010-08-10 18:02:24 -07001804 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001805
Alexandre Courbot372e7222013-02-03 01:29:29 +09001806 offset = gpio_chip_hwgpio(desc);
1807 return chip->set_debounce(chip, offset, debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001808
1809fail:
1810 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001811 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001812 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001813
1814 return status;
1815}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001816EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001817
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001818/**
1819 * gpiod_is_active_low - test whether a GPIO is active-low or not
1820 * @desc: the gpio descriptor to test
1821 *
1822 * Returns 1 if the GPIO is active-low, 0 otherwise.
1823 */
1824int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001825{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001826 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001827}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001828EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001829
1830/* I/O calls are only valid after configuration completed; the relevant
1831 * "is this a valid GPIO" error checks should already have been done.
1832 *
1833 * "Get" operations are often inlinable as reading a pin value register,
1834 * and masking the relevant bit in that register.
1835 *
1836 * When "set" operations are inlinable, they involve writing that mask to
1837 * one register to set a low value, or a different register to set it high.
1838 * Otherwise locking is needed, so there may be little value to inlining.
1839 *
1840 *------------------------------------------------------------------------
1841 *
1842 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1843 * have requested the GPIO. That can include implicit requesting by
1844 * a direction setting call. Marking a gpio as requested locks its chip
1845 * in memory, guaranteeing that these table lookups need no more locking
1846 * and that gpiochip_remove() will fail.
1847 *
1848 * REVISIT when debugging, consider adding some instrumentation to ensure
1849 * that the GPIO was actually requested.
1850 */
1851
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001852static int _gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001853{
1854 struct gpio_chip *chip;
1855 int value;
1856 int offset;
1857
1858 chip = desc->chip;
1859 offset = gpio_chip_hwgpio(desc);
1860 value = chip->get ? chip->get(chip, offset) : 0;
1861 trace_gpio_value(desc_to_gpio(desc), 1, value);
1862 return value;
1863}
1864
David Brownelld2876d02008-02-04 22:28:20 -08001865/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001866 * gpiod_get_raw_value() - return a gpio's raw value
1867 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001868 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001869 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1870 * its ACTIVE_LOW status.
1871 *
1872 * This function should be called from contexts where we cannot sleep, and will
1873 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001874 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001875int gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001876{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001877 if (!desc)
1878 return 0;
Mark Browne4e449e2012-02-17 10:46:00 -08001879 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001880 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001881 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001882}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001883EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001884
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001885/**
1886 * gpiod_get_value() - return a gpio's value
1887 * @desc: gpio whose value will be returned
1888 *
1889 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1890 * account.
1891 *
1892 * This function should be called from contexts where we cannot sleep, and will
1893 * complain if the GPIO chip functions potentially sleep.
1894 */
1895int gpiod_get_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001896{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001897 int value;
1898 if (!desc)
1899 return 0;
1900 /* Should be using gpio_get_value_cansleep() */
1901 WARN_ON(desc->chip->can_sleep);
1902
1903 value = _gpiod_get_raw_value(desc);
1904 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1905 value = !value;
1906
1907 return value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001908}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001909EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001910
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301911/*
1912 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001913 * @desc: gpio descriptor whose state need to be set.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301914 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1915 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001916static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301917{
1918 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001919 struct gpio_chip *chip = desc->chip;
1920 int offset = gpio_chip_hwgpio(desc);
1921
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301922 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001923 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301924 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001925 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301926 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001927 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301928 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001929 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301930 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001931 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301932 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001933 gpiod_err(desc,
1934 "%s: Error in set_value for open drain err %d\n",
1935 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301936}
1937
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301938/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001939 * _gpio_set_open_source_value() - Set the open source gpio's value.
1940 * @desc: gpio descriptor whose state need to be set.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301941 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1942 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001943static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301944{
1945 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001946 struct gpio_chip *chip = desc->chip;
1947 int offset = gpio_chip_hwgpio(desc);
1948
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301949 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001950 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301951 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001952 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301953 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001954 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301955 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001956 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301957 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001958 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301959 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001960 gpiod_err(desc,
1961 "%s: Error in set_value for open source err %d\n",
1962 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301963}
1964
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001965static void _gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001966{
1967 struct gpio_chip *chip;
1968
1969 chip = desc->chip;
1970 trace_gpio_value(desc_to_gpio(desc), 0, value);
1971 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1972 _gpio_set_open_drain_value(desc, value);
1973 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1974 _gpio_set_open_source_value(desc, value);
1975 else
1976 chip->set(chip, gpio_chip_hwgpio(desc), value);
1977}
1978
David Brownelld2876d02008-02-04 22:28:20 -08001979/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001980 * gpiod_set_raw_value() - assign a gpio's raw value
1981 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001982 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001983 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001984 * Set the raw value of the GPIO, i.e. the value of its physical line without
1985 * regard for its ACTIVE_LOW status.
1986 *
1987 * This function should be called from contexts where we cannot sleep, and will
1988 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001989 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001990void gpiod_set_raw_value(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001991{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001992 if (!desc)
1993 return;
Mark Browne4e449e2012-02-17 10:46:00 -08001994 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001995 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001996 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001997}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001998EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001999
2000/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002001 * gpiod_set_value() - assign a gpio's value
2002 * @desc: gpio whose value will be assigned
2003 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002004 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002005 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2006 * account
2007 *
2008 * This function should be called from contexts where we cannot sleep, and will
2009 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002010 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002011void gpiod_set_value(struct gpio_desc *desc, int value)
2012{
2013 if (!desc)
2014 return;
2015 /* Should be using gpio_set_value_cansleep() */
2016 WARN_ON(desc->chip->can_sleep);
2017 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2018 value = !value;
2019 _gpiod_set_raw_value(desc, value);
2020}
2021EXPORT_SYMBOL_GPL(gpiod_set_value);
2022
2023/**
2024 * gpiod_cansleep() - report whether gpio value access may sleep
2025 * @desc: gpio to check
2026 *
2027 */
2028int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002029{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002030 if (!desc)
2031 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002032 return desc->chip->can_sleep;
2033}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002034EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002035
David Brownell0f6d5042008-10-15 22:03:14 -07002036/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002037 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2038 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002039 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002040 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2041 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002042 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002043int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002044{
2045 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002046 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002047
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002048 if (!desc)
2049 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002050 chip = desc->chip;
2051 offset = gpio_chip_hwgpio(desc);
2052 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
2053}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002054EXPORT_SYMBOL_GPL(gpiod_to_irq);
David Brownell0f6d5042008-10-15 22:03:14 -07002055
Linus Walleijd468bf92013-09-24 11:54:38 +02002056/**
2057 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
2058 * @gpio: the GPIO line to lock as used for IRQ
2059 *
2060 * This is used directly by GPIO drivers that want to lock down
2061 * a certain GPIO line to be used as IRQs, for example in the
2062 * .to_irq() callback of their gpio_chip, or in the .irq_enable()
2063 * of its irq_chip implementation if the GPIO is known from that
2064 * code.
2065 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002066int gpiod_lock_as_irq(struct gpio_desc *desc)
Linus Walleijd468bf92013-09-24 11:54:38 +02002067{
2068 if (!desc)
2069 return -EINVAL;
2070
2071 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
2072 gpiod_err(desc,
2073 "%s: tried to flag a GPIO set as output for IRQ\n",
2074 __func__);
2075 return -EIO;
2076 }
2077
2078 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
2079 return 0;
2080}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002081EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002082
2083int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
2084{
2085 return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset));
2086}
2087EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
2088
2089/**
2090 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
2091 * @gpio: the GPIO line to unlock from IRQ usage
2092 *
2093 * This is used directly by GPIO drivers that want to indicate
2094 * that a certain GPIO is no longer used exclusively for IRQ.
2095 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002096void gpiod_unlock_as_irq(struct gpio_desc *desc)
Linus Walleijd468bf92013-09-24 11:54:38 +02002097{
2098 if (!desc)
2099 return;
2100
2101 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2102}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002103EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002104
2105void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2106{
2107 return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset));
2108}
2109EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
David Brownelld2876d02008-02-04 22:28:20 -08002110
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002111/**
2112 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2113 * @desc: gpio whose value will be returned
2114 *
2115 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2116 * its ACTIVE_LOW status.
2117 *
2118 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002119 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002120int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002121{
David Brownelld2876d02008-02-04 22:28:20 -08002122 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002123 if (!desc)
2124 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002125 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002126}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002127EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002128
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002129/**
2130 * gpiod_get_value_cansleep() - return a gpio's value
2131 * @desc: gpio whose value will be returned
2132 *
2133 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2134 * account.
2135 *
2136 * This function is to be called from contexts that can sleep.
2137 */
2138int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002139{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002140 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002141
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002142 might_sleep_if(extra_checks);
2143 if (!desc)
2144 return 0;
2145
2146 value = _gpiod_get_raw_value(desc);
2147 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2148 value = !value;
2149
2150 return value;
2151}
2152EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
2153
2154/**
2155 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2156 * @desc: gpio whose value will be assigned
2157 * @value: value to assign
2158 *
2159 * Set the raw value of the GPIO, i.e. the value of its physical line without
2160 * regard for its ACTIVE_LOW status.
2161 *
2162 * This function is to be called from contexts that can sleep.
2163 */
2164void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002165{
David Brownelld2876d02008-02-04 22:28:20 -08002166 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002167 if (!desc)
2168 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002169 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002170}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002171EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002172
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002173/**
2174 * gpiod_set_value_cansleep() - assign a gpio's value
2175 * @desc: gpio whose value will be assigned
2176 * @value: value to assign
2177 *
2178 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2179 * account
2180 *
2181 * This function is to be called from contexts that can sleep.
2182 */
2183void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002184{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002185 might_sleep_if(extra_checks);
2186 if (!desc)
2187 return;
2188
2189 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2190 value = !value;
2191 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002192}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002193EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002194
David Brownelld2876d02008-02-04 22:28:20 -08002195#ifdef CONFIG_DEBUG_FS
2196
David Brownelld2876d02008-02-04 22:28:20 -08002197static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2198{
2199 unsigned i;
2200 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002201 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002202 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002203 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002204
2205 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2206 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2207 continue;
2208
Alexandre Courbot372e7222013-02-03 01:29:29 +09002209 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002210 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002211 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2212 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002213 gpio, gdesc->label,
2214 is_out ? "out" : "in ",
2215 chip->get
2216 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002217 : "? ",
2218 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002219 seq_printf(s, "\n");
2220 }
2221}
2222
Thierry Redingf9c4a312012-04-12 13:26:01 +02002223static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002224{
Grant Likely362432a2013-02-09 09:41:49 +00002225 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002226 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002227 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002228
2229 s->private = "";
2230
Grant Likely362432a2013-02-09 09:41:49 +00002231 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002232 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002233 if (index-- == 0) {
2234 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002235 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002236 }
2237 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002238
2239 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002240}
2241
2242static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2243{
Grant Likely362432a2013-02-09 09:41:49 +00002244 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002245 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002246 void *ret = NULL;
2247
Grant Likely362432a2013-02-09 09:41:49 +00002248 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002249 if (list_is_last(&chip->list, &gpio_chips))
2250 ret = NULL;
2251 else
2252 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002253 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002254
2255 s->private = "\n";
2256 ++*pos;
2257
2258 return ret;
2259}
2260
2261static void gpiolib_seq_stop(struct seq_file *s, void *v)
2262{
2263}
2264
2265static int gpiolib_seq_show(struct seq_file *s, void *v)
2266{
2267 struct gpio_chip *chip = v;
2268 struct device *dev;
2269
2270 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2271 chip->base, chip->base + chip->ngpio - 1);
2272 dev = chip->dev;
2273 if (dev)
2274 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2275 dev_name(dev));
2276 if (chip->label)
2277 seq_printf(s, ", %s", chip->label);
2278 if (chip->can_sleep)
2279 seq_printf(s, ", can sleep");
2280 seq_printf(s, ":\n");
2281
2282 if (chip->dbg_show)
2283 chip->dbg_show(s, chip);
2284 else
2285 gpiolib_dbg_show(s, chip);
2286
David Brownelld2876d02008-02-04 22:28:20 -08002287 return 0;
2288}
2289
Thierry Redingf9c4a312012-04-12 13:26:01 +02002290static const struct seq_operations gpiolib_seq_ops = {
2291 .start = gpiolib_seq_start,
2292 .next = gpiolib_seq_next,
2293 .stop = gpiolib_seq_stop,
2294 .show = gpiolib_seq_show,
2295};
2296
David Brownelld2876d02008-02-04 22:28:20 -08002297static int gpiolib_open(struct inode *inode, struct file *file)
2298{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002299 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002300}
2301
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002302static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002303 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002304 .open = gpiolib_open,
2305 .read = seq_read,
2306 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002307 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002308};
2309
2310static int __init gpiolib_debugfs_init(void)
2311{
2312 /* /sys/kernel/debug/gpio */
2313 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2314 NULL, NULL, &gpiolib_operations);
2315 return 0;
2316}
2317subsys_initcall(gpiolib_debugfs_init);
2318
2319#endif /* DEBUG_FS */