blob: 8ccf68bb8a40ec0084909a01868460731ff3c8a3 [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
David Brownelld2876d02008-02-04 22:28:20 -080015
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060016#define CREATE_TRACE_POINTS
17#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080018
19/* Optional implementation infrastructure for GPIO interfaces.
20 *
21 * Platforms may want to use this if they tend to use very many GPIOs
22 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
23 *
24 * When kernel footprint or instruction count is an issue, simpler
25 * implementations may be preferred. The GPIO programming interface
26 * allows for inlining speed-critical get/set operations for common
27 * cases, so that access to SOC-integrated GPIOs can sometimes cost
28 * only an instruction or two per bit.
29 */
30
31
32/* When debugging, extend minimal trust to callers and platform code.
33 * Also emit diagnostic messages that may help initial bringup, when
34 * board setup or driver bugs are most common.
35 *
36 * Otherwise, minimize overhead in what may be bitbanging codepaths.
37 */
38#ifdef DEBUG
39#define extra_checks 1
40#else
41#define extra_checks 0
42#endif
43
44/* gpio_lock prevents conflicts during gpio_desc[] table updates.
45 * While any GPIO is requested, its gpio_chip is not removable;
46 * each GPIO's "requested" flag serves as a lock and refcount.
47 */
48static DEFINE_SPINLOCK(gpio_lock);
49
50struct gpio_desc {
51 struct gpio_chip *chip;
52 unsigned long flags;
53/* flag symbols are bit numbers */
54#define FLAG_REQUESTED 0
55#define FLAG_IS_OUT 1
Alexandre Courbot710b40e2013-02-02 23:44:06 +090056#define FLAG_EXPORT 2 /* protected by sysfs_lock */
57#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
58#define FLAG_TRIG_FALL 4 /* trigger on falling edge */
59#define FLAG_TRIG_RISE 5 /* trigger on rising edge */
60#define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */
61#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
62#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070063
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070064#define ID_SHIFT 16 /* add new flags before this one */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070065
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070066#define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
Daniel Glöcknerff77c352009-09-22 16:46:38 -070067#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
David Brownelld2876d02008-02-04 22:28:20 -080068
69#ifdef CONFIG_DEBUG_FS
70 const char *label;
71#endif
72};
73static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
74
Alexandre Courbot1a989d02013-02-03 01:29:24 +090075static LIST_HEAD(gpio_chips);
76
Daniel Glöcknerff77c352009-09-22 16:46:38 -070077#ifdef CONFIG_GPIO_SYSFS
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070078static DEFINE_IDR(dirent_idr);
Daniel Glöcknerff77c352009-09-22 16:46:38 -070079#endif
80
David Brownelld2876d02008-02-04 22:28:20 -080081static inline void desc_set_label(struct gpio_desc *d, const char *label)
82{
83#ifdef CONFIG_DEBUG_FS
84 d->label = label;
85#endif
86}
87
88/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
89 * when setting direction, and otherwise illegal. Until board setup code
90 * and drivers use explicit requests everywhere (which won't happen when
91 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -070092 * message should motivate switching to explicit requests... so should
93 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -070094 *
95 * NOTE: the autorequest mechanism is going away; at this point it's
96 * only "legal" in the sense that (old) code using it won't break yet,
97 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -080098 */
David Brownell35e8bb52008-10-15 22:03:16 -070099static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
David Brownelld2876d02008-02-04 22:28:20 -0800100{
David Brownell8a0cecf2009-04-02 16:57:06 -0700101 const struct gpio_chip *chip = desc->chip;
102 const int gpio = chip->base + offset;
David Brownell35e8bb52008-10-15 22:03:16 -0700103
David Brownell8a0cecf2009-04-02 16:57:06 -0700104 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
105 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700106 if (!try_module_get(chip->owner)) {
107 pr_err("GPIO-%d: module can't be gotten \n", gpio);
108 clear_bit(FLAG_REQUESTED, &desc->flags);
109 /* lose */
110 return -EIO;
111 }
David Brownelld2876d02008-02-04 22:28:20 -0800112 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700113 /* caller must chip->request() w/o spinlock */
114 if (chip->request)
115 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800116 }
David Brownell35e8bb52008-10-15 22:03:16 -0700117 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800118}
119
120/* caller holds gpio_lock *OR* gpio is marked as requested */
Grant Likely1a2d3972011-12-12 09:25:57 -0700121struct gpio_chip *gpio_to_chip(unsigned gpio)
David Brownelld2876d02008-02-04 22:28:20 -0800122{
123 return gpio_desc[gpio].chip;
124}
125
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700126/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
127static int gpiochip_find_base(int ngpio)
128{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900129 struct gpio_chip *chip;
130 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700131
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900132 list_for_each_entry_reverse(chip, &gpio_chips, list) {
133 /* found a free space? */
134 if (chip->base + chip->ngpio <= base)
135 break;
136 else
137 /* nope, check the space right before the chip */
138 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700139 }
140
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900141 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700142 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900143 return base;
144 } else {
145 pr_err("%s: cannot find free range\n", __func__);
146 return -ENOSPC;
147 }
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700148}
149
Mathias Nyman80b0a602012-10-24 17:25:27 +0300150/* caller ensures gpio is valid and requested, chip->get_direction may sleep */
151static int gpio_get_direction(unsigned gpio)
152{
153 struct gpio_chip *chip;
154 struct gpio_desc *desc = &gpio_desc[gpio];
155 int status = -EINVAL;
156
157 chip = gpio_to_chip(gpio);
158 gpio -= chip->base;
159
160 if (!chip->get_direction)
161 return status;
162
163 status = chip->get_direction(chip, gpio);
164 if (status > 0) {
165 /* GPIOF_DIR_IN, or other positive */
166 status = 1;
167 clear_bit(FLAG_IS_OUT, &desc->flags);
168 }
169 if (status == 0) {
170 /* GPIOF_DIR_OUT */
171 set_bit(FLAG_IS_OUT, &desc->flags);
172 }
173 return status;
174}
175
David Brownelld8f388d82008-07-25 01:46:07 -0700176#ifdef CONFIG_GPIO_SYSFS
177
178/* lock protects against unexport_gpio() being called while
179 * sysfs files are active.
180 */
181static DEFINE_MUTEX(sysfs_lock);
182
183/*
184 * /sys/class/gpio/gpioN... only for GPIOs that are exported
185 * /direction
186 * * MAY BE OMITTED if kernel won't allow direction changes
187 * * is read/write as "in" or "out"
188 * * may also be written as "high" or "low", initializing
189 * output value as specified ("out" implies "low")
190 * /value
191 * * always readable, subject to hardware behavior
192 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700193 * /edge
194 * * configures behavior of poll(2) on /value
195 * * available only if pin can generate IRQs on input
196 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800197 * /active_low
198 * * configures polarity of /value
199 * * is read/write as zero/nonzero
200 * * also affects existing and subsequent "falling" and "rising"
201 * /edge configuration
David Brownelld8f388d82008-07-25 01:46:07 -0700202 */
203
204static ssize_t gpio_direction_show(struct device *dev,
205 struct device_attribute *attr, char *buf)
206{
207 const struct gpio_desc *desc = dev_get_drvdata(dev);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300208 unsigned gpio = desc - gpio_desc;
David Brownelld8f388d82008-07-25 01:46:07 -0700209 ssize_t status;
210
211 mutex_lock(&sysfs_lock);
212
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900213 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700214 status = -EIO;
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900215 } else {
Mathias Nyman80b0a602012-10-24 17:25:27 +0300216 gpio_get_direction(gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700217 status = sprintf(buf, "%s\n",
218 test_bit(FLAG_IS_OUT, &desc->flags)
219 ? "out" : "in");
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900220 }
David Brownelld8f388d82008-07-25 01:46:07 -0700221
222 mutex_unlock(&sysfs_lock);
223 return status;
224}
225
226static ssize_t gpio_direction_store(struct device *dev,
227 struct device_attribute *attr, const char *buf, size_t size)
228{
229 const struct gpio_desc *desc = dev_get_drvdata(dev);
230 unsigned gpio = desc - gpio_desc;
231 ssize_t status;
232
233 mutex_lock(&sysfs_lock);
234
235 if (!test_bit(FLAG_EXPORT, &desc->flags))
236 status = -EIO;
237 else if (sysfs_streq(buf, "high"))
238 status = gpio_direction_output(gpio, 1);
239 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
240 status = gpio_direction_output(gpio, 0);
241 else if (sysfs_streq(buf, "in"))
242 status = gpio_direction_input(gpio);
243 else
244 status = -EINVAL;
245
246 mutex_unlock(&sysfs_lock);
247 return status ? : size;
248}
249
Jani Nikula07697462009-12-15 16:46:20 -0800250static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700251 gpio_direction_show, gpio_direction_store);
252
253static ssize_t gpio_value_show(struct device *dev,
254 struct device_attribute *attr, char *buf)
255{
256 const struct gpio_desc *desc = dev_get_drvdata(dev);
257 unsigned gpio = desc - gpio_desc;
258 ssize_t status;
259
260 mutex_lock(&sysfs_lock);
261
Jani Nikula07697462009-12-15 16:46:20 -0800262 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700263 status = -EIO;
Jani Nikula07697462009-12-15 16:46:20 -0800264 } else {
265 int value;
266
267 value = !!gpio_get_value_cansleep(gpio);
268 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
269 value = !value;
270
271 status = sprintf(buf, "%d\n", value);
272 }
David Brownelld8f388d82008-07-25 01:46:07 -0700273
274 mutex_unlock(&sysfs_lock);
275 return status;
276}
277
278static ssize_t gpio_value_store(struct device *dev,
279 struct device_attribute *attr, const char *buf, size_t size)
280{
281 const struct gpio_desc *desc = dev_get_drvdata(dev);
282 unsigned gpio = desc - gpio_desc;
283 ssize_t status;
284
285 mutex_lock(&sysfs_lock);
286
287 if (!test_bit(FLAG_EXPORT, &desc->flags))
288 status = -EIO;
289 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
290 status = -EPERM;
291 else {
292 long value;
293
294 status = strict_strtol(buf, 0, &value);
295 if (status == 0) {
Jani Nikula07697462009-12-15 16:46:20 -0800296 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
297 value = !value;
David Brownelld8f388d82008-07-25 01:46:07 -0700298 gpio_set_value_cansleep(gpio, value != 0);
299 status = size;
300 }
301 }
302
303 mutex_unlock(&sysfs_lock);
304 return status;
305}
306
Jani Nikula07697462009-12-15 16:46:20 -0800307static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700308 gpio_value_show, gpio_value_store);
309
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700310static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
311{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700312 struct sysfs_dirent *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700313
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700314 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700315 return IRQ_HANDLED;
316}
317
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700318static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
319 unsigned long gpio_flags)
320{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700321 struct sysfs_dirent *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700322 unsigned long irq_flags;
323 int ret, irq, id;
324
325 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
326 return 0;
327
328 irq = gpio_to_irq(desc - gpio_desc);
329 if (irq < 0)
330 return -EIO;
331
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700332 id = desc->flags >> ID_SHIFT;
333 value_sd = idr_find(&dirent_idr, id);
334 if (value_sd)
335 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700336
337 desc->flags &= ~GPIO_TRIGGER_MASK;
338
339 if (!gpio_flags) {
340 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700341 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700342 }
343
344 irq_flags = IRQF_SHARED;
345 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800346 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
347 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700348 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800349 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
350 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700351
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700352 if (!value_sd) {
353 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
354 if (!value_sd) {
355 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700356 goto err_out;
357 }
358
359 do {
360 ret = -ENOMEM;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700361 if (idr_pre_get(&dirent_idr, GFP_KERNEL))
362 ret = idr_get_new_above(&dirent_idr, value_sd,
363 1, &id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700364 } while (ret == -EAGAIN);
365
366 if (ret)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700367 goto free_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700368
369 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700370 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700371
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700372 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700373 ret = -ERANGE;
374 goto free_id;
375 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700376 }
377
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700378 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700379 "gpiolib", value_sd);
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700380 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700381 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700382
383 desc->flags |= gpio_flags;
384 return 0;
385
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700386free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700387 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700388 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700389free_sd:
390 if (value_sd)
391 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700392err_out:
393 return ret;
394}
395
396static const struct {
397 const char *name;
398 unsigned long flags;
399} trigger_types[] = {
400 { "none", 0 },
401 { "falling", BIT(FLAG_TRIG_FALL) },
402 { "rising", BIT(FLAG_TRIG_RISE) },
403 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
404};
405
406static ssize_t gpio_edge_show(struct device *dev,
407 struct device_attribute *attr, char *buf)
408{
409 const struct gpio_desc *desc = dev_get_drvdata(dev);
410 ssize_t status;
411
412 mutex_lock(&sysfs_lock);
413
414 if (!test_bit(FLAG_EXPORT, &desc->flags))
415 status = -EIO;
416 else {
417 int i;
418
419 status = 0;
420 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
421 if ((desc->flags & GPIO_TRIGGER_MASK)
422 == trigger_types[i].flags) {
423 status = sprintf(buf, "%s\n",
424 trigger_types[i].name);
425 break;
426 }
427 }
428
429 mutex_unlock(&sysfs_lock);
430 return status;
431}
432
433static ssize_t gpio_edge_store(struct device *dev,
434 struct device_attribute *attr, const char *buf, size_t size)
435{
436 struct gpio_desc *desc = dev_get_drvdata(dev);
437 ssize_t status;
438 int i;
439
440 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
441 if (sysfs_streq(trigger_types[i].name, buf))
442 goto found;
443 return -EINVAL;
444
445found:
446 mutex_lock(&sysfs_lock);
447
448 if (!test_bit(FLAG_EXPORT, &desc->flags))
449 status = -EIO;
450 else {
451 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
452 if (!status)
453 status = size;
454 }
455
456 mutex_unlock(&sysfs_lock);
457
458 return status;
459}
460
461static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
462
Jani Nikula07697462009-12-15 16:46:20 -0800463static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
464 int value)
465{
466 int status = 0;
467
468 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
469 return 0;
470
471 if (value)
472 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
473 else
474 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
475
476 /* reconfigure poll(2) support if enabled on one edge only */
477 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
478 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
479 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
480
481 gpio_setup_irq(desc, dev, 0);
482 status = gpio_setup_irq(desc, dev, trigger_flags);
483 }
484
485 return status;
486}
487
488static ssize_t gpio_active_low_show(struct device *dev,
489 struct device_attribute *attr, char *buf)
490{
491 const struct gpio_desc *desc = dev_get_drvdata(dev);
492 ssize_t status;
493
494 mutex_lock(&sysfs_lock);
495
496 if (!test_bit(FLAG_EXPORT, &desc->flags))
497 status = -EIO;
498 else
499 status = sprintf(buf, "%d\n",
500 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
501
502 mutex_unlock(&sysfs_lock);
503
504 return status;
505}
506
507static ssize_t gpio_active_low_store(struct device *dev,
508 struct device_attribute *attr, const char *buf, size_t size)
509{
510 struct gpio_desc *desc = dev_get_drvdata(dev);
511 ssize_t status;
512
513 mutex_lock(&sysfs_lock);
514
515 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
516 status = -EIO;
517 } else {
518 long value;
519
520 status = strict_strtol(buf, 0, &value);
521 if (status == 0)
522 status = sysfs_set_active_low(desc, dev, value != 0);
523 }
524
525 mutex_unlock(&sysfs_lock);
526
527 return status ? : size;
528}
529
530static const DEVICE_ATTR(active_low, 0644,
531 gpio_active_low_show, gpio_active_low_store);
532
David Brownelld8f388d82008-07-25 01:46:07 -0700533static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d82008-07-25 01:46:07 -0700534 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800535 &dev_attr_active_low.attr,
David Brownelld8f388d82008-07-25 01:46:07 -0700536 NULL,
537};
538
539static const struct attribute_group gpio_attr_group = {
540 .attrs = (struct attribute **) gpio_attrs,
541};
542
543/*
544 * /sys/class/gpio/gpiochipN/
545 * /base ... matching gpio_chip.base (N)
546 * /label ... matching gpio_chip.label
547 * /ngpio ... matching gpio_chip.ngpio
548 */
549
550static ssize_t chip_base_show(struct device *dev,
551 struct device_attribute *attr, char *buf)
552{
553 const struct gpio_chip *chip = dev_get_drvdata(dev);
554
555 return sprintf(buf, "%d\n", chip->base);
556}
557static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
558
559static ssize_t chip_label_show(struct device *dev,
560 struct device_attribute *attr, char *buf)
561{
562 const struct gpio_chip *chip = dev_get_drvdata(dev);
563
564 return sprintf(buf, "%s\n", chip->label ? : "");
565}
566static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
567
568static ssize_t chip_ngpio_show(struct device *dev,
569 struct device_attribute *attr, char *buf)
570{
571 const struct gpio_chip *chip = dev_get_drvdata(dev);
572
573 return sprintf(buf, "%u\n", chip->ngpio);
574}
575static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
576
577static const struct attribute *gpiochip_attrs[] = {
578 &dev_attr_base.attr,
579 &dev_attr_label.attr,
580 &dev_attr_ngpio.attr,
581 NULL,
582};
583
584static const struct attribute_group gpiochip_attr_group = {
585 .attrs = (struct attribute **) gpiochip_attrs,
586};
587
588/*
589 * /sys/class/gpio/export ... write-only
590 * integer N ... number of GPIO to export (full access)
591 * /sys/class/gpio/unexport ... write-only
592 * integer N ... number of GPIO to unexport
593 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100594static ssize_t export_store(struct class *class,
595 struct class_attribute *attr,
596 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700597{
598 long gpio;
599 int status;
600
601 status = strict_strtol(buf, 0, &gpio);
602 if (status < 0)
603 goto done;
604
605 /* No extra locking here; FLAG_SYSFS just signifies that the
606 * request and export were done by on behalf of userspace, so
607 * they may be undone on its behalf too.
608 */
609
610 status = gpio_request(gpio, "sysfs");
Mathias Nymanad2fab32012-10-25 14:03:03 +0300611 if (status < 0) {
612 if (status == -EPROBE_DEFER)
613 status = -ENODEV;
David Brownelld8f388d82008-07-25 01:46:07 -0700614 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +0300615 }
David Brownelld8f388d82008-07-25 01:46:07 -0700616 status = gpio_export(gpio, true);
617 if (status < 0)
618 gpio_free(gpio);
619 else
620 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
621
622done:
623 if (status)
624 pr_debug("%s: status %d\n", __func__, status);
625 return status ? : len;
626}
627
Andi Kleen28812fe2010-01-05 12:48:07 +0100628static ssize_t unexport_store(struct class *class,
629 struct class_attribute *attr,
630 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700631{
632 long gpio;
633 int status;
634
635 status = strict_strtol(buf, 0, &gpio);
636 if (status < 0)
637 goto done;
638
639 status = -EINVAL;
640
641 /* reject bogus commands (gpio_unexport ignores them) */
642 if (!gpio_is_valid(gpio))
643 goto done;
644
645 /* No extra locking here; FLAG_SYSFS just signifies that the
646 * request and export were done by on behalf of userspace, so
647 * they may be undone on its behalf too.
648 */
649 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
650 status = 0;
651 gpio_free(gpio);
652 }
653done:
654 if (status)
655 pr_debug("%s: status %d\n", __func__, status);
656 return status ? : len;
657}
658
659static struct class_attribute gpio_class_attrs[] = {
660 __ATTR(export, 0200, NULL, export_store),
661 __ATTR(unexport, 0200, NULL, unexport_store),
662 __ATTR_NULL,
663};
664
665static struct class gpio_class = {
666 .name = "gpio",
667 .owner = THIS_MODULE,
668
669 .class_attrs = gpio_class_attrs,
670};
671
672
673/**
674 * gpio_export - export a GPIO through sysfs
675 * @gpio: gpio to make available, already requested
676 * @direction_may_change: true if userspace may change gpio direction
677 * Context: arch_initcall or later
678 *
679 * When drivers want to make a GPIO accessible to userspace after they
680 * have requested it -- perhaps while debugging, or as part of their
681 * public interface -- they may use this routine. If the GPIO can
682 * change direction (some can't) and the caller allows it, userspace
683 * will see "direction" sysfs attribute which may be used to change
684 * the gpio's direction. A "value" attribute will always be provided.
685 *
686 * Returns zero on success, else an error.
687 */
688int gpio_export(unsigned gpio, bool direction_may_change)
689{
690 unsigned long flags;
691 struct gpio_desc *desc;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100692 int status;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700693 const char *ioname = NULL;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100694 struct device *dev;
David Brownelld8f388d82008-07-25 01:46:07 -0700695
696 /* can't export until sysfs is available ... */
697 if (!gpio_class.p) {
698 pr_debug("%s: called too early!\n", __func__);
699 return -ENOENT;
700 }
701
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100702 if (!gpio_is_valid(gpio)) {
703 pr_debug("%s: gpio %d is not valid\n", __func__, gpio);
704 return -EINVAL;
705 }
David Brownelld8f388d82008-07-25 01:46:07 -0700706
707 mutex_lock(&sysfs_lock);
708
709 spin_lock_irqsave(&gpio_lock, flags);
710 desc = &gpio_desc[gpio];
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100711 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
712 test_bit(FLAG_EXPORT, &desc->flags)) {
713 spin_unlock_irqrestore(&gpio_lock, flags);
714 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
715 __func__, gpio,
716 test_bit(FLAG_REQUESTED, &desc->flags),
717 test_bit(FLAG_EXPORT, &desc->flags));
Dan Carpenter529f2ad2012-10-26 09:59:43 +0300718 status = -EPERM;
719 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700720 }
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100721
722 if (!desc->chip->direction_input || !desc->chip->direction_output)
723 direction_may_change = false;
David Brownelld8f388d82008-07-25 01:46:07 -0700724 spin_unlock_irqrestore(&gpio_lock, flags);
725
Daniel Silverstone926b663c2009-04-02 16:57:05 -0700726 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
727 ioname = desc->chip->names[gpio - desc->chip->base];
728
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100729 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
730 desc, ioname ? ioname : "gpio%u", gpio);
731 if (IS_ERR(dev)) {
732 status = PTR_ERR(dev);
733 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700734 }
735
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100736 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
David Brownelld8f388d82008-07-25 01:46:07 -0700737 if (status)
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100738 goto fail_unregister_device;
David Brownelld8f388d82008-07-25 01:46:07 -0700739
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100740 if (direction_may_change) {
741 status = device_create_file(dev, &dev_attr_direction);
742 if (status)
743 goto fail_unregister_device;
744 }
745
746 if (gpio_to_irq(gpio) >= 0 && (direction_may_change ||
747 !test_bit(FLAG_IS_OUT, &desc->flags))) {
748 status = device_create_file(dev, &dev_attr_edge);
749 if (status)
750 goto fail_unregister_device;
751 }
752
753 set_bit(FLAG_EXPORT, &desc->flags);
754 mutex_unlock(&sysfs_lock);
755 return 0;
756
757fail_unregister_device:
758 device_unregister(dev);
759fail_unlock:
760 mutex_unlock(&sysfs_lock);
761 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
David Brownelld8f388d82008-07-25 01:46:07 -0700762 return status;
763}
764EXPORT_SYMBOL_GPL(gpio_export);
765
766static int match_export(struct device *dev, void *data)
767{
768 return dev_get_drvdata(dev) == data;
769}
770
771/**
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700772 * gpio_export_link - create a sysfs link to an exported GPIO node
773 * @dev: device under which to create symlink
774 * @name: name of the symlink
775 * @gpio: gpio to create symlink to, already exported
776 *
777 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
778 * node. Caller is responsible for unlinking.
779 *
780 * Returns zero on success, else an error.
781 */
782int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
783{
784 struct gpio_desc *desc;
785 int status = -EINVAL;
786
787 if (!gpio_is_valid(gpio))
788 goto done;
789
790 mutex_lock(&sysfs_lock);
791
792 desc = &gpio_desc[gpio];
793
794 if (test_bit(FLAG_EXPORT, &desc->flags)) {
795 struct device *tdev;
796
797 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
798 if (tdev != NULL) {
799 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
800 name);
801 } else {
802 status = -ENODEV;
803 }
804 }
805
806 mutex_unlock(&sysfs_lock);
807
808done:
809 if (status)
810 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
811
812 return status;
813}
814EXPORT_SYMBOL_GPL(gpio_export_link);
815
Jani Nikula07697462009-12-15 16:46:20 -0800816
817/**
818 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
819 * @gpio: gpio to change
820 * @value: non-zero to use active low, i.e. inverted values
821 *
822 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
823 * The GPIO does not have to be exported yet. If poll(2) support has
824 * been enabled for either rising or falling edge, it will be
825 * reconfigured to follow the new polarity.
826 *
827 * Returns zero on success, else an error.
828 */
829int gpio_sysfs_set_active_low(unsigned gpio, int value)
830{
831 struct gpio_desc *desc;
832 struct device *dev = NULL;
833 int status = -EINVAL;
834
835 if (!gpio_is_valid(gpio))
836 goto done;
837
838 mutex_lock(&sysfs_lock);
839
840 desc = &gpio_desc[gpio];
841
842 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800843 dev = class_find_device(&gpio_class, NULL, desc, match_export);
844 if (dev == NULL) {
845 status = -ENODEV;
846 goto unlock;
847 }
848 }
849
850 status = sysfs_set_active_low(desc, dev, value);
851
852unlock:
853 mutex_unlock(&sysfs_lock);
854
855done:
856 if (status)
857 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
858
859 return status;
860}
861EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
862
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700863/**
David Brownelld8f388d82008-07-25 01:46:07 -0700864 * gpio_unexport - reverse effect of gpio_export()
865 * @gpio: gpio to make unavailable
866 *
867 * This is implicit on gpio_free().
868 */
869void gpio_unexport(unsigned gpio)
870{
871 struct gpio_desc *desc;
Jon Povey6a99ad42010-07-27 13:18:06 -0700872 int status = 0;
Ming Lei864533c2012-02-13 22:53:20 +0800873 struct device *dev = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -0700874
Jon Povey6a99ad42010-07-27 13:18:06 -0700875 if (!gpio_is_valid(gpio)) {
876 status = -EINVAL;
David Brownelld8f388d82008-07-25 01:46:07 -0700877 goto done;
Jon Povey6a99ad42010-07-27 13:18:06 -0700878 }
David Brownelld8f388d82008-07-25 01:46:07 -0700879
880 mutex_lock(&sysfs_lock);
881
882 desc = &gpio_desc[gpio];
Daniel Silverstone926b663c2009-04-02 16:57:05 -0700883
David Brownelld8f388d82008-07-25 01:46:07 -0700884 if (test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700885
886 dev = class_find_device(&gpio_class, NULL, desc, match_export);
887 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700888 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700889 clear_bit(FLAG_EXPORT, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700890 } else
891 status = -ENODEV;
892 }
893
894 mutex_unlock(&sysfs_lock);
Ming Lei864533c2012-02-13 22:53:20 +0800895 if (dev) {
896 device_unregister(dev);
897 put_device(dev);
898 }
David Brownelld8f388d82008-07-25 01:46:07 -0700899done:
900 if (status)
901 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
902}
903EXPORT_SYMBOL_GPL(gpio_unexport);
904
905static int gpiochip_export(struct gpio_chip *chip)
906{
907 int status;
908 struct device *dev;
909
910 /* Many systems register gpio chips for SOC support very early,
911 * before driver model support is available. In those cases we
912 * export this later, in gpiolib_sysfs_init() ... here we just
913 * verify that _some_ field of gpio_class got initialized.
914 */
915 if (!gpio_class.p)
916 return 0;
917
918 /* use chip->base for the ID; it's already known to be unique */
919 mutex_lock(&sysfs_lock);
920 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
921 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800922 if (!IS_ERR(dev)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700923 status = sysfs_create_group(&dev->kobj,
924 &gpiochip_attr_group);
925 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800926 status = PTR_ERR(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700927 chip->exported = (status == 0);
928 mutex_unlock(&sysfs_lock);
929
930 if (status) {
931 unsigned long flags;
932 unsigned gpio;
933
934 spin_lock_irqsave(&gpio_lock, flags);
935 gpio = chip->base;
936 while (gpio_desc[gpio].chip == chip)
937 gpio_desc[gpio++].chip = NULL;
938 spin_unlock_irqrestore(&gpio_lock, flags);
939
940 pr_debug("%s: chip %s status %d\n", __func__,
941 chip->label, status);
942 }
943
944 return status;
945}
946
947static void gpiochip_unexport(struct gpio_chip *chip)
948{
949 int status;
950 struct device *dev;
951
952 mutex_lock(&sysfs_lock);
953 dev = class_find_device(&gpio_class, NULL, chip, match_export);
954 if (dev) {
955 put_device(dev);
956 device_unregister(dev);
957 chip->exported = 0;
958 status = 0;
959 } else
960 status = -ENODEV;
961 mutex_unlock(&sysfs_lock);
962
963 if (status)
964 pr_debug("%s: chip %s status %d\n", __func__,
965 chip->label, status);
966}
967
968static int __init gpiolib_sysfs_init(void)
969{
970 int status;
971 unsigned long flags;
Alexandre Courbot65493e32013-02-03 01:29:25 +0900972 struct gpio_chip *chip;
David Brownelld8f388d82008-07-25 01:46:07 -0700973
974 status = class_register(&gpio_class);
975 if (status < 0)
976 return status;
977
978 /* Scan and register the gpio_chips which registered very
979 * early (e.g. before the class_register above was called).
980 *
981 * We run before arch_initcall() so chip->dev nodes can have
982 * registered, and so arch_initcall() can always gpio_export().
983 */
984 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot65493e32013-02-03 01:29:25 +0900985 list_for_each_entry(chip, &gpio_chips, list) {
David Brownelld8f388d82008-07-25 01:46:07 -0700986 if (!chip || chip->exported)
987 continue;
988
989 spin_unlock_irqrestore(&gpio_lock, flags);
990 status = gpiochip_export(chip);
991 spin_lock_irqsave(&gpio_lock, flags);
992 }
993 spin_unlock_irqrestore(&gpio_lock, flags);
994
995
996 return status;
997}
998postcore_initcall(gpiolib_sysfs_init);
999
1000#else
1001static inline int gpiochip_export(struct gpio_chip *chip)
1002{
1003 return 0;
1004}
1005
1006static inline void gpiochip_unexport(struct gpio_chip *chip)
1007{
1008}
1009
1010#endif /* CONFIG_GPIO_SYSFS */
1011
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001012/*
1013 * Add a new chip to the global chips list, keeping the list of chips sorted
1014 * by base order.
1015 *
1016 * Return -EBUSY if the new chip overlaps with some other chip's integer
1017 * space.
1018 */
1019static int gpiochip_add_to_list(struct gpio_chip *chip)
1020{
1021 struct list_head *pos = &gpio_chips;
1022 struct gpio_chip *_chip;
1023 int err = 0;
1024
1025 /* find where to insert our chip */
1026 list_for_each(pos, &gpio_chips) {
1027 _chip = list_entry(pos, struct gpio_chip, list);
1028 /* shall we insert before _chip? */
1029 if (_chip->base >= chip->base + chip->ngpio)
1030 break;
1031 }
1032
1033 /* are we stepping on the chip right before? */
1034 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1035 _chip = list_entry(pos->prev, struct gpio_chip, list);
1036 if (_chip->base + _chip->ngpio > chip->base) {
1037 dev_err(chip->dev,
1038 "GPIO integer space overlap, cannot add chip\n");
1039 err = -EBUSY;
1040 }
1041 }
1042
1043 if (!err)
1044 list_add_tail(&chip->list, pos);
1045
1046 return err;
1047}
1048
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001049/**
David Brownelld2876d02008-02-04 22:28:20 -08001050 * gpiochip_add() - register a gpio_chip
1051 * @chip: the chip to register, with chip->base initialized
1052 * Context: potentially before irqs or kmalloc will work
1053 *
1054 * Returns a negative errno if the chip can't be registered, such as
1055 * because the chip->base is invalid or already associated with a
1056 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001057 *
David Brownelld8f388d82008-07-25 01:46:07 -07001058 * When gpiochip_add() is called very early during boot, so that GPIOs
1059 * can be freely used, the chip->dev device must be registered before
1060 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1061 * for GPIOs will fail rudely.
1062 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001063 * If chip->base is negative, this requests dynamic assignment of
1064 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001065 */
1066int gpiochip_add(struct gpio_chip *chip)
1067{
1068 unsigned long flags;
1069 int status = 0;
1070 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001071 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001072
Trent Piephobff5fda2008-05-23 13:04:44 -07001073 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001074 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001075 status = -EINVAL;
1076 goto fail;
1077 }
1078
1079 spin_lock_irqsave(&gpio_lock, flags);
1080
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001081 if (base < 0) {
1082 base = gpiochip_find_base(chip->ngpio);
1083 if (base < 0) {
1084 status = base;
David Brownelld8f388d82008-07-25 01:46:07 -07001085 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001086 }
1087 chip->base = base;
1088 }
1089
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001090 status = gpiochip_add_to_list(chip);
1091
David Brownelld2876d02008-02-04 22:28:20 -08001092 if (status == 0) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001093 for (id = base; id < base + chip->ngpio; id++) {
David Brownelld2876d02008-02-04 22:28:20 -08001094 gpio_desc[id].chip = chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001095
1096 /* REVISIT: most hardware initializes GPIOs as
1097 * inputs (often with pullups enabled) so power
1098 * usage is minimized. Linux code should set the
1099 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +03001100 * and in case chip->get_direction is not set,
David Brownelld8f388d82008-07-25 01:46:07 -07001101 * we may expose the wrong direction in sysfs.
1102 */
1103 gpio_desc[id].flags = !chip->direction_input
1104 ? (1 << FLAG_IS_OUT)
1105 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001106 }
1107 }
1108
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301109#ifdef CONFIG_PINCTRL
1110 INIT_LIST_HEAD(&chip->pin_ranges);
1111#endif
1112
Anton Vorontsov391c9702010-06-08 07:48:17 -06001113 of_gpiochip_add(chip);
1114
David Brownelld8f388d82008-07-25 01:46:07 -07001115unlock:
David Brownelld2876d02008-02-04 22:28:20 -08001116 spin_unlock_irqrestore(&gpio_lock, flags);
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001117
1118 if (status)
1119 goto fail;
1120
1121 status = gpiochip_export(chip);
1122 if (status)
1123 goto fail;
1124
H Hartley Sweetenee1c1e72012-05-11 16:58:33 -07001125 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
Grant Likely64842aa2011-11-06 11:36:18 -07001126 chip->base, chip->base + chip->ngpio - 1,
1127 chip->label ? : "generic");
1128
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001129 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001130fail:
1131 /* failures here can mean systems won't boot... */
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001132 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1133 chip->base, chip->base + chip->ngpio - 1,
1134 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001135 return status;
1136}
1137EXPORT_SYMBOL_GPL(gpiochip_add);
1138
1139/**
1140 * gpiochip_remove() - unregister a gpio_chip
1141 * @chip: the chip to unregister
1142 *
1143 * A gpio_chip with any GPIOs still requested may not be removed.
1144 */
1145int gpiochip_remove(struct gpio_chip *chip)
1146{
1147 unsigned long flags;
1148 int status = 0;
1149 unsigned id;
1150
1151 spin_lock_irqsave(&gpio_lock, flags);
1152
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001153 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001154 of_gpiochip_remove(chip);
1155
David Brownelld2876d02008-02-04 22:28:20 -08001156 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1157 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1158 status = -EBUSY;
1159 break;
1160 }
1161 }
1162 if (status == 0) {
1163 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1164 gpio_desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001165
1166 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -08001167 }
1168
1169 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d82008-07-25 01:46:07 -07001170
1171 if (status == 0)
1172 gpiochip_unexport(chip);
1173
David Brownelld2876d02008-02-04 22:28:20 -08001174 return status;
1175}
1176EXPORT_SYMBOL_GPL(gpiochip_remove);
1177
Grant Likely594fa262010-06-08 07:48:16 -06001178/**
1179 * gpiochip_find() - iterator for locating a specific gpio_chip
1180 * @data: data to pass to match function
1181 * @callback: Callback function to check gpio_chip
1182 *
1183 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1184 * determined by a user supplied @match callback. The callback should return
1185 * 0 if the device doesn't match and non-zero if it does. If the callback is
1186 * non-zero, this function will return to the caller and not iterate over any
1187 * more gpio_chips.
1188 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001189struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001190 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001191 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001192{
Alexandre Courbot125eef92013-02-03 01:29:26 +09001193 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001194 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001195
1196 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +09001197 list_for_each_entry(chip, &gpio_chips, list)
1198 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001199 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001200
1201 /* No match? */
1202 if (&chip->list == &gpio_chips)
1203 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001204 spin_unlock_irqrestore(&gpio_lock, flags);
1205
1206 return chip;
1207}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001208EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001209
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301210#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001211
Linus Walleij3f0f8672012-11-20 12:40:15 +01001212/**
1213 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1214 * @chip: the gpiochip to add the range for
1215 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001216 * @gpio_offset: the start offset in the current gpio_chip number space
1217 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001218 * @npins: the number of pins from the offset of each pin space (GPIO and
1219 * pin controller) to accumulate in this range
1220 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001221int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001222 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001223 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301224{
1225 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001226 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301227
Linus Walleij3f0f8672012-11-20 12:40:15 +01001228 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301229 if (!pin_range) {
1230 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1231 chip->label);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001232 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301233 }
1234
Linus Walleij3f0f8672012-11-20 12:40:15 +01001235 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001236 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001237 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301238 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001239 pin_range->range.base = chip->base + gpio_offset;
1240 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301241 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001242 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301243 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001244 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001245 ret = PTR_ERR(pin_range->pctldev);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001246 pr_err("%s: GPIO chip: could not create pin range\n",
1247 chip->label);
1248 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001249 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001250 }
Linus Walleij316511c2012-11-21 08:48:09 +01001251 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1252 chip->label, gpio_offset, gpio_offset + npins - 1,
1253 pinctl_name,
1254 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301255
1256 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001257
1258 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301259}
Linus Walleij165adc92012-11-06 14:49:39 +01001260EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301261
Linus Walleij3f0f8672012-11-20 12:40:15 +01001262/**
1263 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1264 * @chip: the chip to remove all the mappings for
1265 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301266void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1267{
1268 struct gpio_pin_range *pin_range, *tmp;
1269
1270 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1271 list_del(&pin_range->node);
1272 pinctrl_remove_gpio_range(pin_range->pctldev,
1273 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001274 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301275 }
1276}
Linus Walleij165adc92012-11-06 14:49:39 +01001277EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1278
1279#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301280
David Brownelld2876d02008-02-04 22:28:20 -08001281/* These "optional" allocation calls help prevent drivers from stomping
1282 * on each other, and help provide better diagnostics in debugfs.
1283 * They're called even less than the "set direction" calls.
1284 */
1285int gpio_request(unsigned gpio, const char *label)
1286{
1287 struct gpio_desc *desc;
David Brownell35e8bb52008-10-15 22:03:16 -07001288 struct gpio_chip *chip;
Mark Browne9354572012-07-09 12:22:56 +01001289 int status = -EPROBE_DEFER;
David Brownelld2876d02008-02-04 22:28:20 -08001290 unsigned long flags;
1291
1292 spin_lock_irqsave(&gpio_lock, flags);
1293
Mathias Nymanad2fab32012-10-25 14:03:03 +03001294 if (!gpio_is_valid(gpio)) {
1295 status = -EINVAL;
David Brownelld2876d02008-02-04 22:28:20 -08001296 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +03001297 }
David Brownelld2876d02008-02-04 22:28:20 -08001298 desc = &gpio_desc[gpio];
David Brownell35e8bb52008-10-15 22:03:16 -07001299 chip = desc->chip;
1300 if (chip == NULL)
David Brownelld2876d02008-02-04 22:28:20 -08001301 goto done;
1302
David Brownell35e8bb52008-10-15 22:03:16 -07001303 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001304 goto done;
1305
David Brownelld2876d02008-02-04 22:28:20 -08001306 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001307 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001308 */
1309
1310 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1311 desc_set_label(desc, label ? : "?");
1312 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001313 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001314 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001315 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001316 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001317 }
1318
1319 if (chip->request) {
1320 /* chip->request may sleep */
1321 spin_unlock_irqrestore(&gpio_lock, flags);
1322 status = chip->request(chip, gpio - chip->base);
1323 spin_lock_irqsave(&gpio_lock, flags);
1324
1325 if (status < 0) {
1326 desc_set_label(desc, NULL);
1327 module_put(chip->owner);
1328 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001329 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001330 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001331 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001332 if (chip->get_direction) {
1333 /* chip->get_direction may sleep */
1334 spin_unlock_irqrestore(&gpio_lock, flags);
1335 gpio_get_direction(gpio);
1336 spin_lock_irqsave(&gpio_lock, flags);
1337 }
David Brownelld2876d02008-02-04 22:28:20 -08001338done:
1339 if (status)
1340 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1341 gpio, label ? : "?", status);
1342 spin_unlock_irqrestore(&gpio_lock, flags);
1343 return status;
1344}
1345EXPORT_SYMBOL_GPL(gpio_request);
1346
1347void gpio_free(unsigned gpio)
1348{
1349 unsigned long flags;
1350 struct gpio_desc *desc;
David Brownell35e8bb52008-10-15 22:03:16 -07001351 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001352
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001353 might_sleep();
1354
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001355 if (!gpio_is_valid(gpio)) {
David Brownelld2876d02008-02-04 22:28:20 -08001356 WARN_ON(extra_checks);
1357 return;
1358 }
1359
David Brownelld8f388d82008-07-25 01:46:07 -07001360 gpio_unexport(gpio);
1361
David Brownelld2876d02008-02-04 22:28:20 -08001362 spin_lock_irqsave(&gpio_lock, flags);
1363
1364 desc = &gpio_desc[gpio];
David Brownell35e8bb52008-10-15 22:03:16 -07001365 chip = desc->chip;
1366 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1367 if (chip->free) {
1368 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001369 might_sleep_if(chip->can_sleep);
David Brownell35e8bb52008-10-15 22:03:16 -07001370 chip->free(chip, gpio - chip->base);
1371 spin_lock_irqsave(&gpio_lock, flags);
1372 }
David Brownelld2876d02008-02-04 22:28:20 -08001373 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001374 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001375 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001376 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301377 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301378 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001379 } else
David Brownelld2876d02008-02-04 22:28:20 -08001380 WARN_ON(extra_checks);
1381
1382 spin_unlock_irqrestore(&gpio_lock, flags);
1383}
1384EXPORT_SYMBOL_GPL(gpio_free);
1385
Eric Miao3e45f1d2010-03-05 13:44:35 -08001386/**
1387 * gpio_request_one - request a single GPIO with initial configuration
1388 * @gpio: the GPIO number
1389 * @flags: GPIO configuration as specified by GPIOF_*
1390 * @label: a literal description string of this GPIO
1391 */
1392int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1393{
1394 int err;
1395
1396 err = gpio_request(gpio, label);
1397 if (err)
1398 return err;
1399
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301400 if (flags & GPIOF_OPEN_DRAIN)
1401 set_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags);
1402
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301403 if (flags & GPIOF_OPEN_SOURCE)
1404 set_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags);
1405
Eric Miao3e45f1d2010-03-05 13:44:35 -08001406 if (flags & GPIOF_DIR_IN)
1407 err = gpio_direction_input(gpio);
1408 else
1409 err = gpio_direction_output(gpio,
1410 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1411
Aaro Koskinene2548112010-12-21 17:24:22 -08001412 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001413 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -08001414
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001415 if (flags & GPIOF_EXPORT) {
1416 err = gpio_export(gpio, flags & GPIOF_EXPORT_CHANGEABLE);
1417 if (err)
1418 goto free_gpio;
1419 }
1420
1421 return 0;
1422
1423 free_gpio:
1424 gpio_free(gpio);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001425 return err;
1426}
1427EXPORT_SYMBOL_GPL(gpio_request_one);
1428
1429/**
1430 * gpio_request_array - request multiple GPIOs in a single call
1431 * @array: array of the 'struct gpio'
1432 * @num: how many GPIOs in the array
1433 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001434int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001435{
1436 int i, err;
1437
1438 for (i = 0; i < num; i++, array++) {
1439 err = gpio_request_one(array->gpio, array->flags, array->label);
1440 if (err)
1441 goto err_free;
1442 }
1443 return 0;
1444
1445err_free:
1446 while (i--)
1447 gpio_free((--array)->gpio);
1448 return err;
1449}
1450EXPORT_SYMBOL_GPL(gpio_request_array);
1451
1452/**
1453 * gpio_free_array - release multiple GPIOs in a single call
1454 * @array: array of the 'struct gpio'
1455 * @num: how many GPIOs in the array
1456 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001457void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001458{
1459 while (num--)
1460 gpio_free((array++)->gpio);
1461}
1462EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001463
1464/**
1465 * gpiochip_is_requested - return string iff signal was requested
1466 * @chip: controller managing the signal
1467 * @offset: of signal within controller's 0..(ngpio - 1) range
1468 *
1469 * Returns NULL if the GPIO is not currently requested, else a string.
1470 * If debugfs support is enabled, the string returned is the label passed
1471 * to gpio_request(); otherwise it is a meaningless constant.
1472 *
1473 * This function is for use by GPIO controller drivers. The label can
1474 * help with diagnostics, and knowing that the signal is used as a GPIO
1475 * can help avoid accidentally multiplexing it to another controller.
1476 */
1477const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1478{
1479 unsigned gpio = chip->base + offset;
1480
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001481 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
David Brownelld2876d02008-02-04 22:28:20 -08001482 return NULL;
1483 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1484 return NULL;
1485#ifdef CONFIG_DEBUG_FS
1486 return gpio_desc[gpio].label;
1487#else
1488 return "?";
1489#endif
1490}
1491EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1492
1493
1494/* Drivers MUST set GPIO direction before making get/set calls. In
1495 * some cases this is done in early boot, before IRQs are enabled.
1496 *
1497 * As a rule these aren't called more than once (except for drivers
1498 * using the open-drain emulation idiom) so these are natural places
1499 * to accumulate extra debugging checks. Note that we can't (yet)
1500 * rely on gpio_request() having been called beforehand.
1501 */
1502
1503int gpio_direction_input(unsigned gpio)
1504{
1505 unsigned long flags;
1506 struct gpio_chip *chip;
1507 struct gpio_desc *desc = &gpio_desc[gpio];
1508 int status = -EINVAL;
1509
1510 spin_lock_irqsave(&gpio_lock, flags);
1511
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001512 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001513 goto fail;
1514 chip = desc->chip;
1515 if (!chip || !chip->get || !chip->direction_input)
1516 goto fail;
1517 gpio -= chip->base;
1518 if (gpio >= chip->ngpio)
1519 goto fail;
David Brownell35e8bb52008-10-15 22:03:16 -07001520 status = gpio_ensure_requested(desc, gpio);
1521 if (status < 0)
1522 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001523
1524 /* now we know the gpio is valid and chip won't vanish */
1525
1526 spin_unlock_irqrestore(&gpio_lock, flags);
1527
David Brownell9c4ba942010-08-10 18:02:24 -07001528 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001529
David Brownell35e8bb52008-10-15 22:03:16 -07001530 if (status) {
1531 status = chip->request(chip, gpio);
1532 if (status < 0) {
1533 pr_debug("GPIO-%d: chip request fail, %d\n",
1534 chip->base + gpio, status);
1535 /* and it's not available to anyone else ...
1536 * gpio_request() is the fully clean solution.
1537 */
1538 goto lose;
1539 }
1540 }
1541
David Brownelld2876d02008-02-04 22:28:20 -08001542 status = chip->direction_input(chip, gpio);
1543 if (status == 0)
1544 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001545
1546 trace_gpio_direction(chip->base + gpio, 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001547lose:
David Brownelld2876d02008-02-04 22:28:20 -08001548 return status;
1549fail:
1550 spin_unlock_irqrestore(&gpio_lock, flags);
1551 if (status)
1552 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001553 __func__, gpio, status);
David Brownelld2876d02008-02-04 22:28:20 -08001554 return status;
1555}
1556EXPORT_SYMBOL_GPL(gpio_direction_input);
1557
1558int gpio_direction_output(unsigned gpio, int value)
1559{
1560 unsigned long flags;
1561 struct gpio_chip *chip;
1562 struct gpio_desc *desc = &gpio_desc[gpio];
1563 int status = -EINVAL;
1564
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301565 /* Open drain pin should not be driven to 1 */
1566 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1567 return gpio_direction_input(gpio);
1568
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301569 /* Open source pin should not be driven to 0 */
1570 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1571 return gpio_direction_input(gpio);
1572
David Brownelld2876d02008-02-04 22:28:20 -08001573 spin_lock_irqsave(&gpio_lock, flags);
1574
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07001575 if (!gpio_is_valid(gpio))
David Brownelld2876d02008-02-04 22:28:20 -08001576 goto fail;
1577 chip = desc->chip;
1578 if (!chip || !chip->set || !chip->direction_output)
1579 goto fail;
1580 gpio -= chip->base;
1581 if (gpio >= chip->ngpio)
1582 goto fail;
David Brownell35e8bb52008-10-15 22:03:16 -07001583 status = gpio_ensure_requested(desc, gpio);
1584 if (status < 0)
1585 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001586
1587 /* now we know the gpio is valid and chip won't vanish */
1588
1589 spin_unlock_irqrestore(&gpio_lock, flags);
1590
David Brownell9c4ba942010-08-10 18:02:24 -07001591 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001592
David Brownell35e8bb52008-10-15 22:03:16 -07001593 if (status) {
1594 status = chip->request(chip, gpio);
1595 if (status < 0) {
1596 pr_debug("GPIO-%d: chip request fail, %d\n",
1597 chip->base + gpio, status);
1598 /* and it's not available to anyone else ...
1599 * gpio_request() is the fully clean solution.
1600 */
1601 goto lose;
1602 }
1603 }
1604
David Brownelld2876d02008-02-04 22:28:20 -08001605 status = chip->direction_output(chip, gpio, value);
1606 if (status == 0)
1607 set_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001608 trace_gpio_value(chip->base + gpio, 0, value);
1609 trace_gpio_direction(chip->base + gpio, 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001610lose:
David Brownelld2876d02008-02-04 22:28:20 -08001611 return status;
1612fail:
1613 spin_unlock_irqrestore(&gpio_lock, flags);
1614 if (status)
1615 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001616 __func__, gpio, status);
David Brownelld2876d02008-02-04 22:28:20 -08001617 return status;
1618}
1619EXPORT_SYMBOL_GPL(gpio_direction_output);
1620
Felipe Balbic4b5be92010-05-26 14:42:23 -07001621/**
1622 * gpio_set_debounce - sets @debounce time for a @gpio
1623 * @gpio: the gpio to set debounce time
1624 * @debounce: debounce time is microseconds
1625 */
1626int gpio_set_debounce(unsigned gpio, unsigned debounce)
1627{
1628 unsigned long flags;
1629 struct gpio_chip *chip;
1630 struct gpio_desc *desc = &gpio_desc[gpio];
1631 int status = -EINVAL;
1632
1633 spin_lock_irqsave(&gpio_lock, flags);
1634
1635 if (!gpio_is_valid(gpio))
1636 goto fail;
1637 chip = desc->chip;
1638 if (!chip || !chip->set || !chip->set_debounce)
1639 goto fail;
1640 gpio -= chip->base;
1641 if (gpio >= chip->ngpio)
1642 goto fail;
1643 status = gpio_ensure_requested(desc, gpio);
1644 if (status < 0)
1645 goto fail;
1646
1647 /* now we know the gpio is valid and chip won't vanish */
1648
1649 spin_unlock_irqrestore(&gpio_lock, flags);
1650
David Brownell9c4ba942010-08-10 18:02:24 -07001651 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001652
1653 return chip->set_debounce(chip, gpio, debounce);
1654
1655fail:
1656 spin_unlock_irqrestore(&gpio_lock, flags);
1657 if (status)
1658 pr_debug("%s: gpio-%d status %d\n",
1659 __func__, gpio, status);
1660
1661 return status;
1662}
1663EXPORT_SYMBOL_GPL(gpio_set_debounce);
David Brownelld2876d02008-02-04 22:28:20 -08001664
1665/* I/O calls are only valid after configuration completed; the relevant
1666 * "is this a valid GPIO" error checks should already have been done.
1667 *
1668 * "Get" operations are often inlinable as reading a pin value register,
1669 * and masking the relevant bit in that register.
1670 *
1671 * When "set" operations are inlinable, they involve writing that mask to
1672 * one register to set a low value, or a different register to set it high.
1673 * Otherwise locking is needed, so there may be little value to inlining.
1674 *
1675 *------------------------------------------------------------------------
1676 *
1677 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1678 * have requested the GPIO. That can include implicit requesting by
1679 * a direction setting call. Marking a gpio as requested locks its chip
1680 * in memory, guaranteeing that these table lookups need no more locking
1681 * and that gpiochip_remove() will fail.
1682 *
1683 * REVISIT when debugging, consider adding some instrumentation to ensure
1684 * that the GPIO was actually requested.
1685 */
1686
1687/**
1688 * __gpio_get_value() - return a gpio's value
1689 * @gpio: gpio whose value will be returned
1690 * Context: any
1691 *
1692 * This is used directly or indirectly to implement gpio_get_value().
1693 * It returns the zero or nonzero value provided by the associated
1694 * gpio_chip.get() method; or zero if no such method is provided.
1695 */
1696int __gpio_get_value(unsigned gpio)
1697{
1698 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001699 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001700
1701 chip = gpio_to_chip(gpio);
Mark Browne4e449e2012-02-17 10:46:00 -08001702 /* Should be using gpio_get_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001703 WARN_ON(chip->can_sleep);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001704 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1705 trace_gpio_value(gpio, 1, value);
1706 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001707}
1708EXPORT_SYMBOL_GPL(__gpio_get_value);
1709
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301710/*
1711 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1712 * @gpio: Gpio whose state need to be set.
1713 * @chip: Gpio chip.
1714 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1715 */
1716static void _gpio_set_open_drain_value(unsigned gpio,
1717 struct gpio_chip *chip, int value)
1718{
1719 int err = 0;
1720 if (value) {
1721 err = chip->direction_input(chip, gpio - chip->base);
1722 if (!err)
1723 clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1724 } else {
1725 err = chip->direction_output(chip, gpio - chip->base, 0);
1726 if (!err)
1727 set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1728 }
1729 trace_gpio_direction(gpio, value, err);
1730 if (err < 0)
1731 pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
1732 __func__, gpio, err);
1733}
1734
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301735/*
1736 * _gpio_set_open_source() - Set the open source gpio's value.
1737 * @gpio: Gpio whose state need to be set.
1738 * @chip: Gpio chip.
1739 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1740 */
1741static void _gpio_set_open_source_value(unsigned gpio,
1742 struct gpio_chip *chip, int value)
1743{
1744 int err = 0;
1745 if (value) {
1746 err = chip->direction_output(chip, gpio - chip->base, 1);
1747 if (!err)
1748 set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1749 } else {
1750 err = chip->direction_input(chip, gpio - chip->base);
1751 if (!err)
1752 clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1753 }
1754 trace_gpio_direction(gpio, !value, err);
1755 if (err < 0)
1756 pr_err("%s: Error in set_value for open source gpio%d err %d\n",
1757 __func__, gpio, err);
1758}
1759
1760
David Brownelld2876d02008-02-04 22:28:20 -08001761/**
1762 * __gpio_set_value() - assign a gpio's value
1763 * @gpio: gpio whose value will be assigned
1764 * @value: value to assign
1765 * Context: any
1766 *
1767 * This is used directly or indirectly to implement gpio_set_value().
1768 * It invokes the associated gpio_chip.set() method.
1769 */
1770void __gpio_set_value(unsigned gpio, int value)
1771{
1772 struct gpio_chip *chip;
1773
1774 chip = gpio_to_chip(gpio);
Mark Browne4e449e2012-02-17 10:46:00 -08001775 /* Should be using gpio_set_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001776 WARN_ON(chip->can_sleep);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001777 trace_gpio_value(gpio, 0, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301778 if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
1779 _gpio_set_open_drain_value(gpio, chip, value);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301780 else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
1781 _gpio_set_open_source_value(gpio, chip, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301782 else
1783 chip->set(chip, gpio - chip->base, value);
David Brownelld2876d02008-02-04 22:28:20 -08001784}
1785EXPORT_SYMBOL_GPL(__gpio_set_value);
1786
1787/**
1788 * __gpio_cansleep() - report whether gpio value access will sleep
1789 * @gpio: gpio in question
1790 * Context: any
1791 *
1792 * This is used directly or indirectly to implement gpio_cansleep(). It
1793 * returns nonzero if access reading or writing the GPIO value can sleep.
1794 */
1795int __gpio_cansleep(unsigned gpio)
1796{
1797 struct gpio_chip *chip;
1798
1799 /* only call this on GPIOs that are valid! */
1800 chip = gpio_to_chip(gpio);
1801
1802 return chip->can_sleep;
1803}
1804EXPORT_SYMBOL_GPL(__gpio_cansleep);
1805
David Brownell0f6d5042008-10-15 22:03:14 -07001806/**
1807 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1808 * @gpio: gpio whose IRQ will be returned (already requested)
1809 * Context: any
1810 *
1811 * This is used directly or indirectly to implement gpio_to_irq().
1812 * It returns the number of the IRQ signaled by this (input) GPIO,
1813 * or a negative errno.
1814 */
1815int __gpio_to_irq(unsigned gpio)
1816{
1817 struct gpio_chip *chip;
1818
1819 chip = gpio_to_chip(gpio);
1820 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1821}
1822EXPORT_SYMBOL_GPL(__gpio_to_irq);
1823
David Brownelld2876d02008-02-04 22:28:20 -08001824
1825
1826/* There's no value in making it easy to inline GPIO calls that may sleep.
1827 * Common examples include ones connected to I2C or SPI chips.
1828 */
1829
1830int gpio_get_value_cansleep(unsigned gpio)
1831{
1832 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001833 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001834
1835 might_sleep_if(extra_checks);
1836 chip = gpio_to_chip(gpio);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001837 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1838 trace_gpio_value(gpio, 1, value);
1839 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001840}
1841EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1842
1843void gpio_set_value_cansleep(unsigned gpio, int value)
1844{
1845 struct gpio_chip *chip;
1846
1847 might_sleep_if(extra_checks);
1848 chip = gpio_to_chip(gpio);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001849 trace_gpio_value(gpio, 0, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301850 if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
1851 _gpio_set_open_drain_value(gpio, chip, value);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301852 else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
1853 _gpio_set_open_source_value(gpio, chip, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301854 else
1855 chip->set(chip, gpio - chip->base, value);
David Brownelld2876d02008-02-04 22:28:20 -08001856}
1857EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1858
1859
1860#ifdef CONFIG_DEBUG_FS
1861
David Brownelld2876d02008-02-04 22:28:20 -08001862static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1863{
1864 unsigned i;
1865 unsigned gpio = chip->base;
1866 struct gpio_desc *gdesc = &gpio_desc[gpio];
1867 int is_out;
1868
1869 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1870 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1871 continue;
1872
Mathias Nyman80b0a602012-10-24 17:25:27 +03001873 gpio_get_direction(gpio);
David Brownelld2876d02008-02-04 22:28:20 -08001874 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Jarkko Nikula6e8ba722008-11-19 15:36:17 -08001875 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08001876 gpio, gdesc->label,
1877 is_out ? "out" : "in ",
1878 chip->get
1879 ? (chip->get(chip, i) ? "hi" : "lo")
1880 : "? ");
David Brownelld2876d02008-02-04 22:28:20 -08001881 seq_printf(s, "\n");
1882 }
1883}
1884
Thierry Redingf9c4a312012-04-12 13:26:01 +02001885static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08001886{
Thierry Redingf9c4a312012-04-12 13:26:01 +02001887 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001888 loff_t index = *pos;
David Brownelld2876d02008-02-04 22:28:20 -08001889
1890 /* REVISIT this isn't locked against gpio_chip removal ... */
1891
Thierry Redingf9c4a312012-04-12 13:26:01 +02001892 s->private = "";
1893
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001894 list_for_each_entry(chip, &gpio_chips, list)
1895 if (index-- == 0)
1896 return chip;
1897
1898 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001899}
1900
1901static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
1902{
1903 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001904 void *ret = NULL;
1905
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001906 if (list_is_last(&chip->list, &gpio_chips))
1907 ret = NULL;
1908 else
1909 ret = list_entry(chip->list.next, struct gpio_chip, list);
Thierry Redingf9c4a312012-04-12 13:26:01 +02001910
1911 s->private = "\n";
1912 ++*pos;
1913
1914 return ret;
1915}
1916
1917static void gpiolib_seq_stop(struct seq_file *s, void *v)
1918{
1919}
1920
1921static int gpiolib_seq_show(struct seq_file *s, void *v)
1922{
1923 struct gpio_chip *chip = v;
1924 struct device *dev;
1925
1926 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
1927 chip->base, chip->base + chip->ngpio - 1);
1928 dev = chip->dev;
1929 if (dev)
1930 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
1931 dev_name(dev));
1932 if (chip->label)
1933 seq_printf(s, ", %s", chip->label);
1934 if (chip->can_sleep)
1935 seq_printf(s, ", can sleep");
1936 seq_printf(s, ":\n");
1937
1938 if (chip->dbg_show)
1939 chip->dbg_show(s, chip);
1940 else
1941 gpiolib_dbg_show(s, chip);
1942
David Brownelld2876d02008-02-04 22:28:20 -08001943 return 0;
1944}
1945
Thierry Redingf9c4a312012-04-12 13:26:01 +02001946static const struct seq_operations gpiolib_seq_ops = {
1947 .start = gpiolib_seq_start,
1948 .next = gpiolib_seq_next,
1949 .stop = gpiolib_seq_stop,
1950 .show = gpiolib_seq_show,
1951};
1952
David Brownelld2876d02008-02-04 22:28:20 -08001953static int gpiolib_open(struct inode *inode, struct file *file)
1954{
Thierry Redingf9c4a312012-04-12 13:26:01 +02001955 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08001956}
1957
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001958static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02001959 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08001960 .open = gpiolib_open,
1961 .read = seq_read,
1962 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02001963 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08001964};
1965
1966static int __init gpiolib_debugfs_init(void)
1967{
1968 /* /sys/kernel/debug/gpio */
1969 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1970 NULL, NULL, &gpiolib_operations);
1971 return 0;
1972}
1973subsys_initcall(gpiolib_debugfs_init);
1974
1975#endif /* DEBUG_FS */