blob: ca1cb2d756c29c36ae17cb2ca3091004c3b32666 [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
105
David Brownelld2876d02008-02-04 22:28:20 -0800106static inline void desc_set_label(struct gpio_desc *d, const char *label)
107{
108#ifdef CONFIG_DEBUG_FS
109 d->label = label;
110#endif
111}
112
Alexandre Courbot372e7222013-02-03 01:29:29 +0900113/*
114 * Return the GPIO number of the passed descriptor relative to its chip
115 */
116static int gpio_chip_hwgpio(const struct gpio_desc *desc)
117{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900118 return desc - &desc->chip->desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900119}
120
121/**
122 * Convert a GPIO number to its descriptor
123 */
124static struct gpio_desc *gpio_to_desc(unsigned gpio)
125{
126 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
127 return NULL;
128 else
129 return &gpio_desc[gpio];
130}
131
132/**
133 * Convert a GPIO descriptor to the integer namespace.
134 * This should disappear in the future but is needed since we still
135 * use GPIO numbers for error messages and sysfs nodes
136 */
137static int desc_to_gpio(const struct gpio_desc *desc)
138{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900139 return desc->chip->base + gpio_chip_hwgpio(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900140}
141
142
David Brownelld2876d02008-02-04 22:28:20 -0800143/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
144 * when setting direction, and otherwise illegal. Until board setup code
145 * and drivers use explicit requests everywhere (which won't happen when
146 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -0700147 * message should motivate switching to explicit requests... so should
148 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -0700149 *
150 * NOTE: the autorequest mechanism is going away; at this point it's
151 * only "legal" in the sense that (old) code using it won't break yet,
152 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -0800153 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900154static int gpio_ensure_requested(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800155{
David Brownell8a0cecf2009-04-02 16:57:06 -0700156 const struct gpio_chip *chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900157 const int gpio = desc_to_gpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -0700158
David Brownell8a0cecf2009-04-02 16:57:06 -0700159 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
160 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700161 if (!try_module_get(chip->owner)) {
162 pr_err("GPIO-%d: module can't be gotten \n", gpio);
163 clear_bit(FLAG_REQUESTED, &desc->flags);
164 /* lose */
165 return -EIO;
166 }
David Brownelld2876d02008-02-04 22:28:20 -0800167 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700168 /* caller must chip->request() w/o spinlock */
169 if (chip->request)
170 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800171 }
David Brownell35e8bb52008-10-15 22:03:16 -0700172 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800173}
174
Alexandre Courbotdef63432013-02-15 14:46:15 +0900175static struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900176{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900177 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900178}
179
Alexandre Courbot24d76282013-02-15 14:46:16 +0900180/* caller holds gpio_lock *OR* gpio is marked as requested */
Grant Likely1a2d3972011-12-12 09:25:57 -0700181struct gpio_chip *gpio_to_chip(unsigned gpio)
David Brownelld2876d02008-02-04 22:28:20 -0800182{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900183 return gpiod_to_chip(gpio_to_desc(gpio));
David Brownelld2876d02008-02-04 22:28:20 -0800184}
185
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700186/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
187static int gpiochip_find_base(int ngpio)
188{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900189 struct gpio_chip *chip;
190 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700191
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900192 list_for_each_entry_reverse(chip, &gpio_chips, list) {
193 /* found a free space? */
194 if (chip->base + chip->ngpio <= base)
195 break;
196 else
197 /* nope, check the space right before the chip */
198 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700199 }
200
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900201 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700202 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900203 return base;
204 } else {
205 pr_err("%s: cannot find free range\n", __func__);
206 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700207 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700208}
209
Mathias Nyman80b0a602012-10-24 17:25:27 +0300210/* caller ensures gpio is valid and requested, chip->get_direction may sleep */
Alexandre Courbotdef63432013-02-15 14:46:15 +0900211static int gpiod_get_direction(const struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300212{
213 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900214 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300215 int status = -EINVAL;
216
Alexandre Courbot372e7222013-02-03 01:29:29 +0900217 chip = gpiod_to_chip(desc);
218 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300219
220 if (!chip->get_direction)
221 return status;
222
Alexandre Courbot372e7222013-02-03 01:29:29 +0900223 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300224 if (status > 0) {
225 /* GPIOF_DIR_IN, or other positive */
226 status = 1;
Alexandre Courbotdef63432013-02-15 14:46:15 +0900227 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
228 * so it does not affect constness per se */
229 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300230 }
231 if (status == 0) {
232 /* GPIOF_DIR_OUT */
Alexandre Courbotdef63432013-02-15 14:46:15 +0900233 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300234 }
235 return status;
236}
237
David Brownelld8f388d82008-07-25 01:46:07 -0700238#ifdef CONFIG_GPIO_SYSFS
239
240/* lock protects against unexport_gpio() being called while
241 * sysfs files are active.
242 */
243static DEFINE_MUTEX(sysfs_lock);
244
245/*
246 * /sys/class/gpio/gpioN... only for GPIOs that are exported
247 * /direction
248 * * MAY BE OMITTED if kernel won't allow direction changes
249 * * is read/write as "in" or "out"
250 * * may also be written as "high" or "low", initializing
251 * output value as specified ("out" implies "low")
252 * /value
253 * * always readable, subject to hardware behavior
254 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700255 * /edge
256 * * configures behavior of poll(2) on /value
257 * * available only if pin can generate IRQs on input
258 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800259 * /active_low
260 * * configures polarity of /value
261 * * is read/write as zero/nonzero
262 * * also affects existing and subsequent "falling" and "rising"
263 * /edge configuration
David Brownelld8f388d82008-07-25 01:46:07 -0700264 */
265
266static ssize_t gpio_direction_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
268{
Alexandre Courbotdef63432013-02-15 14:46:15 +0900269 const struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700270 ssize_t status;
271
272 mutex_lock(&sysfs_lock);
273
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900274 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700275 status = -EIO;
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900276 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +0900277 gpiod_get_direction(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700278 status = sprintf(buf, "%s\n",
279 test_bit(FLAG_IS_OUT, &desc->flags)
280 ? "out" : "in");
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900281 }
David Brownelld8f388d82008-07-25 01:46:07 -0700282
283 mutex_unlock(&sysfs_lock);
284 return status;
285}
286
287static ssize_t gpio_direction_store(struct device *dev,
288 struct device_attribute *attr, const char *buf, size_t size)
289{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900290 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700291 ssize_t status;
292
293 mutex_lock(&sysfs_lock);
294
295 if (!test_bit(FLAG_EXPORT, &desc->flags))
296 status = -EIO;
297 else if (sysfs_streq(buf, "high"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900298 status = gpiod_direction_output(desc, 1);
David Brownelld8f388d82008-07-25 01:46:07 -0700299 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900300 status = gpiod_direction_output(desc, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700301 else if (sysfs_streq(buf, "in"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900302 status = gpiod_direction_input(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700303 else
304 status = -EINVAL;
305
306 mutex_unlock(&sysfs_lock);
307 return status ? : size;
308}
309
Jani Nikula07697462009-12-15 16:46:20 -0800310static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700311 gpio_direction_show, gpio_direction_store);
312
313static ssize_t gpio_value_show(struct device *dev,
314 struct device_attribute *attr, char *buf)
315{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900316 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700317 ssize_t status;
318
319 mutex_lock(&sysfs_lock);
320
Jani Nikula07697462009-12-15 16:46:20 -0800321 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700322 status = -EIO;
Jani Nikula07697462009-12-15 16:46:20 -0800323 } else {
324 int value;
325
Alexandre Courbot372e7222013-02-03 01:29:29 +0900326 value = !!gpiod_get_value_cansleep(desc);
Jani Nikula07697462009-12-15 16:46:20 -0800327 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
328 value = !value;
329
330 status = sprintf(buf, "%d\n", value);
331 }
David Brownelld8f388d82008-07-25 01:46:07 -0700332
333 mutex_unlock(&sysfs_lock);
334 return status;
335}
336
337static ssize_t gpio_value_store(struct device *dev,
338 struct device_attribute *attr, const char *buf, size_t size)
339{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900340 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700341 ssize_t status;
342
343 mutex_lock(&sysfs_lock);
344
345 if (!test_bit(FLAG_EXPORT, &desc->flags))
346 status = -EIO;
347 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
348 status = -EPERM;
349 else {
350 long value;
351
352 status = strict_strtol(buf, 0, &value);
353 if (status == 0) {
Jani Nikula07697462009-12-15 16:46:20 -0800354 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
355 value = !value;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900356 gpiod_set_value_cansleep(desc, value != 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700357 status = size;
358 }
359 }
360
361 mutex_unlock(&sysfs_lock);
362 return status;
363}
364
Johan Hovolda9808402015-01-13 13:00:05 +0100365static DEVICE_ATTR(value, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700366 gpio_value_show, gpio_value_store);
367
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700368static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
369{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700370 struct sysfs_dirent *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700371
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700372 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700373 return IRQ_HANDLED;
374}
375
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700376static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
377 unsigned long gpio_flags)
378{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700379 struct sysfs_dirent *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700380 unsigned long irq_flags;
381 int ret, irq, id;
382
383 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
384 return 0;
385
Alexandre Courbot372e7222013-02-03 01:29:29 +0900386 irq = gpiod_to_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700387 if (irq < 0)
388 return -EIO;
389
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700390 id = desc->flags >> ID_SHIFT;
391 value_sd = idr_find(&dirent_idr, id);
392 if (value_sd)
393 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700394
395 desc->flags &= ~GPIO_TRIGGER_MASK;
396
397 if (!gpio_flags) {
398 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700399 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700400 }
401
402 irq_flags = IRQF_SHARED;
403 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800404 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
405 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700406 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800407 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
408 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700409
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700410 if (!value_sd) {
411 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
412 if (!value_sd) {
413 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700414 goto err_out;
415 }
416
Tejun Heo62f516b2013-02-27 17:04:06 -0800417 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
418 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700419 goto free_sd;
Tejun Heo62f516b2013-02-27 17:04:06 -0800420 id = ret;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700421
422 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700423 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700424
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700425 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700426 ret = -ERANGE;
427 goto free_id;
428 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700429 }
430
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700431 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700432 "gpiolib", value_sd);
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700433 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700434 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700435
436 desc->flags |= gpio_flags;
437 return 0;
438
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700439free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700440 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700441 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700442free_sd:
443 if (value_sd)
444 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700445err_out:
446 return ret;
447}
448
449static const struct {
450 const char *name;
451 unsigned long flags;
452} trigger_types[] = {
453 { "none", 0 },
454 { "falling", BIT(FLAG_TRIG_FALL) },
455 { "rising", BIT(FLAG_TRIG_RISE) },
456 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
457};
458
459static ssize_t gpio_edge_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
461{
462 const struct gpio_desc *desc = dev_get_drvdata(dev);
463 ssize_t status;
464
465 mutex_lock(&sysfs_lock);
466
467 if (!test_bit(FLAG_EXPORT, &desc->flags))
468 status = -EIO;
469 else {
470 int i;
471
472 status = 0;
473 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
474 if ((desc->flags & GPIO_TRIGGER_MASK)
475 == trigger_types[i].flags) {
476 status = sprintf(buf, "%s\n",
477 trigger_types[i].name);
478 break;
479 }
480 }
481
482 mutex_unlock(&sysfs_lock);
483 return status;
484}
485
486static ssize_t gpio_edge_store(struct device *dev,
487 struct device_attribute *attr, const char *buf, size_t size)
488{
489 struct gpio_desc *desc = dev_get_drvdata(dev);
490 ssize_t status;
491 int i;
492
493 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
494 if (sysfs_streq(trigger_types[i].name, buf))
495 goto found;
496 return -EINVAL;
497
498found:
499 mutex_lock(&sysfs_lock);
500
501 if (!test_bit(FLAG_EXPORT, &desc->flags))
502 status = -EIO;
503 else {
504 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
505 if (!status)
506 status = size;
507 }
508
509 mutex_unlock(&sysfs_lock);
510
511 return status;
512}
513
514static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
515
Jani Nikula07697462009-12-15 16:46:20 -0800516static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
517 int value)
518{
519 int status = 0;
520
521 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
522 return 0;
523
524 if (value)
525 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
526 else
527 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
528
529 /* reconfigure poll(2) support if enabled on one edge only */
530 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
531 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
532 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
533
534 gpio_setup_irq(desc, dev, 0);
535 status = gpio_setup_irq(desc, dev, trigger_flags);
536 }
537
538 return status;
539}
540
541static ssize_t gpio_active_low_show(struct device *dev,
542 struct device_attribute *attr, char *buf)
543{
544 const struct gpio_desc *desc = dev_get_drvdata(dev);
545 ssize_t status;
546
547 mutex_lock(&sysfs_lock);
548
549 if (!test_bit(FLAG_EXPORT, &desc->flags))
550 status = -EIO;
551 else
552 status = sprintf(buf, "%d\n",
553 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
554
555 mutex_unlock(&sysfs_lock);
556
557 return status;
558}
559
560static ssize_t gpio_active_low_store(struct device *dev,
561 struct device_attribute *attr, const char *buf, size_t size)
562{
563 struct gpio_desc *desc = dev_get_drvdata(dev);
564 ssize_t status;
565
566 mutex_lock(&sysfs_lock);
567
568 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
569 status = -EIO;
570 } else {
571 long value;
572
573 status = strict_strtol(buf, 0, &value);
574 if (status == 0)
575 status = sysfs_set_active_low(desc, dev, value != 0);
576 }
577
578 mutex_unlock(&sysfs_lock);
579
580 return status ? : size;
581}
582
Johan Hovolda9808402015-01-13 13:00:05 +0100583static DEVICE_ATTR(active_low, 0644,
Jani Nikula07697462009-12-15 16:46:20 -0800584 gpio_active_low_show, gpio_active_low_store);
585
Johan Hovolda9808402015-01-13 13:00:05 +0100586static struct attribute *gpio_attrs[] = {
David Brownelld8f388d82008-07-25 01:46:07 -0700587 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800588 &dev_attr_active_low.attr,
David Brownelld8f388d82008-07-25 01:46:07 -0700589 NULL,
590};
591
592static const struct attribute_group gpio_attr_group = {
Johan Hovolda9808402015-01-13 13:00:05 +0100593 .attrs = gpio_attrs,
David Brownelld8f388d82008-07-25 01:46:07 -0700594};
595
596/*
597 * /sys/class/gpio/gpiochipN/
598 * /base ... matching gpio_chip.base (N)
599 * /label ... matching gpio_chip.label
600 * /ngpio ... matching gpio_chip.ngpio
601 */
602
603static ssize_t chip_base_show(struct device *dev,
604 struct device_attribute *attr, char *buf)
605{
606 const struct gpio_chip *chip = dev_get_drvdata(dev);
607
608 return sprintf(buf, "%d\n", chip->base);
609}
610static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
611
612static ssize_t chip_label_show(struct device *dev,
613 struct device_attribute *attr, char *buf)
614{
615 const struct gpio_chip *chip = dev_get_drvdata(dev);
616
617 return sprintf(buf, "%s\n", chip->label ? : "");
618}
619static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
620
621static ssize_t chip_ngpio_show(struct device *dev,
622 struct device_attribute *attr, char *buf)
623{
624 const struct gpio_chip *chip = dev_get_drvdata(dev);
625
626 return sprintf(buf, "%u\n", chip->ngpio);
627}
628static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
629
Johan Hovoldaa35a482015-01-13 13:00:04 +0100630static struct attribute *gpiochip_attrs[] = {
David Brownelld8f388d82008-07-25 01:46:07 -0700631 &dev_attr_base.attr,
632 &dev_attr_label.attr,
633 &dev_attr_ngpio.attr,
634 NULL,
635};
636
637static const struct attribute_group gpiochip_attr_group = {
Johan Hovoldaa35a482015-01-13 13:00:04 +0100638 .attrs = gpiochip_attrs,
David Brownelld8f388d82008-07-25 01:46:07 -0700639};
640
641/*
642 * /sys/class/gpio/export ... write-only
643 * integer N ... number of GPIO to export (full access)
644 * /sys/class/gpio/unexport ... write-only
645 * integer N ... number of GPIO to unexport
646 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100647static ssize_t export_store(struct class *class,
648 struct class_attribute *attr,
649 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700650{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900651 long gpio;
652 struct gpio_desc *desc;
653 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700654
655 status = strict_strtol(buf, 0, &gpio);
656 if (status < 0)
657 goto done;
658
Alexandre Courbot372e7222013-02-03 01:29:29 +0900659 desc = gpio_to_desc(gpio);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900660 /* reject invalid GPIOs */
661 if (!desc) {
662 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
663 return -EINVAL;
664 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900665
David Brownelld8f388d82008-07-25 01:46:07 -0700666 /* No extra locking here; FLAG_SYSFS just signifies that the
667 * request and export were done by on behalf of userspace, so
668 * they may be undone on its behalf too.
669 */
670
Alexandre Courbot372e7222013-02-03 01:29:29 +0900671 status = gpiod_request(desc, "sysfs");
Mathias Nymanad2fab32012-10-25 14:03:03 +0300672 if (status < 0) {
673 if (status == -EPROBE_DEFER)
674 status = -ENODEV;
David Brownelld8f388d82008-07-25 01:46:07 -0700675 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +0300676 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900677 status = gpiod_export(desc, true);
David Brownelld8f388d82008-07-25 01:46:07 -0700678 if (status < 0)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900679 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700680 else
Alexandre Courbot372e7222013-02-03 01:29:29 +0900681 set_bit(FLAG_SYSFS, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700682
683done:
684 if (status)
685 pr_debug("%s: status %d\n", __func__, status);
686 return status ? : len;
687}
688
Andi Kleen28812fe2010-01-05 12:48:07 +0100689static ssize_t unexport_store(struct class *class,
690 struct class_attribute *attr,
691 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700692{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900693 long gpio;
694 struct gpio_desc *desc;
695 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700696
697 status = strict_strtol(buf, 0, &gpio);
698 if (status < 0)
699 goto done;
700
Alexandre Courbot372e7222013-02-03 01:29:29 +0900701 desc = gpio_to_desc(gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700702 /* reject bogus commands (gpio_unexport ignores them) */
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900703 if (!desc) {
704 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
705 return -EINVAL;
706 }
707
708 status = -EINVAL;
David Brownelld8f388d82008-07-25 01:46:07 -0700709
710 /* No extra locking here; FLAG_SYSFS just signifies that the
711 * request and export were done by on behalf of userspace, so
712 * they may be undone on its behalf too.
713 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900714 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700715 status = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900716 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700717 }
718done:
719 if (status)
720 pr_debug("%s: status %d\n", __func__, status);
721 return status ? : len;
722}
723
724static struct class_attribute gpio_class_attrs[] = {
725 __ATTR(export, 0200, NULL, export_store),
726 __ATTR(unexport, 0200, NULL, unexport_store),
727 __ATTR_NULL,
728};
729
730static struct class gpio_class = {
731 .name = "gpio",
732 .owner = THIS_MODULE,
733
734 .class_attrs = gpio_class_attrs,
735};
736
737
738/**
739 * gpio_export - export a GPIO through sysfs
740 * @gpio: gpio to make available, already requested
741 * @direction_may_change: true if userspace may change gpio direction
742 * Context: arch_initcall or later
743 *
744 * When drivers want to make a GPIO accessible to userspace after they
745 * have requested it -- perhaps while debugging, or as part of their
746 * public interface -- they may use this routine. If the GPIO can
747 * change direction (some can't) and the caller allows it, userspace
748 * will see "direction" sysfs attribute which may be used to change
749 * the gpio's direction. A "value" attribute will always be provided.
750 *
751 * Returns zero on success, else an error.
752 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900753static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
David Brownelld8f388d82008-07-25 01:46:07 -0700754{
755 unsigned long flags;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100756 int status;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700757 const char *ioname = NULL;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100758 struct device *dev;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900759 int offset;
David Brownelld8f388d82008-07-25 01:46:07 -0700760
761 /* can't export until sysfs is available ... */
762 if (!gpio_class.p) {
763 pr_debug("%s: called too early!\n", __func__);
764 return -ENOENT;
765 }
766
Alexandre Courbot372e7222013-02-03 01:29:29 +0900767 if (!desc) {
768 pr_debug("%s: invalid gpio descriptor\n", __func__);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100769 return -EINVAL;
770 }
David Brownelld8f388d82008-07-25 01:46:07 -0700771
772 mutex_lock(&sysfs_lock);
773
774 spin_lock_irqsave(&gpio_lock, flags);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100775 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
776 test_bit(FLAG_EXPORT, &desc->flags)) {
777 spin_unlock_irqrestore(&gpio_lock, flags);
778 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +0900779 __func__, desc_to_gpio(desc),
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100780 test_bit(FLAG_REQUESTED, &desc->flags),
781 test_bit(FLAG_EXPORT, &desc->flags));
Dan Carpenter529f2ad2012-10-26 09:59:43 +0300782 status = -EPERM;
783 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700784 }
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100785
786 if (!desc->chip->direction_input || !desc->chip->direction_output)
787 direction_may_change = false;
David Brownelld8f388d82008-07-25 01:46:07 -0700788 spin_unlock_irqrestore(&gpio_lock, flags);
789
Alexandre Courbot372e7222013-02-03 01:29:29 +0900790 offset = gpio_chip_hwgpio(desc);
791 if (desc->chip->names && desc->chip->names[offset])
792 ioname = desc->chip->names[offset];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700793
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100794 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Alexandre Courbot372e7222013-02-03 01:29:29 +0900795 desc, ioname ? ioname : "gpio%u",
796 desc_to_gpio(desc));
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100797 if (IS_ERR(dev)) {
798 status = PTR_ERR(dev);
799 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700800 }
801
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100802 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
David Brownelld8f388d82008-07-25 01:46:07 -0700803 if (status)
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100804 goto fail_unregister_device;
David Brownelld8f388d82008-07-25 01:46:07 -0700805
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100806 if (direction_may_change) {
807 status = device_create_file(dev, &dev_attr_direction);
808 if (status)
Johan Hovolda9808402015-01-13 13:00:05 +0100809 goto fail_remove_attr_group;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100810 }
811
Alexandre Courbot372e7222013-02-03 01:29:29 +0900812 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100813 !test_bit(FLAG_IS_OUT, &desc->flags))) {
814 status = device_create_file(dev, &dev_attr_edge);
815 if (status)
Johan Hovolda9808402015-01-13 13:00:05 +0100816 goto fail_remove_attr_direction;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100817 }
818
819 set_bit(FLAG_EXPORT, &desc->flags);
820 mutex_unlock(&sysfs_lock);
821 return 0;
822
Johan Hovolda9808402015-01-13 13:00:05 +0100823fail_remove_attr_direction:
824 device_remove_file(dev, &dev_attr_direction);
825fail_remove_attr_group:
826 sysfs_remove_group(&dev->kobj, &gpio_attr_group);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100827fail_unregister_device:
828 device_unregister(dev);
829fail_unlock:
830 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900831 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
832 status);
David Brownelld8f388d82008-07-25 01:46:07 -0700833 return status;
834}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900835
836int gpio_export(unsigned gpio, bool direction_may_change)
837{
838 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
839}
David Brownelld8f388d82008-07-25 01:46:07 -0700840EXPORT_SYMBOL_GPL(gpio_export);
841
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100842static int match_export(struct device *dev, const void *data)
David Brownelld8f388d82008-07-25 01:46:07 -0700843{
844 return dev_get_drvdata(dev) == data;
845}
846
847/**
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700848 * gpio_export_link - create a sysfs link to an exported GPIO node
849 * @dev: device under which to create symlink
850 * @name: name of the symlink
851 * @gpio: gpio to create symlink to, already exported
852 *
853 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
854 * node. Caller is responsible for unlinking.
855 *
856 * Returns zero on success, else an error.
857 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900858static int gpiod_export_link(struct device *dev, const char *name,
859 struct gpio_desc *desc)
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700860{
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700861 int status = -EINVAL;
862
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900863 if (!desc) {
864 pr_warn("%s: invalid GPIO\n", __func__);
865 return -EINVAL;
866 }
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700867
868 mutex_lock(&sysfs_lock);
869
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700870 if (test_bit(FLAG_EXPORT, &desc->flags)) {
871 struct device *tdev;
872
873 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
874 if (tdev != NULL) {
875 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
876 name);
Johan Hovoldd0d1f542015-01-26 12:02:45 +0100877 put_device(tdev);
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700878 } else {
879 status = -ENODEV;
880 }
881 }
882
883 mutex_unlock(&sysfs_lock);
884
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700885 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900886 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
887 status);
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700888
889 return status;
890}
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700891
Alexandre Courbot372e7222013-02-03 01:29:29 +0900892int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
893{
894 return gpiod_export_link(dev, name, gpio_to_desc(gpio));
895}
896EXPORT_SYMBOL_GPL(gpio_export_link);
Jani Nikula07697462009-12-15 16:46:20 -0800897
898/**
899 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
900 * @gpio: gpio to change
901 * @value: non-zero to use active low, i.e. inverted values
902 *
903 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
904 * The GPIO does not have to be exported yet. If poll(2) support has
905 * been enabled for either rising or falling edge, it will be
906 * reconfigured to follow the new polarity.
907 *
908 * Returns zero on success, else an error.
909 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900910static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
Jani Nikula07697462009-12-15 16:46:20 -0800911{
Jani Nikula07697462009-12-15 16:46:20 -0800912 struct device *dev = NULL;
913 int status = -EINVAL;
914
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900915 if (!desc) {
916 pr_warn("%s: invalid GPIO\n", __func__);
917 return -EINVAL;
918 }
Jani Nikula07697462009-12-15 16:46:20 -0800919
920 mutex_lock(&sysfs_lock);
921
Jani Nikula07697462009-12-15 16:46:20 -0800922 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800923 dev = class_find_device(&gpio_class, NULL, desc, match_export);
924 if (dev == NULL) {
925 status = -ENODEV;
926 goto unlock;
927 }
928 }
929
930 status = sysfs_set_active_low(desc, dev, value);
Johan Hovold4cd925d2015-01-26 12:02:46 +0100931 put_device(dev);
Jani Nikula07697462009-12-15 16:46:20 -0800932unlock:
933 mutex_unlock(&sysfs_lock);
934
Jani Nikula07697462009-12-15 16:46:20 -0800935 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900936 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
937 status);
Jani Nikula07697462009-12-15 16:46:20 -0800938
939 return status;
940}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900941
942int gpio_sysfs_set_active_low(unsigned gpio, int value)
943{
944 return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value);
945}
Jani Nikula07697462009-12-15 16:46:20 -0800946EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
947
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700948/**
David Brownelld8f388d82008-07-25 01:46:07 -0700949 * gpio_unexport - reverse effect of gpio_export()
950 * @gpio: gpio to make unavailable
951 *
952 * This is implicit on gpio_free().
953 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900954static void gpiod_unexport(struct gpio_desc *desc)
David Brownelld8f388d82008-07-25 01:46:07 -0700955{
Jon Povey6a99ad42010-07-27 13:18:06 -0700956 int status = 0;
Ming Lei864533c2012-02-13 22:53:20 +0800957 struct device *dev = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -0700958
Alexandre Courbot372e7222013-02-03 01:29:29 +0900959 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900960 pr_warn("%s: invalid GPIO\n", __func__);
961 return;
Jon Povey6a99ad42010-07-27 13:18:06 -0700962 }
David Brownelld8f388d82008-07-25 01:46:07 -0700963
964 mutex_lock(&sysfs_lock);
965
David Brownelld8f388d82008-07-25 01:46:07 -0700966 if (test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700967
968 dev = class_find_device(&gpio_class, NULL, desc, match_export);
969 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700970 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700971 clear_bit(FLAG_EXPORT, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700972 } else
973 status = -ENODEV;
974 }
975
976 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900977
Ming Lei864533c2012-02-13 22:53:20 +0800978 if (dev) {
Johan Hovolda9808402015-01-13 13:00:05 +0100979 device_remove_file(dev, &dev_attr_edge);
980 device_remove_file(dev, &dev_attr_direction);
981 sysfs_remove_group(&dev->kobj, &gpio_attr_group);
Ming Lei864533c2012-02-13 22:53:20 +0800982 device_unregister(dev);
983 put_device(dev);
984 }
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900985
David Brownelld8f388d82008-07-25 01:46:07 -0700986 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900987 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
988 status);
989}
990
991void gpio_unexport(unsigned gpio)
992{
993 gpiod_unexport(gpio_to_desc(gpio));
David Brownelld8f388d82008-07-25 01:46:07 -0700994}
995EXPORT_SYMBOL_GPL(gpio_unexport);
996
997static int gpiochip_export(struct gpio_chip *chip)
998{
999 int status;
1000 struct device *dev;
1001
1002 /* Many systems register gpio chips for SOC support very early,
1003 * before driver model support is available. In those cases we
1004 * export this later, in gpiolib_sysfs_init() ... here we just
1005 * verify that _some_ field of gpio_class got initialized.
1006 */
1007 if (!gpio_class.p)
1008 return 0;
1009
1010 /* use chip->base for the ID; it's already known to be unique */
1011 mutex_lock(&sysfs_lock);
1012 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
1013 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001014 if (!IS_ERR(dev)) {
David Brownelld8f388d82008-07-25 01:46:07 -07001015 status = sysfs_create_group(&dev->kobj,
1016 &gpiochip_attr_group);
1017 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001018 status = PTR_ERR(dev);
David Brownelld8f388d82008-07-25 01:46:07 -07001019 chip->exported = (status == 0);
1020 mutex_unlock(&sysfs_lock);
1021
1022 if (status) {
1023 unsigned long flags;
1024 unsigned gpio;
1025
1026 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001027 gpio = 0;
1028 while (gpio < chip->ngpio)
1029 chip->desc[gpio++].chip = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -07001030 spin_unlock_irqrestore(&gpio_lock, flags);
1031
1032 pr_debug("%s: chip %s status %d\n", __func__,
1033 chip->label, status);
1034 }
1035
1036 return status;
1037}
1038
1039static void gpiochip_unexport(struct gpio_chip *chip)
1040{
1041 int status;
1042 struct device *dev;
1043
1044 mutex_lock(&sysfs_lock);
1045 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1046 if (dev) {
Johan Hovoldaa35a482015-01-13 13:00:04 +01001047 sysfs_remove_group(&dev->kobj, &gpiochip_attr_group);
David Brownelld8f388d82008-07-25 01:46:07 -07001048 put_device(dev);
1049 device_unregister(dev);
1050 chip->exported = 0;
1051 status = 0;
1052 } else
1053 status = -ENODEV;
1054 mutex_unlock(&sysfs_lock);
1055
1056 if (status)
1057 pr_debug("%s: chip %s status %d\n", __func__,
1058 chip->label, status);
1059}
1060
1061static int __init gpiolib_sysfs_init(void)
1062{
1063 int status;
1064 unsigned long flags;
Alexandre Courbot65493e32013-02-03 01:29:25 +09001065 struct gpio_chip *chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001066
1067 status = class_register(&gpio_class);
1068 if (status < 0)
1069 return status;
1070
1071 /* Scan and register the gpio_chips which registered very
1072 * early (e.g. before the class_register above was called).
1073 *
1074 * We run before arch_initcall() so chip->dev nodes can have
1075 * registered, and so arch_initcall() can always gpio_export().
1076 */
1077 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot65493e32013-02-03 01:29:25 +09001078 list_for_each_entry(chip, &gpio_chips, list) {
David Brownelld8f388d82008-07-25 01:46:07 -07001079 if (!chip || chip->exported)
1080 continue;
1081
1082 spin_unlock_irqrestore(&gpio_lock, flags);
1083 status = gpiochip_export(chip);
1084 spin_lock_irqsave(&gpio_lock, flags);
1085 }
1086 spin_unlock_irqrestore(&gpio_lock, flags);
1087
1088
1089 return status;
1090}
1091postcore_initcall(gpiolib_sysfs_init);
1092
1093#else
1094static inline int gpiochip_export(struct gpio_chip *chip)
1095{
1096 return 0;
1097}
1098
1099static inline void gpiochip_unexport(struct gpio_chip *chip)
1100{
1101}
1102
Alexandre Courbot372e7222013-02-03 01:29:29 +09001103static inline int gpiod_export(struct gpio_desc *desc,
1104 bool direction_may_change)
1105{
1106 return -ENOSYS;
1107}
1108
1109static inline int gpiod_export_link(struct device *dev, const char *name,
1110 struct gpio_desc *desc)
1111{
1112 return -ENOSYS;
1113}
1114
1115static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
1116{
1117 return -ENOSYS;
1118}
1119
1120static inline void gpiod_unexport(struct gpio_desc *desc)
1121{
1122}
1123
David Brownelld8f388d82008-07-25 01:46:07 -07001124#endif /* CONFIG_GPIO_SYSFS */
1125
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001126/*
1127 * Add a new chip to the global chips list, keeping the list of chips sorted
1128 * by base order.
1129 *
1130 * Return -EBUSY if the new chip overlaps with some other chip's integer
1131 * space.
1132 */
1133static int gpiochip_add_to_list(struct gpio_chip *chip)
1134{
1135 struct list_head *pos = &gpio_chips;
1136 struct gpio_chip *_chip;
1137 int err = 0;
1138
1139 /* find where to insert our chip */
1140 list_for_each(pos, &gpio_chips) {
1141 _chip = list_entry(pos, struct gpio_chip, list);
1142 /* shall we insert before _chip? */
1143 if (_chip->base >= chip->base + chip->ngpio)
1144 break;
1145 }
1146
1147 /* are we stepping on the chip right before? */
1148 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1149 _chip = list_entry(pos->prev, struct gpio_chip, list);
1150 if (_chip->base + _chip->ngpio > chip->base) {
1151 dev_err(chip->dev,
1152 "GPIO integer space overlap, cannot add chip\n");
1153 err = -EBUSY;
1154 }
1155 }
1156
1157 if (!err)
1158 list_add_tail(&chip->list, pos);
1159
1160 return err;
1161}
1162
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001163/**
David Brownelld2876d02008-02-04 22:28:20 -08001164 * gpiochip_add() - register a gpio_chip
1165 * @chip: the chip to register, with chip->base initialized
1166 * Context: potentially before irqs or kmalloc will work
1167 *
1168 * Returns a negative errno if the chip can't be registered, such as
1169 * because the chip->base is invalid or already associated with a
1170 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001171 *
David Brownelld8f388d82008-07-25 01:46:07 -07001172 * When gpiochip_add() is called very early during boot, so that GPIOs
1173 * can be freely used, the chip->dev device must be registered before
1174 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1175 * for GPIOs will fail rudely.
1176 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001177 * If chip->base is negative, this requests dynamic assignment of
1178 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001179 */
1180int gpiochip_add(struct gpio_chip *chip)
1181{
1182 unsigned long flags;
1183 int status = 0;
1184 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001185 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001186
Trent Piephobff5fda2008-05-23 13:04:44 -07001187 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001188 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001189 status = -EINVAL;
1190 goto fail;
1191 }
1192
1193 spin_lock_irqsave(&gpio_lock, flags);
1194
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001195 if (base < 0) {
1196 base = gpiochip_find_base(chip->ngpio);
1197 if (base < 0) {
1198 status = base;
David Brownelld8f388d82008-07-25 01:46:07 -07001199 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001200 }
1201 chip->base = base;
1202 }
1203
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001204 status = gpiochip_add_to_list(chip);
1205
David Brownelld2876d02008-02-04 22:28:20 -08001206 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001207 chip->desc = &gpio_desc[chip->base];
1208
1209 for (id = 0; id < chip->ngpio; id++) {
1210 struct gpio_desc *desc = &chip->desc[id];
1211 desc->chip = chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001212
1213 /* REVISIT: most hardware initializes GPIOs as
1214 * inputs (often with pullups enabled) so power
1215 * usage is minimized. Linux code should set the
1216 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +03001217 * and in case chip->get_direction is not set,
David Brownelld8f388d82008-07-25 01:46:07 -07001218 * we may expose the wrong direction in sysfs.
1219 */
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001220 desc->flags = !chip->direction_input
David Brownelld8f388d82008-07-25 01:46:07 -07001221 ? (1 << FLAG_IS_OUT)
1222 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001223 }
1224 }
1225
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301226#ifdef CONFIG_PINCTRL
1227 INIT_LIST_HEAD(&chip->pin_ranges);
1228#endif
1229
Anton Vorontsov391c9702010-06-08 07:48:17 -06001230 of_gpiochip_add(chip);
1231
David Brownelld8f388d82008-07-25 01:46:07 -07001232unlock:
David Brownelld2876d02008-02-04 22:28:20 -08001233 spin_unlock_irqrestore(&gpio_lock, flags);
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001234
1235 if (status)
1236 goto fail;
1237
1238 status = gpiochip_export(chip);
1239 if (status)
1240 goto fail;
1241
H Hartley Sweetenee1c1e72012-05-11 16:58:33 -07001242 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
Grant Likely64842aa2011-11-06 11:36:18 -07001243 chip->base, chip->base + chip->ngpio - 1,
1244 chip->label ? : "generic");
1245
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001246 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001247fail:
1248 /* failures here can mean systems won't boot... */
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001249 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1250 chip->base, chip->base + chip->ngpio - 1,
1251 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001252 return status;
1253}
1254EXPORT_SYMBOL_GPL(gpiochip_add);
1255
1256/**
1257 * gpiochip_remove() - unregister a gpio_chip
1258 * @chip: the chip to unregister
1259 *
1260 * A gpio_chip with any GPIOs still requested may not be removed.
1261 */
1262int gpiochip_remove(struct gpio_chip *chip)
1263{
1264 unsigned long flags;
1265 int status = 0;
1266 unsigned id;
1267
1268 spin_lock_irqsave(&gpio_lock, flags);
1269
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001270 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001271 of_gpiochip_remove(chip);
1272
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001273 for (id = 0; id < chip->ngpio; id++) {
1274 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
David Brownelld2876d02008-02-04 22:28:20 -08001275 status = -EBUSY;
1276 break;
1277 }
1278 }
1279 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001280 for (id = 0; id < chip->ngpio; id++)
1281 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001282
1283 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -08001284 }
1285
1286 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d82008-07-25 01:46:07 -07001287
1288 if (status == 0)
1289 gpiochip_unexport(chip);
1290
David Brownelld2876d02008-02-04 22:28:20 -08001291 return status;
1292}
1293EXPORT_SYMBOL_GPL(gpiochip_remove);
1294
Grant Likely594fa262010-06-08 07:48:16 -06001295/**
1296 * gpiochip_find() - iterator for locating a specific gpio_chip
1297 * @data: data to pass to match function
1298 * @callback: Callback function to check gpio_chip
1299 *
1300 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1301 * determined by a user supplied @match callback. The callback should return
1302 * 0 if the device doesn't match and non-zero if it does. If the callback is
1303 * non-zero, this function will return to the caller and not iterate over any
1304 * more gpio_chips.
1305 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001306struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001307 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001308 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001309{
Alexandre Courbot125eef92013-02-03 01:29:26 +09001310 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001311 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001312
1313 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +09001314 list_for_each_entry(chip, &gpio_chips, list)
1315 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001316 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001317
1318 /* No match? */
1319 if (&chip->list == &gpio_chips)
1320 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001321 spin_unlock_irqrestore(&gpio_lock, flags);
1322
1323 return chip;
1324}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001325EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001326
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301327#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001328
Linus Walleij3f0f8672012-11-20 12:40:15 +01001329/**
1330 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1331 * @chip: the gpiochip to add the range for
1332 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001333 * @gpio_offset: the start offset in the current gpio_chip number space
1334 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001335 * @npins: the number of pins from the offset of each pin space (GPIO and
1336 * pin controller) to accumulate in this range
1337 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001338int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001339 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001340 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301341{
1342 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001343 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301344
Linus Walleij3f0f8672012-11-20 12:40:15 +01001345 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301346 if (!pin_range) {
1347 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1348 chip->label);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001349 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301350 }
1351
Linus Walleij3f0f8672012-11-20 12:40:15 +01001352 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001353 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001354 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301355 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001356 pin_range->range.base = chip->base + gpio_offset;
1357 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301358 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001359 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301360 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001361 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001362 ret = PTR_ERR(pin_range->pctldev);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001363 pr_err("%s: GPIO chip: could not create pin range\n",
1364 chip->label);
1365 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001366 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001367 }
Linus Walleij316511c2012-11-21 08:48:09 +01001368 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1369 chip->label, gpio_offset, gpio_offset + npins - 1,
1370 pinctl_name,
1371 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301372
1373 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001374
1375 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301376}
Linus Walleij165adc92012-11-06 14:49:39 +01001377EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301378
Linus Walleij3f0f8672012-11-20 12:40:15 +01001379/**
1380 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1381 * @chip: the chip to remove all the mappings for
1382 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301383void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1384{
1385 struct gpio_pin_range *pin_range, *tmp;
1386
1387 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1388 list_del(&pin_range->node);
1389 pinctrl_remove_gpio_range(pin_range->pctldev,
1390 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001391 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301392 }
1393}
Linus Walleij165adc92012-11-06 14:49:39 +01001394EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1395
1396#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301397
David Brownelld2876d02008-02-04 22:28:20 -08001398/* These "optional" allocation calls help prevent drivers from stomping
1399 * on each other, and help provide better diagnostics in debugfs.
1400 * They're called even less than the "set direction" calls.
1401 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001402static int gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001403{
David Brownell35e8bb52008-10-15 22:03:16 -07001404 struct gpio_chip *chip;
Mark Browne9354572012-07-09 12:22:56 +01001405 int status = -EPROBE_DEFER;
David Brownelld2876d02008-02-04 22:28:20 -08001406 unsigned long flags;
1407
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001408 if (!desc) {
1409 pr_warn("%s: invalid GPIO\n", __func__);
1410 return -EINVAL;
1411 }
1412
David Brownelld2876d02008-02-04 22:28:20 -08001413 spin_lock_irqsave(&gpio_lock, flags);
1414
David Brownell35e8bb52008-10-15 22:03:16 -07001415 chip = desc->chip;
1416 if (chip == NULL)
David Brownelld2876d02008-02-04 22:28:20 -08001417 goto done;
1418
David Brownell35e8bb52008-10-15 22:03:16 -07001419 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001420 goto done;
1421
David Brownelld2876d02008-02-04 22:28:20 -08001422 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001423 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001424 */
1425
1426 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1427 desc_set_label(desc, label ? : "?");
1428 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001429 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001430 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001431 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001432 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001433 }
1434
1435 if (chip->request) {
1436 /* chip->request may sleep */
1437 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001438 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001439 spin_lock_irqsave(&gpio_lock, flags);
1440
1441 if (status < 0) {
1442 desc_set_label(desc, NULL);
1443 module_put(chip->owner);
1444 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001445 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001446 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001447 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001448 if (chip->get_direction) {
1449 /* chip->get_direction may sleep */
1450 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001451 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001452 spin_lock_irqsave(&gpio_lock, flags);
1453 }
David Brownelld2876d02008-02-04 22:28:20 -08001454done:
1455 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001456 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001457 desc_to_gpio(desc), label ? : "?", status);
David Brownelld2876d02008-02-04 22:28:20 -08001458 spin_unlock_irqrestore(&gpio_lock, flags);
1459 return status;
1460}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001461
1462int gpio_request(unsigned gpio, const char *label)
1463{
1464 return gpiod_request(gpio_to_desc(gpio), label);
1465}
David Brownelld2876d02008-02-04 22:28:20 -08001466EXPORT_SYMBOL_GPL(gpio_request);
1467
Alexandre Courbot372e7222013-02-03 01:29:29 +09001468static void gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001469{
1470 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001471 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001472
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001473 might_sleep();
1474
Alexandre Courbot372e7222013-02-03 01:29:29 +09001475 if (!desc) {
David Brownelld2876d02008-02-04 22:28:20 -08001476 WARN_ON(extra_checks);
1477 return;
1478 }
1479
Alexandre Courbot372e7222013-02-03 01:29:29 +09001480 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07001481
David Brownelld2876d02008-02-04 22:28:20 -08001482 spin_lock_irqsave(&gpio_lock, flags);
1483
David Brownell35e8bb52008-10-15 22:03:16 -07001484 chip = desc->chip;
1485 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1486 if (chip->free) {
1487 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001488 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001489 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001490 spin_lock_irqsave(&gpio_lock, flags);
1491 }
David Brownelld2876d02008-02-04 22:28:20 -08001492 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001493 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001494 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001495 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301496 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301497 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001498 } else
David Brownelld2876d02008-02-04 22:28:20 -08001499 WARN_ON(extra_checks);
1500
1501 spin_unlock_irqrestore(&gpio_lock, flags);
1502}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001503
1504void gpio_free(unsigned gpio)
1505{
1506 gpiod_free(gpio_to_desc(gpio));
1507}
David Brownelld2876d02008-02-04 22:28:20 -08001508EXPORT_SYMBOL_GPL(gpio_free);
1509
Eric Miao3e45f1d2010-03-05 13:44:35 -08001510/**
1511 * gpio_request_one - request a single GPIO with initial configuration
1512 * @gpio: the GPIO number
1513 * @flags: GPIO configuration as specified by GPIOF_*
1514 * @label: a literal description string of this GPIO
1515 */
1516int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1517{
Alexandre Courbot372e7222013-02-03 01:29:29 +09001518 struct gpio_desc *desc;
Eric Miao3e45f1d2010-03-05 13:44:35 -08001519 int err;
1520
Alexandre Courbot372e7222013-02-03 01:29:29 +09001521 desc = gpio_to_desc(gpio);
1522
1523 err = gpiod_request(desc, label);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001524 if (err)
1525 return err;
1526
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301527 if (flags & GPIOF_OPEN_DRAIN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001528 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301529
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301530 if (flags & GPIOF_OPEN_SOURCE)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001531 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301532
Eric Miao3e45f1d2010-03-05 13:44:35 -08001533 if (flags & GPIOF_DIR_IN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001534 err = gpiod_direction_input(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001535 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001536 err = gpiod_direction_output(desc,
Eric Miao3e45f1d2010-03-05 13:44:35 -08001537 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1538
Aaro Koskinene2548112010-12-21 17:24:22 -08001539 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001540 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -08001541
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001542 if (flags & GPIOF_EXPORT) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001543 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001544 if (err)
1545 goto free_gpio;
1546 }
1547
1548 return 0;
1549
1550 free_gpio:
Alexandre Courbot372e7222013-02-03 01:29:29 +09001551 gpiod_free(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001552 return err;
1553}
1554EXPORT_SYMBOL_GPL(gpio_request_one);
1555
1556/**
1557 * gpio_request_array - request multiple GPIOs in a single call
1558 * @array: array of the 'struct gpio'
1559 * @num: how many GPIOs in the array
1560 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001561int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001562{
1563 int i, err;
1564
1565 for (i = 0; i < num; i++, array++) {
1566 err = gpio_request_one(array->gpio, array->flags, array->label);
1567 if (err)
1568 goto err_free;
1569 }
1570 return 0;
1571
1572err_free:
1573 while (i--)
1574 gpio_free((--array)->gpio);
1575 return err;
1576}
1577EXPORT_SYMBOL_GPL(gpio_request_array);
1578
1579/**
1580 * gpio_free_array - release multiple GPIOs in a single call
1581 * @array: array of the 'struct gpio'
1582 * @num: how many GPIOs in the array
1583 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001584void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001585{
1586 while (num--)
1587 gpio_free((array++)->gpio);
1588}
1589EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001590
1591/**
1592 * gpiochip_is_requested - return string iff signal was requested
1593 * @chip: controller managing the signal
1594 * @offset: of signal within controller's 0..(ngpio - 1) range
1595 *
1596 * Returns NULL if the GPIO is not currently requested, else a string.
1597 * If debugfs support is enabled, the string returned is the label passed
1598 * to gpio_request(); otherwise it is a meaningless constant.
1599 *
1600 * This function is for use by GPIO controller drivers. The label can
1601 * help with diagnostics, and knowing that the signal is used as a GPIO
1602 * can help avoid accidentally multiplexing it to another controller.
1603 */
1604const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1605{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001606 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001607
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001608 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -08001609 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001610
1611 desc = &chip->desc[offset];
1612
Alexandre Courbot372e7222013-02-03 01:29:29 +09001613 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001614 return NULL;
1615#ifdef CONFIG_DEBUG_FS
Alexandre Courbot372e7222013-02-03 01:29:29 +09001616 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001617#else
1618 return "?";
1619#endif
1620}
1621EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1622
1623
1624/* Drivers MUST set GPIO direction before making get/set calls. In
1625 * some cases this is done in early boot, before IRQs are enabled.
1626 *
1627 * As a rule these aren't called more than once (except for drivers
1628 * using the open-drain emulation idiom) so these are natural places
1629 * to accumulate extra debugging checks. Note that we can't (yet)
1630 * rely on gpio_request() having been called beforehand.
1631 */
1632
Alexandre Courbot372e7222013-02-03 01:29:29 +09001633static int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001634{
1635 unsigned long flags;
1636 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001637 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001638 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001639
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001640 if (!desc) {
1641 pr_warn("%s: invalid GPIO\n", __func__);
1642 return -EINVAL;
1643 }
1644
David Brownelld2876d02008-02-04 22:28:20 -08001645 spin_lock_irqsave(&gpio_lock, flags);
1646
David Brownelld2876d02008-02-04 22:28:20 -08001647 chip = desc->chip;
1648 if (!chip || !chip->get || !chip->direction_input)
1649 goto fail;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001650 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001651 if (status < 0)
1652 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001653
1654 /* now we know the gpio is valid and chip won't vanish */
1655
1656 spin_unlock_irqrestore(&gpio_lock, flags);
1657
David Brownell9c4ba942010-08-10 18:02:24 -07001658 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001659
Alexandre Courbot372e7222013-02-03 01:29:29 +09001660 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001661 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001662 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001663 if (status < 0) {
1664 pr_debug("GPIO-%d: chip request fail, %d\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +09001665 desc_to_gpio(desc), status);
David Brownell35e8bb52008-10-15 22:03:16 -07001666 /* and it's not available to anyone else ...
1667 * gpio_request() is the fully clean solution.
1668 */
1669 goto lose;
1670 }
1671 }
1672
Alexandre Courbot372e7222013-02-03 01:29:29 +09001673 status = chip->direction_input(chip, offset);
David Brownelld2876d02008-02-04 22:28:20 -08001674 if (status == 0)
1675 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001676
Alexandre Courbot372e7222013-02-03 01:29:29 +09001677 trace_gpio_direction(desc_to_gpio(desc), 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001678lose:
David Brownelld2876d02008-02-04 22:28:20 -08001679 return status;
1680fail:
1681 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001682 if (status)
1683 pr_debug("%s: gpio-%d status %d\n", __func__,
1684 desc_to_gpio(desc), status);
David Brownelld2876d02008-02-04 22:28:20 -08001685 return status;
1686}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001687
1688int gpio_direction_input(unsigned gpio)
1689{
1690 return gpiod_direction_input(gpio_to_desc(gpio));
1691}
David Brownelld2876d02008-02-04 22:28:20 -08001692EXPORT_SYMBOL_GPL(gpio_direction_input);
1693
Alexandre Courbot372e7222013-02-03 01:29:29 +09001694static int gpiod_direction_output(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001695{
1696 unsigned long flags;
1697 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001698 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001699 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001700
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001701 if (!desc) {
1702 pr_warn("%s: invalid GPIO\n", __func__);
1703 return -EINVAL;
1704 }
1705
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301706 /* Open drain pin should not be driven to 1 */
1707 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001708 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301709
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301710 /* Open source pin should not be driven to 0 */
1711 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001712 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301713
David Brownelld2876d02008-02-04 22:28:20 -08001714 spin_lock_irqsave(&gpio_lock, flags);
1715
David Brownelld2876d02008-02-04 22:28:20 -08001716 chip = desc->chip;
1717 if (!chip || !chip->set || !chip->direction_output)
1718 goto fail;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001719 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001720 if (status < 0)
1721 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001722
1723 /* now we know the gpio is valid and chip won't vanish */
1724
1725 spin_unlock_irqrestore(&gpio_lock, flags);
1726
David Brownell9c4ba942010-08-10 18:02:24 -07001727 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001728
Alexandre Courbot372e7222013-02-03 01:29:29 +09001729 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001730 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001731 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001732 if (status < 0) {
1733 pr_debug("GPIO-%d: chip request fail, %d\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +09001734 desc_to_gpio(desc), status);
David Brownell35e8bb52008-10-15 22:03:16 -07001735 /* and it's not available to anyone else ...
1736 * gpio_request() is the fully clean solution.
1737 */
1738 goto lose;
1739 }
1740 }
1741
Alexandre Courbot372e7222013-02-03 01:29:29 +09001742 status = chip->direction_output(chip, offset, value);
David Brownelld2876d02008-02-04 22:28:20 -08001743 if (status == 0)
1744 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001745 trace_gpio_value(desc_to_gpio(desc), 0, value);
1746 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001747lose:
David Brownelld2876d02008-02-04 22:28:20 -08001748 return status;
1749fail:
1750 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001751 if (status)
1752 pr_debug("%s: gpio-%d status %d\n", __func__,
1753 desc_to_gpio(desc), status);
David Brownelld2876d02008-02-04 22:28:20 -08001754 return status;
1755}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001756
1757int gpio_direction_output(unsigned gpio, int value)
1758{
1759 return gpiod_direction_output(gpio_to_desc(gpio), value);
1760}
David Brownelld2876d02008-02-04 22:28:20 -08001761EXPORT_SYMBOL_GPL(gpio_direction_output);
1762
Felipe Balbic4b5be92010-05-26 14:42:23 -07001763/**
1764 * gpio_set_debounce - sets @debounce time for a @gpio
1765 * @gpio: the gpio to set debounce time
1766 * @debounce: debounce time is microseconds
1767 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001768static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001769{
1770 unsigned long flags;
1771 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001772 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001773 int offset;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001774
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001775 if (!desc) {
1776 pr_warn("%s: invalid GPIO\n", __func__);
1777 return -EINVAL;
1778 }
1779
Felipe Balbic4b5be92010-05-26 14:42:23 -07001780 spin_lock_irqsave(&gpio_lock, flags);
1781
Felipe Balbic4b5be92010-05-26 14:42:23 -07001782 chip = desc->chip;
1783 if (!chip || !chip->set || !chip->set_debounce)
1784 goto fail;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001785
1786 status = gpio_ensure_requested(desc);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001787 if (status < 0)
1788 goto fail;
1789
1790 /* now we know the gpio is valid and chip won't vanish */
1791
1792 spin_unlock_irqrestore(&gpio_lock, flags);
1793
David Brownell9c4ba942010-08-10 18:02:24 -07001794 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001795
Alexandre Courbot372e7222013-02-03 01:29:29 +09001796 offset = gpio_chip_hwgpio(desc);
1797 return chip->set_debounce(chip, offset, debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001798
1799fail:
1800 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001801 if (status)
1802 pr_debug("%s: gpio-%d status %d\n", __func__,
1803 desc_to_gpio(desc), status);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001804
1805 return status;
1806}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001807
1808int gpio_set_debounce(unsigned gpio, unsigned debounce)
1809{
1810 return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
1811}
Felipe Balbic4b5be92010-05-26 14:42:23 -07001812EXPORT_SYMBOL_GPL(gpio_set_debounce);
David Brownelld2876d02008-02-04 22:28:20 -08001813
1814/* I/O calls are only valid after configuration completed; the relevant
1815 * "is this a valid GPIO" error checks should already have been done.
1816 *
1817 * "Get" operations are often inlinable as reading a pin value register,
1818 * and masking the relevant bit in that register.
1819 *
1820 * When "set" operations are inlinable, they involve writing that mask to
1821 * one register to set a low value, or a different register to set it high.
1822 * Otherwise locking is needed, so there may be little value to inlining.
1823 *
1824 *------------------------------------------------------------------------
1825 *
1826 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1827 * have requested the GPIO. That can include implicit requesting by
1828 * a direction setting call. Marking a gpio as requested locks its chip
1829 * in memory, guaranteeing that these table lookups need no more locking
1830 * and that gpiochip_remove() will fail.
1831 *
1832 * REVISIT when debugging, consider adding some instrumentation to ensure
1833 * that the GPIO was actually requested.
1834 */
1835
1836/**
1837 * __gpio_get_value() - return a gpio's value
1838 * @gpio: gpio whose value will be returned
1839 * Context: any
1840 *
1841 * This is used directly or indirectly to implement gpio_get_value().
1842 * It returns the zero or nonzero value provided by the associated
1843 * gpio_chip.get() method; or zero if no such method is provided.
1844 */
Alexandre Courbotdef63432013-02-15 14:46:15 +09001845static int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001846{
1847 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001848 int value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001849 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001850
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001851 if (!desc)
1852 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001853 chip = desc->chip;
1854 offset = gpio_chip_hwgpio(desc);
Mark Browne4e449e2012-02-17 10:46:00 -08001855 /* Should be using gpio_get_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001856 WARN_ON(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001857 value = chip->get ? chip->get(chip, offset) : 0;
1858 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001859 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001860}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001861
1862int __gpio_get_value(unsigned gpio)
1863{
1864 return gpiod_get_value(gpio_to_desc(gpio));
1865}
David Brownelld2876d02008-02-04 22:28:20 -08001866EXPORT_SYMBOL_GPL(__gpio_get_value);
1867
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301868/*
1869 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1870 * @gpio: Gpio whose state need to be set.
1871 * @chip: Gpio chip.
1872 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1873 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001874static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301875{
1876 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001877 struct gpio_chip *chip = desc->chip;
1878 int offset = gpio_chip_hwgpio(desc);
1879
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301880 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001881 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301882 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001883 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301884 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001885 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301886 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001887 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301888 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001889 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301890 if (err < 0)
1891 pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +09001892 __func__, desc_to_gpio(desc), err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301893}
1894
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301895/*
1896 * _gpio_set_open_source() - Set the open source gpio's value.
1897 * @gpio: Gpio whose state need to be set.
1898 * @chip: Gpio chip.
1899 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1900 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001901static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301902{
1903 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001904 struct gpio_chip *chip = desc->chip;
1905 int offset = gpio_chip_hwgpio(desc);
1906
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301907 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001908 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301909 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001910 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301911 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001912 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301913 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001914 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301915 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001916 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301917 if (err < 0)
1918 pr_err("%s: Error in set_value for open source gpio%d err %d\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +09001919 __func__, desc_to_gpio(desc), err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301920}
1921
David Brownelld2876d02008-02-04 22:28:20 -08001922/**
1923 * __gpio_set_value() - assign a gpio's value
1924 * @gpio: gpio whose value will be assigned
1925 * @value: value to assign
1926 * Context: any
1927 *
1928 * This is used directly or indirectly to implement gpio_set_value().
1929 * It invokes the associated gpio_chip.set() method.
1930 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001931static void gpiod_set_value(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001932{
1933 struct gpio_chip *chip;
1934
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001935 if (!desc)
1936 return;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001937 chip = desc->chip;
Mark Browne4e449e2012-02-17 10:46:00 -08001938 /* Should be using gpio_set_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001939 WARN_ON(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001940 trace_gpio_value(desc_to_gpio(desc), 0, value);
1941 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1942 _gpio_set_open_drain_value(desc, value);
1943 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1944 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301945 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001946 chip->set(chip, gpio_chip_hwgpio(desc), value);
1947}
1948
1949void __gpio_set_value(unsigned gpio, int value)
1950{
1951 return gpiod_set_value(gpio_to_desc(gpio), value);
David Brownelld2876d02008-02-04 22:28:20 -08001952}
1953EXPORT_SYMBOL_GPL(__gpio_set_value);
1954
1955/**
1956 * __gpio_cansleep() - report whether gpio value access will sleep
1957 * @gpio: gpio in question
1958 * Context: any
1959 *
1960 * This is used directly or indirectly to implement gpio_cansleep(). It
1961 * returns nonzero if access reading or writing the GPIO value can sleep.
1962 */
Alexandre Courbotdef63432013-02-15 14:46:15 +09001963static int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001964{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001965 if (!desc)
1966 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001967 /* only call this on GPIOs that are valid! */
1968 return desc->chip->can_sleep;
1969}
1970
David Brownelld2876d02008-02-04 22:28:20 -08001971int __gpio_cansleep(unsigned gpio)
1972{
Alexandre Courbot372e7222013-02-03 01:29:29 +09001973 return gpiod_cansleep(gpio_to_desc(gpio));
David Brownelld2876d02008-02-04 22:28:20 -08001974}
1975EXPORT_SYMBOL_GPL(__gpio_cansleep);
1976
David Brownell0f6d5042008-10-15 22:03:14 -07001977/**
1978 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1979 * @gpio: gpio whose IRQ will be returned (already requested)
1980 * Context: any
1981 *
1982 * This is used directly or indirectly to implement gpio_to_irq().
1983 * It returns the number of the IRQ signaled by this (input) GPIO,
1984 * or a negative errno.
1985 */
Alexandre Courbotdef63432013-02-15 14:46:15 +09001986static int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001987{
1988 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001989 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001990
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001991 if (!desc)
1992 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001993 chip = desc->chip;
1994 offset = gpio_chip_hwgpio(desc);
1995 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1996}
1997
1998int __gpio_to_irq(unsigned gpio)
1999{
2000 return gpiod_to_irq(gpio_to_desc(gpio));
David Brownell0f6d5042008-10-15 22:03:14 -07002001}
2002EXPORT_SYMBOL_GPL(__gpio_to_irq);
2003
David Brownelld2876d02008-02-04 22:28:20 -08002004
David Brownelld2876d02008-02-04 22:28:20 -08002005/* There's no value in making it easy to inline GPIO calls that may sleep.
2006 * Common examples include ones connected to I2C or SPI chips.
2007 */
2008
Alexandre Courbotdef63432013-02-15 14:46:15 +09002009static int gpiod_get_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002010{
2011 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002012 int value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002013 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08002014
2015 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002016 if (!desc)
2017 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002018 chip = desc->chip;
2019 offset = gpio_chip_hwgpio(desc);
2020 value = chip->get ? chip->get(chip, offset) : 0;
2021 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002022 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002023}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002024
2025int gpio_get_value_cansleep(unsigned gpio)
2026{
2027 return gpiod_get_value_cansleep(gpio_to_desc(gpio));
2028}
David Brownelld2876d02008-02-04 22:28:20 -08002029EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
2030
Alexandre Courbot372e7222013-02-03 01:29:29 +09002031static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002032{
2033 struct gpio_chip *chip;
2034
2035 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002036 if (!desc)
2037 return;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002038 chip = desc->chip;
2039 trace_gpio_value(desc_to_gpio(desc), 0, value);
2040 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2041 _gpio_set_open_drain_value(desc, value);
2042 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2043 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302044 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002045 chip->set(chip, gpio_chip_hwgpio(desc), value);
2046}
2047
2048void gpio_set_value_cansleep(unsigned gpio, int value)
2049{
2050 return gpiod_set_value_cansleep(gpio_to_desc(gpio), value);
David Brownelld2876d02008-02-04 22:28:20 -08002051}
2052EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
2053
David Brownelld2876d02008-02-04 22:28:20 -08002054#ifdef CONFIG_DEBUG_FS
2055
David Brownelld2876d02008-02-04 22:28:20 -08002056static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2057{
2058 unsigned i;
2059 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002060 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002061 int is_out;
2062
2063 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2064 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2065 continue;
2066
Alexandre Courbot372e7222013-02-03 01:29:29 +09002067 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002068 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Jarkko Nikula6e8ba722008-11-19 15:36:17 -08002069 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002070 gpio, gdesc->label,
2071 is_out ? "out" : "in ",
2072 chip->get
2073 ? (chip->get(chip, i) ? "hi" : "lo")
2074 : "? ");
David Brownelld2876d02008-02-04 22:28:20 -08002075 seq_printf(s, "\n");
2076 }
2077}
2078
Thierry Redingf9c4a312012-04-12 13:26:01 +02002079static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002080{
Grant Likely362432a2013-02-09 09:41:49 +00002081 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002082 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002083 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002084
2085 s->private = "";
2086
Grant Likely362432a2013-02-09 09:41:49 +00002087 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002088 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002089 if (index-- == 0) {
2090 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002091 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002092 }
2093 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002094
2095 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002096}
2097
2098static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2099{
Grant Likely362432a2013-02-09 09:41:49 +00002100 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002101 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002102 void *ret = NULL;
2103
Grant Likely362432a2013-02-09 09:41:49 +00002104 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002105 if (list_is_last(&chip->list, &gpio_chips))
2106 ret = NULL;
2107 else
2108 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002109 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002110
2111 s->private = "\n";
2112 ++*pos;
2113
2114 return ret;
2115}
2116
2117static void gpiolib_seq_stop(struct seq_file *s, void *v)
2118{
2119}
2120
2121static int gpiolib_seq_show(struct seq_file *s, void *v)
2122{
2123 struct gpio_chip *chip = v;
2124 struct device *dev;
2125
2126 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2127 chip->base, chip->base + chip->ngpio - 1);
2128 dev = chip->dev;
2129 if (dev)
2130 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2131 dev_name(dev));
2132 if (chip->label)
2133 seq_printf(s, ", %s", chip->label);
2134 if (chip->can_sleep)
2135 seq_printf(s, ", can sleep");
2136 seq_printf(s, ":\n");
2137
2138 if (chip->dbg_show)
2139 chip->dbg_show(s, chip);
2140 else
2141 gpiolib_dbg_show(s, chip);
2142
David Brownelld2876d02008-02-04 22:28:20 -08002143 return 0;
2144}
2145
Thierry Redingf9c4a312012-04-12 13:26:01 +02002146static const struct seq_operations gpiolib_seq_ops = {
2147 .start = gpiolib_seq_start,
2148 .next = gpiolib_seq_next,
2149 .stop = gpiolib_seq_stop,
2150 .show = gpiolib_seq_show,
2151};
2152
David Brownelld2876d02008-02-04 22:28:20 -08002153static int gpiolib_open(struct inode *inode, struct file *file)
2154{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002155 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002156}
2157
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002158static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002159 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002160 .open = gpiolib_open,
2161 .read = seq_read,
2162 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002163 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002164};
2165
2166static int __init gpiolib_debugfs_init(void)
2167{
2168 /* /sys/kernel/debug/gpio */
2169 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2170 NULL, NULL, &gpiolib_operations);
2171 return 0;
2172}
2173subsys_initcall(gpiolib_debugfs_init);
2174
2175#endif /* DEBUG_FS */