blob: e27877ad01635fa947e7300f5b04674947446b3f [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>
David Brownelld8f388d82008-07-25 01:46:07 -07006#include <linux/device.h>
7#include <linux/err.h>
8#include <linux/debugfs.h>
9#include <linux/seq_file.h>
10#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060011#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070012#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
David Brownelld2876d02008-02-04 22:28:20 -080014
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060015#define CREATE_TRACE_POINTS
16#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080017
18/* Optional implementation infrastructure for GPIO interfaces.
19 *
20 * Platforms may want to use this if they tend to use very many GPIOs
21 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
22 *
23 * When kernel footprint or instruction count is an issue, simpler
24 * implementations may be preferred. The GPIO programming interface
25 * allows for inlining speed-critical get/set operations for common
26 * cases, so that access to SOC-integrated GPIOs can sometimes cost
27 * only an instruction or two per bit.
28 */
29
30
31/* When debugging, extend minimal trust to callers and platform code.
32 * Also emit diagnostic messages that may help initial bringup, when
33 * board setup or driver bugs are most common.
34 *
35 * Otherwise, minimize overhead in what may be bitbanging codepaths.
36 */
37#ifdef DEBUG
38#define extra_checks 1
39#else
40#define extra_checks 0
41#endif
42
43/* gpio_lock prevents conflicts during gpio_desc[] table updates.
44 * While any GPIO is requested, its gpio_chip is not removable;
45 * each GPIO's "requested" flag serves as a lock and refcount.
46 */
47static DEFINE_SPINLOCK(gpio_lock);
48
49struct gpio_desc {
50 struct gpio_chip *chip;
51 unsigned long flags;
52/* flag symbols are bit numbers */
53#define FLAG_REQUESTED 0
54#define FLAG_IS_OUT 1
Alexandre Courbot710b40e2013-02-02 23:44:06 +090055#define FLAG_EXPORT 2 /* protected by sysfs_lock */
56#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
57#define FLAG_TRIG_FALL 4 /* trigger on falling edge */
58#define FLAG_TRIG_RISE 5 /* trigger on rising edge */
59#define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */
60#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
61#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070062
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070063#define ID_SHIFT 16 /* add new flags before this one */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070064
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070065#define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
Daniel Glöcknerff77c352009-09-22 16:46:38 -070066#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
David Brownelld2876d02008-02-04 22:28:20 -080067
68#ifdef CONFIG_DEBUG_FS
69 const char *label;
70#endif
71};
72static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
73
Daniel Glöcknerff77c352009-09-22 16:46:38 -070074#ifdef CONFIG_GPIO_SYSFS
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070075static DEFINE_IDR(dirent_idr);
Daniel Glöcknerff77c352009-09-22 16:46:38 -070076#endif
77
David Brownelld2876d02008-02-04 22:28:20 -080078static inline void desc_set_label(struct gpio_desc *d, const char *label)
79{
80#ifdef CONFIG_DEBUG_FS
81 d->label = label;
82#endif
83}
84
85/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
86 * when setting direction, and otherwise illegal. Until board setup code
87 * and drivers use explicit requests everywhere (which won't happen when
88 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -070089 * message should motivate switching to explicit requests... so should
90 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -070091 *
92 * NOTE: the autorequest mechanism is going away; at this point it's
93 * only "legal" in the sense that (old) code using it won't break yet,
94 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -080095 */
David Brownell35e8bb52008-10-15 22:03:16 -070096static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
David Brownelld2876d02008-02-04 22:28:20 -080097{
David Brownell8a0cecf2009-04-02 16:57:06 -070098 const struct gpio_chip *chip = desc->chip;
99 const int gpio = chip->base + offset;
David Brownell35e8bb52008-10-15 22:03:16 -0700100
David Brownell8a0cecf2009-04-02 16:57:06 -0700101 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
102 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700103 if (!try_module_get(chip->owner)) {
104 pr_err("GPIO-%d: module can't be gotten \n", gpio);
105 clear_bit(FLAG_REQUESTED, &desc->flags);
106 /* lose */
107 return -EIO;
108 }
David Brownelld2876d02008-02-04 22:28:20 -0800109 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700110 /* caller must chip->request() w/o spinlock */
111 if (chip->request)
112 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800113 }
David Brownell35e8bb52008-10-15 22:03:16 -0700114 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800115}
116
117/* caller holds gpio_lock *OR* gpio is marked as requested */
Grant Likely1a2d3972011-12-12 09:25:57 -0700118struct gpio_chip *gpio_to_chip(unsigned gpio)
David Brownelld2876d02008-02-04 22:28:20 -0800119{
120 return gpio_desc[gpio].chip;
121}
122
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700123/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
124static int gpiochip_find_base(int ngpio)
125{
126 int i;
127 int spare = 0;
128 int base = -ENOSPC;
129
130 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700131 struct gpio_desc *desc = &gpio_desc[i];
132 struct gpio_chip *chip = desc->chip;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700133
Alexandre Courbot710b40e2013-02-02 23:44:06 +0900134 if (!chip) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700135 spare++;
136 if (spare == ngpio) {
137 base = i;
138 break;
139 }
140 } else {
141 spare = 0;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700142 if (chip)
143 i -= chip->ngpio - 1;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700144 }
145 }
146
147 if (gpio_is_valid(base))
148 pr_debug("%s: found new base at %d\n", __func__, base);
149 return base;
150}
151
Mathias Nyman80b0a602012-10-24 17:25:27 +0300152/* caller ensures gpio is valid and requested, chip->get_direction may sleep */
153static int gpio_get_direction(unsigned gpio)
154{
155 struct gpio_chip *chip;
156 struct gpio_desc *desc = &gpio_desc[gpio];
157 int status = -EINVAL;
158
159 chip = gpio_to_chip(gpio);
160 gpio -= chip->base;
161
162 if (!chip->get_direction)
163 return status;
164
165 status = chip->get_direction(chip, gpio);
166 if (status > 0) {
167 /* GPIOF_DIR_IN, or other positive */
168 status = 1;
169 clear_bit(FLAG_IS_OUT, &desc->flags);
170 }
171 if (status == 0) {
172 /* GPIOF_DIR_OUT */
173 set_bit(FLAG_IS_OUT, &desc->flags);
174 }
175 return status;
176}
177
David Brownelld8f388d82008-07-25 01:46:07 -0700178#ifdef CONFIG_GPIO_SYSFS
179
180/* lock protects against unexport_gpio() being called while
181 * sysfs files are active.
182 */
183static DEFINE_MUTEX(sysfs_lock);
184
185/*
186 * /sys/class/gpio/gpioN... only for GPIOs that are exported
187 * /direction
188 * * MAY BE OMITTED if kernel won't allow direction changes
189 * * is read/write as "in" or "out"
190 * * may also be written as "high" or "low", initializing
191 * output value as specified ("out" implies "low")
192 * /value
193 * * always readable, subject to hardware behavior
194 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700195 * /edge
196 * * configures behavior of poll(2) on /value
197 * * available only if pin can generate IRQs on input
198 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800199 * /active_low
200 * * configures polarity of /value
201 * * is read/write as zero/nonzero
202 * * also affects existing and subsequent "falling" and "rising"
203 * /edge configuration
David Brownelld8f388d82008-07-25 01:46:07 -0700204 */
205
206static ssize_t gpio_direction_show(struct device *dev,
207 struct device_attribute *attr, char *buf)
208{
209 const struct gpio_desc *desc = dev_get_drvdata(dev);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300210 unsigned gpio = desc - gpio_desc;
David Brownelld8f388d82008-07-25 01:46:07 -0700211 ssize_t status;
212
213 mutex_lock(&sysfs_lock);
214
215 if (!test_bit(FLAG_EXPORT, &desc->flags))
216 status = -EIO;
217 else
Mathias Nyman80b0a602012-10-24 17:25:27 +0300218 gpio_get_direction(gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700219 status = sprintf(buf, "%s\n",
220 test_bit(FLAG_IS_OUT, &desc->flags)
221 ? "out" : "in");
222
223 mutex_unlock(&sysfs_lock);
224 return status;
225}
226
227static ssize_t gpio_direction_store(struct device *dev,
228 struct device_attribute *attr, const char *buf, size_t size)
229{
230 const struct gpio_desc *desc = dev_get_drvdata(dev);
231 unsigned gpio = desc - gpio_desc;
232 ssize_t status;
233
234 mutex_lock(&sysfs_lock);
235
236 if (!test_bit(FLAG_EXPORT, &desc->flags))
237 status = -EIO;
238 else if (sysfs_streq(buf, "high"))
239 status = gpio_direction_output(gpio, 1);
240 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
241 status = gpio_direction_output(gpio, 0);
242 else if (sysfs_streq(buf, "in"))
243 status = gpio_direction_input(gpio);
244 else
245 status = -EINVAL;
246
247 mutex_unlock(&sysfs_lock);
248 return status ? : size;
249}
250
Jani Nikula07697462009-12-15 16:46:20 -0800251static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700252 gpio_direction_show, gpio_direction_store);
253
254static ssize_t gpio_value_show(struct device *dev,
255 struct device_attribute *attr, char *buf)
256{
257 const struct gpio_desc *desc = dev_get_drvdata(dev);
258 unsigned gpio = desc - gpio_desc;
259 ssize_t status;
260
261 mutex_lock(&sysfs_lock);
262
Jani Nikula07697462009-12-15 16:46:20 -0800263 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700264 status = -EIO;
Jani Nikula07697462009-12-15 16:46:20 -0800265 } else {
266 int value;
267
268 value = !!gpio_get_value_cansleep(gpio);
269 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
270 value = !value;
271
272 status = sprintf(buf, "%d\n", value);
273 }
David Brownelld8f388d82008-07-25 01:46:07 -0700274
275 mutex_unlock(&sysfs_lock);
276 return status;
277}
278
279static ssize_t gpio_value_store(struct device *dev,
280 struct device_attribute *attr, const char *buf, size_t size)
281{
282 const struct gpio_desc *desc = dev_get_drvdata(dev);
283 unsigned gpio = desc - gpio_desc;
284 ssize_t status;
285
286 mutex_lock(&sysfs_lock);
287
288 if (!test_bit(FLAG_EXPORT, &desc->flags))
289 status = -EIO;
290 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
291 status = -EPERM;
292 else {
293 long value;
294
295 status = strict_strtol(buf, 0, &value);
296 if (status == 0) {
Jani Nikula07697462009-12-15 16:46:20 -0800297 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
298 value = !value;
David Brownelld8f388d82008-07-25 01:46:07 -0700299 gpio_set_value_cansleep(gpio, value != 0);
300 status = size;
301 }
302 }
303
304 mutex_unlock(&sysfs_lock);
305 return status;
306}
307
Jani Nikula07697462009-12-15 16:46:20 -0800308static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700309 gpio_value_show, gpio_value_store);
310
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700311static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
312{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700313 struct sysfs_dirent *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700314
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700315 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700316 return IRQ_HANDLED;
317}
318
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700319static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
320 unsigned long gpio_flags)
321{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700322 struct sysfs_dirent *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700323 unsigned long irq_flags;
324 int ret, irq, id;
325
326 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
327 return 0;
328
329 irq = gpio_to_irq(desc - gpio_desc);
330 if (irq < 0)
331 return -EIO;
332
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700333 id = desc->flags >> ID_SHIFT;
334 value_sd = idr_find(&dirent_idr, id);
335 if (value_sd)
336 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700337
338 desc->flags &= ~GPIO_TRIGGER_MASK;
339
340 if (!gpio_flags) {
341 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700342 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700343 }
344
345 irq_flags = IRQF_SHARED;
346 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800347 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
348 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700349 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800350 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
351 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700352
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700353 if (!value_sd) {
354 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
355 if (!value_sd) {
356 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700357 goto err_out;
358 }
359
360 do {
361 ret = -ENOMEM;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700362 if (idr_pre_get(&dirent_idr, GFP_KERNEL))
363 ret = idr_get_new_above(&dirent_idr, value_sd,
364 1, &id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700365 } while (ret == -EAGAIN);
366
367 if (ret)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700368 goto free_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700369
370 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700371 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700372
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700373 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700374 ret = -ERANGE;
375 goto free_id;
376 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700377 }
378
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700379 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700380 "gpiolib", value_sd);
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700381 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700382 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700383
384 desc->flags |= gpio_flags;
385 return 0;
386
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700387free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700388 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700389 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700390free_sd:
391 if (value_sd)
392 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700393err_out:
394 return ret;
395}
396
397static const struct {
398 const char *name;
399 unsigned long flags;
400} trigger_types[] = {
401 { "none", 0 },
402 { "falling", BIT(FLAG_TRIG_FALL) },
403 { "rising", BIT(FLAG_TRIG_RISE) },
404 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
405};
406
407static ssize_t gpio_edge_show(struct device *dev,
408 struct device_attribute *attr, char *buf)
409{
410 const struct gpio_desc *desc = dev_get_drvdata(dev);
411 ssize_t status;
412
413 mutex_lock(&sysfs_lock);
414
415 if (!test_bit(FLAG_EXPORT, &desc->flags))
416 status = -EIO;
417 else {
418 int i;
419
420 status = 0;
421 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
422 if ((desc->flags & GPIO_TRIGGER_MASK)
423 == trigger_types[i].flags) {
424 status = sprintf(buf, "%s\n",
425 trigger_types[i].name);
426 break;
427 }
428 }
429
430 mutex_unlock(&sysfs_lock);
431 return status;
432}
433
434static ssize_t gpio_edge_store(struct device *dev,
435 struct device_attribute *attr, const char *buf, size_t size)
436{
437 struct gpio_desc *desc = dev_get_drvdata(dev);
438 ssize_t status;
439 int i;
440
441 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
442 if (sysfs_streq(trigger_types[i].name, buf))
443 goto found;
444 return -EINVAL;
445
446found:
447 mutex_lock(&sysfs_lock);
448
449 if (!test_bit(FLAG_EXPORT, &desc->flags))
450 status = -EIO;
451 else {
452 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
453 if (!status)
454 status = size;
455 }
456
457 mutex_unlock(&sysfs_lock);
458
459 return status;
460}
461
462static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
463
Jani Nikula07697462009-12-15 16:46:20 -0800464static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
465 int value)
466{
467 int status = 0;
468
469 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
470 return 0;
471
472 if (value)
473 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
474 else
475 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
476
477 /* reconfigure poll(2) support if enabled on one edge only */
478 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
479 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
480 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
481
482 gpio_setup_irq(desc, dev, 0);
483 status = gpio_setup_irq(desc, dev, trigger_flags);
484 }
485
486 return status;
487}
488
489static ssize_t gpio_active_low_show(struct device *dev,
490 struct device_attribute *attr, char *buf)
491{
492 const struct gpio_desc *desc = dev_get_drvdata(dev);
493 ssize_t status;
494
495 mutex_lock(&sysfs_lock);
496
497 if (!test_bit(FLAG_EXPORT, &desc->flags))
498 status = -EIO;
499 else
500 status = sprintf(buf, "%d\n",
501 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
502
503 mutex_unlock(&sysfs_lock);
504
505 return status;
506}
507
508static ssize_t gpio_active_low_store(struct device *dev,
509 struct device_attribute *attr, const char *buf, size_t size)
510{
511 struct gpio_desc *desc = dev_get_drvdata(dev);
512 ssize_t status;
513
514 mutex_lock(&sysfs_lock);
515
516 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
517 status = -EIO;
518 } else {
519 long value;
520
521 status = strict_strtol(buf, 0, &value);
522 if (status == 0)
523 status = sysfs_set_active_low(desc, dev, value != 0);
524 }
525
526 mutex_unlock(&sysfs_lock);
527
528 return status ? : size;
529}
530
531static const DEVICE_ATTR(active_low, 0644,
532 gpio_active_low_show, gpio_active_low_store);
533
David Brownelld8f388d82008-07-25 01:46:07 -0700534static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d82008-07-25 01:46:07 -0700535 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800536 &dev_attr_active_low.attr,
David Brownelld8f388d82008-07-25 01:46:07 -0700537 NULL,
538};
539
540static const struct attribute_group gpio_attr_group = {
541 .attrs = (struct attribute **) gpio_attrs,
542};
543
544/*
545 * /sys/class/gpio/gpiochipN/
546 * /base ... matching gpio_chip.base (N)
547 * /label ... matching gpio_chip.label
548 * /ngpio ... matching gpio_chip.ngpio
549 */
550
551static ssize_t chip_base_show(struct device *dev,
552 struct device_attribute *attr, char *buf)
553{
554 const struct gpio_chip *chip = dev_get_drvdata(dev);
555
556 return sprintf(buf, "%d\n", chip->base);
557}
558static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
559
560static ssize_t chip_label_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
562{
563 const struct gpio_chip *chip = dev_get_drvdata(dev);
564
565 return sprintf(buf, "%s\n", chip->label ? : "");
566}
567static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
568
569static ssize_t chip_ngpio_show(struct device *dev,
570 struct device_attribute *attr, char *buf)
571{
572 const struct gpio_chip *chip = dev_get_drvdata(dev);
573
574 return sprintf(buf, "%u\n", chip->ngpio);
575}
576static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
577
578static const struct attribute *gpiochip_attrs[] = {
579 &dev_attr_base.attr,
580 &dev_attr_label.attr,
581 &dev_attr_ngpio.attr,
582 NULL,
583};
584
585static const struct attribute_group gpiochip_attr_group = {
586 .attrs = (struct attribute **) gpiochip_attrs,
587};
588
589/*
590 * /sys/class/gpio/export ... write-only
591 * integer N ... number of GPIO to export (full access)
592 * /sys/class/gpio/unexport ... write-only
593 * integer N ... number of GPIO to unexport
594 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100595static ssize_t export_store(struct class *class,
596 struct class_attribute *attr,
597 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700598{
599 long gpio;
600 int status;
601
602 status = strict_strtol(buf, 0, &gpio);
603 if (status < 0)
604 goto done;
605
606 /* No extra locking here; FLAG_SYSFS just signifies that the
607 * request and export were done by on behalf of userspace, so
608 * they may be undone on its behalf too.
609 */
610
611 status = gpio_request(gpio, "sysfs");
Mathias Nymanad2fab32012-10-25 14:03:03 +0300612 if (status < 0) {
613 if (status == -EPROBE_DEFER)
614 status = -ENODEV;
David Brownelld8f388d82008-07-25 01:46:07 -0700615 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +0300616 }
David Brownelld8f388d82008-07-25 01:46:07 -0700617 status = gpio_export(gpio, true);
618 if (status < 0)
619 gpio_free(gpio);
620 else
621 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
622
623done:
624 if (status)
625 pr_debug("%s: status %d\n", __func__, status);
626 return status ? : len;
627}
628
Andi Kleen28812fe2010-01-05 12:48:07 +0100629static ssize_t unexport_store(struct class *class,
630 struct class_attribute *attr,
631 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700632{
633 long gpio;
634 int status;
635
636 status = strict_strtol(buf, 0, &gpio);
637 if (status < 0)
638 goto done;
639
640 status = -EINVAL;
641
642 /* reject bogus commands (gpio_unexport ignores them) */
643 if (!gpio_is_valid(gpio))
644 goto done;
645
646 /* No extra locking here; FLAG_SYSFS just signifies that the
647 * request and export were done by on behalf of userspace, so
648 * they may be undone on its behalf too.
649 */
650 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
651 status = 0;
652 gpio_free(gpio);
653 }
654done:
655 if (status)
656 pr_debug("%s: status %d\n", __func__, status);
657 return status ? : len;
658}
659
660static struct class_attribute gpio_class_attrs[] = {
661 __ATTR(export, 0200, NULL, export_store),
662 __ATTR(unexport, 0200, NULL, unexport_store),
663 __ATTR_NULL,
664};
665
666static struct class gpio_class = {
667 .name = "gpio",
668 .owner = THIS_MODULE,
669
670 .class_attrs = gpio_class_attrs,
671};
672
673
674/**
675 * gpio_export - export a GPIO through sysfs
676 * @gpio: gpio to make available, already requested
677 * @direction_may_change: true if userspace may change gpio direction
678 * Context: arch_initcall or later
679 *
680 * When drivers want to make a GPIO accessible to userspace after they
681 * have requested it -- perhaps while debugging, or as part of their
682 * public interface -- they may use this routine. If the GPIO can
683 * change direction (some can't) and the caller allows it, userspace
684 * will see "direction" sysfs attribute which may be used to change
685 * the gpio's direction. A "value" attribute will always be provided.
686 *
687 * Returns zero on success, else an error.
688 */
689int gpio_export(unsigned gpio, bool direction_may_change)
690{
691 unsigned long flags;
692 struct gpio_desc *desc;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100693 int status;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700694 const char *ioname = NULL;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100695 struct device *dev;
David Brownelld8f388d82008-07-25 01:46:07 -0700696
697 /* can't export until sysfs is available ... */
698 if (!gpio_class.p) {
699 pr_debug("%s: called too early!\n", __func__);
700 return -ENOENT;
701 }
702
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100703 if (!gpio_is_valid(gpio)) {
704 pr_debug("%s: gpio %d is not valid\n", __func__, gpio);
705 return -EINVAL;
706 }
David Brownelld8f388d82008-07-25 01:46:07 -0700707
708 mutex_lock(&sysfs_lock);
709
710 spin_lock_irqsave(&gpio_lock, flags);
711 desc = &gpio_desc[gpio];
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100712 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
713 test_bit(FLAG_EXPORT, &desc->flags)) {
714 spin_unlock_irqrestore(&gpio_lock, flags);
715 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
716 __func__, gpio,
717 test_bit(FLAG_REQUESTED, &desc->flags),
718 test_bit(FLAG_EXPORT, &desc->flags));
Dan Carpenter529f2ad2012-10-26 09:59:43 +0300719 status = -EPERM;
720 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700721 }
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100722
723 if (!desc->chip->direction_input || !desc->chip->direction_output)
724 direction_may_change = false;
David Brownelld8f388d82008-07-25 01:46:07 -0700725 spin_unlock_irqrestore(&gpio_lock, flags);
726
Daniel Silverstone926b6632009-04-02 16:57:05 -0700727 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
728 ioname = desc->chip->names[gpio - desc->chip->base];
729
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100730 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
731 desc, ioname ? ioname : "gpio%u", gpio);
732 if (IS_ERR(dev)) {
733 status = PTR_ERR(dev);
734 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700735 }
736
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100737 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
David Brownelld8f388d82008-07-25 01:46:07 -0700738 if (status)
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100739 goto fail_unregister_device;
David Brownelld8f388d82008-07-25 01:46:07 -0700740
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100741 if (direction_may_change) {
742 status = device_create_file(dev, &dev_attr_direction);
743 if (status)
744 goto fail_unregister_device;
745 }
746
747 if (gpio_to_irq(gpio) >= 0 && (direction_may_change ||
748 !test_bit(FLAG_IS_OUT, &desc->flags))) {
749 status = device_create_file(dev, &dev_attr_edge);
750 if (status)
751 goto fail_unregister_device;
752 }
753
754 set_bit(FLAG_EXPORT, &desc->flags);
755 mutex_unlock(&sysfs_lock);
756 return 0;
757
758fail_unregister_device:
759 device_unregister(dev);
760fail_unlock:
761 mutex_unlock(&sysfs_lock);
762 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
David Brownelld8f388d82008-07-25 01:46:07 -0700763 return status;
764}
765EXPORT_SYMBOL_GPL(gpio_export);
766
767static int match_export(struct device *dev, void *data)
768{
769 return dev_get_drvdata(dev) == data;
770}
771
772/**
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700773 * gpio_export_link - create a sysfs link to an exported GPIO node
774 * @dev: device under which to create symlink
775 * @name: name of the symlink
776 * @gpio: gpio to create symlink to, already exported
777 *
778 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
779 * node. Caller is responsible for unlinking.
780 *
781 * Returns zero on success, else an error.
782 */
783int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
784{
785 struct gpio_desc *desc;
786 int status = -EINVAL;
787
788 if (!gpio_is_valid(gpio))
789 goto done;
790
791 mutex_lock(&sysfs_lock);
792
793 desc = &gpio_desc[gpio];
794
795 if (test_bit(FLAG_EXPORT, &desc->flags)) {
796 struct device *tdev;
797
798 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
799 if (tdev != NULL) {
800 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
801 name);
802 } else {
803 status = -ENODEV;
804 }
805 }
806
807 mutex_unlock(&sysfs_lock);
808
809done:
810 if (status)
811 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
812
813 return status;
814}
815EXPORT_SYMBOL_GPL(gpio_export_link);
816
Jani Nikula07697462009-12-15 16:46:20 -0800817
818/**
819 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
820 * @gpio: gpio to change
821 * @value: non-zero to use active low, i.e. inverted values
822 *
823 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
824 * The GPIO does not have to be exported yet. If poll(2) support has
825 * been enabled for either rising or falling edge, it will be
826 * reconfigured to follow the new polarity.
827 *
828 * Returns zero on success, else an error.
829 */
830int gpio_sysfs_set_active_low(unsigned gpio, int value)
831{
832 struct gpio_desc *desc;
833 struct device *dev = NULL;
834 int status = -EINVAL;
835
836 if (!gpio_is_valid(gpio))
837 goto done;
838
839 mutex_lock(&sysfs_lock);
840
841 desc = &gpio_desc[gpio];
842
843 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800844 dev = class_find_device(&gpio_class, NULL, desc, match_export);
845 if (dev == NULL) {
846 status = -ENODEV;
847 goto unlock;
848 }
849 }
850
851 status = sysfs_set_active_low(desc, dev, value);
852
853unlock:
854 mutex_unlock(&sysfs_lock);
855
856done:
857 if (status)
858 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
859
860 return status;
861}
862EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
863
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700864/**
David Brownelld8f388d82008-07-25 01:46:07 -0700865 * gpio_unexport - reverse effect of gpio_export()
866 * @gpio: gpio to make unavailable
867 *
868 * This is implicit on gpio_free().
869 */
870void gpio_unexport(unsigned gpio)
871{
872 struct gpio_desc *desc;
Jon Povey6a99ad42010-07-27 13:18:06 -0700873 int status = 0;
Ming Lei864533c2012-02-13 22:53:20 +0800874 struct device *dev = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -0700875
Jon Povey6a99ad42010-07-27 13:18:06 -0700876 if (!gpio_is_valid(gpio)) {
877 status = -EINVAL;
David Brownelld8f388d82008-07-25 01:46:07 -0700878 goto done;
Jon Povey6a99ad42010-07-27 13:18:06 -0700879 }
David Brownelld8f388d82008-07-25 01:46:07 -0700880
881 mutex_lock(&sysfs_lock);
882
883 desc = &gpio_desc[gpio];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700884
David Brownelld8f388d82008-07-25 01:46:07 -0700885 if (test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700886
887 dev = class_find_device(&gpio_class, NULL, desc, match_export);
888 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700889 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700890 clear_bit(FLAG_EXPORT, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700891 } else
892 status = -ENODEV;
893 }
894
895 mutex_unlock(&sysfs_lock);
Ming Lei864533c2012-02-13 22:53:20 +0800896 if (dev) {
897 device_unregister(dev);
898 put_device(dev);
899 }
David Brownelld8f388d82008-07-25 01:46:07 -0700900done:
901 if (status)
902 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
903}
904EXPORT_SYMBOL_GPL(gpio_unexport);
905
906static int gpiochip_export(struct gpio_chip *chip)
907{
908 int status;
909 struct device *dev;
910
911 /* Many systems register gpio chips for SOC support very early,
912 * before driver model support is available. In those cases we
913 * export this later, in gpiolib_sysfs_init() ... here we just
914 * verify that _some_ field of gpio_class got initialized.
915 */
916 if (!gpio_class.p)
917 return 0;
918
919 /* use chip->base for the ID; it's already known to be unique */
920 mutex_lock(&sysfs_lock);
921 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
922 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800923 if (!IS_ERR(dev)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700924 status = sysfs_create_group(&dev->kobj,
925 &gpiochip_attr_group);
926 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800927 status = PTR_ERR(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700928 chip->exported = (status == 0);
929 mutex_unlock(&sysfs_lock);
930
931 if (status) {
932 unsigned long flags;
933 unsigned gpio;
934
935 spin_lock_irqsave(&gpio_lock, flags);
936 gpio = chip->base;
937 while (gpio_desc[gpio].chip == chip)
938 gpio_desc[gpio++].chip = NULL;
939 spin_unlock_irqrestore(&gpio_lock, flags);
940
941 pr_debug("%s: chip %s status %d\n", __func__,
942 chip->label, status);
943 }
944
945 return status;
946}
947
948static void gpiochip_unexport(struct gpio_chip *chip)
949{
950 int status;
951 struct device *dev;
952
953 mutex_lock(&sysfs_lock);
954 dev = class_find_device(&gpio_class, NULL, chip, match_export);
955 if (dev) {
956 put_device(dev);
957 device_unregister(dev);
958 chip->exported = 0;
959 status = 0;
960 } else
961 status = -ENODEV;
962 mutex_unlock(&sysfs_lock);
963
964 if (status)
965 pr_debug("%s: chip %s status %d\n", __func__,
966 chip->label, status);
967}
968
969static int __init gpiolib_sysfs_init(void)
970{
971 int status;
972 unsigned long flags;
973 unsigned gpio;
974
975 status = class_register(&gpio_class);
976 if (status < 0)
977 return status;
978
979 /* Scan and register the gpio_chips which registered very
980 * early (e.g. before the class_register above was called).
981 *
982 * We run before arch_initcall() so chip->dev nodes can have
983 * registered, and so arch_initcall() can always gpio_export().
984 */
985 spin_lock_irqsave(&gpio_lock, flags);
986 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
987 struct gpio_chip *chip;
988
989 chip = gpio_desc[gpio].chip;
990 if (!chip || chip->exported)
991 continue;
992
993 spin_unlock_irqrestore(&gpio_lock, flags);
994 status = gpiochip_export(chip);
995 spin_lock_irqsave(&gpio_lock, flags);
996 }
997 spin_unlock_irqrestore(&gpio_lock, flags);
998
999
1000 return status;
1001}
1002postcore_initcall(gpiolib_sysfs_init);
1003
1004#else
1005static inline int gpiochip_export(struct gpio_chip *chip)
1006{
1007 return 0;
1008}
1009
1010static inline void gpiochip_unexport(struct gpio_chip *chip)
1011{
1012}
1013
1014#endif /* CONFIG_GPIO_SYSFS */
1015
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001016/**
David Brownelld2876d02008-02-04 22:28:20 -08001017 * gpiochip_add() - register a gpio_chip
1018 * @chip: the chip to register, with chip->base initialized
1019 * Context: potentially before irqs or kmalloc will work
1020 *
1021 * Returns a negative errno if the chip can't be registered, such as
1022 * because the chip->base is invalid or already associated with a
1023 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001024 *
David Brownelld8f388d82008-07-25 01:46:07 -07001025 * When gpiochip_add() is called very early during boot, so that GPIOs
1026 * can be freely used, the chip->dev device must be registered before
1027 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1028 * for GPIOs will fail rudely.
1029 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001030 * If chip->base is negative, this requests dynamic assignment of
1031 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001032 */
1033int gpiochip_add(struct gpio_chip *chip)
1034{
1035 unsigned long flags;
1036 int status = 0;
1037 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001038 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001039
Trent Piephobff5fda2008-05-23 13:04:44 -07001040 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001041 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001042 status = -EINVAL;
1043 goto fail;
1044 }
1045
1046 spin_lock_irqsave(&gpio_lock, flags);
1047
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001048 if (base < 0) {
1049 base = gpiochip_find_base(chip->ngpio);
1050 if (base < 0) {
1051 status = base;
David Brownelld8f388d82008-07-25 01:46:07 -07001052 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001053 }
1054 chip->base = base;
1055 }
1056
David Brownelld2876d02008-02-04 22:28:20 -08001057 /* these GPIO numbers must not be managed by another gpio_chip */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001058 for (id = base; id < base + chip->ngpio; id++) {
David Brownelld2876d02008-02-04 22:28:20 -08001059 if (gpio_desc[id].chip != NULL) {
1060 status = -EBUSY;
1061 break;
1062 }
1063 }
1064 if (status == 0) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001065 for (id = base; id < base + chip->ngpio; id++) {
David Brownelld2876d02008-02-04 22:28:20 -08001066 gpio_desc[id].chip = chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001067
1068 /* REVISIT: most hardware initializes GPIOs as
1069 * inputs (often with pullups enabled) so power
1070 * usage is minimized. Linux code should set the
1071 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +03001072 * and in case chip->get_direction is not set,
David Brownelld8f388d82008-07-25 01:46:07 -07001073 * we may expose the wrong direction in sysfs.
1074 */
1075 gpio_desc[id].flags = !chip->direction_input
1076 ? (1 << FLAG_IS_OUT)
1077 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001078 }
1079 }
1080
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301081#ifdef CONFIG_PINCTRL
1082 INIT_LIST_HEAD(&chip->pin_ranges);
1083#endif
1084
Anton Vorontsov391c9702010-06-08 07:48:17 -06001085 of_gpiochip_add(chip);
1086
David Brownelld8f388d82008-07-25 01:46:07 -07001087unlock:
David Brownelld2876d02008-02-04 22:28:20 -08001088 spin_unlock_irqrestore(&gpio_lock, flags);
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001089
1090 if (status)
1091 goto fail;
1092
1093 status = gpiochip_export(chip);
1094 if (status)
1095 goto fail;
1096
H Hartley Sweetenee1c1e72012-05-11 16:58:33 -07001097 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
Grant Likely64842aa2011-11-06 11:36:18 -07001098 chip->base, chip->base + chip->ngpio - 1,
1099 chip->label ? : "generic");
1100
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001101 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001102fail:
1103 /* failures here can mean systems won't boot... */
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001104 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1105 chip->base, chip->base + chip->ngpio - 1,
1106 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001107 return status;
1108}
1109EXPORT_SYMBOL_GPL(gpiochip_add);
1110
1111/**
1112 * gpiochip_remove() - unregister a gpio_chip
1113 * @chip: the chip to unregister
1114 *
1115 * A gpio_chip with any GPIOs still requested may not be removed.
1116 */
1117int gpiochip_remove(struct gpio_chip *chip)
1118{
1119 unsigned long flags;
1120 int status = 0;
1121 unsigned id;
1122
1123 spin_lock_irqsave(&gpio_lock, flags);
1124
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001125 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001126 of_gpiochip_remove(chip);
1127
David Brownelld2876d02008-02-04 22:28:20 -08001128 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1129 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1130 status = -EBUSY;
1131 break;
1132 }
1133 }
1134 if (status == 0) {
1135 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1136 gpio_desc[id].chip = NULL;
1137 }
1138
1139 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d82008-07-25 01:46:07 -07001140
1141 if (status == 0)
1142 gpiochip_unexport(chip);
1143
David Brownelld2876d02008-02-04 22:28:20 -08001144 return status;
1145}
1146EXPORT_SYMBOL_GPL(gpiochip_remove);
1147
Grant Likely594fa262010-06-08 07:48:16 -06001148/**
1149 * gpiochip_find() - iterator for locating a specific gpio_chip
1150 * @data: data to pass to match function
1151 * @callback: Callback function to check gpio_chip
1152 *
1153 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1154 * determined by a user supplied @match callback. The callback should return
1155 * 0 if the device doesn't match and non-zero if it does. If the callback is
1156 * non-zero, this function will return to the caller and not iterate over any
1157 * more gpio_chips.
1158 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001159struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001160 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001161 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001162{
1163 struct gpio_chip *chip = NULL;
1164 unsigned long flags;
1165 int i;
1166
1167 spin_lock_irqsave(&gpio_lock, flags);
1168 for (i = 0; i < ARCH_NR_GPIOS; i++) {
1169 if (!gpio_desc[i].chip)
1170 continue;
1171
1172 if (match(gpio_desc[i].chip, data)) {
1173 chip = gpio_desc[i].chip;
1174 break;
1175 }
1176 }
1177 spin_unlock_irqrestore(&gpio_lock, flags);
1178
1179 return chip;
1180}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001181EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001182
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301183#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001184
Linus Walleij3f0f8672012-11-20 12:40:15 +01001185/**
1186 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1187 * @chip: the gpiochip to add the range for
1188 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001189 * @gpio_offset: the start offset in the current gpio_chip number space
1190 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001191 * @npins: the number of pins from the offset of each pin space (GPIO and
1192 * pin controller) to accumulate in this range
1193 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001194int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001195 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001196 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301197{
1198 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001199 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301200
Linus Walleij3f0f8672012-11-20 12:40:15 +01001201 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301202 if (!pin_range) {
1203 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1204 chip->label);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001205 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301206 }
1207
Linus Walleij3f0f8672012-11-20 12:40:15 +01001208 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001209 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001210 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301211 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001212 pin_range->range.base = chip->base + gpio_offset;
1213 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301214 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001215 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301216 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001217 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001218 ret = PTR_ERR(pin_range->pctldev);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001219 pr_err("%s: GPIO chip: could not create pin range\n",
1220 chip->label);
1221 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001222 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001223 }
Linus Walleij316511c2012-11-21 08:48:09 +01001224 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1225 chip->label, gpio_offset, gpio_offset + npins - 1,
1226 pinctl_name,
1227 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301228
1229 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001230
1231 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301232}
Linus Walleij165adc92012-11-06 14:49:39 +01001233EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301234
Linus Walleij3f0f8672012-11-20 12:40:15 +01001235/**
1236 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1237 * @chip: the chip to remove all the mappings for
1238 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301239void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1240{
1241 struct gpio_pin_range *pin_range, *tmp;
1242
1243 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1244 list_del(&pin_range->node);
1245 pinctrl_remove_gpio_range(pin_range->pctldev,
1246 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001247 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301248 }
1249}
Linus Walleij165adc92012-11-06 14:49:39 +01001250EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1251
1252#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301253
David Brownelld2876d02008-02-04 22:28:20 -08001254/* These "optional" allocation calls help prevent drivers from stomping
1255 * on each other, and help provide better diagnostics in debugfs.
1256 * They're called even less than the "set direction" calls.
1257 */
1258int gpio_request(unsigned gpio, const char *label)
1259{
1260 struct gpio_desc *desc;
David Brownell35e8bb52008-10-15 22:03:16 -07001261 struct gpio_chip *chip;
Mark Browne9354572012-07-09 12:22:56 +01001262 int status = -EPROBE_DEFER;
David Brownelld2876d02008-02-04 22:28:20 -08001263 unsigned long flags;
1264
1265 spin_lock_irqsave(&gpio_lock, flags);
1266
Mathias Nymanad2fab32012-10-25 14:03:03 +03001267 if (!gpio_is_valid(gpio)) {
1268 status = -EINVAL;
David Brownelld2876d02008-02-04 22:28:20 -08001269 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +03001270 }
David Brownelld2876d02008-02-04 22:28:20 -08001271 desc = &gpio_desc[gpio];
David Brownell35e8bb52008-10-15 22:03:16 -07001272 chip = desc->chip;
1273 if (chip == NULL)
David Brownelld2876d02008-02-04 22:28:20 -08001274 goto done;
1275
David Brownell35e8bb52008-10-15 22:03:16 -07001276 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001277 goto done;
1278
David Brownelld2876d02008-02-04 22:28:20 -08001279 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001280 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001281 */
1282
1283 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1284 desc_set_label(desc, label ? : "?");
1285 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001286 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001287 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001288 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001289 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001290 }
1291
1292 if (chip->request) {
1293 /* chip->request may sleep */
1294 spin_unlock_irqrestore(&gpio_lock, flags);
1295 status = chip->request(chip, gpio - chip->base);
1296 spin_lock_irqsave(&gpio_lock, flags);
1297
1298 if (status < 0) {
1299 desc_set_label(desc, NULL);
1300 module_put(chip->owner);
1301 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001302 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001303 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001304 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001305 if (chip->get_direction) {
1306 /* chip->get_direction may sleep */
1307 spin_unlock_irqrestore(&gpio_lock, flags);
1308 gpio_get_direction(gpio);
1309 spin_lock_irqsave(&gpio_lock, flags);
1310 }
David Brownelld2876d02008-02-04 22:28:20 -08001311done:
1312 if (status)
1313 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1314 gpio, label ? : "?", status);
1315 spin_unlock_irqrestore(&gpio_lock, flags);
1316 return status;
1317}
1318EXPORT_SYMBOL_GPL(gpio_request);
1319
1320void gpio_free(unsigned gpio)
1321{
1322 unsigned long flags;
1323 struct gpio_desc *desc;
David Brownell35e8bb52008-10-15 22:03:16 -07001324 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001325
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001326 might_sleep();
1327
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001328 if (!gpio_is_valid(gpio)) {
David Brownelld2876d02008-02-04 22:28:20 -08001329 WARN_ON(extra_checks);
1330 return;
1331 }
1332
David Brownelld8f388d82008-07-25 01:46:07 -07001333 gpio_unexport(gpio);
1334
David Brownelld2876d02008-02-04 22:28:20 -08001335 spin_lock_irqsave(&gpio_lock, flags);
1336
1337 desc = &gpio_desc[gpio];
David Brownell35e8bb52008-10-15 22:03:16 -07001338 chip = desc->chip;
1339 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1340 if (chip->free) {
1341 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001342 might_sleep_if(chip->can_sleep);
David Brownell35e8bb52008-10-15 22:03:16 -07001343 chip->free(chip, gpio - chip->base);
1344 spin_lock_irqsave(&gpio_lock, flags);
1345 }
David Brownelld2876d02008-02-04 22:28:20 -08001346 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001347 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001348 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001349 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301350 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301351 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001352 } else
David Brownelld2876d02008-02-04 22:28:20 -08001353 WARN_ON(extra_checks);
1354
1355 spin_unlock_irqrestore(&gpio_lock, flags);
1356}
1357EXPORT_SYMBOL_GPL(gpio_free);
1358
Eric Miao3e45f1d2010-03-05 13:44:35 -08001359/**
1360 * gpio_request_one - request a single GPIO with initial configuration
1361 * @gpio: the GPIO number
1362 * @flags: GPIO configuration as specified by GPIOF_*
1363 * @label: a literal description string of this GPIO
1364 */
1365int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1366{
1367 int err;
1368
1369 err = gpio_request(gpio, label);
1370 if (err)
1371 return err;
1372
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301373 if (flags & GPIOF_OPEN_DRAIN)
1374 set_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags);
1375
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301376 if (flags & GPIOF_OPEN_SOURCE)
1377 set_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags);
1378
Eric Miao3e45f1d2010-03-05 13:44:35 -08001379 if (flags & GPIOF_DIR_IN)
1380 err = gpio_direction_input(gpio);
1381 else
1382 err = gpio_direction_output(gpio,
1383 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1384
Aaro Koskinene2548112010-12-21 17:24:22 -08001385 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001386 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -08001387
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001388 if (flags & GPIOF_EXPORT) {
1389 err = gpio_export(gpio, flags & GPIOF_EXPORT_CHANGEABLE);
1390 if (err)
1391 goto free_gpio;
1392 }
1393
1394 return 0;
1395
1396 free_gpio:
1397 gpio_free(gpio);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001398 return err;
1399}
1400EXPORT_SYMBOL_GPL(gpio_request_one);
1401
1402/**
1403 * gpio_request_array - request multiple GPIOs in a single call
1404 * @array: array of the 'struct gpio'
1405 * @num: how many GPIOs in the array
1406 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001407int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001408{
1409 int i, err;
1410
1411 for (i = 0; i < num; i++, array++) {
1412 err = gpio_request_one(array->gpio, array->flags, array->label);
1413 if (err)
1414 goto err_free;
1415 }
1416 return 0;
1417
1418err_free:
1419 while (i--)
1420 gpio_free((--array)->gpio);
1421 return err;
1422}
1423EXPORT_SYMBOL_GPL(gpio_request_array);
1424
1425/**
1426 * gpio_free_array - release multiple GPIOs in a single call
1427 * @array: array of the 'struct gpio'
1428 * @num: how many GPIOs in the array
1429 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001430void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001431{
1432 while (num--)
1433 gpio_free((array++)->gpio);
1434}
1435EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001436
1437/**
1438 * gpiochip_is_requested - return string iff signal was requested
1439 * @chip: controller managing the signal
1440 * @offset: of signal within controller's 0..(ngpio - 1) range
1441 *
1442 * Returns NULL if the GPIO is not currently requested, else a string.
1443 * If debugfs support is enabled, the string returned is the label passed
1444 * to gpio_request(); otherwise it is a meaningless constant.
1445 *
1446 * This function is for use by GPIO controller drivers. The label can
1447 * help with diagnostics, and knowing that the signal is used as a GPIO
1448 * can help avoid accidentally multiplexing it to another controller.
1449 */
1450const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1451{
1452 unsigned gpio = chip->base + offset;
1453
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001454 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
David Brownelld2876d02008-02-04 22:28:20 -08001455 return NULL;
1456 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1457 return NULL;
1458#ifdef CONFIG_DEBUG_FS
1459 return gpio_desc[gpio].label;
1460#else
1461 return "?";
1462#endif
1463}
1464EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1465
1466
1467/* Drivers MUST set GPIO direction before making get/set calls. In
1468 * some cases this is done in early boot, before IRQs are enabled.
1469 *
1470 * As a rule these aren't called more than once (except for drivers
1471 * using the open-drain emulation idiom) so these are natural places
1472 * to accumulate extra debugging checks. Note that we can't (yet)
1473 * rely on gpio_request() having been called beforehand.
1474 */
1475
1476int gpio_direction_input(unsigned gpio)
1477{
1478 unsigned long flags;
1479 struct gpio_chip *chip;
1480 struct gpio_desc *desc = &gpio_desc[gpio];
1481 int status = -EINVAL;
1482
1483 spin_lock_irqsave(&gpio_lock, flags);
1484
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001485 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001486 goto fail;
1487 chip = desc->chip;
1488 if (!chip || !chip->get || !chip->direction_input)
1489 goto fail;
1490 gpio -= chip->base;
1491 if (gpio >= chip->ngpio)
1492 goto fail;
David Brownell35e8bb52008-10-15 22:03:16 -07001493 status = gpio_ensure_requested(desc, gpio);
1494 if (status < 0)
1495 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001496
1497 /* now we know the gpio is valid and chip won't vanish */
1498
1499 spin_unlock_irqrestore(&gpio_lock, flags);
1500
David Brownell9c4ba942010-08-10 18:02:24 -07001501 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001502
David Brownell35e8bb52008-10-15 22:03:16 -07001503 if (status) {
1504 status = chip->request(chip, gpio);
1505 if (status < 0) {
1506 pr_debug("GPIO-%d: chip request fail, %d\n",
1507 chip->base + gpio, status);
1508 /* and it's not available to anyone else ...
1509 * gpio_request() is the fully clean solution.
1510 */
1511 goto lose;
1512 }
1513 }
1514
David Brownelld2876d02008-02-04 22:28:20 -08001515 status = chip->direction_input(chip, gpio);
1516 if (status == 0)
1517 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001518
1519 trace_gpio_direction(chip->base + gpio, 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001520lose:
David Brownelld2876d02008-02-04 22:28:20 -08001521 return status;
1522fail:
1523 spin_unlock_irqrestore(&gpio_lock, flags);
1524 if (status)
1525 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001526 __func__, gpio, status);
David Brownelld2876d02008-02-04 22:28:20 -08001527 return status;
1528}
1529EXPORT_SYMBOL_GPL(gpio_direction_input);
1530
1531int gpio_direction_output(unsigned gpio, int value)
1532{
1533 unsigned long flags;
1534 struct gpio_chip *chip;
1535 struct gpio_desc *desc = &gpio_desc[gpio];
1536 int status = -EINVAL;
1537
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301538 /* Open drain pin should not be driven to 1 */
1539 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1540 return gpio_direction_input(gpio);
1541
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301542 /* Open source pin should not be driven to 0 */
1543 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1544 return gpio_direction_input(gpio);
1545
David Brownelld2876d02008-02-04 22:28:20 -08001546 spin_lock_irqsave(&gpio_lock, flags);
1547
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001548 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001549 goto fail;
1550 chip = desc->chip;
1551 if (!chip || !chip->set || !chip->direction_output)
1552 goto fail;
1553 gpio -= chip->base;
1554 if (gpio >= chip->ngpio)
1555 goto fail;
David Brownell35e8bb52008-10-15 22:03:16 -07001556 status = gpio_ensure_requested(desc, gpio);
1557 if (status < 0)
1558 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001559
1560 /* now we know the gpio is valid and chip won't vanish */
1561
1562 spin_unlock_irqrestore(&gpio_lock, flags);
1563
David Brownell9c4ba942010-08-10 18:02:24 -07001564 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001565
David Brownell35e8bb52008-10-15 22:03:16 -07001566 if (status) {
1567 status = chip->request(chip, gpio);
1568 if (status < 0) {
1569 pr_debug("GPIO-%d: chip request fail, %d\n",
1570 chip->base + gpio, status);
1571 /* and it's not available to anyone else ...
1572 * gpio_request() is the fully clean solution.
1573 */
1574 goto lose;
1575 }
1576 }
1577
David Brownelld2876d02008-02-04 22:28:20 -08001578 status = chip->direction_output(chip, gpio, value);
1579 if (status == 0)
1580 set_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001581 trace_gpio_value(chip->base + gpio, 0, value);
1582 trace_gpio_direction(chip->base + gpio, 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001583lose:
David Brownelld2876d02008-02-04 22:28:20 -08001584 return status;
1585fail:
1586 spin_unlock_irqrestore(&gpio_lock, flags);
1587 if (status)
1588 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001589 __func__, gpio, status);
David Brownelld2876d02008-02-04 22:28:20 -08001590 return status;
1591}
1592EXPORT_SYMBOL_GPL(gpio_direction_output);
1593
Felipe Balbic4b5be92010-05-26 14:42:23 -07001594/**
1595 * gpio_set_debounce - sets @debounce time for a @gpio
1596 * @gpio: the gpio to set debounce time
1597 * @debounce: debounce time is microseconds
1598 */
1599int gpio_set_debounce(unsigned gpio, unsigned debounce)
1600{
1601 unsigned long flags;
1602 struct gpio_chip *chip;
1603 struct gpio_desc *desc = &gpio_desc[gpio];
1604 int status = -EINVAL;
1605
1606 spin_lock_irqsave(&gpio_lock, flags);
1607
1608 if (!gpio_is_valid(gpio))
1609 goto fail;
1610 chip = desc->chip;
1611 if (!chip || !chip->set || !chip->set_debounce)
1612 goto fail;
1613 gpio -= chip->base;
1614 if (gpio >= chip->ngpio)
1615 goto fail;
1616 status = gpio_ensure_requested(desc, gpio);
1617 if (status < 0)
1618 goto fail;
1619
1620 /* now we know the gpio is valid and chip won't vanish */
1621
1622 spin_unlock_irqrestore(&gpio_lock, flags);
1623
David Brownell9c4ba942010-08-10 18:02:24 -07001624 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001625
1626 return chip->set_debounce(chip, gpio, debounce);
1627
1628fail:
1629 spin_unlock_irqrestore(&gpio_lock, flags);
1630 if (status)
1631 pr_debug("%s: gpio-%d status %d\n",
1632 __func__, gpio, status);
1633
1634 return status;
1635}
1636EXPORT_SYMBOL_GPL(gpio_set_debounce);
David Brownelld2876d02008-02-04 22:28:20 -08001637
1638/* I/O calls are only valid after configuration completed; the relevant
1639 * "is this a valid GPIO" error checks should already have been done.
1640 *
1641 * "Get" operations are often inlinable as reading a pin value register,
1642 * and masking the relevant bit in that register.
1643 *
1644 * When "set" operations are inlinable, they involve writing that mask to
1645 * one register to set a low value, or a different register to set it high.
1646 * Otherwise locking is needed, so there may be little value to inlining.
1647 *
1648 *------------------------------------------------------------------------
1649 *
1650 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1651 * have requested the GPIO. That can include implicit requesting by
1652 * a direction setting call. Marking a gpio as requested locks its chip
1653 * in memory, guaranteeing that these table lookups need no more locking
1654 * and that gpiochip_remove() will fail.
1655 *
1656 * REVISIT when debugging, consider adding some instrumentation to ensure
1657 * that the GPIO was actually requested.
1658 */
1659
1660/**
1661 * __gpio_get_value() - return a gpio's value
1662 * @gpio: gpio whose value will be returned
1663 * Context: any
1664 *
1665 * This is used directly or indirectly to implement gpio_get_value().
1666 * It returns the zero or nonzero value provided by the associated
1667 * gpio_chip.get() method; or zero if no such method is provided.
1668 */
1669int __gpio_get_value(unsigned gpio)
1670{
1671 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001672 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001673
1674 chip = gpio_to_chip(gpio);
Mark Browne4e449e2012-02-17 10:46:00 -08001675 /* Should be using gpio_get_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001676 WARN_ON(chip->can_sleep);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001677 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1678 trace_gpio_value(gpio, 1, value);
1679 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001680}
1681EXPORT_SYMBOL_GPL(__gpio_get_value);
1682
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301683/*
1684 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1685 * @gpio: Gpio whose state need to be set.
1686 * @chip: Gpio chip.
1687 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1688 */
1689static void _gpio_set_open_drain_value(unsigned gpio,
1690 struct gpio_chip *chip, int value)
1691{
1692 int err = 0;
1693 if (value) {
1694 err = chip->direction_input(chip, gpio - chip->base);
1695 if (!err)
1696 clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1697 } else {
1698 err = chip->direction_output(chip, gpio - chip->base, 0);
1699 if (!err)
1700 set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1701 }
1702 trace_gpio_direction(gpio, value, err);
1703 if (err < 0)
1704 pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
1705 __func__, gpio, err);
1706}
1707
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301708/*
1709 * _gpio_set_open_source() - Set the open source gpio's value.
1710 * @gpio: Gpio whose state need to be set.
1711 * @chip: Gpio chip.
1712 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1713 */
1714static void _gpio_set_open_source_value(unsigned gpio,
1715 struct gpio_chip *chip, int value)
1716{
1717 int err = 0;
1718 if (value) {
1719 err = chip->direction_output(chip, gpio - chip->base, 1);
1720 if (!err)
1721 set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1722 } else {
1723 err = chip->direction_input(chip, gpio - chip->base);
1724 if (!err)
1725 clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1726 }
1727 trace_gpio_direction(gpio, !value, err);
1728 if (err < 0)
1729 pr_err("%s: Error in set_value for open source gpio%d err %d\n",
1730 __func__, gpio, err);
1731}
1732
1733
David Brownelld2876d02008-02-04 22:28:20 -08001734/**
1735 * __gpio_set_value() - assign a gpio's value
1736 * @gpio: gpio whose value will be assigned
1737 * @value: value to assign
1738 * Context: any
1739 *
1740 * This is used directly or indirectly to implement gpio_set_value().
1741 * It invokes the associated gpio_chip.set() method.
1742 */
1743void __gpio_set_value(unsigned gpio, int value)
1744{
1745 struct gpio_chip *chip;
1746
1747 chip = gpio_to_chip(gpio);
Mark Browne4e449e2012-02-17 10:46:00 -08001748 /* Should be using gpio_set_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001749 WARN_ON(chip->can_sleep);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001750 trace_gpio_value(gpio, 0, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301751 if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
1752 _gpio_set_open_drain_value(gpio, chip, value);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301753 else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
1754 _gpio_set_open_source_value(gpio, chip, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301755 else
1756 chip->set(chip, gpio - chip->base, value);
David Brownelld2876d02008-02-04 22:28:20 -08001757}
1758EXPORT_SYMBOL_GPL(__gpio_set_value);
1759
1760/**
1761 * __gpio_cansleep() - report whether gpio value access will sleep
1762 * @gpio: gpio in question
1763 * Context: any
1764 *
1765 * This is used directly or indirectly to implement gpio_cansleep(). It
1766 * returns nonzero if access reading or writing the GPIO value can sleep.
1767 */
1768int __gpio_cansleep(unsigned gpio)
1769{
1770 struct gpio_chip *chip;
1771
1772 /* only call this on GPIOs that are valid! */
1773 chip = gpio_to_chip(gpio);
1774
1775 return chip->can_sleep;
1776}
1777EXPORT_SYMBOL_GPL(__gpio_cansleep);
1778
David Brownell0f6d5042008-10-15 22:03:14 -07001779/**
1780 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1781 * @gpio: gpio whose IRQ will be returned (already requested)
1782 * Context: any
1783 *
1784 * This is used directly or indirectly to implement gpio_to_irq().
1785 * It returns the number of the IRQ signaled by this (input) GPIO,
1786 * or a negative errno.
1787 */
1788int __gpio_to_irq(unsigned gpio)
1789{
1790 struct gpio_chip *chip;
1791
1792 chip = gpio_to_chip(gpio);
1793 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1794}
1795EXPORT_SYMBOL_GPL(__gpio_to_irq);
1796
David Brownelld2876d02008-02-04 22:28:20 -08001797
1798
1799/* There's no value in making it easy to inline GPIO calls that may sleep.
1800 * Common examples include ones connected to I2C or SPI chips.
1801 */
1802
1803int gpio_get_value_cansleep(unsigned gpio)
1804{
1805 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001806 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001807
1808 might_sleep_if(extra_checks);
1809 chip = gpio_to_chip(gpio);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001810 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1811 trace_gpio_value(gpio, 1, value);
1812 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001813}
1814EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1815
1816void gpio_set_value_cansleep(unsigned gpio, int value)
1817{
1818 struct gpio_chip *chip;
1819
1820 might_sleep_if(extra_checks);
1821 chip = gpio_to_chip(gpio);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001822 trace_gpio_value(gpio, 0, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301823 if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
1824 _gpio_set_open_drain_value(gpio, chip, value);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301825 else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
1826 _gpio_set_open_source_value(gpio, chip, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301827 else
1828 chip->set(chip, gpio - chip->base, value);
David Brownelld2876d02008-02-04 22:28:20 -08001829}
1830EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1831
1832
1833#ifdef CONFIG_DEBUG_FS
1834
David Brownelld2876d02008-02-04 22:28:20 -08001835static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1836{
1837 unsigned i;
1838 unsigned gpio = chip->base;
1839 struct gpio_desc *gdesc = &gpio_desc[gpio];
1840 int is_out;
1841
1842 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1843 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1844 continue;
1845
Mathias Nyman80b0a602012-10-24 17:25:27 +03001846 gpio_get_direction(gpio);
David Brownelld2876d02008-02-04 22:28:20 -08001847 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Jarkko Nikula6e8ba722008-11-19 15:36:17 -08001848 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08001849 gpio, gdesc->label,
1850 is_out ? "out" : "in ",
1851 chip->get
1852 ? (chip->get(chip, i) ? "hi" : "lo")
1853 : "? ");
David Brownelld2876d02008-02-04 22:28:20 -08001854 seq_printf(s, "\n");
1855 }
1856}
1857
Thierry Redingf9c4a312012-04-12 13:26:01 +02001858static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08001859{
Thierry Redingf9c4a312012-04-12 13:26:01 +02001860 struct gpio_chip *chip = NULL;
1861 unsigned int gpio;
1862 void *ret = NULL;
1863 loff_t index = 0;
David Brownelld2876d02008-02-04 22:28:20 -08001864
1865 /* REVISIT this isn't locked against gpio_chip removal ... */
1866
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001867 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
Thierry Redingf9c4a312012-04-12 13:26:01 +02001868 if (gpio_desc[gpio].chip == chip)
David Brownelld2876d02008-02-04 22:28:20 -08001869 continue;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001870
David Brownelld2876d02008-02-04 22:28:20 -08001871 chip = gpio_desc[gpio].chip;
1872 if (!chip)
1873 continue;
1874
Thierry Redingf9c4a312012-04-12 13:26:01 +02001875 if (index++ >= *pos) {
1876 ret = chip;
1877 break;
1878 }
David Brownelld2876d02008-02-04 22:28:20 -08001879 }
Thierry Redingf9c4a312012-04-12 13:26:01 +02001880
1881 s->private = "";
1882
1883 return ret;
1884}
1885
1886static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
1887{
1888 struct gpio_chip *chip = v;
1889 unsigned int gpio;
1890 void *ret = NULL;
1891
1892 /* skip GPIOs provided by the current chip */
1893 for (gpio = chip->base + chip->ngpio; gpio_is_valid(gpio); gpio++) {
1894 chip = gpio_desc[gpio].chip;
1895 if (chip) {
1896 ret = chip;
1897 break;
1898 }
1899 }
1900
1901 s->private = "\n";
1902 ++*pos;
1903
1904 return ret;
1905}
1906
1907static void gpiolib_seq_stop(struct seq_file *s, void *v)
1908{
1909}
1910
1911static int gpiolib_seq_show(struct seq_file *s, void *v)
1912{
1913 struct gpio_chip *chip = v;
1914 struct device *dev;
1915
1916 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
1917 chip->base, chip->base + chip->ngpio - 1);
1918 dev = chip->dev;
1919 if (dev)
1920 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
1921 dev_name(dev));
1922 if (chip->label)
1923 seq_printf(s, ", %s", chip->label);
1924 if (chip->can_sleep)
1925 seq_printf(s, ", can sleep");
1926 seq_printf(s, ":\n");
1927
1928 if (chip->dbg_show)
1929 chip->dbg_show(s, chip);
1930 else
1931 gpiolib_dbg_show(s, chip);
1932
David Brownelld2876d02008-02-04 22:28:20 -08001933 return 0;
1934}
1935
Thierry Redingf9c4a312012-04-12 13:26:01 +02001936static const struct seq_operations gpiolib_seq_ops = {
1937 .start = gpiolib_seq_start,
1938 .next = gpiolib_seq_next,
1939 .stop = gpiolib_seq_stop,
1940 .show = gpiolib_seq_show,
1941};
1942
David Brownelld2876d02008-02-04 22:28:20 -08001943static int gpiolib_open(struct inode *inode, struct file *file)
1944{
Thierry Redingf9c4a312012-04-12 13:26:01 +02001945 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08001946}
1947
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001948static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02001949 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08001950 .open = gpiolib_open,
1951 .read = seq_read,
1952 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02001953 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08001954};
1955
1956static int __init gpiolib_debugfs_init(void)
1957{
1958 /* /sys/kernel/debug/gpio */
1959 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1960 NULL, NULL, &gpiolib_operations);
1961 return 0;
1962}
1963subsys_initcall(gpiolib_debugfs_init);
1964
1965#endif /* DEBUG_FS */