blob: 417ee75d82d36dce20acd66ee76334bc42c28f0e [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 Brown6424de52013-09-09 10:33:49 +0100105#define gpiod_emerg(desc, fmt, ...) \
106 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
107#define gpiod_crit(desc, fmt, ...) \
108 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
109#define gpiod_err(desc, fmt, ...) \
110 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
111#define gpiod_warn(desc, fmt, ...) \
112 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
113#define gpiod_info(desc, fmt, ...) \
114 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
115#define gpiod_dbg(desc, fmt, ...) \
116 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900117
David Brownelld2876d02008-02-04 22:28:20 -0800118static inline void desc_set_label(struct gpio_desc *d, const char *label)
119{
120#ifdef CONFIG_DEBUG_FS
121 d->label = label;
122#endif
123}
124
Alexandre Courbot372e7222013-02-03 01:29:29 +0900125/*
126 * Return the GPIO number of the passed descriptor relative to its chip
127 */
128static int gpio_chip_hwgpio(const struct gpio_desc *desc)
129{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900130 return desc - &desc->chip->desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900131}
132
133/**
134 * Convert a GPIO number to its descriptor
135 */
136static struct gpio_desc *gpio_to_desc(unsigned gpio)
137{
138 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
139 return NULL;
140 else
141 return &gpio_desc[gpio];
142}
143
144/**
145 * Convert a GPIO descriptor to the integer namespace.
146 * This should disappear in the future but is needed since we still
147 * use GPIO numbers for error messages and sysfs nodes
148 */
149static int desc_to_gpio(const struct gpio_desc *desc)
150{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900151 return desc->chip->base + gpio_chip_hwgpio(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900152}
153
154
David Brownelld2876d02008-02-04 22:28:20 -0800155/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
156 * when setting direction, and otherwise illegal. Until board setup code
157 * and drivers use explicit requests everywhere (which won't happen when
158 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -0700159 * message should motivate switching to explicit requests... so should
160 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -0700161 *
162 * NOTE: the autorequest mechanism is going away; at this point it's
163 * only "legal" in the sense that (old) code using it won't break yet,
164 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -0800165 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900166static int gpio_ensure_requested(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800167{
David Brownell8a0cecf2009-04-02 16:57:06 -0700168 const struct gpio_chip *chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900169 const int gpio = desc_to_gpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -0700170
David Brownell8a0cecf2009-04-02 16:57:06 -0700171 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
172 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700173 if (!try_module_get(chip->owner)) {
174 pr_err("GPIO-%d: module can't be gotten \n", gpio);
175 clear_bit(FLAG_REQUESTED, &desc->flags);
176 /* lose */
177 return -EIO;
178 }
David Brownelld2876d02008-02-04 22:28:20 -0800179 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700180 /* caller must chip->request() w/o spinlock */
181 if (chip->request)
182 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800183 }
David Brownell35e8bb52008-10-15 22:03:16 -0700184 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800185}
186
Alexandre Courbotdef63432013-02-15 14:46:15 +0900187static struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900188{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900189 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900190}
191
Alexandre Courbot24d76282013-02-15 14:46:16 +0900192/* caller holds gpio_lock *OR* gpio is marked as requested */
Grant Likely1a2d3972011-12-12 09:25:57 -0700193struct gpio_chip *gpio_to_chip(unsigned gpio)
David Brownelld2876d02008-02-04 22:28:20 -0800194{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900195 return gpiod_to_chip(gpio_to_desc(gpio));
David Brownelld2876d02008-02-04 22:28:20 -0800196}
197
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700198/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
199static int gpiochip_find_base(int ngpio)
200{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900201 struct gpio_chip *chip;
202 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700203
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900204 list_for_each_entry_reverse(chip, &gpio_chips, list) {
205 /* found a free space? */
206 if (chip->base + chip->ngpio <= base)
207 break;
208 else
209 /* nope, check the space right before the chip */
210 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700211 }
212
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900213 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700214 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900215 return base;
216 } else {
217 pr_err("%s: cannot find free range\n", __func__);
218 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700219 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700220}
221
Mathias Nyman80b0a602012-10-24 17:25:27 +0300222/* caller ensures gpio is valid and requested, chip->get_direction may sleep */
Alexandre Courbotdef63432013-02-15 14:46:15 +0900223static int gpiod_get_direction(const struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300224{
225 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900226 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300227 int status = -EINVAL;
228
Alexandre Courbot372e7222013-02-03 01:29:29 +0900229 chip = gpiod_to_chip(desc);
230 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300231
232 if (!chip->get_direction)
233 return status;
234
Alexandre Courbot372e7222013-02-03 01:29:29 +0900235 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300236 if (status > 0) {
237 /* GPIOF_DIR_IN, or other positive */
238 status = 1;
Alexandre Courbotdef63432013-02-15 14:46:15 +0900239 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
240 * so it does not affect constness per se */
241 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300242 }
243 if (status == 0) {
244 /* GPIOF_DIR_OUT */
Alexandre Courbotdef63432013-02-15 14:46:15 +0900245 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300246 }
247 return status;
248}
249
David Brownelld8f388d82008-07-25 01:46:07 -0700250#ifdef CONFIG_GPIO_SYSFS
251
252/* lock protects against unexport_gpio() being called while
253 * sysfs files are active.
254 */
255static DEFINE_MUTEX(sysfs_lock);
256
257/*
258 * /sys/class/gpio/gpioN... only for GPIOs that are exported
259 * /direction
260 * * MAY BE OMITTED if kernel won't allow direction changes
261 * * is read/write as "in" or "out"
262 * * may also be written as "high" or "low", initializing
263 * output value as specified ("out" implies "low")
264 * /value
265 * * always readable, subject to hardware behavior
266 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700267 * /edge
268 * * configures behavior of poll(2) on /value
269 * * available only if pin can generate IRQs on input
270 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800271 * /active_low
272 * * configures polarity of /value
273 * * is read/write as zero/nonzero
274 * * also affects existing and subsequent "falling" and "rising"
275 * /edge configuration
David Brownelld8f388d82008-07-25 01:46:07 -0700276 */
277
278static ssize_t gpio_direction_show(struct device *dev,
279 struct device_attribute *attr, char *buf)
280{
Alexandre Courbotdef63432013-02-15 14:46:15 +0900281 const struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700282 ssize_t status;
283
284 mutex_lock(&sysfs_lock);
285
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900286 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700287 status = -EIO;
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900288 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +0900289 gpiod_get_direction(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700290 status = sprintf(buf, "%s\n",
291 test_bit(FLAG_IS_OUT, &desc->flags)
292 ? "out" : "in");
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900293 }
David Brownelld8f388d82008-07-25 01:46:07 -0700294
295 mutex_unlock(&sysfs_lock);
296 return status;
297}
298
299static ssize_t gpio_direction_store(struct device *dev,
300 struct device_attribute *attr, const char *buf, size_t size)
301{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900302 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
307 if (!test_bit(FLAG_EXPORT, &desc->flags))
308 status = -EIO;
309 else if (sysfs_streq(buf, "high"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900310 status = gpiod_direction_output(desc, 1);
David Brownelld8f388d82008-07-25 01:46:07 -0700311 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900312 status = gpiod_direction_output(desc, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700313 else if (sysfs_streq(buf, "in"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900314 status = gpiod_direction_input(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700315 else
316 status = -EINVAL;
317
318 mutex_unlock(&sysfs_lock);
319 return status ? : size;
320}
321
Jani Nikula07697462009-12-15 16:46:20 -0800322static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700323 gpio_direction_show, gpio_direction_store);
324
325static ssize_t gpio_value_show(struct device *dev,
326 struct device_attribute *attr, char *buf)
327{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900328 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700329 ssize_t status;
330
331 mutex_lock(&sysfs_lock);
332
Jani Nikula07697462009-12-15 16:46:20 -0800333 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700334 status = -EIO;
Jani Nikula07697462009-12-15 16:46:20 -0800335 } else {
336 int value;
337
Alexandre Courbot372e7222013-02-03 01:29:29 +0900338 value = !!gpiod_get_value_cansleep(desc);
Jani Nikula07697462009-12-15 16:46:20 -0800339 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
340 value = !value;
341
342 status = sprintf(buf, "%d\n", value);
343 }
David Brownelld8f388d82008-07-25 01:46:07 -0700344
345 mutex_unlock(&sysfs_lock);
346 return status;
347}
348
349static ssize_t gpio_value_store(struct device *dev,
350 struct device_attribute *attr, const char *buf, size_t size)
351{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900352 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700353 ssize_t status;
354
355 mutex_lock(&sysfs_lock);
356
357 if (!test_bit(FLAG_EXPORT, &desc->flags))
358 status = -EIO;
359 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
360 status = -EPERM;
361 else {
362 long value;
363
Jingoo Hana3d88c92013-07-19 16:12:50 +0900364 status = kstrtol(buf, 0, &value);
David Brownelld8f388d82008-07-25 01:46:07 -0700365 if (status == 0) {
Jani Nikula07697462009-12-15 16:46:20 -0800366 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
367 value = !value;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900368 gpiod_set_value_cansleep(desc, value != 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700369 status = size;
370 }
371 }
372
373 mutex_unlock(&sysfs_lock);
374 return status;
375}
376
Jani Nikula07697462009-12-15 16:46:20 -0800377static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700378 gpio_value_show, gpio_value_store);
379
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700380static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
381{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700382 struct sysfs_dirent *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700383
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700384 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700385 return IRQ_HANDLED;
386}
387
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700388static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
389 unsigned long gpio_flags)
390{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700391 struct sysfs_dirent *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700392 unsigned long irq_flags;
393 int ret, irq, id;
394
395 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
396 return 0;
397
Alexandre Courbot372e7222013-02-03 01:29:29 +0900398 irq = gpiod_to_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700399 if (irq < 0)
400 return -EIO;
401
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700402 id = desc->flags >> ID_SHIFT;
403 value_sd = idr_find(&dirent_idr, id);
404 if (value_sd)
405 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700406
407 desc->flags &= ~GPIO_TRIGGER_MASK;
408
409 if (!gpio_flags) {
410 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700411 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700412 }
413
414 irq_flags = IRQF_SHARED;
415 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800416 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
417 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700418 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800419 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
420 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700421
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700422 if (!value_sd) {
423 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
424 if (!value_sd) {
425 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700426 goto err_out;
427 }
428
Tejun Heo62f516b2013-02-27 17:04:06 -0800429 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
430 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700431 goto free_sd;
Tejun Heo62f516b2013-02-27 17:04:06 -0800432 id = ret;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700433
434 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700435 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700436
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700437 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700438 ret = -ERANGE;
439 goto free_id;
440 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700441 }
442
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700443 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700444 "gpiolib", value_sd);
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700445 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700446 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700447
448 desc->flags |= gpio_flags;
449 return 0;
450
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700451free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700452 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700453 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700454free_sd:
455 if (value_sd)
456 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700457err_out:
458 return ret;
459}
460
461static const struct {
462 const char *name;
463 unsigned long flags;
464} trigger_types[] = {
465 { "none", 0 },
466 { "falling", BIT(FLAG_TRIG_FALL) },
467 { "rising", BIT(FLAG_TRIG_RISE) },
468 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
469};
470
471static ssize_t gpio_edge_show(struct device *dev,
472 struct device_attribute *attr, char *buf)
473{
474 const struct gpio_desc *desc = dev_get_drvdata(dev);
475 ssize_t status;
476
477 mutex_lock(&sysfs_lock);
478
479 if (!test_bit(FLAG_EXPORT, &desc->flags))
480 status = -EIO;
481 else {
482 int i;
483
484 status = 0;
485 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
486 if ((desc->flags & GPIO_TRIGGER_MASK)
487 == trigger_types[i].flags) {
488 status = sprintf(buf, "%s\n",
489 trigger_types[i].name);
490 break;
491 }
492 }
493
494 mutex_unlock(&sysfs_lock);
495 return status;
496}
497
498static ssize_t gpio_edge_store(struct device *dev,
499 struct device_attribute *attr, const char *buf, size_t size)
500{
501 struct gpio_desc *desc = dev_get_drvdata(dev);
502 ssize_t status;
503 int i;
504
505 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
506 if (sysfs_streq(trigger_types[i].name, buf))
507 goto found;
508 return -EINVAL;
509
510found:
511 mutex_lock(&sysfs_lock);
512
513 if (!test_bit(FLAG_EXPORT, &desc->flags))
514 status = -EIO;
515 else {
516 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
517 if (!status)
518 status = size;
519 }
520
521 mutex_unlock(&sysfs_lock);
522
523 return status;
524}
525
526static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
527
Jani Nikula07697462009-12-15 16:46:20 -0800528static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
529 int value)
530{
531 int status = 0;
532
533 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
534 return 0;
535
536 if (value)
537 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
538 else
539 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
540
541 /* reconfigure poll(2) support if enabled on one edge only */
542 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
543 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
544 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
545
546 gpio_setup_irq(desc, dev, 0);
547 status = gpio_setup_irq(desc, dev, trigger_flags);
548 }
549
550 return status;
551}
552
553static ssize_t gpio_active_low_show(struct device *dev,
554 struct device_attribute *attr, char *buf)
555{
556 const struct gpio_desc *desc = dev_get_drvdata(dev);
557 ssize_t status;
558
559 mutex_lock(&sysfs_lock);
560
561 if (!test_bit(FLAG_EXPORT, &desc->flags))
562 status = -EIO;
563 else
564 status = sprintf(buf, "%d\n",
565 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
566
567 mutex_unlock(&sysfs_lock);
568
569 return status;
570}
571
572static ssize_t gpio_active_low_store(struct device *dev,
573 struct device_attribute *attr, const char *buf, size_t size)
574{
575 struct gpio_desc *desc = dev_get_drvdata(dev);
576 ssize_t status;
577
578 mutex_lock(&sysfs_lock);
579
580 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
581 status = -EIO;
582 } else {
583 long value;
584
Jingoo Hana3d88c92013-07-19 16:12:50 +0900585 status = kstrtol(buf, 0, &value);
Jani Nikula07697462009-12-15 16:46:20 -0800586 if (status == 0)
587 status = sysfs_set_active_low(desc, dev, value != 0);
588 }
589
590 mutex_unlock(&sysfs_lock);
591
592 return status ? : size;
593}
594
595static const DEVICE_ATTR(active_low, 0644,
596 gpio_active_low_show, gpio_active_low_store);
597
David Brownelld8f388d82008-07-25 01:46:07 -0700598static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d82008-07-25 01:46:07 -0700599 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800600 &dev_attr_active_low.attr,
David Brownelld8f388d82008-07-25 01:46:07 -0700601 NULL,
602};
603
604static const struct attribute_group gpio_attr_group = {
605 .attrs = (struct attribute **) gpio_attrs,
606};
607
608/*
609 * /sys/class/gpio/gpiochipN/
610 * /base ... matching gpio_chip.base (N)
611 * /label ... matching gpio_chip.label
612 * /ngpio ... matching gpio_chip.ngpio
613 */
614
615static ssize_t chip_base_show(struct device *dev,
616 struct device_attribute *attr, char *buf)
617{
618 const struct gpio_chip *chip = dev_get_drvdata(dev);
619
620 return sprintf(buf, "%d\n", chip->base);
621}
622static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
623
624static ssize_t chip_label_show(struct device *dev,
625 struct device_attribute *attr, char *buf)
626{
627 const struct gpio_chip *chip = dev_get_drvdata(dev);
628
629 return sprintf(buf, "%s\n", chip->label ? : "");
630}
631static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
632
633static ssize_t chip_ngpio_show(struct device *dev,
634 struct device_attribute *attr, char *buf)
635{
636 const struct gpio_chip *chip = dev_get_drvdata(dev);
637
638 return sprintf(buf, "%u\n", chip->ngpio);
639}
640static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
641
642static const struct attribute *gpiochip_attrs[] = {
643 &dev_attr_base.attr,
644 &dev_attr_label.attr,
645 &dev_attr_ngpio.attr,
646 NULL,
647};
648
649static const struct attribute_group gpiochip_attr_group = {
650 .attrs = (struct attribute **) gpiochip_attrs,
651};
652
653/*
654 * /sys/class/gpio/export ... write-only
655 * integer N ... number of GPIO to export (full access)
656 * /sys/class/gpio/unexport ... write-only
657 * integer N ... number of GPIO to unexport
658 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100659static ssize_t export_store(struct class *class,
660 struct class_attribute *attr,
661 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700662{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900663 long gpio;
664 struct gpio_desc *desc;
665 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700666
Jingoo Hana3d88c92013-07-19 16:12:50 +0900667 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700668 if (status < 0)
669 goto done;
670
Alexandre Courbot372e7222013-02-03 01:29:29 +0900671 desc = gpio_to_desc(gpio);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900672 /* reject invalid GPIOs */
673 if (!desc) {
674 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
675 return -EINVAL;
676 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900677
David Brownelld8f388d82008-07-25 01:46:07 -0700678 /* No extra locking here; FLAG_SYSFS just signifies that the
679 * request and export were done by on behalf of userspace, so
680 * they may be undone on its behalf too.
681 */
682
Alexandre Courbot372e7222013-02-03 01:29:29 +0900683 status = gpiod_request(desc, "sysfs");
Mathias Nymanad2fab32012-10-25 14:03:03 +0300684 if (status < 0) {
685 if (status == -EPROBE_DEFER)
686 status = -ENODEV;
David Brownelld8f388d82008-07-25 01:46:07 -0700687 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +0300688 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900689 status = gpiod_export(desc, true);
David Brownelld8f388d82008-07-25 01:46:07 -0700690 if (status < 0)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900691 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700692 else
Alexandre Courbot372e7222013-02-03 01:29:29 +0900693 set_bit(FLAG_SYSFS, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700694
695done:
696 if (status)
697 pr_debug("%s: status %d\n", __func__, status);
698 return status ? : len;
699}
700
Andi Kleen28812fe2010-01-05 12:48:07 +0100701static ssize_t unexport_store(struct class *class,
702 struct class_attribute *attr,
703 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700704{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900705 long gpio;
706 struct gpio_desc *desc;
707 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700708
Jingoo Hana3d88c92013-07-19 16:12:50 +0900709 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700710 if (status < 0)
711 goto done;
712
Alexandre Courbot372e7222013-02-03 01:29:29 +0900713 desc = gpio_to_desc(gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700714 /* reject bogus commands (gpio_unexport ignores them) */
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900715 if (!desc) {
716 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
717 return -EINVAL;
718 }
719
720 status = -EINVAL;
David Brownelld8f388d82008-07-25 01:46:07 -0700721
722 /* No extra locking here; FLAG_SYSFS just signifies that the
723 * request and export were done by on behalf of userspace, so
724 * they may be undone on its behalf too.
725 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900726 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700727 status = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900728 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700729 }
730done:
731 if (status)
732 pr_debug("%s: status %d\n", __func__, status);
733 return status ? : len;
734}
735
736static struct class_attribute gpio_class_attrs[] = {
737 __ATTR(export, 0200, NULL, export_store),
738 __ATTR(unexport, 0200, NULL, unexport_store),
739 __ATTR_NULL,
740};
741
742static struct class gpio_class = {
743 .name = "gpio",
744 .owner = THIS_MODULE,
745
746 .class_attrs = gpio_class_attrs,
747};
748
749
750/**
751 * gpio_export - export a GPIO through sysfs
752 * @gpio: gpio to make available, already requested
753 * @direction_may_change: true if userspace may change gpio direction
754 * Context: arch_initcall or later
755 *
756 * When drivers want to make a GPIO accessible to userspace after they
757 * have requested it -- perhaps while debugging, or as part of their
758 * public interface -- they may use this routine. If the GPIO can
759 * change direction (some can't) and the caller allows it, userspace
760 * will see "direction" sysfs attribute which may be used to change
761 * the gpio's direction. A "value" attribute will always be provided.
762 *
763 * Returns zero on success, else an error.
764 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900765static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
David Brownelld8f388d82008-07-25 01:46:07 -0700766{
767 unsigned long flags;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100768 int status;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700769 const char *ioname = NULL;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100770 struct device *dev;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900771 int offset;
David Brownelld8f388d82008-07-25 01:46:07 -0700772
773 /* can't export until sysfs is available ... */
774 if (!gpio_class.p) {
775 pr_debug("%s: called too early!\n", __func__);
776 return -ENOENT;
777 }
778
Alexandre Courbot372e7222013-02-03 01:29:29 +0900779 if (!desc) {
780 pr_debug("%s: invalid gpio descriptor\n", __func__);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100781 return -EINVAL;
782 }
David Brownelld8f388d82008-07-25 01:46:07 -0700783
784 mutex_lock(&sysfs_lock);
785
786 spin_lock_irqsave(&gpio_lock, flags);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100787 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
788 test_bit(FLAG_EXPORT, &desc->flags)) {
789 spin_unlock_irqrestore(&gpio_lock, flags);
790 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +0900791 __func__, desc_to_gpio(desc),
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100792 test_bit(FLAG_REQUESTED, &desc->flags),
793 test_bit(FLAG_EXPORT, &desc->flags));
Dan Carpenter529f2ad2012-10-26 09:59:43 +0300794 status = -EPERM;
795 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700796 }
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100797
798 if (!desc->chip->direction_input || !desc->chip->direction_output)
799 direction_may_change = false;
David Brownelld8f388d82008-07-25 01:46:07 -0700800 spin_unlock_irqrestore(&gpio_lock, flags);
801
Alexandre Courbot372e7222013-02-03 01:29:29 +0900802 offset = gpio_chip_hwgpio(desc);
803 if (desc->chip->names && desc->chip->names[offset])
804 ioname = desc->chip->names[offset];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700805
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100806 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Alexandre Courbot372e7222013-02-03 01:29:29 +0900807 desc, ioname ? ioname : "gpio%u",
808 desc_to_gpio(desc));
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100809 if (IS_ERR(dev)) {
810 status = PTR_ERR(dev);
811 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700812 }
813
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100814 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
David Brownelld8f388d82008-07-25 01:46:07 -0700815 if (status)
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100816 goto fail_unregister_device;
David Brownelld8f388d82008-07-25 01:46:07 -0700817
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100818 if (direction_may_change) {
819 status = device_create_file(dev, &dev_attr_direction);
820 if (status)
821 goto fail_unregister_device;
822 }
823
Alexandre Courbot372e7222013-02-03 01:29:29 +0900824 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100825 !test_bit(FLAG_IS_OUT, &desc->flags))) {
826 status = device_create_file(dev, &dev_attr_edge);
827 if (status)
828 goto fail_unregister_device;
829 }
830
831 set_bit(FLAG_EXPORT, &desc->flags);
832 mutex_unlock(&sysfs_lock);
833 return 0;
834
835fail_unregister_device:
836 device_unregister(dev);
837fail_unlock:
838 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900839 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
840 status);
David Brownelld8f388d82008-07-25 01:46:07 -0700841 return status;
842}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900843
844int gpio_export(unsigned gpio, bool direction_may_change)
845{
846 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
847}
David Brownelld8f388d82008-07-25 01:46:07 -0700848EXPORT_SYMBOL_GPL(gpio_export);
849
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100850static int match_export(struct device *dev, const void *data)
David Brownelld8f388d82008-07-25 01:46:07 -0700851{
852 return dev_get_drvdata(dev) == data;
853}
854
855/**
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700856 * gpio_export_link - create a sysfs link to an exported GPIO node
857 * @dev: device under which to create symlink
858 * @name: name of the symlink
859 * @gpio: gpio to create symlink to, already exported
860 *
861 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
862 * node. Caller is responsible for unlinking.
863 *
864 * Returns zero on success, else an error.
865 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900866static int gpiod_export_link(struct device *dev, const char *name,
867 struct gpio_desc *desc)
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700868{
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700869 int status = -EINVAL;
870
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900871 if (!desc) {
872 pr_warn("%s: invalid GPIO\n", __func__);
873 return -EINVAL;
874 }
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700875
876 mutex_lock(&sysfs_lock);
877
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700878 if (test_bit(FLAG_EXPORT, &desc->flags)) {
879 struct device *tdev;
880
881 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
882 if (tdev != NULL) {
883 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
884 name);
885 } else {
886 status = -ENODEV;
887 }
888 }
889
890 mutex_unlock(&sysfs_lock);
891
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700892 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900893 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
894 status);
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700895
896 return status;
897}
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700898
Alexandre Courbot372e7222013-02-03 01:29:29 +0900899int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
900{
901 return gpiod_export_link(dev, name, gpio_to_desc(gpio));
902}
903EXPORT_SYMBOL_GPL(gpio_export_link);
Jani Nikula07697462009-12-15 16:46:20 -0800904
905/**
906 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
907 * @gpio: gpio to change
908 * @value: non-zero to use active low, i.e. inverted values
909 *
910 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
911 * The GPIO does not have to be exported yet. If poll(2) support has
912 * been enabled for either rising or falling edge, it will be
913 * reconfigured to follow the new polarity.
914 *
915 * Returns zero on success, else an error.
916 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900917static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
Jani Nikula07697462009-12-15 16:46:20 -0800918{
Jani Nikula07697462009-12-15 16:46:20 -0800919 struct device *dev = NULL;
920 int status = -EINVAL;
921
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900922 if (!desc) {
923 pr_warn("%s: invalid GPIO\n", __func__);
924 return -EINVAL;
925 }
Jani Nikula07697462009-12-15 16:46:20 -0800926
927 mutex_lock(&sysfs_lock);
928
Jani Nikula07697462009-12-15 16:46:20 -0800929 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800930 dev = class_find_device(&gpio_class, NULL, desc, match_export);
931 if (dev == NULL) {
932 status = -ENODEV;
933 goto unlock;
934 }
935 }
936
937 status = sysfs_set_active_low(desc, dev, value);
938
939unlock:
940 mutex_unlock(&sysfs_lock);
941
Jani Nikula07697462009-12-15 16:46:20 -0800942 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900943 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
944 status);
Jani Nikula07697462009-12-15 16:46:20 -0800945
946 return status;
947}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900948
949int gpio_sysfs_set_active_low(unsigned gpio, int value)
950{
951 return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value);
952}
Jani Nikula07697462009-12-15 16:46:20 -0800953EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
954
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700955/**
David Brownelld8f388d82008-07-25 01:46:07 -0700956 * gpio_unexport - reverse effect of gpio_export()
957 * @gpio: gpio to make unavailable
958 *
959 * This is implicit on gpio_free().
960 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900961static void gpiod_unexport(struct gpio_desc *desc)
David Brownelld8f388d82008-07-25 01:46:07 -0700962{
Jon Povey6a99ad42010-07-27 13:18:06 -0700963 int status = 0;
Ming Lei864533c2012-02-13 22:53:20 +0800964 struct device *dev = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -0700965
Alexandre Courbot372e7222013-02-03 01:29:29 +0900966 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900967 pr_warn("%s: invalid GPIO\n", __func__);
968 return;
Jon Povey6a99ad42010-07-27 13:18:06 -0700969 }
David Brownelld8f388d82008-07-25 01:46:07 -0700970
971 mutex_lock(&sysfs_lock);
972
David Brownelld8f388d82008-07-25 01:46:07 -0700973 if (test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700974
975 dev = class_find_device(&gpio_class, NULL, desc, match_export);
976 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700977 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700978 clear_bit(FLAG_EXPORT, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700979 } else
980 status = -ENODEV;
981 }
982
983 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900984
Ming Lei864533c2012-02-13 22:53:20 +0800985 if (dev) {
986 device_unregister(dev);
987 put_device(dev);
988 }
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900989
David Brownelld8f388d82008-07-25 01:46:07 -0700990 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900991 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
992 status);
993}
994
995void gpio_unexport(unsigned gpio)
996{
997 gpiod_unexport(gpio_to_desc(gpio));
David Brownelld8f388d82008-07-25 01:46:07 -0700998}
999EXPORT_SYMBOL_GPL(gpio_unexport);
1000
1001static int gpiochip_export(struct gpio_chip *chip)
1002{
1003 int status;
1004 struct device *dev;
1005
1006 /* Many systems register gpio chips for SOC support very early,
1007 * before driver model support is available. In those cases we
1008 * export this later, in gpiolib_sysfs_init() ... here we just
1009 * verify that _some_ field of gpio_class got initialized.
1010 */
1011 if (!gpio_class.p)
1012 return 0;
1013
1014 /* use chip->base for the ID; it's already known to be unique */
1015 mutex_lock(&sysfs_lock);
1016 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
1017 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001018 if (!IS_ERR(dev)) {
David Brownelld8f388d82008-07-25 01:46:07 -07001019 status = sysfs_create_group(&dev->kobj,
1020 &gpiochip_attr_group);
1021 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001022 status = PTR_ERR(dev);
David Brownelld8f388d82008-07-25 01:46:07 -07001023 chip->exported = (status == 0);
1024 mutex_unlock(&sysfs_lock);
1025
1026 if (status) {
1027 unsigned long flags;
1028 unsigned gpio;
1029
1030 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001031 gpio = 0;
1032 while (gpio < chip->ngpio)
1033 chip->desc[gpio++].chip = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -07001034 spin_unlock_irqrestore(&gpio_lock, flags);
1035
1036 pr_debug("%s: chip %s status %d\n", __func__,
1037 chip->label, status);
1038 }
1039
1040 return status;
1041}
1042
1043static void gpiochip_unexport(struct gpio_chip *chip)
1044{
1045 int status;
1046 struct device *dev;
1047
1048 mutex_lock(&sysfs_lock);
1049 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1050 if (dev) {
1051 put_device(dev);
1052 device_unregister(dev);
1053 chip->exported = 0;
1054 status = 0;
1055 } else
1056 status = -ENODEV;
1057 mutex_unlock(&sysfs_lock);
1058
1059 if (status)
1060 pr_debug("%s: chip %s status %d\n", __func__,
1061 chip->label, status);
1062}
1063
1064static int __init gpiolib_sysfs_init(void)
1065{
1066 int status;
1067 unsigned long flags;
Alexandre Courbot65493e32013-02-03 01:29:25 +09001068 struct gpio_chip *chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001069
1070 status = class_register(&gpio_class);
1071 if (status < 0)
1072 return status;
1073
1074 /* Scan and register the gpio_chips which registered very
1075 * early (e.g. before the class_register above was called).
1076 *
1077 * We run before arch_initcall() so chip->dev nodes can have
1078 * registered, and so arch_initcall() can always gpio_export().
1079 */
1080 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot65493e32013-02-03 01:29:25 +09001081 list_for_each_entry(chip, &gpio_chips, list) {
David Brownelld8f388d82008-07-25 01:46:07 -07001082 if (!chip || chip->exported)
1083 continue;
1084
1085 spin_unlock_irqrestore(&gpio_lock, flags);
1086 status = gpiochip_export(chip);
1087 spin_lock_irqsave(&gpio_lock, flags);
1088 }
1089 spin_unlock_irqrestore(&gpio_lock, flags);
1090
1091
1092 return status;
1093}
1094postcore_initcall(gpiolib_sysfs_init);
1095
1096#else
1097static inline int gpiochip_export(struct gpio_chip *chip)
1098{
1099 return 0;
1100}
1101
1102static inline void gpiochip_unexport(struct gpio_chip *chip)
1103{
1104}
1105
Alexandre Courbot372e7222013-02-03 01:29:29 +09001106static inline int gpiod_export(struct gpio_desc *desc,
1107 bool direction_may_change)
1108{
1109 return -ENOSYS;
1110}
1111
1112static inline int gpiod_export_link(struct device *dev, const char *name,
1113 struct gpio_desc *desc)
1114{
1115 return -ENOSYS;
1116}
1117
1118static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
1119{
1120 return -ENOSYS;
1121}
1122
1123static inline void gpiod_unexport(struct gpio_desc *desc)
1124{
1125}
1126
David Brownelld8f388d82008-07-25 01:46:07 -07001127#endif /* CONFIG_GPIO_SYSFS */
1128
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001129/*
1130 * Add a new chip to the global chips list, keeping the list of chips sorted
1131 * by base order.
1132 *
1133 * Return -EBUSY if the new chip overlaps with some other chip's integer
1134 * space.
1135 */
1136static int gpiochip_add_to_list(struct gpio_chip *chip)
1137{
1138 struct list_head *pos = &gpio_chips;
1139 struct gpio_chip *_chip;
1140 int err = 0;
1141
1142 /* find where to insert our chip */
1143 list_for_each(pos, &gpio_chips) {
1144 _chip = list_entry(pos, struct gpio_chip, list);
1145 /* shall we insert before _chip? */
1146 if (_chip->base >= chip->base + chip->ngpio)
1147 break;
1148 }
1149
1150 /* are we stepping on the chip right before? */
1151 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1152 _chip = list_entry(pos->prev, struct gpio_chip, list);
1153 if (_chip->base + _chip->ngpio > chip->base) {
1154 dev_err(chip->dev,
1155 "GPIO integer space overlap, cannot add chip\n");
1156 err = -EBUSY;
1157 }
1158 }
1159
1160 if (!err)
1161 list_add_tail(&chip->list, pos);
1162
1163 return err;
1164}
1165
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001166/**
David Brownelld2876d02008-02-04 22:28:20 -08001167 * gpiochip_add() - register a gpio_chip
1168 * @chip: the chip to register, with chip->base initialized
1169 * Context: potentially before irqs or kmalloc will work
1170 *
1171 * Returns a negative errno if the chip can't be registered, such as
1172 * because the chip->base is invalid or already associated with a
1173 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001174 *
David Brownelld8f388d82008-07-25 01:46:07 -07001175 * When gpiochip_add() is called very early during boot, so that GPIOs
1176 * can be freely used, the chip->dev device must be registered before
1177 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1178 * for GPIOs will fail rudely.
1179 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001180 * If chip->base is negative, this requests dynamic assignment of
1181 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001182 */
1183int gpiochip_add(struct gpio_chip *chip)
1184{
1185 unsigned long flags;
1186 int status = 0;
1187 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001188 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001189
Trent Piephobff5fda2008-05-23 13:04:44 -07001190 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001191 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001192 status = -EINVAL;
1193 goto fail;
1194 }
1195
1196 spin_lock_irqsave(&gpio_lock, flags);
1197
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001198 if (base < 0) {
1199 base = gpiochip_find_base(chip->ngpio);
1200 if (base < 0) {
1201 status = base;
David Brownelld8f388d82008-07-25 01:46:07 -07001202 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001203 }
1204 chip->base = base;
1205 }
1206
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001207 status = gpiochip_add_to_list(chip);
1208
David Brownelld2876d02008-02-04 22:28:20 -08001209 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001210 chip->desc = &gpio_desc[chip->base];
1211
1212 for (id = 0; id < chip->ngpio; id++) {
1213 struct gpio_desc *desc = &chip->desc[id];
1214 desc->chip = chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001215
1216 /* REVISIT: most hardware initializes GPIOs as
1217 * inputs (often with pullups enabled) so power
1218 * usage is minimized. Linux code should set the
1219 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +03001220 * and in case chip->get_direction is not set,
David Brownelld8f388d82008-07-25 01:46:07 -07001221 * we may expose the wrong direction in sysfs.
1222 */
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001223 desc->flags = !chip->direction_input
David Brownelld8f388d82008-07-25 01:46:07 -07001224 ? (1 << FLAG_IS_OUT)
1225 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001226 }
1227 }
1228
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001229 spin_unlock_irqrestore(&gpio_lock, flags);
1230
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301231#ifdef CONFIG_PINCTRL
1232 INIT_LIST_HEAD(&chip->pin_ranges);
1233#endif
1234
Anton Vorontsov391c9702010-06-08 07:48:17 -06001235 of_gpiochip_add(chip);
1236
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001237 if (status)
1238 goto fail;
1239
1240 status = gpiochip_export(chip);
1241 if (status)
1242 goto fail;
1243
H Hartley Sweetenee1c1e72012-05-11 16:58:33 -07001244 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
Grant Likely64842aa2011-11-06 11:36:18 -07001245 chip->base, chip->base + chip->ngpio - 1,
1246 chip->label ? : "generic");
1247
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001248 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001249
1250unlock:
1251 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld2876d02008-02-04 22:28:20 -08001252fail:
1253 /* failures here can mean systems won't boot... */
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001254 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1255 chip->base, chip->base + chip->ngpio - 1,
1256 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001257 return status;
1258}
1259EXPORT_SYMBOL_GPL(gpiochip_add);
1260
1261/**
1262 * gpiochip_remove() - unregister a gpio_chip
1263 * @chip: the chip to unregister
1264 *
1265 * A gpio_chip with any GPIOs still requested may not be removed.
1266 */
1267int gpiochip_remove(struct gpio_chip *chip)
1268{
1269 unsigned long flags;
1270 int status = 0;
1271 unsigned id;
1272
1273 spin_lock_irqsave(&gpio_lock, flags);
1274
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001275 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001276 of_gpiochip_remove(chip);
1277
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001278 for (id = 0; id < chip->ngpio; id++) {
1279 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
David Brownelld2876d02008-02-04 22:28:20 -08001280 status = -EBUSY;
1281 break;
1282 }
1283 }
1284 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001285 for (id = 0; id < chip->ngpio; id++)
1286 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001287
1288 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -08001289 }
1290
1291 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d82008-07-25 01:46:07 -07001292
1293 if (status == 0)
1294 gpiochip_unexport(chip);
1295
David Brownelld2876d02008-02-04 22:28:20 -08001296 return status;
1297}
1298EXPORT_SYMBOL_GPL(gpiochip_remove);
1299
Grant Likely594fa262010-06-08 07:48:16 -06001300/**
1301 * gpiochip_find() - iterator for locating a specific gpio_chip
1302 * @data: data to pass to match function
1303 * @callback: Callback function to check gpio_chip
1304 *
1305 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1306 * determined by a user supplied @match callback. The callback should return
1307 * 0 if the device doesn't match and non-zero if it does. If the callback is
1308 * non-zero, this function will return to the caller and not iterate over any
1309 * more gpio_chips.
1310 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001311struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001312 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001313 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001314{
Alexandre Courbot125eef92013-02-03 01:29:26 +09001315 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001316 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001317
1318 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +09001319 list_for_each_entry(chip, &gpio_chips, list)
1320 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001321 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001322
1323 /* No match? */
1324 if (&chip->list == &gpio_chips)
1325 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001326 spin_unlock_irqrestore(&gpio_lock, flags);
1327
1328 return chip;
1329}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001330EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001331
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301332#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001333
Linus Walleij3f0f8672012-11-20 12:40:15 +01001334/**
1335 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1336 * @chip: the gpiochip to add the range for
1337 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001338 * @gpio_offset: the start offset in the current gpio_chip number space
1339 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001340 * @npins: the number of pins from the offset of each pin space (GPIO and
1341 * pin controller) to accumulate in this range
1342 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001343int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001344 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001345 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301346{
1347 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001348 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301349
Linus Walleij3f0f8672012-11-20 12:40:15 +01001350 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301351 if (!pin_range) {
1352 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1353 chip->label);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001354 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301355 }
1356
Linus Walleij3f0f8672012-11-20 12:40:15 +01001357 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001358 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001359 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301360 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001361 pin_range->range.base = chip->base + gpio_offset;
1362 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301363 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001364 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301365 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001366 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001367 ret = PTR_ERR(pin_range->pctldev);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001368 pr_err("%s: GPIO chip: could not create pin range\n",
1369 chip->label);
1370 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001371 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001372 }
Linus Walleij316511c2012-11-21 08:48:09 +01001373 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1374 chip->label, gpio_offset, gpio_offset + npins - 1,
1375 pinctl_name,
1376 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301377
1378 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001379
1380 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301381}
Linus Walleij165adc92012-11-06 14:49:39 +01001382EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301383
Linus Walleij3f0f8672012-11-20 12:40:15 +01001384/**
1385 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1386 * @chip: the chip to remove all the mappings for
1387 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301388void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1389{
1390 struct gpio_pin_range *pin_range, *tmp;
1391
1392 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1393 list_del(&pin_range->node);
1394 pinctrl_remove_gpio_range(pin_range->pctldev,
1395 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001396 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301397 }
1398}
Linus Walleij165adc92012-11-06 14:49:39 +01001399EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1400
1401#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301402
David Brownelld2876d02008-02-04 22:28:20 -08001403/* These "optional" allocation calls help prevent drivers from stomping
1404 * on each other, and help provide better diagnostics in debugfs.
1405 * They're called even less than the "set direction" calls.
1406 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001407static int gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001408{
David Brownell35e8bb52008-10-15 22:03:16 -07001409 struct gpio_chip *chip;
Mark Browne9354572012-07-09 12:22:56 +01001410 int status = -EPROBE_DEFER;
David Brownelld2876d02008-02-04 22:28:20 -08001411 unsigned long flags;
1412
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001413 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001414 pr_warn("%s: invalid GPIO\n", __func__);
1415 return -EINVAL;
1416 }
1417
David Brownelld2876d02008-02-04 22:28:20 -08001418 spin_lock_irqsave(&gpio_lock, flags);
1419
David Brownell35e8bb52008-10-15 22:03:16 -07001420 chip = desc->chip;
David Brownelld2876d02008-02-04 22:28:20 -08001421
David Brownell35e8bb52008-10-15 22:03:16 -07001422 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001423 goto done;
1424
David Brownelld2876d02008-02-04 22:28:20 -08001425 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001426 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001427 */
1428
1429 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1430 desc_set_label(desc, label ? : "?");
1431 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001432 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001433 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001434 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001435 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001436 }
1437
1438 if (chip->request) {
1439 /* chip->request may sleep */
1440 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001441 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001442 spin_lock_irqsave(&gpio_lock, flags);
1443
1444 if (status < 0) {
1445 desc_set_label(desc, NULL);
1446 module_put(chip->owner);
1447 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001448 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001449 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001450 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001451 if (chip->get_direction) {
1452 /* chip->get_direction may sleep */
1453 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001454 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001455 spin_lock_irqsave(&gpio_lock, flags);
1456 }
David Brownelld2876d02008-02-04 22:28:20 -08001457done:
1458 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001459 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001460 desc_to_gpio(desc), label ? : "?", status);
David Brownelld2876d02008-02-04 22:28:20 -08001461 spin_unlock_irqrestore(&gpio_lock, flags);
1462 return status;
1463}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001464
1465int gpio_request(unsigned gpio, const char *label)
1466{
1467 return gpiod_request(gpio_to_desc(gpio), label);
1468}
David Brownelld2876d02008-02-04 22:28:20 -08001469EXPORT_SYMBOL_GPL(gpio_request);
1470
Alexandre Courbot372e7222013-02-03 01:29:29 +09001471static void gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001472{
1473 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001474 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001475
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001476 might_sleep();
1477
Alexandre Courbot372e7222013-02-03 01:29:29 +09001478 if (!desc) {
David Brownelld2876d02008-02-04 22:28:20 -08001479 WARN_ON(extra_checks);
1480 return;
1481 }
1482
Alexandre Courbot372e7222013-02-03 01:29:29 +09001483 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07001484
David Brownelld2876d02008-02-04 22:28:20 -08001485 spin_lock_irqsave(&gpio_lock, flags);
1486
David Brownell35e8bb52008-10-15 22:03:16 -07001487 chip = desc->chip;
1488 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1489 if (chip->free) {
1490 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001491 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001492 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001493 spin_lock_irqsave(&gpio_lock, flags);
1494 }
David Brownelld2876d02008-02-04 22:28:20 -08001495 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001496 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001497 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001498 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301499 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301500 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001501 } else
David Brownelld2876d02008-02-04 22:28:20 -08001502 WARN_ON(extra_checks);
1503
1504 spin_unlock_irqrestore(&gpio_lock, flags);
1505}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001506
1507void gpio_free(unsigned gpio)
1508{
1509 gpiod_free(gpio_to_desc(gpio));
1510}
David Brownelld2876d02008-02-04 22:28:20 -08001511EXPORT_SYMBOL_GPL(gpio_free);
1512
Eric Miao3e45f1d2010-03-05 13:44:35 -08001513/**
1514 * gpio_request_one - request a single GPIO with initial configuration
1515 * @gpio: the GPIO number
1516 * @flags: GPIO configuration as specified by GPIOF_*
1517 * @label: a literal description string of this GPIO
1518 */
1519int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1520{
Alexandre Courbot372e7222013-02-03 01:29:29 +09001521 struct gpio_desc *desc;
Eric Miao3e45f1d2010-03-05 13:44:35 -08001522 int err;
1523
Alexandre Courbot372e7222013-02-03 01:29:29 +09001524 desc = gpio_to_desc(gpio);
1525
1526 err = gpiod_request(desc, label);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001527 if (err)
1528 return err;
1529
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301530 if (flags & GPIOF_OPEN_DRAIN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001531 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301532
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301533 if (flags & GPIOF_OPEN_SOURCE)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001534 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301535
Eric Miao3e45f1d2010-03-05 13:44:35 -08001536 if (flags & GPIOF_DIR_IN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001537 err = gpiod_direction_input(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001538 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001539 err = gpiod_direction_output(desc,
Eric Miao3e45f1d2010-03-05 13:44:35 -08001540 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1541
Aaro Koskinene2548112010-12-21 17:24:22 -08001542 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001543 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -08001544
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001545 if (flags & GPIOF_EXPORT) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001546 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001547 if (err)
1548 goto free_gpio;
1549 }
1550
1551 return 0;
1552
1553 free_gpio:
Alexandre Courbot372e7222013-02-03 01:29:29 +09001554 gpiod_free(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001555 return err;
1556}
1557EXPORT_SYMBOL_GPL(gpio_request_one);
1558
1559/**
1560 * gpio_request_array - request multiple GPIOs in a single call
1561 * @array: array of the 'struct gpio'
1562 * @num: how many GPIOs in the array
1563 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001564int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001565{
1566 int i, err;
1567
1568 for (i = 0; i < num; i++, array++) {
1569 err = gpio_request_one(array->gpio, array->flags, array->label);
1570 if (err)
1571 goto err_free;
1572 }
1573 return 0;
1574
1575err_free:
1576 while (i--)
1577 gpio_free((--array)->gpio);
1578 return err;
1579}
1580EXPORT_SYMBOL_GPL(gpio_request_array);
1581
1582/**
1583 * gpio_free_array - release multiple GPIOs in a single call
1584 * @array: array of the 'struct gpio'
1585 * @num: how many GPIOs in the array
1586 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001587void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001588{
1589 while (num--)
1590 gpio_free((array++)->gpio);
1591}
1592EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001593
1594/**
1595 * gpiochip_is_requested - return string iff signal was requested
1596 * @chip: controller managing the signal
1597 * @offset: of signal within controller's 0..(ngpio - 1) range
1598 *
1599 * Returns NULL if the GPIO is not currently requested, else a string.
1600 * If debugfs support is enabled, the string returned is the label passed
1601 * to gpio_request(); otherwise it is a meaningless constant.
1602 *
1603 * This function is for use by GPIO controller drivers. The label can
1604 * help with diagnostics, and knowing that the signal is used as a GPIO
1605 * can help avoid accidentally multiplexing it to another controller.
1606 */
1607const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1608{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001609 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001610
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001611 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -08001612 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001613
1614 desc = &chip->desc[offset];
1615
Alexandre Courbot372e7222013-02-03 01:29:29 +09001616 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001617 return NULL;
1618#ifdef CONFIG_DEBUG_FS
Alexandre Courbot372e7222013-02-03 01:29:29 +09001619 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001620#else
1621 return "?";
1622#endif
1623}
1624EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1625
1626
1627/* Drivers MUST set GPIO direction before making get/set calls. In
1628 * some cases this is done in early boot, before IRQs are enabled.
1629 *
1630 * As a rule these aren't called more than once (except for drivers
1631 * using the open-drain emulation idiom) so these are natural places
1632 * to accumulate extra debugging checks. Note that we can't (yet)
1633 * rely on gpio_request() having been called beforehand.
1634 */
1635
Alexandre Courbot372e7222013-02-03 01:29:29 +09001636static int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001637{
1638 unsigned long flags;
1639 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001640 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001641 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001642
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001643 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001644 pr_warn("%s: invalid GPIO\n", __func__);
1645 return -EINVAL;
1646 }
1647
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001648 chip = desc->chip;
1649 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001650 gpiod_warn(desc,
1651 "%s: missing get() or direction_input() operations\n",
1652 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001653 return -EIO;
1654 }
1655
David Brownelld2876d02008-02-04 22:28:20 -08001656 spin_lock_irqsave(&gpio_lock, flags);
1657
Alexandre Courbot372e7222013-02-03 01:29:29 +09001658 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001659 if (status < 0)
1660 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001661
1662 /* now we know the gpio is valid and chip won't vanish */
1663
1664 spin_unlock_irqrestore(&gpio_lock, flags);
1665
David Brownell9c4ba942010-08-10 18:02:24 -07001666 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001667
Alexandre Courbot372e7222013-02-03 01:29:29 +09001668 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001669 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001670 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001671 if (status < 0) {
Mark Brown6424de52013-09-09 10:33:49 +01001672 gpiod_dbg(desc, "chip request fail, %d\n", status);
David Brownell35e8bb52008-10-15 22:03:16 -07001673 /* and it's not available to anyone else ...
1674 * gpio_request() is the fully clean solution.
1675 */
1676 goto lose;
1677 }
1678 }
1679
Alexandre Courbot372e7222013-02-03 01:29:29 +09001680 status = chip->direction_input(chip, offset);
David Brownelld2876d02008-02-04 22:28:20 -08001681 if (status == 0)
1682 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001683
Alexandre Courbot372e7222013-02-03 01:29:29 +09001684 trace_gpio_direction(desc_to_gpio(desc), 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001685lose:
David Brownelld2876d02008-02-04 22:28:20 -08001686 return status;
1687fail:
1688 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001689 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001690 gpiod_dbg(desc, "%s status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001691 return status;
1692}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001693
1694int gpio_direction_input(unsigned gpio)
1695{
1696 return gpiod_direction_input(gpio_to_desc(gpio));
1697}
David Brownelld2876d02008-02-04 22:28:20 -08001698EXPORT_SYMBOL_GPL(gpio_direction_input);
1699
Alexandre Courbot372e7222013-02-03 01:29:29 +09001700static int gpiod_direction_output(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001701{
1702 unsigned long flags;
1703 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001704 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001705 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001706
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001707 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001708 pr_warn("%s: invalid GPIO\n", __func__);
1709 return -EINVAL;
1710 }
1711
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301712 /* Open drain pin should not be driven to 1 */
1713 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001714 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301715
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301716 /* Open source pin should not be driven to 0 */
1717 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001718 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301719
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001720 chip = desc->chip;
1721 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001722 gpiod_warn(desc,
1723 "%s: missing set() or direction_output() operations\n",
1724 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001725 return -EIO;
1726 }
1727
David Brownelld2876d02008-02-04 22:28:20 -08001728 spin_lock_irqsave(&gpio_lock, flags);
1729
Alexandre Courbot372e7222013-02-03 01:29:29 +09001730 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001731 if (status < 0)
1732 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001733
1734 /* now we know the gpio is valid and chip won't vanish */
1735
1736 spin_unlock_irqrestore(&gpio_lock, flags);
1737
David Brownell9c4ba942010-08-10 18:02:24 -07001738 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001739
Alexandre Courbot372e7222013-02-03 01:29:29 +09001740 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001741 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001742 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001743 if (status < 0) {
Mark Brown6424de52013-09-09 10:33:49 +01001744 gpiod_dbg(desc, "chip request fail, %d\n", status);
David Brownell35e8bb52008-10-15 22:03:16 -07001745 /* and it's not available to anyone else ...
1746 * gpio_request() is the fully clean solution.
1747 */
1748 goto lose;
1749 }
1750 }
1751
Alexandre Courbot372e7222013-02-03 01:29:29 +09001752 status = chip->direction_output(chip, offset, value);
David Brownelld2876d02008-02-04 22:28:20 -08001753 if (status == 0)
1754 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001755 trace_gpio_value(desc_to_gpio(desc), 0, value);
1756 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001757lose:
David Brownelld2876d02008-02-04 22:28:20 -08001758 return status;
1759fail:
1760 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001761 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001762 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001763 return status;
1764}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001765
1766int gpio_direction_output(unsigned gpio, int value)
1767{
1768 return gpiod_direction_output(gpio_to_desc(gpio), value);
1769}
David Brownelld2876d02008-02-04 22:28:20 -08001770EXPORT_SYMBOL_GPL(gpio_direction_output);
1771
Felipe Balbic4b5be92010-05-26 14:42:23 -07001772/**
1773 * gpio_set_debounce - sets @debounce time for a @gpio
1774 * @gpio: the gpio to set debounce time
1775 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001776 *
1777 * returns -ENOTSUPP if the controller does not support setting
1778 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001779 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001780static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001781{
1782 unsigned long flags;
1783 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001784 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001785 int offset;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001786
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001787 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001788 pr_warn("%s: invalid GPIO\n", __func__);
1789 return -EINVAL;
1790 }
1791
Felipe Balbic4b5be92010-05-26 14:42:23 -07001792 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001793 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001794 gpiod_dbg(desc,
1795 "%s: missing set() or set_debounce() operations\n",
1796 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001797 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001798 }
1799
1800 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001801
1802 status = gpio_ensure_requested(desc);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001803 if (status < 0)
1804 goto fail;
1805
1806 /* now we know the gpio is valid and chip won't vanish */
1807
1808 spin_unlock_irqrestore(&gpio_lock, flags);
1809
David Brownell9c4ba942010-08-10 18:02:24 -07001810 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001811
Alexandre Courbot372e7222013-02-03 01:29:29 +09001812 offset = gpio_chip_hwgpio(desc);
1813 return chip->set_debounce(chip, offset, debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001814
1815fail:
1816 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001817 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001818 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001819
1820 return status;
1821}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001822
1823int gpio_set_debounce(unsigned gpio, unsigned debounce)
1824{
1825 return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
1826}
Felipe Balbic4b5be92010-05-26 14:42:23 -07001827EXPORT_SYMBOL_GPL(gpio_set_debounce);
David Brownelld2876d02008-02-04 22:28:20 -08001828
1829/* I/O calls are only valid after configuration completed; the relevant
1830 * "is this a valid GPIO" error checks should already have been done.
1831 *
1832 * "Get" operations are often inlinable as reading a pin value register,
1833 * and masking the relevant bit in that register.
1834 *
1835 * When "set" operations are inlinable, they involve writing that mask to
1836 * one register to set a low value, or a different register to set it high.
1837 * Otherwise locking is needed, so there may be little value to inlining.
1838 *
1839 *------------------------------------------------------------------------
1840 *
1841 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1842 * have requested the GPIO. That can include implicit requesting by
1843 * a direction setting call. Marking a gpio as requested locks its chip
1844 * in memory, guaranteeing that these table lookups need no more locking
1845 * and that gpiochip_remove() will fail.
1846 *
1847 * REVISIT when debugging, consider adding some instrumentation to ensure
1848 * that the GPIO was actually requested.
1849 */
1850
1851/**
1852 * __gpio_get_value() - return a gpio's value
1853 * @gpio: gpio whose value will be returned
1854 * Context: any
1855 *
1856 * This is used directly or indirectly to implement gpio_get_value().
1857 * It returns the zero or nonzero value provided by the associated
1858 * gpio_chip.get() method; or zero if no such method is provided.
1859 */
Alexandre Courbotdef63432013-02-15 14:46:15 +09001860static int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001861{
1862 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001863 int value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001864 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001865
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001866 if (!desc)
1867 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001868 chip = desc->chip;
1869 offset = gpio_chip_hwgpio(desc);
Mark Browne4e449e2012-02-17 10:46:00 -08001870 /* Should be using gpio_get_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001871 WARN_ON(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001872 value = chip->get ? chip->get(chip, offset) : 0;
1873 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001874 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001875}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001876
1877int __gpio_get_value(unsigned gpio)
1878{
1879 return gpiod_get_value(gpio_to_desc(gpio));
1880}
David Brownelld2876d02008-02-04 22:28:20 -08001881EXPORT_SYMBOL_GPL(__gpio_get_value);
1882
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301883/*
1884 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1885 * @gpio: Gpio whose state need to be set.
1886 * @chip: Gpio chip.
1887 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1888 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001889static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301890{
1891 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001892 struct gpio_chip *chip = desc->chip;
1893 int offset = gpio_chip_hwgpio(desc);
1894
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301895 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001896 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301897 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001898 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301899 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001900 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301901 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001902 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301903 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001904 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301905 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001906 gpiod_err(desc,
1907 "%s: Error in set_value for open drain err %d\n",
1908 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301909}
1910
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301911/*
1912 * _gpio_set_open_source() - Set the open source gpio's value.
1913 * @gpio: Gpio whose state need to be set.
1914 * @chip: Gpio chip.
1915 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1916 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001917static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301918{
1919 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001920 struct gpio_chip *chip = desc->chip;
1921 int offset = gpio_chip_hwgpio(desc);
1922
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301923 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001924 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301925 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001926 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301927 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001928 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301929 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001930 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301931 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001932 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301933 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001934 gpiod_err(desc,
1935 "%s: Error in set_value for open source err %d\n",
1936 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301937}
1938
David Brownelld2876d02008-02-04 22:28:20 -08001939/**
1940 * __gpio_set_value() - assign a gpio's value
1941 * @gpio: gpio whose value will be assigned
1942 * @value: value to assign
1943 * Context: any
1944 *
1945 * This is used directly or indirectly to implement gpio_set_value().
1946 * It invokes the associated gpio_chip.set() method.
1947 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001948static void gpiod_set_value(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001949{
1950 struct gpio_chip *chip;
1951
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001952 if (!desc)
1953 return;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001954 chip = desc->chip;
Mark Browne4e449e2012-02-17 10:46:00 -08001955 /* Should be using gpio_set_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001956 WARN_ON(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001957 trace_gpio_value(desc_to_gpio(desc), 0, value);
1958 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1959 _gpio_set_open_drain_value(desc, value);
1960 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1961 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301962 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001963 chip->set(chip, gpio_chip_hwgpio(desc), value);
1964}
1965
1966void __gpio_set_value(unsigned gpio, int value)
1967{
1968 return gpiod_set_value(gpio_to_desc(gpio), value);
David Brownelld2876d02008-02-04 22:28:20 -08001969}
1970EXPORT_SYMBOL_GPL(__gpio_set_value);
1971
1972/**
1973 * __gpio_cansleep() - report whether gpio value access will sleep
1974 * @gpio: gpio in question
1975 * Context: any
1976 *
1977 * This is used directly or indirectly to implement gpio_cansleep(). It
1978 * returns nonzero if access reading or writing the GPIO value can sleep.
1979 */
Alexandre Courbotdef63432013-02-15 14:46:15 +09001980static int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001981{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001982 if (!desc)
1983 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001984 /* only call this on GPIOs that are valid! */
1985 return desc->chip->can_sleep;
1986}
1987
David Brownelld2876d02008-02-04 22:28:20 -08001988int __gpio_cansleep(unsigned gpio)
1989{
Alexandre Courbot372e7222013-02-03 01:29:29 +09001990 return gpiod_cansleep(gpio_to_desc(gpio));
David Brownelld2876d02008-02-04 22:28:20 -08001991}
1992EXPORT_SYMBOL_GPL(__gpio_cansleep);
1993
David Brownell0f6d5042008-10-15 22:03:14 -07001994/**
1995 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1996 * @gpio: gpio whose IRQ will be returned (already requested)
1997 * Context: any
1998 *
1999 * This is used directly or indirectly to implement gpio_to_irq().
2000 * It returns the number of the IRQ signaled by this (input) GPIO,
2001 * or a negative errno.
2002 */
Alexandre Courbotdef63432013-02-15 14:46:15 +09002003static int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002004{
2005 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002006 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002007
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002008 if (!desc)
2009 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002010 chip = desc->chip;
2011 offset = gpio_chip_hwgpio(desc);
2012 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
2013}
2014
2015int __gpio_to_irq(unsigned gpio)
2016{
2017 return gpiod_to_irq(gpio_to_desc(gpio));
David Brownell0f6d5042008-10-15 22:03:14 -07002018}
2019EXPORT_SYMBOL_GPL(__gpio_to_irq);
2020
David Brownelld2876d02008-02-04 22:28:20 -08002021
David Brownelld2876d02008-02-04 22:28:20 -08002022/* There's no value in making it easy to inline GPIO calls that may sleep.
2023 * Common examples include ones connected to I2C or SPI chips.
2024 */
2025
Alexandre Courbotdef63432013-02-15 14:46:15 +09002026static int gpiod_get_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002027{
2028 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002029 int value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002030 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08002031
2032 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002033 if (!desc)
2034 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002035 chip = desc->chip;
2036 offset = gpio_chip_hwgpio(desc);
2037 value = chip->get ? chip->get(chip, offset) : 0;
2038 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002039 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002040}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002041
2042int gpio_get_value_cansleep(unsigned gpio)
2043{
2044 return gpiod_get_value_cansleep(gpio_to_desc(gpio));
2045}
David Brownelld2876d02008-02-04 22:28:20 -08002046EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
2047
Alexandre Courbot372e7222013-02-03 01:29:29 +09002048static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002049{
2050 struct gpio_chip *chip;
2051
2052 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002053 if (!desc)
2054 return;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002055 chip = desc->chip;
2056 trace_gpio_value(desc_to_gpio(desc), 0, value);
2057 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2058 _gpio_set_open_drain_value(desc, value);
2059 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2060 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302061 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002062 chip->set(chip, gpio_chip_hwgpio(desc), value);
2063}
2064
2065void gpio_set_value_cansleep(unsigned gpio, int value)
2066{
2067 return gpiod_set_value_cansleep(gpio_to_desc(gpio), value);
David Brownelld2876d02008-02-04 22:28:20 -08002068}
2069EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
2070
David Brownelld2876d02008-02-04 22:28:20 -08002071#ifdef CONFIG_DEBUG_FS
2072
David Brownelld2876d02008-02-04 22:28:20 -08002073static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2074{
2075 unsigned i;
2076 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002077 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002078 int is_out;
2079
2080 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2081 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2082 continue;
2083
Alexandre Courbot372e7222013-02-03 01:29:29 +09002084 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002085 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Jarkko Nikula6e8ba722008-11-19 15:36:17 -08002086 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002087 gpio, gdesc->label,
2088 is_out ? "out" : "in ",
2089 chip->get
2090 ? (chip->get(chip, i) ? "hi" : "lo")
2091 : "? ");
David Brownelld2876d02008-02-04 22:28:20 -08002092 seq_printf(s, "\n");
2093 }
2094}
2095
Thierry Redingf9c4a312012-04-12 13:26:01 +02002096static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002097{
Grant Likely362432a2013-02-09 09:41:49 +00002098 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002099 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002100 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002101
2102 s->private = "";
2103
Grant Likely362432a2013-02-09 09:41:49 +00002104 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002105 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002106 if (index-- == 0) {
2107 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002108 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002109 }
2110 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002111
2112 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002113}
2114
2115static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2116{
Grant Likely362432a2013-02-09 09:41:49 +00002117 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002118 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002119 void *ret = NULL;
2120
Grant Likely362432a2013-02-09 09:41:49 +00002121 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002122 if (list_is_last(&chip->list, &gpio_chips))
2123 ret = NULL;
2124 else
2125 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002126 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002127
2128 s->private = "\n";
2129 ++*pos;
2130
2131 return ret;
2132}
2133
2134static void gpiolib_seq_stop(struct seq_file *s, void *v)
2135{
2136}
2137
2138static int gpiolib_seq_show(struct seq_file *s, void *v)
2139{
2140 struct gpio_chip *chip = v;
2141 struct device *dev;
2142
2143 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2144 chip->base, chip->base + chip->ngpio - 1);
2145 dev = chip->dev;
2146 if (dev)
2147 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2148 dev_name(dev));
2149 if (chip->label)
2150 seq_printf(s, ", %s", chip->label);
2151 if (chip->can_sleep)
2152 seq_printf(s, ", can sleep");
2153 seq_printf(s, ":\n");
2154
2155 if (chip->dbg_show)
2156 chip->dbg_show(s, chip);
2157 else
2158 gpiolib_dbg_show(s, chip);
2159
David Brownelld2876d02008-02-04 22:28:20 -08002160 return 0;
2161}
2162
Thierry Redingf9c4a312012-04-12 13:26:01 +02002163static const struct seq_operations gpiolib_seq_ops = {
2164 .start = gpiolib_seq_start,
2165 .next = gpiolib_seq_next,
2166 .stop = gpiolib_seq_stop,
2167 .show = gpiolib_seq_show,
2168};
2169
David Brownelld2876d02008-02-04 22:28:20 -08002170static int gpiolib_open(struct inode *inode, struct file *file)
2171{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002172 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002173}
2174
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002175static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002176 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002177 .open = gpiolib_open,
2178 .read = seq_read,
2179 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002180 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002181};
2182
2183static int __init gpiolib_debugfs_init(void)
2184{
2185 /* /sys/kernel/debug/gpio */
2186 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2187 NULL, NULL, &gpiolib_operations);
2188 return 0;
2189}
2190subsys_initcall(gpiolib_debugfs_init);
2191
2192#endif /* DEBUG_FS */