blob: c81bda0ec2cf1ed63665ccf52a24e30926159b8b [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
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>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
David Brownelld2876d02008-02-04 22:28:20 -080018
Mika Westerberg664e3e52014-01-08 12:40:54 +020019#include "gpiolib.h"
20
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060021#define CREATE_TRACE_POINTS
22#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080023
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070024/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080025 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070026 * The GPIO programming interface allows for inlining speed-critical
27 * get/set operations for common cases, so that access to SOC-integrated
28 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080029 */
30
31
32/* When debugging, extend minimal trust to callers and platform code.
33 * Also emit diagnostic messages that may help initial bringup, when
34 * board setup or driver bugs are most common.
35 *
36 * Otherwise, minimize overhead in what may be bitbanging codepaths.
37 */
38#ifdef DEBUG
39#define extra_checks 1
40#else
41#define extra_checks 0
42#endif
43
44/* gpio_lock prevents conflicts during gpio_desc[] table updates.
45 * While any GPIO is requested, its gpio_chip is not removable;
46 * each GPIO's "requested" flag serves as a lock and refcount.
47 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090048DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080049
David Brownelld2876d02008-02-04 22:28:20 -080050static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
51
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +090052#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
53
Alexandre Courbotbae48da2013-10-17 10:21:38 -070054static DEFINE_MUTEX(gpio_lookup_lock);
55static LIST_HEAD(gpio_lookup_list);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090056LIST_HEAD(gpio_chips);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +020057
David Brownelld2876d02008-02-04 22:28:20 -080058static inline void desc_set_label(struct gpio_desc *d, const char *label)
59{
David Brownelld2876d02008-02-04 22:28:20 -080060 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080061}
62
Alexandre Courbot372e7222013-02-03 01:29:29 +090063/**
64 * Convert a GPIO number to its descriptor
65 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070066struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090067{
68 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
69 return NULL;
70 else
71 return &gpio_desc[gpio];
72}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070073EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090074
75/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090076 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +020077 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090078struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
79 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +020080{
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090081 if (hwnum >= chip->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +090082 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +020083
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090084 return &chip->desc[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +020085}
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
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070099/**
100 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
101 * @desc: descriptor to return the chip of
102 */
103struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900104{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900105 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900106}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700107EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800108
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700109/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
110static int gpiochip_find_base(int ngpio)
111{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900112 struct gpio_chip *chip;
113 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700114
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900115 list_for_each_entry_reverse(chip, &gpio_chips, list) {
116 /* found a free space? */
117 if (chip->base + chip->ngpio <= base)
118 break;
119 else
120 /* nope, check the space right before the chip */
121 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700122 }
123
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900124 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700125 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900126 return base;
127 } else {
128 pr_err("%s: cannot find free range\n", __func__);
129 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700130 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700131}
132
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700133/**
134 * gpiod_get_direction - return the current direction of a GPIO
135 * @desc: GPIO to get the direction of
136 *
137 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
138 *
139 * This function may sleep if gpiod_cansleep() is true.
140 */
141int gpiod_get_direction(const struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300142{
143 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900144 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300145 int status = -EINVAL;
146
Alexandre Courbot372e7222013-02-03 01:29:29 +0900147 chip = gpiod_to_chip(desc);
148 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300149
150 if (!chip->get_direction)
151 return status;
152
Alexandre Courbot372e7222013-02-03 01:29:29 +0900153 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300154 if (status > 0) {
155 /* GPIOF_DIR_IN, or other positive */
156 status = 1;
Alexandre Courbotdef63432013-02-15 14:46:15 +0900157 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
158 * so it does not affect constness per se */
159 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300160 }
161 if (status == 0) {
162 /* GPIOF_DIR_OUT */
Alexandre Courbotdef63432013-02-15 14:46:15 +0900163 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300164 }
165 return status;
166}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700167EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300168
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900169/*
170 * Add a new chip to the global chips list, keeping the list of chips sorted
171 * by base order.
172 *
173 * Return -EBUSY if the new chip overlaps with some other chip's integer
174 * space.
175 */
176static int gpiochip_add_to_list(struct gpio_chip *chip)
177{
178 struct list_head *pos = &gpio_chips;
179 struct gpio_chip *_chip;
180 int err = 0;
181
182 /* find where to insert our chip */
183 list_for_each(pos, &gpio_chips) {
184 _chip = list_entry(pos, struct gpio_chip, list);
185 /* shall we insert before _chip? */
186 if (_chip->base >= chip->base + chip->ngpio)
187 break;
188 }
189
190 /* are we stepping on the chip right before? */
191 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
192 _chip = list_entry(pos->prev, struct gpio_chip, list);
193 if (_chip->base + _chip->ngpio > chip->base) {
194 dev_err(chip->dev,
195 "GPIO integer space overlap, cannot add chip\n");
196 err = -EBUSY;
197 }
198 }
199
200 if (!err)
201 list_add_tail(&chip->list, pos);
202
203 return err;
204}
205
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700206/**
David Brownelld2876d02008-02-04 22:28:20 -0800207 * gpiochip_add() - register a gpio_chip
208 * @chip: the chip to register, with chip->base initialized
209 * Context: potentially before irqs or kmalloc will work
210 *
211 * Returns a negative errno if the chip can't be registered, such as
212 * because the chip->base is invalid or already associated with a
213 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700214 *
David Brownelld8f388d82008-07-25 01:46:07 -0700215 * When gpiochip_add() is called very early during boot, so that GPIOs
216 * can be freely used, the chip->dev device must be registered before
217 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
218 * for GPIOs will fail rudely.
219 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700220 * If chip->base is negative, this requests dynamic assignment of
221 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800222 */
223int gpiochip_add(struct gpio_chip *chip)
224{
225 unsigned long flags;
226 int status = 0;
227 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700228 int base = chip->base;
David Brownelld2876d02008-02-04 22:28:20 -0800229
Trent Piephobff5fda2008-05-23 13:04:44 -0700230 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700231 && base >= 0) {
David Brownelld2876d02008-02-04 22:28:20 -0800232 status = -EINVAL;
233 goto fail;
234 }
235
236 spin_lock_irqsave(&gpio_lock, flags);
237
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700238 if (base < 0) {
239 base = gpiochip_find_base(chip->ngpio);
240 if (base < 0) {
241 status = base;
David Brownelld8f388d82008-07-25 01:46:07 -0700242 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700243 }
244 chip->base = base;
245 }
246
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900247 status = gpiochip_add_to_list(chip);
248
David Brownelld2876d02008-02-04 22:28:20 -0800249 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900250 chip->desc = &gpio_desc[chip->base];
251
252 for (id = 0; id < chip->ngpio; id++) {
253 struct gpio_desc *desc = &chip->desc[id];
254 desc->chip = chip;
David Brownelld8f388d82008-07-25 01:46:07 -0700255
256 /* REVISIT: most hardware initializes GPIOs as
257 * inputs (often with pullups enabled) so power
258 * usage is minimized. Linux code should set the
259 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +0300260 * and in case chip->get_direction is not set,
David Brownelld8f388d82008-07-25 01:46:07 -0700261 * we may expose the wrong direction in sysfs.
262 */
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900263 desc->flags = !chip->direction_input
David Brownelld8f388d82008-07-25 01:46:07 -0700264 ? (1 << FLAG_IS_OUT)
265 : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800266 }
267 }
268
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800269 spin_unlock_irqrestore(&gpio_lock, flags);
270
Johan Hovolde1c76d62015-01-12 17:12:24 +0100271 if (status)
272 goto fail;
273
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530274#ifdef CONFIG_PINCTRL
275 INIT_LIST_HEAD(&chip->pin_ranges);
276#endif
277
Anton Vorontsov391c9702010-06-08 07:48:17 -0600278 of_gpiochip_add(chip);
Mika Westerberg664e3e52014-01-08 12:40:54 +0200279 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600280
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600281 status = gpiochip_export(chip);
Johan Hovolde1c76d62015-01-12 17:12:24 +0100282 if (status) {
283 acpi_gpiochip_remove(chip);
284 of_gpiochip_remove(chip);
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600285 goto fail;
Johan Hovolde1c76d62015-01-12 17:12:24 +0100286 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600287
Andy Shevchenko7589e592013-12-05 11:26:23 +0200288 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700289 chip->base, chip->base + chip->ngpio - 1,
290 chip->label ? : "generic");
291
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600292 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800293
294unlock:
295 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld2876d02008-02-04 22:28:20 -0800296fail:
297 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200298 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600299 chip->base, chip->base + chip->ngpio - 1,
300 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800301 return status;
302}
303EXPORT_SYMBOL_GPL(gpiochip_add);
304
Linus Walleij14250522014-03-25 10:40:18 +0100305/* Forward-declaration */
306static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
307
David Brownelld2876d02008-02-04 22:28:20 -0800308/**
309 * gpiochip_remove() - unregister a gpio_chip
310 * @chip: the chip to unregister
311 *
312 * A gpio_chip with any GPIOs still requested may not be removed.
313 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200314void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800315{
316 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800317 unsigned id;
318
Johan Hovold2bc26f42015-01-12 17:12:27 +0100319 gpiochip_irqchip_remove(chip);
320
Mika Westerberg6072b9dc2014-03-10 14:54:53 +0200321 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100322 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600323 of_gpiochip_remove(chip);
324
Johan Hovold72d8d4d2015-01-12 17:12:28 +0100325 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900326 for (id = 0; id < chip->ngpio; id++) {
abdoulaye berthee1db1702014-07-05 18:28:50 +0200327 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags))
328 dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
David Brownelld2876d02008-02-04 22:28:20 -0800329 }
abdoulaye berthee1db1702014-07-05 18:28:50 +0200330 for (id = 0; id < chip->ngpio; id++)
331 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900332
abdoulaye berthee1db1702014-07-05 18:28:50 +0200333 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800334 spin_unlock_irqrestore(&gpio_lock, flags);
abdoulaye berthee1db1702014-07-05 18:28:50 +0200335 gpiochip_unexport(chip);
David Brownelld2876d02008-02-04 22:28:20 -0800336}
337EXPORT_SYMBOL_GPL(gpiochip_remove);
338
Grant Likely594fa262010-06-08 07:48:16 -0600339/**
340 * gpiochip_find() - iterator for locating a specific gpio_chip
341 * @data: data to pass to match function
342 * @callback: Callback function to check gpio_chip
343 *
344 * Similar to bus_find_device. It returns a reference to a gpio_chip as
345 * determined by a user supplied @match callback. The callback should return
346 * 0 if the device doesn't match and non-zero if it does. If the callback is
347 * non-zero, this function will return to the caller and not iterate over any
348 * more gpio_chips.
349 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600350struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700351 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600352 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600353{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900354 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600355 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600356
357 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900358 list_for_each_entry(chip, &gpio_chips, list)
359 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600360 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900361
362 /* No match? */
363 if (&chip->list == &gpio_chips)
364 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600365 spin_unlock_irqrestore(&gpio_lock, flags);
366
367 return chip;
368}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600369EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800370
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900371static int gpiochip_match_name(struct gpio_chip *chip, void *data)
372{
373 const char *name = data;
374
375 return !strcmp(chip->label, name);
376}
377
378static struct gpio_chip *find_chip_by_name(const char *name)
379{
380 return gpiochip_find((void *)name, gpiochip_match_name);
381}
382
Linus Walleij14250522014-03-25 10:40:18 +0100383#ifdef CONFIG_GPIOLIB_IRQCHIP
384
385/*
386 * The following is irqchip helper code for gpiochips.
387 */
388
389/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200390 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
391 * @gpiochip: the gpiochip to set the irqchip chain to
392 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100393 * @parent_irq: the irq number corresponding to the parent IRQ for this
394 * chained irqchip
395 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200396 * coming out of the gpiochip. If the interrupt is nested rather than
397 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100398 */
399void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
400 struct irq_chip *irqchip,
401 int parent_irq,
402 irq_flow_handler_t parent_handler)
403{
Linus Walleij83141a72014-09-26 13:50:12 +0200404 unsigned int offset;
405
Linus Walleij83141a72014-09-26 13:50:12 +0200406 if (!gpiochip->irqdomain) {
407 chip_err(gpiochip, "called %s before setting up irqchip\n",
408 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200409 return;
410 }
411
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200412 if (parent_handler) {
413 if (gpiochip->can_sleep) {
414 chip_err(gpiochip,
415 "you cannot have chained interrupts on a "
416 "chip that may sleep\n");
417 return;
418 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200419 /*
420 * The parent irqchip is already using the chip_data for this
421 * irqchip, so our callbacks simply use the handler_data.
422 */
423 irq_set_handler_data(parent_irq, gpiochip);
Linus Torvaldsea584592014-10-09 14:58:15 -0400424 irq_set_chained_handler(parent_irq, parent_handler);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200425 }
Linus Walleij83141a72014-09-26 13:50:12 +0200426
427 /* Set the parent IRQ for all affected IRQs */
428 for (offset = 0; offset < gpiochip->ngpio; offset++)
429 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
430 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100431}
432EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
433
Linus Walleije45d1c82014-04-22 14:01:46 +0200434/*
435 * This lock class tells lockdep that GPIO irqs are in a different
436 * category than their parents, so it won't report false recursion.
437 */
438static struct lock_class_key gpiochip_irq_lock_class;
439
Linus Walleij14250522014-03-25 10:40:18 +0100440/**
441 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
442 * @d: the irqdomain used by this irqchip
443 * @irq: the global irq number used by this GPIO irqchip irq
444 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
445 *
446 * This function will set up the mapping for a certain IRQ line on a
447 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
448 * stored inside the gpiochip.
449 */
450static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
451 irq_hw_number_t hwirq)
452{
453 struct gpio_chip *chip = d->host_data;
454
Linus Walleij14250522014-03-25 10:40:18 +0100455 irq_set_chip_data(irq, chip);
Linus Walleije45d1c82014-04-22 14:01:46 +0200456 irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
Linus Walleij7633fb92014-04-09 13:20:38 +0200457 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200458 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300459 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200460 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100461#ifdef CONFIG_ARM
462 set_irq_flags(irq, IRQF_VALID);
463#else
464 irq_set_noprobe(irq);
465#endif
Linus Walleij1333b902014-04-23 16:45:12 +0200466 /*
467 * No set-up of the hardware will happen if IRQ_TYPE_NONE
468 * is passed as default type.
469 */
470 if (chip->irq_default_type != IRQ_TYPE_NONE)
471 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100472
473 return 0;
474}
475
Linus Walleijc3626fd2014-03-28 20:42:01 +0100476static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
477{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200478 struct gpio_chip *chip = d->host_data;
479
Linus Walleijc3626fd2014-03-28 20:42:01 +0100480#ifdef CONFIG_ARM
481 set_irq_flags(irq, 0);
482#endif
Linus Walleij1c8732b2014-04-09 13:34:39 +0200483 if (chip->can_sleep)
484 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100485 irq_set_chip_and_handler(irq, NULL, NULL);
486 irq_set_chip_data(irq, NULL);
487}
488
Linus Walleij14250522014-03-25 10:40:18 +0100489static const struct irq_domain_ops gpiochip_domain_ops = {
490 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100491 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100492 /* Virtually all GPIO irqchips are twocell:ed */
493 .xlate = irq_domain_xlate_twocell,
494};
495
496static int gpiochip_irq_reqres(struct irq_data *d)
497{
498 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
499
500 if (gpio_lock_as_irq(chip, d->hwirq)) {
501 chip_err(chip,
502 "unable to lock HW IRQ %lu for IRQ\n",
503 d->hwirq);
504 return -EINVAL;
505 }
506 return 0;
507}
508
509static void gpiochip_irq_relres(struct irq_data *d)
510{
511 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
512
513 gpio_unlock_as_irq(chip, d->hwirq);
514}
515
516static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
517{
518 return irq_find_mapping(chip->irqdomain, offset);
519}
520
521/**
522 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
523 * @gpiochip: the gpiochip to remove the irqchip from
524 *
525 * This is called only from gpiochip_remove()
526 */
527static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
528{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100529 unsigned int offset;
530
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300531 acpi_gpiochip_free_interrupts(gpiochip);
532
Linus Walleijc3626fd2014-03-28 20:42:01 +0100533 /* Remove all IRQ mappings and delete the domain */
534 if (gpiochip->irqdomain) {
535 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300536 irq_dispose_mapping(
537 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100538 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100539 }
Linus Walleij14250522014-03-25 10:40:18 +0100540
541 if (gpiochip->irqchip) {
542 gpiochip->irqchip->irq_request_resources = NULL;
543 gpiochip->irqchip->irq_release_resources = NULL;
544 gpiochip->irqchip = NULL;
545 }
546}
547
548/**
549 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
550 * @gpiochip: the gpiochip to add the irqchip to
551 * @irqchip: the irqchip to add to the gpiochip
552 * @first_irq: if not dynamically assigned, the base (first) IRQ to
553 * allocate gpiochip irqs from
554 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200555 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
556 * to have the core avoid setting up any default type in the hardware.
Linus Walleij14250522014-03-25 10:40:18 +0100557 *
558 * This function closely associates a certain irqchip with a certain
559 * gpiochip, providing an irq domain to translate the local IRQs to
560 * global irqs in the gpiolib core, and making sure that the gpiochip
561 * is passed as chip data to all related functions. Driver callbacks
562 * need to use container_of() to get their local state containers back
563 * from the gpiochip passed as chip data. An irqdomain will be stored
564 * in the gpiochip that shall be used by the driver to handle IRQ number
565 * translation. The gpiochip will need to be initialized and registered
566 * before calling this function.
567 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100568 * This function will handle two cell:ed simple IRQs and assumes all
569 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100570 * need to be open coded.
571 */
572int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
573 struct irq_chip *irqchip,
574 unsigned int first_irq,
575 irq_flow_handler_t handler,
576 unsigned int type)
577{
578 struct device_node *of_node;
579 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100580 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100581
582 if (!gpiochip || !irqchip)
583 return -EINVAL;
584
585 if (!gpiochip->dev) {
586 pr_err("missing gpiochip .dev parent pointer\n");
587 return -EINVAL;
588 }
589 of_node = gpiochip->dev->of_node;
590#ifdef CONFIG_OF_GPIO
591 /*
592 * If the gpiochip has an assigned OF node this takes precendence
593 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
594 */
595 if (gpiochip->of_node)
596 of_node = gpiochip->of_node;
597#endif
598 gpiochip->irqchip = irqchip;
599 gpiochip->irq_handler = handler;
600 gpiochip->irq_default_type = type;
601 gpiochip->to_irq = gpiochip_to_irq;
602 gpiochip->irqdomain = irq_domain_add_simple(of_node,
603 gpiochip->ngpio, first_irq,
604 &gpiochip_domain_ops, gpiochip);
605 if (!gpiochip->irqdomain) {
606 gpiochip->irqchip = NULL;
607 return -EINVAL;
608 }
609 irqchip->irq_request_resources = gpiochip_irq_reqres;
610 irqchip->irq_release_resources = gpiochip_irq_relres;
611
612 /*
613 * Prepare the mapping since the irqchip shall be orthogonal to
614 * any gpiochip calls. If the first_irq was zero, this is
615 * necessary to allocate descriptors for all IRQs.
616 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100617 for (offset = 0; offset < gpiochip->ngpio; offset++) {
618 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
619 if (offset == 0)
620 /*
621 * Store the base into the gpiochip to be used when
622 * unmapping the irqs.
623 */
624 gpiochip->irq_base = irq_base;
625 }
Linus Walleij14250522014-03-25 10:40:18 +0100626
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300627 acpi_gpiochip_request_interrupts(gpiochip);
628
Linus Walleij14250522014-03-25 10:40:18 +0100629 return 0;
630}
631EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
632
633#else /* CONFIG_GPIOLIB_IRQCHIP */
634
635static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
636
637#endif /* CONFIG_GPIOLIB_IRQCHIP */
638
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530639#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100640
Linus Walleij3f0f8672012-11-20 12:40:15 +0100641/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200642 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
643 * @chip: the gpiochip to add the range for
644 * @pinctrl: the dev_name() of the pin controller to map to
645 * @gpio_offset: the start offset in the current gpio_chip number space
646 * @pin_group: name of the pin group inside the pin controller
647 */
648int gpiochip_add_pingroup_range(struct gpio_chip *chip,
649 struct pinctrl_dev *pctldev,
650 unsigned int gpio_offset, const char *pin_group)
651{
652 struct gpio_pin_range *pin_range;
653 int ret;
654
655 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
656 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200657 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200658 return -ENOMEM;
659 }
660
661 /* Use local offset as range ID */
662 pin_range->range.id = gpio_offset;
663 pin_range->range.gc = chip;
664 pin_range->range.name = chip->label;
665 pin_range->range.base = chip->base + gpio_offset;
666 pin_range->pctldev = pctldev;
667
668 ret = pinctrl_get_group_pins(pctldev, pin_group,
669 &pin_range->range.pins,
670 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100671 if (ret < 0) {
672 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200673 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100674 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200675
676 pinctrl_add_gpio_range(pctldev, &pin_range->range);
677
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200678 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
679 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200680 pinctrl_dev_get_devname(pctldev), pin_group);
681
682 list_add_tail(&pin_range->node, &chip->pin_ranges);
683
684 return 0;
685}
686EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
687
688/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100689 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
690 * @chip: the gpiochip to add the range for
691 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100692 * @gpio_offset: the start offset in the current gpio_chip number space
693 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100694 * @npins: the number of pins from the offset of each pin space (GPIO and
695 * pin controller) to accumulate in this range
696 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100697int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100698 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100699 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530700{
701 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800702 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530703
Linus Walleij3f0f8672012-11-20 12:40:15 +0100704 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530705 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200706 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100707 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530708 }
709
Linus Walleij3f0f8672012-11-20 12:40:15 +0100710 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100711 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100712 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530713 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100714 pin_range->range.base = chip->base + gpio_offset;
715 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530716 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100717 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530718 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100719 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800720 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200721 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100722 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800723 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100724 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200725 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
726 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100727 pinctl_name,
728 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530729
730 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100731
732 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530733}
Linus Walleij165adc92012-11-06 14:49:39 +0100734EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530735
Linus Walleij3f0f8672012-11-20 12:40:15 +0100736/**
737 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
738 * @chip: the chip to remove all the mappings for
739 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530740void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
741{
742 struct gpio_pin_range *pin_range, *tmp;
743
744 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
745 list_del(&pin_range->node);
746 pinctrl_remove_gpio_range(pin_range->pctldev,
747 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100748 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530749 }
750}
Linus Walleij165adc92012-11-06 14:49:39 +0100751EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
752
753#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530754
David Brownelld2876d02008-02-04 22:28:20 -0800755/* These "optional" allocation calls help prevent drivers from stomping
756 * on each other, and help provide better diagnostics in debugfs.
757 * They're called even less than the "set direction" calls.
758 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200759static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800760{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200761 struct gpio_chip *chip = desc->chip;
762 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800763 unsigned long flags;
764
765 spin_lock_irqsave(&gpio_lock, flags);
766
David Brownelld2876d02008-02-04 22:28:20 -0800767 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700768 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800769 */
770
771 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
772 desc_set_label(desc, label ? : "?");
773 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700774 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800775 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800776 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700777 }
778
779 if (chip->request) {
780 /* chip->request may sleep */
781 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900782 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700783 spin_lock_irqsave(&gpio_lock, flags);
784
785 if (status < 0) {
786 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700787 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300788 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700789 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700790 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300791 if (chip->get_direction) {
792 /* chip->get_direction may sleep */
793 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900794 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300795 spin_lock_irqsave(&gpio_lock, flags);
796 }
David Brownelld2876d02008-02-04 22:28:20 -0800797done:
Mika Westerberg77c2d792014-03-10 14:54:50 +0200798 spin_unlock_irqrestore(&gpio_lock, flags);
799 return status;
800}
801
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900802int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200803{
804 int status = -EPROBE_DEFER;
805 struct gpio_chip *chip;
806
807 if (!desc) {
808 pr_warn("%s: invalid GPIO\n", __func__);
809 return -EINVAL;
810 }
811
812 chip = desc->chip;
813 if (!chip)
814 goto done;
815
816 if (try_module_get(chip->owner)) {
817 status = __gpiod_request(desc, label);
818 if (status < 0)
819 module_put(chip->owner);
820 }
821
822done:
David Brownelld2876d02008-02-04 22:28:20 -0800823 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200824 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200825
David Brownelld2876d02008-02-04 22:28:20 -0800826 return status;
827}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900828
Mika Westerberg77c2d792014-03-10 14:54:50 +0200829static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800830{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200831 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -0800832 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -0700833 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800834
Uwe Kleine-König3d599d12008-10-15 22:03:12 -0700835 might_sleep();
836
Alexandre Courbot372e7222013-02-03 01:29:29 +0900837 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700838
David Brownelld2876d02008-02-04 22:28:20 -0800839 spin_lock_irqsave(&gpio_lock, flags);
840
David Brownell35e8bb52008-10-15 22:03:16 -0700841 chip = desc->chip;
842 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
843 if (chip->free) {
844 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -0700845 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900846 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700847 spin_lock_irqsave(&gpio_lock, flags);
848 }
David Brownelld2876d02008-02-04 22:28:20 -0800849 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -0800850 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -0700851 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530852 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +0530853 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200854 ret = true;
855 }
David Brownelld2876d02008-02-04 22:28:20 -0800856
857 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200858 return ret;
859}
860
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900861void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200862{
863 if (desc && __gpiod_free(desc))
864 module_put(desc->chip->owner);
865 else
866 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -0800867}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900868
David Brownelld2876d02008-02-04 22:28:20 -0800869/**
870 * gpiochip_is_requested - return string iff signal was requested
871 * @chip: controller managing the signal
872 * @offset: of signal within controller's 0..(ngpio - 1) range
873 *
874 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +0900875 * The string returned is the label passed to gpio_request(); if none has been
876 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -0800877 *
878 * This function is for use by GPIO controller drivers. The label can
879 * help with diagnostics, and knowing that the signal is used as a GPIO
880 * can help avoid accidentally multiplexing it to another controller.
881 */
882const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
883{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900884 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800885
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900886 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -0800887 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900888
889 desc = &chip->desc[offset];
890
Alexandre Courbot372e7222013-02-03 01:29:29 +0900891 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -0800892 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900893 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -0800894}
895EXPORT_SYMBOL_GPL(gpiochip_is_requested);
896
Mika Westerberg77c2d792014-03-10 14:54:50 +0200897/**
898 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
899 * @desc: GPIO descriptor to request
900 * @label: label for the GPIO
901 *
902 * Function allows GPIO chip drivers to request and use their own GPIO
903 * descriptors via gpiolib API. Difference to gpiod_request() is that this
904 * function will not increase reference count of the GPIO chip module. This
905 * allows the GPIO chip module to be unloaded as needed (we assume that the
906 * GPIO chip driver handles freeing the GPIOs it has requested).
907 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700908struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
909 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200910{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700911 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
912 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200913
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700914 if (IS_ERR(desc)) {
915 chip_err(chip, "failed to get GPIO descriptor\n");
916 return desc;
917 }
918
919 err = __gpiod_request(desc, label);
920 if (err < 0)
921 return ERR_PTR(err);
922
923 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200924}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700925EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200926
927/**
928 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
929 * @desc: GPIO descriptor to free
930 *
931 * Function frees the given GPIO requested previously with
932 * gpiochip_request_own_desc().
933 */
934void gpiochip_free_own_desc(struct gpio_desc *desc)
935{
936 if (desc)
937 __gpiod_free(desc);
938}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700939EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -0800940
941/* Drivers MUST set GPIO direction before making get/set calls. In
942 * some cases this is done in early boot, before IRQs are enabled.
943 *
944 * As a rule these aren't called more than once (except for drivers
945 * using the open-drain emulation idiom) so these are natural places
946 * to accumulate extra debugging checks. Note that we can't (yet)
947 * rely on gpio_request() having been called beforehand.
948 */
949
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700950/**
951 * gpiod_direction_input - set the GPIO direction to input
952 * @desc: GPIO to set to input
953 *
954 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
955 * be called safely on it.
956 *
957 * Return 0 in case of success, else an error code.
958 */
959int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800960{
David Brownelld2876d02008-02-04 22:28:20 -0800961 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800962 int status = -EINVAL;
963
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200964 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900965 pr_warn("%s: invalid GPIO\n", __func__);
966 return -EINVAL;
967 }
968
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200969 chip = desc->chip;
970 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +0100971 gpiod_warn(desc,
972 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +0200973 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200974 return -EIO;
975 }
976
Alexandre Courbotd82da792014-07-22 16:17:43 +0900977 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -0800978 if (status == 0)
979 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -0600980
Alexandre Courbot372e7222013-02-03 01:29:29 +0900981 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +0900982
David Brownelld2876d02008-02-04 22:28:20 -0800983 return status;
984}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700985EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900986
Philipp Zabelef70bbe2014-01-07 12:34:11 +0100987static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -0800988{
David Brownelld2876d02008-02-04 22:28:20 -0800989 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800990 int status = -EINVAL;
991
Linus Walleijd468bf92013-09-24 11:54:38 +0200992 /* GPIOs used for IRQs shall not be set as output */
993 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
994 gpiod_err(desc,
995 "%s: tried to set a GPIO tied to an IRQ as output\n",
996 __func__);
997 return -EIO;
998 }
999
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301000 /* Open drain pin should not be driven to 1 */
1001 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001002 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301003
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301004 /* Open source pin should not be driven to 0 */
1005 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001006 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301007
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001008 chip = desc->chip;
1009 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001010 gpiod_warn(desc,
1011 "%s: missing set() or direction_output() operations\n",
1012 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001013 return -EIO;
1014 }
1015
Alexandre Courbotd82da792014-07-22 16:17:43 +09001016 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001017 if (status == 0)
1018 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001019 trace_gpio_value(desc_to_gpio(desc), 0, value);
1020 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001021 return status;
1022}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001023
1024/**
1025 * gpiod_direction_output_raw - set the GPIO direction to output
1026 * @desc: GPIO to set to output
1027 * @value: initial output value of the GPIO
1028 *
1029 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1030 * be called safely on it. The initial value of the output must be specified
1031 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1032 *
1033 * Return 0 in case of success, else an error code.
1034 */
1035int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1036{
1037 if (!desc || !desc->chip) {
1038 pr_warn("%s: invalid GPIO\n", __func__);
1039 return -EINVAL;
1040 }
1041 return _gpiod_direction_output_raw(desc, value);
1042}
1043EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1044
1045/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301046 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001047 * @desc: GPIO to set to output
1048 * @value: initial output value of the GPIO
1049 *
1050 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1051 * be called safely on it. The initial value of the output must be specified
1052 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1053 * account.
1054 *
1055 * Return 0 in case of success, else an error code.
1056 */
1057int gpiod_direction_output(struct gpio_desc *desc, int value)
1058{
1059 if (!desc || !desc->chip) {
1060 pr_warn("%s: invalid GPIO\n", __func__);
1061 return -EINVAL;
1062 }
1063 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1064 value = !value;
1065 return _gpiod_direction_output_raw(desc, value);
1066}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001067EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001068
Felipe Balbic4b5be92010-05-26 14:42:23 -07001069/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001070 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001071 * @gpio: the gpio to set debounce time
1072 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001073 *
1074 * returns -ENOTSUPP if the controller does not support setting
1075 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001076 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001077int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001078{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001079 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001080
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001081 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001082 pr_warn("%s: invalid GPIO\n", __func__);
1083 return -EINVAL;
1084 }
1085
Felipe Balbic4b5be92010-05-26 14:42:23 -07001086 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001087 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001088 gpiod_dbg(desc,
1089 "%s: missing set() or set_debounce() operations\n",
1090 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001091 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001092 }
1093
Alexandre Courbotd82da792014-07-22 16:17:43 +09001094 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001095}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001096EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001097
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001098/**
1099 * gpiod_is_active_low - test whether a GPIO is active-low or not
1100 * @desc: the gpio descriptor to test
1101 *
1102 * Returns 1 if the GPIO is active-low, 0 otherwise.
1103 */
1104int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001105{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001106 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001107}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001108EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001109
1110/* I/O calls are only valid after configuration completed; the relevant
1111 * "is this a valid GPIO" error checks should already have been done.
1112 *
1113 * "Get" operations are often inlinable as reading a pin value register,
1114 * and masking the relevant bit in that register.
1115 *
1116 * When "set" operations are inlinable, they involve writing that mask to
1117 * one register to set a low value, or a different register to set it high.
1118 * Otherwise locking is needed, so there may be little value to inlining.
1119 *
1120 *------------------------------------------------------------------------
1121 *
1122 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1123 * have requested the GPIO. That can include implicit requesting by
1124 * a direction setting call. Marking a gpio as requested locks its chip
1125 * in memory, guaranteeing that these table lookups need no more locking
1126 * and that gpiochip_remove() will fail.
1127 *
1128 * REVISIT when debugging, consider adding some instrumentation to ensure
1129 * that the GPIO was actually requested.
1130 */
1131
Alexandre Courbot23600962014-03-11 15:52:09 +09001132static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001133{
1134 struct gpio_chip *chip;
Alexandre Courbot23600962014-03-11 15:52:09 +09001135 bool value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001136 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001137
Alexandre Courbot372e7222013-02-03 01:29:29 +09001138 chip = desc->chip;
1139 offset = gpio_chip_hwgpio(desc);
Alexandre Courbot23600962014-03-11 15:52:09 +09001140 value = chip->get ? chip->get(chip, offset) : false;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001141 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001142 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001143}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001144
David Brownelld2876d02008-02-04 22:28:20 -08001145/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001146 * gpiod_get_raw_value() - return a gpio's raw value
1147 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001148 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001149 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1150 * its ACTIVE_LOW status.
1151 *
1152 * This function should be called from contexts where we cannot sleep, and will
1153 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001154 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001155int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001156{
David Brownelld2876d02008-02-04 22:28:20 -08001157 if (!desc)
1158 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001159 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001160 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001161 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001162}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001163EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001164
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001165/**
1166 * gpiod_get_value() - return a gpio's value
1167 * @desc: gpio whose value will be returned
1168 *
1169 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1170 * account.
1171 *
1172 * This function should be called from contexts where we cannot sleep, and will
1173 * complain if the GPIO chip functions potentially sleep.
1174 */
1175int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001176{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001177 int value;
1178 if (!desc)
1179 return 0;
1180 /* Should be using gpio_get_value_cansleep() */
1181 WARN_ON(desc->chip->can_sleep);
1182
1183 value = _gpiod_get_raw_value(desc);
1184 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1185 value = !value;
1186
1187 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001188}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001189EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001190
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301191/*
1192 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001193 * @desc: gpio descriptor whose state need to be set.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301194 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1195 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001196static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301197{
1198 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001199 struct gpio_chip *chip = desc->chip;
1200 int offset = gpio_chip_hwgpio(desc);
1201
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301202 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001203 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301204 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001205 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301206 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001207 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301208 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001209 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301210 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001211 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301212 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001213 gpiod_err(desc,
1214 "%s: Error in set_value for open drain err %d\n",
1215 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301216}
1217
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301218/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001219 * _gpio_set_open_source_value() - Set the open source gpio's value.
1220 * @desc: gpio descriptor whose state need to be set.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301221 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1222 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001223static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301224{
1225 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001226 struct gpio_chip *chip = desc->chip;
1227 int offset = gpio_chip_hwgpio(desc);
1228
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301229 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001230 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301231 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001232 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301233 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001234 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301235 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001236 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301237 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001238 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301239 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001240 gpiod_err(desc,
1241 "%s: Error in set_value for open source err %d\n",
1242 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301243}
1244
Alexandre Courbot23600962014-03-11 15:52:09 +09001245static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001246{
1247 struct gpio_chip *chip;
1248
Alexandre Courbot372e7222013-02-03 01:29:29 +09001249 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001250 trace_gpio_value(desc_to_gpio(desc), 0, value);
1251 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1252 _gpio_set_open_drain_value(desc, value);
1253 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1254 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301255 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001256 chip->set(chip, gpio_chip_hwgpio(desc), value);
1257}
1258
David Brownelld2876d02008-02-04 22:28:20 -08001259/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001260 * gpiod_set_raw_value() - assign a gpio's raw value
1261 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001262 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001263 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001264 * Set the raw value of the GPIO, i.e. the value of its physical line without
1265 * regard for its ACTIVE_LOW status.
1266 *
1267 * This function should be called from contexts where we cannot sleep, and will
1268 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001269 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001270void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001271{
David Brownelld2876d02008-02-04 22:28:20 -08001272 if (!desc)
1273 return;
David Brownelld2876d02008-02-04 22:28:20 -08001274 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001275 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001276 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001277}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001278EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001279
1280/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001281 * gpiod_set_value() - assign a gpio's value
1282 * @desc: gpio whose value will be assigned
1283 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001284 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001285 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1286 * account
1287 *
1288 * This function should be called from contexts where we cannot sleep, and will
1289 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001290 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001291void gpiod_set_value(struct gpio_desc *desc, int value)
1292{
1293 if (!desc)
1294 return;
1295 /* Should be using gpio_set_value_cansleep() */
1296 WARN_ON(desc->chip->can_sleep);
1297 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1298 value = !value;
1299 _gpiod_set_raw_value(desc, value);
1300}
1301EXPORT_SYMBOL_GPL(gpiod_set_value);
1302
1303/**
1304 * gpiod_cansleep() - report whether gpio value access may sleep
1305 * @desc: gpio to check
1306 *
1307 */
1308int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001309{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001310 if (!desc)
1311 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001312 return desc->chip->can_sleep;
1313}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001314EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001315
David Brownell0f6d5042008-10-15 22:03:14 -07001316/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001317 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1318 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001319 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001320 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1321 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001322 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001323int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001324{
1325 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001326 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001327
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001328 if (!desc)
1329 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001330 chip = desc->chip;
1331 offset = gpio_chip_hwgpio(desc);
1332 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1333}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001334EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001335
Linus Walleijd468bf92013-09-24 11:54:38 +02001336/**
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001337 * gpio_lock_as_irq() - lock a GPIO to be used as IRQ
1338 * @chip: the chip the GPIO to lock belongs to
1339 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001340 *
1341 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001342 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001343 */
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001344int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001345{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001346 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001347 return -EINVAL;
1348
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001349 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1350 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001351 "%s: tried to flag a GPIO set as output for IRQ\n",
1352 __func__);
1353 return -EIO;
1354 }
1355
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001356 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001357 return 0;
1358}
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001359EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001360
Linus Walleijd468bf92013-09-24 11:54:38 +02001361/**
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001362 * gpio_unlock_as_irq() - unlock a GPIO used as IRQ
1363 * @chip: the chip the GPIO to lock belongs to
1364 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001365 *
1366 * This is used directly by GPIO drivers that want to indicate
1367 * that a certain GPIO is no longer used exclusively for IRQ.
1368 */
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001369void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001370{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001371 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001372 return;
1373
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001374 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001375}
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001376EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001377
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001378/**
1379 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1380 * @desc: gpio whose value will be returned
1381 *
1382 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1383 * its ACTIVE_LOW status.
1384 *
1385 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001386 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001387int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001388{
David Brownelld2876d02008-02-04 22:28:20 -08001389 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001390 if (!desc)
1391 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001392 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001393}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001394EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001395
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001396/**
1397 * gpiod_get_value_cansleep() - return a gpio's value
1398 * @desc: gpio whose value will be returned
1399 *
1400 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1401 * account.
1402 *
1403 * This function is to be called from contexts that can sleep.
1404 */
1405int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001406{
David Brownelld2876d02008-02-04 22:28:20 -08001407 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001408
1409 might_sleep_if(extra_checks);
1410 if (!desc)
1411 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001412
1413 value = _gpiod_get_raw_value(desc);
1414 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1415 value = !value;
1416
David Brownelld2876d02008-02-04 22:28:20 -08001417 return value;
1418}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001419EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001420
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001421/**
1422 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1423 * @desc: gpio whose value will be assigned
1424 * @value: value to assign
1425 *
1426 * Set the raw value of the GPIO, i.e. the value of its physical line without
1427 * regard for its ACTIVE_LOW status.
1428 *
1429 * This function is to be called from contexts that can sleep.
1430 */
1431void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001432{
David Brownelld2876d02008-02-04 22:28:20 -08001433 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001434 if (!desc)
1435 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001436 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001437}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001438EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001439
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001440/**
1441 * gpiod_set_value_cansleep() - assign a gpio's value
1442 * @desc: gpio whose value will be assigned
1443 * @value: value to assign
1444 *
1445 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1446 * account
1447 *
1448 * This function is to be called from contexts that can sleep.
1449 */
1450void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001451{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001452 might_sleep_if(extra_checks);
1453 if (!desc)
1454 return;
1455
1456 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1457 value = !value;
1458 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001459}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001460EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001461
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001462/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001463 * gpiod_add_lookup_table() - register GPIO device consumers
1464 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001465 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001466void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001467{
1468 mutex_lock(&gpio_lookup_lock);
1469
Alexandre Courbotad824782013-12-03 12:20:11 +09001470 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001471
1472 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001473}
1474
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001475static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001476 unsigned int idx,
1477 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001478{
Thierry Redingdd34c372014-04-23 17:28:09 +02001479 static const char *suffixes[] = { "gpios", "gpio" };
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001480 char prop_name[32]; /* 32 is max size of property name */
1481 enum of_gpio_flags of_flags;
1482 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001483 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001484
Thierry Redingdd34c372014-04-23 17:28:09 +02001485 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1486 if (con_id)
1487 snprintf(prop_name, 32, "%s-%s", con_id, suffixes[i]);
1488 else
1489 snprintf(prop_name, 32, "%s", suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001490
Thierry Redingdd34c372014-04-23 17:28:09 +02001491 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1492 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001493 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001494 break;
1495 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001496
1497 if (IS_ERR(desc))
1498 return desc;
1499
1500 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001501 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001502
1503 return desc;
1504}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001505
Mika Westerberg81f59e92013-10-10 11:01:09 +03001506static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001507 unsigned int idx,
1508 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001509{
Mika Westerberge01f4402013-10-10 11:01:10 +03001510 struct acpi_gpio_info info;
1511 struct gpio_desc *desc;
1512
1513 desc = acpi_get_gpiod_by_index(dev, idx, &info);
1514 if (IS_ERR(desc))
1515 return desc;
1516
1517 if (info.gpioint && info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001518 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001519
1520 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001521}
1522
Alexandre Courbotad824782013-12-03 12:20:11 +09001523static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1524{
1525 const char *dev_id = dev ? dev_name(dev) : NULL;
1526 struct gpiod_lookup_table *table;
1527
1528 mutex_lock(&gpio_lookup_lock);
1529
1530 list_for_each_entry(table, &gpio_lookup_list, list) {
1531 if (table->dev_id && dev_id) {
1532 /*
1533 * Valid strings on both ends, must be identical to have
1534 * a match
1535 */
1536 if (!strcmp(table->dev_id, dev_id))
1537 goto found;
1538 } else {
1539 /*
1540 * One of the pointers is NULL, so both must be to have
1541 * a match
1542 */
1543 if (dev_id == table->dev_id)
1544 goto found;
1545 }
1546 }
1547 table = NULL;
1548
1549found:
1550 mutex_unlock(&gpio_lookup_lock);
1551 return table;
1552}
1553
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001554static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001555 unsigned int idx,
1556 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001557{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001558 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001559 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001560 struct gpiod_lookup *p;
1561
Alexandre Courbotad824782013-12-03 12:20:11 +09001562 table = gpiod_find_lookup_table(dev);
1563 if (!table)
1564 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001565
Alexandre Courbotad824782013-12-03 12:20:11 +09001566 for (p = &table->table[0]; p->chip_label; p++) {
1567 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001568
Alexandre Courbotad824782013-12-03 12:20:11 +09001569 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001570 if (p->idx != idx)
1571 continue;
1572
Alexandre Courbotad824782013-12-03 12:20:11 +09001573 /* If the lookup entry has a con_id, require exact match */
1574 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1575 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001576
Alexandre Courbotad824782013-12-03 12:20:11 +09001577 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001578
Alexandre Courbotad824782013-12-03 12:20:11 +09001579 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001580 dev_err(dev, "cannot find GPIO chip %s\n",
1581 p->chip_label);
1582 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001583 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001584
Alexandre Courbotad824782013-12-03 12:20:11 +09001585 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001586 dev_err(dev,
1587 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1588 idx, chip->ngpio, chip->label);
1589 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001590 }
1591
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001592 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001593 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001594
1595 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001596 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001597
1598 return desc;
1599}
1600
1601/**
Thierry Reding08791622014-04-25 16:54:22 +02001602 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09001603 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001604 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001605 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001606 *
1607 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001608 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1609 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001610 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001611struct gpio_desc *__must_check __gpiod_get(struct device *dev, const char *con_id,
1612 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001613{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001614 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001615}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001616EXPORT_SYMBOL_GPL(__gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001617
1618/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02001619 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1620 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1621 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001622 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02001623 *
1624 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1625 * the requested function it will return NULL. This is convenient for drivers
1626 * that need to handle optional GPIOs.
1627 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001628struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
1629 const char *con_id,
1630 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02001631{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001632 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001633}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001634EXPORT_SYMBOL_GPL(__gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001635
1636/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001637 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02001638 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001639 * @con_id: function within the GPIO consumer
1640 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001641 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001642 *
1643 * This variant of gpiod_get() allows to access GPIOs other than the first
1644 * defined one for functions that define several GPIOs.
1645 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001646 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1647 * requested function and/or index, or another IS_ERR() code if an error
1648 * occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001649 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001650struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001651 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001652 unsigned int idx,
1653 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001654{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001655 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001656 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001657 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001658
1659 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
1660
1661 /* Using device tree? */
1662 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
1663 dev_dbg(dev, "using device tree for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001664 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
Mika Westerberg81f59e92013-10-10 11:01:09 +03001665 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
1666 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001667 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001668 }
1669
1670 /*
1671 * Either we are not using DT or ACPI, or their lookup did not return
1672 * a result. In that case, use platform lookup as a fallback.
1673 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001674 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04001675 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001676 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001677 }
1678
1679 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02001680 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001681 return desc;
1682 }
1683
1684 status = gpiod_request(desc, con_id);
1685
1686 if (status < 0)
1687 return ERR_PTR(status);
1688
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001689 if (lookupflags & GPIO_ACTIVE_LOW)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001690 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001691 if (lookupflags & GPIO_OPEN_DRAIN)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001692 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001693 if (lookupflags & GPIO_OPEN_SOURCE)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001694 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001695
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001696 /* No particular flag request, return here... */
Adrian Hunter72f908c2014-09-22 11:01:16 +03001697 if (!(flags & GPIOD_FLAGS_BIT_DIR_SET))
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001698 return desc;
1699
1700 /* Process flags */
1701 if (flags & GPIOD_FLAGS_BIT_DIR_OUT)
1702 status = gpiod_direction_output(desc,
1703 flags & GPIOD_FLAGS_BIT_DIR_VAL);
1704 else
1705 status = gpiod_direction_input(desc);
1706
1707 if (status < 0) {
1708 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
1709 gpiod_put(desc);
1710 return ERR_PTR(status);
1711 }
1712
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001713 return desc;
1714}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001715EXPORT_SYMBOL_GPL(__gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001716
1717/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02001718 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
1719 * function
1720 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1721 * @con_id: function within the GPIO consumer
1722 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001723 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02001724 *
1725 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
1726 * specified index was assigned to the requested function it will return NULL.
1727 * This is convenient for drivers that need to handle optional GPIOs.
1728 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001729struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02001730 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001731 unsigned int index,
1732 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02001733{
1734 struct gpio_desc *desc;
1735
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001736 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001737 if (IS_ERR(desc)) {
1738 if (PTR_ERR(desc) == -ENOENT)
1739 return NULL;
1740 }
1741
1742 return desc;
1743}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001744EXPORT_SYMBOL_GPL(__gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001745
1746/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001747 * gpiod_put - dispose of a GPIO descriptor
1748 * @desc: GPIO descriptor to dispose of
1749 *
1750 * No descriptor can be used after gpiod_put() has been called on it.
1751 */
1752void gpiod_put(struct gpio_desc *desc)
1753{
1754 gpiod_free(desc);
1755}
1756EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08001757
David Brownelld2876d02008-02-04 22:28:20 -08001758#ifdef CONFIG_DEBUG_FS
1759
1760static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1761{
1762 unsigned i;
1763 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001764 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08001765 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02001766 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08001767
1768 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1769 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1770 continue;
1771
Alexandre Courbot372e7222013-02-03 01:29:29 +09001772 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08001773 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001774 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
1775 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08001776 gpio, gdesc->label,
1777 is_out ? "out" : "in ",
1778 chip->get
1779 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02001780 : "? ",
1781 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08001782 seq_printf(s, "\n");
1783 }
1784}
1785
Thierry Redingf9c4a312012-04-12 13:26:01 +02001786static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08001787{
Grant Likely362432a2013-02-09 09:41:49 +00001788 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001789 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001790 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001791
1792 s->private = "";
1793
Grant Likely362432a2013-02-09 09:41:49 +00001794 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001795 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00001796 if (index-- == 0) {
1797 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001798 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00001799 }
1800 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001801
1802 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001803}
1804
1805static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
1806{
Grant Likely362432a2013-02-09 09:41:49 +00001807 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001808 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001809 void *ret = NULL;
1810
Grant Likely362432a2013-02-09 09:41:49 +00001811 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001812 if (list_is_last(&chip->list, &gpio_chips))
1813 ret = NULL;
1814 else
1815 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00001816 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02001817
1818 s->private = "\n";
1819 ++*pos;
1820
1821 return ret;
1822}
1823
1824static void gpiolib_seq_stop(struct seq_file *s, void *v)
1825{
1826}
1827
1828static int gpiolib_seq_show(struct seq_file *s, void *v)
1829{
1830 struct gpio_chip *chip = v;
1831 struct device *dev;
1832
1833 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
1834 chip->base, chip->base + chip->ngpio - 1);
1835 dev = chip->dev;
1836 if (dev)
1837 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
1838 dev_name(dev));
1839 if (chip->label)
1840 seq_printf(s, ", %s", chip->label);
1841 if (chip->can_sleep)
1842 seq_printf(s, ", can sleep");
1843 seq_printf(s, ":\n");
1844
1845 if (chip->dbg_show)
1846 chip->dbg_show(s, chip);
1847 else
1848 gpiolib_dbg_show(s, chip);
1849
David Brownelld2876d02008-02-04 22:28:20 -08001850 return 0;
1851}
1852
Thierry Redingf9c4a312012-04-12 13:26:01 +02001853static const struct seq_operations gpiolib_seq_ops = {
1854 .start = gpiolib_seq_start,
1855 .next = gpiolib_seq_next,
1856 .stop = gpiolib_seq_stop,
1857 .show = gpiolib_seq_show,
1858};
1859
David Brownelld2876d02008-02-04 22:28:20 -08001860static int gpiolib_open(struct inode *inode, struct file *file)
1861{
Thierry Redingf9c4a312012-04-12 13:26:01 +02001862 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08001863}
1864
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001865static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02001866 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08001867 .open = gpiolib_open,
1868 .read = seq_read,
1869 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02001870 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08001871};
1872
1873static int __init gpiolib_debugfs_init(void)
1874{
1875 /* /sys/kernel/debug/gpio */
1876 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1877 NULL, NULL, &gpiolib_operations);
1878 return 0;
1879}
1880subsys_initcall(gpiolib_debugfs_init);
1881
1882#endif /* DEBUG_FS */