blob: 4eb262a31777b9a0d411cd46edb4df9ba82ab84c [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d2008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Mika Westerberg81f59e92013-10-10 11:01:09 +030013#include <linux/acpi_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070014#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010016#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090017#include <linux/gpio/driver.h>
David Brownelld2876d02008-02-04 22:28:20 -080018
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060019#define CREATE_TRACE_POINTS
20#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080021
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070022/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080023 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070024 * The GPIO programming interface allows for inlining speed-critical
25 * get/set operations for common cases, so that access to SOC-integrated
26 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080027 */
28
29
30/* When debugging, extend minimal trust to callers and platform code.
31 * Also emit diagnostic messages that may help initial bringup, when
32 * board setup or driver bugs are most common.
33 *
34 * Otherwise, minimize overhead in what may be bitbanging codepaths.
35 */
36#ifdef DEBUG
37#define extra_checks 1
38#else
39#define extra_checks 0
40#endif
41
42/* gpio_lock prevents conflicts during gpio_desc[] table updates.
43 * While any GPIO is requested, its gpio_chip is not removable;
44 * each GPIO's "requested" flag serves as a lock and refcount.
45 */
46static DEFINE_SPINLOCK(gpio_lock);
47
48struct gpio_desc {
49 struct gpio_chip *chip;
50 unsigned long flags;
51/* flag symbols are bit numbers */
52#define FLAG_REQUESTED 0
53#define FLAG_IS_OUT 1
Alexandre Courbot710b40e2013-02-02 23:44:06 +090054#define FLAG_EXPORT 2 /* protected by sysfs_lock */
55#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
56#define FLAG_TRIG_FALL 4 /* trigger on falling edge */
57#define FLAG_TRIG_RISE 5 /* trigger on rising edge */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070058#define FLAG_ACTIVE_LOW 6 /* value has active low */
Alexandre Courbot710b40e2013-02-02 23:44:06 +090059#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
60#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
Linus Walleijd468bf92013-09-24 11:54:38 +020061#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070062
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070063#define ID_SHIFT 16 /* add new flags before this one */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070064
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070065#define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
Daniel Glöcknerff77c352009-09-22 16:46:38 -070066#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
David Brownelld2876d02008-02-04 22:28:20 -080067
68#ifdef CONFIG_DEBUG_FS
69 const char *label;
70#endif
71};
72static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
73
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +090074#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
75
Alexandre Courbotbae48da2013-10-17 10:21:38 -070076static DEFINE_MUTEX(gpio_lookup_lock);
77static LIST_HEAD(gpio_lookup_list);
Alexandre Courbot1a989d02013-02-03 01:29:24 +090078static LIST_HEAD(gpio_chips);
79
Daniel Glöcknerff77c352009-09-22 16:46:38 -070080#ifdef CONFIG_GPIO_SYSFS
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070081static DEFINE_IDR(dirent_idr);
Daniel Glöcknerff77c352009-09-22 16:46:38 -070082#endif
83
Alexandre Courbot372e7222013-02-03 01:29:29 +090084static int gpiod_request(struct gpio_desc *desc, const char *label);
85static void gpiod_free(struct gpio_desc *desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090086
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{
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900154 if (offset >= chip->ngpio)
155 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200156
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900157 return &chip->desc[offset];
Linus Walleijd468bf92013-09-24 11:54:38 +0200158}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900159
160/**
161 * Convert a GPIO descriptor to the integer namespace.
162 * This should disappear in the future but is needed since we still
163 * use GPIO numbers for error messages and sysfs nodes
164 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700165int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900166{
Alexandre Courbot8c0fca82013-10-04 10:59:57 -0700167 return desc - &gpio_desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900168}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700169EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900170
171
David Brownelld2876d02008-02-04 22:28:20 -0800172/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
173 * when setting direction, and otherwise illegal. Until board setup code
174 * and drivers use explicit requests everywhere (which won't happen when
175 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -0700176 * message should motivate switching to explicit requests... so should
177 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -0700178 *
179 * NOTE: the autorequest mechanism is going away; at this point it's
180 * only "legal" in the sense that (old) code using it won't break yet,
181 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -0800182 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900183static int gpio_ensure_requested(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800184{
David Brownell8a0cecf2009-04-02 16:57:06 -0700185 const struct gpio_chip *chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900186 const int gpio = desc_to_gpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -0700187
David Brownell8a0cecf2009-04-02 16:57:06 -0700188 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
189 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700190 if (!try_module_get(chip->owner)) {
191 pr_err("GPIO-%d: module can't be gotten \n", gpio);
192 clear_bit(FLAG_REQUESTED, &desc->flags);
193 /* lose */
194 return -EIO;
195 }
David Brownelld2876d02008-02-04 22:28:20 -0800196 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700197 /* caller must chip->request() w/o spinlock */
198 if (chip->request)
199 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800200 }
David Brownell35e8bb52008-10-15 22:03:16 -0700201 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800202}
203
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700204/**
205 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
206 * @desc: descriptor to return the chip of
207 */
208struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900209{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900210 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900211}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700212EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800213
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700214/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
215static int gpiochip_find_base(int ngpio)
216{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900217 struct gpio_chip *chip;
218 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700219
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900220 list_for_each_entry_reverse(chip, &gpio_chips, list) {
221 /* found a free space? */
222 if (chip->base + chip->ngpio <= base)
223 break;
224 else
225 /* nope, check the space right before the chip */
226 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700227 }
228
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900229 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700230 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900231 return base;
232 } else {
233 pr_err("%s: cannot find free range\n", __func__);
234 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700235 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700236}
237
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700238/**
239 * gpiod_get_direction - return the current direction of a GPIO
240 * @desc: GPIO to get the direction of
241 *
242 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
243 *
244 * This function may sleep if gpiod_cansleep() is true.
245 */
246int gpiod_get_direction(const struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300247{
248 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900249 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300250 int status = -EINVAL;
251
Alexandre Courbot372e7222013-02-03 01:29:29 +0900252 chip = gpiod_to_chip(desc);
253 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300254
255 if (!chip->get_direction)
256 return status;
257
Alexandre Courbot372e7222013-02-03 01:29:29 +0900258 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300259 if (status > 0) {
260 /* GPIOF_DIR_IN, or other positive */
261 status = 1;
Alexandre Courbotdef63432013-02-15 14:46:15 +0900262 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
263 * so it does not affect constness per se */
264 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300265 }
266 if (status == 0) {
267 /* GPIOF_DIR_OUT */
Alexandre Courbotdef63432013-02-15 14:46:15 +0900268 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300269 }
270 return status;
271}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700272EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300273
David Brownelld8f388d2008-07-25 01:46:07 -0700274#ifdef CONFIG_GPIO_SYSFS
275
276/* lock protects against unexport_gpio() being called while
277 * sysfs files are active.
278 */
279static DEFINE_MUTEX(sysfs_lock);
280
281/*
282 * /sys/class/gpio/gpioN... only for GPIOs that are exported
283 * /direction
284 * * MAY BE OMITTED if kernel won't allow direction changes
285 * * is read/write as "in" or "out"
286 * * may also be written as "high" or "low", initializing
287 * output value as specified ("out" implies "low")
288 * /value
289 * * always readable, subject to hardware behavior
290 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700291 * /edge
292 * * configures behavior of poll(2) on /value
293 * * available only if pin can generate IRQs on input
294 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800295 * /active_low
296 * * configures polarity of /value
297 * * is read/write as zero/nonzero
298 * * also affects existing and subsequent "falling" and "rising"
299 * /edge configuration
David Brownelld8f388d2008-07-25 01:46:07 -0700300 */
301
302static ssize_t gpio_direction_show(struct device *dev,
303 struct device_attribute *attr, char *buf)
304{
Alexandre Courbotdef63432013-02-15 14:46:15 +0900305 const struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700306 ssize_t status;
307
308 mutex_lock(&sysfs_lock);
309
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900310 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700311 status = -EIO;
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900312 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +0900313 gpiod_get_direction(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700314 status = sprintf(buf, "%s\n",
315 test_bit(FLAG_IS_OUT, &desc->flags)
316 ? "out" : "in");
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900317 }
David Brownelld8f388d2008-07-25 01:46:07 -0700318
319 mutex_unlock(&sysfs_lock);
320 return status;
321}
322
323static ssize_t gpio_direction_store(struct device *dev,
324 struct device_attribute *attr, const char *buf, size_t size)
325{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900326 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700327 ssize_t status;
328
329 mutex_lock(&sysfs_lock);
330
331 if (!test_bit(FLAG_EXPORT, &desc->flags))
332 status = -EIO;
333 else if (sysfs_streq(buf, "high"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900334 status = gpiod_direction_output(desc, 1);
David Brownelld8f388d2008-07-25 01:46:07 -0700335 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900336 status = gpiod_direction_output(desc, 0);
David Brownelld8f388d2008-07-25 01:46:07 -0700337 else if (sysfs_streq(buf, "in"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900338 status = gpiod_direction_input(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700339 else
340 status = -EINVAL;
341
342 mutex_unlock(&sysfs_lock);
343 return status ? : size;
344}
345
Jani Nikula07697462009-12-15 16:46:20 -0800346static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d2008-07-25 01:46:07 -0700347 gpio_direction_show, gpio_direction_store);
348
349static ssize_t gpio_value_show(struct device *dev,
350 struct device_attribute *attr, char *buf)
351{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900352 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700353 ssize_t status;
354
355 mutex_lock(&sysfs_lock);
356
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700357 if (!test_bit(FLAG_EXPORT, &desc->flags))
David Brownelld8f388d2008-07-25 01:46:07 -0700358 status = -EIO;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700359 else
360 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
David Brownelld8f388d2008-07-25 01:46:07 -0700361
362 mutex_unlock(&sysfs_lock);
363 return status;
364}
365
366static ssize_t gpio_value_store(struct device *dev,
367 struct device_attribute *attr, const char *buf, size_t size)
368{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900369 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700370 ssize_t status;
371
372 mutex_lock(&sysfs_lock);
373
374 if (!test_bit(FLAG_EXPORT, &desc->flags))
375 status = -EIO;
376 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
377 status = -EPERM;
378 else {
379 long value;
380
Jingoo Hana3d88c92013-07-19 16:12:50 +0900381 status = kstrtol(buf, 0, &value);
David Brownelld8f388d2008-07-25 01:46:07 -0700382 if (status == 0) {
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700383 gpiod_set_value_cansleep(desc, value);
David Brownelld8f388d2008-07-25 01:46:07 -0700384 status = size;
385 }
386 }
387
388 mutex_unlock(&sysfs_lock);
389 return status;
390}
391
Jani Nikula07697462009-12-15 16:46:20 -0800392static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d2008-07-25 01:46:07 -0700393 gpio_value_show, gpio_value_store);
394
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700395static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
396{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700397 struct sysfs_dirent *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700398
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700399 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700400 return IRQ_HANDLED;
401}
402
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700403static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
404 unsigned long gpio_flags)
405{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700406 struct sysfs_dirent *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700407 unsigned long irq_flags;
408 int ret, irq, id;
409
410 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
411 return 0;
412
Alexandre Courbot372e7222013-02-03 01:29:29 +0900413 irq = gpiod_to_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700414 if (irq < 0)
415 return -EIO;
416
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700417 id = desc->flags >> ID_SHIFT;
418 value_sd = idr_find(&dirent_idr, id);
419 if (value_sd)
420 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700421
422 desc->flags &= ~GPIO_TRIGGER_MASK;
423
424 if (!gpio_flags) {
Linus Walleijd468bf92013-09-24 11:54:38 +0200425 gpiod_unlock_as_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700426 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700427 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700428 }
429
430 irq_flags = IRQF_SHARED;
431 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800432 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
433 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700434 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800435 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
436 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700437
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700438 if (!value_sd) {
Tejun Heo388975c2013-09-11 23:19:13 -0400439 value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700440 if (!value_sd) {
441 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700442 goto err_out;
443 }
444
Tejun Heo62f516b2013-02-27 17:04:06 -0800445 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
446 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700447 goto free_sd;
Tejun Heo62f516b2013-02-27 17:04:06 -0800448 id = ret;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700449
450 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700451 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700452
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700453 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700454 ret = -ERANGE;
455 goto free_id;
456 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700457 }
458
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700459 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700460 "gpiolib", value_sd);
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700461 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700462 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700463
Linus Walleijd468bf92013-09-24 11:54:38 +0200464 ret = gpiod_lock_as_irq(desc);
465 if (ret < 0) {
466 gpiod_warn(desc, "failed to flag the GPIO for IRQ\n");
467 goto free_id;
468 }
469
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700470 desc->flags |= gpio_flags;
471 return 0;
472
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700473free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700474 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700475 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700476free_sd:
477 if (value_sd)
478 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700479err_out:
480 return ret;
481}
482
483static const struct {
484 const char *name;
485 unsigned long flags;
486} trigger_types[] = {
487 { "none", 0 },
488 { "falling", BIT(FLAG_TRIG_FALL) },
489 { "rising", BIT(FLAG_TRIG_RISE) },
490 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
491};
492
493static ssize_t gpio_edge_show(struct device *dev,
494 struct device_attribute *attr, char *buf)
495{
496 const struct gpio_desc *desc = dev_get_drvdata(dev);
497 ssize_t status;
498
499 mutex_lock(&sysfs_lock);
500
501 if (!test_bit(FLAG_EXPORT, &desc->flags))
502 status = -EIO;
503 else {
504 int i;
505
506 status = 0;
507 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
508 if ((desc->flags & GPIO_TRIGGER_MASK)
509 == trigger_types[i].flags) {
510 status = sprintf(buf, "%s\n",
511 trigger_types[i].name);
512 break;
513 }
514 }
515
516 mutex_unlock(&sysfs_lock);
517 return status;
518}
519
520static ssize_t gpio_edge_store(struct device *dev,
521 struct device_attribute *attr, const char *buf, size_t size)
522{
523 struct gpio_desc *desc = dev_get_drvdata(dev);
524 ssize_t status;
525 int i;
526
527 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
528 if (sysfs_streq(trigger_types[i].name, buf))
529 goto found;
530 return -EINVAL;
531
532found:
533 mutex_lock(&sysfs_lock);
534
535 if (!test_bit(FLAG_EXPORT, &desc->flags))
536 status = -EIO;
537 else {
538 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
539 if (!status)
540 status = size;
541 }
542
543 mutex_unlock(&sysfs_lock);
544
545 return status;
546}
547
548static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
549
Jani Nikula07697462009-12-15 16:46:20 -0800550static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
551 int value)
552{
553 int status = 0;
554
555 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
556 return 0;
557
558 if (value)
559 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
560 else
561 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
562
563 /* reconfigure poll(2) support if enabled on one edge only */
564 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
565 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
566 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
567
568 gpio_setup_irq(desc, dev, 0);
569 status = gpio_setup_irq(desc, dev, trigger_flags);
570 }
571
572 return status;
573}
574
575static ssize_t gpio_active_low_show(struct device *dev,
576 struct device_attribute *attr, char *buf)
577{
578 const struct gpio_desc *desc = dev_get_drvdata(dev);
579 ssize_t status;
580
581 mutex_lock(&sysfs_lock);
582
583 if (!test_bit(FLAG_EXPORT, &desc->flags))
584 status = -EIO;
585 else
586 status = sprintf(buf, "%d\n",
587 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
588
589 mutex_unlock(&sysfs_lock);
590
591 return status;
592}
593
594static ssize_t gpio_active_low_store(struct device *dev,
595 struct device_attribute *attr, const char *buf, size_t size)
596{
597 struct gpio_desc *desc = dev_get_drvdata(dev);
598 ssize_t status;
599
600 mutex_lock(&sysfs_lock);
601
602 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
603 status = -EIO;
604 } else {
605 long value;
606
Jingoo Hana3d88c92013-07-19 16:12:50 +0900607 status = kstrtol(buf, 0, &value);
Jani Nikula07697462009-12-15 16:46:20 -0800608 if (status == 0)
609 status = sysfs_set_active_low(desc, dev, value != 0);
610 }
611
612 mutex_unlock(&sysfs_lock);
613
614 return status ? : size;
615}
616
617static const DEVICE_ATTR(active_low, 0644,
618 gpio_active_low_show, gpio_active_low_store);
619
David Brownelld8f388d2008-07-25 01:46:07 -0700620static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d2008-07-25 01:46:07 -0700621 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800622 &dev_attr_active_low.attr,
David Brownelld8f388d2008-07-25 01:46:07 -0700623 NULL,
624};
625
626static const struct attribute_group gpio_attr_group = {
627 .attrs = (struct attribute **) gpio_attrs,
628};
629
630/*
631 * /sys/class/gpio/gpiochipN/
632 * /base ... matching gpio_chip.base (N)
633 * /label ... matching gpio_chip.label
634 * /ngpio ... matching gpio_chip.ngpio
635 */
636
637static ssize_t chip_base_show(struct device *dev,
638 struct device_attribute *attr, char *buf)
639{
640 const struct gpio_chip *chip = dev_get_drvdata(dev);
641
642 return sprintf(buf, "%d\n", chip->base);
643}
644static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
645
646static ssize_t chip_label_show(struct device *dev,
647 struct device_attribute *attr, char *buf)
648{
649 const struct gpio_chip *chip = dev_get_drvdata(dev);
650
651 return sprintf(buf, "%s\n", chip->label ? : "");
652}
653static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
654
655static ssize_t chip_ngpio_show(struct device *dev,
656 struct device_attribute *attr, char *buf)
657{
658 const struct gpio_chip *chip = dev_get_drvdata(dev);
659
660 return sprintf(buf, "%u\n", chip->ngpio);
661}
662static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
663
664static const struct attribute *gpiochip_attrs[] = {
665 &dev_attr_base.attr,
666 &dev_attr_label.attr,
667 &dev_attr_ngpio.attr,
668 NULL,
669};
670
671static const struct attribute_group gpiochip_attr_group = {
672 .attrs = (struct attribute **) gpiochip_attrs,
673};
674
675/*
676 * /sys/class/gpio/export ... write-only
677 * integer N ... number of GPIO to export (full access)
678 * /sys/class/gpio/unexport ... write-only
679 * integer N ... number of GPIO to unexport
680 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100681static ssize_t export_store(struct class *class,
682 struct class_attribute *attr,
683 const char *buf, size_t len)
David Brownelld8f388d2008-07-25 01:46:07 -0700684{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900685 long gpio;
686 struct gpio_desc *desc;
687 int status;
David Brownelld8f388d2008-07-25 01:46:07 -0700688
Jingoo Hana3d88c92013-07-19 16:12:50 +0900689 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d2008-07-25 01:46:07 -0700690 if (status < 0)
691 goto done;
692
Alexandre Courbot372e7222013-02-03 01:29:29 +0900693 desc = gpio_to_desc(gpio);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900694 /* reject invalid GPIOs */
695 if (!desc) {
696 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
697 return -EINVAL;
698 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900699
David Brownelld8f388d2008-07-25 01:46:07 -0700700 /* No extra locking here; FLAG_SYSFS just signifies that the
701 * request and export were done by on behalf of userspace, so
702 * they may be undone on its behalf too.
703 */
704
Alexandre Courbot372e7222013-02-03 01:29:29 +0900705 status = gpiod_request(desc, "sysfs");
Mathias Nymanad2fab32012-10-25 14:03:03 +0300706 if (status < 0) {
707 if (status == -EPROBE_DEFER)
708 status = -ENODEV;
David Brownelld8f388d2008-07-25 01:46:07 -0700709 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +0300710 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900711 status = gpiod_export(desc, true);
David Brownelld8f388d2008-07-25 01:46:07 -0700712 if (status < 0)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900713 gpiod_free(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700714 else
Alexandre Courbot372e7222013-02-03 01:29:29 +0900715 set_bit(FLAG_SYSFS, &desc->flags);
David Brownelld8f388d2008-07-25 01:46:07 -0700716
717done:
718 if (status)
719 pr_debug("%s: status %d\n", __func__, status);
720 return status ? : len;
721}
722
Andi Kleen28812fe2010-01-05 12:48:07 +0100723static ssize_t unexport_store(struct class *class,
724 struct class_attribute *attr,
725 const char *buf, size_t len)
David Brownelld8f388d2008-07-25 01:46:07 -0700726{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900727 long gpio;
728 struct gpio_desc *desc;
729 int status;
David Brownelld8f388d2008-07-25 01:46:07 -0700730
Jingoo Hana3d88c92013-07-19 16:12:50 +0900731 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d2008-07-25 01:46:07 -0700732 if (status < 0)
733 goto done;
734
Alexandre Courbot372e7222013-02-03 01:29:29 +0900735 desc = gpio_to_desc(gpio);
David Brownelld8f388d2008-07-25 01:46:07 -0700736 /* reject bogus commands (gpio_unexport ignores them) */
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900737 if (!desc) {
738 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
739 return -EINVAL;
740 }
741
742 status = -EINVAL;
David Brownelld8f388d2008-07-25 01:46:07 -0700743
744 /* No extra locking here; FLAG_SYSFS just signifies that the
745 * request and export were done by on behalf of userspace, so
746 * they may be undone on its behalf too.
747 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900748 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700749 status = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900750 gpiod_free(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700751 }
752done:
753 if (status)
754 pr_debug("%s: status %d\n", __func__, status);
755 return status ? : len;
756}
757
758static struct class_attribute gpio_class_attrs[] = {
759 __ATTR(export, 0200, NULL, export_store),
760 __ATTR(unexport, 0200, NULL, unexport_store),
761 __ATTR_NULL,
762};
763
764static struct class gpio_class = {
765 .name = "gpio",
766 .owner = THIS_MODULE,
767
768 .class_attrs = gpio_class_attrs,
769};
770
771
772/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700773 * gpiod_export - export a GPIO through sysfs
David Brownelld8f388d2008-07-25 01:46:07 -0700774 * @gpio: gpio to make available, already requested
775 * @direction_may_change: true if userspace may change gpio direction
776 * Context: arch_initcall or later
777 *
778 * When drivers want to make a GPIO accessible to userspace after they
779 * have requested it -- perhaps while debugging, or as part of their
780 * public interface -- they may use this routine. If the GPIO can
781 * change direction (some can't) and the caller allows it, userspace
782 * will see "direction" sysfs attribute which may be used to change
783 * the gpio's direction. A "value" attribute will always be provided.
784 *
785 * Returns zero on success, else an error.
786 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700787int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
David Brownelld8f388d2008-07-25 01:46:07 -0700788{
789 unsigned long flags;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100790 int status;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700791 const char *ioname = NULL;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100792 struct device *dev;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900793 int offset;
David Brownelld8f388d2008-07-25 01:46:07 -0700794
795 /* can't export until sysfs is available ... */
796 if (!gpio_class.p) {
797 pr_debug("%s: called too early!\n", __func__);
798 return -ENOENT;
799 }
800
Alexandre Courbot372e7222013-02-03 01:29:29 +0900801 if (!desc) {
802 pr_debug("%s: invalid gpio descriptor\n", __func__);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100803 return -EINVAL;
804 }
David Brownelld8f388d2008-07-25 01:46:07 -0700805
806 mutex_lock(&sysfs_lock);
807
808 spin_lock_irqsave(&gpio_lock, flags);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100809 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
810 test_bit(FLAG_EXPORT, &desc->flags)) {
811 spin_unlock_irqrestore(&gpio_lock, flags);
812 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +0900813 __func__, desc_to_gpio(desc),
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100814 test_bit(FLAG_REQUESTED, &desc->flags),
815 test_bit(FLAG_EXPORT, &desc->flags));
Dan Carpenter529f2ad2012-10-26 09:59:43 +0300816 status = -EPERM;
817 goto fail_unlock;
David Brownelld8f388d2008-07-25 01:46:07 -0700818 }
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100819
820 if (!desc->chip->direction_input || !desc->chip->direction_output)
821 direction_may_change = false;
David Brownelld8f388d2008-07-25 01:46:07 -0700822 spin_unlock_irqrestore(&gpio_lock, flags);
823
Alexandre Courbot372e7222013-02-03 01:29:29 +0900824 offset = gpio_chip_hwgpio(desc);
825 if (desc->chip->names && desc->chip->names[offset])
826 ioname = desc->chip->names[offset];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700827
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100828 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Alexandre Courbot372e7222013-02-03 01:29:29 +0900829 desc, ioname ? ioname : "gpio%u",
830 desc_to_gpio(desc));
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100831 if (IS_ERR(dev)) {
832 status = PTR_ERR(dev);
833 goto fail_unlock;
David Brownelld8f388d2008-07-25 01:46:07 -0700834 }
835
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100836 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
David Brownelld8f388d2008-07-25 01:46:07 -0700837 if (status)
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100838 goto fail_unregister_device;
David Brownelld8f388d2008-07-25 01:46:07 -0700839
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100840 if (direction_may_change) {
841 status = device_create_file(dev, &dev_attr_direction);
842 if (status)
843 goto fail_unregister_device;
844 }
845
Alexandre Courbot372e7222013-02-03 01:29:29 +0900846 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100847 !test_bit(FLAG_IS_OUT, &desc->flags))) {
848 status = device_create_file(dev, &dev_attr_edge);
849 if (status)
850 goto fail_unregister_device;
851 }
852
853 set_bit(FLAG_EXPORT, &desc->flags);
854 mutex_unlock(&sysfs_lock);
855 return 0;
856
857fail_unregister_device:
858 device_unregister(dev);
859fail_unlock:
860 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900861 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
862 status);
David Brownelld8f388d2008-07-25 01:46:07 -0700863 return status;
864}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700865EXPORT_SYMBOL_GPL(gpiod_export);
David Brownelld8f388d2008-07-25 01:46:07 -0700866
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100867static int match_export(struct device *dev, const void *data)
David Brownelld8f388d2008-07-25 01:46:07 -0700868{
869 return dev_get_drvdata(dev) == data;
870}
871
872/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700873 * gpiod_export_link - create a sysfs link to an exported GPIO node
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700874 * @dev: device under which to create symlink
875 * @name: name of the symlink
876 * @gpio: gpio to create symlink to, already exported
877 *
878 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
879 * node. Caller is responsible for unlinking.
880 *
881 * Returns zero on success, else an error.
882 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700883int gpiod_export_link(struct device *dev, const char *name,
884 struct gpio_desc *desc)
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700885{
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700886 int status = -EINVAL;
887
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900888 if (!desc) {
889 pr_warn("%s: invalid GPIO\n", __func__);
890 return -EINVAL;
891 }
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700892
893 mutex_lock(&sysfs_lock);
894
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700895 if (test_bit(FLAG_EXPORT, &desc->flags)) {
896 struct device *tdev;
897
898 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
899 if (tdev != NULL) {
900 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
901 name);
902 } else {
903 status = -ENODEV;
904 }
905 }
906
907 mutex_unlock(&sysfs_lock);
908
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700909 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900910 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
911 status);
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700912
913 return status;
914}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700915EXPORT_SYMBOL_GPL(gpiod_export_link);
Jani Nikula07697462009-12-15 16:46:20 -0800916
917/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700918 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
Jani Nikula07697462009-12-15 16:46:20 -0800919 * @gpio: gpio to change
920 * @value: non-zero to use active low, i.e. inverted values
921 *
922 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
923 * The GPIO does not have to be exported yet. If poll(2) support has
924 * been enabled for either rising or falling edge, it will be
925 * reconfigured to follow the new polarity.
926 *
927 * Returns zero on success, else an error.
928 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700929int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
Jani Nikula07697462009-12-15 16:46:20 -0800930{
Jani Nikula07697462009-12-15 16:46:20 -0800931 struct device *dev = NULL;
932 int status = -EINVAL;
933
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900934 if (!desc) {
935 pr_warn("%s: invalid GPIO\n", __func__);
936 return -EINVAL;
937 }
Jani Nikula07697462009-12-15 16:46:20 -0800938
939 mutex_lock(&sysfs_lock);
940
Jani Nikula07697462009-12-15 16:46:20 -0800941 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800942 dev = class_find_device(&gpio_class, NULL, desc, match_export);
943 if (dev == NULL) {
944 status = -ENODEV;
945 goto unlock;
946 }
947 }
948
949 status = sysfs_set_active_low(desc, dev, value);
950
951unlock:
952 mutex_unlock(&sysfs_lock);
953
Jani Nikula07697462009-12-15 16:46:20 -0800954 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900955 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
956 status);
Jani Nikula07697462009-12-15 16:46:20 -0800957
958 return status;
959}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700960EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
Jani Nikula07697462009-12-15 16:46:20 -0800961
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700962/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700963 * gpiod_unexport - reverse effect of gpio_export()
David Brownelld8f388d2008-07-25 01:46:07 -0700964 * @gpio: gpio to make unavailable
965 *
966 * This is implicit on gpio_free().
967 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700968void gpiod_unexport(struct gpio_desc *desc)
David Brownelld8f388d2008-07-25 01:46:07 -0700969{
Jon Povey6a99ad42010-07-27 13:18:06 -0700970 int status = 0;
Ming Lei864533c2012-02-13 22:53:20 +0800971 struct device *dev = NULL;
David Brownelld8f388d2008-07-25 01:46:07 -0700972
Alexandre Courbot372e7222013-02-03 01:29:29 +0900973 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900974 pr_warn("%s: invalid GPIO\n", __func__);
975 return;
Jon Povey6a99ad42010-07-27 13:18:06 -0700976 }
David Brownelld8f388d2008-07-25 01:46:07 -0700977
978 mutex_lock(&sysfs_lock);
979
David Brownelld8f388d2008-07-25 01:46:07 -0700980 if (test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700981
982 dev = class_find_device(&gpio_class, NULL, desc, match_export);
983 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700984 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d2008-07-25 01:46:07 -0700985 clear_bit(FLAG_EXPORT, &desc->flags);
David Brownelld8f388d2008-07-25 01:46:07 -0700986 } else
987 status = -ENODEV;
988 }
989
990 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900991
Ming Lei864533c2012-02-13 22:53:20 +0800992 if (dev) {
993 device_unregister(dev);
994 put_device(dev);
995 }
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900996
David Brownelld8f388d2008-07-25 01:46:07 -0700997 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900998 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
999 status);
1000}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001001EXPORT_SYMBOL_GPL(gpiod_unexport);
David Brownelld8f388d2008-07-25 01:46:07 -07001002
1003static int gpiochip_export(struct gpio_chip *chip)
1004{
1005 int status;
1006 struct device *dev;
1007
1008 /* Many systems register gpio chips for SOC support very early,
1009 * before driver model support is available. In those cases we
1010 * export this later, in gpiolib_sysfs_init() ... here we just
1011 * verify that _some_ field of gpio_class got initialized.
1012 */
1013 if (!gpio_class.p)
1014 return 0;
1015
1016 /* use chip->base for the ID; it's already known to be unique */
1017 mutex_lock(&sysfs_lock);
1018 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
1019 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001020 if (!IS_ERR(dev)) {
David Brownelld8f388d2008-07-25 01:46:07 -07001021 status = sysfs_create_group(&dev->kobj,
1022 &gpiochip_attr_group);
1023 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001024 status = PTR_ERR(dev);
David Brownelld8f388d2008-07-25 01:46:07 -07001025 chip->exported = (status == 0);
1026 mutex_unlock(&sysfs_lock);
1027
1028 if (status) {
1029 unsigned long flags;
1030 unsigned gpio;
1031
1032 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001033 gpio = 0;
1034 while (gpio < chip->ngpio)
1035 chip->desc[gpio++].chip = NULL;
David Brownelld8f388d2008-07-25 01:46:07 -07001036 spin_unlock_irqrestore(&gpio_lock, flags);
1037
1038 pr_debug("%s: chip %s status %d\n", __func__,
1039 chip->label, status);
1040 }
1041
1042 return status;
1043}
1044
1045static void gpiochip_unexport(struct gpio_chip *chip)
1046{
1047 int status;
1048 struct device *dev;
1049
1050 mutex_lock(&sysfs_lock);
1051 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1052 if (dev) {
1053 put_device(dev);
1054 device_unregister(dev);
Linus Walleij9fb1f392013-12-04 14:42:46 +01001055 chip->exported = false;
David Brownelld8f388d2008-07-25 01:46:07 -07001056 status = 0;
1057 } else
1058 status = -ENODEV;
1059 mutex_unlock(&sysfs_lock);
1060
1061 if (status)
1062 pr_debug("%s: chip %s status %d\n", __func__,
1063 chip->label, status);
1064}
1065
1066static int __init gpiolib_sysfs_init(void)
1067{
1068 int status;
1069 unsigned long flags;
Alexandre Courbot65493e32013-02-03 01:29:25 +09001070 struct gpio_chip *chip;
David Brownelld8f388d2008-07-25 01:46:07 -07001071
1072 status = class_register(&gpio_class);
1073 if (status < 0)
1074 return status;
1075
1076 /* Scan and register the gpio_chips which registered very
1077 * early (e.g. before the class_register above was called).
1078 *
1079 * We run before arch_initcall() so chip->dev nodes can have
1080 * registered, and so arch_initcall() can always gpio_export().
1081 */
1082 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot65493e32013-02-03 01:29:25 +09001083 list_for_each_entry(chip, &gpio_chips, list) {
David Brownelld8f388d2008-07-25 01:46:07 -07001084 if (!chip || chip->exported)
1085 continue;
1086
1087 spin_unlock_irqrestore(&gpio_lock, flags);
1088 status = gpiochip_export(chip);
1089 spin_lock_irqsave(&gpio_lock, flags);
1090 }
1091 spin_unlock_irqrestore(&gpio_lock, flags);
1092
1093
1094 return status;
1095}
1096postcore_initcall(gpiolib_sysfs_init);
1097
1098#else
1099static inline int gpiochip_export(struct gpio_chip *chip)
1100{
1101 return 0;
1102}
1103
1104static inline void gpiochip_unexport(struct gpio_chip *chip)
1105{
1106}
1107
1108#endif /* CONFIG_GPIO_SYSFS */
1109
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001110/*
1111 * Add a new chip to the global chips list, keeping the list of chips sorted
1112 * by base order.
1113 *
1114 * Return -EBUSY if the new chip overlaps with some other chip's integer
1115 * space.
1116 */
1117static int gpiochip_add_to_list(struct gpio_chip *chip)
1118{
1119 struct list_head *pos = &gpio_chips;
1120 struct gpio_chip *_chip;
1121 int err = 0;
1122
1123 /* find where to insert our chip */
1124 list_for_each(pos, &gpio_chips) {
1125 _chip = list_entry(pos, struct gpio_chip, list);
1126 /* shall we insert before _chip? */
1127 if (_chip->base >= chip->base + chip->ngpio)
1128 break;
1129 }
1130
1131 /* are we stepping on the chip right before? */
1132 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1133 _chip = list_entry(pos->prev, struct gpio_chip, list);
1134 if (_chip->base + _chip->ngpio > chip->base) {
1135 dev_err(chip->dev,
1136 "GPIO integer space overlap, cannot add chip\n");
1137 err = -EBUSY;
1138 }
1139 }
1140
1141 if (!err)
1142 list_add_tail(&chip->list, pos);
1143
1144 return err;
1145}
1146
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001147/**
David Brownelld2876d02008-02-04 22:28:20 -08001148 * gpiochip_add() - register a gpio_chip
1149 * @chip: the chip to register, with chip->base initialized
1150 * Context: potentially before irqs or kmalloc will work
1151 *
1152 * Returns a negative errno if the chip can't be registered, such as
1153 * because the chip->base is invalid or already associated with a
1154 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001155 *
David Brownelld8f388d2008-07-25 01:46:07 -07001156 * When gpiochip_add() is called very early during boot, so that GPIOs
1157 * can be freely used, the chip->dev device must be registered before
1158 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1159 * for GPIOs will fail rudely.
1160 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001161 * If chip->base is negative, this requests dynamic assignment of
1162 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001163 */
1164int gpiochip_add(struct gpio_chip *chip)
1165{
1166 unsigned long flags;
1167 int status = 0;
1168 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001169 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001170
Trent Piephobff5fda2008-05-23 13:04:44 -07001171 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001172 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001173 status = -EINVAL;
1174 goto fail;
1175 }
1176
1177 spin_lock_irqsave(&gpio_lock, flags);
1178
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001179 if (base < 0) {
1180 base = gpiochip_find_base(chip->ngpio);
1181 if (base < 0) {
1182 status = base;
David Brownelld8f388d2008-07-25 01:46:07 -07001183 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001184 }
1185 chip->base = base;
1186 }
1187
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001188 status = gpiochip_add_to_list(chip);
1189
David Brownelld2876d02008-02-04 22:28:20 -08001190 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001191 chip->desc = &gpio_desc[chip->base];
1192
1193 for (id = 0; id < chip->ngpio; id++) {
1194 struct gpio_desc *desc = &chip->desc[id];
1195 desc->chip = chip;
David Brownelld8f388d2008-07-25 01:46:07 -07001196
1197 /* REVISIT: most hardware initializes GPIOs as
1198 * inputs (often with pullups enabled) so power
1199 * usage is minimized. Linux code should set the
1200 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +03001201 * and in case chip->get_direction is not set,
David Brownelld8f388d2008-07-25 01:46:07 -07001202 * we may expose the wrong direction in sysfs.
1203 */
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001204 desc->flags = !chip->direction_input
David Brownelld8f388d2008-07-25 01:46:07 -07001205 ? (1 << FLAG_IS_OUT)
1206 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001207 }
1208 }
1209
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001210 spin_unlock_irqrestore(&gpio_lock, flags);
1211
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301212#ifdef CONFIG_PINCTRL
1213 INIT_LIST_HEAD(&chip->pin_ranges);
1214#endif
1215
Anton Vorontsov391c9702010-06-08 07:48:17 -06001216 of_gpiochip_add(chip);
1217
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001218 if (status)
1219 goto fail;
1220
1221 status = gpiochip_export(chip);
1222 if (status)
1223 goto fail;
1224
H Hartley Sweetenee1c1e72012-05-11 16:58:33 -07001225 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
Grant Likely64842aa2011-11-06 11:36:18 -07001226 chip->base, chip->base + chip->ngpio - 1,
1227 chip->label ? : "generic");
1228
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001229 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001230
1231unlock:
1232 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld2876d02008-02-04 22:28:20 -08001233fail:
1234 /* failures here can mean systems won't boot... */
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001235 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1236 chip->base, chip->base + chip->ngpio - 1,
1237 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001238 return status;
1239}
1240EXPORT_SYMBOL_GPL(gpiochip_add);
1241
1242/**
1243 * gpiochip_remove() - unregister a gpio_chip
1244 * @chip: the chip to unregister
1245 *
1246 * A gpio_chip with any GPIOs still requested may not be removed.
1247 */
1248int gpiochip_remove(struct gpio_chip *chip)
1249{
1250 unsigned long flags;
1251 int status = 0;
1252 unsigned id;
1253
1254 spin_lock_irqsave(&gpio_lock, flags);
1255
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001256 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001257 of_gpiochip_remove(chip);
1258
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001259 for (id = 0; id < chip->ngpio; id++) {
1260 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
David Brownelld2876d02008-02-04 22:28:20 -08001261 status = -EBUSY;
1262 break;
1263 }
1264 }
1265 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001266 for (id = 0; id < chip->ngpio; id++)
1267 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001268
1269 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -08001270 }
1271
1272 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d2008-07-25 01:46:07 -07001273
1274 if (status == 0)
1275 gpiochip_unexport(chip);
1276
David Brownelld2876d02008-02-04 22:28:20 -08001277 return status;
1278}
1279EXPORT_SYMBOL_GPL(gpiochip_remove);
1280
Grant Likely594fa262010-06-08 07:48:16 -06001281/**
1282 * gpiochip_find() - iterator for locating a specific gpio_chip
1283 * @data: data to pass to match function
1284 * @callback: Callback function to check gpio_chip
1285 *
1286 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1287 * determined by a user supplied @match callback. The callback should return
1288 * 0 if the device doesn't match and non-zero if it does. If the callback is
1289 * non-zero, this function will return to the caller and not iterate over any
1290 * more gpio_chips.
1291 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001292struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001293 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001294 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001295{
Alexandre Courbot125eef92013-02-03 01:29:26 +09001296 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001297 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001298
1299 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +09001300 list_for_each_entry(chip, &gpio_chips, list)
1301 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001302 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001303
1304 /* No match? */
1305 if (&chip->list == &gpio_chips)
1306 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001307 spin_unlock_irqrestore(&gpio_lock, flags);
1308
1309 return chip;
1310}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001311EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001312
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001313static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1314{
1315 const char *name = data;
1316
1317 return !strcmp(chip->label, name);
1318}
1319
1320static struct gpio_chip *find_chip_by_name(const char *name)
1321{
1322 return gpiochip_find((void *)name, gpiochip_match_name);
1323}
1324
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301325#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001326
Linus Walleij3f0f8672012-11-20 12:40:15 +01001327/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001328 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1329 * @chip: the gpiochip to add the range for
1330 * @pinctrl: the dev_name() of the pin controller to map to
1331 * @gpio_offset: the start offset in the current gpio_chip number space
1332 * @pin_group: name of the pin group inside the pin controller
1333 */
1334int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1335 struct pinctrl_dev *pctldev,
1336 unsigned int gpio_offset, const char *pin_group)
1337{
1338 struct gpio_pin_range *pin_range;
1339 int ret;
1340
1341 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1342 if (!pin_range) {
1343 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1344 chip->label);
1345 return -ENOMEM;
1346 }
1347
1348 /* Use local offset as range ID */
1349 pin_range->range.id = gpio_offset;
1350 pin_range->range.gc = chip;
1351 pin_range->range.name = chip->label;
1352 pin_range->range.base = chip->base + gpio_offset;
1353 pin_range->pctldev = pctldev;
1354
1355 ret = pinctrl_get_group_pins(pctldev, pin_group,
1356 &pin_range->range.pins,
1357 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001358 if (ret < 0) {
1359 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001360 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001361 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001362
1363 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1364
1365 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PINGRP %s\n",
1366 chip->label, gpio_offset,
1367 gpio_offset + pin_range->range.npins - 1,
1368 pinctrl_dev_get_devname(pctldev), pin_group);
1369
1370 list_add_tail(&pin_range->node, &chip->pin_ranges);
1371
1372 return 0;
1373}
1374EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1375
1376/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001377 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1378 * @chip: the gpiochip to add the range for
1379 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001380 * @gpio_offset: the start offset in the current gpio_chip number space
1381 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001382 * @npins: the number of pins from the offset of each pin space (GPIO and
1383 * pin controller) to accumulate in this range
1384 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001385int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001386 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001387 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301388{
1389 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001390 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301391
Linus Walleij3f0f8672012-11-20 12:40:15 +01001392 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301393 if (!pin_range) {
1394 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1395 chip->label);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001396 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301397 }
1398
Linus Walleij3f0f8672012-11-20 12:40:15 +01001399 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001400 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001401 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301402 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001403 pin_range->range.base = chip->base + gpio_offset;
1404 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301405 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001406 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301407 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001408 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001409 ret = PTR_ERR(pin_range->pctldev);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001410 pr_err("%s: GPIO chip: could not create pin range\n",
1411 chip->label);
1412 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001413 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001414 }
Linus Walleij316511c2012-11-21 08:48:09 +01001415 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1416 chip->label, gpio_offset, gpio_offset + npins - 1,
1417 pinctl_name,
1418 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301419
1420 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001421
1422 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301423}
Linus Walleij165adc92012-11-06 14:49:39 +01001424EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301425
Linus Walleij3f0f8672012-11-20 12:40:15 +01001426/**
1427 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1428 * @chip: the chip to remove all the mappings for
1429 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301430void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1431{
1432 struct gpio_pin_range *pin_range, *tmp;
1433
1434 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1435 list_del(&pin_range->node);
1436 pinctrl_remove_gpio_range(pin_range->pctldev,
1437 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001438 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301439 }
1440}
Linus Walleij165adc92012-11-06 14:49:39 +01001441EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1442
1443#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301444
David Brownelld2876d02008-02-04 22:28:20 -08001445/* These "optional" allocation calls help prevent drivers from stomping
1446 * on each other, and help provide better diagnostics in debugfs.
1447 * They're called even less than the "set direction" calls.
1448 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001449static int gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001450{
David Brownell35e8bb52008-10-15 22:03:16 -07001451 struct gpio_chip *chip;
Mark Browne9354572012-07-09 12:22:56 +01001452 int status = -EPROBE_DEFER;
David Brownelld2876d02008-02-04 22:28:20 -08001453 unsigned long flags;
1454
Alexandre Courbot0204df42013-10-04 10:59:58 -07001455 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001456 pr_warn("%s: invalid GPIO\n", __func__);
1457 return -EINVAL;
1458 }
1459
David Brownelld2876d02008-02-04 22:28:20 -08001460 spin_lock_irqsave(&gpio_lock, flags);
1461
David Brownell35e8bb52008-10-15 22:03:16 -07001462 chip = desc->chip;
Alexandre Courbot0204df42013-10-04 10:59:58 -07001463 if (chip == NULL)
1464 goto done;
David Brownelld2876d02008-02-04 22:28:20 -08001465
David Brownell35e8bb52008-10-15 22:03:16 -07001466 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001467 goto done;
1468
David Brownelld2876d02008-02-04 22:28:20 -08001469 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001470 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001471 */
1472
1473 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1474 desc_set_label(desc, label ? : "?");
1475 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001476 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001477 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001478 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001479 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001480 }
1481
1482 if (chip->request) {
1483 /* chip->request may sleep */
1484 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001485 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001486 spin_lock_irqsave(&gpio_lock, flags);
1487
1488 if (status < 0) {
1489 desc_set_label(desc, NULL);
1490 module_put(chip->owner);
1491 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001492 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001493 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001494 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001495 if (chip->get_direction) {
1496 /* chip->get_direction may sleep */
1497 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001498 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001499 spin_lock_irqsave(&gpio_lock, flags);
1500 }
David Brownelld2876d02008-02-04 22:28:20 -08001501done:
1502 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001503 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001504 desc_to_gpio(desc), label ? : "?", status);
David Brownelld2876d02008-02-04 22:28:20 -08001505 spin_unlock_irqrestore(&gpio_lock, flags);
1506 return status;
1507}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001508
1509int gpio_request(unsigned gpio, const char *label)
1510{
1511 return gpiod_request(gpio_to_desc(gpio), label);
1512}
David Brownelld2876d02008-02-04 22:28:20 -08001513EXPORT_SYMBOL_GPL(gpio_request);
1514
Alexandre Courbot372e7222013-02-03 01:29:29 +09001515static void gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001516{
1517 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001518 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001519
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001520 might_sleep();
1521
Alexandre Courbot372e7222013-02-03 01:29:29 +09001522 if (!desc) {
David Brownelld2876d02008-02-04 22:28:20 -08001523 WARN_ON(extra_checks);
1524 return;
1525 }
1526
Alexandre Courbot372e7222013-02-03 01:29:29 +09001527 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001528
David Brownelld2876d02008-02-04 22:28:20 -08001529 spin_lock_irqsave(&gpio_lock, flags);
1530
David Brownell35e8bb52008-10-15 22:03:16 -07001531 chip = desc->chip;
1532 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1533 if (chip->free) {
1534 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001535 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001536 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001537 spin_lock_irqsave(&gpio_lock, flags);
1538 }
David Brownelld2876d02008-02-04 22:28:20 -08001539 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001540 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001541 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001542 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301543 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301544 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001545 } else
David Brownelld2876d02008-02-04 22:28:20 -08001546 WARN_ON(extra_checks);
1547
1548 spin_unlock_irqrestore(&gpio_lock, flags);
1549}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001550
1551void gpio_free(unsigned gpio)
1552{
1553 gpiod_free(gpio_to_desc(gpio));
1554}
David Brownelld2876d02008-02-04 22:28:20 -08001555EXPORT_SYMBOL_GPL(gpio_free);
1556
Eric Miao3e45f1d2010-03-05 13:44:35 -08001557/**
1558 * gpio_request_one - request a single GPIO with initial configuration
1559 * @gpio: the GPIO number
1560 * @flags: GPIO configuration as specified by GPIOF_*
1561 * @label: a literal description string of this GPIO
1562 */
1563int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1564{
Alexandre Courbot372e7222013-02-03 01:29:29 +09001565 struct gpio_desc *desc;
Eric Miao3e45f1d2010-03-05 13:44:35 -08001566 int err;
1567
Alexandre Courbot372e7222013-02-03 01:29:29 +09001568 desc = gpio_to_desc(gpio);
1569
1570 err = gpiod_request(desc, label);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001571 if (err)
1572 return err;
1573
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301574 if (flags & GPIOF_OPEN_DRAIN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001575 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301576
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301577 if (flags & GPIOF_OPEN_SOURCE)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001578 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301579
Eric Miao3e45f1d2010-03-05 13:44:35 -08001580 if (flags & GPIOF_DIR_IN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001581 err = gpiod_direction_input(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001582 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001583 err = gpiod_direction_output(desc,
Eric Miao3e45f1d2010-03-05 13:44:35 -08001584 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1585
Aaro Koskinene2548112010-12-21 17:24:22 -08001586 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001587 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -08001588
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001589 if (flags & GPIOF_EXPORT) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001590 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001591 if (err)
1592 goto free_gpio;
1593 }
1594
1595 return 0;
1596
1597 free_gpio:
Alexandre Courbot372e7222013-02-03 01:29:29 +09001598 gpiod_free(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001599 return err;
1600}
1601EXPORT_SYMBOL_GPL(gpio_request_one);
1602
1603/**
1604 * gpio_request_array - request multiple GPIOs in a single call
1605 * @array: array of the 'struct gpio'
1606 * @num: how many GPIOs in the array
1607 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001608int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001609{
1610 int i, err;
1611
1612 for (i = 0; i < num; i++, array++) {
1613 err = gpio_request_one(array->gpio, array->flags, array->label);
1614 if (err)
1615 goto err_free;
1616 }
1617 return 0;
1618
1619err_free:
1620 while (i--)
1621 gpio_free((--array)->gpio);
1622 return err;
1623}
1624EXPORT_SYMBOL_GPL(gpio_request_array);
1625
1626/**
1627 * gpio_free_array - release multiple GPIOs in a single call
1628 * @array: array of the 'struct gpio'
1629 * @num: how many GPIOs in the array
1630 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001631void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001632{
1633 while (num--)
1634 gpio_free((array++)->gpio);
1635}
1636EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001637
1638/**
1639 * gpiochip_is_requested - return string iff signal was requested
1640 * @chip: controller managing the signal
1641 * @offset: of signal within controller's 0..(ngpio - 1) range
1642 *
1643 * Returns NULL if the GPIO is not currently requested, else a string.
1644 * If debugfs support is enabled, the string returned is the label passed
1645 * to gpio_request(); otherwise it is a meaningless constant.
1646 *
1647 * This function is for use by GPIO controller drivers. The label can
1648 * help with diagnostics, and knowing that the signal is used as a GPIO
1649 * can help avoid accidentally multiplexing it to another controller.
1650 */
1651const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1652{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001653 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001654
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001655 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -08001656 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001657
1658 desc = &chip->desc[offset];
1659
Alexandre Courbot372e7222013-02-03 01:29:29 +09001660 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001661 return NULL;
1662#ifdef CONFIG_DEBUG_FS
Alexandre Courbot372e7222013-02-03 01:29:29 +09001663 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001664#else
1665 return "?";
1666#endif
1667}
1668EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1669
1670
1671/* Drivers MUST set GPIO direction before making get/set calls. In
1672 * some cases this is done in early boot, before IRQs are enabled.
1673 *
1674 * As a rule these aren't called more than once (except for drivers
1675 * using the open-drain emulation idiom) so these are natural places
1676 * to accumulate extra debugging checks. Note that we can't (yet)
1677 * rely on gpio_request() having been called beforehand.
1678 */
1679
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001680/**
1681 * gpiod_direction_input - set the GPIO direction to input
1682 * @desc: GPIO to set to input
1683 *
1684 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1685 * be called safely on it.
1686 *
1687 * Return 0 in case of success, else an error code.
1688 */
1689int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001690{
1691 unsigned long flags;
1692 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001693 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001694 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001695
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001696 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001697 pr_warn("%s: invalid GPIO\n", __func__);
1698 return -EINVAL;
1699 }
1700
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001701 chip = desc->chip;
1702 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001703 gpiod_warn(desc,
1704 "%s: missing get() or direction_input() operations\n",
1705 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001706 return -EIO;
1707 }
1708
David Brownelld2876d02008-02-04 22:28:20 -08001709 spin_lock_irqsave(&gpio_lock, flags);
1710
Alexandre Courbot372e7222013-02-03 01:29:29 +09001711 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001712 if (status < 0)
1713 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001714
1715 /* now we know the gpio is valid and chip won't vanish */
1716
1717 spin_unlock_irqrestore(&gpio_lock, flags);
1718
David Brownell9c4ba942010-08-10 18:02:24 -07001719 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001720
Alexandre Courbot372e7222013-02-03 01:29:29 +09001721 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001722 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001723 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001724 if (status < 0) {
Mark Brown6424de52013-09-09 10:33:49 +01001725 gpiod_dbg(desc, "chip request fail, %d\n", status);
David Brownell35e8bb52008-10-15 22:03:16 -07001726 /* and it's not available to anyone else ...
1727 * gpio_request() is the fully clean solution.
1728 */
1729 goto lose;
1730 }
1731 }
1732
Alexandre Courbot372e7222013-02-03 01:29:29 +09001733 status = chip->direction_input(chip, offset);
David Brownelld2876d02008-02-04 22:28:20 -08001734 if (status == 0)
1735 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001736
Alexandre Courbot372e7222013-02-03 01:29:29 +09001737 trace_gpio_direction(desc_to_gpio(desc), 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001738lose:
David Brownelld2876d02008-02-04 22:28:20 -08001739 return status;
1740fail:
1741 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001742 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001743 gpiod_dbg(desc, "%s status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001744 return status;
1745}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001746EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001747
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001748/**
1749 * gpiod_direction_output - set the GPIO direction to input
1750 * @desc: GPIO to set to output
1751 * @value: initial output value of the GPIO
1752 *
1753 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1754 * be called safely on it. The initial value of the output must be specified.
1755 *
1756 * Return 0 in case of success, else an error code.
1757 */
1758int gpiod_direction_output(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001759{
1760 unsigned long flags;
1761 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001762 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001763 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001764
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001765 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001766 pr_warn("%s: invalid GPIO\n", __func__);
1767 return -EINVAL;
1768 }
1769
Linus Walleijd468bf92013-09-24 11:54:38 +02001770 /* GPIOs used for IRQs shall not be set as output */
1771 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1772 gpiod_err(desc,
1773 "%s: tried to set a GPIO tied to an IRQ as output\n",
1774 __func__);
1775 return -EIO;
1776 }
1777
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301778 /* Open drain pin should not be driven to 1 */
1779 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001780 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301781
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301782 /* Open source pin should not be driven to 0 */
1783 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001784 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301785
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001786 chip = desc->chip;
1787 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001788 gpiod_warn(desc,
1789 "%s: missing set() or direction_output() operations\n",
1790 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001791 return -EIO;
1792 }
1793
David Brownelld2876d02008-02-04 22:28:20 -08001794 spin_lock_irqsave(&gpio_lock, flags);
1795
Alexandre Courbot372e7222013-02-03 01:29:29 +09001796 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001797 if (status < 0)
1798 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001799
1800 /* now we know the gpio is valid and chip won't vanish */
1801
1802 spin_unlock_irqrestore(&gpio_lock, flags);
1803
David Brownell9c4ba942010-08-10 18:02:24 -07001804 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001805
Alexandre Courbot372e7222013-02-03 01:29:29 +09001806 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001807 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001808 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001809 if (status < 0) {
Mark Brown6424de52013-09-09 10:33:49 +01001810 gpiod_dbg(desc, "chip request fail, %d\n", status);
David Brownell35e8bb52008-10-15 22:03:16 -07001811 /* and it's not available to anyone else ...
1812 * gpio_request() is the fully clean solution.
1813 */
1814 goto lose;
1815 }
1816 }
1817
Alexandre Courbot372e7222013-02-03 01:29:29 +09001818 status = chip->direction_output(chip, offset, value);
David Brownelld2876d02008-02-04 22:28:20 -08001819 if (status == 0)
1820 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001821 trace_gpio_value(desc_to_gpio(desc), 0, value);
1822 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001823lose:
David Brownelld2876d02008-02-04 22:28:20 -08001824 return status;
1825fail:
1826 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001827 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001828 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001829 return status;
1830}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001831EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001832
Felipe Balbic4b5be92010-05-26 14:42:23 -07001833/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001834 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001835 * @gpio: the gpio to set debounce time
1836 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001837 *
1838 * returns -ENOTSUPP if the controller does not support setting
1839 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001840 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001841int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001842{
1843 unsigned long flags;
1844 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001845 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001846 int offset;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001847
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001848 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001849 pr_warn("%s: invalid GPIO\n", __func__);
1850 return -EINVAL;
1851 }
1852
Felipe Balbic4b5be92010-05-26 14:42:23 -07001853 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001854 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001855 gpiod_dbg(desc,
1856 "%s: missing set() or set_debounce() operations\n",
1857 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001858 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001859 }
1860
1861 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001862
1863 status = gpio_ensure_requested(desc);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001864 if (status < 0)
1865 goto fail;
1866
1867 /* now we know the gpio is valid and chip won't vanish */
1868
1869 spin_unlock_irqrestore(&gpio_lock, flags);
1870
David Brownell9c4ba942010-08-10 18:02:24 -07001871 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001872
Alexandre Courbot372e7222013-02-03 01:29:29 +09001873 offset = gpio_chip_hwgpio(desc);
1874 return chip->set_debounce(chip, offset, debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001875
1876fail:
1877 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001878 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001879 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001880
1881 return status;
1882}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001883EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001884
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001885/**
1886 * gpiod_is_active_low - test whether a GPIO is active-low or not
1887 * @desc: the gpio descriptor to test
1888 *
1889 * Returns 1 if the GPIO is active-low, 0 otherwise.
1890 */
1891int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001892{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001893 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001894}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001895EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001896
1897/* I/O calls are only valid after configuration completed; the relevant
1898 * "is this a valid GPIO" error checks should already have been done.
1899 *
1900 * "Get" operations are often inlinable as reading a pin value register,
1901 * and masking the relevant bit in that register.
1902 *
1903 * When "set" operations are inlinable, they involve writing that mask to
1904 * one register to set a low value, or a different register to set it high.
1905 * Otherwise locking is needed, so there may be little value to inlining.
1906 *
1907 *------------------------------------------------------------------------
1908 *
1909 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1910 * have requested the GPIO. That can include implicit requesting by
1911 * a direction setting call. Marking a gpio as requested locks its chip
1912 * in memory, guaranteeing that these table lookups need no more locking
1913 * and that gpiochip_remove() will fail.
1914 *
1915 * REVISIT when debugging, consider adding some instrumentation to ensure
1916 * that the GPIO was actually requested.
1917 */
1918
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001919static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001920{
1921 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001922 int value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001923 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001924
Alexandre Courbot372e7222013-02-03 01:29:29 +09001925 chip = desc->chip;
1926 offset = gpio_chip_hwgpio(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001927 value = chip->get ? chip->get(chip, offset) : 0;
1928 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001929 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001930}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001931
David Brownelld2876d02008-02-04 22:28:20 -08001932/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001933 * gpiod_get_raw_value() - return a gpio's raw value
1934 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001935 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001936 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1937 * its ACTIVE_LOW status.
1938 *
1939 * This function should be called from contexts where we cannot sleep, and will
1940 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001941 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001942int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001943{
David Brownelld2876d02008-02-04 22:28:20 -08001944 if (!desc)
1945 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001946 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001947 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001948 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001949}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001950EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001951
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001952/**
1953 * gpiod_get_value() - return a gpio's value
1954 * @desc: gpio whose value will be returned
1955 *
1956 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1957 * account.
1958 *
1959 * This function should be called from contexts where we cannot sleep, and will
1960 * complain if the GPIO chip functions potentially sleep.
1961 */
1962int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001963{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001964 int value;
1965 if (!desc)
1966 return 0;
1967 /* Should be using gpio_get_value_cansleep() */
1968 WARN_ON(desc->chip->can_sleep);
1969
1970 value = _gpiod_get_raw_value(desc);
1971 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1972 value = !value;
1973
1974 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001975}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001976EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001977
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301978/*
1979 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001980 * @desc: gpio descriptor whose state need to be set.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301981 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1982 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001983static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301984{
1985 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001986 struct gpio_chip *chip = desc->chip;
1987 int offset = gpio_chip_hwgpio(desc);
1988
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301989 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001990 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301991 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001992 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301993 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001994 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301995 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001996 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301997 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001998 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301999 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002000 gpiod_err(desc,
2001 "%s: Error in set_value for open drain err %d\n",
2002 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302003}
2004
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302005/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002006 * _gpio_set_open_source_value() - Set the open source gpio's value.
2007 * @desc: gpio descriptor whose state need to be set.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302008 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
2009 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09002010static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302011{
2012 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002013 struct gpio_chip *chip = desc->chip;
2014 int offset = gpio_chip_hwgpio(desc);
2015
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302016 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002017 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302018 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002019 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302020 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002021 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302022 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002023 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302024 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002025 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302026 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002027 gpiod_err(desc,
2028 "%s: Error in set_value for open source err %d\n",
2029 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302030}
2031
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002032static void _gpiod_set_raw_value(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002033{
2034 struct gpio_chip *chip;
2035
Alexandre Courbot372e7222013-02-03 01:29:29 +09002036 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002037 trace_gpio_value(desc_to_gpio(desc), 0, value);
2038 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2039 _gpio_set_open_drain_value(desc, value);
2040 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2041 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302042 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002043 chip->set(chip, gpio_chip_hwgpio(desc), value);
2044}
2045
David Brownelld2876d02008-02-04 22:28:20 -08002046/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002047 * gpiod_set_raw_value() - assign a gpio's raw value
2048 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002049 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002050 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002051 * Set the raw value of the GPIO, i.e. the value of its physical line without
2052 * regard for its ACTIVE_LOW status.
2053 *
2054 * This function should be called from contexts where we cannot sleep, and will
2055 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002056 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002057void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002058{
David Brownelld2876d02008-02-04 22:28:20 -08002059 if (!desc)
2060 return;
David Brownelld2876d02008-02-04 22:28:20 -08002061 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09002062 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002063 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002064}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002065EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002066
2067/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002068 * gpiod_set_value() - assign a gpio's value
2069 * @desc: gpio whose value will be assigned
2070 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002071 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002072 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2073 * account
2074 *
2075 * This function should be called from contexts where we cannot sleep, and will
2076 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002077 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002078void gpiod_set_value(struct gpio_desc *desc, int value)
2079{
2080 if (!desc)
2081 return;
2082 /* Should be using gpio_set_value_cansleep() */
2083 WARN_ON(desc->chip->can_sleep);
2084 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2085 value = !value;
2086 _gpiod_set_raw_value(desc, value);
2087}
2088EXPORT_SYMBOL_GPL(gpiod_set_value);
2089
2090/**
2091 * gpiod_cansleep() - report whether gpio value access may sleep
2092 * @desc: gpio to check
2093 *
2094 */
2095int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002096{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002097 if (!desc)
2098 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002099 return desc->chip->can_sleep;
2100}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002101EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002102
David Brownell0f6d5042008-10-15 22:03:14 -07002103/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002104 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2105 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002106 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002107 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2108 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002109 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002110int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002111{
2112 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002113 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002114
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002115 if (!desc)
2116 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002117 chip = desc->chip;
2118 offset = gpio_chip_hwgpio(desc);
2119 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
2120}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002121EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002122
Linus Walleijd468bf92013-09-24 11:54:38 +02002123/**
2124 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
2125 * @gpio: the GPIO line to lock as used for IRQ
2126 *
2127 * This is used directly by GPIO drivers that want to lock down
2128 * a certain GPIO line to be used as IRQs, for example in the
2129 * .to_irq() callback of their gpio_chip, or in the .irq_enable()
2130 * of its irq_chip implementation if the GPIO is known from that
2131 * code.
David Brownelld2876d02008-02-04 22:28:20 -08002132 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002133int gpiod_lock_as_irq(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002134{
Linus Walleijd468bf92013-09-24 11:54:38 +02002135 if (!desc)
2136 return -EINVAL;
2137
2138 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
2139 gpiod_err(desc,
2140 "%s: tried to flag a GPIO set as output for IRQ\n",
2141 __func__);
2142 return -EIO;
2143 }
2144
2145 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
2146 return 0;
2147}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002148EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002149
2150int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
2151{
2152 return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset));
2153}
2154EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
2155
2156/**
2157 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
2158 * @gpio: the GPIO line to unlock from IRQ usage
2159 *
2160 * This is used directly by GPIO drivers that want to indicate
2161 * that a certain GPIO is no longer used exclusively for IRQ.
2162 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002163void gpiod_unlock_as_irq(struct gpio_desc *desc)
Linus Walleijd468bf92013-09-24 11:54:38 +02002164{
2165 if (!desc)
2166 return;
2167
2168 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2169}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002170EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002171
2172void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2173{
2174 return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset));
2175}
2176EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
David Brownelld2876d02008-02-04 22:28:20 -08002177
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002178/**
2179 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2180 * @desc: gpio whose value will be returned
2181 *
2182 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2183 * its ACTIVE_LOW status.
2184 *
2185 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002186 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002187int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002188{
David Brownelld2876d02008-02-04 22:28:20 -08002189 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002190 if (!desc)
2191 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002192 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002193}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002194EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002195
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002196/**
2197 * gpiod_get_value_cansleep() - return a gpio's value
2198 * @desc: gpio whose value will be returned
2199 *
2200 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2201 * account.
2202 *
2203 * This function is to be called from contexts that can sleep.
2204 */
2205int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002206{
David Brownelld2876d02008-02-04 22:28:20 -08002207 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002208
2209 might_sleep_if(extra_checks);
2210 if (!desc)
2211 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002212
2213 value = _gpiod_get_raw_value(desc);
2214 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2215 value = !value;
2216
David Brownelld2876d02008-02-04 22:28:20 -08002217 return value;
2218}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002219EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002220
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002221/**
2222 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2223 * @desc: gpio whose value will be assigned
2224 * @value: value to assign
2225 *
2226 * Set the raw value of the GPIO, i.e. the value of its physical line without
2227 * regard for its ACTIVE_LOW status.
2228 *
2229 * This function is to be called from contexts that can sleep.
2230 */
2231void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002232{
David Brownelld2876d02008-02-04 22:28:20 -08002233 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002234 if (!desc)
2235 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002236 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002237}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002238EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002239
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002240/**
2241 * gpiod_set_value_cansleep() - assign a gpio's value
2242 * @desc: gpio whose value will be assigned
2243 * @value: value to assign
2244 *
2245 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2246 * account
2247 *
2248 * This function is to be called from contexts that can sleep.
2249 */
2250void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002251{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002252 might_sleep_if(extra_checks);
2253 if (!desc)
2254 return;
2255
2256 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2257 value = !value;
2258 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002259}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002260EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002261
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002262/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002263 * gpiod_add_lookup_table() - register GPIO device consumers
2264 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002265 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002266void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002267{
2268 mutex_lock(&gpio_lookup_lock);
2269
Alexandre Courbotad824782013-12-03 12:20:11 +09002270 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002271
2272 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002273}
2274
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002275#ifdef CONFIG_OF
2276static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002277 unsigned int idx,
2278 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002279{
2280 char prop_name[32]; /* 32 is max size of property name */
2281 enum of_gpio_flags of_flags;
2282 struct gpio_desc *desc;
2283
2284 if (con_id)
2285 snprintf(prop_name, 32, "%s-gpios", con_id);
2286 else
2287 snprintf(prop_name, 32, "gpios");
2288
2289 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2290 &of_flags);
2291
2292 if (IS_ERR(desc))
2293 return desc;
2294
2295 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002296 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002297
2298 return desc;
2299}
2300#else
2301static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot209e64f2013-11-19 10:37:29 +09002302 unsigned int idx,
2303 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002304{
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 Courbotad824782013-12-03 12:20:11 +09002326static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2327{
2328 const char *dev_id = dev ? dev_name(dev) : NULL;
2329 struct gpiod_lookup_table *table;
2330
2331 mutex_lock(&gpio_lookup_lock);
2332
2333 list_for_each_entry(table, &gpio_lookup_list, list) {
2334 if (table->dev_id && dev_id) {
2335 /*
2336 * Valid strings on both ends, must be identical to have
2337 * a match
2338 */
2339 if (!strcmp(table->dev_id, dev_id))
2340 goto found;
2341 } else {
2342 /*
2343 * One of the pointers is NULL, so both must be to have
2344 * a match
2345 */
2346 if (dev_id == table->dev_id)
2347 goto found;
2348 }
2349 }
2350 table = NULL;
2351
2352found:
2353 mutex_unlock(&gpio_lookup_lock);
2354 return table;
2355}
2356
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002357static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002358 unsigned int idx,
2359 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002360{
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002361 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Alexandre Courbotad824782013-12-03 12:20:11 +09002362 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002363 struct gpiod_lookup *p;
2364
Alexandre Courbotad824782013-12-03 12:20:11 +09002365 table = gpiod_find_lookup_table(dev);
2366 if (!table)
2367 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002368
Alexandre Courbotad824782013-12-03 12:20:11 +09002369 for (p = &table->table[0]; p->chip_label; p++) {
2370 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002371
Alexandre Courbotad824782013-12-03 12:20:11 +09002372 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002373 if (p->idx != idx)
2374 continue;
2375
Alexandre Courbotad824782013-12-03 12:20:11 +09002376 /* If the lookup entry has a con_id, require exact match */
2377 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2378 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002379
Alexandre Courbotad824782013-12-03 12:20:11 +09002380 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002381
Alexandre Courbotad824782013-12-03 12:20:11 +09002382 if (!chip) {
2383 dev_warn(dev, "cannot find GPIO chip %s\n",
2384 p->chip_label);
2385 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002386 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002387
Alexandre Courbotad824782013-12-03 12:20:11 +09002388 if (chip->ngpio <= p->chip_hwnum) {
2389 dev_warn(dev, "GPIO chip %s has %d GPIOs\n",
2390 chip->label, chip->ngpio);
2391 continue;
2392 }
2393
2394 desc = gpiochip_offset_to_desc(chip, p->chip_hwnum);
2395 *flags = p->flags;
2396 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002397
2398 return desc;
2399}
2400
2401/**
2402 * gpio_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002403 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002404 * @con_id: function within the GPIO consumer
2405 *
2406 * Return the GPIO descriptor corresponding to the function con_id of device
2407 * dev, or an IS_ERR() condition if an error occured.
2408 */
2409struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id)
2410{
2411 return gpiod_get_index(dev, con_id, 0);
2412}
2413EXPORT_SYMBOL_GPL(gpiod_get);
2414
2415/**
2416 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2417 * @dev: GPIO consumer
2418 * @con_id: function within the GPIO consumer
2419 * @idx: index of the GPIO to obtain in the consumer
2420 *
2421 * This variant of gpiod_get() allows to access GPIOs other than the first
2422 * defined one for functions that define several GPIOs.
2423 *
2424 * Return a valid GPIO descriptor, or an IS_ERR() condition in case of error.
2425 */
2426struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2427 const char *con_id,
2428 unsigned int idx)
2429{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002430 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002431 int status;
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002432 enum gpio_lookup_flags flags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002433
2434 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2435
2436 /* Using device tree? */
2437 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
2438 dev_dbg(dev, "using device tree for GPIO lookup\n");
2439 desc = of_find_gpio(dev, con_id, idx, &flags);
Mika Westerberg81f59e92013-10-10 11:01:09 +03002440 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
2441 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2442 desc = acpi_find_gpio(dev, con_id, idx, &flags);
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002443 }
2444
2445 /*
2446 * Either we are not using DT or ACPI, or their lookup did not return
2447 * a result. In that case, use platform lookup as a fallback.
2448 */
2449 if (!desc || IS_ERR(desc)) {
2450 struct gpio_desc *pdesc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002451 dev_dbg(dev, "using lookup tables for GPIO lookup");
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002452 pdesc = gpiod_find(dev, con_id, idx, &flags);
2453 /* If used as fallback, do not replace the previous error */
2454 if (!IS_ERR(pdesc) || !desc)
2455 desc = pdesc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002456 }
2457
2458 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002459 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002460 return desc;
2461 }
2462
2463 status = gpiod_request(desc, con_id);
2464
2465 if (status < 0)
2466 return ERR_PTR(status);
2467
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002468 if (flags & GPIO_ACTIVE_LOW)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002469 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002470 if (flags & GPIO_OPEN_DRAIN)
2471 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2472 if (flags & GPIO_OPEN_SOURCE)
2473 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002474
2475 return desc;
2476}
2477EXPORT_SYMBOL_GPL(gpiod_get_index);
2478
2479/**
2480 * gpiod_put - dispose of a GPIO descriptor
2481 * @desc: GPIO descriptor to dispose of
2482 *
2483 * No descriptor can be used after gpiod_put() has been called on it.
2484 */
2485void gpiod_put(struct gpio_desc *desc)
2486{
2487 gpiod_free(desc);
2488}
2489EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002490
David Brownelld2876d02008-02-04 22:28:20 -08002491#ifdef CONFIG_DEBUG_FS
2492
2493static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2494{
2495 unsigned i;
2496 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002497 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002498 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002499 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002500
2501 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2502 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2503 continue;
2504
Alexandre Courbot372e7222013-02-03 01:29:29 +09002505 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002506 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002507 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2508 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002509 gpio, gdesc->label,
2510 is_out ? "out" : "in ",
2511 chip->get
2512 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002513 : "? ",
2514 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002515 seq_printf(s, "\n");
2516 }
2517}
2518
Thierry Redingf9c4a312012-04-12 13:26:01 +02002519static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002520{
Grant Likely362432a2013-02-09 09:41:49 +00002521 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002522 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002523 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002524
2525 s->private = "";
2526
Grant Likely362432a2013-02-09 09:41:49 +00002527 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002528 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002529 if (index-- == 0) {
2530 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002531 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002532 }
2533 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002534
2535 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002536}
2537
2538static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2539{
Grant Likely362432a2013-02-09 09:41:49 +00002540 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002541 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002542 void *ret = NULL;
2543
Grant Likely362432a2013-02-09 09:41:49 +00002544 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002545 if (list_is_last(&chip->list, &gpio_chips))
2546 ret = NULL;
2547 else
2548 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002549 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002550
2551 s->private = "\n";
2552 ++*pos;
2553
2554 return ret;
2555}
2556
2557static void gpiolib_seq_stop(struct seq_file *s, void *v)
2558{
2559}
2560
2561static int gpiolib_seq_show(struct seq_file *s, void *v)
2562{
2563 struct gpio_chip *chip = v;
2564 struct device *dev;
2565
2566 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2567 chip->base, chip->base + chip->ngpio - 1);
2568 dev = chip->dev;
2569 if (dev)
2570 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2571 dev_name(dev));
2572 if (chip->label)
2573 seq_printf(s, ", %s", chip->label);
2574 if (chip->can_sleep)
2575 seq_printf(s, ", can sleep");
2576 seq_printf(s, ":\n");
2577
2578 if (chip->dbg_show)
2579 chip->dbg_show(s, chip);
2580 else
2581 gpiolib_dbg_show(s, chip);
2582
David Brownelld2876d02008-02-04 22:28:20 -08002583 return 0;
2584}
2585
Thierry Redingf9c4a312012-04-12 13:26:01 +02002586static const struct seq_operations gpiolib_seq_ops = {
2587 .start = gpiolib_seq_start,
2588 .next = gpiolib_seq_next,
2589 .stop = gpiolib_seq_stop,
2590 .show = gpiolib_seq_show,
2591};
2592
David Brownelld2876d02008-02-04 22:28:20 -08002593static int gpiolib_open(struct inode *inode, struct file *file)
2594{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002595 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002596}
2597
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002598static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002599 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002600 .open = gpiolib_open,
2601 .read = seq_read,
2602 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002603 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002604};
2605
2606static int __init gpiolib_debugfs_init(void)
2607{
2608 /* /sys/kernel/debug/gpio */
2609 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2610 NULL, NULL, &gpiolib_operations);
2611 return 0;
2612}
2613subsys_initcall(gpiolib_debugfs_init);
2614
2615#endif /* DEBUG_FS */