blob: 15cc0bb65dda84f8e5b8151722434f6073e61e5b [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 */
311int gpiochip_remove(struct gpio_chip *chip)
312{
313 unsigned long flags;
314 int status = 0;
315 unsigned id;
316
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200317 acpi_gpiochip_remove(chip);
318
David Brownelld2876d02008-02-04 22:28:20 -0800319 spin_lock_irqsave(&gpio_lock, flags);
320
Linus Walleij14250522014-03-25 10:40:18 +0100321 gpiochip_irqchip_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
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900325 for (id = 0; id < chip->ngpio; id++) {
326 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
David Brownelld2876d02008-02-04 22:28:20 -0800327 status = -EBUSY;
328 break;
329 }
330 }
331 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900332 for (id = 0; id < chip->ngpio; id++)
333 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900334
335 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800336 }
337
338 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld8f388d2008-07-25 01:46:07 -0700339
340 if (status == 0)
341 gpiochip_unexport(chip);
342
David Brownelld2876d02008-02-04 22:28:20 -0800343 return status;
344}
345EXPORT_SYMBOL_GPL(gpiochip_remove);
346
Grant Likely594fa262010-06-08 07:48:16 -0600347/**
348 * gpiochip_find() - iterator for locating a specific gpio_chip
349 * @data: data to pass to match function
350 * @callback: Callback function to check gpio_chip
351 *
352 * Similar to bus_find_device. It returns a reference to a gpio_chip as
353 * determined by a user supplied @match callback. The callback should return
354 * 0 if the device doesn't match and non-zero if it does. If the callback is
355 * non-zero, this function will return to the caller and not iterate over any
356 * more gpio_chips.
357 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600358struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700359 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600360 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600361{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900362 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600363 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600364
365 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900366 list_for_each_entry(chip, &gpio_chips, list)
367 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600368 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900369
370 /* No match? */
371 if (&chip->list == &gpio_chips)
372 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600373 spin_unlock_irqrestore(&gpio_lock, flags);
374
375 return chip;
376}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600377EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800378
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900379static int gpiochip_match_name(struct gpio_chip *chip, void *data)
380{
381 const char *name = data;
382
383 return !strcmp(chip->label, name);
384}
385
386static struct gpio_chip *find_chip_by_name(const char *name)
387{
388 return gpiochip_find((void *)name, gpiochip_match_name);
389}
390
Linus Walleij14250522014-03-25 10:40:18 +0100391#ifdef CONFIG_GPIOLIB_IRQCHIP
392
393/*
394 * The following is irqchip helper code for gpiochips.
395 */
396
397/**
398 * gpiochip_add_chained_irqchip() - adds a chained irqchip to a gpiochip
399 * @gpiochip: the gpiochip to add the irqchip to
400 * @irqchip: the irqchip to add to the gpiochip
401 * @parent_irq: the irq number corresponding to the parent IRQ for this
402 * chained irqchip
403 * @parent_handler: the parent interrupt handler for the accumulated IRQ
404 * coming out of the gpiochip
405 */
406void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
407 struct irq_chip *irqchip,
408 int parent_irq,
409 irq_flow_handler_t parent_handler)
410{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200411 if (gpiochip->can_sleep) {
412 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
413 return;
414 }
415
Linus Walleij14250522014-03-25 10:40:18 +0100416 irq_set_chained_handler(parent_irq, parent_handler);
417 /*
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);
422}
423EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
424
Linus Walleije45d1c82014-04-22 14:01:46 +0200425/*
426 * This lock class tells lockdep that GPIO irqs are in a different
427 * category than their parents, so it won't report false recursion.
428 */
429static struct lock_class_key gpiochip_irq_lock_class;
430
Linus Walleij14250522014-03-25 10:40:18 +0100431/**
432 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
433 * @d: the irqdomain used by this irqchip
434 * @irq: the global irq number used by this GPIO irqchip irq
435 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
436 *
437 * This function will set up the mapping for a certain IRQ line on a
438 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
439 * stored inside the gpiochip.
440 */
441static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
442 irq_hw_number_t hwirq)
443{
444 struct gpio_chip *chip = d->host_data;
445
Linus Walleij14250522014-03-25 10:40:18 +0100446 irq_set_chip_data(irq, chip);
Linus Walleije45d1c82014-04-22 14:01:46 +0200447 irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
Linus Walleij7633fb92014-04-09 13:20:38 +0200448 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200449 /* Chips that can sleep need nested thread handlers */
450 if (chip->can_sleep)
451 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100452#ifdef CONFIG_ARM
453 set_irq_flags(irq, IRQF_VALID);
454#else
455 irq_set_noprobe(irq);
456#endif
Linus Walleij1333b902014-04-23 16:45:12 +0200457 /*
458 * No set-up of the hardware will happen if IRQ_TYPE_NONE
459 * is passed as default type.
460 */
461 if (chip->irq_default_type != IRQ_TYPE_NONE)
462 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100463
464 return 0;
465}
466
Linus Walleijc3626fd2014-03-28 20:42:01 +0100467static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
468{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200469 struct gpio_chip *chip = d->host_data;
470
Linus Walleijc3626fd2014-03-28 20:42:01 +0100471#ifdef CONFIG_ARM
472 set_irq_flags(irq, 0);
473#endif
Linus Walleij1c8732b2014-04-09 13:34:39 +0200474 if (chip->can_sleep)
475 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100476 irq_set_chip_and_handler(irq, NULL, NULL);
477 irq_set_chip_data(irq, NULL);
478}
479
Linus Walleij14250522014-03-25 10:40:18 +0100480static const struct irq_domain_ops gpiochip_domain_ops = {
481 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100482 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100483 /* Virtually all GPIO irqchips are twocell:ed */
484 .xlate = irq_domain_xlate_twocell,
485};
486
487static int gpiochip_irq_reqres(struct irq_data *d)
488{
489 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
490
491 if (gpio_lock_as_irq(chip, d->hwirq)) {
492 chip_err(chip,
493 "unable to lock HW IRQ %lu for IRQ\n",
494 d->hwirq);
495 return -EINVAL;
496 }
497 return 0;
498}
499
500static void gpiochip_irq_relres(struct irq_data *d)
501{
502 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
503
504 gpio_unlock_as_irq(chip, d->hwirq);
505}
506
507static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
508{
509 return irq_find_mapping(chip->irqdomain, offset);
510}
511
512/**
513 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
514 * @gpiochip: the gpiochip to remove the irqchip from
515 *
516 * This is called only from gpiochip_remove()
517 */
518static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
519{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100520 unsigned int offset;
521
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300522 acpi_gpiochip_free_interrupts(gpiochip);
523
Linus Walleijc3626fd2014-03-28 20:42:01 +0100524 /* Remove all IRQ mappings and delete the domain */
525 if (gpiochip->irqdomain) {
526 for (offset = 0; offset < gpiochip->ngpio; offset++)
527 irq_dispose_mapping(gpiochip->irq_base + offset);
Linus Walleij14250522014-03-25 10:40:18 +0100528 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100529 }
Linus Walleij14250522014-03-25 10:40:18 +0100530
531 if (gpiochip->irqchip) {
532 gpiochip->irqchip->irq_request_resources = NULL;
533 gpiochip->irqchip->irq_release_resources = NULL;
534 gpiochip->irqchip = NULL;
535 }
536}
537
538/**
539 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
540 * @gpiochip: the gpiochip to add the irqchip to
541 * @irqchip: the irqchip to add to the gpiochip
542 * @first_irq: if not dynamically assigned, the base (first) IRQ to
543 * allocate gpiochip irqs from
544 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200545 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
546 * to have the core avoid setting up any default type in the hardware.
Linus Walleij14250522014-03-25 10:40:18 +0100547 *
548 * This function closely associates a certain irqchip with a certain
549 * gpiochip, providing an irq domain to translate the local IRQs to
550 * global irqs in the gpiolib core, and making sure that the gpiochip
551 * is passed as chip data to all related functions. Driver callbacks
552 * need to use container_of() to get their local state containers back
553 * from the gpiochip passed as chip data. An irqdomain will be stored
554 * in the gpiochip that shall be used by the driver to handle IRQ number
555 * translation. The gpiochip will need to be initialized and registered
556 * before calling this function.
557 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100558 * This function will handle two cell:ed simple IRQs and assumes all
559 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100560 * need to be open coded.
561 */
562int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
563 struct irq_chip *irqchip,
564 unsigned int first_irq,
565 irq_flow_handler_t handler,
566 unsigned int type)
567{
568 struct device_node *of_node;
569 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100570 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100571
572 if (!gpiochip || !irqchip)
573 return -EINVAL;
574
575 if (!gpiochip->dev) {
576 pr_err("missing gpiochip .dev parent pointer\n");
577 return -EINVAL;
578 }
579 of_node = gpiochip->dev->of_node;
580#ifdef CONFIG_OF_GPIO
581 /*
582 * If the gpiochip has an assigned OF node this takes precendence
583 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
584 */
585 if (gpiochip->of_node)
586 of_node = gpiochip->of_node;
587#endif
588 gpiochip->irqchip = irqchip;
589 gpiochip->irq_handler = handler;
590 gpiochip->irq_default_type = type;
591 gpiochip->to_irq = gpiochip_to_irq;
592 gpiochip->irqdomain = irq_domain_add_simple(of_node,
593 gpiochip->ngpio, first_irq,
594 &gpiochip_domain_ops, gpiochip);
595 if (!gpiochip->irqdomain) {
596 gpiochip->irqchip = NULL;
597 return -EINVAL;
598 }
599 irqchip->irq_request_resources = gpiochip_irq_reqres;
600 irqchip->irq_release_resources = gpiochip_irq_relres;
601
602 /*
603 * Prepare the mapping since the irqchip shall be orthogonal to
604 * any gpiochip calls. If the first_irq was zero, this is
605 * necessary to allocate descriptors for all IRQs.
606 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100607 for (offset = 0; offset < gpiochip->ngpio; offset++) {
608 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
609 if (offset == 0)
610 /*
611 * Store the base into the gpiochip to be used when
612 * unmapping the irqs.
613 */
614 gpiochip->irq_base = irq_base;
615 }
Linus Walleij14250522014-03-25 10:40:18 +0100616
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300617 acpi_gpiochip_request_interrupts(gpiochip);
618
Linus Walleij14250522014-03-25 10:40:18 +0100619 return 0;
620}
621EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
622
623#else /* CONFIG_GPIOLIB_IRQCHIP */
624
625static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
626
627#endif /* CONFIG_GPIOLIB_IRQCHIP */
628
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530629#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100630
Linus Walleij3f0f8672012-11-20 12:40:15 +0100631/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200632 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
633 * @chip: the gpiochip to add the range for
634 * @pinctrl: the dev_name() of the pin controller to map to
635 * @gpio_offset: the start offset in the current gpio_chip number space
636 * @pin_group: name of the pin group inside the pin controller
637 */
638int gpiochip_add_pingroup_range(struct gpio_chip *chip,
639 struct pinctrl_dev *pctldev,
640 unsigned int gpio_offset, const char *pin_group)
641{
642 struct gpio_pin_range *pin_range;
643 int ret;
644
645 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
646 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200647 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200648 return -ENOMEM;
649 }
650
651 /* Use local offset as range ID */
652 pin_range->range.id = gpio_offset;
653 pin_range->range.gc = chip;
654 pin_range->range.name = chip->label;
655 pin_range->range.base = chip->base + gpio_offset;
656 pin_range->pctldev = pctldev;
657
658 ret = pinctrl_get_group_pins(pctldev, pin_group,
659 &pin_range->range.pins,
660 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100661 if (ret < 0) {
662 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200663 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100664 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200665
666 pinctrl_add_gpio_range(pctldev, &pin_range->range);
667
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200668 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
669 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200670 pinctrl_dev_get_devname(pctldev), pin_group);
671
672 list_add_tail(&pin_range->node, &chip->pin_ranges);
673
674 return 0;
675}
676EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
677
678/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100679 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
680 * @chip: the gpiochip to add the range for
681 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100682 * @gpio_offset: the start offset in the current gpio_chip number space
683 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100684 * @npins: the number of pins from the offset of each pin space (GPIO and
685 * pin controller) to accumulate in this range
686 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100687int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100688 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100689 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530690{
691 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800692 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530693
Linus Walleij3f0f8672012-11-20 12:40:15 +0100694 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530695 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200696 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100697 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530698 }
699
Linus Walleij3f0f8672012-11-20 12:40:15 +0100700 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100701 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100702 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530703 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100704 pin_range->range.base = chip->base + gpio_offset;
705 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530706 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100707 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530708 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100709 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800710 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200711 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100712 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800713 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100714 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200715 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
716 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100717 pinctl_name,
718 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530719
720 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100721
722 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530723}
Linus Walleij165adc92012-11-06 14:49:39 +0100724EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530725
Linus Walleij3f0f8672012-11-20 12:40:15 +0100726/**
727 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
728 * @chip: the chip to remove all the mappings for
729 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530730void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
731{
732 struct gpio_pin_range *pin_range, *tmp;
733
734 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
735 list_del(&pin_range->node);
736 pinctrl_remove_gpio_range(pin_range->pctldev,
737 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100738 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530739 }
740}
Linus Walleij165adc92012-11-06 14:49:39 +0100741EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
742
743#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530744
David Brownelld2876d02008-02-04 22:28:20 -0800745/* These "optional" allocation calls help prevent drivers from stomping
746 * on each other, and help provide better diagnostics in debugfs.
747 * They're called even less than the "set direction" calls.
748 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200749static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800750{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200751 struct gpio_chip *chip = desc->chip;
752 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800753 unsigned long flags;
754
755 spin_lock_irqsave(&gpio_lock, flags);
756
David Brownelld2876d02008-02-04 22:28:20 -0800757 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700758 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800759 */
760
761 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
762 desc_set_label(desc, label ? : "?");
763 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700764 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800765 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800766 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700767 }
768
769 if (chip->request) {
770 /* chip->request may sleep */
771 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900772 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700773 spin_lock_irqsave(&gpio_lock, flags);
774
775 if (status < 0) {
776 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700777 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300778 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700779 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700780 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300781 if (chip->get_direction) {
782 /* chip->get_direction may sleep */
783 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900784 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300785 spin_lock_irqsave(&gpio_lock, flags);
786 }
David Brownelld2876d02008-02-04 22:28:20 -0800787done:
Mika Westerberg77c2d792014-03-10 14:54:50 +0200788 spin_unlock_irqrestore(&gpio_lock, flags);
789 return status;
790}
791
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900792int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200793{
794 int status = -EPROBE_DEFER;
795 struct gpio_chip *chip;
796
797 if (!desc) {
798 pr_warn("%s: invalid GPIO\n", __func__);
799 return -EINVAL;
800 }
801
802 chip = desc->chip;
803 if (!chip)
804 goto done;
805
806 if (try_module_get(chip->owner)) {
807 status = __gpiod_request(desc, label);
808 if (status < 0)
809 module_put(chip->owner);
810 }
811
812done:
David Brownelld2876d02008-02-04 22:28:20 -0800813 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200814 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200815
David Brownelld2876d02008-02-04 22:28:20 -0800816 return status;
817}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900818
Mika Westerberg77c2d792014-03-10 14:54:50 +0200819static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800820{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200821 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -0800822 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -0700823 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800824
Uwe Kleine-König3d599d12008-10-15 22:03:12 -0700825 might_sleep();
826
Alexandre Courbot372e7222013-02-03 01:29:29 +0900827 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700828
David Brownelld2876d02008-02-04 22:28:20 -0800829 spin_lock_irqsave(&gpio_lock, flags);
830
David Brownell35e8bb52008-10-15 22:03:16 -0700831 chip = desc->chip;
832 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
833 if (chip->free) {
834 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -0700835 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900836 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700837 spin_lock_irqsave(&gpio_lock, flags);
838 }
David Brownelld2876d02008-02-04 22:28:20 -0800839 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -0800840 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -0700841 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530842 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +0530843 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200844 ret = true;
845 }
David Brownelld2876d02008-02-04 22:28:20 -0800846
847 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200848 return ret;
849}
850
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900851void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200852{
853 if (desc && __gpiod_free(desc))
854 module_put(desc->chip->owner);
855 else
856 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -0800857}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900858
David Brownelld2876d02008-02-04 22:28:20 -0800859/**
860 * gpiochip_is_requested - return string iff signal was requested
861 * @chip: controller managing the signal
862 * @offset: of signal within controller's 0..(ngpio - 1) range
863 *
864 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +0900865 * The string returned is the label passed to gpio_request(); if none has been
866 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -0800867 *
868 * This function is for use by GPIO controller drivers. The label can
869 * help with diagnostics, and knowing that the signal is used as a GPIO
870 * can help avoid accidentally multiplexing it to another controller.
871 */
872const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
873{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900874 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800875
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900876 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -0800877 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900878
879 desc = &chip->desc[offset];
880
Alexandre Courbot372e7222013-02-03 01:29:29 +0900881 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -0800882 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900883 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -0800884}
885EXPORT_SYMBOL_GPL(gpiochip_is_requested);
886
Mika Westerberg77c2d792014-03-10 14:54:50 +0200887/**
888 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
889 * @desc: GPIO descriptor to request
890 * @label: label for the GPIO
891 *
892 * Function allows GPIO chip drivers to request and use their own GPIO
893 * descriptors via gpiolib API. Difference to gpiod_request() is that this
894 * function will not increase reference count of the GPIO chip module. This
895 * allows the GPIO chip module to be unloaded as needed (we assume that the
896 * GPIO chip driver handles freeing the GPIOs it has requested).
897 */
898int gpiochip_request_own_desc(struct gpio_desc *desc, const char *label)
899{
900 if (!desc || !desc->chip)
901 return -EINVAL;
902
903 return __gpiod_request(desc, label);
904}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700905EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200906
907/**
908 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
909 * @desc: GPIO descriptor to free
910 *
911 * Function frees the given GPIO requested previously with
912 * gpiochip_request_own_desc().
913 */
914void gpiochip_free_own_desc(struct gpio_desc *desc)
915{
916 if (desc)
917 __gpiod_free(desc);
918}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700919EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -0800920
921/* Drivers MUST set GPIO direction before making get/set calls. In
922 * some cases this is done in early boot, before IRQs are enabled.
923 *
924 * As a rule these aren't called more than once (except for drivers
925 * using the open-drain emulation idiom) so these are natural places
926 * to accumulate extra debugging checks. Note that we can't (yet)
927 * rely on gpio_request() having been called beforehand.
928 */
929
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700930/**
931 * gpiod_direction_input - set the GPIO direction to input
932 * @desc: GPIO to set to input
933 *
934 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
935 * be called safely on it.
936 *
937 * Return 0 in case of success, else an error code.
938 */
939int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800940{
David Brownelld2876d02008-02-04 22:28:20 -0800941 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800942 int status = -EINVAL;
943
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200944 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900945 pr_warn("%s: invalid GPIO\n", __func__);
946 return -EINVAL;
947 }
948
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200949 chip = desc->chip;
950 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +0100951 gpiod_warn(desc,
952 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +0200953 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200954 return -EIO;
955 }
956
Alexandre Courbotd82da792014-07-22 16:17:43 +0900957 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -0800958 if (status == 0)
959 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -0600960
Alexandre Courbot372e7222013-02-03 01:29:29 +0900961 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +0900962
David Brownelld2876d02008-02-04 22:28:20 -0800963 return status;
964}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700965EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900966
Philipp Zabelef70bbe2014-01-07 12:34:11 +0100967static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -0800968{
David Brownelld2876d02008-02-04 22:28:20 -0800969 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800970 int status = -EINVAL;
971
Linus Walleijd468bf92013-09-24 11:54:38 +0200972 /* GPIOs used for IRQs shall not be set as output */
973 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
974 gpiod_err(desc,
975 "%s: tried to set a GPIO tied to an IRQ as output\n",
976 __func__);
977 return -EIO;
978 }
979
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530980 /* Open drain pin should not be driven to 1 */
981 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900982 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530983
Laxman Dewangan25553ff2012-02-17 20:26:22 +0530984 /* Open source pin should not be driven to 0 */
985 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +0900986 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +0530987
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200988 chip = desc->chip;
989 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +0100990 gpiod_warn(desc,
991 "%s: missing set() or direction_output() operations\n",
992 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200993 return -EIO;
994 }
995
Alexandre Courbotd82da792014-07-22 16:17:43 +0900996 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -0800997 if (status == 0)
998 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900999 trace_gpio_value(desc_to_gpio(desc), 0, value);
1000 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001001 return status;
1002}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001003
1004/**
1005 * gpiod_direction_output_raw - set the GPIO direction to output
1006 * @desc: GPIO to set to output
1007 * @value: initial output value of the GPIO
1008 *
1009 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1010 * be called safely on it. The initial value of the output must be specified
1011 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1012 *
1013 * Return 0 in case of success, else an error code.
1014 */
1015int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1016{
1017 if (!desc || !desc->chip) {
1018 pr_warn("%s: invalid GPIO\n", __func__);
1019 return -EINVAL;
1020 }
1021 return _gpiod_direction_output_raw(desc, value);
1022}
1023EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1024
1025/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301026 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001027 * @desc: GPIO to set to output
1028 * @value: initial output value of the GPIO
1029 *
1030 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1031 * be called safely on it. The initial value of the output must be specified
1032 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1033 * account.
1034 *
1035 * Return 0 in case of success, else an error code.
1036 */
1037int gpiod_direction_output(struct gpio_desc *desc, int value)
1038{
1039 if (!desc || !desc->chip) {
1040 pr_warn("%s: invalid GPIO\n", __func__);
1041 return -EINVAL;
1042 }
1043 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1044 value = !value;
1045 return _gpiod_direction_output_raw(desc, value);
1046}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001047EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001048
Felipe Balbic4b5be92010-05-26 14:42:23 -07001049/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001050 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001051 * @gpio: the gpio to set debounce time
1052 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001053 *
1054 * returns -ENOTSUPP if the controller does not support setting
1055 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001056 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001057int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001058{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001059 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001060
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001061 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001062 pr_warn("%s: invalid GPIO\n", __func__);
1063 return -EINVAL;
1064 }
1065
Felipe Balbic4b5be92010-05-26 14:42:23 -07001066 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001067 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001068 gpiod_dbg(desc,
1069 "%s: missing set() or set_debounce() operations\n",
1070 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001071 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001072 }
1073
Alexandre Courbotd82da792014-07-22 16:17:43 +09001074 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001075}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001076EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001077
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001078/**
1079 * gpiod_is_active_low - test whether a GPIO is active-low or not
1080 * @desc: the gpio descriptor to test
1081 *
1082 * Returns 1 if the GPIO is active-low, 0 otherwise.
1083 */
1084int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001085{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001086 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001087}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001088EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001089
1090/* I/O calls are only valid after configuration completed; the relevant
1091 * "is this a valid GPIO" error checks should already have been done.
1092 *
1093 * "Get" operations are often inlinable as reading a pin value register,
1094 * and masking the relevant bit in that register.
1095 *
1096 * When "set" operations are inlinable, they involve writing that mask to
1097 * one register to set a low value, or a different register to set it high.
1098 * Otherwise locking is needed, so there may be little value to inlining.
1099 *
1100 *------------------------------------------------------------------------
1101 *
1102 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1103 * have requested the GPIO. That can include implicit requesting by
1104 * a direction setting call. Marking a gpio as requested locks its chip
1105 * in memory, guaranteeing that these table lookups need no more locking
1106 * and that gpiochip_remove() will fail.
1107 *
1108 * REVISIT when debugging, consider adding some instrumentation to ensure
1109 * that the GPIO was actually requested.
1110 */
1111
Alexandre Courbot23600962014-03-11 15:52:09 +09001112static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001113{
1114 struct gpio_chip *chip;
Alexandre Courbot23600962014-03-11 15:52:09 +09001115 bool value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001116 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001117
Alexandre Courbot372e7222013-02-03 01:29:29 +09001118 chip = desc->chip;
1119 offset = gpio_chip_hwgpio(desc);
Alexandre Courbot23600962014-03-11 15:52:09 +09001120 value = chip->get ? chip->get(chip, offset) : false;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001121 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001122 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001123}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001124
David Brownelld2876d02008-02-04 22:28:20 -08001125/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001126 * gpiod_get_raw_value() - return a gpio's raw value
1127 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001128 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001129 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1130 * its ACTIVE_LOW status.
1131 *
1132 * This function should be called from contexts where we cannot sleep, and will
1133 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001134 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001135int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001136{
David Brownelld2876d02008-02-04 22:28:20 -08001137 if (!desc)
1138 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001139 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001140 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001141 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001142}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001143EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001144
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001145/**
1146 * gpiod_get_value() - return a gpio's value
1147 * @desc: gpio whose value will be returned
1148 *
1149 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1150 * account.
1151 *
1152 * This function should be called from contexts where we cannot sleep, and will
1153 * complain if the GPIO chip functions potentially sleep.
1154 */
1155int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001156{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001157 int value;
1158 if (!desc)
1159 return 0;
1160 /* Should be using gpio_get_value_cansleep() */
1161 WARN_ON(desc->chip->can_sleep);
1162
1163 value = _gpiod_get_raw_value(desc);
1164 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1165 value = !value;
1166
1167 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001168}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001169EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001170
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301171/*
1172 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001173 * @desc: gpio descriptor whose state need to be set.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301174 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1175 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001176static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301177{
1178 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001179 struct gpio_chip *chip = desc->chip;
1180 int offset = gpio_chip_hwgpio(desc);
1181
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301182 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001183 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301184 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001185 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301186 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001187 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301188 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001189 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301190 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001191 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301192 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001193 gpiod_err(desc,
1194 "%s: Error in set_value for open drain err %d\n",
1195 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301196}
1197
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301198/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001199 * _gpio_set_open_source_value() - Set the open source gpio's value.
1200 * @desc: gpio descriptor whose state need to be set.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301201 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1202 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001203static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301204{
1205 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001206 struct gpio_chip *chip = desc->chip;
1207 int offset = gpio_chip_hwgpio(desc);
1208
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301209 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001210 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301211 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001212 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301213 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001214 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301215 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001216 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301217 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001218 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301219 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001220 gpiod_err(desc,
1221 "%s: Error in set_value for open source err %d\n",
1222 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301223}
1224
Alexandre Courbot23600962014-03-11 15:52:09 +09001225static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001226{
1227 struct gpio_chip *chip;
1228
Alexandre Courbot372e7222013-02-03 01:29:29 +09001229 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001230 trace_gpio_value(desc_to_gpio(desc), 0, value);
1231 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1232 _gpio_set_open_drain_value(desc, value);
1233 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1234 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301235 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001236 chip->set(chip, gpio_chip_hwgpio(desc), value);
1237}
1238
David Brownelld2876d02008-02-04 22:28:20 -08001239/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001240 * gpiod_set_raw_value() - assign a gpio's raw value
1241 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001242 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001243 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001244 * Set the raw value of the GPIO, i.e. the value of its physical line without
1245 * regard for its ACTIVE_LOW status.
1246 *
1247 * This function should be called from contexts where we cannot sleep, and will
1248 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001249 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001250void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001251{
David Brownelld2876d02008-02-04 22:28:20 -08001252 if (!desc)
1253 return;
David Brownelld2876d02008-02-04 22:28:20 -08001254 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001255 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001256 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001257}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001258EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001259
1260/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001261 * gpiod_set_value() - assign a gpio's value
1262 * @desc: gpio whose value will be assigned
1263 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001264 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001265 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1266 * account
1267 *
1268 * This function should be called from contexts where we cannot sleep, and will
1269 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001270 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001271void gpiod_set_value(struct gpio_desc *desc, int value)
1272{
1273 if (!desc)
1274 return;
1275 /* Should be using gpio_set_value_cansleep() */
1276 WARN_ON(desc->chip->can_sleep);
1277 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1278 value = !value;
1279 _gpiod_set_raw_value(desc, value);
1280}
1281EXPORT_SYMBOL_GPL(gpiod_set_value);
1282
1283/**
1284 * gpiod_cansleep() - report whether gpio value access may sleep
1285 * @desc: gpio to check
1286 *
1287 */
1288int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001289{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001290 if (!desc)
1291 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001292 return desc->chip->can_sleep;
1293}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001294EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001295
David Brownell0f6d5042008-10-15 22:03:14 -07001296/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001297 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1298 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001299 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001300 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1301 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001302 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001303int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001304{
1305 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001306 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001307
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001308 if (!desc)
1309 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001310 chip = desc->chip;
1311 offset = gpio_chip_hwgpio(desc);
1312 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1313}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001314EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001315
Linus Walleijd468bf92013-09-24 11:54:38 +02001316/**
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001317 * gpio_lock_as_irq() - lock a GPIO to be used as IRQ
1318 * @chip: the chip the GPIO to lock belongs to
1319 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001320 *
1321 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001322 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001323 */
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001324int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001325{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001326 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001327 return -EINVAL;
1328
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001329 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1330 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001331 "%s: tried to flag a GPIO set as output for IRQ\n",
1332 __func__);
1333 return -EIO;
1334 }
1335
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001336 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001337 return 0;
1338}
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001339EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001340
Linus Walleijd468bf92013-09-24 11:54:38 +02001341/**
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001342 * gpio_unlock_as_irq() - unlock a GPIO used as IRQ
1343 * @chip: the chip the GPIO to lock belongs to
1344 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001345 *
1346 * This is used directly by GPIO drivers that want to indicate
1347 * that a certain GPIO is no longer used exclusively for IRQ.
1348 */
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001349void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001350{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001351 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001352 return;
1353
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001354 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001355}
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001356EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001357
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001358/**
1359 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1360 * @desc: gpio whose value will be returned
1361 *
1362 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1363 * its ACTIVE_LOW status.
1364 *
1365 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001366 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001367int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001368{
David Brownelld2876d02008-02-04 22:28:20 -08001369 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001370 if (!desc)
1371 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001372 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001373}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001374EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001375
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001376/**
1377 * gpiod_get_value_cansleep() - return a gpio's value
1378 * @desc: gpio whose value will be returned
1379 *
1380 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1381 * account.
1382 *
1383 * This function is to be called from contexts that can sleep.
1384 */
1385int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001386{
David Brownelld2876d02008-02-04 22:28:20 -08001387 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001388
1389 might_sleep_if(extra_checks);
1390 if (!desc)
1391 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001392
1393 value = _gpiod_get_raw_value(desc);
1394 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1395 value = !value;
1396
David Brownelld2876d02008-02-04 22:28:20 -08001397 return value;
1398}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001399EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001400
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001401/**
1402 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1403 * @desc: gpio whose value will be assigned
1404 * @value: value to assign
1405 *
1406 * Set the raw value of the GPIO, i.e. the value of its physical line without
1407 * regard for its ACTIVE_LOW status.
1408 *
1409 * This function is to be called from contexts that can sleep.
1410 */
1411void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001412{
David Brownelld2876d02008-02-04 22:28:20 -08001413 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001414 if (!desc)
1415 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001416 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001417}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001418EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001419
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001420/**
1421 * gpiod_set_value_cansleep() - assign a gpio's value
1422 * @desc: gpio whose value will be assigned
1423 * @value: value to assign
1424 *
1425 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1426 * account
1427 *
1428 * This function is to be called from contexts that can sleep.
1429 */
1430void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001431{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001432 might_sleep_if(extra_checks);
1433 if (!desc)
1434 return;
1435
1436 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1437 value = !value;
1438 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001439}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001440EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001441
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001442/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001443 * gpiod_add_lookup_table() - register GPIO device consumers
1444 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001445 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001446void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001447{
1448 mutex_lock(&gpio_lookup_lock);
1449
Alexandre Courbotad824782013-12-03 12:20:11 +09001450 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001451
1452 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001453}
1454
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001455static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001456 unsigned int idx,
1457 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001458{
Thierry Redingdd34c372014-04-23 17:28:09 +02001459 static const char *suffixes[] = { "gpios", "gpio" };
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001460 char prop_name[32]; /* 32 is max size of property name */
1461 enum of_gpio_flags of_flags;
1462 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001463 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001464
Thierry Redingdd34c372014-04-23 17:28:09 +02001465 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1466 if (con_id)
1467 snprintf(prop_name, 32, "%s-%s", con_id, suffixes[i]);
1468 else
1469 snprintf(prop_name, 32, "%s", suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001470
Thierry Redingdd34c372014-04-23 17:28:09 +02001471 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1472 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001473 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001474 break;
1475 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001476
1477 if (IS_ERR(desc))
1478 return desc;
1479
1480 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001481 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001482
1483 return desc;
1484}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001485
Mika Westerberg81f59e92013-10-10 11:01:09 +03001486static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001487 unsigned int idx,
1488 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001489{
Mika Westerberge01f4402013-10-10 11:01:10 +03001490 struct acpi_gpio_info info;
1491 struct gpio_desc *desc;
1492
1493 desc = acpi_get_gpiod_by_index(dev, idx, &info);
1494 if (IS_ERR(desc))
1495 return desc;
1496
1497 if (info.gpioint && info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001498 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001499
1500 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001501}
1502
Alexandre Courbotad824782013-12-03 12:20:11 +09001503static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1504{
1505 const char *dev_id = dev ? dev_name(dev) : NULL;
1506 struct gpiod_lookup_table *table;
1507
1508 mutex_lock(&gpio_lookup_lock);
1509
1510 list_for_each_entry(table, &gpio_lookup_list, list) {
1511 if (table->dev_id && dev_id) {
1512 /*
1513 * Valid strings on both ends, must be identical to have
1514 * a match
1515 */
1516 if (!strcmp(table->dev_id, dev_id))
1517 goto found;
1518 } else {
1519 /*
1520 * One of the pointers is NULL, so both must be to have
1521 * a match
1522 */
1523 if (dev_id == table->dev_id)
1524 goto found;
1525 }
1526 }
1527 table = NULL;
1528
1529found:
1530 mutex_unlock(&gpio_lookup_lock);
1531 return table;
1532}
1533
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001534static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001535 unsigned int idx,
1536 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001537{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001538 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001539 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001540 struct gpiod_lookup *p;
1541
Alexandre Courbotad824782013-12-03 12:20:11 +09001542 table = gpiod_find_lookup_table(dev);
1543 if (!table)
1544 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001545
Alexandre Courbotad824782013-12-03 12:20:11 +09001546 for (p = &table->table[0]; p->chip_label; p++) {
1547 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001548
Alexandre Courbotad824782013-12-03 12:20:11 +09001549 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001550 if (p->idx != idx)
1551 continue;
1552
Alexandre Courbotad824782013-12-03 12:20:11 +09001553 /* If the lookup entry has a con_id, require exact match */
1554 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1555 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001556
Alexandre Courbotad824782013-12-03 12:20:11 +09001557 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001558
Alexandre Courbotad824782013-12-03 12:20:11 +09001559 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001560 dev_err(dev, "cannot find GPIO chip %s\n",
1561 p->chip_label);
1562 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001563 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001564
Alexandre Courbotad824782013-12-03 12:20:11 +09001565 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001566 dev_err(dev,
1567 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1568 idx, chip->ngpio, chip->label);
1569 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001570 }
1571
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001572 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001573 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001574
1575 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001576 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001577
1578 return desc;
1579}
1580
1581/**
Thierry Reding08791622014-04-25 16:54:22 +02001582 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09001583 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001584 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001585 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001586 *
1587 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001588 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1589 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001590 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001591struct gpio_desc *__must_check __gpiod_get(struct device *dev, const char *con_id,
1592 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001593{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001594 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001595}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001596EXPORT_SYMBOL_GPL(__gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001597
1598/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02001599 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1600 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1601 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001602 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02001603 *
1604 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1605 * the requested function it will return NULL. This is convenient for drivers
1606 * that need to handle optional GPIOs.
1607 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001608struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
1609 const char *con_id,
1610 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02001611{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001612 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001613}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001614EXPORT_SYMBOL_GPL(__gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001615
1616/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001617 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02001618 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001619 * @con_id: function within the GPIO consumer
1620 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001621 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001622 *
1623 * This variant of gpiod_get() allows to access GPIOs other than the first
1624 * defined one for functions that define several GPIOs.
1625 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001626 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1627 * requested function and/or index, or another IS_ERR() code if an error
1628 * occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001629 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001630struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001631 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001632 unsigned int idx,
1633 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001634{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001635 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001636 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001637 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001638
1639 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
1640
1641 /* Using device tree? */
1642 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
1643 dev_dbg(dev, "using device tree for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001644 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
Mika Westerberg81f59e92013-10-10 11:01:09 +03001645 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
1646 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001647 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001648 }
1649
1650 /*
1651 * Either we are not using DT or ACPI, or their lookup did not return
1652 * a result. In that case, use platform lookup as a fallback.
1653 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001654 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001655 dev_dbg(dev, "using lookup tables for GPIO lookup");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001656 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001657 }
1658
1659 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02001660 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001661 return desc;
1662 }
1663
1664 status = gpiod_request(desc, con_id);
1665
1666 if (status < 0)
1667 return ERR_PTR(status);
1668
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001669 if (lookupflags & GPIO_ACTIVE_LOW)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001670 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001671 if (lookupflags & GPIO_OPEN_DRAIN)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001672 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001673 if (lookupflags & GPIO_OPEN_SOURCE)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001674 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001675
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001676 /* No particular flag request, return here... */
1677 if (flags & GPIOD_FLAGS_BIT_DIR_SET)
1678 return desc;
1679
1680 /* Process flags */
1681 if (flags & GPIOD_FLAGS_BIT_DIR_OUT)
1682 status = gpiod_direction_output(desc,
1683 flags & GPIOD_FLAGS_BIT_DIR_VAL);
1684 else
1685 status = gpiod_direction_input(desc);
1686
1687 if (status < 0) {
1688 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
1689 gpiod_put(desc);
1690 return ERR_PTR(status);
1691 }
1692
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001693 return desc;
1694}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001695EXPORT_SYMBOL_GPL(__gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001696
1697/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02001698 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
1699 * function
1700 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1701 * @con_id: function within the GPIO consumer
1702 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001703 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02001704 *
1705 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
1706 * specified index was assigned to the requested function it will return NULL.
1707 * This is convenient for drivers that need to handle optional GPIOs.
1708 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001709struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02001710 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001711 unsigned int index,
1712 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02001713{
1714 struct gpio_desc *desc;
1715
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001716 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001717 if (IS_ERR(desc)) {
1718 if (PTR_ERR(desc) == -ENOENT)
1719 return NULL;
1720 }
1721
1722 return desc;
1723}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001724EXPORT_SYMBOL_GPL(__gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001725
1726/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001727 * gpiod_put - dispose of a GPIO descriptor
1728 * @desc: GPIO descriptor to dispose of
1729 *
1730 * No descriptor can be used after gpiod_put() has been called on it.
1731 */
1732void gpiod_put(struct gpio_desc *desc)
1733{
1734 gpiod_free(desc);
1735}
1736EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08001737
David Brownelld2876d02008-02-04 22:28:20 -08001738#ifdef CONFIG_DEBUG_FS
1739
1740static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1741{
1742 unsigned i;
1743 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001744 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08001745 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02001746 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08001747
1748 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1749 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1750 continue;
1751
Alexandre Courbot372e7222013-02-03 01:29:29 +09001752 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08001753 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001754 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
1755 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08001756 gpio, gdesc->label,
1757 is_out ? "out" : "in ",
1758 chip->get
1759 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02001760 : "? ",
1761 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08001762 seq_printf(s, "\n");
1763 }
1764}
1765
Thierry Redingf9c4a312012-04-12 13:26:01 +02001766static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08001767{
Grant Likely362432a2013-02-09 09:41:49 +00001768 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001769 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001770 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001771
1772 s->private = "";
1773
Grant Likely362432a2013-02-09 09:41:49 +00001774 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001775 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00001776 if (index-- == 0) {
1777 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001778 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00001779 }
1780 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001781
1782 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001783}
1784
1785static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
1786{
Grant Likely362432a2013-02-09 09:41:49 +00001787 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001788 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02001789 void *ret = NULL;
1790
Grant Likely362432a2013-02-09 09:41:49 +00001791 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09001792 if (list_is_last(&chip->list, &gpio_chips))
1793 ret = NULL;
1794 else
1795 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00001796 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02001797
1798 s->private = "\n";
1799 ++*pos;
1800
1801 return ret;
1802}
1803
1804static void gpiolib_seq_stop(struct seq_file *s, void *v)
1805{
1806}
1807
1808static int gpiolib_seq_show(struct seq_file *s, void *v)
1809{
1810 struct gpio_chip *chip = v;
1811 struct device *dev;
1812
1813 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
1814 chip->base, chip->base + chip->ngpio - 1);
1815 dev = chip->dev;
1816 if (dev)
1817 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
1818 dev_name(dev));
1819 if (chip->label)
1820 seq_printf(s, ", %s", chip->label);
1821 if (chip->can_sleep)
1822 seq_printf(s, ", can sleep");
1823 seq_printf(s, ":\n");
1824
1825 if (chip->dbg_show)
1826 chip->dbg_show(s, chip);
1827 else
1828 gpiolib_dbg_show(s, chip);
1829
David Brownelld2876d02008-02-04 22:28:20 -08001830 return 0;
1831}
1832
Thierry Redingf9c4a312012-04-12 13:26:01 +02001833static const struct seq_operations gpiolib_seq_ops = {
1834 .start = gpiolib_seq_start,
1835 .next = gpiolib_seq_next,
1836 .stop = gpiolib_seq_stop,
1837 .show = gpiolib_seq_show,
1838};
1839
David Brownelld2876d02008-02-04 22:28:20 -08001840static int gpiolib_open(struct inode *inode, struct file *file)
1841{
Thierry Redingf9c4a312012-04-12 13:26:01 +02001842 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08001843}
1844
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001845static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02001846 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08001847 .open = gpiolib_open,
1848 .read = seq_read,
1849 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02001850 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08001851};
1852
1853static int __init gpiolib_debugfs_init(void)
1854{
1855 /* /sys/kernel/debug/gpio */
1856 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1857 NULL, NULL, &gpiolib_operations);
1858 return 0;
1859}
1860subsys_initcall(gpiolib_debugfs_init);
1861
1862#endif /* DEBUG_FS */