blob: 7dd4461502940952ebf6323dbb04e1af7e6a5230 [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>
Mika Westerberg81f59e92013-10-10 11:01:09 +030013#include <linux/acpi_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070014#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
David Brownelld2876d02008-02-04 22:28:20 -080016
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060017#define CREATE_TRACE_POINTS
18#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080019
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070020/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080021 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070022 * The GPIO programming interface allows for inlining speed-critical
23 * get/set operations for common cases, so that access to SOC-integrated
24 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080025 */
26
27
28/* When debugging, extend minimal trust to callers and platform code.
29 * Also emit diagnostic messages that may help initial bringup, when
30 * board setup or driver bugs are most common.
31 *
32 * Otherwise, minimize overhead in what may be bitbanging codepaths.
33 */
34#ifdef DEBUG
35#define extra_checks 1
36#else
37#define extra_checks 0
38#endif
39
40/* gpio_lock prevents conflicts during gpio_desc[] table updates.
41 * While any GPIO is requested, its gpio_chip is not removable;
42 * each GPIO's "requested" flag serves as a lock and refcount.
43 */
44static DEFINE_SPINLOCK(gpio_lock);
45
46struct gpio_desc {
47 struct gpio_chip *chip;
48 unsigned long flags;
49/* flag symbols are bit numbers */
50#define FLAG_REQUESTED 0
51#define FLAG_IS_OUT 1
Alexandre Courbot710b40e2013-02-02 23:44:06 +090052#define FLAG_EXPORT 2 /* protected by sysfs_lock */
53#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
54#define FLAG_TRIG_FALL 4 /* trigger on falling edge */
55#define FLAG_TRIG_RISE 5 /* trigger on rising edge */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070056#define FLAG_ACTIVE_LOW 6 /* value has active low */
Alexandre Courbot710b40e2013-02-02 23:44:06 +090057#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
58#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
Linus Walleijd468bf92013-09-24 11:54:38 +020059#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070060
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070061#define ID_SHIFT 16 /* add new flags before this one */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070062
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070063#define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
Daniel Glöcknerff77c352009-09-22 16:46:38 -070064#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
David Brownelld2876d02008-02-04 22:28:20 -080065
66#ifdef CONFIG_DEBUG_FS
67 const char *label;
68#endif
69};
70static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
71
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +090072#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
73
Alexandre Courbotbae48da2013-10-17 10:21:38 -070074static DEFINE_MUTEX(gpio_lookup_lock);
75static LIST_HEAD(gpio_lookup_list);
Alexandre Courbot1a989d02013-02-03 01:29:24 +090076static LIST_HEAD(gpio_chips);
77
Daniel Glöcknerff77c352009-09-22 16:46:38 -070078#ifdef CONFIG_GPIO_SYSFS
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070079static DEFINE_IDR(dirent_idr);
Daniel Glöcknerff77c352009-09-22 16:46:38 -070080#endif
81
Alexandre Courbot372e7222013-02-03 01:29:29 +090082static int gpiod_request(struct gpio_desc *desc, const char *label);
83static void gpiod_free(struct gpio_desc *desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090084
Mark Brown7b17b592013-09-09 10:33:50 +010085#ifdef CONFIG_DEBUG_FS
86#define gpiod_emerg(desc, fmt, ...) \
87 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
88 ##__VA_ARGS__)
89#define gpiod_crit(desc, fmt, ...) \
90 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
91 ##__VA_ARGS__)
92#define gpiod_err(desc, fmt, ...) \
93 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
94 ##__VA_ARGS__)
95#define gpiod_warn(desc, fmt, ...) \
96 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
97 ##__VA_ARGS__)
98#define gpiod_info(desc, fmt, ...) \
99 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
100 ##__VA_ARGS__)
101#define gpiod_dbg(desc, fmt, ...) \
102 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
103 ##__VA_ARGS__)
104#else
Mark Brown6424de52013-09-09 10:33:49 +0100105#define gpiod_emerg(desc, fmt, ...) \
106 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
107#define gpiod_crit(desc, fmt, ...) \
108 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
109#define gpiod_err(desc, fmt, ...) \
110 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
111#define gpiod_warn(desc, fmt, ...) \
112 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
113#define gpiod_info(desc, fmt, ...) \
114 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
115#define gpiod_dbg(desc, fmt, ...) \
116 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Mark Brown7b17b592013-09-09 10:33:50 +0100117#endif
Alexandre Courbot372e7222013-02-03 01:29:29 +0900118
David Brownelld2876d02008-02-04 22:28:20 -0800119static inline void desc_set_label(struct gpio_desc *d, const char *label)
120{
121#ifdef CONFIG_DEBUG_FS
122 d->label = label;
123#endif
124}
125
Alexandre Courbot372e7222013-02-03 01:29:29 +0900126/*
127 * Return the GPIO number of the passed descriptor relative to its chip
128 */
129static int gpio_chip_hwgpio(const struct gpio_desc *desc)
130{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900131 return desc - &desc->chip->desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900132}
133
134/**
135 * Convert a GPIO number to its descriptor
136 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700137struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900138{
139 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
140 return NULL;
141 else
142 return &gpio_desc[gpio];
143}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700144EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900145
146/**
Linus Walleijd468bf92013-09-24 11:54:38 +0200147 * Convert an offset on a certain chip to a corresponding descriptor
148 */
149static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip,
150 unsigned int offset)
151{
152 unsigned int gpio = chip->base + offset;
153
154 return gpio_to_desc(gpio);
155}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900156
157/**
158 * Convert a GPIO descriptor to the integer namespace.
159 * This should disappear in the future but is needed since we still
160 * use GPIO numbers for error messages and sysfs nodes
161 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700162int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900163{
Alexandre Courbot8c0fca82013-10-04 10:59:57 -0700164 return desc - &gpio_desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900165}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700166EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900167
168
David Brownelld2876d02008-02-04 22:28:20 -0800169/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
170 * when setting direction, and otherwise illegal. Until board setup code
171 * and drivers use explicit requests everywhere (which won't happen when
172 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -0700173 * message should motivate switching to explicit requests... so should
174 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -0700175 *
176 * NOTE: the autorequest mechanism is going away; at this point it's
177 * only "legal" in the sense that (old) code using it won't break yet,
178 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -0800179 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900180static int gpio_ensure_requested(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800181{
David Brownell8a0cecf2009-04-02 16:57:06 -0700182 const struct gpio_chip *chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900183 const int gpio = desc_to_gpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -0700184
David Brownell8a0cecf2009-04-02 16:57:06 -0700185 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
186 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700187 if (!try_module_get(chip->owner)) {
188 pr_err("GPIO-%d: module can't be gotten \n", gpio);
189 clear_bit(FLAG_REQUESTED, &desc->flags);
190 /* lose */
191 return -EIO;
192 }
David Brownelld2876d02008-02-04 22:28:20 -0800193 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700194 /* caller must chip->request() w/o spinlock */
195 if (chip->request)
196 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800197 }
David Brownell35e8bb52008-10-15 22:03:16 -0700198 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800199}
200
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700201/**
202 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
203 * @desc: descriptor to return the chip of
204 */
205struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900206{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900207 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900208}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700209EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800210
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700211/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
212static int gpiochip_find_base(int ngpio)
213{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900214 struct gpio_chip *chip;
215 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700216
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900217 list_for_each_entry_reverse(chip, &gpio_chips, list) {
218 /* found a free space? */
219 if (chip->base + chip->ngpio <= base)
220 break;
221 else
222 /* nope, check the space right before the chip */
223 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700224 }
225
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900226 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700227 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900228 return base;
229 } else {
230 pr_err("%s: cannot find free range\n", __func__);
231 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700232 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700233}
234
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700235/**
236 * gpiod_get_direction - return the current direction of a GPIO
237 * @desc: GPIO to get the direction of
238 *
239 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
240 *
241 * This function may sleep if gpiod_cansleep() is true.
242 */
243int gpiod_get_direction(const struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300244{
245 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900246 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300247 int status = -EINVAL;
248
Alexandre Courbot372e7222013-02-03 01:29:29 +0900249 chip = gpiod_to_chip(desc);
250 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300251
252 if (!chip->get_direction)
253 return status;
254
Alexandre Courbot372e7222013-02-03 01:29:29 +0900255 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300256 if (status > 0) {
257 /* GPIOF_DIR_IN, or other positive */
258 status = 1;
Alexandre Courbotdef63432013-02-15 14:46:15 +0900259 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
260 * so it does not affect constness per se */
261 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300262 }
263 if (status == 0) {
264 /* GPIOF_DIR_OUT */
Alexandre Courbotdef63432013-02-15 14:46:15 +0900265 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300266 }
267 return status;
268}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700269EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300270
David Brownelld8f388d82008-07-25 01:46:07 -0700271#ifdef CONFIG_GPIO_SYSFS
272
273/* lock protects against unexport_gpio() being called while
274 * sysfs files are active.
275 */
276static DEFINE_MUTEX(sysfs_lock);
277
278/*
279 * /sys/class/gpio/gpioN... only for GPIOs that are exported
280 * /direction
281 * * MAY BE OMITTED if kernel won't allow direction changes
282 * * is read/write as "in" or "out"
283 * * may also be written as "high" or "low", initializing
284 * output value as specified ("out" implies "low")
285 * /value
286 * * always readable, subject to hardware behavior
287 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700288 * /edge
289 * * configures behavior of poll(2) on /value
290 * * available only if pin can generate IRQs on input
291 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800292 * /active_low
293 * * configures polarity of /value
294 * * is read/write as zero/nonzero
295 * * also affects existing and subsequent "falling" and "rising"
296 * /edge configuration
David Brownelld8f388d82008-07-25 01:46:07 -0700297 */
298
299static ssize_t gpio_direction_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
301{
Alexandre Courbotdef63432013-02-15 14:46:15 +0900302 const struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700303 ssize_t status;
304
305 mutex_lock(&sysfs_lock);
306
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900307 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700308 status = -EIO;
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900309 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +0900310 gpiod_get_direction(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700311 status = sprintf(buf, "%s\n",
312 test_bit(FLAG_IS_OUT, &desc->flags)
313 ? "out" : "in");
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900314 }
David Brownelld8f388d82008-07-25 01:46:07 -0700315
316 mutex_unlock(&sysfs_lock);
317 return status;
318}
319
320static ssize_t gpio_direction_store(struct device *dev,
321 struct device_attribute *attr, const char *buf, size_t size)
322{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900323 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700324 ssize_t status;
325
326 mutex_lock(&sysfs_lock);
327
328 if (!test_bit(FLAG_EXPORT, &desc->flags))
329 status = -EIO;
330 else if (sysfs_streq(buf, "high"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900331 status = gpiod_direction_output(desc, 1);
David Brownelld8f388d82008-07-25 01:46:07 -0700332 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900333 status = gpiod_direction_output(desc, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700334 else if (sysfs_streq(buf, "in"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900335 status = gpiod_direction_input(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700336 else
337 status = -EINVAL;
338
339 mutex_unlock(&sysfs_lock);
340 return status ? : size;
341}
342
Jani Nikula07697462009-12-15 16:46:20 -0800343static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700344 gpio_direction_show, gpio_direction_store);
345
346static ssize_t gpio_value_show(struct device *dev,
347 struct device_attribute *attr, char *buf)
348{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900349 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700350 ssize_t status;
351
352 mutex_lock(&sysfs_lock);
353
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700354 if (!test_bit(FLAG_EXPORT, &desc->flags))
David Brownelld8f388d82008-07-25 01:46:07 -0700355 status = -EIO;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700356 else
357 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
David Brownelld8f388d82008-07-25 01:46:07 -0700358
359 mutex_unlock(&sysfs_lock);
360 return status;
361}
362
363static ssize_t gpio_value_store(struct device *dev,
364 struct device_attribute *attr, const char *buf, size_t size)
365{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900366 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700367 ssize_t status;
368
369 mutex_lock(&sysfs_lock);
370
371 if (!test_bit(FLAG_EXPORT, &desc->flags))
372 status = -EIO;
373 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
374 status = -EPERM;
375 else {
376 long value;
377
Jingoo Hana3d88c92013-07-19 16:12:50 +0900378 status = kstrtol(buf, 0, &value);
David Brownelld8f388d82008-07-25 01:46:07 -0700379 if (status == 0) {
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700380 gpiod_set_value_cansleep(desc, value);
David Brownelld8f388d82008-07-25 01:46:07 -0700381 status = size;
382 }
383 }
384
385 mutex_unlock(&sysfs_lock);
386 return status;
387}
388
Jani Nikula07697462009-12-15 16:46:20 -0800389static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700390 gpio_value_show, gpio_value_store);
391
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700392static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
393{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700394 struct sysfs_dirent *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700395
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700396 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700397 return IRQ_HANDLED;
398}
399
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700400static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
401 unsigned long gpio_flags)
402{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700403 struct sysfs_dirent *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700404 unsigned long irq_flags;
405 int ret, irq, id;
406
407 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
408 return 0;
409
Alexandre Courbot372e7222013-02-03 01:29:29 +0900410 irq = gpiod_to_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700411 if (irq < 0)
412 return -EIO;
413
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700414 id = desc->flags >> ID_SHIFT;
415 value_sd = idr_find(&dirent_idr, id);
416 if (value_sd)
417 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700418
419 desc->flags &= ~GPIO_TRIGGER_MASK;
420
421 if (!gpio_flags) {
Linus Walleijd468bf92013-09-24 11:54:38 +0200422 gpiod_unlock_as_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700423 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700424 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700425 }
426
427 irq_flags = IRQF_SHARED;
428 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800429 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
430 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700431 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800432 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
433 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700434
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700435 if (!value_sd) {
Tejun Heo388975c2013-09-11 23:19:13 -0400436 value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700437 if (!value_sd) {
438 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700439 goto err_out;
440 }
441
Tejun Heo62f516b2013-02-27 17:04:06 -0800442 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
443 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700444 goto free_sd;
Tejun Heo62f516b2013-02-27 17:04:06 -0800445 id = ret;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700446
447 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700448 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700449
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700450 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700451 ret = -ERANGE;
452 goto free_id;
453 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700454 }
455
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700456 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700457 "gpiolib", value_sd);
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700458 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700459 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700460
Linus Walleijd468bf92013-09-24 11:54:38 +0200461 ret = gpiod_lock_as_irq(desc);
462 if (ret < 0) {
463 gpiod_warn(desc, "failed to flag the GPIO for IRQ\n");
464 goto free_id;
465 }
466
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700467 desc->flags |= gpio_flags;
468 return 0;
469
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700470free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700471 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700472 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700473free_sd:
474 if (value_sd)
475 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700476err_out:
477 return ret;
478}
479
480static const struct {
481 const char *name;
482 unsigned long flags;
483} trigger_types[] = {
484 { "none", 0 },
485 { "falling", BIT(FLAG_TRIG_FALL) },
486 { "rising", BIT(FLAG_TRIG_RISE) },
487 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
488};
489
490static ssize_t gpio_edge_show(struct device *dev,
491 struct device_attribute *attr, char *buf)
492{
493 const struct gpio_desc *desc = dev_get_drvdata(dev);
494 ssize_t status;
495
496 mutex_lock(&sysfs_lock);
497
498 if (!test_bit(FLAG_EXPORT, &desc->flags))
499 status = -EIO;
500 else {
501 int i;
502
503 status = 0;
504 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
505 if ((desc->flags & GPIO_TRIGGER_MASK)
506 == trigger_types[i].flags) {
507 status = sprintf(buf, "%s\n",
508 trigger_types[i].name);
509 break;
510 }
511 }
512
513 mutex_unlock(&sysfs_lock);
514 return status;
515}
516
517static ssize_t gpio_edge_store(struct device *dev,
518 struct device_attribute *attr, const char *buf, size_t size)
519{
520 struct gpio_desc *desc = dev_get_drvdata(dev);
521 ssize_t status;
522 int i;
523
524 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
525 if (sysfs_streq(trigger_types[i].name, buf))
526 goto found;
527 return -EINVAL;
528
529found:
530 mutex_lock(&sysfs_lock);
531
532 if (!test_bit(FLAG_EXPORT, &desc->flags))
533 status = -EIO;
534 else {
535 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
536 if (!status)
537 status = size;
538 }
539
540 mutex_unlock(&sysfs_lock);
541
542 return status;
543}
544
545static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
546
Jani Nikula07697462009-12-15 16:46:20 -0800547static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
548 int value)
549{
550 int status = 0;
551
552 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
553 return 0;
554
555 if (value)
556 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
557 else
558 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
559
560 /* reconfigure poll(2) support if enabled on one edge only */
561 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
562 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
563 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
564
565 gpio_setup_irq(desc, dev, 0);
566 status = gpio_setup_irq(desc, dev, trigger_flags);
567 }
568
569 return status;
570}
571
572static ssize_t gpio_active_low_show(struct device *dev,
573 struct device_attribute *attr, char *buf)
574{
575 const struct gpio_desc *desc = dev_get_drvdata(dev);
576 ssize_t status;
577
578 mutex_lock(&sysfs_lock);
579
580 if (!test_bit(FLAG_EXPORT, &desc->flags))
581 status = -EIO;
582 else
583 status = sprintf(buf, "%d\n",
584 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
585
586 mutex_unlock(&sysfs_lock);
587
588 return status;
589}
590
591static ssize_t gpio_active_low_store(struct device *dev,
592 struct device_attribute *attr, const char *buf, size_t size)
593{
594 struct gpio_desc *desc = dev_get_drvdata(dev);
595 ssize_t status;
596
597 mutex_lock(&sysfs_lock);
598
599 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
600 status = -EIO;
601 } else {
602 long value;
603
Jingoo Hana3d88c92013-07-19 16:12:50 +0900604 status = kstrtol(buf, 0, &value);
Jani Nikula07697462009-12-15 16:46:20 -0800605 if (status == 0)
606 status = sysfs_set_active_low(desc, dev, value != 0);
607 }
608
609 mutex_unlock(&sysfs_lock);
610
611 return status ? : size;
612}
613
614static const DEVICE_ATTR(active_low, 0644,
615 gpio_active_low_show, gpio_active_low_store);
616
David Brownelld8f388d82008-07-25 01:46:07 -0700617static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d82008-07-25 01:46:07 -0700618 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800619 &dev_attr_active_low.attr,
David Brownelld8f388d82008-07-25 01:46:07 -0700620 NULL,
621};
622
623static const struct attribute_group gpio_attr_group = {
624 .attrs = (struct attribute **) gpio_attrs,
625};
626
627/*
628 * /sys/class/gpio/gpiochipN/
629 * /base ... matching gpio_chip.base (N)
630 * /label ... matching gpio_chip.label
631 * /ngpio ... matching gpio_chip.ngpio
632 */
633
634static ssize_t chip_base_show(struct device *dev,
635 struct device_attribute *attr, char *buf)
636{
637 const struct gpio_chip *chip = dev_get_drvdata(dev);
638
639 return sprintf(buf, "%d\n", chip->base);
640}
641static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
642
643static ssize_t chip_label_show(struct device *dev,
644 struct device_attribute *attr, char *buf)
645{
646 const struct gpio_chip *chip = dev_get_drvdata(dev);
647
648 return sprintf(buf, "%s\n", chip->label ? : "");
649}
650static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
651
652static ssize_t chip_ngpio_show(struct device *dev,
653 struct device_attribute *attr, char *buf)
654{
655 const struct gpio_chip *chip = dev_get_drvdata(dev);
656
657 return sprintf(buf, "%u\n", chip->ngpio);
658}
659static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
660
661static const struct attribute *gpiochip_attrs[] = {
662 &dev_attr_base.attr,
663 &dev_attr_label.attr,
664 &dev_attr_ngpio.attr,
665 NULL,
666};
667
668static const struct attribute_group gpiochip_attr_group = {
669 .attrs = (struct attribute **) gpiochip_attrs,
670};
671
672/*
673 * /sys/class/gpio/export ... write-only
674 * integer N ... number of GPIO to export (full access)
675 * /sys/class/gpio/unexport ... write-only
676 * integer N ... number of GPIO to unexport
677 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100678static ssize_t export_store(struct class *class,
679 struct class_attribute *attr,
680 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700681{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900682 long gpio;
683 struct gpio_desc *desc;
684 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700685
Jingoo Hana3d88c92013-07-19 16:12:50 +0900686 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700687 if (status < 0)
688 goto done;
689
Alexandre Courbot372e7222013-02-03 01:29:29 +0900690 desc = gpio_to_desc(gpio);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900691 /* reject invalid GPIOs */
692 if (!desc) {
693 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
694 return -EINVAL;
695 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900696
David Brownelld8f388d82008-07-25 01:46:07 -0700697 /* No extra locking here; FLAG_SYSFS just signifies that the
698 * request and export were done by on behalf of userspace, so
699 * they may be undone on its behalf too.
700 */
701
Alexandre Courbot372e7222013-02-03 01:29:29 +0900702 status = gpiod_request(desc, "sysfs");
Mathias Nymanad2fab32012-10-25 14:03:03 +0300703 if (status < 0) {
704 if (status == -EPROBE_DEFER)
705 status = -ENODEV;
David Brownelld8f388d82008-07-25 01:46:07 -0700706 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +0300707 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900708 status = gpiod_export(desc, true);
David Brownelld8f388d82008-07-25 01:46:07 -0700709 if (status < 0)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900710 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700711 else
Alexandre Courbot372e7222013-02-03 01:29:29 +0900712 set_bit(FLAG_SYSFS, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700713
714done:
715 if (status)
716 pr_debug("%s: status %d\n", __func__, status);
717 return status ? : len;
718}
719
Andi Kleen28812fe2010-01-05 12:48:07 +0100720static ssize_t unexport_store(struct class *class,
721 struct class_attribute *attr,
722 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700723{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900724 long gpio;
725 struct gpio_desc *desc;
726 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700727
Jingoo Hana3d88c92013-07-19 16:12:50 +0900728 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700729 if (status < 0)
730 goto done;
731
Alexandre Courbot372e7222013-02-03 01:29:29 +0900732 desc = gpio_to_desc(gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700733 /* reject bogus commands (gpio_unexport ignores them) */
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900734 if (!desc) {
735 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
736 return -EINVAL;
737 }
738
739 status = -EINVAL;
David Brownelld8f388d82008-07-25 01:46:07 -0700740
741 /* No extra locking here; FLAG_SYSFS just signifies that the
742 * request and export were done by on behalf of userspace, so
743 * they may be undone on its behalf too.
744 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900745 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700746 status = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900747 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700748 }
749done:
750 if (status)
751 pr_debug("%s: status %d\n", __func__, status);
752 return status ? : len;
753}
754
755static struct class_attribute gpio_class_attrs[] = {
756 __ATTR(export, 0200, NULL, export_store),
757 __ATTR(unexport, 0200, NULL, unexport_store),
758 __ATTR_NULL,
759};
760
761static struct class gpio_class = {
762 .name = "gpio",
763 .owner = THIS_MODULE,
764
765 .class_attrs = gpio_class_attrs,
766};
767
768
769/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700770 * gpiod_export - export a GPIO through sysfs
David Brownelld8f388d82008-07-25 01:46:07 -0700771 * @gpio: gpio to make available, already requested
772 * @direction_may_change: true if userspace may change gpio direction
773 * Context: arch_initcall or later
774 *
775 * When drivers want to make a GPIO accessible to userspace after they
776 * have requested it -- perhaps while debugging, or as part of their
777 * public interface -- they may use this routine. If the GPIO can
778 * change direction (some can't) and the caller allows it, userspace
779 * will see "direction" sysfs attribute which may be used to change
780 * the gpio's direction. A "value" attribute will always be provided.
781 *
782 * Returns zero on success, else an error.
783 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700784int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
David Brownelld8f388d82008-07-25 01:46:07 -0700785{
786 unsigned long flags;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100787 int status;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700788 const char *ioname = NULL;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100789 struct device *dev;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900790 int offset;
David Brownelld8f388d82008-07-25 01:46:07 -0700791
792 /* can't export until sysfs is available ... */
793 if (!gpio_class.p) {
794 pr_debug("%s: called too early!\n", __func__);
795 return -ENOENT;
796 }
797
Alexandre Courbot372e7222013-02-03 01:29:29 +0900798 if (!desc) {
799 pr_debug("%s: invalid gpio descriptor\n", __func__);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100800 return -EINVAL;
801 }
David Brownelld8f388d82008-07-25 01:46:07 -0700802
803 mutex_lock(&sysfs_lock);
804
805 spin_lock_irqsave(&gpio_lock, flags);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100806 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
807 test_bit(FLAG_EXPORT, &desc->flags)) {
808 spin_unlock_irqrestore(&gpio_lock, flags);
809 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +0900810 __func__, desc_to_gpio(desc),
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100811 test_bit(FLAG_REQUESTED, &desc->flags),
812 test_bit(FLAG_EXPORT, &desc->flags));
Dan Carpenter529f2ad2012-10-26 09:59:43 +0300813 status = -EPERM;
814 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700815 }
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100816
817 if (!desc->chip->direction_input || !desc->chip->direction_output)
818 direction_may_change = false;
David Brownelld8f388d82008-07-25 01:46:07 -0700819 spin_unlock_irqrestore(&gpio_lock, flags);
820
Alexandre Courbot372e7222013-02-03 01:29:29 +0900821 offset = gpio_chip_hwgpio(desc);
822 if (desc->chip->names && desc->chip->names[offset])
823 ioname = desc->chip->names[offset];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700824
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100825 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Alexandre Courbot372e7222013-02-03 01:29:29 +0900826 desc, ioname ? ioname : "gpio%u",
827 desc_to_gpio(desc));
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100828 if (IS_ERR(dev)) {
829 status = PTR_ERR(dev);
830 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700831 }
832
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100833 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
David Brownelld8f388d82008-07-25 01:46:07 -0700834 if (status)
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100835 goto fail_unregister_device;
David Brownelld8f388d82008-07-25 01:46:07 -0700836
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100837 if (direction_may_change) {
838 status = device_create_file(dev, &dev_attr_direction);
839 if (status)
840 goto fail_unregister_device;
841 }
842
Alexandre Courbot372e7222013-02-03 01:29:29 +0900843 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100844 !test_bit(FLAG_IS_OUT, &desc->flags))) {
845 status = device_create_file(dev, &dev_attr_edge);
846 if (status)
847 goto fail_unregister_device;
848 }
849
850 set_bit(FLAG_EXPORT, &desc->flags);
851 mutex_unlock(&sysfs_lock);
852 return 0;
853
854fail_unregister_device:
855 device_unregister(dev);
856fail_unlock:
857 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900858 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
859 status);
David Brownelld8f388d82008-07-25 01:46:07 -0700860 return status;
861}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700862EXPORT_SYMBOL_GPL(gpiod_export);
David Brownelld8f388d82008-07-25 01:46:07 -0700863
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100864static int match_export(struct device *dev, const void *data)
David Brownelld8f388d82008-07-25 01:46:07 -0700865{
866 return dev_get_drvdata(dev) == data;
867}
868
869/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700870 * gpiod_export_link - create a sysfs link to an exported GPIO node
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700871 * @dev: device under which to create symlink
872 * @name: name of the symlink
873 * @gpio: gpio to create symlink to, already exported
874 *
875 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
876 * node. Caller is responsible for unlinking.
877 *
878 * Returns zero on success, else an error.
879 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700880int gpiod_export_link(struct device *dev, const char *name,
881 struct gpio_desc *desc)
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700882{
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700883 int status = -EINVAL;
884
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900885 if (!desc) {
886 pr_warn("%s: invalid GPIO\n", __func__);
887 return -EINVAL;
888 }
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700889
890 mutex_lock(&sysfs_lock);
891
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700892 if (test_bit(FLAG_EXPORT, &desc->flags)) {
893 struct device *tdev;
894
895 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
896 if (tdev != NULL) {
897 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
898 name);
899 } else {
900 status = -ENODEV;
901 }
902 }
903
904 mutex_unlock(&sysfs_lock);
905
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700906 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900907 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
908 status);
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700909
910 return status;
911}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700912EXPORT_SYMBOL_GPL(gpiod_export_link);
Jani Nikula07697462009-12-15 16:46:20 -0800913
914/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700915 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
Jani Nikula07697462009-12-15 16:46:20 -0800916 * @gpio: gpio to change
917 * @value: non-zero to use active low, i.e. inverted values
918 *
919 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
920 * The GPIO does not have to be exported yet. If poll(2) support has
921 * been enabled for either rising or falling edge, it will be
922 * reconfigured to follow the new polarity.
923 *
924 * Returns zero on success, else an error.
925 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700926int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
Jani Nikula07697462009-12-15 16:46:20 -0800927{
Jani Nikula07697462009-12-15 16:46:20 -0800928 struct device *dev = NULL;
929 int status = -EINVAL;
930
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900931 if (!desc) {
932 pr_warn("%s: invalid GPIO\n", __func__);
933 return -EINVAL;
934 }
Jani Nikula07697462009-12-15 16:46:20 -0800935
936 mutex_lock(&sysfs_lock);
937
Jani Nikula07697462009-12-15 16:46:20 -0800938 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800939 dev = class_find_device(&gpio_class, NULL, desc, match_export);
940 if (dev == NULL) {
941 status = -ENODEV;
942 goto unlock;
943 }
944 }
945
946 status = sysfs_set_active_low(desc, dev, value);
947
948unlock:
949 mutex_unlock(&sysfs_lock);
950
Jani Nikula07697462009-12-15 16:46:20 -0800951 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900952 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
953 status);
Jani Nikula07697462009-12-15 16:46:20 -0800954
955 return status;
956}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700957EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
Jani Nikula07697462009-12-15 16:46:20 -0800958
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700959/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700960 * gpiod_unexport - reverse effect of gpio_export()
David Brownelld8f388d82008-07-25 01:46:07 -0700961 * @gpio: gpio to make unavailable
962 *
963 * This is implicit on gpio_free().
964 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700965void gpiod_unexport(struct gpio_desc *desc)
David Brownelld8f388d82008-07-25 01:46:07 -0700966{
Jon Povey6a99ad42010-07-27 13:18:06 -0700967 int status = 0;
Ming Lei864533c2012-02-13 22:53:20 +0800968 struct device *dev = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -0700969
Alexandre Courbot372e7222013-02-03 01:29:29 +0900970 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900971 pr_warn("%s: invalid GPIO\n", __func__);
972 return;
Jon Povey6a99ad42010-07-27 13:18:06 -0700973 }
David Brownelld8f388d82008-07-25 01:46:07 -0700974
975 mutex_lock(&sysfs_lock);
976
David Brownelld8f388d82008-07-25 01:46:07 -0700977 if (test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700978
979 dev = class_find_device(&gpio_class, NULL, desc, match_export);
980 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700981 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700982 clear_bit(FLAG_EXPORT, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700983 } else
984 status = -ENODEV;
985 }
986
987 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900988
Ming Lei864533c2012-02-13 22:53:20 +0800989 if (dev) {
990 device_unregister(dev);
991 put_device(dev);
992 }
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900993
David Brownelld8f388d82008-07-25 01:46:07 -0700994 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900995 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
996 status);
997}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700998EXPORT_SYMBOL_GPL(gpiod_unexport);
David Brownelld8f388d82008-07-25 01:46:07 -0700999
1000static int gpiochip_export(struct gpio_chip *chip)
1001{
1002 int status;
1003 struct device *dev;
1004
1005 /* Many systems register gpio chips for SOC support very early,
1006 * before driver model support is available. In those cases we
1007 * export this later, in gpiolib_sysfs_init() ... here we just
1008 * verify that _some_ field of gpio_class got initialized.
1009 */
1010 if (!gpio_class.p)
1011 return 0;
1012
1013 /* use chip->base for the ID; it's already known to be unique */
1014 mutex_lock(&sysfs_lock);
1015 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
1016 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001017 if (!IS_ERR(dev)) {
David Brownelld8f388d82008-07-25 01:46:07 -07001018 status = sysfs_create_group(&dev->kobj,
1019 &gpiochip_attr_group);
1020 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001021 status = PTR_ERR(dev);
David Brownelld8f388d82008-07-25 01:46:07 -07001022 chip->exported = (status == 0);
1023 mutex_unlock(&sysfs_lock);
1024
1025 if (status) {
1026 unsigned long flags;
1027 unsigned gpio;
1028
1029 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001030 gpio = 0;
1031 while (gpio < chip->ngpio)
1032 chip->desc[gpio++].chip = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -07001033 spin_unlock_irqrestore(&gpio_lock, flags);
1034
1035 pr_debug("%s: chip %s status %d\n", __func__,
1036 chip->label, status);
1037 }
1038
1039 return status;
1040}
1041
1042static void gpiochip_unexport(struct gpio_chip *chip)
1043{
1044 int status;
1045 struct device *dev;
1046
1047 mutex_lock(&sysfs_lock);
1048 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1049 if (dev) {
1050 put_device(dev);
1051 device_unregister(dev);
1052 chip->exported = 0;
1053 status = 0;
1054 } else
1055 status = -ENODEV;
1056 mutex_unlock(&sysfs_lock);
1057
1058 if (status)
1059 pr_debug("%s: chip %s status %d\n", __func__,
1060 chip->label, status);
1061}
1062
1063static int __init gpiolib_sysfs_init(void)
1064{
1065 int status;
1066 unsigned long flags;
Alexandre Courbot65493e32013-02-03 01:29:25 +09001067 struct gpio_chip *chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001068
1069 status = class_register(&gpio_class);
1070 if (status < 0)
1071 return status;
1072
1073 /* Scan and register the gpio_chips which registered very
1074 * early (e.g. before the class_register above was called).
1075 *
1076 * We run before arch_initcall() so chip->dev nodes can have
1077 * registered, and so arch_initcall() can always gpio_export().
1078 */
1079 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot65493e32013-02-03 01:29:25 +09001080 list_for_each_entry(chip, &gpio_chips, list) {
David Brownelld8f388d82008-07-25 01:46:07 -07001081 if (!chip || chip->exported)
1082 continue;
1083
1084 spin_unlock_irqrestore(&gpio_lock, flags);
1085 status = gpiochip_export(chip);
1086 spin_lock_irqsave(&gpio_lock, flags);
1087 }
1088 spin_unlock_irqrestore(&gpio_lock, flags);
1089
1090
1091 return status;
1092}
1093postcore_initcall(gpiolib_sysfs_init);
1094
1095#else
1096static inline int gpiochip_export(struct gpio_chip *chip)
1097{
1098 return 0;
1099}
1100
1101static inline void gpiochip_unexport(struct gpio_chip *chip)
1102{
1103}
1104
1105#endif /* CONFIG_GPIO_SYSFS */
1106
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001107/*
1108 * Add a new chip to the global chips list, keeping the list of chips sorted
1109 * by base order.
1110 *
1111 * Return -EBUSY if the new chip overlaps with some other chip's integer
1112 * space.
1113 */
1114static int gpiochip_add_to_list(struct gpio_chip *chip)
1115{
1116 struct list_head *pos = &gpio_chips;
1117 struct gpio_chip *_chip;
1118 int err = 0;
1119
1120 /* find where to insert our chip */
1121 list_for_each(pos, &gpio_chips) {
1122 _chip = list_entry(pos, struct gpio_chip, list);
1123 /* shall we insert before _chip? */
1124 if (_chip->base >= chip->base + chip->ngpio)
1125 break;
1126 }
1127
1128 /* are we stepping on the chip right before? */
1129 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1130 _chip = list_entry(pos->prev, struct gpio_chip, list);
1131 if (_chip->base + _chip->ngpio > chip->base) {
1132 dev_err(chip->dev,
1133 "GPIO integer space overlap, cannot add chip\n");
1134 err = -EBUSY;
1135 }
1136 }
1137
1138 if (!err)
1139 list_add_tail(&chip->list, pos);
1140
1141 return err;
1142}
1143
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001144/**
David Brownelld2876d02008-02-04 22:28:20 -08001145 * gpiochip_add() - register a gpio_chip
1146 * @chip: the chip to register, with chip->base initialized
1147 * Context: potentially before irqs or kmalloc will work
1148 *
1149 * Returns a negative errno if the chip can't be registered, such as
1150 * because the chip->base is invalid or already associated with a
1151 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001152 *
David Brownelld8f388d82008-07-25 01:46:07 -07001153 * When gpiochip_add() is called very early during boot, so that GPIOs
1154 * can be freely used, the chip->dev device must be registered before
1155 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1156 * for GPIOs will fail rudely.
1157 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001158 * If chip->base is negative, this requests dynamic assignment of
1159 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001160 */
1161int gpiochip_add(struct gpio_chip *chip)
1162{
1163 unsigned long flags;
1164 int status = 0;
1165 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001166 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001167
Trent Piephobff5fda2008-05-23 13:04:44 -07001168 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001169 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001170 status = -EINVAL;
1171 goto fail;
1172 }
1173
1174 spin_lock_irqsave(&gpio_lock, flags);
1175
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001176 if (base < 0) {
1177 base = gpiochip_find_base(chip->ngpio);
1178 if (base < 0) {
1179 status = base;
David Brownelld8f388d82008-07-25 01:46:07 -07001180 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001181 }
1182 chip->base = base;
1183 }
1184
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001185 status = gpiochip_add_to_list(chip);
1186
David Brownelld2876d02008-02-04 22:28:20 -08001187 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001188 chip->desc = &gpio_desc[chip->base];
1189
1190 for (id = 0; id < chip->ngpio; id++) {
1191 struct gpio_desc *desc = &chip->desc[id];
1192 desc->chip = chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001193
1194 /* REVISIT: most hardware initializes GPIOs as
1195 * inputs (often with pullups enabled) so power
1196 * usage is minimized. Linux code should set the
1197 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +03001198 * and in case chip->get_direction is not set,
David Brownelld8f388d82008-07-25 01:46:07 -07001199 * we may expose the wrong direction in sysfs.
1200 */
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001201 desc->flags = !chip->direction_input
David Brownelld8f388d82008-07-25 01:46:07 -07001202 ? (1 << FLAG_IS_OUT)
1203 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001204 }
1205 }
1206
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001207 spin_unlock_irqrestore(&gpio_lock, flags);
1208
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301209#ifdef CONFIG_PINCTRL
1210 INIT_LIST_HEAD(&chip->pin_ranges);
1211#endif
1212
Anton Vorontsov391c9702010-06-08 07:48:17 -06001213 of_gpiochip_add(chip);
1214
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001215 if (status)
1216 goto fail;
1217
1218 status = gpiochip_export(chip);
1219 if (status)
1220 goto fail;
1221
H Hartley Sweetenee1c1e72012-05-11 16:58:33 -07001222 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
Grant Likely64842aa2011-11-06 11:36:18 -07001223 chip->base, chip->base + chip->ngpio - 1,
1224 chip->label ? : "generic");
1225
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001226 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001227
1228unlock:
1229 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld2876d02008-02-04 22:28:20 -08001230fail:
1231 /* failures here can mean systems won't boot... */
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001232 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1233 chip->base, chip->base + chip->ngpio - 1,
1234 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001235 return status;
1236}
1237EXPORT_SYMBOL_GPL(gpiochip_add);
1238
1239/**
1240 * gpiochip_remove() - unregister a gpio_chip
1241 * @chip: the chip to unregister
1242 *
1243 * A gpio_chip with any GPIOs still requested may not be removed.
1244 */
1245int gpiochip_remove(struct gpio_chip *chip)
1246{
1247 unsigned long flags;
1248 int status = 0;
1249 unsigned id;
1250
1251 spin_lock_irqsave(&gpio_lock, flags);
1252
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001253 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001254 of_gpiochip_remove(chip);
1255
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001256 for (id = 0; id < chip->ngpio; id++) {
1257 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
David Brownelld2876d02008-02-04 22:28:20 -08001258 status = -EBUSY;
1259 break;
1260 }
1261 }
1262 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001263 for (id = 0; id < chip->ngpio; id++)
1264 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001265
1266 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -08001267 }
1268
1269 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d82008-07-25 01:46:07 -07001270
1271 if (status == 0)
1272 gpiochip_unexport(chip);
1273
David Brownelld2876d02008-02-04 22:28:20 -08001274 return status;
1275}
1276EXPORT_SYMBOL_GPL(gpiochip_remove);
1277
Grant Likely594fa262010-06-08 07:48:16 -06001278/**
1279 * gpiochip_find() - iterator for locating a specific gpio_chip
1280 * @data: data to pass to match function
1281 * @callback: Callback function to check gpio_chip
1282 *
1283 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1284 * determined by a user supplied @match callback. The callback should return
1285 * 0 if the device doesn't match and non-zero if it does. If the callback is
1286 * non-zero, this function will return to the caller and not iterate over any
1287 * more gpio_chips.
1288 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001289struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001290 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001291 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001292{
Alexandre Courbot125eef92013-02-03 01:29:26 +09001293 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001294 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001295
1296 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +09001297 list_for_each_entry(chip, &gpio_chips, list)
1298 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001299 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001300
1301 /* No match? */
1302 if (&chip->list == &gpio_chips)
1303 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001304 spin_unlock_irqrestore(&gpio_lock, flags);
1305
1306 return chip;
1307}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001308EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001309
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301310#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001311
Linus Walleij3f0f8672012-11-20 12:40:15 +01001312/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001313 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1314 * @chip: the gpiochip to add the range for
1315 * @pinctrl: the dev_name() of the pin controller to map to
1316 * @gpio_offset: the start offset in the current gpio_chip number space
1317 * @pin_group: name of the pin group inside the pin controller
1318 */
1319int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1320 struct pinctrl_dev *pctldev,
1321 unsigned int gpio_offset, const char *pin_group)
1322{
1323 struct gpio_pin_range *pin_range;
1324 int ret;
1325
1326 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1327 if (!pin_range) {
1328 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1329 chip->label);
1330 return -ENOMEM;
1331 }
1332
1333 /* Use local offset as range ID */
1334 pin_range->range.id = gpio_offset;
1335 pin_range->range.gc = chip;
1336 pin_range->range.name = chip->label;
1337 pin_range->range.base = chip->base + gpio_offset;
1338 pin_range->pctldev = pctldev;
1339
1340 ret = pinctrl_get_group_pins(pctldev, pin_group,
1341 &pin_range->range.pins,
1342 &pin_range->range.npins);
1343 if (ret < 0)
1344 return ret;
1345
1346 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1347
1348 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PINGRP %s\n",
1349 chip->label, gpio_offset,
1350 gpio_offset + pin_range->range.npins - 1,
1351 pinctrl_dev_get_devname(pctldev), pin_group);
1352
1353 list_add_tail(&pin_range->node, &chip->pin_ranges);
1354
1355 return 0;
1356}
1357EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1358
1359/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001360 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1361 * @chip: the gpiochip to add the range for
1362 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001363 * @gpio_offset: the start offset in the current gpio_chip number space
1364 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001365 * @npins: the number of pins from the offset of each pin space (GPIO and
1366 * pin controller) to accumulate in this range
1367 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001368int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001369 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001370 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301371{
1372 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001373 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301374
Linus Walleij3f0f8672012-11-20 12:40:15 +01001375 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301376 if (!pin_range) {
1377 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1378 chip->label);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001379 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301380 }
1381
Linus Walleij3f0f8672012-11-20 12:40:15 +01001382 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001383 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001384 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301385 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001386 pin_range->range.base = chip->base + gpio_offset;
1387 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301388 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001389 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301390 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001391 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001392 ret = PTR_ERR(pin_range->pctldev);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001393 pr_err("%s: GPIO chip: could not create pin range\n",
1394 chip->label);
1395 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001396 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001397 }
Linus Walleij316511c2012-11-21 08:48:09 +01001398 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1399 chip->label, gpio_offset, gpio_offset + npins - 1,
1400 pinctl_name,
1401 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301402
1403 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001404
1405 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301406}
Linus Walleij165adc92012-11-06 14:49:39 +01001407EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301408
Linus Walleij3f0f8672012-11-20 12:40:15 +01001409/**
1410 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1411 * @chip: the chip to remove all the mappings for
1412 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301413void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1414{
1415 struct gpio_pin_range *pin_range, *tmp;
1416
1417 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1418 list_del(&pin_range->node);
1419 pinctrl_remove_gpio_range(pin_range->pctldev,
1420 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001421 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301422 }
1423}
Linus Walleij165adc92012-11-06 14:49:39 +01001424EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1425
1426#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301427
David Brownelld2876d02008-02-04 22:28:20 -08001428/* These "optional" allocation calls help prevent drivers from stomping
1429 * on each other, and help provide better diagnostics in debugfs.
1430 * They're called even less than the "set direction" calls.
1431 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001432static int gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001433{
David Brownell35e8bb52008-10-15 22:03:16 -07001434 struct gpio_chip *chip;
Mark Browne9354572012-07-09 12:22:56 +01001435 int status = -EPROBE_DEFER;
David Brownelld2876d02008-02-04 22:28:20 -08001436 unsigned long flags;
1437
Alexandre Courbot0204df42013-10-04 10:59:58 -07001438 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001439 pr_warn("%s: invalid GPIO\n", __func__);
1440 return -EINVAL;
1441 }
1442
David Brownelld2876d02008-02-04 22:28:20 -08001443 spin_lock_irqsave(&gpio_lock, flags);
1444
David Brownell35e8bb52008-10-15 22:03:16 -07001445 chip = desc->chip;
Alexandre Courbot0204df42013-10-04 10:59:58 -07001446 if (chip == NULL)
1447 goto done;
David Brownelld2876d02008-02-04 22:28:20 -08001448
David Brownell35e8bb52008-10-15 22:03:16 -07001449 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001450 goto done;
1451
David Brownelld2876d02008-02-04 22:28:20 -08001452 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001453 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001454 */
1455
1456 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1457 desc_set_label(desc, label ? : "?");
1458 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001459 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001460 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001461 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001462 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001463 }
1464
1465 if (chip->request) {
1466 /* chip->request may sleep */
1467 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001468 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001469 spin_lock_irqsave(&gpio_lock, flags);
1470
1471 if (status < 0) {
1472 desc_set_label(desc, NULL);
1473 module_put(chip->owner);
1474 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001475 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001476 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001477 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001478 if (chip->get_direction) {
1479 /* chip->get_direction may sleep */
1480 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001481 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001482 spin_lock_irqsave(&gpio_lock, flags);
1483 }
David Brownelld2876d02008-02-04 22:28:20 -08001484done:
1485 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001486 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001487 desc_to_gpio(desc), label ? : "?", status);
David Brownelld2876d02008-02-04 22:28:20 -08001488 spin_unlock_irqrestore(&gpio_lock, flags);
1489 return status;
1490}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001491
1492int gpio_request(unsigned gpio, const char *label)
1493{
1494 return gpiod_request(gpio_to_desc(gpio), label);
1495}
David Brownelld2876d02008-02-04 22:28:20 -08001496EXPORT_SYMBOL_GPL(gpio_request);
1497
Alexandre Courbot372e7222013-02-03 01:29:29 +09001498static void gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001499{
1500 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001501 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001502
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001503 might_sleep();
1504
Alexandre Courbot372e7222013-02-03 01:29:29 +09001505 if (!desc) {
David Brownelld2876d02008-02-04 22:28:20 -08001506 WARN_ON(extra_checks);
1507 return;
1508 }
1509
Alexandre Courbot372e7222013-02-03 01:29:29 +09001510 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07001511
David Brownelld2876d02008-02-04 22:28:20 -08001512 spin_lock_irqsave(&gpio_lock, flags);
1513
David Brownell35e8bb52008-10-15 22:03:16 -07001514 chip = desc->chip;
1515 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1516 if (chip->free) {
1517 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001518 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001519 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001520 spin_lock_irqsave(&gpio_lock, flags);
1521 }
David Brownelld2876d02008-02-04 22:28:20 -08001522 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001523 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001524 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001525 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301526 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301527 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001528 } else
David Brownelld2876d02008-02-04 22:28:20 -08001529 WARN_ON(extra_checks);
1530
1531 spin_unlock_irqrestore(&gpio_lock, flags);
1532}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001533
1534void gpio_free(unsigned gpio)
1535{
1536 gpiod_free(gpio_to_desc(gpio));
1537}
David Brownelld2876d02008-02-04 22:28:20 -08001538EXPORT_SYMBOL_GPL(gpio_free);
1539
Eric Miao3e45f1d2010-03-05 13:44:35 -08001540/**
1541 * gpio_request_one - request a single GPIO with initial configuration
1542 * @gpio: the GPIO number
1543 * @flags: GPIO configuration as specified by GPIOF_*
1544 * @label: a literal description string of this GPIO
1545 */
1546int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1547{
Alexandre Courbot372e7222013-02-03 01:29:29 +09001548 struct gpio_desc *desc;
Eric Miao3e45f1d2010-03-05 13:44:35 -08001549 int err;
1550
Alexandre Courbot372e7222013-02-03 01:29:29 +09001551 desc = gpio_to_desc(gpio);
1552
1553 err = gpiod_request(desc, label);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001554 if (err)
1555 return err;
1556
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301557 if (flags & GPIOF_OPEN_DRAIN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001558 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301559
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301560 if (flags & GPIOF_OPEN_SOURCE)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001561 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301562
Eric Miao3e45f1d2010-03-05 13:44:35 -08001563 if (flags & GPIOF_DIR_IN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001564 err = gpiod_direction_input(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001565 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001566 err = gpiod_direction_output(desc,
Eric Miao3e45f1d2010-03-05 13:44:35 -08001567 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1568
Aaro Koskinene2548112010-12-21 17:24:22 -08001569 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001570 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -08001571
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001572 if (flags & GPIOF_EXPORT) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001573 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001574 if (err)
1575 goto free_gpio;
1576 }
1577
1578 return 0;
1579
1580 free_gpio:
Alexandre Courbot372e7222013-02-03 01:29:29 +09001581 gpiod_free(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001582 return err;
1583}
1584EXPORT_SYMBOL_GPL(gpio_request_one);
1585
1586/**
1587 * gpio_request_array - request multiple GPIOs in a single call
1588 * @array: array of the 'struct gpio'
1589 * @num: how many GPIOs in the array
1590 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001591int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001592{
1593 int i, err;
1594
1595 for (i = 0; i < num; i++, array++) {
1596 err = gpio_request_one(array->gpio, array->flags, array->label);
1597 if (err)
1598 goto err_free;
1599 }
1600 return 0;
1601
1602err_free:
1603 while (i--)
1604 gpio_free((--array)->gpio);
1605 return err;
1606}
1607EXPORT_SYMBOL_GPL(gpio_request_array);
1608
1609/**
1610 * gpio_free_array - release multiple GPIOs in a single call
1611 * @array: array of the 'struct gpio'
1612 * @num: how many GPIOs in the array
1613 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001614void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001615{
1616 while (num--)
1617 gpio_free((array++)->gpio);
1618}
1619EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001620
1621/**
1622 * gpiochip_is_requested - return string iff signal was requested
1623 * @chip: controller managing the signal
1624 * @offset: of signal within controller's 0..(ngpio - 1) range
1625 *
1626 * Returns NULL if the GPIO is not currently requested, else a string.
1627 * If debugfs support is enabled, the string returned is the label passed
1628 * to gpio_request(); otherwise it is a meaningless constant.
1629 *
1630 * This function is for use by GPIO controller drivers. The label can
1631 * help with diagnostics, and knowing that the signal is used as a GPIO
1632 * can help avoid accidentally multiplexing it to another controller.
1633 */
1634const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1635{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001636 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001637
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001638 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -08001639 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001640
1641 desc = &chip->desc[offset];
1642
Alexandre Courbot372e7222013-02-03 01:29:29 +09001643 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001644 return NULL;
1645#ifdef CONFIG_DEBUG_FS
Alexandre Courbot372e7222013-02-03 01:29:29 +09001646 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001647#else
1648 return "?";
1649#endif
1650}
1651EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1652
1653
1654/* Drivers MUST set GPIO direction before making get/set calls. In
1655 * some cases this is done in early boot, before IRQs are enabled.
1656 *
1657 * As a rule these aren't called more than once (except for drivers
1658 * using the open-drain emulation idiom) so these are natural places
1659 * to accumulate extra debugging checks. Note that we can't (yet)
1660 * rely on gpio_request() having been called beforehand.
1661 */
1662
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001663/**
1664 * gpiod_direction_input - set the GPIO direction to input
1665 * @desc: GPIO to set to input
1666 *
1667 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1668 * be called safely on it.
1669 *
1670 * Return 0 in case of success, else an error code.
1671 */
1672int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001673{
1674 unsigned long flags;
1675 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001676 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001677 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001678
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001679 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001680 pr_warn("%s: invalid GPIO\n", __func__);
1681 return -EINVAL;
1682 }
1683
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001684 chip = desc->chip;
1685 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001686 gpiod_warn(desc,
1687 "%s: missing get() or direction_input() operations\n",
1688 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001689 return -EIO;
1690 }
1691
David Brownelld2876d02008-02-04 22:28:20 -08001692 spin_lock_irqsave(&gpio_lock, flags);
1693
Alexandre Courbot372e7222013-02-03 01:29:29 +09001694 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001695 if (status < 0)
1696 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001697
1698 /* now we know the gpio is valid and chip won't vanish */
1699
1700 spin_unlock_irqrestore(&gpio_lock, flags);
1701
David Brownell9c4ba942010-08-10 18:02:24 -07001702 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001703
Alexandre Courbot372e7222013-02-03 01:29:29 +09001704 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001705 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001706 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001707 if (status < 0) {
Mark Brown6424de52013-09-09 10:33:49 +01001708 gpiod_dbg(desc, "chip request fail, %d\n", status);
David Brownell35e8bb52008-10-15 22:03:16 -07001709 /* and it's not available to anyone else ...
1710 * gpio_request() is the fully clean solution.
1711 */
1712 goto lose;
1713 }
1714 }
1715
Alexandre Courbot372e7222013-02-03 01:29:29 +09001716 status = chip->direction_input(chip, offset);
David Brownelld2876d02008-02-04 22:28:20 -08001717 if (status == 0)
1718 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001719
Alexandre Courbot372e7222013-02-03 01:29:29 +09001720 trace_gpio_direction(desc_to_gpio(desc), 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001721lose:
David Brownelld2876d02008-02-04 22:28:20 -08001722 return status;
1723fail:
1724 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001725 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001726 gpiod_dbg(desc, "%s status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001727 return status;
1728}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001729EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001730
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001731/**
1732 * gpiod_direction_output - set the GPIO direction to input
1733 * @desc: GPIO to set to output
1734 * @value: initial output value of the GPIO
1735 *
1736 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1737 * be called safely on it. The initial value of the output must be specified.
1738 *
1739 * Return 0 in case of success, else an error code.
1740 */
1741int gpiod_direction_output(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001742{
1743 unsigned long flags;
1744 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001745 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001746 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001747
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001748 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001749 pr_warn("%s: invalid GPIO\n", __func__);
1750 return -EINVAL;
1751 }
1752
Linus Walleijd468bf92013-09-24 11:54:38 +02001753 /* GPIOs used for IRQs shall not be set as output */
1754 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1755 gpiod_err(desc,
1756 "%s: tried to set a GPIO tied to an IRQ as output\n",
1757 __func__);
1758 return -EIO;
1759 }
1760
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301761 /* Open drain pin should not be driven to 1 */
1762 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001763 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301764
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301765 /* Open source pin should not be driven to 0 */
1766 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001767 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301768
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001769 chip = desc->chip;
1770 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001771 gpiod_warn(desc,
1772 "%s: missing set() or direction_output() operations\n",
1773 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001774 return -EIO;
1775 }
1776
David Brownelld2876d02008-02-04 22:28:20 -08001777 spin_lock_irqsave(&gpio_lock, flags);
1778
Alexandre Courbot372e7222013-02-03 01:29:29 +09001779 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001780 if (status < 0)
1781 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001782
1783 /* now we know the gpio is valid and chip won't vanish */
1784
1785 spin_unlock_irqrestore(&gpio_lock, flags);
1786
David Brownell9c4ba942010-08-10 18:02:24 -07001787 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001788
Alexandre Courbot372e7222013-02-03 01:29:29 +09001789 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001790 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001791 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001792 if (status < 0) {
Mark Brown6424de52013-09-09 10:33:49 +01001793 gpiod_dbg(desc, "chip request fail, %d\n", status);
David Brownell35e8bb52008-10-15 22:03:16 -07001794 /* and it's not available to anyone else ...
1795 * gpio_request() is the fully clean solution.
1796 */
1797 goto lose;
1798 }
1799 }
1800
Alexandre Courbot372e7222013-02-03 01:29:29 +09001801 status = chip->direction_output(chip, offset, value);
David Brownelld2876d02008-02-04 22:28:20 -08001802 if (status == 0)
1803 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001804 trace_gpio_value(desc_to_gpio(desc), 0, value);
1805 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001806lose:
David Brownelld2876d02008-02-04 22:28:20 -08001807 return status;
1808fail:
1809 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001810 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001811 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001812 return status;
1813}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001814EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001815
Felipe Balbic4b5be92010-05-26 14:42:23 -07001816/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001817 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001818 * @gpio: the gpio to set debounce time
1819 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001820 *
1821 * returns -ENOTSUPP if the controller does not support setting
1822 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001823 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001824int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001825{
1826 unsigned long flags;
1827 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001828 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001829 int offset;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001830
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001831 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001832 pr_warn("%s: invalid GPIO\n", __func__);
1833 return -EINVAL;
1834 }
1835
Felipe Balbic4b5be92010-05-26 14:42:23 -07001836 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001837 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001838 gpiod_dbg(desc,
1839 "%s: missing set() or set_debounce() operations\n",
1840 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001841 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001842 }
1843
1844 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001845
1846 status = gpio_ensure_requested(desc);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001847 if (status < 0)
1848 goto fail;
1849
1850 /* now we know the gpio is valid and chip won't vanish */
1851
1852 spin_unlock_irqrestore(&gpio_lock, flags);
1853
David Brownell9c4ba942010-08-10 18:02:24 -07001854 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001855
Alexandre Courbot372e7222013-02-03 01:29:29 +09001856 offset = gpio_chip_hwgpio(desc);
1857 return chip->set_debounce(chip, offset, debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001858
1859fail:
1860 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001861 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001862 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001863
1864 return status;
1865}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001866EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001867
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001868/**
1869 * gpiod_is_active_low - test whether a GPIO is active-low or not
1870 * @desc: the gpio descriptor to test
1871 *
1872 * Returns 1 if the GPIO is active-low, 0 otherwise.
1873 */
1874int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001875{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001876 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001877}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001878EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001879
1880/* I/O calls are only valid after configuration completed; the relevant
1881 * "is this a valid GPIO" error checks should already have been done.
1882 *
1883 * "Get" operations are often inlinable as reading a pin value register,
1884 * and masking the relevant bit in that register.
1885 *
1886 * When "set" operations are inlinable, they involve writing that mask to
1887 * one register to set a low value, or a different register to set it high.
1888 * Otherwise locking is needed, so there may be little value to inlining.
1889 *
1890 *------------------------------------------------------------------------
1891 *
1892 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1893 * have requested the GPIO. That can include implicit requesting by
1894 * a direction setting call. Marking a gpio as requested locks its chip
1895 * in memory, guaranteeing that these table lookups need no more locking
1896 * and that gpiochip_remove() will fail.
1897 *
1898 * REVISIT when debugging, consider adding some instrumentation to ensure
1899 * that the GPIO was actually requested.
1900 */
1901
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001902static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001903{
1904 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001905 int value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001906 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001907
Alexandre Courbot372e7222013-02-03 01:29:29 +09001908 chip = desc->chip;
1909 offset = gpio_chip_hwgpio(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001910 value = chip->get ? chip->get(chip, offset) : 0;
1911 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001912 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001913}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001914
David Brownelld2876d02008-02-04 22:28:20 -08001915/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001916 * gpiod_get_raw_value() - return a gpio's raw value
1917 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001918 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001919 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1920 * its ACTIVE_LOW status.
1921 *
1922 * This function should be called from contexts where we cannot sleep, and will
1923 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001924 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001925int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001926{
David Brownelld2876d02008-02-04 22:28:20 -08001927 if (!desc)
1928 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001929 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001930 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001931 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001932}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001933EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001934
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001935/**
1936 * gpiod_get_value() - return a gpio's value
1937 * @desc: gpio whose value will be returned
1938 *
1939 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1940 * account.
1941 *
1942 * This function should be called from contexts where we cannot sleep, and will
1943 * complain if the GPIO chip functions potentially sleep.
1944 */
1945int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001946{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001947 int value;
1948 if (!desc)
1949 return 0;
1950 /* Should be using gpio_get_value_cansleep() */
1951 WARN_ON(desc->chip->can_sleep);
1952
1953 value = _gpiod_get_raw_value(desc);
1954 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1955 value = !value;
1956
1957 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001958}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001959EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001960
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301961/*
1962 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001963 * @desc: gpio descriptor whose state need to be set.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301964 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1965 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001966static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301967{
1968 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001969 struct gpio_chip *chip = desc->chip;
1970 int offset = gpio_chip_hwgpio(desc);
1971
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301972 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001973 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301974 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001975 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301976 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001977 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301978 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001979 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301980 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001981 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301982 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001983 gpiod_err(desc,
1984 "%s: Error in set_value for open drain err %d\n",
1985 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301986}
1987
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301988/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001989 * _gpio_set_open_source_value() - Set the open source gpio's value.
1990 * @desc: gpio descriptor whose state need to be set.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301991 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1992 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001993static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301994{
1995 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001996 struct gpio_chip *chip = desc->chip;
1997 int offset = gpio_chip_hwgpio(desc);
1998
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301999 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002000 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302001 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002002 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302003 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002004 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302005 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002006 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302007 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002008 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302009 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002010 gpiod_err(desc,
2011 "%s: Error in set_value for open source err %d\n",
2012 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302013}
2014
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002015static void _gpiod_set_raw_value(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002016{
2017 struct gpio_chip *chip;
2018
Alexandre Courbot372e7222013-02-03 01:29:29 +09002019 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002020 trace_gpio_value(desc_to_gpio(desc), 0, value);
2021 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2022 _gpio_set_open_drain_value(desc, value);
2023 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2024 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302025 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002026 chip->set(chip, gpio_chip_hwgpio(desc), value);
2027}
2028
David Brownelld2876d02008-02-04 22:28:20 -08002029/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002030 * gpiod_set_raw_value() - assign a gpio's raw value
2031 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002032 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002033 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002034 * Set the raw value of the GPIO, i.e. the value of its physical line without
2035 * regard for its ACTIVE_LOW status.
2036 *
2037 * This function should be called from contexts where we cannot sleep, and will
2038 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002039 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002040void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002041{
David Brownelld2876d02008-02-04 22:28:20 -08002042 if (!desc)
2043 return;
David Brownelld2876d02008-02-04 22:28:20 -08002044 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09002045 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002046 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002047}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002048EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002049
2050/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002051 * gpiod_set_value() - assign a gpio's value
2052 * @desc: gpio whose value will be assigned
2053 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002054 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002055 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2056 * account
2057 *
2058 * This function should be called from contexts where we cannot sleep, and will
2059 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002060 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002061void gpiod_set_value(struct gpio_desc *desc, int value)
2062{
2063 if (!desc)
2064 return;
2065 /* Should be using gpio_set_value_cansleep() */
2066 WARN_ON(desc->chip->can_sleep);
2067 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2068 value = !value;
2069 _gpiod_set_raw_value(desc, value);
2070}
2071EXPORT_SYMBOL_GPL(gpiod_set_value);
2072
2073/**
2074 * gpiod_cansleep() - report whether gpio value access may sleep
2075 * @desc: gpio to check
2076 *
2077 */
2078int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002079{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002080 if (!desc)
2081 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002082 return desc->chip->can_sleep;
2083}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002084EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002085
David Brownell0f6d5042008-10-15 22:03:14 -07002086/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002087 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2088 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002089 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002090 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2091 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002092 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002093int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002094{
2095 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002096 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002097
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002098 if (!desc)
2099 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002100 chip = desc->chip;
2101 offset = gpio_chip_hwgpio(desc);
2102 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
2103}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002104EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002105
Linus Walleijd468bf92013-09-24 11:54:38 +02002106/**
2107 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
2108 * @gpio: the GPIO line to lock as used for IRQ
2109 *
2110 * This is used directly by GPIO drivers that want to lock down
2111 * a certain GPIO line to be used as IRQs, for example in the
2112 * .to_irq() callback of their gpio_chip, or in the .irq_enable()
2113 * of its irq_chip implementation if the GPIO is known from that
2114 * code.
David Brownelld2876d02008-02-04 22:28:20 -08002115 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002116int gpiod_lock_as_irq(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002117{
Linus Walleijd468bf92013-09-24 11:54:38 +02002118 if (!desc)
2119 return -EINVAL;
2120
2121 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
2122 gpiod_err(desc,
2123 "%s: tried to flag a GPIO set as output for IRQ\n",
2124 __func__);
2125 return -EIO;
2126 }
2127
2128 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
2129 return 0;
2130}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002131EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002132
2133int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
2134{
2135 return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset));
2136}
2137EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
2138
2139/**
2140 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
2141 * @gpio: the GPIO line to unlock from IRQ usage
2142 *
2143 * This is used directly by GPIO drivers that want to indicate
2144 * that a certain GPIO is no longer used exclusively for IRQ.
2145 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002146void gpiod_unlock_as_irq(struct gpio_desc *desc)
Linus Walleijd468bf92013-09-24 11:54:38 +02002147{
2148 if (!desc)
2149 return;
2150
2151 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2152}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002153EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002154
2155void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2156{
2157 return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset));
2158}
2159EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
David Brownelld2876d02008-02-04 22:28:20 -08002160
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002161/**
2162 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2163 * @desc: gpio whose value will be returned
2164 *
2165 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2166 * its ACTIVE_LOW status.
2167 *
2168 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002169 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002170int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002171{
David Brownelld2876d02008-02-04 22:28:20 -08002172 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002173 if (!desc)
2174 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002175 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002176}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002177EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002178
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002179/**
2180 * gpiod_get_value_cansleep() - return a gpio's value
2181 * @desc: gpio whose value will be returned
2182 *
2183 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2184 * account.
2185 *
2186 * This function is to be called from contexts that can sleep.
2187 */
2188int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002189{
David Brownelld2876d02008-02-04 22:28:20 -08002190 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002191
2192 might_sleep_if(extra_checks);
2193 if (!desc)
2194 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002195
2196 value = _gpiod_get_raw_value(desc);
2197 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2198 value = !value;
2199
David Brownelld2876d02008-02-04 22:28:20 -08002200 return value;
2201}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002202EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002203
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002204/**
2205 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2206 * @desc: gpio whose value will be assigned
2207 * @value: value to assign
2208 *
2209 * Set the raw value of the GPIO, i.e. the value of its physical line without
2210 * regard for its ACTIVE_LOW status.
2211 *
2212 * This function is to be called from contexts that can sleep.
2213 */
2214void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002215{
David Brownelld2876d02008-02-04 22:28:20 -08002216 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002217 if (!desc)
2218 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002219 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002220}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002221EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002222
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002223/**
2224 * gpiod_set_value_cansleep() - assign a gpio's value
2225 * @desc: gpio whose value will be assigned
2226 * @value: value to assign
2227 *
2228 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2229 * account
2230 *
2231 * This function is to be called from contexts that can sleep.
2232 */
2233void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002234{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002235 might_sleep_if(extra_checks);
2236 if (!desc)
2237 return;
2238
2239 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2240 value = !value;
2241 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002242}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002243EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002244
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002245/**
2246 * gpiod_add_table() - register GPIO device consumers
2247 * @table: array of consumers to register
2248 * @num: number of consumers in table
2249 */
2250void gpiod_add_table(struct gpiod_lookup *table, size_t size)
2251{
2252 mutex_lock(&gpio_lookup_lock);
2253
2254 while (size--) {
2255 list_add_tail(&table->list, &gpio_lookup_list);
2256 table++;
2257 }
2258
2259 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002260}
2261
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002262/*
2263 * Caller must have a acquired gpio_lookup_lock
2264 */
2265static struct gpio_chip *find_chip_by_name(const char *name)
David Brownelld8f388d82008-07-25 01:46:07 -07002266{
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002267 struct gpio_chip *chip = NULL;
2268
2269 list_for_each_entry(chip, &gpio_lookup_list, list) {
2270 if (chip->label == NULL)
2271 continue;
2272 if (!strcmp(chip->label, name))
2273 break;
2274 }
2275
2276 return chip;
David Brownelld2876d02008-02-04 22:28:20 -08002277}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002278
2279#ifdef CONFIG_OF
2280static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
2281 unsigned int idx, unsigned long *flags)
2282{
2283 char prop_name[32]; /* 32 is max size of property name */
2284 enum of_gpio_flags of_flags;
2285 struct gpio_desc *desc;
2286
2287 if (con_id)
2288 snprintf(prop_name, 32, "%s-gpios", con_id);
2289 else
2290 snprintf(prop_name, 32, "gpios");
2291
2292 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2293 &of_flags);
2294
2295 if (IS_ERR(desc))
2296 return desc;
2297
2298 if (of_flags & OF_GPIO_ACTIVE_LOW)
2299 *flags |= GPIOF_ACTIVE_LOW;
2300
2301 return desc;
2302}
2303#else
2304static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
2305 unsigned int idx, unsigned long *flags)
2306{
2307 return ERR_PTR(-ENODEV);
2308}
2309#endif
2310
Mika Westerberg81f59e92013-10-10 11:01:09 +03002311static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
2312 unsigned int idx, unsigned long *flags)
2313{
Mika Westerberge01f4402013-10-10 11:01:10 +03002314 struct acpi_gpio_info info;
2315 struct gpio_desc *desc;
2316
2317 desc = acpi_get_gpiod_by_index(dev, idx, &info);
2318 if (IS_ERR(desc))
2319 return desc;
2320
2321 if (info.gpioint && info.active_low)
2322 *flags |= GPIOF_ACTIVE_LOW;
2323
2324 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002325}
2326
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002327static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
2328 unsigned int idx, unsigned long *flags)
2329{
2330 const char *dev_id = dev ? dev_name(dev) : NULL;
2331 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2332 unsigned int match, best = 0;
2333 struct gpiod_lookup *p;
2334
2335 mutex_lock(&gpio_lookup_lock);
2336
2337 list_for_each_entry(p, &gpio_lookup_list, list) {
2338 match = 0;
2339
2340 if (p->dev_id) {
2341 if (!dev_id || strcmp(p->dev_id, dev_id))
2342 continue;
2343
2344 match += 2;
2345 }
2346
2347 if (p->con_id) {
2348 if (!con_id || strcmp(p->con_id, con_id))
2349 continue;
2350
2351 match += 1;
2352 }
2353
2354 if (p->idx != idx)
2355 continue;
2356
2357 if (match > best) {
2358 struct gpio_chip *chip;
2359
2360 chip = find_chip_by_name(p->chip_label);
2361
2362 if (!chip) {
2363 dev_warn(dev, "cannot find GPIO chip %s\n",
2364 p->chip_label);
2365 continue;
2366 }
2367
2368 if (chip->ngpio >= p->chip_hwnum) {
2369 dev_warn(dev, "GPIO chip %s has %d GPIOs\n",
2370 chip->label, chip->ngpio);
2371 continue;
2372 }
2373
2374 desc = gpio_to_desc(chip->base + p->chip_hwnum);
2375 *flags = p->flags;
2376
2377 if (match != 3)
2378 best = match;
2379 else
2380 break;
2381 }
2382 }
2383
2384 mutex_unlock(&gpio_lookup_lock);
2385
2386 return desc;
2387}
2388
2389/**
2390 * gpio_get - obtain a GPIO for a given GPIO function
2391 * @dev: GPIO consumer
2392 * @con_id: function within the GPIO consumer
2393 *
2394 * Return the GPIO descriptor corresponding to the function con_id of device
2395 * dev, or an IS_ERR() condition if an error occured.
2396 */
2397struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id)
2398{
2399 return gpiod_get_index(dev, con_id, 0);
2400}
2401EXPORT_SYMBOL_GPL(gpiod_get);
2402
2403/**
2404 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2405 * @dev: GPIO consumer
2406 * @con_id: function within the GPIO consumer
2407 * @idx: index of the GPIO to obtain in the consumer
2408 *
2409 * This variant of gpiod_get() allows to access GPIOs other than the first
2410 * defined one for functions that define several GPIOs.
2411 *
2412 * Return a valid GPIO descriptor, or an IS_ERR() condition in case of error.
2413 */
2414struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2415 const char *con_id,
2416 unsigned int idx)
2417{
2418 struct gpio_desc *desc;
2419 int status;
2420 unsigned long flags = 0;
2421
2422 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2423
2424 /* Using device tree? */
2425 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
2426 dev_dbg(dev, "using device tree for GPIO lookup\n");
2427 desc = of_find_gpio(dev, con_id, idx, &flags);
Mika Westerberg81f59e92013-10-10 11:01:09 +03002428 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
2429 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2430 desc = acpi_find_gpio(dev, con_id, idx, &flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002431 } else {
2432 dev_dbg(dev, "using lookup tables for GPIO lookup");
2433 desc = gpiod_find(dev, con_id, idx, &flags);
2434 }
2435
2436 if (IS_ERR(desc)) {
2437 dev_warn(dev, "lookup for GPIO %s failed\n", con_id);
2438 return desc;
2439 }
2440
2441 status = gpiod_request(desc, con_id);
2442
2443 if (status < 0)
2444 return ERR_PTR(status);
2445
2446 if (flags & GPIOF_ACTIVE_LOW)
2447 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2448
2449 return desc;
2450}
2451EXPORT_SYMBOL_GPL(gpiod_get_index);
2452
2453/**
2454 * gpiod_put - dispose of a GPIO descriptor
2455 * @desc: GPIO descriptor to dispose of
2456 *
2457 * No descriptor can be used after gpiod_put() has been called on it.
2458 */
2459void gpiod_put(struct gpio_desc *desc)
2460{
2461 gpiod_free(desc);
2462}
2463EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002464
David Brownelld2876d02008-02-04 22:28:20 -08002465#ifdef CONFIG_DEBUG_FS
2466
2467static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2468{
2469 unsigned i;
2470 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002471 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002472 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002473 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002474
2475 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2476 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2477 continue;
2478
Alexandre Courbot372e7222013-02-03 01:29:29 +09002479 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002480 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002481 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2482 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002483 gpio, gdesc->label,
2484 is_out ? "out" : "in ",
2485 chip->get
2486 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002487 : "? ",
2488 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002489 seq_printf(s, "\n");
2490 }
2491}
2492
Thierry Redingf9c4a312012-04-12 13:26:01 +02002493static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002494{
Grant Likely362432a2013-02-09 09:41:49 +00002495 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002496 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002497 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002498
2499 s->private = "";
2500
Grant Likely362432a2013-02-09 09:41:49 +00002501 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002502 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002503 if (index-- == 0) {
2504 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002505 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002506 }
2507 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002508
2509 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002510}
2511
2512static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2513{
Grant Likely362432a2013-02-09 09:41:49 +00002514 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002515 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002516 void *ret = NULL;
2517
Grant Likely362432a2013-02-09 09:41:49 +00002518 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002519 if (list_is_last(&chip->list, &gpio_chips))
2520 ret = NULL;
2521 else
2522 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002523 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002524
2525 s->private = "\n";
2526 ++*pos;
2527
2528 return ret;
2529}
2530
2531static void gpiolib_seq_stop(struct seq_file *s, void *v)
2532{
2533}
2534
2535static int gpiolib_seq_show(struct seq_file *s, void *v)
2536{
2537 struct gpio_chip *chip = v;
2538 struct device *dev;
2539
2540 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2541 chip->base, chip->base + chip->ngpio - 1);
2542 dev = chip->dev;
2543 if (dev)
2544 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2545 dev_name(dev));
2546 if (chip->label)
2547 seq_printf(s, ", %s", chip->label);
2548 if (chip->can_sleep)
2549 seq_printf(s, ", can sleep");
2550 seq_printf(s, ":\n");
2551
2552 if (chip->dbg_show)
2553 chip->dbg_show(s, chip);
2554 else
2555 gpiolib_dbg_show(s, chip);
2556
David Brownelld2876d02008-02-04 22:28:20 -08002557 return 0;
2558}
2559
Thierry Redingf9c4a312012-04-12 13:26:01 +02002560static const struct seq_operations gpiolib_seq_ops = {
2561 .start = gpiolib_seq_start,
2562 .next = gpiolib_seq_next,
2563 .stop = gpiolib_seq_stop,
2564 .show = gpiolib_seq_show,
2565};
2566
David Brownelld2876d02008-02-04 22:28:20 -08002567static int gpiolib_open(struct inode *inode, struct file *file)
2568{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002569 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002570}
2571
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002572static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002573 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002574 .open = gpiolib_open,
2575 .read = seq_read,
2576 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002577 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002578};
2579
2580static int __init gpiolib_debugfs_init(void)
2581{
2582 /* /sys/kernel/debug/gpio */
2583 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2584 NULL, NULL, &gpiolib_operations);
2585 return 0;
2586}
2587subsys_initcall(gpiolib_debugfs_init);
2588
2589#endif /* DEBUG_FS */