blob: 1014cb5e10b0ae781b50464e2598a6d72a3453fc [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 Brownelld8f388d2008-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 */
Linus Walleijd468bf92013-09-24 11:54:38 +020063#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070064
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070065#define ID_SHIFT 16 /* add new flags before this one */
Daniel Glöcknerff77c352009-09-22 16:46:38 -070066
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070067#define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
Daniel Glöcknerff77c352009-09-22 16:46:38 -070068#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
David Brownelld2876d02008-02-04 22:28:20 -080069
70#ifdef CONFIG_DEBUG_FS
71 const char *label;
72#endif
73};
74static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
75
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +090076#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
77
Alexandre Courbot1a989d02013-02-03 01:29:24 +090078static LIST_HEAD(gpio_chips);
79
Daniel Glöcknerff77c352009-09-22 16:46:38 -070080#ifdef CONFIG_GPIO_SYSFS
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -070081static DEFINE_IDR(dirent_idr);
Daniel Glöcknerff77c352009-09-22 16:46:38 -070082#endif
83
Alexandre Courbot372e7222013-02-03 01:29:29 +090084/*
85 * Internal gpiod_* API using descriptors instead of the integer namespace.
86 * Most of this should eventually go public.
87 */
88static int gpiod_request(struct gpio_desc *desc, const char *label);
89static void gpiod_free(struct gpio_desc *desc);
90static int gpiod_direction_input(struct gpio_desc *desc);
91static int gpiod_direction_output(struct gpio_desc *desc, int value);
Alexandre Courbotdef63432013-02-15 14:46:15 +090092static int gpiod_get_direction(const struct gpio_desc *desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090093static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
Alexandre Courbotdef63432013-02-15 14:46:15 +090094static int gpiod_get_value_cansleep(const struct gpio_desc *desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090095static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
Alexandre Courbotdef63432013-02-15 14:46:15 +090096static int gpiod_get_value(const struct gpio_desc *desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090097static void gpiod_set_value(struct gpio_desc *desc, int value);
Alexandre Courbotdef63432013-02-15 14:46:15 +090098static int gpiod_cansleep(const struct gpio_desc *desc);
99static int gpiod_to_irq(const struct gpio_desc *desc);
Linus Walleijd468bf92013-09-24 11:54:38 +0200100static int gpiod_lock_as_irq(struct gpio_desc *desc);
101static void gpiod_unlock_as_irq(struct gpio_desc *desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900102static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
103static int gpiod_export_link(struct device *dev, const char *name,
104 struct gpio_desc *desc);
105static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
106static void gpiod_unexport(struct gpio_desc *desc);
107
Mark Brown7b17b592013-09-09 10:33:50 +0100108#ifdef CONFIG_DEBUG_FS
109#define gpiod_emerg(desc, fmt, ...) \
110 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
111 ##__VA_ARGS__)
112#define gpiod_crit(desc, fmt, ...) \
113 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
114 ##__VA_ARGS__)
115#define gpiod_err(desc, fmt, ...) \
116 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
117 ##__VA_ARGS__)
118#define gpiod_warn(desc, fmt, ...) \
119 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
120 ##__VA_ARGS__)
121#define gpiod_info(desc, fmt, ...) \
122 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
123 ##__VA_ARGS__)
124#define gpiod_dbg(desc, fmt, ...) \
125 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
126 ##__VA_ARGS__)
127#else
Mark Brown6424de52013-09-09 10:33:49 +0100128#define gpiod_emerg(desc, fmt, ...) \
129 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
130#define gpiod_crit(desc, fmt, ...) \
131 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
132#define gpiod_err(desc, fmt, ...) \
133 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
134#define gpiod_warn(desc, fmt, ...) \
135 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
136#define gpiod_info(desc, fmt, ...) \
137 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
138#define gpiod_dbg(desc, fmt, ...) \
139 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
Mark Brown7b17b592013-09-09 10:33:50 +0100140#endif
Alexandre Courbot372e7222013-02-03 01:29:29 +0900141
David Brownelld2876d02008-02-04 22:28:20 -0800142static inline void desc_set_label(struct gpio_desc *d, const char *label)
143{
144#ifdef CONFIG_DEBUG_FS
145 d->label = label;
146#endif
147}
148
Alexandre Courbot372e7222013-02-03 01:29:29 +0900149/*
150 * Return the GPIO number of the passed descriptor relative to its chip
151 */
152static int gpio_chip_hwgpio(const struct gpio_desc *desc)
153{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900154 return desc - &desc->chip->desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +0900155}
156
157/**
158 * Convert a GPIO number to its descriptor
159 */
160static struct gpio_desc *gpio_to_desc(unsigned gpio)
161{
162 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
163 return NULL;
164 else
165 return &gpio_desc[gpio];
166}
167
168/**
Linus Walleijd468bf92013-09-24 11:54:38 +0200169 * Convert an offset on a certain chip to a corresponding descriptor
170 */
171static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip,
172 unsigned int offset)
173{
174 unsigned int gpio = chip->base + offset;
175
176 return gpio_to_desc(gpio);
177}
178
179/**
Alexandre Courbot372e7222013-02-03 01:29:29 +0900180 * Convert a GPIO descriptor to the integer namespace.
181 * This should disappear in the future but is needed since we still
182 * use GPIO numbers for error messages and sysfs nodes
183 */
184static int desc_to_gpio(const struct gpio_desc *desc)
185{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900186 return desc->chip->base + gpio_chip_hwgpio(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900187}
188
189
David Brownelld2876d02008-02-04 22:28:20 -0800190/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
191 * when setting direction, and otherwise illegal. Until board setup code
192 * and drivers use explicit requests everywhere (which won't happen when
193 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -0700194 * message should motivate switching to explicit requests... so should
195 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -0700196 *
197 * NOTE: the autorequest mechanism is going away; at this point it's
198 * only "legal" in the sense that (old) code using it won't break yet,
199 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -0800200 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900201static int gpio_ensure_requested(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800202{
David Brownell8a0cecf2009-04-02 16:57:06 -0700203 const struct gpio_chip *chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900204 const int gpio = desc_to_gpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -0700205
David Brownell8a0cecf2009-04-02 16:57:06 -0700206 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
207 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700208 if (!try_module_get(chip->owner)) {
209 pr_err("GPIO-%d: module can't be gotten \n", gpio);
210 clear_bit(FLAG_REQUESTED, &desc->flags);
211 /* lose */
212 return -EIO;
213 }
David Brownelld2876d02008-02-04 22:28:20 -0800214 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700215 /* caller must chip->request() w/o spinlock */
216 if (chip->request)
217 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800218 }
David Brownell35e8bb52008-10-15 22:03:16 -0700219 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800220}
221
Alexandre Courbotdef63432013-02-15 14:46:15 +0900222static struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900223{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900224 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900225}
226
Alexandre Courbot24d76282013-02-15 14:46:16 +0900227/* caller holds gpio_lock *OR* gpio is marked as requested */
Grant Likely1a2d3972011-12-12 09:25:57 -0700228struct gpio_chip *gpio_to_chip(unsigned gpio)
David Brownelld2876d02008-02-04 22:28:20 -0800229{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900230 return gpiod_to_chip(gpio_to_desc(gpio));
David Brownelld2876d02008-02-04 22:28:20 -0800231}
232
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700233/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
234static int gpiochip_find_base(int ngpio)
235{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900236 struct gpio_chip *chip;
237 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700238
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900239 list_for_each_entry_reverse(chip, &gpio_chips, list) {
240 /* found a free space? */
241 if (chip->base + chip->ngpio <= base)
242 break;
243 else
244 /* nope, check the space right before the chip */
245 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700246 }
247
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900248 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700249 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900250 return base;
251 } else {
252 pr_err("%s: cannot find free range\n", __func__);
253 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700254 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700255}
256
Mathias Nyman80b0a602012-10-24 17:25:27 +0300257/* caller ensures gpio is valid and requested, chip->get_direction may sleep */
Alexandre Courbotdef63432013-02-15 14:46:15 +0900258static int gpiod_get_direction(const struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300259{
260 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900261 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300262 int status = -EINVAL;
263
Alexandre Courbot372e7222013-02-03 01:29:29 +0900264 chip = gpiod_to_chip(desc);
265 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300266
267 if (!chip->get_direction)
268 return status;
269
Alexandre Courbot372e7222013-02-03 01:29:29 +0900270 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300271 if (status > 0) {
272 /* GPIOF_DIR_IN, or other positive */
273 status = 1;
Alexandre Courbotdef63432013-02-15 14:46:15 +0900274 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
275 * so it does not affect constness per se */
276 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300277 }
278 if (status == 0) {
279 /* GPIOF_DIR_OUT */
Alexandre Courbotdef63432013-02-15 14:46:15 +0900280 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300281 }
282 return status;
283}
284
David Brownelld8f388d2008-07-25 01:46:07 -0700285#ifdef CONFIG_GPIO_SYSFS
286
287/* lock protects against unexport_gpio() being called while
288 * sysfs files are active.
289 */
290static DEFINE_MUTEX(sysfs_lock);
291
292/*
293 * /sys/class/gpio/gpioN... only for GPIOs that are exported
294 * /direction
295 * * MAY BE OMITTED if kernel won't allow direction changes
296 * * is read/write as "in" or "out"
297 * * may also be written as "high" or "low", initializing
298 * output value as specified ("out" implies "low")
299 * /value
300 * * always readable, subject to hardware behavior
301 * * may be writable, as zero/nonzero
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700302 * /edge
303 * * configures behavior of poll(2) on /value
304 * * available only if pin can generate IRQs on input
305 * * is read/write as "none", "falling", "rising", or "both"
Jani Nikula07697462009-12-15 16:46:20 -0800306 * /active_low
307 * * configures polarity of /value
308 * * is read/write as zero/nonzero
309 * * also affects existing and subsequent "falling" and "rising"
310 * /edge configuration
David Brownelld8f388d2008-07-25 01:46:07 -0700311 */
312
313static ssize_t gpio_direction_show(struct device *dev,
314 struct device_attribute *attr, char *buf)
315{
Alexandre Courbotdef63432013-02-15 14:46:15 +0900316 const struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700317 ssize_t status;
318
319 mutex_lock(&sysfs_lock);
320
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900321 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700322 status = -EIO;
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900323 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +0900324 gpiod_get_direction(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700325 status = sprintf(buf, "%s\n",
326 test_bit(FLAG_IS_OUT, &desc->flags)
327 ? "out" : "in");
Alexandre Courbot476171ce2013-02-04 17:42:04 +0900328 }
David Brownelld8f388d2008-07-25 01:46:07 -0700329
330 mutex_unlock(&sysfs_lock);
331 return status;
332}
333
334static ssize_t gpio_direction_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 Brownelld8f388d2008-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 (sysfs_streq(buf, "high"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900345 status = gpiod_direction_output(desc, 1);
David Brownelld8f388d2008-07-25 01:46:07 -0700346 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900347 status = gpiod_direction_output(desc, 0);
David Brownelld8f388d2008-07-25 01:46:07 -0700348 else if (sysfs_streq(buf, "in"))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900349 status = gpiod_direction_input(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700350 else
351 status = -EINVAL;
352
353 mutex_unlock(&sysfs_lock);
354 return status ? : size;
355}
356
Jani Nikula07697462009-12-15 16:46:20 -0800357static /* const */ DEVICE_ATTR(direction, 0644,
David Brownelld8f388d2008-07-25 01:46:07 -0700358 gpio_direction_show, gpio_direction_store);
359
360static ssize_t gpio_value_show(struct device *dev,
361 struct device_attribute *attr, char *buf)
362{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900363 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700364 ssize_t status;
365
366 mutex_lock(&sysfs_lock);
367
Jani Nikula07697462009-12-15 16:46:20 -0800368 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700369 status = -EIO;
Jani Nikula07697462009-12-15 16:46:20 -0800370 } else {
371 int value;
372
Alexandre Courbot372e7222013-02-03 01:29:29 +0900373 value = !!gpiod_get_value_cansleep(desc);
Jani Nikula07697462009-12-15 16:46:20 -0800374 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
375 value = !value;
376
377 status = sprintf(buf, "%d\n", value);
378 }
David Brownelld8f388d2008-07-25 01:46:07 -0700379
380 mutex_unlock(&sysfs_lock);
381 return status;
382}
383
384static ssize_t gpio_value_store(struct device *dev,
385 struct device_attribute *attr, const char *buf, size_t size)
386{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900387 struct gpio_desc *desc = dev_get_drvdata(dev);
David Brownelld8f388d2008-07-25 01:46:07 -0700388 ssize_t status;
389
390 mutex_lock(&sysfs_lock);
391
392 if (!test_bit(FLAG_EXPORT, &desc->flags))
393 status = -EIO;
394 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
395 status = -EPERM;
396 else {
397 long value;
398
Jingoo Hana3d88c92013-07-19 16:12:50 +0900399 status = kstrtol(buf, 0, &value);
David Brownelld8f388d2008-07-25 01:46:07 -0700400 if (status == 0) {
Jani Nikula07697462009-12-15 16:46:20 -0800401 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
402 value = !value;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900403 gpiod_set_value_cansleep(desc, value != 0);
David Brownelld8f388d2008-07-25 01:46:07 -0700404 status = size;
405 }
406 }
407
408 mutex_unlock(&sysfs_lock);
409 return status;
410}
411
Jani Nikula07697462009-12-15 16:46:20 -0800412static const DEVICE_ATTR(value, 0644,
David Brownelld8f388d2008-07-25 01:46:07 -0700413 gpio_value_show, gpio_value_store);
414
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700415static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
416{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700417 struct sysfs_dirent *value_sd = priv;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700418
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700419 sysfs_notify_dirent(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700420 return IRQ_HANDLED;
421}
422
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700423static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
424 unsigned long gpio_flags)
425{
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700426 struct sysfs_dirent *value_sd;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700427 unsigned long irq_flags;
428 int ret, irq, id;
429
430 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
431 return 0;
432
Alexandre Courbot372e7222013-02-03 01:29:29 +0900433 irq = gpiod_to_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700434 if (irq < 0)
435 return -EIO;
436
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700437 id = desc->flags >> ID_SHIFT;
438 value_sd = idr_find(&dirent_idr, id);
439 if (value_sd)
440 free_irq(irq, value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700441
442 desc->flags &= ~GPIO_TRIGGER_MASK;
443
444 if (!gpio_flags) {
Linus Walleijd468bf92013-09-24 11:54:38 +0200445 gpiod_unlock_as_irq(desc);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700446 ret = 0;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700447 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700448 }
449
450 irq_flags = IRQF_SHARED;
451 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800452 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
453 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700454 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
Jani Nikula07697462009-12-15 16:46:20 -0800455 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
456 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700457
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700458 if (!value_sd) {
459 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
460 if (!value_sd) {
461 ret = -ENODEV;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700462 goto err_out;
463 }
464
Tejun Heo62f516b2013-02-27 17:04:06 -0800465 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
466 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700467 goto free_sd;
Tejun Heo62f516b2013-02-27 17:04:06 -0800468 id = ret;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700469
470 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700471 desc->flags |= (unsigned long)id << ID_SHIFT;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700472
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700473 if (desc->flags >> ID_SHIFT != id) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700474 ret = -ERANGE;
475 goto free_id;
476 }
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700477 }
478
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700479 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700480 "gpiolib", value_sd);
Daniel Gl?ckner364fadb32010-08-10 18:02:26 -0700481 if (ret < 0)
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700482 goto free_id;
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700483
Linus Walleijd468bf92013-09-24 11:54:38 +0200484 ret = gpiod_lock_as_irq(desc);
485 if (ret < 0) {
486 gpiod_warn(desc, "failed to flag the GPIO for IRQ\n");
487 goto free_id;
488 }
489
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700490 desc->flags |= gpio_flags;
491 return 0;
492
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700493free_id:
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700494 idr_remove(&dirent_idr, id);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700495 desc->flags &= GPIO_FLAGS_MASK;
Daniel Gl?ckner5ba18212010-08-10 18:02:25 -0700496free_sd:
497 if (value_sd)
498 sysfs_put(value_sd);
Daniel Glöcknerff77c352009-09-22 16:46:38 -0700499err_out:
500 return ret;
501}
502
503static const struct {
504 const char *name;
505 unsigned long flags;
506} trigger_types[] = {
507 { "none", 0 },
508 { "falling", BIT(FLAG_TRIG_FALL) },
509 { "rising", BIT(FLAG_TRIG_RISE) },
510 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
511};
512
513static ssize_t gpio_edge_show(struct device *dev,
514 struct device_attribute *attr, char *buf)
515{
516 const struct gpio_desc *desc = dev_get_drvdata(dev);
517 ssize_t status;
518
519 mutex_lock(&sysfs_lock);
520
521 if (!test_bit(FLAG_EXPORT, &desc->flags))
522 status = -EIO;
523 else {
524 int i;
525
526 status = 0;
527 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
528 if ((desc->flags & GPIO_TRIGGER_MASK)
529 == trigger_types[i].flags) {
530 status = sprintf(buf, "%s\n",
531 trigger_types[i].name);
532 break;
533 }
534 }
535
536 mutex_unlock(&sysfs_lock);
537 return status;
538}
539
540static ssize_t gpio_edge_store(struct device *dev,
541 struct device_attribute *attr, const char *buf, size_t size)
542{
543 struct gpio_desc *desc = dev_get_drvdata(dev);
544 ssize_t status;
545 int i;
546
547 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
548 if (sysfs_streq(trigger_types[i].name, buf))
549 goto found;
550 return -EINVAL;
551
552found:
553 mutex_lock(&sysfs_lock);
554
555 if (!test_bit(FLAG_EXPORT, &desc->flags))
556 status = -EIO;
557 else {
558 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
559 if (!status)
560 status = size;
561 }
562
563 mutex_unlock(&sysfs_lock);
564
565 return status;
566}
567
568static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
569
Jani Nikula07697462009-12-15 16:46:20 -0800570static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
571 int value)
572{
573 int status = 0;
574
575 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
576 return 0;
577
578 if (value)
579 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
580 else
581 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
582
583 /* reconfigure poll(2) support if enabled on one edge only */
584 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
585 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
586 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
587
588 gpio_setup_irq(desc, dev, 0);
589 status = gpio_setup_irq(desc, dev, trigger_flags);
590 }
591
592 return status;
593}
594
595static ssize_t gpio_active_low_show(struct device *dev,
596 struct device_attribute *attr, char *buf)
597{
598 const struct gpio_desc *desc = dev_get_drvdata(dev);
599 ssize_t status;
600
601 mutex_lock(&sysfs_lock);
602
603 if (!test_bit(FLAG_EXPORT, &desc->flags))
604 status = -EIO;
605 else
606 status = sprintf(buf, "%d\n",
607 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
608
609 mutex_unlock(&sysfs_lock);
610
611 return status;
612}
613
614static ssize_t gpio_active_low_store(struct device *dev,
615 struct device_attribute *attr, const char *buf, size_t size)
616{
617 struct gpio_desc *desc = dev_get_drvdata(dev);
618 ssize_t status;
619
620 mutex_lock(&sysfs_lock);
621
622 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
623 status = -EIO;
624 } else {
625 long value;
626
Jingoo Hana3d88c92013-07-19 16:12:50 +0900627 status = kstrtol(buf, 0, &value);
Jani Nikula07697462009-12-15 16:46:20 -0800628 if (status == 0)
629 status = sysfs_set_active_low(desc, dev, value != 0);
630 }
631
632 mutex_unlock(&sysfs_lock);
633
634 return status ? : size;
635}
636
637static const DEVICE_ATTR(active_low, 0644,
638 gpio_active_low_show, gpio_active_low_store);
639
David Brownelld8f388d2008-07-25 01:46:07 -0700640static const struct attribute *gpio_attrs[] = {
David Brownelld8f388d2008-07-25 01:46:07 -0700641 &dev_attr_value.attr,
Jani Nikula07697462009-12-15 16:46:20 -0800642 &dev_attr_active_low.attr,
David Brownelld8f388d2008-07-25 01:46:07 -0700643 NULL,
644};
645
646static const struct attribute_group gpio_attr_group = {
647 .attrs = (struct attribute **) gpio_attrs,
648};
649
650/*
651 * /sys/class/gpio/gpiochipN/
652 * /base ... matching gpio_chip.base (N)
653 * /label ... matching gpio_chip.label
654 * /ngpio ... matching gpio_chip.ngpio
655 */
656
657static ssize_t chip_base_show(struct device *dev,
658 struct device_attribute *attr, char *buf)
659{
660 const struct gpio_chip *chip = dev_get_drvdata(dev);
661
662 return sprintf(buf, "%d\n", chip->base);
663}
664static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
665
666static ssize_t chip_label_show(struct device *dev,
667 struct device_attribute *attr, char *buf)
668{
669 const struct gpio_chip *chip = dev_get_drvdata(dev);
670
671 return sprintf(buf, "%s\n", chip->label ? : "");
672}
673static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
674
675static ssize_t chip_ngpio_show(struct device *dev,
676 struct device_attribute *attr, char *buf)
677{
678 const struct gpio_chip *chip = dev_get_drvdata(dev);
679
680 return sprintf(buf, "%u\n", chip->ngpio);
681}
682static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
683
684static const struct attribute *gpiochip_attrs[] = {
685 &dev_attr_base.attr,
686 &dev_attr_label.attr,
687 &dev_attr_ngpio.attr,
688 NULL,
689};
690
691static const struct attribute_group gpiochip_attr_group = {
692 .attrs = (struct attribute **) gpiochip_attrs,
693};
694
695/*
696 * /sys/class/gpio/export ... write-only
697 * integer N ... number of GPIO to export (full access)
698 * /sys/class/gpio/unexport ... write-only
699 * integer N ... number of GPIO to unexport
700 */
Andi Kleen28812fe2010-01-05 12:48:07 +0100701static ssize_t export_store(struct class *class,
702 struct class_attribute *attr,
703 const char *buf, size_t len)
David Brownelld8f388d2008-07-25 01:46:07 -0700704{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900705 long gpio;
706 struct gpio_desc *desc;
707 int status;
David Brownelld8f388d2008-07-25 01:46:07 -0700708
Jingoo Hana3d88c92013-07-19 16:12:50 +0900709 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d2008-07-25 01:46:07 -0700710 if (status < 0)
711 goto done;
712
Alexandre Courbot372e7222013-02-03 01:29:29 +0900713 desc = gpio_to_desc(gpio);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900714 /* reject invalid GPIOs */
715 if (!desc) {
716 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
717 return -EINVAL;
718 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900719
David Brownelld8f388d2008-07-25 01:46:07 -0700720 /* No extra locking here; FLAG_SYSFS just signifies that the
721 * request and export were done by on behalf of userspace, so
722 * they may be undone on its behalf too.
723 */
724
Alexandre Courbot372e7222013-02-03 01:29:29 +0900725 status = gpiod_request(desc, "sysfs");
Mathias Nymanad2fab32012-10-25 14:03:03 +0300726 if (status < 0) {
727 if (status == -EPROBE_DEFER)
728 status = -ENODEV;
David Brownelld8f388d2008-07-25 01:46:07 -0700729 goto done;
Mathias Nymanad2fab32012-10-25 14:03:03 +0300730 }
Alexandre Courbot372e7222013-02-03 01:29:29 +0900731 status = gpiod_export(desc, true);
David Brownelld8f388d2008-07-25 01:46:07 -0700732 if (status < 0)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900733 gpiod_free(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700734 else
Alexandre Courbot372e7222013-02-03 01:29:29 +0900735 set_bit(FLAG_SYSFS, &desc->flags);
David Brownelld8f388d2008-07-25 01:46:07 -0700736
737done:
738 if (status)
739 pr_debug("%s: status %d\n", __func__, status);
740 return status ? : len;
741}
742
Andi Kleen28812fe2010-01-05 12:48:07 +0100743static ssize_t unexport_store(struct class *class,
744 struct class_attribute *attr,
745 const char *buf, size_t len)
David Brownelld8f388d2008-07-25 01:46:07 -0700746{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900747 long gpio;
748 struct gpio_desc *desc;
749 int status;
David Brownelld8f388d2008-07-25 01:46:07 -0700750
Jingoo Hana3d88c92013-07-19 16:12:50 +0900751 status = kstrtol(buf, 0, &gpio);
David Brownelld8f388d2008-07-25 01:46:07 -0700752 if (status < 0)
753 goto done;
754
Alexandre Courbot372e7222013-02-03 01:29:29 +0900755 desc = gpio_to_desc(gpio);
David Brownelld8f388d2008-07-25 01:46:07 -0700756 /* reject bogus commands (gpio_unexport ignores them) */
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900757 if (!desc) {
758 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
759 return -EINVAL;
760 }
761
762 status = -EINVAL;
David Brownelld8f388d2008-07-25 01:46:07 -0700763
764 /* No extra locking here; FLAG_SYSFS just signifies that the
765 * request and export were done by on behalf of userspace, so
766 * they may be undone on its behalf too.
767 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900768 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -0700769 status = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900770 gpiod_free(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700771 }
772done:
773 if (status)
774 pr_debug("%s: status %d\n", __func__, status);
775 return status ? : len;
776}
777
778static struct class_attribute gpio_class_attrs[] = {
779 __ATTR(export, 0200, NULL, export_store),
780 __ATTR(unexport, 0200, NULL, unexport_store),
781 __ATTR_NULL,
782};
783
784static struct class gpio_class = {
785 .name = "gpio",
786 .owner = THIS_MODULE,
787
788 .class_attrs = gpio_class_attrs,
789};
790
791
792/**
793 * gpio_export - export a GPIO through sysfs
794 * @gpio: gpio to make available, already requested
795 * @direction_may_change: true if userspace may change gpio direction
796 * Context: arch_initcall or later
797 *
798 * When drivers want to make a GPIO accessible to userspace after they
799 * have requested it -- perhaps while debugging, or as part of their
800 * public interface -- they may use this routine. If the GPIO can
801 * change direction (some can't) and the caller allows it, userspace
802 * will see "direction" sysfs attribute which may be used to change
803 * the gpio's direction. A "value" attribute will always be provided.
804 *
805 * Returns zero on success, else an error.
806 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900807static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
David Brownelld8f388d2008-07-25 01:46:07 -0700808{
809 unsigned long flags;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100810 int status;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700811 const char *ioname = NULL;
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100812 struct device *dev;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900813 int offset;
David Brownelld8f388d2008-07-25 01:46:07 -0700814
815 /* can't export until sysfs is available ... */
816 if (!gpio_class.p) {
817 pr_debug("%s: called too early!\n", __func__);
818 return -ENOENT;
819 }
820
Alexandre Courbot372e7222013-02-03 01:29:29 +0900821 if (!desc) {
822 pr_debug("%s: invalid gpio descriptor\n", __func__);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100823 return -EINVAL;
824 }
David Brownelld8f388d2008-07-25 01:46:07 -0700825
826 mutex_lock(&sysfs_lock);
827
828 spin_lock_irqsave(&gpio_lock, flags);
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100829 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
830 test_bit(FLAG_EXPORT, &desc->flags)) {
831 spin_unlock_irqrestore(&gpio_lock, flags);
832 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
Alexandre Courbot372e7222013-02-03 01:29:29 +0900833 __func__, desc_to_gpio(desc),
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100834 test_bit(FLAG_REQUESTED, &desc->flags),
835 test_bit(FLAG_EXPORT, &desc->flags));
Dan Carpenter529f2ad2012-10-26 09:59:43 +0300836 status = -EPERM;
837 goto fail_unlock;
David Brownelld8f388d2008-07-25 01:46:07 -0700838 }
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100839
840 if (!desc->chip->direction_input || !desc->chip->direction_output)
841 direction_may_change = false;
David Brownelld8f388d2008-07-25 01:46:07 -0700842 spin_unlock_irqrestore(&gpio_lock, flags);
843
Alexandre Courbot372e7222013-02-03 01:29:29 +0900844 offset = gpio_chip_hwgpio(desc);
845 if (desc->chip->names && desc->chip->names[offset])
846 ioname = desc->chip->names[offset];
Daniel Silverstone926b6632009-04-02 16:57:05 -0700847
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100848 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
Alexandre Courbot372e7222013-02-03 01:29:29 +0900849 desc, ioname ? ioname : "gpio%u",
850 desc_to_gpio(desc));
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100851 if (IS_ERR(dev)) {
852 status = PTR_ERR(dev);
853 goto fail_unlock;
David Brownelld8f388d2008-07-25 01:46:07 -0700854 }
855
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100856 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
David Brownelld8f388d2008-07-25 01:46:07 -0700857 if (status)
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100858 goto fail_unregister_device;
David Brownelld8f388d2008-07-25 01:46:07 -0700859
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100860 if (direction_may_change) {
861 status = device_create_file(dev, &dev_attr_direction);
862 if (status)
863 goto fail_unregister_device;
864 }
865
Alexandre Courbot372e7222013-02-03 01:29:29 +0900866 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
Ryan Mallonfc4e2512012-10-22 11:39:12 +1100867 !test_bit(FLAG_IS_OUT, &desc->flags))) {
868 status = device_create_file(dev, &dev_attr_edge);
869 if (status)
870 goto fail_unregister_device;
871 }
872
873 set_bit(FLAG_EXPORT, &desc->flags);
874 mutex_unlock(&sysfs_lock);
875 return 0;
876
877fail_unregister_device:
878 device_unregister(dev);
879fail_unlock:
880 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900881 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
882 status);
David Brownelld8f388d2008-07-25 01:46:07 -0700883 return status;
884}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900885
886int gpio_export(unsigned gpio, bool direction_may_change)
887{
888 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
889}
David Brownelld8f388d2008-07-25 01:46:07 -0700890EXPORT_SYMBOL_GPL(gpio_export);
891
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100892static int match_export(struct device *dev, const void *data)
David Brownelld8f388d2008-07-25 01:46:07 -0700893{
894 return dev_get_drvdata(dev) == data;
895}
896
897/**
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700898 * gpio_export_link - create a sysfs link to an exported GPIO node
899 * @dev: device under which to create symlink
900 * @name: name of the symlink
901 * @gpio: gpio to create symlink to, already exported
902 *
903 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
904 * node. Caller is responsible for unlinking.
905 *
906 * Returns zero on success, else an error.
907 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900908static int gpiod_export_link(struct device *dev, const char *name,
909 struct gpio_desc *desc)
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700910{
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700911 int status = -EINVAL;
912
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900913 if (!desc) {
914 pr_warn("%s: invalid GPIO\n", __func__);
915 return -EINVAL;
916 }
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700917
918 mutex_lock(&sysfs_lock);
919
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700920 if (test_bit(FLAG_EXPORT, &desc->flags)) {
921 struct device *tdev;
922
923 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
924 if (tdev != NULL) {
925 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
926 name);
927 } else {
928 status = -ENODEV;
929 }
930 }
931
932 mutex_unlock(&sysfs_lock);
933
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700934 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900935 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
936 status);
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700937
938 return status;
939}
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700940
Alexandre Courbot372e7222013-02-03 01:29:29 +0900941int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
942{
943 return gpiod_export_link(dev, name, gpio_to_desc(gpio));
944}
945EXPORT_SYMBOL_GPL(gpio_export_link);
Jani Nikula07697462009-12-15 16:46:20 -0800946
947/**
948 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
949 * @gpio: gpio to change
950 * @value: non-zero to use active low, i.e. inverted values
951 *
952 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
953 * The GPIO does not have to be exported yet. If poll(2) support has
954 * been enabled for either rising or falling edge, it will be
955 * reconfigured to follow the new polarity.
956 *
957 * Returns zero on success, else an error.
958 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900959static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
Jani Nikula07697462009-12-15 16:46:20 -0800960{
Jani Nikula07697462009-12-15 16:46:20 -0800961 struct device *dev = NULL;
962 int status = -EINVAL;
963
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900964 if (!desc) {
965 pr_warn("%s: invalid GPIO\n", __func__);
966 return -EINVAL;
967 }
Jani Nikula07697462009-12-15 16:46:20 -0800968
969 mutex_lock(&sysfs_lock);
970
Jani Nikula07697462009-12-15 16:46:20 -0800971 if (test_bit(FLAG_EXPORT, &desc->flags)) {
Jani Nikula07697462009-12-15 16:46:20 -0800972 dev = class_find_device(&gpio_class, NULL, desc, match_export);
973 if (dev == NULL) {
974 status = -ENODEV;
975 goto unlock;
976 }
977 }
978
979 status = sysfs_set_active_low(desc, dev, value);
980
981unlock:
982 mutex_unlock(&sysfs_lock);
983
Jani Nikula07697462009-12-15 16:46:20 -0800984 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900985 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
986 status);
Jani Nikula07697462009-12-15 16:46:20 -0800987
988 return status;
989}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900990
991int gpio_sysfs_set_active_low(unsigned gpio, int value)
992{
993 return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value);
994}
Jani Nikula07697462009-12-15 16:46:20 -0800995EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
996
Jani Nikulaa4177ee2009-09-22 16:46:33 -0700997/**
David Brownelld8f388d2008-07-25 01:46:07 -0700998 * gpio_unexport - reverse effect of gpio_export()
999 * @gpio: gpio to make unavailable
1000 *
1001 * This is implicit on gpio_free().
1002 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001003static void gpiod_unexport(struct gpio_desc *desc)
David Brownelld8f388d2008-07-25 01:46:07 -07001004{
Jon Povey6a99ad42010-07-27 13:18:06 -07001005 int status = 0;
Ming Lei864533c2012-02-13 22:53:20 +08001006 struct device *dev = NULL;
David Brownelld8f388d2008-07-25 01:46:07 -07001007
Alexandre Courbot372e7222013-02-03 01:29:29 +09001008 if (!desc) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001009 pr_warn("%s: invalid GPIO\n", __func__);
1010 return;
Jon Povey6a99ad42010-07-27 13:18:06 -07001011 }
David Brownelld8f388d2008-07-25 01:46:07 -07001012
1013 mutex_lock(&sysfs_lock);
1014
David Brownelld8f388d2008-07-25 01:46:07 -07001015 if (test_bit(FLAG_EXPORT, &desc->flags)) {
David Brownelld8f388d2008-07-25 01:46:07 -07001016
1017 dev = class_find_device(&gpio_class, NULL, desc, match_export);
1018 if (dev) {
Daniel Glöcknerff77c352009-09-22 16:46:38 -07001019 gpio_setup_irq(desc, dev, 0);
David Brownelld8f388d2008-07-25 01:46:07 -07001020 clear_bit(FLAG_EXPORT, &desc->flags);
David Brownelld8f388d2008-07-25 01:46:07 -07001021 } else
1022 status = -ENODEV;
1023 }
1024
1025 mutex_unlock(&sysfs_lock);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001026
Ming Lei864533c2012-02-13 22:53:20 +08001027 if (dev) {
1028 device_unregister(dev);
1029 put_device(dev);
1030 }
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001031
David Brownelld8f388d2008-07-25 01:46:07 -07001032 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001033 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
1034 status);
1035}
1036
1037void gpio_unexport(unsigned gpio)
1038{
1039 gpiod_unexport(gpio_to_desc(gpio));
David Brownelld8f388d2008-07-25 01:46:07 -07001040}
1041EXPORT_SYMBOL_GPL(gpio_unexport);
1042
1043static int gpiochip_export(struct gpio_chip *chip)
1044{
1045 int status;
1046 struct device *dev;
1047
1048 /* Many systems register gpio chips for SOC support very early,
1049 * before driver model support is available. In those cases we
1050 * export this later, in gpiolib_sysfs_init() ... here we just
1051 * verify that _some_ field of gpio_class got initialized.
1052 */
1053 if (!gpio_class.p)
1054 return 0;
1055
1056 /* use chip->base for the ID; it's already known to be unique */
1057 mutex_lock(&sysfs_lock);
1058 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
1059 "gpiochip%d", chip->base);
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001060 if (!IS_ERR(dev)) {
David Brownelld8f388d2008-07-25 01:46:07 -07001061 status = sysfs_create_group(&dev->kobj,
1062 &gpiochip_attr_group);
1063 } else
Sergei Shtylyovd62668e2009-11-11 14:26:50 -08001064 status = PTR_ERR(dev);
David Brownelld8f388d2008-07-25 01:46:07 -07001065 chip->exported = (status == 0);
1066 mutex_unlock(&sysfs_lock);
1067
1068 if (status) {
1069 unsigned long flags;
1070 unsigned gpio;
1071
1072 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001073 gpio = 0;
1074 while (gpio < chip->ngpio)
1075 chip->desc[gpio++].chip = NULL;
David Brownelld8f388d2008-07-25 01:46:07 -07001076 spin_unlock_irqrestore(&gpio_lock, flags);
1077
1078 pr_debug("%s: chip %s status %d\n", __func__,
1079 chip->label, status);
1080 }
1081
1082 return status;
1083}
1084
1085static void gpiochip_unexport(struct gpio_chip *chip)
1086{
1087 int status;
1088 struct device *dev;
1089
1090 mutex_lock(&sysfs_lock);
1091 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1092 if (dev) {
1093 put_device(dev);
1094 device_unregister(dev);
1095 chip->exported = 0;
1096 status = 0;
1097 } else
1098 status = -ENODEV;
1099 mutex_unlock(&sysfs_lock);
1100
1101 if (status)
1102 pr_debug("%s: chip %s status %d\n", __func__,
1103 chip->label, status);
1104}
1105
1106static int __init gpiolib_sysfs_init(void)
1107{
1108 int status;
1109 unsigned long flags;
Alexandre Courbot65493e32013-02-03 01:29:25 +09001110 struct gpio_chip *chip;
David Brownelld8f388d2008-07-25 01:46:07 -07001111
1112 status = class_register(&gpio_class);
1113 if (status < 0)
1114 return status;
1115
1116 /* Scan and register the gpio_chips which registered very
1117 * early (e.g. before the class_register above was called).
1118 *
1119 * We run before arch_initcall() so chip->dev nodes can have
1120 * registered, and so arch_initcall() can always gpio_export().
1121 */
1122 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot65493e32013-02-03 01:29:25 +09001123 list_for_each_entry(chip, &gpio_chips, list) {
David Brownelld8f388d2008-07-25 01:46:07 -07001124 if (!chip || chip->exported)
1125 continue;
1126
1127 spin_unlock_irqrestore(&gpio_lock, flags);
1128 status = gpiochip_export(chip);
1129 spin_lock_irqsave(&gpio_lock, flags);
1130 }
1131 spin_unlock_irqrestore(&gpio_lock, flags);
1132
1133
1134 return status;
1135}
1136postcore_initcall(gpiolib_sysfs_init);
1137
1138#else
1139static inline int gpiochip_export(struct gpio_chip *chip)
1140{
1141 return 0;
1142}
1143
1144static inline void gpiochip_unexport(struct gpio_chip *chip)
1145{
1146}
1147
Alexandre Courbot372e7222013-02-03 01:29:29 +09001148static inline int gpiod_export(struct gpio_desc *desc,
1149 bool direction_may_change)
1150{
1151 return -ENOSYS;
1152}
1153
1154static inline int gpiod_export_link(struct device *dev, const char *name,
1155 struct gpio_desc *desc)
1156{
1157 return -ENOSYS;
1158}
1159
1160static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
1161{
1162 return -ENOSYS;
1163}
1164
1165static inline void gpiod_unexport(struct gpio_desc *desc)
1166{
1167}
1168
David Brownelld8f388d2008-07-25 01:46:07 -07001169#endif /* CONFIG_GPIO_SYSFS */
1170
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001171/*
1172 * Add a new chip to the global chips list, keeping the list of chips sorted
1173 * by base order.
1174 *
1175 * Return -EBUSY if the new chip overlaps with some other chip's integer
1176 * space.
1177 */
1178static int gpiochip_add_to_list(struct gpio_chip *chip)
1179{
1180 struct list_head *pos = &gpio_chips;
1181 struct gpio_chip *_chip;
1182 int err = 0;
1183
1184 /* find where to insert our chip */
1185 list_for_each(pos, &gpio_chips) {
1186 _chip = list_entry(pos, struct gpio_chip, list);
1187 /* shall we insert before _chip? */
1188 if (_chip->base >= chip->base + chip->ngpio)
1189 break;
1190 }
1191
1192 /* are we stepping on the chip right before? */
1193 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1194 _chip = list_entry(pos->prev, struct gpio_chip, list);
1195 if (_chip->base + _chip->ngpio > chip->base) {
1196 dev_err(chip->dev,
1197 "GPIO integer space overlap, cannot add chip\n");
1198 err = -EBUSY;
1199 }
1200 }
1201
1202 if (!err)
1203 list_add_tail(&chip->list, pos);
1204
1205 return err;
1206}
1207
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001208/**
David Brownelld2876d02008-02-04 22:28:20 -08001209 * gpiochip_add() - register a gpio_chip
1210 * @chip: the chip to register, with chip->base initialized
1211 * Context: potentially before irqs or kmalloc will work
1212 *
1213 * Returns a negative errno if the chip can't be registered, such as
1214 * because the chip->base is invalid or already associated with a
1215 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001216 *
David Brownelld8f388d2008-07-25 01:46:07 -07001217 * When gpiochip_add() is called very early during boot, so that GPIOs
1218 * can be freely used, the chip->dev device must be registered before
1219 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1220 * for GPIOs will fail rudely.
1221 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001222 * If chip->base is negative, this requests dynamic assignment of
1223 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001224 */
1225int gpiochip_add(struct gpio_chip *chip)
1226{
1227 unsigned long flags;
1228 int status = 0;
1229 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001230 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -08001231
Trent Piephobff5fda2008-05-23 13:04:44 -07001232 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001233 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -08001234 status = -EINVAL;
1235 goto fail;
1236 }
1237
1238 spin_lock_irqsave(&gpio_lock, flags);
1239
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001240 if (base < 0) {
1241 base = gpiochip_find_base(chip->ngpio);
1242 if (base < 0) {
1243 status = base;
David Brownelld8f388d2008-07-25 01:46:07 -07001244 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001245 }
1246 chip->base = base;
1247 }
1248
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001249 status = gpiochip_add_to_list(chip);
1250
David Brownelld2876d02008-02-04 22:28:20 -08001251 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001252 chip->desc = &gpio_desc[chip->base];
1253
1254 for (id = 0; id < chip->ngpio; id++) {
1255 struct gpio_desc *desc = &chip->desc[id];
1256 desc->chip = chip;
David Brownelld8f388d2008-07-25 01:46:07 -07001257
1258 /* REVISIT: most hardware initializes GPIOs as
1259 * inputs (often with pullups enabled) so power
1260 * usage is minimized. Linux code should set the
1261 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +03001262 * and in case chip->get_direction is not set,
David Brownelld8f388d2008-07-25 01:46:07 -07001263 * we may expose the wrong direction in sysfs.
1264 */
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001265 desc->flags = !chip->direction_input
David Brownelld8f388d2008-07-25 01:46:07 -07001266 ? (1 << FLAG_IS_OUT)
1267 : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001268 }
1269 }
1270
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001271 spin_unlock_irqrestore(&gpio_lock, flags);
1272
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301273#ifdef CONFIG_PINCTRL
1274 INIT_LIST_HEAD(&chip->pin_ranges);
1275#endif
1276
Anton Vorontsov391c9702010-06-08 07:48:17 -06001277 of_gpiochip_add(chip);
1278
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001279 if (status)
1280 goto fail;
1281
1282 status = gpiochip_export(chip);
1283 if (status)
1284 goto fail;
1285
H Hartley Sweetenee1c1e72012-05-11 16:58:33 -07001286 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
Grant Likely64842aa2011-11-06 11:36:18 -07001287 chip->base, chip->base + chip->ngpio - 1,
1288 chip->label ? : "generic");
1289
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001290 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001291
1292unlock:
1293 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld2876d02008-02-04 22:28:20 -08001294fail:
1295 /* failures here can mean systems won't boot... */
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001296 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1297 chip->base, chip->base + chip->ngpio - 1,
1298 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -08001299 return status;
1300}
1301EXPORT_SYMBOL_GPL(gpiochip_add);
1302
1303/**
1304 * gpiochip_remove() - unregister a gpio_chip
1305 * @chip: the chip to unregister
1306 *
1307 * A gpio_chip with any GPIOs still requested may not be removed.
1308 */
1309int gpiochip_remove(struct gpio_chip *chip)
1310{
1311 unsigned long flags;
1312 int status = 0;
1313 unsigned id;
1314
1315 spin_lock_irqsave(&gpio_lock, flags);
1316
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001317 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001318 of_gpiochip_remove(chip);
1319
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001320 for (id = 0; id < chip->ngpio; id++) {
1321 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
David Brownelld2876d02008-02-04 22:28:20 -08001322 status = -EBUSY;
1323 break;
1324 }
1325 }
1326 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001327 for (id = 0; id < chip->ngpio; id++)
1328 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001329
1330 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -08001331 }
1332
1333 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d2008-07-25 01:46:07 -07001334
1335 if (status == 0)
1336 gpiochip_unexport(chip);
1337
David Brownelld2876d02008-02-04 22:28:20 -08001338 return status;
1339}
1340EXPORT_SYMBOL_GPL(gpiochip_remove);
1341
Grant Likely594fa262010-06-08 07:48:16 -06001342/**
1343 * gpiochip_find() - iterator for locating a specific gpio_chip
1344 * @data: data to pass to match function
1345 * @callback: Callback function to check gpio_chip
1346 *
1347 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1348 * determined by a user supplied @match callback. The callback should return
1349 * 0 if the device doesn't match and non-zero if it does. If the callback is
1350 * non-zero, this function will return to the caller and not iterate over any
1351 * more gpio_chips.
1352 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001353struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001354 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001355 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001356{
Alexandre Courbot125eef92013-02-03 01:29:26 +09001357 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001358 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001359
1360 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +09001361 list_for_each_entry(chip, &gpio_chips, list)
1362 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001363 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001364
1365 /* No match? */
1366 if (&chip->list == &gpio_chips)
1367 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001368 spin_unlock_irqrestore(&gpio_lock, flags);
1369
1370 return chip;
1371}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001372EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001373
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301374#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001375
Linus Walleij3f0f8672012-11-20 12:40:15 +01001376/**
1377 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1378 * @chip: the gpiochip to add the range for
1379 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001380 * @gpio_offset: the start offset in the current gpio_chip number space
1381 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001382 * @npins: the number of pins from the offset of each pin space (GPIO and
1383 * pin controller) to accumulate in this range
1384 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001385int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001386 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001387 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301388{
1389 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001390 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301391
Linus Walleij3f0f8672012-11-20 12:40:15 +01001392 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301393 if (!pin_range) {
1394 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1395 chip->label);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001396 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301397 }
1398
Linus Walleij3f0f8672012-11-20 12:40:15 +01001399 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001400 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001401 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301402 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001403 pin_range->range.base = chip->base + gpio_offset;
1404 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301405 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001406 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301407 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001408 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001409 ret = PTR_ERR(pin_range->pctldev);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001410 pr_err("%s: GPIO chip: could not create pin range\n",
1411 chip->label);
1412 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001413 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001414 }
Linus Walleij316511c2012-11-21 08:48:09 +01001415 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1416 chip->label, gpio_offset, gpio_offset + npins - 1,
1417 pinctl_name,
1418 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301419
1420 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001421
1422 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301423}
Linus Walleij165adc92012-11-06 14:49:39 +01001424EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301425
Linus Walleij3f0f8672012-11-20 12:40:15 +01001426/**
1427 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1428 * @chip: the chip to remove all the mappings for
1429 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301430void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1431{
1432 struct gpio_pin_range *pin_range, *tmp;
1433
1434 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1435 list_del(&pin_range->node);
1436 pinctrl_remove_gpio_range(pin_range->pctldev,
1437 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001438 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301439 }
1440}
Linus Walleij165adc92012-11-06 14:49:39 +01001441EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1442
1443#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301444
David Brownelld2876d02008-02-04 22:28:20 -08001445/* These "optional" allocation calls help prevent drivers from stomping
1446 * on each other, and help provide better diagnostics in debugfs.
1447 * They're called even less than the "set direction" calls.
1448 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001449static int gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001450{
David Brownell35e8bb52008-10-15 22:03:16 -07001451 struct gpio_chip *chip;
Mark Browne9354572012-07-09 12:22:56 +01001452 int status = -EPROBE_DEFER;
David Brownelld2876d02008-02-04 22:28:20 -08001453 unsigned long flags;
1454
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001455 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001456 pr_warn("%s: invalid GPIO\n", __func__);
1457 return -EINVAL;
1458 }
1459
David Brownelld2876d02008-02-04 22:28:20 -08001460 spin_lock_irqsave(&gpio_lock, flags);
1461
David Brownell35e8bb52008-10-15 22:03:16 -07001462 chip = desc->chip;
David Brownelld2876d02008-02-04 22:28:20 -08001463
David Brownell35e8bb52008-10-15 22:03:16 -07001464 if (!try_module_get(chip->owner))
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001465 goto done;
1466
David Brownelld2876d02008-02-04 22:28:20 -08001467 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001468 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001469 */
1470
1471 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1472 desc_set_label(desc, label ? : "?");
1473 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001474 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001475 status = -EBUSY;
David Brownell35e8bb52008-10-15 22:03:16 -07001476 module_put(chip->owner);
Magnus Damm7460db52009-01-29 14:25:12 -08001477 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001478 }
1479
1480 if (chip->request) {
1481 /* chip->request may sleep */
1482 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001483 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001484 spin_lock_irqsave(&gpio_lock, flags);
1485
1486 if (status < 0) {
1487 desc_set_label(desc, NULL);
1488 module_put(chip->owner);
1489 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001490 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001491 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001492 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001493 if (chip->get_direction) {
1494 /* chip->get_direction may sleep */
1495 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001496 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001497 spin_lock_irqsave(&gpio_lock, flags);
1498 }
David Brownelld2876d02008-02-04 22:28:20 -08001499done:
1500 if (status)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001501 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001502 desc_to_gpio(desc), label ? : "?", status);
David Brownelld2876d02008-02-04 22:28:20 -08001503 spin_unlock_irqrestore(&gpio_lock, flags);
1504 return status;
1505}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001506
1507int gpio_request(unsigned gpio, const char *label)
1508{
1509 return gpiod_request(gpio_to_desc(gpio), label);
1510}
David Brownelld2876d02008-02-04 22:28:20 -08001511EXPORT_SYMBOL_GPL(gpio_request);
1512
Alexandre Courbot372e7222013-02-03 01:29:29 +09001513static void gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001514{
1515 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001516 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001517
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001518 might_sleep();
1519
Alexandre Courbot372e7222013-02-03 01:29:29 +09001520 if (!desc) {
David Brownelld2876d02008-02-04 22:28:20 -08001521 WARN_ON(extra_checks);
1522 return;
1523 }
1524
Alexandre Courbot372e7222013-02-03 01:29:29 +09001525 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001526
David Brownelld2876d02008-02-04 22:28:20 -08001527 spin_lock_irqsave(&gpio_lock, flags);
1528
David Brownell35e8bb52008-10-15 22:03:16 -07001529 chip = desc->chip;
1530 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1531 if (chip->free) {
1532 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001533 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001534 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001535 spin_lock_irqsave(&gpio_lock, flags);
1536 }
David Brownelld2876d02008-02-04 22:28:20 -08001537 desc_set_label(desc, NULL);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001538 module_put(desc->chip->owner);
Jani Nikula07697462009-12-15 16:46:20 -08001539 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001540 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301541 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301542 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001543 } else
David Brownelld2876d02008-02-04 22:28:20 -08001544 WARN_ON(extra_checks);
1545
1546 spin_unlock_irqrestore(&gpio_lock, flags);
1547}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001548
1549void gpio_free(unsigned gpio)
1550{
1551 gpiod_free(gpio_to_desc(gpio));
1552}
David Brownelld2876d02008-02-04 22:28:20 -08001553EXPORT_SYMBOL_GPL(gpio_free);
1554
Eric Miao3e45f1d2010-03-05 13:44:35 -08001555/**
1556 * gpio_request_one - request a single GPIO with initial configuration
1557 * @gpio: the GPIO number
1558 * @flags: GPIO configuration as specified by GPIOF_*
1559 * @label: a literal description string of this GPIO
1560 */
1561int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1562{
Alexandre Courbot372e7222013-02-03 01:29:29 +09001563 struct gpio_desc *desc;
Eric Miao3e45f1d2010-03-05 13:44:35 -08001564 int err;
1565
Alexandre Courbot372e7222013-02-03 01:29:29 +09001566 desc = gpio_to_desc(gpio);
1567
1568 err = gpiod_request(desc, label);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001569 if (err)
1570 return err;
1571
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301572 if (flags & GPIOF_OPEN_DRAIN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001573 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301574
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301575 if (flags & GPIOF_OPEN_SOURCE)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001576 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301577
Eric Miao3e45f1d2010-03-05 13:44:35 -08001578 if (flags & GPIOF_DIR_IN)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001579 err = gpiod_direction_input(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001580 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001581 err = gpiod_direction_output(desc,
Eric Miao3e45f1d2010-03-05 13:44:35 -08001582 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1583
Aaro Koskinene2548112010-12-21 17:24:22 -08001584 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001585 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -08001586
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001587 if (flags & GPIOF_EXPORT) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001588 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
Wolfram Sangfc3a1f02011-12-13 18:34:01 +01001589 if (err)
1590 goto free_gpio;
1591 }
1592
1593 return 0;
1594
1595 free_gpio:
Alexandre Courbot372e7222013-02-03 01:29:29 +09001596 gpiod_free(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -08001597 return err;
1598}
1599EXPORT_SYMBOL_GPL(gpio_request_one);
1600
1601/**
1602 * gpio_request_array - request multiple GPIOs in a single call
1603 * @array: array of the 'struct gpio'
1604 * @num: how many GPIOs in the array
1605 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001606int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001607{
1608 int i, err;
1609
1610 for (i = 0; i < num; i++, array++) {
1611 err = gpio_request_one(array->gpio, array->flags, array->label);
1612 if (err)
1613 goto err_free;
1614 }
1615 return 0;
1616
1617err_free:
1618 while (i--)
1619 gpio_free((--array)->gpio);
1620 return err;
1621}
1622EXPORT_SYMBOL_GPL(gpio_request_array);
1623
1624/**
1625 * gpio_free_array - release multiple GPIOs in a single call
1626 * @array: array of the 'struct gpio'
1627 * @num: how many GPIOs in the array
1628 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -07001629void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -08001630{
1631 while (num--)
1632 gpio_free((array++)->gpio);
1633}
1634EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -08001635
1636/**
1637 * gpiochip_is_requested - return string iff signal was requested
1638 * @chip: controller managing the signal
1639 * @offset: of signal within controller's 0..(ngpio - 1) range
1640 *
1641 * Returns NULL if the GPIO is not currently requested, else a string.
1642 * If debugfs support is enabled, the string returned is the label passed
1643 * to gpio_request(); otherwise it is a meaningless constant.
1644 *
1645 * This function is for use by GPIO controller drivers. The label can
1646 * help with diagnostics, and knowing that the signal is used as a GPIO
1647 * can help avoid accidentally multiplexing it to another controller.
1648 */
1649const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1650{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001651 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001652
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001653 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -08001654 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001655
1656 desc = &chip->desc[offset];
1657
Alexandre Courbot372e7222013-02-03 01:29:29 +09001658 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001659 return NULL;
1660#ifdef CONFIG_DEBUG_FS
Alexandre Courbot372e7222013-02-03 01:29:29 +09001661 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001662#else
1663 return "?";
1664#endif
1665}
1666EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1667
1668
1669/* Drivers MUST set GPIO direction before making get/set calls. In
1670 * some cases this is done in early boot, before IRQs are enabled.
1671 *
1672 * As a rule these aren't called more than once (except for drivers
1673 * using the open-drain emulation idiom) so these are natural places
1674 * to accumulate extra debugging checks. Note that we can't (yet)
1675 * rely on gpio_request() having been called beforehand.
1676 */
1677
Alexandre Courbot372e7222013-02-03 01:29:29 +09001678static int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001679{
1680 unsigned long flags;
1681 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001682 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001683 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001684
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001685 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001686 pr_warn("%s: invalid GPIO\n", __func__);
1687 return -EINVAL;
1688 }
1689
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001690 chip = desc->chip;
1691 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001692 gpiod_warn(desc,
1693 "%s: missing get() or direction_input() operations\n",
1694 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001695 return -EIO;
1696 }
1697
David Brownelld2876d02008-02-04 22:28:20 -08001698 spin_lock_irqsave(&gpio_lock, flags);
1699
Alexandre Courbot372e7222013-02-03 01:29:29 +09001700 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001701 if (status < 0)
1702 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001703
1704 /* now we know the gpio is valid and chip won't vanish */
1705
1706 spin_unlock_irqrestore(&gpio_lock, flags);
1707
David Brownell9c4ba942010-08-10 18:02:24 -07001708 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001709
Alexandre Courbot372e7222013-02-03 01:29:29 +09001710 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001711 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001712 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001713 if (status < 0) {
Mark Brown6424de52013-09-09 10:33:49 +01001714 gpiod_dbg(desc, "chip request fail, %d\n", status);
David Brownell35e8bb52008-10-15 22:03:16 -07001715 /* and it's not available to anyone else ...
1716 * gpio_request() is the fully clean solution.
1717 */
1718 goto lose;
1719 }
1720 }
1721
Alexandre Courbot372e7222013-02-03 01:29:29 +09001722 status = chip->direction_input(chip, offset);
David Brownelld2876d02008-02-04 22:28:20 -08001723 if (status == 0)
1724 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001725
Alexandre Courbot372e7222013-02-03 01:29:29 +09001726 trace_gpio_direction(desc_to_gpio(desc), 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001727lose:
David Brownelld2876d02008-02-04 22:28:20 -08001728 return status;
1729fail:
1730 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001731 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001732 gpiod_dbg(desc, "%s status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001733 return status;
1734}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001735
1736int gpio_direction_input(unsigned gpio)
1737{
1738 return gpiod_direction_input(gpio_to_desc(gpio));
1739}
David Brownelld2876d02008-02-04 22:28:20 -08001740EXPORT_SYMBOL_GPL(gpio_direction_input);
1741
Alexandre Courbot372e7222013-02-03 01:29:29 +09001742static int gpiod_direction_output(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001743{
1744 unsigned long flags;
1745 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001746 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001747 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001748
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001749 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001750 pr_warn("%s: invalid GPIO\n", __func__);
1751 return -EINVAL;
1752 }
1753
Linus Walleijd468bf92013-09-24 11:54:38 +02001754 /* GPIOs used for IRQs shall not be set as output */
1755 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1756 gpiod_err(desc,
1757 "%s: tried to set a GPIO tied to an IRQ as output\n",
1758 __func__);
1759 return -EIO;
1760 }
1761
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301762 /* Open drain pin should not be driven to 1 */
1763 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001764 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301765
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301766 /* Open source pin should not be driven to 0 */
1767 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001768 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301769
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001770 chip = desc->chip;
1771 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001772 gpiod_warn(desc,
1773 "%s: missing set() or direction_output() operations\n",
1774 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001775 return -EIO;
1776 }
1777
David Brownelld2876d02008-02-04 22:28:20 -08001778 spin_lock_irqsave(&gpio_lock, flags);
1779
Alexandre Courbot372e7222013-02-03 01:29:29 +09001780 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001781 if (status < 0)
1782 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001783
1784 /* now we know the gpio is valid and chip won't vanish */
1785
1786 spin_unlock_irqrestore(&gpio_lock, flags);
1787
David Brownell9c4ba942010-08-10 18:02:24 -07001788 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001789
Alexandre Courbot372e7222013-02-03 01:29:29 +09001790 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001791 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001792 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001793 if (status < 0) {
Mark Brown6424de52013-09-09 10:33:49 +01001794 gpiod_dbg(desc, "chip request fail, %d\n", status);
David Brownell35e8bb52008-10-15 22:03:16 -07001795 /* and it's not available to anyone else ...
1796 * gpio_request() is the fully clean solution.
1797 */
1798 goto lose;
1799 }
1800 }
1801
Alexandre Courbot372e7222013-02-03 01:29:29 +09001802 status = chip->direction_output(chip, offset, value);
David Brownelld2876d02008-02-04 22:28:20 -08001803 if (status == 0)
1804 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001805 trace_gpio_value(desc_to_gpio(desc), 0, value);
1806 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001807lose:
David Brownelld2876d02008-02-04 22:28:20 -08001808 return status;
1809fail:
1810 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001811 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001812 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001813 return status;
1814}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001815
1816int gpio_direction_output(unsigned gpio, int value)
1817{
1818 return gpiod_direction_output(gpio_to_desc(gpio), value);
1819}
David Brownelld2876d02008-02-04 22:28:20 -08001820EXPORT_SYMBOL_GPL(gpio_direction_output);
1821
Felipe Balbic4b5be92010-05-26 14:42:23 -07001822/**
1823 * gpio_set_debounce - sets @debounce time for a @gpio
1824 * @gpio: the gpio to set debounce time
1825 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001826 *
1827 * returns -ENOTSUPP if the controller does not support setting
1828 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001829 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001830static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001831{
1832 unsigned long flags;
1833 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001834 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001835 int offset;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001836
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001837 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001838 pr_warn("%s: invalid GPIO\n", __func__);
1839 return -EINVAL;
1840 }
1841
Felipe Balbic4b5be92010-05-26 14:42:23 -07001842 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001843 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001844 gpiod_dbg(desc,
1845 "%s: missing set() or set_debounce() operations\n",
1846 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001847 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001848 }
1849
1850 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001851
1852 status = gpio_ensure_requested(desc);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001853 if (status < 0)
1854 goto fail;
1855
1856 /* now we know the gpio is valid and chip won't vanish */
1857
1858 spin_unlock_irqrestore(&gpio_lock, flags);
1859
David Brownell9c4ba942010-08-10 18:02:24 -07001860 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001861
Alexandre Courbot372e7222013-02-03 01:29:29 +09001862 offset = gpio_chip_hwgpio(desc);
1863 return chip->set_debounce(chip, offset, debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001864
1865fail:
1866 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001867 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001868 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001869
1870 return status;
1871}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001872
1873int gpio_set_debounce(unsigned gpio, unsigned debounce)
1874{
1875 return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
1876}
Felipe Balbic4b5be92010-05-26 14:42:23 -07001877EXPORT_SYMBOL_GPL(gpio_set_debounce);
David Brownelld2876d02008-02-04 22:28:20 -08001878
1879/* I/O calls are only valid after configuration completed; the relevant
1880 * "is this a valid GPIO" error checks should already have been done.
1881 *
1882 * "Get" operations are often inlinable as reading a pin value register,
1883 * and masking the relevant bit in that register.
1884 *
1885 * When "set" operations are inlinable, they involve writing that mask to
1886 * one register to set a low value, or a different register to set it high.
1887 * Otherwise locking is needed, so there may be little value to inlining.
1888 *
1889 *------------------------------------------------------------------------
1890 *
1891 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1892 * have requested the GPIO. That can include implicit requesting by
1893 * a direction setting call. Marking a gpio as requested locks its chip
1894 * in memory, guaranteeing that these table lookups need no more locking
1895 * and that gpiochip_remove() will fail.
1896 *
1897 * REVISIT when debugging, consider adding some instrumentation to ensure
1898 * that the GPIO was actually requested.
1899 */
1900
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001901static int _gpiod_get_value(const struct gpio_desc *desc)
1902{
1903 struct gpio_chip *chip;
1904 int value;
1905 int offset;
1906
1907 chip = desc->chip;
1908 offset = gpio_chip_hwgpio(desc);
1909 value = chip->get ? chip->get(chip, offset) : 0;
1910 trace_gpio_value(desc_to_gpio(desc), 1, value);
1911 return value;
1912}
1913
David Brownelld2876d02008-02-04 22:28:20 -08001914/**
1915 * __gpio_get_value() - return a gpio's value
1916 * @gpio: gpio whose value will be returned
1917 * Context: any
1918 *
1919 * This is used directly or indirectly to implement gpio_get_value().
1920 * It returns the zero or nonzero value provided by the associated
1921 * gpio_chip.get() method; or zero if no such method is provided.
1922 */
Alexandre Courbotdef63432013-02-15 14:46:15 +09001923static int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001924{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001925 if (!desc)
1926 return 0;
Mark Browne4e449e2012-02-17 10:46:00 -08001927 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001928 WARN_ON(desc->chip->can_sleep);
1929 return _gpiod_get_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001930}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001931
1932int __gpio_get_value(unsigned gpio)
1933{
1934 return gpiod_get_value(gpio_to_desc(gpio));
1935}
David Brownelld2876d02008-02-04 22:28:20 -08001936EXPORT_SYMBOL_GPL(__gpio_get_value);
1937
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301938/*
1939 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1940 * @gpio: Gpio whose state need to be set.
1941 * @chip: Gpio chip.
1942 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1943 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001944static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301945{
1946 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001947 struct gpio_chip *chip = desc->chip;
1948 int offset = gpio_chip_hwgpio(desc);
1949
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301950 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001951 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301952 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001953 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301954 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001955 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301956 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001957 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301958 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001959 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301960 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001961 gpiod_err(desc,
1962 "%s: Error in set_value for open drain err %d\n",
1963 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301964}
1965
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301966/*
1967 * _gpio_set_open_source() - Set the open source gpio's value.
1968 * @gpio: Gpio whose state need to be set.
1969 * @chip: Gpio chip.
1970 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1971 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09001972static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301973{
1974 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001975 struct gpio_chip *chip = desc->chip;
1976 int offset = gpio_chip_hwgpio(desc);
1977
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301978 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001979 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301980 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001981 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301982 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001983 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301984 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001985 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301986 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001987 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301988 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001989 gpiod_err(desc,
1990 "%s: Error in set_value for open source err %d\n",
1991 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301992}
1993
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001994static void _gpiod_set_value(struct gpio_desc *desc, int value)
1995{
1996 struct gpio_chip *chip;
1997
1998 chip = desc->chip;
1999 trace_gpio_value(desc_to_gpio(desc), 0, value);
2000 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2001 _gpio_set_open_drain_value(desc, value);
2002 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2003 _gpio_set_open_source_value(desc, value);
2004 else
2005 chip->set(chip, gpio_chip_hwgpio(desc), value);
2006}
2007
David Brownelld2876d02008-02-04 22:28:20 -08002008/**
2009 * __gpio_set_value() - assign a gpio's value
2010 * @gpio: gpio whose value will be assigned
2011 * @value: value to assign
2012 * Context: any
2013 *
2014 * This is used directly or indirectly to implement gpio_set_value().
2015 * It invokes the associated gpio_chip.set() method.
2016 */
Alexandre Courbot372e7222013-02-03 01:29:29 +09002017static void gpiod_set_value(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002018{
David Brownelld2876d02008-02-04 22:28:20 -08002019
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002020 if (!desc)
2021 return;
Mark Browne4e449e2012-02-17 10:46:00 -08002022 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09002023 WARN_ON(desc->chip->can_sleep);
2024 _gpiod_set_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002025}
2026
2027void __gpio_set_value(unsigned gpio, int value)
2028{
2029 return gpiod_set_value(gpio_to_desc(gpio), value);
David Brownelld2876d02008-02-04 22:28:20 -08002030}
2031EXPORT_SYMBOL_GPL(__gpio_set_value);
2032
2033/**
2034 * __gpio_cansleep() - report whether gpio value access will sleep
2035 * @gpio: gpio in question
2036 * Context: any
2037 *
2038 * This is used directly or indirectly to implement gpio_cansleep(). It
2039 * returns nonzero if access reading or writing the GPIO value can sleep.
2040 */
Alexandre Courbotdef63432013-02-15 14:46:15 +09002041static int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002042{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002043 if (!desc)
2044 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002045 /* only call this on GPIOs that are valid! */
2046 return desc->chip->can_sleep;
2047}
2048
David Brownelld2876d02008-02-04 22:28:20 -08002049int __gpio_cansleep(unsigned gpio)
2050{
Alexandre Courbot372e7222013-02-03 01:29:29 +09002051 return gpiod_cansleep(gpio_to_desc(gpio));
David Brownelld2876d02008-02-04 22:28:20 -08002052}
2053EXPORT_SYMBOL_GPL(__gpio_cansleep);
2054
David Brownell0f6d5042008-10-15 22:03:14 -07002055/**
2056 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
2057 * @gpio: gpio whose IRQ will be returned (already requested)
2058 * Context: any
2059 *
2060 * This is used directly or indirectly to implement gpio_to_irq().
2061 * It returns the number of the IRQ signaled by this (input) GPIO,
2062 * or a negative errno.
2063 */
Alexandre Courbotdef63432013-02-15 14:46:15 +09002064static int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002065{
2066 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002067 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002068
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002069 if (!desc)
2070 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002071 chip = desc->chip;
2072 offset = gpio_chip_hwgpio(desc);
2073 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
2074}
2075
2076int __gpio_to_irq(unsigned gpio)
2077{
2078 return gpiod_to_irq(gpio_to_desc(gpio));
David Brownell0f6d5042008-10-15 22:03:14 -07002079}
2080EXPORT_SYMBOL_GPL(__gpio_to_irq);
2081
Linus Walleijd468bf92013-09-24 11:54:38 +02002082/**
2083 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
2084 * @gpio: the GPIO line to lock as used for IRQ
2085 *
2086 * This is used directly by GPIO drivers that want to lock down
2087 * a certain GPIO line to be used as IRQs, for example in the
2088 * .to_irq() callback of their gpio_chip, or in the .irq_enable()
2089 * of its irq_chip implementation if the GPIO is known from that
2090 * code.
2091 */
2092static int gpiod_lock_as_irq(struct gpio_desc *desc)
2093{
2094 if (!desc)
2095 return -EINVAL;
2096
2097 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
2098 gpiod_err(desc,
2099 "%s: tried to flag a GPIO set as output for IRQ\n",
2100 __func__);
2101 return -EIO;
2102 }
2103
2104 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
2105 return 0;
2106}
2107
2108int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
2109{
2110 return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset));
2111}
2112EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
2113
2114/**
2115 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
2116 * @gpio: the GPIO line to unlock from IRQ usage
2117 *
2118 * This is used directly by GPIO drivers that want to indicate
2119 * that a certain GPIO is no longer used exclusively for IRQ.
2120 */
2121static void gpiod_unlock_as_irq(struct gpio_desc *desc)
2122{
2123 if (!desc)
2124 return;
2125
2126 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2127}
2128
2129void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2130{
2131 return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset));
2132}
2133EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
David Brownelld2876d02008-02-04 22:28:20 -08002134
David Brownelld2876d02008-02-04 22:28:20 -08002135/* There's no value in making it easy to inline GPIO calls that may sleep.
2136 * Common examples include ones connected to I2C or SPI chips.
2137 */
2138
Alexandre Courbotdef63432013-02-15 14:46:15 +09002139static int gpiod_get_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002140{
David Brownelld2876d02008-02-04 22:28:20 -08002141 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002142 if (!desc)
2143 return 0;
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09002144 return _gpiod_get_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002145}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002146
2147int gpio_get_value_cansleep(unsigned gpio)
2148{
2149 return gpiod_get_value_cansleep(gpio_to_desc(gpio));
2150}
David Brownelld2876d02008-02-04 22:28:20 -08002151EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
2152
Alexandre Courbot372e7222013-02-03 01:29:29 +09002153static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002154{
David Brownelld2876d02008-02-04 22:28:20 -08002155 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002156 if (!desc)
2157 return;
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09002158 _gpiod_set_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002159}
2160
2161void gpio_set_value_cansleep(unsigned gpio, int value)
2162{
2163 return gpiod_set_value_cansleep(gpio_to_desc(gpio), value);
David Brownelld2876d02008-02-04 22:28:20 -08002164}
2165EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
2166
David Brownelld2876d02008-02-04 22:28:20 -08002167#ifdef CONFIG_DEBUG_FS
2168
David Brownelld2876d02008-02-04 22:28:20 -08002169static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2170{
2171 unsigned i;
2172 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002173 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002174 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002175 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002176
2177 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2178 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2179 continue;
2180
Alexandre Courbot372e7222013-02-03 01:29:29 +09002181 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002182 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002183 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2184 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002185 gpio, gdesc->label,
2186 is_out ? "out" : "in ",
2187 chip->get
2188 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002189 : "? ",
2190 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002191 seq_printf(s, "\n");
2192 }
2193}
2194
Thierry Redingf9c4a312012-04-12 13:26:01 +02002195static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002196{
Grant Likely362432a2013-02-09 09:41:49 +00002197 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002198 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002199 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002200
2201 s->private = "";
2202
Grant Likely362432a2013-02-09 09:41:49 +00002203 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002204 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002205 if (index-- == 0) {
2206 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002207 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002208 }
2209 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002210
2211 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002212}
2213
2214static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2215{
Grant Likely362432a2013-02-09 09:41:49 +00002216 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002217 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002218 void *ret = NULL;
2219
Grant Likely362432a2013-02-09 09:41:49 +00002220 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002221 if (list_is_last(&chip->list, &gpio_chips))
2222 ret = NULL;
2223 else
2224 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002225 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002226
2227 s->private = "\n";
2228 ++*pos;
2229
2230 return ret;
2231}
2232
2233static void gpiolib_seq_stop(struct seq_file *s, void *v)
2234{
2235}
2236
2237static int gpiolib_seq_show(struct seq_file *s, void *v)
2238{
2239 struct gpio_chip *chip = v;
2240 struct device *dev;
2241
2242 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2243 chip->base, chip->base + chip->ngpio - 1);
2244 dev = chip->dev;
2245 if (dev)
2246 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2247 dev_name(dev));
2248 if (chip->label)
2249 seq_printf(s, ", %s", chip->label);
2250 if (chip->can_sleep)
2251 seq_printf(s, ", can sleep");
2252 seq_printf(s, ":\n");
2253
2254 if (chip->dbg_show)
2255 chip->dbg_show(s, chip);
2256 else
2257 gpiolib_dbg_show(s, chip);
2258
David Brownelld2876d02008-02-04 22:28:20 -08002259 return 0;
2260}
2261
Thierry Redingf9c4a312012-04-12 13:26:01 +02002262static const struct seq_operations gpiolib_seq_ops = {
2263 .start = gpiolib_seq_start,
2264 .next = gpiolib_seq_next,
2265 .stop = gpiolib_seq_stop,
2266 .show = gpiolib_seq_show,
2267};
2268
David Brownelld2876d02008-02-04 22:28:20 -08002269static int gpiolib_open(struct inode *inode, struct file *file)
2270{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002271 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002272}
2273
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002274static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002275 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002276 .open = gpiolib_open,
2277 .read = seq_read,
2278 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002279 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002280};
2281
2282static int __init gpiolib_debugfs_init(void)
2283{
2284 /* /sys/kernel/debug/gpio */
2285 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2286 NULL, NULL, &gpiolib_operations);
2287 return 0;
2288}
2289subsys_initcall(gpiolib_debugfs_init);
2290
2291#endif /* DEBUG_FS */