blob: eed70c36e8ac6eb266eefe85a1b72d1a0e02ca4c [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d2008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020018#include <linux/pinctrl/consumer.h>
David Brownelld2876d02008-02-04 22:28:20 -080019
Mika Westerberg664e3e52014-01-08 12:40:54 +020020#include "gpiolib.h"
21
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060022#define CREATE_TRACE_POINTS
23#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080024
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070025/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080026 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070027 * The GPIO programming interface allows for inlining speed-critical
28 * get/set operations for common cases, so that access to SOC-integrated
29 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080030 */
31
32
33/* When debugging, extend minimal trust to callers and platform code.
34 * Also emit diagnostic messages that may help initial bringup, when
35 * board setup or driver bugs are most common.
36 *
37 * Otherwise, minimize overhead in what may be bitbanging codepaths.
38 */
39#ifdef DEBUG
40#define extra_checks 1
41#else
42#define extra_checks 0
43#endif
44
45/* gpio_lock prevents conflicts during gpio_desc[] table updates.
46 * While any GPIO is requested, its gpio_chip is not removable;
47 * each GPIO's "requested" flag serves as a lock and refcount.
48 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090049DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080050
Alexandre Courbotbae48da2013-10-17 10:21:38 -070051static DEFINE_MUTEX(gpio_lookup_lock);
52static LIST_HEAD(gpio_lookup_list);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090053LIST_HEAD(gpio_chips);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +020054
Johan Hovold6d867502015-05-04 17:23:25 +020055
56static void gpiochip_free_hogs(struct gpio_chip *chip);
57static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
58
59
David Brownelld2876d02008-02-04 22:28:20 -080060static inline void desc_set_label(struct gpio_desc *d, const char *label)
61{
David Brownelld2876d02008-02-04 22:28:20 -080062 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080063}
64
Alexandre Courbot372e7222013-02-03 01:29:29 +090065/**
66 * Convert a GPIO number to its descriptor
67 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070068struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090069{
Alexandre Courbot14e85c02014-11-19 16:51:27 +090070 struct gpio_chip *chip;
71 unsigned long flags;
72
73 spin_lock_irqsave(&gpio_lock, flags);
74
75 list_for_each_entry(chip, &gpio_chips, list) {
76 if (chip->base <= gpio && chip->base + chip->ngpio > gpio) {
77 spin_unlock_irqrestore(&gpio_lock, flags);
78 return &chip->desc[gpio - chip->base];
79 }
80 }
81
82 spin_unlock_irqrestore(&gpio_lock, flags);
83
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +090084 if (!gpio_is_valid(gpio))
85 WARN(1, "invalid GPIO %d\n", gpio);
86
Alexandre Courbot14e85c02014-11-19 16:51:27 +090087 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +090088}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070089EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090090
91/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090092 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +020093 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090094struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
95 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +020096{
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090097 if (hwnum >= chip->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +090098 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +020099
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900100 return &chip->desc[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200101}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900102
103/**
104 * Convert a GPIO descriptor to the integer namespace.
105 * This should disappear in the future but is needed since we still
106 * use GPIO numbers for error messages and sysfs nodes
107 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700108int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900109{
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900110 return desc->chip->base + (desc - &desc->chip->desc[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900111}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700112EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900113
114
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700115/**
116 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
117 * @desc: descriptor to return the chip of
118 */
119struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900120{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900121 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900122}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700123EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800124
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700125/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
126static int gpiochip_find_base(int ngpio)
127{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900128 struct gpio_chip *chip;
129 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700130
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900131 list_for_each_entry_reverse(chip, &gpio_chips, list) {
132 /* found a free space? */
133 if (chip->base + chip->ngpio <= base)
134 break;
135 else
136 /* nope, check the space right before the chip */
137 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700138 }
139
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900140 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700141 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900142 return base;
143 } else {
144 pr_err("%s: cannot find free range\n", __func__);
145 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700146 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700147}
148
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700149/**
150 * gpiod_get_direction - return the current direction of a GPIO
151 * @desc: GPIO to get the direction of
152 *
153 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
154 *
155 * This function may sleep if gpiod_cansleep() is true.
156 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900157int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300158{
159 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900160 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300161 int status = -EINVAL;
162
Alexandre Courbot372e7222013-02-03 01:29:29 +0900163 chip = gpiod_to_chip(desc);
164 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300165
166 if (!chip->get_direction)
167 return status;
168
Alexandre Courbot372e7222013-02-03 01:29:29 +0900169 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300170 if (status > 0) {
171 /* GPIOF_DIR_IN, or other positive */
172 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900173 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300174 }
175 if (status == 0) {
176 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900177 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300178 }
179 return status;
180}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700181EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300182
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900183/*
184 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800185 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900186 *
187 * Return -EBUSY if the new chip overlaps with some other chip's integer
188 * space.
189 */
190static int gpiochip_add_to_list(struct gpio_chip *chip)
191{
Masahiro Yamadad1aceb82015-07-21 14:45:40 +0900192 struct list_head *pos;
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800193 struct gpio_chip *iterator;
194 struct gpio_chip *previous = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900195
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800196 if (list_empty(&gpio_chips)) {
197 pos = gpio_chips.next;
198 goto found;
199 }
200
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900201 list_for_each(pos, &gpio_chips) {
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800202 iterator = list_entry(pos, struct gpio_chip, list);
203 if (iterator->base >= chip->base + chip->ngpio) {
204 /*
205 * Iterator is the first GPIO chip so there is no
206 * previous one
207 */
208 if (previous == NULL) {
209 goto found;
210 } else {
211 /*
212 * We found a valid range(means
213 * [base, base + ngpio - 1]) between previous
214 * and iterator chip.
215 */
216 if (previous->base + previous->ngpio
217 <= chip->base)
218 goto found;
219 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900220 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800221 previous = iterator;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900222 }
223
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800224 /* We are beyond the last chip in the list */
225 if (iterator->base + iterator->ngpio <= chip->base)
226 goto found;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900227
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800228 dev_err(chip->parent,
229 "GPIO integer space overlap, cannot add chip\n");
230 return -EBUSY;
231
232found:
233 list_add_tail(&chip->list, pos);
234 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900235}
236
Linus Walleijf881bab2015-09-23 16:20:43 -0700237/**
238 * Convert a GPIO name to its descriptor
239 */
240static struct gpio_desc *gpio_name_to_desc(const char * const name)
241{
242 struct gpio_chip *chip;
243 unsigned long flags;
244
245 spin_lock_irqsave(&gpio_lock, flags);
246
247 list_for_each_entry(chip, &gpio_chips, list) {
248 int i;
249
250 for (i = 0; i != chip->ngpio; ++i) {
251 struct gpio_desc *gpio = &chip->desc[i];
252
253 if (!gpio->name)
254 continue;
255
256 if (!strcmp(gpio->name, name)) {
257 spin_unlock_irqrestore(&gpio_lock, flags);
258 return gpio;
259 }
260 }
261 }
262
263 spin_unlock_irqrestore(&gpio_lock, flags);
264
265 return NULL;
266}
267
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200268/*
269 * Takes the names from gc->names and checks if they are all unique. If they
270 * are, they are assigned to their gpio descriptors.
271 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800272 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200273 */
274static int gpiochip_set_desc_names(struct gpio_chip *gc)
275{
276 int i;
277
278 if (!gc->names)
279 return 0;
280
281 /* First check all names if they are unique */
282 for (i = 0; i != gc->ngpio; ++i) {
283 struct gpio_desc *gpio;
284
285 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700286 if (gpio)
Linus Walleij58383c72015-11-04 09:56:26 +0100287 dev_warn(gc->parent, "Detected name collision for "
Linus Walleijf881bab2015-09-23 16:20:43 -0700288 "GPIO name '%s'\n",
289 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200290 }
291
292 /* Then add all names to the GPIO descriptors */
293 for (i = 0; i != gc->ngpio; ++i)
294 gc->desc[i].name = gc->names[i];
295
296 return 0;
297}
298
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700299/**
David Brownelld2876d02008-02-04 22:28:20 -0800300 * gpiochip_add() - register a gpio_chip
301 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900302 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -0800303 *
304 * Returns a negative errno if the chip can't be registered, such as
305 * because the chip->base is invalid or already associated with a
306 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700307 *
David Brownelld8f388d2008-07-25 01:46:07 -0700308 * When gpiochip_add() is called very early during boot, so that GPIOs
309 * can be freely used, the chip->dev device must be registered before
310 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
311 * for GPIOs will fail rudely.
312 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700313 * If chip->base is negative, this requests dynamic assignment of
314 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800315 */
316int gpiochip_add(struct gpio_chip *chip)
317{
318 unsigned long flags;
319 int status = 0;
320 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700321 int base = chip->base;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900322 struct gpio_desc *descs;
David Brownelld2876d02008-02-04 22:28:20 -0800323
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900324 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
325 if (!descs)
326 return -ENOMEM;
David Brownelld2876d02008-02-04 22:28:20 -0800327
328 spin_lock_irqsave(&gpio_lock, flags);
329
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700330 if (base < 0) {
331 base = gpiochip_find_base(chip->ngpio);
332 if (base < 0) {
333 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100334 spin_unlock_irqrestore(&gpio_lock, flags);
335 goto err_free_descs;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700336 }
337 chip->base = base;
338 }
339
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900340 status = gpiochip_add_to_list(chip);
Johan Hovold05aa5202015-01-12 17:12:26 +0100341 if (status) {
342 spin_unlock_irqrestore(&gpio_lock, flags);
343 goto err_free_descs;
344 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900345
Johan Hovold05aa5202015-01-12 17:12:26 +0100346 for (id = 0; id < chip->ngpio; id++) {
347 struct gpio_desc *desc = &descs[id];
David Brownelld8f388d2008-07-25 01:46:07 -0700348
Johan Hovold05aa5202015-01-12 17:12:26 +0100349 desc->chip = chip;
350
351 /* REVISIT: most hardware initializes GPIOs as inputs (often
352 * with pullups enabled) so power usage is minimized. Linux
353 * code should set the gpio direction first thing; but until
354 * it does, and in case chip->get_direction is not set, we may
355 * expose the wrong direction in sysfs.
356 */
357 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800358 }
359
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900360 chip->desc = descs;
361
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800362 spin_unlock_irqrestore(&gpio_lock, flags);
363
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530364#ifdef CONFIG_PINCTRL
365 INIT_LIST_HEAD(&chip->pin_ranges);
366#endif
367
Linus Walleij58383c72015-11-04 09:56:26 +0100368 if (!chip->owner && chip->parent && chip->parent->driver)
369 chip->owner = chip->parent->driver->owner;
Grygorii Strashko37269602015-06-25 20:30:51 +0300370
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200371 status = gpiochip_set_desc_names(chip);
372 if (status)
373 goto err_remove_from_list;
374
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200375 status = of_gpiochip_add(chip);
376 if (status)
377 goto err_remove_chip;
378
Mika Westerberg664e3e52014-01-08 12:40:54 +0200379 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600380
Johan Hovold426577b2015-05-04 17:10:32 +0200381 status = gpiochip_sysfs_register(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100382 if (status)
383 goto err_remove_chip;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600384
Andy Shevchenko7589e592013-12-05 11:26:23 +0200385 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700386 chip->base, chip->base + chip->ngpio - 1,
387 chip->label ? : "generic");
388
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600389 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800390
Johan Hovold225fce82015-01-12 17:12:25 +0100391err_remove_chip:
392 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200393 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100394 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200395err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100396 spin_lock_irqsave(&gpio_lock, flags);
397 list_del(&chip->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800398 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold05aa5202015-01-12 17:12:26 +0100399 chip->desc = NULL;
Johan Hovold225fce82015-01-12 17:12:25 +0100400err_free_descs:
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900401 kfree(descs);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900402
David Brownelld2876d02008-02-04 22:28:20 -0800403 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200404 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600405 chip->base, chip->base + chip->ngpio - 1,
406 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800407 return status;
408}
409EXPORT_SYMBOL_GPL(gpiochip_add);
410
411/**
412 * gpiochip_remove() - unregister a gpio_chip
413 * @chip: the chip to unregister
414 *
415 * A gpio_chip with any GPIOs still requested may not be removed.
416 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200417void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800418{
Johan Hovoldfab28b82015-05-04 17:10:27 +0200419 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800420 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800421 unsigned id;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200422 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800423
Johan Hovold426577b2015-05-04 17:10:32 +0200424 gpiochip_sysfs_unregister(chip);
Johan Hovold01cca932015-01-12 17:12:29 +0100425
Johan Hovold00acc3d2015-01-12 17:12:27 +0100426 gpiochip_irqchip_remove(chip);
427
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200428 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100429 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600430 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600431 of_gpiochip_remove(chip);
432
Johan Hovold6798aca2015-01-12 17:12:28 +0100433 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900434 for (id = 0; id < chip->ngpio; id++) {
Johan Hovoldfab28b82015-05-04 17:10:27 +0200435 desc = &chip->desc[id];
436 desc->chip = NULL;
437 if (test_bit(FLAG_REQUESTED, &desc->flags))
438 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800439 }
abdoulaye berthee1db1702014-07-05 18:28:50 +0200440 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800441 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900442
Johan Hovoldfab28b82015-05-04 17:10:27 +0200443 if (requested)
Linus Walleij58383c72015-11-04 09:56:26 +0100444 dev_crit(chip->parent,
445 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +0200446
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900447 kfree(chip->desc);
448 chip->desc = NULL;
David Brownelld2876d02008-02-04 22:28:20 -0800449}
450EXPORT_SYMBOL_GPL(gpiochip_remove);
451
Grant Likely594fa262010-06-08 07:48:16 -0600452/**
453 * gpiochip_find() - iterator for locating a specific gpio_chip
454 * @data: data to pass to match function
455 * @callback: Callback function to check gpio_chip
456 *
457 * Similar to bus_find_device. It returns a reference to a gpio_chip as
458 * determined by a user supplied @match callback. The callback should return
459 * 0 if the device doesn't match and non-zero if it does. If the callback is
460 * non-zero, this function will return to the caller and not iterate over any
461 * more gpio_chips.
462 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600463struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700464 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600465 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600466{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900467 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600468 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600469
470 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900471 list_for_each_entry(chip, &gpio_chips, list)
472 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600473 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900474
475 /* No match? */
476 if (&chip->list == &gpio_chips)
477 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600478 spin_unlock_irqrestore(&gpio_lock, flags);
479
480 return chip;
481}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600482EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800483
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900484static int gpiochip_match_name(struct gpio_chip *chip, void *data)
485{
486 const char *name = data;
487
488 return !strcmp(chip->label, name);
489}
490
491static struct gpio_chip *find_chip_by_name(const char *name)
492{
493 return gpiochip_find((void *)name, gpiochip_match_name);
494}
495
Linus Walleij14250522014-03-25 10:40:18 +0100496#ifdef CONFIG_GPIOLIB_IRQCHIP
497
498/*
499 * The following is irqchip helper code for gpiochips.
500 */
501
502/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200503 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
504 * @gpiochip: the gpiochip to set the irqchip chain to
505 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100506 * @parent_irq: the irq number corresponding to the parent IRQ for this
507 * chained irqchip
508 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200509 * coming out of the gpiochip. If the interrupt is nested rather than
510 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100511 */
512void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
513 struct irq_chip *irqchip,
514 int parent_irq,
515 irq_flow_handler_t parent_handler)
516{
Linus Walleij83141a72014-09-26 13:50:12 +0200517 unsigned int offset;
518
Linus Walleij83141a72014-09-26 13:50:12 +0200519 if (!gpiochip->irqdomain) {
520 chip_err(gpiochip, "called %s before setting up irqchip\n",
521 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200522 return;
523 }
524
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200525 if (parent_handler) {
526 if (gpiochip->can_sleep) {
527 chip_err(gpiochip,
528 "you cannot have chained interrupts on a "
529 "chip that may sleep\n");
530 return;
531 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200532 /*
533 * The parent irqchip is already using the chip_data for this
534 * irqchip, so our callbacks simply use the handler_data.
535 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +0200536 irq_set_chained_handler_and_data(parent_irq, parent_handler,
537 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300538
539 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200540 }
Linus Walleij83141a72014-09-26 13:50:12 +0200541
542 /* Set the parent IRQ for all affected IRQs */
543 for (offset = 0; offset < gpiochip->ngpio; offset++)
544 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
545 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100546}
547EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
548
549/**
550 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
551 * @d: the irqdomain used by this irqchip
552 * @irq: the global irq number used by this GPIO irqchip irq
553 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
554 *
555 * This function will set up the mapping for a certain IRQ line on a
556 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
557 * stored inside the gpiochip.
558 */
559static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
560 irq_hw_number_t hwirq)
561{
562 struct gpio_chip *chip = d->host_data;
563
Linus Walleij14250522014-03-25 10:40:18 +0100564 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300565 /*
566 * This lock class tells lockdep that GPIO irqs are in a different
567 * category than their parents, so it won't report false recursion.
568 */
569 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +0200570 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200571 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300572 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200573 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100574 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -0500575
Linus Walleij1333b902014-04-23 16:45:12 +0200576 /*
577 * No set-up of the hardware will happen if IRQ_TYPE_NONE
578 * is passed as default type.
579 */
580 if (chip->irq_default_type != IRQ_TYPE_NONE)
581 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100582
583 return 0;
584}
585
Linus Walleijc3626fd2014-03-28 20:42:01 +0100586static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
587{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200588 struct gpio_chip *chip = d->host_data;
589
Linus Walleij1c8732b2014-04-09 13:34:39 +0200590 if (chip->can_sleep)
591 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100592 irq_set_chip_and_handler(irq, NULL, NULL);
593 irq_set_chip_data(irq, NULL);
594}
595
Linus Walleij14250522014-03-25 10:40:18 +0100596static const struct irq_domain_ops gpiochip_domain_ops = {
597 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100598 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100599 /* Virtually all GPIO irqchips are twocell:ed */
600 .xlate = irq_domain_xlate_twocell,
601};
602
603static int gpiochip_irq_reqres(struct irq_data *d)
604{
605 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
606
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300607 if (!try_module_get(chip->owner))
608 return -ENODEV;
609
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900610 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100611 chip_err(chip,
612 "unable to lock HW IRQ %lu for IRQ\n",
613 d->hwirq);
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300614 module_put(chip->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100615 return -EINVAL;
616 }
617 return 0;
618}
619
620static void gpiochip_irq_relres(struct irq_data *d)
621{
622 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
623
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900624 gpiochip_unlock_as_irq(chip, d->hwirq);
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300625 module_put(chip->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100626}
627
628static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
629{
630 return irq_find_mapping(chip->irqdomain, offset);
631}
632
633/**
634 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
635 * @gpiochip: the gpiochip to remove the irqchip from
636 *
637 * This is called only from gpiochip_remove()
638 */
639static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
640{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100641 unsigned int offset;
642
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300643 acpi_gpiochip_free_interrupts(gpiochip);
644
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300645 if (gpiochip->irq_parent) {
646 irq_set_chained_handler(gpiochip->irq_parent, NULL);
647 irq_set_handler_data(gpiochip->irq_parent, NULL);
648 }
649
Linus Walleijc3626fd2014-03-28 20:42:01 +0100650 /* Remove all IRQ mappings and delete the domain */
651 if (gpiochip->irqdomain) {
652 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300653 irq_dispose_mapping(
654 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100655 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100656 }
Linus Walleij14250522014-03-25 10:40:18 +0100657
658 if (gpiochip->irqchip) {
659 gpiochip->irqchip->irq_request_resources = NULL;
660 gpiochip->irqchip->irq_release_resources = NULL;
661 gpiochip->irqchip = NULL;
662 }
663}
664
665/**
666 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
667 * @gpiochip: the gpiochip to add the irqchip to
668 * @irqchip: the irqchip to add to the gpiochip
669 * @first_irq: if not dynamically assigned, the base (first) IRQ to
670 * allocate gpiochip irqs from
671 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200672 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
673 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300674 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +0100675 *
676 * This function closely associates a certain irqchip with a certain
677 * gpiochip, providing an irq domain to translate the local IRQs to
678 * global irqs in the gpiolib core, and making sure that the gpiochip
679 * is passed as chip data to all related functions. Driver callbacks
680 * need to use container_of() to get their local state containers back
681 * from the gpiochip passed as chip data. An irqdomain will be stored
682 * in the gpiochip that shall be used by the driver to handle IRQ number
683 * translation. The gpiochip will need to be initialized and registered
684 * before calling this function.
685 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100686 * This function will handle two cell:ed simple IRQs and assumes all
687 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100688 * need to be open coded.
689 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300690int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
691 struct irq_chip *irqchip,
692 unsigned int first_irq,
693 irq_flow_handler_t handler,
694 unsigned int type,
695 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +0100696{
697 struct device_node *of_node;
698 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100699 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100700
701 if (!gpiochip || !irqchip)
702 return -EINVAL;
703
Linus Walleij58383c72015-11-04 09:56:26 +0100704 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +0100705 pr_err("missing gpiochip .dev parent pointer\n");
706 return -EINVAL;
707 }
Linus Walleij58383c72015-11-04 09:56:26 +0100708 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +0100709#ifdef CONFIG_OF_GPIO
710 /*
Colin Cronin20a8a962015-05-18 11:41:43 -0700711 * If the gpiochip has an assigned OF node this takes precedence
Linus Walleij14250522014-03-25 10:40:18 +0100712 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
713 */
714 if (gpiochip->of_node)
715 of_node = gpiochip->of_node;
716#endif
717 gpiochip->irqchip = irqchip;
718 gpiochip->irq_handler = handler;
719 gpiochip->irq_default_type = type;
720 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300721 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +0100722 gpiochip->irqdomain = irq_domain_add_simple(of_node,
723 gpiochip->ngpio, first_irq,
724 &gpiochip_domain_ops, gpiochip);
725 if (!gpiochip->irqdomain) {
726 gpiochip->irqchip = NULL;
727 return -EINVAL;
728 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +0200729
730 /*
731 * It is possible for a driver to override this, but only if the
732 * alternative functions are both implemented.
733 */
734 if (!irqchip->irq_request_resources &&
735 !irqchip->irq_release_resources) {
736 irqchip->irq_request_resources = gpiochip_irq_reqres;
737 irqchip->irq_release_resources = gpiochip_irq_relres;
738 }
Linus Walleij14250522014-03-25 10:40:18 +0100739
740 /*
741 * Prepare the mapping since the irqchip shall be orthogonal to
742 * any gpiochip calls. If the first_irq was zero, this is
743 * necessary to allocate descriptors for all IRQs.
744 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100745 for (offset = 0; offset < gpiochip->ngpio; offset++) {
746 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
747 if (offset == 0)
748 /*
749 * Store the base into the gpiochip to be used when
750 * unmapping the irqs.
751 */
752 gpiochip->irq_base = irq_base;
753 }
Linus Walleij14250522014-03-25 10:40:18 +0100754
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300755 acpi_gpiochip_request_interrupts(gpiochip);
756
Linus Walleij14250522014-03-25 10:40:18 +0100757 return 0;
758}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300759EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +0100760
761#else /* CONFIG_GPIOLIB_IRQCHIP */
762
763static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
764
765#endif /* CONFIG_GPIOLIB_IRQCHIP */
766
Jonas Gorskic771c2f2015-10-11 17:34:15 +0200767/**
768 * gpiochip_generic_request() - request the gpio function for a pin
769 * @chip: the gpiochip owning the GPIO
770 * @offset: the offset of the GPIO to request for GPIO function
771 */
772int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
773{
774 return pinctrl_request_gpio(chip->base + offset);
775}
776EXPORT_SYMBOL_GPL(gpiochip_generic_request);
777
778/**
779 * gpiochip_generic_free() - free the gpio function from a pin
780 * @chip: the gpiochip to request the gpio function for
781 * @offset: the offset of the GPIO to free from GPIO function
782 */
783void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
784{
785 pinctrl_free_gpio(chip->base + offset);
786}
787EXPORT_SYMBOL_GPL(gpiochip_generic_free);
788
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530789#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100790
Linus Walleij3f0f8672012-11-20 12:40:15 +0100791/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200792 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
793 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +0200794 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +0200795 * @gpio_offset: the start offset in the current gpio_chip number space
796 * @pin_group: name of the pin group inside the pin controller
797 */
798int gpiochip_add_pingroup_range(struct gpio_chip *chip,
799 struct pinctrl_dev *pctldev,
800 unsigned int gpio_offset, const char *pin_group)
801{
802 struct gpio_pin_range *pin_range;
803 int ret;
804
805 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
806 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200807 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200808 return -ENOMEM;
809 }
810
811 /* Use local offset as range ID */
812 pin_range->range.id = gpio_offset;
813 pin_range->range.gc = chip;
814 pin_range->range.name = chip->label;
815 pin_range->range.base = chip->base + gpio_offset;
816 pin_range->pctldev = pctldev;
817
818 ret = pinctrl_get_group_pins(pctldev, pin_group,
819 &pin_range->range.pins,
820 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100821 if (ret < 0) {
822 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200823 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100824 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200825
826 pinctrl_add_gpio_range(pctldev, &pin_range->range);
827
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200828 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
829 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200830 pinctrl_dev_get_devname(pctldev), pin_group);
831
832 list_add_tail(&pin_range->node, &chip->pin_ranges);
833
834 return 0;
835}
836EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
837
838/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100839 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
840 * @chip: the gpiochip to add the range for
841 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100842 * @gpio_offset: the start offset in the current gpio_chip number space
843 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100844 * @npins: the number of pins from the offset of each pin space (GPIO and
845 * pin controller) to accumulate in this range
846 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100847int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100848 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100849 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530850{
851 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800852 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530853
Linus Walleij3f0f8672012-11-20 12:40:15 +0100854 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530855 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200856 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100857 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530858 }
859
Linus Walleij3f0f8672012-11-20 12:40:15 +0100860 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100861 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100862 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530863 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100864 pin_range->range.base = chip->base + gpio_offset;
865 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530866 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100867 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530868 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100869 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800870 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200871 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100872 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800873 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100874 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200875 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
876 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100877 pinctl_name,
878 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530879
880 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100881
882 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530883}
Linus Walleij165adc92012-11-06 14:49:39 +0100884EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530885
Linus Walleij3f0f8672012-11-20 12:40:15 +0100886/**
887 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
888 * @chip: the chip to remove all the mappings for
889 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530890void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
891{
892 struct gpio_pin_range *pin_range, *tmp;
893
894 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
895 list_del(&pin_range->node);
896 pinctrl_remove_gpio_range(pin_range->pctldev,
897 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100898 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530899 }
900}
Linus Walleij165adc92012-11-06 14:49:39 +0100901EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
902
903#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530904
David Brownelld2876d02008-02-04 22:28:20 -0800905/* These "optional" allocation calls help prevent drivers from stomping
906 * on each other, and help provide better diagnostics in debugfs.
907 * They're called even less than the "set direction" calls.
908 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200909static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800910{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200911 struct gpio_chip *chip = desc->chip;
912 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800913 unsigned long flags;
914
915 spin_lock_irqsave(&gpio_lock, flags);
916
David Brownelld2876d02008-02-04 22:28:20 -0800917 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700918 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800919 */
920
921 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
922 desc_set_label(desc, label ? : "?");
923 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700924 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800925 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800926 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700927 }
928
929 if (chip->request) {
930 /* chip->request may sleep */
931 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900932 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700933 spin_lock_irqsave(&gpio_lock, flags);
934
935 if (status < 0) {
936 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700937 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300938 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700939 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700940 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300941 if (chip->get_direction) {
942 /* chip->get_direction may sleep */
943 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900944 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300945 spin_lock_irqsave(&gpio_lock, flags);
946 }
David Brownelld2876d02008-02-04 22:28:20 -0800947done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +0300948 if (status < 0) {
949 /* Clear flags that might have been set by the caller before
950 * requesting the GPIO.
951 */
952 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
953 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
954 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
955 }
Mika Westerberg77c2d792014-03-10 14:54:50 +0200956 spin_unlock_irqrestore(&gpio_lock, flags);
957 return status;
958}
959
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900960int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200961{
962 int status = -EPROBE_DEFER;
963 struct gpio_chip *chip;
964
965 if (!desc) {
966 pr_warn("%s: invalid GPIO\n", __func__);
967 return -EINVAL;
968 }
969
970 chip = desc->chip;
971 if (!chip)
972 goto done;
973
974 if (try_module_get(chip->owner)) {
975 status = __gpiod_request(desc, label);
976 if (status < 0)
977 module_put(chip->owner);
978 }
979
980done:
David Brownelld2876d02008-02-04 22:28:20 -0800981 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200982 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200983
David Brownelld2876d02008-02-04 22:28:20 -0800984 return status;
985}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900986
Mika Westerberg77c2d792014-03-10 14:54:50 +0200987static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800988{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200989 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -0800990 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -0700991 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800992
Uwe Kleine-König3d599d12008-10-15 22:03:12 -0700993 might_sleep();
994
Alexandre Courbot372e7222013-02-03 01:29:29 +0900995 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700996
David Brownelld2876d02008-02-04 22:28:20 -0800997 spin_lock_irqsave(&gpio_lock, flags);
998
David Brownell35e8bb52008-10-15 22:03:16 -0700999 chip = desc->chip;
1000 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1001 if (chip->free) {
1002 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001003 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001004 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001005 spin_lock_irqsave(&gpio_lock, flags);
1006 }
David Brownelld2876d02008-02-04 22:28:20 -08001007 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001008 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001009 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301010 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301011 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001012 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001013 ret = true;
1014 }
David Brownelld2876d02008-02-04 22:28:20 -08001015
1016 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001017 return ret;
1018}
1019
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001020void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001021{
1022 if (desc && __gpiod_free(desc))
1023 module_put(desc->chip->owner);
1024 else
1025 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -08001026}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001027
David Brownelld2876d02008-02-04 22:28:20 -08001028/**
1029 * gpiochip_is_requested - return string iff signal was requested
1030 * @chip: controller managing the signal
1031 * @offset: of signal within controller's 0..(ngpio - 1) range
1032 *
1033 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001034 * The string returned is the label passed to gpio_request(); if none has been
1035 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001036 *
1037 * This function is for use by GPIO controller drivers. The label can
1038 * help with diagnostics, and knowing that the signal is used as a GPIO
1039 * can help avoid accidentally multiplexing it to another controller.
1040 */
1041const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1042{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001043 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001044
Dirk Behme48b59532015-08-18 18:02:32 +02001045 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001046 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001047
1048 desc = &chip->desc[offset];
1049
Alexandre Courbot372e7222013-02-03 01:29:29 +09001050 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001051 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001052 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001053}
1054EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1055
Mika Westerberg77c2d792014-03-10 14:54:50 +02001056/**
1057 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1058 * @desc: GPIO descriptor to request
1059 * @label: label for the GPIO
1060 *
1061 * Function allows GPIO chip drivers to request and use their own GPIO
1062 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1063 * function will not increase reference count of the GPIO chip module. This
1064 * allows the GPIO chip module to be unloaded as needed (we assume that the
1065 * GPIO chip driver handles freeing the GPIOs it has requested).
1066 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001067struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1068 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001069{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001070 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1071 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001072
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001073 if (IS_ERR(desc)) {
1074 chip_err(chip, "failed to get GPIO descriptor\n");
1075 return desc;
1076 }
1077
1078 err = __gpiod_request(desc, label);
1079 if (err < 0)
1080 return ERR_PTR(err);
1081
1082 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001083}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001084EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001085
1086/**
1087 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1088 * @desc: GPIO descriptor to free
1089 *
1090 * Function frees the given GPIO requested previously with
1091 * gpiochip_request_own_desc().
1092 */
1093void gpiochip_free_own_desc(struct gpio_desc *desc)
1094{
1095 if (desc)
1096 __gpiod_free(desc);
1097}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001098EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08001099
1100/* Drivers MUST set GPIO direction before making get/set calls. In
1101 * some cases this is done in early boot, before IRQs are enabled.
1102 *
1103 * As a rule these aren't called more than once (except for drivers
1104 * using the open-drain emulation idiom) so these are natural places
1105 * to accumulate extra debugging checks. Note that we can't (yet)
1106 * rely on gpio_request() having been called beforehand.
1107 */
1108
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001109/**
1110 * gpiod_direction_input - set the GPIO direction to input
1111 * @desc: GPIO to set to input
1112 *
1113 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1114 * be called safely on it.
1115 *
1116 * Return 0 in case of success, else an error code.
1117 */
1118int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001119{
David Brownelld2876d02008-02-04 22:28:20 -08001120 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001121 int status = -EINVAL;
1122
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001123 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001124 pr_warn("%s: invalid GPIO\n", __func__);
1125 return -EINVAL;
1126 }
1127
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001128 chip = desc->chip;
1129 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001130 gpiod_warn(desc,
1131 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001132 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001133 return -EIO;
1134 }
1135
Alexandre Courbotd82da792014-07-22 16:17:43 +09001136 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001137 if (status == 0)
1138 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001139
Alexandre Courbot372e7222013-02-03 01:29:29 +09001140 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001141
David Brownelld2876d02008-02-04 22:28:20 -08001142 return status;
1143}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001144EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001145
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001146static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001147{
David Brownelld2876d02008-02-04 22:28:20 -08001148 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001149 int status = -EINVAL;
1150
Linus Walleijd468bf92013-09-24 11:54:38 +02001151 /* GPIOs used for IRQs shall not be set as output */
1152 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1153 gpiod_err(desc,
1154 "%s: tried to set a GPIO tied to an IRQ as output\n",
1155 __func__);
1156 return -EIO;
1157 }
1158
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301159 /* Open drain pin should not be driven to 1 */
1160 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001161 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301162
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301163 /* Open source pin should not be driven to 0 */
1164 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001165 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301166
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001167 chip = desc->chip;
1168 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001169 gpiod_warn(desc,
1170 "%s: missing set() or direction_output() operations\n",
1171 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001172 return -EIO;
1173 }
1174
Alexandre Courbotd82da792014-07-22 16:17:43 +09001175 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001176 if (status == 0)
1177 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001178 trace_gpio_value(desc_to_gpio(desc), 0, value);
1179 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001180 return status;
1181}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001182
1183/**
1184 * gpiod_direction_output_raw - set the GPIO direction to output
1185 * @desc: GPIO to set to output
1186 * @value: initial output value of the GPIO
1187 *
1188 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1189 * be called safely on it. The initial value of the output must be specified
1190 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1191 *
1192 * Return 0 in case of success, else an error code.
1193 */
1194int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1195{
1196 if (!desc || !desc->chip) {
1197 pr_warn("%s: invalid GPIO\n", __func__);
1198 return -EINVAL;
1199 }
1200 return _gpiod_direction_output_raw(desc, value);
1201}
1202EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1203
1204/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301205 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001206 * @desc: GPIO to set to output
1207 * @value: initial output value of the GPIO
1208 *
1209 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1210 * be called safely on it. The initial value of the output must be specified
1211 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1212 * account.
1213 *
1214 * Return 0 in case of success, else an error code.
1215 */
1216int gpiod_direction_output(struct gpio_desc *desc, int value)
1217{
1218 if (!desc || !desc->chip) {
1219 pr_warn("%s: invalid GPIO\n", __func__);
1220 return -EINVAL;
1221 }
1222 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1223 value = !value;
1224 return _gpiod_direction_output_raw(desc, value);
1225}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001226EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001227
Felipe Balbic4b5be92010-05-26 14:42:23 -07001228/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001229 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001230 * @gpio: the gpio to set debounce time
1231 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001232 *
1233 * returns -ENOTSUPP if the controller does not support setting
1234 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001235 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001236int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001237{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001238 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001239
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001240 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001241 pr_warn("%s: invalid GPIO\n", __func__);
1242 return -EINVAL;
1243 }
1244
Felipe Balbic4b5be92010-05-26 14:42:23 -07001245 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001246 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001247 gpiod_dbg(desc,
1248 "%s: missing set() or set_debounce() operations\n",
1249 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001250 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001251 }
1252
Alexandre Courbotd82da792014-07-22 16:17:43 +09001253 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001254}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001255EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001256
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001257/**
1258 * gpiod_is_active_low - test whether a GPIO is active-low or not
1259 * @desc: the gpio descriptor to test
1260 *
1261 * Returns 1 if the GPIO is active-low, 0 otherwise.
1262 */
1263int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001264{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001265 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001266}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001267EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001268
1269/* I/O calls are only valid after configuration completed; the relevant
1270 * "is this a valid GPIO" error checks should already have been done.
1271 *
1272 * "Get" operations are often inlinable as reading a pin value register,
1273 * and masking the relevant bit in that register.
1274 *
1275 * When "set" operations are inlinable, they involve writing that mask to
1276 * one register to set a low value, or a different register to set it high.
1277 * Otherwise locking is needed, so there may be little value to inlining.
1278 *
1279 *------------------------------------------------------------------------
1280 *
1281 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1282 * have requested the GPIO. That can include implicit requesting by
1283 * a direction setting call. Marking a gpio as requested locks its chip
1284 * in memory, guaranteeing that these table lookups need no more locking
1285 * and that gpiochip_remove() will fail.
1286 *
1287 * REVISIT when debugging, consider adding some instrumentation to ensure
1288 * that the GPIO was actually requested.
1289 */
1290
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001291static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001292{
1293 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001294 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001295 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001296
Alexandre Courbot372e7222013-02-03 01:29:29 +09001297 chip = desc->chip;
1298 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001299 value = chip->get ? chip->get(chip, offset) : -EIO;
1300 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001301 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001302 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001303}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001304
David Brownelld2876d02008-02-04 22:28:20 -08001305/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001306 * gpiod_get_raw_value() - return a gpio's raw value
1307 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001308 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001309 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001310 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001311 *
1312 * This function should be called from contexts where we cannot sleep, and will
1313 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001314 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001315int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001316{
David Brownelld2876d02008-02-04 22:28:20 -08001317 if (!desc)
1318 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001319 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001320 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001321 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001322}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001323EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001324
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001325/**
1326 * gpiod_get_value() - return a gpio's value
1327 * @desc: gpio whose value will be returned
1328 *
1329 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001330 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001331 *
1332 * This function should be called from contexts where we cannot sleep, and will
1333 * complain if the GPIO chip functions potentially sleep.
1334 */
1335int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001336{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001337 int value;
1338 if (!desc)
1339 return 0;
1340 /* Should be using gpio_get_value_cansleep() */
1341 WARN_ON(desc->chip->can_sleep);
1342
1343 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001344 if (value < 0)
1345 return value;
1346
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001347 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1348 value = !value;
1349
1350 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001351}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001352EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001353
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301354/*
1355 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001356 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001357 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301358 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001359static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301360{
1361 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001362 struct gpio_chip *chip = desc->chip;
1363 int offset = gpio_chip_hwgpio(desc);
1364
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301365 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001366 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301367 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001368 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301369 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001370 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301371 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001372 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301373 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001374 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301375 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001376 gpiod_err(desc,
1377 "%s: Error in set_value for open drain err %d\n",
1378 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301379}
1380
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301381/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001382 * _gpio_set_open_source_value() - Set the open source gpio's value.
1383 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001384 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301385 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001386static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301387{
1388 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001389 struct gpio_chip *chip = desc->chip;
1390 int offset = gpio_chip_hwgpio(desc);
1391
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301392 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001393 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301394 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001395 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301396 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001397 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301398 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001399 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301400 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001401 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301402 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001403 gpiod_err(desc,
1404 "%s: Error in set_value for open source err %d\n",
1405 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301406}
1407
Alexandre Courbot23600962014-03-11 15:52:09 +09001408static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001409{
1410 struct gpio_chip *chip;
1411
Alexandre Courbot372e7222013-02-03 01:29:29 +09001412 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001413 trace_gpio_value(desc_to_gpio(desc), 0, value);
1414 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1415 _gpio_set_open_drain_value(desc, value);
1416 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1417 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301418 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001419 chip->set(chip, gpio_chip_hwgpio(desc), value);
1420}
1421
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001422/*
1423 * set multiple outputs on the same chip;
1424 * use the chip's set_multiple function if available;
1425 * otherwise set the outputs sequentially;
1426 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1427 * defines which outputs are to be changed
1428 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1429 * defines the values the outputs specified by mask are to be set to
1430 */
1431static void gpio_chip_set_multiple(struct gpio_chip *chip,
1432 unsigned long *mask, unsigned long *bits)
1433{
1434 if (chip->set_multiple) {
1435 chip->set_multiple(chip, mask, bits);
1436 } else {
1437 int i;
1438 for (i = 0; i < chip->ngpio; i++) {
1439 if (mask[BIT_WORD(i)] == 0) {
1440 /* no more set bits in this mask word;
1441 * skip ahead to the next word */
1442 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1443 continue;
1444 }
1445 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001446 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001447 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001448 }
1449 }
1450}
1451
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001452static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1453 unsigned int array_size,
1454 struct gpio_desc **desc_array,
1455 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001456{
1457 int i = 0;
1458
1459 while (i < array_size) {
1460 struct gpio_chip *chip = desc_array[i]->chip;
1461 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1462 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1463 int count = 0;
1464
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001465 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001466 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001467
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001468 memset(mask, 0, sizeof(mask));
1469 do {
1470 struct gpio_desc *desc = desc_array[i];
1471 int hwgpio = gpio_chip_hwgpio(desc);
1472 int value = value_array[i];
1473
1474 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1475 value = !value;
1476 trace_gpio_value(desc_to_gpio(desc), 0, value);
1477 /*
1478 * collect all normal outputs belonging to the same chip
1479 * open drain and open source outputs are set individually
1480 */
1481 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001482 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001483 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1484 _gpio_set_open_source_value(desc, value);
1485 } else {
1486 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001487 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001488 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001489 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001490 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001491 count++;
1492 }
1493 i++;
1494 } while ((i < array_size) && (desc_array[i]->chip == chip));
1495 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001496 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001497 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001498 }
1499}
1500
David Brownelld2876d02008-02-04 22:28:20 -08001501/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001502 * gpiod_set_raw_value() - assign a gpio's raw value
1503 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001504 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001505 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001506 * Set the raw value of the GPIO, i.e. the value of its physical line without
1507 * regard for its ACTIVE_LOW status.
1508 *
1509 * This function should be called from contexts where we cannot sleep, and will
1510 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001511 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001512void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001513{
David Brownelld2876d02008-02-04 22:28:20 -08001514 if (!desc)
1515 return;
David Brownelld2876d02008-02-04 22:28:20 -08001516 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001517 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001518 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001519}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001520EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001521
1522/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001523 * gpiod_set_value() - assign a gpio's value
1524 * @desc: gpio whose value will be assigned
1525 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001526 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001527 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1528 * account
1529 *
1530 * This function should be called from contexts where we cannot sleep, and will
1531 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001532 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001533void gpiod_set_value(struct gpio_desc *desc, int value)
1534{
1535 if (!desc)
1536 return;
1537 /* Should be using gpio_set_value_cansleep() */
1538 WARN_ON(desc->chip->can_sleep);
1539 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1540 value = !value;
1541 _gpiod_set_raw_value(desc, value);
1542}
1543EXPORT_SYMBOL_GPL(gpiod_set_value);
1544
1545/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001546 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001547 * @array_size: number of elements in the descriptor / value arrays
1548 * @desc_array: array of GPIO descriptors whose values will be assigned
1549 * @value_array: array of values to assign
1550 *
1551 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1552 * without regard for their ACTIVE_LOW status.
1553 *
1554 * This function should be called from contexts where we cannot sleep, and will
1555 * complain if the GPIO chip functions potentially sleep.
1556 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001557void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001558 struct gpio_desc **desc_array, int *value_array)
1559{
1560 if (!desc_array)
1561 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001562 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1563 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001564}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001565EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001566
1567/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001568 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001569 * @array_size: number of elements in the descriptor / value arrays
1570 * @desc_array: array of GPIO descriptors whose values will be assigned
1571 * @value_array: array of values to assign
1572 *
1573 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1574 * into account.
1575 *
1576 * This function should be called from contexts where we cannot sleep, and will
1577 * complain if the GPIO chip functions potentially sleep.
1578 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001579void gpiod_set_array_value(unsigned int array_size,
1580 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001581{
1582 if (!desc_array)
1583 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001584 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1585 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001586}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001587EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001588
1589/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001590 * gpiod_cansleep() - report whether gpio value access may sleep
1591 * @desc: gpio to check
1592 *
1593 */
1594int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001595{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001596 if (!desc)
1597 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001598 return desc->chip->can_sleep;
1599}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001600EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001601
David Brownell0f6d5042008-10-15 22:03:14 -07001602/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001603 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1604 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001605 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001606 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1607 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001608 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001609int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001610{
1611 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001612 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001613
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001614 if (!desc)
1615 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001616 chip = desc->chip;
1617 offset = gpio_chip_hwgpio(desc);
1618 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1619}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001620EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001621
Linus Walleijd468bf92013-09-24 11:54:38 +02001622/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001623 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001624 * @chip: the chip the GPIO to lock belongs to
1625 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001626 *
1627 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001628 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001629 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001630int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001631{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001632 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001633 return -EINVAL;
1634
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001635 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1636 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001637 "%s: tried to flag a GPIO set as output for IRQ\n",
1638 __func__);
1639 return -EIO;
1640 }
1641
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001642 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001643 return 0;
1644}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001645EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001646
Linus Walleijd468bf92013-09-24 11:54:38 +02001647/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001648 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001649 * @chip: the chip the GPIO to lock belongs to
1650 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001651 *
1652 * This is used directly by GPIO drivers that want to indicate
1653 * that a certain GPIO is no longer used exclusively for IRQ.
1654 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001655void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001656{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001657 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001658 return;
1659
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001660 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001661}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001662EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001663
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001664/**
1665 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1666 * @desc: gpio whose value will be returned
1667 *
1668 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001669 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001670 *
1671 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001672 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001673int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001674{
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 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001678 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001679}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001680EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001681
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001682/**
1683 * gpiod_get_value_cansleep() - return a gpio's value
1684 * @desc: gpio whose value will be returned
1685 *
1686 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001687 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001688 *
1689 * This function is to be called from contexts that can sleep.
1690 */
1691int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001692{
David Brownelld2876d02008-02-04 22:28:20 -08001693 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001694
1695 might_sleep_if(extra_checks);
1696 if (!desc)
1697 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001698
1699 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001700 if (value < 0)
1701 return value;
1702
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001703 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1704 value = !value;
1705
David Brownelld2876d02008-02-04 22:28:20 -08001706 return value;
1707}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001708EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001709
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001710/**
1711 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1712 * @desc: gpio whose value will be assigned
1713 * @value: value to assign
1714 *
1715 * Set the raw value of the GPIO, i.e. the value of its physical line without
1716 * regard for its ACTIVE_LOW status.
1717 *
1718 * This function is to be called from contexts that can sleep.
1719 */
1720void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001721{
David Brownelld2876d02008-02-04 22:28:20 -08001722 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001723 if (!desc)
1724 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001725 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001726}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001727EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001728
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001729/**
1730 * gpiod_set_value_cansleep() - assign a gpio's value
1731 * @desc: gpio whose value will be assigned
1732 * @value: value to assign
1733 *
1734 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1735 * account
1736 *
1737 * This function is to be called from contexts that can sleep.
1738 */
1739void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001740{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001741 might_sleep_if(extra_checks);
1742 if (!desc)
1743 return;
1744
1745 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1746 value = !value;
1747 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001748}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001749EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001750
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001751/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001752 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001753 * @array_size: number of elements in the descriptor / value arrays
1754 * @desc_array: array of GPIO descriptors whose values will be assigned
1755 * @value_array: array of values to assign
1756 *
1757 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1758 * without regard for their ACTIVE_LOW status.
1759 *
1760 * This function is to be called from contexts that can sleep.
1761 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001762void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1763 struct gpio_desc **desc_array,
1764 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001765{
1766 might_sleep_if(extra_checks);
1767 if (!desc_array)
1768 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001769 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1770 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001771}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001772EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001773
1774/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001775 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001776 * @array_size: number of elements in the descriptor / value arrays
1777 * @desc_array: array of GPIO descriptors whose values will be assigned
1778 * @value_array: array of values to assign
1779 *
1780 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1781 * into account.
1782 *
1783 * This function is to be called from contexts that can sleep.
1784 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001785void gpiod_set_array_value_cansleep(unsigned int array_size,
1786 struct gpio_desc **desc_array,
1787 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001788{
1789 might_sleep_if(extra_checks);
1790 if (!desc_array)
1791 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001792 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1793 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001794}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001795EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001796
1797/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001798 * gpiod_add_lookup_table() - register GPIO device consumers
1799 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001800 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001801void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001802{
1803 mutex_lock(&gpio_lookup_lock);
1804
Alexandre Courbotad824782013-12-03 12:20:11 +09001805 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001806
1807 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001808}
1809
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05301810/**
1811 * gpiod_remove_lookup_table() - unregister GPIO device consumers
1812 * @table: table of consumers to unregister
1813 */
1814void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
1815{
1816 mutex_lock(&gpio_lookup_lock);
1817
1818 list_del(&table->list);
1819
1820 mutex_unlock(&gpio_lookup_lock);
1821}
1822
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001823static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001824 unsigned int idx,
1825 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001826{
1827 char prop_name[32]; /* 32 is max size of property name */
1828 enum of_gpio_flags of_flags;
1829 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001830 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001831
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001832 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02001833 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01001834 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001835 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02001836 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01001837 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001838 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001839
Thierry Redingdd34c372014-04-23 17:28:09 +02001840 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1841 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001842 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001843 break;
1844 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001845
1846 if (IS_ERR(desc))
1847 return desc;
1848
1849 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001850 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001851
Laurent Pinchart90b665f2015-10-13 00:20:21 +03001852 if (of_flags & OF_GPIO_SINGLE_ENDED) {
1853 if (of_flags & OF_GPIO_ACTIVE_LOW)
1854 *flags |= GPIO_OPEN_DRAIN;
1855 else
1856 *flags |= GPIO_OPEN_SOURCE;
1857 }
1858
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001859 return desc;
1860}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001861
Mika Westerberg81f59e92013-10-10 11:01:09 +03001862static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001863 unsigned int idx,
1864 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001865{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001866 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03001867 struct acpi_gpio_info info;
1868 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001869 char propname[32];
1870 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03001871
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001872 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001873 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001874 if (con_id && strcmp(con_id, "gpios")) {
1875 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001876 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001877 } else {
1878 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001879 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001880 }
Mika Westerberge01f4402013-10-10 11:01:10 +03001881
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001882 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1883 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1884 break;
1885 }
1886
1887 /* Then from plain _CRS GPIOs */
1888 if (IS_ERR(desc)) {
Dmitry Torokhov9c3c9bc2015-11-11 11:45:30 -08001889 if (!acpi_can_fallback_to_crs(adev, con_id))
1890 return ERR_PTR(-ENOENT);
1891
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001892 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1893 if (IS_ERR(desc))
1894 return desc;
1895 }
1896
1897 if (info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001898 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001899
1900 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001901}
1902
Alexandre Courbotad824782013-12-03 12:20:11 +09001903static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1904{
1905 const char *dev_id = dev ? dev_name(dev) : NULL;
1906 struct gpiod_lookup_table *table;
1907
1908 mutex_lock(&gpio_lookup_lock);
1909
1910 list_for_each_entry(table, &gpio_lookup_list, list) {
1911 if (table->dev_id && dev_id) {
1912 /*
1913 * Valid strings on both ends, must be identical to have
1914 * a match
1915 */
1916 if (!strcmp(table->dev_id, dev_id))
1917 goto found;
1918 } else {
1919 /*
1920 * One of the pointers is NULL, so both must be to have
1921 * a match
1922 */
1923 if (dev_id == table->dev_id)
1924 goto found;
1925 }
1926 }
1927 table = NULL;
1928
1929found:
1930 mutex_unlock(&gpio_lookup_lock);
1931 return table;
1932}
1933
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001934static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001935 unsigned int idx,
1936 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001937{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001938 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001939 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001940 struct gpiod_lookup *p;
1941
Alexandre Courbotad824782013-12-03 12:20:11 +09001942 table = gpiod_find_lookup_table(dev);
1943 if (!table)
1944 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001945
Alexandre Courbotad824782013-12-03 12:20:11 +09001946 for (p = &table->table[0]; p->chip_label; p++) {
1947 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001948
Alexandre Courbotad824782013-12-03 12:20:11 +09001949 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001950 if (p->idx != idx)
1951 continue;
1952
Alexandre Courbotad824782013-12-03 12:20:11 +09001953 /* If the lookup entry has a con_id, require exact match */
1954 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1955 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001956
Alexandre Courbotad824782013-12-03 12:20:11 +09001957 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001958
Alexandre Courbotad824782013-12-03 12:20:11 +09001959 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001960 dev_err(dev, "cannot find GPIO chip %s\n",
1961 p->chip_label);
1962 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001963 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001964
Alexandre Courbotad824782013-12-03 12:20:11 +09001965 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001966 dev_err(dev,
1967 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1968 idx, chip->ngpio, chip->label);
1969 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001970 }
1971
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001972 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001973 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001974
1975 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001976 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001977
1978 return desc;
1979}
1980
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001981static int dt_gpio_count(struct device *dev, const char *con_id)
1982{
1983 int ret;
1984 char propname[32];
1985 unsigned int i;
1986
1987 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1988 if (con_id)
1989 snprintf(propname, sizeof(propname), "%s-%s",
1990 con_id, gpio_suffixes[i]);
1991 else
1992 snprintf(propname, sizeof(propname), "%s",
1993 gpio_suffixes[i]);
1994
1995 ret = of_gpio_named_count(dev->of_node, propname);
1996 if (ret >= 0)
1997 break;
1998 }
1999 return ret;
2000}
2001
2002static int platform_gpio_count(struct device *dev, const char *con_id)
2003{
2004 struct gpiod_lookup_table *table;
2005 struct gpiod_lookup *p;
2006 unsigned int count = 0;
2007
2008 table = gpiod_find_lookup_table(dev);
2009 if (!table)
2010 return -ENOENT;
2011
2012 for (p = &table->table[0]; p->chip_label; p++) {
2013 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2014 (!con_id && !p->con_id))
2015 count++;
2016 }
2017 if (!count)
2018 return -ENOENT;
2019
2020 return count;
2021}
2022
2023/**
2024 * gpiod_count - return the number of GPIOs associated with a device / function
2025 * or -ENOENT if no GPIO has been assigned to the requested function
2026 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2027 * @con_id: function within the GPIO consumer
2028 */
2029int gpiod_count(struct device *dev, const char *con_id)
2030{
2031 int count = -ENOENT;
2032
2033 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2034 count = dt_gpio_count(dev, con_id);
2035 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2036 count = acpi_gpio_count(dev, con_id);
2037
2038 if (count < 0)
2039 count = platform_gpio_count(dev, con_id);
2040
2041 return count;
2042}
2043EXPORT_SYMBOL_GPL(gpiod_count);
2044
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002045/**
Thierry Reding08791622014-04-25 16:54:22 +02002046 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002047 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002048 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002049 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002050 *
2051 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002052 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07002053 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002054 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002055struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002056 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002057{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002058 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002059}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002060EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002061
2062/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002063 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2064 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2065 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002066 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002067 *
2068 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2069 * the requested function it will return NULL. This is convenient for drivers
2070 * that need to handle optional GPIOs.
2071 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002072struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002073 const char *con_id,
2074 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002075{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002076 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002077}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002078EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002079
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002080/**
2081 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2082 * @desc: gpio to be setup
2083 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2084 * of_get_gpio_hog()
2085 *
2086 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2087 */
2088static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2089{
2090 if (lflags & GPIO_ACTIVE_LOW)
2091 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2092 if (lflags & GPIO_OPEN_DRAIN)
2093 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2094 if (lflags & GPIO_OPEN_SOURCE)
2095 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2096}
Benoit Parrotf625d462015-02-02 11:44:44 -06002097
2098/**
2099 * gpiod_configure_flags - helper function to configure a given GPIO
2100 * @desc: gpio whose value will be assigned
2101 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06002102 * @dflags: gpiod_flags - optional GPIO initialization flags
2103 *
2104 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2105 * requested function and/or index, or another IS_ERR() code if an error
2106 * occurred while trying to acquire the GPIO.
2107 */
2108static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002109 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06002110{
2111 int status;
2112
Benoit Parrotf625d462015-02-02 11:44:44 -06002113 /* No particular flag request, return here... */
2114 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2115 pr_debug("no flags found for %s\n", con_id);
2116 return 0;
2117 }
2118
2119 /* Process flags */
2120 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2121 status = gpiod_direction_output(desc,
2122 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2123 else
2124 status = gpiod_direction_input(desc);
2125
2126 return status;
2127}
2128
Thierry Reding29a1f2332014-04-25 17:10:06 +02002129/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002130 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002131 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002132 * @con_id: function within the GPIO consumer
2133 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002134 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002135 *
2136 * This variant of gpiod_get() allows to access GPIOs other than the first
2137 * defined one for functions that define several GPIOs.
2138 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002139 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2140 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07002141 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002142 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002143struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002144 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002145 unsigned int idx,
2146 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002147{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002148 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002149 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002150 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002151
2152 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2153
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002154 if (dev) {
2155 /* Using device tree? */
2156 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2157 dev_dbg(dev, "using device tree for GPIO lookup\n");
2158 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2159 } else if (ACPI_COMPANION(dev)) {
2160 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2161 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2162 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002163 }
2164
2165 /*
2166 * Either we are not using DT or ACPI, or their lookup did not return
2167 * a result. In that case, use platform lookup as a fallback.
2168 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002169 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002170 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002171 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002172 }
2173
2174 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002175 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002176 return desc;
2177 }
2178
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002179 gpiod_parse_flags(desc, lookupflags);
2180
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002181 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002182 if (status < 0)
2183 return ERR_PTR(status);
2184
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002185 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002186 if (status < 0) {
2187 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2188 gpiod_put(desc);
2189 return ERR_PTR(status);
2190 }
2191
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002192 return desc;
2193}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002194EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002195
2196/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002197 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2198 * @fwnode: handle of the firmware node
2199 * @propname: name of the firmware property representing the GPIO
2200 *
2201 * This function can be used for drivers that get their configuration
2202 * from firmware.
2203 *
2204 * Function properly finds the corresponding GPIO using whatever is the
2205 * underlying firmware interface and then makes sure that the GPIO
2206 * descriptor is requested before it is returned to the caller.
2207 *
2208 * In case of error an ERR_PTR() is returned.
2209 */
2210struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2211 const char *propname)
2212{
2213 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2214 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002215 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02002216 int ret;
2217
2218 if (!fwnode)
2219 return ERR_PTR(-EINVAL);
2220
2221 if (is_of_node(fwnode)) {
2222 enum of_gpio_flags flags;
2223
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002224 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002225 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002226 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02002227 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002228 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2229 }
Mika Westerberg40b73182014-10-21 13:33:59 +02002230 } else if (is_acpi_node(fwnode)) {
2231 struct acpi_gpio_info info;
2232
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02002233 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02002234 if (!IS_ERR(desc))
2235 active_low = info.active_low;
2236 }
2237
2238 if (IS_ERR(desc))
2239 return desc;
2240
Mika Westerberg40b73182014-10-21 13:33:59 +02002241 if (active_low)
2242 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2243
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002244 if (single_ended) {
2245 if (active_low)
2246 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2247 else
2248 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2249 }
2250
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002251 ret = gpiod_request(desc, NULL);
2252 if (ret)
2253 return ERR_PTR(ret);
2254
Mika Westerberg40b73182014-10-21 13:33:59 +02002255 return desc;
2256}
2257EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2258
2259/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002260 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2261 * function
2262 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2263 * @con_id: function within the GPIO consumer
2264 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002265 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002266 *
2267 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2268 * specified index was assigned to the requested function it will return NULL.
2269 * This is convenient for drivers that need to handle optional GPIOs.
2270 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002271struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002272 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002273 unsigned int index,
2274 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002275{
2276 struct gpio_desc *desc;
2277
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002278 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002279 if (IS_ERR(desc)) {
2280 if (PTR_ERR(desc) == -ENOENT)
2281 return NULL;
2282 }
2283
2284 return desc;
2285}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002286EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002287
2288/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002289 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2290 * @desc: gpio whose value will be assigned
2291 * @name: gpio line name
2292 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2293 * of_get_gpio_hog()
2294 * @dflags: gpiod_flags - optional GPIO initialization flags
2295 */
2296int gpiod_hog(struct gpio_desc *desc, const char *name,
2297 unsigned long lflags, enum gpiod_flags dflags)
2298{
2299 struct gpio_chip *chip;
2300 struct gpio_desc *local_desc;
2301 int hwnum;
2302 int status;
2303
2304 chip = gpiod_to_chip(desc);
2305 hwnum = gpio_chip_hwgpio(desc);
2306
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002307 gpiod_parse_flags(desc, lflags);
2308
Benoit Parrotf625d462015-02-02 11:44:44 -06002309 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2310 if (IS_ERR(local_desc)) {
Linus Walleija7138902015-06-05 11:36:10 +02002311 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2312 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002313 return PTR_ERR(local_desc);
2314 }
2315
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002316 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002317 if (status < 0) {
Linus Walleija7138902015-06-05 11:36:10 +02002318 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2319 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002320 gpiochip_free_own_desc(desc);
2321 return status;
2322 }
2323
2324 /* Mark GPIO as hogged so it can be identified and removed later */
2325 set_bit(FLAG_IS_HOGGED, &desc->flags);
2326
2327 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2328 desc_to_gpio(desc), name,
2329 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2330 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2331 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2332
2333 return 0;
2334}
2335
2336/**
2337 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2338 * @chip: gpio chip to act on
2339 *
2340 * This is only used by of_gpiochip_remove to free hogged gpios
2341 */
2342static void gpiochip_free_hogs(struct gpio_chip *chip)
2343{
2344 int id;
2345
2346 for (id = 0; id < chip->ngpio; id++) {
2347 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2348 gpiochip_free_own_desc(&chip->desc[id]);
2349 }
2350}
2351
2352/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002353 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2354 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2355 * @con_id: function within the GPIO consumer
2356 * @flags: optional GPIO initialization flags
2357 *
2358 * This function acquires all the GPIOs defined under a given function.
2359 *
2360 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2361 * no GPIO has been assigned to the requested function, or another IS_ERR()
2362 * code if an error occurred while trying to acquire the GPIOs.
2363 */
2364struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2365 const char *con_id,
2366 enum gpiod_flags flags)
2367{
2368 struct gpio_desc *desc;
2369 struct gpio_descs *descs;
2370 int count;
2371
2372 count = gpiod_count(dev, con_id);
2373 if (count < 0)
2374 return ERR_PTR(count);
2375
2376 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2377 GFP_KERNEL);
2378 if (!descs)
2379 return ERR_PTR(-ENOMEM);
2380
2381 for (descs->ndescs = 0; descs->ndescs < count; ) {
2382 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2383 if (IS_ERR(desc)) {
2384 gpiod_put_array(descs);
2385 return ERR_CAST(desc);
2386 }
2387 descs->desc[descs->ndescs] = desc;
2388 descs->ndescs++;
2389 }
2390 return descs;
2391}
2392EXPORT_SYMBOL_GPL(gpiod_get_array);
2393
2394/**
2395 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2396 * function
2397 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2398 * @con_id: function within the GPIO consumer
2399 * @flags: optional GPIO initialization flags
2400 *
2401 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2402 * assigned to the requested function it will return NULL.
2403 */
2404struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2405 const char *con_id,
2406 enum gpiod_flags flags)
2407{
2408 struct gpio_descs *descs;
2409
2410 descs = gpiod_get_array(dev, con_id, flags);
2411 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2412 return NULL;
2413
2414 return descs;
2415}
2416EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2417
2418/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002419 * gpiod_put - dispose of a GPIO descriptor
2420 * @desc: GPIO descriptor to dispose of
2421 *
2422 * No descriptor can be used after gpiod_put() has been called on it.
2423 */
2424void gpiod_put(struct gpio_desc *desc)
2425{
2426 gpiod_free(desc);
2427}
2428EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002429
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002430/**
2431 * gpiod_put_array - dispose of multiple GPIO descriptors
2432 * @descs: struct gpio_descs containing an array of descriptors
2433 */
2434void gpiod_put_array(struct gpio_descs *descs)
2435{
2436 unsigned int i;
2437
2438 for (i = 0; i < descs->ndescs; i++)
2439 gpiod_put(descs->desc[i]);
2440
2441 kfree(descs);
2442}
2443EXPORT_SYMBOL_GPL(gpiod_put_array);
2444
David Brownelld2876d02008-02-04 22:28:20 -08002445#ifdef CONFIG_DEBUG_FS
2446
2447static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2448{
2449 unsigned i;
2450 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002451 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002452 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002453 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002454
2455 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02002456 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2457 if (gdesc->name) {
2458 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2459 gpio, gdesc->name);
2460 }
David Brownelld2876d02008-02-04 22:28:20 -08002461 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02002462 }
David Brownelld2876d02008-02-04 22:28:20 -08002463
Alexandre Courbot372e7222013-02-03 01:29:29 +09002464 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002465 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002466 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02002467 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2468 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08002469 is_out ? "out" : "in ",
2470 chip->get
2471 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002472 : "? ",
2473 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002474 seq_printf(s, "\n");
2475 }
2476}
2477
Thierry Redingf9c4a312012-04-12 13:26:01 +02002478static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002479{
Grant Likely362432a2013-02-09 09:41:49 +00002480 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002481 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002482 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002483
2484 s->private = "";
2485
Grant Likely362432a2013-02-09 09:41:49 +00002486 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002487 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002488 if (index-- == 0) {
2489 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002490 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002491 }
2492 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002493
2494 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002495}
2496
2497static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2498{
Grant Likely362432a2013-02-09 09:41:49 +00002499 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002500 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002501 void *ret = NULL;
2502
Grant Likely362432a2013-02-09 09:41:49 +00002503 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002504 if (list_is_last(&chip->list, &gpio_chips))
2505 ret = NULL;
2506 else
2507 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002508 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002509
2510 s->private = "\n";
2511 ++*pos;
2512
2513 return ret;
2514}
2515
2516static void gpiolib_seq_stop(struct seq_file *s, void *v)
2517{
2518}
2519
2520static int gpiolib_seq_show(struct seq_file *s, void *v)
2521{
2522 struct gpio_chip *chip = v;
2523 struct device *dev;
2524
2525 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2526 chip->base, chip->base + chip->ngpio - 1);
Linus Walleij58383c72015-11-04 09:56:26 +01002527 dev = chip->parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002528 if (dev)
2529 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2530 dev_name(dev));
2531 if (chip->label)
2532 seq_printf(s, ", %s", chip->label);
2533 if (chip->can_sleep)
2534 seq_printf(s, ", can sleep");
2535 seq_printf(s, ":\n");
2536
2537 if (chip->dbg_show)
2538 chip->dbg_show(s, chip);
2539 else
2540 gpiolib_dbg_show(s, chip);
2541
David Brownelld2876d02008-02-04 22:28:20 -08002542 return 0;
2543}
2544
Thierry Redingf9c4a312012-04-12 13:26:01 +02002545static const struct seq_operations gpiolib_seq_ops = {
2546 .start = gpiolib_seq_start,
2547 .next = gpiolib_seq_next,
2548 .stop = gpiolib_seq_stop,
2549 .show = gpiolib_seq_show,
2550};
2551
David Brownelld2876d02008-02-04 22:28:20 -08002552static int gpiolib_open(struct inode *inode, struct file *file)
2553{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002554 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002555}
2556
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002557static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002558 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002559 .open = gpiolib_open,
2560 .read = seq_read,
2561 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002562 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002563};
2564
2565static int __init gpiolib_debugfs_init(void)
2566{
2567 /* /sys/kernel/debug/gpio */
2568 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2569 NULL, NULL, &gpiolib_operations);
2570 return 0;
2571}
2572subsys_initcall(gpiolib_debugfs_init);
2573
2574#endif /* DEBUG_FS */