blob: fd2b71c7099769f12e48be87d80f5f9714894deb [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
Anton Vorontsov169b6a72008-04-28 02:14:47 -070055#define FLAG_RESERVED 2
David Brownelld8f388d82008-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
Mathias Nyman80b0a602012-10-24 17:25:27 +0300194/* caller ensures gpio is valid and requested, chip->get_direction may sleep */
195static int gpio_get_direction(unsigned gpio)
196{
197 struct gpio_chip *chip;
198 struct gpio_desc *desc = &gpio_desc[gpio];
199 int status = -EINVAL;
200
201 chip = gpio_to_chip(gpio);
202 gpio -= chip->base;
203
204 if (!chip->get_direction)
205 return status;
206
207 status = chip->get_direction(chip, gpio);
208 if (status > 0) {
209 /* GPIOF_DIR_IN, or other positive */
210 status = 1;
211 clear_bit(FLAG_IS_OUT, &desc->flags);
212 }
213 if (status == 0) {
214 /* GPIOF_DIR_OUT */
215 set_bit(FLAG_IS_OUT, &desc->flags);
216 }
217 return status;
218}
219
David Brownelld8f388d82008-07-25 01:46:07 -0700220#ifdef CONFIG_GPIO_SYSFS
221
222/* lock protects against unexport_gpio() being called while
223 * sysfs files are active.
224 */
225static DEFINE_MUTEX(sysfs_lock);
226
227/*
228 * /sys/class/gpio/gpioN... only for GPIOs that are exported
229 * /direction
230 * * MAY BE OMITTED if kernel won't allow direction changes
231 * * is read/write as "in" or "out"
232 * * may also be written as "high" or "low", initializing
233 * output value as specified ("out" implies "low")
234 * /value
235 * * always readable, subject to hardware behavior
236 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700237 * /edge
238 * * configures behavior of poll(2) on /value
239 * * available only if pin can generate IRQs on input
240 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800241 * /active_low
242 * * configures polarity of /value
243 * * is read/write as zero/nonzero
244 * * also affects existing and subsequent "falling" and "rising"
245 * /edge configuration
David Brownelld8f388d82008-07-25 01:46:07 -0700246 */
247
248static ssize_t gpio_direction_show(struct device *dev,
249 struct device_attribute *attr, char *buf)
250{
251 const struct gpio_desc *desc = dev_get_drvdata(dev);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300252 unsigned gpio = desc - gpio_desc;
David Brownelld8f388d82008-07-25 01:46:07 -0700253 ssize_t status;
254
255 mutex_lock(&sysfs_lock);
256
257 if (!test_bit(FLAG_EXPORT, &desc->flags))
258 status = -EIO;
259 else
Mathias Nyman80b0a602012-10-24 17:25:27 +0300260 gpio_get_direction(gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700261 status = sprintf(buf, "%s\n",
262 test_bit(FLAG_IS_OUT, &desc->flags)
263 ? "out" : "in");
264
265 mutex_unlock(&sysfs_lock);
266 return status;
267}
268
269static ssize_t gpio_direction_store(struct device *dev,
270 struct device_attribute *attr, const char *buf, size_t size)
271{
272 const struct gpio_desc *desc = dev_get_drvdata(dev);
273 unsigned gpio = desc - gpio_desc;
274 ssize_t status;
275
276 mutex_lock(&sysfs_lock);
277
278 if (!test_bit(FLAG_EXPORT, &desc->flags))
279 status = -EIO;
280 else if (sysfs_streq(buf, "high"))
281 status = gpio_direction_output(gpio, 1);
282 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
283 status = gpio_direction_output(gpio, 0);
284 else if (sysfs_streq(buf, "in"))
285 status = gpio_direction_input(gpio);
286 else
287 status = -EINVAL;
288
289 mutex_unlock(&sysfs_lock);
290 return status ? : size;
291}
292
Jani Nikula07697462009-12-15 16:46:20 -0800293static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700294 gpio_direction_show, gpio_direction_store);
295
296static ssize_t gpio_value_show(struct device *dev,
297 struct device_attribute *attr, char *buf)
298{
299 const struct gpio_desc *desc = dev_get_drvdata(dev);
300 unsigned gpio = desc - gpio_desc;
301 ssize_t status;
302
303 mutex_lock(&sysfs_lock);
304
Jani Nikula07697462009-12-15 16:46:20 -0800305 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700306 status = -EIO;
Jani Nikula07697462009-12-15 16:46:20 -0800307 } else {
308 int value;
309
310 value = !!gpio_get_value_cansleep(gpio);
311 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
312 value = !value;
313
314 status = sprintf(buf, "%d\n", value);
315 }
David Brownelld8f388d82008-07-25 01:46:07 -0700316
317 mutex_unlock(&sysfs_lock);
318 return status;
319}
320
321static ssize_t gpio_value_store(struct device *dev,
322 struct device_attribute *attr, const char *buf, size_t size)
323{
324 const struct gpio_desc *desc = dev_get_drvdata(dev);
325 unsigned gpio = desc - gpio_desc;
326 ssize_t status;
327
328 mutex_lock(&sysfs_lock);
329
330 if (!test_bit(FLAG_EXPORT, &desc->flags))
331 status = -EIO;
332 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
333 status = -EPERM;
334 else {
335 long value;
336
337 status = strict_strtol(buf, 0, &value);
338 if (status == 0) {
Jani Nikula07697462009-12-15 16:46:20 -0800339 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
340 value = !value;
David Brownelld8f388d82008-07-25 01:46:07 -0700341 gpio_set_value_cansleep(gpio, value != 0);
342 status = size;
343 }
344 }
345
346 mutex_unlock(&sysfs_lock);
347 return status;
348}
349
Jani Nikula07697462009-12-15 16:46:20 -0800350static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700351 gpio_value_show, gpio_value_store);
352
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700353static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
354{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700355 struct sysfs_dirent *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700356
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700357 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700358 return IRQ_HANDLED;
359}
360
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700361static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
362 unsigned long gpio_flags)
363{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700364 struct sysfs_dirent *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700365 unsigned long irq_flags;
366 int ret, irq, id;
367
368 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
369 return 0;
370
371 irq = gpio_to_irq(desc - gpio_desc);
372 if (irq < 0)
373 return -EIO;
374
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700375 id = desc->flags >> ID_SHIFT;
376 value_sd = idr_find(&dirent_idr, id);
377 if (value_sd)
378 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700379
380 desc->flags &= ~GPIO_TRIGGER_MASK;
381
382 if (!gpio_flags) {
383 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700384 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700385 }
386
387 irq_flags = IRQF_SHARED;
388 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800389 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
390 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700391 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800392 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
393 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700394
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700395 if (!value_sd) {
396 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
397 if (!value_sd) {
398 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700399 goto err_out;
400 }
401
402 do {
403 ret = -ENOMEM;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700404 if (idr_pre_get(&dirent_idr, GFP_KERNEL))
405 ret = idr_get_new_above(&dirent_idr, value_sd,
406 1, &id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700407 } while (ret == -EAGAIN);
408
409 if (ret)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700410 goto free_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700411
412 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700413 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700414
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700415 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700416 ret = -ERANGE;
417 goto free_id;
418 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700419 }
420
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700421 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700422 "gpiolib", value_sd);
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700423 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700424 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700425
426 desc->flags |= gpio_flags;
427 return 0;
428
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700429free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700430 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700431 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700432free_sd:
433 if (value_sd)
434 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700435err_out:
436 return ret;
437}
438
439static const struct {
440 const char *name;
441 unsigned long flags;
442} trigger_types[] = {
443 { "none", 0 },
444 { "falling", BIT(FLAG_TRIG_FALL) },
445 { "rising", BIT(FLAG_TRIG_RISE) },
446 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
447};
448
449static ssize_t gpio_edge_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
451{
452 const struct gpio_desc *desc = dev_get_drvdata(dev);
453 ssize_t status;
454
455 mutex_lock(&sysfs_lock);
456
457 if (!test_bit(FLAG_EXPORT, &desc->flags))
458 status = -EIO;
459 else {
460 int i;
461
462 status = 0;
463 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
464 if ((desc->flags & GPIO_TRIGGER_MASK)
465 == trigger_types[i].flags) {
466 status = sprintf(buf, "%s\n",
467 trigger_types[i].name);
468 break;
469 }
470 }
471
472 mutex_unlock(&sysfs_lock);
473 return status;
474}
475
476static ssize_t gpio_edge_store(struct device *dev,
477 struct device_attribute *attr, const char *buf, size_t size)
478{
479 struct gpio_desc *desc = dev_get_drvdata(dev);
480 ssize_t status;
481 int i;
482
483 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
484 if (sysfs_streq(trigger_types[i].name, buf))
485 goto found;
486 return -EINVAL;
487
488found:
489 mutex_lock(&sysfs_lock);
490
491 if (!test_bit(FLAG_EXPORT, &desc->flags))
492 status = -EIO;
493 else {
494 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
495 if (!status)
496 status = size;
497 }
498
499 mutex_unlock(&sysfs_lock);
500
501 return status;
502}
503
504static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
505
Jani Nikula07697462009-12-15 16:46:20 -0800506static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
507 int value)
508{
509 int status = 0;
510
511 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
512 return 0;
513
514 if (value)
515 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
516 else
517 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
518
519 /* reconfigure poll(2) support if enabled on one edge only */
520 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
521 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
522 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
523
524 gpio_setup_irq(desc, dev, 0);
525 status = gpio_setup_irq(desc, dev, trigger_flags);
526 }
527
528 return status;
529}
530
531static ssize_t gpio_active_low_show(struct device *dev,
532 struct device_attribute *attr, char *buf)
533{
534 const struct gpio_desc *desc = dev_get_drvdata(dev);
535 ssize_t status;
536
537 mutex_lock(&sysfs_lock);
538
539 if (!test_bit(FLAG_EXPORT, &desc->flags))
540 status = -EIO;
541 else
542 status = sprintf(buf, "%d\n",
543 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
544
545 mutex_unlock(&sysfs_lock);
546
547 return status;
548}
549
550static ssize_t gpio_active_low_store(struct device *dev,
551 struct device_attribute *attr, const char *buf, size_t size)
552{
553 struct gpio_desc *desc = dev_get_drvdata(dev);
554 ssize_t status;
555
556 mutex_lock(&sysfs_lock);
557
558 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
559 status = -EIO;
560 } else {
561 long value;
562
563 status = strict_strtol(buf, 0, &value);
564 if (status == 0)
565 status = sysfs_set_active_low(desc, dev, value != 0);
566 }
567
568 mutex_unlock(&sysfs_lock);
569
570 return status ? : size;
571}
572
573static const DEVICE_ATTR(active_low, 0644,
574 gpio_active_low_show, gpio_active_low_store);
575
David Brownelld8f388d82008-07-25 01:46:07 -0700576static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d82008-07-25 01:46:07 -0700577 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800578 &dev_attr_active_low.attr,
David Brownelld8f388d82008-07-25 01:46:07 -0700579 NULL,
580};
581
582static const struct attribute_group gpio_attr_group = {
583 .attrs = (struct attribute **) gpio_attrs,
584};
585
586/*
587 * /sys/class/gpio/gpiochipN/
588 * /base ... matching gpio_chip.base (N)
589 * /label ... matching gpio_chip.label
590 * /ngpio ... matching gpio_chip.ngpio
591 */
592
593static ssize_t chip_base_show(struct device *dev,
594 struct device_attribute *attr, char *buf)
595{
596 const struct gpio_chip *chip = dev_get_drvdata(dev);
597
598 return sprintf(buf, "%d\n", chip->base);
599}
600static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
601
602static ssize_t chip_label_show(struct device *dev,
603 struct device_attribute *attr, char *buf)
604{
605 const struct gpio_chip *chip = dev_get_drvdata(dev);
606
607 return sprintf(buf, "%s\n", chip->label ? : "");
608}
609static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
610
611static ssize_t chip_ngpio_show(struct device *dev,
612 struct device_attribute *attr, char *buf)
613{
614 const struct gpio_chip *chip = dev_get_drvdata(dev);
615
616 return sprintf(buf, "%u\n", chip->ngpio);
617}
618static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
619
620static const struct attribute *gpiochip_attrs[] = {
621 &dev_attr_base.attr,
622 &dev_attr_label.attr,
623 &dev_attr_ngpio.attr,
624 NULL,
625};
626
627static const struct attribute_group gpiochip_attr_group = {
628 .attrs = (struct attribute **) gpiochip_attrs,
629};
630
631/*
632 * /sys/class/gpio/export ... write-only
633 * integer N ... number of GPIO to export (full access)
634 * /sys/class/gpio/unexport ... write-only
635 * integer N ... number of GPIO to unexport
636 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100637static ssize_t export_store(struct class *class,
638 struct class_attribute *attr,
639 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700640{
641 long gpio;
642 int status;
643
644 status = strict_strtol(buf, 0, &gpio);
645 if (status < 0)
646 goto done;
647
648 /* No extra locking here; FLAG_SYSFS just signifies that the
649 * request and export were done by on behalf of userspace, so
650 * they may be undone on its behalf too.
651 */
652
653 status = gpio_request(gpio, "sysfs");
654 if (status < 0)
655 goto done;
656
657 status = gpio_export(gpio, true);
658 if (status < 0)
659 gpio_free(gpio);
660 else
661 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
662
663done:
664 if (status)
665 pr_debug("%s: status %d\n", __func__, status);
666 return status ? : len;
667}
668
Andi Kleen28812fe2010-01-05 12:48:07 +0100669static ssize_t unexport_store(struct class *class,
670 struct class_attribute *attr,
671 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700672{
673 long gpio;
674 int status;
675
676 status = strict_strtol(buf, 0, &gpio);
677 if (status < 0)
678 goto done;
679
680 status = -EINVAL;
681
682 /* reject bogus commands (gpio_unexport ignores them) */
683 if (!gpio_is_valid(gpio))
684 goto done;
685
686 /* No extra locking here; FLAG_SYSFS just signifies that the
687 * request and export were done by on behalf of userspace, so
688 * they may be undone on its behalf too.
689 */
690 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
691 status = 0;
692 gpio_free(gpio);
693 }
694done:
695 if (status)
696 pr_debug("%s: status %d\n", __func__, status);
697 return status ? : len;
698}
699
700static struct class_attribute gpio_class_attrs[] = {
701 __ATTR(export, 0200, NULL, export_store),
702 __ATTR(unexport, 0200, NULL, unexport_store),
703 __ATTR_NULL,
704};
705
706static struct class gpio_class = {
707 .name = "gpio",
708 .owner = THIS_MODULE,
709
710 .class_attrs = gpio_class_attrs,
711};
712
713
714/**
715 * gpio_export - export a GPIO through sysfs
716 * @gpio: gpio to make available, already requested
717 * @direction_may_change: true if userspace may change gpio direction
718 * Context: arch_initcall or later
719 *
720 * When drivers want to make a GPIO accessible to userspace after they
721 * have requested it -- perhaps while debugging, or as part of their
722 * public interface -- they may use this routine. If the GPIO can
723 * change direction (some can't) and the caller allows it, userspace
724 * will see "direction" sysfs attribute which may be used to change
725 * the gpio's direction. A "value" attribute will always be provided.
726 *
727 * Returns zero on success, else an error.
728 */
729int gpio_export(unsigned gpio, bool direction_may_change)
730{
731 unsigned long flags;
732 struct gpio_desc *desc;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100733 int status;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700734 const char *ioname = NULL;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100735 struct device *dev;
David Brownelld8f388d82008-07-25 01:46:07 -0700736
737 /* can't export until sysfs is available ... */
738 if (!gpio_class.p) {
739 pr_debug("%s: called too early!\n", __func__);
740 return -ENOENT;
741 }
742
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100743 if (!gpio_is_valid(gpio)) {
744 pr_debug("%s: gpio %d is not valid\n", __func__, gpio);
745 return -EINVAL;
746 }
David Brownelld8f388d82008-07-25 01:46:07 -0700747
748 mutex_lock(&sysfs_lock);
749
750 spin_lock_irqsave(&gpio_lock, flags);
751 desc = &gpio_desc[gpio];
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100752 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
753 test_bit(FLAG_EXPORT, &desc->flags)) {
754 spin_unlock_irqrestore(&gpio_lock, flags);
755 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
756 __func__, gpio,
757 test_bit(FLAG_REQUESTED, &desc->flags),
758 test_bit(FLAG_EXPORT, &desc->flags));
Dan Carpenter529f2ad2012-10-26 09:59:43 +0300759 status = -EPERM;
760 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700761 }
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100762
763 if (!desc->chip->direction_input || !desc->chip->direction_output)
764 direction_may_change = false;
David Brownelld8f388d82008-07-25 01:46:07 -0700765 spin_unlock_irqrestore(&gpio_lock, flags);
766
Daniel Silverstone926b6632009-04-02 16:57:05 -0700767 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
768 ioname = desc->chip->names[gpio - desc->chip->base];
769
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100770 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
771 desc, ioname ? ioname : "gpio%u", gpio);
772 if (IS_ERR(dev)) {
773 status = PTR_ERR(dev);
774 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700775 }
776
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100777 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
David Brownelld8f388d82008-07-25 01:46:07 -0700778 if (status)
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100779 goto fail_unregister_device;
David Brownelld8f388d82008-07-25 01:46:07 -0700780
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100781 if (direction_may_change) {
782 status = device_create_file(dev, &dev_attr_direction);
783 if (status)
784 goto fail_unregister_device;
785 }
786
787 if (gpio_to_irq(gpio) >= 0 && (direction_may_change ||
788 !test_bit(FLAG_IS_OUT, &desc->flags))) {
789 status = device_create_file(dev, &dev_attr_edge);
790 if (status)
791 goto fail_unregister_device;
792 }
793
794 set_bit(FLAG_EXPORT, &desc->flags);
795 mutex_unlock(&sysfs_lock);
796 return 0;
797
798fail_unregister_device:
799 device_unregister(dev);
800fail_unlock:
801 mutex_unlock(&sysfs_lock);
802 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
David Brownelld8f388d82008-07-25 01:46:07 -0700803 return status;
804}
805EXPORT_SYMBOL_GPL(gpio_export);
806
807static int match_export(struct device *dev, void *data)
808{
809 return dev_get_drvdata(dev) == data;
810}
811
812/**
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700813 * gpio_export_link - create a sysfs link to an exported GPIO node
814 * @dev: device under which to create symlink
815 * @name: name of the symlink
816 * @gpio: gpio to create symlink to, already exported
817 *
818 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
819 * node. Caller is responsible for unlinking.
820 *
821 * Returns zero on success, else an error.
822 */
823int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
824{
825 struct gpio_desc *desc;
826 int status = -EINVAL;
827
828 if (!gpio_is_valid(gpio))
829 goto done;
830
831 mutex_lock(&sysfs_lock);
832
833 desc = &gpio_desc[gpio];
834
835 if (test_bit(FLAG_EXPORT, &desc->flags)) {
836 struct device *tdev;
837
838 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
839 if (tdev != NULL) {
840 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
841 name);
842 } else {
843 status = -ENODEV;
844 }
845 }
846
847 mutex_unlock(&sysfs_lock);
848
849done:
850 if (status)
851 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
852
853 return status;
854}
855EXPORT_SYMBOL_GPL(gpio_export_link);
856
Jani Nikula07697462009-12-15 16:46:20 -0800857
858/**
859 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
860 * @gpio: gpio to change
861 * @value: non-zero to use active low, i.e. inverted values
862 *
863 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
864 * The GPIO does not have to be exported yet. If poll(2) support has
865 * been enabled for either rising or falling edge, it will be
866 * reconfigured to follow the new polarity.
867 *
868 * Returns zero on success, else an error.
869 */
870int gpio_sysfs_set_active_low(unsigned gpio, int value)
871{
872 struct gpio_desc *desc;
873 struct device *dev = NULL;
874 int status = -EINVAL;
875
876 if (!gpio_is_valid(gpio))
877 goto done;
878
879 mutex_lock(&sysfs_lock);
880
881 desc = &gpio_desc[gpio];
882
883 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800884 dev = class_find_device(&gpio_class, NULL, desc, match_export);
885 if (dev == NULL) {
886 status = -ENODEV;
887 goto unlock;
888 }
889 }
890
891 status = sysfs_set_active_low(desc, dev, value);
892
893unlock:
894 mutex_unlock(&sysfs_lock);
895
896done:
897 if (status)
898 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
899
900 return status;
901}
902EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
903
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700904/**
David Brownelld8f388d82008-07-25 01:46:07 -0700905 * gpio_unexport - reverse effect of gpio_export()
906 * @gpio: gpio to make unavailable
907 *
908 * This is implicit on gpio_free().
909 */
910void gpio_unexport(unsigned gpio)
911{
912 struct gpio_desc *desc;
Jon Povey6a99ad42010-07-27 13:18:06 -0700913 int status = 0;
Ming Lei864533c2012-02-13 22:53:20 +0800914 struct device *dev = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -0700915
Jon Povey6a99ad42010-07-27 13:18:06 -0700916 if (!gpio_is_valid(gpio)) {
917 status = -EINVAL;
David Brownelld8f388d82008-07-25 01:46:07 -0700918 goto done;
Jon Povey6a99ad42010-07-27 13:18:06 -0700919 }
David Brownelld8f388d82008-07-25 01:46:07 -0700920
921 mutex_lock(&sysfs_lock);
922
923 desc = &gpio_desc[gpio];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700924
David Brownelld8f388d82008-07-25 01:46:07 -0700925 if (test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700926
927 dev = class_find_device(&gpio_class, NULL, desc, match_export);
928 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700929 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700930 clear_bit(FLAG_EXPORT, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700931 } else
932 status = -ENODEV;
933 }
934
935 mutex_unlock(&sysfs_lock);
Ming Lei864533c2012-02-13 22:53:20 +0800936 if (dev) {
937 device_unregister(dev);
938 put_device(dev);
939 }
David Brownelld8f388d82008-07-25 01:46:07 -0700940done:
941 if (status)
942 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
943}
944EXPORT_SYMBOL_GPL(gpio_unexport);
945
946static int gpiochip_export(struct gpio_chip *chip)
947{
948 int status;
949 struct device *dev;
950
951 /* Many systems register gpio chips for SOC support very early,
952 * before driver model support is available. In those cases we
953 * export this later, in gpiolib_sysfs_init() ... here we just
954 * verify that _some_ field of gpio_class got initialized.
955 */
956 if (!gpio_class.p)
957 return 0;
958
959 /* use chip->base for the ID; it's already known to be unique */
960 mutex_lock(&sysfs_lock);
961 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
962 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800963 if (!IS_ERR(dev)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700964 status = sysfs_create_group(&dev->kobj,
965 &gpiochip_attr_group);
966 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800967 status = PTR_ERR(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700968 chip->exported = (status == 0);
969 mutex_unlock(&sysfs_lock);
970
971 if (status) {
972 unsigned long flags;
973 unsigned gpio;
974
975 spin_lock_irqsave(&gpio_lock, flags);
976 gpio = chip->base;
977 while (gpio_desc[gpio].chip == chip)
978 gpio_desc[gpio++].chip = NULL;
979 spin_unlock_irqrestore(&gpio_lock, flags);
980
981 pr_debug("%s: chip %s status %d\n", __func__,
982 chip->label, status);
983 }
984
985 return status;
986}
987
988static void gpiochip_unexport(struct gpio_chip *chip)
989{
990 int status;
991 struct device *dev;
992
993 mutex_lock(&sysfs_lock);
994 dev = class_find_device(&gpio_class, NULL, chip, match_export);
995 if (dev) {
996 put_device(dev);
997 device_unregister(dev);
998 chip->exported = 0;
999 status = 0;
1000 } else
1001 status = -ENODEV;
1002 mutex_unlock(&sysfs_lock);
1003
1004 if (status)
1005 pr_debug("%s: chip %s status %d\n", __func__,
1006 chip->label, status);
1007}
1008
1009static int __init gpiolib_sysfs_init(void)
1010{
1011 int status;
1012 unsigned long flags;
1013 unsigned gpio;
1014
1015 status = class_register(&gpio_class);
1016 if (status < 0)
1017 return status;
1018
1019 /* Scan and register the gpio_chips which registered very
1020 * early (e.g. before the class_register above was called).
1021 *
1022 * We run before arch_initcall() so chip->dev nodes can have
1023 * registered, and so arch_initcall() can always gpio_export().
1024 */
1025 spin_lock_irqsave(&gpio_lock, flags);
1026 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
1027 struct gpio_chip *chip;
1028
1029 chip = gpio_desc[gpio].chip;
1030 if (!chip || chip->exported)
1031 continue;
1032
1033 spin_unlock_irqrestore(&gpio_lock, flags);
1034 status = gpiochip_export(chip);
1035 spin_lock_irqsave(&gpio_lock, flags);
1036 }
1037 spin_unlock_irqrestore(&gpio_lock, flags);
1038
1039
1040 return status;
1041}
1042postcore_initcall(gpiolib_sysfs_init);
1043
1044#else
1045static inline int gpiochip_export(struct gpio_chip *chip)
1046{
1047 return 0;
1048}
1049
1050static inline void gpiochip_unexport(struct gpio_chip *chip)
1051{
1052}
1053
1054#endif /* CONFIG_GPIO_SYSFS */
1055
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001056/**
David Brownelld2876d02008-02-04 22:28:20 -08001057 * gpiochip_add() - register a gpio_chip
1058 * @chip: the chip to register, with chip->base initialized
1059 * Context: potentially before irqs or kmalloc will work
1060 *
1061 * Returns a negative errno if the chip can't be registered, such as
1062 * because the chip->base is invalid or already associated with a
1063 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001064 *
David Brownelld8f388d82008-07-25 01:46:07 -07001065 * When gpiochip_add() is called very early during boot, so that GPIOs
1066 * can be freely used, the chip->dev device must be registered before
1067 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1068 * for GPIOs will fail rudely.
1069 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001070 * If chip->base is negative, this requests dynamic assignment of
1071 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001072 */
1073int gpiochip_add(struct gpio_chip *chip)
1074{
1075 unsigned long flags;
1076 int status = 0;
1077 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001078 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001079
Trent Piephobff5fda2008-05-23 13:04:44 -07001080 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001081 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001082 status = -EINVAL;
1083 goto fail;
1084 }
1085
1086 spin_lock_irqsave(&gpio_lock, flags);
1087
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001088 if (base < 0) {
1089 base = gpiochip_find_base(chip->ngpio);
1090 if (base < 0) {
1091 status = base;
David Brownelld8f388d82008-07-25 01:46:07 -07001092 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001093 }
1094 chip->base = base;
1095 }
1096
David Brownelld2876d02008-02-04 22:28:20 -08001097 /* these GPIO numbers must not be managed by another gpio_chip */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001098 for (id = base; id < base + chip->ngpio; id++) {
David Brownelld2876d02008-02-04 22:28:20 -08001099 if (gpio_desc[id].chip != NULL) {
1100 status = -EBUSY;
1101 break;
1102 }
1103 }
1104 if (status == 0) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001105 for (id = base; id < base + chip->ngpio; id++) {
David Brownelld2876d02008-02-04 22:28:20 -08001106 gpio_desc[id].chip = chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001107
1108 /* REVISIT: most hardware initializes GPIOs as
1109 * inputs (often with pullups enabled) so power
1110 * usage is minimized. Linux code should set the
1111 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +03001112 * and in case chip->get_direction is not set,
David Brownelld8f388d82008-07-25 01:46:07 -07001113 * we may expose the wrong direction in sysfs.
1114 */
1115 gpio_desc[id].flags = !chip->direction_input
1116 ? (1 << FLAG_IS_OUT)
1117 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001118 }
1119 }
1120
Anton Vorontsov391c9702010-06-08 07:48:17 -06001121 of_gpiochip_add(chip);
1122
David Brownelld8f388d82008-07-25 01:46:07 -07001123unlock:
David Brownelld2876d02008-02-04 22:28:20 -08001124 spin_unlock_irqrestore(&gpio_lock, flags);
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001125
1126 if (status)
1127 goto fail;
1128
1129 status = gpiochip_export(chip);
1130 if (status)
1131 goto fail;
1132
H Hartley Sweetenee1c1e72012-05-11 16:58:33 -07001133 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
Grant Likely64842aa2011-11-06 11:36:18 -07001134 chip->base, chip->base + chip->ngpio - 1,
1135 chip->label ? : "generic");
1136
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001137 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001138fail:
1139 /* failures here can mean systems won't boot... */
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001140 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1141 chip->base, chip->base + chip->ngpio - 1,
1142 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001143 return status;
1144}
1145EXPORT_SYMBOL_GPL(gpiochip_add);
1146
1147/**
1148 * gpiochip_remove() - unregister a gpio_chip
1149 * @chip: the chip to unregister
1150 *
1151 * A gpio_chip with any GPIOs still requested may not be removed.
1152 */
1153int gpiochip_remove(struct gpio_chip *chip)
1154{
1155 unsigned long flags;
1156 int status = 0;
1157 unsigned id;
1158
1159 spin_lock_irqsave(&gpio_lock, flags);
1160
Anton Vorontsov391c9702010-06-08 07:48:17 -06001161 of_gpiochip_remove(chip);
1162
David Brownelld2876d02008-02-04 22:28:20 -08001163 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1164 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1165 status = -EBUSY;
1166 break;
1167 }
1168 }
1169 if (status == 0) {
1170 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1171 gpio_desc[id].chip = NULL;
1172 }
1173
1174 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d82008-07-25 01:46:07 -07001175
1176 if (status == 0)
1177 gpiochip_unexport(chip);
1178
David Brownelld2876d02008-02-04 22:28:20 -08001179 return status;
1180}
1181EXPORT_SYMBOL_GPL(gpiochip_remove);
1182
Grant Likely594fa262010-06-08 07:48:16 -06001183/**
1184 * gpiochip_find() - iterator for locating a specific gpio_chip
1185 * @data: data to pass to match function
1186 * @callback: Callback function to check gpio_chip
1187 *
1188 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1189 * determined by a user supplied @match callback. The callback should return
1190 * 0 if the device doesn't match and non-zero if it does. If the callback is
1191 * non-zero, this function will return to the caller and not iterate over any
1192 * more gpio_chips.
1193 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001194struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001195 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001196 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001197{
1198 struct gpio_chip *chip = NULL;
1199 unsigned long flags;
1200 int i;
1201
1202 spin_lock_irqsave(&gpio_lock, flags);
1203 for (i = 0; i < ARCH_NR_GPIOS; i++) {
1204 if (!gpio_desc[i].chip)
1205 continue;
1206
1207 if (match(gpio_desc[i].chip, data)) {
1208 chip = gpio_desc[i].chip;
1209 break;
1210 }
1211 }
1212 spin_unlock_irqrestore(&gpio_lock, flags);
1213
1214 return chip;
1215}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001216EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001217
1218/* These "optional" allocation calls help prevent drivers from stomping
1219 * on each other, and help provide better diagnostics in debugfs.
1220 * They're called even less than the "set direction" calls.
1221 */
1222int gpio_request(unsigned gpio, const char *label)
1223{
1224 struct gpio_desc *desc;
David Brownell35e8bb52008-10-15 22:03:16 -07001225 struct gpio_chip *chip;
Mark Browne9354572012-07-09 12:22:56 +01001226 int status = -EPROBE_DEFER;
David Brownelld2876d02008-02-04 22:28:20 -08001227 unsigned long flags;
1228
1229 spin_lock_irqsave(&gpio_lock, flags);
1230
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001231 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001232 goto done;
1233 desc = &gpio_desc[gpio];
David Brownell35e8bb52008-10-15 22:03:16 -07001234 chip = desc->chip;
1235 if (chip == NULL)
David Brownelld2876d02008-02-04 22:28:20 -08001236 goto done;
1237
David Brownell35e8bb52008-10-15 22:03:16 -07001238 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001239 goto done;
1240
David Brownelld2876d02008-02-04 22:28:20 -08001241 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001242 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001243 */
1244
1245 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1246 desc_set_label(desc, label ? : "?");
1247 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001248 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001249 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001250 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001251 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001252 }
1253
1254 if (chip->request) {
1255 /* chip->request may sleep */
1256 spin_unlock_irqrestore(&gpio_lock, flags);
1257 status = chip->request(chip, gpio - chip->base);
1258 spin_lock_irqsave(&gpio_lock, flags);
1259
1260 if (status < 0) {
1261 desc_set_label(desc, NULL);
1262 module_put(chip->owner);
1263 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001264 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001265 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001266 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001267 if (chip->get_direction) {
1268 /* chip->get_direction may sleep */
1269 spin_unlock_irqrestore(&gpio_lock, flags);
1270 gpio_get_direction(gpio);
1271 spin_lock_irqsave(&gpio_lock, flags);
1272 }
David Brownelld2876d02008-02-04 22:28:20 -08001273done:
1274 if (status)
1275 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1276 gpio, label ? : "?", status);
1277 spin_unlock_irqrestore(&gpio_lock, flags);
1278 return status;
1279}
1280EXPORT_SYMBOL_GPL(gpio_request);
1281
1282void gpio_free(unsigned gpio)
1283{
1284 unsigned long flags;
1285 struct gpio_desc *desc;
David Brownell35e8bb52008-10-15 22:03:16 -07001286 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001287
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001288 might_sleep();
1289
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001290 if (!gpio_is_valid(gpio)) {
David Brownelld2876d02008-02-04 22:28:20 -08001291 WARN_ON(extra_checks);
1292 return;
1293 }
1294
David Brownelld8f388d82008-07-25 01:46:07 -07001295 gpio_unexport(gpio);
1296
David Brownelld2876d02008-02-04 22:28:20 -08001297 spin_lock_irqsave(&gpio_lock, flags);
1298
1299 desc = &gpio_desc[gpio];
David Brownell35e8bb52008-10-15 22:03:16 -07001300 chip = desc->chip;
1301 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1302 if (chip->free) {
1303 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001304 might_sleep_if(chip->can_sleep);
David Brownell35e8bb52008-10-15 22:03:16 -07001305 chip->free(chip, gpio - chip->base);
1306 spin_lock_irqsave(&gpio_lock, flags);
1307 }
David Brownelld2876d02008-02-04 22:28:20 -08001308 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001309 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001310 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001311 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301312 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301313 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001314 } else
David Brownelld2876d02008-02-04 22:28:20 -08001315 WARN_ON(extra_checks);
1316
1317 spin_unlock_irqrestore(&gpio_lock, flags);
1318}
1319EXPORT_SYMBOL_GPL(gpio_free);
1320
Eric Miao3e45f1d2010-03-05 13:44:35 -08001321/**
1322 * gpio_request_one - request a single GPIO with initial configuration
1323 * @gpio: the GPIO number
1324 * @flags: GPIO configuration as specified by GPIOF_*
1325 * @label: a literal description string of this GPIO
1326 */
1327int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1328{
1329 int err;
1330
1331 err = gpio_request(gpio, label);
1332 if (err)
1333 return err;
1334
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301335 if (flags & GPIOF_OPEN_DRAIN)
1336 set_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags);
1337
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301338 if (flags & GPIOF_OPEN_SOURCE)
1339 set_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags);
1340
Eric Miao3e45f1d2010-03-05 13:44:35 -08001341 if (flags & GPIOF_DIR_IN)
1342 err = gpio_direction_input(gpio);
1343 else
1344 err = gpio_direction_output(gpio,
1345 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1346
Aaro Koskinene2548112010-12-21 17:24:22 -08001347 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001348 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -08001349
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001350 if (flags & GPIOF_EXPORT) {
1351 err = gpio_export(gpio, flags & GPIOF_EXPORT_CHANGEABLE);
1352 if (err)
1353 goto free_gpio;
1354 }
1355
1356 return 0;
1357
1358 free_gpio:
1359 gpio_free(gpio);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001360 return err;
1361}
1362EXPORT_SYMBOL_GPL(gpio_request_one);
1363
1364/**
1365 * gpio_request_array - request multiple GPIOs in a single call
1366 * @array: array of the 'struct gpio'
1367 * @num: how many GPIOs in the array
1368 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001369int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001370{
1371 int i, err;
1372
1373 for (i = 0; i < num; i++, array++) {
1374 err = gpio_request_one(array->gpio, array->flags, array->label);
1375 if (err)
1376 goto err_free;
1377 }
1378 return 0;
1379
1380err_free:
1381 while (i--)
1382 gpio_free((--array)->gpio);
1383 return err;
1384}
1385EXPORT_SYMBOL_GPL(gpio_request_array);
1386
1387/**
1388 * gpio_free_array - release multiple GPIOs in a single call
1389 * @array: array of the 'struct gpio'
1390 * @num: how many GPIOs in the array
1391 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001392void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001393{
1394 while (num--)
1395 gpio_free((array++)->gpio);
1396}
1397EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001398
1399/**
1400 * gpiochip_is_requested - return string iff signal was requested
1401 * @chip: controller managing the signal
1402 * @offset: of signal within controller's 0..(ngpio - 1) range
1403 *
1404 * Returns NULL if the GPIO is not currently requested, else a string.
1405 * If debugfs support is enabled, the string returned is the label passed
1406 * to gpio_request(); otherwise it is a meaningless constant.
1407 *
1408 * This function is for use by GPIO controller drivers. The label can
1409 * help with diagnostics, and knowing that the signal is used as a GPIO
1410 * can help avoid accidentally multiplexing it to another controller.
1411 */
1412const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1413{
1414 unsigned gpio = chip->base + offset;
1415
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001416 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
David Brownelld2876d02008-02-04 22:28:20 -08001417 return NULL;
1418 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1419 return NULL;
1420#ifdef CONFIG_DEBUG_FS
1421 return gpio_desc[gpio].label;
1422#else
1423 return "?";
1424#endif
1425}
1426EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1427
1428
1429/* Drivers MUST set GPIO direction before making get/set calls. In
1430 * some cases this is done in early boot, before IRQs are enabled.
1431 *
1432 * As a rule these aren't called more than once (except for drivers
1433 * using the open-drain emulation idiom) so these are natural places
1434 * to accumulate extra debugging checks. Note that we can't (yet)
1435 * rely on gpio_request() having been called beforehand.
1436 */
1437
1438int gpio_direction_input(unsigned gpio)
1439{
1440 unsigned long flags;
1441 struct gpio_chip *chip;
1442 struct gpio_desc *desc = &gpio_desc[gpio];
1443 int status = -EINVAL;
1444
1445 spin_lock_irqsave(&gpio_lock, flags);
1446
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001447 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001448 goto fail;
1449 chip = desc->chip;
1450 if (!chip || !chip->get || !chip->direction_input)
1451 goto fail;
1452 gpio -= chip->base;
1453 if (gpio >= chip->ngpio)
1454 goto fail;
David Brownell35e8bb52008-10-15 22:03:16 -07001455 status = gpio_ensure_requested(desc, gpio);
1456 if (status < 0)
1457 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001458
1459 /* now we know the gpio is valid and chip won't vanish */
1460
1461 spin_unlock_irqrestore(&gpio_lock, flags);
1462
David Brownell9c4ba942010-08-10 18:02:24 -07001463 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001464
David Brownell35e8bb52008-10-15 22:03:16 -07001465 if (status) {
1466 status = chip->request(chip, gpio);
1467 if (status < 0) {
1468 pr_debug("GPIO-%d: chip request fail, %d\n",
1469 chip->base + gpio, status);
1470 /* and it's not available to anyone else ...
1471 * gpio_request() is the fully clean solution.
1472 */
1473 goto lose;
1474 }
1475 }
1476
David Brownelld2876d02008-02-04 22:28:20 -08001477 status = chip->direction_input(chip, gpio);
1478 if (status == 0)
1479 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001480
1481 trace_gpio_direction(chip->base + gpio, 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001482lose:
David Brownelld2876d02008-02-04 22:28:20 -08001483 return status;
1484fail:
1485 spin_unlock_irqrestore(&gpio_lock, flags);
1486 if (status)
1487 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001488 __func__, gpio, status);
David Brownelld2876d02008-02-04 22:28:20 -08001489 return status;
1490}
1491EXPORT_SYMBOL_GPL(gpio_direction_input);
1492
1493int gpio_direction_output(unsigned gpio, int value)
1494{
1495 unsigned long flags;
1496 struct gpio_chip *chip;
1497 struct gpio_desc *desc = &gpio_desc[gpio];
1498 int status = -EINVAL;
1499
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301500 /* Open drain pin should not be driven to 1 */
1501 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1502 return gpio_direction_input(gpio);
1503
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301504 /* Open source pin should not be driven to 0 */
1505 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1506 return gpio_direction_input(gpio);
1507
David Brownelld2876d02008-02-04 22:28:20 -08001508 spin_lock_irqsave(&gpio_lock, flags);
1509
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001510 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001511 goto fail;
1512 chip = desc->chip;
1513 if (!chip || !chip->set || !chip->direction_output)
1514 goto fail;
1515 gpio -= chip->base;
1516 if (gpio >= chip->ngpio)
1517 goto fail;
David Brownell35e8bb52008-10-15 22:03:16 -07001518 status = gpio_ensure_requested(desc, gpio);
1519 if (status < 0)
1520 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001521
1522 /* now we know the gpio is valid and chip won't vanish */
1523
1524 spin_unlock_irqrestore(&gpio_lock, flags);
1525
David Brownell9c4ba942010-08-10 18:02:24 -07001526 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001527
David Brownell35e8bb52008-10-15 22:03:16 -07001528 if (status) {
1529 status = chip->request(chip, gpio);
1530 if (status < 0) {
1531 pr_debug("GPIO-%d: chip request fail, %d\n",
1532 chip->base + gpio, status);
1533 /* and it's not available to anyone else ...
1534 * gpio_request() is the fully clean solution.
1535 */
1536 goto lose;
1537 }
1538 }
1539
David Brownelld2876d02008-02-04 22:28:20 -08001540 status = chip->direction_output(chip, gpio, value);
1541 if (status == 0)
1542 set_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001543 trace_gpio_value(chip->base + gpio, 0, value);
1544 trace_gpio_direction(chip->base + gpio, 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001545lose:
David Brownelld2876d02008-02-04 22:28:20 -08001546 return status;
1547fail:
1548 spin_unlock_irqrestore(&gpio_lock, flags);
1549 if (status)
1550 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001551 __func__, gpio, status);
David Brownelld2876d02008-02-04 22:28:20 -08001552 return status;
1553}
1554EXPORT_SYMBOL_GPL(gpio_direction_output);
1555
Felipe Balbic4b5be92010-05-26 14:42:23 -07001556/**
1557 * gpio_set_debounce - sets @debounce time for a @gpio
1558 * @gpio: the gpio to set debounce time
1559 * @debounce: debounce time is microseconds
1560 */
1561int gpio_set_debounce(unsigned gpio, unsigned debounce)
1562{
1563 unsigned long flags;
1564 struct gpio_chip *chip;
1565 struct gpio_desc *desc = &gpio_desc[gpio];
1566 int status = -EINVAL;
1567
1568 spin_lock_irqsave(&gpio_lock, flags);
1569
1570 if (!gpio_is_valid(gpio))
1571 goto fail;
1572 chip = desc->chip;
1573 if (!chip || !chip->set || !chip->set_debounce)
1574 goto fail;
1575 gpio -= chip->base;
1576 if (gpio >= chip->ngpio)
1577 goto fail;
1578 status = gpio_ensure_requested(desc, gpio);
1579 if (status < 0)
1580 goto fail;
1581
1582 /* now we know the gpio is valid and chip won't vanish */
1583
1584 spin_unlock_irqrestore(&gpio_lock, flags);
1585
David Brownell9c4ba942010-08-10 18:02:24 -07001586 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001587
1588 return chip->set_debounce(chip, gpio, debounce);
1589
1590fail:
1591 spin_unlock_irqrestore(&gpio_lock, flags);
1592 if (status)
1593 pr_debug("%s: gpio-%d status %d\n",
1594 __func__, gpio, status);
1595
1596 return status;
1597}
1598EXPORT_SYMBOL_GPL(gpio_set_debounce);
David Brownelld2876d02008-02-04 22:28:20 -08001599
1600/* I/O calls are only valid after configuration completed; the relevant
1601 * "is this a valid GPIO" error checks should already have been done.
1602 *
1603 * "Get" operations are often inlinable as reading a pin value register,
1604 * and masking the relevant bit in that register.
1605 *
1606 * When "set" operations are inlinable, they involve writing that mask to
1607 * one register to set a low value, or a different register to set it high.
1608 * Otherwise locking is needed, so there may be little value to inlining.
1609 *
1610 *------------------------------------------------------------------------
1611 *
1612 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1613 * have requested the GPIO. That can include implicit requesting by
1614 * a direction setting call. Marking a gpio as requested locks its chip
1615 * in memory, guaranteeing that these table lookups need no more locking
1616 * and that gpiochip_remove() will fail.
1617 *
1618 * REVISIT when debugging, consider adding some instrumentation to ensure
1619 * that the GPIO was actually requested.
1620 */
1621
1622/**
1623 * __gpio_get_value() - return a gpio's value
1624 * @gpio: gpio whose value will be returned
1625 * Context: any
1626 *
1627 * This is used directly or indirectly to implement gpio_get_value().
1628 * It returns the zero or nonzero value provided by the associated
1629 * gpio_chip.get() method; or zero if no such method is provided.
1630 */
1631int __gpio_get_value(unsigned gpio)
1632{
1633 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001634 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001635
1636 chip = gpio_to_chip(gpio);
Mark Browne4e449e2012-02-17 10:46:00 -08001637 /* Should be using gpio_get_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001638 WARN_ON(chip->can_sleep);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001639 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1640 trace_gpio_value(gpio, 1, value);
1641 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001642}
1643EXPORT_SYMBOL_GPL(__gpio_get_value);
1644
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301645/*
1646 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1647 * @gpio: Gpio whose state need to be set.
1648 * @chip: Gpio chip.
1649 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1650 */
1651static void _gpio_set_open_drain_value(unsigned gpio,
1652 struct gpio_chip *chip, int value)
1653{
1654 int err = 0;
1655 if (value) {
1656 err = chip->direction_input(chip, gpio - chip->base);
1657 if (!err)
1658 clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1659 } else {
1660 err = chip->direction_output(chip, gpio - chip->base, 0);
1661 if (!err)
1662 set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1663 }
1664 trace_gpio_direction(gpio, value, err);
1665 if (err < 0)
1666 pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
1667 __func__, gpio, err);
1668}
1669
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301670/*
1671 * _gpio_set_open_source() - Set the open source gpio's value.
1672 * @gpio: Gpio whose state need to be set.
1673 * @chip: Gpio chip.
1674 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1675 */
1676static void _gpio_set_open_source_value(unsigned gpio,
1677 struct gpio_chip *chip, int value)
1678{
1679 int err = 0;
1680 if (value) {
1681 err = chip->direction_output(chip, gpio - chip->base, 1);
1682 if (!err)
1683 set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1684 } else {
1685 err = chip->direction_input(chip, gpio - chip->base);
1686 if (!err)
1687 clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1688 }
1689 trace_gpio_direction(gpio, !value, err);
1690 if (err < 0)
1691 pr_err("%s: Error in set_value for open source gpio%d err %d\n",
1692 __func__, gpio, err);
1693}
1694
1695
David Brownelld2876d02008-02-04 22:28:20 -08001696/**
1697 * __gpio_set_value() - assign a gpio's value
1698 * @gpio: gpio whose value will be assigned
1699 * @value: value to assign
1700 * Context: any
1701 *
1702 * This is used directly or indirectly to implement gpio_set_value().
1703 * It invokes the associated gpio_chip.set() method.
1704 */
1705void __gpio_set_value(unsigned gpio, int value)
1706{
1707 struct gpio_chip *chip;
1708
1709 chip = gpio_to_chip(gpio);
Mark Browne4e449e2012-02-17 10:46:00 -08001710 /* Should be using gpio_set_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001711 WARN_ON(chip->can_sleep);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001712 trace_gpio_value(gpio, 0, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301713 if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
1714 _gpio_set_open_drain_value(gpio, chip, value);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301715 else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
1716 _gpio_set_open_source_value(gpio, chip, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301717 else
1718 chip->set(chip, gpio - chip->base, value);
David Brownelld2876d02008-02-04 22:28:20 -08001719}
1720EXPORT_SYMBOL_GPL(__gpio_set_value);
1721
1722/**
1723 * __gpio_cansleep() - report whether gpio value access will sleep
1724 * @gpio: gpio in question
1725 * Context: any
1726 *
1727 * This is used directly or indirectly to implement gpio_cansleep(). It
1728 * returns nonzero if access reading or writing the GPIO value can sleep.
1729 */
1730int __gpio_cansleep(unsigned gpio)
1731{
1732 struct gpio_chip *chip;
1733
1734 /* only call this on GPIOs that are valid! */
1735 chip = gpio_to_chip(gpio);
1736
1737 return chip->can_sleep;
1738}
1739EXPORT_SYMBOL_GPL(__gpio_cansleep);
1740
David Brownell0f6d5042008-10-15 22:03:14 -07001741/**
1742 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1743 * @gpio: gpio whose IRQ will be returned (already requested)
1744 * Context: any
1745 *
1746 * This is used directly or indirectly to implement gpio_to_irq().
1747 * It returns the number of the IRQ signaled by this (input) GPIO,
1748 * or a negative errno.
1749 */
1750int __gpio_to_irq(unsigned gpio)
1751{
1752 struct gpio_chip *chip;
1753
1754 chip = gpio_to_chip(gpio);
1755 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1756}
1757EXPORT_SYMBOL_GPL(__gpio_to_irq);
1758
David Brownelld2876d02008-02-04 22:28:20 -08001759
1760
1761/* There's no value in making it easy to inline GPIO calls that may sleep.
1762 * Common examples include ones connected to I2C or SPI chips.
1763 */
1764
1765int gpio_get_value_cansleep(unsigned gpio)
1766{
1767 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001768 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001769
1770 might_sleep_if(extra_checks);
1771 chip = gpio_to_chip(gpio);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001772 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1773 trace_gpio_value(gpio, 1, value);
1774 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001775}
1776EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1777
1778void gpio_set_value_cansleep(unsigned gpio, int value)
1779{
1780 struct gpio_chip *chip;
1781
1782 might_sleep_if(extra_checks);
1783 chip = gpio_to_chip(gpio);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001784 trace_gpio_value(gpio, 0, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301785 if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
1786 _gpio_set_open_drain_value(gpio, chip, value);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301787 else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
1788 _gpio_set_open_source_value(gpio, chip, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301789 else
1790 chip->set(chip, gpio - chip->base, value);
David Brownelld2876d02008-02-04 22:28:20 -08001791}
1792EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1793
1794
1795#ifdef CONFIG_DEBUG_FS
1796
David Brownelld2876d02008-02-04 22:28:20 -08001797static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1798{
1799 unsigned i;
1800 unsigned gpio = chip->base;
1801 struct gpio_desc *gdesc = &gpio_desc[gpio];
1802 int is_out;
1803
1804 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1805 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1806 continue;
1807
Mathias Nyman80b0a602012-10-24 17:25:27 +03001808 gpio_get_direction(gpio);
David Brownelld2876d02008-02-04 22:28:20 -08001809 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Jarkko Nikula6e8ba722008-11-19 15:36:17 -08001810 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08001811 gpio, gdesc->label,
1812 is_out ? "out" : "in ",
1813 chip->get
1814 ? (chip->get(chip, i) ? "hi" : "lo")
1815 : "? ");
David Brownelld2876d02008-02-04 22:28:20 -08001816 seq_printf(s, "\n");
1817 }
1818}
1819
Thierry Redingf9c4a312012-04-12 13:26:01 +02001820static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08001821{
Thierry Redingf9c4a312012-04-12 13:26:01 +02001822 struct gpio_chip *chip = NULL;
1823 unsigned int gpio;
1824 void *ret = NULL;
1825 loff_t index = 0;
David Brownelld2876d02008-02-04 22:28:20 -08001826
1827 /* REVISIT this isn't locked against gpio_chip removal ... */
1828
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001829 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
Thierry Redingf9c4a312012-04-12 13:26:01 +02001830 if (gpio_desc[gpio].chip == chip)
David Brownelld2876d02008-02-04 22:28:20 -08001831 continue;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001832
David Brownelld2876d02008-02-04 22:28:20 -08001833 chip = gpio_desc[gpio].chip;
1834 if (!chip)
1835 continue;
1836
Thierry Redingf9c4a312012-04-12 13:26:01 +02001837 if (index++ >= *pos) {
1838 ret = chip;
1839 break;
1840 }
David Brownelld2876d02008-02-04 22:28:20 -08001841 }
Thierry Redingf9c4a312012-04-12 13:26:01 +02001842
1843 s->private = "";
1844
1845 return ret;
1846}
1847
1848static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
1849{
1850 struct gpio_chip *chip = v;
1851 unsigned int gpio;
1852 void *ret = NULL;
1853
1854 /* skip GPIOs provided by the current chip */
1855 for (gpio = chip->base + chip->ngpio; gpio_is_valid(gpio); gpio++) {
1856 chip = gpio_desc[gpio].chip;
1857 if (chip) {
1858 ret = chip;
1859 break;
1860 }
1861 }
1862
1863 s->private = "\n";
1864 ++*pos;
1865
1866 return ret;
1867}
1868
1869static void gpiolib_seq_stop(struct seq_file *s, void *v)
1870{
1871}
1872
1873static int gpiolib_seq_show(struct seq_file *s, void *v)
1874{
1875 struct gpio_chip *chip = v;
1876 struct device *dev;
1877
1878 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
1879 chip->base, chip->base + chip->ngpio - 1);
1880 dev = chip->dev;
1881 if (dev)
1882 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
1883 dev_name(dev));
1884 if (chip->label)
1885 seq_printf(s, ", %s", chip->label);
1886 if (chip->can_sleep)
1887 seq_printf(s, ", can sleep");
1888 seq_printf(s, ":\n");
1889
1890 if (chip->dbg_show)
1891 chip->dbg_show(s, chip);
1892 else
1893 gpiolib_dbg_show(s, chip);
1894
David Brownelld2876d02008-02-04 22:28:20 -08001895 return 0;
1896}
1897
Thierry Redingf9c4a312012-04-12 13:26:01 +02001898static const struct seq_operations gpiolib_seq_ops = {
1899 .start = gpiolib_seq_start,
1900 .next = gpiolib_seq_next,
1901 .stop = gpiolib_seq_stop,
1902 .show = gpiolib_seq_show,
1903};
1904
David Brownelld2876d02008-02-04 22:28:20 -08001905static int gpiolib_open(struct inode *inode, struct file *file)
1906{
Thierry Redingf9c4a312012-04-12 13:26:01 +02001907 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08001908}
1909
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001910static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02001911 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08001912 .open = gpiolib_open,
1913 .read = seq_read,
1914 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02001915 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08001916};
1917
1918static int __init gpiolib_debugfs_init(void)
1919{
1920 /* /sys/kernel/debug/gpio */
1921 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1922 NULL, NULL, &gpiolib_operations);
1923 return 0;
1924}
1925subsys_initcall(gpiolib_debugfs_init);
1926
1927#endif /* DEBUG_FS */