blob: 487afe6f22fcd6872b16995cffcb8179c374a276 [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
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +090050#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
51
Alexandre Courbotbae48da2013-10-17 10:21:38 -070052static DEFINE_MUTEX(gpio_lookup_lock);
53static LIST_HEAD(gpio_lookup_list);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090054LIST_HEAD(gpio_chips);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +020055
David Brownelld2876d02008-02-04 22:28:20 -080056static inline void desc_set_label(struct gpio_desc *d, const char *label)
57{
David Brownelld2876d02008-02-04 22:28:20 -080058 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080059}
60
Alexandre Courbot372e7222013-02-03 01:29:29 +090061/**
62 * Convert a GPIO number to its descriptor
63 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070064struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090065{
Alexandre Courbot14e85c02014-11-19 16:51:27 +090066 struct gpio_chip *chip;
67 unsigned long flags;
68
69 spin_lock_irqsave(&gpio_lock, flags);
70
71 list_for_each_entry(chip, &gpio_chips, list) {
72 if (chip->base <= gpio && chip->base + chip->ngpio > gpio) {
73 spin_unlock_irqrestore(&gpio_lock, flags);
74 return &chip->desc[gpio - chip->base];
75 }
76 }
77
78 spin_unlock_irqrestore(&gpio_lock, flags);
79
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +090080 if (!gpio_is_valid(gpio))
81 WARN(1, "invalid GPIO %d\n", gpio);
82
Alexandre Courbot14e85c02014-11-19 16:51:27 +090083 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +090084}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070085EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090086
87/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090088 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +020089 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090090struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
91 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +020092{
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090093 if (hwnum >= chip->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +090094 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +020095
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090096 return &chip->desc[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +020097}
Alexandre Courbot372e7222013-02-03 01:29:29 +090098
99/**
100 * Convert a GPIO descriptor to the integer namespace.
101 * This should disappear in the future but is needed since we still
102 * use GPIO numbers for error messages and sysfs nodes
103 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700104int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900105{
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900106 return desc->chip->base + (desc - &desc->chip->desc[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900107}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700108EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900109
110
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700111/**
112 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
113 * @desc: descriptor to return the chip of
114 */
115struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900116{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900117 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900118}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700119EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800120
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700121/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
122static int gpiochip_find_base(int ngpio)
123{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900124 struct gpio_chip *chip;
125 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700126
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900127 list_for_each_entry_reverse(chip, &gpio_chips, list) {
128 /* found a free space? */
129 if (chip->base + chip->ngpio <= base)
130 break;
131 else
132 /* nope, check the space right before the chip */
133 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700134 }
135
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900136 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700137 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900138 return base;
139 } else {
140 pr_err("%s: cannot find free range\n", __func__);
141 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700142 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700143}
144
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700145/**
146 * gpiod_get_direction - return the current direction of a GPIO
147 * @desc: GPIO to get the direction of
148 *
149 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
150 *
151 * This function may sleep if gpiod_cansleep() is true.
152 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900153int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300154{
155 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900156 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300157 int status = -EINVAL;
158
Alexandre Courbot372e7222013-02-03 01:29:29 +0900159 chip = gpiod_to_chip(desc);
160 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300161
162 if (!chip->get_direction)
163 return status;
164
Alexandre Courbot372e7222013-02-03 01:29:29 +0900165 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300166 if (status > 0) {
167 /* GPIOF_DIR_IN, or other positive */
168 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900169 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300170 }
171 if (status == 0) {
172 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900173 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300174 }
175 return status;
176}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700177EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300178
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900179/*
180 * Add a new chip to the global chips list, keeping the list of chips sorted
181 * by base order.
182 *
183 * Return -EBUSY if the new chip overlaps with some other chip's integer
184 * space.
185 */
186static int gpiochip_add_to_list(struct gpio_chip *chip)
187{
188 struct list_head *pos = &gpio_chips;
189 struct gpio_chip *_chip;
190 int err = 0;
191
192 /* find where to insert our chip */
193 list_for_each(pos, &gpio_chips) {
194 _chip = list_entry(pos, struct gpio_chip, list);
195 /* shall we insert before _chip? */
196 if (_chip->base >= chip->base + chip->ngpio)
197 break;
198 }
199
200 /* are we stepping on the chip right before? */
201 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
202 _chip = list_entry(pos->prev, struct gpio_chip, list);
203 if (_chip->base + _chip->ngpio > chip->base) {
204 dev_err(chip->dev,
205 "GPIO integer space overlap, cannot add chip\n");
206 err = -EBUSY;
207 }
208 }
209
210 if (!err)
211 list_add_tail(&chip->list, pos);
212
213 return err;
214}
215
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700216/**
David Brownelld2876d02008-02-04 22:28:20 -0800217 * gpiochip_add() - register a gpio_chip
218 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900219 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -0800220 *
221 * Returns a negative errno if the chip can't be registered, such as
222 * because the chip->base is invalid or already associated with a
223 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700224 *
David Brownelld8f388d2008-07-25 01:46:07 -0700225 * When gpiochip_add() is called very early during boot, so that GPIOs
226 * can be freely used, the chip->dev device must be registered before
227 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
228 * for GPIOs will fail rudely.
229 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700230 * If chip->base is negative, this requests dynamic assignment of
231 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800232 */
233int gpiochip_add(struct gpio_chip *chip)
234{
235 unsigned long flags;
236 int status = 0;
237 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700238 int base = chip->base;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900239 struct gpio_desc *descs;
David Brownelld2876d02008-02-04 22:28:20 -0800240
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900241 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
242 if (!descs)
243 return -ENOMEM;
David Brownelld2876d02008-02-04 22:28:20 -0800244
245 spin_lock_irqsave(&gpio_lock, flags);
246
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700247 if (base < 0) {
248 base = gpiochip_find_base(chip->ngpio);
249 if (base < 0) {
250 status = base;
David Brownelld8f388d2008-07-25 01:46:07 -0700251 goto unlock;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700252 }
253 chip->base = base;
254 }
255
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900256 status = gpiochip_add_to_list(chip);
257
David Brownelld2876d02008-02-04 22:28:20 -0800258 if (status == 0) {
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900259 for (id = 0; id < chip->ngpio; id++) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900260 struct gpio_desc *desc = &descs[id];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900261 desc->chip = chip;
David Brownelld8f388d2008-07-25 01:46:07 -0700262
263 /* REVISIT: most hardware initializes GPIOs as
264 * inputs (often with pullups enabled) so power
265 * usage is minimized. Linux code should set the
266 * gpio direction first thing; but until it does,
Mathias Nyman80b0a602012-10-24 17:25:27 +0300267 * and in case chip->get_direction is not set,
David Brownelld8f388d2008-07-25 01:46:07 -0700268 * we may expose the wrong direction in sysfs.
269 */
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900270 desc->flags = !chip->direction_input
David Brownelld8f388d2008-07-25 01:46:07 -0700271 ? (1 << FLAG_IS_OUT)
272 : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800273 }
274 }
275
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900276 chip->desc = descs;
277
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800278 spin_unlock_irqrestore(&gpio_lock, flags);
279
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530280#ifdef CONFIG_PINCTRL
281 INIT_LIST_HEAD(&chip->pin_ranges);
282#endif
283
Anton Vorontsov391c9702010-06-08 07:48:17 -0600284 of_gpiochip_add(chip);
Mika Westerberg664e3e52014-01-08 12:40:54 +0200285 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600286
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600287 if (status)
288 goto fail;
289
290 status = gpiochip_export(chip);
291 if (status)
292 goto fail;
293
Andy Shevchenko7589e592013-12-05 11:26:23 +0200294 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700295 chip->base, chip->base + chip->ngpio - 1,
296 chip->label ? : "generic");
297
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600298 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800299
300unlock:
301 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownelld2876d02008-02-04 22:28:20 -0800302fail:
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900303 kfree(descs);
304 chip->desc = NULL;
305
David Brownelld2876d02008-02-04 22:28:20 -0800306 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200307 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600308 chip->base, chip->base + chip->ngpio - 1,
309 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800310 return status;
311}
312EXPORT_SYMBOL_GPL(gpiochip_add);
313
Linus Walleij14250522014-03-25 10:40:18 +0100314/* Forward-declaration */
315static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
316
David Brownelld2876d02008-02-04 22:28:20 -0800317/**
318 * gpiochip_remove() - unregister a gpio_chip
319 * @chip: the chip to unregister
320 *
321 * A gpio_chip with any GPIOs still requested may not be removed.
322 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200323void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800324{
325 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800326 unsigned id;
327
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200328 acpi_gpiochip_remove(chip);
329
David Brownelld2876d02008-02-04 22:28:20 -0800330 spin_lock_irqsave(&gpio_lock, flags);
331
Linus Walleij14250522014-03-25 10:40:18 +0100332 gpiochip_irqchip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100333 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600334 of_gpiochip_remove(chip);
335
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900336 for (id = 0; id < chip->ngpio; id++) {
abdoulaye berthee1db1702014-07-05 18:28:50 +0200337 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags))
338 dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
David Brownelld2876d02008-02-04 22:28:20 -0800339 }
abdoulaye berthee1db1702014-07-05 18:28:50 +0200340 for (id = 0; id < chip->ngpio; id++)
341 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900342
abdoulaye berthee1db1702014-07-05 18:28:50 +0200343 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800344 spin_unlock_irqrestore(&gpio_lock, flags);
abdoulaye berthee1db1702014-07-05 18:28:50 +0200345 gpiochip_unexport(chip);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900346
347 kfree(chip->desc);
348 chip->desc = NULL;
David Brownelld2876d02008-02-04 22:28:20 -0800349}
350EXPORT_SYMBOL_GPL(gpiochip_remove);
351
Grant Likely594fa262010-06-08 07:48:16 -0600352/**
353 * gpiochip_find() - iterator for locating a specific gpio_chip
354 * @data: data to pass to match function
355 * @callback: Callback function to check gpio_chip
356 *
357 * Similar to bus_find_device. It returns a reference to a gpio_chip as
358 * determined by a user supplied @match callback. The callback should return
359 * 0 if the device doesn't match and non-zero if it does. If the callback is
360 * non-zero, this function will return to the caller and not iterate over any
361 * more gpio_chips.
362 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600363struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700364 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600365 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600366{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900367 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600368 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600369
370 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900371 list_for_each_entry(chip, &gpio_chips, list)
372 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600373 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900374
375 /* No match? */
376 if (&chip->list == &gpio_chips)
377 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600378 spin_unlock_irqrestore(&gpio_lock, flags);
379
380 return chip;
381}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600382EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800383
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900384static int gpiochip_match_name(struct gpio_chip *chip, void *data)
385{
386 const char *name = data;
387
388 return !strcmp(chip->label, name);
389}
390
391static struct gpio_chip *find_chip_by_name(const char *name)
392{
393 return gpiochip_find((void *)name, gpiochip_match_name);
394}
395
Linus Walleij14250522014-03-25 10:40:18 +0100396#ifdef CONFIG_GPIOLIB_IRQCHIP
397
398/*
399 * The following is irqchip helper code for gpiochips.
400 */
401
402/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200403 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
404 * @gpiochip: the gpiochip to set the irqchip chain to
405 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100406 * @parent_irq: the irq number corresponding to the parent IRQ for this
407 * chained irqchip
408 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200409 * coming out of the gpiochip. If the interrupt is nested rather than
410 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100411 */
412void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
413 struct irq_chip *irqchip,
414 int parent_irq,
415 irq_flow_handler_t parent_handler)
416{
Linus Walleij83141a72014-09-26 13:50:12 +0200417 unsigned int offset;
418
Linus Walleij83141a72014-09-26 13:50:12 +0200419 if (!gpiochip->irqdomain) {
420 chip_err(gpiochip, "called %s before setting up irqchip\n",
421 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200422 return;
423 }
424
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200425 if (parent_handler) {
426 if (gpiochip->can_sleep) {
427 chip_err(gpiochip,
428 "you cannot have chained interrupts on a "
429 "chip that may sleep\n");
430 return;
431 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200432 /*
433 * The parent irqchip is already using the chip_data for this
434 * irqchip, so our callbacks simply use the handler_data.
435 */
436 irq_set_handler_data(parent_irq, gpiochip);
Linus Torvaldsea584592014-10-09 14:58:15 -0400437 irq_set_chained_handler(parent_irq, parent_handler);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200438 }
Linus Walleij83141a72014-09-26 13:50:12 +0200439
440 /* Set the parent IRQ for all affected IRQs */
441 for (offset = 0; offset < gpiochip->ngpio; offset++)
442 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
443 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100444}
445EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
446
Linus Walleije45d1c82014-04-22 14:01:46 +0200447/*
448 * This lock class tells lockdep that GPIO irqs are in a different
449 * category than their parents, so it won't report false recursion.
450 */
451static struct lock_class_key gpiochip_irq_lock_class;
452
Linus Walleij14250522014-03-25 10:40:18 +0100453/**
454 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
455 * @d: the irqdomain used by this irqchip
456 * @irq: the global irq number used by this GPIO irqchip irq
457 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
458 *
459 * This function will set up the mapping for a certain IRQ line on a
460 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
461 * stored inside the gpiochip.
462 */
463static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
464 irq_hw_number_t hwirq)
465{
466 struct gpio_chip *chip = d->host_data;
467
Linus Walleij14250522014-03-25 10:40:18 +0100468 irq_set_chip_data(irq, chip);
Linus Walleije45d1c82014-04-22 14:01:46 +0200469 irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
Linus Walleij7633fb92014-04-09 13:20:38 +0200470 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200471 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300472 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200473 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100474#ifdef CONFIG_ARM
475 set_irq_flags(irq, IRQF_VALID);
476#else
477 irq_set_noprobe(irq);
478#endif
Linus Walleij1333b902014-04-23 16:45:12 +0200479 /*
480 * No set-up of the hardware will happen if IRQ_TYPE_NONE
481 * is passed as default type.
482 */
483 if (chip->irq_default_type != IRQ_TYPE_NONE)
484 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100485
486 return 0;
487}
488
Linus Walleijc3626fd2014-03-28 20:42:01 +0100489static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
490{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200491 struct gpio_chip *chip = d->host_data;
492
Linus Walleijc3626fd2014-03-28 20:42:01 +0100493#ifdef CONFIG_ARM
494 set_irq_flags(irq, 0);
495#endif
Linus Walleij1c8732b2014-04-09 13:34:39 +0200496 if (chip->can_sleep)
497 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100498 irq_set_chip_and_handler(irq, NULL, NULL);
499 irq_set_chip_data(irq, NULL);
500}
501
Linus Walleij14250522014-03-25 10:40:18 +0100502static const struct irq_domain_ops gpiochip_domain_ops = {
503 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100504 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100505 /* Virtually all GPIO irqchips are twocell:ed */
506 .xlate = irq_domain_xlate_twocell,
507};
508
509static int gpiochip_irq_reqres(struct irq_data *d)
510{
511 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
512
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900513 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100514 chip_err(chip,
515 "unable to lock HW IRQ %lu for IRQ\n",
516 d->hwirq);
517 return -EINVAL;
518 }
519 return 0;
520}
521
522static void gpiochip_irq_relres(struct irq_data *d)
523{
524 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
525
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900526 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleij14250522014-03-25 10:40:18 +0100527}
528
529static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
530{
531 return irq_find_mapping(chip->irqdomain, offset);
532}
533
534/**
535 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
536 * @gpiochip: the gpiochip to remove the irqchip from
537 *
538 * This is called only from gpiochip_remove()
539 */
540static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
541{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100542 unsigned int offset;
543
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300544 acpi_gpiochip_free_interrupts(gpiochip);
545
Linus Walleijc3626fd2014-03-28 20:42:01 +0100546 /* Remove all IRQ mappings and delete the domain */
547 if (gpiochip->irqdomain) {
548 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300549 irq_dispose_mapping(
550 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100551 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100552 }
Linus Walleij14250522014-03-25 10:40:18 +0100553
554 if (gpiochip->irqchip) {
555 gpiochip->irqchip->irq_request_resources = NULL;
556 gpiochip->irqchip->irq_release_resources = NULL;
557 gpiochip->irqchip = NULL;
558 }
559}
560
561/**
562 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
563 * @gpiochip: the gpiochip to add the irqchip to
564 * @irqchip: the irqchip to add to the gpiochip
565 * @first_irq: if not dynamically assigned, the base (first) IRQ to
566 * allocate gpiochip irqs from
567 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200568 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
569 * to have the core avoid setting up any default type in the hardware.
Linus Walleij14250522014-03-25 10:40:18 +0100570 *
571 * This function closely associates a certain irqchip with a certain
572 * gpiochip, providing an irq domain to translate the local IRQs to
573 * global irqs in the gpiolib core, and making sure that the gpiochip
574 * is passed as chip data to all related functions. Driver callbacks
575 * need to use container_of() to get their local state containers back
576 * from the gpiochip passed as chip data. An irqdomain will be stored
577 * in the gpiochip that shall be used by the driver to handle IRQ number
578 * translation. The gpiochip will need to be initialized and registered
579 * before calling this function.
580 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100581 * This function will handle two cell:ed simple IRQs and assumes all
582 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100583 * need to be open coded.
584 */
585int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
586 struct irq_chip *irqchip,
587 unsigned int first_irq,
588 irq_flow_handler_t handler,
589 unsigned int type)
590{
591 struct device_node *of_node;
592 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100593 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100594
595 if (!gpiochip || !irqchip)
596 return -EINVAL;
597
598 if (!gpiochip->dev) {
599 pr_err("missing gpiochip .dev parent pointer\n");
600 return -EINVAL;
601 }
602 of_node = gpiochip->dev->of_node;
603#ifdef CONFIG_OF_GPIO
604 /*
605 * If the gpiochip has an assigned OF node this takes precendence
606 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
607 */
608 if (gpiochip->of_node)
609 of_node = gpiochip->of_node;
610#endif
611 gpiochip->irqchip = irqchip;
612 gpiochip->irq_handler = handler;
613 gpiochip->irq_default_type = type;
614 gpiochip->to_irq = gpiochip_to_irq;
615 gpiochip->irqdomain = irq_domain_add_simple(of_node,
616 gpiochip->ngpio, first_irq,
617 &gpiochip_domain_ops, gpiochip);
618 if (!gpiochip->irqdomain) {
619 gpiochip->irqchip = NULL;
620 return -EINVAL;
621 }
622 irqchip->irq_request_resources = gpiochip_irq_reqres;
623 irqchip->irq_release_resources = gpiochip_irq_relres;
624
625 /*
626 * Prepare the mapping since the irqchip shall be orthogonal to
627 * any gpiochip calls. If the first_irq was zero, this is
628 * necessary to allocate descriptors for all IRQs.
629 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100630 for (offset = 0; offset < gpiochip->ngpio; offset++) {
631 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
632 if (offset == 0)
633 /*
634 * Store the base into the gpiochip to be used when
635 * unmapping the irqs.
636 */
637 gpiochip->irq_base = irq_base;
638 }
Linus Walleij14250522014-03-25 10:40:18 +0100639
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300640 acpi_gpiochip_request_interrupts(gpiochip);
641
Linus Walleij14250522014-03-25 10:40:18 +0100642 return 0;
643}
644EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
645
646#else /* CONFIG_GPIOLIB_IRQCHIP */
647
648static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
649
650#endif /* CONFIG_GPIOLIB_IRQCHIP */
651
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530652#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100653
Linus Walleij3f0f8672012-11-20 12:40:15 +0100654/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200655 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
656 * @chip: the gpiochip to add the range for
657 * @pinctrl: the dev_name() of the pin controller to map to
658 * @gpio_offset: the start offset in the current gpio_chip number space
659 * @pin_group: name of the pin group inside the pin controller
660 */
661int gpiochip_add_pingroup_range(struct gpio_chip *chip,
662 struct pinctrl_dev *pctldev,
663 unsigned int gpio_offset, const char *pin_group)
664{
665 struct gpio_pin_range *pin_range;
666 int ret;
667
668 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
669 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200670 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200671 return -ENOMEM;
672 }
673
674 /* Use local offset as range ID */
675 pin_range->range.id = gpio_offset;
676 pin_range->range.gc = chip;
677 pin_range->range.name = chip->label;
678 pin_range->range.base = chip->base + gpio_offset;
679 pin_range->pctldev = pctldev;
680
681 ret = pinctrl_get_group_pins(pctldev, pin_group,
682 &pin_range->range.pins,
683 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100684 if (ret < 0) {
685 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200686 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100687 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200688
689 pinctrl_add_gpio_range(pctldev, &pin_range->range);
690
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200691 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
692 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200693 pinctrl_dev_get_devname(pctldev), pin_group);
694
695 list_add_tail(&pin_range->node, &chip->pin_ranges);
696
697 return 0;
698}
699EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
700
701/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100702 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
703 * @chip: the gpiochip to add the range for
704 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100705 * @gpio_offset: the start offset in the current gpio_chip number space
706 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100707 * @npins: the number of pins from the offset of each pin space (GPIO and
708 * pin controller) to accumulate in this range
709 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100710int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100711 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100712 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530713{
714 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800715 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530716
Linus Walleij3f0f8672012-11-20 12:40:15 +0100717 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530718 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200719 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100720 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530721 }
722
Linus Walleij3f0f8672012-11-20 12:40:15 +0100723 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100724 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100725 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530726 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100727 pin_range->range.base = chip->base + gpio_offset;
728 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530729 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100730 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530731 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100732 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800733 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200734 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100735 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800736 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100737 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200738 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
739 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100740 pinctl_name,
741 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530742
743 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100744
745 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530746}
Linus Walleij165adc92012-11-06 14:49:39 +0100747EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530748
Linus Walleij3f0f8672012-11-20 12:40:15 +0100749/**
750 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
751 * @chip: the chip to remove all the mappings for
752 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530753void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
754{
755 struct gpio_pin_range *pin_range, *tmp;
756
757 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
758 list_del(&pin_range->node);
759 pinctrl_remove_gpio_range(pin_range->pctldev,
760 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100761 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530762 }
763}
Linus Walleij165adc92012-11-06 14:49:39 +0100764EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
765
766#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530767
David Brownelld2876d02008-02-04 22:28:20 -0800768/* These "optional" allocation calls help prevent drivers from stomping
769 * on each other, and help provide better diagnostics in debugfs.
770 * They're called even less than the "set direction" calls.
771 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200772static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800773{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200774 struct gpio_chip *chip = desc->chip;
775 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800776 unsigned long flags;
777
778 spin_lock_irqsave(&gpio_lock, flags);
779
David Brownelld2876d02008-02-04 22:28:20 -0800780 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700781 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800782 */
783
784 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
785 desc_set_label(desc, label ? : "?");
786 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700787 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800788 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800789 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700790 }
791
792 if (chip->request) {
793 /* chip->request may sleep */
794 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900795 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700796 spin_lock_irqsave(&gpio_lock, flags);
797
798 if (status < 0) {
799 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700800 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300801 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700802 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700803 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300804 if (chip->get_direction) {
805 /* chip->get_direction may sleep */
806 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900807 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300808 spin_lock_irqsave(&gpio_lock, flags);
809 }
David Brownelld2876d02008-02-04 22:28:20 -0800810done:
Mika Westerberg77c2d792014-03-10 14:54:50 +0200811 spin_unlock_irqrestore(&gpio_lock, flags);
812 return status;
813}
814
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900815int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200816{
817 int status = -EPROBE_DEFER;
818 struct gpio_chip *chip;
819
820 if (!desc) {
821 pr_warn("%s: invalid GPIO\n", __func__);
822 return -EINVAL;
823 }
824
825 chip = desc->chip;
826 if (!chip)
827 goto done;
828
829 if (try_module_get(chip->owner)) {
830 status = __gpiod_request(desc, label);
831 if (status < 0)
832 module_put(chip->owner);
833 }
834
835done:
David Brownelld2876d02008-02-04 22:28:20 -0800836 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200837 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200838
David Brownelld2876d02008-02-04 22:28:20 -0800839 return status;
840}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900841
Mika Westerberg77c2d792014-03-10 14:54:50 +0200842static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800843{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200844 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -0800845 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -0700846 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800847
Uwe Kleine-König3d599d12008-10-15 22:03:12 -0700848 might_sleep();
849
Alexandre Courbot372e7222013-02-03 01:29:29 +0900850 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700851
David Brownelld2876d02008-02-04 22:28:20 -0800852 spin_lock_irqsave(&gpio_lock, flags);
853
David Brownell35e8bb52008-10-15 22:03:16 -0700854 chip = desc->chip;
855 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
856 if (chip->free) {
857 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -0700858 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900859 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700860 spin_lock_irqsave(&gpio_lock, flags);
861 }
David Brownelld2876d02008-02-04 22:28:20 -0800862 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -0800863 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -0700864 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530865 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +0530866 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200867 ret = true;
868 }
David Brownelld2876d02008-02-04 22:28:20 -0800869
870 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200871 return ret;
872}
873
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900874void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200875{
876 if (desc && __gpiod_free(desc))
877 module_put(desc->chip->owner);
878 else
879 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -0800880}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900881
David Brownelld2876d02008-02-04 22:28:20 -0800882/**
883 * gpiochip_is_requested - return string iff signal was requested
884 * @chip: controller managing the signal
885 * @offset: of signal within controller's 0..(ngpio - 1) range
886 *
887 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +0900888 * The string returned is the label passed to gpio_request(); if none has been
889 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -0800890 *
891 * This function is for use by GPIO controller drivers. The label can
892 * help with diagnostics, and knowing that the signal is used as a GPIO
893 * can help avoid accidentally multiplexing it to another controller.
894 */
895const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
896{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900897 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800898
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900899 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -0800900 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900901
902 desc = &chip->desc[offset];
903
Alexandre Courbot372e7222013-02-03 01:29:29 +0900904 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -0800905 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900906 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -0800907}
908EXPORT_SYMBOL_GPL(gpiochip_is_requested);
909
Mika Westerberg77c2d792014-03-10 14:54:50 +0200910/**
911 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
912 * @desc: GPIO descriptor to request
913 * @label: label for the GPIO
914 *
915 * Function allows GPIO chip drivers to request and use their own GPIO
916 * descriptors via gpiolib API. Difference to gpiod_request() is that this
917 * function will not increase reference count of the GPIO chip module. This
918 * allows the GPIO chip module to be unloaded as needed (we assume that the
919 * GPIO chip driver handles freeing the GPIOs it has requested).
920 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700921struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
922 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200923{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700924 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
925 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200926
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700927 if (IS_ERR(desc)) {
928 chip_err(chip, "failed to get GPIO descriptor\n");
929 return desc;
930 }
931
932 err = __gpiod_request(desc, label);
933 if (err < 0)
934 return ERR_PTR(err);
935
936 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200937}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700938EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200939
940/**
941 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
942 * @desc: GPIO descriptor to free
943 *
944 * Function frees the given GPIO requested previously with
945 * gpiochip_request_own_desc().
946 */
947void gpiochip_free_own_desc(struct gpio_desc *desc)
948{
949 if (desc)
950 __gpiod_free(desc);
951}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700952EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -0800953
954/* Drivers MUST set GPIO direction before making get/set calls. In
955 * some cases this is done in early boot, before IRQs are enabled.
956 *
957 * As a rule these aren't called more than once (except for drivers
958 * using the open-drain emulation idiom) so these are natural places
959 * to accumulate extra debugging checks. Note that we can't (yet)
960 * rely on gpio_request() having been called beforehand.
961 */
962
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700963/**
964 * gpiod_direction_input - set the GPIO direction to input
965 * @desc: GPIO to set to input
966 *
967 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
968 * be called safely on it.
969 *
970 * Return 0 in case of success, else an error code.
971 */
972int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800973{
David Brownelld2876d02008-02-04 22:28:20 -0800974 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800975 int status = -EINVAL;
976
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200977 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900978 pr_warn("%s: invalid GPIO\n", __func__);
979 return -EINVAL;
980 }
981
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200982 chip = desc->chip;
983 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +0100984 gpiod_warn(desc,
985 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +0200986 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200987 return -EIO;
988 }
989
Alexandre Courbotd82da792014-07-22 16:17:43 +0900990 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -0800991 if (status == 0)
992 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -0600993
Alexandre Courbot372e7222013-02-03 01:29:29 +0900994 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +0900995
David Brownelld2876d02008-02-04 22:28:20 -0800996 return status;
997}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700998EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900999
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001000static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001001{
David Brownelld2876d02008-02-04 22:28:20 -08001002 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001003 int status = -EINVAL;
1004
Linus Walleijd468bf92013-09-24 11:54:38 +02001005 /* GPIOs used for IRQs shall not be set as output */
1006 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1007 gpiod_err(desc,
1008 "%s: tried to set a GPIO tied to an IRQ as output\n",
1009 __func__);
1010 return -EIO;
1011 }
1012
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301013 /* Open drain pin should not be driven to 1 */
1014 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001015 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301016
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301017 /* Open source pin should not be driven to 0 */
1018 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001019 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301020
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001021 chip = desc->chip;
1022 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001023 gpiod_warn(desc,
1024 "%s: missing set() or direction_output() operations\n",
1025 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001026 return -EIO;
1027 }
1028
Alexandre Courbotd82da792014-07-22 16:17:43 +09001029 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001030 if (status == 0)
1031 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001032 trace_gpio_value(desc_to_gpio(desc), 0, value);
1033 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001034 return status;
1035}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001036
1037/**
1038 * gpiod_direction_output_raw - set the GPIO direction to output
1039 * @desc: GPIO to set to output
1040 * @value: initial output value of the GPIO
1041 *
1042 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1043 * be called safely on it. The initial value of the output must be specified
1044 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1045 *
1046 * Return 0 in case of success, else an error code.
1047 */
1048int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1049{
1050 if (!desc || !desc->chip) {
1051 pr_warn("%s: invalid GPIO\n", __func__);
1052 return -EINVAL;
1053 }
1054 return _gpiod_direction_output_raw(desc, value);
1055}
1056EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1057
1058/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301059 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001060 * @desc: GPIO to set to output
1061 * @value: initial output value of the GPIO
1062 *
1063 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1064 * be called safely on it. The initial value of the output must be specified
1065 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1066 * account.
1067 *
1068 * Return 0 in case of success, else an error code.
1069 */
1070int gpiod_direction_output(struct gpio_desc *desc, int value)
1071{
1072 if (!desc || !desc->chip) {
1073 pr_warn("%s: invalid GPIO\n", __func__);
1074 return -EINVAL;
1075 }
1076 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1077 value = !value;
1078 return _gpiod_direction_output_raw(desc, value);
1079}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001080EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001081
Felipe Balbic4b5be92010-05-26 14:42:23 -07001082/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001083 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001084 * @gpio: the gpio to set debounce time
1085 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001086 *
1087 * returns -ENOTSUPP if the controller does not support setting
1088 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001089 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001090int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001091{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001092 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001093
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001094 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001095 pr_warn("%s: invalid GPIO\n", __func__);
1096 return -EINVAL;
1097 }
1098
Felipe Balbic4b5be92010-05-26 14:42:23 -07001099 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001100 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001101 gpiod_dbg(desc,
1102 "%s: missing set() or set_debounce() operations\n",
1103 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001104 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001105 }
1106
Alexandre Courbotd82da792014-07-22 16:17:43 +09001107 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001108}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001109EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001110
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001111/**
1112 * gpiod_is_active_low - test whether a GPIO is active-low or not
1113 * @desc: the gpio descriptor to test
1114 *
1115 * Returns 1 if the GPIO is active-low, 0 otherwise.
1116 */
1117int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001118{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001119 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001120}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001121EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001122
1123/* I/O calls are only valid after configuration completed; the relevant
1124 * "is this a valid GPIO" error checks should already have been done.
1125 *
1126 * "Get" operations are often inlinable as reading a pin value register,
1127 * and masking the relevant bit in that register.
1128 *
1129 * When "set" operations are inlinable, they involve writing that mask to
1130 * one register to set a low value, or a different register to set it high.
1131 * Otherwise locking is needed, so there may be little value to inlining.
1132 *
1133 *------------------------------------------------------------------------
1134 *
1135 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1136 * have requested the GPIO. That can include implicit requesting by
1137 * a direction setting call. Marking a gpio as requested locks its chip
1138 * in memory, guaranteeing that these table lookups need no more locking
1139 * and that gpiochip_remove() will fail.
1140 *
1141 * REVISIT when debugging, consider adding some instrumentation to ensure
1142 * that the GPIO was actually requested.
1143 */
1144
Alexandre Courbot23600962014-03-11 15:52:09 +09001145static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001146{
1147 struct gpio_chip *chip;
Alexandre Courbot23600962014-03-11 15:52:09 +09001148 bool value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001149 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001150
Alexandre Courbot372e7222013-02-03 01:29:29 +09001151 chip = desc->chip;
1152 offset = gpio_chip_hwgpio(desc);
Alexandre Courbot23600962014-03-11 15:52:09 +09001153 value = chip->get ? chip->get(chip, offset) : false;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001154 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001155 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001156}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001157
David Brownelld2876d02008-02-04 22:28:20 -08001158/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001159 * gpiod_get_raw_value() - return a gpio's raw value
1160 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001161 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001162 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1163 * its ACTIVE_LOW status.
1164 *
1165 * This function should be called from contexts where we cannot sleep, and will
1166 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001167 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001168int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001169{
David Brownelld2876d02008-02-04 22:28:20 -08001170 if (!desc)
1171 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001172 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001173 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001174 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001175}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001176EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001177
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001178/**
1179 * gpiod_get_value() - return a gpio's value
1180 * @desc: gpio whose value will be returned
1181 *
1182 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1183 * account.
1184 *
1185 * This function should be called from contexts where we cannot sleep, and will
1186 * complain if the GPIO chip functions potentially sleep.
1187 */
1188int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001189{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001190 int value;
1191 if (!desc)
1192 return 0;
1193 /* Should be using gpio_get_value_cansleep() */
1194 WARN_ON(desc->chip->can_sleep);
1195
1196 value = _gpiod_get_raw_value(desc);
1197 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1198 value = !value;
1199
1200 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001201}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001202EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001203
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301204/*
1205 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001206 * @desc: gpio descriptor whose state need to be set.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301207 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1208 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001209static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301210{
1211 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001212 struct gpio_chip *chip = desc->chip;
1213 int offset = gpio_chip_hwgpio(desc);
1214
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301215 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001216 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301217 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001218 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301219 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001220 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301221 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001222 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301223 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001224 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301225 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001226 gpiod_err(desc,
1227 "%s: Error in set_value for open drain err %d\n",
1228 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301229}
1230
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301231/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001232 * _gpio_set_open_source_value() - Set the open source gpio's value.
1233 * @desc: gpio descriptor whose state need to be set.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301234 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1235 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001236static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301237{
1238 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001239 struct gpio_chip *chip = desc->chip;
1240 int offset = gpio_chip_hwgpio(desc);
1241
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301242 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001243 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301244 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001245 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301246 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001247 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301248 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001249 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301250 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001251 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301252 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001253 gpiod_err(desc,
1254 "%s: Error in set_value for open source err %d\n",
1255 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301256}
1257
Alexandre Courbot23600962014-03-11 15:52:09 +09001258static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001259{
1260 struct gpio_chip *chip;
1261
Alexandre Courbot372e7222013-02-03 01:29:29 +09001262 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001263 trace_gpio_value(desc_to_gpio(desc), 0, value);
1264 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1265 _gpio_set_open_drain_value(desc, value);
1266 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1267 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301268 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001269 chip->set(chip, gpio_chip_hwgpio(desc), value);
1270}
1271
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001272/*
1273 * set multiple outputs on the same chip;
1274 * use the chip's set_multiple function if available;
1275 * otherwise set the outputs sequentially;
1276 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1277 * defines which outputs are to be changed
1278 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1279 * defines the values the outputs specified by mask are to be set to
1280 */
1281static void gpio_chip_set_multiple(struct gpio_chip *chip,
1282 unsigned long *mask, unsigned long *bits)
1283{
1284 if (chip->set_multiple) {
1285 chip->set_multiple(chip, mask, bits);
1286 } else {
1287 int i;
1288 for (i = 0; i < chip->ngpio; i++) {
1289 if (mask[BIT_WORD(i)] == 0) {
1290 /* no more set bits in this mask word;
1291 * skip ahead to the next word */
1292 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1293 continue;
1294 }
1295 /* set outputs if the corresponding mask bit is set */
1296 if (__test_and_clear_bit(i, mask)) {
1297 chip->set(chip, i, test_bit(i, bits));
1298 }
1299 }
1300 }
1301}
1302
1303static void gpiod_set_array_priv(bool raw, bool can_sleep,
1304 unsigned int array_size,
1305 struct gpio_desc **desc_array,
1306 int *value_array)
1307{
1308 int i = 0;
1309
1310 while (i < array_size) {
1311 struct gpio_chip *chip = desc_array[i]->chip;
1312 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1313 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1314 int count = 0;
1315
1316 if (!can_sleep) {
1317 WARN_ON(chip->can_sleep);
1318 }
1319 memset(mask, 0, sizeof(mask));
1320 do {
1321 struct gpio_desc *desc = desc_array[i];
1322 int hwgpio = gpio_chip_hwgpio(desc);
1323 int value = value_array[i];
1324
1325 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1326 value = !value;
1327 trace_gpio_value(desc_to_gpio(desc), 0, value);
1328 /*
1329 * collect all normal outputs belonging to the same chip
1330 * open drain and open source outputs are set individually
1331 */
1332 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
1333 _gpio_set_open_drain_value(desc,value);
1334 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1335 _gpio_set_open_source_value(desc, value);
1336 } else {
1337 __set_bit(hwgpio, mask);
1338 if (value) {
1339 __set_bit(hwgpio, bits);
1340 } else {
1341 __clear_bit(hwgpio, bits);
1342 }
1343 count++;
1344 }
1345 i++;
1346 } while ((i < array_size) && (desc_array[i]->chip == chip));
1347 /* push collected bits to outputs */
1348 if (count != 0) {
1349 gpio_chip_set_multiple(chip, mask, bits);
1350 }
1351 }
1352}
1353
David Brownelld2876d02008-02-04 22:28:20 -08001354/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001355 * gpiod_set_raw_value() - assign a gpio's raw value
1356 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001357 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001358 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001359 * Set the raw value of the GPIO, i.e. the value of its physical line without
1360 * regard for its ACTIVE_LOW status.
1361 *
1362 * This function should be called from contexts where we cannot sleep, and will
1363 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001364 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001365void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001366{
David Brownelld2876d02008-02-04 22:28:20 -08001367 if (!desc)
1368 return;
David Brownelld2876d02008-02-04 22:28:20 -08001369 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001370 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001371 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001372}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001373EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001374
1375/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001376 * gpiod_set_value() - assign a gpio's value
1377 * @desc: gpio whose value will be assigned
1378 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001379 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001380 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1381 * account
1382 *
1383 * This function should be called from contexts where we cannot sleep, and will
1384 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001385 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001386void gpiod_set_value(struct gpio_desc *desc, int value)
1387{
1388 if (!desc)
1389 return;
1390 /* Should be using gpio_set_value_cansleep() */
1391 WARN_ON(desc->chip->can_sleep);
1392 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1393 value = !value;
1394 _gpiod_set_raw_value(desc, value);
1395}
1396EXPORT_SYMBOL_GPL(gpiod_set_value);
1397
1398/**
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001399 * gpiod_set_raw_array() - assign values to an array of GPIOs
1400 * @array_size: number of elements in the descriptor / value arrays
1401 * @desc_array: array of GPIO descriptors whose values will be assigned
1402 * @value_array: array of values to assign
1403 *
1404 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1405 * without regard for their ACTIVE_LOW status.
1406 *
1407 * This function should be called from contexts where we cannot sleep, and will
1408 * complain if the GPIO chip functions potentially sleep.
1409 */
1410void gpiod_set_raw_array(unsigned int array_size,
1411 struct gpio_desc **desc_array, int *value_array)
1412{
1413 if (!desc_array)
1414 return;
1415 gpiod_set_array_priv(true, false, array_size, desc_array, value_array);
1416}
1417EXPORT_SYMBOL_GPL(gpiod_set_raw_array);
1418
1419/**
1420 * gpiod_set_array() - assign values to an array of GPIOs
1421 * @array_size: number of elements in the descriptor / value arrays
1422 * @desc_array: array of GPIO descriptors whose values will be assigned
1423 * @value_array: array of values to assign
1424 *
1425 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1426 * into account.
1427 *
1428 * This function should be called from contexts where we cannot sleep, and will
1429 * complain if the GPIO chip functions potentially sleep.
1430 */
1431void gpiod_set_array(unsigned int array_size,
1432 struct gpio_desc **desc_array, int *value_array)
1433{
1434 if (!desc_array)
1435 return;
1436 gpiod_set_array_priv(false, false, array_size, desc_array, value_array);
1437}
1438EXPORT_SYMBOL_GPL(gpiod_set_array);
1439
1440/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001441 * gpiod_cansleep() - report whether gpio value access may sleep
1442 * @desc: gpio to check
1443 *
1444 */
1445int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001446{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001447 if (!desc)
1448 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001449 return desc->chip->can_sleep;
1450}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001451EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001452
David Brownell0f6d5042008-10-15 22:03:14 -07001453/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001454 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1455 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001456 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001457 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1458 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001459 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001460int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001461{
1462 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001463 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001464
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001465 if (!desc)
1466 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001467 chip = desc->chip;
1468 offset = gpio_chip_hwgpio(desc);
1469 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1470}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001471EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001472
Linus Walleijd468bf92013-09-24 11:54:38 +02001473/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001474 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001475 * @chip: the chip the GPIO to lock belongs to
1476 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001477 *
1478 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001479 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001480 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001481int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001482{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001483 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001484 return -EINVAL;
1485
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001486 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1487 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001488 "%s: tried to flag a GPIO set as output for IRQ\n",
1489 __func__);
1490 return -EIO;
1491 }
1492
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001493 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001494 return 0;
1495}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001496EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001497
Linus Walleijd468bf92013-09-24 11:54:38 +02001498/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001499 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001500 * @chip: the chip the GPIO to lock belongs to
1501 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001502 *
1503 * This is used directly by GPIO drivers that want to indicate
1504 * that a certain GPIO is no longer used exclusively for IRQ.
1505 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001506void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001507{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001508 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001509 return;
1510
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001511 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001512}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001513EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001514
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001515/**
1516 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1517 * @desc: gpio whose value will be returned
1518 *
1519 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1520 * its ACTIVE_LOW status.
1521 *
1522 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001523 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001524int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001525{
David Brownelld2876d02008-02-04 22:28:20 -08001526 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001527 if (!desc)
1528 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001529 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001530}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001531EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001532
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001533/**
1534 * gpiod_get_value_cansleep() - return a gpio's value
1535 * @desc: gpio whose value will be returned
1536 *
1537 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1538 * account.
1539 *
1540 * This function is to be called from contexts that can sleep.
1541 */
1542int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001543{
David Brownelld2876d02008-02-04 22:28:20 -08001544 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001545
1546 might_sleep_if(extra_checks);
1547 if (!desc)
1548 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001549
1550 value = _gpiod_get_raw_value(desc);
1551 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1552 value = !value;
1553
David Brownelld2876d02008-02-04 22:28:20 -08001554 return value;
1555}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001556EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001557
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001558/**
1559 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1560 * @desc: gpio whose value will be assigned
1561 * @value: value to assign
1562 *
1563 * Set the raw value of the GPIO, i.e. the value of its physical line without
1564 * regard for its ACTIVE_LOW status.
1565 *
1566 * This function is to be called from contexts that can sleep.
1567 */
1568void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001569{
David Brownelld2876d02008-02-04 22:28:20 -08001570 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001571 if (!desc)
1572 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001573 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001574}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001575EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001576
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001577/**
1578 * gpiod_set_value_cansleep() - assign a gpio's value
1579 * @desc: gpio whose value will be assigned
1580 * @value: value to assign
1581 *
1582 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1583 * account
1584 *
1585 * This function is to be called from contexts that can sleep.
1586 */
1587void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001588{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001589 might_sleep_if(extra_checks);
1590 if (!desc)
1591 return;
1592
1593 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1594 value = !value;
1595 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001596}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001597EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001598
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001599/**
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001600 * gpiod_set_raw_array_cansleep() - assign values to an array of GPIOs
1601 * @array_size: number of elements in the descriptor / value arrays
1602 * @desc_array: array of GPIO descriptors whose values will be assigned
1603 * @value_array: array of values to assign
1604 *
1605 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1606 * without regard for their ACTIVE_LOW status.
1607 *
1608 * This function is to be called from contexts that can sleep.
1609 */
1610void gpiod_set_raw_array_cansleep(unsigned int array_size,
1611 struct gpio_desc **desc_array,
1612 int *value_array)
1613{
1614 might_sleep_if(extra_checks);
1615 if (!desc_array)
1616 return;
1617 gpiod_set_array_priv(true, true, array_size, desc_array, value_array);
1618}
1619EXPORT_SYMBOL_GPL(gpiod_set_raw_array_cansleep);
1620
1621/**
1622 * gpiod_set_array_cansleep() - assign values to an array of GPIOs
1623 * @array_size: number of elements in the descriptor / value arrays
1624 * @desc_array: array of GPIO descriptors whose values will be assigned
1625 * @value_array: array of values to assign
1626 *
1627 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1628 * into account.
1629 *
1630 * This function is to be called from contexts that can sleep.
1631 */
1632void gpiod_set_array_cansleep(unsigned int array_size,
1633 struct gpio_desc **desc_array,
1634 int *value_array)
1635{
1636 might_sleep_if(extra_checks);
1637 if (!desc_array)
1638 return;
1639 gpiod_set_array_priv(false, true, array_size, desc_array, value_array);
1640}
1641EXPORT_SYMBOL_GPL(gpiod_set_array_cansleep);
1642
1643/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001644 * gpiod_add_lookup_table() - register GPIO device consumers
1645 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001646 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001647void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001648{
1649 mutex_lock(&gpio_lookup_lock);
1650
Alexandre Courbotad824782013-12-03 12:20:11 +09001651 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001652
1653 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001654}
1655
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001656static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001657 unsigned int idx,
1658 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001659{
Thierry Redingdd34c372014-04-23 17:28:09 +02001660 static const char *suffixes[] = { "gpios", "gpio" };
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001661 char prop_name[32]; /* 32 is max size of property name */
1662 enum of_gpio_flags of_flags;
1663 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001664 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001665
Thierry Redingdd34c372014-04-23 17:28:09 +02001666 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1667 if (con_id)
1668 snprintf(prop_name, 32, "%s-%s", con_id, suffixes[i]);
1669 else
1670 snprintf(prop_name, 32, "%s", suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001671
Thierry Redingdd34c372014-04-23 17:28:09 +02001672 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1673 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001674 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001675 break;
1676 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001677
1678 if (IS_ERR(desc))
1679 return desc;
1680
1681 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001682 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001683
1684 return desc;
1685}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001686
Mika Westerberg81f59e92013-10-10 11:01:09 +03001687static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001688 unsigned int idx,
1689 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001690{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001691 static const char * const suffixes[] = { "gpios", "gpio" };
1692 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03001693 struct acpi_gpio_info info;
1694 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001695 char propname[32];
1696 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03001697
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001698 /* Try first from _DSD */
1699 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1700 if (con_id && strcmp(con_id, "gpios")) {
1701 snprintf(propname, sizeof(propname), "%s-%s",
1702 con_id, suffixes[i]);
1703 } else {
1704 snprintf(propname, sizeof(propname), "%s",
1705 suffixes[i]);
1706 }
Mika Westerberge01f4402013-10-10 11:01:10 +03001707
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001708 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1709 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1710 break;
1711 }
1712
1713 /* Then from plain _CRS GPIOs */
1714 if (IS_ERR(desc)) {
1715 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1716 if (IS_ERR(desc))
1717 return desc;
1718 }
1719
1720 if (info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001721 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001722
1723 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001724}
1725
Alexandre Courbotad824782013-12-03 12:20:11 +09001726static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1727{
1728 const char *dev_id = dev ? dev_name(dev) : NULL;
1729 struct gpiod_lookup_table *table;
1730
1731 mutex_lock(&gpio_lookup_lock);
1732
1733 list_for_each_entry(table, &gpio_lookup_list, list) {
1734 if (table->dev_id && dev_id) {
1735 /*
1736 * Valid strings on both ends, must be identical to have
1737 * a match
1738 */
1739 if (!strcmp(table->dev_id, dev_id))
1740 goto found;
1741 } else {
1742 /*
1743 * One of the pointers is NULL, so both must be to have
1744 * a match
1745 */
1746 if (dev_id == table->dev_id)
1747 goto found;
1748 }
1749 }
1750 table = NULL;
1751
1752found:
1753 mutex_unlock(&gpio_lookup_lock);
1754 return table;
1755}
1756
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001757static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001758 unsigned int idx,
1759 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001760{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001761 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001762 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001763 struct gpiod_lookup *p;
1764
Alexandre Courbotad824782013-12-03 12:20:11 +09001765 table = gpiod_find_lookup_table(dev);
1766 if (!table)
1767 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001768
Alexandre Courbotad824782013-12-03 12:20:11 +09001769 for (p = &table->table[0]; p->chip_label; p++) {
1770 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001771
Alexandre Courbotad824782013-12-03 12:20:11 +09001772 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001773 if (p->idx != idx)
1774 continue;
1775
Alexandre Courbotad824782013-12-03 12:20:11 +09001776 /* If the lookup entry has a con_id, require exact match */
1777 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1778 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001779
Alexandre Courbotad824782013-12-03 12:20:11 +09001780 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001781
Alexandre Courbotad824782013-12-03 12:20:11 +09001782 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001783 dev_err(dev, "cannot find GPIO chip %s\n",
1784 p->chip_label);
1785 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001786 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001787
Alexandre Courbotad824782013-12-03 12:20:11 +09001788 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001789 dev_err(dev,
1790 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1791 idx, chip->ngpio, chip->label);
1792 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001793 }
1794
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001795 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001796 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001797
1798 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001799 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001800
1801 return desc;
1802}
1803
1804/**
Thierry Reding08791622014-04-25 16:54:22 +02001805 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09001806 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001807 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001808 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001809 *
1810 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001811 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1812 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001813 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001814struct gpio_desc *__must_check __gpiod_get(struct device *dev, const char *con_id,
1815 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001816{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001817 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001818}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001819EXPORT_SYMBOL_GPL(__gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001820
1821/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02001822 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1823 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1824 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001825 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02001826 *
1827 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1828 * the requested function it will return NULL. This is convenient for drivers
1829 * that need to handle optional GPIOs.
1830 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001831struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
1832 const char *con_id,
1833 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02001834{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001835 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001836}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001837EXPORT_SYMBOL_GPL(__gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001838
1839/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001840 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02001841 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001842 * @con_id: function within the GPIO consumer
1843 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001844 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001845 *
1846 * This variant of gpiod_get() allows to access GPIOs other than the first
1847 * defined one for functions that define several GPIOs.
1848 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001849 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1850 * requested function and/or index, or another IS_ERR() code if an error
1851 * occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001852 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001853struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001854 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001855 unsigned int idx,
1856 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001857{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001858 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001859 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001860 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001861
1862 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
1863
1864 /* Using device tree? */
1865 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
1866 dev_dbg(dev, "using device tree for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001867 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
Mika Westerberg81f59e92013-10-10 11:01:09 +03001868 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
1869 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001870 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001871 }
1872
1873 /*
1874 * Either we are not using DT or ACPI, or their lookup did not return
1875 * a result. In that case, use platform lookup as a fallback.
1876 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001877 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04001878 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001879 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001880 }
1881
1882 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02001883 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001884 return desc;
1885 }
1886
1887 status = gpiod_request(desc, con_id);
1888
1889 if (status < 0)
1890 return ERR_PTR(status);
1891
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001892 if (lookupflags & GPIO_ACTIVE_LOW)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001893 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001894 if (lookupflags & GPIO_OPEN_DRAIN)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001895 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001896 if (lookupflags & GPIO_OPEN_SOURCE)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001897 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001898
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001899 /* No particular flag request, return here... */
Adrian Hunter72f908c2014-09-22 11:01:16 +03001900 if (!(flags & GPIOD_FLAGS_BIT_DIR_SET))
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001901 return desc;
1902
1903 /* Process flags */
1904 if (flags & GPIOD_FLAGS_BIT_DIR_OUT)
1905 status = gpiod_direction_output(desc,
1906 flags & GPIOD_FLAGS_BIT_DIR_VAL);
1907 else
1908 status = gpiod_direction_input(desc);
1909
1910 if (status < 0) {
1911 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
1912 gpiod_put(desc);
1913 return ERR_PTR(status);
1914 }
1915
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001916 return desc;
1917}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001918EXPORT_SYMBOL_GPL(__gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001919
1920/**
Mika Westerberg40b73182014-10-21 13:33:59 +02001921 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
1922 * @fwnode: handle of the firmware node
1923 * @propname: name of the firmware property representing the GPIO
1924 *
1925 * This function can be used for drivers that get their configuration
1926 * from firmware.
1927 *
1928 * Function properly finds the corresponding GPIO using whatever is the
1929 * underlying firmware interface and then makes sure that the GPIO
1930 * descriptor is requested before it is returned to the caller.
1931 *
1932 * In case of error an ERR_PTR() is returned.
1933 */
1934struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
1935 const char *propname)
1936{
1937 struct gpio_desc *desc = ERR_PTR(-ENODEV);
1938 bool active_low = false;
1939 int ret;
1940
1941 if (!fwnode)
1942 return ERR_PTR(-EINVAL);
1943
1944 if (is_of_node(fwnode)) {
1945 enum of_gpio_flags flags;
1946
1947 desc = of_get_named_gpiod_flags(of_node(fwnode), propname, 0,
1948 &flags);
1949 if (!IS_ERR(desc))
1950 active_low = flags & OF_GPIO_ACTIVE_LOW;
1951 } else if (is_acpi_node(fwnode)) {
1952 struct acpi_gpio_info info;
1953
1954 desc = acpi_get_gpiod_by_index(acpi_node(fwnode), propname, 0,
1955 &info);
1956 if (!IS_ERR(desc))
1957 active_low = info.active_low;
1958 }
1959
1960 if (IS_ERR(desc))
1961 return desc;
1962
1963 ret = gpiod_request(desc, NULL);
1964 if (ret)
1965 return ERR_PTR(ret);
1966
1967 /* Only value flag can be set from both DT and ACPI is active_low */
1968 if (active_low)
1969 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
1970
1971 return desc;
1972}
1973EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
1974
1975/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02001976 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
1977 * function
1978 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1979 * @con_id: function within the GPIO consumer
1980 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001981 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02001982 *
1983 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
1984 * specified index was assigned to the requested function it will return NULL.
1985 * This is convenient for drivers that need to handle optional GPIOs.
1986 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001987struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02001988 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001989 unsigned int index,
1990 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02001991{
1992 struct gpio_desc *desc;
1993
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001994 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001995 if (IS_ERR(desc)) {
1996 if (PTR_ERR(desc) == -ENOENT)
1997 return NULL;
1998 }
1999
2000 return desc;
2001}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002002EXPORT_SYMBOL_GPL(__gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002003
2004/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002005 * gpiod_put - dispose of a GPIO descriptor
2006 * @desc: GPIO descriptor to dispose of
2007 *
2008 * No descriptor can be used after gpiod_put() has been called on it.
2009 */
2010void gpiod_put(struct gpio_desc *desc)
2011{
2012 gpiod_free(desc);
2013}
2014EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002015
David Brownelld2876d02008-02-04 22:28:20 -08002016#ifdef CONFIG_DEBUG_FS
2017
2018static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2019{
2020 unsigned i;
2021 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002022 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002023 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002024 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002025
2026 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2027 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2028 continue;
2029
Alexandre Courbot372e7222013-02-03 01:29:29 +09002030 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002031 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002032 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2033 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002034 gpio, gdesc->label,
2035 is_out ? "out" : "in ",
2036 chip->get
2037 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002038 : "? ",
2039 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002040 seq_printf(s, "\n");
2041 }
2042}
2043
Thierry Redingf9c4a312012-04-12 13:26:01 +02002044static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002045{
Grant Likely362432a2013-02-09 09:41:49 +00002046 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002047 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002048 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002049
2050 s->private = "";
2051
Grant Likely362432a2013-02-09 09:41:49 +00002052 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002053 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002054 if (index-- == 0) {
2055 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002056 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002057 }
2058 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002059
2060 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002061}
2062
2063static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2064{
Grant Likely362432a2013-02-09 09:41:49 +00002065 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002066 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002067 void *ret = NULL;
2068
Grant Likely362432a2013-02-09 09:41:49 +00002069 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002070 if (list_is_last(&chip->list, &gpio_chips))
2071 ret = NULL;
2072 else
2073 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002074 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002075
2076 s->private = "\n";
2077 ++*pos;
2078
2079 return ret;
2080}
2081
2082static void gpiolib_seq_stop(struct seq_file *s, void *v)
2083{
2084}
2085
2086static int gpiolib_seq_show(struct seq_file *s, void *v)
2087{
2088 struct gpio_chip *chip = v;
2089 struct device *dev;
2090
2091 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2092 chip->base, chip->base + chip->ngpio - 1);
2093 dev = chip->dev;
2094 if (dev)
2095 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2096 dev_name(dev));
2097 if (chip->label)
2098 seq_printf(s, ", %s", chip->label);
2099 if (chip->can_sleep)
2100 seq_printf(s, ", can sleep");
2101 seq_printf(s, ":\n");
2102
2103 if (chip->dbg_show)
2104 chip->dbg_show(s, chip);
2105 else
2106 gpiolib_dbg_show(s, chip);
2107
David Brownelld2876d02008-02-04 22:28:20 -08002108 return 0;
2109}
2110
Thierry Redingf9c4a312012-04-12 13:26:01 +02002111static const struct seq_operations gpiolib_seq_ops = {
2112 .start = gpiolib_seq_start,
2113 .next = gpiolib_seq_next,
2114 .stop = gpiolib_seq_stop,
2115 .show = gpiolib_seq_show,
2116};
2117
David Brownelld2876d02008-02-04 22:28:20 -08002118static int gpiolib_open(struct inode *inode, struct file *file)
2119{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002120 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002121}
2122
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002123static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002124 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002125 .open = gpiolib_open,
2126 .read = seq_read,
2127 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002128 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002129};
2130
2131static int __init gpiolib_debugfs_init(void)
2132{
2133 /* /sys/kernel/debug/gpio */
2134 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2135 NULL, NULL, &gpiolib_operations);
2136 return 0;
2137}
2138subsys_initcall(gpiolib_debugfs_init);
2139
2140#endif /* DEBUG_FS */