blob: 0f8173051edca39f654a6069126927516f4182da [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
David Brownelld2876d02008-02-04 22:28:20 -080018
Mika Westerberg664e3e52014-01-08 12:40:54 +020019#include "gpiolib.h"
20
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060021#define CREATE_TRACE_POINTS
22#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080023
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070024/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080025 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070026 * The GPIO programming interface allows for inlining speed-critical
27 * get/set operations for common cases, so that access to SOC-integrated
28 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080029 */
30
31
32/* When debugging, extend minimal trust to callers and platform code.
33 * Also emit diagnostic messages that may help initial bringup, when
34 * board setup or driver bugs are most common.
35 *
36 * Otherwise, minimize overhead in what may be bitbanging codepaths.
37 */
38#ifdef DEBUG
39#define extra_checks 1
40#else
41#define extra_checks 0
42#endif
43
44/* gpio_lock prevents conflicts during gpio_desc[] table updates.
45 * While any GPIO is requested, its gpio_chip is not removable;
46 * each GPIO's "requested" flag serves as a lock and refcount.
47 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090048DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080049
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 Brownelld8f388d82008-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;
Johan Hovold225fce82015-01-12 17:12:25 +0100251 spin_unlock_irqrestore(&gpio_lock, flags);
252 goto err_free_descs;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700253 }
254 chip->base = base;
255 }
256
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900257 status = gpiochip_add_to_list(chip);
Johan Hovold05aa5202015-01-12 17:12:26 +0100258 if (status) {
259 spin_unlock_irqrestore(&gpio_lock, flags);
260 goto err_free_descs;
261 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900262
Johan Hovold05aa5202015-01-12 17:12:26 +0100263 for (id = 0; id < chip->ngpio; id++) {
264 struct gpio_desc *desc = &descs[id];
David Brownelld8f388d82008-07-25 01:46:07 -0700265
Johan Hovold05aa5202015-01-12 17:12:26 +0100266 desc->chip = chip;
267
268 /* REVISIT: most hardware initializes GPIOs as inputs (often
269 * with pullups enabled) so power usage is minimized. Linux
270 * code should set the gpio direction first thing; but until
271 * it does, and in case chip->get_direction is not set, we may
272 * expose the wrong direction in sysfs.
273 */
274 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800275 }
276
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900277 chip->desc = descs;
278
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800279 spin_unlock_irqrestore(&gpio_lock, flags);
280
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530281#ifdef CONFIG_PINCTRL
282 INIT_LIST_HEAD(&chip->pin_ranges);
283#endif
284
Anton Vorontsov391c9702010-06-08 07:48:17 -0600285 of_gpiochip_add(chip);
Mika Westerberg664e3e52014-01-08 12:40:54 +0200286 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600287
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600288 status = gpiochip_export(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100289 if (status)
290 goto err_remove_chip;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600291
Andy Shevchenko7589e592013-12-05 11:26:23 +0200292 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700293 chip->base, chip->base + chip->ngpio - 1,
294 chip->label ? : "generic");
295
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600296 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800297
Johan Hovold225fce82015-01-12 17:12:25 +0100298err_remove_chip:
299 acpi_gpiochip_remove(chip);
300 of_gpiochip_remove(chip);
301 spin_lock_irqsave(&gpio_lock, flags);
302 list_del(&chip->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800303 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold05aa5202015-01-12 17:12:26 +0100304 chip->desc = NULL;
Johan Hovold225fce82015-01-12 17:12:25 +0100305err_free_descs:
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900306 kfree(descs);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900307
David Brownelld2876d02008-02-04 22:28:20 -0800308 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200309 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600310 chip->base, chip->base + chip->ngpio - 1,
311 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800312 return status;
313}
314EXPORT_SYMBOL_GPL(gpiochip_add);
315
Linus Walleij14250522014-03-25 10:40:18 +0100316/* Forward-declaration */
317static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
318
David Brownelld2876d02008-02-04 22:28:20 -0800319/**
320 * gpiochip_remove() - unregister a gpio_chip
321 * @chip: the chip to unregister
322 *
323 * A gpio_chip with any GPIOs still requested may not be removed.
324 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200325void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800326{
327 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800328 unsigned id;
329
Johan Hovold00acc3d2015-01-12 17:12:27 +0100330 gpiochip_irqchip_remove(chip);
331
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200332 acpi_gpiochip_remove(chip);
333
David Brownelld2876d02008-02-04 22:28:20 -0800334 spin_lock_irqsave(&gpio_lock, flags);
335
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100336 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600337 of_gpiochip_remove(chip);
338
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900339 for (id = 0; id < chip->ngpio; id++) {
abdoulaye berthee1db1702014-07-05 18:28:50 +0200340 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags))
341 dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
David Brownelld2876d02008-02-04 22:28:20 -0800342 }
abdoulaye berthee1db1702014-07-05 18:28:50 +0200343 for (id = 0; id < chip->ngpio; id++)
344 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900345
abdoulaye berthee1db1702014-07-05 18:28:50 +0200346 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800347 spin_unlock_irqrestore(&gpio_lock, flags);
abdoulaye berthee1db1702014-07-05 18:28:50 +0200348 gpiochip_unexport(chip);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900349
350 kfree(chip->desc);
351 chip->desc = NULL;
David Brownelld2876d02008-02-04 22:28:20 -0800352}
353EXPORT_SYMBOL_GPL(gpiochip_remove);
354
Grant Likely594fa262010-06-08 07:48:16 -0600355/**
356 * gpiochip_find() - iterator for locating a specific gpio_chip
357 * @data: data to pass to match function
358 * @callback: Callback function to check gpio_chip
359 *
360 * Similar to bus_find_device. It returns a reference to a gpio_chip as
361 * determined by a user supplied @match callback. The callback should return
362 * 0 if the device doesn't match and non-zero if it does. If the callback is
363 * non-zero, this function will return to the caller and not iterate over any
364 * more gpio_chips.
365 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600366struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700367 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600368 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600369{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900370 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600371 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600372
373 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900374 list_for_each_entry(chip, &gpio_chips, list)
375 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600376 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900377
378 /* No match? */
379 if (&chip->list == &gpio_chips)
380 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600381 spin_unlock_irqrestore(&gpio_lock, flags);
382
383 return chip;
384}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600385EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800386
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900387static int gpiochip_match_name(struct gpio_chip *chip, void *data)
388{
389 const char *name = data;
390
391 return !strcmp(chip->label, name);
392}
393
394static struct gpio_chip *find_chip_by_name(const char *name)
395{
396 return gpiochip_find((void *)name, gpiochip_match_name);
397}
398
Linus Walleij14250522014-03-25 10:40:18 +0100399#ifdef CONFIG_GPIOLIB_IRQCHIP
400
401/*
402 * The following is irqchip helper code for gpiochips.
403 */
404
405/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200406 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
407 * @gpiochip: the gpiochip to set the irqchip chain to
408 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100409 * @parent_irq: the irq number corresponding to the parent IRQ for this
410 * chained irqchip
411 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200412 * coming out of the gpiochip. If the interrupt is nested rather than
413 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100414 */
415void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
416 struct irq_chip *irqchip,
417 int parent_irq,
418 irq_flow_handler_t parent_handler)
419{
Linus Walleij83141a72014-09-26 13:50:12 +0200420 unsigned int offset;
421
Linus Walleij83141a72014-09-26 13:50:12 +0200422 if (!gpiochip->irqdomain) {
423 chip_err(gpiochip, "called %s before setting up irqchip\n",
424 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200425 return;
426 }
427
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200428 if (parent_handler) {
429 if (gpiochip->can_sleep) {
430 chip_err(gpiochip,
431 "you cannot have chained interrupts on a "
432 "chip that may sleep\n");
433 return;
434 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200435 /*
436 * The parent irqchip is already using the chip_data for this
437 * irqchip, so our callbacks simply use the handler_data.
438 */
439 irq_set_handler_data(parent_irq, gpiochip);
Linus Torvaldsea584592014-10-09 14:58:15 -0400440 irq_set_chained_handler(parent_irq, parent_handler);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200441 }
Linus Walleij83141a72014-09-26 13:50:12 +0200442
443 /* Set the parent IRQ for all affected IRQs */
444 for (offset = 0; offset < gpiochip->ngpio; offset++)
445 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
446 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100447}
448EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
449
Linus Walleije45d1c82014-04-22 14:01:46 +0200450/*
451 * This lock class tells lockdep that GPIO irqs are in a different
452 * category than their parents, so it won't report false recursion.
453 */
454static struct lock_class_key gpiochip_irq_lock_class;
455
Linus Walleij14250522014-03-25 10:40:18 +0100456/**
457 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
458 * @d: the irqdomain used by this irqchip
459 * @irq: the global irq number used by this GPIO irqchip irq
460 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
461 *
462 * This function will set up the mapping for a certain IRQ line on a
463 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
464 * stored inside the gpiochip.
465 */
466static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
467 irq_hw_number_t hwirq)
468{
469 struct gpio_chip *chip = d->host_data;
470
Linus Walleij14250522014-03-25 10:40:18 +0100471 irq_set_chip_data(irq, chip);
Linus Walleije45d1c82014-04-22 14:01:46 +0200472 irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
Linus Walleij7633fb92014-04-09 13:20:38 +0200473 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200474 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300475 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200476 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100477#ifdef CONFIG_ARM
478 set_irq_flags(irq, IRQF_VALID);
479#else
480 irq_set_noprobe(irq);
481#endif
Linus Walleij1333b902014-04-23 16:45:12 +0200482 /*
483 * No set-up of the hardware will happen if IRQ_TYPE_NONE
484 * is passed as default type.
485 */
486 if (chip->irq_default_type != IRQ_TYPE_NONE)
487 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100488
489 return 0;
490}
491
Linus Walleijc3626fd2014-03-28 20:42:01 +0100492static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
493{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200494 struct gpio_chip *chip = d->host_data;
495
Linus Walleijc3626fd2014-03-28 20:42:01 +0100496#ifdef CONFIG_ARM
497 set_irq_flags(irq, 0);
498#endif
Linus Walleij1c8732b2014-04-09 13:34:39 +0200499 if (chip->can_sleep)
500 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100501 irq_set_chip_and_handler(irq, NULL, NULL);
502 irq_set_chip_data(irq, NULL);
503}
504
Linus Walleij14250522014-03-25 10:40:18 +0100505static const struct irq_domain_ops gpiochip_domain_ops = {
506 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100507 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100508 /* Virtually all GPIO irqchips are twocell:ed */
509 .xlate = irq_domain_xlate_twocell,
510};
511
512static int gpiochip_irq_reqres(struct irq_data *d)
513{
514 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
515
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900516 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100517 chip_err(chip,
518 "unable to lock HW IRQ %lu for IRQ\n",
519 d->hwirq);
520 return -EINVAL;
521 }
522 return 0;
523}
524
525static void gpiochip_irq_relres(struct irq_data *d)
526{
527 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
528
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900529 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleij14250522014-03-25 10:40:18 +0100530}
531
532static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
533{
534 return irq_find_mapping(chip->irqdomain, offset);
535}
536
537/**
538 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
539 * @gpiochip: the gpiochip to remove the irqchip from
540 *
541 * This is called only from gpiochip_remove()
542 */
543static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
544{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100545 unsigned int offset;
546
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300547 acpi_gpiochip_free_interrupts(gpiochip);
548
Linus Walleijc3626fd2014-03-28 20:42:01 +0100549 /* Remove all IRQ mappings and delete the domain */
550 if (gpiochip->irqdomain) {
551 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300552 irq_dispose_mapping(
553 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100554 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100555 }
Linus Walleij14250522014-03-25 10:40:18 +0100556
557 if (gpiochip->irqchip) {
558 gpiochip->irqchip->irq_request_resources = NULL;
559 gpiochip->irqchip->irq_release_resources = NULL;
560 gpiochip->irqchip = NULL;
561 }
562}
563
564/**
565 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
566 * @gpiochip: the gpiochip to add the irqchip to
567 * @irqchip: the irqchip to add to the gpiochip
568 * @first_irq: if not dynamically assigned, the base (first) IRQ to
569 * allocate gpiochip irqs from
570 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200571 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
572 * to have the core avoid setting up any default type in the hardware.
Linus Walleij14250522014-03-25 10:40:18 +0100573 *
574 * This function closely associates a certain irqchip with a certain
575 * gpiochip, providing an irq domain to translate the local IRQs to
576 * global irqs in the gpiolib core, and making sure that the gpiochip
577 * is passed as chip data to all related functions. Driver callbacks
578 * need to use container_of() to get their local state containers back
579 * from the gpiochip passed as chip data. An irqdomain will be stored
580 * in the gpiochip that shall be used by the driver to handle IRQ number
581 * translation. The gpiochip will need to be initialized and registered
582 * before calling this function.
583 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100584 * This function will handle two cell:ed simple IRQs and assumes all
585 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100586 * need to be open coded.
587 */
588int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
589 struct irq_chip *irqchip,
590 unsigned int first_irq,
591 irq_flow_handler_t handler,
592 unsigned int type)
593{
594 struct device_node *of_node;
595 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100596 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100597
598 if (!gpiochip || !irqchip)
599 return -EINVAL;
600
601 if (!gpiochip->dev) {
602 pr_err("missing gpiochip .dev parent pointer\n");
603 return -EINVAL;
604 }
605 of_node = gpiochip->dev->of_node;
606#ifdef CONFIG_OF_GPIO
607 /*
608 * If the gpiochip has an assigned OF node this takes precendence
609 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
610 */
611 if (gpiochip->of_node)
612 of_node = gpiochip->of_node;
613#endif
614 gpiochip->irqchip = irqchip;
615 gpiochip->irq_handler = handler;
616 gpiochip->irq_default_type = type;
617 gpiochip->to_irq = gpiochip_to_irq;
618 gpiochip->irqdomain = irq_domain_add_simple(of_node,
619 gpiochip->ngpio, first_irq,
620 &gpiochip_domain_ops, gpiochip);
621 if (!gpiochip->irqdomain) {
622 gpiochip->irqchip = NULL;
623 return -EINVAL;
624 }
625 irqchip->irq_request_resources = gpiochip_irq_reqres;
626 irqchip->irq_release_resources = gpiochip_irq_relres;
627
628 /*
629 * Prepare the mapping since the irqchip shall be orthogonal to
630 * any gpiochip calls. If the first_irq was zero, this is
631 * necessary to allocate descriptors for all IRQs.
632 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100633 for (offset = 0; offset < gpiochip->ngpio; offset++) {
634 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
635 if (offset == 0)
636 /*
637 * Store the base into the gpiochip to be used when
638 * unmapping the irqs.
639 */
640 gpiochip->irq_base = irq_base;
641 }
Linus Walleij14250522014-03-25 10:40:18 +0100642
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300643 acpi_gpiochip_request_interrupts(gpiochip);
644
Linus Walleij14250522014-03-25 10:40:18 +0100645 return 0;
646}
647EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
648
649#else /* CONFIG_GPIOLIB_IRQCHIP */
650
651static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
652
653#endif /* CONFIG_GPIOLIB_IRQCHIP */
654
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530655#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100656
Linus Walleij3f0f8672012-11-20 12:40:15 +0100657/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200658 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
659 * @chip: the gpiochip to add the range for
660 * @pinctrl: the dev_name() of the pin controller to map to
661 * @gpio_offset: the start offset in the current gpio_chip number space
662 * @pin_group: name of the pin group inside the pin controller
663 */
664int gpiochip_add_pingroup_range(struct gpio_chip *chip,
665 struct pinctrl_dev *pctldev,
666 unsigned int gpio_offset, const char *pin_group)
667{
668 struct gpio_pin_range *pin_range;
669 int ret;
670
671 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
672 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200673 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200674 return -ENOMEM;
675 }
676
677 /* Use local offset as range ID */
678 pin_range->range.id = gpio_offset;
679 pin_range->range.gc = chip;
680 pin_range->range.name = chip->label;
681 pin_range->range.base = chip->base + gpio_offset;
682 pin_range->pctldev = pctldev;
683
684 ret = pinctrl_get_group_pins(pctldev, pin_group,
685 &pin_range->range.pins,
686 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100687 if (ret < 0) {
688 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200689 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100690 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200691
692 pinctrl_add_gpio_range(pctldev, &pin_range->range);
693
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200694 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
695 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200696 pinctrl_dev_get_devname(pctldev), pin_group);
697
698 list_add_tail(&pin_range->node, &chip->pin_ranges);
699
700 return 0;
701}
702EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
703
704/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100705 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
706 * @chip: the gpiochip to add the range for
707 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100708 * @gpio_offset: the start offset in the current gpio_chip number space
709 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100710 * @npins: the number of pins from the offset of each pin space (GPIO and
711 * pin controller) to accumulate in this range
712 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100713int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100714 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100715 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530716{
717 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800718 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530719
Linus Walleij3f0f8672012-11-20 12:40:15 +0100720 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530721 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200722 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100723 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530724 }
725
Linus Walleij3f0f8672012-11-20 12:40:15 +0100726 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100727 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100728 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530729 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100730 pin_range->range.base = chip->base + gpio_offset;
731 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530732 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100733 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530734 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100735 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800736 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200737 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100738 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800739 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100740 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200741 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
742 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100743 pinctl_name,
744 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530745
746 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100747
748 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530749}
Linus Walleij165adc92012-11-06 14:49:39 +0100750EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530751
Linus Walleij3f0f8672012-11-20 12:40:15 +0100752/**
753 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
754 * @chip: the chip to remove all the mappings for
755 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530756void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
757{
758 struct gpio_pin_range *pin_range, *tmp;
759
760 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
761 list_del(&pin_range->node);
762 pinctrl_remove_gpio_range(pin_range->pctldev,
763 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100764 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530765 }
766}
Linus Walleij165adc92012-11-06 14:49:39 +0100767EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
768
769#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530770
David Brownelld2876d02008-02-04 22:28:20 -0800771/* These "optional" allocation calls help prevent drivers from stomping
772 * on each other, and help provide better diagnostics in debugfs.
773 * They're called even less than the "set direction" calls.
774 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200775static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800776{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200777 struct gpio_chip *chip = desc->chip;
778 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800779 unsigned long flags;
780
781 spin_lock_irqsave(&gpio_lock, flags);
782
David Brownelld2876d02008-02-04 22:28:20 -0800783 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700784 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800785 */
786
787 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
788 desc_set_label(desc, label ? : "?");
789 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700790 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800791 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800792 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700793 }
794
795 if (chip->request) {
796 /* chip->request may sleep */
797 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900798 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700799 spin_lock_irqsave(&gpio_lock, flags);
800
801 if (status < 0) {
802 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700803 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300804 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700805 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700806 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300807 if (chip->get_direction) {
808 /* chip->get_direction may sleep */
809 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900810 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300811 spin_lock_irqsave(&gpio_lock, flags);
812 }
David Brownelld2876d02008-02-04 22:28:20 -0800813done:
Mika Westerberg77c2d792014-03-10 14:54:50 +0200814 spin_unlock_irqrestore(&gpio_lock, flags);
815 return status;
816}
817
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900818int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200819{
820 int status = -EPROBE_DEFER;
821 struct gpio_chip *chip;
822
823 if (!desc) {
824 pr_warn("%s: invalid GPIO\n", __func__);
825 return -EINVAL;
826 }
827
828 chip = desc->chip;
829 if (!chip)
830 goto done;
831
832 if (try_module_get(chip->owner)) {
833 status = __gpiod_request(desc, label);
834 if (status < 0)
835 module_put(chip->owner);
836 }
837
838done:
David Brownelld2876d02008-02-04 22:28:20 -0800839 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200840 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200841
David Brownelld2876d02008-02-04 22:28:20 -0800842 return status;
843}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900844
Mika Westerberg77c2d792014-03-10 14:54:50 +0200845static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800846{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200847 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -0800848 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -0700849 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800850
Uwe Kleine-König3d599d12008-10-15 22:03:12 -0700851 might_sleep();
852
Alexandre Courbot372e7222013-02-03 01:29:29 +0900853 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700854
David Brownelld2876d02008-02-04 22:28:20 -0800855 spin_lock_irqsave(&gpio_lock, flags);
856
David Brownell35e8bb52008-10-15 22:03:16 -0700857 chip = desc->chip;
858 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
859 if (chip->free) {
860 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -0700861 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900862 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700863 spin_lock_irqsave(&gpio_lock, flags);
864 }
David Brownelld2876d02008-02-04 22:28:20 -0800865 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -0800866 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -0700867 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530868 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +0530869 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200870 ret = true;
871 }
David Brownelld2876d02008-02-04 22:28:20 -0800872
873 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200874 return ret;
875}
876
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900877void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200878{
879 if (desc && __gpiod_free(desc))
880 module_put(desc->chip->owner);
881 else
882 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -0800883}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900884
David Brownelld2876d02008-02-04 22:28:20 -0800885/**
886 * gpiochip_is_requested - return string iff signal was requested
887 * @chip: controller managing the signal
888 * @offset: of signal within controller's 0..(ngpio - 1) range
889 *
890 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +0900891 * The string returned is the label passed to gpio_request(); if none has been
892 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -0800893 *
894 * This function is for use by GPIO controller drivers. The label can
895 * help with diagnostics, and knowing that the signal is used as a GPIO
896 * can help avoid accidentally multiplexing it to another controller.
897 */
898const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
899{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900900 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800901
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900902 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -0800903 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900904
905 desc = &chip->desc[offset];
906
Alexandre Courbot372e7222013-02-03 01:29:29 +0900907 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -0800908 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900909 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -0800910}
911EXPORT_SYMBOL_GPL(gpiochip_is_requested);
912
Mika Westerberg77c2d792014-03-10 14:54:50 +0200913/**
914 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
915 * @desc: GPIO descriptor to request
916 * @label: label for the GPIO
917 *
918 * Function allows GPIO chip drivers to request and use their own GPIO
919 * descriptors via gpiolib API. Difference to gpiod_request() is that this
920 * function will not increase reference count of the GPIO chip module. This
921 * allows the GPIO chip module to be unloaded as needed (we assume that the
922 * GPIO chip driver handles freeing the GPIOs it has requested).
923 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700924struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
925 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200926{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700927 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
928 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200929
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700930 if (IS_ERR(desc)) {
931 chip_err(chip, "failed to get GPIO descriptor\n");
932 return desc;
933 }
934
935 err = __gpiod_request(desc, label);
936 if (err < 0)
937 return ERR_PTR(err);
938
939 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200940}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700941EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200942
943/**
944 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
945 * @desc: GPIO descriptor to free
946 *
947 * Function frees the given GPIO requested previously with
948 * gpiochip_request_own_desc().
949 */
950void gpiochip_free_own_desc(struct gpio_desc *desc)
951{
952 if (desc)
953 __gpiod_free(desc);
954}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700955EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -0800956
957/* Drivers MUST set GPIO direction before making get/set calls. In
958 * some cases this is done in early boot, before IRQs are enabled.
959 *
960 * As a rule these aren't called more than once (except for drivers
961 * using the open-drain emulation idiom) so these are natural places
962 * to accumulate extra debugging checks. Note that we can't (yet)
963 * rely on gpio_request() having been called beforehand.
964 */
965
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700966/**
967 * gpiod_direction_input - set the GPIO direction to input
968 * @desc: GPIO to set to input
969 *
970 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
971 * be called safely on it.
972 *
973 * Return 0 in case of success, else an error code.
974 */
975int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800976{
David Brownelld2876d02008-02-04 22:28:20 -0800977 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800978 int status = -EINVAL;
979
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200980 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900981 pr_warn("%s: invalid GPIO\n", __func__);
982 return -EINVAL;
983 }
984
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200985 chip = desc->chip;
986 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +0100987 gpiod_warn(desc,
988 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +0200989 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200990 return -EIO;
991 }
992
Alexandre Courbotd82da792014-07-22 16:17:43 +0900993 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -0800994 if (status == 0)
995 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -0600996
Alexandre Courbot372e7222013-02-03 01:29:29 +0900997 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +0900998
David Brownelld2876d02008-02-04 22:28:20 -0800999 return status;
1000}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001001EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001002
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001003static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001004{
David Brownelld2876d02008-02-04 22:28:20 -08001005 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001006 int status = -EINVAL;
1007
Linus Walleijd468bf92013-09-24 11:54:38 +02001008 /* GPIOs used for IRQs shall not be set as output */
1009 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1010 gpiod_err(desc,
1011 "%s: tried to set a GPIO tied to an IRQ as output\n",
1012 __func__);
1013 return -EIO;
1014 }
1015
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301016 /* Open drain pin should not be driven to 1 */
1017 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001018 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301019
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301020 /* Open source pin should not be driven to 0 */
1021 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001022 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301023
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001024 chip = desc->chip;
1025 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001026 gpiod_warn(desc,
1027 "%s: missing set() or direction_output() operations\n",
1028 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001029 return -EIO;
1030 }
1031
Alexandre Courbotd82da792014-07-22 16:17:43 +09001032 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001033 if (status == 0)
1034 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001035 trace_gpio_value(desc_to_gpio(desc), 0, value);
1036 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001037 return status;
1038}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001039
1040/**
1041 * gpiod_direction_output_raw - set the GPIO direction to output
1042 * @desc: GPIO to set to output
1043 * @value: initial output value of the GPIO
1044 *
1045 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1046 * be called safely on it. The initial value of the output must be specified
1047 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1048 *
1049 * Return 0 in case of success, else an error code.
1050 */
1051int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1052{
1053 if (!desc || !desc->chip) {
1054 pr_warn("%s: invalid GPIO\n", __func__);
1055 return -EINVAL;
1056 }
1057 return _gpiod_direction_output_raw(desc, value);
1058}
1059EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1060
1061/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301062 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001063 * @desc: GPIO to set to output
1064 * @value: initial output value of the GPIO
1065 *
1066 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1067 * be called safely on it. The initial value of the output must be specified
1068 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1069 * account.
1070 *
1071 * Return 0 in case of success, else an error code.
1072 */
1073int gpiod_direction_output(struct gpio_desc *desc, int value)
1074{
1075 if (!desc || !desc->chip) {
1076 pr_warn("%s: invalid GPIO\n", __func__);
1077 return -EINVAL;
1078 }
1079 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1080 value = !value;
1081 return _gpiod_direction_output_raw(desc, value);
1082}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001083EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001084
Felipe Balbic4b5be92010-05-26 14:42:23 -07001085/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001086 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001087 * @gpio: the gpio to set debounce time
1088 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001089 *
1090 * returns -ENOTSUPP if the controller does not support setting
1091 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001092 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001093int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001094{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001095 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001096
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001097 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001098 pr_warn("%s: invalid GPIO\n", __func__);
1099 return -EINVAL;
1100 }
1101
Felipe Balbic4b5be92010-05-26 14:42:23 -07001102 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001103 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001104 gpiod_dbg(desc,
1105 "%s: missing set() or set_debounce() operations\n",
1106 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001107 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001108 }
1109
Alexandre Courbotd82da792014-07-22 16:17:43 +09001110 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001111}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001112EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001113
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001114/**
1115 * gpiod_is_active_low - test whether a GPIO is active-low or not
1116 * @desc: the gpio descriptor to test
1117 *
1118 * Returns 1 if the GPIO is active-low, 0 otherwise.
1119 */
1120int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001121{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001122 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001123}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001124EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001125
1126/* I/O calls are only valid after configuration completed; the relevant
1127 * "is this a valid GPIO" error checks should already have been done.
1128 *
1129 * "Get" operations are often inlinable as reading a pin value register,
1130 * and masking the relevant bit in that register.
1131 *
1132 * When "set" operations are inlinable, they involve writing that mask to
1133 * one register to set a low value, or a different register to set it high.
1134 * Otherwise locking is needed, so there may be little value to inlining.
1135 *
1136 *------------------------------------------------------------------------
1137 *
1138 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1139 * have requested the GPIO. That can include implicit requesting by
1140 * a direction setting call. Marking a gpio as requested locks its chip
1141 * in memory, guaranteeing that these table lookups need no more locking
1142 * and that gpiochip_remove() will fail.
1143 *
1144 * REVISIT when debugging, consider adding some instrumentation to ensure
1145 * that the GPIO was actually requested.
1146 */
1147
Alexandre Courbot23600962014-03-11 15:52:09 +09001148static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001149{
1150 struct gpio_chip *chip;
Alexandre Courbot23600962014-03-11 15:52:09 +09001151 bool value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001152 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001153
Alexandre Courbot372e7222013-02-03 01:29:29 +09001154 chip = desc->chip;
1155 offset = gpio_chip_hwgpio(desc);
Alexandre Courbot23600962014-03-11 15:52:09 +09001156 value = chip->get ? chip->get(chip, offset) : false;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001157 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001158 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001159}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001160
David Brownelld2876d02008-02-04 22:28:20 -08001161/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001162 * gpiod_get_raw_value() - return a gpio's raw value
1163 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001164 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001165 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1166 * its ACTIVE_LOW status.
1167 *
1168 * This function should be called from contexts where we cannot sleep, and will
1169 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001170 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001171int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001172{
David Brownelld2876d02008-02-04 22:28:20 -08001173 if (!desc)
1174 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001175 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001176 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001177 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001178}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001179EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001180
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001181/**
1182 * gpiod_get_value() - return a gpio's value
1183 * @desc: gpio whose value will be returned
1184 *
1185 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1186 * account.
1187 *
1188 * This function should be called from contexts where we cannot sleep, and will
1189 * complain if the GPIO chip functions potentially sleep.
1190 */
1191int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001192{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001193 int value;
1194 if (!desc)
1195 return 0;
1196 /* Should be using gpio_get_value_cansleep() */
1197 WARN_ON(desc->chip->can_sleep);
1198
1199 value = _gpiod_get_raw_value(desc);
1200 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1201 value = !value;
1202
1203 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001204}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001205EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001206
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301207/*
1208 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001209 * @desc: gpio descriptor whose state need to be set.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301210 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1211 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001212static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301213{
1214 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001215 struct gpio_chip *chip = desc->chip;
1216 int offset = gpio_chip_hwgpio(desc);
1217
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301218 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001219 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301220 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001221 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301222 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001223 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301224 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001225 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301226 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001227 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301228 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001229 gpiod_err(desc,
1230 "%s: Error in set_value for open drain err %d\n",
1231 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301232}
1233
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301234/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001235 * _gpio_set_open_source_value() - Set the open source gpio's value.
1236 * @desc: gpio descriptor whose state need to be set.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301237 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1238 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001239static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301240{
1241 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001242 struct gpio_chip *chip = desc->chip;
1243 int offset = gpio_chip_hwgpio(desc);
1244
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301245 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001246 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301247 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001248 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301249 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001250 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301251 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001252 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301253 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001254 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301255 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001256 gpiod_err(desc,
1257 "%s: Error in set_value for open source err %d\n",
1258 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301259}
1260
Alexandre Courbot23600962014-03-11 15:52:09 +09001261static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001262{
1263 struct gpio_chip *chip;
1264
Alexandre Courbot372e7222013-02-03 01:29:29 +09001265 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001266 trace_gpio_value(desc_to_gpio(desc), 0, value);
1267 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1268 _gpio_set_open_drain_value(desc, value);
1269 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1270 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301271 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001272 chip->set(chip, gpio_chip_hwgpio(desc), value);
1273}
1274
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001275/*
1276 * set multiple outputs on the same chip;
1277 * use the chip's set_multiple function if available;
1278 * otherwise set the outputs sequentially;
1279 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1280 * defines which outputs are to be changed
1281 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1282 * defines the values the outputs specified by mask are to be set to
1283 */
1284static void gpio_chip_set_multiple(struct gpio_chip *chip,
1285 unsigned long *mask, unsigned long *bits)
1286{
1287 if (chip->set_multiple) {
1288 chip->set_multiple(chip, mask, bits);
1289 } else {
1290 int i;
1291 for (i = 0; i < chip->ngpio; i++) {
1292 if (mask[BIT_WORD(i)] == 0) {
1293 /* no more set bits in this mask word;
1294 * skip ahead to the next word */
1295 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1296 continue;
1297 }
1298 /* set outputs if the corresponding mask bit is set */
1299 if (__test_and_clear_bit(i, mask)) {
1300 chip->set(chip, i, test_bit(i, bits));
1301 }
1302 }
1303 }
1304}
1305
1306static void gpiod_set_array_priv(bool raw, bool can_sleep,
1307 unsigned int array_size,
1308 struct gpio_desc **desc_array,
1309 int *value_array)
1310{
1311 int i = 0;
1312
1313 while (i < array_size) {
1314 struct gpio_chip *chip = desc_array[i]->chip;
1315 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1316 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1317 int count = 0;
1318
1319 if (!can_sleep) {
1320 WARN_ON(chip->can_sleep);
1321 }
1322 memset(mask, 0, sizeof(mask));
1323 do {
1324 struct gpio_desc *desc = desc_array[i];
1325 int hwgpio = gpio_chip_hwgpio(desc);
1326 int value = value_array[i];
1327
1328 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1329 value = !value;
1330 trace_gpio_value(desc_to_gpio(desc), 0, value);
1331 /*
1332 * collect all normal outputs belonging to the same chip
1333 * open drain and open source outputs are set individually
1334 */
1335 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
1336 _gpio_set_open_drain_value(desc,value);
1337 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1338 _gpio_set_open_source_value(desc, value);
1339 } else {
1340 __set_bit(hwgpio, mask);
1341 if (value) {
1342 __set_bit(hwgpio, bits);
1343 } else {
1344 __clear_bit(hwgpio, bits);
1345 }
1346 count++;
1347 }
1348 i++;
1349 } while ((i < array_size) && (desc_array[i]->chip == chip));
1350 /* push collected bits to outputs */
1351 if (count != 0) {
1352 gpio_chip_set_multiple(chip, mask, bits);
1353 }
1354 }
1355}
1356
David Brownelld2876d02008-02-04 22:28:20 -08001357/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001358 * gpiod_set_raw_value() - assign a gpio's raw value
1359 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001360 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001361 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001362 * Set the raw value of the GPIO, i.e. the value of its physical line without
1363 * regard for its ACTIVE_LOW status.
1364 *
1365 * This function should be called from contexts where we cannot sleep, and will
1366 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001367 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001368void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001369{
David Brownelld2876d02008-02-04 22:28:20 -08001370 if (!desc)
1371 return;
David Brownelld2876d02008-02-04 22:28:20 -08001372 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001373 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001374 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001375}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001376EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001377
1378/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001379 * gpiod_set_value() - assign a gpio's value
1380 * @desc: gpio whose value will be assigned
1381 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001382 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001383 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1384 * account
1385 *
1386 * This function should be called from contexts where we cannot sleep, and will
1387 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001388 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001389void gpiod_set_value(struct gpio_desc *desc, int value)
1390{
1391 if (!desc)
1392 return;
1393 /* Should be using gpio_set_value_cansleep() */
1394 WARN_ON(desc->chip->can_sleep);
1395 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1396 value = !value;
1397 _gpiod_set_raw_value(desc, value);
1398}
1399EXPORT_SYMBOL_GPL(gpiod_set_value);
1400
1401/**
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001402 * gpiod_set_raw_array() - assign values to an array of GPIOs
1403 * @array_size: number of elements in the descriptor / value arrays
1404 * @desc_array: array of GPIO descriptors whose values will be assigned
1405 * @value_array: array of values to assign
1406 *
1407 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1408 * without regard for their ACTIVE_LOW status.
1409 *
1410 * This function should be called from contexts where we cannot sleep, and will
1411 * complain if the GPIO chip functions potentially sleep.
1412 */
1413void gpiod_set_raw_array(unsigned int array_size,
1414 struct gpio_desc **desc_array, int *value_array)
1415{
1416 if (!desc_array)
1417 return;
1418 gpiod_set_array_priv(true, false, array_size, desc_array, value_array);
1419}
1420EXPORT_SYMBOL_GPL(gpiod_set_raw_array);
1421
1422/**
1423 * gpiod_set_array() - assign values to an array of GPIOs
1424 * @array_size: number of elements in the descriptor / value arrays
1425 * @desc_array: array of GPIO descriptors whose values will be assigned
1426 * @value_array: array of values to assign
1427 *
1428 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1429 * into account.
1430 *
1431 * This function should be called from contexts where we cannot sleep, and will
1432 * complain if the GPIO chip functions potentially sleep.
1433 */
1434void gpiod_set_array(unsigned int array_size,
1435 struct gpio_desc **desc_array, int *value_array)
1436{
1437 if (!desc_array)
1438 return;
1439 gpiod_set_array_priv(false, false, array_size, desc_array, value_array);
1440}
1441EXPORT_SYMBOL_GPL(gpiod_set_array);
1442
1443/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001444 * gpiod_cansleep() - report whether gpio value access may sleep
1445 * @desc: gpio to check
1446 *
1447 */
1448int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001449{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001450 if (!desc)
1451 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001452 return desc->chip->can_sleep;
1453}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001454EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001455
David Brownell0f6d5042008-10-15 22:03:14 -07001456/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001457 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1458 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001459 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001460 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1461 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001462 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001463int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001464{
1465 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001466 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001467
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001468 if (!desc)
1469 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001470 chip = desc->chip;
1471 offset = gpio_chip_hwgpio(desc);
1472 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1473}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001474EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001475
Linus Walleijd468bf92013-09-24 11:54:38 +02001476/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001477 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001478 * @chip: the chip the GPIO to lock belongs to
1479 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001480 *
1481 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001482 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001483 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001484int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001485{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001486 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001487 return -EINVAL;
1488
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001489 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1490 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001491 "%s: tried to flag a GPIO set as output for IRQ\n",
1492 __func__);
1493 return -EIO;
1494 }
1495
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001496 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001497 return 0;
1498}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001499EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001500
Linus Walleijd468bf92013-09-24 11:54:38 +02001501/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001502 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001503 * @chip: the chip the GPIO to lock belongs to
1504 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001505 *
1506 * This is used directly by GPIO drivers that want to indicate
1507 * that a certain GPIO is no longer used exclusively for IRQ.
1508 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001509void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001510{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001511 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001512 return;
1513
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001514 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001515}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001516EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001517
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001518/**
1519 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1520 * @desc: gpio whose value will be returned
1521 *
1522 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1523 * its ACTIVE_LOW status.
1524 *
1525 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001526 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001527int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001528{
David Brownelld2876d02008-02-04 22:28:20 -08001529 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001530 if (!desc)
1531 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001532 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001533}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001534EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001535
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001536/**
1537 * gpiod_get_value_cansleep() - return a gpio's value
1538 * @desc: gpio whose value will be returned
1539 *
1540 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1541 * account.
1542 *
1543 * This function is to be called from contexts that can sleep.
1544 */
1545int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001546{
David Brownelld2876d02008-02-04 22:28:20 -08001547 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001548
1549 might_sleep_if(extra_checks);
1550 if (!desc)
1551 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001552
1553 value = _gpiod_get_raw_value(desc);
1554 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1555 value = !value;
1556
David Brownelld2876d02008-02-04 22:28:20 -08001557 return value;
1558}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001559EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001560
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001561/**
1562 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1563 * @desc: gpio whose value will be assigned
1564 * @value: value to assign
1565 *
1566 * Set the raw value of the GPIO, i.e. the value of its physical line without
1567 * regard for its ACTIVE_LOW status.
1568 *
1569 * This function is to be called from contexts that can sleep.
1570 */
1571void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001572{
David Brownelld2876d02008-02-04 22:28:20 -08001573 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001574 if (!desc)
1575 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001576 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001577}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001578EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001579
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001580/**
1581 * gpiod_set_value_cansleep() - assign a gpio's value
1582 * @desc: gpio whose value will be assigned
1583 * @value: value to assign
1584 *
1585 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1586 * account
1587 *
1588 * This function is to be called from contexts that can sleep.
1589 */
1590void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001591{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001592 might_sleep_if(extra_checks);
1593 if (!desc)
1594 return;
1595
1596 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1597 value = !value;
1598 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001599}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001600EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001601
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001602/**
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001603 * gpiod_set_raw_array_cansleep() - assign values to an array of GPIOs
1604 * @array_size: number of elements in the descriptor / value arrays
1605 * @desc_array: array of GPIO descriptors whose values will be assigned
1606 * @value_array: array of values to assign
1607 *
1608 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1609 * without regard for their ACTIVE_LOW status.
1610 *
1611 * This function is to be called from contexts that can sleep.
1612 */
1613void gpiod_set_raw_array_cansleep(unsigned int array_size,
1614 struct gpio_desc **desc_array,
1615 int *value_array)
1616{
1617 might_sleep_if(extra_checks);
1618 if (!desc_array)
1619 return;
1620 gpiod_set_array_priv(true, true, array_size, desc_array, value_array);
1621}
1622EXPORT_SYMBOL_GPL(gpiod_set_raw_array_cansleep);
1623
1624/**
1625 * gpiod_set_array_cansleep() - assign values to an array of GPIOs
1626 * @array_size: number of elements in the descriptor / value arrays
1627 * @desc_array: array of GPIO descriptors whose values will be assigned
1628 * @value_array: array of values to assign
1629 *
1630 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1631 * into account.
1632 *
1633 * This function is to be called from contexts that can sleep.
1634 */
1635void gpiod_set_array_cansleep(unsigned int array_size,
1636 struct gpio_desc **desc_array,
1637 int *value_array)
1638{
1639 might_sleep_if(extra_checks);
1640 if (!desc_array)
1641 return;
1642 gpiod_set_array_priv(false, true, array_size, desc_array, value_array);
1643}
1644EXPORT_SYMBOL_GPL(gpiod_set_array_cansleep);
1645
1646/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001647 * gpiod_add_lookup_table() - register GPIO device consumers
1648 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001649 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001650void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001651{
1652 mutex_lock(&gpio_lookup_lock);
1653
Alexandre Courbotad824782013-12-03 12:20:11 +09001654 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001655
1656 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001657}
1658
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001659static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001660 unsigned int idx,
1661 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001662{
Thierry Redingdd34c372014-04-23 17:28:09 +02001663 static const char *suffixes[] = { "gpios", "gpio" };
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001664 char prop_name[32]; /* 32 is max size of property name */
1665 enum of_gpio_flags of_flags;
1666 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001667 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001668
Thierry Redingdd34c372014-04-23 17:28:09 +02001669 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1670 if (con_id)
1671 snprintf(prop_name, 32, "%s-%s", con_id, suffixes[i]);
1672 else
1673 snprintf(prop_name, 32, "%s", suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001674
Thierry Redingdd34c372014-04-23 17:28:09 +02001675 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1676 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001677 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001678 break;
1679 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001680
1681 if (IS_ERR(desc))
1682 return desc;
1683
1684 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001685 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001686
1687 return desc;
1688}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001689
Mika Westerberg81f59e92013-10-10 11:01:09 +03001690static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001691 unsigned int idx,
1692 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001693{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001694 static const char * const suffixes[] = { "gpios", "gpio" };
1695 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03001696 struct acpi_gpio_info info;
1697 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001698 char propname[32];
1699 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03001700
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001701 /* Try first from _DSD */
1702 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1703 if (con_id && strcmp(con_id, "gpios")) {
1704 snprintf(propname, sizeof(propname), "%s-%s",
1705 con_id, suffixes[i]);
1706 } else {
1707 snprintf(propname, sizeof(propname), "%s",
1708 suffixes[i]);
1709 }
Mika Westerberge01f4402013-10-10 11:01:10 +03001710
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001711 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1712 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1713 break;
1714 }
1715
1716 /* Then from plain _CRS GPIOs */
1717 if (IS_ERR(desc)) {
1718 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1719 if (IS_ERR(desc))
1720 return desc;
1721 }
1722
1723 if (info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001724 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001725
1726 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001727}
1728
Alexandre Courbotad824782013-12-03 12:20:11 +09001729static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1730{
1731 const char *dev_id = dev ? dev_name(dev) : NULL;
1732 struct gpiod_lookup_table *table;
1733
1734 mutex_lock(&gpio_lookup_lock);
1735
1736 list_for_each_entry(table, &gpio_lookup_list, list) {
1737 if (table->dev_id && dev_id) {
1738 /*
1739 * Valid strings on both ends, must be identical to have
1740 * a match
1741 */
1742 if (!strcmp(table->dev_id, dev_id))
1743 goto found;
1744 } else {
1745 /*
1746 * One of the pointers is NULL, so both must be to have
1747 * a match
1748 */
1749 if (dev_id == table->dev_id)
1750 goto found;
1751 }
1752 }
1753 table = NULL;
1754
1755found:
1756 mutex_unlock(&gpio_lookup_lock);
1757 return table;
1758}
1759
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001760static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001761 unsigned int idx,
1762 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001763{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001764 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001765 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001766 struct gpiod_lookup *p;
1767
Alexandre Courbotad824782013-12-03 12:20:11 +09001768 table = gpiod_find_lookup_table(dev);
1769 if (!table)
1770 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001771
Alexandre Courbotad824782013-12-03 12:20:11 +09001772 for (p = &table->table[0]; p->chip_label; p++) {
1773 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001774
Alexandre Courbotad824782013-12-03 12:20:11 +09001775 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001776 if (p->idx != idx)
1777 continue;
1778
Alexandre Courbotad824782013-12-03 12:20:11 +09001779 /* If the lookup entry has a con_id, require exact match */
1780 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1781 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001782
Alexandre Courbotad824782013-12-03 12:20:11 +09001783 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001784
Alexandre Courbotad824782013-12-03 12:20:11 +09001785 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001786 dev_err(dev, "cannot find GPIO chip %s\n",
1787 p->chip_label);
1788 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001789 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001790
Alexandre Courbotad824782013-12-03 12:20:11 +09001791 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001792 dev_err(dev,
1793 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1794 idx, chip->ngpio, chip->label);
1795 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001796 }
1797
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001798 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001799 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001800
1801 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001802 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001803
1804 return desc;
1805}
1806
1807/**
Thierry Reding08791622014-04-25 16:54:22 +02001808 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09001809 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001810 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001811 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001812 *
1813 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001814 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1815 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001816 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001817struct gpio_desc *__must_check __gpiod_get(struct device *dev, const char *con_id,
1818 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001819{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001820 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001821}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001822EXPORT_SYMBOL_GPL(__gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001823
1824/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02001825 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1826 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1827 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001828 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02001829 *
1830 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1831 * the requested function it will return NULL. This is convenient for drivers
1832 * that need to handle optional GPIOs.
1833 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001834struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
1835 const char *con_id,
1836 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02001837{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001838 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001839}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001840EXPORT_SYMBOL_GPL(__gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001841
1842/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001843 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02001844 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001845 * @con_id: function within the GPIO consumer
1846 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001847 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001848 *
1849 * This variant of gpiod_get() allows to access GPIOs other than the first
1850 * defined one for functions that define several GPIOs.
1851 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001852 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1853 * requested function and/or index, or another IS_ERR() code if an error
1854 * occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001855 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001856struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001857 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001858 unsigned int idx,
1859 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001860{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001861 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001862 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001863 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001864
1865 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
1866
1867 /* Using device tree? */
1868 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
1869 dev_dbg(dev, "using device tree for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001870 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
Mika Westerberg81f59e92013-10-10 11:01:09 +03001871 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
1872 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001873 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001874 }
1875
1876 /*
1877 * Either we are not using DT or ACPI, or their lookup did not return
1878 * a result. In that case, use platform lookup as a fallback.
1879 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001880 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04001881 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001882 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001883 }
1884
1885 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02001886 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001887 return desc;
1888 }
1889
1890 status = gpiod_request(desc, con_id);
1891
1892 if (status < 0)
1893 return ERR_PTR(status);
1894
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001895 if (lookupflags & GPIO_ACTIVE_LOW)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001896 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001897 if (lookupflags & GPIO_OPEN_DRAIN)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001898 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001899 if (lookupflags & GPIO_OPEN_SOURCE)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001900 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001901
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001902 /* No particular flag request, return here... */
Adrian Hunter72f908c2014-09-22 11:01:16 +03001903 if (!(flags & GPIOD_FLAGS_BIT_DIR_SET))
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001904 return desc;
1905
1906 /* Process flags */
1907 if (flags & GPIOD_FLAGS_BIT_DIR_OUT)
1908 status = gpiod_direction_output(desc,
1909 flags & GPIOD_FLAGS_BIT_DIR_VAL);
1910 else
1911 status = gpiod_direction_input(desc);
1912
1913 if (status < 0) {
1914 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
1915 gpiod_put(desc);
1916 return ERR_PTR(status);
1917 }
1918
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001919 return desc;
1920}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001921EXPORT_SYMBOL_GPL(__gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001922
1923/**
Mika Westerberg40b73182014-10-21 13:33:59 +02001924 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
1925 * @fwnode: handle of the firmware node
1926 * @propname: name of the firmware property representing the GPIO
1927 *
1928 * This function can be used for drivers that get their configuration
1929 * from firmware.
1930 *
1931 * Function properly finds the corresponding GPIO using whatever is the
1932 * underlying firmware interface and then makes sure that the GPIO
1933 * descriptor is requested before it is returned to the caller.
1934 *
1935 * In case of error an ERR_PTR() is returned.
1936 */
1937struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
1938 const char *propname)
1939{
1940 struct gpio_desc *desc = ERR_PTR(-ENODEV);
1941 bool active_low = false;
1942 int ret;
1943
1944 if (!fwnode)
1945 return ERR_PTR(-EINVAL);
1946
1947 if (is_of_node(fwnode)) {
1948 enum of_gpio_flags flags;
1949
1950 desc = of_get_named_gpiod_flags(of_node(fwnode), propname, 0,
1951 &flags);
1952 if (!IS_ERR(desc))
1953 active_low = flags & OF_GPIO_ACTIVE_LOW;
1954 } else if (is_acpi_node(fwnode)) {
1955 struct acpi_gpio_info info;
1956
1957 desc = acpi_get_gpiod_by_index(acpi_node(fwnode), propname, 0,
1958 &info);
1959 if (!IS_ERR(desc))
1960 active_low = info.active_low;
1961 }
1962
1963 if (IS_ERR(desc))
1964 return desc;
1965
1966 ret = gpiod_request(desc, NULL);
1967 if (ret)
1968 return ERR_PTR(ret);
1969
1970 /* Only value flag can be set from both DT and ACPI is active_low */
1971 if (active_low)
1972 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
1973
1974 return desc;
1975}
1976EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
1977
1978/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02001979 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
1980 * function
1981 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1982 * @con_id: function within the GPIO consumer
1983 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001984 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02001985 *
1986 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
1987 * specified index was assigned to the requested function it will return NULL.
1988 * This is convenient for drivers that need to handle optional GPIOs.
1989 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001990struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02001991 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001992 unsigned int index,
1993 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02001994{
1995 struct gpio_desc *desc;
1996
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001997 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001998 if (IS_ERR(desc)) {
1999 if (PTR_ERR(desc) == -ENOENT)
2000 return NULL;
2001 }
2002
2003 return desc;
2004}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002005EXPORT_SYMBOL_GPL(__gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002006
2007/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002008 * gpiod_put - dispose of a GPIO descriptor
2009 * @desc: GPIO descriptor to dispose of
2010 *
2011 * No descriptor can be used after gpiod_put() has been called on it.
2012 */
2013void gpiod_put(struct gpio_desc *desc)
2014{
2015 gpiod_free(desc);
2016}
2017EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002018
David Brownelld2876d02008-02-04 22:28:20 -08002019#ifdef CONFIG_DEBUG_FS
2020
2021static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2022{
2023 unsigned i;
2024 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002025 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002026 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002027 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002028
2029 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2030 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2031 continue;
2032
Alexandre Courbot372e7222013-02-03 01:29:29 +09002033 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002034 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002035 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2036 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002037 gpio, gdesc->label,
2038 is_out ? "out" : "in ",
2039 chip->get
2040 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002041 : "? ",
2042 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002043 seq_printf(s, "\n");
2044 }
2045}
2046
Thierry Redingf9c4a312012-04-12 13:26:01 +02002047static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002048{
Grant Likely362432a2013-02-09 09:41:49 +00002049 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002050 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002051 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002052
2053 s->private = "";
2054
Grant Likely362432a2013-02-09 09:41:49 +00002055 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002056 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002057 if (index-- == 0) {
2058 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002059 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002060 }
2061 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002062
2063 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002064}
2065
2066static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2067{
Grant Likely362432a2013-02-09 09:41:49 +00002068 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002069 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002070 void *ret = NULL;
2071
Grant Likely362432a2013-02-09 09:41:49 +00002072 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002073 if (list_is_last(&chip->list, &gpio_chips))
2074 ret = NULL;
2075 else
2076 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002077 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002078
2079 s->private = "\n";
2080 ++*pos;
2081
2082 return ret;
2083}
2084
2085static void gpiolib_seq_stop(struct seq_file *s, void *v)
2086{
2087}
2088
2089static int gpiolib_seq_show(struct seq_file *s, void *v)
2090{
2091 struct gpio_chip *chip = v;
2092 struct device *dev;
2093
2094 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2095 chip->base, chip->base + chip->ngpio - 1);
2096 dev = chip->dev;
2097 if (dev)
2098 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2099 dev_name(dev));
2100 if (chip->label)
2101 seq_printf(s, ", %s", chip->label);
2102 if (chip->can_sleep)
2103 seq_printf(s, ", can sleep");
2104 seq_printf(s, ":\n");
2105
2106 if (chip->dbg_show)
2107 chip->dbg_show(s, chip);
2108 else
2109 gpiolib_dbg_show(s, chip);
2110
David Brownelld2876d02008-02-04 22:28:20 -08002111 return 0;
2112}
2113
Thierry Redingf9c4a312012-04-12 13:26:01 +02002114static const struct seq_operations gpiolib_seq_ops = {
2115 .start = gpiolib_seq_start,
2116 .next = gpiolib_seq_next,
2117 .stop = gpiolib_seq_stop,
2118 .show = gpiolib_seq_show,
2119};
2120
David Brownelld2876d02008-02-04 22:28:20 -08002121static int gpiolib_open(struct inode *inode, struct file *file)
2122{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002123 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002124}
2125
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002126static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002127 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002128 .open = gpiolib_open,
2129 .read = seq_read,
2130 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002131 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002132};
2133
2134static int __init gpiolib_debugfs_init(void)
2135{
2136 /* /sys/kernel/debug/gpio */
2137 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2138 NULL, NULL, &gpiolib_operations);
2139 return 0;
2140}
2141subsys_initcall(gpiolib_debugfs_init);
2142
2143#endif /* DEBUG_FS */