blob: 1a8a7a8f803f0e2f3f94761294e8d5e35052a89d [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
David Brownelld2876d02008-02-04 22:28:20 -080015
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060016#define CREATE_TRACE_POINTS
17#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080018
19/* Optional implementation infrastructure for GPIO interfaces.
20 *
21 * Platforms may want to use this if they tend to use very many GPIOs
22 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
23 *
24 * When kernel footprint or instruction count is an issue, simpler
25 * implementations may be preferred. The GPIO programming interface
26 * allows for inlining speed-critical get/set operations for common
27 * cases, so that access to SOC-integrated GPIOs can sometimes cost
28 * only an instruction or two per bit.
29 */
30
31
32/* When debugging, extend minimal trust to callers and platform code.
33 * Also emit diagnostic messages that may help initial bringup, when
34 * board setup or driver bugs are most common.
35 *
36 * Otherwise, minimize overhead in what may be bitbanging codepaths.
37 */
38#ifdef DEBUG
39#define extra_checks 1
40#else
41#define extra_checks 0
42#endif
43
44/* gpio_lock prevents conflicts during gpio_desc[] table updates.
45 * While any GPIO is requested, its gpio_chip is not removable;
46 * each GPIO's "requested" flag serves as a lock and refcount.
47 */
48static DEFINE_SPINLOCK(gpio_lock);
49
50struct gpio_desc {
51 struct gpio_chip *chip;
52 unsigned long flags;
53/* flag symbols are bit numbers */
54#define FLAG_REQUESTED 0
55#define FLAG_IS_OUT 1
Alexandre Courbot710b40e2013-02-02 23:44:06 +090056#define FLAG_EXPORT 2 /* protected by sysfs_lock */
57#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
58#define FLAG_TRIG_FALL 4 /* trigger on falling edge */
59#define FLAG_TRIG_RISE 5 /* trigger on rising edge */
60#define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */
61#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
62#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070063
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070064#define ID_SHIFT 16 /* add new flags before this one */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070065
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070066#define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
Daniel Glöcknerff77c352009-09-22 16:46:38 -070067#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
David Brownelld2876d02008-02-04 22:28:20 -080068
69#ifdef CONFIG_DEBUG_FS
70 const char *label;
71#endif
72};
73static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
74
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +090075#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
76
Alexandre Courbot1a989d02013-02-03 01:29:24 +090077static LIST_HEAD(gpio_chips);
78
Daniel Glöcknerff77c352009-09-22 16:46:38 -070079#ifdef CONFIG_GPIO_SYSFS
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070080static DEFINE_IDR(dirent_idr);
Daniel Glöcknerff77c352009-09-22 16:46:38 -070081#endif
82
Alexandre Courbot372e7222013-02-03 01:29:29 +090083/*
84 * Internal gpiod_* API using descriptors instead of the integer namespace.
85 * Most of this should eventually go public.
86 */
87static int gpiod_request(struct gpio_desc *desc, const char *label);
88static void gpiod_free(struct gpio_desc *desc);
89static int gpiod_direction_input(struct gpio_desc *desc);
90static int gpiod_direction_output(struct gpio_desc *desc, int value);
91static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
92static int gpiod_get_value_cansleep(struct gpio_desc *desc);
93static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
94static int gpiod_get_value(struct gpio_desc *desc);
95static void gpiod_set_value(struct gpio_desc *desc, int value);
96static int gpiod_cansleep(struct gpio_desc *desc);
97static int gpiod_to_irq(struct gpio_desc *desc);
98static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
99static int gpiod_export_link(struct device *dev, const char *name,
100 struct gpio_desc *desc);
101static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
102static void gpiod_unexport(struct gpio_desc *desc);
103
104
David Brownelld2876d02008-02-04 22:28:20 -0800105static inline void desc_set_label(struct gpio_desc *d, const char *label)
106{
107#ifdef CONFIG_DEBUG_FS
108 d->label = label;
109#endif
110}
111
Alexandre Courbot372e7222013-02-03 01:29:29 +0900112/*
113 * Return the GPIO number of the passed descriptor relative to its chip
114 */
115static int gpio_chip_hwgpio(const struct gpio_desc *desc)
116{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900117 return desc - &desc->chip->desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900118}
119
120/**
121 * Convert a GPIO number to its descriptor
122 */
123static struct gpio_desc *gpio_to_desc(unsigned gpio)
124{
125 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
126 return NULL;
127 else
128 return &gpio_desc[gpio];
129}
130
131/**
132 * Convert a GPIO descriptor to the integer namespace.
133 * This should disappear in the future but is needed since we still
134 * use GPIO numbers for error messages and sysfs nodes
135 */
136static int desc_to_gpio(const struct gpio_desc *desc)
137{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900138 return desc->chip->base + gpio_chip_hwgpio(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900139}
140
141
David Brownelld2876d02008-02-04 22:28:20 -0800142/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
143 * when setting direction, and otherwise illegal. Until board setup code
144 * and drivers use explicit requests everywhere (which won't happen when
145 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -0700146 * message should motivate switching to explicit requests... so should
147 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -0700148 *
149 * NOTE: the autorequest mechanism is going away; at this point it's
150 * only "legal" in the sense that (old) code using it won't break yet,
151 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -0800152 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900153static int gpio_ensure_requested(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800154{
David Brownell8a0cecf2009-04-02 16:57:06 -0700155 const struct gpio_chip *chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900156 const int gpio = desc_to_gpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -0700157
David Brownell8a0cecf2009-04-02 16:57:06 -0700158 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
159 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700160 if (!try_module_get(chip->owner)) {
161 pr_err("GPIO-%d: module can't be gotten \n", gpio);
162 clear_bit(FLAG_REQUESTED, &desc->flags);
163 /* lose */
164 return -EIO;
165 }
David Brownelld2876d02008-02-04 22:28:20 -0800166 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700167 /* caller must chip->request() w/o spinlock */
168 if (chip->request)
169 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800170 }
David Brownell35e8bb52008-10-15 22:03:16 -0700171 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800172}
173
174/* caller holds gpio_lock *OR* gpio is marked as requested */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900175static struct gpio_chip *gpiod_to_chip(struct gpio_desc *desc)
176{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900177 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900178}
179
Grant Likely1a2d3972011-12-12 09:25:57 -0700180struct gpio_chip *gpio_to_chip(unsigned gpio)
David Brownelld2876d02008-02-04 22:28:20 -0800181{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900182 return gpiod_to_chip(gpio_to_desc(gpio));
David Brownelld2876d02008-02-04 22:28:20 -0800183}
184
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700185/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
186static int gpiochip_find_base(int ngpio)
187{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900188 struct gpio_chip *chip;
189 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700190
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900191 list_for_each_entry_reverse(chip, &gpio_chips, list) {
192 /* found a free space? */
193 if (chip->base + chip->ngpio <= base)
194 break;
195 else
196 /* nope, check the space right before the chip */
197 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700198 }
199
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900200 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700201 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900202 return base;
203 } else {
204 pr_err("%s: cannot find free range\n", __func__);
205 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700206 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700207}
208
Mathias Nyman80b0a602012-10-24 17:25:27 +0300209/* caller ensures gpio is valid and requested, chip->get_direction may sleep */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900210static int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300211{
212 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900213 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300214 int status = -EINVAL;
215
Alexandre Courbot372e7222013-02-03 01:29:29 +0900216 chip = gpiod_to_chip(desc);
217 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300218
219 if (!chip->get_direction)
220 return status;
221
Alexandre Courbot372e7222013-02-03 01:29:29 +0900222 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300223 if (status > 0) {
224 /* GPIOF_DIR_IN, or other positive */
225 status = 1;
226 clear_bit(FLAG_IS_OUT, &desc->flags);
227 }
228 if (status == 0) {
229 /* GPIOF_DIR_OUT */
230 set_bit(FLAG_IS_OUT, &desc->flags);
231 }
232 return status;
233}
234
David Brownelld8f388d82008-07-25 01:46:07 -0700235#ifdef CONFIG_GPIO_SYSFS
236
237/* lock protects against unexport_gpio() being called while
238 * sysfs files are active.
239 */
240static DEFINE_MUTEX(sysfs_lock);
241
242/*
243 * /sys/class/gpio/gpioN... only for GPIOs that are exported
244 * /direction
245 * * MAY BE OMITTED if kernel won't allow direction changes
246 * * is read/write as "in" or "out"
247 * * may also be written as "high" or "low", initializing
248 * output value as specified ("out" implies "low")
249 * /value
250 * * always readable, subject to hardware behavior
251 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700252 * /edge
253 * * configures behavior of poll(2) on /value
254 * * available only if pin can generate IRQs on input
255 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800256 * /active_low
257 * * configures polarity of /value
258 * * is read/write as zero/nonzero
259 * * also affects existing and subsequent "falling" and "rising"
260 * /edge configuration
David Brownelld8f388d82008-07-25 01:46:07 -0700261 */
262
263static ssize_t gpio_direction_show(struct device *dev,
264 struct device_attribute *attr, char *buf)
265{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900266 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700267 ssize_t status;
268
269 mutex_lock(&sysfs_lock);
270
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900271 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700272 status = -EIO;
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900273 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +0900274 gpiod_get_direction(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700275 status = sprintf(buf, "%s\n",
276 test_bit(FLAG_IS_OUT, &desc->flags)
277 ? "out" : "in");
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900278 }
David Brownelld8f388d82008-07-25 01:46:07 -0700279
280 mutex_unlock(&sysfs_lock);
281 return status;
282}
283
284static ssize_t gpio_direction_store(struct device *dev,
285 struct device_attribute *attr, const char *buf, size_t size)
286{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900287 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700288 ssize_t status;
289
290 mutex_lock(&sysfs_lock);
291
292 if (!test_bit(FLAG_EXPORT, &desc->flags))
293 status = -EIO;
294 else if (sysfs_streq(buf, "high"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900295 status = gpiod_direction_output(desc, 1);
David Brownelld8f388d82008-07-25 01:46:07 -0700296 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900297 status = gpiod_direction_output(desc, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700298 else if (sysfs_streq(buf, "in"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900299 status = gpiod_direction_input(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700300 else
301 status = -EINVAL;
302
303 mutex_unlock(&sysfs_lock);
304 return status ? : size;
305}
306
Jani Nikula07697462009-12-15 16:46:20 -0800307static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700308 gpio_direction_show, gpio_direction_store);
309
310static ssize_t gpio_value_show(struct device *dev,
311 struct device_attribute *attr, char *buf)
312{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900313 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700314 ssize_t status;
315
316 mutex_lock(&sysfs_lock);
317
Jani Nikula07697462009-12-15 16:46:20 -0800318 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700319 status = -EIO;
Jani Nikula07697462009-12-15 16:46:20 -0800320 } else {
321 int value;
322
Alexandre Courbot372e7222013-02-03 01:29:29 +0900323 value = !!gpiod_get_value_cansleep(desc);
Jani Nikula07697462009-12-15 16:46:20 -0800324 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
325 value = !value;
326
327 status = sprintf(buf, "%d\n", value);
328 }
David Brownelld8f388d82008-07-25 01:46:07 -0700329
330 mutex_unlock(&sysfs_lock);
331 return status;
332}
333
334static ssize_t gpio_value_store(struct device *dev,
335 struct device_attribute *attr, const char *buf, size_t size)
336{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900337 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700338 ssize_t status;
339
340 mutex_lock(&sysfs_lock);
341
342 if (!test_bit(FLAG_EXPORT, &desc->flags))
343 status = -EIO;
344 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
345 status = -EPERM;
346 else {
347 long value;
348
349 status = strict_strtol(buf, 0, &value);
350 if (status == 0) {
Jani Nikula07697462009-12-15 16:46:20 -0800351 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
352 value = !value;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900353 gpiod_set_value_cansleep(desc, value != 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700354 status = size;
355 }
356 }
357
358 mutex_unlock(&sysfs_lock);
359 return status;
360}
361
Jani Nikula07697462009-12-15 16:46:20 -0800362static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700363 gpio_value_show, gpio_value_store);
364
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700365static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
366{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700367 struct sysfs_dirent *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700368
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700369 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700370 return IRQ_HANDLED;
371}
372
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700373static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
374 unsigned long gpio_flags)
375{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700376 struct sysfs_dirent *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700377 unsigned long irq_flags;
378 int ret, irq, id;
379
380 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
381 return 0;
382
Alexandre Courbot372e7222013-02-03 01:29:29 +0900383 irq = gpiod_to_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700384 if (irq < 0)
385 return -EIO;
386
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700387 id = desc->flags >> ID_SHIFT;
388 value_sd = idr_find(&dirent_idr, id);
389 if (value_sd)
390 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700391
392 desc->flags &= ~GPIO_TRIGGER_MASK;
393
394 if (!gpio_flags) {
395 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700396 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700397 }
398
399 irq_flags = IRQF_SHARED;
400 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800401 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
402 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700403 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800404 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
405 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700406
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700407 if (!value_sd) {
408 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
409 if (!value_sd) {
410 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700411 goto err_out;
412 }
413
Tejun Heo62f516b2013-02-27 17:04:06 -0800414 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
415 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700416 goto free_sd;
Tejun Heo62f516b2013-02-27 17:04:06 -0800417 id = ret;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700418
419 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700420 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700421
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700422 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700423 ret = -ERANGE;
424 goto free_id;
425 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700426 }
427
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700428 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700429 "gpiolib", value_sd);
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700430 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700431 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700432
433 desc->flags |= gpio_flags;
434 return 0;
435
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700436free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700437 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700438 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700439free_sd:
440 if (value_sd)
441 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700442err_out:
443 return ret;
444}
445
446static const struct {
447 const char *name;
448 unsigned long flags;
449} trigger_types[] = {
450 { "none", 0 },
451 { "falling", BIT(FLAG_TRIG_FALL) },
452 { "rising", BIT(FLAG_TRIG_RISE) },
453 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
454};
455
456static ssize_t gpio_edge_show(struct device *dev,
457 struct device_attribute *attr, char *buf)
458{
459 const struct gpio_desc *desc = dev_get_drvdata(dev);
460 ssize_t status;
461
462 mutex_lock(&sysfs_lock);
463
464 if (!test_bit(FLAG_EXPORT, &desc->flags))
465 status = -EIO;
466 else {
467 int i;
468
469 status = 0;
470 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
471 if ((desc->flags & GPIO_TRIGGER_MASK)
472 == trigger_types[i].flags) {
473 status = sprintf(buf, "%s\n",
474 trigger_types[i].name);
475 break;
476 }
477 }
478
479 mutex_unlock(&sysfs_lock);
480 return status;
481}
482
483static ssize_t gpio_edge_store(struct device *dev,
484 struct device_attribute *attr, const char *buf, size_t size)
485{
486 struct gpio_desc *desc = dev_get_drvdata(dev);
487 ssize_t status;
488 int i;
489
490 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
491 if (sysfs_streq(trigger_types[i].name, buf))
492 goto found;
493 return -EINVAL;
494
495found:
496 mutex_lock(&sysfs_lock);
497
498 if (!test_bit(FLAG_EXPORT, &desc->flags))
499 status = -EIO;
500 else {
501 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
502 if (!status)
503 status = size;
504 }
505
506 mutex_unlock(&sysfs_lock);
507
508 return status;
509}
510
511static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
512
Jani Nikula07697462009-12-15 16:46:20 -0800513static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
514 int value)
515{
516 int status = 0;
517
518 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
519 return 0;
520
521 if (value)
522 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
523 else
524 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
525
526 /* reconfigure poll(2) support if enabled on one edge only */
527 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
528 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
529 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
530
531 gpio_setup_irq(desc, dev, 0);
532 status = gpio_setup_irq(desc, dev, trigger_flags);
533 }
534
535 return status;
536}
537
538static ssize_t gpio_active_low_show(struct device *dev,
539 struct device_attribute *attr, char *buf)
540{
541 const struct gpio_desc *desc = dev_get_drvdata(dev);
542 ssize_t status;
543
544 mutex_lock(&sysfs_lock);
545
546 if (!test_bit(FLAG_EXPORT, &desc->flags))
547 status = -EIO;
548 else
549 status = sprintf(buf, "%d\n",
550 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
551
552 mutex_unlock(&sysfs_lock);
553
554 return status;
555}
556
557static ssize_t gpio_active_low_store(struct device *dev,
558 struct device_attribute *attr, const char *buf, size_t size)
559{
560 struct gpio_desc *desc = dev_get_drvdata(dev);
561 ssize_t status;
562
563 mutex_lock(&sysfs_lock);
564
565 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
566 status = -EIO;
567 } else {
568 long value;
569
570 status = strict_strtol(buf, 0, &value);
571 if (status == 0)
572 status = sysfs_set_active_low(desc, dev, value != 0);
573 }
574
575 mutex_unlock(&sysfs_lock);
576
577 return status ? : size;
578}
579
580static const DEVICE_ATTR(active_low, 0644,
581 gpio_active_low_show, gpio_active_low_store);
582
David Brownelld8f388d82008-07-25 01:46:07 -0700583static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d82008-07-25 01:46:07 -0700584 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800585 &dev_attr_active_low.attr,
David Brownelld8f388d82008-07-25 01:46:07 -0700586 NULL,
587};
588
589static const struct attribute_group gpio_attr_group = {
590 .attrs = (struct attribute **) gpio_attrs,
591};
592
593/*
594 * /sys/class/gpio/gpiochipN/
595 * /base ... matching gpio_chip.base (N)
596 * /label ... matching gpio_chip.label
597 * /ngpio ... matching gpio_chip.ngpio
598 */
599
600static ssize_t chip_base_show(struct device *dev,
601 struct device_attribute *attr, char *buf)
602{
603 const struct gpio_chip *chip = dev_get_drvdata(dev);
604
605 return sprintf(buf, "%d\n", chip->base);
606}
607static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
608
609static ssize_t chip_label_show(struct device *dev,
610 struct device_attribute *attr, char *buf)
611{
612 const struct gpio_chip *chip = dev_get_drvdata(dev);
613
614 return sprintf(buf, "%s\n", chip->label ? : "");
615}
616static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
617
618static ssize_t chip_ngpio_show(struct device *dev,
619 struct device_attribute *attr, char *buf)
620{
621 const struct gpio_chip *chip = dev_get_drvdata(dev);
622
623 return sprintf(buf, "%u\n", chip->ngpio);
624}
625static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
626
627static const struct attribute *gpiochip_attrs[] = {
628 &dev_attr_base.attr,
629 &dev_attr_label.attr,
630 &dev_attr_ngpio.attr,
631 NULL,
632};
633
634static const struct attribute_group gpiochip_attr_group = {
635 .attrs = (struct attribute **) gpiochip_attrs,
636};
637
638/*
639 * /sys/class/gpio/export ... write-only
640 * integer N ... number of GPIO to export (full access)
641 * /sys/class/gpio/unexport ... write-only
642 * integer N ... number of GPIO to unexport
643 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100644static ssize_t export_store(struct class *class,
645 struct class_attribute *attr,
646 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700647{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900648 long gpio;
649 struct gpio_desc *desc;
650 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700651
652 status = strict_strtol(buf, 0, &gpio);
653 if (status < 0)
654 goto done;
655
Alexandre Courbot372e7222013-02-03 01:29:29 +0900656 desc = gpio_to_desc(gpio);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900657 /* reject invalid GPIOs */
658 if (!desc) {
659 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
660 return -EINVAL;
661 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900662
David Brownelld8f388d82008-07-25 01:46:07 -0700663 /* No extra locking here; FLAG_SYSFS just signifies that the
664 * request and export were done by on behalf of userspace, so
665 * they may be undone on its behalf too.
666 */
667
Alexandre Courbot372e7222013-02-03 01:29:29 +0900668 status = gpiod_request(desc, "sysfs");
Mathias Nymanad2fab32012-10-25 14:03:03 +0300669 if (status < 0) {
670 if (status == -EPROBE_DEFER)
671 status = -ENODEV;
David Brownelld8f388d82008-07-25 01:46:07 -0700672 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +0300673 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900674 status = gpiod_export(desc, true);
David Brownelld8f388d82008-07-25 01:46:07 -0700675 if (status < 0)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900676 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700677 else
Alexandre Courbot372e7222013-02-03 01:29:29 +0900678 set_bit(FLAG_SYSFS, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700679
680done:
681 if (status)
682 pr_debug("%s: status %d\n", __func__, status);
683 return status ? : len;
684}
685
Andi Kleen28812fe2010-01-05 12:48:07 +0100686static ssize_t unexport_store(struct class *class,
687 struct class_attribute *attr,
688 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700689{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900690 long gpio;
691 struct gpio_desc *desc;
692 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700693
694 status = strict_strtol(buf, 0, &gpio);
695 if (status < 0)
696 goto done;
697
Alexandre Courbot372e7222013-02-03 01:29:29 +0900698 desc = gpio_to_desc(gpio);
David Brownelld8f388d82008-07-25 01:46:07 -0700699 /* reject bogus commands (gpio_unexport ignores them) */
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900700 if (!desc) {
701 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
702 return -EINVAL;
703 }
704
705 status = -EINVAL;
David Brownelld8f388d82008-07-25 01:46:07 -0700706
707 /* No extra locking here; FLAG_SYSFS just signifies that the
708 * request and export were done by on behalf of userspace, so
709 * they may be undone on its behalf too.
710 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900711 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700712 status = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900713 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700714 }
715done:
716 if (status)
717 pr_debug("%s: status %d\n", __func__, status);
718 return status ? : len;
719}
720
721static struct class_attribute gpio_class_attrs[] = {
722 __ATTR(export, 0200, NULL, export_store),
723 __ATTR(unexport, 0200, NULL, unexport_store),
724 __ATTR_NULL,
725};
726
727static struct class gpio_class = {
728 .name = "gpio",
729 .owner = THIS_MODULE,
730
731 .class_attrs = gpio_class_attrs,
732};
733
734
735/**
736 * gpio_export - export a GPIO through sysfs
737 * @gpio: gpio to make available, already requested
738 * @direction_may_change: true if userspace may change gpio direction
739 * Context: arch_initcall or later
740 *
741 * When drivers want to make a GPIO accessible to userspace after they
742 * have requested it -- perhaps while debugging, or as part of their
743 * public interface -- they may use this routine. If the GPIO can
744 * change direction (some can't) and the caller allows it, userspace
745 * will see "direction" sysfs attribute which may be used to change
746 * the gpio's direction. A "value" attribute will always be provided.
747 *
748 * Returns zero on success, else an error.
749 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900750static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
David Brownelld8f388d82008-07-25 01:46:07 -0700751{
752 unsigned long flags;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100753 int status;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700754 const char *ioname = NULL;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100755 struct device *dev;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900756 int offset;
David Brownelld8f388d82008-07-25 01:46:07 -0700757
758 /* can't export until sysfs is available ... */
759 if (!gpio_class.p) {
760 pr_debug("%s: called too early!\n", __func__);
761 return -ENOENT;
762 }
763
Alexandre Courbot372e7222013-02-03 01:29:29 +0900764 if (!desc) {
765 pr_debug("%s: invalid gpio descriptor\n", __func__);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100766 return -EINVAL;
767 }
David Brownelld8f388d82008-07-25 01:46:07 -0700768
769 mutex_lock(&sysfs_lock);
770
771 spin_lock_irqsave(&gpio_lock, flags);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100772 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
773 test_bit(FLAG_EXPORT, &desc->flags)) {
774 spin_unlock_irqrestore(&gpio_lock, flags);
775 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +0900776 __func__, desc_to_gpio(desc),
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100777 test_bit(FLAG_REQUESTED, &desc->flags),
778 test_bit(FLAG_EXPORT, &desc->flags));
Dan Carpenter529f2ad2012-10-26 09:59:43 +0300779 status = -EPERM;
780 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700781 }
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100782
783 if (!desc->chip->direction_input || !desc->chip->direction_output)
784 direction_may_change = false;
David Brownelld8f388d82008-07-25 01:46:07 -0700785 spin_unlock_irqrestore(&gpio_lock, flags);
786
Alexandre Courbot372e7222013-02-03 01:29:29 +0900787 offset = gpio_chip_hwgpio(desc);
788 if (desc->chip->names && desc->chip->names[offset])
789 ioname = desc->chip->names[offset];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700790
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100791 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Alexandre Courbot372e7222013-02-03 01:29:29 +0900792 desc, ioname ? ioname : "gpio%u",
793 desc_to_gpio(desc));
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100794 if (IS_ERR(dev)) {
795 status = PTR_ERR(dev);
796 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700797 }
798
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100799 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
David Brownelld8f388d82008-07-25 01:46:07 -0700800 if (status)
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100801 goto fail_unregister_device;
David Brownelld8f388d82008-07-25 01:46:07 -0700802
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100803 if (direction_may_change) {
804 status = device_create_file(dev, &dev_attr_direction);
805 if (status)
806 goto fail_unregister_device;
807 }
808
Alexandre Courbot372e7222013-02-03 01:29:29 +0900809 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100810 !test_bit(FLAG_IS_OUT, &desc->flags))) {
811 status = device_create_file(dev, &dev_attr_edge);
812 if (status)
813 goto fail_unregister_device;
814 }
815
816 set_bit(FLAG_EXPORT, &desc->flags);
817 mutex_unlock(&sysfs_lock);
818 return 0;
819
820fail_unregister_device:
821 device_unregister(dev);
822fail_unlock:
823 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900824 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
825 status);
David Brownelld8f388d82008-07-25 01:46:07 -0700826 return status;
827}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900828
829int gpio_export(unsigned gpio, bool direction_may_change)
830{
831 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
832}
David Brownelld8f388d82008-07-25 01:46:07 -0700833EXPORT_SYMBOL_GPL(gpio_export);
834
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100835static int match_export(struct device *dev, const void *data)
David Brownelld8f388d82008-07-25 01:46:07 -0700836{
837 return dev_get_drvdata(dev) == data;
838}
839
840/**
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700841 * gpio_export_link - create a sysfs link to an exported GPIO node
842 * @dev: device under which to create symlink
843 * @name: name of the symlink
844 * @gpio: gpio to create symlink to, already exported
845 *
846 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
847 * node. Caller is responsible for unlinking.
848 *
849 * Returns zero on success, else an error.
850 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900851static int gpiod_export_link(struct device *dev, const char *name,
852 struct gpio_desc *desc)
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700853{
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700854 int status = -EINVAL;
855
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900856 if (!desc) {
857 pr_warn("%s: invalid GPIO\n", __func__);
858 return -EINVAL;
859 }
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700860
861 mutex_lock(&sysfs_lock);
862
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700863 if (test_bit(FLAG_EXPORT, &desc->flags)) {
864 struct device *tdev;
865
866 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
867 if (tdev != NULL) {
868 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
869 name);
870 } else {
871 status = -ENODEV;
872 }
873 }
874
875 mutex_unlock(&sysfs_lock);
876
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700877 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900878 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
879 status);
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700880
881 return status;
882}
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700883
Alexandre Courbot372e7222013-02-03 01:29:29 +0900884int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
885{
886 return gpiod_export_link(dev, name, gpio_to_desc(gpio));
887}
888EXPORT_SYMBOL_GPL(gpio_export_link);
Jani Nikula07697462009-12-15 16:46:20 -0800889
890/**
891 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
892 * @gpio: gpio to change
893 * @value: non-zero to use active low, i.e. inverted values
894 *
895 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
896 * The GPIO does not have to be exported yet. If poll(2) support has
897 * been enabled for either rising or falling edge, it will be
898 * reconfigured to follow the new polarity.
899 *
900 * Returns zero on success, else an error.
901 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900902static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
Jani Nikula07697462009-12-15 16:46:20 -0800903{
Jani Nikula07697462009-12-15 16:46:20 -0800904 struct device *dev = NULL;
905 int status = -EINVAL;
906
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900907 if (!desc) {
908 pr_warn("%s: invalid GPIO\n", __func__);
909 return -EINVAL;
910 }
Jani Nikula07697462009-12-15 16:46:20 -0800911
912 mutex_lock(&sysfs_lock);
913
Jani Nikula07697462009-12-15 16:46:20 -0800914 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800915 dev = class_find_device(&gpio_class, NULL, desc, match_export);
916 if (dev == NULL) {
917 status = -ENODEV;
918 goto unlock;
919 }
920 }
921
922 status = sysfs_set_active_low(desc, dev, value);
923
924unlock:
925 mutex_unlock(&sysfs_lock);
926
Jani Nikula07697462009-12-15 16:46:20 -0800927 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900928 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
929 status);
Jani Nikula07697462009-12-15 16:46:20 -0800930
931 return status;
932}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900933
934int gpio_sysfs_set_active_low(unsigned gpio, int value)
935{
936 return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value);
937}
Jani Nikula07697462009-12-15 16:46:20 -0800938EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
939
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700940/**
David Brownelld8f388d82008-07-25 01:46:07 -0700941 * gpio_unexport - reverse effect of gpio_export()
942 * @gpio: gpio to make unavailable
943 *
944 * This is implicit on gpio_free().
945 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900946static void gpiod_unexport(struct gpio_desc *desc)
David Brownelld8f388d82008-07-25 01:46:07 -0700947{
Jon Povey6a99ad42010-07-27 13:18:06 -0700948 int status = 0;
Ming Lei864533c2012-02-13 22:53:20 +0800949 struct device *dev = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -0700950
Alexandre Courbot372e7222013-02-03 01:29:29 +0900951 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900952 pr_warn("%s: invalid GPIO\n", __func__);
953 return;
Jon Povey6a99ad42010-07-27 13:18:06 -0700954 }
David Brownelld8f388d82008-07-25 01:46:07 -0700955
956 mutex_lock(&sysfs_lock);
957
David Brownelld8f388d82008-07-25 01:46:07 -0700958 if (test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700959
960 dev = class_find_device(&gpio_class, NULL, desc, match_export);
961 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700962 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700963 clear_bit(FLAG_EXPORT, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700964 } else
965 status = -ENODEV;
966 }
967
968 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900969
Ming Lei864533c2012-02-13 22:53:20 +0800970 if (dev) {
971 device_unregister(dev);
972 put_device(dev);
973 }
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900974
David Brownelld8f388d82008-07-25 01:46:07 -0700975 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900976 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
977 status);
978}
979
980void gpio_unexport(unsigned gpio)
981{
982 gpiod_unexport(gpio_to_desc(gpio));
David Brownelld8f388d82008-07-25 01:46:07 -0700983}
984EXPORT_SYMBOL_GPL(gpio_unexport);
985
986static int gpiochip_export(struct gpio_chip *chip)
987{
988 int status;
989 struct device *dev;
990
991 /* Many systems register gpio chips for SOC support very early,
992 * before driver model support is available. In those cases we
993 * export this later, in gpiolib_sysfs_init() ... here we just
994 * verify that _some_ field of gpio_class got initialized.
995 */
996 if (!gpio_class.p)
997 return 0;
998
999 /* use chip->base for the ID; it's already known to be unique */
1000 mutex_lock(&sysfs_lock);
1001 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
1002 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001003 if (!IS_ERR(dev)) {
David Brownelld8f388d82008-07-25 01:46:07 -07001004 status = sysfs_create_group(&dev->kobj,
1005 &gpiochip_attr_group);
1006 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001007 status = PTR_ERR(dev);
David Brownelld8f388d82008-07-25 01:46:07 -07001008 chip->exported = (status == 0);
1009 mutex_unlock(&sysfs_lock);
1010
1011 if (status) {
1012 unsigned long flags;
1013 unsigned gpio;
1014
1015 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001016 gpio = 0;
1017 while (gpio < chip->ngpio)
1018 chip->desc[gpio++].chip = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -07001019 spin_unlock_irqrestore(&gpio_lock, flags);
1020
1021 pr_debug("%s: chip %s status %d\n", __func__,
1022 chip->label, status);
1023 }
1024
1025 return status;
1026}
1027
1028static void gpiochip_unexport(struct gpio_chip *chip)
1029{
1030 int status;
1031 struct device *dev;
1032
1033 mutex_lock(&sysfs_lock);
1034 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1035 if (dev) {
1036 put_device(dev);
1037 device_unregister(dev);
1038 chip->exported = 0;
1039 status = 0;
1040 } else
1041 status = -ENODEV;
1042 mutex_unlock(&sysfs_lock);
1043
1044 if (status)
1045 pr_debug("%s: chip %s status %d\n", __func__,
1046 chip->label, status);
1047}
1048
1049static int __init gpiolib_sysfs_init(void)
1050{
1051 int status;
1052 unsigned long flags;
Alexandre Courbot65493e32013-02-03 01:29:25 +09001053 struct gpio_chip *chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001054
1055 status = class_register(&gpio_class);
1056 if (status < 0)
1057 return status;
1058
1059 /* Scan and register the gpio_chips which registered very
1060 * early (e.g. before the class_register above was called).
1061 *
1062 * We run before arch_initcall() so chip->dev nodes can have
1063 * registered, and so arch_initcall() can always gpio_export().
1064 */
1065 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot65493e32013-02-03 01:29:25 +09001066 list_for_each_entry(chip, &gpio_chips, list) {
David Brownelld8f388d82008-07-25 01:46:07 -07001067 if (!chip || chip->exported)
1068 continue;
1069
1070 spin_unlock_irqrestore(&gpio_lock, flags);
1071 status = gpiochip_export(chip);
1072 spin_lock_irqsave(&gpio_lock, flags);
1073 }
1074 spin_unlock_irqrestore(&gpio_lock, flags);
1075
1076
1077 return status;
1078}
1079postcore_initcall(gpiolib_sysfs_init);
1080
1081#else
1082static inline int gpiochip_export(struct gpio_chip *chip)
1083{
1084 return 0;
1085}
1086
1087static inline void gpiochip_unexport(struct gpio_chip *chip)
1088{
1089}
1090
Alexandre Courbot372e7222013-02-03 01:29:29 +09001091static inline int gpiod_export(struct gpio_desc *desc,
1092 bool direction_may_change)
1093{
1094 return -ENOSYS;
1095}
1096
1097static inline int gpiod_export_link(struct device *dev, const char *name,
1098 struct gpio_desc *desc)
1099{
1100 return -ENOSYS;
1101}
1102
1103static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
1104{
1105 return -ENOSYS;
1106}
1107
1108static inline void gpiod_unexport(struct gpio_desc *desc)
1109{
1110}
1111
David Brownelld8f388d82008-07-25 01:46:07 -07001112#endif /* CONFIG_GPIO_SYSFS */
1113
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001114/*
1115 * Add a new chip to the global chips list, keeping the list of chips sorted
1116 * by base order.
1117 *
1118 * Return -EBUSY if the new chip overlaps with some other chip's integer
1119 * space.
1120 */
1121static int gpiochip_add_to_list(struct gpio_chip *chip)
1122{
1123 struct list_head *pos = &gpio_chips;
1124 struct gpio_chip *_chip;
1125 int err = 0;
1126
1127 /* find where to insert our chip */
1128 list_for_each(pos, &gpio_chips) {
1129 _chip = list_entry(pos, struct gpio_chip, list);
1130 /* shall we insert before _chip? */
1131 if (_chip->base >= chip->base + chip->ngpio)
1132 break;
1133 }
1134
1135 /* are we stepping on the chip right before? */
1136 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1137 _chip = list_entry(pos->prev, struct gpio_chip, list);
1138 if (_chip->base + _chip->ngpio > chip->base) {
1139 dev_err(chip->dev,
1140 "GPIO integer space overlap, cannot add chip\n");
1141 err = -EBUSY;
1142 }
1143 }
1144
1145 if (!err)
1146 list_add_tail(&chip->list, pos);
1147
1148 return err;
1149}
1150
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001151/**
David Brownelld2876d02008-02-04 22:28:20 -08001152 * gpiochip_add() - register a gpio_chip
1153 * @chip: the chip to register, with chip->base initialized
1154 * Context: potentially before irqs or kmalloc will work
1155 *
1156 * Returns a negative errno if the chip can't be registered, such as
1157 * because the chip->base is invalid or already associated with a
1158 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001159 *
David Brownelld8f388d82008-07-25 01:46:07 -07001160 * When gpiochip_add() is called very early during boot, so that GPIOs
1161 * can be freely used, the chip->dev device must be registered before
1162 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1163 * for GPIOs will fail rudely.
1164 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001165 * If chip->base is negative, this requests dynamic assignment of
1166 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001167 */
1168int gpiochip_add(struct gpio_chip *chip)
1169{
1170 unsigned long flags;
1171 int status = 0;
1172 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001173 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001174
Trent Piephobff5fda2008-05-23 13:04:44 -07001175 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001176 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001177 status = -EINVAL;
1178 goto fail;
1179 }
1180
1181 spin_lock_irqsave(&gpio_lock, flags);
1182
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001183 if (base < 0) {
1184 base = gpiochip_find_base(chip->ngpio);
1185 if (base < 0) {
1186 status = base;
David Brownelld8f388d82008-07-25 01:46:07 -07001187 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001188 }
1189 chip->base = base;
1190 }
1191
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001192 status = gpiochip_add_to_list(chip);
1193
David Brownelld2876d02008-02-04 22:28:20 -08001194 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001195 chip->desc = &gpio_desc[chip->base];
1196
1197 for (id = 0; id < chip->ngpio; id++) {
1198 struct gpio_desc *desc = &chip->desc[id];
1199 desc->chip = chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001200
1201 /* REVISIT: most hardware initializes GPIOs as
1202 * inputs (often with pullups enabled) so power
1203 * usage is minimized. Linux code should set the
1204 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +03001205 * and in case chip->get_direction is not set,
David Brownelld8f388d82008-07-25 01:46:07 -07001206 * we may expose the wrong direction in sysfs.
1207 */
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001208 desc->flags = !chip->direction_input
David Brownelld8f388d82008-07-25 01:46:07 -07001209 ? (1 << FLAG_IS_OUT)
1210 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001211 }
1212 }
1213
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301214#ifdef CONFIG_PINCTRL
1215 INIT_LIST_HEAD(&chip->pin_ranges);
1216#endif
1217
Anton Vorontsov391c9702010-06-08 07:48:17 -06001218 of_gpiochip_add(chip);
1219
David Brownelld8f388d82008-07-25 01:46:07 -07001220unlock:
David Brownelld2876d02008-02-04 22:28:20 -08001221 spin_unlock_irqrestore(&gpio_lock, flags);
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001222
1223 if (status)
1224 goto fail;
1225
1226 status = gpiochip_export(chip);
1227 if (status)
1228 goto fail;
1229
H Hartley Sweetenee1c1e72012-05-11 16:58:33 -07001230 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
Grant Likely64842aa2011-11-06 11:36:18 -07001231 chip->base, chip->base + chip->ngpio - 1,
1232 chip->label ? : "generic");
1233
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001234 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001235fail:
1236 /* failures here can mean systems won't boot... */
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001237 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1238 chip->base, chip->base + chip->ngpio - 1,
1239 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001240 return status;
1241}
1242EXPORT_SYMBOL_GPL(gpiochip_add);
1243
1244/**
1245 * gpiochip_remove() - unregister a gpio_chip
1246 * @chip: the chip to unregister
1247 *
1248 * A gpio_chip with any GPIOs still requested may not be removed.
1249 */
1250int gpiochip_remove(struct gpio_chip *chip)
1251{
1252 unsigned long flags;
1253 int status = 0;
1254 unsigned id;
1255
1256 spin_lock_irqsave(&gpio_lock, flags);
1257
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001258 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001259 of_gpiochip_remove(chip);
1260
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001261 for (id = 0; id < chip->ngpio; id++) {
1262 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
David Brownelld2876d02008-02-04 22:28:20 -08001263 status = -EBUSY;
1264 break;
1265 }
1266 }
1267 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001268 for (id = 0; id < chip->ngpio; id++)
1269 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001270
1271 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -08001272 }
1273
1274 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d82008-07-25 01:46:07 -07001275
1276 if (status == 0)
1277 gpiochip_unexport(chip);
1278
David Brownelld2876d02008-02-04 22:28:20 -08001279 return status;
1280}
1281EXPORT_SYMBOL_GPL(gpiochip_remove);
1282
Grant Likely594fa262010-06-08 07:48:16 -06001283/**
1284 * gpiochip_find() - iterator for locating a specific gpio_chip
1285 * @data: data to pass to match function
1286 * @callback: Callback function to check gpio_chip
1287 *
1288 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1289 * determined by a user supplied @match callback. The callback should return
1290 * 0 if the device doesn't match and non-zero if it does. If the callback is
1291 * non-zero, this function will return to the caller and not iterate over any
1292 * more gpio_chips.
1293 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001294struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001295 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001296 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001297{
Alexandre Courbot125eef92013-02-03 01:29:26 +09001298 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001299 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001300
1301 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +09001302 list_for_each_entry(chip, &gpio_chips, list)
1303 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001304 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001305
1306 /* No match? */
1307 if (&chip->list == &gpio_chips)
1308 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001309 spin_unlock_irqrestore(&gpio_lock, flags);
1310
1311 return chip;
1312}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001313EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001314
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301315#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001316
Linus Walleij3f0f8672012-11-20 12:40:15 +01001317/**
1318 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1319 * @chip: the gpiochip to add the range for
1320 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001321 * @gpio_offset: the start offset in the current gpio_chip number space
1322 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001323 * @npins: the number of pins from the offset of each pin space (GPIO and
1324 * pin controller) to accumulate in this range
1325 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001326int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001327 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001328 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301329{
1330 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001331 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301332
Linus Walleij3f0f8672012-11-20 12:40:15 +01001333 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301334 if (!pin_range) {
1335 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1336 chip->label);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001337 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301338 }
1339
Linus Walleij3f0f8672012-11-20 12:40:15 +01001340 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001341 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001342 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301343 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001344 pin_range->range.base = chip->base + gpio_offset;
1345 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301346 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001347 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301348 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001349 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001350 ret = PTR_ERR(pin_range->pctldev);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001351 pr_err("%s: GPIO chip: could not create pin range\n",
1352 chip->label);
1353 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001354 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001355 }
Linus Walleij316511c2012-11-21 08:48:09 +01001356 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1357 chip->label, gpio_offset, gpio_offset + npins - 1,
1358 pinctl_name,
1359 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301360
1361 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001362
1363 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301364}
Linus Walleij165adc92012-11-06 14:49:39 +01001365EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301366
Linus Walleij3f0f8672012-11-20 12:40:15 +01001367/**
1368 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1369 * @chip: the chip to remove all the mappings for
1370 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301371void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1372{
1373 struct gpio_pin_range *pin_range, *tmp;
1374
1375 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1376 list_del(&pin_range->node);
1377 pinctrl_remove_gpio_range(pin_range->pctldev,
1378 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001379 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301380 }
1381}
Linus Walleij165adc92012-11-06 14:49:39 +01001382EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1383
1384#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301385
David Brownelld2876d02008-02-04 22:28:20 -08001386/* These "optional" allocation calls help prevent drivers from stomping
1387 * on each other, and help provide better diagnostics in debugfs.
1388 * They're called even less than the "set direction" calls.
1389 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001390static int gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001391{
David Brownell35e8bb52008-10-15 22:03:16 -07001392 struct gpio_chip *chip;
Mark Browne9354572012-07-09 12:22:56 +01001393 int status = -EPROBE_DEFER;
David Brownelld2876d02008-02-04 22:28:20 -08001394 unsigned long flags;
1395
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001396 if (!desc) {
1397 pr_warn("%s: invalid GPIO\n", __func__);
1398 return -EINVAL;
1399 }
1400
David Brownelld2876d02008-02-04 22:28:20 -08001401 spin_lock_irqsave(&gpio_lock, flags);
1402
David Brownell35e8bb52008-10-15 22:03:16 -07001403 chip = desc->chip;
1404 if (chip == NULL)
David Brownelld2876d02008-02-04 22:28:20 -08001405 goto done;
1406
David Brownell35e8bb52008-10-15 22:03:16 -07001407 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001408 goto done;
1409
David Brownelld2876d02008-02-04 22:28:20 -08001410 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001411 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001412 */
1413
1414 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1415 desc_set_label(desc, label ? : "?");
1416 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001417 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001418 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001419 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001420 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001421 }
1422
1423 if (chip->request) {
1424 /* chip->request may sleep */
1425 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001426 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001427 spin_lock_irqsave(&gpio_lock, flags);
1428
1429 if (status < 0) {
1430 desc_set_label(desc, NULL);
1431 module_put(chip->owner);
1432 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001433 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001434 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001435 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001436 if (chip->get_direction) {
1437 /* chip->get_direction may sleep */
1438 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001439 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001440 spin_lock_irqsave(&gpio_lock, flags);
1441 }
David Brownelld2876d02008-02-04 22:28:20 -08001442done:
1443 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001444 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001445 desc_to_gpio(desc), label ? : "?", status);
David Brownelld2876d02008-02-04 22:28:20 -08001446 spin_unlock_irqrestore(&gpio_lock, flags);
1447 return status;
1448}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001449
1450int gpio_request(unsigned gpio, const char *label)
1451{
1452 return gpiod_request(gpio_to_desc(gpio), label);
1453}
David Brownelld2876d02008-02-04 22:28:20 -08001454EXPORT_SYMBOL_GPL(gpio_request);
1455
Alexandre Courbot372e7222013-02-03 01:29:29 +09001456static void gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001457{
1458 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001459 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001460
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001461 might_sleep();
1462
Alexandre Courbot372e7222013-02-03 01:29:29 +09001463 if (!desc) {
David Brownelld2876d02008-02-04 22:28:20 -08001464 WARN_ON(extra_checks);
1465 return;
1466 }
1467
Alexandre Courbot372e7222013-02-03 01:29:29 +09001468 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07001469
David Brownelld2876d02008-02-04 22:28:20 -08001470 spin_lock_irqsave(&gpio_lock, flags);
1471
David Brownell35e8bb52008-10-15 22:03:16 -07001472 chip = desc->chip;
1473 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1474 if (chip->free) {
1475 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001476 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001477 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001478 spin_lock_irqsave(&gpio_lock, flags);
1479 }
David Brownelld2876d02008-02-04 22:28:20 -08001480 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001481 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001482 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001483 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301484 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301485 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001486 } else
David Brownelld2876d02008-02-04 22:28:20 -08001487 WARN_ON(extra_checks);
1488
1489 spin_unlock_irqrestore(&gpio_lock, flags);
1490}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001491
1492void gpio_free(unsigned gpio)
1493{
1494 gpiod_free(gpio_to_desc(gpio));
1495}
David Brownelld2876d02008-02-04 22:28:20 -08001496EXPORT_SYMBOL_GPL(gpio_free);
1497
Eric Miao3e45f1d2010-03-05 13:44:35 -08001498/**
1499 * gpio_request_one - request a single GPIO with initial configuration
1500 * @gpio: the GPIO number
1501 * @flags: GPIO configuration as specified by GPIOF_*
1502 * @label: a literal description string of this GPIO
1503 */
1504int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1505{
Alexandre Courbot372e7222013-02-03 01:29:29 +09001506 struct gpio_desc *desc;
Eric Miao3e45f1d2010-03-05 13:44:35 -08001507 int err;
1508
Alexandre Courbot372e7222013-02-03 01:29:29 +09001509 desc = gpio_to_desc(gpio);
1510
1511 err = gpiod_request(desc, label);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001512 if (err)
1513 return err;
1514
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301515 if (flags & GPIOF_OPEN_DRAIN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001516 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301517
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301518 if (flags & GPIOF_OPEN_SOURCE)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001519 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301520
Eric Miao3e45f1d2010-03-05 13:44:35 -08001521 if (flags & GPIOF_DIR_IN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001522 err = gpiod_direction_input(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001523 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001524 err = gpiod_direction_output(desc,
Eric Miao3e45f1d2010-03-05 13:44:35 -08001525 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1526
Aaro Koskinene2548112010-12-21 17:24:22 -08001527 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001528 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -08001529
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001530 if (flags & GPIOF_EXPORT) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001531 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001532 if (err)
1533 goto free_gpio;
1534 }
1535
1536 return 0;
1537
1538 free_gpio:
Alexandre Courbot372e7222013-02-03 01:29:29 +09001539 gpiod_free(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001540 return err;
1541}
1542EXPORT_SYMBOL_GPL(gpio_request_one);
1543
1544/**
1545 * gpio_request_array - request multiple GPIOs in a single call
1546 * @array: array of the 'struct gpio'
1547 * @num: how many GPIOs in the array
1548 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001549int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001550{
1551 int i, err;
1552
1553 for (i = 0; i < num; i++, array++) {
1554 err = gpio_request_one(array->gpio, array->flags, array->label);
1555 if (err)
1556 goto err_free;
1557 }
1558 return 0;
1559
1560err_free:
1561 while (i--)
1562 gpio_free((--array)->gpio);
1563 return err;
1564}
1565EXPORT_SYMBOL_GPL(gpio_request_array);
1566
1567/**
1568 * gpio_free_array - release multiple GPIOs in a single call
1569 * @array: array of the 'struct gpio'
1570 * @num: how many GPIOs in the array
1571 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001572void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001573{
1574 while (num--)
1575 gpio_free((array++)->gpio);
1576}
1577EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001578
1579/**
1580 * gpiochip_is_requested - return string iff signal was requested
1581 * @chip: controller managing the signal
1582 * @offset: of signal within controller's 0..(ngpio - 1) range
1583 *
1584 * Returns NULL if the GPIO is not currently requested, else a string.
1585 * If debugfs support is enabled, the string returned is the label passed
1586 * to gpio_request(); otherwise it is a meaningless constant.
1587 *
1588 * This function is for use by GPIO controller drivers. The label can
1589 * help with diagnostics, and knowing that the signal is used as a GPIO
1590 * can help avoid accidentally multiplexing it to another controller.
1591 */
1592const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1593{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001594 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001595
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001596 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -08001597 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001598
1599 desc = &chip->desc[offset];
1600
Alexandre Courbot372e7222013-02-03 01:29:29 +09001601 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001602 return NULL;
1603#ifdef CONFIG_DEBUG_FS
Alexandre Courbot372e7222013-02-03 01:29:29 +09001604 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001605#else
1606 return "?";
1607#endif
1608}
1609EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1610
1611
1612/* Drivers MUST set GPIO direction before making get/set calls. In
1613 * some cases this is done in early boot, before IRQs are enabled.
1614 *
1615 * As a rule these aren't called more than once (except for drivers
1616 * using the open-drain emulation idiom) so these are natural places
1617 * to accumulate extra debugging checks. Note that we can't (yet)
1618 * rely on gpio_request() having been called beforehand.
1619 */
1620
Alexandre Courbot372e7222013-02-03 01:29:29 +09001621static int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001622{
1623 unsigned long flags;
1624 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001625 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001626 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001627
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001628 if (!desc) {
1629 pr_warn("%s: invalid GPIO\n", __func__);
1630 return -EINVAL;
1631 }
1632
David Brownelld2876d02008-02-04 22:28:20 -08001633 spin_lock_irqsave(&gpio_lock, flags);
1634
David Brownelld2876d02008-02-04 22:28:20 -08001635 chip = desc->chip;
1636 if (!chip || !chip->get || !chip->direction_input)
1637 goto fail;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001638 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001639 if (status < 0)
1640 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001641
1642 /* now we know the gpio is valid and chip won't vanish */
1643
1644 spin_unlock_irqrestore(&gpio_lock, flags);
1645
David Brownell9c4ba942010-08-10 18:02:24 -07001646 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001647
Alexandre Courbot372e7222013-02-03 01:29:29 +09001648 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001649 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001650 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001651 if (status < 0) {
1652 pr_debug("GPIO-%d: chip request fail, %d\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +09001653 desc_to_gpio(desc), status);
David Brownell35e8bb52008-10-15 22:03:16 -07001654 /* and it's not available to anyone else ...
1655 * gpio_request() is the fully clean solution.
1656 */
1657 goto lose;
1658 }
1659 }
1660
Alexandre Courbot372e7222013-02-03 01:29:29 +09001661 status = chip->direction_input(chip, offset);
David Brownelld2876d02008-02-04 22:28:20 -08001662 if (status == 0)
1663 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001664
Alexandre Courbot372e7222013-02-03 01:29:29 +09001665 trace_gpio_direction(desc_to_gpio(desc), 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001666lose:
David Brownelld2876d02008-02-04 22:28:20 -08001667 return status;
1668fail:
1669 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001670 if (status)
1671 pr_debug("%s: gpio-%d status %d\n", __func__,
1672 desc_to_gpio(desc), status);
David Brownelld2876d02008-02-04 22:28:20 -08001673 return status;
1674}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001675
1676int gpio_direction_input(unsigned gpio)
1677{
1678 return gpiod_direction_input(gpio_to_desc(gpio));
1679}
David Brownelld2876d02008-02-04 22:28:20 -08001680EXPORT_SYMBOL_GPL(gpio_direction_input);
1681
Alexandre Courbot372e7222013-02-03 01:29:29 +09001682static int gpiod_direction_output(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001683{
1684 unsigned long flags;
1685 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001686 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001687 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001688
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001689 if (!desc) {
1690 pr_warn("%s: invalid GPIO\n", __func__);
1691 return -EINVAL;
1692 }
1693
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301694 /* Open drain pin should not be driven to 1 */
1695 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001696 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301697
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301698 /* Open source pin should not be driven to 0 */
1699 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001700 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301701
David Brownelld2876d02008-02-04 22:28:20 -08001702 spin_lock_irqsave(&gpio_lock, flags);
1703
David Brownelld2876d02008-02-04 22:28:20 -08001704 chip = desc->chip;
1705 if (!chip || !chip->set || !chip->direction_output)
1706 goto fail;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001707 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001708 if (status < 0)
1709 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001710
1711 /* now we know the gpio is valid and chip won't vanish */
1712
1713 spin_unlock_irqrestore(&gpio_lock, flags);
1714
David Brownell9c4ba942010-08-10 18:02:24 -07001715 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001716
Alexandre Courbot372e7222013-02-03 01:29:29 +09001717 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001718 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001719 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001720 if (status < 0) {
1721 pr_debug("GPIO-%d: chip request fail, %d\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +09001722 desc_to_gpio(desc), status);
David Brownell35e8bb52008-10-15 22:03:16 -07001723 /* and it's not available to anyone else ...
1724 * gpio_request() is the fully clean solution.
1725 */
1726 goto lose;
1727 }
1728 }
1729
Alexandre Courbot372e7222013-02-03 01:29:29 +09001730 status = chip->direction_output(chip, offset, value);
David Brownelld2876d02008-02-04 22:28:20 -08001731 if (status == 0)
1732 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001733 trace_gpio_value(desc_to_gpio(desc), 0, value);
1734 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001735lose:
David Brownelld2876d02008-02-04 22:28:20 -08001736 return status;
1737fail:
1738 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001739 if (status)
1740 pr_debug("%s: gpio-%d status %d\n", __func__,
1741 desc_to_gpio(desc), status);
David Brownelld2876d02008-02-04 22:28:20 -08001742 return status;
1743}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001744
1745int gpio_direction_output(unsigned gpio, int value)
1746{
1747 return gpiod_direction_output(gpio_to_desc(gpio), value);
1748}
David Brownelld2876d02008-02-04 22:28:20 -08001749EXPORT_SYMBOL_GPL(gpio_direction_output);
1750
Felipe Balbic4b5be92010-05-26 14:42:23 -07001751/**
1752 * gpio_set_debounce - sets @debounce time for a @gpio
1753 * @gpio: the gpio to set debounce time
1754 * @debounce: debounce time is microseconds
1755 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001756static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001757{
1758 unsigned long flags;
1759 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001760 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001761 int offset;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001762
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001763 if (!desc) {
1764 pr_warn("%s: invalid GPIO\n", __func__);
1765 return -EINVAL;
1766 }
1767
Felipe Balbic4b5be92010-05-26 14:42:23 -07001768 spin_lock_irqsave(&gpio_lock, flags);
1769
Felipe Balbic4b5be92010-05-26 14:42:23 -07001770 chip = desc->chip;
1771 if (!chip || !chip->set || !chip->set_debounce)
1772 goto fail;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001773
1774 status = gpio_ensure_requested(desc);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001775 if (status < 0)
1776 goto fail;
1777
1778 /* now we know the gpio is valid and chip won't vanish */
1779
1780 spin_unlock_irqrestore(&gpio_lock, flags);
1781
David Brownell9c4ba942010-08-10 18:02:24 -07001782 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001783
Alexandre Courbot372e7222013-02-03 01:29:29 +09001784 offset = gpio_chip_hwgpio(desc);
1785 return chip->set_debounce(chip, offset, debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001786
1787fail:
1788 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001789 if (status)
1790 pr_debug("%s: gpio-%d status %d\n", __func__,
1791 desc_to_gpio(desc), status);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001792
1793 return status;
1794}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001795
1796int gpio_set_debounce(unsigned gpio, unsigned debounce)
1797{
1798 return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
1799}
Felipe Balbic4b5be92010-05-26 14:42:23 -07001800EXPORT_SYMBOL_GPL(gpio_set_debounce);
David Brownelld2876d02008-02-04 22:28:20 -08001801
1802/* I/O calls are only valid after configuration completed; the relevant
1803 * "is this a valid GPIO" error checks should already have been done.
1804 *
1805 * "Get" operations are often inlinable as reading a pin value register,
1806 * and masking the relevant bit in that register.
1807 *
1808 * When "set" operations are inlinable, they involve writing that mask to
1809 * one register to set a low value, or a different register to set it high.
1810 * Otherwise locking is needed, so there may be little value to inlining.
1811 *
1812 *------------------------------------------------------------------------
1813 *
1814 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1815 * have requested the GPIO. That can include implicit requesting by
1816 * a direction setting call. Marking a gpio as requested locks its chip
1817 * in memory, guaranteeing that these table lookups need no more locking
1818 * and that gpiochip_remove() will fail.
1819 *
1820 * REVISIT when debugging, consider adding some instrumentation to ensure
1821 * that the GPIO was actually requested.
1822 */
1823
1824/**
1825 * __gpio_get_value() - return a gpio's value
1826 * @gpio: gpio whose value will be returned
1827 * Context: any
1828 *
1829 * This is used directly or indirectly to implement gpio_get_value().
1830 * It returns the zero or nonzero value provided by the associated
1831 * gpio_chip.get() method; or zero if no such method is provided.
1832 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001833static int gpiod_get_value(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001834{
1835 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001836 int value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001837 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001838
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001839 if (!desc)
1840 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001841 chip = desc->chip;
1842 offset = gpio_chip_hwgpio(desc);
Mark Browne4e449e2012-02-17 10:46:00 -08001843 /* Should be using gpio_get_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001844 WARN_ON(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001845 value = chip->get ? chip->get(chip, offset) : 0;
1846 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001847 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001848}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001849
1850int __gpio_get_value(unsigned gpio)
1851{
1852 return gpiod_get_value(gpio_to_desc(gpio));
1853}
David Brownelld2876d02008-02-04 22:28:20 -08001854EXPORT_SYMBOL_GPL(__gpio_get_value);
1855
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301856/*
1857 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1858 * @gpio: Gpio whose state need to be set.
1859 * @chip: Gpio chip.
1860 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1861 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001862static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301863{
1864 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001865 struct gpio_chip *chip = desc->chip;
1866 int offset = gpio_chip_hwgpio(desc);
1867
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301868 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001869 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301870 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001871 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301872 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001873 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301874 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001875 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301876 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001877 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301878 if (err < 0)
1879 pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +09001880 __func__, desc_to_gpio(desc), err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301881}
1882
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301883/*
1884 * _gpio_set_open_source() - Set the open source gpio's value.
1885 * @gpio: Gpio whose state need to be set.
1886 * @chip: Gpio chip.
1887 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1888 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001889static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301890{
1891 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001892 struct gpio_chip *chip = desc->chip;
1893 int offset = gpio_chip_hwgpio(desc);
1894
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301895 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001896 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301897 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001898 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301899 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001900 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301901 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001902 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301903 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001904 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301905 if (err < 0)
1906 pr_err("%s: Error in set_value for open source gpio%d err %d\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +09001907 __func__, desc_to_gpio(desc), err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301908}
1909
David Brownelld2876d02008-02-04 22:28:20 -08001910/**
1911 * __gpio_set_value() - assign a gpio's value
1912 * @gpio: gpio whose value will be assigned
1913 * @value: value to assign
1914 * Context: any
1915 *
1916 * This is used directly or indirectly to implement gpio_set_value().
1917 * It invokes the associated gpio_chip.set() method.
1918 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001919static void gpiod_set_value(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001920{
1921 struct gpio_chip *chip;
1922
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001923 if (!desc)
1924 return;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001925 chip = desc->chip;
Mark Browne4e449e2012-02-17 10:46:00 -08001926 /* Should be using gpio_set_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001927 WARN_ON(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001928 trace_gpio_value(desc_to_gpio(desc), 0, value);
1929 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1930 _gpio_set_open_drain_value(desc, value);
1931 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1932 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301933 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001934 chip->set(chip, gpio_chip_hwgpio(desc), value);
1935}
1936
1937void __gpio_set_value(unsigned gpio, int value)
1938{
1939 return gpiod_set_value(gpio_to_desc(gpio), value);
David Brownelld2876d02008-02-04 22:28:20 -08001940}
1941EXPORT_SYMBOL_GPL(__gpio_set_value);
1942
1943/**
1944 * __gpio_cansleep() - report whether gpio value access will sleep
1945 * @gpio: gpio in question
1946 * Context: any
1947 *
1948 * This is used directly or indirectly to implement gpio_cansleep(). It
1949 * returns nonzero if access reading or writing the GPIO value can sleep.
1950 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001951static int gpiod_cansleep(struct gpio_desc *desc)
1952{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001953 if (!desc)
1954 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001955 /* only call this on GPIOs that are valid! */
1956 return desc->chip->can_sleep;
1957}
1958
David Brownelld2876d02008-02-04 22:28:20 -08001959int __gpio_cansleep(unsigned gpio)
1960{
Alexandre Courbot372e7222013-02-03 01:29:29 +09001961 return gpiod_cansleep(gpio_to_desc(gpio));
David Brownelld2876d02008-02-04 22:28:20 -08001962}
1963EXPORT_SYMBOL_GPL(__gpio_cansleep);
1964
David Brownell0f6d5042008-10-15 22:03:14 -07001965/**
1966 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1967 * @gpio: gpio whose IRQ will be returned (already requested)
1968 * Context: any
1969 *
1970 * This is used directly or indirectly to implement gpio_to_irq().
1971 * It returns the number of the IRQ signaled by this (input) GPIO,
1972 * or a negative errno.
1973 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001974static int gpiod_to_irq(struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001975{
1976 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001977 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001978
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001979 if (!desc)
1980 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001981 chip = desc->chip;
1982 offset = gpio_chip_hwgpio(desc);
1983 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1984}
1985
1986int __gpio_to_irq(unsigned gpio)
1987{
1988 return gpiod_to_irq(gpio_to_desc(gpio));
David Brownell0f6d5042008-10-15 22:03:14 -07001989}
1990EXPORT_SYMBOL_GPL(__gpio_to_irq);
1991
David Brownelld2876d02008-02-04 22:28:20 -08001992
David Brownelld2876d02008-02-04 22:28:20 -08001993/* There's no value in making it easy to inline GPIO calls that may sleep.
1994 * Common examples include ones connected to I2C or SPI chips.
1995 */
1996
Alexandre Courbot372e7222013-02-03 01:29:29 +09001997static int gpiod_get_value_cansleep(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001998{
1999 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002000 int value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002001 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08002002
2003 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002004 if (!desc)
2005 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002006 chip = desc->chip;
2007 offset = gpio_chip_hwgpio(desc);
2008 value = chip->get ? chip->get(chip, offset) : 0;
2009 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002010 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002011}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002012
2013int gpio_get_value_cansleep(unsigned gpio)
2014{
2015 return gpiod_get_value_cansleep(gpio_to_desc(gpio));
2016}
David Brownelld2876d02008-02-04 22:28:20 -08002017EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
2018
Alexandre Courbot372e7222013-02-03 01:29:29 +09002019static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002020{
2021 struct gpio_chip *chip;
2022
2023 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002024 if (!desc)
2025 return;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002026 chip = desc->chip;
2027 trace_gpio_value(desc_to_gpio(desc), 0, value);
2028 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2029 _gpio_set_open_drain_value(desc, value);
2030 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2031 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302032 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002033 chip->set(chip, gpio_chip_hwgpio(desc), value);
2034}
2035
2036void gpio_set_value_cansleep(unsigned gpio, int value)
2037{
2038 return gpiod_set_value_cansleep(gpio_to_desc(gpio), value);
David Brownelld2876d02008-02-04 22:28:20 -08002039}
2040EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
2041
David Brownelld2876d02008-02-04 22:28:20 -08002042#ifdef CONFIG_DEBUG_FS
2043
David Brownelld2876d02008-02-04 22:28:20 -08002044static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2045{
2046 unsigned i;
2047 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002048 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002049 int is_out;
2050
2051 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2052 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2053 continue;
2054
Alexandre Courbot372e7222013-02-03 01:29:29 +09002055 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002056 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Jarkko Nikula6e8ba722008-11-19 15:36:17 -08002057 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002058 gpio, gdesc->label,
2059 is_out ? "out" : "in ",
2060 chip->get
2061 ? (chip->get(chip, i) ? "hi" : "lo")
2062 : "? ");
David Brownelld2876d02008-02-04 22:28:20 -08002063 seq_printf(s, "\n");
2064 }
2065}
2066
Thierry Redingf9c4a312012-04-12 13:26:01 +02002067static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002068{
Grant Likely362432a2013-02-09 09:41:49 +00002069 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002070 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002071 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002072
2073 s->private = "";
2074
Grant Likely362432a2013-02-09 09:41:49 +00002075 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002076 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002077 if (index-- == 0) {
2078 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002079 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002080 }
2081 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002082
2083 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002084}
2085
2086static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2087{
Grant Likely362432a2013-02-09 09:41:49 +00002088 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002089 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002090 void *ret = NULL;
2091
Grant Likely362432a2013-02-09 09:41:49 +00002092 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002093 if (list_is_last(&chip->list, &gpio_chips))
2094 ret = NULL;
2095 else
2096 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002097 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002098
2099 s->private = "\n";
2100 ++*pos;
2101
2102 return ret;
2103}
2104
2105static void gpiolib_seq_stop(struct seq_file *s, void *v)
2106{
2107}
2108
2109static int gpiolib_seq_show(struct seq_file *s, void *v)
2110{
2111 struct gpio_chip *chip = v;
2112 struct device *dev;
2113
2114 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2115 chip->base, chip->base + chip->ngpio - 1);
2116 dev = chip->dev;
2117 if (dev)
2118 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2119 dev_name(dev));
2120 if (chip->label)
2121 seq_printf(s, ", %s", chip->label);
2122 if (chip->can_sleep)
2123 seq_printf(s, ", can sleep");
2124 seq_printf(s, ":\n");
2125
2126 if (chip->dbg_show)
2127 chip->dbg_show(s, chip);
2128 else
2129 gpiolib_dbg_show(s, chip);
2130
David Brownelld2876d02008-02-04 22:28:20 -08002131 return 0;
2132}
2133
Thierry Redingf9c4a312012-04-12 13:26:01 +02002134static const struct seq_operations gpiolib_seq_ops = {
2135 .start = gpiolib_seq_start,
2136 .next = gpiolib_seq_next,
2137 .stop = gpiolib_seq_stop,
2138 .show = gpiolib_seq_show,
2139};
2140
David Brownelld2876d02008-02-04 22:28:20 -08002141static int gpiolib_open(struct inode *inode, struct file *file)
2142{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002143 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002144}
2145
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002146static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002147 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002148 .open = gpiolib_open,
2149 .read = seq_read,
2150 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002151 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002152};
2153
2154static int __init gpiolib_debugfs_init(void)
2155{
2156 /* /sys/kernel/debug/gpio */
2157 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2158 NULL, NULL, &gpiolib_operations);
2159 return 0;
2160}
2161subsys_initcall(gpiolib_debugfs_init);
2162
2163#endif /* DEBUG_FS */