blob: 89e01538e40ee845176460539551f2992b05eece [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
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800328 if (chip->ngpio == 0) {
329 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
330 return -EINVAL;
331 }
332
David Brownelld2876d02008-02-04 22:28:20 -0800333 spin_lock_irqsave(&gpio_lock, flags);
334
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700335 if (base < 0) {
336 base = gpiochip_find_base(chip->ngpio);
337 if (base < 0) {
338 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100339 spin_unlock_irqrestore(&gpio_lock, flags);
340 goto err_free_descs;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700341 }
342 chip->base = base;
343 }
344
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900345 status = gpiochip_add_to_list(chip);
Johan Hovold05aa5202015-01-12 17:12:26 +0100346 if (status) {
347 spin_unlock_irqrestore(&gpio_lock, flags);
348 goto err_free_descs;
349 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900350
Johan Hovold05aa5202015-01-12 17:12:26 +0100351 for (id = 0; id < chip->ngpio; id++) {
352 struct gpio_desc *desc = &descs[id];
David Brownelld8f388d2008-07-25 01:46:07 -0700353
Johan Hovold05aa5202015-01-12 17:12:26 +0100354 desc->chip = chip;
355
356 /* REVISIT: most hardware initializes GPIOs as inputs (often
357 * with pullups enabled) so power usage is minimized. Linux
358 * code should set the gpio direction first thing; but until
359 * it does, and in case chip->get_direction is not set, we may
360 * expose the wrong direction in sysfs.
361 */
362 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800363 }
364
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900365 chip->desc = descs;
366
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800367 spin_unlock_irqrestore(&gpio_lock, flags);
368
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530369#ifdef CONFIG_PINCTRL
370 INIT_LIST_HEAD(&chip->pin_ranges);
371#endif
372
Linus Walleij58383c72015-11-04 09:56:26 +0100373 if (!chip->owner && chip->parent && chip->parent->driver)
374 chip->owner = chip->parent->driver->owner;
Grygorii Strashko37269602015-06-25 20:30:51 +0300375
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200376 status = gpiochip_set_desc_names(chip);
377 if (status)
378 goto err_remove_from_list;
379
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200380 status = of_gpiochip_add(chip);
381 if (status)
382 goto err_remove_chip;
383
Mika Westerberg664e3e52014-01-08 12:40:54 +0200384 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600385
Johan Hovold426577b2015-05-04 17:10:32 +0200386 status = gpiochip_sysfs_register(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100387 if (status)
388 goto err_remove_chip;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600389
Andy Shevchenko7589e592013-12-05 11:26:23 +0200390 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700391 chip->base, chip->base + chip->ngpio - 1,
392 chip->label ? : "generic");
393
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600394 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800395
Johan Hovold225fce82015-01-12 17:12:25 +0100396err_remove_chip:
397 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200398 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100399 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200400err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100401 spin_lock_irqsave(&gpio_lock, flags);
402 list_del(&chip->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800403 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold05aa5202015-01-12 17:12:26 +0100404 chip->desc = NULL;
Johan Hovold225fce82015-01-12 17:12:25 +0100405err_free_descs:
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900406 kfree(descs);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900407
David Brownelld2876d02008-02-04 22:28:20 -0800408 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200409 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600410 chip->base, chip->base + chip->ngpio - 1,
411 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800412 return status;
413}
414EXPORT_SYMBOL_GPL(gpiochip_add);
415
416/**
417 * gpiochip_remove() - unregister a gpio_chip
418 * @chip: the chip to unregister
419 *
420 * A gpio_chip with any GPIOs still requested may not be removed.
421 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200422void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800423{
Johan Hovoldfab28b82015-05-04 17:10:27 +0200424 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800425 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800426 unsigned id;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200427 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800428
Johan Hovold426577b2015-05-04 17:10:32 +0200429 gpiochip_sysfs_unregister(chip);
Johan Hovold01cca932015-01-12 17:12:29 +0100430
Johan Hovold00acc3d2015-01-12 17:12:27 +0100431 gpiochip_irqchip_remove(chip);
432
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200433 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100434 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600435 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600436 of_gpiochip_remove(chip);
437
Johan Hovold6798aca2015-01-12 17:12:28 +0100438 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900439 for (id = 0; id < chip->ngpio; id++) {
Johan Hovoldfab28b82015-05-04 17:10:27 +0200440 desc = &chip->desc[id];
441 desc->chip = NULL;
442 if (test_bit(FLAG_REQUESTED, &desc->flags))
443 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800444 }
abdoulaye berthee1db1702014-07-05 18:28:50 +0200445 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800446 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900447
Johan Hovoldfab28b82015-05-04 17:10:27 +0200448 if (requested)
Linus Walleij58383c72015-11-04 09:56:26 +0100449 dev_crit(chip->parent,
450 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +0200451
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900452 kfree(chip->desc);
453 chip->desc = NULL;
David Brownelld2876d02008-02-04 22:28:20 -0800454}
455EXPORT_SYMBOL_GPL(gpiochip_remove);
456
Grant Likely594fa262010-06-08 07:48:16 -0600457/**
458 * gpiochip_find() - iterator for locating a specific gpio_chip
459 * @data: data to pass to match function
460 * @callback: Callback function to check gpio_chip
461 *
462 * Similar to bus_find_device. It returns a reference to a gpio_chip as
463 * determined by a user supplied @match callback. The callback should return
464 * 0 if the device doesn't match and non-zero if it does. If the callback is
465 * non-zero, this function will return to the caller and not iterate over any
466 * more gpio_chips.
467 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600468struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700469 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600470 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600471{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900472 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600473 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600474
475 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900476 list_for_each_entry(chip, &gpio_chips, list)
477 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600478 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900479
480 /* No match? */
481 if (&chip->list == &gpio_chips)
482 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600483 spin_unlock_irqrestore(&gpio_lock, flags);
484
485 return chip;
486}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600487EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800488
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900489static int gpiochip_match_name(struct gpio_chip *chip, void *data)
490{
491 const char *name = data;
492
493 return !strcmp(chip->label, name);
494}
495
496static struct gpio_chip *find_chip_by_name(const char *name)
497{
498 return gpiochip_find((void *)name, gpiochip_match_name);
499}
500
Linus Walleij14250522014-03-25 10:40:18 +0100501#ifdef CONFIG_GPIOLIB_IRQCHIP
502
503/*
504 * The following is irqchip helper code for gpiochips.
505 */
506
507/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200508 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
509 * @gpiochip: the gpiochip to set the irqchip chain to
510 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100511 * @parent_irq: the irq number corresponding to the parent IRQ for this
512 * chained irqchip
513 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200514 * coming out of the gpiochip. If the interrupt is nested rather than
515 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100516 */
517void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
518 struct irq_chip *irqchip,
519 int parent_irq,
520 irq_flow_handler_t parent_handler)
521{
Linus Walleij83141a72014-09-26 13:50:12 +0200522 unsigned int offset;
523
Linus Walleij83141a72014-09-26 13:50:12 +0200524 if (!gpiochip->irqdomain) {
525 chip_err(gpiochip, "called %s before setting up irqchip\n",
526 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200527 return;
528 }
529
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200530 if (parent_handler) {
531 if (gpiochip->can_sleep) {
532 chip_err(gpiochip,
533 "you cannot have chained interrupts on a "
534 "chip that may sleep\n");
535 return;
536 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200537 /*
538 * The parent irqchip is already using the chip_data for this
539 * irqchip, so our callbacks simply use the handler_data.
540 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +0200541 irq_set_chained_handler_and_data(parent_irq, parent_handler,
542 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300543
544 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200545 }
Linus Walleij83141a72014-09-26 13:50:12 +0200546
547 /* Set the parent IRQ for all affected IRQs */
548 for (offset = 0; offset < gpiochip->ngpio; offset++)
549 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
550 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100551}
552EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
553
554/**
555 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
556 * @d: the irqdomain used by this irqchip
557 * @irq: the global irq number used by this GPIO irqchip irq
558 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
559 *
560 * This function will set up the mapping for a certain IRQ line on a
561 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
562 * stored inside the gpiochip.
563 */
564static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
565 irq_hw_number_t hwirq)
566{
567 struct gpio_chip *chip = d->host_data;
568
Linus Walleij14250522014-03-25 10:40:18 +0100569 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300570 /*
571 * This lock class tells lockdep that GPIO irqs are in a different
572 * category than their parents, so it won't report false recursion.
573 */
574 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +0200575 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200576 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300577 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200578 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100579 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -0500580
Linus Walleij1333b902014-04-23 16:45:12 +0200581 /*
582 * No set-up of the hardware will happen if IRQ_TYPE_NONE
583 * is passed as default type.
584 */
585 if (chip->irq_default_type != IRQ_TYPE_NONE)
586 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100587
588 return 0;
589}
590
Linus Walleijc3626fd2014-03-28 20:42:01 +0100591static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
592{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200593 struct gpio_chip *chip = d->host_data;
594
Linus Walleij1c8732b2014-04-09 13:34:39 +0200595 if (chip->can_sleep)
596 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100597 irq_set_chip_and_handler(irq, NULL, NULL);
598 irq_set_chip_data(irq, NULL);
599}
600
Linus Walleij14250522014-03-25 10:40:18 +0100601static const struct irq_domain_ops gpiochip_domain_ops = {
602 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100603 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100604 /* Virtually all GPIO irqchips are twocell:ed */
605 .xlate = irq_domain_xlate_twocell,
606};
607
608static int gpiochip_irq_reqres(struct irq_data *d)
609{
610 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
611
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300612 if (!try_module_get(chip->owner))
613 return -ENODEV;
614
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900615 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100616 chip_err(chip,
617 "unable to lock HW IRQ %lu for IRQ\n",
618 d->hwirq);
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300619 module_put(chip->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100620 return -EINVAL;
621 }
622 return 0;
623}
624
625static void gpiochip_irq_relres(struct irq_data *d)
626{
627 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
628
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900629 gpiochip_unlock_as_irq(chip, d->hwirq);
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300630 module_put(chip->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100631}
632
633static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
634{
635 return irq_find_mapping(chip->irqdomain, offset);
636}
637
638/**
639 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
640 * @gpiochip: the gpiochip to remove the irqchip from
641 *
642 * This is called only from gpiochip_remove()
643 */
644static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
645{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100646 unsigned int offset;
647
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300648 acpi_gpiochip_free_interrupts(gpiochip);
649
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300650 if (gpiochip->irq_parent) {
651 irq_set_chained_handler(gpiochip->irq_parent, NULL);
652 irq_set_handler_data(gpiochip->irq_parent, NULL);
653 }
654
Linus Walleijc3626fd2014-03-28 20:42:01 +0100655 /* Remove all IRQ mappings and delete the domain */
656 if (gpiochip->irqdomain) {
657 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300658 irq_dispose_mapping(
659 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100660 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100661 }
Linus Walleij14250522014-03-25 10:40:18 +0100662
663 if (gpiochip->irqchip) {
664 gpiochip->irqchip->irq_request_resources = NULL;
665 gpiochip->irqchip->irq_release_resources = NULL;
666 gpiochip->irqchip = NULL;
667 }
668}
669
670/**
671 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
672 * @gpiochip: the gpiochip to add the irqchip to
673 * @irqchip: the irqchip to add to the gpiochip
674 * @first_irq: if not dynamically assigned, the base (first) IRQ to
675 * allocate gpiochip irqs from
676 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200677 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
678 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300679 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +0100680 *
681 * This function closely associates a certain irqchip with a certain
682 * gpiochip, providing an irq domain to translate the local IRQs to
683 * global irqs in the gpiolib core, and making sure that the gpiochip
684 * is passed as chip data to all related functions. Driver callbacks
685 * need to use container_of() to get their local state containers back
686 * from the gpiochip passed as chip data. An irqdomain will be stored
687 * in the gpiochip that shall be used by the driver to handle IRQ number
688 * translation. The gpiochip will need to be initialized and registered
689 * before calling this function.
690 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100691 * This function will handle two cell:ed simple IRQs and assumes all
692 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100693 * need to be open coded.
694 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300695int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
696 struct irq_chip *irqchip,
697 unsigned int first_irq,
698 irq_flow_handler_t handler,
699 unsigned int type,
700 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +0100701{
702 struct device_node *of_node;
703 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100704 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100705
706 if (!gpiochip || !irqchip)
707 return -EINVAL;
708
Linus Walleij58383c72015-11-04 09:56:26 +0100709 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +0100710 pr_err("missing gpiochip .dev parent pointer\n");
711 return -EINVAL;
712 }
Linus Walleij58383c72015-11-04 09:56:26 +0100713 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +0100714#ifdef CONFIG_OF_GPIO
715 /*
Colin Cronin20a8a962015-05-18 11:41:43 -0700716 * If the gpiochip has an assigned OF node this takes precedence
Linus Walleij14250522014-03-25 10:40:18 +0100717 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
718 */
719 if (gpiochip->of_node)
720 of_node = gpiochip->of_node;
721#endif
722 gpiochip->irqchip = irqchip;
723 gpiochip->irq_handler = handler;
724 gpiochip->irq_default_type = type;
725 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300726 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +0100727 gpiochip->irqdomain = irq_domain_add_simple(of_node,
728 gpiochip->ngpio, first_irq,
729 &gpiochip_domain_ops, gpiochip);
730 if (!gpiochip->irqdomain) {
731 gpiochip->irqchip = NULL;
732 return -EINVAL;
733 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +0200734
735 /*
736 * It is possible for a driver to override this, but only if the
737 * alternative functions are both implemented.
738 */
739 if (!irqchip->irq_request_resources &&
740 !irqchip->irq_release_resources) {
741 irqchip->irq_request_resources = gpiochip_irq_reqres;
742 irqchip->irq_release_resources = gpiochip_irq_relres;
743 }
Linus Walleij14250522014-03-25 10:40:18 +0100744
745 /*
746 * Prepare the mapping since the irqchip shall be orthogonal to
747 * any gpiochip calls. If the first_irq was zero, this is
748 * necessary to allocate descriptors for all IRQs.
749 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100750 for (offset = 0; offset < gpiochip->ngpio; offset++) {
751 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
752 if (offset == 0)
753 /*
754 * Store the base into the gpiochip to be used when
755 * unmapping the irqs.
756 */
757 gpiochip->irq_base = irq_base;
758 }
Linus Walleij14250522014-03-25 10:40:18 +0100759
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300760 acpi_gpiochip_request_interrupts(gpiochip);
761
Linus Walleij14250522014-03-25 10:40:18 +0100762 return 0;
763}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300764EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +0100765
766#else /* CONFIG_GPIOLIB_IRQCHIP */
767
768static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
769
770#endif /* CONFIG_GPIOLIB_IRQCHIP */
771
Jonas Gorskic771c2f2015-10-11 17:34:15 +0200772/**
773 * gpiochip_generic_request() - request the gpio function for a pin
774 * @chip: the gpiochip owning the GPIO
775 * @offset: the offset of the GPIO to request for GPIO function
776 */
777int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
778{
779 return pinctrl_request_gpio(chip->base + offset);
780}
781EXPORT_SYMBOL_GPL(gpiochip_generic_request);
782
783/**
784 * gpiochip_generic_free() - free the gpio function from a pin
785 * @chip: the gpiochip to request the gpio function for
786 * @offset: the offset of the GPIO to free from GPIO function
787 */
788void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
789{
790 pinctrl_free_gpio(chip->base + offset);
791}
792EXPORT_SYMBOL_GPL(gpiochip_generic_free);
793
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530794#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100795
Linus Walleij3f0f8672012-11-20 12:40:15 +0100796/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200797 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
798 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +0200799 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +0200800 * @gpio_offset: the start offset in the current gpio_chip number space
801 * @pin_group: name of the pin group inside the pin controller
802 */
803int gpiochip_add_pingroup_range(struct gpio_chip *chip,
804 struct pinctrl_dev *pctldev,
805 unsigned int gpio_offset, const char *pin_group)
806{
807 struct gpio_pin_range *pin_range;
808 int ret;
809
810 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
811 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200812 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200813 return -ENOMEM;
814 }
815
816 /* Use local offset as range ID */
817 pin_range->range.id = gpio_offset;
818 pin_range->range.gc = chip;
819 pin_range->range.name = chip->label;
820 pin_range->range.base = chip->base + gpio_offset;
821 pin_range->pctldev = pctldev;
822
823 ret = pinctrl_get_group_pins(pctldev, pin_group,
824 &pin_range->range.pins,
825 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100826 if (ret < 0) {
827 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200828 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100829 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200830
831 pinctrl_add_gpio_range(pctldev, &pin_range->range);
832
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200833 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
834 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200835 pinctrl_dev_get_devname(pctldev), pin_group);
836
837 list_add_tail(&pin_range->node, &chip->pin_ranges);
838
839 return 0;
840}
841EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
842
843/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100844 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
845 * @chip: the gpiochip to add the range for
846 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100847 * @gpio_offset: the start offset in the current gpio_chip number space
848 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100849 * @npins: the number of pins from the offset of each pin space (GPIO and
850 * pin controller) to accumulate in this range
851 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100852int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100853 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100854 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530855{
856 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800857 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530858
Linus Walleij3f0f8672012-11-20 12:40:15 +0100859 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530860 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200861 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100862 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530863 }
864
Linus Walleij3f0f8672012-11-20 12:40:15 +0100865 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100866 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100867 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530868 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100869 pin_range->range.base = chip->base + gpio_offset;
870 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530871 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100872 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530873 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100874 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800875 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200876 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100877 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800878 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100879 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200880 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
881 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100882 pinctl_name,
883 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530884
885 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100886
887 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530888}
Linus Walleij165adc92012-11-06 14:49:39 +0100889EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530890
Linus Walleij3f0f8672012-11-20 12:40:15 +0100891/**
892 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
893 * @chip: the chip to remove all the mappings for
894 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530895void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
896{
897 struct gpio_pin_range *pin_range, *tmp;
898
899 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
900 list_del(&pin_range->node);
901 pinctrl_remove_gpio_range(pin_range->pctldev,
902 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100903 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530904 }
905}
Linus Walleij165adc92012-11-06 14:49:39 +0100906EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
907
908#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530909
David Brownelld2876d02008-02-04 22:28:20 -0800910/* These "optional" allocation calls help prevent drivers from stomping
911 * on each other, and help provide better diagnostics in debugfs.
912 * They're called even less than the "set direction" calls.
913 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200914static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800915{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200916 struct gpio_chip *chip = desc->chip;
917 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800918 unsigned long flags;
919
920 spin_lock_irqsave(&gpio_lock, flags);
921
David Brownelld2876d02008-02-04 22:28:20 -0800922 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700923 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800924 */
925
926 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
927 desc_set_label(desc, label ? : "?");
928 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700929 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800930 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800931 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700932 }
933
934 if (chip->request) {
935 /* chip->request may sleep */
936 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900937 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700938 spin_lock_irqsave(&gpio_lock, flags);
939
940 if (status < 0) {
941 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700942 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300943 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700944 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700945 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300946 if (chip->get_direction) {
947 /* chip->get_direction may sleep */
948 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900949 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300950 spin_lock_irqsave(&gpio_lock, flags);
951 }
David Brownelld2876d02008-02-04 22:28:20 -0800952done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +0300953 if (status < 0) {
954 /* Clear flags that might have been set by the caller before
955 * requesting the GPIO.
956 */
957 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
958 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
959 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
960 }
Mika Westerberg77c2d792014-03-10 14:54:50 +0200961 spin_unlock_irqrestore(&gpio_lock, flags);
962 return status;
963}
964
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900965int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200966{
967 int status = -EPROBE_DEFER;
968 struct gpio_chip *chip;
969
970 if (!desc) {
971 pr_warn("%s: invalid GPIO\n", __func__);
972 return -EINVAL;
973 }
974
975 chip = desc->chip;
976 if (!chip)
977 goto done;
978
979 if (try_module_get(chip->owner)) {
980 status = __gpiod_request(desc, label);
981 if (status < 0)
982 module_put(chip->owner);
983 }
984
985done:
David Brownelld2876d02008-02-04 22:28:20 -0800986 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200987 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200988
David Brownelld2876d02008-02-04 22:28:20 -0800989 return status;
990}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900991
Mika Westerberg77c2d792014-03-10 14:54:50 +0200992static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800993{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200994 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -0800995 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -0700996 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800997
Uwe Kleine-König3d599d12008-10-15 22:03:12 -0700998 might_sleep();
999
Alexandre Courbot372e7222013-02-03 01:29:29 +09001000 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001001
David Brownelld2876d02008-02-04 22:28:20 -08001002 spin_lock_irqsave(&gpio_lock, flags);
1003
David Brownell35e8bb52008-10-15 22:03:16 -07001004 chip = desc->chip;
1005 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1006 if (chip->free) {
1007 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001008 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001009 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001010 spin_lock_irqsave(&gpio_lock, flags);
1011 }
David Brownelld2876d02008-02-04 22:28:20 -08001012 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001013 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001014 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301015 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301016 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001017 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001018 ret = true;
1019 }
David Brownelld2876d02008-02-04 22:28:20 -08001020
1021 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001022 return ret;
1023}
1024
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001025void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001026{
1027 if (desc && __gpiod_free(desc))
1028 module_put(desc->chip->owner);
1029 else
1030 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -08001031}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001032
David Brownelld2876d02008-02-04 22:28:20 -08001033/**
1034 * gpiochip_is_requested - return string iff signal was requested
1035 * @chip: controller managing the signal
1036 * @offset: of signal within controller's 0..(ngpio - 1) range
1037 *
1038 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001039 * The string returned is the label passed to gpio_request(); if none has been
1040 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001041 *
1042 * This function is for use by GPIO controller drivers. The label can
1043 * help with diagnostics, and knowing that the signal is used as a GPIO
1044 * can help avoid accidentally multiplexing it to another controller.
1045 */
1046const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1047{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001048 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001049
Dirk Behme48b59532015-08-18 18:02:32 +02001050 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001051 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001052
1053 desc = &chip->desc[offset];
1054
Alexandre Courbot372e7222013-02-03 01:29:29 +09001055 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001056 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001057 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001058}
1059EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1060
Mika Westerberg77c2d792014-03-10 14:54:50 +02001061/**
1062 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1063 * @desc: GPIO descriptor to request
1064 * @label: label for the GPIO
1065 *
1066 * Function allows GPIO chip drivers to request and use their own GPIO
1067 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1068 * function will not increase reference count of the GPIO chip module. This
1069 * allows the GPIO chip module to be unloaded as needed (we assume that the
1070 * GPIO chip driver handles freeing the GPIOs it has requested).
1071 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001072struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1073 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001074{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001075 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1076 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001077
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001078 if (IS_ERR(desc)) {
1079 chip_err(chip, "failed to get GPIO descriptor\n");
1080 return desc;
1081 }
1082
1083 err = __gpiod_request(desc, label);
1084 if (err < 0)
1085 return ERR_PTR(err);
1086
1087 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001088}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001089EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001090
1091/**
1092 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1093 * @desc: GPIO descriptor to free
1094 *
1095 * Function frees the given GPIO requested previously with
1096 * gpiochip_request_own_desc().
1097 */
1098void gpiochip_free_own_desc(struct gpio_desc *desc)
1099{
1100 if (desc)
1101 __gpiod_free(desc);
1102}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001103EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08001104
1105/* Drivers MUST set GPIO direction before making get/set calls. In
1106 * some cases this is done in early boot, before IRQs are enabled.
1107 *
1108 * As a rule these aren't called more than once (except for drivers
1109 * using the open-drain emulation idiom) so these are natural places
1110 * to accumulate extra debugging checks. Note that we can't (yet)
1111 * rely on gpio_request() having been called beforehand.
1112 */
1113
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001114/**
1115 * gpiod_direction_input - set the GPIO direction to input
1116 * @desc: GPIO to set to input
1117 *
1118 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1119 * be called safely on it.
1120 *
1121 * Return 0 in case of success, else an error code.
1122 */
1123int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001124{
David Brownelld2876d02008-02-04 22:28:20 -08001125 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001126 int status = -EINVAL;
1127
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001128 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001129 pr_warn("%s: invalid GPIO\n", __func__);
1130 return -EINVAL;
1131 }
1132
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001133 chip = desc->chip;
1134 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001135 gpiod_warn(desc,
1136 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001137 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001138 return -EIO;
1139 }
1140
Alexandre Courbotd82da792014-07-22 16:17:43 +09001141 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001142 if (status == 0)
1143 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001144
Alexandre Courbot372e7222013-02-03 01:29:29 +09001145 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001146
David Brownelld2876d02008-02-04 22:28:20 -08001147 return status;
1148}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001149EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001150
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001151static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001152{
David Brownelld2876d02008-02-04 22:28:20 -08001153 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001154 int status = -EINVAL;
1155
Linus Walleijd468bf92013-09-24 11:54:38 +02001156 /* GPIOs used for IRQs shall not be set as output */
1157 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1158 gpiod_err(desc,
1159 "%s: tried to set a GPIO tied to an IRQ as output\n",
1160 __func__);
1161 return -EIO;
1162 }
1163
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301164 /* Open drain pin should not be driven to 1 */
1165 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001166 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301167
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301168 /* Open source pin should not be driven to 0 */
1169 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001170 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301171
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001172 chip = desc->chip;
1173 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001174 gpiod_warn(desc,
1175 "%s: missing set() or direction_output() operations\n",
1176 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001177 return -EIO;
1178 }
1179
Alexandre Courbotd82da792014-07-22 16:17:43 +09001180 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001181 if (status == 0)
1182 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001183 trace_gpio_value(desc_to_gpio(desc), 0, value);
1184 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001185 return status;
1186}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001187
1188/**
1189 * gpiod_direction_output_raw - set the GPIO direction to output
1190 * @desc: GPIO to set to output
1191 * @value: initial output value of the GPIO
1192 *
1193 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1194 * be called safely on it. The initial value of the output must be specified
1195 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1196 *
1197 * Return 0 in case of success, else an error code.
1198 */
1199int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1200{
1201 if (!desc || !desc->chip) {
1202 pr_warn("%s: invalid GPIO\n", __func__);
1203 return -EINVAL;
1204 }
1205 return _gpiod_direction_output_raw(desc, value);
1206}
1207EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1208
1209/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301210 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001211 * @desc: GPIO to set to output
1212 * @value: initial output value of the GPIO
1213 *
1214 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1215 * be called safely on it. The initial value of the output must be specified
1216 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1217 * account.
1218 *
1219 * Return 0 in case of success, else an error code.
1220 */
1221int gpiod_direction_output(struct gpio_desc *desc, int value)
1222{
1223 if (!desc || !desc->chip) {
1224 pr_warn("%s: invalid GPIO\n", __func__);
1225 return -EINVAL;
1226 }
1227 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1228 value = !value;
1229 return _gpiod_direction_output_raw(desc, value);
1230}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001231EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001232
Felipe Balbic4b5be92010-05-26 14:42:23 -07001233/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001234 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001235 * @gpio: the gpio to set debounce time
1236 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001237 *
1238 * returns -ENOTSUPP if the controller does not support setting
1239 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001240 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001241int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001242{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001243 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001244
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001245 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001246 pr_warn("%s: invalid GPIO\n", __func__);
1247 return -EINVAL;
1248 }
1249
Felipe Balbic4b5be92010-05-26 14:42:23 -07001250 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001251 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001252 gpiod_dbg(desc,
1253 "%s: missing set() or set_debounce() operations\n",
1254 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001255 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001256 }
1257
Alexandre Courbotd82da792014-07-22 16:17:43 +09001258 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001259}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001260EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001261
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001262/**
1263 * gpiod_is_active_low - test whether a GPIO is active-low or not
1264 * @desc: the gpio descriptor to test
1265 *
1266 * Returns 1 if the GPIO is active-low, 0 otherwise.
1267 */
1268int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001269{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001270 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001271}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001272EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001273
1274/* I/O calls are only valid after configuration completed; the relevant
1275 * "is this a valid GPIO" error checks should already have been done.
1276 *
1277 * "Get" operations are often inlinable as reading a pin value register,
1278 * and masking the relevant bit in that register.
1279 *
1280 * When "set" operations are inlinable, they involve writing that mask to
1281 * one register to set a low value, or a different register to set it high.
1282 * Otherwise locking is needed, so there may be little value to inlining.
1283 *
1284 *------------------------------------------------------------------------
1285 *
1286 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1287 * have requested the GPIO. That can include implicit requesting by
1288 * a direction setting call. Marking a gpio as requested locks its chip
1289 * in memory, guaranteeing that these table lookups need no more locking
1290 * and that gpiochip_remove() will fail.
1291 *
1292 * REVISIT when debugging, consider adding some instrumentation to ensure
1293 * that the GPIO was actually requested.
1294 */
1295
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001296static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001297{
1298 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001299 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001300 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001301
Alexandre Courbot372e7222013-02-03 01:29:29 +09001302 chip = desc->chip;
1303 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001304 value = chip->get ? chip->get(chip, offset) : -EIO;
1305 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001306 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001307 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001308}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001309
David Brownelld2876d02008-02-04 22:28:20 -08001310/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001311 * gpiod_get_raw_value() - return a gpio's raw value
1312 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001313 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001314 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001315 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001316 *
1317 * This function should be called from contexts where we cannot sleep, and will
1318 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001319 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001320int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001321{
David Brownelld2876d02008-02-04 22:28:20 -08001322 if (!desc)
1323 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001324 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001325 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001326 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001327}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001328EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001329
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001330/**
1331 * gpiod_get_value() - return a gpio's value
1332 * @desc: gpio whose value will be returned
1333 *
1334 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001335 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001336 *
1337 * This function should be called from contexts where we cannot sleep, and will
1338 * complain if the GPIO chip functions potentially sleep.
1339 */
1340int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001341{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001342 int value;
1343 if (!desc)
1344 return 0;
1345 /* Should be using gpio_get_value_cansleep() */
1346 WARN_ON(desc->chip->can_sleep);
1347
1348 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001349 if (value < 0)
1350 return value;
1351
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001352 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1353 value = !value;
1354
1355 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001356}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001357EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001358
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301359/*
1360 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001361 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001362 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301363 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001364static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301365{
1366 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001367 struct gpio_chip *chip = desc->chip;
1368 int offset = gpio_chip_hwgpio(desc);
1369
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301370 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001371 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301372 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001373 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301374 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001375 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301376 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001377 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301378 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001379 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301380 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001381 gpiod_err(desc,
1382 "%s: Error in set_value for open drain err %d\n",
1383 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301384}
1385
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301386/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001387 * _gpio_set_open_source_value() - Set the open source gpio's value.
1388 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001389 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301390 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001391static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301392{
1393 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001394 struct gpio_chip *chip = desc->chip;
1395 int offset = gpio_chip_hwgpio(desc);
1396
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301397 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001398 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301399 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001400 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301401 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001402 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301403 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001404 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301405 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001406 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301407 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001408 gpiod_err(desc,
1409 "%s: Error in set_value for open source err %d\n",
1410 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301411}
1412
Alexandre Courbot23600962014-03-11 15:52:09 +09001413static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001414{
1415 struct gpio_chip *chip;
1416
Alexandre Courbot372e7222013-02-03 01:29:29 +09001417 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001418 trace_gpio_value(desc_to_gpio(desc), 0, value);
1419 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1420 _gpio_set_open_drain_value(desc, value);
1421 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1422 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301423 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001424 chip->set(chip, gpio_chip_hwgpio(desc), value);
1425}
1426
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001427/*
1428 * set multiple outputs on the same chip;
1429 * use the chip's set_multiple function if available;
1430 * otherwise set the outputs sequentially;
1431 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1432 * defines which outputs are to be changed
1433 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1434 * defines the values the outputs specified by mask are to be set to
1435 */
1436static void gpio_chip_set_multiple(struct gpio_chip *chip,
1437 unsigned long *mask, unsigned long *bits)
1438{
1439 if (chip->set_multiple) {
1440 chip->set_multiple(chip, mask, bits);
1441 } else {
1442 int i;
1443 for (i = 0; i < chip->ngpio; i++) {
1444 if (mask[BIT_WORD(i)] == 0) {
1445 /* no more set bits in this mask word;
1446 * skip ahead to the next word */
1447 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1448 continue;
1449 }
1450 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001451 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001452 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001453 }
1454 }
1455}
1456
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001457static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1458 unsigned int array_size,
1459 struct gpio_desc **desc_array,
1460 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001461{
1462 int i = 0;
1463
1464 while (i < array_size) {
1465 struct gpio_chip *chip = desc_array[i]->chip;
1466 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1467 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1468 int count = 0;
1469
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001470 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001471 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001472
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001473 memset(mask, 0, sizeof(mask));
1474 do {
1475 struct gpio_desc *desc = desc_array[i];
1476 int hwgpio = gpio_chip_hwgpio(desc);
1477 int value = value_array[i];
1478
1479 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1480 value = !value;
1481 trace_gpio_value(desc_to_gpio(desc), 0, value);
1482 /*
1483 * collect all normal outputs belonging to the same chip
1484 * open drain and open source outputs are set individually
1485 */
1486 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001487 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001488 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1489 _gpio_set_open_source_value(desc, value);
1490 } else {
1491 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001492 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001493 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001494 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001495 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001496 count++;
1497 }
1498 i++;
1499 } while ((i < array_size) && (desc_array[i]->chip == chip));
1500 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001501 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001502 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001503 }
1504}
1505
David Brownelld2876d02008-02-04 22:28:20 -08001506/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001507 * gpiod_set_raw_value() - assign a gpio's raw value
1508 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001509 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001510 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001511 * Set the raw value of the GPIO, i.e. the value of its physical line without
1512 * regard for its ACTIVE_LOW status.
1513 *
1514 * This function should be called from contexts where we cannot sleep, and will
1515 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001516 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001517void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001518{
David Brownelld2876d02008-02-04 22:28:20 -08001519 if (!desc)
1520 return;
David Brownelld2876d02008-02-04 22:28:20 -08001521 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001522 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001523 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001524}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001525EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001526
1527/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001528 * gpiod_set_value() - assign a gpio's value
1529 * @desc: gpio whose value will be assigned
1530 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001531 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001532 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1533 * account
1534 *
1535 * This function should be called from contexts where we cannot sleep, and will
1536 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001537 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001538void gpiod_set_value(struct gpio_desc *desc, int value)
1539{
1540 if (!desc)
1541 return;
1542 /* Should be using gpio_set_value_cansleep() */
1543 WARN_ON(desc->chip->can_sleep);
1544 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1545 value = !value;
1546 _gpiod_set_raw_value(desc, value);
1547}
1548EXPORT_SYMBOL_GPL(gpiod_set_value);
1549
1550/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001551 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001552 * @array_size: number of elements in the descriptor / value arrays
1553 * @desc_array: array of GPIO descriptors whose values will be assigned
1554 * @value_array: array of values to assign
1555 *
1556 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1557 * without regard for their ACTIVE_LOW status.
1558 *
1559 * This function should be called from contexts where we cannot sleep, and will
1560 * complain if the GPIO chip functions potentially sleep.
1561 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001562void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001563 struct gpio_desc **desc_array, int *value_array)
1564{
1565 if (!desc_array)
1566 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001567 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1568 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001569}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001570EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001571
1572/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001573 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001574 * @array_size: number of elements in the descriptor / value arrays
1575 * @desc_array: array of GPIO descriptors whose values will be assigned
1576 * @value_array: array of values to assign
1577 *
1578 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1579 * into account.
1580 *
1581 * This function should be called from contexts where we cannot sleep, and will
1582 * complain if the GPIO chip functions potentially sleep.
1583 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001584void gpiod_set_array_value(unsigned int array_size,
1585 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001586{
1587 if (!desc_array)
1588 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001589 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1590 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001591}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001592EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001593
1594/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001595 * gpiod_cansleep() - report whether gpio value access may sleep
1596 * @desc: gpio to check
1597 *
1598 */
1599int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001600{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001601 if (!desc)
1602 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001603 return desc->chip->can_sleep;
1604}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001605EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001606
David Brownell0f6d5042008-10-15 22:03:14 -07001607/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001608 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1609 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001610 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001611 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1612 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001613 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001614int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001615{
1616 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001617 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001618
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001619 if (!desc)
1620 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001621 chip = desc->chip;
1622 offset = gpio_chip_hwgpio(desc);
1623 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1624}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001625EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001626
Linus Walleijd468bf92013-09-24 11:54:38 +02001627/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001628 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001629 * @chip: the chip the GPIO to lock belongs to
1630 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001631 *
1632 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001633 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001634 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001635int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001636{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001637 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001638 return -EINVAL;
1639
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001640 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1641 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001642 "%s: tried to flag a GPIO set as output for IRQ\n",
1643 __func__);
1644 return -EIO;
1645 }
1646
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001647 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001648 return 0;
1649}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001650EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001651
Linus Walleijd468bf92013-09-24 11:54:38 +02001652/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001653 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001654 * @chip: the chip the GPIO to lock belongs to
1655 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001656 *
1657 * This is used directly by GPIO drivers that want to indicate
1658 * that a certain GPIO is no longer used exclusively for IRQ.
1659 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001660void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001661{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001662 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001663 return;
1664
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001665 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001666}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001667EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001668
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001669/**
1670 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1671 * @desc: gpio whose value will be returned
1672 *
1673 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001674 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001675 *
1676 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001677 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001678int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001679{
David Brownelld2876d02008-02-04 22:28:20 -08001680 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001681 if (!desc)
1682 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001683 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001684}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001685EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001686
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001687/**
1688 * gpiod_get_value_cansleep() - return a gpio's value
1689 * @desc: gpio whose value will be returned
1690 *
1691 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001692 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001693 *
1694 * This function is to be called from contexts that can sleep.
1695 */
1696int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001697{
David Brownelld2876d02008-02-04 22:28:20 -08001698 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001699
1700 might_sleep_if(extra_checks);
1701 if (!desc)
1702 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001703
1704 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001705 if (value < 0)
1706 return value;
1707
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001708 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1709 value = !value;
1710
David Brownelld2876d02008-02-04 22:28:20 -08001711 return value;
1712}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001713EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001714
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001715/**
1716 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1717 * @desc: gpio whose value will be assigned
1718 * @value: value to assign
1719 *
1720 * Set the raw value of the GPIO, i.e. the value of its physical line without
1721 * regard for its ACTIVE_LOW status.
1722 *
1723 * This function is to be called from contexts that can sleep.
1724 */
1725void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001726{
David Brownelld2876d02008-02-04 22:28:20 -08001727 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001728 if (!desc)
1729 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001730 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001731}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001732EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001733
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001734/**
1735 * gpiod_set_value_cansleep() - assign a gpio's value
1736 * @desc: gpio whose value will be assigned
1737 * @value: value to assign
1738 *
1739 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1740 * account
1741 *
1742 * This function is to be called from contexts that can sleep.
1743 */
1744void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001745{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001746 might_sleep_if(extra_checks);
1747 if (!desc)
1748 return;
1749
1750 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1751 value = !value;
1752 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001753}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001754EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001755
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001756/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001757 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001758 * @array_size: number of elements in the descriptor / value arrays
1759 * @desc_array: array of GPIO descriptors whose values will be assigned
1760 * @value_array: array of values to assign
1761 *
1762 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1763 * without regard for their ACTIVE_LOW status.
1764 *
1765 * This function is to be called from contexts that can sleep.
1766 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001767void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1768 struct gpio_desc **desc_array,
1769 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001770{
1771 might_sleep_if(extra_checks);
1772 if (!desc_array)
1773 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001774 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1775 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001776}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001777EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001778
1779/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001780 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001781 * @array_size: number of elements in the descriptor / value arrays
1782 * @desc_array: array of GPIO descriptors whose values will be assigned
1783 * @value_array: array of values to assign
1784 *
1785 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1786 * into account.
1787 *
1788 * This function is to be called from contexts that can sleep.
1789 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001790void gpiod_set_array_value_cansleep(unsigned int array_size,
1791 struct gpio_desc **desc_array,
1792 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001793{
1794 might_sleep_if(extra_checks);
1795 if (!desc_array)
1796 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001797 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1798 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001799}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001800EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001801
1802/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001803 * gpiod_add_lookup_table() - register GPIO device consumers
1804 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001805 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001806void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001807{
1808 mutex_lock(&gpio_lookup_lock);
1809
Alexandre Courbotad824782013-12-03 12:20:11 +09001810 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001811
1812 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001813}
1814
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05301815/**
1816 * gpiod_remove_lookup_table() - unregister GPIO device consumers
1817 * @table: table of consumers to unregister
1818 */
1819void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
1820{
1821 mutex_lock(&gpio_lookup_lock);
1822
1823 list_del(&table->list);
1824
1825 mutex_unlock(&gpio_lookup_lock);
1826}
1827
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001828static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001829 unsigned int idx,
1830 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001831{
1832 char prop_name[32]; /* 32 is max size of property name */
1833 enum of_gpio_flags of_flags;
1834 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001835 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001836
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001837 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02001838 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01001839 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001840 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02001841 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01001842 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001843 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001844
Thierry Redingdd34c372014-04-23 17:28:09 +02001845 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1846 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001847 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001848 break;
1849 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001850
1851 if (IS_ERR(desc))
1852 return desc;
1853
1854 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001855 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001856
Laurent Pinchart90b665f2015-10-13 00:20:21 +03001857 if (of_flags & OF_GPIO_SINGLE_ENDED) {
1858 if (of_flags & OF_GPIO_ACTIVE_LOW)
1859 *flags |= GPIO_OPEN_DRAIN;
1860 else
1861 *flags |= GPIO_OPEN_SOURCE;
1862 }
1863
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001864 return desc;
1865}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001866
Mika Westerberg81f59e92013-10-10 11:01:09 +03001867static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001868 unsigned int idx,
1869 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001870{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001871 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03001872 struct acpi_gpio_info info;
1873 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001874 char propname[32];
1875 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03001876
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001877 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001878 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001879 if (con_id && strcmp(con_id, "gpios")) {
1880 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001881 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001882 } else {
1883 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001884 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001885 }
Mika Westerberge01f4402013-10-10 11:01:10 +03001886
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001887 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1888 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1889 break;
1890 }
1891
1892 /* Then from plain _CRS GPIOs */
1893 if (IS_ERR(desc)) {
Dmitry Torokhov9c3c9bc2015-11-11 11:45:30 -08001894 if (!acpi_can_fallback_to_crs(adev, con_id))
1895 return ERR_PTR(-ENOENT);
1896
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001897 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1898 if (IS_ERR(desc))
1899 return desc;
1900 }
1901
1902 if (info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001903 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001904
1905 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001906}
1907
Alexandre Courbotad824782013-12-03 12:20:11 +09001908static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1909{
1910 const char *dev_id = dev ? dev_name(dev) : NULL;
1911 struct gpiod_lookup_table *table;
1912
1913 mutex_lock(&gpio_lookup_lock);
1914
1915 list_for_each_entry(table, &gpio_lookup_list, list) {
1916 if (table->dev_id && dev_id) {
1917 /*
1918 * Valid strings on both ends, must be identical to have
1919 * a match
1920 */
1921 if (!strcmp(table->dev_id, dev_id))
1922 goto found;
1923 } else {
1924 /*
1925 * One of the pointers is NULL, so both must be to have
1926 * a match
1927 */
1928 if (dev_id == table->dev_id)
1929 goto found;
1930 }
1931 }
1932 table = NULL;
1933
1934found:
1935 mutex_unlock(&gpio_lookup_lock);
1936 return table;
1937}
1938
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001939static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001940 unsigned int idx,
1941 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001942{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001943 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001944 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001945 struct gpiod_lookup *p;
1946
Alexandre Courbotad824782013-12-03 12:20:11 +09001947 table = gpiod_find_lookup_table(dev);
1948 if (!table)
1949 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001950
Alexandre Courbotad824782013-12-03 12:20:11 +09001951 for (p = &table->table[0]; p->chip_label; p++) {
1952 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001953
Alexandre Courbotad824782013-12-03 12:20:11 +09001954 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001955 if (p->idx != idx)
1956 continue;
1957
Alexandre Courbotad824782013-12-03 12:20:11 +09001958 /* If the lookup entry has a con_id, require exact match */
1959 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1960 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001961
Alexandre Courbotad824782013-12-03 12:20:11 +09001962 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001963
Alexandre Courbotad824782013-12-03 12:20:11 +09001964 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001965 dev_err(dev, "cannot find GPIO chip %s\n",
1966 p->chip_label);
1967 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001968 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001969
Alexandre Courbotad824782013-12-03 12:20:11 +09001970 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001971 dev_err(dev,
1972 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1973 idx, chip->ngpio, chip->label);
1974 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001975 }
1976
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001977 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001978 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001979
1980 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001981 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001982
1983 return desc;
1984}
1985
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001986static int dt_gpio_count(struct device *dev, const char *con_id)
1987{
1988 int ret;
1989 char propname[32];
1990 unsigned int i;
1991
1992 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1993 if (con_id)
1994 snprintf(propname, sizeof(propname), "%s-%s",
1995 con_id, gpio_suffixes[i]);
1996 else
1997 snprintf(propname, sizeof(propname), "%s",
1998 gpio_suffixes[i]);
1999
2000 ret = of_gpio_named_count(dev->of_node, propname);
2001 if (ret >= 0)
2002 break;
2003 }
2004 return ret;
2005}
2006
2007static int platform_gpio_count(struct device *dev, const char *con_id)
2008{
2009 struct gpiod_lookup_table *table;
2010 struct gpiod_lookup *p;
2011 unsigned int count = 0;
2012
2013 table = gpiod_find_lookup_table(dev);
2014 if (!table)
2015 return -ENOENT;
2016
2017 for (p = &table->table[0]; p->chip_label; p++) {
2018 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2019 (!con_id && !p->con_id))
2020 count++;
2021 }
2022 if (!count)
2023 return -ENOENT;
2024
2025 return count;
2026}
2027
2028/**
2029 * gpiod_count - return the number of GPIOs associated with a device / function
2030 * or -ENOENT if no GPIO has been assigned to the requested function
2031 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2032 * @con_id: function within the GPIO consumer
2033 */
2034int gpiod_count(struct device *dev, const char *con_id)
2035{
2036 int count = -ENOENT;
2037
2038 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2039 count = dt_gpio_count(dev, con_id);
2040 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2041 count = acpi_gpio_count(dev, con_id);
2042
2043 if (count < 0)
2044 count = platform_gpio_count(dev, con_id);
2045
2046 return count;
2047}
2048EXPORT_SYMBOL_GPL(gpiod_count);
2049
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002050/**
Thierry Reding08791622014-04-25 16:54:22 +02002051 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002052 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002053 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002054 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002055 *
2056 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002057 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07002058 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002059 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002060struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002061 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002062{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002063 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002064}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002065EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002066
2067/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002068 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2069 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2070 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002071 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002072 *
2073 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2074 * the requested function it will return NULL. This is convenient for drivers
2075 * that need to handle optional GPIOs.
2076 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002077struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002078 const char *con_id,
2079 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002080{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002081 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002082}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002083EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002084
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002085/**
2086 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2087 * @desc: gpio to be setup
2088 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2089 * of_get_gpio_hog()
2090 *
2091 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2092 */
2093static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2094{
2095 if (lflags & GPIO_ACTIVE_LOW)
2096 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2097 if (lflags & GPIO_OPEN_DRAIN)
2098 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2099 if (lflags & GPIO_OPEN_SOURCE)
2100 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2101}
Benoit Parrotf625d462015-02-02 11:44:44 -06002102
2103/**
2104 * gpiod_configure_flags - helper function to configure a given GPIO
2105 * @desc: gpio whose value will be assigned
2106 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06002107 * @dflags: gpiod_flags - optional GPIO initialization flags
2108 *
2109 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2110 * requested function and/or index, or another IS_ERR() code if an error
2111 * occurred while trying to acquire the GPIO.
2112 */
2113static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002114 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06002115{
2116 int status;
2117
Benoit Parrotf625d462015-02-02 11:44:44 -06002118 /* No particular flag request, return here... */
2119 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2120 pr_debug("no flags found for %s\n", con_id);
2121 return 0;
2122 }
2123
2124 /* Process flags */
2125 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2126 status = gpiod_direction_output(desc,
2127 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2128 else
2129 status = gpiod_direction_input(desc);
2130
2131 return status;
2132}
2133
Thierry Reding29a1f2332014-04-25 17:10:06 +02002134/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002135 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002136 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002137 * @con_id: function within the GPIO consumer
2138 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002139 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002140 *
2141 * This variant of gpiod_get() allows to access GPIOs other than the first
2142 * defined one for functions that define several GPIOs.
2143 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002144 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2145 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07002146 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002147 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002148struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002149 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002150 unsigned int idx,
2151 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002152{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002153 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002154 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002155 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002156
2157 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2158
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002159 if (dev) {
2160 /* Using device tree? */
2161 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2162 dev_dbg(dev, "using device tree for GPIO lookup\n");
2163 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2164 } else if (ACPI_COMPANION(dev)) {
2165 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2166 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2167 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002168 }
2169
2170 /*
2171 * Either we are not using DT or ACPI, or their lookup did not return
2172 * a result. In that case, use platform lookup as a fallback.
2173 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002174 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002175 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002176 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002177 }
2178
2179 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002180 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002181 return desc;
2182 }
2183
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002184 gpiod_parse_flags(desc, lookupflags);
2185
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002186 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002187 if (status < 0)
2188 return ERR_PTR(status);
2189
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002190 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002191 if (status < 0) {
2192 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2193 gpiod_put(desc);
2194 return ERR_PTR(status);
2195 }
2196
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002197 return desc;
2198}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002199EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002200
2201/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002202 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2203 * @fwnode: handle of the firmware node
2204 * @propname: name of the firmware property representing the GPIO
2205 *
2206 * This function can be used for drivers that get their configuration
2207 * from firmware.
2208 *
2209 * Function properly finds the corresponding GPIO using whatever is the
2210 * underlying firmware interface and then makes sure that the GPIO
2211 * descriptor is requested before it is returned to the caller.
2212 *
2213 * In case of error an ERR_PTR() is returned.
2214 */
2215struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2216 const char *propname)
2217{
2218 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2219 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002220 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02002221 int ret;
2222
2223 if (!fwnode)
2224 return ERR_PTR(-EINVAL);
2225
2226 if (is_of_node(fwnode)) {
2227 enum of_gpio_flags flags;
2228
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002229 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002230 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002231 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02002232 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002233 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2234 }
Mika Westerberg40b73182014-10-21 13:33:59 +02002235 } else if (is_acpi_node(fwnode)) {
2236 struct acpi_gpio_info info;
2237
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02002238 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02002239 if (!IS_ERR(desc))
2240 active_low = info.active_low;
2241 }
2242
2243 if (IS_ERR(desc))
2244 return desc;
2245
Mika Westerberg40b73182014-10-21 13:33:59 +02002246 if (active_low)
2247 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2248
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002249 if (single_ended) {
2250 if (active_low)
2251 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2252 else
2253 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2254 }
2255
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002256 ret = gpiod_request(desc, NULL);
2257 if (ret)
2258 return ERR_PTR(ret);
2259
Mika Westerberg40b73182014-10-21 13:33:59 +02002260 return desc;
2261}
2262EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2263
2264/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002265 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2266 * function
2267 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2268 * @con_id: function within the GPIO consumer
2269 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002270 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002271 *
2272 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2273 * specified index was assigned to the requested function it will return NULL.
2274 * This is convenient for drivers that need to handle optional GPIOs.
2275 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002276struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002277 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002278 unsigned int index,
2279 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002280{
2281 struct gpio_desc *desc;
2282
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002283 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002284 if (IS_ERR(desc)) {
2285 if (PTR_ERR(desc) == -ENOENT)
2286 return NULL;
2287 }
2288
2289 return desc;
2290}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002291EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002292
2293/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002294 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2295 * @desc: gpio whose value will be assigned
2296 * @name: gpio line name
2297 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2298 * of_get_gpio_hog()
2299 * @dflags: gpiod_flags - optional GPIO initialization flags
2300 */
2301int gpiod_hog(struct gpio_desc *desc, const char *name,
2302 unsigned long lflags, enum gpiod_flags dflags)
2303{
2304 struct gpio_chip *chip;
2305 struct gpio_desc *local_desc;
2306 int hwnum;
2307 int status;
2308
2309 chip = gpiod_to_chip(desc);
2310 hwnum = gpio_chip_hwgpio(desc);
2311
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002312 gpiod_parse_flags(desc, lflags);
2313
Benoit Parrotf625d462015-02-02 11:44:44 -06002314 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2315 if (IS_ERR(local_desc)) {
Linus Walleija7138902015-06-05 11:36:10 +02002316 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2317 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002318 return PTR_ERR(local_desc);
2319 }
2320
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002321 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002322 if (status < 0) {
Linus Walleija7138902015-06-05 11:36:10 +02002323 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2324 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002325 gpiochip_free_own_desc(desc);
2326 return status;
2327 }
2328
2329 /* Mark GPIO as hogged so it can be identified and removed later */
2330 set_bit(FLAG_IS_HOGGED, &desc->flags);
2331
2332 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2333 desc_to_gpio(desc), name,
2334 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2335 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2336 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2337
2338 return 0;
2339}
2340
2341/**
2342 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2343 * @chip: gpio chip to act on
2344 *
2345 * This is only used by of_gpiochip_remove to free hogged gpios
2346 */
2347static void gpiochip_free_hogs(struct gpio_chip *chip)
2348{
2349 int id;
2350
2351 for (id = 0; id < chip->ngpio; id++) {
2352 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2353 gpiochip_free_own_desc(&chip->desc[id]);
2354 }
2355}
2356
2357/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002358 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2359 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2360 * @con_id: function within the GPIO consumer
2361 * @flags: optional GPIO initialization flags
2362 *
2363 * This function acquires all the GPIOs defined under a given function.
2364 *
2365 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2366 * no GPIO has been assigned to the requested function, or another IS_ERR()
2367 * code if an error occurred while trying to acquire the GPIOs.
2368 */
2369struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2370 const char *con_id,
2371 enum gpiod_flags flags)
2372{
2373 struct gpio_desc *desc;
2374 struct gpio_descs *descs;
2375 int count;
2376
2377 count = gpiod_count(dev, con_id);
2378 if (count < 0)
2379 return ERR_PTR(count);
2380
2381 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2382 GFP_KERNEL);
2383 if (!descs)
2384 return ERR_PTR(-ENOMEM);
2385
2386 for (descs->ndescs = 0; descs->ndescs < count; ) {
2387 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2388 if (IS_ERR(desc)) {
2389 gpiod_put_array(descs);
2390 return ERR_CAST(desc);
2391 }
2392 descs->desc[descs->ndescs] = desc;
2393 descs->ndescs++;
2394 }
2395 return descs;
2396}
2397EXPORT_SYMBOL_GPL(gpiod_get_array);
2398
2399/**
2400 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2401 * function
2402 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2403 * @con_id: function within the GPIO consumer
2404 * @flags: optional GPIO initialization flags
2405 *
2406 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2407 * assigned to the requested function it will return NULL.
2408 */
2409struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2410 const char *con_id,
2411 enum gpiod_flags flags)
2412{
2413 struct gpio_descs *descs;
2414
2415 descs = gpiod_get_array(dev, con_id, flags);
2416 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2417 return NULL;
2418
2419 return descs;
2420}
2421EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2422
2423/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002424 * gpiod_put - dispose of a GPIO descriptor
2425 * @desc: GPIO descriptor to dispose of
2426 *
2427 * No descriptor can be used after gpiod_put() has been called on it.
2428 */
2429void gpiod_put(struct gpio_desc *desc)
2430{
2431 gpiod_free(desc);
2432}
2433EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002434
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002435/**
2436 * gpiod_put_array - dispose of multiple GPIO descriptors
2437 * @descs: struct gpio_descs containing an array of descriptors
2438 */
2439void gpiod_put_array(struct gpio_descs *descs)
2440{
2441 unsigned int i;
2442
2443 for (i = 0; i < descs->ndescs; i++)
2444 gpiod_put(descs->desc[i]);
2445
2446 kfree(descs);
2447}
2448EXPORT_SYMBOL_GPL(gpiod_put_array);
2449
David Brownelld2876d02008-02-04 22:28:20 -08002450#ifdef CONFIG_DEBUG_FS
2451
2452static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2453{
2454 unsigned i;
2455 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002456 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002457 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002458 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002459
2460 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02002461 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2462 if (gdesc->name) {
2463 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2464 gpio, gdesc->name);
2465 }
David Brownelld2876d02008-02-04 22:28:20 -08002466 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02002467 }
David Brownelld2876d02008-02-04 22:28:20 -08002468
Alexandre Courbot372e7222013-02-03 01:29:29 +09002469 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002470 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002471 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02002472 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2473 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08002474 is_out ? "out" : "in ",
2475 chip->get
2476 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002477 : "? ",
2478 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002479 seq_printf(s, "\n");
2480 }
2481}
2482
Thierry Redingf9c4a312012-04-12 13:26:01 +02002483static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002484{
Grant Likely362432a2013-02-09 09:41:49 +00002485 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002486 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002487 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002488
2489 s->private = "";
2490
Grant Likely362432a2013-02-09 09:41:49 +00002491 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002492 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002493 if (index-- == 0) {
2494 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002495 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002496 }
2497 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002498
2499 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002500}
2501
2502static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2503{
Grant Likely362432a2013-02-09 09:41:49 +00002504 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002505 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002506 void *ret = NULL;
2507
Grant Likely362432a2013-02-09 09:41:49 +00002508 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002509 if (list_is_last(&chip->list, &gpio_chips))
2510 ret = NULL;
2511 else
2512 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002513 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002514
2515 s->private = "\n";
2516 ++*pos;
2517
2518 return ret;
2519}
2520
2521static void gpiolib_seq_stop(struct seq_file *s, void *v)
2522{
2523}
2524
2525static int gpiolib_seq_show(struct seq_file *s, void *v)
2526{
2527 struct gpio_chip *chip = v;
2528 struct device *dev;
2529
2530 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2531 chip->base, chip->base + chip->ngpio - 1);
Linus Walleij58383c72015-11-04 09:56:26 +01002532 dev = chip->parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002533 if (dev)
2534 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2535 dev_name(dev));
2536 if (chip->label)
2537 seq_printf(s, ", %s", chip->label);
2538 if (chip->can_sleep)
2539 seq_printf(s, ", can sleep");
2540 seq_printf(s, ":\n");
2541
2542 if (chip->dbg_show)
2543 chip->dbg_show(s, chip);
2544 else
2545 gpiolib_dbg_show(s, chip);
2546
David Brownelld2876d02008-02-04 22:28:20 -08002547 return 0;
2548}
2549
Thierry Redingf9c4a312012-04-12 13:26:01 +02002550static const struct seq_operations gpiolib_seq_ops = {
2551 .start = gpiolib_seq_start,
2552 .next = gpiolib_seq_next,
2553 .stop = gpiolib_seq_stop,
2554 .show = gpiolib_seq_show,
2555};
2556
David Brownelld2876d02008-02-04 22:28:20 -08002557static int gpiolib_open(struct inode *inode, struct file *file)
2558{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002559 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002560}
2561
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002562static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002563 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002564 .open = gpiolib_open,
2565 .read = seq_read,
2566 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002567 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002568};
2569
2570static int __init gpiolib_debugfs_init(void)
2571{
2572 /* /sys/kernel/debug/gpio */
2573 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2574 NULL, NULL, &gpiolib_operations);
2575 return 0;
2576}
2577subsys_initcall(gpiolib_debugfs_init);
2578
2579#endif /* DEBUG_FS */