blob: 7b35e5093ef581bf55145aa9a191b9f52947560b [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>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
David Brownelld2876d02008-02-04 22:28:20 -080017
Mika Westerberg664e3e52014-01-08 12:40:54 +020018#include "gpiolib.h"
19
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060020#define CREATE_TRACE_POINTS
21#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080022
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070023/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080024 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070025 * The GPIO programming interface allows for inlining speed-critical
26 * get/set operations for common cases, so that access to SOC-integrated
27 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080028 */
29
30
31/* When debugging, extend minimal trust to callers and platform code.
32 * Also emit diagnostic messages that may help initial bringup, when
33 * board setup or driver bugs are most common.
34 *
35 * Otherwise, minimize overhead in what may be bitbanging codepaths.
36 */
37#ifdef DEBUG
38#define extra_checks 1
39#else
40#define extra_checks 0
41#endif
42
43/* gpio_lock prevents conflicts during gpio_desc[] table updates.
44 * While any GPIO is requested, its gpio_chip is not removable;
45 * each GPIO's "requested" flag serves as a lock and refcount.
46 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090047DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080048
David Brownelld2876d02008-02-04 22:28:20 -080049static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
50
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +090051#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
52
Alexandre Courbotbae48da2013-10-17 10:21:38 -070053static DEFINE_MUTEX(gpio_lookup_lock);
54static LIST_HEAD(gpio_lookup_list);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090055LIST_HEAD(gpio_chips);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +020056
David Brownelld2876d02008-02-04 22:28:20 -080057static inline void desc_set_label(struct gpio_desc *d, const char *label)
58{
David Brownelld2876d02008-02-04 22:28:20 -080059 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080060}
61
Alexandre Courbot372e7222013-02-03 01:29:29 +090062/**
63 * Convert a GPIO number to its descriptor
64 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070065struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090066{
67 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
68 return NULL;
69 else
70 return &gpio_desc[gpio];
71}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070072EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090073
74/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090075 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +020076 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090077struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
78 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +020079{
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090080 if (hwnum >= chip->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +090081 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +020082
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090083 return &chip->desc[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +020084}
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090085EXPORT_SYMBOL_GPL(gpiochip_get_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090086
87/**
88 * Convert a GPIO descriptor to the integer namespace.
89 * This should disappear in the future but is needed since we still
90 * use GPIO numbers for error messages and sysfs nodes
91 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070092int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +090093{
Alexandre Courbot8c0fca82013-10-04 10:59:57 -070094 return desc - &gpio_desc[0];
Alexandre Courbot372e7222013-02-03 01:29:29 +090095}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070096EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +090097
98
David Brownelld2876d02008-02-04 22:28:20 -080099/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
100 * when setting direction, and otherwise illegal. Until board setup code
101 * and drivers use explicit requests everywhere (which won't happen when
102 * those calls have no teeth) we can't avoid autorequesting. This nag
David Brownell35e8bb52008-10-15 22:03:16 -0700103 * message should motivate switching to explicit requests... so should
104 * the weaker cleanup after faults, compared to gpio_request().
David Brownell8a0cecf2009-04-02 16:57:06 -0700105 *
106 * NOTE: the autorequest mechanism is going away; at this point it's
107 * only "legal" in the sense that (old) code using it won't break yet,
108 * but instead only triggers a WARN() stack dump.
David Brownelld2876d02008-02-04 22:28:20 -0800109 */
Alexandre Courbot372e7222013-02-03 01:29:29 +0900110static int gpio_ensure_requested(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800111{
David Brownell8a0cecf2009-04-02 16:57:06 -0700112 const struct gpio_chip *chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900113 const int gpio = desc_to_gpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -0700114
David Brownell8a0cecf2009-04-02 16:57:06 -0700115 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
116 "autorequest GPIO-%d\n", gpio)) {
David Brownell35e8bb52008-10-15 22:03:16 -0700117 if (!try_module_get(chip->owner)) {
Andy Shevchenko7589e592013-12-05 11:26:23 +0200118 gpiod_err(desc, "%s: module can't be gotten\n",
119 __func__);
David Brownell35e8bb52008-10-15 22:03:16 -0700120 clear_bit(FLAG_REQUESTED, &desc->flags);
121 /* lose */
122 return -EIO;
123 }
David Brownelld2876d02008-02-04 22:28:20 -0800124 desc_set_label(desc, "[auto]");
David Brownell35e8bb52008-10-15 22:03:16 -0700125 /* caller must chip->request() w/o spinlock */
126 if (chip->request)
127 return 1;
David Brownelld2876d02008-02-04 22:28:20 -0800128 }
David Brownell35e8bb52008-10-15 22:03:16 -0700129 return 0;
David Brownelld2876d02008-02-04 22:28:20 -0800130}
131
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700132/**
133 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
134 * @desc: descriptor to return the chip of
135 */
136struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900137{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900138 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900139}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700140EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800141
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700142/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
143static int gpiochip_find_base(int ngpio)
144{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900145 struct gpio_chip *chip;
146 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700147
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900148 list_for_each_entry_reverse(chip, &gpio_chips, list) {
149 /* found a free space? */
150 if (chip->base + chip->ngpio <= base)
151 break;
152 else
153 /* nope, check the space right before the chip */
154 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700155 }
156
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900157 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700158 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900159 return base;
160 } else {
161 pr_err("%s: cannot find free range\n", __func__);
162 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700163 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700164}
165
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700166/**
167 * gpiod_get_direction - return the current direction of a GPIO
168 * @desc: GPIO to get the direction of
169 *
170 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
171 *
172 * This function may sleep if gpiod_cansleep() is true.
173 */
174int gpiod_get_direction(const struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300175{
176 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900177 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300178 int status = -EINVAL;
179
Alexandre Courbot372e7222013-02-03 01:29:29 +0900180 chip = gpiod_to_chip(desc);
181 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300182
183 if (!chip->get_direction)
184 return status;
185
Alexandre Courbot372e7222013-02-03 01:29:29 +0900186 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300187 if (status > 0) {
188 /* GPIOF_DIR_IN, or other positive */
189 status = 1;
Alexandre Courbotdef63432013-02-15 14:46:15 +0900190 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
191 * so it does not affect constness per se */
192 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300193 }
194 if (status == 0) {
195 /* GPIOF_DIR_OUT */
Alexandre Courbotdef63432013-02-15 14:46:15 +0900196 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300197 }
198 return status;
199}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700200EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300201
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900202/*
203 * Add a new chip to the global chips list, keeping the list of chips sorted
204 * by base order.
205 *
206 * Return -EBUSY if the new chip overlaps with some other chip's integer
207 * space.
208 */
209static int gpiochip_add_to_list(struct gpio_chip *chip)
210{
211 struct list_head *pos = &gpio_chips;
212 struct gpio_chip *_chip;
213 int err = 0;
214
215 /* find where to insert our chip */
216 list_for_each(pos, &gpio_chips) {
217 _chip = list_entry(pos, struct gpio_chip, list);
218 /* shall we insert before _chip? */
219 if (_chip->base >= chip->base + chip->ngpio)
220 break;
221 }
222
223 /* are we stepping on the chip right before? */
224 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
225 _chip = list_entry(pos->prev, struct gpio_chip, list);
226 if (_chip->base + _chip->ngpio > chip->base) {
227 dev_err(chip->dev,
228 "GPIO integer space overlap, cannot add chip\n");
229 err = -EBUSY;
230 }
231 }
232
233 if (!err)
234 list_add_tail(&chip->list, pos);
235
236 return err;
237}
238
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700239/**
David Brownelld2876d02008-02-04 22:28:20 -0800240 * gpiochip_add() - register a gpio_chip
241 * @chip: the chip to register, with chip->base initialized
242 * Context: potentially before irqs or kmalloc will work
243 *
244 * Returns a negative errno if the chip can't be registered, such as
245 * because the chip->base is invalid or already associated with a
246 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700247 *
David Brownelld8f388d2008-07-25 01:46:07 -0700248 * When gpiochip_add() is called very early during boot, so that GPIOs
249 * can be freely used, the chip->dev device must be registered before
250 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
251 * for GPIOs will fail rudely.
252 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700253 * If chip->base is negative, this requests dynamic assignment of
254 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800255 */
256int gpiochip_add(struct gpio_chip *chip)
257{
258 unsigned long flags;
259 int status = 0;
260 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700261 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -0800262
Trent Piephobff5fda2008-05-23 13:04:44 -0700263 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700264 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -0800265 status = -EINVAL;
266 goto fail;
267 }
268
269 spin_lock_irqsave(&gpio_lock, flags);
270
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700271 if (base < 0) {
272 base = gpiochip_find_base(chip->ngpio);
273 if (base < 0) {
274 status = base;
David Brownelld8f388d2008-07-25 01:46:07 -0700275 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700276 }
277 chip->base = base;
278 }
279
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900280 status = gpiochip_add_to_list(chip);
281
David Brownelld2876d02008-02-04 22:28:20 -0800282 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900283 chip->desc = &gpio_desc[chip->base];
284
285 for (id = 0; id < chip->ngpio; id++) {
286 struct gpio_desc *desc = &chip->desc[id];
287 desc->chip = chip;
David Brownelld8f388d2008-07-25 01:46:07 -0700288
289 /* REVISIT: most hardware initializes GPIOs as
290 * inputs (often with pullups enabled) so power
291 * usage is minimized. Linux code should set the
292 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +0300293 * and in case chip->get_direction is not set,
David Brownelld8f388d2008-07-25 01:46:07 -0700294 * we may expose the wrong direction in sysfs.
295 */
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900296 desc->flags = !chip->direction_input
David Brownelld8f388d2008-07-25 01:46:07 -0700297 ? (1 << FLAG_IS_OUT)
298 : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800299 }
300 }
301
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800302 spin_unlock_irqrestore(&gpio_lock, flags);
303
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530304#ifdef CONFIG_PINCTRL
305 INIT_LIST_HEAD(&chip->pin_ranges);
306#endif
307
Anton Vorontsov391c9702010-06-08 07:48:17 -0600308 of_gpiochip_add(chip);
Mika Westerberg664e3e52014-01-08 12:40:54 +0200309 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600310
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600311 if (status)
312 goto fail;
313
314 status = gpiochip_export(chip);
315 if (status)
316 goto fail;
317
Andy Shevchenko7589e592013-12-05 11:26:23 +0200318 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700319 chip->base, chip->base + chip->ngpio - 1,
320 chip->label ? : "generic");
321
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600322 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800323
324unlock:
325 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld2876d02008-02-04 22:28:20 -0800326fail:
327 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200328 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600329 chip->base, chip->base + chip->ngpio - 1,
330 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800331 return status;
332}
333EXPORT_SYMBOL_GPL(gpiochip_add);
334
Linus Walleij14250522014-03-25 10:40:18 +0100335/* Forward-declaration */
336static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
337
David Brownelld2876d02008-02-04 22:28:20 -0800338/**
339 * gpiochip_remove() - unregister a gpio_chip
340 * @chip: the chip to unregister
341 *
342 * A gpio_chip with any GPIOs still requested may not be removed.
343 */
344int gpiochip_remove(struct gpio_chip *chip)
345{
346 unsigned long flags;
347 int status = 0;
348 unsigned id;
349
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200350 acpi_gpiochip_remove(chip);
351
David Brownelld2876d02008-02-04 22:28:20 -0800352 spin_lock_irqsave(&gpio_lock, flags);
353
Linus Walleij14250522014-03-25 10:40:18 +0100354 gpiochip_irqchip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100355 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600356 of_gpiochip_remove(chip);
357
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900358 for (id = 0; id < chip->ngpio; id++) {
359 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
David Brownelld2876d02008-02-04 22:28:20 -0800360 status = -EBUSY;
361 break;
362 }
363 }
364 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900365 for (id = 0; id < chip->ngpio; id++)
366 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900367
368 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800369 }
370
371 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d2008-07-25 01:46:07 -0700372
373 if (status == 0)
374 gpiochip_unexport(chip);
375
David Brownelld2876d02008-02-04 22:28:20 -0800376 return status;
377}
378EXPORT_SYMBOL_GPL(gpiochip_remove);
379
Grant Likely594fa262010-06-08 07:48:16 -0600380/**
381 * gpiochip_find() - iterator for locating a specific gpio_chip
382 * @data: data to pass to match function
383 * @callback: Callback function to check gpio_chip
384 *
385 * Similar to bus_find_device. It returns a reference to a gpio_chip as
386 * determined by a user supplied @match callback. The callback should return
387 * 0 if the device doesn't match and non-zero if it does. If the callback is
388 * non-zero, this function will return to the caller and not iterate over any
389 * more gpio_chips.
390 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600391struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700392 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600393 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600394{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900395 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600396 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600397
398 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900399 list_for_each_entry(chip, &gpio_chips, list)
400 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600401 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900402
403 /* No match? */
404 if (&chip->list == &gpio_chips)
405 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600406 spin_unlock_irqrestore(&gpio_lock, flags);
407
408 return chip;
409}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600410EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800411
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900412static int gpiochip_match_name(struct gpio_chip *chip, void *data)
413{
414 const char *name = data;
415
416 return !strcmp(chip->label, name);
417}
418
419static struct gpio_chip *find_chip_by_name(const char *name)
420{
421 return gpiochip_find((void *)name, gpiochip_match_name);
422}
423
Linus Walleij14250522014-03-25 10:40:18 +0100424#ifdef CONFIG_GPIOLIB_IRQCHIP
425
426/*
427 * The following is irqchip helper code for gpiochips.
428 */
429
430/**
431 * gpiochip_add_chained_irqchip() - adds a chained irqchip to a gpiochip
432 * @gpiochip: the gpiochip to add the irqchip to
433 * @irqchip: the irqchip to add to the gpiochip
434 * @parent_irq: the irq number corresponding to the parent IRQ for this
435 * chained irqchip
436 * @parent_handler: the parent interrupt handler for the accumulated IRQ
437 * coming out of the gpiochip
438 */
439void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
440 struct irq_chip *irqchip,
441 int parent_irq,
442 irq_flow_handler_t parent_handler)
443{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200444 if (gpiochip->can_sleep) {
445 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
446 return;
447 }
448
Linus Walleij14250522014-03-25 10:40:18 +0100449 irq_set_chained_handler(parent_irq, parent_handler);
450 /*
451 * The parent irqchip is already using the chip_data for this
452 * irqchip, so our callbacks simply use the handler_data.
453 */
454 irq_set_handler_data(parent_irq, gpiochip);
455}
456EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
457
Linus Walleije45d1c82014-04-22 14:01:46 +0200458/*
459 * This lock class tells lockdep that GPIO irqs are in a different
460 * category than their parents, so it won't report false recursion.
461 */
462static struct lock_class_key gpiochip_irq_lock_class;
463
Linus Walleij14250522014-03-25 10:40:18 +0100464/**
465 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
466 * @d: the irqdomain used by this irqchip
467 * @irq: the global irq number used by this GPIO irqchip irq
468 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
469 *
470 * This function will set up the mapping for a certain IRQ line on a
471 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
472 * stored inside the gpiochip.
473 */
474static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
475 irq_hw_number_t hwirq)
476{
477 struct gpio_chip *chip = d->host_data;
478
Linus Walleij14250522014-03-25 10:40:18 +0100479 irq_set_chip_data(irq, chip);
Linus Walleije45d1c82014-04-22 14:01:46 +0200480 irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
Linus Walleij7633fb92014-04-09 13:20:38 +0200481 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200482 /* Chips that can sleep need nested thread handlers */
483 if (chip->can_sleep)
484 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100485#ifdef CONFIG_ARM
486 set_irq_flags(irq, IRQF_VALID);
487#else
488 irq_set_noprobe(irq);
489#endif
Linus Walleij1333b902014-04-23 16:45:12 +0200490 /*
491 * No set-up of the hardware will happen if IRQ_TYPE_NONE
492 * is passed as default type.
493 */
494 if (chip->irq_default_type != IRQ_TYPE_NONE)
495 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100496
497 return 0;
498}
499
Linus Walleijc3626fd2014-03-28 20:42:01 +0100500static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
501{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200502 struct gpio_chip *chip = d->host_data;
503
Linus Walleijc3626fd2014-03-28 20:42:01 +0100504#ifdef CONFIG_ARM
505 set_irq_flags(irq, 0);
506#endif
Linus Walleij1c8732b2014-04-09 13:34:39 +0200507 if (chip->can_sleep)
508 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100509 irq_set_chip_and_handler(irq, NULL, NULL);
510 irq_set_chip_data(irq, NULL);
511}
512
Linus Walleij14250522014-03-25 10:40:18 +0100513static const struct irq_domain_ops gpiochip_domain_ops = {
514 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100515 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100516 /* Virtually all GPIO irqchips are twocell:ed */
517 .xlate = irq_domain_xlate_twocell,
518};
519
520static int gpiochip_irq_reqres(struct irq_data *d)
521{
522 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
523
524 if (gpio_lock_as_irq(chip, d->hwirq)) {
525 chip_err(chip,
526 "unable to lock HW IRQ %lu for IRQ\n",
527 d->hwirq);
528 return -EINVAL;
529 }
530 return 0;
531}
532
533static void gpiochip_irq_relres(struct irq_data *d)
534{
535 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
536
537 gpio_unlock_as_irq(chip, d->hwirq);
538}
539
540static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
541{
542 return irq_find_mapping(chip->irqdomain, offset);
543}
544
545/**
546 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
547 * @gpiochip: the gpiochip to remove the irqchip from
548 *
549 * This is called only from gpiochip_remove()
550 */
551static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
552{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100553 unsigned int offset;
554
555 /* Remove all IRQ mappings and delete the domain */
556 if (gpiochip->irqdomain) {
557 for (offset = 0; offset < gpiochip->ngpio; offset++)
558 irq_dispose_mapping(gpiochip->irq_base + offset);
Linus Walleij14250522014-03-25 10:40:18 +0100559 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100560 }
Linus Walleij14250522014-03-25 10:40:18 +0100561
562 if (gpiochip->irqchip) {
563 gpiochip->irqchip->irq_request_resources = NULL;
564 gpiochip->irqchip->irq_release_resources = NULL;
565 gpiochip->irqchip = NULL;
566 }
567}
568
569/**
570 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
571 * @gpiochip: the gpiochip to add the irqchip to
572 * @irqchip: the irqchip to add to the gpiochip
573 * @first_irq: if not dynamically assigned, the base (first) IRQ to
574 * allocate gpiochip irqs from
575 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200576 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
577 * to have the core avoid setting up any default type in the hardware.
Linus Walleij14250522014-03-25 10:40:18 +0100578 *
579 * This function closely associates a certain irqchip with a certain
580 * gpiochip, providing an irq domain to translate the local IRQs to
581 * global irqs in the gpiolib core, and making sure that the gpiochip
582 * is passed as chip data to all related functions. Driver callbacks
583 * need to use container_of() to get their local state containers back
584 * from the gpiochip passed as chip data. An irqdomain will be stored
585 * in the gpiochip that shall be used by the driver to handle IRQ number
586 * translation. The gpiochip will need to be initialized and registered
587 * before calling this function.
588 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100589 * This function will handle two cell:ed simple IRQs and assumes all
590 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100591 * need to be open coded.
592 */
593int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
594 struct irq_chip *irqchip,
595 unsigned int first_irq,
596 irq_flow_handler_t handler,
597 unsigned int type)
598{
599 struct device_node *of_node;
600 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100601 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100602
603 if (!gpiochip || !irqchip)
604 return -EINVAL;
605
606 if (!gpiochip->dev) {
607 pr_err("missing gpiochip .dev parent pointer\n");
608 return -EINVAL;
609 }
610 of_node = gpiochip->dev->of_node;
611#ifdef CONFIG_OF_GPIO
612 /*
613 * If the gpiochip has an assigned OF node this takes precendence
614 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
615 */
616 if (gpiochip->of_node)
617 of_node = gpiochip->of_node;
618#endif
619 gpiochip->irqchip = irqchip;
620 gpiochip->irq_handler = handler;
621 gpiochip->irq_default_type = type;
622 gpiochip->to_irq = gpiochip_to_irq;
623 gpiochip->irqdomain = irq_domain_add_simple(of_node,
624 gpiochip->ngpio, first_irq,
625 &gpiochip_domain_ops, gpiochip);
626 if (!gpiochip->irqdomain) {
627 gpiochip->irqchip = NULL;
628 return -EINVAL;
629 }
630 irqchip->irq_request_resources = gpiochip_irq_reqres;
631 irqchip->irq_release_resources = gpiochip_irq_relres;
632
633 /*
634 * Prepare the mapping since the irqchip shall be orthogonal to
635 * any gpiochip calls. If the first_irq was zero, this is
636 * necessary to allocate descriptors for all IRQs.
637 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100638 for (offset = 0; offset < gpiochip->ngpio; offset++) {
639 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
640 if (offset == 0)
641 /*
642 * Store the base into the gpiochip to be used when
643 * unmapping the irqs.
644 */
645 gpiochip->irq_base = irq_base;
646 }
Linus Walleij14250522014-03-25 10:40:18 +0100647
648 return 0;
649}
650EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
651
652#else /* CONFIG_GPIOLIB_IRQCHIP */
653
654static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
655
656#endif /* CONFIG_GPIOLIB_IRQCHIP */
657
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530658#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100659
Linus Walleij3f0f8672012-11-20 12:40:15 +0100660/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200661 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
662 * @chip: the gpiochip to add the range for
663 * @pinctrl: the dev_name() of the pin controller to map to
664 * @gpio_offset: the start offset in the current gpio_chip number space
665 * @pin_group: name of the pin group inside the pin controller
666 */
667int gpiochip_add_pingroup_range(struct gpio_chip *chip,
668 struct pinctrl_dev *pctldev,
669 unsigned int gpio_offset, const char *pin_group)
670{
671 struct gpio_pin_range *pin_range;
672 int ret;
673
674 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
675 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200676 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200677 return -ENOMEM;
678 }
679
680 /* Use local offset as range ID */
681 pin_range->range.id = gpio_offset;
682 pin_range->range.gc = chip;
683 pin_range->range.name = chip->label;
684 pin_range->range.base = chip->base + gpio_offset;
685 pin_range->pctldev = pctldev;
686
687 ret = pinctrl_get_group_pins(pctldev, pin_group,
688 &pin_range->range.pins,
689 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100690 if (ret < 0) {
691 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200692 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100693 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200694
695 pinctrl_add_gpio_range(pctldev, &pin_range->range);
696
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200697 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
698 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200699 pinctrl_dev_get_devname(pctldev), pin_group);
700
701 list_add_tail(&pin_range->node, &chip->pin_ranges);
702
703 return 0;
704}
705EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
706
707/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100708 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
709 * @chip: the gpiochip to add the range for
710 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100711 * @gpio_offset: the start offset in the current gpio_chip number space
712 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100713 * @npins: the number of pins from the offset of each pin space (GPIO and
714 * pin controller) to accumulate in this range
715 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100716int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100717 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100718 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530719{
720 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800721 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530722
Linus Walleij3f0f8672012-11-20 12:40:15 +0100723 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530724 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200725 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100726 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530727 }
728
Linus Walleij3f0f8672012-11-20 12:40:15 +0100729 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100730 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100731 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530732 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100733 pin_range->range.base = chip->base + gpio_offset;
734 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530735 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100736 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530737 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100738 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800739 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200740 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100741 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800742 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100743 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200744 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
745 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100746 pinctl_name,
747 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530748
749 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100750
751 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530752}
Linus Walleij165adc92012-11-06 14:49:39 +0100753EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530754
Linus Walleij3f0f8672012-11-20 12:40:15 +0100755/**
756 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
757 * @chip: the chip to remove all the mappings for
758 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530759void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
760{
761 struct gpio_pin_range *pin_range, *tmp;
762
763 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
764 list_del(&pin_range->node);
765 pinctrl_remove_gpio_range(pin_range->pctldev,
766 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100767 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530768 }
769}
Linus Walleij165adc92012-11-06 14:49:39 +0100770EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
771
772#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530773
David Brownelld2876d02008-02-04 22:28:20 -0800774/* These "optional" allocation calls help prevent drivers from stomping
775 * on each other, and help provide better diagnostics in debugfs.
776 * They're called even less than the "set direction" calls.
777 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200778static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800779{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200780 struct gpio_chip *chip = desc->chip;
781 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800782 unsigned long flags;
783
784 spin_lock_irqsave(&gpio_lock, flags);
785
David Brownelld2876d02008-02-04 22:28:20 -0800786 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700787 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800788 */
789
790 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
791 desc_set_label(desc, label ? : "?");
792 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700793 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800794 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800795 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700796 }
797
798 if (chip->request) {
799 /* chip->request may sleep */
800 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900801 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700802 spin_lock_irqsave(&gpio_lock, flags);
803
804 if (status < 0) {
805 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700806 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300807 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700808 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700809 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300810 if (chip->get_direction) {
811 /* chip->get_direction may sleep */
812 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900813 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300814 spin_lock_irqsave(&gpio_lock, flags);
815 }
David Brownelld2876d02008-02-04 22:28:20 -0800816done:
Mika Westerberg77c2d792014-03-10 14:54:50 +0200817 spin_unlock_irqrestore(&gpio_lock, flags);
818 return status;
819}
820
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900821int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200822{
823 int status = -EPROBE_DEFER;
824 struct gpio_chip *chip;
825
826 if (!desc) {
827 pr_warn("%s: invalid GPIO\n", __func__);
828 return -EINVAL;
829 }
830
831 chip = desc->chip;
832 if (!chip)
833 goto done;
834
835 if (try_module_get(chip->owner)) {
836 status = __gpiod_request(desc, label);
837 if (status < 0)
838 module_put(chip->owner);
839 }
840
841done:
David Brownelld2876d02008-02-04 22:28:20 -0800842 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200843 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200844
David Brownelld2876d02008-02-04 22:28:20 -0800845 return status;
846}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900847
848int gpio_request(unsigned gpio, const char *label)
849{
850 return gpiod_request(gpio_to_desc(gpio), label);
851}
David Brownelld2876d02008-02-04 22:28:20 -0800852EXPORT_SYMBOL_GPL(gpio_request);
853
Mika Westerberg77c2d792014-03-10 14:54:50 +0200854static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800855{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200856 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -0800857 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -0700858 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800859
Uwe Kleine-König3d599d12008-10-15 22:03:12 -0700860 might_sleep();
861
Alexandre Courbot372e7222013-02-03 01:29:29 +0900862 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700863
David Brownelld2876d02008-02-04 22:28:20 -0800864 spin_lock_irqsave(&gpio_lock, flags);
865
David Brownell35e8bb52008-10-15 22:03:16 -0700866 chip = desc->chip;
867 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
868 if (chip->free) {
869 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -0700870 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900871 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700872 spin_lock_irqsave(&gpio_lock, flags);
873 }
David Brownelld2876d02008-02-04 22:28:20 -0800874 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -0800875 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -0700876 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530877 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +0530878 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200879 ret = true;
880 }
David Brownelld2876d02008-02-04 22:28:20 -0800881
882 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200883 return ret;
884}
885
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900886void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200887{
888 if (desc && __gpiod_free(desc))
889 module_put(desc->chip->owner);
890 else
891 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -0800892}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900893
894void gpio_free(unsigned gpio)
895{
896 gpiod_free(gpio_to_desc(gpio));
897}
David Brownelld2876d02008-02-04 22:28:20 -0800898EXPORT_SYMBOL_GPL(gpio_free);
899
Eric Miao3e45f1d2010-03-05 13:44:35 -0800900/**
901 * gpio_request_one - request a single GPIO with initial configuration
902 * @gpio: the GPIO number
903 * @flags: GPIO configuration as specified by GPIOF_*
904 * @label: a literal description string of this GPIO
905 */
906int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
907{
Alexandre Courbot372e7222013-02-03 01:29:29 +0900908 struct gpio_desc *desc;
Eric Miao3e45f1d2010-03-05 13:44:35 -0800909 int err;
910
Alexandre Courbot372e7222013-02-03 01:29:29 +0900911 desc = gpio_to_desc(gpio);
912
913 err = gpiod_request(desc, label);
Eric Miao3e45f1d2010-03-05 13:44:35 -0800914 if (err)
915 return err;
916
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530917 if (flags & GPIOF_OPEN_DRAIN)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900918 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530919
Laxman Dewangan25553ff2012-02-17 20:26:22 +0530920 if (flags & GPIOF_OPEN_SOURCE)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900921 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +0530922
Eric Miao3e45f1d2010-03-05 13:44:35 -0800923 if (flags & GPIOF_DIR_IN)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900924 err = gpiod_direction_input(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -0800925 else
Philipp Zabelef70bbe2014-01-07 12:34:11 +0100926 err = gpiod_direction_output_raw(desc,
Eric Miao3e45f1d2010-03-05 13:44:35 -0800927 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
928
Aaro Koskinene2548112010-12-21 17:24:22 -0800929 if (err)
Wolfram Sangfc3a1f02011-12-13 18:34:01 +0100930 goto free_gpio;
Aaro Koskinene2548112010-12-21 17:24:22 -0800931
Wolfram Sangfc3a1f02011-12-13 18:34:01 +0100932 if (flags & GPIOF_EXPORT) {
Alexandre Courbot372e7222013-02-03 01:29:29 +0900933 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
Wolfram Sangfc3a1f02011-12-13 18:34:01 +0100934 if (err)
935 goto free_gpio;
936 }
937
938 return 0;
939
940 free_gpio:
Alexandre Courbot372e7222013-02-03 01:29:29 +0900941 gpiod_free(desc);
Eric Miao3e45f1d2010-03-05 13:44:35 -0800942 return err;
943}
944EXPORT_SYMBOL_GPL(gpio_request_one);
945
946/**
947 * gpio_request_array - request multiple GPIOs in a single call
948 * @array: array of the 'struct gpio'
949 * @num: how many GPIOs in the array
950 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -0700951int gpio_request_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -0800952{
953 int i, err;
954
955 for (i = 0; i < num; i++, array++) {
956 err = gpio_request_one(array->gpio, array->flags, array->label);
957 if (err)
958 goto err_free;
959 }
960 return 0;
961
962err_free:
963 while (i--)
964 gpio_free((--array)->gpio);
965 return err;
966}
967EXPORT_SYMBOL_GPL(gpio_request_array);
968
969/**
970 * gpio_free_array - release multiple GPIOs in a single call
971 * @array: array of the 'struct gpio'
972 * @num: how many GPIOs in the array
973 */
Lars-Peter Clausen7c295972011-05-25 16:20:31 -0700974void gpio_free_array(const struct gpio *array, size_t num)
Eric Miao3e45f1d2010-03-05 13:44:35 -0800975{
976 while (num--)
977 gpio_free((array++)->gpio);
978}
979EXPORT_SYMBOL_GPL(gpio_free_array);
David Brownelld2876d02008-02-04 22:28:20 -0800980
981/**
982 * gpiochip_is_requested - return string iff signal was requested
983 * @chip: controller managing the signal
984 * @offset: of signal within controller's 0..(ngpio - 1) range
985 *
986 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +0900987 * The string returned is the label passed to gpio_request(); if none has been
988 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -0800989 *
990 * This function is for use by GPIO controller drivers. The label can
991 * help with diagnostics, and knowing that the signal is used as a GPIO
992 * can help avoid accidentally multiplexing it to another controller.
993 */
994const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
995{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900996 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800997
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900998 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -0800999 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001000
1001 desc = &chip->desc[offset];
1002
Alexandre Courbot372e7222013-02-03 01:29:29 +09001003 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001004 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001005 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001006}
1007EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1008
Mika Westerberg77c2d792014-03-10 14:54:50 +02001009/**
1010 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1011 * @desc: GPIO descriptor to request
1012 * @label: label for the GPIO
1013 *
1014 * Function allows GPIO chip drivers to request and use their own GPIO
1015 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1016 * function will not increase reference count of the GPIO chip module. This
1017 * allows the GPIO chip module to be unloaded as needed (we assume that the
1018 * GPIO chip driver handles freeing the GPIOs it has requested).
1019 */
1020int gpiochip_request_own_desc(struct gpio_desc *desc, const char *label)
1021{
1022 if (!desc || !desc->chip)
1023 return -EINVAL;
1024
1025 return __gpiod_request(desc, label);
1026}
1027
1028/**
1029 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1030 * @desc: GPIO descriptor to free
1031 *
1032 * Function frees the given GPIO requested previously with
1033 * gpiochip_request_own_desc().
1034 */
1035void gpiochip_free_own_desc(struct gpio_desc *desc)
1036{
1037 if (desc)
1038 __gpiod_free(desc);
1039}
David Brownelld2876d02008-02-04 22:28:20 -08001040
1041/* Drivers MUST set GPIO direction before making get/set calls. In
1042 * some cases this is done in early boot, before IRQs are enabled.
1043 *
1044 * As a rule these aren't called more than once (except for drivers
1045 * using the open-drain emulation idiom) so these are natural places
1046 * to accumulate extra debugging checks. Note that we can't (yet)
1047 * rely on gpio_request() having been called beforehand.
1048 */
1049
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001050/**
1051 * gpiod_direction_input - set the GPIO direction to input
1052 * @desc: GPIO to set to input
1053 *
1054 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1055 * be called safely on it.
1056 *
1057 * Return 0 in case of success, else an error code.
1058 */
1059int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001060{
1061 unsigned long flags;
1062 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001063 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001064 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001065
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001066 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001067 pr_warn("%s: invalid GPIO\n", __func__);
1068 return -EINVAL;
1069 }
1070
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001071 chip = desc->chip;
1072 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001073 gpiod_warn(desc,
1074 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001075 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001076 return -EIO;
1077 }
1078
David Brownelld2876d02008-02-04 22:28:20 -08001079 spin_lock_irqsave(&gpio_lock, flags);
1080
Alexandre Courbot372e7222013-02-03 01:29:29 +09001081 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001082 if (status < 0)
1083 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001084
1085 /* now we know the gpio is valid and chip won't vanish */
1086
1087 spin_unlock_irqrestore(&gpio_lock, flags);
1088
David Brownell9c4ba942010-08-10 18:02:24 -07001089 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001090
Alexandre Courbot372e7222013-02-03 01:29:29 +09001091 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001092 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001093 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001094 if (status < 0) {
Andy Shevchenko7589e592013-12-05 11:26:23 +02001095 gpiod_dbg(desc, "%s: chip request fail, %d\n",
1096 __func__, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001097 /* and it's not available to anyone else ...
1098 * gpio_request() is the fully clean solution.
1099 */
1100 goto lose;
1101 }
1102 }
1103
Alexandre Courbot372e7222013-02-03 01:29:29 +09001104 status = chip->direction_input(chip, offset);
David Brownelld2876d02008-02-04 22:28:20 -08001105 if (status == 0)
1106 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001107
Alexandre Courbot372e7222013-02-03 01:29:29 +09001108 trace_gpio_direction(desc_to_gpio(desc), 1, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001109lose:
David Brownelld2876d02008-02-04 22:28:20 -08001110 return status;
1111fail:
1112 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001113 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001114 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001115 return status;
1116}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001117EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001118
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001119static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001120{
1121 unsigned long flags;
1122 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001123 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001124 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001125
Linus Walleijd468bf92013-09-24 11:54:38 +02001126 /* GPIOs used for IRQs shall not be set as output */
1127 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1128 gpiod_err(desc,
1129 "%s: tried to set a GPIO tied to an IRQ as output\n",
1130 __func__);
1131 return -EIO;
1132 }
1133
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301134 /* Open drain pin should not be driven to 1 */
1135 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001136 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301137
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301138 /* Open source pin should not be driven to 0 */
1139 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001140 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301141
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001142 chip = desc->chip;
1143 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001144 gpiod_warn(desc,
1145 "%s: missing set() or direction_output() operations\n",
1146 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001147 return -EIO;
1148 }
1149
David Brownelld2876d02008-02-04 22:28:20 -08001150 spin_lock_irqsave(&gpio_lock, flags);
1151
Alexandre Courbot372e7222013-02-03 01:29:29 +09001152 status = gpio_ensure_requested(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001153 if (status < 0)
1154 goto fail;
David Brownelld2876d02008-02-04 22:28:20 -08001155
1156 /* now we know the gpio is valid and chip won't vanish */
1157
1158 spin_unlock_irqrestore(&gpio_lock, flags);
1159
David Brownell9c4ba942010-08-10 18:02:24 -07001160 might_sleep_if(chip->can_sleep);
David Brownelld2876d02008-02-04 22:28:20 -08001161
Alexandre Courbot372e7222013-02-03 01:29:29 +09001162 offset = gpio_chip_hwgpio(desc);
David Brownell35e8bb52008-10-15 22:03:16 -07001163 if (status) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001164 status = chip->request(chip, offset);
David Brownell35e8bb52008-10-15 22:03:16 -07001165 if (status < 0) {
Andy Shevchenko7589e592013-12-05 11:26:23 +02001166 gpiod_dbg(desc, "%s: chip request fail, %d\n",
1167 __func__, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001168 /* and it's not available to anyone else ...
1169 * gpio_request() is the fully clean solution.
1170 */
1171 goto lose;
1172 }
1173 }
1174
Alexandre Courbot372e7222013-02-03 01:29:29 +09001175 status = chip->direction_output(chip, offset, value);
David Brownelld2876d02008-02-04 22:28:20 -08001176 if (status == 0)
1177 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001178 trace_gpio_value(desc_to_gpio(desc), 0, value);
1179 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownell35e8bb52008-10-15 22:03:16 -07001180lose:
David Brownelld2876d02008-02-04 22:28:20 -08001181 return status;
1182fail:
1183 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001184 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001185 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
David Brownelld2876d02008-02-04 22:28:20 -08001186 return status;
1187}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001188
1189/**
1190 * gpiod_direction_output_raw - set the GPIO direction to output
1191 * @desc: GPIO to set to output
1192 * @value: initial output value of the GPIO
1193 *
1194 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1195 * be called safely on it. The initial value of the output must be specified
1196 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1197 *
1198 * Return 0 in case of success, else an error code.
1199 */
1200int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1201{
1202 if (!desc || !desc->chip) {
1203 pr_warn("%s: invalid GPIO\n", __func__);
1204 return -EINVAL;
1205 }
1206 return _gpiod_direction_output_raw(desc, value);
1207}
1208EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1209
1210/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301211 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001212 * @desc: GPIO to set to output
1213 * @value: initial output value of the GPIO
1214 *
1215 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1216 * be called safely on it. The initial value of the output must be specified
1217 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1218 * account.
1219 *
1220 * Return 0 in case of success, else an error code.
1221 */
1222int gpiod_direction_output(struct gpio_desc *desc, int value)
1223{
1224 if (!desc || !desc->chip) {
1225 pr_warn("%s: invalid GPIO\n", __func__);
1226 return -EINVAL;
1227 }
1228 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1229 value = !value;
1230 return _gpiod_direction_output_raw(desc, value);
1231}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001232EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001233
Felipe Balbic4b5be92010-05-26 14:42:23 -07001234/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001235 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001236 * @gpio: the gpio to set debounce time
1237 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001238 *
1239 * returns -ENOTSUPP if the controller does not support setting
1240 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001241 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001242int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001243{
1244 unsigned long flags;
1245 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001246 int status = -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001247 int offset;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001248
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001249 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001250 pr_warn("%s: invalid GPIO\n", __func__);
1251 return -EINVAL;
1252 }
1253
Felipe Balbic4b5be92010-05-26 14:42:23 -07001254 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001255 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001256 gpiod_dbg(desc,
1257 "%s: missing set() or set_debounce() operations\n",
1258 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001259 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001260 }
1261
1262 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001263
1264 status = gpio_ensure_requested(desc);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001265 if (status < 0)
1266 goto fail;
1267
1268 /* now we know the gpio is valid and chip won't vanish */
1269
1270 spin_unlock_irqrestore(&gpio_lock, flags);
1271
David Brownell9c4ba942010-08-10 18:02:24 -07001272 might_sleep_if(chip->can_sleep);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001273
Alexandre Courbot372e7222013-02-03 01:29:29 +09001274 offset = gpio_chip_hwgpio(desc);
1275 return chip->set_debounce(chip, offset, debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001276
1277fail:
1278 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001279 if (status)
Mark Brown6424de52013-09-09 10:33:49 +01001280 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001281
1282 return status;
1283}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001284EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001285
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001286/**
1287 * gpiod_is_active_low - test whether a GPIO is active-low or not
1288 * @desc: the gpio descriptor to test
1289 *
1290 * Returns 1 if the GPIO is active-low, 0 otherwise.
1291 */
1292int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001293{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001294 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001295}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001296EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001297
1298/* I/O calls are only valid after configuration completed; the relevant
1299 * "is this a valid GPIO" error checks should already have been done.
1300 *
1301 * "Get" operations are often inlinable as reading a pin value register,
1302 * and masking the relevant bit in that register.
1303 *
1304 * When "set" operations are inlinable, they involve writing that mask to
1305 * one register to set a low value, or a different register to set it high.
1306 * Otherwise locking is needed, so there may be little value to inlining.
1307 *
1308 *------------------------------------------------------------------------
1309 *
1310 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1311 * have requested the GPIO. That can include implicit requesting by
1312 * a direction setting call. Marking a gpio as requested locks its chip
1313 * in memory, guaranteeing that these table lookups need no more locking
1314 * and that gpiochip_remove() will fail.
1315 *
1316 * REVISIT when debugging, consider adding some instrumentation to ensure
1317 * that the GPIO was actually requested.
1318 */
1319
Alexandre Courbot23600962014-03-11 15:52:09 +09001320static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001321{
1322 struct gpio_chip *chip;
Alexandre Courbot23600962014-03-11 15:52:09 +09001323 bool value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001324 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001325
Alexandre Courbot372e7222013-02-03 01:29:29 +09001326 chip = desc->chip;
1327 offset = gpio_chip_hwgpio(desc);
Alexandre Courbot23600962014-03-11 15:52:09 +09001328 value = chip->get ? chip->get(chip, offset) : false;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001329 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001330 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001331}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001332
David Brownelld2876d02008-02-04 22:28:20 -08001333/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001334 * gpiod_get_raw_value() - return a gpio's raw value
1335 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001336 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001337 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1338 * its ACTIVE_LOW status.
1339 *
1340 * This function should be called from contexts where we cannot sleep, and will
1341 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001342 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001343int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001344{
David Brownelld2876d02008-02-04 22:28:20 -08001345 if (!desc)
1346 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001347 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001348 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001349 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001350}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001351EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001352
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001353/**
1354 * gpiod_get_value() - return a gpio's value
1355 * @desc: gpio whose value will be returned
1356 *
1357 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1358 * account.
1359 *
1360 * This function should be called from contexts where we cannot sleep, and will
1361 * complain if the GPIO chip functions potentially sleep.
1362 */
1363int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001364{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001365 int value;
1366 if (!desc)
1367 return 0;
1368 /* Should be using gpio_get_value_cansleep() */
1369 WARN_ON(desc->chip->can_sleep);
1370
1371 value = _gpiod_get_raw_value(desc);
1372 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1373 value = !value;
1374
1375 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001376}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001377EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001378
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301379/*
1380 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001381 * @desc: gpio descriptor whose state need to be set.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301382 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1383 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001384static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301385{
1386 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001387 struct gpio_chip *chip = desc->chip;
1388 int offset = gpio_chip_hwgpio(desc);
1389
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301390 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001391 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301392 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001393 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301394 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001395 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301396 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001397 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301398 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001399 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301400 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001401 gpiod_err(desc,
1402 "%s: Error in set_value for open drain err %d\n",
1403 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301404}
1405
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301406/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001407 * _gpio_set_open_source_value() - Set the open source gpio's value.
1408 * @desc: gpio descriptor whose state need to be set.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301409 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1410 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001411static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301412{
1413 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001414 struct gpio_chip *chip = desc->chip;
1415 int offset = gpio_chip_hwgpio(desc);
1416
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301417 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001418 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301419 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001420 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301421 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001422 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301423 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001424 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301425 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001426 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301427 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001428 gpiod_err(desc,
1429 "%s: Error in set_value for open source err %d\n",
1430 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301431}
1432
Alexandre Courbot23600962014-03-11 15:52:09 +09001433static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001434{
1435 struct gpio_chip *chip;
1436
Alexandre Courbot372e7222013-02-03 01:29:29 +09001437 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001438 trace_gpio_value(desc_to_gpio(desc), 0, value);
1439 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1440 _gpio_set_open_drain_value(desc, value);
1441 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1442 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301443 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001444 chip->set(chip, gpio_chip_hwgpio(desc), value);
1445}
1446
David Brownelld2876d02008-02-04 22:28:20 -08001447/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001448 * gpiod_set_raw_value() - assign a gpio's raw value
1449 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001450 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001451 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001452 * Set the raw value of the GPIO, i.e. the value of its physical line without
1453 * regard for its ACTIVE_LOW status.
1454 *
1455 * This function should be called from contexts where we cannot sleep, and will
1456 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001457 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001458void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001459{
David Brownelld2876d02008-02-04 22:28:20 -08001460 if (!desc)
1461 return;
David Brownelld2876d02008-02-04 22:28:20 -08001462 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001463 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001464 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001465}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001466EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001467
1468/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001469 * gpiod_set_value() - assign a gpio's value
1470 * @desc: gpio whose value will be assigned
1471 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001472 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001473 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1474 * account
1475 *
1476 * This function should be called from contexts where we cannot sleep, and will
1477 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001478 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001479void gpiod_set_value(struct gpio_desc *desc, int value)
1480{
1481 if (!desc)
1482 return;
1483 /* Should be using gpio_set_value_cansleep() */
1484 WARN_ON(desc->chip->can_sleep);
1485 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1486 value = !value;
1487 _gpiod_set_raw_value(desc, value);
1488}
1489EXPORT_SYMBOL_GPL(gpiod_set_value);
1490
1491/**
1492 * gpiod_cansleep() - report whether gpio value access may sleep
1493 * @desc: gpio to check
1494 *
1495 */
1496int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001497{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001498 if (!desc)
1499 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001500 return desc->chip->can_sleep;
1501}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001502EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001503
David Brownell0f6d5042008-10-15 22:03:14 -07001504/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001505 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1506 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001507 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001508 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1509 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001510 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001511int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001512{
1513 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001514 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001515
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001516 if (!desc)
1517 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001518 chip = desc->chip;
1519 offset = gpio_chip_hwgpio(desc);
1520 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1521}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001522EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001523
Linus Walleijd468bf92013-09-24 11:54:38 +02001524/**
1525 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
1526 * @gpio: the GPIO line to lock as used for IRQ
1527 *
1528 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001529 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001530 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001531int gpiod_lock_as_irq(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001532{
Linus Walleijd468bf92013-09-24 11:54:38 +02001533 if (!desc)
1534 return -EINVAL;
1535
1536 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
1537 gpiod_err(desc,
1538 "%s: tried to flag a GPIO set as output for IRQ\n",
1539 __func__);
1540 return -EIO;
1541 }
1542
1543 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
1544 return 0;
1545}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001546EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001547
1548int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
1549{
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001550 return gpiod_lock_as_irq(gpiochip_get_desc(chip, offset));
Linus Walleijd468bf92013-09-24 11:54:38 +02001551}
1552EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
1553
1554/**
1555 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
1556 * @gpio: the GPIO line to unlock from IRQ usage
1557 *
1558 * This is used directly by GPIO drivers that want to indicate
1559 * that a certain GPIO is no longer used exclusively for IRQ.
1560 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001561void gpiod_unlock_as_irq(struct gpio_desc *desc)
Linus Walleijd468bf92013-09-24 11:54:38 +02001562{
1563 if (!desc)
1564 return;
1565
1566 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
1567}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001568EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001569
1570void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
1571{
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001572 return gpiod_unlock_as_irq(gpiochip_get_desc(chip, offset));
Linus Walleijd468bf92013-09-24 11:54:38 +02001573}
1574EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
David Brownelld2876d02008-02-04 22:28:20 -08001575
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001576/**
1577 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1578 * @desc: gpio whose value will be returned
1579 *
1580 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1581 * its ACTIVE_LOW status.
1582 *
1583 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001584 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001585int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001586{
David Brownelld2876d02008-02-04 22:28:20 -08001587 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001588 if (!desc)
1589 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001590 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001591}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001592EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001593
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001594/**
1595 * gpiod_get_value_cansleep() - return a gpio's value
1596 * @desc: gpio whose value will be returned
1597 *
1598 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1599 * account.
1600 *
1601 * This function is to be called from contexts that can sleep.
1602 */
1603int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001604{
David Brownelld2876d02008-02-04 22:28:20 -08001605 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001606
1607 might_sleep_if(extra_checks);
1608 if (!desc)
1609 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001610
1611 value = _gpiod_get_raw_value(desc);
1612 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1613 value = !value;
1614
David Brownelld2876d02008-02-04 22:28:20 -08001615 return value;
1616}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001617EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001618
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001619/**
1620 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1621 * @desc: gpio whose value will be assigned
1622 * @value: value to assign
1623 *
1624 * Set the raw value of the GPIO, i.e. the value of its physical line without
1625 * regard for its ACTIVE_LOW status.
1626 *
1627 * This function is to be called from contexts that can sleep.
1628 */
1629void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001630{
David Brownelld2876d02008-02-04 22:28:20 -08001631 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001632 if (!desc)
1633 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001634 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001635}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001636EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001637
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001638/**
1639 * gpiod_set_value_cansleep() - assign a gpio's value
1640 * @desc: gpio whose value will be assigned
1641 * @value: value to assign
1642 *
1643 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1644 * account
1645 *
1646 * This function is to be called from contexts that can sleep.
1647 */
1648void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001649{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001650 might_sleep_if(extra_checks);
1651 if (!desc)
1652 return;
1653
1654 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1655 value = !value;
1656 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001657}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001658EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001659
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001660/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001661 * gpiod_add_lookup_table() - register GPIO device consumers
1662 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001663 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001664void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001665{
1666 mutex_lock(&gpio_lookup_lock);
1667
Alexandre Courbotad824782013-12-03 12:20:11 +09001668 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001669
1670 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001671}
1672
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001673static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001674 unsigned int idx,
1675 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001676{
Thierry Redingdd34c372014-04-23 17:28:09 +02001677 static const char *suffixes[] = { "gpios", "gpio" };
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001678 char prop_name[32]; /* 32 is max size of property name */
1679 enum of_gpio_flags of_flags;
1680 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001681 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001682
Thierry Redingdd34c372014-04-23 17:28:09 +02001683 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1684 if (con_id)
1685 snprintf(prop_name, 32, "%s-%s", con_id, suffixes[i]);
1686 else
1687 snprintf(prop_name, 32, "%s", suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001688
Thierry Redingdd34c372014-04-23 17:28:09 +02001689 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1690 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001691 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001692 break;
1693 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001694
1695 if (IS_ERR(desc))
1696 return desc;
1697
1698 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001699 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001700
1701 return desc;
1702}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001703
Mika Westerberg81f59e92013-10-10 11:01:09 +03001704static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001705 unsigned int idx,
1706 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001707{
Mika Westerberge01f4402013-10-10 11:01:10 +03001708 struct acpi_gpio_info info;
1709 struct gpio_desc *desc;
1710
1711 desc = acpi_get_gpiod_by_index(dev, idx, &info);
1712 if (IS_ERR(desc))
1713 return desc;
1714
1715 if (info.gpioint && info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001716 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001717
1718 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001719}
1720
Alexandre Courbotad824782013-12-03 12:20:11 +09001721static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1722{
1723 const char *dev_id = dev ? dev_name(dev) : NULL;
1724 struct gpiod_lookup_table *table;
1725
1726 mutex_lock(&gpio_lookup_lock);
1727
1728 list_for_each_entry(table, &gpio_lookup_list, list) {
1729 if (table->dev_id && dev_id) {
1730 /*
1731 * Valid strings on both ends, must be identical to have
1732 * a match
1733 */
1734 if (!strcmp(table->dev_id, dev_id))
1735 goto found;
1736 } else {
1737 /*
1738 * One of the pointers is NULL, so both must be to have
1739 * a match
1740 */
1741 if (dev_id == table->dev_id)
1742 goto found;
1743 }
1744 }
1745 table = NULL;
1746
1747found:
1748 mutex_unlock(&gpio_lookup_lock);
1749 return table;
1750}
1751
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001752static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001753 unsigned int idx,
1754 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001755{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001756 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001757 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001758 struct gpiod_lookup *p;
1759
Alexandre Courbotad824782013-12-03 12:20:11 +09001760 table = gpiod_find_lookup_table(dev);
1761 if (!table)
1762 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001763
Alexandre Courbotad824782013-12-03 12:20:11 +09001764 for (p = &table->table[0]; p->chip_label; p++) {
1765 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001766
Alexandre Courbotad824782013-12-03 12:20:11 +09001767 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001768 if (p->idx != idx)
1769 continue;
1770
Alexandre Courbotad824782013-12-03 12:20:11 +09001771 /* If the lookup entry has a con_id, require exact match */
1772 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1773 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001774
Alexandre Courbotad824782013-12-03 12:20:11 +09001775 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001776
Alexandre Courbotad824782013-12-03 12:20:11 +09001777 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001778 dev_err(dev, "cannot find GPIO chip %s\n",
1779 p->chip_label);
1780 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001781 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001782
Alexandre Courbotad824782013-12-03 12:20:11 +09001783 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001784 dev_err(dev,
1785 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1786 idx, chip->ngpio, chip->label);
1787 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001788 }
1789
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001790 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001791 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001792
1793 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001794 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001795
1796 return desc;
1797}
1798
1799/**
Thierry Reding08791622014-04-25 16:54:22 +02001800 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09001801 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001802 * @con_id: function within the GPIO consumer
1803 *
1804 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001805 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1806 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001807 */
1808struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id)
1809{
1810 return gpiod_get_index(dev, con_id, 0);
1811}
1812EXPORT_SYMBOL_GPL(gpiod_get);
1813
1814/**
Thierry Reding29a1f232014-04-25 17:10:06 +02001815 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1816 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1817 * @con_id: function within the GPIO consumer
1818 *
1819 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1820 * the requested function it will return NULL. This is convenient for drivers
1821 * that need to handle optional GPIOs.
1822 */
1823struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
1824 const char *con_id)
1825{
1826 return gpiod_get_index_optional(dev, con_id, 0);
1827}
1828EXPORT_SYMBOL_GPL(gpiod_get_optional);
1829
1830/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001831 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02001832 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001833 * @con_id: function within the GPIO consumer
1834 * @idx: index of the GPIO to obtain in the consumer
1835 *
1836 * This variant of gpiod_get() allows to access GPIOs other than the first
1837 * defined one for functions that define several GPIOs.
1838 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001839 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1840 * requested function and/or index, or another IS_ERR() code if an error
1841 * occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001842 */
1843struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
1844 const char *con_id,
1845 unsigned int idx)
1846{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001847 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001848 int status;
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001849 enum gpio_lookup_flags flags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001850
1851 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
1852
1853 /* Using device tree? */
1854 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
1855 dev_dbg(dev, "using device tree for GPIO lookup\n");
1856 desc = of_find_gpio(dev, con_id, idx, &flags);
Mika Westerberg81f59e92013-10-10 11:01:09 +03001857 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
1858 dev_dbg(dev, "using ACPI for GPIO lookup\n");
1859 desc = acpi_find_gpio(dev, con_id, idx, &flags);
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001860 }
1861
1862 /*
1863 * Either we are not using DT or ACPI, or their lookup did not return
1864 * a result. In that case, use platform lookup as a fallback.
1865 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001866 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001867 dev_dbg(dev, "using lookup tables for GPIO lookup");
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001868 desc = gpiod_find(dev, con_id, idx, &flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001869 }
1870
1871 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02001872 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001873 return desc;
1874 }
1875
1876 status = gpiod_request(desc, con_id);
1877
1878 if (status < 0)
1879 return ERR_PTR(status);
1880
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001881 if (flags & GPIO_ACTIVE_LOW)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001882 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001883 if (flags & GPIO_OPEN_DRAIN)
1884 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1885 if (flags & GPIO_OPEN_SOURCE)
1886 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001887
1888 return desc;
1889}
1890EXPORT_SYMBOL_GPL(gpiod_get_index);
1891
1892/**
Thierry Reding29a1f232014-04-25 17:10:06 +02001893 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
1894 * function
1895 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1896 * @con_id: function within the GPIO consumer
1897 * @index: index of the GPIO to obtain in the consumer
1898 *
1899 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
1900 * specified index was assigned to the requested function it will return NULL.
1901 * This is convenient for drivers that need to handle optional GPIOs.
1902 */
1903struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
1904 const char *con_id,
1905 unsigned int index)
1906{
1907 struct gpio_desc *desc;
1908
1909 desc = gpiod_get_index(dev, con_id, index);
1910 if (IS_ERR(desc)) {
1911 if (PTR_ERR(desc) == -ENOENT)
1912 return NULL;
1913 }
1914
1915 return desc;
1916}
1917EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
1918
1919/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001920 * gpiod_put - dispose of a GPIO descriptor
1921 * @desc: GPIO descriptor to dispose of
1922 *
1923 * No descriptor can be used after gpiod_put() has been called on it.
1924 */
1925void gpiod_put(struct gpio_desc *desc)
1926{
1927 gpiod_free(desc);
1928}
1929EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08001930
David Brownelld2876d02008-02-04 22:28:20 -08001931#ifdef CONFIG_DEBUG_FS
1932
1933static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1934{
1935 unsigned i;
1936 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001937 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08001938 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02001939 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08001940
1941 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1942 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1943 continue;
1944
Alexandre Courbot372e7222013-02-03 01:29:29 +09001945 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08001946 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001947 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
1948 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08001949 gpio, gdesc->label,
1950 is_out ? "out" : "in ",
1951 chip->get
1952 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02001953 : "? ",
1954 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08001955 seq_printf(s, "\n");
1956 }
1957}
1958
Thierry Redingf9c4a312012-04-12 13:26:01 +02001959static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08001960{
Grant Likely362432a2013-02-09 09:41:49 +00001961 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001962 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001963 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001964
1965 s->private = "";
1966
Grant Likely362432a2013-02-09 09:41:49 +00001967 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001968 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00001969 if (index-- == 0) {
1970 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001971 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00001972 }
1973 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001974
1975 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001976}
1977
1978static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
1979{
Grant Likely362432a2013-02-09 09:41:49 +00001980 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001981 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001982 void *ret = NULL;
1983
Grant Likely362432a2013-02-09 09:41:49 +00001984 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001985 if (list_is_last(&chip->list, &gpio_chips))
1986 ret = NULL;
1987 else
1988 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00001989 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02001990
1991 s->private = "\n";
1992 ++*pos;
1993
1994 return ret;
1995}
1996
1997static void gpiolib_seq_stop(struct seq_file *s, void *v)
1998{
1999}
2000
2001static int gpiolib_seq_show(struct seq_file *s, void *v)
2002{
2003 struct gpio_chip *chip = v;
2004 struct device *dev;
2005
2006 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2007 chip->base, chip->base + chip->ngpio - 1);
2008 dev = chip->dev;
2009 if (dev)
2010 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2011 dev_name(dev));
2012 if (chip->label)
2013 seq_printf(s, ", %s", chip->label);
2014 if (chip->can_sleep)
2015 seq_printf(s, ", can sleep");
2016 seq_printf(s, ":\n");
2017
2018 if (chip->dbg_show)
2019 chip->dbg_show(s, chip);
2020 else
2021 gpiolib_dbg_show(s, chip);
2022
David Brownelld2876d02008-02-04 22:28:20 -08002023 return 0;
2024}
2025
Thierry Redingf9c4a312012-04-12 13:26:01 +02002026static const struct seq_operations gpiolib_seq_ops = {
2027 .start = gpiolib_seq_start,
2028 .next = gpiolib_seq_next,
2029 .stop = gpiolib_seq_stop,
2030 .show = gpiolib_seq_show,
2031};
2032
David Brownelld2876d02008-02-04 22:28:20 -08002033static int gpiolib_open(struct inode *inode, struct file *file)
2034{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002035 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002036}
2037
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002038static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002039 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002040 .open = gpiolib_open,
2041 .read = seq_read,
2042 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002043 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002044};
2045
2046static int __init gpiolib_debugfs_init(void)
2047{
2048 /* /sys/kernel/debug/gpio */
2049 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2050 NULL, NULL, &gpiolib_operations);
2051 return 0;
2052}
2053subsys_initcall(gpiolib_debugfs_init);
2054
2055#endif /* DEBUG_FS */