blob: 5c1ba879f889055865d5f2adc7df467ffb9a0a66 [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{
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800192 struct gpio_chip *iterator;
193 struct gpio_chip *previous = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900194
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800195 if (list_empty(&gpio_chips)) {
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530196 list_add_tail(&chip->list, &gpio_chips);
197 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900198 }
199
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530200 list_for_each_entry(iterator, &gpio_chips, list) {
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800201 if (iterator->base >= chip->base + chip->ngpio) {
202 /*
203 * Iterator is the first GPIO chip so there is no
204 * previous one
205 */
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530206 if (!previous) {
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800207 goto found;
208 } else {
209 /*
210 * We found a valid range(means
211 * [base, base + ngpio - 1]) between previous
212 * and iterator chip.
213 */
214 if (previous->base + previous->ngpio
215 <= chip->base)
216 goto found;
217 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900218 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800219 previous = iterator;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900220 }
221
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530222 /*
223 * We are beyond the last chip in the list and iterator now
224 * points to the head.
225 * Let iterator point to the last chip in the list.
226 */
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900227
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530228 iterator = list_last_entry(&gpio_chips, struct gpio_chip, list);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500229 if (iterator->base + iterator->ngpio <= chip->base) {
230 list_add(&chip->list, &iterator->list);
231 return 0;
232 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900233
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800234 dev_err(chip->parent,
235 "GPIO integer space overlap, cannot add chip\n");
236 return -EBUSY;
237
238found:
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530239 list_add_tail(&chip->list, &iterator->list);
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800240 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900241}
242
Linus Walleijf881bab2015-09-23 16:20:43 -0700243/**
244 * Convert a GPIO name to its descriptor
245 */
246static struct gpio_desc *gpio_name_to_desc(const char * const name)
247{
248 struct gpio_chip *chip;
249 unsigned long flags;
250
251 spin_lock_irqsave(&gpio_lock, flags);
252
253 list_for_each_entry(chip, &gpio_chips, list) {
254 int i;
255
256 for (i = 0; i != chip->ngpio; ++i) {
257 struct gpio_desc *gpio = &chip->desc[i];
258
Vladimir Zapolskiyd06165b2015-11-11 14:36:53 +0200259 if (!gpio->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700260 continue;
261
262 if (!strcmp(gpio->name, name)) {
263 spin_unlock_irqrestore(&gpio_lock, flags);
264 return gpio;
265 }
266 }
267 }
268
269 spin_unlock_irqrestore(&gpio_lock, flags);
270
271 return NULL;
272}
273
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200274/*
275 * Takes the names from gc->names and checks if they are all unique. If they
276 * are, they are assigned to their gpio descriptors.
277 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800278 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200279 */
280static int gpiochip_set_desc_names(struct gpio_chip *gc)
281{
282 int i;
283
284 if (!gc->names)
285 return 0;
286
287 /* First check all names if they are unique */
288 for (i = 0; i != gc->ngpio; ++i) {
289 struct gpio_desc *gpio;
290
291 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700292 if (gpio)
Linus Walleij58383c72015-11-04 09:56:26 +0100293 dev_warn(gc->parent, "Detected name collision for "
Linus Walleijf881bab2015-09-23 16:20:43 -0700294 "GPIO name '%s'\n",
295 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200296 }
297
298 /* Then add all names to the GPIO descriptors */
299 for (i = 0; i != gc->ngpio; ++i)
300 gc->desc[i].name = gc->names[i];
301
302 return 0;
303}
304
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700305/**
Linus Walleijb08ea352015-12-03 15:14:13 +0100306 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -0800307 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900308 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -0800309 *
310 * Returns a negative errno if the chip can't be registered, such as
311 * because the chip->base is invalid or already associated with a
312 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700313 *
Linus Walleijb08ea352015-12-03 15:14:13 +0100314 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800315 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d2008-07-25 01:46:07 -0700316 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
317 * for GPIOs will fail rudely.
318 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700319 * If chip->base is negative, this requests dynamic assignment of
320 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800321 */
Linus Walleijb08ea352015-12-03 15:14:13 +0100322int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -0800323{
324 unsigned long flags;
325 int status = 0;
326 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700327 int base = chip->base;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900328 struct gpio_desc *descs;
David Brownelld2876d02008-02-04 22:28:20 -0800329
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900330 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
331 if (!descs)
332 return -ENOMEM;
David Brownelld2876d02008-02-04 22:28:20 -0800333
Linus Walleijb08ea352015-12-03 15:14:13 +0100334 chip->data = data;
335
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800336 if (chip->ngpio == 0) {
337 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
338 return -EINVAL;
339 }
340
David Brownelld2876d02008-02-04 22:28:20 -0800341 spin_lock_irqsave(&gpio_lock, flags);
342
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700343 if (base < 0) {
344 base = gpiochip_find_base(chip->ngpio);
345 if (base < 0) {
346 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100347 spin_unlock_irqrestore(&gpio_lock, flags);
348 goto err_free_descs;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700349 }
350 chip->base = base;
351 }
352
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900353 status = gpiochip_add_to_list(chip);
Johan Hovold05aa5202015-01-12 17:12:26 +0100354 if (status) {
355 spin_unlock_irqrestore(&gpio_lock, flags);
356 goto err_free_descs;
357 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900358
Johan Hovold05aa5202015-01-12 17:12:26 +0100359 for (id = 0; id < chip->ngpio; id++) {
360 struct gpio_desc *desc = &descs[id];
David Brownelld8f388d2008-07-25 01:46:07 -0700361
Johan Hovold05aa5202015-01-12 17:12:26 +0100362 desc->chip = chip;
363
364 /* REVISIT: most hardware initializes GPIOs as inputs (often
365 * with pullups enabled) so power usage is minimized. Linux
366 * code should set the gpio direction first thing; but until
367 * it does, and in case chip->get_direction is not set, we may
368 * expose the wrong direction in sysfs.
369 */
370 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800371 }
372
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900373 chip->desc = descs;
374
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800375 spin_unlock_irqrestore(&gpio_lock, flags);
376
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530377#ifdef CONFIG_PINCTRL
378 INIT_LIST_HEAD(&chip->pin_ranges);
379#endif
380
Linus Walleij58383c72015-11-04 09:56:26 +0100381 if (!chip->owner && chip->parent && chip->parent->driver)
382 chip->owner = chip->parent->driver->owner;
Grygorii Strashko37269602015-06-25 20:30:51 +0300383
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200384 status = gpiochip_set_desc_names(chip);
385 if (status)
386 goto err_remove_from_list;
387
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200388 status = of_gpiochip_add(chip);
389 if (status)
390 goto err_remove_chip;
391
Mika Westerberg664e3e52014-01-08 12:40:54 +0200392 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600393
Johan Hovold426577b2015-05-04 17:10:32 +0200394 status = gpiochip_sysfs_register(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100395 if (status)
396 goto err_remove_chip;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600397
Andy Shevchenko7589e592013-12-05 11:26:23 +0200398 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700399 chip->base, chip->base + chip->ngpio - 1,
400 chip->label ? : "generic");
401
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600402 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800403
Johan Hovold225fce82015-01-12 17:12:25 +0100404err_remove_chip:
405 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200406 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100407 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200408err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100409 spin_lock_irqsave(&gpio_lock, flags);
410 list_del(&chip->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800411 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold05aa5202015-01-12 17:12:26 +0100412 chip->desc = NULL;
Johan Hovold225fce82015-01-12 17:12:25 +0100413err_free_descs:
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900414 kfree(descs);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900415
David Brownelld2876d02008-02-04 22:28:20 -0800416 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200417 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600418 chip->base, chip->base + chip->ngpio - 1,
419 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800420 return status;
421}
Linus Walleijb08ea352015-12-03 15:14:13 +0100422EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -0800423
424/**
425 * gpiochip_remove() - unregister a gpio_chip
426 * @chip: the chip to unregister
427 *
428 * A gpio_chip with any GPIOs still requested may not be removed.
429 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200430void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800431{
Johan Hovoldfab28b82015-05-04 17:10:27 +0200432 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800433 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800434 unsigned id;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200435 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800436
Johan Hovold426577b2015-05-04 17:10:32 +0200437 gpiochip_sysfs_unregister(chip);
Johan Hovold01cca932015-01-12 17:12:29 +0100438
Johan Hovold00acc3d2015-01-12 17:12:27 +0100439 gpiochip_irqchip_remove(chip);
440
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200441 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100442 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600443 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600444 of_gpiochip_remove(chip);
445
Johan Hovold6798aca2015-01-12 17:12:28 +0100446 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900447 for (id = 0; id < chip->ngpio; id++) {
Johan Hovoldfab28b82015-05-04 17:10:27 +0200448 desc = &chip->desc[id];
449 desc->chip = NULL;
450 if (test_bit(FLAG_REQUESTED, &desc->flags))
451 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800452 }
abdoulaye berthee1db1702014-07-05 18:28:50 +0200453 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800454 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900455
Johan Hovoldfab28b82015-05-04 17:10:27 +0200456 if (requested)
Linus Walleij58383c72015-11-04 09:56:26 +0100457 dev_crit(chip->parent,
458 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +0200459
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900460 kfree(chip->desc);
461 chip->desc = NULL;
David Brownelld2876d02008-02-04 22:28:20 -0800462}
463EXPORT_SYMBOL_GPL(gpiochip_remove);
464
Grant Likely594fa262010-06-08 07:48:16 -0600465/**
466 * gpiochip_find() - iterator for locating a specific gpio_chip
467 * @data: data to pass to match function
468 * @callback: Callback function to check gpio_chip
469 *
470 * Similar to bus_find_device. It returns a reference to a gpio_chip as
471 * determined by a user supplied @match callback. The callback should return
472 * 0 if the device doesn't match and non-zero if it does. If the callback is
473 * non-zero, this function will return to the caller and not iterate over any
474 * more gpio_chips.
475 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600476struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700477 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600478 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600479{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900480 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600481 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600482
483 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900484 list_for_each_entry(chip, &gpio_chips, list)
485 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600486 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900487
488 /* No match? */
489 if (&chip->list == &gpio_chips)
490 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600491 spin_unlock_irqrestore(&gpio_lock, flags);
492
493 return chip;
494}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600495EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800496
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900497static int gpiochip_match_name(struct gpio_chip *chip, void *data)
498{
499 const char *name = data;
500
501 return !strcmp(chip->label, name);
502}
503
504static struct gpio_chip *find_chip_by_name(const char *name)
505{
506 return gpiochip_find((void *)name, gpiochip_match_name);
507}
508
Linus Walleij14250522014-03-25 10:40:18 +0100509#ifdef CONFIG_GPIOLIB_IRQCHIP
510
511/*
512 * The following is irqchip helper code for gpiochips.
513 */
514
515/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200516 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
517 * @gpiochip: the gpiochip to set the irqchip chain to
518 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100519 * @parent_irq: the irq number corresponding to the parent IRQ for this
520 * chained irqchip
521 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200522 * coming out of the gpiochip. If the interrupt is nested rather than
523 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100524 */
525void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
526 struct irq_chip *irqchip,
527 int parent_irq,
528 irq_flow_handler_t parent_handler)
529{
Linus Walleij83141a72014-09-26 13:50:12 +0200530 unsigned int offset;
531
Linus Walleij83141a72014-09-26 13:50:12 +0200532 if (!gpiochip->irqdomain) {
533 chip_err(gpiochip, "called %s before setting up irqchip\n",
534 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200535 return;
536 }
537
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200538 if (parent_handler) {
539 if (gpiochip->can_sleep) {
540 chip_err(gpiochip,
541 "you cannot have chained interrupts on a "
542 "chip that may sleep\n");
543 return;
544 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200545 /*
546 * The parent irqchip is already using the chip_data for this
547 * irqchip, so our callbacks simply use the handler_data.
548 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +0200549 irq_set_chained_handler_and_data(parent_irq, parent_handler,
550 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300551
552 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200553 }
Linus Walleij83141a72014-09-26 13:50:12 +0200554
555 /* Set the parent IRQ for all affected IRQs */
556 for (offset = 0; offset < gpiochip->ngpio; offset++)
557 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
558 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100559}
560EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
561
562/**
563 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
564 * @d: the irqdomain used by this irqchip
565 * @irq: the global irq number used by this GPIO irqchip irq
566 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
567 *
568 * This function will set up the mapping for a certain IRQ line on a
569 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
570 * stored inside the gpiochip.
571 */
572static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
573 irq_hw_number_t hwirq)
574{
575 struct gpio_chip *chip = d->host_data;
576
Linus Walleij14250522014-03-25 10:40:18 +0100577 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300578 /*
579 * This lock class tells lockdep that GPIO irqs are in a different
580 * category than their parents, so it won't report false recursion.
581 */
582 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +0200583 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200584 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300585 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200586 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100587 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -0500588
Linus Walleij1333b902014-04-23 16:45:12 +0200589 /*
590 * No set-up of the hardware will happen if IRQ_TYPE_NONE
591 * is passed as default type.
592 */
593 if (chip->irq_default_type != IRQ_TYPE_NONE)
594 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100595
596 return 0;
597}
598
Linus Walleijc3626fd2014-03-28 20:42:01 +0100599static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
600{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200601 struct gpio_chip *chip = d->host_data;
602
Linus Walleij1c8732b2014-04-09 13:34:39 +0200603 if (chip->can_sleep)
604 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100605 irq_set_chip_and_handler(irq, NULL, NULL);
606 irq_set_chip_data(irq, NULL);
607}
608
Linus Walleij14250522014-03-25 10:40:18 +0100609static const struct irq_domain_ops gpiochip_domain_ops = {
610 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100611 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100612 /* Virtually all GPIO irqchips are twocell:ed */
613 .xlate = irq_domain_xlate_twocell,
614};
615
616static int gpiochip_irq_reqres(struct irq_data *d)
617{
618 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
619
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300620 if (!try_module_get(chip->owner))
621 return -ENODEV;
622
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900623 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100624 chip_err(chip,
625 "unable to lock HW IRQ %lu for IRQ\n",
626 d->hwirq);
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300627 module_put(chip->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100628 return -EINVAL;
629 }
630 return 0;
631}
632
633static void gpiochip_irq_relres(struct irq_data *d)
634{
635 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
636
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900637 gpiochip_unlock_as_irq(chip, d->hwirq);
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300638 module_put(chip->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100639}
640
641static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
642{
643 return irq_find_mapping(chip->irqdomain, offset);
644}
645
646/**
647 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
648 * @gpiochip: the gpiochip to remove the irqchip from
649 *
650 * This is called only from gpiochip_remove()
651 */
652static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
653{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100654 unsigned int offset;
655
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300656 acpi_gpiochip_free_interrupts(gpiochip);
657
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300658 if (gpiochip->irq_parent) {
659 irq_set_chained_handler(gpiochip->irq_parent, NULL);
660 irq_set_handler_data(gpiochip->irq_parent, NULL);
661 }
662
Linus Walleijc3626fd2014-03-28 20:42:01 +0100663 /* Remove all IRQ mappings and delete the domain */
664 if (gpiochip->irqdomain) {
665 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300666 irq_dispose_mapping(
667 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100668 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100669 }
Linus Walleij14250522014-03-25 10:40:18 +0100670
671 if (gpiochip->irqchip) {
672 gpiochip->irqchip->irq_request_resources = NULL;
673 gpiochip->irqchip->irq_release_resources = NULL;
674 gpiochip->irqchip = NULL;
675 }
676}
677
678/**
679 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
680 * @gpiochip: the gpiochip to add the irqchip to
681 * @irqchip: the irqchip to add to the gpiochip
682 * @first_irq: if not dynamically assigned, the base (first) IRQ to
683 * allocate gpiochip irqs from
684 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200685 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
686 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300687 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +0100688 *
689 * This function closely associates a certain irqchip with a certain
690 * gpiochip, providing an irq domain to translate the local IRQs to
691 * global irqs in the gpiolib core, and making sure that the gpiochip
692 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +0100693 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +0100694 * from the gpiochip passed as chip data. An irqdomain will be stored
695 * in the gpiochip that shall be used by the driver to handle IRQ number
696 * translation. The gpiochip will need to be initialized and registered
697 * before calling this function.
698 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100699 * This function will handle two cell:ed simple IRQs and assumes all
700 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100701 * need to be open coded.
702 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300703int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
704 struct irq_chip *irqchip,
705 unsigned int first_irq,
706 irq_flow_handler_t handler,
707 unsigned int type,
708 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +0100709{
710 struct device_node *of_node;
711 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100712 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100713
714 if (!gpiochip || !irqchip)
715 return -EINVAL;
716
Linus Walleij58383c72015-11-04 09:56:26 +0100717 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +0100718 pr_err("missing gpiochip .dev parent pointer\n");
719 return -EINVAL;
720 }
Linus Walleij58383c72015-11-04 09:56:26 +0100721 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +0100722#ifdef CONFIG_OF_GPIO
723 /*
Colin Cronin20a8a962015-05-18 11:41:43 -0700724 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800725 * FIXME: get rid of this and use gpiochip->parent->of_node
726 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +0100727 */
728 if (gpiochip->of_node)
729 of_node = gpiochip->of_node;
730#endif
731 gpiochip->irqchip = irqchip;
732 gpiochip->irq_handler = handler;
733 gpiochip->irq_default_type = type;
734 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300735 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +0100736 gpiochip->irqdomain = irq_domain_add_simple(of_node,
737 gpiochip->ngpio, first_irq,
738 &gpiochip_domain_ops, gpiochip);
739 if (!gpiochip->irqdomain) {
740 gpiochip->irqchip = NULL;
741 return -EINVAL;
742 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +0200743
744 /*
745 * It is possible for a driver to override this, but only if the
746 * alternative functions are both implemented.
747 */
748 if (!irqchip->irq_request_resources &&
749 !irqchip->irq_release_resources) {
750 irqchip->irq_request_resources = gpiochip_irq_reqres;
751 irqchip->irq_release_resources = gpiochip_irq_relres;
752 }
Linus Walleij14250522014-03-25 10:40:18 +0100753
754 /*
755 * Prepare the mapping since the irqchip shall be orthogonal to
756 * any gpiochip calls. If the first_irq was zero, this is
757 * necessary to allocate descriptors for all IRQs.
758 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100759 for (offset = 0; offset < gpiochip->ngpio; offset++) {
760 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
761 if (offset == 0)
762 /*
763 * Store the base into the gpiochip to be used when
764 * unmapping the irqs.
765 */
766 gpiochip->irq_base = irq_base;
767 }
Linus Walleij14250522014-03-25 10:40:18 +0100768
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300769 acpi_gpiochip_request_interrupts(gpiochip);
770
Linus Walleij14250522014-03-25 10:40:18 +0100771 return 0;
772}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300773EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +0100774
775#else /* CONFIG_GPIOLIB_IRQCHIP */
776
777static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
778
779#endif /* CONFIG_GPIOLIB_IRQCHIP */
780
Jonas Gorskic771c2f2015-10-11 17:34:15 +0200781/**
782 * gpiochip_generic_request() - request the gpio function for a pin
783 * @chip: the gpiochip owning the GPIO
784 * @offset: the offset of the GPIO to request for GPIO function
785 */
786int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
787{
788 return pinctrl_request_gpio(chip->base + offset);
789}
790EXPORT_SYMBOL_GPL(gpiochip_generic_request);
791
792/**
793 * gpiochip_generic_free() - free the gpio function from a pin
794 * @chip: the gpiochip to request the gpio function for
795 * @offset: the offset of the GPIO to free from GPIO function
796 */
797void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
798{
799 pinctrl_free_gpio(chip->base + offset);
800}
801EXPORT_SYMBOL_GPL(gpiochip_generic_free);
802
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530803#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100804
Linus Walleij3f0f8672012-11-20 12:40:15 +0100805/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200806 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
807 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +0200808 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +0200809 * @gpio_offset: the start offset in the current gpio_chip number space
810 * @pin_group: name of the pin group inside the pin controller
811 */
812int gpiochip_add_pingroup_range(struct gpio_chip *chip,
813 struct pinctrl_dev *pctldev,
814 unsigned int gpio_offset, const char *pin_group)
815{
816 struct gpio_pin_range *pin_range;
817 int ret;
818
819 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
820 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200821 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200822 return -ENOMEM;
823 }
824
825 /* Use local offset as range ID */
826 pin_range->range.id = gpio_offset;
827 pin_range->range.gc = chip;
828 pin_range->range.name = chip->label;
829 pin_range->range.base = chip->base + gpio_offset;
830 pin_range->pctldev = pctldev;
831
832 ret = pinctrl_get_group_pins(pctldev, pin_group,
833 &pin_range->range.pins,
834 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100835 if (ret < 0) {
836 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200837 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100838 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200839
840 pinctrl_add_gpio_range(pctldev, &pin_range->range);
841
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200842 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
843 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200844 pinctrl_dev_get_devname(pctldev), pin_group);
845
846 list_add_tail(&pin_range->node, &chip->pin_ranges);
847
848 return 0;
849}
850EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
851
852/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100853 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
854 * @chip: the gpiochip to add the range for
855 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100856 * @gpio_offset: the start offset in the current gpio_chip number space
857 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100858 * @npins: the number of pins from the offset of each pin space (GPIO and
859 * pin controller) to accumulate in this range
860 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100861int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100862 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100863 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530864{
865 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800866 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530867
Linus Walleij3f0f8672012-11-20 12:40:15 +0100868 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530869 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200870 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100871 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530872 }
873
Linus Walleij3f0f8672012-11-20 12:40:15 +0100874 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100875 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100876 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530877 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100878 pin_range->range.base = chip->base + gpio_offset;
879 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530880 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100881 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530882 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100883 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800884 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200885 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100886 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800887 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100888 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200889 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
890 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100891 pinctl_name,
892 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530893
894 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100895
896 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530897}
Linus Walleij165adc92012-11-06 14:49:39 +0100898EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530899
Linus Walleij3f0f8672012-11-20 12:40:15 +0100900/**
901 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
902 * @chip: the chip to remove all the mappings for
903 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530904void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
905{
906 struct gpio_pin_range *pin_range, *tmp;
907
908 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
909 list_del(&pin_range->node);
910 pinctrl_remove_gpio_range(pin_range->pctldev,
911 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100912 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530913 }
914}
Linus Walleij165adc92012-11-06 14:49:39 +0100915EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
916
917#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530918
David Brownelld2876d02008-02-04 22:28:20 -0800919/* These "optional" allocation calls help prevent drivers from stomping
920 * on each other, and help provide better diagnostics in debugfs.
921 * They're called even less than the "set direction" calls.
922 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200923static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800924{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200925 struct gpio_chip *chip = desc->chip;
926 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800927 unsigned long flags;
928
929 spin_lock_irqsave(&gpio_lock, flags);
930
David Brownelld2876d02008-02-04 22:28:20 -0800931 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700932 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800933 */
934
935 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
936 desc_set_label(desc, label ? : "?");
937 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700938 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800939 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800940 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700941 }
942
943 if (chip->request) {
944 /* chip->request may sleep */
945 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900946 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700947 spin_lock_irqsave(&gpio_lock, flags);
948
949 if (status < 0) {
950 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700951 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300952 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700953 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700954 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300955 if (chip->get_direction) {
956 /* chip->get_direction may sleep */
957 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900958 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300959 spin_lock_irqsave(&gpio_lock, flags);
960 }
David Brownelld2876d02008-02-04 22:28:20 -0800961done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +0300962 if (status < 0) {
963 /* Clear flags that might have been set by the caller before
964 * requesting the GPIO.
965 */
966 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
967 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
968 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
969 }
Mika Westerberg77c2d792014-03-10 14:54:50 +0200970 spin_unlock_irqrestore(&gpio_lock, flags);
971 return status;
972}
973
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900974int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200975{
976 int status = -EPROBE_DEFER;
977 struct gpio_chip *chip;
978
979 if (!desc) {
980 pr_warn("%s: invalid GPIO\n", __func__);
981 return -EINVAL;
982 }
983
984 chip = desc->chip;
985 if (!chip)
986 goto done;
987
988 if (try_module_get(chip->owner)) {
989 status = __gpiod_request(desc, label);
990 if (status < 0)
991 module_put(chip->owner);
992 }
993
994done:
David Brownelld2876d02008-02-04 22:28:20 -0800995 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200996 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200997
David Brownelld2876d02008-02-04 22:28:20 -0800998 return status;
999}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001000
Mika Westerberg77c2d792014-03-10 14:54:50 +02001001static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001002{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001003 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001004 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001005 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001006
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001007 might_sleep();
1008
Alexandre Courbot372e7222013-02-03 01:29:29 +09001009 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001010
David Brownelld2876d02008-02-04 22:28:20 -08001011 spin_lock_irqsave(&gpio_lock, flags);
1012
David Brownell35e8bb52008-10-15 22:03:16 -07001013 chip = desc->chip;
1014 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1015 if (chip->free) {
1016 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001017 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001018 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001019 spin_lock_irqsave(&gpio_lock, flags);
1020 }
David Brownelld2876d02008-02-04 22:28:20 -08001021 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001022 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001023 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301024 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301025 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001026 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001027 ret = true;
1028 }
David Brownelld2876d02008-02-04 22:28:20 -08001029
1030 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001031 return ret;
1032}
1033
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001034void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001035{
1036 if (desc && __gpiod_free(desc))
1037 module_put(desc->chip->owner);
1038 else
1039 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -08001040}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001041
David Brownelld2876d02008-02-04 22:28:20 -08001042/**
1043 * gpiochip_is_requested - return string iff signal was requested
1044 * @chip: controller managing the signal
1045 * @offset: of signal within controller's 0..(ngpio - 1) range
1046 *
1047 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001048 * The string returned is the label passed to gpio_request(); if none has been
1049 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001050 *
1051 * This function is for use by GPIO controller drivers. The label can
1052 * help with diagnostics, and knowing that the signal is used as a GPIO
1053 * can help avoid accidentally multiplexing it to another controller.
1054 */
1055const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1056{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001057 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001058
Dirk Behme48b59532015-08-18 18:02:32 +02001059 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001060 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001061
1062 desc = &chip->desc[offset];
1063
Alexandre Courbot372e7222013-02-03 01:29:29 +09001064 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001065 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001066 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001067}
1068EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1069
Mika Westerberg77c2d792014-03-10 14:54:50 +02001070/**
1071 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1072 * @desc: GPIO descriptor to request
1073 * @label: label for the GPIO
1074 *
1075 * Function allows GPIO chip drivers to request and use their own GPIO
1076 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1077 * function will not increase reference count of the GPIO chip module. This
1078 * allows the GPIO chip module to be unloaded as needed (we assume that the
1079 * GPIO chip driver handles freeing the GPIOs it has requested).
1080 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001081struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1082 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001083{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001084 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1085 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001086
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001087 if (IS_ERR(desc)) {
1088 chip_err(chip, "failed to get GPIO descriptor\n");
1089 return desc;
1090 }
1091
1092 err = __gpiod_request(desc, label);
1093 if (err < 0)
1094 return ERR_PTR(err);
1095
1096 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001097}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001098EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001099
1100/**
1101 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1102 * @desc: GPIO descriptor to free
1103 *
1104 * Function frees the given GPIO requested previously with
1105 * gpiochip_request_own_desc().
1106 */
1107void gpiochip_free_own_desc(struct gpio_desc *desc)
1108{
1109 if (desc)
1110 __gpiod_free(desc);
1111}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001112EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08001113
1114/* Drivers MUST set GPIO direction before making get/set calls. In
1115 * some cases this is done in early boot, before IRQs are enabled.
1116 *
1117 * As a rule these aren't called more than once (except for drivers
1118 * using the open-drain emulation idiom) so these are natural places
1119 * to accumulate extra debugging checks. Note that we can't (yet)
1120 * rely on gpio_request() having been called beforehand.
1121 */
1122
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001123/**
1124 * gpiod_direction_input - set the GPIO direction to input
1125 * @desc: GPIO to set to input
1126 *
1127 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1128 * be called safely on it.
1129 *
1130 * Return 0 in case of success, else an error code.
1131 */
1132int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001133{
David Brownelld2876d02008-02-04 22:28:20 -08001134 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001135 int status = -EINVAL;
1136
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001137 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001138 pr_warn("%s: invalid GPIO\n", __func__);
1139 return -EINVAL;
1140 }
1141
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001142 chip = desc->chip;
1143 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001144 gpiod_warn(desc,
1145 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001146 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001147 return -EIO;
1148 }
1149
Alexandre Courbotd82da792014-07-22 16:17:43 +09001150 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001151 if (status == 0)
1152 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001153
Alexandre Courbot372e7222013-02-03 01:29:29 +09001154 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001155
David Brownelld2876d02008-02-04 22:28:20 -08001156 return status;
1157}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001158EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001159
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001160static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001161{
David Brownelld2876d02008-02-04 22:28:20 -08001162 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001163 int status = -EINVAL;
1164
Linus Walleijd468bf92013-09-24 11:54:38 +02001165 /* GPIOs used for IRQs shall not be set as output */
1166 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1167 gpiod_err(desc,
1168 "%s: tried to set a GPIO tied to an IRQ as output\n",
1169 __func__);
1170 return -EIO;
1171 }
1172
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301173 /* Open drain pin should not be driven to 1 */
1174 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001175 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301176
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301177 /* Open source pin should not be driven to 0 */
1178 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001179 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301180
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001181 chip = desc->chip;
1182 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001183 gpiod_warn(desc,
1184 "%s: missing set() or direction_output() operations\n",
1185 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001186 return -EIO;
1187 }
1188
Alexandre Courbotd82da792014-07-22 16:17:43 +09001189 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001190 if (status == 0)
1191 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001192 trace_gpio_value(desc_to_gpio(desc), 0, value);
1193 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001194 return status;
1195}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001196
1197/**
1198 * gpiod_direction_output_raw - set the GPIO direction to output
1199 * @desc: GPIO to set to output
1200 * @value: initial output value of the GPIO
1201 *
1202 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1203 * be called safely on it. The initial value of the output must be specified
1204 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1205 *
1206 * Return 0 in case of success, else an error code.
1207 */
1208int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1209{
1210 if (!desc || !desc->chip) {
1211 pr_warn("%s: invalid GPIO\n", __func__);
1212 return -EINVAL;
1213 }
1214 return _gpiod_direction_output_raw(desc, value);
1215}
1216EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1217
1218/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301219 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001220 * @desc: GPIO to set to output
1221 * @value: initial output value of the GPIO
1222 *
1223 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1224 * be called safely on it. The initial value of the output must be specified
1225 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1226 * account.
1227 *
1228 * Return 0 in case of success, else an error code.
1229 */
1230int gpiod_direction_output(struct gpio_desc *desc, int value)
1231{
1232 if (!desc || !desc->chip) {
1233 pr_warn("%s: invalid GPIO\n", __func__);
1234 return -EINVAL;
1235 }
1236 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1237 value = !value;
1238 return _gpiod_direction_output_raw(desc, value);
1239}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001240EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001241
Felipe Balbic4b5be92010-05-26 14:42:23 -07001242/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001243 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001244 * @gpio: the gpio to set debounce time
1245 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001246 *
1247 * returns -ENOTSUPP if the controller does not support setting
1248 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001249 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001250int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001251{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001252 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001253
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001254 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001255 pr_warn("%s: invalid GPIO\n", __func__);
1256 return -EINVAL;
1257 }
1258
Felipe Balbic4b5be92010-05-26 14:42:23 -07001259 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001260 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001261 gpiod_dbg(desc,
1262 "%s: missing set() or set_debounce() operations\n",
1263 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001264 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001265 }
1266
Alexandre Courbotd82da792014-07-22 16:17:43 +09001267 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001268}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001269EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001270
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001271/**
1272 * gpiod_is_active_low - test whether a GPIO is active-low or not
1273 * @desc: the gpio descriptor to test
1274 *
1275 * Returns 1 if the GPIO is active-low, 0 otherwise.
1276 */
1277int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001278{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001279 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001280}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001281EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001282
1283/* I/O calls are only valid after configuration completed; the relevant
1284 * "is this a valid GPIO" error checks should already have been done.
1285 *
1286 * "Get" operations are often inlinable as reading a pin value register,
1287 * and masking the relevant bit in that register.
1288 *
1289 * When "set" operations are inlinable, they involve writing that mask to
1290 * one register to set a low value, or a different register to set it high.
1291 * Otherwise locking is needed, so there may be little value to inlining.
1292 *
1293 *------------------------------------------------------------------------
1294 *
1295 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1296 * have requested the GPIO. That can include implicit requesting by
1297 * a direction setting call. Marking a gpio as requested locks its chip
1298 * in memory, guaranteeing that these table lookups need no more locking
1299 * and that gpiochip_remove() will fail.
1300 *
1301 * REVISIT when debugging, consider adding some instrumentation to ensure
1302 * that the GPIO was actually requested.
1303 */
1304
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001305static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001306{
1307 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001308 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001309 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001310
Alexandre Courbot372e7222013-02-03 01:29:29 +09001311 chip = desc->chip;
1312 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001313 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01001314 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001315 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001316 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001317}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001318
David Brownelld2876d02008-02-04 22:28:20 -08001319/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001320 * gpiod_get_raw_value() - return a gpio's raw value
1321 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001322 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001323 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001324 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001325 *
1326 * This function should be called from contexts where we cannot sleep, and will
1327 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001328 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001329int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001330{
David Brownelld2876d02008-02-04 22:28:20 -08001331 if (!desc)
1332 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001333 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001334 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001335 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001336}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001337EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001338
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001339/**
1340 * gpiod_get_value() - return a gpio's value
1341 * @desc: gpio whose value will be returned
1342 *
1343 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001344 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001345 *
1346 * This function should be called from contexts where we cannot sleep, and will
1347 * complain if the GPIO chip functions potentially sleep.
1348 */
1349int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001350{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001351 int value;
1352 if (!desc)
1353 return 0;
1354 /* Should be using gpio_get_value_cansleep() */
1355 WARN_ON(desc->chip->can_sleep);
1356
1357 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001358 if (value < 0)
1359 return value;
1360
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001361 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1362 value = !value;
1363
1364 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001365}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001366EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001367
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301368/*
1369 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001370 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001371 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301372 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001373static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301374{
1375 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001376 struct gpio_chip *chip = desc->chip;
1377 int offset = gpio_chip_hwgpio(desc);
1378
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301379 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001380 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301381 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001382 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301383 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001384 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301385 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001386 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301387 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001388 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301389 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001390 gpiod_err(desc,
1391 "%s: Error in set_value for open drain err %d\n",
1392 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301393}
1394
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301395/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001396 * _gpio_set_open_source_value() - Set the open source gpio's value.
1397 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001398 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301399 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001400static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301401{
1402 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001403 struct gpio_chip *chip = desc->chip;
1404 int offset = gpio_chip_hwgpio(desc);
1405
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301406 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001407 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301408 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001409 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301410 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001411 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301412 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001413 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301414 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001415 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301416 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001417 gpiod_err(desc,
1418 "%s: Error in set_value for open source err %d\n",
1419 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301420}
1421
Alexandre Courbot23600962014-03-11 15:52:09 +09001422static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001423{
1424 struct gpio_chip *chip;
1425
Alexandre Courbot372e7222013-02-03 01:29:29 +09001426 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001427 trace_gpio_value(desc_to_gpio(desc), 0, value);
1428 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1429 _gpio_set_open_drain_value(desc, value);
1430 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1431 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301432 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001433 chip->set(chip, gpio_chip_hwgpio(desc), value);
1434}
1435
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001436/*
1437 * set multiple outputs on the same chip;
1438 * use the chip's set_multiple function if available;
1439 * otherwise set the outputs sequentially;
1440 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1441 * defines which outputs are to be changed
1442 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1443 * defines the values the outputs specified by mask are to be set to
1444 */
1445static void gpio_chip_set_multiple(struct gpio_chip *chip,
1446 unsigned long *mask, unsigned long *bits)
1447{
1448 if (chip->set_multiple) {
1449 chip->set_multiple(chip, mask, bits);
1450 } else {
1451 int i;
1452 for (i = 0; i < chip->ngpio; i++) {
1453 if (mask[BIT_WORD(i)] == 0) {
1454 /* no more set bits in this mask word;
1455 * skip ahead to the next word */
1456 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1457 continue;
1458 }
1459 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001460 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001461 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001462 }
1463 }
1464}
1465
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001466static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1467 unsigned int array_size,
1468 struct gpio_desc **desc_array,
1469 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001470{
1471 int i = 0;
1472
1473 while (i < array_size) {
1474 struct gpio_chip *chip = desc_array[i]->chip;
1475 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1476 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1477 int count = 0;
1478
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001479 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001480 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001481
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001482 memset(mask, 0, sizeof(mask));
1483 do {
1484 struct gpio_desc *desc = desc_array[i];
1485 int hwgpio = gpio_chip_hwgpio(desc);
1486 int value = value_array[i];
1487
1488 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1489 value = !value;
1490 trace_gpio_value(desc_to_gpio(desc), 0, value);
1491 /*
1492 * collect all normal outputs belonging to the same chip
1493 * open drain and open source outputs are set individually
1494 */
1495 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001496 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001497 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1498 _gpio_set_open_source_value(desc, value);
1499 } else {
1500 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001501 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001502 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001503 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001504 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001505 count++;
1506 }
1507 i++;
1508 } while ((i < array_size) && (desc_array[i]->chip == chip));
1509 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001510 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001511 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001512 }
1513}
1514
David Brownelld2876d02008-02-04 22:28:20 -08001515/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001516 * gpiod_set_raw_value() - assign a gpio's raw value
1517 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001518 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001519 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001520 * Set the raw value of the GPIO, i.e. the value of its physical line without
1521 * regard for its ACTIVE_LOW status.
1522 *
1523 * This function should be called from contexts where we cannot sleep, and will
1524 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001525 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001526void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001527{
David Brownelld2876d02008-02-04 22:28:20 -08001528 if (!desc)
1529 return;
David Brownelld2876d02008-02-04 22:28:20 -08001530 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001531 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001532 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001533}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001534EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001535
1536/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001537 * gpiod_set_value() - assign a gpio's value
1538 * @desc: gpio whose value will be assigned
1539 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001540 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001541 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1542 * account
1543 *
1544 * This function should be called from contexts where we cannot sleep, and will
1545 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001546 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001547void gpiod_set_value(struct gpio_desc *desc, int value)
1548{
1549 if (!desc)
1550 return;
1551 /* Should be using gpio_set_value_cansleep() */
1552 WARN_ON(desc->chip->can_sleep);
1553 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1554 value = !value;
1555 _gpiod_set_raw_value(desc, value);
1556}
1557EXPORT_SYMBOL_GPL(gpiod_set_value);
1558
1559/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001560 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001561 * @array_size: number of elements in the descriptor / value arrays
1562 * @desc_array: array of GPIO descriptors whose values will be assigned
1563 * @value_array: array of values to assign
1564 *
1565 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1566 * without regard for their ACTIVE_LOW status.
1567 *
1568 * This function should be called from contexts where we cannot sleep, and will
1569 * complain if the GPIO chip functions potentially sleep.
1570 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001571void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001572 struct gpio_desc **desc_array, int *value_array)
1573{
1574 if (!desc_array)
1575 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001576 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1577 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001578}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001579EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001580
1581/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001582 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001583 * @array_size: number of elements in the descriptor / value arrays
1584 * @desc_array: array of GPIO descriptors whose values will be assigned
1585 * @value_array: array of values to assign
1586 *
1587 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1588 * into account.
1589 *
1590 * This function should be called from contexts where we cannot sleep, and will
1591 * complain if the GPIO chip functions potentially sleep.
1592 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001593void gpiod_set_array_value(unsigned int array_size,
1594 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001595{
1596 if (!desc_array)
1597 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001598 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1599 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001600}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001601EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001602
1603/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001604 * gpiod_cansleep() - report whether gpio value access may sleep
1605 * @desc: gpio to check
1606 *
1607 */
1608int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001609{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001610 if (!desc)
1611 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001612 return desc->chip->can_sleep;
1613}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001614EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001615
David Brownell0f6d5042008-10-15 22:03:14 -07001616/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001617 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1618 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001619 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001620 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1621 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001622 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001623int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001624{
1625 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001626 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001627
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001628 if (!desc)
1629 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001630 chip = desc->chip;
1631 offset = gpio_chip_hwgpio(desc);
1632 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1633}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001634EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001635
Linus Walleijd468bf92013-09-24 11:54:38 +02001636/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001637 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001638 * @chip: the chip the GPIO to lock belongs to
1639 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001640 *
1641 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001642 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001643 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001644int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001645{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001646 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001647 return -EINVAL;
1648
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001649 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1650 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001651 "%s: tried to flag a GPIO set as output for IRQ\n",
1652 __func__);
1653 return -EIO;
1654 }
1655
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001656 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001657 return 0;
1658}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001659EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001660
Linus Walleijd468bf92013-09-24 11:54:38 +02001661/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001662 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001663 * @chip: the chip the GPIO to lock belongs to
1664 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001665 *
1666 * This is used directly by GPIO drivers that want to indicate
1667 * that a certain GPIO is no longer used exclusively for IRQ.
1668 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001669void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001670{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001671 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001672 return;
1673
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001674 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001675}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001676EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001677
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001678/**
1679 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1680 * @desc: gpio whose value will be returned
1681 *
1682 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001683 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001684 *
1685 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001686 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001687int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001688{
David Brownelld2876d02008-02-04 22:28:20 -08001689 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001690 if (!desc)
1691 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001692 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001693}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001694EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001695
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001696/**
1697 * gpiod_get_value_cansleep() - return a gpio's value
1698 * @desc: gpio whose value will be returned
1699 *
1700 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001701 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001702 *
1703 * This function is to be called from contexts that can sleep.
1704 */
1705int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001706{
David Brownelld2876d02008-02-04 22:28:20 -08001707 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001708
1709 might_sleep_if(extra_checks);
1710 if (!desc)
1711 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001712
1713 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001714 if (value < 0)
1715 return value;
1716
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001717 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1718 value = !value;
1719
David Brownelld2876d02008-02-04 22:28:20 -08001720 return value;
1721}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001722EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001723
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001724/**
1725 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1726 * @desc: gpio whose value will be assigned
1727 * @value: value to assign
1728 *
1729 * Set the raw value of the GPIO, i.e. the value of its physical line without
1730 * regard for its ACTIVE_LOW status.
1731 *
1732 * This function is to be called from contexts that can sleep.
1733 */
1734void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001735{
David Brownelld2876d02008-02-04 22:28:20 -08001736 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001737 if (!desc)
1738 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001739 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001740}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001741EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001742
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001743/**
1744 * gpiod_set_value_cansleep() - assign a gpio's value
1745 * @desc: gpio whose value will be assigned
1746 * @value: value to assign
1747 *
1748 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1749 * account
1750 *
1751 * This function is to be called from contexts that can sleep.
1752 */
1753void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001754{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001755 might_sleep_if(extra_checks);
1756 if (!desc)
1757 return;
1758
1759 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1760 value = !value;
1761 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001762}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001763EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001764
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001765/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001766 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001767 * @array_size: number of elements in the descriptor / value arrays
1768 * @desc_array: array of GPIO descriptors whose values will be assigned
1769 * @value_array: array of values to assign
1770 *
1771 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1772 * without regard for their ACTIVE_LOW status.
1773 *
1774 * This function is to be called from contexts that can sleep.
1775 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001776void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1777 struct gpio_desc **desc_array,
1778 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001779{
1780 might_sleep_if(extra_checks);
1781 if (!desc_array)
1782 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001783 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1784 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001785}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001786EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001787
1788/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001789 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001790 * @array_size: number of elements in the descriptor / value arrays
1791 * @desc_array: array of GPIO descriptors whose values will be assigned
1792 * @value_array: array of values to assign
1793 *
1794 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1795 * into account.
1796 *
1797 * This function is to be called from contexts that can sleep.
1798 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001799void gpiod_set_array_value_cansleep(unsigned int array_size,
1800 struct gpio_desc **desc_array,
1801 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001802{
1803 might_sleep_if(extra_checks);
1804 if (!desc_array)
1805 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001806 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1807 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001808}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001809EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001810
1811/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001812 * gpiod_add_lookup_table() - register GPIO device consumers
1813 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001814 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001815void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001816{
1817 mutex_lock(&gpio_lookup_lock);
1818
Alexandre Courbotad824782013-12-03 12:20:11 +09001819 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001820
1821 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001822}
1823
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05301824/**
1825 * gpiod_remove_lookup_table() - unregister GPIO device consumers
1826 * @table: table of consumers to unregister
1827 */
1828void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
1829{
1830 mutex_lock(&gpio_lookup_lock);
1831
1832 list_del(&table->list);
1833
1834 mutex_unlock(&gpio_lookup_lock);
1835}
1836
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001837static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001838 unsigned int idx,
1839 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001840{
1841 char prop_name[32]; /* 32 is max size of property name */
1842 enum of_gpio_flags of_flags;
1843 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001844 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001845
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001846 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02001847 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01001848 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001849 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02001850 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01001851 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001852 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001853
Thierry Redingdd34c372014-04-23 17:28:09 +02001854 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1855 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001856 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001857 break;
1858 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001859
1860 if (IS_ERR(desc))
1861 return desc;
1862
1863 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001864 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001865
Laurent Pinchart90b665f2015-10-13 00:20:21 +03001866 if (of_flags & OF_GPIO_SINGLE_ENDED) {
1867 if (of_flags & OF_GPIO_ACTIVE_LOW)
1868 *flags |= GPIO_OPEN_DRAIN;
1869 else
1870 *flags |= GPIO_OPEN_SOURCE;
1871 }
1872
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001873 return desc;
1874}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001875
Mika Westerberg81f59e92013-10-10 11:01:09 +03001876static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001877 unsigned int idx,
1878 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001879{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001880 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03001881 struct acpi_gpio_info info;
1882 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001883 char propname[32];
1884 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03001885
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001886 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001887 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001888 if (con_id && strcmp(con_id, "gpios")) {
1889 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001890 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001891 } else {
1892 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001893 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001894 }
Mika Westerberge01f4402013-10-10 11:01:10 +03001895
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001896 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1897 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1898 break;
1899 }
1900
1901 /* Then from plain _CRS GPIOs */
1902 if (IS_ERR(desc)) {
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08001903 if (!acpi_can_fallback_to_crs(adev, con_id))
1904 return ERR_PTR(-ENOENT);
1905
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001906 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1907 if (IS_ERR(desc))
1908 return desc;
1909 }
1910
Christophe RICARD52044722015-12-23 23:25:34 +01001911 if (info.polarity == GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001912 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001913
1914 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001915}
1916
Alexandre Courbotad824782013-12-03 12:20:11 +09001917static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1918{
1919 const char *dev_id = dev ? dev_name(dev) : NULL;
1920 struct gpiod_lookup_table *table;
1921
1922 mutex_lock(&gpio_lookup_lock);
1923
1924 list_for_each_entry(table, &gpio_lookup_list, list) {
1925 if (table->dev_id && dev_id) {
1926 /*
1927 * Valid strings on both ends, must be identical to have
1928 * a match
1929 */
1930 if (!strcmp(table->dev_id, dev_id))
1931 goto found;
1932 } else {
1933 /*
1934 * One of the pointers is NULL, so both must be to have
1935 * a match
1936 */
1937 if (dev_id == table->dev_id)
1938 goto found;
1939 }
1940 }
1941 table = NULL;
1942
1943found:
1944 mutex_unlock(&gpio_lookup_lock);
1945 return table;
1946}
1947
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001948static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001949 unsigned int idx,
1950 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001951{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001952 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001953 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001954 struct gpiod_lookup *p;
1955
Alexandre Courbotad824782013-12-03 12:20:11 +09001956 table = gpiod_find_lookup_table(dev);
1957 if (!table)
1958 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001959
Alexandre Courbotad824782013-12-03 12:20:11 +09001960 for (p = &table->table[0]; p->chip_label; p++) {
1961 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001962
Alexandre Courbotad824782013-12-03 12:20:11 +09001963 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001964 if (p->idx != idx)
1965 continue;
1966
Alexandre Courbotad824782013-12-03 12:20:11 +09001967 /* If the lookup entry has a con_id, require exact match */
1968 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1969 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001970
Alexandre Courbotad824782013-12-03 12:20:11 +09001971 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001972
Alexandre Courbotad824782013-12-03 12:20:11 +09001973 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001974 dev_err(dev, "cannot find GPIO chip %s\n",
1975 p->chip_label);
1976 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001977 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001978
Alexandre Courbotad824782013-12-03 12:20:11 +09001979 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001980 dev_err(dev,
1981 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1982 idx, chip->ngpio, chip->label);
1983 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001984 }
1985
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001986 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001987 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001988
1989 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001990 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001991
1992 return desc;
1993}
1994
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001995static int dt_gpio_count(struct device *dev, const char *con_id)
1996{
1997 int ret;
1998 char propname[32];
1999 unsigned int i;
2000
2001 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2002 if (con_id)
2003 snprintf(propname, sizeof(propname), "%s-%s",
2004 con_id, gpio_suffixes[i]);
2005 else
2006 snprintf(propname, sizeof(propname), "%s",
2007 gpio_suffixes[i]);
2008
2009 ret = of_gpio_named_count(dev->of_node, propname);
2010 if (ret >= 0)
2011 break;
2012 }
2013 return ret;
2014}
2015
2016static int platform_gpio_count(struct device *dev, const char *con_id)
2017{
2018 struct gpiod_lookup_table *table;
2019 struct gpiod_lookup *p;
2020 unsigned int count = 0;
2021
2022 table = gpiod_find_lookup_table(dev);
2023 if (!table)
2024 return -ENOENT;
2025
2026 for (p = &table->table[0]; p->chip_label; p++) {
2027 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2028 (!con_id && !p->con_id))
2029 count++;
2030 }
2031 if (!count)
2032 return -ENOENT;
2033
2034 return count;
2035}
2036
2037/**
2038 * gpiod_count - return the number of GPIOs associated with a device / function
2039 * or -ENOENT if no GPIO has been assigned to the requested function
2040 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2041 * @con_id: function within the GPIO consumer
2042 */
2043int gpiod_count(struct device *dev, const char *con_id)
2044{
2045 int count = -ENOENT;
2046
2047 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2048 count = dt_gpio_count(dev, con_id);
2049 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2050 count = acpi_gpio_count(dev, con_id);
2051
2052 if (count < 0)
2053 count = platform_gpio_count(dev, con_id);
2054
2055 return count;
2056}
2057EXPORT_SYMBOL_GPL(gpiod_count);
2058
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002059/**
Thierry Reding08791622014-04-25 16:54:22 +02002060 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002061 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002062 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002063 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002064 *
2065 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002066 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07002067 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002068 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002069struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002070 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002071{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002072 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002073}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002074EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002075
2076/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002077 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2078 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2079 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002080 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002081 *
2082 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2083 * the requested function it will return NULL. This is convenient for drivers
2084 * that need to handle optional GPIOs.
2085 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002086struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002087 const char *con_id,
2088 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002089{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002090 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002091}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002092EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002093
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002094/**
2095 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2096 * @desc: gpio to be setup
2097 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2098 * of_get_gpio_hog()
2099 *
2100 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2101 */
2102static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2103{
2104 if (lflags & GPIO_ACTIVE_LOW)
2105 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2106 if (lflags & GPIO_OPEN_DRAIN)
2107 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2108 if (lflags & GPIO_OPEN_SOURCE)
2109 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2110}
Benoit Parrotf625d462015-02-02 11:44:44 -06002111
2112/**
2113 * gpiod_configure_flags - helper function to configure a given GPIO
2114 * @desc: gpio whose value will be assigned
2115 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06002116 * @dflags: gpiod_flags - optional GPIO initialization flags
2117 *
2118 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2119 * requested function and/or index, or another IS_ERR() code if an error
2120 * occurred while trying to acquire the GPIO.
2121 */
2122static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002123 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06002124{
2125 int status;
2126
Benoit Parrotf625d462015-02-02 11:44:44 -06002127 /* No particular flag request, return here... */
2128 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2129 pr_debug("no flags found for %s\n", con_id);
2130 return 0;
2131 }
2132
2133 /* Process flags */
2134 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2135 status = gpiod_direction_output(desc,
2136 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2137 else
2138 status = gpiod_direction_input(desc);
2139
2140 return status;
2141}
2142
Thierry Reding29a1f2332014-04-25 17:10:06 +02002143/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002144 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002145 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002146 * @con_id: function within the GPIO consumer
2147 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002148 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002149 *
2150 * This variant of gpiod_get() allows to access GPIOs other than the first
2151 * defined one for functions that define several GPIOs.
2152 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002153 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2154 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07002155 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002156 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002157struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002158 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002159 unsigned int idx,
2160 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002161{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002162 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002163 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002164 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002165
2166 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2167
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002168 if (dev) {
2169 /* Using device tree? */
2170 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2171 dev_dbg(dev, "using device tree for GPIO lookup\n");
2172 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2173 } else if (ACPI_COMPANION(dev)) {
2174 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2175 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2176 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002177 }
2178
2179 /*
2180 * Either we are not using DT or ACPI, or their lookup did not return
2181 * a result. In that case, use platform lookup as a fallback.
2182 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002183 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002184 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002185 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002186 }
2187
2188 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002189 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002190 return desc;
2191 }
2192
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002193 gpiod_parse_flags(desc, lookupflags);
2194
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002195 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002196 if (status < 0)
2197 return ERR_PTR(status);
2198
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002199 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002200 if (status < 0) {
2201 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2202 gpiod_put(desc);
2203 return ERR_PTR(status);
2204 }
2205
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002206 return desc;
2207}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002208EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002209
2210/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002211 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2212 * @fwnode: handle of the firmware node
2213 * @propname: name of the firmware property representing the GPIO
2214 *
2215 * This function can be used for drivers that get their configuration
2216 * from firmware.
2217 *
2218 * Function properly finds the corresponding GPIO using whatever is the
2219 * underlying firmware interface and then makes sure that the GPIO
2220 * descriptor is requested before it is returned to the caller.
2221 *
2222 * In case of error an ERR_PTR() is returned.
2223 */
2224struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2225 const char *propname)
2226{
2227 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2228 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002229 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02002230 int ret;
2231
2232 if (!fwnode)
2233 return ERR_PTR(-EINVAL);
2234
2235 if (is_of_node(fwnode)) {
2236 enum of_gpio_flags flags;
2237
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002238 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002239 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002240 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02002241 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002242 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2243 }
Mika Westerberg40b73182014-10-21 13:33:59 +02002244 } else if (is_acpi_node(fwnode)) {
2245 struct acpi_gpio_info info;
2246
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02002247 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02002248 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01002249 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02002250 }
2251
2252 if (IS_ERR(desc))
2253 return desc;
2254
Mika Westerberg40b73182014-10-21 13:33:59 +02002255 if (active_low)
2256 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2257
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002258 if (single_ended) {
2259 if (active_low)
2260 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2261 else
2262 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2263 }
2264
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002265 ret = gpiod_request(desc, NULL);
2266 if (ret)
2267 return ERR_PTR(ret);
2268
Mika Westerberg40b73182014-10-21 13:33:59 +02002269 return desc;
2270}
2271EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2272
2273/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002274 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2275 * function
2276 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2277 * @con_id: function within the GPIO consumer
2278 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002279 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002280 *
2281 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2282 * specified index was assigned to the requested function it will return NULL.
2283 * This is convenient for drivers that need to handle optional GPIOs.
2284 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002285struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002286 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002287 unsigned int index,
2288 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002289{
2290 struct gpio_desc *desc;
2291
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002292 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002293 if (IS_ERR(desc)) {
2294 if (PTR_ERR(desc) == -ENOENT)
2295 return NULL;
2296 }
2297
2298 return desc;
2299}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002300EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002301
2302/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002303 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2304 * @desc: gpio whose value will be assigned
2305 * @name: gpio line name
2306 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2307 * of_get_gpio_hog()
2308 * @dflags: gpiod_flags - optional GPIO initialization flags
2309 */
2310int gpiod_hog(struct gpio_desc *desc, const char *name,
2311 unsigned long lflags, enum gpiod_flags dflags)
2312{
2313 struct gpio_chip *chip;
2314 struct gpio_desc *local_desc;
2315 int hwnum;
2316 int status;
2317
2318 chip = gpiod_to_chip(desc);
2319 hwnum = gpio_chip_hwgpio(desc);
2320
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002321 gpiod_parse_flags(desc, lflags);
2322
Benoit Parrotf625d462015-02-02 11:44:44 -06002323 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2324 if (IS_ERR(local_desc)) {
Linus Walleija7138902015-06-05 11:36:10 +02002325 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2326 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002327 return PTR_ERR(local_desc);
2328 }
2329
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002330 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002331 if (status < 0) {
Linus Walleija7138902015-06-05 11:36:10 +02002332 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2333 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002334 gpiochip_free_own_desc(desc);
2335 return status;
2336 }
2337
2338 /* Mark GPIO as hogged so it can be identified and removed later */
2339 set_bit(FLAG_IS_HOGGED, &desc->flags);
2340
2341 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2342 desc_to_gpio(desc), name,
2343 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2344 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2345 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2346
2347 return 0;
2348}
2349
2350/**
2351 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2352 * @chip: gpio chip to act on
2353 *
2354 * This is only used by of_gpiochip_remove to free hogged gpios
2355 */
2356static void gpiochip_free_hogs(struct gpio_chip *chip)
2357{
2358 int id;
2359
2360 for (id = 0; id < chip->ngpio; id++) {
2361 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2362 gpiochip_free_own_desc(&chip->desc[id]);
2363 }
2364}
2365
2366/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002367 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2368 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2369 * @con_id: function within the GPIO consumer
2370 * @flags: optional GPIO initialization flags
2371 *
2372 * This function acquires all the GPIOs defined under a given function.
2373 *
2374 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2375 * no GPIO has been assigned to the requested function, or another IS_ERR()
2376 * code if an error occurred while trying to acquire the GPIOs.
2377 */
2378struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2379 const char *con_id,
2380 enum gpiod_flags flags)
2381{
2382 struct gpio_desc *desc;
2383 struct gpio_descs *descs;
2384 int count;
2385
2386 count = gpiod_count(dev, con_id);
2387 if (count < 0)
2388 return ERR_PTR(count);
2389
2390 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2391 GFP_KERNEL);
2392 if (!descs)
2393 return ERR_PTR(-ENOMEM);
2394
2395 for (descs->ndescs = 0; descs->ndescs < count; ) {
2396 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2397 if (IS_ERR(desc)) {
2398 gpiod_put_array(descs);
2399 return ERR_CAST(desc);
2400 }
2401 descs->desc[descs->ndescs] = desc;
2402 descs->ndescs++;
2403 }
2404 return descs;
2405}
2406EXPORT_SYMBOL_GPL(gpiod_get_array);
2407
2408/**
2409 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2410 * function
2411 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2412 * @con_id: function within the GPIO consumer
2413 * @flags: optional GPIO initialization flags
2414 *
2415 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2416 * assigned to the requested function it will return NULL.
2417 */
2418struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2419 const char *con_id,
2420 enum gpiod_flags flags)
2421{
2422 struct gpio_descs *descs;
2423
2424 descs = gpiod_get_array(dev, con_id, flags);
2425 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2426 return NULL;
2427
2428 return descs;
2429}
2430EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2431
2432/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002433 * gpiod_put - dispose of a GPIO descriptor
2434 * @desc: GPIO descriptor to dispose of
2435 *
2436 * No descriptor can be used after gpiod_put() has been called on it.
2437 */
2438void gpiod_put(struct gpio_desc *desc)
2439{
2440 gpiod_free(desc);
2441}
2442EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002443
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002444/**
2445 * gpiod_put_array - dispose of multiple GPIO descriptors
2446 * @descs: struct gpio_descs containing an array of descriptors
2447 */
2448void gpiod_put_array(struct gpio_descs *descs)
2449{
2450 unsigned int i;
2451
2452 for (i = 0; i < descs->ndescs; i++)
2453 gpiod_put(descs->desc[i]);
2454
2455 kfree(descs);
2456}
2457EXPORT_SYMBOL_GPL(gpiod_put_array);
2458
David Brownelld2876d02008-02-04 22:28:20 -08002459#ifdef CONFIG_DEBUG_FS
2460
2461static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2462{
2463 unsigned i;
2464 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002465 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002466 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002467 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002468
2469 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02002470 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2471 if (gdesc->name) {
2472 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2473 gpio, gdesc->name);
2474 }
David Brownelld2876d02008-02-04 22:28:20 -08002475 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02002476 }
David Brownelld2876d02008-02-04 22:28:20 -08002477
Alexandre Courbot372e7222013-02-03 01:29:29 +09002478 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002479 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002480 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02002481 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2482 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08002483 is_out ? "out" : "in ",
2484 chip->get
2485 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002486 : "? ",
2487 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002488 seq_printf(s, "\n");
2489 }
2490}
2491
Thierry Redingf9c4a312012-04-12 13:26:01 +02002492static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002493{
Grant Likely362432a2013-02-09 09:41:49 +00002494 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002495 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002496 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002497
2498 s->private = "";
2499
Grant Likely362432a2013-02-09 09:41:49 +00002500 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002501 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002502 if (index-- == 0) {
2503 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002504 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002505 }
2506 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002507
2508 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002509}
2510
2511static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2512{
Grant Likely362432a2013-02-09 09:41:49 +00002513 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002514 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002515 void *ret = NULL;
2516
Grant Likely362432a2013-02-09 09:41:49 +00002517 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002518 if (list_is_last(&chip->list, &gpio_chips))
2519 ret = NULL;
2520 else
2521 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002522 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002523
2524 s->private = "\n";
2525 ++*pos;
2526
2527 return ret;
2528}
2529
2530static void gpiolib_seq_stop(struct seq_file *s, void *v)
2531{
2532}
2533
2534static int gpiolib_seq_show(struct seq_file *s, void *v)
2535{
2536 struct gpio_chip *chip = v;
2537 struct device *dev;
2538
2539 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2540 chip->base, chip->base + chip->ngpio - 1);
Linus Walleij58383c72015-11-04 09:56:26 +01002541 dev = chip->parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002542 if (dev)
2543 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2544 dev_name(dev));
2545 if (chip->label)
2546 seq_printf(s, ", %s", chip->label);
2547 if (chip->can_sleep)
2548 seq_printf(s, ", can sleep");
2549 seq_printf(s, ":\n");
2550
2551 if (chip->dbg_show)
2552 chip->dbg_show(s, chip);
2553 else
2554 gpiolib_dbg_show(s, chip);
2555
David Brownelld2876d02008-02-04 22:28:20 -08002556 return 0;
2557}
2558
Thierry Redingf9c4a312012-04-12 13:26:01 +02002559static const struct seq_operations gpiolib_seq_ops = {
2560 .start = gpiolib_seq_start,
2561 .next = gpiolib_seq_next,
2562 .stop = gpiolib_seq_stop,
2563 .show = gpiolib_seq_show,
2564};
2565
David Brownelld2876d02008-02-04 22:28:20 -08002566static int gpiolib_open(struct inode *inode, struct file *file)
2567{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002568 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002569}
2570
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002571static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002572 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002573 .open = gpiolib_open,
2574 .read = seq_read,
2575 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002576 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002577};
2578
2579static int __init gpiolib_debugfs_init(void)
2580{
2581 /* /sys/kernel/debug/gpio */
2582 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2583 NULL, NULL, &gpiolib_operations);
2584 return 0;
2585}
2586subsys_initcall(gpiolib_debugfs_init);
2587
2588#endif /* DEBUG_FS */