blob: 866431f674c357bf7c5fc1ed9bb5335260caf2b5 [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
David Brownelld2876d02008-02-04 22:28:20 -080015
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060016#define CREATE_TRACE_POINTS
17#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080018
19/* Optional implementation infrastructure for GPIO interfaces.
20 *
21 * Platforms may want to use this if they tend to use very many GPIOs
22 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
23 *
24 * When kernel footprint or instruction count is an issue, simpler
25 * implementations may be preferred. The GPIO programming interface
26 * allows for inlining speed-critical get/set operations for common
27 * cases, so that access to SOC-integrated GPIOs can sometimes cost
28 * only an instruction or two per bit.
29 */
30
31
32/* When debugging, extend minimal trust to callers and platform code.
33 * Also emit diagnostic messages that may help initial bringup, when
34 * board setup or driver bugs are most common.
35 *
36 * Otherwise, minimize overhead in what may be bitbanging codepaths.
37 */
38#ifdef DEBUG
39#define extra_checks 1
40#else
41#define extra_checks 0
42#endif
43
44/* gpio_lock prevents conflicts during gpio_desc[] table updates.
45 * While any GPIO is requested, its gpio_chip is not removable;
46 * each GPIO's "requested" flag serves as a lock and refcount.
47 */
48static DEFINE_SPINLOCK(gpio_lock);
49
50struct gpio_desc {
51 struct gpio_chip *chip;
52 unsigned long flags;
53/* flag symbols are bit numbers */
54#define FLAG_REQUESTED 0
55#define FLAG_IS_OUT 1
Alexandre Courbot710b40e2013-02-02 23:44:06 +090056#define FLAG_EXPORT 2 /* protected by sysfs_lock */
57#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
58#define FLAG_TRIG_FALL 4 /* trigger on falling edge */
59#define FLAG_TRIG_RISE 5 /* trigger on rising edge */
60#define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */
61#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
62#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070063
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070064#define ID_SHIFT 16 /* add new flags before this one */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070065
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070066#define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
Daniel Glöcknerff77c352009-09-22 16:46:38 -070067#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
David Brownelld2876d02008-02-04 22:28:20 -080068
69#ifdef CONFIG_DEBUG_FS
70 const char *label;
71#endif
72};
73static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
74
Alexandre Courbot1a989d02013-02-03 01:29:24 +090075static LIST_HEAD(gpio_chips);
76
Daniel Glöcknerff77c352009-09-22 16:46:38 -070077#ifdef CONFIG_GPIO_SYSFS
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070078static DEFINE_IDR(dirent_idr);
Daniel Glöcknerff77c352009-09-22 16:46:38 -070079#endif
80
Alexandre Courbot372e7222013-02-03 01:29:29 +090081/*
82 * Internal gpiod_* API using descriptors instead of the integer namespace.
83 * Most of this should eventually go public.
84 */
85static int gpiod_request(struct gpio_desc *desc, const char *label);
86static void gpiod_free(struct gpio_desc *desc);
87static int gpiod_direction_input(struct gpio_desc *desc);
88static int gpiod_direction_output(struct gpio_desc *desc, int value);
89static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
90static int gpiod_get_value_cansleep(struct gpio_desc *desc);
91static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
92static int gpiod_get_value(struct gpio_desc *desc);
93static void gpiod_set_value(struct gpio_desc *desc, int value);
94static int gpiod_cansleep(struct gpio_desc *desc);
95static int gpiod_to_irq(struct gpio_desc *desc);
96static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
97static int gpiod_export_link(struct device *dev, const char *name,
98 struct gpio_desc *desc);
99static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
100static void gpiod_unexport(struct gpio_desc *desc);
101
102
David Brownelld2876d02008-02-04 22:28:20 -0800103static inline void desc_set_label(struct gpio_desc *d, const char *label)
104{
105#ifdef CONFIG_DEBUG_FS
106 d->label = label;
107#endif
108}
109
Alexandre Courbot372e7222013-02-03 01:29:29 +0900110/*
111 * Return the GPIO number of the passed descriptor relative to its chip
112 */
113static int gpio_chip_hwgpio(const struct gpio_desc *desc)
114{
115 return (desc - &gpio_desc[0]) - desc->chip->base;
116}
117
118/**
119 * Convert a GPIO number to its descriptor
120 */
121static struct gpio_desc *gpio_to_desc(unsigned gpio)
122{
123 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
124 return NULL;
125 else
126 return &gpio_desc[gpio];
127}
128
129/**
130 * Convert a GPIO descriptor to the integer namespace.
131 * This should disappear in the future but is needed since we still
132 * use GPIO numbers for error messages and sysfs nodes
133 */
134static int desc_to_gpio(const struct gpio_desc *desc)
135{
136 return desc - &gpio_desc[0];
137}
138
139
David Brownelld2876d02008-02-04 22:28:20 -0800140/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
141 * when setting direction, and otherwise illegal. Until board setup code
142 * and drivers use explicit requests everywhere (which won't happen when
143 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -0700144 * message should motivate switching to explicit requests... so should
145 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -0700146 *
147 * NOTE: the autorequest mechanism is going away; at this point it's
148 * only "legal" in the sense that (old) code using it won't break yet,
149 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -0800150 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900151static int gpio_ensure_requested(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800152{
David Brownell8a0cecf2009-04-02 16:57:06 -0700153 const struct gpio_chip *chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900154 const int gpio = desc_to_gpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -0700155
David Brownell8a0cecf2009-04-02 16:57:06 -0700156 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
157 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700158 if (!try_module_get(chip->owner)) {
159 pr_err("GPIO-%d: module can't be gotten \n", gpio);
160 clear_bit(FLAG_REQUESTED, &desc->flags);
161 /* lose */
162 return -EIO;
163 }
David Brownelld2876d02008-02-04 22:28:20 -0800164 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700165 /* caller must chip->request() w/o spinlock */
166 if (chip->request)
167 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800168 }
David Brownell35e8bb52008-10-15 22:03:16 -0700169 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800170}
171
172/* caller holds gpio_lock *OR* gpio is marked as requested */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900173static struct gpio_chip *gpiod_to_chip(struct gpio_desc *desc)
174{
175 return desc->chip;
176}
177
Grant Likely1a2d3972011-12-12 09:25:57 -0700178struct gpio_chip *gpio_to_chip(unsigned gpio)
David Brownelld2876d02008-02-04 22:28:20 -0800179{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900180 return gpiod_to_chip(gpio_to_desc(gpio));
David Brownelld2876d02008-02-04 22:28:20 -0800181}
182
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700183/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
184static int gpiochip_find_base(int ngpio)
185{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900186 struct gpio_chip *chip;
187 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700188
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900189 list_for_each_entry_reverse(chip, &gpio_chips, list) {
190 /* found a free space? */
191 if (chip->base + chip->ngpio <= base)
192 break;
193 else
194 /* nope, check the space right before the chip */
195 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700196 }
197
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900198 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700199 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900200 return base;
201 } else {
202 pr_err("%s: cannot find free range\n", __func__);
203 return -ENOSPC;
204 }
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700205}
206
Mathias Nyman80b0a602012-10-24 17:25:27 +0300207/* caller ensures gpio is valid and requested, chip->get_direction may sleep */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900208static int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300209{
210 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900211 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300212 int status = -EINVAL;
213
Alexandre Courbot372e7222013-02-03 01:29:29 +0900214 chip = gpiod_to_chip(desc);
215 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300216
217 if (!chip->get_direction)
218 return status;
219
Alexandre Courbot372e7222013-02-03 01:29:29 +0900220 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300221 if (status > 0) {
222 /* GPIOF_DIR_IN, or other positive */
223 status = 1;
224 clear_bit(FLAG_IS_OUT, &desc->flags);
225 }
226 if (status == 0) {
227 /* GPIOF_DIR_OUT */
228 set_bit(FLAG_IS_OUT, &desc->flags);
229 }
230 return status;
231}
232
David Brownelld8f388d82008-07-25 01:46:07 -0700233#ifdef CONFIG_GPIO_SYSFS
234
235/* lock protects against unexport_gpio() being called while
236 * sysfs files are active.
237 */
238static DEFINE_MUTEX(sysfs_lock);
239
240/*
241 * /sys/class/gpio/gpioN... only for GPIOs that are exported
242 * /direction
243 * * MAY BE OMITTED if kernel won't allow direction changes
244 * * is read/write as "in" or "out"
245 * * may also be written as "high" or "low", initializing
246 * output value as specified ("out" implies "low")
247 * /value
248 * * always readable, subject to hardware behavior
249 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700250 * /edge
251 * * configures behavior of poll(2) on /value
252 * * available only if pin can generate IRQs on input
253 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800254 * /active_low
255 * * configures polarity of /value
256 * * is read/write as zero/nonzero
257 * * also affects existing and subsequent "falling" and "rising"
258 * /edge configuration
David Brownelld8f388d82008-07-25 01:46:07 -0700259 */
260
261static ssize_t gpio_direction_show(struct device *dev,
262 struct device_attribute *attr, char *buf)
263{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900264 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700265 ssize_t status;
266
267 mutex_lock(&sysfs_lock);
268
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900269 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700270 status = -EIO;
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900271 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +0900272 gpiod_get_direction(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700273 status = sprintf(buf, "%s\n",
274 test_bit(FLAG_IS_OUT, &desc->flags)
275 ? "out" : "in");
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900276 }
David Brownelld8f388d82008-07-25 01:46:07 -0700277
278 mutex_unlock(&sysfs_lock);
279 return status;
280}
281
282static ssize_t gpio_direction_store(struct device *dev,
283 struct device_attribute *attr, const char *buf, size_t size)
284{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900285 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700286 ssize_t status;
287
288 mutex_lock(&sysfs_lock);
289
290 if (!test_bit(FLAG_EXPORT, &desc->flags))
291 status = -EIO;
292 else if (sysfs_streq(buf, "high"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900293 status = gpiod_direction_output(desc, 1);
David Brownelld8f388d82008-07-25 01:46:07 -0700294 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900295 status = gpiod_direction_output(desc, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700296 else if (sysfs_streq(buf, "in"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900297 status = gpiod_direction_input(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700298 else
299 status = -EINVAL;
300
301 mutex_unlock(&sysfs_lock);
302 return status ? : size;
303}
304
Jani Nikula07697462009-12-15 16:46:20 -0800305static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700306 gpio_direction_show, gpio_direction_store);
307
308static ssize_t gpio_value_show(struct device *dev,
309 struct device_attribute *attr, char *buf)
310{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900311 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700312 ssize_t status;
313
314 mutex_lock(&sysfs_lock);
315
Jani Nikula07697462009-12-15 16:46:20 -0800316 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700317 status = -EIO;
Jani Nikula07697462009-12-15 16:46:20 -0800318 } else {
319 int value;
320
Alexandre Courbot372e7222013-02-03 01:29:29 +0900321 value = !!gpiod_get_value_cansleep(desc);
Jani Nikula07697462009-12-15 16:46:20 -0800322 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
323 value = !value;
324
325 status = sprintf(buf, "%d\n", value);
326 }
David Brownelld8f388d82008-07-25 01:46:07 -0700327
328 mutex_unlock(&sysfs_lock);
329 return status;
330}
331
332static ssize_t gpio_value_store(struct device *dev,
333 struct device_attribute *attr, const char *buf, size_t size)
334{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900335 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d82008-07-25 01:46:07 -0700336 ssize_t status;
337
338 mutex_lock(&sysfs_lock);
339
340 if (!test_bit(FLAG_EXPORT, &desc->flags))
341 status = -EIO;
342 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
343 status = -EPERM;
344 else {
345 long value;
346
347 status = strict_strtol(buf, 0, &value);
348 if (status == 0) {
Jani Nikula07697462009-12-15 16:46:20 -0800349 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
350 value = !value;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900351 gpiod_set_value_cansleep(desc, value != 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700352 status = size;
353 }
354 }
355
356 mutex_unlock(&sysfs_lock);
357 return status;
358}
359
Jani Nikula07697462009-12-15 16:46:20 -0800360static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d82008-07-25 01:46:07 -0700361 gpio_value_show, gpio_value_store);
362
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700363static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
364{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700365 struct sysfs_dirent *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700366
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700367 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700368 return IRQ_HANDLED;
369}
370
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700371static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
372 unsigned long gpio_flags)
373{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700374 struct sysfs_dirent *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700375 unsigned long irq_flags;
376 int ret, irq, id;
377
378 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
379 return 0;
380
Alexandre Courbot372e7222013-02-03 01:29:29 +0900381 irq = gpiod_to_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700382 if (irq < 0)
383 return -EIO;
384
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700385 id = desc->flags >> ID_SHIFT;
386 value_sd = idr_find(&dirent_idr, id);
387 if (value_sd)
388 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700389
390 desc->flags &= ~GPIO_TRIGGER_MASK;
391
392 if (!gpio_flags) {
393 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700394 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700395 }
396
397 irq_flags = IRQF_SHARED;
398 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800399 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
400 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700401 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800402 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
403 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700404
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700405 if (!value_sd) {
406 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
407 if (!value_sd) {
408 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700409 goto err_out;
410 }
411
412 do {
413 ret = -ENOMEM;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700414 if (idr_pre_get(&dirent_idr, GFP_KERNEL))
415 ret = idr_get_new_above(&dirent_idr, value_sd,
416 1, &id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700417 } while (ret == -EAGAIN);
418
419 if (ret)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700420 goto free_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700421
422 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700423 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700424
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700425 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700426 ret = -ERANGE;
427 goto free_id;
428 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700429 }
430
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700431 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700432 "gpiolib", value_sd);
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700433 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700434 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700435
436 desc->flags |= gpio_flags;
437 return 0;
438
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700439free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700440 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700441 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700442free_sd:
443 if (value_sd)
444 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700445err_out:
446 return ret;
447}
448
449static const struct {
450 const char *name;
451 unsigned long flags;
452} trigger_types[] = {
453 { "none", 0 },
454 { "falling", BIT(FLAG_TRIG_FALL) },
455 { "rising", BIT(FLAG_TRIG_RISE) },
456 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
457};
458
459static ssize_t gpio_edge_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
461{
462 const struct gpio_desc *desc = dev_get_drvdata(dev);
463 ssize_t status;
464
465 mutex_lock(&sysfs_lock);
466
467 if (!test_bit(FLAG_EXPORT, &desc->flags))
468 status = -EIO;
469 else {
470 int i;
471
472 status = 0;
473 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
474 if ((desc->flags & GPIO_TRIGGER_MASK)
475 == trigger_types[i].flags) {
476 status = sprintf(buf, "%s\n",
477 trigger_types[i].name);
478 break;
479 }
480 }
481
482 mutex_unlock(&sysfs_lock);
483 return status;
484}
485
486static ssize_t gpio_edge_store(struct device *dev,
487 struct device_attribute *attr, const char *buf, size_t size)
488{
489 struct gpio_desc *desc = dev_get_drvdata(dev);
490 ssize_t status;
491 int i;
492
493 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
494 if (sysfs_streq(trigger_types[i].name, buf))
495 goto found;
496 return -EINVAL;
497
498found:
499 mutex_lock(&sysfs_lock);
500
501 if (!test_bit(FLAG_EXPORT, &desc->flags))
502 status = -EIO;
503 else {
504 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
505 if (!status)
506 status = size;
507 }
508
509 mutex_unlock(&sysfs_lock);
510
511 return status;
512}
513
514static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
515
Jani Nikula07697462009-12-15 16:46:20 -0800516static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
517 int value)
518{
519 int status = 0;
520
521 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
522 return 0;
523
524 if (value)
525 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
526 else
527 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
528
529 /* reconfigure poll(2) support if enabled on one edge only */
530 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
531 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
532 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
533
534 gpio_setup_irq(desc, dev, 0);
535 status = gpio_setup_irq(desc, dev, trigger_flags);
536 }
537
538 return status;
539}
540
541static ssize_t gpio_active_low_show(struct device *dev,
542 struct device_attribute *attr, char *buf)
543{
544 const struct gpio_desc *desc = dev_get_drvdata(dev);
545 ssize_t status;
546
547 mutex_lock(&sysfs_lock);
548
549 if (!test_bit(FLAG_EXPORT, &desc->flags))
550 status = -EIO;
551 else
552 status = sprintf(buf, "%d\n",
553 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
554
555 mutex_unlock(&sysfs_lock);
556
557 return status;
558}
559
560static ssize_t gpio_active_low_store(struct device *dev,
561 struct device_attribute *attr, const char *buf, size_t size)
562{
563 struct gpio_desc *desc = dev_get_drvdata(dev);
564 ssize_t status;
565
566 mutex_lock(&sysfs_lock);
567
568 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
569 status = -EIO;
570 } else {
571 long value;
572
573 status = strict_strtol(buf, 0, &value);
574 if (status == 0)
575 status = sysfs_set_active_low(desc, dev, value != 0);
576 }
577
578 mutex_unlock(&sysfs_lock);
579
580 return status ? : size;
581}
582
583static const DEVICE_ATTR(active_low, 0644,
584 gpio_active_low_show, gpio_active_low_store);
585
David Brownelld8f388d82008-07-25 01:46:07 -0700586static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d82008-07-25 01:46:07 -0700587 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800588 &dev_attr_active_low.attr,
David Brownelld8f388d82008-07-25 01:46:07 -0700589 NULL,
590};
591
592static const struct attribute_group gpio_attr_group = {
593 .attrs = (struct attribute **) gpio_attrs,
594};
595
596/*
597 * /sys/class/gpio/gpiochipN/
598 * /base ... matching gpio_chip.base (N)
599 * /label ... matching gpio_chip.label
600 * /ngpio ... matching gpio_chip.ngpio
601 */
602
603static ssize_t chip_base_show(struct device *dev,
604 struct device_attribute *attr, char *buf)
605{
606 const struct gpio_chip *chip = dev_get_drvdata(dev);
607
608 return sprintf(buf, "%d\n", chip->base);
609}
610static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
611
612static ssize_t chip_label_show(struct device *dev,
613 struct device_attribute *attr, char *buf)
614{
615 const struct gpio_chip *chip = dev_get_drvdata(dev);
616
617 return sprintf(buf, "%s\n", chip->label ? : "");
618}
619static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
620
621static ssize_t chip_ngpio_show(struct device *dev,
622 struct device_attribute *attr, char *buf)
623{
624 const struct gpio_chip *chip = dev_get_drvdata(dev);
625
626 return sprintf(buf, "%u\n", chip->ngpio);
627}
628static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
629
630static const struct attribute *gpiochip_attrs[] = {
631 &dev_attr_base.attr,
632 &dev_attr_label.attr,
633 &dev_attr_ngpio.attr,
634 NULL,
635};
636
637static const struct attribute_group gpiochip_attr_group = {
638 .attrs = (struct attribute **) gpiochip_attrs,
639};
640
641/*
642 * /sys/class/gpio/export ... write-only
643 * integer N ... number of GPIO to export (full access)
644 * /sys/class/gpio/unexport ... write-only
645 * integer N ... number of GPIO to unexport
646 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100647static ssize_t export_store(struct class *class,
648 struct class_attribute *attr,
649 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700650{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900651 long gpio;
652 struct gpio_desc *desc;
653 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700654
655 status = strict_strtol(buf, 0, &gpio);
656 if (status < 0)
657 goto done;
658
Alexandre Courbot372e7222013-02-03 01:29:29 +0900659 desc = gpio_to_desc(gpio);
660
David Brownelld8f388d82008-07-25 01:46:07 -0700661 /* No extra locking here; FLAG_SYSFS just signifies that the
662 * request and export were done by on behalf of userspace, so
663 * they may be undone on its behalf too.
664 */
665
Alexandre Courbot372e7222013-02-03 01:29:29 +0900666 status = gpiod_request(desc, "sysfs");
Mathias Nymanad2fab32012-10-25 14:03:03 +0300667 if (status < 0) {
668 if (status == -EPROBE_DEFER)
669 status = -ENODEV;
David Brownelld8f388d82008-07-25 01:46:07 -0700670 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +0300671 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900672 status = gpiod_export(desc, true);
David Brownelld8f388d82008-07-25 01:46:07 -0700673 if (status < 0)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900674 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700675 else
Alexandre Courbot372e7222013-02-03 01:29:29 +0900676 set_bit(FLAG_SYSFS, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700677
678done:
679 if (status)
680 pr_debug("%s: status %d\n", __func__, status);
681 return status ? : len;
682}
683
Andi Kleen28812fe2010-01-05 12:48:07 +0100684static ssize_t unexport_store(struct class *class,
685 struct class_attribute *attr,
686 const char *buf, size_t len)
David Brownelld8f388d82008-07-25 01:46:07 -0700687{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900688 long gpio;
689 struct gpio_desc *desc;
690 int status;
David Brownelld8f388d82008-07-25 01:46:07 -0700691
692 status = strict_strtol(buf, 0, &gpio);
693 if (status < 0)
694 goto done;
695
696 status = -EINVAL;
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 Courbot372e7222013-02-03 01:29:29 +0900700 if (!desc)
David Brownelld8f388d82008-07-25 01:46:07 -0700701 goto done;
702
703 /* No extra locking here; FLAG_SYSFS just signifies that the
704 * request and export were done by on behalf of userspace, so
705 * they may be undone on its behalf too.
706 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900707 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700708 status = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900709 gpiod_free(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700710 }
711done:
712 if (status)
713 pr_debug("%s: status %d\n", __func__, status);
714 return status ? : len;
715}
716
717static struct class_attribute gpio_class_attrs[] = {
718 __ATTR(export, 0200, NULL, export_store),
719 __ATTR(unexport, 0200, NULL, unexport_store),
720 __ATTR_NULL,
721};
722
723static struct class gpio_class = {
724 .name = "gpio",
725 .owner = THIS_MODULE,
726
727 .class_attrs = gpio_class_attrs,
728};
729
730
731/**
732 * gpio_export - export a GPIO through sysfs
733 * @gpio: gpio to make available, already requested
734 * @direction_may_change: true if userspace may change gpio direction
735 * Context: arch_initcall or later
736 *
737 * When drivers want to make a GPIO accessible to userspace after they
738 * have requested it -- perhaps while debugging, or as part of their
739 * public interface -- they may use this routine. If the GPIO can
740 * change direction (some can't) and the caller allows it, userspace
741 * will see "direction" sysfs attribute which may be used to change
742 * the gpio's direction. A "value" attribute will always be provided.
743 *
744 * Returns zero on success, else an error.
745 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900746static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
David Brownelld8f388d82008-07-25 01:46:07 -0700747{
748 unsigned long flags;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100749 int status;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700750 const char *ioname = NULL;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100751 struct device *dev;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900752 int offset;
David Brownelld8f388d82008-07-25 01:46:07 -0700753
754 /* can't export until sysfs is available ... */
755 if (!gpio_class.p) {
756 pr_debug("%s: called too early!\n", __func__);
757 return -ENOENT;
758 }
759
Alexandre Courbot372e7222013-02-03 01:29:29 +0900760 if (!desc) {
761 pr_debug("%s: invalid gpio descriptor\n", __func__);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100762 return -EINVAL;
763 }
David Brownelld8f388d82008-07-25 01:46:07 -0700764
765 mutex_lock(&sysfs_lock);
766
767 spin_lock_irqsave(&gpio_lock, flags);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100768 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
769 test_bit(FLAG_EXPORT, &desc->flags)) {
770 spin_unlock_irqrestore(&gpio_lock, flags);
771 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +0900772 __func__, desc_to_gpio(desc),
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100773 test_bit(FLAG_REQUESTED, &desc->flags),
774 test_bit(FLAG_EXPORT, &desc->flags));
Dan Carpenter529f2ad2012-10-26 09:59:43 +0300775 status = -EPERM;
776 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700777 }
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100778
779 if (!desc->chip->direction_input || !desc->chip->direction_output)
780 direction_may_change = false;
David Brownelld8f388d82008-07-25 01:46:07 -0700781 spin_unlock_irqrestore(&gpio_lock, flags);
782
Alexandre Courbot372e7222013-02-03 01:29:29 +0900783 offset = gpio_chip_hwgpio(desc);
784 if (desc->chip->names && desc->chip->names[offset])
785 ioname = desc->chip->names[offset];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700786
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100787 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Alexandre Courbot372e7222013-02-03 01:29:29 +0900788 desc, ioname ? ioname : "gpio%u",
789 desc_to_gpio(desc));
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100790 if (IS_ERR(dev)) {
791 status = PTR_ERR(dev);
792 goto fail_unlock;
David Brownelld8f388d82008-07-25 01:46:07 -0700793 }
794
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100795 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
David Brownelld8f388d82008-07-25 01:46:07 -0700796 if (status)
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100797 goto fail_unregister_device;
David Brownelld8f388d82008-07-25 01:46:07 -0700798
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100799 if (direction_may_change) {
800 status = device_create_file(dev, &dev_attr_direction);
801 if (status)
802 goto fail_unregister_device;
803 }
804
Alexandre Courbot372e7222013-02-03 01:29:29 +0900805 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100806 !test_bit(FLAG_IS_OUT, &desc->flags))) {
807 status = device_create_file(dev, &dev_attr_edge);
808 if (status)
809 goto fail_unregister_device;
810 }
811
812 set_bit(FLAG_EXPORT, &desc->flags);
813 mutex_unlock(&sysfs_lock);
814 return 0;
815
816fail_unregister_device:
817 device_unregister(dev);
818fail_unlock:
819 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900820 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
821 status);
David Brownelld8f388d82008-07-25 01:46:07 -0700822 return status;
823}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900824
825int gpio_export(unsigned gpio, bool direction_may_change)
826{
827 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
828}
David Brownelld8f388d82008-07-25 01:46:07 -0700829EXPORT_SYMBOL_GPL(gpio_export);
830
831static int match_export(struct device *dev, void *data)
832{
833 return dev_get_drvdata(dev) == data;
834}
835
836/**
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700837 * gpio_export_link - create a sysfs link to an exported GPIO node
838 * @dev: device under which to create symlink
839 * @name: name of the symlink
840 * @gpio: gpio to create symlink to, already exported
841 *
842 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
843 * node. Caller is responsible for unlinking.
844 *
845 * Returns zero on success, else an error.
846 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900847static int gpiod_export_link(struct device *dev, const char *name,
848 struct gpio_desc *desc)
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700849{
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700850 int status = -EINVAL;
851
Alexandre Courbot372e7222013-02-03 01:29:29 +0900852 if (!desc)
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700853 goto done;
854
855 mutex_lock(&sysfs_lock);
856
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700857 if (test_bit(FLAG_EXPORT, &desc->flags)) {
858 struct device *tdev;
859
860 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
861 if (tdev != NULL) {
862 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
863 name);
864 } else {
865 status = -ENODEV;
866 }
867 }
868
869 mutex_unlock(&sysfs_lock);
870
871done:
872 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900873 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
874 status);
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700875
876 return status;
877}
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700878
Alexandre Courbot372e7222013-02-03 01:29:29 +0900879int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
880{
881 return gpiod_export_link(dev, name, gpio_to_desc(gpio));
882}
883EXPORT_SYMBOL_GPL(gpio_export_link);
Jani Nikula07697462009-12-15 16:46:20 -0800884
885/**
886 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
887 * @gpio: gpio to change
888 * @value: non-zero to use active low, i.e. inverted values
889 *
890 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
891 * The GPIO does not have to be exported yet. If poll(2) support has
892 * been enabled for either rising or falling edge, it will be
893 * reconfigured to follow the new polarity.
894 *
895 * Returns zero on success, else an error.
896 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900897static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
Jani Nikula07697462009-12-15 16:46:20 -0800898{
Jani Nikula07697462009-12-15 16:46:20 -0800899 struct device *dev = NULL;
900 int status = -EINVAL;
901
Alexandre Courbot372e7222013-02-03 01:29:29 +0900902 if (!desc)
Jani Nikula07697462009-12-15 16:46:20 -0800903 goto done;
904
905 mutex_lock(&sysfs_lock);
906
Jani Nikula07697462009-12-15 16:46:20 -0800907 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800908 dev = class_find_device(&gpio_class, NULL, desc, match_export);
909 if (dev == NULL) {
910 status = -ENODEV;
911 goto unlock;
912 }
913 }
914
915 status = sysfs_set_active_low(desc, dev, value);
916
917unlock:
918 mutex_unlock(&sysfs_lock);
919
920done:
921 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900922 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
923 status);
Jani Nikula07697462009-12-15 16:46:20 -0800924
925 return status;
926}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900927
928int gpio_sysfs_set_active_low(unsigned gpio, int value)
929{
930 return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value);
931}
Jani Nikula07697462009-12-15 16:46:20 -0800932EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
933
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700934/**
David Brownelld8f388d82008-07-25 01:46:07 -0700935 * gpio_unexport - reverse effect of gpio_export()
936 * @gpio: gpio to make unavailable
937 *
938 * This is implicit on gpio_free().
939 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900940static void gpiod_unexport(struct gpio_desc *desc)
David Brownelld8f388d82008-07-25 01:46:07 -0700941{
Jon Povey6a99ad42010-07-27 13:18:06 -0700942 int status = 0;
Ming Lei864533c2012-02-13 22:53:20 +0800943 struct device *dev = NULL;
David Brownelld8f388d82008-07-25 01:46:07 -0700944
Alexandre Courbot372e7222013-02-03 01:29:29 +0900945 if (!desc) {
Jon Povey6a99ad42010-07-27 13:18:06 -0700946 status = -EINVAL;
David Brownelld8f388d82008-07-25 01:46:07 -0700947 goto done;
Jon Povey6a99ad42010-07-27 13:18:06 -0700948 }
David Brownelld8f388d82008-07-25 01:46:07 -0700949
950 mutex_lock(&sysfs_lock);
951
David Brownelld8f388d82008-07-25 01:46:07 -0700952 if (test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700953
954 dev = class_find_device(&gpio_class, NULL, desc, match_export);
955 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700956 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d82008-07-25 01:46:07 -0700957 clear_bit(FLAG_EXPORT, &desc->flags);
David Brownelld8f388d82008-07-25 01:46:07 -0700958 } else
959 status = -ENODEV;
960 }
961
962 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900963
Ming Lei864533c2012-02-13 22:53:20 +0800964 if (dev) {
965 device_unregister(dev);
966 put_device(dev);
967 }
David Brownelld8f388d82008-07-25 01:46:07 -0700968done:
969 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900970 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
971 status);
972}
973
974void gpio_unexport(unsigned gpio)
975{
976 gpiod_unexport(gpio_to_desc(gpio));
David Brownelld8f388d82008-07-25 01:46:07 -0700977}
978EXPORT_SYMBOL_GPL(gpio_unexport);
979
980static int gpiochip_export(struct gpio_chip *chip)
981{
982 int status;
983 struct device *dev;
984
985 /* Many systems register gpio chips for SOC support very early,
986 * before driver model support is available. In those cases we
987 * export this later, in gpiolib_sysfs_init() ... here we just
988 * verify that _some_ field of gpio_class got initialized.
989 */
990 if (!gpio_class.p)
991 return 0;
992
993 /* use chip->base for the ID; it's already known to be unique */
994 mutex_lock(&sysfs_lock);
995 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
996 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -0800997 if (!IS_ERR(dev)) {
David Brownelld8f388d82008-07-25 01:46:07 -0700998 status = sysfs_create_group(&dev->kobj,
999 &gpiochip_attr_group);
1000 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001001 status = PTR_ERR(dev);
David Brownelld8f388d82008-07-25 01:46:07 -07001002 chip->exported = (status == 0);
1003 mutex_unlock(&sysfs_lock);
1004
1005 if (status) {
1006 unsigned long flags;
1007 unsigned gpio;
1008
1009 spin_lock_irqsave(&gpio_lock, flags);
1010 gpio = chip->base;
1011 while (gpio_desc[gpio].chip == chip)
1012 gpio_desc[gpio++].chip = NULL;
1013 spin_unlock_irqrestore(&gpio_lock, flags);
1014
1015 pr_debug("%s: chip %s status %d\n", __func__,
1016 chip->label, status);
1017 }
1018
1019 return status;
1020}
1021
1022static void gpiochip_unexport(struct gpio_chip *chip)
1023{
1024 int status;
1025 struct device *dev;
1026
1027 mutex_lock(&sysfs_lock);
1028 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1029 if (dev) {
1030 put_device(dev);
1031 device_unregister(dev);
1032 chip->exported = 0;
1033 status = 0;
1034 } else
1035 status = -ENODEV;
1036 mutex_unlock(&sysfs_lock);
1037
1038 if (status)
1039 pr_debug("%s: chip %s status %d\n", __func__,
1040 chip->label, status);
1041}
1042
1043static int __init gpiolib_sysfs_init(void)
1044{
1045 int status;
1046 unsigned long flags;
Alexandre Courbot65493e32013-02-03 01:29:25 +09001047 struct gpio_chip *chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001048
1049 status = class_register(&gpio_class);
1050 if (status < 0)
1051 return status;
1052
1053 /* Scan and register the gpio_chips which registered very
1054 * early (e.g. before the class_register above was called).
1055 *
1056 * We run before arch_initcall() so chip->dev nodes can have
1057 * registered, and so arch_initcall() can always gpio_export().
1058 */
1059 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot65493e32013-02-03 01:29:25 +09001060 list_for_each_entry(chip, &gpio_chips, list) {
David Brownelld8f388d82008-07-25 01:46:07 -07001061 if (!chip || chip->exported)
1062 continue;
1063
1064 spin_unlock_irqrestore(&gpio_lock, flags);
1065 status = gpiochip_export(chip);
1066 spin_lock_irqsave(&gpio_lock, flags);
1067 }
1068 spin_unlock_irqrestore(&gpio_lock, flags);
1069
1070
1071 return status;
1072}
1073postcore_initcall(gpiolib_sysfs_init);
1074
1075#else
1076static inline int gpiochip_export(struct gpio_chip *chip)
1077{
1078 return 0;
1079}
1080
1081static inline void gpiochip_unexport(struct gpio_chip *chip)
1082{
1083}
1084
Alexandre Courbot372e7222013-02-03 01:29:29 +09001085static inline int gpiod_export(struct gpio_desc *desc,
1086 bool direction_may_change)
1087{
1088 return -ENOSYS;
1089}
1090
1091static inline int gpiod_export_link(struct device *dev, const char *name,
1092 struct gpio_desc *desc)
1093{
1094 return -ENOSYS;
1095}
1096
1097static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
1098{
1099 return -ENOSYS;
1100}
1101
1102static inline void gpiod_unexport(struct gpio_desc *desc)
1103{
1104}
1105
David Brownelld8f388d82008-07-25 01:46:07 -07001106#endif /* CONFIG_GPIO_SYSFS */
1107
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001108/*
1109 * Add a new chip to the global chips list, keeping the list of chips sorted
1110 * by base order.
1111 *
1112 * Return -EBUSY if the new chip overlaps with some other chip's integer
1113 * space.
1114 */
1115static int gpiochip_add_to_list(struct gpio_chip *chip)
1116{
1117 struct list_head *pos = &gpio_chips;
1118 struct gpio_chip *_chip;
1119 int err = 0;
1120
1121 /* find where to insert our chip */
1122 list_for_each(pos, &gpio_chips) {
1123 _chip = list_entry(pos, struct gpio_chip, list);
1124 /* shall we insert before _chip? */
1125 if (_chip->base >= chip->base + chip->ngpio)
1126 break;
1127 }
1128
1129 /* are we stepping on the chip right before? */
1130 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1131 _chip = list_entry(pos->prev, struct gpio_chip, list);
1132 if (_chip->base + _chip->ngpio > chip->base) {
1133 dev_err(chip->dev,
1134 "GPIO integer space overlap, cannot add chip\n");
1135 err = -EBUSY;
1136 }
1137 }
1138
1139 if (!err)
1140 list_add_tail(&chip->list, pos);
1141
1142 return err;
1143}
1144
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001145/**
David Brownelld2876d02008-02-04 22:28:20 -08001146 * gpiochip_add() - register a gpio_chip
1147 * @chip: the chip to register, with chip->base initialized
1148 * Context: potentially before irqs or kmalloc will work
1149 *
1150 * Returns a negative errno if the chip can't be registered, such as
1151 * because the chip->base is invalid or already associated with a
1152 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001153 *
David Brownelld8f388d82008-07-25 01:46:07 -07001154 * When gpiochip_add() is called very early during boot, so that GPIOs
1155 * can be freely used, the chip->dev device must be registered before
1156 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1157 * for GPIOs will fail rudely.
1158 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001159 * If chip->base is negative, this requests dynamic assignment of
1160 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001161 */
1162int gpiochip_add(struct gpio_chip *chip)
1163{
1164 unsigned long flags;
1165 int status = 0;
1166 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001167 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001168
Trent Piephobff5fda2008-05-23 13:04:44 -07001169 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001170 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001171 status = -EINVAL;
1172 goto fail;
1173 }
1174
1175 spin_lock_irqsave(&gpio_lock, flags);
1176
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001177 if (base < 0) {
1178 base = gpiochip_find_base(chip->ngpio);
1179 if (base < 0) {
1180 status = base;
David Brownelld8f388d82008-07-25 01:46:07 -07001181 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001182 }
1183 chip->base = base;
1184 }
1185
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001186 status = gpiochip_add_to_list(chip);
1187
David Brownelld2876d02008-02-04 22:28:20 -08001188 if (status == 0) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001189 for (id = base; id < base + chip->ngpio; id++) {
David Brownelld2876d02008-02-04 22:28:20 -08001190 gpio_desc[id].chip = chip;
David Brownelld8f388d82008-07-25 01:46:07 -07001191
1192 /* REVISIT: most hardware initializes GPIOs as
1193 * inputs (often with pullups enabled) so power
1194 * usage is minimized. Linux code should set the
1195 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +03001196 * and in case chip->get_direction is not set,
David Brownelld8f388d82008-07-25 01:46:07 -07001197 * we may expose the wrong direction in sysfs.
1198 */
1199 gpio_desc[id].flags = !chip->direction_input
1200 ? (1 << FLAG_IS_OUT)
1201 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001202 }
1203 }
1204
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301205#ifdef CONFIG_PINCTRL
1206 INIT_LIST_HEAD(&chip->pin_ranges);
1207#endif
1208
Anton Vorontsov391c9702010-06-08 07:48:17 -06001209 of_gpiochip_add(chip);
1210
David Brownelld8f388d82008-07-25 01:46:07 -07001211unlock:
David Brownelld2876d02008-02-04 22:28:20 -08001212 spin_unlock_irqrestore(&gpio_lock, flags);
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001213
1214 if (status)
1215 goto fail;
1216
1217 status = gpiochip_export(chip);
1218 if (status)
1219 goto fail;
1220
H Hartley Sweetenee1c1e72012-05-11 16:58:33 -07001221 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
Grant Likely64842aa2011-11-06 11:36:18 -07001222 chip->base, chip->base + chip->ngpio - 1,
1223 chip->label ? : "generic");
1224
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001225 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001226fail:
1227 /* failures here can mean systems won't boot... */
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001228 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1229 chip->base, chip->base + chip->ngpio - 1,
1230 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001231 return status;
1232}
1233EXPORT_SYMBOL_GPL(gpiochip_add);
1234
1235/**
1236 * gpiochip_remove() - unregister a gpio_chip
1237 * @chip: the chip to unregister
1238 *
1239 * A gpio_chip with any GPIOs still requested may not be removed.
1240 */
1241int gpiochip_remove(struct gpio_chip *chip)
1242{
1243 unsigned long flags;
1244 int status = 0;
1245 unsigned id;
1246
1247 spin_lock_irqsave(&gpio_lock, flags);
1248
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001249 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001250 of_gpiochip_remove(chip);
1251
David Brownelld2876d02008-02-04 22:28:20 -08001252 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1253 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1254 status = -EBUSY;
1255 break;
1256 }
1257 }
1258 if (status == 0) {
1259 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1260 gpio_desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001261
1262 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -08001263 }
1264
1265 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d82008-07-25 01:46:07 -07001266
1267 if (status == 0)
1268 gpiochip_unexport(chip);
1269
David Brownelld2876d02008-02-04 22:28:20 -08001270 return status;
1271}
1272EXPORT_SYMBOL_GPL(gpiochip_remove);
1273
Grant Likely594fa262010-06-08 07:48:16 -06001274/**
1275 * gpiochip_find() - iterator for locating a specific gpio_chip
1276 * @data: data to pass to match function
1277 * @callback: Callback function to check gpio_chip
1278 *
1279 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1280 * determined by a user supplied @match callback. The callback should return
1281 * 0 if the device doesn't match and non-zero if it does. If the callback is
1282 * non-zero, this function will return to the caller and not iterate over any
1283 * more gpio_chips.
1284 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001285struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001286 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001287 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001288{
Alexandre Courbot125eef92013-02-03 01:29:26 +09001289 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001290 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001291
1292 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +09001293 list_for_each_entry(chip, &gpio_chips, list)
1294 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001295 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001296
1297 /* No match? */
1298 if (&chip->list == &gpio_chips)
1299 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001300 spin_unlock_irqrestore(&gpio_lock, flags);
1301
1302 return chip;
1303}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001304EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001305
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301306#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001307
Linus Walleij3f0f8672012-11-20 12:40:15 +01001308/**
1309 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1310 * @chip: the gpiochip to add the range for
1311 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001312 * @gpio_offset: the start offset in the current gpio_chip number space
1313 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001314 * @npins: the number of pins from the offset of each pin space (GPIO and
1315 * pin controller) to accumulate in this range
1316 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001317int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001318 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001319 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301320{
1321 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001322 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301323
Linus Walleij3f0f8672012-11-20 12:40:15 +01001324 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301325 if (!pin_range) {
1326 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1327 chip->label);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001328 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301329 }
1330
Linus Walleij3f0f8672012-11-20 12:40:15 +01001331 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001332 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001333 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301334 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001335 pin_range->range.base = chip->base + gpio_offset;
1336 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301337 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001338 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301339 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001340 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001341 ret = PTR_ERR(pin_range->pctldev);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001342 pr_err("%s: GPIO chip: could not create pin range\n",
1343 chip->label);
1344 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001345 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001346 }
Linus Walleij316511c2012-11-21 08:48:09 +01001347 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1348 chip->label, gpio_offset, gpio_offset + npins - 1,
1349 pinctl_name,
1350 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301351
1352 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001353
1354 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301355}
Linus Walleij165adc92012-11-06 14:49:39 +01001356EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301357
Linus Walleij3f0f8672012-11-20 12:40:15 +01001358/**
1359 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1360 * @chip: the chip to remove all the mappings for
1361 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301362void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1363{
1364 struct gpio_pin_range *pin_range, *tmp;
1365
1366 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1367 list_del(&pin_range->node);
1368 pinctrl_remove_gpio_range(pin_range->pctldev,
1369 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001370 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301371 }
1372}
Linus Walleij165adc92012-11-06 14:49:39 +01001373EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1374
1375#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301376
David Brownelld2876d02008-02-04 22:28:20 -08001377/* These "optional" allocation calls help prevent drivers from stomping
1378 * on each other, and help provide better diagnostics in debugfs.
1379 * They're called even less than the "set direction" calls.
1380 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001381static int gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001382{
David Brownell35e8bb52008-10-15 22:03:16 -07001383 struct gpio_chip *chip;
Mark Browne9354572012-07-09 12:22:56 +01001384 int status = -EPROBE_DEFER;
David Brownelld2876d02008-02-04 22:28:20 -08001385 unsigned long flags;
1386
1387 spin_lock_irqsave(&gpio_lock, flags);
1388
Alexandre Courbot372e7222013-02-03 01:29:29 +09001389 if (!desc) {
Mathias Nymanad2fab32012-10-25 14:03:03 +03001390 status = -EINVAL;
David Brownelld2876d02008-02-04 22:28:20 -08001391 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +03001392 }
David Brownell35e8bb52008-10-15 22:03:16 -07001393 chip = desc->chip;
1394 if (chip == NULL)
David Brownelld2876d02008-02-04 22:28:20 -08001395 goto done;
1396
David Brownell35e8bb52008-10-15 22:03:16 -07001397 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001398 goto done;
1399
David Brownelld2876d02008-02-04 22:28:20 -08001400 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001401 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001402 */
1403
1404 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1405 desc_set_label(desc, label ? : "?");
1406 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001407 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001408 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001409 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001410 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001411 }
1412
1413 if (chip->request) {
1414 /* chip->request may sleep */
1415 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001416 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001417 spin_lock_irqsave(&gpio_lock, flags);
1418
1419 if (status < 0) {
1420 desc_set_label(desc, NULL);
1421 module_put(chip->owner);
1422 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001423 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001424 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001425 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001426 if (chip->get_direction) {
1427 /* chip->get_direction may sleep */
1428 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001429 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001430 spin_lock_irqsave(&gpio_lock, flags);
1431 }
David Brownelld2876d02008-02-04 22:28:20 -08001432done:
1433 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001434 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
1435 desc ? desc_to_gpio(desc) : -1,
1436 label ? : "?", status);
David Brownelld2876d02008-02-04 22:28:20 -08001437 spin_unlock_irqrestore(&gpio_lock, flags);
1438 return status;
1439}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001440
1441int gpio_request(unsigned gpio, const char *label)
1442{
1443 return gpiod_request(gpio_to_desc(gpio), label);
1444}
David Brownelld2876d02008-02-04 22:28:20 -08001445EXPORT_SYMBOL_GPL(gpio_request);
1446
Alexandre Courbot372e7222013-02-03 01:29:29 +09001447static void gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001448{
1449 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001450 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001451
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001452 might_sleep();
1453
Alexandre Courbot372e7222013-02-03 01:29:29 +09001454 if (!desc) {
David Brownelld2876d02008-02-04 22:28:20 -08001455 WARN_ON(extra_checks);
1456 return;
1457 }
1458
Alexandre Courbot372e7222013-02-03 01:29:29 +09001459 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07001460
David Brownelld2876d02008-02-04 22:28:20 -08001461 spin_lock_irqsave(&gpio_lock, flags);
1462
David Brownell35e8bb52008-10-15 22:03:16 -07001463 chip = desc->chip;
1464 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1465 if (chip->free) {
1466 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001467 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001468 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001469 spin_lock_irqsave(&gpio_lock, flags);
1470 }
David Brownelld2876d02008-02-04 22:28:20 -08001471 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001472 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001473 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001474 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301475 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301476 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001477 } else
David Brownelld2876d02008-02-04 22:28:20 -08001478 WARN_ON(extra_checks);
1479
1480 spin_unlock_irqrestore(&gpio_lock, flags);
1481}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001482
1483void gpio_free(unsigned gpio)
1484{
1485 gpiod_free(gpio_to_desc(gpio));
1486}
David Brownelld2876d02008-02-04 22:28:20 -08001487EXPORT_SYMBOL_GPL(gpio_free);
1488
Eric Miao3e45f1d2010-03-05 13:44:35 -08001489/**
1490 * gpio_request_one - request a single GPIO with initial configuration
1491 * @gpio: the GPIO number
1492 * @flags: GPIO configuration as specified by GPIOF_*
1493 * @label: a literal description string of this GPIO
1494 */
1495int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1496{
Alexandre Courbot372e7222013-02-03 01:29:29 +09001497 struct gpio_desc *desc;
Eric Miao3e45f1d2010-03-05 13:44:35 -08001498 int err;
1499
Alexandre Courbot372e7222013-02-03 01:29:29 +09001500 desc = gpio_to_desc(gpio);
1501
1502 err = gpiod_request(desc, label);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001503 if (err)
1504 return err;
1505
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301506 if (flags & GPIOF_OPEN_DRAIN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001507 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301508
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301509 if (flags & GPIOF_OPEN_SOURCE)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001510 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301511
Eric Miao3e45f1d2010-03-05 13:44:35 -08001512 if (flags & GPIOF_DIR_IN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001513 err = gpiod_direction_input(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001514 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001515 err = gpiod_direction_output(desc,
Eric Miao3e45f1d2010-03-05 13:44:35 -08001516 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1517
Aaro Koskinene2548112010-12-21 17:24:22 -08001518 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001519 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -08001520
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001521 if (flags & GPIOF_EXPORT) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001522 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001523 if (err)
1524 goto free_gpio;
1525 }
1526
1527 return 0;
1528
1529 free_gpio:
Alexandre Courbot372e7222013-02-03 01:29:29 +09001530 gpiod_free(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001531 return err;
1532}
1533EXPORT_SYMBOL_GPL(gpio_request_one);
1534
1535/**
1536 * gpio_request_array - request multiple GPIOs in a single call
1537 * @array: array of the 'struct gpio'
1538 * @num: how many GPIOs in the array
1539 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001540int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001541{
1542 int i, err;
1543
1544 for (i = 0; i < num; i++, array++) {
1545 err = gpio_request_one(array->gpio, array->flags, array->label);
1546 if (err)
1547 goto err_free;
1548 }
1549 return 0;
1550
1551err_free:
1552 while (i--)
1553 gpio_free((--array)->gpio);
1554 return err;
1555}
1556EXPORT_SYMBOL_GPL(gpio_request_array);
1557
1558/**
1559 * gpio_free_array - release multiple GPIOs in a single call
1560 * @array: array of the 'struct gpio'
1561 * @num: how many GPIOs in the array
1562 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001563void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001564{
1565 while (num--)
1566 gpio_free((array++)->gpio);
1567}
1568EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001569
1570/**
1571 * gpiochip_is_requested - return string iff signal was requested
1572 * @chip: controller managing the signal
1573 * @offset: of signal within controller's 0..(ngpio - 1) range
1574 *
1575 * Returns NULL if the GPIO is not currently requested, else a string.
1576 * If debugfs support is enabled, the string returned is the label passed
1577 * to gpio_request(); otherwise it is a meaningless constant.
1578 *
1579 * This function is for use by GPIO controller drivers. The label can
1580 * help with diagnostics, and knowing that the signal is used as a GPIO
1581 * can help avoid accidentally multiplexing it to another controller.
1582 */
1583const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1584{
1585 unsigned gpio = chip->base + offset;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001586 struct gpio_desc *desc = &gpio_desc[gpio];
David Brownelld2876d02008-02-04 22:28:20 -08001587
Alexandre Courbot372e7222013-02-03 01:29:29 +09001588 if (!gpio_is_valid(gpio) || desc->chip != chip)
David Brownelld2876d02008-02-04 22:28:20 -08001589 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001590 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001591 return NULL;
1592#ifdef CONFIG_DEBUG_FS
Alexandre Courbot372e7222013-02-03 01:29:29 +09001593 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001594#else
1595 return "?";
1596#endif
1597}
1598EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1599
1600
1601/* Drivers MUST set GPIO direction before making get/set calls. In
1602 * some cases this is done in early boot, before IRQs are enabled.
1603 *
1604 * As a rule these aren't called more than once (except for drivers
1605 * using the open-drain emulation idiom) so these are natural places
1606 * to accumulate extra debugging checks. Note that we can't (yet)
1607 * rely on gpio_request() having been called beforehand.
1608 */
1609
Alexandre Courbot372e7222013-02-03 01:29:29 +09001610static int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001611{
1612 unsigned long flags;
1613 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001614 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001615 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001616
1617 spin_lock_irqsave(&gpio_lock, flags);
1618
Alexandre Courbot372e7222013-02-03 01:29:29 +09001619 if (!desc)
David Brownelld2876d02008-02-04 22:28:20 -08001620 goto fail;
1621 chip = desc->chip;
1622 if (!chip || !chip->get || !chip->direction_input)
1623 goto fail;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001624 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001625 if (status < 0)
1626 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001627
1628 /* now we know the gpio is valid and chip won't vanish */
1629
1630 spin_unlock_irqrestore(&gpio_lock, flags);
1631
David Brownell9c4ba942010-08-10 18:02:24 -07001632 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001633
Alexandre Courbot372e7222013-02-03 01:29:29 +09001634 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001635 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001636 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001637 if (status < 0) {
1638 pr_debug("GPIO-%d: chip request fail, %d\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +09001639 desc_to_gpio(desc), status);
David Brownell35e8bb52008-10-15 22:03:16 -07001640 /* and it's not available to anyone else ...
1641 * gpio_request() is the fully clean solution.
1642 */
1643 goto lose;
1644 }
1645 }
1646
Alexandre Courbot372e7222013-02-03 01:29:29 +09001647 status = chip->direction_input(chip, offset);
David Brownelld2876d02008-02-04 22:28:20 -08001648 if (status == 0)
1649 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001650
Alexandre Courbot372e7222013-02-03 01:29:29 +09001651 trace_gpio_direction(desc_to_gpio(desc), 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001652lose:
David Brownelld2876d02008-02-04 22:28:20 -08001653 return status;
1654fail:
1655 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001656 if (status) {
1657 int gpio = -1;
1658 if (desc)
1659 gpio = desc_to_gpio(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001660 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001661 __func__, gpio, status);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001662 }
David Brownelld2876d02008-02-04 22:28:20 -08001663 return status;
1664}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001665
1666int gpio_direction_input(unsigned gpio)
1667{
1668 return gpiod_direction_input(gpio_to_desc(gpio));
1669}
David Brownelld2876d02008-02-04 22:28:20 -08001670EXPORT_SYMBOL_GPL(gpio_direction_input);
1671
Alexandre Courbot372e7222013-02-03 01:29:29 +09001672static int gpiod_direction_output(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001673{
1674 unsigned long flags;
1675 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001676 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001677 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001678
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301679 /* Open drain pin should not be driven to 1 */
1680 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001681 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301682
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301683 /* Open source pin should not be driven to 0 */
1684 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001685 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301686
David Brownelld2876d02008-02-04 22:28:20 -08001687 spin_lock_irqsave(&gpio_lock, flags);
1688
Alexandre Courbot372e7222013-02-03 01:29:29 +09001689 if (!desc)
David Brownelld2876d02008-02-04 22:28:20 -08001690 goto fail;
1691 chip = desc->chip;
1692 if (!chip || !chip->set || !chip->direction_output)
1693 goto fail;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001694 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001695 if (status < 0)
1696 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001697
1698 /* now we know the gpio is valid and chip won't vanish */
1699
1700 spin_unlock_irqrestore(&gpio_lock, flags);
1701
David Brownell9c4ba942010-08-10 18:02:24 -07001702 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001703
Alexandre Courbot372e7222013-02-03 01:29:29 +09001704 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001705 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001706 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001707 if (status < 0) {
1708 pr_debug("GPIO-%d: chip request fail, %d\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +09001709 desc_to_gpio(desc), status);
David Brownell35e8bb52008-10-15 22:03:16 -07001710 /* and it's not available to anyone else ...
1711 * gpio_request() is the fully clean solution.
1712 */
1713 goto lose;
1714 }
1715 }
1716
Alexandre Courbot372e7222013-02-03 01:29:29 +09001717 status = chip->direction_output(chip, offset, value);
David Brownelld2876d02008-02-04 22:28:20 -08001718 if (status == 0)
1719 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001720 trace_gpio_value(desc_to_gpio(desc), 0, value);
1721 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001722lose:
David Brownelld2876d02008-02-04 22:28:20 -08001723 return status;
1724fail:
1725 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001726 if (status) {
1727 int gpio = -1;
1728 if (desc)
1729 gpio = desc_to_gpio(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001730 pr_debug("%s: gpio-%d status %d\n",
Harvey Harrison145980a2008-04-30 00:54:57 -07001731 __func__, gpio, status);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001732 }
David Brownelld2876d02008-02-04 22:28:20 -08001733 return status;
1734}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001735
1736int gpio_direction_output(unsigned gpio, int value)
1737{
1738 return gpiod_direction_output(gpio_to_desc(gpio), value);
1739}
David Brownelld2876d02008-02-04 22:28:20 -08001740EXPORT_SYMBOL_GPL(gpio_direction_output);
1741
Felipe Balbic4b5be92010-05-26 14:42:23 -07001742/**
1743 * gpio_set_debounce - sets @debounce time for a @gpio
1744 * @gpio: the gpio to set debounce time
1745 * @debounce: debounce time is microseconds
1746 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001747static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001748{
1749 unsigned long flags;
1750 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001751 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001752 int offset;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001753
1754 spin_lock_irqsave(&gpio_lock, flags);
1755
Alexandre Courbot372e7222013-02-03 01:29:29 +09001756 if (!desc)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001757 goto fail;
1758 chip = desc->chip;
1759 if (!chip || !chip->set || !chip->set_debounce)
1760 goto fail;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001761
1762 status = gpio_ensure_requested(desc);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001763 if (status < 0)
1764 goto fail;
1765
1766 /* now we know the gpio is valid and chip won't vanish */
1767
1768 spin_unlock_irqrestore(&gpio_lock, flags);
1769
David Brownell9c4ba942010-08-10 18:02:24 -07001770 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001771
Alexandre Courbot372e7222013-02-03 01:29:29 +09001772 offset = gpio_chip_hwgpio(desc);
1773 return chip->set_debounce(chip, offset, debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001774
1775fail:
1776 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001777 if (status) {
1778 int gpio = -1;
1779 if (desc)
1780 gpio = desc_to_gpio(desc);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001781 pr_debug("%s: gpio-%d status %d\n",
1782 __func__, gpio, status);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001783 }
Felipe Balbic4b5be92010-05-26 14:42:23 -07001784
1785 return status;
1786}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001787
1788int gpio_set_debounce(unsigned gpio, unsigned debounce)
1789{
1790 return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
1791}
Felipe Balbic4b5be92010-05-26 14:42:23 -07001792EXPORT_SYMBOL_GPL(gpio_set_debounce);
David Brownelld2876d02008-02-04 22:28:20 -08001793
1794/* I/O calls are only valid after configuration completed; the relevant
1795 * "is this a valid GPIO" error checks should already have been done.
1796 *
1797 * "Get" operations are often inlinable as reading a pin value register,
1798 * and masking the relevant bit in that register.
1799 *
1800 * When "set" operations are inlinable, they involve writing that mask to
1801 * one register to set a low value, or a different register to set it high.
1802 * Otherwise locking is needed, so there may be little value to inlining.
1803 *
1804 *------------------------------------------------------------------------
1805 *
1806 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1807 * have requested the GPIO. That can include implicit requesting by
1808 * a direction setting call. Marking a gpio as requested locks its chip
1809 * in memory, guaranteeing that these table lookups need no more locking
1810 * and that gpiochip_remove() will fail.
1811 *
1812 * REVISIT when debugging, consider adding some instrumentation to ensure
1813 * that the GPIO was actually requested.
1814 */
1815
1816/**
1817 * __gpio_get_value() - return a gpio's value
1818 * @gpio: gpio whose value will be returned
1819 * Context: any
1820 *
1821 * This is used directly or indirectly to implement gpio_get_value().
1822 * It returns the zero or nonzero value provided by the associated
1823 * gpio_chip.get() method; or zero if no such method is provided.
1824 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001825static int gpiod_get_value(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001826{
1827 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001828 int value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001829 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001830
Alexandre Courbot372e7222013-02-03 01:29:29 +09001831 chip = desc->chip;
1832 offset = gpio_chip_hwgpio(desc);
Mark Browne4e449e2012-02-17 10:46:00 -08001833 /* Should be using gpio_get_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001834 WARN_ON(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001835 value = chip->get ? chip->get(chip, offset) : 0;
1836 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001837 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001838}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001839
1840int __gpio_get_value(unsigned gpio)
1841{
1842 return gpiod_get_value(gpio_to_desc(gpio));
1843}
David Brownelld2876d02008-02-04 22:28:20 -08001844EXPORT_SYMBOL_GPL(__gpio_get_value);
1845
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301846/*
1847 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1848 * @gpio: Gpio whose state need to be set.
1849 * @chip: Gpio chip.
1850 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1851 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001852static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301853{
1854 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001855 struct gpio_chip *chip = desc->chip;
1856 int offset = gpio_chip_hwgpio(desc);
1857
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301858 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001859 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301860 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001861 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301862 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001863 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301864 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001865 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301866 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001867 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301868 if (err < 0)
1869 pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +09001870 __func__, desc_to_gpio(desc), err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301871}
1872
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301873/*
1874 * _gpio_set_open_source() - Set the open source gpio's value.
1875 * @gpio: Gpio whose state need to be set.
1876 * @chip: Gpio chip.
1877 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1878 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001879static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301880{
1881 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001882 struct gpio_chip *chip = desc->chip;
1883 int offset = gpio_chip_hwgpio(desc);
1884
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301885 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001886 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301887 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001888 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301889 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001890 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301891 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001892 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301893 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001894 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301895 if (err < 0)
1896 pr_err("%s: Error in set_value for open source gpio%d err %d\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +09001897 __func__, desc_to_gpio(desc), err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301898}
1899
David Brownelld2876d02008-02-04 22:28:20 -08001900/**
1901 * __gpio_set_value() - assign a gpio's value
1902 * @gpio: gpio whose value will be assigned
1903 * @value: value to assign
1904 * Context: any
1905 *
1906 * This is used directly or indirectly to implement gpio_set_value().
1907 * It invokes the associated gpio_chip.set() method.
1908 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001909static void gpiod_set_value(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001910{
1911 struct gpio_chip *chip;
1912
Alexandre Courbot372e7222013-02-03 01:29:29 +09001913 chip = desc->chip;
Mark Browne4e449e2012-02-17 10:46:00 -08001914 /* Should be using gpio_set_value_cansleep() */
David Brownell9c4ba942010-08-10 18:02:24 -07001915 WARN_ON(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001916 trace_gpio_value(desc_to_gpio(desc), 0, value);
1917 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1918 _gpio_set_open_drain_value(desc, value);
1919 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1920 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301921 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001922 chip->set(chip, gpio_chip_hwgpio(desc), value);
1923}
1924
1925void __gpio_set_value(unsigned gpio, int value)
1926{
1927 return gpiod_set_value(gpio_to_desc(gpio), value);
David Brownelld2876d02008-02-04 22:28:20 -08001928}
1929EXPORT_SYMBOL_GPL(__gpio_set_value);
1930
1931/**
1932 * __gpio_cansleep() - report whether gpio value access will sleep
1933 * @gpio: gpio in question
1934 * Context: any
1935 *
1936 * This is used directly or indirectly to implement gpio_cansleep(). It
1937 * returns nonzero if access reading or writing the GPIO value can sleep.
1938 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001939static int gpiod_cansleep(struct gpio_desc *desc)
1940{
1941 /* only call this on GPIOs that are valid! */
1942 return desc->chip->can_sleep;
1943}
1944
David Brownelld2876d02008-02-04 22:28:20 -08001945int __gpio_cansleep(unsigned gpio)
1946{
Alexandre Courbot372e7222013-02-03 01:29:29 +09001947 return gpiod_cansleep(gpio_to_desc(gpio));
David Brownelld2876d02008-02-04 22:28:20 -08001948}
1949EXPORT_SYMBOL_GPL(__gpio_cansleep);
1950
David Brownell0f6d5042008-10-15 22:03:14 -07001951/**
1952 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1953 * @gpio: gpio whose IRQ will be returned (already requested)
1954 * Context: any
1955 *
1956 * This is used directly or indirectly to implement gpio_to_irq().
1957 * It returns the number of the IRQ signaled by this (input) GPIO,
1958 * or a negative errno.
1959 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001960static int gpiod_to_irq(struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001961{
1962 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001963 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001964
Alexandre Courbot372e7222013-02-03 01:29:29 +09001965 chip = desc->chip;
1966 offset = gpio_chip_hwgpio(desc);
1967 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1968}
1969
1970int __gpio_to_irq(unsigned gpio)
1971{
1972 return gpiod_to_irq(gpio_to_desc(gpio));
David Brownell0f6d5042008-10-15 22:03:14 -07001973}
1974EXPORT_SYMBOL_GPL(__gpio_to_irq);
1975
David Brownelld2876d02008-02-04 22:28:20 -08001976
David Brownelld2876d02008-02-04 22:28:20 -08001977/* There's no value in making it easy to inline GPIO calls that may sleep.
1978 * Common examples include ones connected to I2C or SPI chips.
1979 */
1980
Alexandre Courbot372e7222013-02-03 01:29:29 +09001981static int gpiod_get_value_cansleep(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001982{
1983 struct gpio_chip *chip;
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001984 int value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001985 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001986
1987 might_sleep_if(extra_checks);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001988 chip = desc->chip;
1989 offset = gpio_chip_hwgpio(desc);
1990 value = chip->get ? chip->get(chip, offset) : 0;
1991 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001992 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001993}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001994
1995int gpio_get_value_cansleep(unsigned gpio)
1996{
1997 return gpiod_get_value_cansleep(gpio_to_desc(gpio));
1998}
David Brownelld2876d02008-02-04 22:28:20 -08001999EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
2000
Alexandre Courbot372e7222013-02-03 01:29:29 +09002001static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002002{
2003 struct gpio_chip *chip;
2004
2005 might_sleep_if(extra_checks);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002006 chip = desc->chip;
2007 trace_gpio_value(desc_to_gpio(desc), 0, value);
2008 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2009 _gpio_set_open_drain_value(desc, value);
2010 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2011 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302012 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002013 chip->set(chip, gpio_chip_hwgpio(desc), value);
2014}
2015
2016void gpio_set_value_cansleep(unsigned gpio, int value)
2017{
2018 return gpiod_set_value_cansleep(gpio_to_desc(gpio), value);
David Brownelld2876d02008-02-04 22:28:20 -08002019}
2020EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
2021
David Brownelld2876d02008-02-04 22:28:20 -08002022#ifdef CONFIG_DEBUG_FS
2023
David Brownelld2876d02008-02-04 22:28:20 -08002024static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2025{
2026 unsigned i;
2027 unsigned gpio = chip->base;
2028 struct gpio_desc *gdesc = &gpio_desc[gpio];
2029 int is_out;
2030
2031 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2032 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2033 continue;
2034
Alexandre Courbot372e7222013-02-03 01:29:29 +09002035 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002036 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Jarkko Nikula6e8ba722008-11-19 15:36:17 -08002037 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002038 gpio, gdesc->label,
2039 is_out ? "out" : "in ",
2040 chip->get
2041 ? (chip->get(chip, i) ? "hi" : "lo")
2042 : "? ");
David Brownelld2876d02008-02-04 22:28:20 -08002043 seq_printf(s, "\n");
2044 }
2045}
2046
Thierry Redingf9c4a312012-04-12 13:26:01 +02002047static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002048{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002049 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002050 loff_t index = *pos;
David Brownelld2876d02008-02-04 22:28:20 -08002051
2052 /* REVISIT this isn't locked against gpio_chip removal ... */
2053
Thierry Redingf9c4a312012-04-12 13:26:01 +02002054 s->private = "";
2055
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002056 list_for_each_entry(chip, &gpio_chips, list)
2057 if (index-- == 0)
2058 return chip;
2059
2060 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002061}
2062
2063static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2064{
2065 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002066 void *ret = NULL;
2067
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002068 if (list_is_last(&chip->list, &gpio_chips))
2069 ret = NULL;
2070 else
2071 ret = list_entry(chip->list.next, struct gpio_chip, list);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002072
2073 s->private = "\n";
2074 ++*pos;
2075
2076 return ret;
2077}
2078
2079static void gpiolib_seq_stop(struct seq_file *s, void *v)
2080{
2081}
2082
2083static int gpiolib_seq_show(struct seq_file *s, void *v)
2084{
2085 struct gpio_chip *chip = v;
2086 struct device *dev;
2087
2088 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2089 chip->base, chip->base + chip->ngpio - 1);
2090 dev = chip->dev;
2091 if (dev)
2092 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2093 dev_name(dev));
2094 if (chip->label)
2095 seq_printf(s, ", %s", chip->label);
2096 if (chip->can_sleep)
2097 seq_printf(s, ", can sleep");
2098 seq_printf(s, ":\n");
2099
2100 if (chip->dbg_show)
2101 chip->dbg_show(s, chip);
2102 else
2103 gpiolib_dbg_show(s, chip);
2104
David Brownelld2876d02008-02-04 22:28:20 -08002105 return 0;
2106}
2107
Thierry Redingf9c4a312012-04-12 13:26:01 +02002108static const struct seq_operations gpiolib_seq_ops = {
2109 .start = gpiolib_seq_start,
2110 .next = gpiolib_seq_next,
2111 .stop = gpiolib_seq_stop,
2112 .show = gpiolib_seq_show,
2113};
2114
David Brownelld2876d02008-02-04 22:28:20 -08002115static int gpiolib_open(struct inode *inode, struct file *file)
2116{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002117 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002118}
2119
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002120static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002121 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002122 .open = gpiolib_open,
2123 .read = seq_read,
2124 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002125 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002126};
2127
2128static int __init gpiolib_debugfs_init(void)
2129{
2130 /* /sys/kernel/debug/gpio */
2131 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2132 NULL, NULL, &gpiolib_operations);
2133 return 0;
2134}
2135subsys_initcall(gpiolib_debugfs_init);
2136
2137#endif /* DEBUG_FS */