blob: 8048c3dbb8add3a775b1a202f41c835e13d6624c [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
David Brownelld2876d02008-02-04 22:28:20 -080015
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060016#define CREATE_TRACE_POINTS
17#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080018
19/* Optional implementation infrastructure for GPIO interfaces.
20 *
21 * Platforms may want to use this if they tend to use very many GPIOs
22 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
23 *
24 * When kernel footprint or instruction count is an issue, simpler
25 * implementations may be preferred. The GPIO programming interface
26 * allows for inlining speed-critical get/set operations for common
27 * cases, so that access to SOC-integrated GPIOs can sometimes cost
28 * only an instruction or two per bit.
29 */
30
31
32/* When debugging, extend minimal trust to callers and platform code.
33 * Also emit diagnostic messages that may help initial bringup, when
34 * board setup or driver bugs are most common.
35 *
36 * Otherwise, minimize overhead in what may be bitbanging codepaths.
37 */
38#ifdef DEBUG
39#define extra_checks 1
40#else
41#define extra_checks 0
42#endif
43
44/* gpio_lock prevents conflicts during gpio_desc[] table updates.
45 * While any GPIO is requested, its gpio_chip is not removable;
46 * each GPIO's "requested" flag serves as a lock and refcount.
47 */
48static DEFINE_SPINLOCK(gpio_lock);
49
50struct gpio_desc {
51 struct gpio_chip *chip;
52 unsigned long flags;
53/* flag symbols are bit numbers */
54#define FLAG_REQUESTED 0
55#define FLAG_IS_OUT 1
Alexandre Courbot710b40e2013-02-02 23:44:06 +090056#define FLAG_EXPORT 2 /* protected by sysfs_lock */
57#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
58#define FLAG_TRIG_FALL 4 /* trigger on falling edge */
59#define FLAG_TRIG_RISE 5 /* trigger on rising edge */
60#define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */
61#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
62#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070063
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070064#define ID_SHIFT 16 /* add new flags before this one */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070065
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070066#define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
Daniel Glöcknerff77c352009-09-22 16:46:38 -070067#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
David Brownelld2876d02008-02-04 22:28:20 -080068
69#ifdef CONFIG_DEBUG_FS
70 const char *label;
71#endif
72};
73static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
74
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +090075#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
76
Alexandre Courbot1a989d02013-02-03 01:29:24 +090077static LIST_HEAD(gpio_chips);
78
Daniel Glöcknerff77c352009-09-22 16:46:38 -070079#ifdef CONFIG_GPIO_SYSFS
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070080static DEFINE_IDR(dirent_idr);
Daniel Glöcknerff77c352009-09-22 16:46:38 -070081#endif
82
Alexandre Courbot372e7222013-02-03 01:29:29 +090083/*
84 * Internal gpiod_* API using descriptors instead of the integer namespace.
85 * Most of this should eventually go public.
86 */
87static int gpiod_request(struct gpio_desc *desc, const char *label);
88static void gpiod_free(struct gpio_desc *desc);
89static int gpiod_direction_input(struct gpio_desc *desc);
90static int gpiod_direction_output(struct gpio_desc *desc, int value);
Alexandre Courbotdef63432013-02-15 14:46:15 +090091static int gpiod_get_direction(const struct gpio_desc *desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090092static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
Alexandre Courbotdef63432013-02-15 14:46:15 +090093static int gpiod_get_value_cansleep(const struct gpio_desc *desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090094static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
Alexandre Courbotdef63432013-02-15 14:46:15 +090095static int gpiod_get_value(const struct gpio_desc *desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090096static void gpiod_set_value(struct gpio_desc *desc, int value);
Alexandre Courbotdef63432013-02-15 14:46:15 +090097static int gpiod_cansleep(const struct gpio_desc *desc);
98static int gpiod_to_irq(const struct gpio_desc *desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090099static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
100static int gpiod_export_link(struct device *dev, const char *name,
101 struct gpio_desc *desc);
102static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
103static void gpiod_unexport(struct gpio_desc *desc);
104
Mark Brown7b17b592013-09-09 10:33:50 +0100105#ifdef CONFIG_DEBUG_FS
106#define gpiod_emerg(desc, fmt, ...) \
107 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
108 ##__VA_ARGS__)
109#define gpiod_crit(desc, fmt, ...) \
110 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
111 ##__VA_ARGS__)
112#define gpiod_err(desc, fmt, ...) \
113 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
114 ##__VA_ARGS__)
115#define gpiod_warn(desc, fmt, ...) \
116 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
117 ##__VA_ARGS__)
118#define gpiod_info(desc, fmt, ...) \
119 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
120 ##__VA_ARGS__)
121#define gpiod_dbg(desc, fmt, ...) \
122 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
123 ##__VA_ARGS__)
124#else
Mark Brown6424de52013-09-09 10:33:49 +0100125#define gpiod_emerg(desc, fmt, ...) \
126 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
127#define gpiod_crit(desc, fmt, ...) \
128 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
129#define gpiod_err(desc, fmt, ...) \
130 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
131#define gpiod_warn(desc, fmt, ...) \
132 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
133#define gpiod_info(desc, fmt, ...) \
134 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
135#define gpiod_dbg(desc, fmt, ...) \
136 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Mark Brown7b17b592013-09-09 10:33:50 +0100137#endif
Alexandre Courbot372e7222013-02-03 01:29:29 +0900138
David Brownelld2876d02008-02-04 22:28:20 -0800139static inline void desc_set_label(struct gpio_desc *d, const char *label)
140{
141#ifdef CONFIG_DEBUG_FS
142 d->label = label;
143#endif
144}
145
Alexandre Courbot372e7222013-02-03 01:29:29 +0900146/*
147 * Return the GPIO number of the passed descriptor relative to its chip
148 */
149static int gpio_chip_hwgpio(const struct gpio_desc *desc)
150{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900151 return desc - &desc->chip->desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900152}
153
154/**
155 * Convert a GPIO number to its descriptor
156 */
157static struct gpio_desc *gpio_to_desc(unsigned gpio)
158{
159 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
160 return NULL;
161 else
162 return &gpio_desc[gpio];
163}
164
165/**
166 * Convert a GPIO descriptor to the integer namespace.
167 * This should disappear in the future but is needed since we still
168 * use GPIO numbers for error messages and sysfs nodes
169 */
170static int desc_to_gpio(const struct gpio_desc *desc)
171{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900172 return desc->chip->base + gpio_chip_hwgpio(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900173}
174
175
David Brownelld2876d02008-02-04 22:28:20 -0800176/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
177 * when setting direction, and otherwise illegal. Until board setup code
178 * and drivers use explicit requests everywhere (which won't happen when
179 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -0700180 * message should motivate switching to explicit requests... so should
181 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -0700182 *
183 * NOTE: the autorequest mechanism is going away; at this point it's
184 * only "legal" in the sense that (old) code using it won't break yet,
185 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -0800186 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900187static int gpio_ensure_requested(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800188{
David Brownell8a0cecf2009-04-02 16:57:06 -0700189 const struct gpio_chip *chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900190 const int gpio = desc_to_gpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -0700191
David Brownell8a0cecf2009-04-02 16:57:06 -0700192 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
193 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700194 if (!try_module_get(chip->owner)) {
195 pr_err("GPIO-%d: module can't be gotten \n", gpio);
196 clear_bit(FLAG_REQUESTED, &desc->flags);
197 /* lose */
198 return -EIO;
199 }
David Brownelld2876d02008-02-04 22:28:20 -0800200 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700201 /* caller must chip->request() w/o spinlock */
202 if (chip->request)
203 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800204 }
David Brownell35e8bb52008-10-15 22:03:16 -0700205 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800206}
207
Alexandre Courbotdef63432013-02-15 14:46:15 +0900208static struct 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}
212
Alexandre Courbot24d76282013-02-15 14:46:16 +0900213/* caller holds gpio_lock *OR* gpio is marked as requested */
Grant Likely1a2d3972011-12-12 09:25:57 -0700214struct gpio_chip *gpio_to_chip(unsigned gpio)
David Brownelld2876d02008-02-04 22:28:20 -0800215{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900216 return gpiod_to_chip(gpio_to_desc(gpio));
David Brownelld2876d02008-02-04 22:28:20 -0800217}
218
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700219/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
220static int gpiochip_find_base(int ngpio)
221{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900222 struct gpio_chip *chip;
223 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700224
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900225 list_for_each_entry_reverse(chip, &gpio_chips, list) {
226 /* found a free space? */
227 if (chip->base + chip->ngpio <= base)
228 break;
229 else
230 /* nope, check the space right before the chip */
231 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700232 }
233
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900234 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700235 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900236 return base;
237 } else {
238 pr_err("%s: cannot find free range\n", __func__);
239 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700240 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700241}
242
Mathias Nyman80b0a602012-10-24 17:25:27 +0300243/* caller ensures gpio is valid and requested, chip->get_direction may sleep */
Alexandre Courbotdef63432013-02-15 14:46:15 +0900244static int gpiod_get_direction(const struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300245{
246 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900247 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300248 int status = -EINVAL;
249
Alexandre Courbot372e7222013-02-03 01:29:29 +0900250 chip = gpiod_to_chip(desc);
251 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300252
253 if (!chip->get_direction)
254 return status;
255
Alexandre Courbot372e7222013-02-03 01:29:29 +0900256 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300257 if (status > 0) {
258 /* GPIOF_DIR_IN, or other positive */
259 status = 1;
Alexandre Courbotdef63432013-02-15 14:46:15 +0900260 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
261 * so it does not affect constness per se */
262 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300263 }
264 if (status == 0) {
265 /* GPIOF_DIR_OUT */
Alexandre Courbotdef63432013-02-15 14:46:15 +0900266 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300267 }
268 return status;
269}
270
David Brownelld8f388d82008-07-25 01:46:07 -0700271#ifdef CONFIG_GPIO_SYSFS
272
273/* lock protects against unexport_gpio() being called while
274 * sysfs files are active.
275 */
276static DEFINE_MUTEX(sysfs_lock);
277
278/*
279 * /sys/class/gpio/gpioN... only for GPIOs that are exported
280 * /direction
281 * * MAY BE OMITTED if kernel won't allow direction changes
282 * * is read/write as "in" or "out"
283 * * may also be written as "high" or "low", initializing
284 * output value as specified ("out" implies "low")
285 * /value
286 * * always readable, subject to hardware behavior
287 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700288 * /edge
289 * * configures behavior of poll(2) on /value
290 * * available only if pin can generate IRQs on input
291 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800292 * /active_low
293 * * configures polarity of /value
294 * * is read/write as zero/nonzero
295 * * also affects existing and subsequent "falling" and "rising"
296 * /edge configuration
David Brownelld8f388d82008-07-25 01:46:07 -0700297 */
298
299static ssize_t gpio_direction_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
301{
Alexandre Courbotdef63432013-02-15 14:46:15 +0900302 const struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700303 ssize_t status;
304
305 mutex_lock(&sysfs_lock);
306
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900307 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700308 status = -EIO;
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900309 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +0900310 gpiod_get_direction(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700311 status = sprintf(buf, "%s\n",
312 test_bit(FLAG_IS_OUT, &desc->flags)
313 ? "out" : "in");
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900314 }
David Brownelld8f388d82008-07-25 01:46:07 -0700315
316 mutex_unlock(&sysfs_lock);
317 return status;
318}
319
320static ssize_t gpio_direction_store(struct device *dev,
321 struct device_attribute *attr, const char *buf, size_t size)
322{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900323 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700324 ssize_t status;
325
326 mutex_lock(&sysfs_lock);
327
328 if (!test_bit(FLAG_EXPORT, &desc->flags))
329 status = -EIO;
330 else if (sysfs_streq(buf, "high"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900331 status = gpiod_direction_output(desc, 1);
David Brownelld8f388d82008-07-25 01:46:07 -0700332 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900333 status = gpiod_direction_output(desc, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700334 else if (sysfs_streq(buf, "in"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900335 status = gpiod_direction_input(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700336 else
337 status = -EINVAL;
338
339 mutex_unlock(&sysfs_lock);
340 return status ? : size;
341}
342
Jani Nikula07697462009-12-15 16:46:20 -0800343static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700344 gpio_direction_show, gpio_direction_store);
345
346static ssize_t gpio_value_show(struct device *dev,
347 struct device_attribute *attr, char *buf)
348{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900349 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700350 ssize_t status;
351
352 mutex_lock(&sysfs_lock);
353
Jani Nikula07697462009-12-15 16:46:20 -0800354 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700355 status = -EIO;
Jani Nikula07697462009-12-15 16:46:20 -0800356 } else {
357 int value;
358
Alexandre Courbot372e7222013-02-03 01:29:29 +0900359 value = !!gpiod_get_value_cansleep(desc);
Jani Nikula07697462009-12-15 16:46:20 -0800360 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
361 value = !value;
362
363 status = sprintf(buf, "%d\n", value);
364 }
David Brownelld8f388d82008-07-25 01:46:07 -0700365
366 mutex_unlock(&sysfs_lock);
367 return status;
368}
369
370static ssize_t gpio_value_store(struct device *dev,
371 struct device_attribute *attr, const char *buf, size_t size)
372{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900373 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700374 ssize_t status;
375
376 mutex_lock(&sysfs_lock);
377
378 if (!test_bit(FLAG_EXPORT, &desc->flags))
379 status = -EIO;
380 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
381 status = -EPERM;
382 else {
383 long value;
384
Jingoo Hana3d88c92013-07-19 16:12:50 +0900385 status = kstrtol(buf, 0, &value);
David Brownelld8f388d82008-07-25 01:46:07 -0700386 if (status == 0) {
Jani Nikula07697462009-12-15 16:46:20 -0800387 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
388 value = !value;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900389 gpiod_set_value_cansleep(desc, value != 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700390 status = size;
391 }
392 }
393
394 mutex_unlock(&sysfs_lock);
395 return status;
396}
397
Jani Nikula07697462009-12-15 16:46:20 -0800398static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700399 gpio_value_show, gpio_value_store);
400
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700401static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
402{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700403 struct sysfs_dirent *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700404
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700405 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700406 return IRQ_HANDLED;
407}
408
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700409static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
410 unsigned long gpio_flags)
411{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700412 struct sysfs_dirent *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700413 unsigned long irq_flags;
414 int ret, irq, id;
415
416 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
417 return 0;
418
Alexandre Courbot372e7222013-02-03 01:29:29 +0900419 irq = gpiod_to_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700420 if (irq < 0)
421 return -EIO;
422
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700423 id = desc->flags >> ID_SHIFT;
424 value_sd = idr_find(&dirent_idr, id);
425 if (value_sd)
426 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700427
428 desc->flags &= ~GPIO_TRIGGER_MASK;
429
430 if (!gpio_flags) {
431 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700432 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700433 }
434
435 irq_flags = IRQF_SHARED;
436 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800437 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
438 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700439 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800440 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
441 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700442
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700443 if (!value_sd) {
444 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
445 if (!value_sd) {
446 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700447 goto err_out;
448 }
449
Tejun Heo62f516b2013-02-27 17:04:06 -0800450 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
451 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700452 goto free_sd;
Tejun Heo62f516b2013-02-27 17:04:06 -0800453 id = ret;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700454
455 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700456 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700457
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700458 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700459 ret = -ERANGE;
460 goto free_id;
461 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700462 }
463
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700464 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700465 "gpiolib", value_sd);
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700466 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700467 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700468
469 desc->flags |= gpio_flags;
470 return 0;
471
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700472free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700473 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700474 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700475free_sd:
476 if (value_sd)
477 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700478err_out:
479 return ret;
480}
481
482static const struct {
483 const char *name;
484 unsigned long flags;
485} trigger_types[] = {
486 { "none", 0 },
487 { "falling", BIT(FLAG_TRIG_FALL) },
488 { "rising", BIT(FLAG_TRIG_RISE) },
489 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
490};
491
492static ssize_t gpio_edge_show(struct device *dev,
493 struct device_attribute *attr, char *buf)
494{
495 const struct gpio_desc *desc = dev_get_drvdata(dev);
496 ssize_t status;
497
498 mutex_lock(&sysfs_lock);
499
500 if (!test_bit(FLAG_EXPORT, &desc->flags))
501 status = -EIO;
502 else {
503 int i;
504
505 status = 0;
506 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
507 if ((desc->flags & GPIO_TRIGGER_MASK)
508 == trigger_types[i].flags) {
509 status = sprintf(buf, "%s\n",
510 trigger_types[i].name);
511 break;
512 }
513 }
514
515 mutex_unlock(&sysfs_lock);
516 return status;
517}
518
519static ssize_t gpio_edge_store(struct device *dev,
520 struct device_attribute *attr, const char *buf, size_t size)
521{
522 struct gpio_desc *desc = dev_get_drvdata(dev);
523 ssize_t status;
524 int i;
525
526 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
527 if (sysfs_streq(trigger_types[i].name, buf))
528 goto found;
529 return -EINVAL;
530
531found:
532 mutex_lock(&sysfs_lock);
533
534 if (!test_bit(FLAG_EXPORT, &desc->flags))
535 status = -EIO;
536 else {
537 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
538 if (!status)
539 status = size;
540 }
541
542 mutex_unlock(&sysfs_lock);
543
544 return status;
545}
546
547static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
548
Jani Nikula07697462009-12-15 16:46:20 -0800549static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
550 int value)
551{
552 int status = 0;
553
554 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
555 return 0;
556
557 if (value)
558 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
559 else
560 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
561
562 /* reconfigure poll(2) support if enabled on one edge only */
563 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
564 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
565 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
566
567 gpio_setup_irq(desc, dev, 0);
568 status = gpio_setup_irq(desc, dev, trigger_flags);
569 }
570
571 return status;
572}
573
574static ssize_t gpio_active_low_show(struct device *dev,
575 struct device_attribute *attr, char *buf)
576{
577 const struct gpio_desc *desc = dev_get_drvdata(dev);
578 ssize_t status;
579
580 mutex_lock(&sysfs_lock);
581
582 if (!test_bit(FLAG_EXPORT, &desc->flags))
583 status = -EIO;
584 else
585 status = sprintf(buf, "%d\n",
586 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
587
588 mutex_unlock(&sysfs_lock);
589
590 return status;
591}
592
593static ssize_t gpio_active_low_store(struct device *dev,
594 struct device_attribute *attr, const char *buf, size_t size)
595{
596 struct gpio_desc *desc = dev_get_drvdata(dev);
597 ssize_t status;
598
599 mutex_lock(&sysfs_lock);
600
601 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
602 status = -EIO;
603 } else {
604 long value;
605
Jingoo Hana3d88c92013-07-19 16:12:50 +0900606 status = kstrtol(buf, 0, &value);
Jani Nikula07697462009-12-15 16:46:20 -0800607 if (status == 0)
608 status = sysfs_set_active_low(desc, dev, value != 0);
609 }
610
611 mutex_unlock(&sysfs_lock);
612
613 return status ? : size;
614}
615
616static const DEVICE_ATTR(active_low, 0644,
617 gpio_active_low_show, gpio_active_low_store);
618
David Brownelld8f388d82008-07-25 01:46:07 -0700619static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d82008-07-25 01:46:07 -0700620 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800621 &dev_attr_active_low.attr,
David Brownelld8f388d82008-07-25 01:46:07 -0700622 NULL,
623};
624
625static const struct attribute_group gpio_attr_group = {
626 .attrs = (struct attribute **) gpio_attrs,
627};
628
629/*
630 * /sys/class/gpio/gpiochipN/
631 * /base ... matching gpio_chip.base (N)
632 * /label ... matching gpio_chip.label
633 * /ngpio ... matching gpio_chip.ngpio
634 */
635
636static ssize_t chip_base_show(struct device *dev,
637 struct device_attribute *attr, char *buf)
638{
639 const struct gpio_chip *chip = dev_get_drvdata(dev);
640
641 return sprintf(buf, "%d\n", chip->base);
642}
643static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
644
645static ssize_t chip_label_show(struct device *dev,
646 struct device_attribute *attr, char *buf)
647{
648 const struct gpio_chip *chip = dev_get_drvdata(dev);
649
650 return sprintf(buf, "%s\n", chip->label ? : "");
651}
652static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
653
654static ssize_t chip_ngpio_show(struct device *dev,
655 struct device_attribute *attr, char *buf)
656{
657 const struct gpio_chip *chip = dev_get_drvdata(dev);
658
659 return sprintf(buf, "%u\n", chip->ngpio);
660}
661static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
662
663static const struct attribute *gpiochip_attrs[] = {
664 &dev_attr_base.attr,
665 &dev_attr_label.attr,
666 &dev_attr_ngpio.attr,
667 NULL,
668};
669
670static const struct attribute_group gpiochip_attr_group = {
671 .attrs = (struct attribute **) gpiochip_attrs,
672};
673
674/*
675 * /sys/class/gpio/export ... write-only
676 * integer N ... number of GPIO to export (full access)
677 * /sys/class/gpio/unexport ... write-only
678 * integer N ... number of GPIO to unexport
679 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100680static ssize_t export_store(struct class *class,
681 struct class_attribute *attr,
682 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700683{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900684 long gpio;
685 struct gpio_desc *desc;
686 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700687
Jingoo Hana3d88c92013-07-19 16:12:50 +0900688 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700689 if (status < 0)
690 goto done;
691
Alexandre Courbot372e7222013-02-03 01:29:29 +0900692 desc = gpio_to_desc(gpio);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900693 /* reject invalid GPIOs */
694 if (!desc) {
695 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
696 return -EINVAL;
697 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900698
David Brownelld8f388d82008-07-25 01:46:07 -0700699 /* No extra locking here; FLAG_SYSFS just signifies that the
700 * request and export were done by on behalf of userspace, so
701 * they may be undone on its behalf too.
702 */
703
Alexandre Courbot372e7222013-02-03 01:29:29 +0900704 status = gpiod_request(desc, "sysfs");
Mathias Nymanad2fab32012-10-25 14:03:03 +0300705 if (status < 0) {
706 if (status == -EPROBE_DEFER)
707 status = -ENODEV;
David Brownelld8f388d82008-07-25 01:46:07 -0700708 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +0300709 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900710 status = gpiod_export(desc, true);
David Brownelld8f388d82008-07-25 01:46:07 -0700711 if (status < 0)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900712 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700713 else
Alexandre Courbot372e7222013-02-03 01:29:29 +0900714 set_bit(FLAG_SYSFS, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700715
716done:
717 if (status)
718 pr_debug("%s: status %d\n", __func__, status);
719 return status ? : len;
720}
721
Andi Kleen28812fe2010-01-05 12:48:07 +0100722static ssize_t unexport_store(struct class *class,
723 struct class_attribute *attr,
724 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700725{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900726 long gpio;
727 struct gpio_desc *desc;
728 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700729
Jingoo Hana3d88c92013-07-19 16:12:50 +0900730 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700731 if (status < 0)
732 goto done;
733
Alexandre Courbot372e7222013-02-03 01:29:29 +0900734 desc = gpio_to_desc(gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700735 /* reject bogus commands (gpio_unexport ignores them) */
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900736 if (!desc) {
737 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
738 return -EINVAL;
739 }
740
741 status = -EINVAL;
David Brownelld8f388d82008-07-25 01:46:07 -0700742
743 /* No extra locking here; FLAG_SYSFS just signifies that the
744 * request and export were done by on behalf of userspace, so
745 * they may be undone on its behalf too.
746 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900747 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700748 status = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900749 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700750 }
751done:
752 if (status)
753 pr_debug("%s: status %d\n", __func__, status);
754 return status ? : len;
755}
756
757static struct class_attribute gpio_class_attrs[] = {
758 __ATTR(export, 0200, NULL, export_store),
759 __ATTR(unexport, 0200, NULL, unexport_store),
760 __ATTR_NULL,
761};
762
763static struct class gpio_class = {
764 .name = "gpio",
765 .owner = THIS_MODULE,
766
767 .class_attrs = gpio_class_attrs,
768};
769
770
771/**
772 * gpio_export - export a GPIO through sysfs
773 * @gpio: gpio to make available, already requested
774 * @direction_may_change: true if userspace may change gpio direction
775 * Context: arch_initcall or later
776 *
777 * When drivers want to make a GPIO accessible to userspace after they
778 * have requested it -- perhaps while debugging, or as part of their
779 * public interface -- they may use this routine. If the GPIO can
780 * change direction (some can't) and the caller allows it, userspace
781 * will see "direction" sysfs attribute which may be used to change
782 * the gpio's direction. A "value" attribute will always be provided.
783 *
784 * Returns zero on success, else an error.
785 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900786static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
David Brownelld8f388d82008-07-25 01:46:07 -0700787{
788 unsigned long flags;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100789 int status;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700790 const char *ioname = NULL;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100791 struct device *dev;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900792 int offset;
David Brownelld8f388d82008-07-25 01:46:07 -0700793
794 /* can't export until sysfs is available ... */
795 if (!gpio_class.p) {
796 pr_debug("%s: called too early!\n", __func__);
797 return -ENOENT;
798 }
799
Alexandre Courbot372e7222013-02-03 01:29:29 +0900800 if (!desc) {
801 pr_debug("%s: invalid gpio descriptor\n", __func__);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100802 return -EINVAL;
803 }
David Brownelld8f388d82008-07-25 01:46:07 -0700804
805 mutex_lock(&sysfs_lock);
806
807 spin_lock_irqsave(&gpio_lock, flags);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100808 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
809 test_bit(FLAG_EXPORT, &desc->flags)) {
810 spin_unlock_irqrestore(&gpio_lock, flags);
811 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +0900812 __func__, desc_to_gpio(desc),
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100813 test_bit(FLAG_REQUESTED, &desc->flags),
814 test_bit(FLAG_EXPORT, &desc->flags));
Dan Carpenter529f2ad2012-10-26 09:59:43 +0300815 status = -EPERM;
816 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700817 }
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100818
819 if (!desc->chip->direction_input || !desc->chip->direction_output)
820 direction_may_change = false;
David Brownelld8f388d82008-07-25 01:46:07 -0700821 spin_unlock_irqrestore(&gpio_lock, flags);
822
Alexandre Courbot372e7222013-02-03 01:29:29 +0900823 offset = gpio_chip_hwgpio(desc);
824 if (desc->chip->names && desc->chip->names[offset])
825 ioname = desc->chip->names[offset];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700826
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100827 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Alexandre Courbot372e7222013-02-03 01:29:29 +0900828 desc, ioname ? ioname : "gpio%u",
829 desc_to_gpio(desc));
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100830 if (IS_ERR(dev)) {
831 status = PTR_ERR(dev);
832 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700833 }
834
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100835 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
David Brownelld8f388d82008-07-25 01:46:07 -0700836 if (status)
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100837 goto fail_unregister_device;
David Brownelld8f388d82008-07-25 01:46:07 -0700838
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100839 if (direction_may_change) {
840 status = device_create_file(dev, &dev_attr_direction);
841 if (status)
842 goto fail_unregister_device;
843 }
844
Alexandre Courbot372e7222013-02-03 01:29:29 +0900845 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100846 !test_bit(FLAG_IS_OUT, &desc->flags))) {
847 status = device_create_file(dev, &dev_attr_edge);
848 if (status)
849 goto fail_unregister_device;
850 }
851
852 set_bit(FLAG_EXPORT, &desc->flags);
853 mutex_unlock(&sysfs_lock);
854 return 0;
855
856fail_unregister_device:
857 device_unregister(dev);
858fail_unlock:
859 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900860 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
861 status);
David Brownelld8f388d82008-07-25 01:46:07 -0700862 return status;
863}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900864
865int gpio_export(unsigned gpio, bool direction_may_change)
866{
867 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
868}
David Brownelld8f388d82008-07-25 01:46:07 -0700869EXPORT_SYMBOL_GPL(gpio_export);
870
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100871static int match_export(struct device *dev, const void *data)
David Brownelld8f388d82008-07-25 01:46:07 -0700872{
873 return dev_get_drvdata(dev) == data;
874}
875
876/**
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700877 * gpio_export_link - create a sysfs link to an exported GPIO node
878 * @dev: device under which to create symlink
879 * @name: name of the symlink
880 * @gpio: gpio to create symlink to, already exported
881 *
882 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
883 * node. Caller is responsible for unlinking.
884 *
885 * Returns zero on success, else an error.
886 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900887static int gpiod_export_link(struct device *dev, const char *name,
888 struct gpio_desc *desc)
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700889{
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700890 int status = -EINVAL;
891
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900892 if (!desc) {
893 pr_warn("%s: invalid GPIO\n", __func__);
894 return -EINVAL;
895 }
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700896
897 mutex_lock(&sysfs_lock);
898
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700899 if (test_bit(FLAG_EXPORT, &desc->flags)) {
900 struct device *tdev;
901
902 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
903 if (tdev != NULL) {
904 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
905 name);
906 } else {
907 status = -ENODEV;
908 }
909 }
910
911 mutex_unlock(&sysfs_lock);
912
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700913 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900914 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
915 status);
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700916
917 return status;
918}
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700919
Alexandre Courbot372e7222013-02-03 01:29:29 +0900920int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
921{
922 return gpiod_export_link(dev, name, gpio_to_desc(gpio));
923}
924EXPORT_SYMBOL_GPL(gpio_export_link);
Jani Nikula07697462009-12-15 16:46:20 -0800925
926/**
927 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
928 * @gpio: gpio to change
929 * @value: non-zero to use active low, i.e. inverted values
930 *
931 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
932 * The GPIO does not have to be exported yet. If poll(2) support has
933 * been enabled for either rising or falling edge, it will be
934 * reconfigured to follow the new polarity.
935 *
936 * Returns zero on success, else an error.
937 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900938static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
Jani Nikula07697462009-12-15 16:46:20 -0800939{
Jani Nikula07697462009-12-15 16:46:20 -0800940 struct device *dev = NULL;
941 int status = -EINVAL;
942
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900943 if (!desc) {
944 pr_warn("%s: invalid GPIO\n", __func__);
945 return -EINVAL;
946 }
Jani Nikula07697462009-12-15 16:46:20 -0800947
948 mutex_lock(&sysfs_lock);
949
Jani Nikula07697462009-12-15 16:46:20 -0800950 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800951 dev = class_find_device(&gpio_class, NULL, desc, match_export);
952 if (dev == NULL) {
953 status = -ENODEV;
954 goto unlock;
955 }
956 }
957
958 status = sysfs_set_active_low(desc, dev, value);
959
960unlock:
961 mutex_unlock(&sysfs_lock);
962
Jani Nikula07697462009-12-15 16:46:20 -0800963 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900964 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
965 status);
Jani Nikula07697462009-12-15 16:46:20 -0800966
967 return status;
968}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900969
970int gpio_sysfs_set_active_low(unsigned gpio, int value)
971{
972 return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value);
973}
Jani Nikula07697462009-12-15 16:46:20 -0800974EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
975
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700976/**
David Brownelld8f388d82008-07-25 01:46:07 -0700977 * gpio_unexport - reverse effect of gpio_export()
978 * @gpio: gpio to make unavailable
979 *
980 * This is implicit on gpio_free().
981 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900982static void gpiod_unexport(struct gpio_desc *desc)
David Brownelld8f388d82008-07-25 01:46:07 -0700983{
Jon Povey6a99ad42010-07-27 13:18:06 -0700984 int status = 0;
Ming Lei864533c2012-02-13 22:53:20 +0800985 struct device *dev = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -0700986
Alexandre Courbot372e7222013-02-03 01:29:29 +0900987 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900988 pr_warn("%s: invalid GPIO\n", __func__);
989 return;
Jon Povey6a99ad42010-07-27 13:18:06 -0700990 }
David Brownelld8f388d82008-07-25 01:46:07 -0700991
992 mutex_lock(&sysfs_lock);
993
David Brownelld8f388d82008-07-25 01:46:07 -0700994 if (test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700995
996 dev = class_find_device(&gpio_class, NULL, desc, match_export);
997 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700998 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700999 clear_bit(FLAG_EXPORT, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -07001000 } else
1001 status = -ENODEV;
1002 }
1003
1004 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001005
Ming Lei864533c2012-02-13 22:53:20 +08001006 if (dev) {
1007 device_unregister(dev);
1008 put_device(dev);
1009 }
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001010
David Brownelld8f388d82008-07-25 01:46:07 -07001011 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001012 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
1013 status);
1014}
1015
1016void gpio_unexport(unsigned gpio)
1017{
1018 gpiod_unexport(gpio_to_desc(gpio));
David Brownelld8f388d82008-07-25 01:46:07 -07001019}
1020EXPORT_SYMBOL_GPL(gpio_unexport);
1021
1022static int gpiochip_export(struct gpio_chip *chip)
1023{
1024 int status;
1025 struct device *dev;
1026
1027 /* Many systems register gpio chips for SOC support very early,
1028 * before driver model support is available. In those cases we
1029 * export this later, in gpiolib_sysfs_init() ... here we just
1030 * verify that _some_ field of gpio_class got initialized.
1031 */
1032 if (!gpio_class.p)
1033 return 0;
1034
1035 /* use chip->base for the ID; it's already known to be unique */
1036 mutex_lock(&sysfs_lock);
1037 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
1038 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001039 if (!IS_ERR(dev)) {
David Brownelld8f388d82008-07-25 01:46:07 -07001040 status = sysfs_create_group(&dev->kobj,
1041 &gpiochip_attr_group);
1042 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001043 status = PTR_ERR(dev);
David Brownelld8f388d82008-07-25 01:46:07 -07001044 chip->exported = (status == 0);
1045 mutex_unlock(&sysfs_lock);
1046
1047 if (status) {
1048 unsigned long flags;
1049 unsigned gpio;
1050
1051 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001052 gpio = 0;
1053 while (gpio < chip->ngpio)
1054 chip->desc[gpio++].chip = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -07001055 spin_unlock_irqrestore(&gpio_lock, flags);
1056
1057 pr_debug("%s: chip %s status %d\n", __func__,
1058 chip->label, status);
1059 }
1060
1061 return status;
1062}
1063
1064static void gpiochip_unexport(struct gpio_chip *chip)
1065{
1066 int status;
1067 struct device *dev;
1068
1069 mutex_lock(&sysfs_lock);
1070 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1071 if (dev) {
1072 put_device(dev);
1073 device_unregister(dev);
1074 chip->exported = 0;
1075 status = 0;
1076 } else
1077 status = -ENODEV;
1078 mutex_unlock(&sysfs_lock);
1079
1080 if (status)
1081 pr_debug("%s: chip %s status %d\n", __func__,
1082 chip->label, status);
1083}
1084
1085static int __init gpiolib_sysfs_init(void)
1086{
1087 int status;
1088 unsigned long flags;
Alexandre Courbot65493e32013-02-03 01:29:25 +09001089 struct gpio_chip *chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001090
1091 status = class_register(&gpio_class);
1092 if (status < 0)
1093 return status;
1094
1095 /* Scan and register the gpio_chips which registered very
1096 * early (e.g. before the class_register above was called).
1097 *
1098 * We run before arch_initcall() so chip->dev nodes can have
1099 * registered, and so arch_initcall() can always gpio_export().
1100 */
1101 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot65493e32013-02-03 01:29:25 +09001102 list_for_each_entry(chip, &gpio_chips, list) {
David Brownelld8f388d82008-07-25 01:46:07 -07001103 if (!chip || chip->exported)
1104 continue;
1105
1106 spin_unlock_irqrestore(&gpio_lock, flags);
1107 status = gpiochip_export(chip);
1108 spin_lock_irqsave(&gpio_lock, flags);
1109 }
1110 spin_unlock_irqrestore(&gpio_lock, flags);
1111
1112
1113 return status;
1114}
1115postcore_initcall(gpiolib_sysfs_init);
1116
1117#else
1118static inline int gpiochip_export(struct gpio_chip *chip)
1119{
1120 return 0;
1121}
1122
1123static inline void gpiochip_unexport(struct gpio_chip *chip)
1124{
1125}
1126
Alexandre Courbot372e7222013-02-03 01:29:29 +09001127static inline int gpiod_export(struct gpio_desc *desc,
1128 bool direction_may_change)
1129{
1130 return -ENOSYS;
1131}
1132
1133static inline int gpiod_export_link(struct device *dev, const char *name,
1134 struct gpio_desc *desc)
1135{
1136 return -ENOSYS;
1137}
1138
1139static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
1140{
1141 return -ENOSYS;
1142}
1143
1144static inline void gpiod_unexport(struct gpio_desc *desc)
1145{
1146}
1147
David Brownelld8f388d82008-07-25 01:46:07 -07001148#endif /* CONFIG_GPIO_SYSFS */
1149
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001150/*
1151 * Add a new chip to the global chips list, keeping the list of chips sorted
1152 * by base order.
1153 *
1154 * Return -EBUSY if the new chip overlaps with some other chip's integer
1155 * space.
1156 */
1157static int gpiochip_add_to_list(struct gpio_chip *chip)
1158{
1159 struct list_head *pos = &gpio_chips;
1160 struct gpio_chip *_chip;
1161 int err = 0;
1162
1163 /* find where to insert our chip */
1164 list_for_each(pos, &gpio_chips) {
1165 _chip = list_entry(pos, struct gpio_chip, list);
1166 /* shall we insert before _chip? */
1167 if (_chip->base >= chip->base + chip->ngpio)
1168 break;
1169 }
1170
1171 /* are we stepping on the chip right before? */
1172 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1173 _chip = list_entry(pos->prev, struct gpio_chip, list);
1174 if (_chip->base + _chip->ngpio > chip->base) {
1175 dev_err(chip->dev,
1176 "GPIO integer space overlap, cannot add chip\n");
1177 err = -EBUSY;
1178 }
1179 }
1180
1181 if (!err)
1182 list_add_tail(&chip->list, pos);
1183
1184 return err;
1185}
1186
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001187/**
David Brownelld2876d02008-02-04 22:28:20 -08001188 * gpiochip_add() - register a gpio_chip
1189 * @chip: the chip to register, with chip->base initialized
1190 * Context: potentially before irqs or kmalloc will work
1191 *
1192 * Returns a negative errno if the chip can't be registered, such as
1193 * because the chip->base is invalid or already associated with a
1194 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001195 *
David Brownelld8f388d82008-07-25 01:46:07 -07001196 * When gpiochip_add() is called very early during boot, so that GPIOs
1197 * can be freely used, the chip->dev device must be registered before
1198 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1199 * for GPIOs will fail rudely.
1200 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001201 * If chip->base is negative, this requests dynamic assignment of
1202 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001203 */
1204int gpiochip_add(struct gpio_chip *chip)
1205{
1206 unsigned long flags;
1207 int status = 0;
1208 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001209 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001210
Trent Piephobff5fda2008-05-23 13:04:44 -07001211 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001212 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001213 status = -EINVAL;
1214 goto fail;
1215 }
1216
1217 spin_lock_irqsave(&gpio_lock, flags);
1218
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001219 if (base < 0) {
1220 base = gpiochip_find_base(chip->ngpio);
1221 if (base < 0) {
1222 status = base;
David Brownelld8f388d82008-07-25 01:46:07 -07001223 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001224 }
1225 chip->base = base;
1226 }
1227
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001228 status = gpiochip_add_to_list(chip);
1229
David Brownelld2876d02008-02-04 22:28:20 -08001230 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001231 chip->desc = &gpio_desc[chip->base];
1232
1233 for (id = 0; id < chip->ngpio; id++) {
1234 struct gpio_desc *desc = &chip->desc[id];
1235 desc->chip = chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001236
1237 /* REVISIT: most hardware initializes GPIOs as
1238 * inputs (often with pullups enabled) so power
1239 * usage is minimized. Linux code should set the
1240 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +03001241 * and in case chip->get_direction is not set,
David Brownelld8f388d82008-07-25 01:46:07 -07001242 * we may expose the wrong direction in sysfs.
1243 */
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001244 desc->flags = !chip->direction_input
David Brownelld8f388d82008-07-25 01:46:07 -07001245 ? (1 << FLAG_IS_OUT)
1246 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001247 }
1248 }
1249
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001250 spin_unlock_irqrestore(&gpio_lock, flags);
1251
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301252#ifdef CONFIG_PINCTRL
1253 INIT_LIST_HEAD(&chip->pin_ranges);
1254#endif
1255
Anton Vorontsov391c9702010-06-08 07:48:17 -06001256 of_gpiochip_add(chip);
1257
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001258 if (status)
1259 goto fail;
1260
1261 status = gpiochip_export(chip);
1262 if (status)
1263 goto fail;
1264
H Hartley Sweetenee1c1e72012-05-11 16:58:33 -07001265 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
Grant Likely64842aa2011-11-06 11:36:18 -07001266 chip->base, chip->base + chip->ngpio - 1,
1267 chip->label ? : "generic");
1268
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001269 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001270
1271unlock:
1272 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld2876d02008-02-04 22:28:20 -08001273fail:
1274 /* failures here can mean systems won't boot... */
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001275 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1276 chip->base, chip->base + chip->ngpio - 1,
1277 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001278 return status;
1279}
1280EXPORT_SYMBOL_GPL(gpiochip_add);
1281
1282/**
1283 * gpiochip_remove() - unregister a gpio_chip
1284 * @chip: the chip to unregister
1285 *
1286 * A gpio_chip with any GPIOs still requested may not be removed.
1287 */
1288int gpiochip_remove(struct gpio_chip *chip)
1289{
1290 unsigned long flags;
1291 int status = 0;
1292 unsigned id;
1293
1294 spin_lock_irqsave(&gpio_lock, flags);
1295
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001296 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001297 of_gpiochip_remove(chip);
1298
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001299 for (id = 0; id < chip->ngpio; id++) {
1300 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
David Brownelld2876d02008-02-04 22:28:20 -08001301 status = -EBUSY;
1302 break;
1303 }
1304 }
1305 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001306 for (id = 0; id < chip->ngpio; id++)
1307 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001308
1309 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -08001310 }
1311
1312 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d82008-07-25 01:46:07 -07001313
1314 if (status == 0)
1315 gpiochip_unexport(chip);
1316
David Brownelld2876d02008-02-04 22:28:20 -08001317 return status;
1318}
1319EXPORT_SYMBOL_GPL(gpiochip_remove);
1320
Grant Likely594fa262010-06-08 07:48:16 -06001321/**
1322 * gpiochip_find() - iterator for locating a specific gpio_chip
1323 * @data: data to pass to match function
1324 * @callback: Callback function to check gpio_chip
1325 *
1326 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1327 * determined by a user supplied @match callback. The callback should return
1328 * 0 if the device doesn't match and non-zero if it does. If the callback is
1329 * non-zero, this function will return to the caller and not iterate over any
1330 * more gpio_chips.
1331 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001332struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001333 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001334 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001335{
Alexandre Courbot125eef92013-02-03 01:29:26 +09001336 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001337 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001338
1339 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +09001340 list_for_each_entry(chip, &gpio_chips, list)
1341 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001342 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001343
1344 /* No match? */
1345 if (&chip->list == &gpio_chips)
1346 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001347 spin_unlock_irqrestore(&gpio_lock, flags);
1348
1349 return chip;
1350}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001351EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001352
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301353#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001354
Linus Walleij3f0f8672012-11-20 12:40:15 +01001355/**
1356 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1357 * @chip: the gpiochip to add the range for
1358 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001359 * @gpio_offset: the start offset in the current gpio_chip number space
1360 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001361 * @npins: the number of pins from the offset of each pin space (GPIO and
1362 * pin controller) to accumulate in this range
1363 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001364int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001365 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001366 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301367{
1368 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001369 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301370
Linus Walleij3f0f8672012-11-20 12:40:15 +01001371 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301372 if (!pin_range) {
1373 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1374 chip->label);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001375 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301376 }
1377
Linus Walleij3f0f8672012-11-20 12:40:15 +01001378 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001379 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001380 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301381 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001382 pin_range->range.base = chip->base + gpio_offset;
1383 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301384 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001385 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301386 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001387 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001388 ret = PTR_ERR(pin_range->pctldev);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001389 pr_err("%s: GPIO chip: could not create pin range\n",
1390 chip->label);
1391 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001392 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001393 }
Linus Walleij316511c2012-11-21 08:48:09 +01001394 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1395 chip->label, gpio_offset, gpio_offset + npins - 1,
1396 pinctl_name,
1397 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301398
1399 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001400
1401 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301402}
Linus Walleij165adc92012-11-06 14:49:39 +01001403EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301404
Linus Walleij3f0f8672012-11-20 12:40:15 +01001405/**
1406 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1407 * @chip: the chip to remove all the mappings for
1408 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301409void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1410{
1411 struct gpio_pin_range *pin_range, *tmp;
1412
1413 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1414 list_del(&pin_range->node);
1415 pinctrl_remove_gpio_range(pin_range->pctldev,
1416 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001417 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301418 }
1419}
Linus Walleij165adc92012-11-06 14:49:39 +01001420EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1421
1422#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301423
David Brownelld2876d02008-02-04 22:28:20 -08001424/* These "optional" allocation calls help prevent drivers from stomping
1425 * on each other, and help provide better diagnostics in debugfs.
1426 * They're called even less than the "set direction" calls.
1427 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001428static int gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001429{
David Brownell35e8bb52008-10-15 22:03:16 -07001430 struct gpio_chip *chip;
Mark Browne9354572012-07-09 12:22:56 +01001431 int status = -EPROBE_DEFER;
David Brownelld2876d02008-02-04 22:28:20 -08001432 unsigned long flags;
1433
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001434 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001435 pr_warn("%s: invalid GPIO\n", __func__);
1436 return -EINVAL;
1437 }
1438
David Brownelld2876d02008-02-04 22:28:20 -08001439 spin_lock_irqsave(&gpio_lock, flags);
1440
David Brownell35e8bb52008-10-15 22:03:16 -07001441 chip = desc->chip;
David Brownelld2876d02008-02-04 22:28:20 -08001442
David Brownell35e8bb52008-10-15 22:03:16 -07001443 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001444 goto done;
1445
David Brownelld2876d02008-02-04 22:28:20 -08001446 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001447 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001448 */
1449
1450 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1451 desc_set_label(desc, label ? : "?");
1452 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001453 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001454 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001455 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001456 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001457 }
1458
1459 if (chip->request) {
1460 /* chip->request may sleep */
1461 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001462 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001463 spin_lock_irqsave(&gpio_lock, flags);
1464
1465 if (status < 0) {
1466 desc_set_label(desc, NULL);
1467 module_put(chip->owner);
1468 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001469 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001470 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001471 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001472 if (chip->get_direction) {
1473 /* chip->get_direction may sleep */
1474 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001475 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001476 spin_lock_irqsave(&gpio_lock, flags);
1477 }
David Brownelld2876d02008-02-04 22:28:20 -08001478done:
1479 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001480 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001481 desc_to_gpio(desc), label ? : "?", status);
David Brownelld2876d02008-02-04 22:28:20 -08001482 spin_unlock_irqrestore(&gpio_lock, flags);
1483 return status;
1484}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001485
1486int gpio_request(unsigned gpio, const char *label)
1487{
1488 return gpiod_request(gpio_to_desc(gpio), label);
1489}
David Brownelld2876d02008-02-04 22:28:20 -08001490EXPORT_SYMBOL_GPL(gpio_request);
1491
Alexandre Courbot372e7222013-02-03 01:29:29 +09001492static void gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001493{
1494 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001495 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001496
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001497 might_sleep();
1498
Alexandre Courbot372e7222013-02-03 01:29:29 +09001499 if (!desc) {
David Brownelld2876d02008-02-04 22:28:20 -08001500 WARN_ON(extra_checks);
1501 return;
1502 }
1503
Alexandre Courbot372e7222013-02-03 01:29:29 +09001504 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07001505
David Brownelld2876d02008-02-04 22:28:20 -08001506 spin_lock_irqsave(&gpio_lock, flags);
1507
David Brownell35e8bb52008-10-15 22:03:16 -07001508 chip = desc->chip;
1509 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1510 if (chip->free) {
1511 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001512 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001513 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001514 spin_lock_irqsave(&gpio_lock, flags);
1515 }
David Brownelld2876d02008-02-04 22:28:20 -08001516 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001517 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001518 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001519 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301520 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301521 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001522 } else
David Brownelld2876d02008-02-04 22:28:20 -08001523 WARN_ON(extra_checks);
1524
1525 spin_unlock_irqrestore(&gpio_lock, flags);
1526}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001527
1528void gpio_free(unsigned gpio)
1529{
1530 gpiod_free(gpio_to_desc(gpio));
1531}
David Brownelld2876d02008-02-04 22:28:20 -08001532EXPORT_SYMBOL_GPL(gpio_free);
1533
Eric Miao3e45f1d2010-03-05 13:44:35 -08001534/**
1535 * gpio_request_one - request a single GPIO with initial configuration
1536 * @gpio: the GPIO number
1537 * @flags: GPIO configuration as specified by GPIOF_*
1538 * @label: a literal description string of this GPIO
1539 */
1540int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1541{
Alexandre Courbot372e7222013-02-03 01:29:29 +09001542 struct gpio_desc *desc;
Eric Miao3e45f1d2010-03-05 13:44:35 -08001543 int err;
1544
Alexandre Courbot372e7222013-02-03 01:29:29 +09001545 desc = gpio_to_desc(gpio);
1546
1547 err = gpiod_request(desc, label);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001548 if (err)
1549 return err;
1550
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301551 if (flags & GPIOF_OPEN_DRAIN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001552 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301553
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301554 if (flags & GPIOF_OPEN_SOURCE)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001555 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301556
Eric Miao3e45f1d2010-03-05 13:44:35 -08001557 if (flags & GPIOF_DIR_IN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001558 err = gpiod_direction_input(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001559 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001560 err = gpiod_direction_output(desc,
Eric Miao3e45f1d2010-03-05 13:44:35 -08001561 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1562
Aaro Koskinene2548112010-12-21 17:24:22 -08001563 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001564 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -08001565
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001566 if (flags & GPIOF_EXPORT) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001567 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001568 if (err)
1569 goto free_gpio;
1570 }
1571
1572 return 0;
1573
1574 free_gpio:
Alexandre Courbot372e7222013-02-03 01:29:29 +09001575 gpiod_free(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001576 return err;
1577}
1578EXPORT_SYMBOL_GPL(gpio_request_one);
1579
1580/**
1581 * gpio_request_array - request multiple GPIOs in a single call
1582 * @array: array of the 'struct gpio'
1583 * @num: how many GPIOs in the array
1584 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001585int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001586{
1587 int i, err;
1588
1589 for (i = 0; i < num; i++, array++) {
1590 err = gpio_request_one(array->gpio, array->flags, array->label);
1591 if (err)
1592 goto err_free;
1593 }
1594 return 0;
1595
1596err_free:
1597 while (i--)
1598 gpio_free((--array)->gpio);
1599 return err;
1600}
1601EXPORT_SYMBOL_GPL(gpio_request_array);
1602
1603/**
1604 * gpio_free_array - release 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 -07001608void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001609{
1610 while (num--)
1611 gpio_free((array++)->gpio);
1612}
1613EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001614
1615/**
1616 * gpiochip_is_requested - return string iff signal was requested
1617 * @chip: controller managing the signal
1618 * @offset: of signal within controller's 0..(ngpio - 1) range
1619 *
1620 * Returns NULL if the GPIO is not currently requested, else a string.
1621 * If debugfs support is enabled, the string returned is the label passed
1622 * to gpio_request(); otherwise it is a meaningless constant.
1623 *
1624 * This function is for use by GPIO controller drivers. The label can
1625 * help with diagnostics, and knowing that the signal is used as a GPIO
1626 * can help avoid accidentally multiplexing it to another controller.
1627 */
1628const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1629{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001630 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001631
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001632 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -08001633 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001634
1635 desc = &chip->desc[offset];
1636
Alexandre Courbot372e7222013-02-03 01:29:29 +09001637 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001638 return NULL;
1639#ifdef CONFIG_DEBUG_FS
Alexandre Courbot372e7222013-02-03 01:29:29 +09001640 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001641#else
1642 return "?";
1643#endif
1644}
1645EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1646
1647
1648/* Drivers MUST set GPIO direction before making get/set calls. In
1649 * some cases this is done in early boot, before IRQs are enabled.
1650 *
1651 * As a rule these aren't called more than once (except for drivers
1652 * using the open-drain emulation idiom) so these are natural places
1653 * to accumulate extra debugging checks. Note that we can't (yet)
1654 * rely on gpio_request() having been called beforehand.
1655 */
1656
Alexandre Courbot372e7222013-02-03 01:29:29 +09001657static int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001658{
1659 unsigned long flags;
1660 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001661 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001662 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001663
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001664 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001665 pr_warn("%s: invalid GPIO\n", __func__);
1666 return -EINVAL;
1667 }
1668
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001669 chip = desc->chip;
1670 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001671 gpiod_warn(desc,
1672 "%s: missing get() or direction_input() operations\n",
1673 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001674 return -EIO;
1675 }
1676
David Brownelld2876d02008-02-04 22:28:20 -08001677 spin_lock_irqsave(&gpio_lock, flags);
1678
Alexandre Courbot372e7222013-02-03 01:29:29 +09001679 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001680 if (status < 0)
1681 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001682
1683 /* now we know the gpio is valid and chip won't vanish */
1684
1685 spin_unlock_irqrestore(&gpio_lock, flags);
1686
David Brownell9c4ba942010-08-10 18:02:24 -07001687 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001688
Alexandre Courbot372e7222013-02-03 01:29:29 +09001689 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001690 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001691 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001692 if (status < 0) {
Mark Brown6424de52013-09-09 10:33:49 +01001693 gpiod_dbg(desc, "chip request fail, %d\n", status);
David Brownell35e8bb52008-10-15 22:03:16 -07001694 /* and it's not available to anyone else ...
1695 * gpio_request() is the fully clean solution.
1696 */
1697 goto lose;
1698 }
1699 }
1700
Alexandre Courbot372e7222013-02-03 01:29:29 +09001701 status = chip->direction_input(chip, offset);
David Brownelld2876d02008-02-04 22:28:20 -08001702 if (status == 0)
1703 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001704
Alexandre Courbot372e7222013-02-03 01:29:29 +09001705 trace_gpio_direction(desc_to_gpio(desc), 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001706lose:
David Brownelld2876d02008-02-04 22:28:20 -08001707 return status;
1708fail:
1709 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001710 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001711 gpiod_dbg(desc, "%s status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001712 return status;
1713}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001714
1715int gpio_direction_input(unsigned gpio)
1716{
1717 return gpiod_direction_input(gpio_to_desc(gpio));
1718}
David Brownelld2876d02008-02-04 22:28:20 -08001719EXPORT_SYMBOL_GPL(gpio_direction_input);
1720
Alexandre Courbot372e7222013-02-03 01:29:29 +09001721static int gpiod_direction_output(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001722{
1723 unsigned long flags;
1724 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001725 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001726 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001727
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001728 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001729 pr_warn("%s: invalid GPIO\n", __func__);
1730 return -EINVAL;
1731 }
1732
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301733 /* Open drain pin should not be driven to 1 */
1734 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001735 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301736
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301737 /* Open source pin should not be driven to 0 */
1738 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001739 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301740
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001741 chip = desc->chip;
1742 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001743 gpiod_warn(desc,
1744 "%s: missing set() or direction_output() operations\n",
1745 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001746 return -EIO;
1747 }
1748
David Brownelld2876d02008-02-04 22:28:20 -08001749 spin_lock_irqsave(&gpio_lock, flags);
1750
Alexandre Courbot372e7222013-02-03 01:29:29 +09001751 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001752 if (status < 0)
1753 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001754
1755 /* now we know the gpio is valid and chip won't vanish */
1756
1757 spin_unlock_irqrestore(&gpio_lock, flags);
1758
David Brownell9c4ba942010-08-10 18:02:24 -07001759 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001760
Alexandre Courbot372e7222013-02-03 01:29:29 +09001761 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001762 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001763 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001764 if (status < 0) {
Mark Brown6424de52013-09-09 10:33:49 +01001765 gpiod_dbg(desc, "chip request fail, %d\n", status);
David Brownell35e8bb52008-10-15 22:03:16 -07001766 /* and it's not available to anyone else ...
1767 * gpio_request() is the fully clean solution.
1768 */
1769 goto lose;
1770 }
1771 }
1772
Alexandre Courbot372e7222013-02-03 01:29:29 +09001773 status = chip->direction_output(chip, offset, value);
David Brownelld2876d02008-02-04 22:28:20 -08001774 if (status == 0)
1775 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001776 trace_gpio_value(desc_to_gpio(desc), 0, value);
1777 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001778lose:
David Brownelld2876d02008-02-04 22:28:20 -08001779 return status;
1780fail:
1781 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001782 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001783 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001784 return status;
1785}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001786
1787int gpio_direction_output(unsigned gpio, int value)
1788{
1789 return gpiod_direction_output(gpio_to_desc(gpio), value);
1790}
David Brownelld2876d02008-02-04 22:28:20 -08001791EXPORT_SYMBOL_GPL(gpio_direction_output);
1792
Felipe Balbic4b5be92010-05-26 14:42:23 -07001793/**
1794 * gpio_set_debounce - sets @debounce time for a @gpio
1795 * @gpio: the gpio to set debounce time
1796 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001797 *
1798 * returns -ENOTSUPP if the controller does not support setting
1799 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001800 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001801static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001802{
1803 unsigned long flags;
1804 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001805 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001806 int offset;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001807
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001808 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001809 pr_warn("%s: invalid GPIO\n", __func__);
1810 return -EINVAL;
1811 }
1812
Felipe Balbic4b5be92010-05-26 14:42:23 -07001813 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001814 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001815 gpiod_dbg(desc,
1816 "%s: missing set() or set_debounce() operations\n",
1817 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001818 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001819 }
1820
1821 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001822
1823 status = gpio_ensure_requested(desc);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001824 if (status < 0)
1825 goto fail;
1826
1827 /* now we know the gpio is valid and chip won't vanish */
1828
1829 spin_unlock_irqrestore(&gpio_lock, flags);
1830
David Brownell9c4ba942010-08-10 18:02:24 -07001831 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001832
Alexandre Courbot372e7222013-02-03 01:29:29 +09001833 offset = gpio_chip_hwgpio(desc);
1834 return chip->set_debounce(chip, offset, debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001835
1836fail:
1837 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001838 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001839 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001840
1841 return status;
1842}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001843
1844int gpio_set_debounce(unsigned gpio, unsigned debounce)
1845{
1846 return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
1847}
Felipe Balbic4b5be92010-05-26 14:42:23 -07001848EXPORT_SYMBOL_GPL(gpio_set_debounce);
David Brownelld2876d02008-02-04 22:28:20 -08001849
1850/* I/O calls are only valid after configuration completed; the relevant
1851 * "is this a valid GPIO" error checks should already have been done.
1852 *
1853 * "Get" operations are often inlinable as reading a pin value register,
1854 * and masking the relevant bit in that register.
1855 *
1856 * When "set" operations are inlinable, they involve writing that mask to
1857 * one register to set a low value, or a different register to set it high.
1858 * Otherwise locking is needed, so there may be little value to inlining.
1859 *
1860 *------------------------------------------------------------------------
1861 *
1862 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1863 * have requested the GPIO. That can include implicit requesting by
1864 * a direction setting call. Marking a gpio as requested locks its chip
1865 * in memory, guaranteeing that these table lookups need no more locking
1866 * and that gpiochip_remove() will fail.
1867 *
1868 * REVISIT when debugging, consider adding some instrumentation to ensure
1869 * that the GPIO was actually requested.
1870 */
1871
1872/**
1873 * __gpio_get_value() - return a gpio's value
1874 * @gpio: gpio whose value will be returned
1875 * Context: any
1876 *
1877 * This is used directly or indirectly to implement gpio_get_value().
1878 * It returns the zero or nonzero value provided by the associated
1879 * gpio_chip.get() method; or zero if no such method is provided.
1880 */
Alexandre Courbotdef63432013-02-15 14:46:15 +09001881static int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001882{
1883 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001884 int value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001885 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001886
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001887 if (!desc)
1888 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001889 chip = desc->chip;
1890 offset = gpio_chip_hwgpio(desc);
Mark Browne4e449e2012-02-17 10:46:00 -08001891 /* Should be using gpio_get_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001892 WARN_ON(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001893 value = chip->get ? chip->get(chip, offset) : 0;
1894 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001895 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001896}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001897
1898int __gpio_get_value(unsigned gpio)
1899{
1900 return gpiod_get_value(gpio_to_desc(gpio));
1901}
David Brownelld2876d02008-02-04 22:28:20 -08001902EXPORT_SYMBOL_GPL(__gpio_get_value);
1903
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301904/*
1905 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1906 * @gpio: Gpio whose state need to be set.
1907 * @chip: Gpio chip.
1908 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1909 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001910static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301911{
1912 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001913 struct gpio_chip *chip = desc->chip;
1914 int offset = gpio_chip_hwgpio(desc);
1915
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301916 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001917 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301918 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001919 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301920 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001921 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301922 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001923 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301924 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001925 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301926 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001927 gpiod_err(desc,
1928 "%s: Error in set_value for open drain err %d\n",
1929 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301930}
1931
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301932/*
1933 * _gpio_set_open_source() - Set the open source gpio's value.
1934 * @gpio: Gpio whose state need to be set.
1935 * @chip: Gpio chip.
1936 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1937 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001938static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301939{
1940 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001941 struct gpio_chip *chip = desc->chip;
1942 int offset = gpio_chip_hwgpio(desc);
1943
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301944 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001945 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301946 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001947 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301948 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001949 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301950 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001951 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301952 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001953 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301954 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001955 gpiod_err(desc,
1956 "%s: Error in set_value for open source err %d\n",
1957 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301958}
1959
David Brownelld2876d02008-02-04 22:28:20 -08001960/**
1961 * __gpio_set_value() - assign a gpio's value
1962 * @gpio: gpio whose value will be assigned
1963 * @value: value to assign
1964 * Context: any
1965 *
1966 * This is used directly or indirectly to implement gpio_set_value().
1967 * It invokes the associated gpio_chip.set() method.
1968 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001969static void gpiod_set_value(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001970{
1971 struct gpio_chip *chip;
1972
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001973 if (!desc)
1974 return;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001975 chip = desc->chip;
Mark Browne4e449e2012-02-17 10:46:00 -08001976 /* Should be using gpio_set_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001977 WARN_ON(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001978 trace_gpio_value(desc_to_gpio(desc), 0, value);
1979 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1980 _gpio_set_open_drain_value(desc, value);
1981 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1982 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301983 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001984 chip->set(chip, gpio_chip_hwgpio(desc), value);
1985}
1986
1987void __gpio_set_value(unsigned gpio, int value)
1988{
1989 return gpiod_set_value(gpio_to_desc(gpio), value);
David Brownelld2876d02008-02-04 22:28:20 -08001990}
1991EXPORT_SYMBOL_GPL(__gpio_set_value);
1992
1993/**
1994 * __gpio_cansleep() - report whether gpio value access will sleep
1995 * @gpio: gpio in question
1996 * Context: any
1997 *
1998 * This is used directly or indirectly to implement gpio_cansleep(). It
1999 * returns nonzero if access reading or writing the GPIO value can sleep.
2000 */
Alexandre Courbotdef63432013-02-15 14:46:15 +09002001static int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002002{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002003 if (!desc)
2004 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002005 /* only call this on GPIOs that are valid! */
2006 return desc->chip->can_sleep;
2007}
2008
David Brownelld2876d02008-02-04 22:28:20 -08002009int __gpio_cansleep(unsigned gpio)
2010{
Alexandre Courbot372e7222013-02-03 01:29:29 +09002011 return gpiod_cansleep(gpio_to_desc(gpio));
David Brownelld2876d02008-02-04 22:28:20 -08002012}
2013EXPORT_SYMBOL_GPL(__gpio_cansleep);
2014
David Brownell0f6d5042008-10-15 22:03:14 -07002015/**
2016 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
2017 * @gpio: gpio whose IRQ will be returned (already requested)
2018 * Context: any
2019 *
2020 * This is used directly or indirectly to implement gpio_to_irq().
2021 * It returns the number of the IRQ signaled by this (input) GPIO,
2022 * or a negative errno.
2023 */
Alexandre Courbotdef63432013-02-15 14:46:15 +09002024static int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002025{
2026 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002027 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002028
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002029 if (!desc)
2030 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002031 chip = desc->chip;
2032 offset = gpio_chip_hwgpio(desc);
2033 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
2034}
2035
2036int __gpio_to_irq(unsigned gpio)
2037{
2038 return gpiod_to_irq(gpio_to_desc(gpio));
David Brownell0f6d5042008-10-15 22:03:14 -07002039}
2040EXPORT_SYMBOL_GPL(__gpio_to_irq);
2041
David Brownelld2876d02008-02-04 22:28:20 -08002042
David Brownelld2876d02008-02-04 22:28:20 -08002043/* There's no value in making it easy to inline GPIO calls that may sleep.
2044 * Common examples include ones connected to I2C or SPI chips.
2045 */
2046
Alexandre Courbotdef63432013-02-15 14:46:15 +09002047static int gpiod_get_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002048{
2049 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002050 int value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002051 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08002052
2053 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002054 if (!desc)
2055 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002056 chip = desc->chip;
2057 offset = gpio_chip_hwgpio(desc);
2058 value = chip->get ? chip->get(chip, offset) : 0;
2059 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002060 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002061}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002062
2063int gpio_get_value_cansleep(unsigned gpio)
2064{
2065 return gpiod_get_value_cansleep(gpio_to_desc(gpio));
2066}
David Brownelld2876d02008-02-04 22:28:20 -08002067EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
2068
Alexandre Courbot372e7222013-02-03 01:29:29 +09002069static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002070{
2071 struct gpio_chip *chip;
2072
2073 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002074 if (!desc)
2075 return;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002076 chip = desc->chip;
2077 trace_gpio_value(desc_to_gpio(desc), 0, value);
2078 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2079 _gpio_set_open_drain_value(desc, value);
2080 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2081 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302082 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002083 chip->set(chip, gpio_chip_hwgpio(desc), value);
2084}
2085
2086void gpio_set_value_cansleep(unsigned gpio, int value)
2087{
2088 return gpiod_set_value_cansleep(gpio_to_desc(gpio), value);
David Brownelld2876d02008-02-04 22:28:20 -08002089}
2090EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
2091
David Brownelld2876d02008-02-04 22:28:20 -08002092#ifdef CONFIG_DEBUG_FS
2093
David Brownelld2876d02008-02-04 22:28:20 -08002094static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2095{
2096 unsigned i;
2097 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002098 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002099 int is_out;
2100
2101 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2102 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2103 continue;
2104
Alexandre Courbot372e7222013-02-03 01:29:29 +09002105 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002106 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Jarkko Nikula6e8ba722008-11-19 15:36:17 -08002107 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002108 gpio, gdesc->label,
2109 is_out ? "out" : "in ",
2110 chip->get
2111 ? (chip->get(chip, i) ? "hi" : "lo")
2112 : "? ");
David Brownelld2876d02008-02-04 22:28:20 -08002113 seq_printf(s, "\n");
2114 }
2115}
2116
Thierry Redingf9c4a312012-04-12 13:26:01 +02002117static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002118{
Grant Likely362432a2013-02-09 09:41:49 +00002119 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002120 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002121 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002122
2123 s->private = "";
2124
Grant Likely362432a2013-02-09 09:41:49 +00002125 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002126 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002127 if (index-- == 0) {
2128 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002129 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002130 }
2131 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002132
2133 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002134}
2135
2136static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2137{
Grant Likely362432a2013-02-09 09:41:49 +00002138 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002139 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002140 void *ret = NULL;
2141
Grant Likely362432a2013-02-09 09:41:49 +00002142 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002143 if (list_is_last(&chip->list, &gpio_chips))
2144 ret = NULL;
2145 else
2146 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002147 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002148
2149 s->private = "\n";
2150 ++*pos;
2151
2152 return ret;
2153}
2154
2155static void gpiolib_seq_stop(struct seq_file *s, void *v)
2156{
2157}
2158
2159static int gpiolib_seq_show(struct seq_file *s, void *v)
2160{
2161 struct gpio_chip *chip = v;
2162 struct device *dev;
2163
2164 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2165 chip->base, chip->base + chip->ngpio - 1);
2166 dev = chip->dev;
2167 if (dev)
2168 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2169 dev_name(dev));
2170 if (chip->label)
2171 seq_printf(s, ", %s", chip->label);
2172 if (chip->can_sleep)
2173 seq_printf(s, ", can sleep");
2174 seq_printf(s, ":\n");
2175
2176 if (chip->dbg_show)
2177 chip->dbg_show(s, chip);
2178 else
2179 gpiolib_dbg_show(s, chip);
2180
David Brownelld2876d02008-02-04 22:28:20 -08002181 return 0;
2182}
2183
Thierry Redingf9c4a312012-04-12 13:26:01 +02002184static const struct seq_operations gpiolib_seq_ops = {
2185 .start = gpiolib_seq_start,
2186 .next = gpiolib_seq_next,
2187 .stop = gpiolib_seq_stop,
2188 .show = gpiolib_seq_show,
2189};
2190
David Brownelld2876d02008-02-04 22:28:20 -08002191static int gpiolib_open(struct inode *inode, struct file *file)
2192{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002193 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002194}
2195
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002196static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002197 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002198 .open = gpiolib_open,
2199 .read = seq_read,
2200 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002201 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002202};
2203
2204static int __init gpiolib_debugfs_init(void)
2205{
2206 /* /sys/kernel/debug/gpio */
2207 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2208 NULL, NULL, &gpiolib_operations);
2209 return 0;
2210}
2211subsys_initcall(gpiolib_debugfs_init);
2212
2213#endif /* DEBUG_FS */