blob: 8eba02db5608dbc4b471f4672b29fa5d75da0620 [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d2008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020018#include <linux/pinctrl/consumer.h>
David Brownelld2876d02008-02-04 22:28:20 -080019
Mika Westerberg664e3e52014-01-08 12:40:54 +020020#include "gpiolib.h"
21
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060022#define CREATE_TRACE_POINTS
23#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080024
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070025/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080026 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070027 * The GPIO programming interface allows for inlining speed-critical
28 * get/set operations for common cases, so that access to SOC-integrated
29 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080030 */
31
32
33/* When debugging, extend minimal trust to callers and platform code.
34 * Also emit diagnostic messages that may help initial bringup, when
35 * board setup or driver bugs are most common.
36 *
37 * Otherwise, minimize overhead in what may be bitbanging codepaths.
38 */
39#ifdef DEBUG
40#define extra_checks 1
41#else
42#define extra_checks 0
43#endif
44
45/* gpio_lock prevents conflicts during gpio_desc[] table updates.
46 * While any GPIO is requested, its gpio_chip is not removable;
47 * each GPIO's "requested" flag serves as a lock and refcount.
48 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090049DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080050
Alexandre Courbotbae48da2013-10-17 10:21:38 -070051static DEFINE_MUTEX(gpio_lookup_lock);
52static LIST_HEAD(gpio_lookup_list);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090053LIST_HEAD(gpio_chips);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +020054
Johan Hovold6d867502015-05-04 17:23:25 +020055
56static void gpiochip_free_hogs(struct gpio_chip *chip);
57static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
58
59
David Brownelld2876d02008-02-04 22:28:20 -080060static inline void desc_set_label(struct gpio_desc *d, const char *label)
61{
David Brownelld2876d02008-02-04 22:28:20 -080062 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080063}
64
Alexandre Courbot372e7222013-02-03 01:29:29 +090065/**
66 * Convert a GPIO number to its descriptor
67 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070068struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090069{
Alexandre Courbot14e85c02014-11-19 16:51:27 +090070 struct gpio_chip *chip;
71 unsigned long flags;
72
73 spin_lock_irqsave(&gpio_lock, flags);
74
75 list_for_each_entry(chip, &gpio_chips, list) {
76 if (chip->base <= gpio && chip->base + chip->ngpio > gpio) {
77 spin_unlock_irqrestore(&gpio_lock, flags);
78 return &chip->desc[gpio - chip->base];
79 }
80 }
81
82 spin_unlock_irqrestore(&gpio_lock, flags);
83
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +090084 if (!gpio_is_valid(gpio))
85 WARN(1, "invalid GPIO %d\n", gpio);
86
Alexandre Courbot14e85c02014-11-19 16:51:27 +090087 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +090088}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070089EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090090
91/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090092 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +020093 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090094struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
95 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +020096{
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090097 if (hwnum >= chip->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +090098 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +020099
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900100 return &chip->desc[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200101}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900102
103/**
104 * Convert a GPIO descriptor to the integer namespace.
105 * This should disappear in the future but is needed since we still
106 * use GPIO numbers for error messages and sysfs nodes
107 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700108int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900109{
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900110 return desc->chip->base + (desc - &desc->chip->desc[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900111}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700112EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900113
114
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700115/**
116 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
117 * @desc: descriptor to return the chip of
118 */
119struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900120{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900121 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900122}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700123EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800124
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700125/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
126static int gpiochip_find_base(int ngpio)
127{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900128 struct gpio_chip *chip;
129 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700130
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900131 list_for_each_entry_reverse(chip, &gpio_chips, list) {
132 /* found a free space? */
133 if (chip->base + chip->ngpio <= base)
134 break;
135 else
136 /* nope, check the space right before the chip */
137 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700138 }
139
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900140 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700141 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900142 return base;
143 } else {
144 pr_err("%s: cannot find free range\n", __func__);
145 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700146 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700147}
148
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700149/**
150 * gpiod_get_direction - return the current direction of a GPIO
151 * @desc: GPIO to get the direction of
152 *
153 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
154 *
155 * This function may sleep if gpiod_cansleep() is true.
156 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900157int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300158{
159 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900160 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300161 int status = -EINVAL;
162
Alexandre Courbot372e7222013-02-03 01:29:29 +0900163 chip = gpiod_to_chip(desc);
164 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300165
166 if (!chip->get_direction)
167 return status;
168
Alexandre Courbot372e7222013-02-03 01:29:29 +0900169 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300170 if (status > 0) {
171 /* GPIOF_DIR_IN, or other positive */
172 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900173 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300174 }
175 if (status == 0) {
176 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900177 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300178 }
179 return status;
180}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700181EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300182
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900183/*
184 * Add a new chip to the global chips list, keeping the list of chips sorted
185 * by base order.
186 *
187 * Return -EBUSY if the new chip overlaps with some other chip's integer
188 * space.
189 */
190static int gpiochip_add_to_list(struct gpio_chip *chip)
191{
Masahiro Yamadad1aceb82015-07-21 14:45:40 +0900192 struct list_head *pos;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900193 struct gpio_chip *_chip;
194 int err = 0;
195
196 /* find where to insert our chip */
197 list_for_each(pos, &gpio_chips) {
198 _chip = list_entry(pos, struct gpio_chip, list);
199 /* shall we insert before _chip? */
200 if (_chip->base >= chip->base + chip->ngpio)
201 break;
202 }
203
204 /* are we stepping on the chip right before? */
205 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
206 _chip = list_entry(pos->prev, struct gpio_chip, list);
207 if (_chip->base + _chip->ngpio > chip->base) {
208 dev_err(chip->dev,
209 "GPIO integer space overlap, cannot add chip\n");
210 err = -EBUSY;
211 }
212 }
213
214 if (!err)
215 list_add_tail(&chip->list, pos);
216
217 return err;
218}
219
Linus Walleijf881bab2015-09-23 16:20:43 -0700220/**
221 * Convert a GPIO name to its descriptor
222 */
223static struct gpio_desc *gpio_name_to_desc(const char * const name)
224{
225 struct gpio_chip *chip;
226 unsigned long flags;
227
228 spin_lock_irqsave(&gpio_lock, flags);
229
230 list_for_each_entry(chip, &gpio_chips, list) {
231 int i;
232
233 for (i = 0; i != chip->ngpio; ++i) {
234 struct gpio_desc *gpio = &chip->desc[i];
235
236 if (!gpio->name)
237 continue;
238
239 if (!strcmp(gpio->name, name)) {
240 spin_unlock_irqrestore(&gpio_lock, flags);
241 return gpio;
242 }
243 }
244 }
245
246 spin_unlock_irqrestore(&gpio_lock, flags);
247
248 return NULL;
249}
250
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200251/*
252 * Takes the names from gc->names and checks if they are all unique. If they
253 * are, they are assigned to their gpio descriptors.
254 *
255 * Returns -EEXIST if one of the names is already used for a different GPIO.
256 */
257static int gpiochip_set_desc_names(struct gpio_chip *gc)
258{
259 int i;
260
261 if (!gc->names)
262 return 0;
263
264 /* First check all names if they are unique */
265 for (i = 0; i != gc->ngpio; ++i) {
266 struct gpio_desc *gpio;
267
268 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700269 if (gpio)
270 dev_warn(gc->dev, "Detected name collision for "
271 "GPIO name '%s'\n",
272 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200273 }
274
275 /* Then add all names to the GPIO descriptors */
276 for (i = 0; i != gc->ngpio; ++i)
277 gc->desc[i].name = gc->names[i];
278
279 return 0;
280}
281
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700282/**
David Brownelld2876d02008-02-04 22:28:20 -0800283 * gpiochip_add() - register a gpio_chip
284 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900285 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -0800286 *
287 * Returns a negative errno if the chip can't be registered, such as
288 * because the chip->base is invalid or already associated with a
289 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700290 *
David Brownelld8f388d2008-07-25 01:46:07 -0700291 * When gpiochip_add() is called very early during boot, so that GPIOs
292 * can be freely used, the chip->dev device must be registered before
293 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
294 * for GPIOs will fail rudely.
295 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700296 * If chip->base is negative, this requests dynamic assignment of
297 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800298 */
299int gpiochip_add(struct gpio_chip *chip)
300{
301 unsigned long flags;
302 int status = 0;
303 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700304 int base = chip->base;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900305 struct gpio_desc *descs;
David Brownelld2876d02008-02-04 22:28:20 -0800306
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900307 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
308 if (!descs)
309 return -ENOMEM;
David Brownelld2876d02008-02-04 22:28:20 -0800310
311 spin_lock_irqsave(&gpio_lock, flags);
312
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700313 if (base < 0) {
314 base = gpiochip_find_base(chip->ngpio);
315 if (base < 0) {
316 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100317 spin_unlock_irqrestore(&gpio_lock, flags);
318 goto err_free_descs;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700319 }
320 chip->base = base;
321 }
322
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900323 status = gpiochip_add_to_list(chip);
Johan Hovold05aa5202015-01-12 17:12:26 +0100324 if (status) {
325 spin_unlock_irqrestore(&gpio_lock, flags);
326 goto err_free_descs;
327 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900328
Johan Hovold05aa5202015-01-12 17:12:26 +0100329 for (id = 0; id < chip->ngpio; id++) {
330 struct gpio_desc *desc = &descs[id];
David Brownelld8f388d2008-07-25 01:46:07 -0700331
Johan Hovold05aa5202015-01-12 17:12:26 +0100332 desc->chip = chip;
333
334 /* REVISIT: most hardware initializes GPIOs as inputs (often
335 * with pullups enabled) so power usage is minimized. Linux
336 * code should set the gpio direction first thing; but until
337 * it does, and in case chip->get_direction is not set, we may
338 * expose the wrong direction in sysfs.
339 */
340 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800341 }
342
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900343 chip->desc = descs;
344
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800345 spin_unlock_irqrestore(&gpio_lock, flags);
346
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530347#ifdef CONFIG_PINCTRL
348 INIT_LIST_HEAD(&chip->pin_ranges);
349#endif
350
Grygorii Strashko37269602015-06-25 20:30:51 +0300351 if (!chip->owner && chip->dev && chip->dev->driver)
352 chip->owner = chip->dev->driver->owner;
353
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200354 status = gpiochip_set_desc_names(chip);
355 if (status)
356 goto err_remove_from_list;
357
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200358 status = of_gpiochip_add(chip);
359 if (status)
360 goto err_remove_chip;
361
Mika Westerberg664e3e52014-01-08 12:40:54 +0200362 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600363
Johan Hovold426577b2015-05-04 17:10:32 +0200364 status = gpiochip_sysfs_register(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100365 if (status)
366 goto err_remove_chip;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600367
Andy Shevchenko7589e592013-12-05 11:26:23 +0200368 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700369 chip->base, chip->base + chip->ngpio - 1,
370 chip->label ? : "generic");
371
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600372 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800373
Johan Hovold225fce82015-01-12 17:12:25 +0100374err_remove_chip:
375 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200376 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100377 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200378err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100379 spin_lock_irqsave(&gpio_lock, flags);
380 list_del(&chip->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800381 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold05aa5202015-01-12 17:12:26 +0100382 chip->desc = NULL;
Johan Hovold225fce82015-01-12 17:12:25 +0100383err_free_descs:
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900384 kfree(descs);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900385
David Brownelld2876d02008-02-04 22:28:20 -0800386 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200387 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600388 chip->base, chip->base + chip->ngpio - 1,
389 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800390 return status;
391}
392EXPORT_SYMBOL_GPL(gpiochip_add);
393
394/**
395 * gpiochip_remove() - unregister a gpio_chip
396 * @chip: the chip to unregister
397 *
398 * A gpio_chip with any GPIOs still requested may not be removed.
399 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200400void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800401{
Johan Hovoldfab28b82015-05-04 17:10:27 +0200402 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800403 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800404 unsigned id;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200405 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800406
Johan Hovold426577b2015-05-04 17:10:32 +0200407 gpiochip_sysfs_unregister(chip);
Johan Hovold01cca932015-01-12 17:12:29 +0100408
Johan Hovold00acc3d2015-01-12 17:12:27 +0100409 gpiochip_irqchip_remove(chip);
410
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200411 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100412 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600413 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600414 of_gpiochip_remove(chip);
415
Johan Hovold6798aca2015-01-12 17:12:28 +0100416 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900417 for (id = 0; id < chip->ngpio; id++) {
Johan Hovoldfab28b82015-05-04 17:10:27 +0200418 desc = &chip->desc[id];
419 desc->chip = NULL;
420 if (test_bit(FLAG_REQUESTED, &desc->flags))
421 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800422 }
abdoulaye berthee1db1702014-07-05 18:28:50 +0200423 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800424 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900425
Johan Hovoldfab28b82015-05-04 17:10:27 +0200426 if (requested)
427 dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
428
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900429 kfree(chip->desc);
430 chip->desc = NULL;
David Brownelld2876d02008-02-04 22:28:20 -0800431}
432EXPORT_SYMBOL_GPL(gpiochip_remove);
433
Grant Likely594fa262010-06-08 07:48:16 -0600434/**
435 * gpiochip_find() - iterator for locating a specific gpio_chip
436 * @data: data to pass to match function
437 * @callback: Callback function to check gpio_chip
438 *
439 * Similar to bus_find_device. It returns a reference to a gpio_chip as
440 * determined by a user supplied @match callback. The callback should return
441 * 0 if the device doesn't match and non-zero if it does. If the callback is
442 * non-zero, this function will return to the caller and not iterate over any
443 * more gpio_chips.
444 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600445struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700446 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600447 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600448{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900449 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600450 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600451
452 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900453 list_for_each_entry(chip, &gpio_chips, list)
454 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600455 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900456
457 /* No match? */
458 if (&chip->list == &gpio_chips)
459 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600460 spin_unlock_irqrestore(&gpio_lock, flags);
461
462 return chip;
463}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600464EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800465
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900466static int gpiochip_match_name(struct gpio_chip *chip, void *data)
467{
468 const char *name = data;
469
470 return !strcmp(chip->label, name);
471}
472
473static struct gpio_chip *find_chip_by_name(const char *name)
474{
475 return gpiochip_find((void *)name, gpiochip_match_name);
476}
477
Linus Walleij14250522014-03-25 10:40:18 +0100478#ifdef CONFIG_GPIOLIB_IRQCHIP
479
480/*
481 * The following is irqchip helper code for gpiochips.
482 */
483
484/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200485 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
486 * @gpiochip: the gpiochip to set the irqchip chain to
487 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100488 * @parent_irq: the irq number corresponding to the parent IRQ for this
489 * chained irqchip
490 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200491 * coming out of the gpiochip. If the interrupt is nested rather than
492 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100493 */
494void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
495 struct irq_chip *irqchip,
496 int parent_irq,
497 irq_flow_handler_t parent_handler)
498{
Linus Walleij83141a72014-09-26 13:50:12 +0200499 unsigned int offset;
500
Linus Walleij83141a72014-09-26 13:50:12 +0200501 if (!gpiochip->irqdomain) {
502 chip_err(gpiochip, "called %s before setting up irqchip\n",
503 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200504 return;
505 }
506
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200507 if (parent_handler) {
508 if (gpiochip->can_sleep) {
509 chip_err(gpiochip,
510 "you cannot have chained interrupts on a "
511 "chip that may sleep\n");
512 return;
513 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200514 /*
515 * The parent irqchip is already using the chip_data for this
516 * irqchip, so our callbacks simply use the handler_data.
517 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +0200518 irq_set_chained_handler_and_data(parent_irq, parent_handler,
519 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300520
521 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200522 }
Linus Walleij83141a72014-09-26 13:50:12 +0200523
524 /* Set the parent IRQ for all affected IRQs */
525 for (offset = 0; offset < gpiochip->ngpio; offset++)
526 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
527 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100528}
529EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
530
531/**
532 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
533 * @d: the irqdomain used by this irqchip
534 * @irq: the global irq number used by this GPIO irqchip irq
535 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
536 *
537 * This function will set up the mapping for a certain IRQ line on a
538 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
539 * stored inside the gpiochip.
540 */
541static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
542 irq_hw_number_t hwirq)
543{
544 struct gpio_chip *chip = d->host_data;
545
Linus Walleij14250522014-03-25 10:40:18 +0100546 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300547 /*
548 * This lock class tells lockdep that GPIO irqs are in a different
549 * category than their parents, so it won't report false recursion.
550 */
551 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +0200552 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200553 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300554 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200555 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100556 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -0500557
Linus Walleij1333b902014-04-23 16:45:12 +0200558 /*
559 * No set-up of the hardware will happen if IRQ_TYPE_NONE
560 * is passed as default type.
561 */
562 if (chip->irq_default_type != IRQ_TYPE_NONE)
563 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100564
565 return 0;
566}
567
Linus Walleijc3626fd2014-03-28 20:42:01 +0100568static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
569{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200570 struct gpio_chip *chip = d->host_data;
571
Linus Walleij1c8732b2014-04-09 13:34:39 +0200572 if (chip->can_sleep)
573 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100574 irq_set_chip_and_handler(irq, NULL, NULL);
575 irq_set_chip_data(irq, NULL);
576}
577
Linus Walleij14250522014-03-25 10:40:18 +0100578static const struct irq_domain_ops gpiochip_domain_ops = {
579 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100580 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100581 /* Virtually all GPIO irqchips are twocell:ed */
582 .xlate = irq_domain_xlate_twocell,
583};
584
585static int gpiochip_irq_reqres(struct irq_data *d)
586{
587 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
588
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300589 if (!try_module_get(chip->owner))
590 return -ENODEV;
591
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900592 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100593 chip_err(chip,
594 "unable to lock HW IRQ %lu for IRQ\n",
595 d->hwirq);
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300596 module_put(chip->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100597 return -EINVAL;
598 }
599 return 0;
600}
601
602static void gpiochip_irq_relres(struct irq_data *d)
603{
604 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
605
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900606 gpiochip_unlock_as_irq(chip, d->hwirq);
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300607 module_put(chip->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100608}
609
610static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
611{
612 return irq_find_mapping(chip->irqdomain, offset);
613}
614
615/**
616 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
617 * @gpiochip: the gpiochip to remove the irqchip from
618 *
619 * This is called only from gpiochip_remove()
620 */
621static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
622{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100623 unsigned int offset;
624
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300625 acpi_gpiochip_free_interrupts(gpiochip);
626
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300627 if (gpiochip->irq_parent) {
628 irq_set_chained_handler(gpiochip->irq_parent, NULL);
629 irq_set_handler_data(gpiochip->irq_parent, NULL);
630 }
631
Linus Walleijc3626fd2014-03-28 20:42:01 +0100632 /* Remove all IRQ mappings and delete the domain */
633 if (gpiochip->irqdomain) {
634 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300635 irq_dispose_mapping(
636 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100637 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100638 }
Linus Walleij14250522014-03-25 10:40:18 +0100639
640 if (gpiochip->irqchip) {
641 gpiochip->irqchip->irq_request_resources = NULL;
642 gpiochip->irqchip->irq_release_resources = NULL;
643 gpiochip->irqchip = NULL;
644 }
645}
646
647/**
648 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
649 * @gpiochip: the gpiochip to add the irqchip to
650 * @irqchip: the irqchip to add to the gpiochip
651 * @first_irq: if not dynamically assigned, the base (first) IRQ to
652 * allocate gpiochip irqs from
653 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200654 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
655 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300656 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +0100657 *
658 * This function closely associates a certain irqchip with a certain
659 * gpiochip, providing an irq domain to translate the local IRQs to
660 * global irqs in the gpiolib core, and making sure that the gpiochip
661 * is passed as chip data to all related functions. Driver callbacks
662 * need to use container_of() to get their local state containers back
663 * from the gpiochip passed as chip data. An irqdomain will be stored
664 * in the gpiochip that shall be used by the driver to handle IRQ number
665 * translation. The gpiochip will need to be initialized and registered
666 * before calling this function.
667 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100668 * This function will handle two cell:ed simple IRQs and assumes all
669 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100670 * need to be open coded.
671 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300672int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
673 struct irq_chip *irqchip,
674 unsigned int first_irq,
675 irq_flow_handler_t handler,
676 unsigned int type,
677 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +0100678{
679 struct device_node *of_node;
680 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100681 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100682
683 if (!gpiochip || !irqchip)
684 return -EINVAL;
685
686 if (!gpiochip->dev) {
687 pr_err("missing gpiochip .dev parent pointer\n");
688 return -EINVAL;
689 }
690 of_node = gpiochip->dev->of_node;
691#ifdef CONFIG_OF_GPIO
692 /*
Colin Cronin20a8a962015-05-18 11:41:43 -0700693 * If the gpiochip has an assigned OF node this takes precedence
Linus Walleij14250522014-03-25 10:40:18 +0100694 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
695 */
696 if (gpiochip->of_node)
697 of_node = gpiochip->of_node;
698#endif
699 gpiochip->irqchip = irqchip;
700 gpiochip->irq_handler = handler;
701 gpiochip->irq_default_type = type;
702 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300703 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +0100704 gpiochip->irqdomain = irq_domain_add_simple(of_node,
705 gpiochip->ngpio, first_irq,
706 &gpiochip_domain_ops, gpiochip);
707 if (!gpiochip->irqdomain) {
708 gpiochip->irqchip = NULL;
709 return -EINVAL;
710 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +0200711
712 /*
713 * It is possible for a driver to override this, but only if the
714 * alternative functions are both implemented.
715 */
716 if (!irqchip->irq_request_resources &&
717 !irqchip->irq_release_resources) {
718 irqchip->irq_request_resources = gpiochip_irq_reqres;
719 irqchip->irq_release_resources = gpiochip_irq_relres;
720 }
Linus Walleij14250522014-03-25 10:40:18 +0100721
722 /*
723 * Prepare the mapping since the irqchip shall be orthogonal to
724 * any gpiochip calls. If the first_irq was zero, this is
725 * necessary to allocate descriptors for all IRQs.
726 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100727 for (offset = 0; offset < gpiochip->ngpio; offset++) {
728 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
729 if (offset == 0)
730 /*
731 * Store the base into the gpiochip to be used when
732 * unmapping the irqs.
733 */
734 gpiochip->irq_base = irq_base;
735 }
Linus Walleij14250522014-03-25 10:40:18 +0100736
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300737 acpi_gpiochip_request_interrupts(gpiochip);
738
Linus Walleij14250522014-03-25 10:40:18 +0100739 return 0;
740}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300741EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +0100742
743#else /* CONFIG_GPIOLIB_IRQCHIP */
744
745static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
746
747#endif /* CONFIG_GPIOLIB_IRQCHIP */
748
Jonas Gorskic771c2f2015-10-11 17:34:15 +0200749/**
750 * gpiochip_generic_request() - request the gpio function for a pin
751 * @chip: the gpiochip owning the GPIO
752 * @offset: the offset of the GPIO to request for GPIO function
753 */
754int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
755{
756 return pinctrl_request_gpio(chip->base + offset);
757}
758EXPORT_SYMBOL_GPL(gpiochip_generic_request);
759
760/**
761 * gpiochip_generic_free() - free the gpio function from a pin
762 * @chip: the gpiochip to request the gpio function for
763 * @offset: the offset of the GPIO to free from GPIO function
764 */
765void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
766{
767 pinctrl_free_gpio(chip->base + offset);
768}
769EXPORT_SYMBOL_GPL(gpiochip_generic_free);
770
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530771#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100772
Linus Walleij3f0f8672012-11-20 12:40:15 +0100773/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200774 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
775 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +0200776 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +0200777 * @gpio_offset: the start offset in the current gpio_chip number space
778 * @pin_group: name of the pin group inside the pin controller
779 */
780int gpiochip_add_pingroup_range(struct gpio_chip *chip,
781 struct pinctrl_dev *pctldev,
782 unsigned int gpio_offset, const char *pin_group)
783{
784 struct gpio_pin_range *pin_range;
785 int ret;
786
787 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
788 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200789 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200790 return -ENOMEM;
791 }
792
793 /* Use local offset as range ID */
794 pin_range->range.id = gpio_offset;
795 pin_range->range.gc = chip;
796 pin_range->range.name = chip->label;
797 pin_range->range.base = chip->base + gpio_offset;
798 pin_range->pctldev = pctldev;
799
800 ret = pinctrl_get_group_pins(pctldev, pin_group,
801 &pin_range->range.pins,
802 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100803 if (ret < 0) {
804 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200805 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100806 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200807
808 pinctrl_add_gpio_range(pctldev, &pin_range->range);
809
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200810 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
811 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200812 pinctrl_dev_get_devname(pctldev), pin_group);
813
814 list_add_tail(&pin_range->node, &chip->pin_ranges);
815
816 return 0;
817}
818EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
819
820/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100821 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
822 * @chip: the gpiochip to add the range for
823 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100824 * @gpio_offset: the start offset in the current gpio_chip number space
825 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100826 * @npins: the number of pins from the offset of each pin space (GPIO and
827 * pin controller) to accumulate in this range
828 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100829int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100830 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100831 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530832{
833 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800834 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530835
Linus Walleij3f0f8672012-11-20 12:40:15 +0100836 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530837 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200838 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100839 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530840 }
841
Linus Walleij3f0f8672012-11-20 12:40:15 +0100842 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100843 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100844 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530845 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100846 pin_range->range.base = chip->base + gpio_offset;
847 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530848 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100849 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530850 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100851 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800852 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200853 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100854 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800855 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100856 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200857 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
858 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100859 pinctl_name,
860 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530861
862 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100863
864 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530865}
Linus Walleij165adc92012-11-06 14:49:39 +0100866EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530867
Linus Walleij3f0f8672012-11-20 12:40:15 +0100868/**
869 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
870 * @chip: the chip to remove all the mappings for
871 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530872void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
873{
874 struct gpio_pin_range *pin_range, *tmp;
875
876 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
877 list_del(&pin_range->node);
878 pinctrl_remove_gpio_range(pin_range->pctldev,
879 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100880 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530881 }
882}
Linus Walleij165adc92012-11-06 14:49:39 +0100883EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
884
885#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530886
David Brownelld2876d02008-02-04 22:28:20 -0800887/* These "optional" allocation calls help prevent drivers from stomping
888 * on each other, and help provide better diagnostics in debugfs.
889 * They're called even less than the "set direction" calls.
890 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200891static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800892{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200893 struct gpio_chip *chip = desc->chip;
894 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800895 unsigned long flags;
896
897 spin_lock_irqsave(&gpio_lock, flags);
898
David Brownelld2876d02008-02-04 22:28:20 -0800899 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700900 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800901 */
902
903 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
904 desc_set_label(desc, label ? : "?");
905 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700906 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800907 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800908 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700909 }
910
911 if (chip->request) {
912 /* chip->request may sleep */
913 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900914 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700915 spin_lock_irqsave(&gpio_lock, flags);
916
917 if (status < 0) {
918 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700919 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300920 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700921 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700922 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300923 if (chip->get_direction) {
924 /* chip->get_direction may sleep */
925 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900926 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300927 spin_lock_irqsave(&gpio_lock, flags);
928 }
David Brownelld2876d02008-02-04 22:28:20 -0800929done:
Mika Westerberg77c2d792014-03-10 14:54:50 +0200930 spin_unlock_irqrestore(&gpio_lock, flags);
931 return status;
932}
933
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900934int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200935{
936 int status = -EPROBE_DEFER;
937 struct gpio_chip *chip;
938
939 if (!desc) {
940 pr_warn("%s: invalid GPIO\n", __func__);
941 return -EINVAL;
942 }
943
944 chip = desc->chip;
945 if (!chip)
946 goto done;
947
948 if (try_module_get(chip->owner)) {
949 status = __gpiod_request(desc, label);
950 if (status < 0)
951 module_put(chip->owner);
952 }
953
954done:
David Brownelld2876d02008-02-04 22:28:20 -0800955 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200956 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200957
David Brownelld2876d02008-02-04 22:28:20 -0800958 return status;
959}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900960
Mika Westerberg77c2d792014-03-10 14:54:50 +0200961static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800962{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200963 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -0800964 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -0700965 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800966
Uwe Kleine-König3d599d12008-10-15 22:03:12 -0700967 might_sleep();
968
Alexandre Courbot372e7222013-02-03 01:29:29 +0900969 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700970
David Brownelld2876d02008-02-04 22:28:20 -0800971 spin_lock_irqsave(&gpio_lock, flags);
972
David Brownell35e8bb52008-10-15 22:03:16 -0700973 chip = desc->chip;
974 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
975 if (chip->free) {
976 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -0700977 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900978 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700979 spin_lock_irqsave(&gpio_lock, flags);
980 }
David Brownelld2876d02008-02-04 22:28:20 -0800981 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -0800982 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -0700983 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530984 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +0530985 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -0600986 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200987 ret = true;
988 }
David Brownelld2876d02008-02-04 22:28:20 -0800989
990 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200991 return ret;
992}
993
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900994void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200995{
996 if (desc && __gpiod_free(desc))
997 module_put(desc->chip->owner);
998 else
999 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -08001000}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001001
David Brownelld2876d02008-02-04 22:28:20 -08001002/**
1003 * gpiochip_is_requested - return string iff signal was requested
1004 * @chip: controller managing the signal
1005 * @offset: of signal within controller's 0..(ngpio - 1) range
1006 *
1007 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001008 * The string returned is the label passed to gpio_request(); if none has been
1009 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001010 *
1011 * This function is for use by GPIO controller drivers. The label can
1012 * help with diagnostics, and knowing that the signal is used as a GPIO
1013 * can help avoid accidentally multiplexing it to another controller.
1014 */
1015const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1016{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001017 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001018
Dirk Behme48b59532015-08-18 18:02:32 +02001019 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001020 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001021
1022 desc = &chip->desc[offset];
1023
Alexandre Courbot372e7222013-02-03 01:29:29 +09001024 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001025 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001026 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001027}
1028EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1029
Mika Westerberg77c2d792014-03-10 14:54:50 +02001030/**
1031 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1032 * @desc: GPIO descriptor to request
1033 * @label: label for the GPIO
1034 *
1035 * Function allows GPIO chip drivers to request and use their own GPIO
1036 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1037 * function will not increase reference count of the GPIO chip module. This
1038 * allows the GPIO chip module to be unloaded as needed (we assume that the
1039 * GPIO chip driver handles freeing the GPIOs it has requested).
1040 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001041struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1042 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001043{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001044 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1045 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001046
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001047 if (IS_ERR(desc)) {
1048 chip_err(chip, "failed to get GPIO descriptor\n");
1049 return desc;
1050 }
1051
1052 err = __gpiod_request(desc, label);
1053 if (err < 0)
1054 return ERR_PTR(err);
1055
1056 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001057}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001058EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001059
1060/**
1061 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1062 * @desc: GPIO descriptor to free
1063 *
1064 * Function frees the given GPIO requested previously with
1065 * gpiochip_request_own_desc().
1066 */
1067void gpiochip_free_own_desc(struct gpio_desc *desc)
1068{
1069 if (desc)
1070 __gpiod_free(desc);
1071}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001072EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08001073
1074/* Drivers MUST set GPIO direction before making get/set calls. In
1075 * some cases this is done in early boot, before IRQs are enabled.
1076 *
1077 * As a rule these aren't called more than once (except for drivers
1078 * using the open-drain emulation idiom) so these are natural places
1079 * to accumulate extra debugging checks. Note that we can't (yet)
1080 * rely on gpio_request() having been called beforehand.
1081 */
1082
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001083/**
1084 * gpiod_direction_input - set the GPIO direction to input
1085 * @desc: GPIO to set to input
1086 *
1087 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1088 * be called safely on it.
1089 *
1090 * Return 0 in case of success, else an error code.
1091 */
1092int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001093{
David Brownelld2876d02008-02-04 22:28:20 -08001094 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001095 int status = -EINVAL;
1096
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
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001102 chip = desc->chip;
1103 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001104 gpiod_warn(desc,
1105 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001106 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001107 return -EIO;
1108 }
1109
Alexandre Courbotd82da792014-07-22 16:17:43 +09001110 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001111 if (status == 0)
1112 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001113
Alexandre Courbot372e7222013-02-03 01:29:29 +09001114 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001115
David Brownelld2876d02008-02-04 22:28:20 -08001116 return status;
1117}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001118EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001119
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001120static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001121{
David Brownelld2876d02008-02-04 22:28:20 -08001122 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001123 int status = -EINVAL;
1124
Linus Walleijd468bf92013-09-24 11:54:38 +02001125 /* GPIOs used for IRQs shall not be set as output */
1126 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1127 gpiod_err(desc,
1128 "%s: tried to set a GPIO tied to an IRQ as output\n",
1129 __func__);
1130 return -EIO;
1131 }
1132
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301133 /* Open drain pin should not be driven to 1 */
1134 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001135 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301136
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301137 /* Open source pin should not be driven to 0 */
1138 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001139 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301140
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001141 chip = desc->chip;
1142 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001143 gpiod_warn(desc,
1144 "%s: missing set() or direction_output() operations\n",
1145 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001146 return -EIO;
1147 }
1148
Alexandre Courbotd82da792014-07-22 16:17:43 +09001149 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001150 if (status == 0)
1151 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001152 trace_gpio_value(desc_to_gpio(desc), 0, value);
1153 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001154 return status;
1155}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001156
1157/**
1158 * gpiod_direction_output_raw - set the GPIO direction to output
1159 * @desc: GPIO to set to output
1160 * @value: initial output value of the GPIO
1161 *
1162 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1163 * be called safely on it. The initial value of the output must be specified
1164 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1165 *
1166 * Return 0 in case of success, else an error code.
1167 */
1168int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1169{
1170 if (!desc || !desc->chip) {
1171 pr_warn("%s: invalid GPIO\n", __func__);
1172 return -EINVAL;
1173 }
1174 return _gpiod_direction_output_raw(desc, value);
1175}
1176EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1177
1178/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301179 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001180 * @desc: GPIO to set to output
1181 * @value: initial output value of the GPIO
1182 *
1183 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1184 * be called safely on it. The initial value of the output must be specified
1185 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1186 * account.
1187 *
1188 * Return 0 in case of success, else an error code.
1189 */
1190int gpiod_direction_output(struct gpio_desc *desc, int value)
1191{
1192 if (!desc || !desc->chip) {
1193 pr_warn("%s: invalid GPIO\n", __func__);
1194 return -EINVAL;
1195 }
1196 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1197 value = !value;
1198 return _gpiod_direction_output_raw(desc, value);
1199}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001200EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001201
Felipe Balbic4b5be92010-05-26 14:42:23 -07001202/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001203 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001204 * @gpio: the gpio to set debounce time
1205 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001206 *
1207 * returns -ENOTSUPP if the controller does not support setting
1208 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001209 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001210int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001211{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001212 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001213
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001214 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001215 pr_warn("%s: invalid GPIO\n", __func__);
1216 return -EINVAL;
1217 }
1218
Felipe Balbic4b5be92010-05-26 14:42:23 -07001219 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001220 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001221 gpiod_dbg(desc,
1222 "%s: missing set() or set_debounce() operations\n",
1223 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001224 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001225 }
1226
Alexandre Courbotd82da792014-07-22 16:17:43 +09001227 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001228}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001229EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001230
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001231/**
1232 * gpiod_is_active_low - test whether a GPIO is active-low or not
1233 * @desc: the gpio descriptor to test
1234 *
1235 * Returns 1 if the GPIO is active-low, 0 otherwise.
1236 */
1237int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001238{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001239 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001240}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001241EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001242
1243/* I/O calls are only valid after configuration completed; the relevant
1244 * "is this a valid GPIO" error checks should already have been done.
1245 *
1246 * "Get" operations are often inlinable as reading a pin value register,
1247 * and masking the relevant bit in that register.
1248 *
1249 * When "set" operations are inlinable, they involve writing that mask to
1250 * one register to set a low value, or a different register to set it high.
1251 * Otherwise locking is needed, so there may be little value to inlining.
1252 *
1253 *------------------------------------------------------------------------
1254 *
1255 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1256 * have requested the GPIO. That can include implicit requesting by
1257 * a direction setting call. Marking a gpio as requested locks its chip
1258 * in memory, guaranteeing that these table lookups need no more locking
1259 * and that gpiochip_remove() will fail.
1260 *
1261 * REVISIT when debugging, consider adding some instrumentation to ensure
1262 * that the GPIO was actually requested.
1263 */
1264
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001265static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001266{
1267 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001268 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001269 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001270
Alexandre Courbot372e7222013-02-03 01:29:29 +09001271 chip = desc->chip;
1272 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001273 value = chip->get ? chip->get(chip, offset) : -EIO;
1274 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001275 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001276 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001277}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001278
David Brownelld2876d02008-02-04 22:28:20 -08001279/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001280 * gpiod_get_raw_value() - return a gpio's raw value
1281 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001282 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001283 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001284 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001285 *
1286 * This function should be called from contexts where we cannot sleep, and will
1287 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001288 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001289int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001290{
David Brownelld2876d02008-02-04 22:28:20 -08001291 if (!desc)
1292 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001293 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001294 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001295 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001296}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001297EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001298
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001299/**
1300 * gpiod_get_value() - return a gpio's value
1301 * @desc: gpio whose value will be returned
1302 *
1303 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001304 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001305 *
1306 * This function should be called from contexts where we cannot sleep, and will
1307 * complain if the GPIO chip functions potentially sleep.
1308 */
1309int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001310{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001311 int value;
1312 if (!desc)
1313 return 0;
1314 /* Should be using gpio_get_value_cansleep() */
1315 WARN_ON(desc->chip->can_sleep);
1316
1317 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001318 if (value < 0)
1319 return value;
1320
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001321 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1322 value = !value;
1323
1324 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001325}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001326EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001327
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301328/*
1329 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001330 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001331 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301332 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001333static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301334{
1335 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001336 struct gpio_chip *chip = desc->chip;
1337 int offset = gpio_chip_hwgpio(desc);
1338
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301339 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001340 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301341 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001342 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301343 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001344 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301345 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001346 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301347 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001348 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301349 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001350 gpiod_err(desc,
1351 "%s: Error in set_value for open drain err %d\n",
1352 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301353}
1354
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301355/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001356 * _gpio_set_open_source_value() - Set the open source gpio's value.
1357 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001358 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301359 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001360static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301361{
1362 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001363 struct gpio_chip *chip = desc->chip;
1364 int offset = gpio_chip_hwgpio(desc);
1365
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301366 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001367 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301368 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001369 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301370 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001371 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301372 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001373 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301374 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001375 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301376 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001377 gpiod_err(desc,
1378 "%s: Error in set_value for open source err %d\n",
1379 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301380}
1381
Alexandre Courbot23600962014-03-11 15:52:09 +09001382static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001383{
1384 struct gpio_chip *chip;
1385
Alexandre Courbot372e7222013-02-03 01:29:29 +09001386 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001387 trace_gpio_value(desc_to_gpio(desc), 0, value);
1388 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1389 _gpio_set_open_drain_value(desc, value);
1390 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1391 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301392 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001393 chip->set(chip, gpio_chip_hwgpio(desc), value);
1394}
1395
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001396/*
1397 * set multiple outputs on the same chip;
1398 * use the chip's set_multiple function if available;
1399 * otherwise set the outputs sequentially;
1400 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1401 * defines which outputs are to be changed
1402 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1403 * defines the values the outputs specified by mask are to be set to
1404 */
1405static void gpio_chip_set_multiple(struct gpio_chip *chip,
1406 unsigned long *mask, unsigned long *bits)
1407{
1408 if (chip->set_multiple) {
1409 chip->set_multiple(chip, mask, bits);
1410 } else {
1411 int i;
1412 for (i = 0; i < chip->ngpio; i++) {
1413 if (mask[BIT_WORD(i)] == 0) {
1414 /* no more set bits in this mask word;
1415 * skip ahead to the next word */
1416 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1417 continue;
1418 }
1419 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001420 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001421 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001422 }
1423 }
1424}
1425
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001426static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1427 unsigned int array_size,
1428 struct gpio_desc **desc_array,
1429 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001430{
1431 int i = 0;
1432
1433 while (i < array_size) {
1434 struct gpio_chip *chip = desc_array[i]->chip;
1435 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1436 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1437 int count = 0;
1438
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001439 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001440 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001441
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001442 memset(mask, 0, sizeof(mask));
1443 do {
1444 struct gpio_desc *desc = desc_array[i];
1445 int hwgpio = gpio_chip_hwgpio(desc);
1446 int value = value_array[i];
1447
1448 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1449 value = !value;
1450 trace_gpio_value(desc_to_gpio(desc), 0, value);
1451 /*
1452 * collect all normal outputs belonging to the same chip
1453 * open drain and open source outputs are set individually
1454 */
1455 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001456 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001457 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1458 _gpio_set_open_source_value(desc, value);
1459 } else {
1460 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001461 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001462 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001463 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001464 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001465 count++;
1466 }
1467 i++;
1468 } while ((i < array_size) && (desc_array[i]->chip == chip));
1469 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001470 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001471 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001472 }
1473}
1474
David Brownelld2876d02008-02-04 22:28:20 -08001475/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001476 * gpiod_set_raw_value() - assign a gpio's raw value
1477 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001478 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001479 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001480 * Set the raw value of the GPIO, i.e. the value of its physical line without
1481 * regard for its ACTIVE_LOW status.
1482 *
1483 * This function should be called from contexts where we cannot sleep, and will
1484 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001485 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001486void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001487{
David Brownelld2876d02008-02-04 22:28:20 -08001488 if (!desc)
1489 return;
David Brownelld2876d02008-02-04 22:28:20 -08001490 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001491 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001492 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001493}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001494EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001495
1496/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001497 * gpiod_set_value() - assign a gpio's value
1498 * @desc: gpio whose value will be assigned
1499 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001500 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001501 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1502 * account
1503 *
1504 * This function should be called from contexts where we cannot sleep, and will
1505 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001506 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001507void gpiod_set_value(struct gpio_desc *desc, int value)
1508{
1509 if (!desc)
1510 return;
1511 /* Should be using gpio_set_value_cansleep() */
1512 WARN_ON(desc->chip->can_sleep);
1513 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1514 value = !value;
1515 _gpiod_set_raw_value(desc, value);
1516}
1517EXPORT_SYMBOL_GPL(gpiod_set_value);
1518
1519/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001520 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001521 * @array_size: number of elements in the descriptor / value arrays
1522 * @desc_array: array of GPIO descriptors whose values will be assigned
1523 * @value_array: array of values to assign
1524 *
1525 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1526 * without regard for their ACTIVE_LOW status.
1527 *
1528 * This function should be called from contexts where we cannot sleep, and will
1529 * complain if the GPIO chip functions potentially sleep.
1530 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001531void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001532 struct gpio_desc **desc_array, int *value_array)
1533{
1534 if (!desc_array)
1535 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001536 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1537 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001538}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001539EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001540
1541/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001542 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001543 * @array_size: number of elements in the descriptor / value arrays
1544 * @desc_array: array of GPIO descriptors whose values will be assigned
1545 * @value_array: array of values to assign
1546 *
1547 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1548 * into account.
1549 *
1550 * This function should be called from contexts where we cannot sleep, and will
1551 * complain if the GPIO chip functions potentially sleep.
1552 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001553void gpiod_set_array_value(unsigned int array_size,
1554 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001555{
1556 if (!desc_array)
1557 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001558 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1559 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001560}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001561EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001562
1563/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001564 * gpiod_cansleep() - report whether gpio value access may sleep
1565 * @desc: gpio to check
1566 *
1567 */
1568int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001569{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001570 if (!desc)
1571 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001572 return desc->chip->can_sleep;
1573}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001574EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001575
David Brownell0f6d5042008-10-15 22:03:14 -07001576/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001577 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1578 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001579 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001580 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1581 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001582 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001583int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001584{
1585 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001586 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001587
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001588 if (!desc)
1589 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001590 chip = desc->chip;
1591 offset = gpio_chip_hwgpio(desc);
1592 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1593}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001594EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001595
Linus Walleijd468bf92013-09-24 11:54:38 +02001596/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001597 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001598 * @chip: the chip the GPIO to lock belongs to
1599 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001600 *
1601 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001602 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001603 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001604int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001605{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001606 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001607 return -EINVAL;
1608
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001609 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1610 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001611 "%s: tried to flag a GPIO set as output for IRQ\n",
1612 __func__);
1613 return -EIO;
1614 }
1615
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001616 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001617 return 0;
1618}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001619EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001620
Linus Walleijd468bf92013-09-24 11:54:38 +02001621/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001622 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001623 * @chip: the chip the GPIO to lock belongs to
1624 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001625 *
1626 * This is used directly by GPIO drivers that want to indicate
1627 * that a certain GPIO is no longer used exclusively for IRQ.
1628 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001629void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001630{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001631 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001632 return;
1633
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001634 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001635}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001636EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001637
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001638/**
1639 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1640 * @desc: gpio whose value will be returned
1641 *
1642 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001643 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001644 *
1645 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001646 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001647int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001648{
David Brownelld2876d02008-02-04 22:28:20 -08001649 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001650 if (!desc)
1651 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001652 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001653}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001654EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001655
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001656/**
1657 * gpiod_get_value_cansleep() - return a gpio's value
1658 * @desc: gpio whose value will be returned
1659 *
1660 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001661 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001662 *
1663 * This function is to be called from contexts that can sleep.
1664 */
1665int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001666{
David Brownelld2876d02008-02-04 22:28:20 -08001667 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001668
1669 might_sleep_if(extra_checks);
1670 if (!desc)
1671 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001672
1673 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001674 if (value < 0)
1675 return value;
1676
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001677 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1678 value = !value;
1679
David Brownelld2876d02008-02-04 22:28:20 -08001680 return value;
1681}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001682EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001683
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001684/**
1685 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1686 * @desc: gpio whose value will be assigned
1687 * @value: value to assign
1688 *
1689 * Set the raw value of the GPIO, i.e. the value of its physical line without
1690 * regard for its ACTIVE_LOW status.
1691 *
1692 * This function is to be called from contexts that can sleep.
1693 */
1694void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001695{
David Brownelld2876d02008-02-04 22:28:20 -08001696 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001697 if (!desc)
1698 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001699 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001700}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001701EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001702
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001703/**
1704 * gpiod_set_value_cansleep() - assign a gpio's value
1705 * @desc: gpio whose value will be assigned
1706 * @value: value to assign
1707 *
1708 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1709 * account
1710 *
1711 * This function is to be called from contexts that can sleep.
1712 */
1713void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001714{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001715 might_sleep_if(extra_checks);
1716 if (!desc)
1717 return;
1718
1719 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1720 value = !value;
1721 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001722}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001723EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001724
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001725/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001726 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001727 * @array_size: number of elements in the descriptor / value arrays
1728 * @desc_array: array of GPIO descriptors whose values will be assigned
1729 * @value_array: array of values to assign
1730 *
1731 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1732 * without regard for their ACTIVE_LOW status.
1733 *
1734 * This function is to be called from contexts that can sleep.
1735 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001736void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1737 struct gpio_desc **desc_array,
1738 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001739{
1740 might_sleep_if(extra_checks);
1741 if (!desc_array)
1742 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001743 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1744 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001745}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001746EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001747
1748/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001749 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001750 * @array_size: number of elements in the descriptor / value arrays
1751 * @desc_array: array of GPIO descriptors whose values will be assigned
1752 * @value_array: array of values to assign
1753 *
1754 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1755 * into account.
1756 *
1757 * This function is to be called from contexts that can sleep.
1758 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001759void gpiod_set_array_value_cansleep(unsigned int array_size,
1760 struct gpio_desc **desc_array,
1761 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001762{
1763 might_sleep_if(extra_checks);
1764 if (!desc_array)
1765 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001766 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1767 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001768}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001769EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001770
1771/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001772 * gpiod_add_lookup_table() - register GPIO device consumers
1773 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001774 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001775void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001776{
1777 mutex_lock(&gpio_lookup_lock);
1778
Alexandre Courbotad824782013-12-03 12:20:11 +09001779 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001780
1781 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001782}
1783
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05301784/**
1785 * gpiod_remove_lookup_table() - unregister GPIO device consumers
1786 * @table: table of consumers to unregister
1787 */
1788void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
1789{
1790 mutex_lock(&gpio_lookup_lock);
1791
1792 list_del(&table->list);
1793
1794 mutex_unlock(&gpio_lookup_lock);
1795}
1796
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001797static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001798 unsigned int idx,
1799 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001800{
1801 char prop_name[32]; /* 32 is max size of property name */
1802 enum of_gpio_flags of_flags;
1803 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001804 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001805
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001806 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02001807 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01001808 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001809 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02001810 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01001811 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001812 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001813
Thierry Redingdd34c372014-04-23 17:28:09 +02001814 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1815 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001816 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001817 break;
1818 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001819
1820 if (IS_ERR(desc))
1821 return desc;
1822
1823 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001824 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001825
1826 return desc;
1827}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001828
Mika Westerberg81f59e92013-10-10 11:01:09 +03001829static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001830 unsigned int idx,
1831 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001832{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001833 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03001834 struct acpi_gpio_info info;
1835 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001836 char propname[32];
1837 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03001838
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001839 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001840 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001841 if (con_id && strcmp(con_id, "gpios")) {
1842 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001843 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001844 } else {
1845 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001846 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001847 }
Mika Westerberge01f4402013-10-10 11:01:10 +03001848
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001849 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1850 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1851 break;
1852 }
1853
1854 /* Then from plain _CRS GPIOs */
1855 if (IS_ERR(desc)) {
1856 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1857 if (IS_ERR(desc))
1858 return desc;
1859 }
1860
1861 if (info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001862 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001863
1864 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001865}
1866
Alexandre Courbotad824782013-12-03 12:20:11 +09001867static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1868{
1869 const char *dev_id = dev ? dev_name(dev) : NULL;
1870 struct gpiod_lookup_table *table;
1871
1872 mutex_lock(&gpio_lookup_lock);
1873
1874 list_for_each_entry(table, &gpio_lookup_list, list) {
1875 if (table->dev_id && dev_id) {
1876 /*
1877 * Valid strings on both ends, must be identical to have
1878 * a match
1879 */
1880 if (!strcmp(table->dev_id, dev_id))
1881 goto found;
1882 } else {
1883 /*
1884 * One of the pointers is NULL, so both must be to have
1885 * a match
1886 */
1887 if (dev_id == table->dev_id)
1888 goto found;
1889 }
1890 }
1891 table = NULL;
1892
1893found:
1894 mutex_unlock(&gpio_lookup_lock);
1895 return table;
1896}
1897
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001898static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001899 unsigned int idx,
1900 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001901{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001902 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001903 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001904 struct gpiod_lookup *p;
1905
Alexandre Courbotad824782013-12-03 12:20:11 +09001906 table = gpiod_find_lookup_table(dev);
1907 if (!table)
1908 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001909
Alexandre Courbotad824782013-12-03 12:20:11 +09001910 for (p = &table->table[0]; p->chip_label; p++) {
1911 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001912
Alexandre Courbotad824782013-12-03 12:20:11 +09001913 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001914 if (p->idx != idx)
1915 continue;
1916
Alexandre Courbotad824782013-12-03 12:20:11 +09001917 /* If the lookup entry has a con_id, require exact match */
1918 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1919 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001920
Alexandre Courbotad824782013-12-03 12:20:11 +09001921 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001922
Alexandre Courbotad824782013-12-03 12:20:11 +09001923 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001924 dev_err(dev, "cannot find GPIO chip %s\n",
1925 p->chip_label);
1926 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001927 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001928
Alexandre Courbotad824782013-12-03 12:20:11 +09001929 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001930 dev_err(dev,
1931 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1932 idx, chip->ngpio, chip->label);
1933 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001934 }
1935
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001936 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001937 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001938
1939 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001940 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001941
1942 return desc;
1943}
1944
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001945static int dt_gpio_count(struct device *dev, const char *con_id)
1946{
1947 int ret;
1948 char propname[32];
1949 unsigned int i;
1950
1951 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1952 if (con_id)
1953 snprintf(propname, sizeof(propname), "%s-%s",
1954 con_id, gpio_suffixes[i]);
1955 else
1956 snprintf(propname, sizeof(propname), "%s",
1957 gpio_suffixes[i]);
1958
1959 ret = of_gpio_named_count(dev->of_node, propname);
1960 if (ret >= 0)
1961 break;
1962 }
1963 return ret;
1964}
1965
1966static int platform_gpio_count(struct device *dev, const char *con_id)
1967{
1968 struct gpiod_lookup_table *table;
1969 struct gpiod_lookup *p;
1970 unsigned int count = 0;
1971
1972 table = gpiod_find_lookup_table(dev);
1973 if (!table)
1974 return -ENOENT;
1975
1976 for (p = &table->table[0]; p->chip_label; p++) {
1977 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
1978 (!con_id && !p->con_id))
1979 count++;
1980 }
1981 if (!count)
1982 return -ENOENT;
1983
1984 return count;
1985}
1986
1987/**
1988 * gpiod_count - return the number of GPIOs associated with a device / function
1989 * or -ENOENT if no GPIO has been assigned to the requested function
1990 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1991 * @con_id: function within the GPIO consumer
1992 */
1993int gpiod_count(struct device *dev, const char *con_id)
1994{
1995 int count = -ENOENT;
1996
1997 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
1998 count = dt_gpio_count(dev, con_id);
1999 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2000 count = acpi_gpio_count(dev, con_id);
2001
2002 if (count < 0)
2003 count = platform_gpio_count(dev, con_id);
2004
2005 return count;
2006}
2007EXPORT_SYMBOL_GPL(gpiod_count);
2008
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002009/**
Thierry Reding08791622014-04-25 16:54:22 +02002010 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002011 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002012 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002013 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002014 *
2015 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002016 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07002017 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002018 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002019struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002020 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002021{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002022 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002023}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002024EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002025
2026/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002027 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2028 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2029 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002030 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002031 *
2032 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2033 * the requested function it will return NULL. This is convenient for drivers
2034 * that need to handle optional GPIOs.
2035 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002036struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002037 const char *con_id,
2038 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002039{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002040 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002041}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002042EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002043
Benoit Parrotf625d462015-02-02 11:44:44 -06002044
2045/**
2046 * gpiod_configure_flags - helper function to configure a given GPIO
2047 * @desc: gpio whose value will be assigned
2048 * @con_id: function within the GPIO consumer
2049 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2050 * of_get_gpio_hog()
2051 * @dflags: gpiod_flags - optional GPIO initialization flags
2052 *
2053 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2054 * requested function and/or index, or another IS_ERR() code if an error
2055 * occurred while trying to acquire the GPIO.
2056 */
2057static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
2058 unsigned long lflags, enum gpiod_flags dflags)
2059{
2060 int status;
2061
2062 if (lflags & GPIO_ACTIVE_LOW)
2063 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2064 if (lflags & GPIO_OPEN_DRAIN)
2065 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2066 if (lflags & GPIO_OPEN_SOURCE)
2067 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2068
2069 /* No particular flag request, return here... */
2070 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2071 pr_debug("no flags found for %s\n", con_id);
2072 return 0;
2073 }
2074
2075 /* Process flags */
2076 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2077 status = gpiod_direction_output(desc,
2078 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2079 else
2080 status = gpiod_direction_input(desc);
2081
2082 return status;
2083}
2084
Thierry Reding29a1f2332014-04-25 17:10:06 +02002085/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002086 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002087 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002088 * @con_id: function within the GPIO consumer
2089 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002090 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002091 *
2092 * This variant of gpiod_get() allows to access GPIOs other than the first
2093 * defined one for functions that define several GPIOs.
2094 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002095 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2096 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07002097 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002098 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002099struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002100 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002101 unsigned int idx,
2102 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002103{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002104 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002105 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002106 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002107
2108 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2109
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002110 if (dev) {
2111 /* Using device tree? */
2112 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2113 dev_dbg(dev, "using device tree for GPIO lookup\n");
2114 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2115 } else if (ACPI_COMPANION(dev)) {
2116 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2117 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2118 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002119 }
2120
2121 /*
2122 * Either we are not using DT or ACPI, or their lookup did not return
2123 * a result. In that case, use platform lookup as a fallback.
2124 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002125 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002126 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002127 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002128 }
2129
2130 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002131 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002132 return desc;
2133 }
2134
2135 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002136 if (status < 0)
2137 return ERR_PTR(status);
2138
Benoit Parrotf625d462015-02-02 11:44:44 -06002139 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002140 if (status < 0) {
2141 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2142 gpiod_put(desc);
2143 return ERR_PTR(status);
2144 }
2145
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002146 return desc;
2147}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002148EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002149
2150/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002151 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2152 * @fwnode: handle of the firmware node
2153 * @propname: name of the firmware property representing the GPIO
2154 *
2155 * This function can be used for drivers that get their configuration
2156 * from firmware.
2157 *
2158 * Function properly finds the corresponding GPIO using whatever is the
2159 * underlying firmware interface and then makes sure that the GPIO
2160 * descriptor is requested before it is returned to the caller.
2161 *
2162 * In case of error an ERR_PTR() is returned.
2163 */
2164struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2165 const char *propname)
2166{
2167 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2168 bool active_low = false;
2169 int ret;
2170
2171 if (!fwnode)
2172 return ERR_PTR(-EINVAL);
2173
2174 if (is_of_node(fwnode)) {
2175 enum of_gpio_flags flags;
2176
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002177 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002178 &flags);
2179 if (!IS_ERR(desc))
2180 active_low = flags & OF_GPIO_ACTIVE_LOW;
2181 } else if (is_acpi_node(fwnode)) {
2182 struct acpi_gpio_info info;
2183
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002184 desc = acpi_get_gpiod_by_index(to_acpi_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002185 &info);
2186 if (!IS_ERR(desc))
2187 active_low = info.active_low;
2188 }
2189
2190 if (IS_ERR(desc))
2191 return desc;
2192
2193 ret = gpiod_request(desc, NULL);
2194 if (ret)
2195 return ERR_PTR(ret);
2196
2197 /* Only value flag can be set from both DT and ACPI is active_low */
2198 if (active_low)
2199 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2200
2201 return desc;
2202}
2203EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2204
2205/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002206 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2207 * function
2208 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2209 * @con_id: function within the GPIO consumer
2210 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002211 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002212 *
2213 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2214 * specified index was assigned to the requested function it will return NULL.
2215 * This is convenient for drivers that need to handle optional GPIOs.
2216 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002217struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002218 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002219 unsigned int index,
2220 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002221{
2222 struct gpio_desc *desc;
2223
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002224 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002225 if (IS_ERR(desc)) {
2226 if (PTR_ERR(desc) == -ENOENT)
2227 return NULL;
2228 }
2229
2230 return desc;
2231}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002232EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002233
2234/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002235 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2236 * @desc: gpio whose value will be assigned
2237 * @name: gpio line name
2238 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2239 * of_get_gpio_hog()
2240 * @dflags: gpiod_flags - optional GPIO initialization flags
2241 */
2242int gpiod_hog(struct gpio_desc *desc, const char *name,
2243 unsigned long lflags, enum gpiod_flags dflags)
2244{
2245 struct gpio_chip *chip;
2246 struct gpio_desc *local_desc;
2247 int hwnum;
2248 int status;
2249
2250 chip = gpiod_to_chip(desc);
2251 hwnum = gpio_chip_hwgpio(desc);
2252
2253 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2254 if (IS_ERR(local_desc)) {
Linus Walleija7138902015-06-05 11:36:10 +02002255 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2256 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002257 return PTR_ERR(local_desc);
2258 }
2259
2260 status = gpiod_configure_flags(desc, name, lflags, dflags);
2261 if (status < 0) {
Linus Walleija7138902015-06-05 11:36:10 +02002262 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2263 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002264 gpiochip_free_own_desc(desc);
2265 return status;
2266 }
2267
2268 /* Mark GPIO as hogged so it can be identified and removed later */
2269 set_bit(FLAG_IS_HOGGED, &desc->flags);
2270
2271 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2272 desc_to_gpio(desc), name,
2273 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2274 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2275 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2276
2277 return 0;
2278}
2279
2280/**
2281 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2282 * @chip: gpio chip to act on
2283 *
2284 * This is only used by of_gpiochip_remove to free hogged gpios
2285 */
2286static void gpiochip_free_hogs(struct gpio_chip *chip)
2287{
2288 int id;
2289
2290 for (id = 0; id < chip->ngpio; id++) {
2291 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2292 gpiochip_free_own_desc(&chip->desc[id]);
2293 }
2294}
2295
2296/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002297 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2298 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2299 * @con_id: function within the GPIO consumer
2300 * @flags: optional GPIO initialization flags
2301 *
2302 * This function acquires all the GPIOs defined under a given function.
2303 *
2304 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2305 * no GPIO has been assigned to the requested function, or another IS_ERR()
2306 * code if an error occurred while trying to acquire the GPIOs.
2307 */
2308struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2309 const char *con_id,
2310 enum gpiod_flags flags)
2311{
2312 struct gpio_desc *desc;
2313 struct gpio_descs *descs;
2314 int count;
2315
2316 count = gpiod_count(dev, con_id);
2317 if (count < 0)
2318 return ERR_PTR(count);
2319
2320 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2321 GFP_KERNEL);
2322 if (!descs)
2323 return ERR_PTR(-ENOMEM);
2324
2325 for (descs->ndescs = 0; descs->ndescs < count; ) {
2326 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2327 if (IS_ERR(desc)) {
2328 gpiod_put_array(descs);
2329 return ERR_CAST(desc);
2330 }
2331 descs->desc[descs->ndescs] = desc;
2332 descs->ndescs++;
2333 }
2334 return descs;
2335}
2336EXPORT_SYMBOL_GPL(gpiod_get_array);
2337
2338/**
2339 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2340 * function
2341 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2342 * @con_id: function within the GPIO consumer
2343 * @flags: optional GPIO initialization flags
2344 *
2345 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2346 * assigned to the requested function it will return NULL.
2347 */
2348struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2349 const char *con_id,
2350 enum gpiod_flags flags)
2351{
2352 struct gpio_descs *descs;
2353
2354 descs = gpiod_get_array(dev, con_id, flags);
2355 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2356 return NULL;
2357
2358 return descs;
2359}
2360EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2361
2362/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002363 * gpiod_put - dispose of a GPIO descriptor
2364 * @desc: GPIO descriptor to dispose of
2365 *
2366 * No descriptor can be used after gpiod_put() has been called on it.
2367 */
2368void gpiod_put(struct gpio_desc *desc)
2369{
2370 gpiod_free(desc);
2371}
2372EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002373
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002374/**
2375 * gpiod_put_array - dispose of multiple GPIO descriptors
2376 * @descs: struct gpio_descs containing an array of descriptors
2377 */
2378void gpiod_put_array(struct gpio_descs *descs)
2379{
2380 unsigned int i;
2381
2382 for (i = 0; i < descs->ndescs; i++)
2383 gpiod_put(descs->desc[i]);
2384
2385 kfree(descs);
2386}
2387EXPORT_SYMBOL_GPL(gpiod_put_array);
2388
David Brownelld2876d02008-02-04 22:28:20 -08002389#ifdef CONFIG_DEBUG_FS
2390
2391static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2392{
2393 unsigned i;
2394 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002395 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002396 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002397 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002398
2399 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02002400 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2401 if (gdesc->name) {
2402 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2403 gpio, gdesc->name);
2404 }
David Brownelld2876d02008-02-04 22:28:20 -08002405 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02002406 }
David Brownelld2876d02008-02-04 22:28:20 -08002407
Alexandre Courbot372e7222013-02-03 01:29:29 +09002408 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002409 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002410 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02002411 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2412 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08002413 is_out ? "out" : "in ",
2414 chip->get
2415 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002416 : "? ",
2417 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002418 seq_printf(s, "\n");
2419 }
2420}
2421
Thierry Redingf9c4a312012-04-12 13:26:01 +02002422static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002423{
Grant Likely362432a2013-02-09 09:41:49 +00002424 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002425 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002426 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002427
2428 s->private = "";
2429
Grant Likely362432a2013-02-09 09:41:49 +00002430 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002431 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002432 if (index-- == 0) {
2433 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002434 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002435 }
2436 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002437
2438 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002439}
2440
2441static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2442{
Grant Likely362432a2013-02-09 09:41:49 +00002443 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002444 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002445 void *ret = NULL;
2446
Grant Likely362432a2013-02-09 09:41:49 +00002447 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002448 if (list_is_last(&chip->list, &gpio_chips))
2449 ret = NULL;
2450 else
2451 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002452 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002453
2454 s->private = "\n";
2455 ++*pos;
2456
2457 return ret;
2458}
2459
2460static void gpiolib_seq_stop(struct seq_file *s, void *v)
2461{
2462}
2463
2464static int gpiolib_seq_show(struct seq_file *s, void *v)
2465{
2466 struct gpio_chip *chip = v;
2467 struct device *dev;
2468
2469 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2470 chip->base, chip->base + chip->ngpio - 1);
2471 dev = chip->dev;
2472 if (dev)
2473 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2474 dev_name(dev));
2475 if (chip->label)
2476 seq_printf(s, ", %s", chip->label);
2477 if (chip->can_sleep)
2478 seq_printf(s, ", can sleep");
2479 seq_printf(s, ":\n");
2480
2481 if (chip->dbg_show)
2482 chip->dbg_show(s, chip);
2483 else
2484 gpiolib_dbg_show(s, chip);
2485
David Brownelld2876d02008-02-04 22:28:20 -08002486 return 0;
2487}
2488
Thierry Redingf9c4a312012-04-12 13:26:01 +02002489static const struct seq_operations gpiolib_seq_ops = {
2490 .start = gpiolib_seq_start,
2491 .next = gpiolib_seq_next,
2492 .stop = gpiolib_seq_stop,
2493 .show = gpiolib_seq_show,
2494};
2495
David Brownelld2876d02008-02-04 22:28:20 -08002496static int gpiolib_open(struct inode *inode, struct file *file)
2497{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002498 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002499}
2500
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002501static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002502 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002503 .open = gpiolib_open,
2504 .read = seq_read,
2505 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002506 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002507};
2508
2509static int __init gpiolib_debugfs_init(void)
2510{
2511 /* /sys/kernel/debug/gpio */
2512 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2513 NULL, NULL, &gpiolib_operations);
2514 return 0;
2515}
2516subsys_initcall(gpiolib_debugfs_init);
2517
2518#endif /* DEBUG_FS */