blob: 26e27c1fecb1da76aff1d0801bf2741104ed3dff [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 Brownelld8f388d2008-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
Anton Vorontsov169b6a72008-04-28 02:14:47 -070055#define FLAG_RESERVED 2
David Brownelld8f388d2008-07-25 01:46:07 -070056#define FLAG_EXPORT 3 /* protected by sysfs_lock */
57#define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070058#define FLAG_TRIG_FALL 5 /* trigger on falling edge */
59#define FLAG_TRIG_RISE 6 /* trigger on rising edge */
Jani Nikula07697462009-12-15 16:46:20 -080060#define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */
Laxman Dewanganaca5ce12012-02-17 20:26:21 +053061#define FLAG_OPEN_DRAIN 8 /* Gpio is open drain type */
Laxman Dewangan25553ff2012-02-17 20:26:22 +053062#define FLAG_OPEN_SOURCE 9 /* 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
Daniel Glöcknerff77c352009-09-22 16:46:38 -070075#ifdef CONFIG_GPIO_SYSFS
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070076static DEFINE_IDR(dirent_idr);
Daniel Glöcknerff77c352009-09-22 16:46:38 -070077#endif
78
David Brownelld2876d02008-02-04 22:28:20 -080079static inline void desc_set_label(struct gpio_desc *d, const char *label)
80{
81#ifdef CONFIG_DEBUG_FS
82 d->label = label;
83#endif
84}
85
86/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
87 * when setting direction, and otherwise illegal. Until board setup code
88 * and drivers use explicit requests everywhere (which won't happen when
89 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -070090 * message should motivate switching to explicit requests... so should
91 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -070092 *
93 * NOTE: the autorequest mechanism is going away; at this point it's
94 * only "legal" in the sense that (old) code using it won't break yet,
95 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -080096 */
David Brownell35e8bb52008-10-15 22:03:16 -070097static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
David Brownelld2876d02008-02-04 22:28:20 -080098{
David Brownell8a0cecf2009-04-02 16:57:06 -070099 const struct gpio_chip *chip = desc->chip;
100 const int gpio = chip->base + offset;
David Brownell35e8bb52008-10-15 22:03:16 -0700101
David Brownell8a0cecf2009-04-02 16:57:06 -0700102 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
103 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700104 if (!try_module_get(chip->owner)) {
105 pr_err("GPIO-%d: module can't be gotten \n", gpio);
106 clear_bit(FLAG_REQUESTED, &desc->flags);
107 /* lose */
108 return -EIO;
109 }
David Brownelld2876d02008-02-04 22:28:20 -0800110 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700111 /* caller must chip->request() w/o spinlock */
112 if (chip->request)
113 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800114 }
David Brownell35e8bb52008-10-15 22:03:16 -0700115 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800116}
117
118/* caller holds gpio_lock *OR* gpio is marked as requested */
Grant Likely1a2d3972011-12-12 09:25:57 -0700119struct gpio_chip *gpio_to_chip(unsigned gpio)
David Brownelld2876d02008-02-04 22:28:20 -0800120{
121 return gpio_desc[gpio].chip;
122}
123
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700124/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
125static int gpiochip_find_base(int ngpio)
126{
127 int i;
128 int spare = 0;
129 int base = -ENOSPC;
130
131 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700132 struct gpio_desc *desc = &gpio_desc[i];
133 struct gpio_chip *chip = desc->chip;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700134
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700135 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700136 spare++;
137 if (spare == ngpio) {
138 base = i;
139 break;
140 }
141 } else {
142 spare = 0;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700143 if (chip)
144 i -= chip->ngpio - 1;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700145 }
146 }
147
148 if (gpio_is_valid(base))
149 pr_debug("%s: found new base at %d\n", __func__, base);
150 return base;
151}
152
David Brownelld2876d02008-02-04 22:28:20 -0800153/**
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700154 * gpiochip_reserve() - reserve range of gpios to use with platform code only
155 * @start: starting gpio number
156 * @ngpio: number of gpios to reserve
157 * Context: platform init, potentially before irqs or kmalloc will work
158 *
159 * Returns a negative errno if any gpio within the range is already reserved
160 * or registered, else returns zero as a success code. Use this function
161 * to mark a range of gpios as unavailable for dynamic gpio number allocation,
162 * for example because its driver support is not yet loaded.
163 */
164int __init gpiochip_reserve(int start, int ngpio)
165{
166 int ret = 0;
167 unsigned long flags;
168 int i;
169
Trent Piephobff5fda2008-05-23 13:04:44 -0700170 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700171 return -EINVAL;
172
173 spin_lock_irqsave(&gpio_lock, flags);
174
175 for (i = start; i < start + ngpio; i++) {
176 struct gpio_desc *desc = &gpio_desc[i];
177
178 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
179 ret = -EBUSY;
180 goto err;
181 }
182
183 set_bit(FLAG_RESERVED, &desc->flags);
184 }
185
186 pr_debug("%s: reserved gpios from %d to %d\n",
187 __func__, start, start + ngpio - 1);
188err:
189 spin_unlock_irqrestore(&gpio_lock, flags);
190
191 return ret;
192}
193
David Brownelld8f388d2008-07-25 01:46:07 -0700194#ifdef CONFIG_GPIO_SYSFS
195
196/* lock protects against unexport_gpio() being called while
197 * sysfs files are active.
198 */
199static DEFINE_MUTEX(sysfs_lock);
200
201/*
202 * /sys/class/gpio/gpioN... only for GPIOs that are exported
203 * /direction
204 * * MAY BE OMITTED if kernel won't allow direction changes
205 * * is read/write as "in" or "out"
206 * * may also be written as "high" or "low", initializing
207 * output value as specified ("out" implies "low")
208 * /value
209 * * always readable, subject to hardware behavior
210 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700211 * /edge
212 * * configures behavior of poll(2) on /value
213 * * available only if pin can generate IRQs on input
214 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800215 * /active_low
216 * * configures polarity of /value
217 * * is read/write as zero/nonzero
218 * * also affects existing and subsequent "falling" and "rising"
219 * /edge configuration
David Brownelld8f388d2008-07-25 01:46:07 -0700220 */
221
222static ssize_t gpio_direction_show(struct device *dev,
223 struct device_attribute *attr, char *buf)
224{
225 const struct gpio_desc *desc = dev_get_drvdata(dev);
226 ssize_t status;
227
228 mutex_lock(&sysfs_lock);
229
230 if (!test_bit(FLAG_EXPORT, &desc->flags))
231 status = -EIO;
232 else
233 status = sprintf(buf, "%s\n",
234 test_bit(FLAG_IS_OUT, &desc->flags)
235 ? "out" : "in");
236
237 mutex_unlock(&sysfs_lock);
238 return status;
239}
240
241static ssize_t gpio_direction_store(struct device *dev,
242 struct device_attribute *attr, const char *buf, size_t size)
243{
244 const struct gpio_desc *desc = dev_get_drvdata(dev);
245 unsigned gpio = desc - gpio_desc;
246 ssize_t status;
247
248 mutex_lock(&sysfs_lock);
249
250 if (!test_bit(FLAG_EXPORT, &desc->flags))
251 status = -EIO;
252 else if (sysfs_streq(buf, "high"))
253 status = gpio_direction_output(gpio, 1);
254 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
255 status = gpio_direction_output(gpio, 0);
256 else if (sysfs_streq(buf, "in"))
257 status = gpio_direction_input(gpio);
258 else
259 status = -EINVAL;
260
261 mutex_unlock(&sysfs_lock);
262 return status ? : size;
263}
264
Jani Nikula07697462009-12-15 16:46:20 -0800265static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d2008-07-25 01:46:07 -0700266 gpio_direction_show, gpio_direction_store);
267
268static ssize_t gpio_value_show(struct device *dev,
269 struct device_attribute *attr, char *buf)
270{
271 const struct gpio_desc *desc = dev_get_drvdata(dev);
272 unsigned gpio = desc - gpio_desc;
273 ssize_t status;
274
275 mutex_lock(&sysfs_lock);
276
Jani Nikula07697462009-12-15 16:46:20 -0800277 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700278 status = -EIO;
Jani Nikula07697462009-12-15 16:46:20 -0800279 } else {
280 int value;
281
282 value = !!gpio_get_value_cansleep(gpio);
283 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
284 value = !value;
285
286 status = sprintf(buf, "%d\n", value);
287 }
David Brownelld8f388d2008-07-25 01:46:07 -0700288
289 mutex_unlock(&sysfs_lock);
290 return status;
291}
292
293static ssize_t gpio_value_store(struct device *dev,
294 struct device_attribute *attr, const char *buf, size_t size)
295{
296 const struct gpio_desc *desc = dev_get_drvdata(dev);
297 unsigned gpio = desc - gpio_desc;
298 ssize_t status;
299
300 mutex_lock(&sysfs_lock);
301
302 if (!test_bit(FLAG_EXPORT, &desc->flags))
303 status = -EIO;
304 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
305 status = -EPERM;
306 else {
307 long value;
308
309 status = strict_strtol(buf, 0, &value);
310 if (status == 0) {
Jani Nikula07697462009-12-15 16:46:20 -0800311 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
312 value = !value;
David Brownelld8f388d2008-07-25 01:46:07 -0700313 gpio_set_value_cansleep(gpio, value != 0);
314 status = size;
315 }
316 }
317
318 mutex_unlock(&sysfs_lock);
319 return status;
320}
321
Jani Nikula07697462009-12-15 16:46:20 -0800322static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d2008-07-25 01:46:07 -0700323 gpio_value_show, gpio_value_store);
324
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700325static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
326{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700327 struct sysfs_dirent *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700328
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700329 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700330 return IRQ_HANDLED;
331}
332
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700333static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
334 unsigned long gpio_flags)
335{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700336 struct sysfs_dirent *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700337 unsigned long irq_flags;
338 int ret, irq, id;
339
340 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
341 return 0;
342
343 irq = gpio_to_irq(desc - gpio_desc);
344 if (irq < 0)
345 return -EIO;
346
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700347 id = desc->flags >> ID_SHIFT;
348 value_sd = idr_find(&dirent_idr, id);
349 if (value_sd)
350 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700351
352 desc->flags &= ~GPIO_TRIGGER_MASK;
353
354 if (!gpio_flags) {
355 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700356 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700357 }
358
359 irq_flags = IRQF_SHARED;
360 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800361 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
362 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700363 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800364 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
365 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700366
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700367 if (!value_sd) {
368 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
369 if (!value_sd) {
370 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700371 goto err_out;
372 }
373
374 do {
375 ret = -ENOMEM;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700376 if (idr_pre_get(&dirent_idr, GFP_KERNEL))
377 ret = idr_get_new_above(&dirent_idr, value_sd,
378 1, &id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700379 } while (ret == -EAGAIN);
380
381 if (ret)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700382 goto free_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700383
384 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700385 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700386
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700387 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700388 ret = -ERANGE;
389 goto free_id;
390 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700391 }
392
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700393 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700394 "gpiolib", value_sd);
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700395 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700396 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700397
398 desc->flags |= gpio_flags;
399 return 0;
400
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700401free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700402 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700403 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700404free_sd:
405 if (value_sd)
406 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700407err_out:
408 return ret;
409}
410
411static const struct {
412 const char *name;
413 unsigned long flags;
414} trigger_types[] = {
415 { "none", 0 },
416 { "falling", BIT(FLAG_TRIG_FALL) },
417 { "rising", BIT(FLAG_TRIG_RISE) },
418 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
419};
420
421static ssize_t gpio_edge_show(struct device *dev,
422 struct device_attribute *attr, char *buf)
423{
424 const struct gpio_desc *desc = dev_get_drvdata(dev);
425 ssize_t status;
426
427 mutex_lock(&sysfs_lock);
428
429 if (!test_bit(FLAG_EXPORT, &desc->flags))
430 status = -EIO;
431 else {
432 int i;
433
434 status = 0;
435 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
436 if ((desc->flags & GPIO_TRIGGER_MASK)
437 == trigger_types[i].flags) {
438 status = sprintf(buf, "%s\n",
439 trigger_types[i].name);
440 break;
441 }
442 }
443
444 mutex_unlock(&sysfs_lock);
445 return status;
446}
447
448static ssize_t gpio_edge_store(struct device *dev,
449 struct device_attribute *attr, const char *buf, size_t size)
450{
451 struct gpio_desc *desc = dev_get_drvdata(dev);
452 ssize_t status;
453 int i;
454
455 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
456 if (sysfs_streq(trigger_types[i].name, buf))
457 goto found;
458 return -EINVAL;
459
460found:
461 mutex_lock(&sysfs_lock);
462
463 if (!test_bit(FLAG_EXPORT, &desc->flags))
464 status = -EIO;
465 else {
466 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
467 if (!status)
468 status = size;
469 }
470
471 mutex_unlock(&sysfs_lock);
472
473 return status;
474}
475
476static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
477
Jani Nikula07697462009-12-15 16:46:20 -0800478static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
479 int value)
480{
481 int status = 0;
482
483 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
484 return 0;
485
486 if (value)
487 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
488 else
489 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
490
491 /* reconfigure poll(2) support if enabled on one edge only */
492 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
493 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
494 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
495
496 gpio_setup_irq(desc, dev, 0);
497 status = gpio_setup_irq(desc, dev, trigger_flags);
498 }
499
500 return status;
501}
502
503static ssize_t gpio_active_low_show(struct device *dev,
504 struct device_attribute *attr, char *buf)
505{
506 const struct gpio_desc *desc = dev_get_drvdata(dev);
507 ssize_t status;
508
509 mutex_lock(&sysfs_lock);
510
511 if (!test_bit(FLAG_EXPORT, &desc->flags))
512 status = -EIO;
513 else
514 status = sprintf(buf, "%d\n",
515 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
516
517 mutex_unlock(&sysfs_lock);
518
519 return status;
520}
521
522static ssize_t gpio_active_low_store(struct device *dev,
523 struct device_attribute *attr, const char *buf, size_t size)
524{
525 struct gpio_desc *desc = dev_get_drvdata(dev);
526 ssize_t status;
527
528 mutex_lock(&sysfs_lock);
529
530 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
531 status = -EIO;
532 } else {
533 long value;
534
535 status = strict_strtol(buf, 0, &value);
536 if (status == 0)
537 status = sysfs_set_active_low(desc, dev, value != 0);
538 }
539
540 mutex_unlock(&sysfs_lock);
541
542 return status ? : size;
543}
544
545static const DEVICE_ATTR(active_low, 0644,
546 gpio_active_low_show, gpio_active_low_store);
547
David Brownelld8f388d2008-07-25 01:46:07 -0700548static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d2008-07-25 01:46:07 -0700549 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800550 &dev_attr_active_low.attr,
David Brownelld8f388d2008-07-25 01:46:07 -0700551 NULL,
552};
553
554static const struct attribute_group gpio_attr_group = {
555 .attrs = (struct attribute **) gpio_attrs,
556};
557
558/*
559 * /sys/class/gpio/gpiochipN/
560 * /base ... matching gpio_chip.base (N)
561 * /label ... matching gpio_chip.label
562 * /ngpio ... matching gpio_chip.ngpio
563 */
564
565static ssize_t chip_base_show(struct device *dev,
566 struct device_attribute *attr, char *buf)
567{
568 const struct gpio_chip *chip = dev_get_drvdata(dev);
569
570 return sprintf(buf, "%d\n", chip->base);
571}
572static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
573
574static ssize_t chip_label_show(struct device *dev,
575 struct device_attribute *attr, char *buf)
576{
577 const struct gpio_chip *chip = dev_get_drvdata(dev);
578
579 return sprintf(buf, "%s\n", chip->label ? : "");
580}
581static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
582
583static ssize_t chip_ngpio_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
585{
586 const struct gpio_chip *chip = dev_get_drvdata(dev);
587
588 return sprintf(buf, "%u\n", chip->ngpio);
589}
590static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
591
592static const struct attribute *gpiochip_attrs[] = {
593 &dev_attr_base.attr,
594 &dev_attr_label.attr,
595 &dev_attr_ngpio.attr,
596 NULL,
597};
598
599static const struct attribute_group gpiochip_attr_group = {
600 .attrs = (struct attribute **) gpiochip_attrs,
601};
602
603/*
604 * /sys/class/gpio/export ... write-only
605 * integer N ... number of GPIO to export (full access)
606 * /sys/class/gpio/unexport ... write-only
607 * integer N ... number of GPIO to unexport
608 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100609static ssize_t export_store(struct class *class,
610 struct class_attribute *attr,
611 const char *buf, size_t len)
David Brownelld8f388d2008-07-25 01:46:07 -0700612{
613 long gpio;
614 int status;
615
616 status = strict_strtol(buf, 0, &gpio);
617 if (status < 0)
618 goto done;
619
620 /* No extra locking here; FLAG_SYSFS just signifies that the
621 * request and export were done by on behalf of userspace, so
622 * they may be undone on its behalf too.
623 */
624
625 status = gpio_request(gpio, "sysfs");
Mathias Nymanad2fab32012-10-25 14:03:03 +0300626 if (status < 0) {
627 if (status == -EPROBE_DEFER)
628 status = -ENODEV;
David Brownelld8f388d2008-07-25 01:46:07 -0700629 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +0300630 }
David Brownelld8f388d2008-07-25 01:46:07 -0700631 status = gpio_export(gpio, true);
632 if (status < 0)
633 gpio_free(gpio);
634 else
635 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
636
637done:
638 if (status)
639 pr_debug("%s: status %d\n", __func__, status);
640 return status ? : len;
641}
642
Andi Kleen28812fe2010-01-05 12:48:07 +0100643static ssize_t unexport_store(struct class *class,
644 struct class_attribute *attr,
645 const char *buf, size_t len)
David Brownelld8f388d2008-07-25 01:46:07 -0700646{
647 long gpio;
648 int status;
649
650 status = strict_strtol(buf, 0, &gpio);
651 if (status < 0)
652 goto done;
653
654 status = -EINVAL;
655
656 /* reject bogus commands (gpio_unexport ignores them) */
657 if (!gpio_is_valid(gpio))
658 goto done;
659
660 /* No extra locking here; FLAG_SYSFS just signifies that the
661 * request and export were done by on behalf of userspace, so
662 * they may be undone on its behalf too.
663 */
664 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
665 status = 0;
666 gpio_free(gpio);
667 }
668done:
669 if (status)
670 pr_debug("%s: status %d\n", __func__, status);
671 return status ? : len;
672}
673
674static struct class_attribute gpio_class_attrs[] = {
675 __ATTR(export, 0200, NULL, export_store),
676 __ATTR(unexport, 0200, NULL, unexport_store),
677 __ATTR_NULL,
678};
679
680static struct class gpio_class = {
681 .name = "gpio",
682 .owner = THIS_MODULE,
683
684 .class_attrs = gpio_class_attrs,
685};
686
687
688/**
689 * gpio_export - export a GPIO through sysfs
690 * @gpio: gpio to make available, already requested
691 * @direction_may_change: true if userspace may change gpio direction
692 * Context: arch_initcall or later
693 *
694 * When drivers want to make a GPIO accessible to userspace after they
695 * have requested it -- perhaps while debugging, or as part of their
696 * public interface -- they may use this routine. If the GPIO can
697 * change direction (some can't) and the caller allows it, userspace
698 * will see "direction" sysfs attribute which may be used to change
699 * the gpio's direction. A "value" attribute will always be provided.
700 *
701 * Returns zero on success, else an error.
702 */
703int gpio_export(unsigned gpio, bool direction_may_change)
704{
705 unsigned long flags;
706 struct gpio_desc *desc;
707 int status = -EINVAL;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700708 const char *ioname = NULL;
David Brownelld8f388d2008-07-25 01:46:07 -0700709
710 /* can't export until sysfs is available ... */
711 if (!gpio_class.p) {
712 pr_debug("%s: called too early!\n", __func__);
713 return -ENOENT;
714 }
715
716 if (!gpio_is_valid(gpio))
717 goto done;
718
719 mutex_lock(&sysfs_lock);
720
721 spin_lock_irqsave(&gpio_lock, flags);
722 desc = &gpio_desc[gpio];
723 if (test_bit(FLAG_REQUESTED, &desc->flags)
724 && !test_bit(FLAG_EXPORT, &desc->flags)) {
725 status = 0;
726 if (!desc->chip->direction_input
727 || !desc->chip->direction_output)
728 direction_may_change = false;
729 }
730 spin_unlock_irqrestore(&gpio_lock, flags);
731
Daniel Silverstone926b6632009-04-02 16:57:05 -0700732 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
733 ioname = desc->chip->names[gpio - desc->chip->base];
734
David Brownelld8f388d2008-07-25 01:46:07 -0700735 if (status == 0) {
736 struct device *dev;
737
738 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Uwe Kleine-König7cfe1392010-05-26 14:42:18 -0700739 desc, ioname ? ioname : "gpio%u", gpio);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800740 if (!IS_ERR(dev)) {
Jani Nikula07697462009-12-15 16:46:20 -0800741 status = sysfs_create_group(&dev->kobj,
David Brownelld8f388d2008-07-25 01:46:07 -0700742 &gpio_attr_group);
Jani Nikula07697462009-12-15 16:46:20 -0800743
744 if (!status && direction_may_change)
David Brownelld8f388d2008-07-25 01:46:07 -0700745 status = device_create_file(dev,
Jani Nikula07697462009-12-15 16:46:20 -0800746 &dev_attr_direction);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700747
748 if (!status && gpio_to_irq(gpio) >= 0
749 && (direction_may_change
750 || !test_bit(FLAG_IS_OUT,
751 &desc->flags)))
752 status = device_create_file(dev,
753 &dev_attr_edge);
754
David Brownelld8f388d2008-07-25 01:46:07 -0700755 if (status != 0)
756 device_unregister(dev);
757 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800758 status = PTR_ERR(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700759 if (status == 0)
760 set_bit(FLAG_EXPORT, &desc->flags);
761 }
762
763 mutex_unlock(&sysfs_lock);
764
765done:
766 if (status)
767 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
768
769 return status;
770}
771EXPORT_SYMBOL_GPL(gpio_export);
772
773static int match_export(struct device *dev, void *data)
774{
775 return dev_get_drvdata(dev) == data;
776}
777
778/**
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700779 * gpio_export_link - create a sysfs link to an exported GPIO node
780 * @dev: device under which to create symlink
781 * @name: name of the symlink
782 * @gpio: gpio to create symlink to, already exported
783 *
784 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
785 * node. Caller is responsible for unlinking.
786 *
787 * Returns zero on success, else an error.
788 */
789int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
790{
791 struct gpio_desc *desc;
792 int status = -EINVAL;
793
794 if (!gpio_is_valid(gpio))
795 goto done;
796
797 mutex_lock(&sysfs_lock);
798
799 desc = &gpio_desc[gpio];
800
801 if (test_bit(FLAG_EXPORT, &desc->flags)) {
802 struct device *tdev;
803
804 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
805 if (tdev != NULL) {
806 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
807 name);
808 } else {
809 status = -ENODEV;
810 }
811 }
812
813 mutex_unlock(&sysfs_lock);
814
815done:
816 if (status)
817 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
818
819 return status;
820}
821EXPORT_SYMBOL_GPL(gpio_export_link);
822
Jani Nikula07697462009-12-15 16:46:20 -0800823
824/**
825 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
826 * @gpio: gpio to change
827 * @value: non-zero to use active low, i.e. inverted values
828 *
829 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
830 * The GPIO does not have to be exported yet. If poll(2) support has
831 * been enabled for either rising or falling edge, it will be
832 * reconfigured to follow the new polarity.
833 *
834 * Returns zero on success, else an error.
835 */
836int gpio_sysfs_set_active_low(unsigned gpio, int value)
837{
838 struct gpio_desc *desc;
839 struct device *dev = NULL;
840 int status = -EINVAL;
841
842 if (!gpio_is_valid(gpio))
843 goto done;
844
845 mutex_lock(&sysfs_lock);
846
847 desc = &gpio_desc[gpio];
848
849 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800850 dev = class_find_device(&gpio_class, NULL, desc, match_export);
851 if (dev == NULL) {
852 status = -ENODEV;
853 goto unlock;
854 }
855 }
856
857 status = sysfs_set_active_low(desc, dev, value);
858
859unlock:
860 mutex_unlock(&sysfs_lock);
861
862done:
863 if (status)
864 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
865
866 return status;
867}
868EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
869
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700870/**
David Brownelld8f388d2008-07-25 01:46:07 -0700871 * gpio_unexport - reverse effect of gpio_export()
872 * @gpio: gpio to make unavailable
873 *
874 * This is implicit on gpio_free().
875 */
876void gpio_unexport(unsigned gpio)
877{
878 struct gpio_desc *desc;
Jon Povey6a99ad42010-07-27 13:18:06 -0700879 int status = 0;
Ming Lei864533c2012-02-13 22:53:20 +0800880 struct device *dev = NULL;
David Brownelld8f388d2008-07-25 01:46:07 -0700881
Jon Povey6a99ad42010-07-27 13:18:06 -0700882 if (!gpio_is_valid(gpio)) {
883 status = -EINVAL;
David Brownelld8f388d2008-07-25 01:46:07 -0700884 goto done;
Jon Povey6a99ad42010-07-27 13:18:06 -0700885 }
David Brownelld8f388d2008-07-25 01:46:07 -0700886
887 mutex_lock(&sysfs_lock);
888
889 desc = &gpio_desc[gpio];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700890
David Brownelld8f388d2008-07-25 01:46:07 -0700891 if (test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700892
893 dev = class_find_device(&gpio_class, NULL, desc, match_export);
894 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700895 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d2008-07-25 01:46:07 -0700896 clear_bit(FLAG_EXPORT, &desc->flags);
David Brownelld8f388d2008-07-25 01:46:07 -0700897 } else
898 status = -ENODEV;
899 }
900
901 mutex_unlock(&sysfs_lock);
Ming Lei864533c2012-02-13 22:53:20 +0800902 if (dev) {
903 device_unregister(dev);
904 put_device(dev);
905 }
David Brownelld8f388d2008-07-25 01:46:07 -0700906done:
907 if (status)
908 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
909}
910EXPORT_SYMBOL_GPL(gpio_unexport);
911
912static int gpiochip_export(struct gpio_chip *chip)
913{
914 int status;
915 struct device *dev;
916
917 /* Many systems register gpio chips for SOC support very early,
918 * before driver model support is available. In those cases we
919 * export this later, in gpiolib_sysfs_init() ... here we just
920 * verify that _some_ field of gpio_class got initialized.
921 */
922 if (!gpio_class.p)
923 return 0;
924
925 /* use chip->base for the ID; it's already known to be unique */
926 mutex_lock(&sysfs_lock);
927 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
928 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800929 if (!IS_ERR(dev)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700930 status = sysfs_create_group(&dev->kobj,
931 &gpiochip_attr_group);
932 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800933 status = PTR_ERR(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700934 chip->exported = (status == 0);
935 mutex_unlock(&sysfs_lock);
936
937 if (status) {
938 unsigned long flags;
939 unsigned gpio;
940
941 spin_lock_irqsave(&gpio_lock, flags);
942 gpio = chip->base;
943 while (gpio_desc[gpio].chip == chip)
944 gpio_desc[gpio++].chip = NULL;
945 spin_unlock_irqrestore(&gpio_lock, flags);
946
947 pr_debug("%s: chip %s status %d\n", __func__,
948 chip->label, status);
949 }
950
951 return status;
952}
953
954static void gpiochip_unexport(struct gpio_chip *chip)
955{
956 int status;
957 struct device *dev;
958
959 mutex_lock(&sysfs_lock);
960 dev = class_find_device(&gpio_class, NULL, chip, match_export);
961 if (dev) {
962 put_device(dev);
963 device_unregister(dev);
964 chip->exported = 0;
965 status = 0;
966 } else
967 status = -ENODEV;
968 mutex_unlock(&sysfs_lock);
969
970 if (status)
971 pr_debug("%s: chip %s status %d\n", __func__,
972 chip->label, status);
973}
974
975static int __init gpiolib_sysfs_init(void)
976{
977 int status;
978 unsigned long flags;
979 unsigned gpio;
980
981 status = class_register(&gpio_class);
982 if (status < 0)
983 return status;
984
985 /* Scan and register the gpio_chips which registered very
986 * early (e.g. before the class_register above was called).
987 *
988 * We run before arch_initcall() so chip->dev nodes can have
989 * registered, and so arch_initcall() can always gpio_export().
990 */
991 spin_lock_irqsave(&gpio_lock, flags);
992 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
993 struct gpio_chip *chip;
994
995 chip = gpio_desc[gpio].chip;
996 if (!chip || chip->exported)
997 continue;
998
999 spin_unlock_irqrestore(&gpio_lock, flags);
1000 status = gpiochip_export(chip);
1001 spin_lock_irqsave(&gpio_lock, flags);
1002 }
1003 spin_unlock_irqrestore(&gpio_lock, flags);
1004
1005
1006 return status;
1007}
1008postcore_initcall(gpiolib_sysfs_init);
1009
1010#else
1011static inline int gpiochip_export(struct gpio_chip *chip)
1012{
1013 return 0;
1014}
1015
1016static inline void gpiochip_unexport(struct gpio_chip *chip)
1017{
1018}
1019
1020#endif /* CONFIG_GPIO_SYSFS */
1021
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001022/**
David Brownelld2876d02008-02-04 22:28:20 -08001023 * gpiochip_add() - register a gpio_chip
1024 * @chip: the chip to register, with chip->base initialized
1025 * Context: potentially before irqs or kmalloc will work
1026 *
1027 * Returns a negative errno if the chip can't be registered, such as
1028 * because the chip->base is invalid or already associated with a
1029 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001030 *
David Brownelld8f388d2008-07-25 01:46:07 -07001031 * When gpiochip_add() is called very early during boot, so that GPIOs
1032 * can be freely used, the chip->dev device must be registered before
1033 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1034 * for GPIOs will fail rudely.
1035 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001036 * If chip->base is negative, this requests dynamic assignment of
1037 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001038 */
1039int gpiochip_add(struct gpio_chip *chip)
1040{
1041 unsigned long flags;
1042 int status = 0;
1043 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001044 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001045
Trent Piephobff5fda2008-05-23 13:04:44 -07001046 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001047 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001048 status = -EINVAL;
1049 goto fail;
1050 }
1051
1052 spin_lock_irqsave(&gpio_lock, flags);
1053
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001054 if (base < 0) {
1055 base = gpiochip_find_base(chip->ngpio);
1056 if (base < 0) {
1057 status = base;
David Brownelld8f388d2008-07-25 01:46:07 -07001058 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001059 }
1060 chip->base = base;
1061 }
1062
David Brownelld2876d02008-02-04 22:28:20 -08001063 /* these GPIO numbers must not be managed by another gpio_chip */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001064 for (id = base; id < base + chip->ngpio; id++) {
David Brownelld2876d02008-02-04 22:28:20 -08001065 if (gpio_desc[id].chip != NULL) {
1066 status = -EBUSY;
1067 break;
1068 }
1069 }
1070 if (status == 0) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001071 for (id = base; id < base + chip->ngpio; id++) {
David Brownelld2876d02008-02-04 22:28:20 -08001072 gpio_desc[id].chip = chip;
David Brownelld8f388d2008-07-25 01:46:07 -07001073
1074 /* REVISIT: most hardware initializes GPIOs as
1075 * inputs (often with pullups enabled) so power
1076 * usage is minimized. Linux code should set the
1077 * gpio direction first thing; but until it does,
1078 * we may expose the wrong direction in sysfs.
1079 */
1080 gpio_desc[id].flags = !chip->direction_input
1081 ? (1 << FLAG_IS_OUT)
1082 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001083 }
1084 }
1085
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301086#ifdef CONFIG_PINCTRL
1087 INIT_LIST_HEAD(&chip->pin_ranges);
1088#endif
1089
Anton Vorontsov391c9702010-06-08 07:48:17 -06001090 of_gpiochip_add(chip);
1091
David Brownelld8f388d2008-07-25 01:46:07 -07001092unlock:
David Brownelld2876d02008-02-04 22:28:20 -08001093 spin_unlock_irqrestore(&gpio_lock, flags);
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001094
1095 if (status)
1096 goto fail;
1097
1098 status = gpiochip_export(chip);
1099 if (status)
1100 goto fail;
1101
H Hartley Sweetenee1c1e72012-05-11 16:58:33 -07001102 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
Grant Likely64842aa2011-11-06 11:36:18 -07001103 chip->base, chip->base + chip->ngpio - 1,
1104 chip->label ? : "generic");
1105
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001106 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001107fail:
1108 /* failures here can mean systems won't boot... */
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001109 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1110 chip->base, chip->base + chip->ngpio - 1,
1111 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001112 return status;
1113}
1114EXPORT_SYMBOL_GPL(gpiochip_add);
1115
1116/**
1117 * gpiochip_remove() - unregister a gpio_chip
1118 * @chip: the chip to unregister
1119 *
1120 * A gpio_chip with any GPIOs still requested may not be removed.
1121 */
1122int gpiochip_remove(struct gpio_chip *chip)
1123{
1124 unsigned long flags;
1125 int status = 0;
1126 unsigned id;
1127
1128 spin_lock_irqsave(&gpio_lock, flags);
1129
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001130 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001131 of_gpiochip_remove(chip);
1132
David Brownelld2876d02008-02-04 22:28:20 -08001133 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1134 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1135 status = -EBUSY;
1136 break;
1137 }
1138 }
1139 if (status == 0) {
1140 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1141 gpio_desc[id].chip = NULL;
1142 }
1143
1144 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d2008-07-25 01:46:07 -07001145
1146 if (status == 0)
1147 gpiochip_unexport(chip);
1148
David Brownelld2876d02008-02-04 22:28:20 -08001149 return status;
1150}
1151EXPORT_SYMBOL_GPL(gpiochip_remove);
1152
Grant Likely594fa262010-06-08 07:48:16 -06001153/**
1154 * gpiochip_find() - iterator for locating a specific gpio_chip
1155 * @data: data to pass to match function
1156 * @callback: Callback function to check gpio_chip
1157 *
1158 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1159 * determined by a user supplied @match callback. The callback should return
1160 * 0 if the device doesn't match and non-zero if it does. If the callback is
1161 * non-zero, this function will return to the caller and not iterate over any
1162 * more gpio_chips.
1163 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001164struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001165 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001166 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001167{
1168 struct gpio_chip *chip = NULL;
1169 unsigned long flags;
1170 int i;
1171
1172 spin_lock_irqsave(&gpio_lock, flags);
1173 for (i = 0; i < ARCH_NR_GPIOS; i++) {
1174 if (!gpio_desc[i].chip)
1175 continue;
1176
1177 if (match(gpio_desc[i].chip, data)) {
1178 chip = gpio_desc[i].chip;
1179 break;
1180 }
1181 }
1182 spin_unlock_irqrestore(&gpio_lock, flags);
1183
1184 return chip;
1185}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001186EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001187
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301188#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001189
Linus Walleij3f0f8672012-11-20 12:40:15 +01001190/**
1191 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1192 * @chip: the gpiochip to add the range for
1193 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001194 * @gpio_offset: the start offset in the current gpio_chip number space
1195 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001196 * @npins: the number of pins from the offset of each pin space (GPIO and
1197 * pin controller) to accumulate in this range
1198 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001199int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001200 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001201 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301202{
1203 struct gpio_pin_range *pin_range;
1204
Linus Walleij3f0f8672012-11-20 12:40:15 +01001205 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301206 if (!pin_range) {
1207 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1208 chip->label);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001209 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301210 }
1211
Linus Walleij3f0f8672012-11-20 12:40:15 +01001212 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001213 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001214 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301215 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001216 pin_range->range.base = chip->base + gpio_offset;
1217 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301218 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001219 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301220 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001221 if (IS_ERR(pin_range->pctldev)) {
Linus Walleij3f0f8672012-11-20 12:40:15 +01001222 pr_err("%s: GPIO chip: could not create pin range\n",
1223 chip->label);
1224 kfree(pin_range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001225 return PTR_ERR(pin_range->pctldev);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001226 }
Linus Walleij316511c2012-11-21 08:48:09 +01001227 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1228 chip->label, gpio_offset, gpio_offset + npins - 1,
1229 pinctl_name,
1230 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301231
1232 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001233
1234 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301235}
Linus Walleij165adc92012-11-06 14:49:39 +01001236EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301237
Linus Walleij3f0f8672012-11-20 12:40:15 +01001238/**
1239 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1240 * @chip: the chip to remove all the mappings for
1241 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301242void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1243{
1244 struct gpio_pin_range *pin_range, *tmp;
1245
1246 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1247 list_del(&pin_range->node);
1248 pinctrl_remove_gpio_range(pin_range->pctldev,
1249 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001250 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301251 }
1252}
Linus Walleij165adc92012-11-06 14:49:39 +01001253EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1254
1255#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301256
David Brownelld2876d02008-02-04 22:28:20 -08001257/* These "optional" allocation calls help prevent drivers from stomping
1258 * on each other, and help provide better diagnostics in debugfs.
1259 * They're called even less than the "set direction" calls.
1260 */
1261int gpio_request(unsigned gpio, const char *label)
1262{
1263 struct gpio_desc *desc;
David Brownell35e8bb52008-10-15 22:03:16 -07001264 struct gpio_chip *chip;
Mark Browne9354572012-07-09 12:22:56 +01001265 int status = -EPROBE_DEFER;
David Brownelld2876d02008-02-04 22:28:20 -08001266 unsigned long flags;
1267
1268 spin_lock_irqsave(&gpio_lock, flags);
1269
Mathias Nymanad2fab32012-10-25 14:03:03 +03001270 if (!gpio_is_valid(gpio)) {
1271 status = -EINVAL;
David Brownelld2876d02008-02-04 22:28:20 -08001272 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +03001273 }
David Brownelld2876d02008-02-04 22:28:20 -08001274 desc = &gpio_desc[gpio];
David Brownell35e8bb52008-10-15 22:03:16 -07001275 chip = desc->chip;
1276 if (chip == NULL)
David Brownelld2876d02008-02-04 22:28:20 -08001277 goto done;
1278
David Brownell35e8bb52008-10-15 22:03:16 -07001279 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001280 goto done;
1281
David Brownelld2876d02008-02-04 22:28:20 -08001282 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001283 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001284 */
1285
1286 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1287 desc_set_label(desc, label ? : "?");
1288 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001289 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001290 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001291 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001292 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001293 }
1294
1295 if (chip->request) {
1296 /* chip->request may sleep */
1297 spin_unlock_irqrestore(&gpio_lock, flags);
1298 status = chip->request(chip, gpio - chip->base);
1299 spin_lock_irqsave(&gpio_lock, flags);
1300
1301 if (status < 0) {
1302 desc_set_label(desc, NULL);
1303 module_put(chip->owner);
1304 clear_bit(FLAG_REQUESTED, &desc->flags);
1305 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001306 }
David Brownelld2876d02008-02-04 22:28:20 -08001307
1308done:
1309 if (status)
1310 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1311 gpio, label ? : "?", status);
1312 spin_unlock_irqrestore(&gpio_lock, flags);
1313 return status;
1314}
1315EXPORT_SYMBOL_GPL(gpio_request);
1316
1317void gpio_free(unsigned gpio)
1318{
1319 unsigned long flags;
1320 struct gpio_desc *desc;
David Brownell35e8bb52008-10-15 22:03:16 -07001321 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001322
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001323 might_sleep();
1324
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001325 if (!gpio_is_valid(gpio)) {
David Brownelld2876d02008-02-04 22:28:20 -08001326 WARN_ON(extra_checks);
1327 return;
1328 }
1329
David Brownelld8f388d2008-07-25 01:46:07 -07001330 gpio_unexport(gpio);
1331
David Brownelld2876d02008-02-04 22:28:20 -08001332 spin_lock_irqsave(&gpio_lock, flags);
1333
1334 desc = &gpio_desc[gpio];
David Brownell35e8bb52008-10-15 22:03:16 -07001335 chip = desc->chip;
1336 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1337 if (chip->free) {
1338 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001339 might_sleep_if(chip->can_sleep);
David Brownell35e8bb52008-10-15 22:03:16 -07001340 chip->free(chip, gpio - chip->base);
1341 spin_lock_irqsave(&gpio_lock, flags);
1342 }
David Brownelld2876d02008-02-04 22:28:20 -08001343 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001344 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001345 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001346 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301347 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301348 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001349 } else
David Brownelld2876d02008-02-04 22:28:20 -08001350 WARN_ON(extra_checks);
1351
1352 spin_unlock_irqrestore(&gpio_lock, flags);
1353}
1354EXPORT_SYMBOL_GPL(gpio_free);
1355
Eric Miao3e45f1d2010-03-05 13:44:35 -08001356/**
1357 * gpio_request_one - request a single GPIO with initial configuration
1358 * @gpio: the GPIO number
1359 * @flags: GPIO configuration as specified by GPIOF_*
1360 * @label: a literal description string of this GPIO
1361 */
1362int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1363{
1364 int err;
1365
1366 err = gpio_request(gpio, label);
1367 if (err)
1368 return err;
1369
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301370 if (flags & GPIOF_OPEN_DRAIN)
1371 set_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags);
1372
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301373 if (flags & GPIOF_OPEN_SOURCE)
1374 set_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags);
1375
Eric Miao3e45f1d2010-03-05 13:44:35 -08001376 if (flags & GPIOF_DIR_IN)
1377 err = gpio_direction_input(gpio);
1378 else
1379 err = gpio_direction_output(gpio,
1380 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1381
Aaro Koskinene2548112010-12-21 17:24:22 -08001382 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001383 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -08001384
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001385 if (flags & GPIOF_EXPORT) {
1386 err = gpio_export(gpio, flags & GPIOF_EXPORT_CHANGEABLE);
1387 if (err)
1388 goto free_gpio;
1389 }
1390
1391 return 0;
1392
1393 free_gpio:
1394 gpio_free(gpio);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001395 return err;
1396}
1397EXPORT_SYMBOL_GPL(gpio_request_one);
1398
1399/**
1400 * gpio_request_array - request multiple GPIOs in a single call
1401 * @array: array of the 'struct gpio'
1402 * @num: how many GPIOs in the array
1403 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001404int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001405{
1406 int i, err;
1407
1408 for (i = 0; i < num; i++, array++) {
1409 err = gpio_request_one(array->gpio, array->flags, array->label);
1410 if (err)
1411 goto err_free;
1412 }
1413 return 0;
1414
1415err_free:
1416 while (i--)
1417 gpio_free((--array)->gpio);
1418 return err;
1419}
1420EXPORT_SYMBOL_GPL(gpio_request_array);
1421
1422/**
1423 * gpio_free_array - release multiple GPIOs in a single call
1424 * @array: array of the 'struct gpio'
1425 * @num: how many GPIOs in the array
1426 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001427void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001428{
1429 while (num--)
1430 gpio_free((array++)->gpio);
1431}
1432EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001433
1434/**
1435 * gpiochip_is_requested - return string iff signal was requested
1436 * @chip: controller managing the signal
1437 * @offset: of signal within controller's 0..(ngpio - 1) range
1438 *
1439 * Returns NULL if the GPIO is not currently requested, else a string.
1440 * If debugfs support is enabled, the string returned is the label passed
1441 * to gpio_request(); otherwise it is a meaningless constant.
1442 *
1443 * This function is for use by GPIO controller drivers. The label can
1444 * help with diagnostics, and knowing that the signal is used as a GPIO
1445 * can help avoid accidentally multiplexing it to another controller.
1446 */
1447const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1448{
1449 unsigned gpio = chip->base + offset;
1450
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001451 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
David Brownelld2876d02008-02-04 22:28:20 -08001452 return NULL;
1453 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1454 return NULL;
1455#ifdef CONFIG_DEBUG_FS
1456 return gpio_desc[gpio].label;
1457#else
1458 return "?";
1459#endif
1460}
1461EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1462
1463
1464/* Drivers MUST set GPIO direction before making get/set calls. In
1465 * some cases this is done in early boot, before IRQs are enabled.
1466 *
1467 * As a rule these aren't called more than once (except for drivers
1468 * using the open-drain emulation idiom) so these are natural places
1469 * to accumulate extra debugging checks. Note that we can't (yet)
1470 * rely on gpio_request() having been called beforehand.
1471 */
1472
1473int gpio_direction_input(unsigned gpio)
1474{
1475 unsigned long flags;
1476 struct gpio_chip *chip;
1477 struct gpio_desc *desc = &gpio_desc[gpio];
1478 int status = -EINVAL;
1479
1480 spin_lock_irqsave(&gpio_lock, flags);
1481
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001482 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001483 goto fail;
1484 chip = desc->chip;
1485 if (!chip || !chip->get || !chip->direction_input)
1486 goto fail;
1487 gpio -= chip->base;
1488 if (gpio >= chip->ngpio)
1489 goto fail;
David Brownell35e8bb52008-10-15 22:03:16 -07001490 status = gpio_ensure_requested(desc, gpio);
1491 if (status < 0)
1492 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001493
1494 /* now we know the gpio is valid and chip won't vanish */
1495
1496 spin_unlock_irqrestore(&gpio_lock, flags);
1497
David Brownell9c4ba942010-08-10 18:02:24 -07001498 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001499
David Brownell35e8bb52008-10-15 22:03:16 -07001500 if (status) {
1501 status = chip->request(chip, gpio);
1502 if (status < 0) {
1503 pr_debug("GPIO-%d: chip request fail, %d\n",
1504 chip->base + gpio, status);
1505 /* and it's not available to anyone else ...
1506 * gpio_request() is the fully clean solution.
1507 */
1508 goto lose;
1509 }
1510 }
1511
David Brownelld2876d02008-02-04 22:28:20 -08001512 status = chip->direction_input(chip, gpio);
1513 if (status == 0)
1514 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001515
1516 trace_gpio_direction(chip->base + gpio, 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001517lose:
David Brownelld2876d02008-02-04 22:28:20 -08001518 return status;
1519fail:
1520 spin_unlock_irqrestore(&gpio_lock, flags);
1521 if (status)
1522 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001523 __func__, gpio, status);
David Brownelld2876d02008-02-04 22:28:20 -08001524 return status;
1525}
1526EXPORT_SYMBOL_GPL(gpio_direction_input);
1527
1528int gpio_direction_output(unsigned gpio, int value)
1529{
1530 unsigned long flags;
1531 struct gpio_chip *chip;
1532 struct gpio_desc *desc = &gpio_desc[gpio];
1533 int status = -EINVAL;
1534
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301535 /* Open drain pin should not be driven to 1 */
1536 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1537 return gpio_direction_input(gpio);
1538
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301539 /* Open source pin should not be driven to 0 */
1540 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1541 return gpio_direction_input(gpio);
1542
David Brownelld2876d02008-02-04 22:28:20 -08001543 spin_lock_irqsave(&gpio_lock, flags);
1544
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001545 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001546 goto fail;
1547 chip = desc->chip;
1548 if (!chip || !chip->set || !chip->direction_output)
1549 goto fail;
1550 gpio -= chip->base;
1551 if (gpio >= chip->ngpio)
1552 goto fail;
David Brownell35e8bb52008-10-15 22:03:16 -07001553 status = gpio_ensure_requested(desc, gpio);
1554 if (status < 0)
1555 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001556
1557 /* now we know the gpio is valid and chip won't vanish */
1558
1559 spin_unlock_irqrestore(&gpio_lock, flags);
1560
David Brownell9c4ba942010-08-10 18:02:24 -07001561 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001562
David Brownell35e8bb52008-10-15 22:03:16 -07001563 if (status) {
1564 status = chip->request(chip, gpio);
1565 if (status < 0) {
1566 pr_debug("GPIO-%d: chip request fail, %d\n",
1567 chip->base + gpio, status);
1568 /* and it's not available to anyone else ...
1569 * gpio_request() is the fully clean solution.
1570 */
1571 goto lose;
1572 }
1573 }
1574
David Brownelld2876d02008-02-04 22:28:20 -08001575 status = chip->direction_output(chip, gpio, value);
1576 if (status == 0)
1577 set_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001578 trace_gpio_value(chip->base + gpio, 0, value);
1579 trace_gpio_direction(chip->base + gpio, 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001580lose:
David Brownelld2876d02008-02-04 22:28:20 -08001581 return status;
1582fail:
1583 spin_unlock_irqrestore(&gpio_lock, flags);
1584 if (status)
1585 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001586 __func__, gpio, status);
David Brownelld2876d02008-02-04 22:28:20 -08001587 return status;
1588}
1589EXPORT_SYMBOL_GPL(gpio_direction_output);
1590
Felipe Balbic4b5be92010-05-26 14:42:23 -07001591/**
1592 * gpio_set_debounce - sets @debounce time for a @gpio
1593 * @gpio: the gpio to set debounce time
1594 * @debounce: debounce time is microseconds
1595 */
1596int gpio_set_debounce(unsigned gpio, unsigned debounce)
1597{
1598 unsigned long flags;
1599 struct gpio_chip *chip;
1600 struct gpio_desc *desc = &gpio_desc[gpio];
1601 int status = -EINVAL;
1602
1603 spin_lock_irqsave(&gpio_lock, flags);
1604
1605 if (!gpio_is_valid(gpio))
1606 goto fail;
1607 chip = desc->chip;
1608 if (!chip || !chip->set || !chip->set_debounce)
1609 goto fail;
1610 gpio -= chip->base;
1611 if (gpio >= chip->ngpio)
1612 goto fail;
1613 status = gpio_ensure_requested(desc, gpio);
1614 if (status < 0)
1615 goto fail;
1616
1617 /* now we know the gpio is valid and chip won't vanish */
1618
1619 spin_unlock_irqrestore(&gpio_lock, flags);
1620
David Brownell9c4ba942010-08-10 18:02:24 -07001621 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001622
1623 return chip->set_debounce(chip, gpio, debounce);
1624
1625fail:
1626 spin_unlock_irqrestore(&gpio_lock, flags);
1627 if (status)
1628 pr_debug("%s: gpio-%d status %d\n",
1629 __func__, gpio, status);
1630
1631 return status;
1632}
1633EXPORT_SYMBOL_GPL(gpio_set_debounce);
David Brownelld2876d02008-02-04 22:28:20 -08001634
1635/* I/O calls are only valid after configuration completed; the relevant
1636 * "is this a valid GPIO" error checks should already have been done.
1637 *
1638 * "Get" operations are often inlinable as reading a pin value register,
1639 * and masking the relevant bit in that register.
1640 *
1641 * When "set" operations are inlinable, they involve writing that mask to
1642 * one register to set a low value, or a different register to set it high.
1643 * Otherwise locking is needed, so there may be little value to inlining.
1644 *
1645 *------------------------------------------------------------------------
1646 *
1647 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1648 * have requested the GPIO. That can include implicit requesting by
1649 * a direction setting call. Marking a gpio as requested locks its chip
1650 * in memory, guaranteeing that these table lookups need no more locking
1651 * and that gpiochip_remove() will fail.
1652 *
1653 * REVISIT when debugging, consider adding some instrumentation to ensure
1654 * that the GPIO was actually requested.
1655 */
1656
1657/**
1658 * __gpio_get_value() - return a gpio's value
1659 * @gpio: gpio whose value will be returned
1660 * Context: any
1661 *
1662 * This is used directly or indirectly to implement gpio_get_value().
1663 * It returns the zero or nonzero value provided by the associated
1664 * gpio_chip.get() method; or zero if no such method is provided.
1665 */
1666int __gpio_get_value(unsigned gpio)
1667{
1668 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001669 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001670
1671 chip = gpio_to_chip(gpio);
Mark Browne4e449e2012-02-17 10:46:00 -08001672 /* Should be using gpio_get_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001673 WARN_ON(chip->can_sleep);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001674 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1675 trace_gpio_value(gpio, 1, value);
1676 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001677}
1678EXPORT_SYMBOL_GPL(__gpio_get_value);
1679
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301680/*
1681 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1682 * @gpio: Gpio whose state need to be set.
1683 * @chip: Gpio chip.
1684 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1685 */
1686static void _gpio_set_open_drain_value(unsigned gpio,
1687 struct gpio_chip *chip, int value)
1688{
1689 int err = 0;
1690 if (value) {
1691 err = chip->direction_input(chip, gpio - chip->base);
1692 if (!err)
1693 clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1694 } else {
1695 err = chip->direction_output(chip, gpio - chip->base, 0);
1696 if (!err)
1697 set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1698 }
1699 trace_gpio_direction(gpio, value, err);
1700 if (err < 0)
1701 pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
1702 __func__, gpio, err);
1703}
1704
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301705/*
1706 * _gpio_set_open_source() - Set the open source gpio's value.
1707 * @gpio: Gpio whose state need to be set.
1708 * @chip: Gpio chip.
1709 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1710 */
1711static void _gpio_set_open_source_value(unsigned gpio,
1712 struct gpio_chip *chip, int value)
1713{
1714 int err = 0;
1715 if (value) {
1716 err = chip->direction_output(chip, gpio - chip->base, 1);
1717 if (!err)
1718 set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1719 } else {
1720 err = chip->direction_input(chip, gpio - chip->base);
1721 if (!err)
1722 clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1723 }
1724 trace_gpio_direction(gpio, !value, err);
1725 if (err < 0)
1726 pr_err("%s: Error in set_value for open source gpio%d err %d\n",
1727 __func__, gpio, err);
1728}
1729
1730
David Brownelld2876d02008-02-04 22:28:20 -08001731/**
1732 * __gpio_set_value() - assign a gpio's value
1733 * @gpio: gpio whose value will be assigned
1734 * @value: value to assign
1735 * Context: any
1736 *
1737 * This is used directly or indirectly to implement gpio_set_value().
1738 * It invokes the associated gpio_chip.set() method.
1739 */
1740void __gpio_set_value(unsigned gpio, int value)
1741{
1742 struct gpio_chip *chip;
1743
1744 chip = gpio_to_chip(gpio);
Mark Browne4e449e2012-02-17 10:46:00 -08001745 /* Should be using gpio_set_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001746 WARN_ON(chip->can_sleep);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001747 trace_gpio_value(gpio, 0, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301748 if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
1749 _gpio_set_open_drain_value(gpio, chip, value);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301750 else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
1751 _gpio_set_open_source_value(gpio, chip, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301752 else
1753 chip->set(chip, gpio - chip->base, value);
David Brownelld2876d02008-02-04 22:28:20 -08001754}
1755EXPORT_SYMBOL_GPL(__gpio_set_value);
1756
1757/**
1758 * __gpio_cansleep() - report whether gpio value access will sleep
1759 * @gpio: gpio in question
1760 * Context: any
1761 *
1762 * This is used directly or indirectly to implement gpio_cansleep(). It
1763 * returns nonzero if access reading or writing the GPIO value can sleep.
1764 */
1765int __gpio_cansleep(unsigned gpio)
1766{
1767 struct gpio_chip *chip;
1768
1769 /* only call this on GPIOs that are valid! */
1770 chip = gpio_to_chip(gpio);
1771
1772 return chip->can_sleep;
1773}
1774EXPORT_SYMBOL_GPL(__gpio_cansleep);
1775
David Brownell0f6d5042008-10-15 22:03:14 -07001776/**
1777 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1778 * @gpio: gpio whose IRQ will be returned (already requested)
1779 * Context: any
1780 *
1781 * This is used directly or indirectly to implement gpio_to_irq().
1782 * It returns the number of the IRQ signaled by this (input) GPIO,
1783 * or a negative errno.
1784 */
1785int __gpio_to_irq(unsigned gpio)
1786{
1787 struct gpio_chip *chip;
1788
1789 chip = gpio_to_chip(gpio);
1790 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1791}
1792EXPORT_SYMBOL_GPL(__gpio_to_irq);
1793
David Brownelld2876d02008-02-04 22:28:20 -08001794
1795
1796/* There's no value in making it easy to inline GPIO calls that may sleep.
1797 * Common examples include ones connected to I2C or SPI chips.
1798 */
1799
1800int gpio_get_value_cansleep(unsigned gpio)
1801{
1802 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001803 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001804
1805 might_sleep_if(extra_checks);
1806 chip = gpio_to_chip(gpio);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001807 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1808 trace_gpio_value(gpio, 1, value);
1809 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001810}
1811EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1812
1813void gpio_set_value_cansleep(unsigned gpio, int value)
1814{
1815 struct gpio_chip *chip;
1816
1817 might_sleep_if(extra_checks);
1818 chip = gpio_to_chip(gpio);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001819 trace_gpio_value(gpio, 0, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301820 if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
1821 _gpio_set_open_drain_value(gpio, chip, value);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301822 else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
1823 _gpio_set_open_source_value(gpio, chip, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301824 else
1825 chip->set(chip, gpio - chip->base, value);
David Brownelld2876d02008-02-04 22:28:20 -08001826}
1827EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1828
1829
1830#ifdef CONFIG_DEBUG_FS
1831
David Brownelld2876d02008-02-04 22:28:20 -08001832static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1833{
1834 unsigned i;
1835 unsigned gpio = chip->base;
1836 struct gpio_desc *gdesc = &gpio_desc[gpio];
1837 int is_out;
1838
1839 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1840 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1841 continue;
1842
1843 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Jarkko Nikula6e8ba722008-11-19 15:36:17 -08001844 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08001845 gpio, gdesc->label,
1846 is_out ? "out" : "in ",
1847 chip->get
1848 ? (chip->get(chip, i) ? "hi" : "lo")
1849 : "? ");
David Brownelld2876d02008-02-04 22:28:20 -08001850 seq_printf(s, "\n");
1851 }
1852}
1853
Thierry Redingf9c4a312012-04-12 13:26:01 +02001854static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08001855{
Thierry Redingf9c4a312012-04-12 13:26:01 +02001856 struct gpio_chip *chip = NULL;
1857 unsigned int gpio;
1858 void *ret = NULL;
1859 loff_t index = 0;
David Brownelld2876d02008-02-04 22:28:20 -08001860
1861 /* REVISIT this isn't locked against gpio_chip removal ... */
1862
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001863 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
Thierry Redingf9c4a312012-04-12 13:26:01 +02001864 if (gpio_desc[gpio].chip == chip)
David Brownelld2876d02008-02-04 22:28:20 -08001865 continue;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001866
David Brownelld2876d02008-02-04 22:28:20 -08001867 chip = gpio_desc[gpio].chip;
1868 if (!chip)
1869 continue;
1870
Thierry Redingf9c4a312012-04-12 13:26:01 +02001871 if (index++ >= *pos) {
1872 ret = chip;
1873 break;
1874 }
David Brownelld2876d02008-02-04 22:28:20 -08001875 }
Thierry Redingf9c4a312012-04-12 13:26:01 +02001876
1877 s->private = "";
1878
1879 return ret;
1880}
1881
1882static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
1883{
1884 struct gpio_chip *chip = v;
1885 unsigned int gpio;
1886 void *ret = NULL;
1887
1888 /* skip GPIOs provided by the current chip */
1889 for (gpio = chip->base + chip->ngpio; gpio_is_valid(gpio); gpio++) {
1890 chip = gpio_desc[gpio].chip;
1891 if (chip) {
1892 ret = chip;
1893 break;
1894 }
1895 }
1896
1897 s->private = "\n";
1898 ++*pos;
1899
1900 return ret;
1901}
1902
1903static void gpiolib_seq_stop(struct seq_file *s, void *v)
1904{
1905}
1906
1907static int gpiolib_seq_show(struct seq_file *s, void *v)
1908{
1909 struct gpio_chip *chip = v;
1910 struct device *dev;
1911
1912 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
1913 chip->base, chip->base + chip->ngpio - 1);
1914 dev = chip->dev;
1915 if (dev)
1916 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
1917 dev_name(dev));
1918 if (chip->label)
1919 seq_printf(s, ", %s", chip->label);
1920 if (chip->can_sleep)
1921 seq_printf(s, ", can sleep");
1922 seq_printf(s, ":\n");
1923
1924 if (chip->dbg_show)
1925 chip->dbg_show(s, chip);
1926 else
1927 gpiolib_dbg_show(s, chip);
1928
David Brownelld2876d02008-02-04 22:28:20 -08001929 return 0;
1930}
1931
Thierry Redingf9c4a312012-04-12 13:26:01 +02001932static const struct seq_operations gpiolib_seq_ops = {
1933 .start = gpiolib_seq_start,
1934 .next = gpiolib_seq_next,
1935 .stop = gpiolib_seq_stop,
1936 .show = gpiolib_seq_show,
1937};
1938
David Brownelld2876d02008-02-04 22:28:20 -08001939static int gpiolib_open(struct inode *inode, struct file *file)
1940{
Thierry Redingf9c4a312012-04-12 13:26:01 +02001941 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08001942}
1943
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001944static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02001945 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08001946 .open = gpiolib_open,
1947 .read = seq_read,
1948 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02001949 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08001950};
1951
1952static int __init gpiolib_debugfs_init(void)
1953{
1954 /* /sys/kernel/debug/gpio */
1955 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1956 NULL, NULL, &gpiolib_operations);
1957 return 0;
1958}
1959subsys_initcall(gpiolib_debugfs_init);
1960
1961#endif /* DEBUG_FS */