blob: e8e98ca25ec7e468c95164a791866ae477f74f2b [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d2008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
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 Brownelld8f388d2008-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 Brownelld8f388d2008-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 Brownelld8f388d2008-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 Brownelld8f388d2008-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 Brownelld8f388d2008-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
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530271#ifdef CONFIG_PINCTRL
272 INIT_LIST_HEAD(&chip->pin_ranges);
273#endif
274
Anton Vorontsov391c9702010-06-08 07:48:17 -0600275 of_gpiochip_add(chip);
Mika Westerberg664e3e52014-01-08 12:40:54 +0200276 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600277
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600278 if (status)
279 goto fail;
280
281 status = gpiochip_export(chip);
282 if (status)
283 goto fail;
284
Andy Shevchenko7589e592013-12-05 11:26:23 +0200285 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700286 chip->base, chip->base + chip->ngpio - 1,
287 chip->label ? : "generic");
288
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600289 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800290
291unlock:
292 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld2876d02008-02-04 22:28:20 -0800293fail:
294 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200295 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600296 chip->base, chip->base + chip->ngpio - 1,
297 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800298 return status;
299}
300EXPORT_SYMBOL_GPL(gpiochip_add);
301
Linus Walleij14250522014-03-25 10:40:18 +0100302/* Forward-declaration */
303static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
304
David Brownelld2876d02008-02-04 22:28:20 -0800305/**
306 * gpiochip_remove() - unregister a gpio_chip
307 * @chip: the chip to unregister
308 *
309 * A gpio_chip with any GPIOs still requested may not be removed.
310 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200311void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800312{
313 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800314 unsigned id;
315
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200316 acpi_gpiochip_remove(chip);
317
David Brownelld2876d02008-02-04 22:28:20 -0800318 spin_lock_irqsave(&gpio_lock, flags);
319
Linus Walleij14250522014-03-25 10:40:18 +0100320 gpiochip_irqchip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100321 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600322 of_gpiochip_remove(chip);
323
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900324 for (id = 0; id < chip->ngpio; id++) {
abdoulaye berthee1db1702014-07-05 18:28:50 +0200325 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags))
326 dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
David Brownelld2876d02008-02-04 22:28:20 -0800327 }
abdoulaye berthee1db1702014-07-05 18:28:50 +0200328 for (id = 0; id < chip->ngpio; id++)
329 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900330
abdoulaye berthee1db1702014-07-05 18:28:50 +0200331 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800332 spin_unlock_irqrestore(&gpio_lock, flags);
abdoulaye berthee1db1702014-07-05 18:28:50 +0200333 gpiochip_unexport(chip);
David Brownelld2876d02008-02-04 22:28:20 -0800334}
335EXPORT_SYMBOL_GPL(gpiochip_remove);
336
Grant Likely594fa262010-06-08 07:48:16 -0600337/**
338 * gpiochip_find() - iterator for locating a specific gpio_chip
339 * @data: data to pass to match function
340 * @callback: Callback function to check gpio_chip
341 *
342 * Similar to bus_find_device. It returns a reference to a gpio_chip as
343 * determined by a user supplied @match callback. The callback should return
344 * 0 if the device doesn't match and non-zero if it does. If the callback is
345 * non-zero, this function will return to the caller and not iterate over any
346 * more gpio_chips.
347 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600348struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700349 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600350 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600351{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900352 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600353 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600354
355 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900356 list_for_each_entry(chip, &gpio_chips, list)
357 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600358 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900359
360 /* No match? */
361 if (&chip->list == &gpio_chips)
362 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600363 spin_unlock_irqrestore(&gpio_lock, flags);
364
365 return chip;
366}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600367EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800368
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900369static int gpiochip_match_name(struct gpio_chip *chip, void *data)
370{
371 const char *name = data;
372
373 return !strcmp(chip->label, name);
374}
375
376static struct gpio_chip *find_chip_by_name(const char *name)
377{
378 return gpiochip_find((void *)name, gpiochip_match_name);
379}
380
Linus Walleij14250522014-03-25 10:40:18 +0100381#ifdef CONFIG_GPIOLIB_IRQCHIP
382
383/*
384 * The following is irqchip helper code for gpiochips.
385 */
386
387/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200388 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
389 * @gpiochip: the gpiochip to set the irqchip chain to
390 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100391 * @parent_irq: the irq number corresponding to the parent IRQ for this
392 * chained irqchip
393 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200394 * coming out of the gpiochip. If the interrupt is nested rather than
395 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100396 */
397void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
398 struct irq_chip *irqchip,
399 int parent_irq,
400 irq_flow_handler_t parent_handler)
401{
Linus Walleij83141a72014-09-26 13:50:12 +0200402 unsigned int offset;
403
Linus Walleij83141a72014-09-26 13:50:12 +0200404 if (!gpiochip->irqdomain) {
405 chip_err(gpiochip, "called %s before setting up irqchip\n",
406 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200407 return;
408 }
409
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200410 if (parent_handler) {
411 if (gpiochip->can_sleep) {
412 chip_err(gpiochip,
413 "you cannot have chained interrupts on a "
414 "chip that may sleep\n");
415 return;
416 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200417 /*
418 * The parent irqchip is already using the chip_data for this
419 * irqchip, so our callbacks simply use the handler_data.
420 */
421 irq_set_handler_data(parent_irq, gpiochip);
Linus Torvaldsea584592014-10-09 14:58:15 -0400422 irq_set_chained_handler(parent_irq, parent_handler);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200423 }
Linus Walleij83141a72014-09-26 13:50:12 +0200424
425 /* Set the parent IRQ for all affected IRQs */
426 for (offset = 0; offset < gpiochip->ngpio; offset++)
427 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
428 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100429}
430EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
431
Linus Walleije45d1c82014-04-22 14:01:46 +0200432/*
433 * This lock class tells lockdep that GPIO irqs are in a different
434 * category than their parents, so it won't report false recursion.
435 */
436static struct lock_class_key gpiochip_irq_lock_class;
437
Linus Walleij14250522014-03-25 10:40:18 +0100438/**
439 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
440 * @d: the irqdomain used by this irqchip
441 * @irq: the global irq number used by this GPIO irqchip irq
442 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
443 *
444 * This function will set up the mapping for a certain IRQ line on a
445 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
446 * stored inside the gpiochip.
447 */
448static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
449 irq_hw_number_t hwirq)
450{
451 struct gpio_chip *chip = d->host_data;
452
Linus Walleij14250522014-03-25 10:40:18 +0100453 irq_set_chip_data(irq, chip);
Linus Walleije45d1c82014-04-22 14:01:46 +0200454 irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
Linus Walleij7633fb92014-04-09 13:20:38 +0200455 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200456 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300457 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200458 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100459#ifdef CONFIG_ARM
460 set_irq_flags(irq, IRQF_VALID);
461#else
462 irq_set_noprobe(irq);
463#endif
Linus Walleij1333b902014-04-23 16:45:12 +0200464 /*
465 * No set-up of the hardware will happen if IRQ_TYPE_NONE
466 * is passed as default type.
467 */
468 if (chip->irq_default_type != IRQ_TYPE_NONE)
469 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100470
471 return 0;
472}
473
Linus Walleijc3626fd2014-03-28 20:42:01 +0100474static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
475{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200476 struct gpio_chip *chip = d->host_data;
477
Linus Walleijc3626fd2014-03-28 20:42:01 +0100478#ifdef CONFIG_ARM
479 set_irq_flags(irq, 0);
480#endif
Linus Walleij1c8732b2014-04-09 13:34:39 +0200481 if (chip->can_sleep)
482 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100483 irq_set_chip_and_handler(irq, NULL, NULL);
484 irq_set_chip_data(irq, NULL);
485}
486
Linus Walleij14250522014-03-25 10:40:18 +0100487static const struct irq_domain_ops gpiochip_domain_ops = {
488 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100489 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100490 /* Virtually all GPIO irqchips are twocell:ed */
491 .xlate = irq_domain_xlate_twocell,
492};
493
494static int gpiochip_irq_reqres(struct irq_data *d)
495{
496 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
497
498 if (gpio_lock_as_irq(chip, d->hwirq)) {
499 chip_err(chip,
500 "unable to lock HW IRQ %lu for IRQ\n",
501 d->hwirq);
502 return -EINVAL;
503 }
504 return 0;
505}
506
507static void gpiochip_irq_relres(struct irq_data *d)
508{
509 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
510
511 gpio_unlock_as_irq(chip, d->hwirq);
512}
513
514static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
515{
516 return irq_find_mapping(chip->irqdomain, offset);
517}
518
519/**
520 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
521 * @gpiochip: the gpiochip to remove the irqchip from
522 *
523 * This is called only from gpiochip_remove()
524 */
525static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
526{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100527 unsigned int offset;
528
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300529 acpi_gpiochip_free_interrupts(gpiochip);
530
Linus Walleijc3626fd2014-03-28 20:42:01 +0100531 /* Remove all IRQ mappings and delete the domain */
532 if (gpiochip->irqdomain) {
533 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300534 irq_dispose_mapping(
535 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100536 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100537 }
Linus Walleij14250522014-03-25 10:40:18 +0100538
539 if (gpiochip->irqchip) {
540 gpiochip->irqchip->irq_request_resources = NULL;
541 gpiochip->irqchip->irq_release_resources = NULL;
542 gpiochip->irqchip = NULL;
543 }
544}
545
546/**
547 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
548 * @gpiochip: the gpiochip to add the irqchip to
549 * @irqchip: the irqchip to add to the gpiochip
550 * @first_irq: if not dynamically assigned, the base (first) IRQ to
551 * allocate gpiochip irqs from
552 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200553 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
554 * to have the core avoid setting up any default type in the hardware.
Linus Walleij14250522014-03-25 10:40:18 +0100555 *
556 * This function closely associates a certain irqchip with a certain
557 * gpiochip, providing an irq domain to translate the local IRQs to
558 * global irqs in the gpiolib core, and making sure that the gpiochip
559 * is passed as chip data to all related functions. Driver callbacks
560 * need to use container_of() to get their local state containers back
561 * from the gpiochip passed as chip data. An irqdomain will be stored
562 * in the gpiochip that shall be used by the driver to handle IRQ number
563 * translation. The gpiochip will need to be initialized and registered
564 * before calling this function.
565 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100566 * This function will handle two cell:ed simple IRQs and assumes all
567 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100568 * need to be open coded.
569 */
570int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
571 struct irq_chip *irqchip,
572 unsigned int first_irq,
573 irq_flow_handler_t handler,
574 unsigned int type)
575{
576 struct device_node *of_node;
577 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100578 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100579
580 if (!gpiochip || !irqchip)
581 return -EINVAL;
582
583 if (!gpiochip->dev) {
584 pr_err("missing gpiochip .dev parent pointer\n");
585 return -EINVAL;
586 }
587 of_node = gpiochip->dev->of_node;
588#ifdef CONFIG_OF_GPIO
589 /*
590 * If the gpiochip has an assigned OF node this takes precendence
591 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
592 */
593 if (gpiochip->of_node)
594 of_node = gpiochip->of_node;
595#endif
596 gpiochip->irqchip = irqchip;
597 gpiochip->irq_handler = handler;
598 gpiochip->irq_default_type = type;
599 gpiochip->to_irq = gpiochip_to_irq;
600 gpiochip->irqdomain = irq_domain_add_simple(of_node,
601 gpiochip->ngpio, first_irq,
602 &gpiochip_domain_ops, gpiochip);
603 if (!gpiochip->irqdomain) {
604 gpiochip->irqchip = NULL;
605 return -EINVAL;
606 }
607 irqchip->irq_request_resources = gpiochip_irq_reqres;
608 irqchip->irq_release_resources = gpiochip_irq_relres;
609
610 /*
611 * Prepare the mapping since the irqchip shall be orthogonal to
612 * any gpiochip calls. If the first_irq was zero, this is
613 * necessary to allocate descriptors for all IRQs.
614 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100615 for (offset = 0; offset < gpiochip->ngpio; offset++) {
616 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
617 if (offset == 0)
618 /*
619 * Store the base into the gpiochip to be used when
620 * unmapping the irqs.
621 */
622 gpiochip->irq_base = irq_base;
623 }
Linus Walleij14250522014-03-25 10:40:18 +0100624
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300625 acpi_gpiochip_request_interrupts(gpiochip);
626
Linus Walleij14250522014-03-25 10:40:18 +0100627 return 0;
628}
629EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
630
631#else /* CONFIG_GPIOLIB_IRQCHIP */
632
633static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
634
635#endif /* CONFIG_GPIOLIB_IRQCHIP */
636
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530637#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100638
Linus Walleij3f0f8672012-11-20 12:40:15 +0100639/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200640 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
641 * @chip: the gpiochip to add the range for
642 * @pinctrl: the dev_name() of the pin controller to map to
643 * @gpio_offset: the start offset in the current gpio_chip number space
644 * @pin_group: name of the pin group inside the pin controller
645 */
646int gpiochip_add_pingroup_range(struct gpio_chip *chip,
647 struct pinctrl_dev *pctldev,
648 unsigned int gpio_offset, const char *pin_group)
649{
650 struct gpio_pin_range *pin_range;
651 int ret;
652
653 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
654 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200655 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200656 return -ENOMEM;
657 }
658
659 /* Use local offset as range ID */
660 pin_range->range.id = gpio_offset;
661 pin_range->range.gc = chip;
662 pin_range->range.name = chip->label;
663 pin_range->range.base = chip->base + gpio_offset;
664 pin_range->pctldev = pctldev;
665
666 ret = pinctrl_get_group_pins(pctldev, pin_group,
667 &pin_range->range.pins,
668 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100669 if (ret < 0) {
670 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200671 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100672 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200673
674 pinctrl_add_gpio_range(pctldev, &pin_range->range);
675
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200676 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
677 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200678 pinctrl_dev_get_devname(pctldev), pin_group);
679
680 list_add_tail(&pin_range->node, &chip->pin_ranges);
681
682 return 0;
683}
684EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
685
686/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100687 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
688 * @chip: the gpiochip to add the range for
689 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100690 * @gpio_offset: the start offset in the current gpio_chip number space
691 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100692 * @npins: the number of pins from the offset of each pin space (GPIO and
693 * pin controller) to accumulate in this range
694 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100695int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100696 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100697 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530698{
699 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800700 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530701
Linus Walleij3f0f8672012-11-20 12:40:15 +0100702 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530703 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200704 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100705 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530706 }
707
Linus Walleij3f0f8672012-11-20 12:40:15 +0100708 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100709 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100710 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530711 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100712 pin_range->range.base = chip->base + gpio_offset;
713 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530714 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100715 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530716 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100717 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800718 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200719 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100720 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800721 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100722 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200723 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
724 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100725 pinctl_name,
726 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530727
728 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100729
730 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530731}
Linus Walleij165adc92012-11-06 14:49:39 +0100732EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530733
Linus Walleij3f0f8672012-11-20 12:40:15 +0100734/**
735 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
736 * @chip: the chip to remove all the mappings for
737 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530738void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
739{
740 struct gpio_pin_range *pin_range, *tmp;
741
742 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
743 list_del(&pin_range->node);
744 pinctrl_remove_gpio_range(pin_range->pctldev,
745 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100746 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530747 }
748}
Linus Walleij165adc92012-11-06 14:49:39 +0100749EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
750
751#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530752
David Brownelld2876d02008-02-04 22:28:20 -0800753/* These "optional" allocation calls help prevent drivers from stomping
754 * on each other, and help provide better diagnostics in debugfs.
755 * They're called even less than the "set direction" calls.
756 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200757static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800758{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200759 struct gpio_chip *chip = desc->chip;
760 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800761 unsigned long flags;
762
763 spin_lock_irqsave(&gpio_lock, flags);
764
David Brownelld2876d02008-02-04 22:28:20 -0800765 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700766 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800767 */
768
769 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
770 desc_set_label(desc, label ? : "?");
771 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700772 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800773 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800774 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700775 }
776
777 if (chip->request) {
778 /* chip->request may sleep */
779 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900780 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700781 spin_lock_irqsave(&gpio_lock, flags);
782
783 if (status < 0) {
784 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700785 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300786 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700787 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700788 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300789 if (chip->get_direction) {
790 /* chip->get_direction may sleep */
791 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900792 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300793 spin_lock_irqsave(&gpio_lock, flags);
794 }
David Brownelld2876d02008-02-04 22:28:20 -0800795done:
Mika Westerberg77c2d792014-03-10 14:54:50 +0200796 spin_unlock_irqrestore(&gpio_lock, flags);
797 return status;
798}
799
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900800int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200801{
802 int status = -EPROBE_DEFER;
803 struct gpio_chip *chip;
804
805 if (!desc) {
806 pr_warn("%s: invalid GPIO\n", __func__);
807 return -EINVAL;
808 }
809
810 chip = desc->chip;
811 if (!chip)
812 goto done;
813
814 if (try_module_get(chip->owner)) {
815 status = __gpiod_request(desc, label);
816 if (status < 0)
817 module_put(chip->owner);
818 }
819
820done:
David Brownelld2876d02008-02-04 22:28:20 -0800821 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200822 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200823
David Brownelld2876d02008-02-04 22:28:20 -0800824 return status;
825}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900826
Mika Westerberg77c2d792014-03-10 14:54:50 +0200827static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800828{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200829 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -0800830 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -0700831 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800832
Uwe Kleine-König3d599d12008-10-15 22:03:12 -0700833 might_sleep();
834
Alexandre Courbot372e7222013-02-03 01:29:29 +0900835 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700836
David Brownelld2876d02008-02-04 22:28:20 -0800837 spin_lock_irqsave(&gpio_lock, flags);
838
David Brownell35e8bb52008-10-15 22:03:16 -0700839 chip = desc->chip;
840 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
841 if (chip->free) {
842 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -0700843 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900844 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700845 spin_lock_irqsave(&gpio_lock, flags);
846 }
David Brownelld2876d02008-02-04 22:28:20 -0800847 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -0800848 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -0700849 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530850 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +0530851 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200852 ret = true;
853 }
David Brownelld2876d02008-02-04 22:28:20 -0800854
855 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200856 return ret;
857}
858
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900859void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200860{
861 if (desc && __gpiod_free(desc))
862 module_put(desc->chip->owner);
863 else
864 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -0800865}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900866
David Brownelld2876d02008-02-04 22:28:20 -0800867/**
868 * gpiochip_is_requested - return string iff signal was requested
869 * @chip: controller managing the signal
870 * @offset: of signal within controller's 0..(ngpio - 1) range
871 *
872 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +0900873 * The string returned is the label passed to gpio_request(); if none has been
874 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -0800875 *
876 * This function is for use by GPIO controller drivers. The label can
877 * help with diagnostics, and knowing that the signal is used as a GPIO
878 * can help avoid accidentally multiplexing it to another controller.
879 */
880const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
881{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900882 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800883
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900884 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -0800885 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900886
887 desc = &chip->desc[offset];
888
Alexandre Courbot372e7222013-02-03 01:29:29 +0900889 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -0800890 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900891 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -0800892}
893EXPORT_SYMBOL_GPL(gpiochip_is_requested);
894
Mika Westerberg77c2d792014-03-10 14:54:50 +0200895/**
896 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
897 * @desc: GPIO descriptor to request
898 * @label: label for the GPIO
899 *
900 * Function allows GPIO chip drivers to request and use their own GPIO
901 * descriptors via gpiolib API. Difference to gpiod_request() is that this
902 * function will not increase reference count of the GPIO chip module. This
903 * allows the GPIO chip module to be unloaded as needed (we assume that the
904 * GPIO chip driver handles freeing the GPIOs it has requested).
905 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700906struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
907 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200908{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700909 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
910 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200911
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700912 if (IS_ERR(desc)) {
913 chip_err(chip, "failed to get GPIO descriptor\n");
914 return desc;
915 }
916
917 err = __gpiod_request(desc, label);
918 if (err < 0)
919 return ERR_PTR(err);
920
921 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200922}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700923EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200924
925/**
926 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
927 * @desc: GPIO descriptor to free
928 *
929 * Function frees the given GPIO requested previously with
930 * gpiochip_request_own_desc().
931 */
932void gpiochip_free_own_desc(struct gpio_desc *desc)
933{
934 if (desc)
935 __gpiod_free(desc);
936}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700937EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -0800938
939/* Drivers MUST set GPIO direction before making get/set calls. In
940 * some cases this is done in early boot, before IRQs are enabled.
941 *
942 * As a rule these aren't called more than once (except for drivers
943 * using the open-drain emulation idiom) so these are natural places
944 * to accumulate extra debugging checks. Note that we can't (yet)
945 * rely on gpio_request() having been called beforehand.
946 */
947
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700948/**
949 * gpiod_direction_input - set the GPIO direction to input
950 * @desc: GPIO to set to input
951 *
952 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
953 * be called safely on it.
954 *
955 * Return 0 in case of success, else an error code.
956 */
957int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800958{
David Brownelld2876d02008-02-04 22:28:20 -0800959 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800960 int status = -EINVAL;
961
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200962 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900963 pr_warn("%s: invalid GPIO\n", __func__);
964 return -EINVAL;
965 }
966
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200967 chip = desc->chip;
968 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +0100969 gpiod_warn(desc,
970 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +0200971 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200972 return -EIO;
973 }
974
Alexandre Courbotd82da792014-07-22 16:17:43 +0900975 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -0800976 if (status == 0)
977 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -0600978
Alexandre Courbot372e7222013-02-03 01:29:29 +0900979 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +0900980
David Brownelld2876d02008-02-04 22:28:20 -0800981 return status;
982}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700983EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900984
Philipp Zabelef70bbe2014-01-07 12:34:11 +0100985static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -0800986{
David Brownelld2876d02008-02-04 22:28:20 -0800987 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800988 int status = -EINVAL;
989
Linus Walleijd468bf92013-09-24 11:54:38 +0200990 /* GPIOs used for IRQs shall not be set as output */
991 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
992 gpiod_err(desc,
993 "%s: tried to set a GPIO tied to an IRQ as output\n",
994 __func__);
995 return -EIO;
996 }
997
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530998 /* Open drain pin should not be driven to 1 */
999 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001000 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301001
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301002 /* Open source pin should not be driven to 0 */
1003 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001004 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301005
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001006 chip = desc->chip;
1007 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001008 gpiod_warn(desc,
1009 "%s: missing set() or direction_output() operations\n",
1010 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001011 return -EIO;
1012 }
1013
Alexandre Courbotd82da792014-07-22 16:17:43 +09001014 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001015 if (status == 0)
1016 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001017 trace_gpio_value(desc_to_gpio(desc), 0, value);
1018 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001019 return status;
1020}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001021
1022/**
1023 * gpiod_direction_output_raw - set the GPIO direction to output
1024 * @desc: GPIO to set to output
1025 * @value: initial output value of the GPIO
1026 *
1027 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1028 * be called safely on it. The initial value of the output must be specified
1029 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1030 *
1031 * Return 0 in case of success, else an error code.
1032 */
1033int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1034{
1035 if (!desc || !desc->chip) {
1036 pr_warn("%s: invalid GPIO\n", __func__);
1037 return -EINVAL;
1038 }
1039 return _gpiod_direction_output_raw(desc, value);
1040}
1041EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1042
1043/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301044 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001045 * @desc: GPIO to set to output
1046 * @value: initial output value of the GPIO
1047 *
1048 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1049 * be called safely on it. The initial value of the output must be specified
1050 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1051 * account.
1052 *
1053 * Return 0 in case of success, else an error code.
1054 */
1055int gpiod_direction_output(struct gpio_desc *desc, int value)
1056{
1057 if (!desc || !desc->chip) {
1058 pr_warn("%s: invalid GPIO\n", __func__);
1059 return -EINVAL;
1060 }
1061 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1062 value = !value;
1063 return _gpiod_direction_output_raw(desc, value);
1064}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001065EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001066
Felipe Balbic4b5be92010-05-26 14:42:23 -07001067/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001068 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001069 * @gpio: the gpio to set debounce time
1070 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001071 *
1072 * returns -ENOTSUPP if the controller does not support setting
1073 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001074 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001075int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001076{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001077 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001078
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001079 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001080 pr_warn("%s: invalid GPIO\n", __func__);
1081 return -EINVAL;
1082 }
1083
Felipe Balbic4b5be92010-05-26 14:42:23 -07001084 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001085 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001086 gpiod_dbg(desc,
1087 "%s: missing set() or set_debounce() operations\n",
1088 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001089 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001090 }
1091
Alexandre Courbotd82da792014-07-22 16:17:43 +09001092 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001093}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001094EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001095
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001096/**
1097 * gpiod_is_active_low - test whether a GPIO is active-low or not
1098 * @desc: the gpio descriptor to test
1099 *
1100 * Returns 1 if the GPIO is active-low, 0 otherwise.
1101 */
1102int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001103{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001104 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001105}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001106EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001107
1108/* I/O calls are only valid after configuration completed; the relevant
1109 * "is this a valid GPIO" error checks should already have been done.
1110 *
1111 * "Get" operations are often inlinable as reading a pin value register,
1112 * and masking the relevant bit in that register.
1113 *
1114 * When "set" operations are inlinable, they involve writing that mask to
1115 * one register to set a low value, or a different register to set it high.
1116 * Otherwise locking is needed, so there may be little value to inlining.
1117 *
1118 *------------------------------------------------------------------------
1119 *
1120 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1121 * have requested the GPIO. That can include implicit requesting by
1122 * a direction setting call. Marking a gpio as requested locks its chip
1123 * in memory, guaranteeing that these table lookups need no more locking
1124 * and that gpiochip_remove() will fail.
1125 *
1126 * REVISIT when debugging, consider adding some instrumentation to ensure
1127 * that the GPIO was actually requested.
1128 */
1129
Alexandre Courbot23600962014-03-11 15:52:09 +09001130static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001131{
1132 struct gpio_chip *chip;
Alexandre Courbot23600962014-03-11 15:52:09 +09001133 bool value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001134 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001135
Alexandre Courbot372e7222013-02-03 01:29:29 +09001136 chip = desc->chip;
1137 offset = gpio_chip_hwgpio(desc);
Alexandre Courbot23600962014-03-11 15:52:09 +09001138 value = chip->get ? chip->get(chip, offset) : false;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001139 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001140 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001141}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001142
David Brownelld2876d02008-02-04 22:28:20 -08001143/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001144 * gpiod_get_raw_value() - return a gpio's raw value
1145 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001146 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001147 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1148 * its ACTIVE_LOW status.
1149 *
1150 * This function should be called from contexts where we cannot sleep, and will
1151 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001152 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001153int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001154{
David Brownelld2876d02008-02-04 22:28:20 -08001155 if (!desc)
1156 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001157 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001158 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001159 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001160}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001161EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001162
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001163/**
1164 * gpiod_get_value() - return a gpio's value
1165 * @desc: gpio whose value will be returned
1166 *
1167 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1168 * account.
1169 *
1170 * This function should be called from contexts where we cannot sleep, and will
1171 * complain if the GPIO chip functions potentially sleep.
1172 */
1173int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001174{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001175 int value;
1176 if (!desc)
1177 return 0;
1178 /* Should be using gpio_get_value_cansleep() */
1179 WARN_ON(desc->chip->can_sleep);
1180
1181 value = _gpiod_get_raw_value(desc);
1182 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1183 value = !value;
1184
1185 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001186}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001187EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001188
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301189/*
1190 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001191 * @desc: gpio descriptor whose state need to be set.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301192 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1193 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001194static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301195{
1196 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001197 struct gpio_chip *chip = desc->chip;
1198 int offset = gpio_chip_hwgpio(desc);
1199
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301200 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001201 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301202 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001203 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301204 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001205 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301206 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001207 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301208 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001209 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301210 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001211 gpiod_err(desc,
1212 "%s: Error in set_value for open drain err %d\n",
1213 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301214}
1215
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301216/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001217 * _gpio_set_open_source_value() - Set the open source gpio's value.
1218 * @desc: gpio descriptor whose state need to be set.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301219 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1220 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001221static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301222{
1223 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001224 struct gpio_chip *chip = desc->chip;
1225 int offset = gpio_chip_hwgpio(desc);
1226
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301227 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001228 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301229 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001230 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301231 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001232 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301233 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001234 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301235 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001236 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301237 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001238 gpiod_err(desc,
1239 "%s: Error in set_value for open source err %d\n",
1240 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301241}
1242
Alexandre Courbot23600962014-03-11 15:52:09 +09001243static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001244{
1245 struct gpio_chip *chip;
1246
Alexandre Courbot372e7222013-02-03 01:29:29 +09001247 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001248 trace_gpio_value(desc_to_gpio(desc), 0, value);
1249 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1250 _gpio_set_open_drain_value(desc, value);
1251 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1252 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301253 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001254 chip->set(chip, gpio_chip_hwgpio(desc), value);
1255}
1256
David Brownelld2876d02008-02-04 22:28:20 -08001257/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001258 * gpiod_set_raw_value() - assign a gpio's raw value
1259 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001260 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001261 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001262 * Set the raw value of the GPIO, i.e. the value of its physical line without
1263 * regard for its ACTIVE_LOW status.
1264 *
1265 * This function should be called from contexts where we cannot sleep, and will
1266 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001267 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001268void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001269{
David Brownelld2876d02008-02-04 22:28:20 -08001270 if (!desc)
1271 return;
David Brownelld2876d02008-02-04 22:28:20 -08001272 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001273 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001274 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001275}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001276EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001277
1278/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001279 * gpiod_set_value() - assign a gpio's value
1280 * @desc: gpio whose value will be assigned
1281 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001282 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001283 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1284 * account
1285 *
1286 * This function should be called from contexts where we cannot sleep, and will
1287 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001288 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001289void gpiod_set_value(struct gpio_desc *desc, int value)
1290{
1291 if (!desc)
1292 return;
1293 /* Should be using gpio_set_value_cansleep() */
1294 WARN_ON(desc->chip->can_sleep);
1295 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1296 value = !value;
1297 _gpiod_set_raw_value(desc, value);
1298}
1299EXPORT_SYMBOL_GPL(gpiod_set_value);
1300
1301/**
1302 * gpiod_cansleep() - report whether gpio value access may sleep
1303 * @desc: gpio to check
1304 *
1305 */
1306int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001307{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001308 if (!desc)
1309 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001310 return desc->chip->can_sleep;
1311}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001312EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001313
David Brownell0f6d5042008-10-15 22:03:14 -07001314/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001315 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1316 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001317 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001318 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1319 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001320 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001321int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001322{
1323 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001324 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001325
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001326 if (!desc)
1327 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001328 chip = desc->chip;
1329 offset = gpio_chip_hwgpio(desc);
1330 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1331}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001332EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001333
Linus Walleijd468bf92013-09-24 11:54:38 +02001334/**
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001335 * gpio_lock_as_irq() - lock a GPIO to be used as IRQ
1336 * @chip: the chip the GPIO to lock belongs to
1337 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001338 *
1339 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001340 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001341 */
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001342int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001343{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001344 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001345 return -EINVAL;
1346
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001347 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1348 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001349 "%s: tried to flag a GPIO set as output for IRQ\n",
1350 __func__);
1351 return -EIO;
1352 }
1353
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001354 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001355 return 0;
1356}
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001357EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001358
Linus Walleijd468bf92013-09-24 11:54:38 +02001359/**
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001360 * gpio_unlock_as_irq() - unlock a GPIO used as IRQ
1361 * @chip: the chip the GPIO to lock belongs to
1362 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001363 *
1364 * This is used directly by GPIO drivers that want to indicate
1365 * that a certain GPIO is no longer used exclusively for IRQ.
1366 */
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001367void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001368{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001369 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001370 return;
1371
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001372 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001373}
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001374EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001375
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001376/**
1377 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1378 * @desc: gpio whose value will be returned
1379 *
1380 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1381 * its ACTIVE_LOW status.
1382 *
1383 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001384 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001385int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001386{
David Brownelld2876d02008-02-04 22:28:20 -08001387 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001388 if (!desc)
1389 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001390 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001391}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001392EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001393
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001394/**
1395 * gpiod_get_value_cansleep() - return a gpio's value
1396 * @desc: gpio whose value will be returned
1397 *
1398 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1399 * account.
1400 *
1401 * This function is to be called from contexts that can sleep.
1402 */
1403int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001404{
David Brownelld2876d02008-02-04 22:28:20 -08001405 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001406
1407 might_sleep_if(extra_checks);
1408 if (!desc)
1409 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001410
1411 value = _gpiod_get_raw_value(desc);
1412 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1413 value = !value;
1414
David Brownelld2876d02008-02-04 22:28:20 -08001415 return value;
1416}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001417EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001418
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001419/**
1420 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1421 * @desc: gpio whose value will be assigned
1422 * @value: value to assign
1423 *
1424 * Set the raw value of the GPIO, i.e. the value of its physical line without
1425 * regard for its ACTIVE_LOW status.
1426 *
1427 * This function is to be called from contexts that can sleep.
1428 */
1429void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001430{
David Brownelld2876d02008-02-04 22:28:20 -08001431 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001432 if (!desc)
1433 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001434 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001435}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001436EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001437
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001438/**
1439 * gpiod_set_value_cansleep() - assign a gpio's value
1440 * @desc: gpio whose value will be assigned
1441 * @value: value to assign
1442 *
1443 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1444 * account
1445 *
1446 * This function is to be called from contexts that can sleep.
1447 */
1448void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001449{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001450 might_sleep_if(extra_checks);
1451 if (!desc)
1452 return;
1453
1454 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1455 value = !value;
1456 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001457}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001458EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001459
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001460/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001461 * gpiod_add_lookup_table() - register GPIO device consumers
1462 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001463 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001464void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001465{
1466 mutex_lock(&gpio_lookup_lock);
1467
Alexandre Courbotad824782013-12-03 12:20:11 +09001468 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001469
1470 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001471}
1472
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001473static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001474 unsigned int idx,
1475 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001476{
Thierry Redingdd34c372014-04-23 17:28:09 +02001477 static const char *suffixes[] = { "gpios", "gpio" };
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001478 char prop_name[32]; /* 32 is max size of property name */
1479 enum of_gpio_flags of_flags;
1480 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001481 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001482
Thierry Redingdd34c372014-04-23 17:28:09 +02001483 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1484 if (con_id)
1485 snprintf(prop_name, 32, "%s-%s", con_id, suffixes[i]);
1486 else
1487 snprintf(prop_name, 32, "%s", suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001488
Thierry Redingdd34c372014-04-23 17:28:09 +02001489 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1490 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001491 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001492 break;
1493 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001494
1495 if (IS_ERR(desc))
1496 return desc;
1497
1498 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001499 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001500
1501 return desc;
1502}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001503
Mika Westerberg81f59e92013-10-10 11:01:09 +03001504static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001505 unsigned int idx,
1506 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001507{
Mika Westerberge01f4402013-10-10 11:01:10 +03001508 struct acpi_gpio_info info;
1509 struct gpio_desc *desc;
1510
1511 desc = acpi_get_gpiod_by_index(dev, idx, &info);
1512 if (IS_ERR(desc))
1513 return desc;
1514
1515 if (info.gpioint && info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001516 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001517
1518 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001519}
1520
Alexandre Courbotad824782013-12-03 12:20:11 +09001521static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1522{
1523 const char *dev_id = dev ? dev_name(dev) : NULL;
1524 struct gpiod_lookup_table *table;
1525
1526 mutex_lock(&gpio_lookup_lock);
1527
1528 list_for_each_entry(table, &gpio_lookup_list, list) {
1529 if (table->dev_id && dev_id) {
1530 /*
1531 * Valid strings on both ends, must be identical to have
1532 * a match
1533 */
1534 if (!strcmp(table->dev_id, dev_id))
1535 goto found;
1536 } else {
1537 /*
1538 * One of the pointers is NULL, so both must be to have
1539 * a match
1540 */
1541 if (dev_id == table->dev_id)
1542 goto found;
1543 }
1544 }
1545 table = NULL;
1546
1547found:
1548 mutex_unlock(&gpio_lookup_lock);
1549 return table;
1550}
1551
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001552static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001553 unsigned int idx,
1554 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001555{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001556 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001557 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001558 struct gpiod_lookup *p;
1559
Alexandre Courbotad824782013-12-03 12:20:11 +09001560 table = gpiod_find_lookup_table(dev);
1561 if (!table)
1562 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001563
Alexandre Courbotad824782013-12-03 12:20:11 +09001564 for (p = &table->table[0]; p->chip_label; p++) {
1565 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001566
Alexandre Courbotad824782013-12-03 12:20:11 +09001567 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001568 if (p->idx != idx)
1569 continue;
1570
Alexandre Courbotad824782013-12-03 12:20:11 +09001571 /* If the lookup entry has a con_id, require exact match */
1572 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1573 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001574
Alexandre Courbotad824782013-12-03 12:20:11 +09001575 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001576
Alexandre Courbotad824782013-12-03 12:20:11 +09001577 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001578 dev_err(dev, "cannot find GPIO chip %s\n",
1579 p->chip_label);
1580 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001581 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001582
Alexandre Courbotad824782013-12-03 12:20:11 +09001583 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001584 dev_err(dev,
1585 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1586 idx, chip->ngpio, chip->label);
1587 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001588 }
1589
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001590 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001591 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001592
1593 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001594 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001595
1596 return desc;
1597}
1598
1599/**
Thierry Reding08791622014-04-25 16:54:22 +02001600 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09001601 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001602 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001603 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001604 *
1605 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001606 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1607 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001608 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001609struct gpio_desc *__must_check __gpiod_get(struct device *dev, const char *con_id,
1610 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001611{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001612 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001613}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001614EXPORT_SYMBOL_GPL(__gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001615
1616/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02001617 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1618 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1619 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001620 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02001621 *
1622 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1623 * the requested function it will return NULL. This is convenient for drivers
1624 * that need to handle optional GPIOs.
1625 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001626struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
1627 const char *con_id,
1628 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02001629{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001630 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001631}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001632EXPORT_SYMBOL_GPL(__gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001633
1634/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001635 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02001636 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001637 * @con_id: function within the GPIO consumer
1638 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001639 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001640 *
1641 * This variant of gpiod_get() allows to access GPIOs other than the first
1642 * defined one for functions that define several GPIOs.
1643 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001644 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1645 * requested function and/or index, or another IS_ERR() code if an error
1646 * occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001647 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001648struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001649 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001650 unsigned int idx,
1651 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001652{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001653 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001654 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001655 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001656
1657 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
1658
1659 /* Using device tree? */
1660 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
1661 dev_dbg(dev, "using device tree for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001662 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
Mika Westerberg81f59e92013-10-10 11:01:09 +03001663 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
1664 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001665 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001666 }
1667
1668 /*
1669 * Either we are not using DT or ACPI, or their lookup did not return
1670 * a result. In that case, use platform lookup as a fallback.
1671 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001672 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04001673 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001674 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001675 }
1676
1677 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02001678 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001679 return desc;
1680 }
1681
1682 status = gpiod_request(desc, con_id);
1683
1684 if (status < 0)
1685 return ERR_PTR(status);
1686
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001687 if (lookupflags & GPIO_ACTIVE_LOW)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001688 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001689 if (lookupflags & GPIO_OPEN_DRAIN)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001690 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001691 if (lookupflags & GPIO_OPEN_SOURCE)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001692 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001693
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001694 /* No particular flag request, return here... */
Adrian Hunter72f908c2014-09-22 11:01:16 +03001695 if (!(flags & GPIOD_FLAGS_BIT_DIR_SET))
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001696 return desc;
1697
1698 /* Process flags */
1699 if (flags & GPIOD_FLAGS_BIT_DIR_OUT)
1700 status = gpiod_direction_output(desc,
1701 flags & GPIOD_FLAGS_BIT_DIR_VAL);
1702 else
1703 status = gpiod_direction_input(desc);
1704
1705 if (status < 0) {
1706 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
1707 gpiod_put(desc);
1708 return ERR_PTR(status);
1709 }
1710
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001711 return desc;
1712}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001713EXPORT_SYMBOL_GPL(__gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001714
1715/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02001716 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
1717 * function
1718 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1719 * @con_id: function within the GPIO consumer
1720 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001721 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02001722 *
1723 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
1724 * specified index was assigned to the requested function it will return NULL.
1725 * This is convenient for drivers that need to handle optional GPIOs.
1726 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001727struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02001728 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001729 unsigned int index,
1730 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02001731{
1732 struct gpio_desc *desc;
1733
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001734 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001735 if (IS_ERR(desc)) {
1736 if (PTR_ERR(desc) == -ENOENT)
1737 return NULL;
1738 }
1739
1740 return desc;
1741}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001742EXPORT_SYMBOL_GPL(__gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001743
1744/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001745 * gpiod_put - dispose of a GPIO descriptor
1746 * @desc: GPIO descriptor to dispose of
1747 *
1748 * No descriptor can be used after gpiod_put() has been called on it.
1749 */
1750void gpiod_put(struct gpio_desc *desc)
1751{
1752 gpiod_free(desc);
1753}
1754EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08001755
David Brownelld2876d02008-02-04 22:28:20 -08001756#ifdef CONFIG_DEBUG_FS
1757
1758static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1759{
1760 unsigned i;
1761 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001762 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08001763 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02001764 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08001765
1766 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1767 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1768 continue;
1769
Alexandre Courbot372e7222013-02-03 01:29:29 +09001770 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08001771 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001772 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
1773 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08001774 gpio, gdesc->label,
1775 is_out ? "out" : "in ",
1776 chip->get
1777 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02001778 : "? ",
1779 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08001780 seq_printf(s, "\n");
1781 }
1782}
1783
Thierry Redingf9c4a312012-04-12 13:26:01 +02001784static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08001785{
Grant Likely362432a2013-02-09 09:41:49 +00001786 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001787 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001788 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001789
1790 s->private = "";
1791
Grant Likely362432a2013-02-09 09:41:49 +00001792 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001793 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00001794 if (index-- == 0) {
1795 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001796 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00001797 }
1798 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001799
1800 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001801}
1802
1803static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
1804{
Grant Likely362432a2013-02-09 09:41:49 +00001805 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001806 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001807 void *ret = NULL;
1808
Grant Likely362432a2013-02-09 09:41:49 +00001809 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001810 if (list_is_last(&chip->list, &gpio_chips))
1811 ret = NULL;
1812 else
1813 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00001814 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02001815
1816 s->private = "\n";
1817 ++*pos;
1818
1819 return ret;
1820}
1821
1822static void gpiolib_seq_stop(struct seq_file *s, void *v)
1823{
1824}
1825
1826static int gpiolib_seq_show(struct seq_file *s, void *v)
1827{
1828 struct gpio_chip *chip = v;
1829 struct device *dev;
1830
1831 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
1832 chip->base, chip->base + chip->ngpio - 1);
1833 dev = chip->dev;
1834 if (dev)
1835 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
1836 dev_name(dev));
1837 if (chip->label)
1838 seq_printf(s, ", %s", chip->label);
1839 if (chip->can_sleep)
1840 seq_printf(s, ", can sleep");
1841 seq_printf(s, ":\n");
1842
1843 if (chip->dbg_show)
1844 chip->dbg_show(s, chip);
1845 else
1846 gpiolib_dbg_show(s, chip);
1847
David Brownelld2876d02008-02-04 22:28:20 -08001848 return 0;
1849}
1850
Thierry Redingf9c4a312012-04-12 13:26:01 +02001851static const struct seq_operations gpiolib_seq_ops = {
1852 .start = gpiolib_seq_start,
1853 .next = gpiolib_seq_next,
1854 .stop = gpiolib_seq_stop,
1855 .show = gpiolib_seq_show,
1856};
1857
David Brownelld2876d02008-02-04 22:28:20 -08001858static int gpiolib_open(struct inode *inode, struct file *file)
1859{
Thierry Redingf9c4a312012-04-12 13:26:01 +02001860 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08001861}
1862
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001863static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02001864 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08001865 .open = gpiolib_open,
1866 .read = seq_read,
1867 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02001868 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08001869};
1870
1871static int __init gpiolib_debugfs_init(void)
1872{
1873 /* /sys/kernel/debug/gpio */
1874 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1875 NULL, NULL, &gpiolib_operations);
1876 return 0;
1877}
1878subsys_initcall(gpiolib_debugfs_init);
1879
1880#endif /* DEBUG_FS */