blob: 490198365ce47810ae75c558cb753acb8cc2a7e8 [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>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010016#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090017#include <linux/gpio/driver.h>
David Brownelld2876d02008-02-04 22:28:20 -080018
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060019#define CREATE_TRACE_POINTS
20#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080021
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070022/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080023 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070024 * The GPIO programming interface allows for inlining speed-critical
25 * get/set operations for common cases, so that access to SOC-integrated
26 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080027 */
28
29
30/* When debugging, extend minimal trust to callers and platform code.
31 * Also emit diagnostic messages that may help initial bringup, when
32 * board setup or driver bugs are most common.
33 *
34 * Otherwise, minimize overhead in what may be bitbanging codepaths.
35 */
36#ifdef DEBUG
37#define extra_checks 1
38#else
39#define extra_checks 0
40#endif
41
42/* gpio_lock prevents conflicts during gpio_desc[] table updates.
43 * While any GPIO is requested, its gpio_chip is not removable;
44 * each GPIO's "requested" flag serves as a lock and refcount.
45 */
46static DEFINE_SPINLOCK(gpio_lock);
47
48struct gpio_desc {
49 struct gpio_chip *chip;
50 unsigned long flags;
51/* flag symbols are bit numbers */
52#define FLAG_REQUESTED 0
53#define FLAG_IS_OUT 1
Alexandre Courbot710b40e2013-02-02 23:44:06 +090054#define FLAG_EXPORT 2 /* protected by sysfs_lock */
55#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
56#define FLAG_TRIG_FALL 4 /* trigger on falling edge */
57#define FLAG_TRIG_RISE 5 /* trigger on rising edge */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070058#define FLAG_ACTIVE_LOW 6 /* value has active low */
Alexandre Courbot710b40e2013-02-02 23:44:06 +090059#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
60#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
Linus Walleijd468bf92013-09-24 11:54:38 +020061#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070062
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070063#define ID_SHIFT 16 /* add new flags before this one */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070064
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070065#define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
Daniel Glöcknerff77c352009-09-22 16:46:38 -070066#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
David Brownelld2876d02008-02-04 22:28:20 -080067
68#ifdef CONFIG_DEBUG_FS
69 const char *label;
70#endif
71};
72static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
73
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +090074#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
75
Alexandre Courbotbae48da2013-10-17 10:21:38 -070076static DEFINE_MUTEX(gpio_lookup_lock);
77static LIST_HEAD(gpio_lookup_list);
Alexandre Courbot1a989d02013-02-03 01:29:24 +090078static LIST_HEAD(gpio_chips);
79
Daniel Glöcknerff77c352009-09-22 16:46:38 -070080#ifdef CONFIG_GPIO_SYSFS
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070081static DEFINE_IDR(dirent_idr);
Daniel Glöcknerff77c352009-09-22 16:46:38 -070082#endif
83
Alexandre Courbot372e7222013-02-03 01:29:29 +090084static int gpiod_request(struct gpio_desc *desc, const char *label);
85static void gpiod_free(struct gpio_desc *desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090086
Mark Brown7b17b592013-09-09 10:33:50 +010087#ifdef CONFIG_DEBUG_FS
88#define gpiod_emerg(desc, fmt, ...) \
89 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
90 ##__VA_ARGS__)
91#define gpiod_crit(desc, fmt, ...) \
92 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
93 ##__VA_ARGS__)
94#define gpiod_err(desc, fmt, ...) \
95 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
96 ##__VA_ARGS__)
97#define gpiod_warn(desc, fmt, ...) \
98 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
99 ##__VA_ARGS__)
100#define gpiod_info(desc, fmt, ...) \
101 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
102 ##__VA_ARGS__)
103#define gpiod_dbg(desc, fmt, ...) \
104 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
105 ##__VA_ARGS__)
106#else
Mark Brown6424de52013-09-09 10:33:49 +0100107#define gpiod_emerg(desc, fmt, ...) \
108 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
109#define gpiod_crit(desc, fmt, ...) \
110 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
111#define gpiod_err(desc, fmt, ...) \
112 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
113#define gpiod_warn(desc, fmt, ...) \
114 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
115#define gpiod_info(desc, fmt, ...) \
116 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
117#define gpiod_dbg(desc, fmt, ...) \
118 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Mark Brown7b17b592013-09-09 10:33:50 +0100119#endif
Alexandre Courbot372e7222013-02-03 01:29:29 +0900120
David Brownelld2876d02008-02-04 22:28:20 -0800121static inline void desc_set_label(struct gpio_desc *d, const char *label)
122{
123#ifdef CONFIG_DEBUG_FS
124 d->label = label;
125#endif
126}
127
Alexandre Courbot372e7222013-02-03 01:29:29 +0900128/*
129 * Return the GPIO number of the passed descriptor relative to its chip
130 */
131static int gpio_chip_hwgpio(const struct gpio_desc *desc)
132{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900133 return desc - &desc->chip->desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900134}
135
136/**
137 * Convert a GPIO number to its descriptor
138 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700139struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900140{
141 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
142 return NULL;
143 else
144 return &gpio_desc[gpio];
145}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700146EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900147
148/**
Linus Walleijd468bf92013-09-24 11:54:38 +0200149 * Convert an offset on a certain chip to a corresponding descriptor
150 */
151static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip,
152 unsigned int offset)
153{
154 unsigned int gpio = chip->base + offset;
155
156 return gpio_to_desc(gpio);
157}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900158
159/**
160 * Convert a GPIO descriptor to the integer namespace.
161 * This should disappear in the future but is needed since we still
162 * use GPIO numbers for error messages and sysfs nodes
163 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700164int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900165{
Alexandre Courbot8c0fca82013-10-04 10:59:57 -0700166 return desc - &gpio_desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900167}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700168EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900169
170
David Brownelld2876d02008-02-04 22:28:20 -0800171/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
172 * when setting direction, and otherwise illegal. Until board setup code
173 * and drivers use explicit requests everywhere (which won't happen when
174 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -0700175 * message should motivate switching to explicit requests... so should
176 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -0700177 *
178 * NOTE: the autorequest mechanism is going away; at this point it's
179 * only "legal" in the sense that (old) code using it won't break yet,
180 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -0800181 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900182static int gpio_ensure_requested(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800183{
David Brownell8a0cecf2009-04-02 16:57:06 -0700184 const struct gpio_chip *chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900185 const int gpio = desc_to_gpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -0700186
David Brownell8a0cecf2009-04-02 16:57:06 -0700187 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
188 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700189 if (!try_module_get(chip->owner)) {
190 pr_err("GPIO-%d: module can't be gotten \n", gpio);
191 clear_bit(FLAG_REQUESTED, &desc->flags);
192 /* lose */
193 return -EIO;
194 }
David Brownelld2876d02008-02-04 22:28:20 -0800195 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700196 /* caller must chip->request() w/o spinlock */
197 if (chip->request)
198 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800199 }
David Brownell35e8bb52008-10-15 22:03:16 -0700200 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800201}
202
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700203/**
204 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
205 * @desc: descriptor to return the chip of
206 */
207struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900208{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900209 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900210}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700211EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800212
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700213/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
214static int gpiochip_find_base(int ngpio)
215{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900216 struct gpio_chip *chip;
217 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700218
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900219 list_for_each_entry_reverse(chip, &gpio_chips, list) {
220 /* found a free space? */
221 if (chip->base + chip->ngpio <= base)
222 break;
223 else
224 /* nope, check the space right before the chip */
225 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700226 }
227
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900228 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700229 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900230 return base;
231 } else {
232 pr_err("%s: cannot find free range\n", __func__);
233 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700234 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700235}
236
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700237/**
238 * gpiod_get_direction - return the current direction of a GPIO
239 * @desc: GPIO to get the direction of
240 *
241 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
242 *
243 * This function may sleep if gpiod_cansleep() is true.
244 */
245int gpiod_get_direction(const struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300246{
247 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900248 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300249 int status = -EINVAL;
250
Alexandre Courbot372e7222013-02-03 01:29:29 +0900251 chip = gpiod_to_chip(desc);
252 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300253
254 if (!chip->get_direction)
255 return status;
256
Alexandre Courbot372e7222013-02-03 01:29:29 +0900257 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300258 if (status > 0) {
259 /* GPIOF_DIR_IN, or other positive */
260 status = 1;
Alexandre Courbotdef63432013-02-15 14:46:15 +0900261 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
262 * so it does not affect constness per se */
263 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300264 }
265 if (status == 0) {
266 /* GPIOF_DIR_OUT */
Alexandre Courbotdef63432013-02-15 14:46:15 +0900267 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300268 }
269 return status;
270}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700271EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300272
David Brownelld8f388d82008-07-25 01:46:07 -0700273#ifdef CONFIG_GPIO_SYSFS
274
275/* lock protects against unexport_gpio() being called while
276 * sysfs files are active.
277 */
278static DEFINE_MUTEX(sysfs_lock);
279
280/*
281 * /sys/class/gpio/gpioN... only for GPIOs that are exported
282 * /direction
283 * * MAY BE OMITTED if kernel won't allow direction changes
284 * * is read/write as "in" or "out"
285 * * may also be written as "high" or "low", initializing
286 * output value as specified ("out" implies "low")
287 * /value
288 * * always readable, subject to hardware behavior
289 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700290 * /edge
291 * * configures behavior of poll(2) on /value
292 * * available only if pin can generate IRQs on input
293 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800294 * /active_low
295 * * configures polarity of /value
296 * * is read/write as zero/nonzero
297 * * also affects existing and subsequent "falling" and "rising"
298 * /edge configuration
David Brownelld8f388d82008-07-25 01:46:07 -0700299 */
300
301static ssize_t gpio_direction_show(struct device *dev,
302 struct device_attribute *attr, char *buf)
303{
Alexandre Courbotdef63432013-02-15 14:46:15 +0900304 const struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700305 ssize_t status;
306
307 mutex_lock(&sysfs_lock);
308
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900309 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700310 status = -EIO;
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900311 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +0900312 gpiod_get_direction(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700313 status = sprintf(buf, "%s\n",
314 test_bit(FLAG_IS_OUT, &desc->flags)
315 ? "out" : "in");
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900316 }
David Brownelld8f388d82008-07-25 01:46:07 -0700317
318 mutex_unlock(&sysfs_lock);
319 return status;
320}
321
322static ssize_t gpio_direction_store(struct device *dev,
323 struct device_attribute *attr, const char *buf, size_t size)
324{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900325 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700326 ssize_t status;
327
328 mutex_lock(&sysfs_lock);
329
330 if (!test_bit(FLAG_EXPORT, &desc->flags))
331 status = -EIO;
332 else if (sysfs_streq(buf, "high"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900333 status = gpiod_direction_output(desc, 1);
David Brownelld8f388d82008-07-25 01:46:07 -0700334 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900335 status = gpiod_direction_output(desc, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700336 else if (sysfs_streq(buf, "in"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900337 status = gpiod_direction_input(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700338 else
339 status = -EINVAL;
340
341 mutex_unlock(&sysfs_lock);
342 return status ? : size;
343}
344
Jani Nikula07697462009-12-15 16:46:20 -0800345static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700346 gpio_direction_show, gpio_direction_store);
347
348static ssize_t gpio_value_show(struct device *dev,
349 struct device_attribute *attr, char *buf)
350{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900351 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700352 ssize_t status;
353
354 mutex_lock(&sysfs_lock);
355
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700356 if (!test_bit(FLAG_EXPORT, &desc->flags))
David Brownelld8f388d82008-07-25 01:46:07 -0700357 status = -EIO;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700358 else
359 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
David Brownelld8f388d82008-07-25 01:46:07 -0700360
361 mutex_unlock(&sysfs_lock);
362 return status;
363}
364
365static ssize_t gpio_value_store(struct device *dev,
366 struct device_attribute *attr, const char *buf, size_t size)
367{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900368 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700369 ssize_t status;
370
371 mutex_lock(&sysfs_lock);
372
373 if (!test_bit(FLAG_EXPORT, &desc->flags))
374 status = -EIO;
375 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
376 status = -EPERM;
377 else {
378 long value;
379
Jingoo Hana3d88c92013-07-19 16:12:50 +0900380 status = kstrtol(buf, 0, &value);
David Brownelld8f388d82008-07-25 01:46:07 -0700381 if (status == 0) {
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700382 gpiod_set_value_cansleep(desc, value);
David Brownelld8f388d82008-07-25 01:46:07 -0700383 status = size;
384 }
385 }
386
387 mutex_unlock(&sysfs_lock);
388 return status;
389}
390
Jani Nikula07697462009-12-15 16:46:20 -0800391static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700392 gpio_value_show, gpio_value_store);
393
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700394static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
395{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700396 struct sysfs_dirent *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700397
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700398 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700399 return IRQ_HANDLED;
400}
401
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700402static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
403 unsigned long gpio_flags)
404{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700405 struct sysfs_dirent *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700406 unsigned long irq_flags;
407 int ret, irq, id;
408
409 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
410 return 0;
411
Alexandre Courbot372e7222013-02-03 01:29:29 +0900412 irq = gpiod_to_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700413 if (irq < 0)
414 return -EIO;
415
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700416 id = desc->flags >> ID_SHIFT;
417 value_sd = idr_find(&dirent_idr, id);
418 if (value_sd)
419 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700420
421 desc->flags &= ~GPIO_TRIGGER_MASK;
422
423 if (!gpio_flags) {
Linus Walleijd468bf92013-09-24 11:54:38 +0200424 gpiod_unlock_as_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700425 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700426 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700427 }
428
429 irq_flags = IRQF_SHARED;
430 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800431 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
432 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700433 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800434 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
435 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700436
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700437 if (!value_sd) {
Tejun Heo388975c2013-09-11 23:19:13 -0400438 value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700439 if (!value_sd) {
440 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700441 goto err_out;
442 }
443
Tejun Heo62f516b2013-02-27 17:04:06 -0800444 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
445 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700446 goto free_sd;
Tejun Heo62f516b2013-02-27 17:04:06 -0800447 id = ret;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700448
449 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700450 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700451
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700452 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700453 ret = -ERANGE;
454 goto free_id;
455 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700456 }
457
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700458 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700459 "gpiolib", value_sd);
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700460 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700461 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700462
Linus Walleijd468bf92013-09-24 11:54:38 +0200463 ret = gpiod_lock_as_irq(desc);
464 if (ret < 0) {
465 gpiod_warn(desc, "failed to flag the GPIO for IRQ\n");
466 goto free_id;
467 }
468
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700469 desc->flags |= gpio_flags;
470 return 0;
471
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700472free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700473 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700474 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700475free_sd:
476 if (value_sd)
477 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700478err_out:
479 return ret;
480}
481
482static const struct {
483 const char *name;
484 unsigned long flags;
485} trigger_types[] = {
486 { "none", 0 },
487 { "falling", BIT(FLAG_TRIG_FALL) },
488 { "rising", BIT(FLAG_TRIG_RISE) },
489 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
490};
491
492static ssize_t gpio_edge_show(struct device *dev,
493 struct device_attribute *attr, char *buf)
494{
495 const struct gpio_desc *desc = dev_get_drvdata(dev);
496 ssize_t status;
497
498 mutex_lock(&sysfs_lock);
499
500 if (!test_bit(FLAG_EXPORT, &desc->flags))
501 status = -EIO;
502 else {
503 int i;
504
505 status = 0;
506 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
507 if ((desc->flags & GPIO_TRIGGER_MASK)
508 == trigger_types[i].flags) {
509 status = sprintf(buf, "%s\n",
510 trigger_types[i].name);
511 break;
512 }
513 }
514
515 mutex_unlock(&sysfs_lock);
516 return status;
517}
518
519static ssize_t gpio_edge_store(struct device *dev,
520 struct device_attribute *attr, const char *buf, size_t size)
521{
522 struct gpio_desc *desc = dev_get_drvdata(dev);
523 ssize_t status;
524 int i;
525
526 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
527 if (sysfs_streq(trigger_types[i].name, buf))
528 goto found;
529 return -EINVAL;
530
531found:
532 mutex_lock(&sysfs_lock);
533
534 if (!test_bit(FLAG_EXPORT, &desc->flags))
535 status = -EIO;
536 else {
537 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
538 if (!status)
539 status = size;
540 }
541
542 mutex_unlock(&sysfs_lock);
543
544 return status;
545}
546
547static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
548
Jani Nikula07697462009-12-15 16:46:20 -0800549static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
550 int value)
551{
552 int status = 0;
553
554 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
555 return 0;
556
557 if (value)
558 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
559 else
560 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
561
562 /* reconfigure poll(2) support if enabled on one edge only */
563 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
564 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
565 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
566
567 gpio_setup_irq(desc, dev, 0);
568 status = gpio_setup_irq(desc, dev, trigger_flags);
569 }
570
571 return status;
572}
573
574static ssize_t gpio_active_low_show(struct device *dev,
575 struct device_attribute *attr, char *buf)
576{
577 const struct gpio_desc *desc = dev_get_drvdata(dev);
578 ssize_t status;
579
580 mutex_lock(&sysfs_lock);
581
582 if (!test_bit(FLAG_EXPORT, &desc->flags))
583 status = -EIO;
584 else
585 status = sprintf(buf, "%d\n",
586 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
587
588 mutex_unlock(&sysfs_lock);
589
590 return status;
591}
592
593static ssize_t gpio_active_low_store(struct device *dev,
594 struct device_attribute *attr, const char *buf, size_t size)
595{
596 struct gpio_desc *desc = dev_get_drvdata(dev);
597 ssize_t status;
598
599 mutex_lock(&sysfs_lock);
600
601 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
602 status = -EIO;
603 } else {
604 long value;
605
Jingoo Hana3d88c92013-07-19 16:12:50 +0900606 status = kstrtol(buf, 0, &value);
Jani Nikula07697462009-12-15 16:46:20 -0800607 if (status == 0)
608 status = sysfs_set_active_low(desc, dev, value != 0);
609 }
610
611 mutex_unlock(&sysfs_lock);
612
613 return status ? : size;
614}
615
616static const DEVICE_ATTR(active_low, 0644,
617 gpio_active_low_show, gpio_active_low_store);
618
David Brownelld8f388d82008-07-25 01:46:07 -0700619static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d82008-07-25 01:46:07 -0700620 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800621 &dev_attr_active_low.attr,
David Brownelld8f388d82008-07-25 01:46:07 -0700622 NULL,
623};
624
625static const struct attribute_group gpio_attr_group = {
626 .attrs = (struct attribute **) gpio_attrs,
627};
628
629/*
630 * /sys/class/gpio/gpiochipN/
631 * /base ... matching gpio_chip.base (N)
632 * /label ... matching gpio_chip.label
633 * /ngpio ... matching gpio_chip.ngpio
634 */
635
636static ssize_t chip_base_show(struct device *dev,
637 struct device_attribute *attr, char *buf)
638{
639 const struct gpio_chip *chip = dev_get_drvdata(dev);
640
641 return sprintf(buf, "%d\n", chip->base);
642}
643static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
644
645static ssize_t chip_label_show(struct device *dev,
646 struct device_attribute *attr, char *buf)
647{
648 const struct gpio_chip *chip = dev_get_drvdata(dev);
649
650 return sprintf(buf, "%s\n", chip->label ? : "");
651}
652static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
653
654static ssize_t chip_ngpio_show(struct device *dev,
655 struct device_attribute *attr, char *buf)
656{
657 const struct gpio_chip *chip = dev_get_drvdata(dev);
658
659 return sprintf(buf, "%u\n", chip->ngpio);
660}
661static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
662
663static const struct attribute *gpiochip_attrs[] = {
664 &dev_attr_base.attr,
665 &dev_attr_label.attr,
666 &dev_attr_ngpio.attr,
667 NULL,
668};
669
670static const struct attribute_group gpiochip_attr_group = {
671 .attrs = (struct attribute **) gpiochip_attrs,
672};
673
674/*
675 * /sys/class/gpio/export ... write-only
676 * integer N ... number of GPIO to export (full access)
677 * /sys/class/gpio/unexport ... write-only
678 * integer N ... number of GPIO to unexport
679 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100680static ssize_t export_store(struct class *class,
681 struct class_attribute *attr,
682 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700683{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900684 long gpio;
685 struct gpio_desc *desc;
686 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700687
Jingoo Hana3d88c92013-07-19 16:12:50 +0900688 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700689 if (status < 0)
690 goto done;
691
Alexandre Courbot372e7222013-02-03 01:29:29 +0900692 desc = gpio_to_desc(gpio);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900693 /* reject invalid GPIOs */
694 if (!desc) {
695 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
696 return -EINVAL;
697 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900698
David Brownelld8f388d82008-07-25 01:46:07 -0700699 /* No extra locking here; FLAG_SYSFS just signifies that the
700 * request and export were done by on behalf of userspace, so
701 * they may be undone on its behalf too.
702 */
703
Alexandre Courbot372e7222013-02-03 01:29:29 +0900704 status = gpiod_request(desc, "sysfs");
Mathias Nymanad2fab32012-10-25 14:03:03 +0300705 if (status < 0) {
706 if (status == -EPROBE_DEFER)
707 status = -ENODEV;
David Brownelld8f388d82008-07-25 01:46:07 -0700708 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +0300709 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900710 status = gpiod_export(desc, true);
David Brownelld8f388d82008-07-25 01:46:07 -0700711 if (status < 0)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900712 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700713 else
Alexandre Courbot372e7222013-02-03 01:29:29 +0900714 set_bit(FLAG_SYSFS, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700715
716done:
717 if (status)
718 pr_debug("%s: status %d\n", __func__, status);
719 return status ? : len;
720}
721
Andi Kleen28812fe2010-01-05 12:48:07 +0100722static ssize_t unexport_store(struct class *class,
723 struct class_attribute *attr,
724 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700725{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900726 long gpio;
727 struct gpio_desc *desc;
728 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700729
Jingoo Hana3d88c92013-07-19 16:12:50 +0900730 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700731 if (status < 0)
732 goto done;
733
Alexandre Courbot372e7222013-02-03 01:29:29 +0900734 desc = gpio_to_desc(gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700735 /* reject bogus commands (gpio_unexport ignores them) */
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900736 if (!desc) {
737 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
738 return -EINVAL;
739 }
740
741 status = -EINVAL;
David Brownelld8f388d82008-07-25 01:46:07 -0700742
743 /* No extra locking here; FLAG_SYSFS just signifies that the
744 * request and export were done by on behalf of userspace, so
745 * they may be undone on its behalf too.
746 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900747 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700748 status = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900749 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700750 }
751done:
752 if (status)
753 pr_debug("%s: status %d\n", __func__, status);
754 return status ? : len;
755}
756
757static struct class_attribute gpio_class_attrs[] = {
758 __ATTR(export, 0200, NULL, export_store),
759 __ATTR(unexport, 0200, NULL, unexport_store),
760 __ATTR_NULL,
761};
762
763static struct class gpio_class = {
764 .name = "gpio",
765 .owner = THIS_MODULE,
766
767 .class_attrs = gpio_class_attrs,
768};
769
770
771/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700772 * gpiod_export - export a GPIO through sysfs
David Brownelld8f388d82008-07-25 01:46:07 -0700773 * @gpio: gpio to make available, already requested
774 * @direction_may_change: true if userspace may change gpio direction
775 * Context: arch_initcall or later
776 *
777 * When drivers want to make a GPIO accessible to userspace after they
778 * have requested it -- perhaps while debugging, or as part of their
779 * public interface -- they may use this routine. If the GPIO can
780 * change direction (some can't) and the caller allows it, userspace
781 * will see "direction" sysfs attribute which may be used to change
782 * the gpio's direction. A "value" attribute will always be provided.
783 *
784 * Returns zero on success, else an error.
785 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700786int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
David Brownelld8f388d82008-07-25 01:46:07 -0700787{
788 unsigned long flags;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100789 int status;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700790 const char *ioname = NULL;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100791 struct device *dev;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900792 int offset;
David Brownelld8f388d82008-07-25 01:46:07 -0700793
794 /* can't export until sysfs is available ... */
795 if (!gpio_class.p) {
796 pr_debug("%s: called too early!\n", __func__);
797 return -ENOENT;
798 }
799
Alexandre Courbot372e7222013-02-03 01:29:29 +0900800 if (!desc) {
801 pr_debug("%s: invalid gpio descriptor\n", __func__);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100802 return -EINVAL;
803 }
David Brownelld8f388d82008-07-25 01:46:07 -0700804
805 mutex_lock(&sysfs_lock);
806
807 spin_lock_irqsave(&gpio_lock, flags);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100808 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
809 test_bit(FLAG_EXPORT, &desc->flags)) {
810 spin_unlock_irqrestore(&gpio_lock, flags);
811 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +0900812 __func__, desc_to_gpio(desc),
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100813 test_bit(FLAG_REQUESTED, &desc->flags),
814 test_bit(FLAG_EXPORT, &desc->flags));
Dan Carpenter529f2ad2012-10-26 09:59:43 +0300815 status = -EPERM;
816 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700817 }
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100818
819 if (!desc->chip->direction_input || !desc->chip->direction_output)
820 direction_may_change = false;
David Brownelld8f388d82008-07-25 01:46:07 -0700821 spin_unlock_irqrestore(&gpio_lock, flags);
822
Alexandre Courbot372e7222013-02-03 01:29:29 +0900823 offset = gpio_chip_hwgpio(desc);
824 if (desc->chip->names && desc->chip->names[offset])
825 ioname = desc->chip->names[offset];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700826
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100827 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Alexandre Courbot372e7222013-02-03 01:29:29 +0900828 desc, ioname ? ioname : "gpio%u",
829 desc_to_gpio(desc));
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100830 if (IS_ERR(dev)) {
831 status = PTR_ERR(dev);
832 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700833 }
834
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100835 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
David Brownelld8f388d82008-07-25 01:46:07 -0700836 if (status)
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100837 goto fail_unregister_device;
David Brownelld8f388d82008-07-25 01:46:07 -0700838
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100839 if (direction_may_change) {
840 status = device_create_file(dev, &dev_attr_direction);
841 if (status)
842 goto fail_unregister_device;
843 }
844
Alexandre Courbot372e7222013-02-03 01:29:29 +0900845 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100846 !test_bit(FLAG_IS_OUT, &desc->flags))) {
847 status = device_create_file(dev, &dev_attr_edge);
848 if (status)
849 goto fail_unregister_device;
850 }
851
852 set_bit(FLAG_EXPORT, &desc->flags);
853 mutex_unlock(&sysfs_lock);
854 return 0;
855
856fail_unregister_device:
857 device_unregister(dev);
858fail_unlock:
859 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900860 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
861 status);
David Brownelld8f388d82008-07-25 01:46:07 -0700862 return status;
863}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700864EXPORT_SYMBOL_GPL(gpiod_export);
David Brownelld8f388d82008-07-25 01:46:07 -0700865
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100866static int match_export(struct device *dev, const void *data)
David Brownelld8f388d82008-07-25 01:46:07 -0700867{
868 return dev_get_drvdata(dev) == data;
869}
870
871/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700872 * gpiod_export_link - create a sysfs link to an exported GPIO node
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700873 * @dev: device under which to create symlink
874 * @name: name of the symlink
875 * @gpio: gpio to create symlink to, already exported
876 *
877 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
878 * node. Caller is responsible for unlinking.
879 *
880 * Returns zero on success, else an error.
881 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700882int gpiod_export_link(struct device *dev, const char *name,
883 struct gpio_desc *desc)
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700884{
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700885 int status = -EINVAL;
886
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900887 if (!desc) {
888 pr_warn("%s: invalid GPIO\n", __func__);
889 return -EINVAL;
890 }
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700891
892 mutex_lock(&sysfs_lock);
893
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700894 if (test_bit(FLAG_EXPORT, &desc->flags)) {
895 struct device *tdev;
896
897 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
898 if (tdev != NULL) {
899 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
900 name);
901 } else {
902 status = -ENODEV;
903 }
904 }
905
906 mutex_unlock(&sysfs_lock);
907
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700908 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900909 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
910 status);
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700911
912 return status;
913}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700914EXPORT_SYMBOL_GPL(gpiod_export_link);
Jani Nikula07697462009-12-15 16:46:20 -0800915
916/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700917 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
Jani Nikula07697462009-12-15 16:46:20 -0800918 * @gpio: gpio to change
919 * @value: non-zero to use active low, i.e. inverted values
920 *
921 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
922 * The GPIO does not have to be exported yet. If poll(2) support has
923 * been enabled for either rising or falling edge, it will be
924 * reconfigured to follow the new polarity.
925 *
926 * Returns zero on success, else an error.
927 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700928int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
Jani Nikula07697462009-12-15 16:46:20 -0800929{
Jani Nikula07697462009-12-15 16:46:20 -0800930 struct device *dev = NULL;
931 int status = -EINVAL;
932
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900933 if (!desc) {
934 pr_warn("%s: invalid GPIO\n", __func__);
935 return -EINVAL;
936 }
Jani Nikula07697462009-12-15 16:46:20 -0800937
938 mutex_lock(&sysfs_lock);
939
Jani Nikula07697462009-12-15 16:46:20 -0800940 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800941 dev = class_find_device(&gpio_class, NULL, desc, match_export);
942 if (dev == NULL) {
943 status = -ENODEV;
944 goto unlock;
945 }
946 }
947
948 status = sysfs_set_active_low(desc, dev, value);
949
950unlock:
951 mutex_unlock(&sysfs_lock);
952
Jani Nikula07697462009-12-15 16:46:20 -0800953 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900954 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
955 status);
Jani Nikula07697462009-12-15 16:46:20 -0800956
957 return status;
958}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700959EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
Jani Nikula07697462009-12-15 16:46:20 -0800960
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700961/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700962 * gpiod_unexport - reverse effect of gpio_export()
David Brownelld8f388d82008-07-25 01:46:07 -0700963 * @gpio: gpio to make unavailable
964 *
965 * This is implicit on gpio_free().
966 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700967void gpiod_unexport(struct gpio_desc *desc)
David Brownelld8f388d82008-07-25 01:46:07 -0700968{
Jon Povey6a99ad42010-07-27 13:18:06 -0700969 int status = 0;
Ming Lei864533c2012-02-13 22:53:20 +0800970 struct device *dev = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -0700971
Alexandre Courbot372e7222013-02-03 01:29:29 +0900972 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900973 pr_warn("%s: invalid GPIO\n", __func__);
974 return;
Jon Povey6a99ad42010-07-27 13:18:06 -0700975 }
David Brownelld8f388d82008-07-25 01:46:07 -0700976
977 mutex_lock(&sysfs_lock);
978
David Brownelld8f388d82008-07-25 01:46:07 -0700979 if (test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700980
981 dev = class_find_device(&gpio_class, NULL, desc, match_export);
982 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700983 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700984 clear_bit(FLAG_EXPORT, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700985 } else
986 status = -ENODEV;
987 }
988
989 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900990
Ming Lei864533c2012-02-13 22:53:20 +0800991 if (dev) {
992 device_unregister(dev);
993 put_device(dev);
994 }
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900995
David Brownelld8f388d82008-07-25 01:46:07 -0700996 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900997 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
998 status);
999}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001000EXPORT_SYMBOL_GPL(gpiod_unexport);
David Brownelld8f388d82008-07-25 01:46:07 -07001001
1002static int gpiochip_export(struct gpio_chip *chip)
1003{
1004 int status;
1005 struct device *dev;
1006
1007 /* Many systems register gpio chips for SOC support very early,
1008 * before driver model support is available. In those cases we
1009 * export this later, in gpiolib_sysfs_init() ... here we just
1010 * verify that _some_ field of gpio_class got initialized.
1011 */
1012 if (!gpio_class.p)
1013 return 0;
1014
1015 /* use chip->base for the ID; it's already known to be unique */
1016 mutex_lock(&sysfs_lock);
1017 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
1018 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001019 if (!IS_ERR(dev)) {
David Brownelld8f388d82008-07-25 01:46:07 -07001020 status = sysfs_create_group(&dev->kobj,
1021 &gpiochip_attr_group);
1022 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001023 status = PTR_ERR(dev);
David Brownelld8f388d82008-07-25 01:46:07 -07001024 chip->exported = (status == 0);
1025 mutex_unlock(&sysfs_lock);
1026
1027 if (status) {
1028 unsigned long flags;
1029 unsigned gpio;
1030
1031 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001032 gpio = 0;
1033 while (gpio < chip->ngpio)
1034 chip->desc[gpio++].chip = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -07001035 spin_unlock_irqrestore(&gpio_lock, flags);
1036
1037 pr_debug("%s: chip %s status %d\n", __func__,
1038 chip->label, status);
1039 }
1040
1041 return status;
1042}
1043
1044static void gpiochip_unexport(struct gpio_chip *chip)
1045{
1046 int status;
1047 struct device *dev;
1048
1049 mutex_lock(&sysfs_lock);
1050 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1051 if (dev) {
1052 put_device(dev);
1053 device_unregister(dev);
1054 chip->exported = 0;
1055 status = 0;
1056 } else
1057 status = -ENODEV;
1058 mutex_unlock(&sysfs_lock);
1059
1060 if (status)
1061 pr_debug("%s: chip %s status %d\n", __func__,
1062 chip->label, status);
1063}
1064
1065static int __init gpiolib_sysfs_init(void)
1066{
1067 int status;
1068 unsigned long flags;
Alexandre Courbot65493e32013-02-03 01:29:25 +09001069 struct gpio_chip *chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001070
1071 status = class_register(&gpio_class);
1072 if (status < 0)
1073 return status;
1074
1075 /* Scan and register the gpio_chips which registered very
1076 * early (e.g. before the class_register above was called).
1077 *
1078 * We run before arch_initcall() so chip->dev nodes can have
1079 * registered, and so arch_initcall() can always gpio_export().
1080 */
1081 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot65493e32013-02-03 01:29:25 +09001082 list_for_each_entry(chip, &gpio_chips, list) {
David Brownelld8f388d82008-07-25 01:46:07 -07001083 if (!chip || chip->exported)
1084 continue;
1085
1086 spin_unlock_irqrestore(&gpio_lock, flags);
1087 status = gpiochip_export(chip);
1088 spin_lock_irqsave(&gpio_lock, flags);
1089 }
1090 spin_unlock_irqrestore(&gpio_lock, flags);
1091
1092
1093 return status;
1094}
1095postcore_initcall(gpiolib_sysfs_init);
1096
1097#else
1098static inline int gpiochip_export(struct gpio_chip *chip)
1099{
1100 return 0;
1101}
1102
1103static inline void gpiochip_unexport(struct gpio_chip *chip)
1104{
1105}
1106
1107#endif /* CONFIG_GPIO_SYSFS */
1108
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001109/*
1110 * Add a new chip to the global chips list, keeping the list of chips sorted
1111 * by base order.
1112 *
1113 * Return -EBUSY if the new chip overlaps with some other chip's integer
1114 * space.
1115 */
1116static int gpiochip_add_to_list(struct gpio_chip *chip)
1117{
1118 struct list_head *pos = &gpio_chips;
1119 struct gpio_chip *_chip;
1120 int err = 0;
1121
1122 /* find where to insert our chip */
1123 list_for_each(pos, &gpio_chips) {
1124 _chip = list_entry(pos, struct gpio_chip, list);
1125 /* shall we insert before _chip? */
1126 if (_chip->base >= chip->base + chip->ngpio)
1127 break;
1128 }
1129
1130 /* are we stepping on the chip right before? */
1131 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1132 _chip = list_entry(pos->prev, struct gpio_chip, list);
1133 if (_chip->base + _chip->ngpio > chip->base) {
1134 dev_err(chip->dev,
1135 "GPIO integer space overlap, cannot add chip\n");
1136 err = -EBUSY;
1137 }
1138 }
1139
1140 if (!err)
1141 list_add_tail(&chip->list, pos);
1142
1143 return err;
1144}
1145
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001146/**
David Brownelld2876d02008-02-04 22:28:20 -08001147 * gpiochip_add() - register a gpio_chip
1148 * @chip: the chip to register, with chip->base initialized
1149 * Context: potentially before irqs or kmalloc will work
1150 *
1151 * Returns a negative errno if the chip can't be registered, such as
1152 * because the chip->base is invalid or already associated with a
1153 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001154 *
David Brownelld8f388d82008-07-25 01:46:07 -07001155 * When gpiochip_add() is called very early during boot, so that GPIOs
1156 * can be freely used, the chip->dev device must be registered before
1157 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1158 * for GPIOs will fail rudely.
1159 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001160 * If chip->base is negative, this requests dynamic assignment of
1161 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001162 */
1163int gpiochip_add(struct gpio_chip *chip)
1164{
1165 unsigned long flags;
1166 int status = 0;
1167 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001168 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001169
Trent Piephobff5fda2008-05-23 13:04:44 -07001170 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001171 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001172 status = -EINVAL;
1173 goto fail;
1174 }
1175
1176 spin_lock_irqsave(&gpio_lock, flags);
1177
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001178 if (base < 0) {
1179 base = gpiochip_find_base(chip->ngpio);
1180 if (base < 0) {
1181 status = base;
David Brownelld8f388d82008-07-25 01:46:07 -07001182 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001183 }
1184 chip->base = base;
1185 }
1186
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001187 status = gpiochip_add_to_list(chip);
1188
David Brownelld2876d02008-02-04 22:28:20 -08001189 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001190 chip->desc = &gpio_desc[chip->base];
1191
1192 for (id = 0; id < chip->ngpio; id++) {
1193 struct gpio_desc *desc = &chip->desc[id];
1194 desc->chip = chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001195
1196 /* REVISIT: most hardware initializes GPIOs as
1197 * inputs (often with pullups enabled) so power
1198 * usage is minimized. Linux code should set the
1199 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +03001200 * and in case chip->get_direction is not set,
David Brownelld8f388d82008-07-25 01:46:07 -07001201 * we may expose the wrong direction in sysfs.
1202 */
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001203 desc->flags = !chip->direction_input
David Brownelld8f388d82008-07-25 01:46:07 -07001204 ? (1 << FLAG_IS_OUT)
1205 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001206 }
1207 }
1208
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001209 spin_unlock_irqrestore(&gpio_lock, flags);
1210
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301211#ifdef CONFIG_PINCTRL
1212 INIT_LIST_HEAD(&chip->pin_ranges);
1213#endif
1214
Anton Vorontsov391c9702010-06-08 07:48:17 -06001215 of_gpiochip_add(chip);
1216
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001217 if (status)
1218 goto fail;
1219
1220 status = gpiochip_export(chip);
1221 if (status)
1222 goto fail;
1223
H Hartley Sweetenee1c1e72012-05-11 16:58:33 -07001224 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
Grant Likely64842aa2011-11-06 11:36:18 -07001225 chip->base, chip->base + chip->ngpio - 1,
1226 chip->label ? : "generic");
1227
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001228 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001229
1230unlock:
1231 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld2876d02008-02-04 22:28:20 -08001232fail:
1233 /* failures here can mean systems won't boot... */
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001234 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1235 chip->base, chip->base + chip->ngpio - 1,
1236 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001237 return status;
1238}
1239EXPORT_SYMBOL_GPL(gpiochip_add);
1240
1241/**
1242 * gpiochip_remove() - unregister a gpio_chip
1243 * @chip: the chip to unregister
1244 *
1245 * A gpio_chip with any GPIOs still requested may not be removed.
1246 */
1247int gpiochip_remove(struct gpio_chip *chip)
1248{
1249 unsigned long flags;
1250 int status = 0;
1251 unsigned id;
1252
1253 spin_lock_irqsave(&gpio_lock, flags);
1254
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001255 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001256 of_gpiochip_remove(chip);
1257
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001258 for (id = 0; id < chip->ngpio; id++) {
1259 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
David Brownelld2876d02008-02-04 22:28:20 -08001260 status = -EBUSY;
1261 break;
1262 }
1263 }
1264 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001265 for (id = 0; id < chip->ngpio; id++)
1266 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001267
1268 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -08001269 }
1270
1271 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d82008-07-25 01:46:07 -07001272
1273 if (status == 0)
1274 gpiochip_unexport(chip);
1275
David Brownelld2876d02008-02-04 22:28:20 -08001276 return status;
1277}
1278EXPORT_SYMBOL_GPL(gpiochip_remove);
1279
Grant Likely594fa262010-06-08 07:48:16 -06001280/**
1281 * gpiochip_find() - iterator for locating a specific gpio_chip
1282 * @data: data to pass to match function
1283 * @callback: Callback function to check gpio_chip
1284 *
1285 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1286 * determined by a user supplied @match callback. The callback should return
1287 * 0 if the device doesn't match and non-zero if it does. If the callback is
1288 * non-zero, this function will return to the caller and not iterate over any
1289 * more gpio_chips.
1290 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001291struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001292 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001293 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001294{
Alexandre Courbot125eef92013-02-03 01:29:26 +09001295 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001296 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001297
1298 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +09001299 list_for_each_entry(chip, &gpio_chips, list)
1300 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001301 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001302
1303 /* No match? */
1304 if (&chip->list == &gpio_chips)
1305 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001306 spin_unlock_irqrestore(&gpio_lock, flags);
1307
1308 return chip;
1309}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001310EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001311
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001312static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1313{
1314 const char *name = data;
1315
1316 return !strcmp(chip->label, name);
1317}
1318
1319static struct gpio_chip *find_chip_by_name(const char *name)
1320{
1321 return gpiochip_find((void *)name, gpiochip_match_name);
1322}
1323
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301324#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001325
Linus Walleij3f0f8672012-11-20 12:40:15 +01001326/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001327 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1328 * @chip: the gpiochip to add the range for
1329 * @pinctrl: the dev_name() of the pin controller to map to
1330 * @gpio_offset: the start offset in the current gpio_chip number space
1331 * @pin_group: name of the pin group inside the pin controller
1332 */
1333int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1334 struct pinctrl_dev *pctldev,
1335 unsigned int gpio_offset, const char *pin_group)
1336{
1337 struct gpio_pin_range *pin_range;
1338 int ret;
1339
1340 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1341 if (!pin_range) {
1342 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1343 chip->label);
1344 return -ENOMEM;
1345 }
1346
1347 /* Use local offset as range ID */
1348 pin_range->range.id = gpio_offset;
1349 pin_range->range.gc = chip;
1350 pin_range->range.name = chip->label;
1351 pin_range->range.base = chip->base + gpio_offset;
1352 pin_range->pctldev = pctldev;
1353
1354 ret = pinctrl_get_group_pins(pctldev, pin_group,
1355 &pin_range->range.pins,
1356 &pin_range->range.npins);
1357 if (ret < 0)
1358 return ret;
1359
1360 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1361
1362 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PINGRP %s\n",
1363 chip->label, gpio_offset,
1364 gpio_offset + pin_range->range.npins - 1,
1365 pinctrl_dev_get_devname(pctldev), pin_group);
1366
1367 list_add_tail(&pin_range->node, &chip->pin_ranges);
1368
1369 return 0;
1370}
1371EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1372
1373/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001374 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1375 * @chip: the gpiochip to add the range for
1376 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001377 * @gpio_offset: the start offset in the current gpio_chip number space
1378 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001379 * @npins: the number of pins from the offset of each pin space (GPIO and
1380 * pin controller) to accumulate in this range
1381 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001382int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001383 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001384 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301385{
1386 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001387 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301388
Linus Walleij3f0f8672012-11-20 12:40:15 +01001389 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301390 if (!pin_range) {
1391 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1392 chip->label);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001393 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301394 }
1395
Linus Walleij3f0f8672012-11-20 12:40:15 +01001396 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001397 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001398 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301399 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001400 pin_range->range.base = chip->base + gpio_offset;
1401 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301402 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001403 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301404 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001405 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001406 ret = PTR_ERR(pin_range->pctldev);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001407 pr_err("%s: GPIO chip: could not create pin range\n",
1408 chip->label);
1409 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001410 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001411 }
Linus Walleij316511c2012-11-21 08:48:09 +01001412 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1413 chip->label, gpio_offset, gpio_offset + npins - 1,
1414 pinctl_name,
1415 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301416
1417 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001418
1419 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301420}
Linus Walleij165adc92012-11-06 14:49:39 +01001421EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301422
Linus Walleij3f0f8672012-11-20 12:40:15 +01001423/**
1424 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1425 * @chip: the chip to remove all the mappings for
1426 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301427void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1428{
1429 struct gpio_pin_range *pin_range, *tmp;
1430
1431 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1432 list_del(&pin_range->node);
1433 pinctrl_remove_gpio_range(pin_range->pctldev,
1434 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001435 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301436 }
1437}
Linus Walleij165adc92012-11-06 14:49:39 +01001438EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1439
1440#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301441
David Brownelld2876d02008-02-04 22:28:20 -08001442/* These "optional" allocation calls help prevent drivers from stomping
1443 * on each other, and help provide better diagnostics in debugfs.
1444 * They're called even less than the "set direction" calls.
1445 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001446static int gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001447{
David Brownell35e8bb52008-10-15 22:03:16 -07001448 struct gpio_chip *chip;
Mark Browne9354572012-07-09 12:22:56 +01001449 int status = -EPROBE_DEFER;
David Brownelld2876d02008-02-04 22:28:20 -08001450 unsigned long flags;
1451
Alexandre Courbot0204df42013-10-04 10:59:58 -07001452 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001453 pr_warn("%s: invalid GPIO\n", __func__);
1454 return -EINVAL;
1455 }
1456
David Brownelld2876d02008-02-04 22:28:20 -08001457 spin_lock_irqsave(&gpio_lock, flags);
1458
David Brownell35e8bb52008-10-15 22:03:16 -07001459 chip = desc->chip;
Alexandre Courbot0204df42013-10-04 10:59:58 -07001460 if (chip == NULL)
1461 goto done;
David Brownelld2876d02008-02-04 22:28:20 -08001462
David Brownell35e8bb52008-10-15 22:03:16 -07001463 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001464 goto done;
1465
David Brownelld2876d02008-02-04 22:28:20 -08001466 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001467 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001468 */
1469
1470 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1471 desc_set_label(desc, label ? : "?");
1472 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001473 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001474 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001475 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001476 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001477 }
1478
1479 if (chip->request) {
1480 /* chip->request may sleep */
1481 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001482 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001483 spin_lock_irqsave(&gpio_lock, flags);
1484
1485 if (status < 0) {
1486 desc_set_label(desc, NULL);
1487 module_put(chip->owner);
1488 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001489 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001490 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001491 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001492 if (chip->get_direction) {
1493 /* chip->get_direction may sleep */
1494 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001495 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001496 spin_lock_irqsave(&gpio_lock, flags);
1497 }
David Brownelld2876d02008-02-04 22:28:20 -08001498done:
1499 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001500 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001501 desc_to_gpio(desc), label ? : "?", status);
David Brownelld2876d02008-02-04 22:28:20 -08001502 spin_unlock_irqrestore(&gpio_lock, flags);
1503 return status;
1504}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001505
1506int gpio_request(unsigned gpio, const char *label)
1507{
1508 return gpiod_request(gpio_to_desc(gpio), label);
1509}
David Brownelld2876d02008-02-04 22:28:20 -08001510EXPORT_SYMBOL_GPL(gpio_request);
1511
Alexandre Courbot372e7222013-02-03 01:29:29 +09001512static void gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001513{
1514 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001515 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001516
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001517 might_sleep();
1518
Alexandre Courbot372e7222013-02-03 01:29:29 +09001519 if (!desc) {
David Brownelld2876d02008-02-04 22:28:20 -08001520 WARN_ON(extra_checks);
1521 return;
1522 }
1523
Alexandre Courbot372e7222013-02-03 01:29:29 +09001524 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07001525
David Brownelld2876d02008-02-04 22:28:20 -08001526 spin_lock_irqsave(&gpio_lock, flags);
1527
David Brownell35e8bb52008-10-15 22:03:16 -07001528 chip = desc->chip;
1529 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1530 if (chip->free) {
1531 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001532 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001533 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001534 spin_lock_irqsave(&gpio_lock, flags);
1535 }
David Brownelld2876d02008-02-04 22:28:20 -08001536 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001537 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001538 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001539 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301540 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301541 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001542 } else
David Brownelld2876d02008-02-04 22:28:20 -08001543 WARN_ON(extra_checks);
1544
1545 spin_unlock_irqrestore(&gpio_lock, flags);
1546}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001547
1548void gpio_free(unsigned gpio)
1549{
1550 gpiod_free(gpio_to_desc(gpio));
1551}
David Brownelld2876d02008-02-04 22:28:20 -08001552EXPORT_SYMBOL_GPL(gpio_free);
1553
Eric Miao3e45f1d2010-03-05 13:44:35 -08001554/**
1555 * gpio_request_one - request a single GPIO with initial configuration
1556 * @gpio: the GPIO number
1557 * @flags: GPIO configuration as specified by GPIOF_*
1558 * @label: a literal description string of this GPIO
1559 */
1560int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1561{
Alexandre Courbot372e7222013-02-03 01:29:29 +09001562 struct gpio_desc *desc;
Eric Miao3e45f1d2010-03-05 13:44:35 -08001563 int err;
1564
Alexandre Courbot372e7222013-02-03 01:29:29 +09001565 desc = gpio_to_desc(gpio);
1566
1567 err = gpiod_request(desc, label);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001568 if (err)
1569 return err;
1570
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301571 if (flags & GPIOF_OPEN_DRAIN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001572 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301573
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301574 if (flags & GPIOF_OPEN_SOURCE)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001575 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301576
Eric Miao3e45f1d2010-03-05 13:44:35 -08001577 if (flags & GPIOF_DIR_IN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001578 err = gpiod_direction_input(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001579 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001580 err = gpiod_direction_output(desc,
Eric Miao3e45f1d2010-03-05 13:44:35 -08001581 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1582
Aaro Koskinene2548112010-12-21 17:24:22 -08001583 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001584 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -08001585
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001586 if (flags & GPIOF_EXPORT) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001587 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001588 if (err)
1589 goto free_gpio;
1590 }
1591
1592 return 0;
1593
1594 free_gpio:
Alexandre Courbot372e7222013-02-03 01:29:29 +09001595 gpiod_free(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001596 return err;
1597}
1598EXPORT_SYMBOL_GPL(gpio_request_one);
1599
1600/**
1601 * gpio_request_array - request multiple GPIOs in a single call
1602 * @array: array of the 'struct gpio'
1603 * @num: how many GPIOs in the array
1604 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001605int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001606{
1607 int i, err;
1608
1609 for (i = 0; i < num; i++, array++) {
1610 err = gpio_request_one(array->gpio, array->flags, array->label);
1611 if (err)
1612 goto err_free;
1613 }
1614 return 0;
1615
1616err_free:
1617 while (i--)
1618 gpio_free((--array)->gpio);
1619 return err;
1620}
1621EXPORT_SYMBOL_GPL(gpio_request_array);
1622
1623/**
1624 * gpio_free_array - release multiple GPIOs in a single call
1625 * @array: array of the 'struct gpio'
1626 * @num: how many GPIOs in the array
1627 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001628void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001629{
1630 while (num--)
1631 gpio_free((array++)->gpio);
1632}
1633EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001634
1635/**
1636 * gpiochip_is_requested - return string iff signal was requested
1637 * @chip: controller managing the signal
1638 * @offset: of signal within controller's 0..(ngpio - 1) range
1639 *
1640 * Returns NULL if the GPIO is not currently requested, else a string.
1641 * If debugfs support is enabled, the string returned is the label passed
1642 * to gpio_request(); otherwise it is a meaningless constant.
1643 *
1644 * This function is for use by GPIO controller drivers. The label can
1645 * help with diagnostics, and knowing that the signal is used as a GPIO
1646 * can help avoid accidentally multiplexing it to another controller.
1647 */
1648const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1649{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001650 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001651
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001652 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -08001653 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001654
1655 desc = &chip->desc[offset];
1656
Alexandre Courbot372e7222013-02-03 01:29:29 +09001657 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001658 return NULL;
1659#ifdef CONFIG_DEBUG_FS
Alexandre Courbot372e7222013-02-03 01:29:29 +09001660 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001661#else
1662 return "?";
1663#endif
1664}
1665EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1666
1667
1668/* Drivers MUST set GPIO direction before making get/set calls. In
1669 * some cases this is done in early boot, before IRQs are enabled.
1670 *
1671 * As a rule these aren't called more than once (except for drivers
1672 * using the open-drain emulation idiom) so these are natural places
1673 * to accumulate extra debugging checks. Note that we can't (yet)
1674 * rely on gpio_request() having been called beforehand.
1675 */
1676
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001677/**
1678 * gpiod_direction_input - set the GPIO direction to input
1679 * @desc: GPIO to set to input
1680 *
1681 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1682 * be called safely on it.
1683 *
1684 * Return 0 in case of success, else an error code.
1685 */
1686int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001687{
1688 unsigned long flags;
1689 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001690 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001691 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001692
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001693 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001694 pr_warn("%s: invalid GPIO\n", __func__);
1695 return -EINVAL;
1696 }
1697
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001698 chip = desc->chip;
1699 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001700 gpiod_warn(desc,
1701 "%s: missing get() or direction_input() operations\n",
1702 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001703 return -EIO;
1704 }
1705
David Brownelld2876d02008-02-04 22:28:20 -08001706 spin_lock_irqsave(&gpio_lock, flags);
1707
Alexandre Courbot372e7222013-02-03 01:29:29 +09001708 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001709 if (status < 0)
1710 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001711
1712 /* now we know the gpio is valid and chip won't vanish */
1713
1714 spin_unlock_irqrestore(&gpio_lock, flags);
1715
David Brownell9c4ba942010-08-10 18:02:24 -07001716 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001717
Alexandre Courbot372e7222013-02-03 01:29:29 +09001718 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001719 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001720 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001721 if (status < 0) {
Mark Brown6424de52013-09-09 10:33:49 +01001722 gpiod_dbg(desc, "chip request fail, %d\n", status);
David Brownell35e8bb52008-10-15 22:03:16 -07001723 /* and it's not available to anyone else ...
1724 * gpio_request() is the fully clean solution.
1725 */
1726 goto lose;
1727 }
1728 }
1729
Alexandre Courbot372e7222013-02-03 01:29:29 +09001730 status = chip->direction_input(chip, offset);
David Brownelld2876d02008-02-04 22:28:20 -08001731 if (status == 0)
1732 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001733
Alexandre Courbot372e7222013-02-03 01:29:29 +09001734 trace_gpio_direction(desc_to_gpio(desc), 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001735lose:
David Brownelld2876d02008-02-04 22:28:20 -08001736 return status;
1737fail:
1738 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001739 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001740 gpiod_dbg(desc, "%s status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001741 return status;
1742}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001743EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001744
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001745/**
1746 * gpiod_direction_output - set the GPIO direction to input
1747 * @desc: GPIO to set to output
1748 * @value: initial output value of the GPIO
1749 *
1750 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1751 * be called safely on it. The initial value of the output must be specified.
1752 *
1753 * Return 0 in case of success, else an error code.
1754 */
1755int gpiod_direction_output(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001756{
1757 unsigned long flags;
1758 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001759 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001760 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001761
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001762 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001763 pr_warn("%s: invalid GPIO\n", __func__);
1764 return -EINVAL;
1765 }
1766
Linus Walleijd468bf92013-09-24 11:54:38 +02001767 /* GPIOs used for IRQs shall not be set as output */
1768 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1769 gpiod_err(desc,
1770 "%s: tried to set a GPIO tied to an IRQ as output\n",
1771 __func__);
1772 return -EIO;
1773 }
1774
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301775 /* Open drain pin should not be driven to 1 */
1776 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001777 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301778
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301779 /* Open source pin should not be driven to 0 */
1780 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001781 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301782
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001783 chip = desc->chip;
1784 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001785 gpiod_warn(desc,
1786 "%s: missing set() or direction_output() operations\n",
1787 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001788 return -EIO;
1789 }
1790
David Brownelld2876d02008-02-04 22:28:20 -08001791 spin_lock_irqsave(&gpio_lock, flags);
1792
Alexandre Courbot372e7222013-02-03 01:29:29 +09001793 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001794 if (status < 0)
1795 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001796
1797 /* now we know the gpio is valid and chip won't vanish */
1798
1799 spin_unlock_irqrestore(&gpio_lock, flags);
1800
David Brownell9c4ba942010-08-10 18:02:24 -07001801 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001802
Alexandre Courbot372e7222013-02-03 01:29:29 +09001803 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001804 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001805 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001806 if (status < 0) {
Mark Brown6424de52013-09-09 10:33:49 +01001807 gpiod_dbg(desc, "chip request fail, %d\n", status);
David Brownell35e8bb52008-10-15 22:03:16 -07001808 /* and it's not available to anyone else ...
1809 * gpio_request() is the fully clean solution.
1810 */
1811 goto lose;
1812 }
1813 }
1814
Alexandre Courbot372e7222013-02-03 01:29:29 +09001815 status = chip->direction_output(chip, offset, value);
David Brownelld2876d02008-02-04 22:28:20 -08001816 if (status == 0)
1817 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001818 trace_gpio_value(desc_to_gpio(desc), 0, value);
1819 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001820lose:
David Brownelld2876d02008-02-04 22:28:20 -08001821 return status;
1822fail:
1823 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001824 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001825 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001826 return status;
1827}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001828EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001829
Felipe Balbic4b5be92010-05-26 14:42:23 -07001830/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001831 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001832 * @gpio: the gpio to set debounce time
1833 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001834 *
1835 * returns -ENOTSUPP if the controller does not support setting
1836 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001837 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001838int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001839{
1840 unsigned long flags;
1841 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001842 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001843 int offset;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001844
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001845 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001846 pr_warn("%s: invalid GPIO\n", __func__);
1847 return -EINVAL;
1848 }
1849
Felipe Balbic4b5be92010-05-26 14:42:23 -07001850 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001851 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001852 gpiod_dbg(desc,
1853 "%s: missing set() or set_debounce() operations\n",
1854 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001855 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001856 }
1857
1858 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001859
1860 status = gpio_ensure_requested(desc);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001861 if (status < 0)
1862 goto fail;
1863
1864 /* now we know the gpio is valid and chip won't vanish */
1865
1866 spin_unlock_irqrestore(&gpio_lock, flags);
1867
David Brownell9c4ba942010-08-10 18:02:24 -07001868 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001869
Alexandre Courbot372e7222013-02-03 01:29:29 +09001870 offset = gpio_chip_hwgpio(desc);
1871 return chip->set_debounce(chip, offset, debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001872
1873fail:
1874 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001875 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001876 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001877
1878 return status;
1879}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001880EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001881
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001882/**
1883 * gpiod_is_active_low - test whether a GPIO is active-low or not
1884 * @desc: the gpio descriptor to test
1885 *
1886 * Returns 1 if the GPIO is active-low, 0 otherwise.
1887 */
1888int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001889{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001890 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001891}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001892EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001893
1894/* I/O calls are only valid after configuration completed; the relevant
1895 * "is this a valid GPIO" error checks should already have been done.
1896 *
1897 * "Get" operations are often inlinable as reading a pin value register,
1898 * and masking the relevant bit in that register.
1899 *
1900 * When "set" operations are inlinable, they involve writing that mask to
1901 * one register to set a low value, or a different register to set it high.
1902 * Otherwise locking is needed, so there may be little value to inlining.
1903 *
1904 *------------------------------------------------------------------------
1905 *
1906 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1907 * have requested the GPIO. That can include implicit requesting by
1908 * a direction setting call. Marking a gpio as requested locks its chip
1909 * in memory, guaranteeing that these table lookups need no more locking
1910 * and that gpiochip_remove() will fail.
1911 *
1912 * REVISIT when debugging, consider adding some instrumentation to ensure
1913 * that the GPIO was actually requested.
1914 */
1915
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001916static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001917{
1918 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001919 int value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001920 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001921
Alexandre Courbot372e7222013-02-03 01:29:29 +09001922 chip = desc->chip;
1923 offset = gpio_chip_hwgpio(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001924 value = chip->get ? chip->get(chip, offset) : 0;
1925 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001926 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001927}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001928
David Brownelld2876d02008-02-04 22:28:20 -08001929/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001930 * gpiod_get_raw_value() - return a gpio's raw value
1931 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001932 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001933 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1934 * its ACTIVE_LOW status.
1935 *
1936 * This function should be called from contexts where we cannot sleep, and will
1937 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001938 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001939int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001940{
David Brownelld2876d02008-02-04 22:28:20 -08001941 if (!desc)
1942 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001943 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001944 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001945 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001946}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001947EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001948
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001949/**
1950 * gpiod_get_value() - return a gpio's value
1951 * @desc: gpio whose value will be returned
1952 *
1953 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1954 * account.
1955 *
1956 * This function should be called from contexts where we cannot sleep, and will
1957 * complain if the GPIO chip functions potentially sleep.
1958 */
1959int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001960{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001961 int value;
1962 if (!desc)
1963 return 0;
1964 /* Should be using gpio_get_value_cansleep() */
1965 WARN_ON(desc->chip->can_sleep);
1966
1967 value = _gpiod_get_raw_value(desc);
1968 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1969 value = !value;
1970
1971 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001972}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001973EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001974
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301975/*
1976 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001977 * @desc: gpio descriptor whose state need to be set.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301978 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1979 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001980static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301981{
1982 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001983 struct gpio_chip *chip = desc->chip;
1984 int offset = gpio_chip_hwgpio(desc);
1985
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301986 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001987 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301988 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001989 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301990 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001991 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301992 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001993 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301994 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001995 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301996 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001997 gpiod_err(desc,
1998 "%s: Error in set_value for open drain err %d\n",
1999 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302000}
2001
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302002/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002003 * _gpio_set_open_source_value() - Set the open source gpio's value.
2004 * @desc: gpio descriptor whose state need to be set.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302005 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
2006 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09002007static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302008{
2009 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002010 struct gpio_chip *chip = desc->chip;
2011 int offset = gpio_chip_hwgpio(desc);
2012
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302013 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002014 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302015 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002016 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302017 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002018 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302019 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002020 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302021 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002022 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302023 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002024 gpiod_err(desc,
2025 "%s: Error in set_value for open source err %d\n",
2026 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302027}
2028
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002029static void _gpiod_set_raw_value(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002030{
2031 struct gpio_chip *chip;
2032
Alexandre Courbot372e7222013-02-03 01:29:29 +09002033 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002034 trace_gpio_value(desc_to_gpio(desc), 0, value);
2035 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2036 _gpio_set_open_drain_value(desc, value);
2037 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2038 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302039 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002040 chip->set(chip, gpio_chip_hwgpio(desc), value);
2041}
2042
David Brownelld2876d02008-02-04 22:28:20 -08002043/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002044 * gpiod_set_raw_value() - assign a gpio's raw value
2045 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002046 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002047 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002048 * Set the raw value of the GPIO, i.e. the value of its physical line without
2049 * regard for its ACTIVE_LOW status.
2050 *
2051 * This function should be called from contexts where we cannot sleep, and will
2052 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002053 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002054void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002055{
David Brownelld2876d02008-02-04 22:28:20 -08002056 if (!desc)
2057 return;
David Brownelld2876d02008-02-04 22:28:20 -08002058 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09002059 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002060 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002061}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002062EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002063
2064/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002065 * gpiod_set_value() - assign a gpio's value
2066 * @desc: gpio whose value will be assigned
2067 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002068 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002069 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2070 * account
2071 *
2072 * This function should be called from contexts where we cannot sleep, and will
2073 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002074 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002075void gpiod_set_value(struct gpio_desc *desc, int value)
2076{
2077 if (!desc)
2078 return;
2079 /* Should be using gpio_set_value_cansleep() */
2080 WARN_ON(desc->chip->can_sleep);
2081 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2082 value = !value;
2083 _gpiod_set_raw_value(desc, value);
2084}
2085EXPORT_SYMBOL_GPL(gpiod_set_value);
2086
2087/**
2088 * gpiod_cansleep() - report whether gpio value access may sleep
2089 * @desc: gpio to check
2090 *
2091 */
2092int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002093{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002094 if (!desc)
2095 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002096 return desc->chip->can_sleep;
2097}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002098EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002099
David Brownell0f6d5042008-10-15 22:03:14 -07002100/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002101 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2102 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002103 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002104 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2105 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002106 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002107int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002108{
2109 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002110 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002111
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002112 if (!desc)
2113 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002114 chip = desc->chip;
2115 offset = gpio_chip_hwgpio(desc);
2116 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
2117}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002118EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002119
Linus Walleijd468bf92013-09-24 11:54:38 +02002120/**
2121 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
2122 * @gpio: the GPIO line to lock as used for IRQ
2123 *
2124 * This is used directly by GPIO drivers that want to lock down
2125 * a certain GPIO line to be used as IRQs, for example in the
2126 * .to_irq() callback of their gpio_chip, or in the .irq_enable()
2127 * of its irq_chip implementation if the GPIO is known from that
2128 * code.
David Brownelld2876d02008-02-04 22:28:20 -08002129 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002130int gpiod_lock_as_irq(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002131{
Linus Walleijd468bf92013-09-24 11:54:38 +02002132 if (!desc)
2133 return -EINVAL;
2134
2135 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
2136 gpiod_err(desc,
2137 "%s: tried to flag a GPIO set as output for IRQ\n",
2138 __func__);
2139 return -EIO;
2140 }
2141
2142 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
2143 return 0;
2144}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002145EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002146
2147int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
2148{
2149 return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset));
2150}
2151EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
2152
2153/**
2154 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
2155 * @gpio: the GPIO line to unlock from IRQ usage
2156 *
2157 * This is used directly by GPIO drivers that want to indicate
2158 * that a certain GPIO is no longer used exclusively for IRQ.
2159 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002160void gpiod_unlock_as_irq(struct gpio_desc *desc)
Linus Walleijd468bf92013-09-24 11:54:38 +02002161{
2162 if (!desc)
2163 return;
2164
2165 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2166}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002167EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002168
2169void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2170{
2171 return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset));
2172}
2173EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
David Brownelld2876d02008-02-04 22:28:20 -08002174
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002175/**
2176 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2177 * @desc: gpio whose value will be returned
2178 *
2179 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2180 * its ACTIVE_LOW status.
2181 *
2182 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002183 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002184int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002185{
David Brownelld2876d02008-02-04 22:28:20 -08002186 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002187 if (!desc)
2188 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002189 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002190}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002191EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002192
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002193/**
2194 * gpiod_get_value_cansleep() - return a gpio's value
2195 * @desc: gpio whose value will be returned
2196 *
2197 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2198 * account.
2199 *
2200 * This function is to be called from contexts that can sleep.
2201 */
2202int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002203{
David Brownelld2876d02008-02-04 22:28:20 -08002204 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002205
2206 might_sleep_if(extra_checks);
2207 if (!desc)
2208 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002209
2210 value = _gpiod_get_raw_value(desc);
2211 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2212 value = !value;
2213
David Brownelld2876d02008-02-04 22:28:20 -08002214 return value;
2215}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002216EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002217
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002218/**
2219 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2220 * @desc: gpio whose value will be assigned
2221 * @value: value to assign
2222 *
2223 * Set the raw value of the GPIO, i.e. the value of its physical line without
2224 * regard for its ACTIVE_LOW status.
2225 *
2226 * This function is to be called from contexts that can sleep.
2227 */
2228void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002229{
David Brownelld2876d02008-02-04 22:28:20 -08002230 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002231 if (!desc)
2232 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002233 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002234}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002235EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002236
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002237/**
2238 * gpiod_set_value_cansleep() - assign a gpio's value
2239 * @desc: gpio whose value will be assigned
2240 * @value: value to assign
2241 *
2242 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2243 * account
2244 *
2245 * This function is to be called from contexts that can sleep.
2246 */
2247void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002248{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002249 might_sleep_if(extra_checks);
2250 if (!desc)
2251 return;
2252
2253 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2254 value = !value;
2255 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002256}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002257EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002258
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002259/**
2260 * gpiod_add_table() - register GPIO device consumers
2261 * @table: array of consumers to register
2262 * @num: number of consumers in table
2263 */
2264void gpiod_add_table(struct gpiod_lookup *table, size_t size)
2265{
2266 mutex_lock(&gpio_lookup_lock);
2267
2268 while (size--) {
2269 list_add_tail(&table->list, &gpio_lookup_list);
2270 table++;
2271 }
2272
2273 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002274}
2275
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002276#ifdef CONFIG_OF
2277static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002278 unsigned int idx,
2279 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002280{
2281 char prop_name[32]; /* 32 is max size of property name */
2282 enum of_gpio_flags of_flags;
2283 struct gpio_desc *desc;
2284
2285 if (con_id)
2286 snprintf(prop_name, 32, "%s-gpios", con_id);
2287 else
2288 snprintf(prop_name, 32, "gpios");
2289
2290 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2291 &of_flags);
2292
2293 if (IS_ERR(desc))
2294 return desc;
2295
2296 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002297 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002298
2299 return desc;
2300}
2301#else
2302static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
2303 unsigned int idx, unsigned long *flags)
2304{
2305 return ERR_PTR(-ENODEV);
2306}
2307#endif
2308
Mika Westerberg81f59e92013-10-10 11:01:09 +03002309static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002310 unsigned int idx,
2311 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03002312{
Mika Westerberge01f4402013-10-10 11:01:10 +03002313 struct acpi_gpio_info info;
2314 struct gpio_desc *desc;
2315
2316 desc = acpi_get_gpiod_by_index(dev, idx, &info);
2317 if (IS_ERR(desc))
2318 return desc;
2319
2320 if (info.gpioint && info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002321 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002322
2323 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002324}
2325
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002326static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002327 unsigned int idx,
2328 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002329{
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;
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002420 enum gpio_lookup_flags flags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002421
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
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002446 if (flags & GPIO_ACTIVE_LOW)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002447 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002448 if (flags & GPIO_OPEN_DRAIN)
2449 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2450 if (flags & GPIO_OPEN_SOURCE)
2451 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002452
2453 return desc;
2454}
2455EXPORT_SYMBOL_GPL(gpiod_get_index);
2456
2457/**
2458 * gpiod_put - dispose of a GPIO descriptor
2459 * @desc: GPIO descriptor to dispose of
2460 *
2461 * No descriptor can be used after gpiod_put() has been called on it.
2462 */
2463void gpiod_put(struct gpio_desc *desc)
2464{
2465 gpiod_free(desc);
2466}
2467EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002468
David Brownelld2876d02008-02-04 22:28:20 -08002469#ifdef CONFIG_DEBUG_FS
2470
2471static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2472{
2473 unsigned i;
2474 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002475 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002476 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002477 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002478
2479 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2480 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2481 continue;
2482
Alexandre Courbot372e7222013-02-03 01:29:29 +09002483 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002484 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002485 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2486 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002487 gpio, gdesc->label,
2488 is_out ? "out" : "in ",
2489 chip->get
2490 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002491 : "? ",
2492 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002493 seq_printf(s, "\n");
2494 }
2495}
2496
Thierry Redingf9c4a312012-04-12 13:26:01 +02002497static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002498{
Grant Likely362432a2013-02-09 09:41:49 +00002499 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002500 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002501 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002502
2503 s->private = "";
2504
Grant Likely362432a2013-02-09 09:41:49 +00002505 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002506 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002507 if (index-- == 0) {
2508 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002509 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002510 }
2511 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002512
2513 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002514}
2515
2516static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2517{
Grant Likely362432a2013-02-09 09:41:49 +00002518 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002519 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002520 void *ret = NULL;
2521
Grant Likely362432a2013-02-09 09:41:49 +00002522 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002523 if (list_is_last(&chip->list, &gpio_chips))
2524 ret = NULL;
2525 else
2526 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002527 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002528
2529 s->private = "\n";
2530 ++*pos;
2531
2532 return ret;
2533}
2534
2535static void gpiolib_seq_stop(struct seq_file *s, void *v)
2536{
2537}
2538
2539static int gpiolib_seq_show(struct seq_file *s, void *v)
2540{
2541 struct gpio_chip *chip = v;
2542 struct device *dev;
2543
2544 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2545 chip->base, chip->base + chip->ngpio - 1);
2546 dev = chip->dev;
2547 if (dev)
2548 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2549 dev_name(dev));
2550 if (chip->label)
2551 seq_printf(s, ", %s", chip->label);
2552 if (chip->can_sleep)
2553 seq_printf(s, ", can sleep");
2554 seq_printf(s, ":\n");
2555
2556 if (chip->dbg_show)
2557 chip->dbg_show(s, chip);
2558 else
2559 gpiolib_dbg_show(s, chip);
2560
David Brownelld2876d02008-02-04 22:28:20 -08002561 return 0;
2562}
2563
Thierry Redingf9c4a312012-04-12 13:26:01 +02002564static const struct seq_operations gpiolib_seq_ops = {
2565 .start = gpiolib_seq_start,
2566 .next = gpiolib_seq_next,
2567 .stop = gpiolib_seq_stop,
2568 .show = gpiolib_seq_show,
2569};
2570
David Brownelld2876d02008-02-04 22:28:20 -08002571static int gpiolib_open(struct inode *inode, struct file *file)
2572{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002573 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002574}
2575
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002576static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002577 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002578 .open = gpiolib_open,
2579 .read = seq_read,
2580 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002581 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002582};
2583
2584static int __init gpiolib_debugfs_init(void)
2585{
2586 /* /sys/kernel/debug/gpio */
2587 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2588 NULL, NULL, &gpiolib_operations);
2589 return 0;
2590}
2591subsys_initcall(gpiolib_debugfs_init);
2592
2593#endif /* DEBUG_FS */