blob: 7c7c39c46eb7d154e7e513aa56e0de864beaa404 [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d2008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
David Brownelld2876d02008-02-04 22:28:20 -080018
Mika Westerberg664e3e52014-01-08 12:40:54 +020019#include "gpiolib.h"
20
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060021#define CREATE_TRACE_POINTS
22#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080023
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070024/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080025 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070026 * The GPIO programming interface allows for inlining speed-critical
27 * get/set operations for common cases, so that access to SOC-integrated
28 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080029 */
30
31
32/* When debugging, extend minimal trust to callers and platform code.
33 * Also emit diagnostic messages that may help initial bringup, when
34 * board setup or driver bugs are most common.
35 *
36 * Otherwise, minimize overhead in what may be bitbanging codepaths.
37 */
38#ifdef DEBUG
39#define extra_checks 1
40#else
41#define extra_checks 0
42#endif
43
44/* gpio_lock prevents conflicts during gpio_desc[] table updates.
45 * While any GPIO is requested, its gpio_chip is not removable;
46 * each GPIO's "requested" flag serves as a lock and refcount.
47 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090048DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080049
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +090050#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
51
Alexandre Courbotbae48da2013-10-17 10:21:38 -070052static DEFINE_MUTEX(gpio_lookup_lock);
53static LIST_HEAD(gpio_lookup_list);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090054LIST_HEAD(gpio_chips);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +020055
Johan Hovold6d867502015-05-04 17:23:25 +020056
57static void gpiochip_free_hogs(struct gpio_chip *chip);
58static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
59
60
David Brownelld2876d02008-02-04 22:28:20 -080061static inline void desc_set_label(struct gpio_desc *d, const char *label)
62{
David Brownelld2876d02008-02-04 22:28:20 -080063 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080064}
65
Alexandre Courbot372e7222013-02-03 01:29:29 +090066/**
67 * Convert a GPIO number to its descriptor
68 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070069struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090070{
Alexandre Courbot14e85c02014-11-19 16:51:27 +090071 struct gpio_chip *chip;
72 unsigned long flags;
73
74 spin_lock_irqsave(&gpio_lock, flags);
75
76 list_for_each_entry(chip, &gpio_chips, list) {
77 if (chip->base <= gpio && chip->base + chip->ngpio > gpio) {
78 spin_unlock_irqrestore(&gpio_lock, flags);
79 return &chip->desc[gpio - chip->base];
80 }
81 }
82
83 spin_unlock_irqrestore(&gpio_lock, flags);
84
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +090085 if (!gpio_is_valid(gpio))
86 WARN(1, "invalid GPIO %d\n", gpio);
87
Alexandre Courbot14e85c02014-11-19 16:51:27 +090088 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +090089}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070090EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090091
92/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090093 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +020094 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090095struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
96 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +020097{
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090098 if (hwnum >= chip->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +090099 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200100
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900101 return &chip->desc[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200102}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900103
104/**
105 * Convert a GPIO descriptor to the integer namespace.
106 * This should disappear in the future but is needed since we still
107 * use GPIO numbers for error messages and sysfs nodes
108 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700109int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900110{
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900111 return desc->chip->base + (desc - &desc->chip->desc[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900112}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700113EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900114
115
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700116/**
117 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
118 * @desc: descriptor to return the chip of
119 */
120struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900121{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900122 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900123}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700124EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800125
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700126/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
127static int gpiochip_find_base(int ngpio)
128{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900129 struct gpio_chip *chip;
130 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700131
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900132 list_for_each_entry_reverse(chip, &gpio_chips, list) {
133 /* found a free space? */
134 if (chip->base + chip->ngpio <= base)
135 break;
136 else
137 /* nope, check the space right before the chip */
138 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700139 }
140
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900141 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700142 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900143 return base;
144 } else {
145 pr_err("%s: cannot find free range\n", __func__);
146 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700147 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700148}
149
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700150/**
151 * gpiod_get_direction - return the current direction of a GPIO
152 * @desc: GPIO to get the direction of
153 *
154 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
155 *
156 * This function may sleep if gpiod_cansleep() is true.
157 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900158int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300159{
160 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900161 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300162 int status = -EINVAL;
163
Alexandre Courbot372e7222013-02-03 01:29:29 +0900164 chip = gpiod_to_chip(desc);
165 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300166
167 if (!chip->get_direction)
168 return status;
169
Alexandre Courbot372e7222013-02-03 01:29:29 +0900170 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300171 if (status > 0) {
172 /* GPIOF_DIR_IN, or other positive */
173 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900174 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300175 }
176 if (status == 0) {
177 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900178 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300179 }
180 return status;
181}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700182EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300183
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900184/*
185 * Add a new chip to the global chips list, keeping the list of chips sorted
186 * by base order.
187 *
188 * Return -EBUSY if the new chip overlaps with some other chip's integer
189 * space.
190 */
191static int gpiochip_add_to_list(struct gpio_chip *chip)
192{
Masahiro Yamadad1aceb82015-07-21 14:45:40 +0900193 struct list_head *pos;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900194 struct gpio_chip *_chip;
195 int err = 0;
196
197 /* find where to insert our chip */
198 list_for_each(pos, &gpio_chips) {
199 _chip = list_entry(pos, struct gpio_chip, list);
200 /* shall we insert before _chip? */
201 if (_chip->base >= chip->base + chip->ngpio)
202 break;
203 }
204
205 /* are we stepping on the chip right before? */
206 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
207 _chip = list_entry(pos->prev, struct gpio_chip, list);
208 if (_chip->base + _chip->ngpio > chip->base) {
209 dev_err(chip->dev,
210 "GPIO integer space overlap, cannot add chip\n");
211 err = -EBUSY;
212 }
213 }
214
215 if (!err)
216 list_add_tail(&chip->list, pos);
217
218 return err;
219}
220
Linus Walleijf881bab2015-09-23 16:20:43 -0700221/**
222 * Convert a GPIO name to its descriptor
223 */
224static struct gpio_desc *gpio_name_to_desc(const char * const name)
225{
226 struct gpio_chip *chip;
227 unsigned long flags;
228
229 spin_lock_irqsave(&gpio_lock, flags);
230
231 list_for_each_entry(chip, &gpio_chips, list) {
232 int i;
233
234 for (i = 0; i != chip->ngpio; ++i) {
235 struct gpio_desc *gpio = &chip->desc[i];
236
237 if (!gpio->name)
238 continue;
239
240 if (!strcmp(gpio->name, name)) {
241 spin_unlock_irqrestore(&gpio_lock, flags);
242 return gpio;
243 }
244 }
245 }
246
247 spin_unlock_irqrestore(&gpio_lock, flags);
248
249 return NULL;
250}
251
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200252/*
253 * Takes the names from gc->names and checks if they are all unique. If they
254 * are, they are assigned to their gpio descriptors.
255 *
256 * Returns -EEXIST if one of the names is already used for a different GPIO.
257 */
258static int gpiochip_set_desc_names(struct gpio_chip *gc)
259{
260 int i;
261
262 if (!gc->names)
263 return 0;
264
265 /* First check all names if they are unique */
266 for (i = 0; i != gc->ngpio; ++i) {
267 struct gpio_desc *gpio;
268
269 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700270 if (gpio)
271 dev_warn(gc->dev, "Detected name collision for "
272 "GPIO name '%s'\n",
273 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200274 }
275
276 /* Then add all names to the GPIO descriptors */
277 for (i = 0; i != gc->ngpio; ++i)
278 gc->desc[i].name = gc->names[i];
279
280 return 0;
281}
282
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700283/**
David Brownelld2876d02008-02-04 22:28:20 -0800284 * gpiochip_add() - register a gpio_chip
285 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900286 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -0800287 *
288 * Returns a negative errno if the chip can't be registered, such as
289 * because the chip->base is invalid or already associated with a
290 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700291 *
David Brownelld8f388d2008-07-25 01:46:07 -0700292 * When gpiochip_add() is called very early during boot, so that GPIOs
293 * can be freely used, the chip->dev device must be registered before
294 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
295 * for GPIOs will fail rudely.
296 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700297 * If chip->base is negative, this requests dynamic assignment of
298 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800299 */
300int gpiochip_add(struct gpio_chip *chip)
301{
302 unsigned long flags;
303 int status = 0;
304 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700305 int base = chip->base;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900306 struct gpio_desc *descs;
David Brownelld2876d02008-02-04 22:28:20 -0800307
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900308 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
309 if (!descs)
310 return -ENOMEM;
David Brownelld2876d02008-02-04 22:28:20 -0800311
312 spin_lock_irqsave(&gpio_lock, flags);
313
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700314 if (base < 0) {
315 base = gpiochip_find_base(chip->ngpio);
316 if (base < 0) {
317 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100318 spin_unlock_irqrestore(&gpio_lock, flags);
319 goto err_free_descs;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700320 }
321 chip->base = base;
322 }
323
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900324 status = gpiochip_add_to_list(chip);
Johan Hovold05aa5202015-01-12 17:12:26 +0100325 if (status) {
326 spin_unlock_irqrestore(&gpio_lock, flags);
327 goto err_free_descs;
328 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900329
Johan Hovold05aa5202015-01-12 17:12:26 +0100330 for (id = 0; id < chip->ngpio; id++) {
331 struct gpio_desc *desc = &descs[id];
David Brownelld8f388d2008-07-25 01:46:07 -0700332
Johan Hovold05aa5202015-01-12 17:12:26 +0100333 desc->chip = chip;
334
335 /* REVISIT: most hardware initializes GPIOs as inputs (often
336 * with pullups enabled) so power usage is minimized. Linux
337 * code should set the gpio direction first thing; but until
338 * it does, and in case chip->get_direction is not set, we may
339 * expose the wrong direction in sysfs.
340 */
341 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800342 }
343
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900344 chip->desc = descs;
345
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800346 spin_unlock_irqrestore(&gpio_lock, flags);
347
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530348#ifdef CONFIG_PINCTRL
349 INIT_LIST_HEAD(&chip->pin_ranges);
350#endif
351
Grygorii Strashko37269602015-06-25 20:30:51 +0300352 if (!chip->owner && chip->dev && chip->dev->driver)
353 chip->owner = chip->dev->driver->owner;
354
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200355 status = gpiochip_set_desc_names(chip);
356 if (status)
357 goto err_remove_from_list;
358
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200359 status = of_gpiochip_add(chip);
360 if (status)
361 goto err_remove_chip;
362
Mika Westerberg664e3e52014-01-08 12:40:54 +0200363 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600364
Johan Hovold426577b2015-05-04 17:10:32 +0200365 status = gpiochip_sysfs_register(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100366 if (status)
367 goto err_remove_chip;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600368
Andy Shevchenko7589e592013-12-05 11:26:23 +0200369 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700370 chip->base, chip->base + chip->ngpio - 1,
371 chip->label ? : "generic");
372
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600373 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800374
Johan Hovold225fce82015-01-12 17:12:25 +0100375err_remove_chip:
376 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200377 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100378 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200379err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100380 spin_lock_irqsave(&gpio_lock, flags);
381 list_del(&chip->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800382 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold05aa5202015-01-12 17:12:26 +0100383 chip->desc = NULL;
Johan Hovold225fce82015-01-12 17:12:25 +0100384err_free_descs:
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900385 kfree(descs);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900386
David Brownelld2876d02008-02-04 22:28:20 -0800387 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200388 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600389 chip->base, chip->base + chip->ngpio - 1,
390 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800391 return status;
392}
393EXPORT_SYMBOL_GPL(gpiochip_add);
394
395/**
396 * gpiochip_remove() - unregister a gpio_chip
397 * @chip: the chip to unregister
398 *
399 * A gpio_chip with any GPIOs still requested may not be removed.
400 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200401void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800402{
Johan Hovoldfab28b82015-05-04 17:10:27 +0200403 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800404 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800405 unsigned id;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200406 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800407
Johan Hovold426577b2015-05-04 17:10:32 +0200408 gpiochip_sysfs_unregister(chip);
Johan Hovold01cca932015-01-12 17:12:29 +0100409
Johan Hovold00acc3d2015-01-12 17:12:27 +0100410 gpiochip_irqchip_remove(chip);
411
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200412 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100413 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600414 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600415 of_gpiochip_remove(chip);
416
Johan Hovold6798aca2015-01-12 17:12:28 +0100417 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900418 for (id = 0; id < chip->ngpio; id++) {
Johan Hovoldfab28b82015-05-04 17:10:27 +0200419 desc = &chip->desc[id];
420 desc->chip = NULL;
421 if (test_bit(FLAG_REQUESTED, &desc->flags))
422 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800423 }
abdoulaye berthee1db1702014-07-05 18:28:50 +0200424 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800425 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900426
Johan Hovoldfab28b82015-05-04 17:10:27 +0200427 if (requested)
428 dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
429
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900430 kfree(chip->desc);
431 chip->desc = NULL;
David Brownelld2876d02008-02-04 22:28:20 -0800432}
433EXPORT_SYMBOL_GPL(gpiochip_remove);
434
Grant Likely594fa262010-06-08 07:48:16 -0600435/**
436 * gpiochip_find() - iterator for locating a specific gpio_chip
437 * @data: data to pass to match function
438 * @callback: Callback function to check gpio_chip
439 *
440 * Similar to bus_find_device. It returns a reference to a gpio_chip as
441 * determined by a user supplied @match callback. The callback should return
442 * 0 if the device doesn't match and non-zero if it does. If the callback is
443 * non-zero, this function will return to the caller and not iterate over any
444 * more gpio_chips.
445 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600446struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700447 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600448 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600449{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900450 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600451 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600452
453 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900454 list_for_each_entry(chip, &gpio_chips, list)
455 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600456 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900457
458 /* No match? */
459 if (&chip->list == &gpio_chips)
460 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600461 spin_unlock_irqrestore(&gpio_lock, flags);
462
463 return chip;
464}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600465EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800466
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900467static int gpiochip_match_name(struct gpio_chip *chip, void *data)
468{
469 const char *name = data;
470
471 return !strcmp(chip->label, name);
472}
473
474static struct gpio_chip *find_chip_by_name(const char *name)
475{
476 return gpiochip_find((void *)name, gpiochip_match_name);
477}
478
Linus Walleij14250522014-03-25 10:40:18 +0100479#ifdef CONFIG_GPIOLIB_IRQCHIP
480
481/*
482 * The following is irqchip helper code for gpiochips.
483 */
484
485/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200486 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
487 * @gpiochip: the gpiochip to set the irqchip chain to
488 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100489 * @parent_irq: the irq number corresponding to the parent IRQ for this
490 * chained irqchip
491 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200492 * coming out of the gpiochip. If the interrupt is nested rather than
493 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100494 */
495void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
496 struct irq_chip *irqchip,
497 int parent_irq,
498 irq_flow_handler_t parent_handler)
499{
Linus Walleij83141a72014-09-26 13:50:12 +0200500 unsigned int offset;
501
Linus Walleij83141a72014-09-26 13:50:12 +0200502 if (!gpiochip->irqdomain) {
503 chip_err(gpiochip, "called %s before setting up irqchip\n",
504 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200505 return;
506 }
507
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200508 if (parent_handler) {
509 if (gpiochip->can_sleep) {
510 chip_err(gpiochip,
511 "you cannot have chained interrupts on a "
512 "chip that may sleep\n");
513 return;
514 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200515 /*
516 * The parent irqchip is already using the chip_data for this
517 * irqchip, so our callbacks simply use the handler_data.
518 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +0200519 irq_set_chained_handler_and_data(parent_irq, parent_handler,
520 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300521
522 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200523 }
Linus Walleij83141a72014-09-26 13:50:12 +0200524
525 /* Set the parent IRQ for all affected IRQs */
526 for (offset = 0; offset < gpiochip->ngpio; offset++)
527 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
528 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100529}
530EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
531
532/**
533 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
534 * @d: the irqdomain used by this irqchip
535 * @irq: the global irq number used by this GPIO irqchip irq
536 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
537 *
538 * This function will set up the mapping for a certain IRQ line on a
539 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
540 * stored inside the gpiochip.
541 */
542static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
543 irq_hw_number_t hwirq)
544{
545 struct gpio_chip *chip = d->host_data;
546
Linus Walleij14250522014-03-25 10:40:18 +0100547 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300548 /*
549 * This lock class tells lockdep that GPIO irqs are in a different
550 * category than their parents, so it won't report false recursion.
551 */
552 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +0200553 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200554 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300555 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200556 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100557 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -0500558
Linus Walleij1333b902014-04-23 16:45:12 +0200559 /*
560 * No set-up of the hardware will happen if IRQ_TYPE_NONE
561 * is passed as default type.
562 */
563 if (chip->irq_default_type != IRQ_TYPE_NONE)
564 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100565
566 return 0;
567}
568
Linus Walleijc3626fd2014-03-28 20:42:01 +0100569static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
570{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200571 struct gpio_chip *chip = d->host_data;
572
Linus Walleij1c8732b2014-04-09 13:34:39 +0200573 if (chip->can_sleep)
574 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100575 irq_set_chip_and_handler(irq, NULL, NULL);
576 irq_set_chip_data(irq, NULL);
577}
578
Linus Walleij14250522014-03-25 10:40:18 +0100579static const struct irq_domain_ops gpiochip_domain_ops = {
580 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100581 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100582 /* Virtually all GPIO irqchips are twocell:ed */
583 .xlate = irq_domain_xlate_twocell,
584};
585
586static int gpiochip_irq_reqres(struct irq_data *d)
587{
588 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
589
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300590 if (!try_module_get(chip->owner))
591 return -ENODEV;
592
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900593 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100594 chip_err(chip,
595 "unable to lock HW IRQ %lu for IRQ\n",
596 d->hwirq);
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300597 module_put(chip->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100598 return -EINVAL;
599 }
600 return 0;
601}
602
603static void gpiochip_irq_relres(struct irq_data *d)
604{
605 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
606
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900607 gpiochip_unlock_as_irq(chip, d->hwirq);
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300608 module_put(chip->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100609}
610
611static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
612{
613 return irq_find_mapping(chip->irqdomain, offset);
614}
615
616/**
617 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
618 * @gpiochip: the gpiochip to remove the irqchip from
619 *
620 * This is called only from gpiochip_remove()
621 */
622static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
623{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100624 unsigned int offset;
625
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300626 acpi_gpiochip_free_interrupts(gpiochip);
627
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300628 if (gpiochip->irq_parent) {
629 irq_set_chained_handler(gpiochip->irq_parent, NULL);
630 irq_set_handler_data(gpiochip->irq_parent, NULL);
631 }
632
Linus Walleijc3626fd2014-03-28 20:42:01 +0100633 /* Remove all IRQ mappings and delete the domain */
634 if (gpiochip->irqdomain) {
635 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300636 irq_dispose_mapping(
637 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100638 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100639 }
Linus Walleij14250522014-03-25 10:40:18 +0100640
641 if (gpiochip->irqchip) {
642 gpiochip->irqchip->irq_request_resources = NULL;
643 gpiochip->irqchip->irq_release_resources = NULL;
644 gpiochip->irqchip = NULL;
645 }
646}
647
648/**
649 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
650 * @gpiochip: the gpiochip to add the irqchip to
651 * @irqchip: the irqchip to add to the gpiochip
652 * @first_irq: if not dynamically assigned, the base (first) IRQ to
653 * allocate gpiochip irqs from
654 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200655 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
656 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300657 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +0100658 *
659 * This function closely associates a certain irqchip with a certain
660 * gpiochip, providing an irq domain to translate the local IRQs to
661 * global irqs in the gpiolib core, and making sure that the gpiochip
662 * is passed as chip data to all related functions. Driver callbacks
663 * need to use container_of() to get their local state containers back
664 * from the gpiochip passed as chip data. An irqdomain will be stored
665 * in the gpiochip that shall be used by the driver to handle IRQ number
666 * translation. The gpiochip will need to be initialized and registered
667 * before calling this function.
668 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100669 * This function will handle two cell:ed simple IRQs and assumes all
670 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100671 * need to be open coded.
672 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300673int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
674 struct irq_chip *irqchip,
675 unsigned int first_irq,
676 irq_flow_handler_t handler,
677 unsigned int type,
678 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +0100679{
680 struct device_node *of_node;
681 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100682 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100683
684 if (!gpiochip || !irqchip)
685 return -EINVAL;
686
687 if (!gpiochip->dev) {
688 pr_err("missing gpiochip .dev parent pointer\n");
689 return -EINVAL;
690 }
691 of_node = gpiochip->dev->of_node;
692#ifdef CONFIG_OF_GPIO
693 /*
Colin Cronin20a8a962015-05-18 11:41:43 -0700694 * If the gpiochip has an assigned OF node this takes precedence
Linus Walleij14250522014-03-25 10:40:18 +0100695 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
696 */
697 if (gpiochip->of_node)
698 of_node = gpiochip->of_node;
699#endif
700 gpiochip->irqchip = irqchip;
701 gpiochip->irq_handler = handler;
702 gpiochip->irq_default_type = type;
703 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300704 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +0100705 gpiochip->irqdomain = irq_domain_add_simple(of_node,
706 gpiochip->ngpio, first_irq,
707 &gpiochip_domain_ops, gpiochip);
708 if (!gpiochip->irqdomain) {
709 gpiochip->irqchip = NULL;
710 return -EINVAL;
711 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +0200712
713 /*
714 * It is possible for a driver to override this, but only if the
715 * alternative functions are both implemented.
716 */
717 if (!irqchip->irq_request_resources &&
718 !irqchip->irq_release_resources) {
719 irqchip->irq_request_resources = gpiochip_irq_reqres;
720 irqchip->irq_release_resources = gpiochip_irq_relres;
721 }
Linus Walleij14250522014-03-25 10:40:18 +0100722
723 /*
724 * Prepare the mapping since the irqchip shall be orthogonal to
725 * any gpiochip calls. If the first_irq was zero, this is
726 * necessary to allocate descriptors for all IRQs.
727 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100728 for (offset = 0; offset < gpiochip->ngpio; offset++) {
729 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
730 if (offset == 0)
731 /*
732 * Store the base into the gpiochip to be used when
733 * unmapping the irqs.
734 */
735 gpiochip->irq_base = irq_base;
736 }
Linus Walleij14250522014-03-25 10:40:18 +0100737
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300738 acpi_gpiochip_request_interrupts(gpiochip);
739
Linus Walleij14250522014-03-25 10:40:18 +0100740 return 0;
741}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300742EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +0100743
744#else /* CONFIG_GPIOLIB_IRQCHIP */
745
746static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
747
748#endif /* CONFIG_GPIOLIB_IRQCHIP */
749
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530750#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100751
Linus Walleij3f0f8672012-11-20 12:40:15 +0100752/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200753 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
754 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +0200755 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +0200756 * @gpio_offset: the start offset in the current gpio_chip number space
757 * @pin_group: name of the pin group inside the pin controller
758 */
759int gpiochip_add_pingroup_range(struct gpio_chip *chip,
760 struct pinctrl_dev *pctldev,
761 unsigned int gpio_offset, const char *pin_group)
762{
763 struct gpio_pin_range *pin_range;
764 int ret;
765
766 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
767 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200768 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200769 return -ENOMEM;
770 }
771
772 /* Use local offset as range ID */
773 pin_range->range.id = gpio_offset;
774 pin_range->range.gc = chip;
775 pin_range->range.name = chip->label;
776 pin_range->range.base = chip->base + gpio_offset;
777 pin_range->pctldev = pctldev;
778
779 ret = pinctrl_get_group_pins(pctldev, pin_group,
780 &pin_range->range.pins,
781 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100782 if (ret < 0) {
783 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200784 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100785 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200786
787 pinctrl_add_gpio_range(pctldev, &pin_range->range);
788
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200789 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
790 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200791 pinctrl_dev_get_devname(pctldev), pin_group);
792
793 list_add_tail(&pin_range->node, &chip->pin_ranges);
794
795 return 0;
796}
797EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
798
799/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100800 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
801 * @chip: the gpiochip to add the range for
802 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100803 * @gpio_offset: the start offset in the current gpio_chip number space
804 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100805 * @npins: the number of pins from the offset of each pin space (GPIO and
806 * pin controller) to accumulate in this range
807 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100808int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100809 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100810 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530811{
812 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800813 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530814
Linus Walleij3f0f8672012-11-20 12:40:15 +0100815 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530816 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200817 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100818 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530819 }
820
Linus Walleij3f0f8672012-11-20 12:40:15 +0100821 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100822 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100823 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530824 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100825 pin_range->range.base = chip->base + gpio_offset;
826 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530827 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100828 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530829 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100830 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800831 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200832 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100833 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800834 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100835 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200836 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
837 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100838 pinctl_name,
839 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530840
841 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100842
843 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530844}
Linus Walleij165adc92012-11-06 14:49:39 +0100845EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530846
Linus Walleij3f0f8672012-11-20 12:40:15 +0100847/**
848 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
849 * @chip: the chip to remove all the mappings for
850 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530851void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
852{
853 struct gpio_pin_range *pin_range, *tmp;
854
855 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
856 list_del(&pin_range->node);
857 pinctrl_remove_gpio_range(pin_range->pctldev,
858 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100859 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530860 }
861}
Linus Walleij165adc92012-11-06 14:49:39 +0100862EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
863
864#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530865
David Brownelld2876d02008-02-04 22:28:20 -0800866/* These "optional" allocation calls help prevent drivers from stomping
867 * on each other, and help provide better diagnostics in debugfs.
868 * They're called even less than the "set direction" calls.
869 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200870static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800871{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200872 struct gpio_chip *chip = desc->chip;
873 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800874 unsigned long flags;
875
876 spin_lock_irqsave(&gpio_lock, flags);
877
David Brownelld2876d02008-02-04 22:28:20 -0800878 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700879 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800880 */
881
882 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
883 desc_set_label(desc, label ? : "?");
884 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700885 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800886 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800887 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700888 }
889
890 if (chip->request) {
891 /* chip->request may sleep */
892 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900893 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700894 spin_lock_irqsave(&gpio_lock, flags);
895
896 if (status < 0) {
897 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700898 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300899 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700900 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700901 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300902 if (chip->get_direction) {
903 /* chip->get_direction may sleep */
904 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900905 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300906 spin_lock_irqsave(&gpio_lock, flags);
907 }
David Brownelld2876d02008-02-04 22:28:20 -0800908done:
Mika Westerberg77c2d792014-03-10 14:54:50 +0200909 spin_unlock_irqrestore(&gpio_lock, flags);
910 return status;
911}
912
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900913int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200914{
915 int status = -EPROBE_DEFER;
916 struct gpio_chip *chip;
917
918 if (!desc) {
919 pr_warn("%s: invalid GPIO\n", __func__);
920 return -EINVAL;
921 }
922
923 chip = desc->chip;
924 if (!chip)
925 goto done;
926
927 if (try_module_get(chip->owner)) {
928 status = __gpiod_request(desc, label);
929 if (status < 0)
930 module_put(chip->owner);
931 }
932
933done:
David Brownelld2876d02008-02-04 22:28:20 -0800934 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200935 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200936
David Brownelld2876d02008-02-04 22:28:20 -0800937 return status;
938}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900939
Mika Westerberg77c2d792014-03-10 14:54:50 +0200940static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800941{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200942 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -0800943 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -0700944 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800945
Uwe Kleine-König3d599d12008-10-15 22:03:12 -0700946 might_sleep();
947
Alexandre Courbot372e7222013-02-03 01:29:29 +0900948 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700949
David Brownelld2876d02008-02-04 22:28:20 -0800950 spin_lock_irqsave(&gpio_lock, flags);
951
David Brownell35e8bb52008-10-15 22:03:16 -0700952 chip = desc->chip;
953 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
954 if (chip->free) {
955 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -0700956 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900957 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700958 spin_lock_irqsave(&gpio_lock, flags);
959 }
David Brownelld2876d02008-02-04 22:28:20 -0800960 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -0800961 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -0700962 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530963 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +0530964 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -0600965 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200966 ret = true;
967 }
David Brownelld2876d02008-02-04 22:28:20 -0800968
969 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200970 return ret;
971}
972
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900973void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200974{
975 if (desc && __gpiod_free(desc))
976 module_put(desc->chip->owner);
977 else
978 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -0800979}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900980
David Brownelld2876d02008-02-04 22:28:20 -0800981/**
982 * gpiochip_is_requested - return string iff signal was requested
983 * @chip: controller managing the signal
984 * @offset: of signal within controller's 0..(ngpio - 1) range
985 *
986 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +0900987 * The string returned is the label passed to gpio_request(); if none has been
988 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -0800989 *
990 * This function is for use by GPIO controller drivers. The label can
991 * help with diagnostics, and knowing that the signal is used as a GPIO
992 * can help avoid accidentally multiplexing it to another controller.
993 */
994const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
995{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900996 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800997
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900998 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -0800999 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001000
1001 desc = &chip->desc[offset];
1002
Alexandre Courbot372e7222013-02-03 01:29:29 +09001003 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001004 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001005 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001006}
1007EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1008
Mika Westerberg77c2d792014-03-10 14:54:50 +02001009/**
1010 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1011 * @desc: GPIO descriptor to request
1012 * @label: label for the GPIO
1013 *
1014 * Function allows GPIO chip drivers to request and use their own GPIO
1015 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1016 * function will not increase reference count of the GPIO chip module. This
1017 * allows the GPIO chip module to be unloaded as needed (we assume that the
1018 * GPIO chip driver handles freeing the GPIOs it has requested).
1019 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001020struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1021 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001022{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001023 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1024 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001025
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001026 if (IS_ERR(desc)) {
1027 chip_err(chip, "failed to get GPIO descriptor\n");
1028 return desc;
1029 }
1030
1031 err = __gpiod_request(desc, label);
1032 if (err < 0)
1033 return ERR_PTR(err);
1034
1035 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001036}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001037EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001038
1039/**
1040 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1041 * @desc: GPIO descriptor to free
1042 *
1043 * Function frees the given GPIO requested previously with
1044 * gpiochip_request_own_desc().
1045 */
1046void gpiochip_free_own_desc(struct gpio_desc *desc)
1047{
1048 if (desc)
1049 __gpiod_free(desc);
1050}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001051EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08001052
1053/* Drivers MUST set GPIO direction before making get/set calls. In
1054 * some cases this is done in early boot, before IRQs are enabled.
1055 *
1056 * As a rule these aren't called more than once (except for drivers
1057 * using the open-drain emulation idiom) so these are natural places
1058 * to accumulate extra debugging checks. Note that we can't (yet)
1059 * rely on gpio_request() having been called beforehand.
1060 */
1061
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001062/**
1063 * gpiod_direction_input - set the GPIO direction to input
1064 * @desc: GPIO to set to input
1065 *
1066 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1067 * be called safely on it.
1068 *
1069 * Return 0 in case of success, else an error code.
1070 */
1071int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001072{
David Brownelld2876d02008-02-04 22:28:20 -08001073 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001074 int status = -EINVAL;
1075
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001076 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001077 pr_warn("%s: invalid GPIO\n", __func__);
1078 return -EINVAL;
1079 }
1080
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001081 chip = desc->chip;
1082 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001083 gpiod_warn(desc,
1084 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001085 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001086 return -EIO;
1087 }
1088
Alexandre Courbotd82da792014-07-22 16:17:43 +09001089 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001090 if (status == 0)
1091 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001092
Alexandre Courbot372e7222013-02-03 01:29:29 +09001093 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001094
David Brownelld2876d02008-02-04 22:28:20 -08001095 return status;
1096}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001097EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001098
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001099static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001100{
David Brownelld2876d02008-02-04 22:28:20 -08001101 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001102 int status = -EINVAL;
1103
Linus Walleijd468bf92013-09-24 11:54:38 +02001104 /* GPIOs used for IRQs shall not be set as output */
1105 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1106 gpiod_err(desc,
1107 "%s: tried to set a GPIO tied to an IRQ as output\n",
1108 __func__);
1109 return -EIO;
1110 }
1111
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301112 /* Open drain pin should not be driven to 1 */
1113 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001114 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301115
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301116 /* Open source pin should not be driven to 0 */
1117 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001118 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301119
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001120 chip = desc->chip;
1121 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001122 gpiod_warn(desc,
1123 "%s: missing set() or direction_output() operations\n",
1124 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001125 return -EIO;
1126 }
1127
Alexandre Courbotd82da792014-07-22 16:17:43 +09001128 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001129 if (status == 0)
1130 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001131 trace_gpio_value(desc_to_gpio(desc), 0, value);
1132 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001133 return status;
1134}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001135
1136/**
1137 * gpiod_direction_output_raw - set the GPIO direction to output
1138 * @desc: GPIO to set to output
1139 * @value: initial output value of the GPIO
1140 *
1141 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1142 * be called safely on it. The initial value of the output must be specified
1143 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1144 *
1145 * Return 0 in case of success, else an error code.
1146 */
1147int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1148{
1149 if (!desc || !desc->chip) {
1150 pr_warn("%s: invalid GPIO\n", __func__);
1151 return -EINVAL;
1152 }
1153 return _gpiod_direction_output_raw(desc, value);
1154}
1155EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1156
1157/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301158 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001159 * @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 the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1165 * account.
1166 *
1167 * Return 0 in case of success, else an error code.
1168 */
1169int gpiod_direction_output(struct gpio_desc *desc, int value)
1170{
1171 if (!desc || !desc->chip) {
1172 pr_warn("%s: invalid GPIO\n", __func__);
1173 return -EINVAL;
1174 }
1175 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1176 value = !value;
1177 return _gpiod_direction_output_raw(desc, value);
1178}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001179EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001180
Felipe Balbic4b5be92010-05-26 14:42:23 -07001181/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001182 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001183 * @gpio: the gpio to set debounce time
1184 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001185 *
1186 * returns -ENOTSUPP if the controller does not support setting
1187 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001188 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001189int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001190{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001191 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001192
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001193 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001194 pr_warn("%s: invalid GPIO\n", __func__);
1195 return -EINVAL;
1196 }
1197
Felipe Balbic4b5be92010-05-26 14:42:23 -07001198 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001199 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001200 gpiod_dbg(desc,
1201 "%s: missing set() or set_debounce() operations\n",
1202 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001203 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001204 }
1205
Alexandre Courbotd82da792014-07-22 16:17:43 +09001206 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001207}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001208EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001209
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001210/**
1211 * gpiod_is_active_low - test whether a GPIO is active-low or not
1212 * @desc: the gpio descriptor to test
1213 *
1214 * Returns 1 if the GPIO is active-low, 0 otherwise.
1215 */
1216int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001217{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001218 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001219}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001220EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001221
1222/* I/O calls are only valid after configuration completed; the relevant
1223 * "is this a valid GPIO" error checks should already have been done.
1224 *
1225 * "Get" operations are often inlinable as reading a pin value register,
1226 * and masking the relevant bit in that register.
1227 *
1228 * When "set" operations are inlinable, they involve writing that mask to
1229 * one register to set a low value, or a different register to set it high.
1230 * Otherwise locking is needed, so there may be little value to inlining.
1231 *
1232 *------------------------------------------------------------------------
1233 *
1234 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1235 * have requested the GPIO. That can include implicit requesting by
1236 * a direction setting call. Marking a gpio as requested locks its chip
1237 * in memory, guaranteeing that these table lookups need no more locking
1238 * and that gpiochip_remove() will fail.
1239 *
1240 * REVISIT when debugging, consider adding some instrumentation to ensure
1241 * that the GPIO was actually requested.
1242 */
1243
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001244static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001245{
1246 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001247 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001248 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001249
Alexandre Courbot372e7222013-02-03 01:29:29 +09001250 chip = desc->chip;
1251 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001252 value = chip->get ? chip->get(chip, offset) : -EIO;
1253 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001254 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001255 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001256}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001257
David Brownelld2876d02008-02-04 22:28:20 -08001258/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001259 * gpiod_get_raw_value() - return a gpio's raw value
1260 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001261 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001262 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001263 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001264 *
1265 * This function should be called from contexts where we cannot sleep, and will
1266 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001267 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001268int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001269{
David Brownelld2876d02008-02-04 22:28:20 -08001270 if (!desc)
1271 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001272 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001273 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001274 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001275}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001276EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001277
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001278/**
1279 * gpiod_get_value() - return a gpio's value
1280 * @desc: gpio whose value will be returned
1281 *
1282 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001283 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001284 *
1285 * This function should be called from contexts where we cannot sleep, and will
1286 * complain if the GPIO chip functions potentially sleep.
1287 */
1288int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001289{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001290 int value;
1291 if (!desc)
1292 return 0;
1293 /* Should be using gpio_get_value_cansleep() */
1294 WARN_ON(desc->chip->can_sleep);
1295
1296 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001297 if (value < 0)
1298 return value;
1299
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001300 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1301 value = !value;
1302
1303 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001304}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001305EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001306
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301307/*
1308 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001309 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001310 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301311 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001312static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301313{
1314 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001315 struct gpio_chip *chip = desc->chip;
1316 int offset = gpio_chip_hwgpio(desc);
1317
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301318 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001319 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301320 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001321 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301322 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001323 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301324 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001325 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301326 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001327 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301328 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001329 gpiod_err(desc,
1330 "%s: Error in set_value for open drain err %d\n",
1331 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301332}
1333
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301334/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001335 * _gpio_set_open_source_value() - Set the open source gpio's value.
1336 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001337 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301338 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001339static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301340{
1341 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001342 struct gpio_chip *chip = desc->chip;
1343 int offset = gpio_chip_hwgpio(desc);
1344
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301345 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001346 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301347 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001348 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301349 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001350 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301351 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001352 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301353 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001354 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301355 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001356 gpiod_err(desc,
1357 "%s: Error in set_value for open source err %d\n",
1358 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301359}
1360
Alexandre Courbot23600962014-03-11 15:52:09 +09001361static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001362{
1363 struct gpio_chip *chip;
1364
Alexandre Courbot372e7222013-02-03 01:29:29 +09001365 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001366 trace_gpio_value(desc_to_gpio(desc), 0, value);
1367 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1368 _gpio_set_open_drain_value(desc, value);
1369 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1370 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301371 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001372 chip->set(chip, gpio_chip_hwgpio(desc), value);
1373}
1374
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001375/*
1376 * set multiple outputs on the same chip;
1377 * use the chip's set_multiple function if available;
1378 * otherwise set the outputs sequentially;
1379 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1380 * defines which outputs are to be changed
1381 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1382 * defines the values the outputs specified by mask are to be set to
1383 */
1384static void gpio_chip_set_multiple(struct gpio_chip *chip,
1385 unsigned long *mask, unsigned long *bits)
1386{
1387 if (chip->set_multiple) {
1388 chip->set_multiple(chip, mask, bits);
1389 } else {
1390 int i;
1391 for (i = 0; i < chip->ngpio; i++) {
1392 if (mask[BIT_WORD(i)] == 0) {
1393 /* no more set bits in this mask word;
1394 * skip ahead to the next word */
1395 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1396 continue;
1397 }
1398 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001399 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001400 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001401 }
1402 }
1403}
1404
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001405static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1406 unsigned int array_size,
1407 struct gpio_desc **desc_array,
1408 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001409{
1410 int i = 0;
1411
1412 while (i < array_size) {
1413 struct gpio_chip *chip = desc_array[i]->chip;
1414 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1415 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1416 int count = 0;
1417
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001418 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001419 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001420
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001421 memset(mask, 0, sizeof(mask));
1422 do {
1423 struct gpio_desc *desc = desc_array[i];
1424 int hwgpio = gpio_chip_hwgpio(desc);
1425 int value = value_array[i];
1426
1427 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1428 value = !value;
1429 trace_gpio_value(desc_to_gpio(desc), 0, value);
1430 /*
1431 * collect all normal outputs belonging to the same chip
1432 * open drain and open source outputs are set individually
1433 */
1434 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001435 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001436 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1437 _gpio_set_open_source_value(desc, value);
1438 } else {
1439 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001440 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001441 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001442 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001443 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001444 count++;
1445 }
1446 i++;
1447 } while ((i < array_size) && (desc_array[i]->chip == chip));
1448 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001449 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001450 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001451 }
1452}
1453
David Brownelld2876d02008-02-04 22:28:20 -08001454/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001455 * gpiod_set_raw_value() - assign a gpio's raw value
1456 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001457 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001458 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001459 * Set the raw value of the GPIO, i.e. the value of its physical line without
1460 * regard for its ACTIVE_LOW status.
1461 *
1462 * This function should be called from contexts where we cannot sleep, and will
1463 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001464 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001465void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001466{
David Brownelld2876d02008-02-04 22:28:20 -08001467 if (!desc)
1468 return;
David Brownelld2876d02008-02-04 22:28:20 -08001469 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001470 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001471 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001472}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001473EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001474
1475/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001476 * gpiod_set_value() - assign a gpio's value
1477 * @desc: gpio whose value will be assigned
1478 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001479 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001480 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1481 * account
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_value(struct gpio_desc *desc, int value)
1487{
1488 if (!desc)
1489 return;
1490 /* Should be using gpio_set_value_cansleep() */
1491 WARN_ON(desc->chip->can_sleep);
1492 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1493 value = !value;
1494 _gpiod_set_raw_value(desc, value);
1495}
1496EXPORT_SYMBOL_GPL(gpiod_set_value);
1497
1498/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001499 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001500 * @array_size: number of elements in the descriptor / value arrays
1501 * @desc_array: array of GPIO descriptors whose values will be assigned
1502 * @value_array: array of values to assign
1503 *
1504 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1505 * without regard for their ACTIVE_LOW status.
1506 *
1507 * This function should be called from contexts where we cannot sleep, and will
1508 * complain if the GPIO chip functions potentially sleep.
1509 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001510void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001511 struct gpio_desc **desc_array, int *value_array)
1512{
1513 if (!desc_array)
1514 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001515 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1516 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001517}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001518EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001519
1520/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001521 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001522 * @array_size: number of elements in the descriptor / value arrays
1523 * @desc_array: array of GPIO descriptors whose values will be assigned
1524 * @value_array: array of values to assign
1525 *
1526 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1527 * into account.
1528 *
1529 * This function should be called from contexts where we cannot sleep, and will
1530 * complain if the GPIO chip functions potentially sleep.
1531 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001532void gpiod_set_array_value(unsigned int array_size,
1533 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001534{
1535 if (!desc_array)
1536 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001537 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1538 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001539}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001540EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001541
1542/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001543 * gpiod_cansleep() - report whether gpio value access may sleep
1544 * @desc: gpio to check
1545 *
1546 */
1547int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001548{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001549 if (!desc)
1550 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001551 return desc->chip->can_sleep;
1552}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001553EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001554
David Brownell0f6d5042008-10-15 22:03:14 -07001555/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001556 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1557 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001558 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001559 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1560 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001561 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001562int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001563{
1564 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001565 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001566
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001567 if (!desc)
1568 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001569 chip = desc->chip;
1570 offset = gpio_chip_hwgpio(desc);
1571 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1572}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001573EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001574
Linus Walleijd468bf92013-09-24 11:54:38 +02001575/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001576 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001577 * @chip: the chip the GPIO to lock belongs to
1578 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001579 *
1580 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001581 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001582 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001583int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001584{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001585 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001586 return -EINVAL;
1587
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001588 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1589 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001590 "%s: tried to flag a GPIO set as output for IRQ\n",
1591 __func__);
1592 return -EIO;
1593 }
1594
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001595 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001596 return 0;
1597}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001598EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001599
Linus Walleijd468bf92013-09-24 11:54:38 +02001600/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001601 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001602 * @chip: the chip the GPIO to lock belongs to
1603 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001604 *
1605 * This is used directly by GPIO drivers that want to indicate
1606 * that a certain GPIO is no longer used exclusively for IRQ.
1607 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001608void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001609{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001610 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001611 return;
1612
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001613 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001614}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001615EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001616
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001617/**
1618 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1619 * @desc: gpio whose value will be returned
1620 *
1621 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001622 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001623 *
1624 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001625 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001626int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001627{
David Brownelld2876d02008-02-04 22:28:20 -08001628 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001629 if (!desc)
1630 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001631 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001632}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001633EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001634
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001635/**
1636 * gpiod_get_value_cansleep() - return a gpio's value
1637 * @desc: gpio whose value will be returned
1638 *
1639 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001640 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001641 *
1642 * This function is to be called from contexts that can sleep.
1643 */
1644int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001645{
David Brownelld2876d02008-02-04 22:28:20 -08001646 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001647
1648 might_sleep_if(extra_checks);
1649 if (!desc)
1650 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001651
1652 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001653 if (value < 0)
1654 return value;
1655
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001656 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1657 value = !value;
1658
David Brownelld2876d02008-02-04 22:28:20 -08001659 return value;
1660}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001661EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001662
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001663/**
1664 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1665 * @desc: gpio whose value will be assigned
1666 * @value: value to assign
1667 *
1668 * Set the raw value of the GPIO, i.e. the value of its physical line without
1669 * regard for its ACTIVE_LOW status.
1670 *
1671 * This function is to be called from contexts that can sleep.
1672 */
1673void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001674{
David Brownelld2876d02008-02-04 22:28:20 -08001675 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001676 if (!desc)
1677 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001678 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001679}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001680EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001681
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001682/**
1683 * gpiod_set_value_cansleep() - assign a gpio's value
1684 * @desc: gpio whose value will be assigned
1685 * @value: value to assign
1686 *
1687 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1688 * account
1689 *
1690 * This function is to be called from contexts that can sleep.
1691 */
1692void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001693{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001694 might_sleep_if(extra_checks);
1695 if (!desc)
1696 return;
1697
1698 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1699 value = !value;
1700 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001701}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001702EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001703
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001704/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001705 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001706 * @array_size: number of elements in the descriptor / value arrays
1707 * @desc_array: array of GPIO descriptors whose values will be assigned
1708 * @value_array: array of values to assign
1709 *
1710 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1711 * without regard for their ACTIVE_LOW status.
1712 *
1713 * This function is to be called from contexts that can sleep.
1714 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001715void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1716 struct gpio_desc **desc_array,
1717 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001718{
1719 might_sleep_if(extra_checks);
1720 if (!desc_array)
1721 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001722 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1723 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001724}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001725EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001726
1727/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001728 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001729 * @array_size: number of elements in the descriptor / value arrays
1730 * @desc_array: array of GPIO descriptors whose values will be assigned
1731 * @value_array: array of values to assign
1732 *
1733 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1734 * into account.
1735 *
1736 * This function is to be called from contexts that can sleep.
1737 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001738void gpiod_set_array_value_cansleep(unsigned int array_size,
1739 struct gpio_desc **desc_array,
1740 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001741{
1742 might_sleep_if(extra_checks);
1743 if (!desc_array)
1744 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001745 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1746 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001747}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001748EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001749
1750/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001751 * gpiod_add_lookup_table() - register GPIO device consumers
1752 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001753 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001754void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001755{
1756 mutex_lock(&gpio_lookup_lock);
1757
Alexandre Courbotad824782013-12-03 12:20:11 +09001758 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001759
1760 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001761}
1762
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05301763/**
1764 * gpiod_remove_lookup_table() - unregister GPIO device consumers
1765 * @table: table of consumers to unregister
1766 */
1767void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
1768{
1769 mutex_lock(&gpio_lookup_lock);
1770
1771 list_del(&table->list);
1772
1773 mutex_unlock(&gpio_lookup_lock);
1774}
1775
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001776static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001777 unsigned int idx,
1778 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001779{
1780 char prop_name[32]; /* 32 is max size of property name */
1781 enum of_gpio_flags of_flags;
1782 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001783 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001784
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001785 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02001786 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01001787 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001788 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02001789 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01001790 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001791 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001792
Thierry Redingdd34c372014-04-23 17:28:09 +02001793 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1794 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001795 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001796 break;
1797 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001798
1799 if (IS_ERR(desc))
1800 return desc;
1801
1802 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001803 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001804
1805 return desc;
1806}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001807
Mika Westerberg81f59e92013-10-10 11:01:09 +03001808static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001809 unsigned int idx,
1810 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001811{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001812 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03001813 struct acpi_gpio_info info;
1814 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001815 char propname[32];
1816 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03001817
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001818 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001819 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001820 if (con_id && strcmp(con_id, "gpios")) {
1821 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001822 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001823 } else {
1824 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001825 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001826 }
Mika Westerberge01f4402013-10-10 11:01:10 +03001827
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001828 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1829 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1830 break;
1831 }
1832
1833 /* Then from plain _CRS GPIOs */
1834 if (IS_ERR(desc)) {
1835 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1836 if (IS_ERR(desc))
1837 return desc;
1838 }
1839
1840 if (info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001841 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001842
1843 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001844}
1845
Alexandre Courbotad824782013-12-03 12:20:11 +09001846static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1847{
1848 const char *dev_id = dev ? dev_name(dev) : NULL;
1849 struct gpiod_lookup_table *table;
1850
1851 mutex_lock(&gpio_lookup_lock);
1852
1853 list_for_each_entry(table, &gpio_lookup_list, list) {
1854 if (table->dev_id && dev_id) {
1855 /*
1856 * Valid strings on both ends, must be identical to have
1857 * a match
1858 */
1859 if (!strcmp(table->dev_id, dev_id))
1860 goto found;
1861 } else {
1862 /*
1863 * One of the pointers is NULL, so both must be to have
1864 * a match
1865 */
1866 if (dev_id == table->dev_id)
1867 goto found;
1868 }
1869 }
1870 table = NULL;
1871
1872found:
1873 mutex_unlock(&gpio_lookup_lock);
1874 return table;
1875}
1876
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001877static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001878 unsigned int idx,
1879 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001880{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001881 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001882 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001883 struct gpiod_lookup *p;
1884
Alexandre Courbotad824782013-12-03 12:20:11 +09001885 table = gpiod_find_lookup_table(dev);
1886 if (!table)
1887 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001888
Alexandre Courbotad824782013-12-03 12:20:11 +09001889 for (p = &table->table[0]; p->chip_label; p++) {
1890 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001891
Alexandre Courbotad824782013-12-03 12:20:11 +09001892 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001893 if (p->idx != idx)
1894 continue;
1895
Alexandre Courbotad824782013-12-03 12:20:11 +09001896 /* If the lookup entry has a con_id, require exact match */
1897 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1898 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001899
Alexandre Courbotad824782013-12-03 12:20:11 +09001900 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001901
Alexandre Courbotad824782013-12-03 12:20:11 +09001902 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001903 dev_err(dev, "cannot find GPIO chip %s\n",
1904 p->chip_label);
1905 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001906 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001907
Alexandre Courbotad824782013-12-03 12:20:11 +09001908 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001909 dev_err(dev,
1910 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1911 idx, chip->ngpio, chip->label);
1912 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001913 }
1914
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001915 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001916 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001917
1918 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001919 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001920
1921 return desc;
1922}
1923
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001924static int dt_gpio_count(struct device *dev, const char *con_id)
1925{
1926 int ret;
1927 char propname[32];
1928 unsigned int i;
1929
1930 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1931 if (con_id)
1932 snprintf(propname, sizeof(propname), "%s-%s",
1933 con_id, gpio_suffixes[i]);
1934 else
1935 snprintf(propname, sizeof(propname), "%s",
1936 gpio_suffixes[i]);
1937
1938 ret = of_gpio_named_count(dev->of_node, propname);
1939 if (ret >= 0)
1940 break;
1941 }
1942 return ret;
1943}
1944
1945static int platform_gpio_count(struct device *dev, const char *con_id)
1946{
1947 struct gpiod_lookup_table *table;
1948 struct gpiod_lookup *p;
1949 unsigned int count = 0;
1950
1951 table = gpiod_find_lookup_table(dev);
1952 if (!table)
1953 return -ENOENT;
1954
1955 for (p = &table->table[0]; p->chip_label; p++) {
1956 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
1957 (!con_id && !p->con_id))
1958 count++;
1959 }
1960 if (!count)
1961 return -ENOENT;
1962
1963 return count;
1964}
1965
1966/**
1967 * gpiod_count - return the number of GPIOs associated with a device / function
1968 * or -ENOENT if no GPIO has been assigned to the requested function
1969 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1970 * @con_id: function within the GPIO consumer
1971 */
1972int gpiod_count(struct device *dev, const char *con_id)
1973{
1974 int count = -ENOENT;
1975
1976 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
1977 count = dt_gpio_count(dev, con_id);
1978 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
1979 count = acpi_gpio_count(dev, con_id);
1980
1981 if (count < 0)
1982 count = platform_gpio_count(dev, con_id);
1983
1984 return count;
1985}
1986EXPORT_SYMBOL_GPL(gpiod_count);
1987
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001988/**
Thierry Reding08791622014-04-25 16:54:22 +02001989 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09001990 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001991 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001992 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001993 *
1994 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001995 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07001996 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001997 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01001998struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001999 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002000{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002001 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002002}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002003EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002004
2005/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002006 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2007 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2008 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002009 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002010 *
2011 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2012 * the requested function it will return NULL. This is convenient for drivers
2013 * that need to handle optional GPIOs.
2014 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002015struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002016 const char *con_id,
2017 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002018{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002019 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002020}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002021EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002022
Benoit Parrotf625d462015-02-02 11:44:44 -06002023
2024/**
2025 * gpiod_configure_flags - helper function to configure a given GPIO
2026 * @desc: gpio whose value will be assigned
2027 * @con_id: function within the GPIO consumer
2028 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2029 * of_get_gpio_hog()
2030 * @dflags: gpiod_flags - optional GPIO initialization flags
2031 *
2032 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2033 * requested function and/or index, or another IS_ERR() code if an error
2034 * occurred while trying to acquire the GPIO.
2035 */
2036static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
2037 unsigned long lflags, enum gpiod_flags dflags)
2038{
2039 int status;
2040
2041 if (lflags & GPIO_ACTIVE_LOW)
2042 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2043 if (lflags & GPIO_OPEN_DRAIN)
2044 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2045 if (lflags & GPIO_OPEN_SOURCE)
2046 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2047
2048 /* No particular flag request, return here... */
2049 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2050 pr_debug("no flags found for %s\n", con_id);
2051 return 0;
2052 }
2053
2054 /* Process flags */
2055 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2056 status = gpiod_direction_output(desc,
2057 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2058 else
2059 status = gpiod_direction_input(desc);
2060
2061 return status;
2062}
2063
Thierry Reding29a1f2332014-04-25 17:10:06 +02002064/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002065 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002066 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002067 * @con_id: function within the GPIO consumer
2068 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002069 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002070 *
2071 * This variant of gpiod_get() allows to access GPIOs other than the first
2072 * defined one for functions that define several GPIOs.
2073 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002074 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2075 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07002076 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002077 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002078struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002079 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002080 unsigned int idx,
2081 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002082{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002083 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002084 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002085 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002086
2087 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2088
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002089 if (dev) {
2090 /* Using device tree? */
2091 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2092 dev_dbg(dev, "using device tree for GPIO lookup\n");
2093 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2094 } else if (ACPI_COMPANION(dev)) {
2095 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2096 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2097 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002098 }
2099
2100 /*
2101 * Either we are not using DT or ACPI, or their lookup did not return
2102 * a result. In that case, use platform lookup as a fallback.
2103 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002104 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002105 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002106 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002107 }
2108
2109 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002110 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002111 return desc;
2112 }
2113
2114 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002115 if (status < 0)
2116 return ERR_PTR(status);
2117
Benoit Parrotf625d462015-02-02 11:44:44 -06002118 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002119 if (status < 0) {
2120 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2121 gpiod_put(desc);
2122 return ERR_PTR(status);
2123 }
2124
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002125 return desc;
2126}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002127EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002128
2129/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002130 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2131 * @fwnode: handle of the firmware node
2132 * @propname: name of the firmware property representing the GPIO
2133 *
2134 * This function can be used for drivers that get their configuration
2135 * from firmware.
2136 *
2137 * Function properly finds the corresponding GPIO using whatever is the
2138 * underlying firmware interface and then makes sure that the GPIO
2139 * descriptor is requested before it is returned to the caller.
2140 *
2141 * In case of error an ERR_PTR() is returned.
2142 */
2143struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2144 const char *propname)
2145{
2146 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2147 bool active_low = false;
2148 int ret;
2149
2150 if (!fwnode)
2151 return ERR_PTR(-EINVAL);
2152
2153 if (is_of_node(fwnode)) {
2154 enum of_gpio_flags flags;
2155
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002156 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002157 &flags);
2158 if (!IS_ERR(desc))
2159 active_low = flags & OF_GPIO_ACTIVE_LOW;
2160 } else if (is_acpi_node(fwnode)) {
2161 struct acpi_gpio_info info;
2162
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002163 desc = acpi_get_gpiod_by_index(to_acpi_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002164 &info);
2165 if (!IS_ERR(desc))
2166 active_low = info.active_low;
2167 }
2168
2169 if (IS_ERR(desc))
2170 return desc;
2171
2172 ret = gpiod_request(desc, NULL);
2173 if (ret)
2174 return ERR_PTR(ret);
2175
2176 /* Only value flag can be set from both DT and ACPI is active_low */
2177 if (active_low)
2178 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2179
2180 return desc;
2181}
2182EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2183
2184/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002185 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2186 * function
2187 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2188 * @con_id: function within the GPIO consumer
2189 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002190 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002191 *
2192 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2193 * specified index was assigned to the requested function it will return NULL.
2194 * This is convenient for drivers that need to handle optional GPIOs.
2195 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002196struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002197 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002198 unsigned int index,
2199 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002200{
2201 struct gpio_desc *desc;
2202
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002203 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002204 if (IS_ERR(desc)) {
2205 if (PTR_ERR(desc) == -ENOENT)
2206 return NULL;
2207 }
2208
2209 return desc;
2210}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002211EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002212
2213/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002214 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2215 * @desc: gpio whose value will be assigned
2216 * @name: gpio line name
2217 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2218 * of_get_gpio_hog()
2219 * @dflags: gpiod_flags - optional GPIO initialization flags
2220 */
2221int gpiod_hog(struct gpio_desc *desc, const char *name,
2222 unsigned long lflags, enum gpiod_flags dflags)
2223{
2224 struct gpio_chip *chip;
2225 struct gpio_desc *local_desc;
2226 int hwnum;
2227 int status;
2228
2229 chip = gpiod_to_chip(desc);
2230 hwnum = gpio_chip_hwgpio(desc);
2231
2232 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2233 if (IS_ERR(local_desc)) {
Linus Walleija7138902015-06-05 11:36:10 +02002234 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2235 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002236 return PTR_ERR(local_desc);
2237 }
2238
2239 status = gpiod_configure_flags(desc, name, lflags, dflags);
2240 if (status < 0) {
Linus Walleija7138902015-06-05 11:36:10 +02002241 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2242 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002243 gpiochip_free_own_desc(desc);
2244 return status;
2245 }
2246
2247 /* Mark GPIO as hogged so it can be identified and removed later */
2248 set_bit(FLAG_IS_HOGGED, &desc->flags);
2249
2250 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2251 desc_to_gpio(desc), name,
2252 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2253 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2254 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2255
2256 return 0;
2257}
2258
2259/**
2260 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2261 * @chip: gpio chip to act on
2262 *
2263 * This is only used by of_gpiochip_remove to free hogged gpios
2264 */
2265static void gpiochip_free_hogs(struct gpio_chip *chip)
2266{
2267 int id;
2268
2269 for (id = 0; id < chip->ngpio; id++) {
2270 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2271 gpiochip_free_own_desc(&chip->desc[id]);
2272 }
2273}
2274
2275/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002276 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2277 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2278 * @con_id: function within the GPIO consumer
2279 * @flags: optional GPIO initialization flags
2280 *
2281 * This function acquires all the GPIOs defined under a given function.
2282 *
2283 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2284 * no GPIO has been assigned to the requested function, or another IS_ERR()
2285 * code if an error occurred while trying to acquire the GPIOs.
2286 */
2287struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2288 const char *con_id,
2289 enum gpiod_flags flags)
2290{
2291 struct gpio_desc *desc;
2292 struct gpio_descs *descs;
2293 int count;
2294
2295 count = gpiod_count(dev, con_id);
2296 if (count < 0)
2297 return ERR_PTR(count);
2298
2299 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2300 GFP_KERNEL);
2301 if (!descs)
2302 return ERR_PTR(-ENOMEM);
2303
2304 for (descs->ndescs = 0; descs->ndescs < count; ) {
2305 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2306 if (IS_ERR(desc)) {
2307 gpiod_put_array(descs);
2308 return ERR_CAST(desc);
2309 }
2310 descs->desc[descs->ndescs] = desc;
2311 descs->ndescs++;
2312 }
2313 return descs;
2314}
2315EXPORT_SYMBOL_GPL(gpiod_get_array);
2316
2317/**
2318 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2319 * function
2320 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2321 * @con_id: function within the GPIO consumer
2322 * @flags: optional GPIO initialization flags
2323 *
2324 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2325 * assigned to the requested function it will return NULL.
2326 */
2327struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2328 const char *con_id,
2329 enum gpiod_flags flags)
2330{
2331 struct gpio_descs *descs;
2332
2333 descs = gpiod_get_array(dev, con_id, flags);
2334 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2335 return NULL;
2336
2337 return descs;
2338}
2339EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2340
2341/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002342 * gpiod_put - dispose of a GPIO descriptor
2343 * @desc: GPIO descriptor to dispose of
2344 *
2345 * No descriptor can be used after gpiod_put() has been called on it.
2346 */
2347void gpiod_put(struct gpio_desc *desc)
2348{
2349 gpiod_free(desc);
2350}
2351EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002352
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002353/**
2354 * gpiod_put_array - dispose of multiple GPIO descriptors
2355 * @descs: struct gpio_descs containing an array of descriptors
2356 */
2357void gpiod_put_array(struct gpio_descs *descs)
2358{
2359 unsigned int i;
2360
2361 for (i = 0; i < descs->ndescs; i++)
2362 gpiod_put(descs->desc[i]);
2363
2364 kfree(descs);
2365}
2366EXPORT_SYMBOL_GPL(gpiod_put_array);
2367
David Brownelld2876d02008-02-04 22:28:20 -08002368#ifdef CONFIG_DEBUG_FS
2369
2370static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2371{
2372 unsigned i;
2373 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002374 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002375 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002376 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002377
2378 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02002379 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2380 if (gdesc->name) {
2381 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2382 gpio, gdesc->name);
2383 }
David Brownelld2876d02008-02-04 22:28:20 -08002384 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02002385 }
David Brownelld2876d02008-02-04 22:28:20 -08002386
Alexandre Courbot372e7222013-02-03 01:29:29 +09002387 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002388 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002389 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02002390 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2391 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08002392 is_out ? "out" : "in ",
2393 chip->get
2394 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002395 : "? ",
2396 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002397 seq_printf(s, "\n");
2398 }
2399}
2400
Thierry Redingf9c4a312012-04-12 13:26:01 +02002401static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002402{
Grant Likely362432a2013-02-09 09:41:49 +00002403 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002404 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002405 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002406
2407 s->private = "";
2408
Grant Likely362432a2013-02-09 09:41:49 +00002409 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002410 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002411 if (index-- == 0) {
2412 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002413 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002414 }
2415 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002416
2417 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002418}
2419
2420static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2421{
Grant Likely362432a2013-02-09 09:41:49 +00002422 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002423 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002424 void *ret = NULL;
2425
Grant Likely362432a2013-02-09 09:41:49 +00002426 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002427 if (list_is_last(&chip->list, &gpio_chips))
2428 ret = NULL;
2429 else
2430 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002431 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002432
2433 s->private = "\n";
2434 ++*pos;
2435
2436 return ret;
2437}
2438
2439static void gpiolib_seq_stop(struct seq_file *s, void *v)
2440{
2441}
2442
2443static int gpiolib_seq_show(struct seq_file *s, void *v)
2444{
2445 struct gpio_chip *chip = v;
2446 struct device *dev;
2447
2448 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2449 chip->base, chip->base + chip->ngpio - 1);
2450 dev = chip->dev;
2451 if (dev)
2452 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2453 dev_name(dev));
2454 if (chip->label)
2455 seq_printf(s, ", %s", chip->label);
2456 if (chip->can_sleep)
2457 seq_printf(s, ", can sleep");
2458 seq_printf(s, ":\n");
2459
2460 if (chip->dbg_show)
2461 chip->dbg_show(s, chip);
2462 else
2463 gpiolib_dbg_show(s, chip);
2464
David Brownelld2876d02008-02-04 22:28:20 -08002465 return 0;
2466}
2467
Thierry Redingf9c4a312012-04-12 13:26:01 +02002468static const struct seq_operations gpiolib_seq_ops = {
2469 .start = gpiolib_seq_start,
2470 .next = gpiolib_seq_next,
2471 .stop = gpiolib_seq_stop,
2472 .show = gpiolib_seq_show,
2473};
2474
David Brownelld2876d02008-02-04 22:28:20 -08002475static int gpiolib_open(struct inode *inode, struct file *file)
2476{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002477 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002478}
2479
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002480static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002481 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002482 .open = gpiolib_open,
2483 .read = seq_read,
2484 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002485 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002486};
2487
2488static int __init gpiolib_debugfs_init(void)
2489{
2490 /* /sys/kernel/debug/gpio */
2491 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2492 NULL, NULL, &gpiolib_operations);
2493 return 0;
2494}
2495subsys_initcall(gpiolib_debugfs_init);
2496
2497#endif /* DEBUG_FS */