blob: 3db34e74bc340a86e7d17099e6c747bc38c0175e [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;
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800198 }
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 */
227
228 iterator = list_last_entry(&gpio_chips, struct gpio_chip, list);
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800229 if (iterator->base + iterator->ngpio <= chip->base)
230 goto found;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900231
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800232 dev_err(chip->parent,
233 "GPIO integer space overlap, cannot add chip\n");
234 return -EBUSY;
235
236found:
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530237 list_add_tail(&chip->list, &iterator->list);
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800238 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900239}
240
Linus Walleijf881bab2015-09-23 16:20:43 -0700241/**
242 * Convert a GPIO name to its descriptor
243 */
244static struct gpio_desc *gpio_name_to_desc(const char * const name)
245{
246 struct gpio_chip *chip;
247 unsigned long flags;
248
249 spin_lock_irqsave(&gpio_lock, flags);
250
251 list_for_each_entry(chip, &gpio_chips, list) {
252 int i;
253
254 for (i = 0; i != chip->ngpio; ++i) {
255 struct gpio_desc *gpio = &chip->desc[i];
256
Vladimir Zapolskiyd06165b2015-11-11 14:36:53 +0200257 if (!gpio->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700258 continue;
259
260 if (!strcmp(gpio->name, name)) {
261 spin_unlock_irqrestore(&gpio_lock, flags);
262 return gpio;
263 }
264 }
265 }
266
267 spin_unlock_irqrestore(&gpio_lock, flags);
268
269 return NULL;
270}
271
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200272/*
273 * Takes the names from gc->names and checks if they are all unique. If they
274 * are, they are assigned to their gpio descriptors.
275 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800276 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200277 */
278static int gpiochip_set_desc_names(struct gpio_chip *gc)
279{
280 int i;
281
282 if (!gc->names)
283 return 0;
284
285 /* First check all names if they are unique */
286 for (i = 0; i != gc->ngpio; ++i) {
287 struct gpio_desc *gpio;
288
289 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700290 if (gpio)
Linus Walleij58383c72015-11-04 09:56:26 +0100291 dev_warn(gc->parent, "Detected name collision for "
Linus Walleijf881bab2015-09-23 16:20:43 -0700292 "GPIO name '%s'\n",
293 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200294 }
295
296 /* Then add all names to the GPIO descriptors */
297 for (i = 0; i != gc->ngpio; ++i)
298 gc->desc[i].name = gc->names[i];
299
300 return 0;
301}
302
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700303/**
Linus Walleijb08ea352015-12-03 15:14:13 +0100304 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -0800305 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900306 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -0800307 *
308 * Returns a negative errno if the chip can't be registered, such as
309 * because the chip->base is invalid or already associated with a
310 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700311 *
Linus Walleijb08ea352015-12-03 15:14:13 +0100312 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800313 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d2008-07-25 01:46:07 -0700314 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
315 * for GPIOs will fail rudely.
316 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700317 * If chip->base is negative, this requests dynamic assignment of
318 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800319 */
Linus Walleijb08ea352015-12-03 15:14:13 +0100320int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -0800321{
322 unsigned long flags;
323 int status = 0;
324 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700325 int base = chip->base;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900326 struct gpio_desc *descs;
David Brownelld2876d02008-02-04 22:28:20 -0800327
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900328 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
329 if (!descs)
330 return -ENOMEM;
David Brownelld2876d02008-02-04 22:28:20 -0800331
Linus Walleijb08ea352015-12-03 15:14:13 +0100332 chip->data = data;
333
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800334 if (chip->ngpio == 0) {
335 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
336 return -EINVAL;
337 }
338
David Brownelld2876d02008-02-04 22:28:20 -0800339 spin_lock_irqsave(&gpio_lock, flags);
340
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700341 if (base < 0) {
342 base = gpiochip_find_base(chip->ngpio);
343 if (base < 0) {
344 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100345 spin_unlock_irqrestore(&gpio_lock, flags);
346 goto err_free_descs;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700347 }
348 chip->base = base;
349 }
350
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900351 status = gpiochip_add_to_list(chip);
Johan Hovold05aa5202015-01-12 17:12:26 +0100352 if (status) {
353 spin_unlock_irqrestore(&gpio_lock, flags);
354 goto err_free_descs;
355 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900356
Johan Hovold05aa5202015-01-12 17:12:26 +0100357 for (id = 0; id < chip->ngpio; id++) {
358 struct gpio_desc *desc = &descs[id];
David Brownelld8f388d2008-07-25 01:46:07 -0700359
Johan Hovold05aa5202015-01-12 17:12:26 +0100360 desc->chip = chip;
361
362 /* REVISIT: most hardware initializes GPIOs as inputs (often
363 * with pullups enabled) so power usage is minimized. Linux
364 * code should set the gpio direction first thing; but until
365 * it does, and in case chip->get_direction is not set, we may
366 * expose the wrong direction in sysfs.
367 */
368 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800369 }
370
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900371 chip->desc = descs;
372
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800373 spin_unlock_irqrestore(&gpio_lock, flags);
374
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530375#ifdef CONFIG_PINCTRL
376 INIT_LIST_HEAD(&chip->pin_ranges);
377#endif
378
Linus Walleij58383c72015-11-04 09:56:26 +0100379 if (!chip->owner && chip->parent && chip->parent->driver)
380 chip->owner = chip->parent->driver->owner;
Grygorii Strashko37269602015-06-25 20:30:51 +0300381
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200382 status = gpiochip_set_desc_names(chip);
383 if (status)
384 goto err_remove_from_list;
385
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200386 status = of_gpiochip_add(chip);
387 if (status)
388 goto err_remove_chip;
389
Mika Westerberg664e3e52014-01-08 12:40:54 +0200390 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600391
Johan Hovold426577b2015-05-04 17:10:32 +0200392 status = gpiochip_sysfs_register(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100393 if (status)
394 goto err_remove_chip;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600395
Andy Shevchenko7589e592013-12-05 11:26:23 +0200396 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700397 chip->base, chip->base + chip->ngpio - 1,
398 chip->label ? : "generic");
399
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600400 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800401
Johan Hovold225fce82015-01-12 17:12:25 +0100402err_remove_chip:
403 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200404 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100405 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200406err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100407 spin_lock_irqsave(&gpio_lock, flags);
408 list_del(&chip->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800409 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold05aa5202015-01-12 17:12:26 +0100410 chip->desc = NULL;
Johan Hovold225fce82015-01-12 17:12:25 +0100411err_free_descs:
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900412 kfree(descs);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900413
David Brownelld2876d02008-02-04 22:28:20 -0800414 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200415 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600416 chip->base, chip->base + chip->ngpio - 1,
417 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800418 return status;
419}
Linus Walleijb08ea352015-12-03 15:14:13 +0100420EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -0800421
422/**
423 * gpiochip_remove() - unregister a gpio_chip
424 * @chip: the chip to unregister
425 *
426 * A gpio_chip with any GPIOs still requested may not be removed.
427 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200428void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800429{
Johan Hovoldfab28b82015-05-04 17:10:27 +0200430 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800431 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800432 unsigned id;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200433 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800434
Johan Hovold426577b2015-05-04 17:10:32 +0200435 gpiochip_sysfs_unregister(chip);
Johan Hovold01cca932015-01-12 17:12:29 +0100436
Johan Hovold00acc3d2015-01-12 17:12:27 +0100437 gpiochip_irqchip_remove(chip);
438
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200439 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100440 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600441 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600442 of_gpiochip_remove(chip);
443
Johan Hovold6798aca2015-01-12 17:12:28 +0100444 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900445 for (id = 0; id < chip->ngpio; id++) {
Johan Hovoldfab28b82015-05-04 17:10:27 +0200446 desc = &chip->desc[id];
447 desc->chip = NULL;
448 if (test_bit(FLAG_REQUESTED, &desc->flags))
449 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800450 }
abdoulaye berthee1db1702014-07-05 18:28:50 +0200451 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800452 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900453
Johan Hovoldfab28b82015-05-04 17:10:27 +0200454 if (requested)
Linus Walleij58383c72015-11-04 09:56:26 +0100455 dev_crit(chip->parent,
456 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +0200457
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900458 kfree(chip->desc);
459 chip->desc = NULL;
David Brownelld2876d02008-02-04 22:28:20 -0800460}
461EXPORT_SYMBOL_GPL(gpiochip_remove);
462
Grant Likely594fa262010-06-08 07:48:16 -0600463/**
464 * gpiochip_find() - iterator for locating a specific gpio_chip
465 * @data: data to pass to match function
466 * @callback: Callback function to check gpio_chip
467 *
468 * Similar to bus_find_device. It returns a reference to a gpio_chip as
469 * determined by a user supplied @match callback. The callback should return
470 * 0 if the device doesn't match and non-zero if it does. If the callback is
471 * non-zero, this function will return to the caller and not iterate over any
472 * more gpio_chips.
473 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600474struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700475 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600476 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600477{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900478 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600479 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600480
481 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900482 list_for_each_entry(chip, &gpio_chips, list)
483 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600484 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900485
486 /* No match? */
487 if (&chip->list == &gpio_chips)
488 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600489 spin_unlock_irqrestore(&gpio_lock, flags);
490
491 return chip;
492}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600493EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800494
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900495static int gpiochip_match_name(struct gpio_chip *chip, void *data)
496{
497 const char *name = data;
498
499 return !strcmp(chip->label, name);
500}
501
502static struct gpio_chip *find_chip_by_name(const char *name)
503{
504 return gpiochip_find((void *)name, gpiochip_match_name);
505}
506
Linus Walleij14250522014-03-25 10:40:18 +0100507#ifdef CONFIG_GPIOLIB_IRQCHIP
508
509/*
510 * The following is irqchip helper code for gpiochips.
511 */
512
513/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200514 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
515 * @gpiochip: the gpiochip to set the irqchip chain to
516 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100517 * @parent_irq: the irq number corresponding to the parent IRQ for this
518 * chained irqchip
519 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200520 * coming out of the gpiochip. If the interrupt is nested rather than
521 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100522 */
523void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
524 struct irq_chip *irqchip,
525 int parent_irq,
526 irq_flow_handler_t parent_handler)
527{
Linus Walleij83141a72014-09-26 13:50:12 +0200528 unsigned int offset;
529
Linus Walleij83141a72014-09-26 13:50:12 +0200530 if (!gpiochip->irqdomain) {
531 chip_err(gpiochip, "called %s before setting up irqchip\n",
532 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200533 return;
534 }
535
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200536 if (parent_handler) {
537 if (gpiochip->can_sleep) {
538 chip_err(gpiochip,
539 "you cannot have chained interrupts on a "
540 "chip that may sleep\n");
541 return;
542 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200543 /*
544 * The parent irqchip is already using the chip_data for this
545 * irqchip, so our callbacks simply use the handler_data.
546 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +0200547 irq_set_chained_handler_and_data(parent_irq, parent_handler,
548 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300549
550 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200551 }
Linus Walleij83141a72014-09-26 13:50:12 +0200552
553 /* Set the parent IRQ for all affected IRQs */
554 for (offset = 0; offset < gpiochip->ngpio; offset++)
555 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
556 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100557}
558EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
559
560/**
561 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
562 * @d: the irqdomain used by this irqchip
563 * @irq: the global irq number used by this GPIO irqchip irq
564 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
565 *
566 * This function will set up the mapping for a certain IRQ line on a
567 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
568 * stored inside the gpiochip.
569 */
570static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
571 irq_hw_number_t hwirq)
572{
573 struct gpio_chip *chip = d->host_data;
574
Linus Walleij14250522014-03-25 10:40:18 +0100575 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300576 /*
577 * This lock class tells lockdep that GPIO irqs are in a different
578 * category than their parents, so it won't report false recursion.
579 */
580 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +0200581 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200582 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300583 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200584 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100585 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -0500586
Linus Walleij1333b902014-04-23 16:45:12 +0200587 /*
588 * No set-up of the hardware will happen if IRQ_TYPE_NONE
589 * is passed as default type.
590 */
591 if (chip->irq_default_type != IRQ_TYPE_NONE)
592 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100593
594 return 0;
595}
596
Linus Walleijc3626fd2014-03-28 20:42:01 +0100597static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
598{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200599 struct gpio_chip *chip = d->host_data;
600
Linus Walleij1c8732b2014-04-09 13:34:39 +0200601 if (chip->can_sleep)
602 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100603 irq_set_chip_and_handler(irq, NULL, NULL);
604 irq_set_chip_data(irq, NULL);
605}
606
Linus Walleij14250522014-03-25 10:40:18 +0100607static const struct irq_domain_ops gpiochip_domain_ops = {
608 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100609 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100610 /* Virtually all GPIO irqchips are twocell:ed */
611 .xlate = irq_domain_xlate_twocell,
612};
613
614static int gpiochip_irq_reqres(struct irq_data *d)
615{
616 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
617
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300618 if (!try_module_get(chip->owner))
619 return -ENODEV;
620
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900621 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100622 chip_err(chip,
623 "unable to lock HW IRQ %lu for IRQ\n",
624 d->hwirq);
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300625 module_put(chip->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100626 return -EINVAL;
627 }
628 return 0;
629}
630
631static void gpiochip_irq_relres(struct irq_data *d)
632{
633 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
634
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900635 gpiochip_unlock_as_irq(chip, d->hwirq);
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300636 module_put(chip->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100637}
638
639static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
640{
641 return irq_find_mapping(chip->irqdomain, offset);
642}
643
644/**
645 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
646 * @gpiochip: the gpiochip to remove the irqchip from
647 *
648 * This is called only from gpiochip_remove()
649 */
650static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
651{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100652 unsigned int offset;
653
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300654 acpi_gpiochip_free_interrupts(gpiochip);
655
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300656 if (gpiochip->irq_parent) {
657 irq_set_chained_handler(gpiochip->irq_parent, NULL);
658 irq_set_handler_data(gpiochip->irq_parent, NULL);
659 }
660
Linus Walleijc3626fd2014-03-28 20:42:01 +0100661 /* Remove all IRQ mappings and delete the domain */
662 if (gpiochip->irqdomain) {
663 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300664 irq_dispose_mapping(
665 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100666 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100667 }
Linus Walleij14250522014-03-25 10:40:18 +0100668
669 if (gpiochip->irqchip) {
670 gpiochip->irqchip->irq_request_resources = NULL;
671 gpiochip->irqchip->irq_release_resources = NULL;
672 gpiochip->irqchip = NULL;
673 }
674}
675
676/**
677 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
678 * @gpiochip: the gpiochip to add the irqchip to
679 * @irqchip: the irqchip to add to the gpiochip
680 * @first_irq: if not dynamically assigned, the base (first) IRQ to
681 * allocate gpiochip irqs from
682 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200683 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
684 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300685 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +0100686 *
687 * This function closely associates a certain irqchip with a certain
688 * gpiochip, providing an irq domain to translate the local IRQs to
689 * global irqs in the gpiolib core, and making sure that the gpiochip
690 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +0100691 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +0100692 * from the gpiochip passed as chip data. An irqdomain will be stored
693 * in the gpiochip that shall be used by the driver to handle IRQ number
694 * translation. The gpiochip will need to be initialized and registered
695 * before calling this function.
696 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100697 * This function will handle two cell:ed simple IRQs and assumes all
698 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100699 * need to be open coded.
700 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300701int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
702 struct irq_chip *irqchip,
703 unsigned int first_irq,
704 irq_flow_handler_t handler,
705 unsigned int type,
706 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +0100707{
708 struct device_node *of_node;
709 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100710 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100711
712 if (!gpiochip || !irqchip)
713 return -EINVAL;
714
Linus Walleij58383c72015-11-04 09:56:26 +0100715 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +0100716 pr_err("missing gpiochip .dev parent pointer\n");
717 return -EINVAL;
718 }
Linus Walleij58383c72015-11-04 09:56:26 +0100719 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +0100720#ifdef CONFIG_OF_GPIO
721 /*
Colin Cronin20a8a962015-05-18 11:41:43 -0700722 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800723 * FIXME: get rid of this and use gpiochip->parent->of_node
724 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +0100725 */
726 if (gpiochip->of_node)
727 of_node = gpiochip->of_node;
728#endif
729 gpiochip->irqchip = irqchip;
730 gpiochip->irq_handler = handler;
731 gpiochip->irq_default_type = type;
732 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300733 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +0100734 gpiochip->irqdomain = irq_domain_add_simple(of_node,
735 gpiochip->ngpio, first_irq,
736 &gpiochip_domain_ops, gpiochip);
737 if (!gpiochip->irqdomain) {
738 gpiochip->irqchip = NULL;
739 return -EINVAL;
740 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +0200741
742 /*
743 * It is possible for a driver to override this, but only if the
744 * alternative functions are both implemented.
745 */
746 if (!irqchip->irq_request_resources &&
747 !irqchip->irq_release_resources) {
748 irqchip->irq_request_resources = gpiochip_irq_reqres;
749 irqchip->irq_release_resources = gpiochip_irq_relres;
750 }
Linus Walleij14250522014-03-25 10:40:18 +0100751
752 /*
753 * Prepare the mapping since the irqchip shall be orthogonal to
754 * any gpiochip calls. If the first_irq was zero, this is
755 * necessary to allocate descriptors for all IRQs.
756 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100757 for (offset = 0; offset < gpiochip->ngpio; offset++) {
758 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
759 if (offset == 0)
760 /*
761 * Store the base into the gpiochip to be used when
762 * unmapping the irqs.
763 */
764 gpiochip->irq_base = irq_base;
765 }
Linus Walleij14250522014-03-25 10:40:18 +0100766
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300767 acpi_gpiochip_request_interrupts(gpiochip);
768
Linus Walleij14250522014-03-25 10:40:18 +0100769 return 0;
770}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300771EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +0100772
773#else /* CONFIG_GPIOLIB_IRQCHIP */
774
775static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
776
777#endif /* CONFIG_GPIOLIB_IRQCHIP */
778
Jonas Gorskic771c2f2015-10-11 17:34:15 +0200779/**
780 * gpiochip_generic_request() - request the gpio function for a pin
781 * @chip: the gpiochip owning the GPIO
782 * @offset: the offset of the GPIO to request for GPIO function
783 */
784int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
785{
786 return pinctrl_request_gpio(chip->base + offset);
787}
788EXPORT_SYMBOL_GPL(gpiochip_generic_request);
789
790/**
791 * gpiochip_generic_free() - free the gpio function from a pin
792 * @chip: the gpiochip to request the gpio function for
793 * @offset: the offset of the GPIO to free from GPIO function
794 */
795void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
796{
797 pinctrl_free_gpio(chip->base + offset);
798}
799EXPORT_SYMBOL_GPL(gpiochip_generic_free);
800
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530801#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100802
Linus Walleij3f0f8672012-11-20 12:40:15 +0100803/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200804 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
805 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +0200806 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +0200807 * @gpio_offset: the start offset in the current gpio_chip number space
808 * @pin_group: name of the pin group inside the pin controller
809 */
810int gpiochip_add_pingroup_range(struct gpio_chip *chip,
811 struct pinctrl_dev *pctldev,
812 unsigned int gpio_offset, const char *pin_group)
813{
814 struct gpio_pin_range *pin_range;
815 int ret;
816
817 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
818 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200819 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200820 return -ENOMEM;
821 }
822
823 /* Use local offset as range ID */
824 pin_range->range.id = gpio_offset;
825 pin_range->range.gc = chip;
826 pin_range->range.name = chip->label;
827 pin_range->range.base = chip->base + gpio_offset;
828 pin_range->pctldev = pctldev;
829
830 ret = pinctrl_get_group_pins(pctldev, pin_group,
831 &pin_range->range.pins,
832 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100833 if (ret < 0) {
834 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200835 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100836 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200837
838 pinctrl_add_gpio_range(pctldev, &pin_range->range);
839
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200840 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
841 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200842 pinctrl_dev_get_devname(pctldev), pin_group);
843
844 list_add_tail(&pin_range->node, &chip->pin_ranges);
845
846 return 0;
847}
848EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
849
850/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100851 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
852 * @chip: the gpiochip to add the range for
853 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100854 * @gpio_offset: the start offset in the current gpio_chip number space
855 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100856 * @npins: the number of pins from the offset of each pin space (GPIO and
857 * pin controller) to accumulate in this range
858 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100859int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100860 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100861 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530862{
863 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800864 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530865
Linus Walleij3f0f8672012-11-20 12:40:15 +0100866 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530867 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200868 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100869 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530870 }
871
Linus Walleij3f0f8672012-11-20 12:40:15 +0100872 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100873 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100874 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530875 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100876 pin_range->range.base = chip->base + gpio_offset;
877 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530878 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100879 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530880 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100881 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800882 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200883 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100884 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800885 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100886 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200887 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
888 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100889 pinctl_name,
890 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530891
892 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100893
894 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530895}
Linus Walleij165adc92012-11-06 14:49:39 +0100896EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530897
Linus Walleij3f0f8672012-11-20 12:40:15 +0100898/**
899 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
900 * @chip: the chip to remove all the mappings for
901 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530902void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
903{
904 struct gpio_pin_range *pin_range, *tmp;
905
906 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
907 list_del(&pin_range->node);
908 pinctrl_remove_gpio_range(pin_range->pctldev,
909 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100910 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530911 }
912}
Linus Walleij165adc92012-11-06 14:49:39 +0100913EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
914
915#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530916
David Brownelld2876d02008-02-04 22:28:20 -0800917/* These "optional" allocation calls help prevent drivers from stomping
918 * on each other, and help provide better diagnostics in debugfs.
919 * They're called even less than the "set direction" calls.
920 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200921static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800922{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200923 struct gpio_chip *chip = desc->chip;
924 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800925 unsigned long flags;
926
927 spin_lock_irqsave(&gpio_lock, flags);
928
David Brownelld2876d02008-02-04 22:28:20 -0800929 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700930 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800931 */
932
933 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
934 desc_set_label(desc, label ? : "?");
935 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700936 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800937 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800938 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700939 }
940
941 if (chip->request) {
942 /* chip->request may sleep */
943 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900944 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700945 spin_lock_irqsave(&gpio_lock, flags);
946
947 if (status < 0) {
948 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700949 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300950 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700951 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700952 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300953 if (chip->get_direction) {
954 /* chip->get_direction may sleep */
955 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900956 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300957 spin_lock_irqsave(&gpio_lock, flags);
958 }
David Brownelld2876d02008-02-04 22:28:20 -0800959done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +0300960 if (status < 0) {
961 /* Clear flags that might have been set by the caller before
962 * requesting the GPIO.
963 */
964 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
965 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
966 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
967 }
Mika Westerberg77c2d792014-03-10 14:54:50 +0200968 spin_unlock_irqrestore(&gpio_lock, flags);
969 return status;
970}
971
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900972int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200973{
974 int status = -EPROBE_DEFER;
975 struct gpio_chip *chip;
976
977 if (!desc) {
978 pr_warn("%s: invalid GPIO\n", __func__);
979 return -EINVAL;
980 }
981
982 chip = desc->chip;
983 if (!chip)
984 goto done;
985
986 if (try_module_get(chip->owner)) {
987 status = __gpiod_request(desc, label);
988 if (status < 0)
989 module_put(chip->owner);
990 }
991
992done:
David Brownelld2876d02008-02-04 22:28:20 -0800993 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200994 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200995
David Brownelld2876d02008-02-04 22:28:20 -0800996 return status;
997}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900998
Mika Westerberg77c2d792014-03-10 14:54:50 +0200999static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001000{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001001 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001002 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001003 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001004
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001005 might_sleep();
1006
Alexandre Courbot372e7222013-02-03 01:29:29 +09001007 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001008
David Brownelld2876d02008-02-04 22:28:20 -08001009 spin_lock_irqsave(&gpio_lock, flags);
1010
David Brownell35e8bb52008-10-15 22:03:16 -07001011 chip = desc->chip;
1012 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1013 if (chip->free) {
1014 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001015 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001016 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001017 spin_lock_irqsave(&gpio_lock, flags);
1018 }
David Brownelld2876d02008-02-04 22:28:20 -08001019 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001020 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001021 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301022 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301023 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001024 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001025 ret = true;
1026 }
David Brownelld2876d02008-02-04 22:28:20 -08001027
1028 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001029 return ret;
1030}
1031
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001032void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001033{
1034 if (desc && __gpiod_free(desc))
1035 module_put(desc->chip->owner);
1036 else
1037 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -08001038}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001039
David Brownelld2876d02008-02-04 22:28:20 -08001040/**
1041 * gpiochip_is_requested - return string iff signal was requested
1042 * @chip: controller managing the signal
1043 * @offset: of signal within controller's 0..(ngpio - 1) range
1044 *
1045 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001046 * The string returned is the label passed to gpio_request(); if none has been
1047 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001048 *
1049 * This function is for use by GPIO controller drivers. The label can
1050 * help with diagnostics, and knowing that the signal is used as a GPIO
1051 * can help avoid accidentally multiplexing it to another controller.
1052 */
1053const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1054{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001055 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001056
Dirk Behme48b59532015-08-18 18:02:32 +02001057 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001058 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001059
1060 desc = &chip->desc[offset];
1061
Alexandre Courbot372e7222013-02-03 01:29:29 +09001062 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001063 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001064 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001065}
1066EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1067
Mika Westerberg77c2d792014-03-10 14:54:50 +02001068/**
1069 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1070 * @desc: GPIO descriptor to request
1071 * @label: label for the GPIO
1072 *
1073 * Function allows GPIO chip drivers to request and use their own GPIO
1074 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1075 * function will not increase reference count of the GPIO chip module. This
1076 * allows the GPIO chip module to be unloaded as needed (we assume that the
1077 * GPIO chip driver handles freeing the GPIOs it has requested).
1078 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001079struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1080 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001081{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001082 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1083 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001084
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001085 if (IS_ERR(desc)) {
1086 chip_err(chip, "failed to get GPIO descriptor\n");
1087 return desc;
1088 }
1089
1090 err = __gpiod_request(desc, label);
1091 if (err < 0)
1092 return ERR_PTR(err);
1093
1094 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001095}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001096EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001097
1098/**
1099 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1100 * @desc: GPIO descriptor to free
1101 *
1102 * Function frees the given GPIO requested previously with
1103 * gpiochip_request_own_desc().
1104 */
1105void gpiochip_free_own_desc(struct gpio_desc *desc)
1106{
1107 if (desc)
1108 __gpiod_free(desc);
1109}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001110EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08001111
1112/* Drivers MUST set GPIO direction before making get/set calls. In
1113 * some cases this is done in early boot, before IRQs are enabled.
1114 *
1115 * As a rule these aren't called more than once (except for drivers
1116 * using the open-drain emulation idiom) so these are natural places
1117 * to accumulate extra debugging checks. Note that we can't (yet)
1118 * rely on gpio_request() having been called beforehand.
1119 */
1120
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001121/**
1122 * gpiod_direction_input - set the GPIO direction to input
1123 * @desc: GPIO to set to input
1124 *
1125 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1126 * be called safely on it.
1127 *
1128 * Return 0 in case of success, else an error code.
1129 */
1130int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001131{
David Brownelld2876d02008-02-04 22:28:20 -08001132 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001133 int status = -EINVAL;
1134
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001135 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001136 pr_warn("%s: invalid GPIO\n", __func__);
1137 return -EINVAL;
1138 }
1139
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001140 chip = desc->chip;
1141 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001142 gpiod_warn(desc,
1143 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001144 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001145 return -EIO;
1146 }
1147
Alexandre Courbotd82da792014-07-22 16:17:43 +09001148 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001149 if (status == 0)
1150 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001151
Alexandre Courbot372e7222013-02-03 01:29:29 +09001152 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001153
David Brownelld2876d02008-02-04 22:28:20 -08001154 return status;
1155}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001156EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001157
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001158static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001159{
David Brownelld2876d02008-02-04 22:28:20 -08001160 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001161 int status = -EINVAL;
1162
Linus Walleijd468bf92013-09-24 11:54:38 +02001163 /* GPIOs used for IRQs shall not be set as output */
1164 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1165 gpiod_err(desc,
1166 "%s: tried to set a GPIO tied to an IRQ as output\n",
1167 __func__);
1168 return -EIO;
1169 }
1170
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301171 /* Open drain pin should not be driven to 1 */
1172 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001173 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301174
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301175 /* Open source pin should not be driven to 0 */
1176 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001177 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301178
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001179 chip = desc->chip;
1180 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001181 gpiod_warn(desc,
1182 "%s: missing set() or direction_output() operations\n",
1183 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001184 return -EIO;
1185 }
1186
Alexandre Courbotd82da792014-07-22 16:17:43 +09001187 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001188 if (status == 0)
1189 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001190 trace_gpio_value(desc_to_gpio(desc), 0, value);
1191 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001192 return status;
1193}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001194
1195/**
1196 * gpiod_direction_output_raw - set the GPIO direction to output
1197 * @desc: GPIO to set to output
1198 * @value: initial output value of the GPIO
1199 *
1200 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1201 * be called safely on it. The initial value of the output must be specified
1202 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1203 *
1204 * Return 0 in case of success, else an error code.
1205 */
1206int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1207{
1208 if (!desc || !desc->chip) {
1209 pr_warn("%s: invalid GPIO\n", __func__);
1210 return -EINVAL;
1211 }
1212 return _gpiod_direction_output_raw(desc, value);
1213}
1214EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1215
1216/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301217 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001218 * @desc: GPIO to set to output
1219 * @value: initial output value of the GPIO
1220 *
1221 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1222 * be called safely on it. The initial value of the output must be specified
1223 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1224 * account.
1225 *
1226 * Return 0 in case of success, else an error code.
1227 */
1228int gpiod_direction_output(struct gpio_desc *desc, int value)
1229{
1230 if (!desc || !desc->chip) {
1231 pr_warn("%s: invalid GPIO\n", __func__);
1232 return -EINVAL;
1233 }
1234 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1235 value = !value;
1236 return _gpiod_direction_output_raw(desc, value);
1237}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001238EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001239
Felipe Balbic4b5be92010-05-26 14:42:23 -07001240/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001241 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001242 * @gpio: the gpio to set debounce time
1243 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001244 *
1245 * returns -ENOTSUPP if the controller does not support setting
1246 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001247 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001248int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001249{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001250 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001251
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001252 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001253 pr_warn("%s: invalid GPIO\n", __func__);
1254 return -EINVAL;
1255 }
1256
Felipe Balbic4b5be92010-05-26 14:42:23 -07001257 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001258 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001259 gpiod_dbg(desc,
1260 "%s: missing set() or set_debounce() operations\n",
1261 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001262 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001263 }
1264
Alexandre Courbotd82da792014-07-22 16:17:43 +09001265 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001266}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001267EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001268
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001269/**
1270 * gpiod_is_active_low - test whether a GPIO is active-low or not
1271 * @desc: the gpio descriptor to test
1272 *
1273 * Returns 1 if the GPIO is active-low, 0 otherwise.
1274 */
1275int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001276{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001277 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001278}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001279EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001280
1281/* I/O calls are only valid after configuration completed; the relevant
1282 * "is this a valid GPIO" error checks should already have been done.
1283 *
1284 * "Get" operations are often inlinable as reading a pin value register,
1285 * and masking the relevant bit in that register.
1286 *
1287 * When "set" operations are inlinable, they involve writing that mask to
1288 * one register to set a low value, or a different register to set it high.
1289 * Otherwise locking is needed, so there may be little value to inlining.
1290 *
1291 *------------------------------------------------------------------------
1292 *
1293 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1294 * have requested the GPIO. That can include implicit requesting by
1295 * a direction setting call. Marking a gpio as requested locks its chip
1296 * in memory, guaranteeing that these table lookups need no more locking
1297 * and that gpiochip_remove() will fail.
1298 *
1299 * REVISIT when debugging, consider adding some instrumentation to ensure
1300 * that the GPIO was actually requested.
1301 */
1302
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001303static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001304{
1305 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001306 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001307 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001308
Alexandre Courbot372e7222013-02-03 01:29:29 +09001309 chip = desc->chip;
1310 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001311 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01001312 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001313 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001314 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001315}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001316
David Brownelld2876d02008-02-04 22:28:20 -08001317/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001318 * gpiod_get_raw_value() - return a gpio's raw value
1319 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001320 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001321 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001322 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001323 *
1324 * This function should be called from contexts where we cannot sleep, and will
1325 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001326 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001327int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001328{
David Brownelld2876d02008-02-04 22:28:20 -08001329 if (!desc)
1330 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001331 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001332 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001333 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001334}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001335EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001336
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001337/**
1338 * gpiod_get_value() - return a gpio's value
1339 * @desc: gpio whose value will be returned
1340 *
1341 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001342 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001343 *
1344 * This function should be called from contexts where we cannot sleep, and will
1345 * complain if the GPIO chip functions potentially sleep.
1346 */
1347int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001348{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001349 int value;
1350 if (!desc)
1351 return 0;
1352 /* Should be using gpio_get_value_cansleep() */
1353 WARN_ON(desc->chip->can_sleep);
1354
1355 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001356 if (value < 0)
1357 return value;
1358
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001359 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1360 value = !value;
1361
1362 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001363}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001364EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001365
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301366/*
1367 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001368 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001369 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301370 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001371static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301372{
1373 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001374 struct gpio_chip *chip = desc->chip;
1375 int offset = gpio_chip_hwgpio(desc);
1376
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301377 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001378 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301379 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001380 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301381 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001382 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301383 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001384 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301385 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001386 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301387 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001388 gpiod_err(desc,
1389 "%s: Error in set_value for open drain err %d\n",
1390 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301391}
1392
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301393/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001394 * _gpio_set_open_source_value() - Set the open source gpio's value.
1395 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001396 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301397 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001398static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301399{
1400 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001401 struct gpio_chip *chip = desc->chip;
1402 int offset = gpio_chip_hwgpio(desc);
1403
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301404 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001405 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301406 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001407 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301408 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001409 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301410 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001411 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301412 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001413 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301414 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001415 gpiod_err(desc,
1416 "%s: Error in set_value for open source err %d\n",
1417 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301418}
1419
Alexandre Courbot23600962014-03-11 15:52:09 +09001420static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001421{
1422 struct gpio_chip *chip;
1423
Alexandre Courbot372e7222013-02-03 01:29:29 +09001424 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001425 trace_gpio_value(desc_to_gpio(desc), 0, value);
1426 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1427 _gpio_set_open_drain_value(desc, value);
1428 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1429 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301430 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001431 chip->set(chip, gpio_chip_hwgpio(desc), value);
1432}
1433
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001434/*
1435 * set multiple outputs on the same chip;
1436 * use the chip's set_multiple function if available;
1437 * otherwise set the outputs sequentially;
1438 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1439 * defines which outputs are to be changed
1440 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1441 * defines the values the outputs specified by mask are to be set to
1442 */
1443static void gpio_chip_set_multiple(struct gpio_chip *chip,
1444 unsigned long *mask, unsigned long *bits)
1445{
1446 if (chip->set_multiple) {
1447 chip->set_multiple(chip, mask, bits);
1448 } else {
1449 int i;
1450 for (i = 0; i < chip->ngpio; i++) {
1451 if (mask[BIT_WORD(i)] == 0) {
1452 /* no more set bits in this mask word;
1453 * skip ahead to the next word */
1454 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1455 continue;
1456 }
1457 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001458 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001459 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001460 }
1461 }
1462}
1463
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001464static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1465 unsigned int array_size,
1466 struct gpio_desc **desc_array,
1467 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001468{
1469 int i = 0;
1470
1471 while (i < array_size) {
1472 struct gpio_chip *chip = desc_array[i]->chip;
1473 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1474 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1475 int count = 0;
1476
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001477 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001478 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001479
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001480 memset(mask, 0, sizeof(mask));
1481 do {
1482 struct gpio_desc *desc = desc_array[i];
1483 int hwgpio = gpio_chip_hwgpio(desc);
1484 int value = value_array[i];
1485
1486 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1487 value = !value;
1488 trace_gpio_value(desc_to_gpio(desc), 0, value);
1489 /*
1490 * collect all normal outputs belonging to the same chip
1491 * open drain and open source outputs are set individually
1492 */
1493 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001494 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001495 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1496 _gpio_set_open_source_value(desc, value);
1497 } else {
1498 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001499 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001500 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001501 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001502 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001503 count++;
1504 }
1505 i++;
1506 } while ((i < array_size) && (desc_array[i]->chip == chip));
1507 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001508 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001509 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001510 }
1511}
1512
David Brownelld2876d02008-02-04 22:28:20 -08001513/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001514 * gpiod_set_raw_value() - assign a gpio's raw value
1515 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001516 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001517 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001518 * Set the raw value of the GPIO, i.e. the value of its physical line without
1519 * regard for its ACTIVE_LOW status.
1520 *
1521 * This function should be called from contexts where we cannot sleep, and will
1522 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001523 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001524void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001525{
David Brownelld2876d02008-02-04 22:28:20 -08001526 if (!desc)
1527 return;
David Brownelld2876d02008-02-04 22:28:20 -08001528 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001529 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001530 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001531}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001532EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001533
1534/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001535 * gpiod_set_value() - assign a gpio's value
1536 * @desc: gpio whose value will be assigned
1537 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001538 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001539 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1540 * account
1541 *
1542 * This function should be called from contexts where we cannot sleep, and will
1543 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001544 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001545void gpiod_set_value(struct gpio_desc *desc, int value)
1546{
1547 if (!desc)
1548 return;
1549 /* Should be using gpio_set_value_cansleep() */
1550 WARN_ON(desc->chip->can_sleep);
1551 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1552 value = !value;
1553 _gpiod_set_raw_value(desc, value);
1554}
1555EXPORT_SYMBOL_GPL(gpiod_set_value);
1556
1557/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001558 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001559 * @array_size: number of elements in the descriptor / value arrays
1560 * @desc_array: array of GPIO descriptors whose values will be assigned
1561 * @value_array: array of values to assign
1562 *
1563 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1564 * without regard for their ACTIVE_LOW status.
1565 *
1566 * This function should be called from contexts where we cannot sleep, and will
1567 * complain if the GPIO chip functions potentially sleep.
1568 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001569void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001570 struct gpio_desc **desc_array, int *value_array)
1571{
1572 if (!desc_array)
1573 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001574 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1575 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001576}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001577EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001578
1579/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001580 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001581 * @array_size: number of elements in the descriptor / value arrays
1582 * @desc_array: array of GPIO descriptors whose values will be assigned
1583 * @value_array: array of values to assign
1584 *
1585 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1586 * into account.
1587 *
1588 * This function should be called from contexts where we cannot sleep, and will
1589 * complain if the GPIO chip functions potentially sleep.
1590 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001591void gpiod_set_array_value(unsigned int array_size,
1592 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001593{
1594 if (!desc_array)
1595 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001596 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1597 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001598}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001599EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001600
1601/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001602 * gpiod_cansleep() - report whether gpio value access may sleep
1603 * @desc: gpio to check
1604 *
1605 */
1606int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001607{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001608 if (!desc)
1609 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001610 return desc->chip->can_sleep;
1611}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001612EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001613
David Brownell0f6d5042008-10-15 22:03:14 -07001614/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001615 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1616 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001617 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001618 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1619 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001620 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001621int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001622{
1623 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001624 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001625
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001626 if (!desc)
1627 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001628 chip = desc->chip;
1629 offset = gpio_chip_hwgpio(desc);
1630 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1631}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001632EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001633
Linus Walleijd468bf92013-09-24 11:54:38 +02001634/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001635 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001636 * @chip: the chip the GPIO to lock belongs to
1637 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001638 *
1639 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001640 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001641 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001642int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001643{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001644 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001645 return -EINVAL;
1646
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001647 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1648 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001649 "%s: tried to flag a GPIO set as output for IRQ\n",
1650 __func__);
1651 return -EIO;
1652 }
1653
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001654 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001655 return 0;
1656}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001657EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001658
Linus Walleijd468bf92013-09-24 11:54:38 +02001659/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001660 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001661 * @chip: the chip the GPIO to lock belongs to
1662 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001663 *
1664 * This is used directly by GPIO drivers that want to indicate
1665 * that a certain GPIO is no longer used exclusively for IRQ.
1666 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001667void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001668{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001669 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001670 return;
1671
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001672 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001673}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001674EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001675
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001676/**
1677 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1678 * @desc: gpio whose value will be returned
1679 *
1680 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001681 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001682 *
1683 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001684 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001685int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001686{
David Brownelld2876d02008-02-04 22:28:20 -08001687 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001688 if (!desc)
1689 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001690 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001691}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001692EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001693
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001694/**
1695 * gpiod_get_value_cansleep() - return a gpio's value
1696 * @desc: gpio whose value will be returned
1697 *
1698 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001699 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001700 *
1701 * This function is to be called from contexts that can sleep.
1702 */
1703int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001704{
David Brownelld2876d02008-02-04 22:28:20 -08001705 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001706
1707 might_sleep_if(extra_checks);
1708 if (!desc)
1709 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001710
1711 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001712 if (value < 0)
1713 return value;
1714
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001715 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1716 value = !value;
1717
David Brownelld2876d02008-02-04 22:28:20 -08001718 return value;
1719}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001720EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001721
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001722/**
1723 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1724 * @desc: gpio whose value will be assigned
1725 * @value: value to assign
1726 *
1727 * Set the raw value of the GPIO, i.e. the value of its physical line without
1728 * regard for its ACTIVE_LOW status.
1729 *
1730 * This function is to be called from contexts that can sleep.
1731 */
1732void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001733{
David Brownelld2876d02008-02-04 22:28:20 -08001734 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001735 if (!desc)
1736 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001737 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001738}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001739EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001740
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001741/**
1742 * gpiod_set_value_cansleep() - assign a gpio's value
1743 * @desc: gpio whose value will be assigned
1744 * @value: value to assign
1745 *
1746 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1747 * account
1748 *
1749 * This function is to be called from contexts that can sleep.
1750 */
1751void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001752{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001753 might_sleep_if(extra_checks);
1754 if (!desc)
1755 return;
1756
1757 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1758 value = !value;
1759 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001760}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001761EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001762
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001763/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001764 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001765 * @array_size: number of elements in the descriptor / value arrays
1766 * @desc_array: array of GPIO descriptors whose values will be assigned
1767 * @value_array: array of values to assign
1768 *
1769 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1770 * without regard for their ACTIVE_LOW status.
1771 *
1772 * This function is to be called from contexts that can sleep.
1773 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001774void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1775 struct gpio_desc **desc_array,
1776 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001777{
1778 might_sleep_if(extra_checks);
1779 if (!desc_array)
1780 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001781 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1782 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001783}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001784EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001785
1786/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001787 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001788 * @array_size: number of elements in the descriptor / value arrays
1789 * @desc_array: array of GPIO descriptors whose values will be assigned
1790 * @value_array: array of values to assign
1791 *
1792 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1793 * into account.
1794 *
1795 * This function is to be called from contexts that can sleep.
1796 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001797void gpiod_set_array_value_cansleep(unsigned int array_size,
1798 struct gpio_desc **desc_array,
1799 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001800{
1801 might_sleep_if(extra_checks);
1802 if (!desc_array)
1803 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001804 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1805 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001806}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001807EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001808
1809/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001810 * gpiod_add_lookup_table() - register GPIO device consumers
1811 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001812 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001813void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001814{
1815 mutex_lock(&gpio_lookup_lock);
1816
Alexandre Courbotad824782013-12-03 12:20:11 +09001817 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001818
1819 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001820}
1821
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05301822/**
1823 * gpiod_remove_lookup_table() - unregister GPIO device consumers
1824 * @table: table of consumers to unregister
1825 */
1826void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
1827{
1828 mutex_lock(&gpio_lookup_lock);
1829
1830 list_del(&table->list);
1831
1832 mutex_unlock(&gpio_lookup_lock);
1833}
1834
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001835static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001836 unsigned int idx,
1837 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001838{
1839 char prop_name[32]; /* 32 is max size of property name */
1840 enum of_gpio_flags of_flags;
1841 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001842 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001843
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001844 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02001845 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01001846 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001847 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02001848 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01001849 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001850 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001851
Thierry Redingdd34c372014-04-23 17:28:09 +02001852 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1853 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001854 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001855 break;
1856 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001857
1858 if (IS_ERR(desc))
1859 return desc;
1860
1861 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001862 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001863
Laurent Pinchart90b665f2015-10-13 00:20:21 +03001864 if (of_flags & OF_GPIO_SINGLE_ENDED) {
1865 if (of_flags & OF_GPIO_ACTIVE_LOW)
1866 *flags |= GPIO_OPEN_DRAIN;
1867 else
1868 *flags |= GPIO_OPEN_SOURCE;
1869 }
1870
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001871 return desc;
1872}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001873
Mika Westerberg81f59e92013-10-10 11:01:09 +03001874static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001875 unsigned int idx,
1876 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001877{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001878 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03001879 struct acpi_gpio_info info;
1880 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001881 char propname[32];
1882 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03001883
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001884 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001885 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001886 if (con_id && strcmp(con_id, "gpios")) {
1887 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001888 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001889 } else {
1890 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001891 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001892 }
Mika Westerberge01f4402013-10-10 11:01:10 +03001893
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001894 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1895 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1896 break;
1897 }
1898
1899 /* Then from plain _CRS GPIOs */
1900 if (IS_ERR(desc)) {
Dmitry Torokhov9c3c9bc2015-11-11 11:45:30 -08001901 if (!acpi_can_fallback_to_crs(adev, con_id))
1902 return ERR_PTR(-ENOENT);
1903
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001904 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1905 if (IS_ERR(desc))
1906 return desc;
1907 }
1908
1909 if (info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001910 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001911
1912 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001913}
1914
Alexandre Courbotad824782013-12-03 12:20:11 +09001915static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1916{
1917 const char *dev_id = dev ? dev_name(dev) : NULL;
1918 struct gpiod_lookup_table *table;
1919
1920 mutex_lock(&gpio_lookup_lock);
1921
1922 list_for_each_entry(table, &gpio_lookup_list, list) {
1923 if (table->dev_id && dev_id) {
1924 /*
1925 * Valid strings on both ends, must be identical to have
1926 * a match
1927 */
1928 if (!strcmp(table->dev_id, dev_id))
1929 goto found;
1930 } else {
1931 /*
1932 * One of the pointers is NULL, so both must be to have
1933 * a match
1934 */
1935 if (dev_id == table->dev_id)
1936 goto found;
1937 }
1938 }
1939 table = NULL;
1940
1941found:
1942 mutex_unlock(&gpio_lookup_lock);
1943 return table;
1944}
1945
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001946static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001947 unsigned int idx,
1948 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001949{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001950 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001951 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001952 struct gpiod_lookup *p;
1953
Alexandre Courbotad824782013-12-03 12:20:11 +09001954 table = gpiod_find_lookup_table(dev);
1955 if (!table)
1956 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001957
Alexandre Courbotad824782013-12-03 12:20:11 +09001958 for (p = &table->table[0]; p->chip_label; p++) {
1959 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001960
Alexandre Courbotad824782013-12-03 12:20:11 +09001961 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001962 if (p->idx != idx)
1963 continue;
1964
Alexandre Courbotad824782013-12-03 12:20:11 +09001965 /* If the lookup entry has a con_id, require exact match */
1966 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1967 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001968
Alexandre Courbotad824782013-12-03 12:20:11 +09001969 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001970
Alexandre Courbotad824782013-12-03 12:20:11 +09001971 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001972 dev_err(dev, "cannot find GPIO chip %s\n",
1973 p->chip_label);
1974 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001975 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001976
Alexandre Courbotad824782013-12-03 12:20:11 +09001977 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001978 dev_err(dev,
1979 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1980 idx, chip->ngpio, chip->label);
1981 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001982 }
1983
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001984 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001985 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001986
1987 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001988 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001989
1990 return desc;
1991}
1992
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001993static int dt_gpio_count(struct device *dev, const char *con_id)
1994{
1995 int ret;
1996 char propname[32];
1997 unsigned int i;
1998
1999 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2000 if (con_id)
2001 snprintf(propname, sizeof(propname), "%s-%s",
2002 con_id, gpio_suffixes[i]);
2003 else
2004 snprintf(propname, sizeof(propname), "%s",
2005 gpio_suffixes[i]);
2006
2007 ret = of_gpio_named_count(dev->of_node, propname);
2008 if (ret >= 0)
2009 break;
2010 }
2011 return ret;
2012}
2013
2014static int platform_gpio_count(struct device *dev, const char *con_id)
2015{
2016 struct gpiod_lookup_table *table;
2017 struct gpiod_lookup *p;
2018 unsigned int count = 0;
2019
2020 table = gpiod_find_lookup_table(dev);
2021 if (!table)
2022 return -ENOENT;
2023
2024 for (p = &table->table[0]; p->chip_label; p++) {
2025 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2026 (!con_id && !p->con_id))
2027 count++;
2028 }
2029 if (!count)
2030 return -ENOENT;
2031
2032 return count;
2033}
2034
2035/**
2036 * gpiod_count - return the number of GPIOs associated with a device / function
2037 * or -ENOENT if no GPIO has been assigned to the requested function
2038 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2039 * @con_id: function within the GPIO consumer
2040 */
2041int gpiod_count(struct device *dev, const char *con_id)
2042{
2043 int count = -ENOENT;
2044
2045 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2046 count = dt_gpio_count(dev, con_id);
2047 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2048 count = acpi_gpio_count(dev, con_id);
2049
2050 if (count < 0)
2051 count = platform_gpio_count(dev, con_id);
2052
2053 return count;
2054}
2055EXPORT_SYMBOL_GPL(gpiod_count);
2056
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002057/**
Thierry Reding08791622014-04-25 16:54:22 +02002058 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002059 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002060 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002061 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002062 *
2063 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002064 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07002065 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002066 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002067struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002068 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002069{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002070 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002071}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002072EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002073
2074/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002075 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2076 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2077 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002078 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002079 *
2080 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2081 * the requested function it will return NULL. This is convenient for drivers
2082 * that need to handle optional GPIOs.
2083 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002084struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002085 const char *con_id,
2086 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002087{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002088 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002089}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002090EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002091
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002092/**
2093 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2094 * @desc: gpio to be setup
2095 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2096 * of_get_gpio_hog()
2097 *
2098 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2099 */
2100static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2101{
2102 if (lflags & GPIO_ACTIVE_LOW)
2103 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2104 if (lflags & GPIO_OPEN_DRAIN)
2105 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2106 if (lflags & GPIO_OPEN_SOURCE)
2107 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2108}
Benoit Parrotf625d462015-02-02 11:44:44 -06002109
2110/**
2111 * gpiod_configure_flags - helper function to configure a given GPIO
2112 * @desc: gpio whose value will be assigned
2113 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06002114 * @dflags: gpiod_flags - optional GPIO initialization flags
2115 *
2116 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2117 * requested function and/or index, or another IS_ERR() code if an error
2118 * occurred while trying to acquire the GPIO.
2119 */
2120static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002121 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06002122{
2123 int status;
2124
Benoit Parrotf625d462015-02-02 11:44:44 -06002125 /* No particular flag request, return here... */
2126 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2127 pr_debug("no flags found for %s\n", con_id);
2128 return 0;
2129 }
2130
2131 /* Process flags */
2132 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2133 status = gpiod_direction_output(desc,
2134 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2135 else
2136 status = gpiod_direction_input(desc);
2137
2138 return status;
2139}
2140
Thierry Reding29a1f2332014-04-25 17:10:06 +02002141/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002142 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002143 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002144 * @con_id: function within the GPIO consumer
2145 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002146 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002147 *
2148 * This variant of gpiod_get() allows to access GPIOs other than the first
2149 * defined one for functions that define several GPIOs.
2150 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002151 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2152 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07002153 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002154 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002155struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002156 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002157 unsigned int idx,
2158 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002159{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002160 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002161 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002162 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002163
2164 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2165
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002166 if (dev) {
2167 /* Using device tree? */
2168 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2169 dev_dbg(dev, "using device tree for GPIO lookup\n");
2170 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2171 } else if (ACPI_COMPANION(dev)) {
2172 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2173 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2174 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002175 }
2176
2177 /*
2178 * Either we are not using DT or ACPI, or their lookup did not return
2179 * a result. In that case, use platform lookup as a fallback.
2180 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002181 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002182 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002183 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002184 }
2185
2186 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002187 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002188 return desc;
2189 }
2190
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002191 gpiod_parse_flags(desc, lookupflags);
2192
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002193 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002194 if (status < 0)
2195 return ERR_PTR(status);
2196
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002197 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002198 if (status < 0) {
2199 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2200 gpiod_put(desc);
2201 return ERR_PTR(status);
2202 }
2203
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002204 return desc;
2205}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002206EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002207
2208/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002209 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2210 * @fwnode: handle of the firmware node
2211 * @propname: name of the firmware property representing the GPIO
2212 *
2213 * This function can be used for drivers that get their configuration
2214 * from firmware.
2215 *
2216 * Function properly finds the corresponding GPIO using whatever is the
2217 * underlying firmware interface and then makes sure that the GPIO
2218 * descriptor is requested before it is returned to the caller.
2219 *
2220 * In case of error an ERR_PTR() is returned.
2221 */
2222struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2223 const char *propname)
2224{
2225 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2226 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002227 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02002228 int ret;
2229
2230 if (!fwnode)
2231 return ERR_PTR(-EINVAL);
2232
2233 if (is_of_node(fwnode)) {
2234 enum of_gpio_flags flags;
2235
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002236 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002237 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002238 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02002239 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002240 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2241 }
Mika Westerberg40b73182014-10-21 13:33:59 +02002242 } else if (is_acpi_node(fwnode)) {
2243 struct acpi_gpio_info info;
2244
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02002245 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02002246 if (!IS_ERR(desc))
2247 active_low = info.active_low;
2248 }
2249
2250 if (IS_ERR(desc))
2251 return desc;
2252
Mika Westerberg40b73182014-10-21 13:33:59 +02002253 if (active_low)
2254 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2255
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002256 if (single_ended) {
2257 if (active_low)
2258 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2259 else
2260 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2261 }
2262
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002263 ret = gpiod_request(desc, NULL);
2264 if (ret)
2265 return ERR_PTR(ret);
2266
Mika Westerberg40b73182014-10-21 13:33:59 +02002267 return desc;
2268}
2269EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2270
2271/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002272 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2273 * function
2274 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2275 * @con_id: function within the GPIO consumer
2276 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002277 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002278 *
2279 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2280 * specified index was assigned to the requested function it will return NULL.
2281 * This is convenient for drivers that need to handle optional GPIOs.
2282 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002283struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002284 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002285 unsigned int index,
2286 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002287{
2288 struct gpio_desc *desc;
2289
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002290 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002291 if (IS_ERR(desc)) {
2292 if (PTR_ERR(desc) == -ENOENT)
2293 return NULL;
2294 }
2295
2296 return desc;
2297}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002298EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002299
2300/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002301 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2302 * @desc: gpio whose value will be assigned
2303 * @name: gpio line name
2304 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2305 * of_get_gpio_hog()
2306 * @dflags: gpiod_flags - optional GPIO initialization flags
2307 */
2308int gpiod_hog(struct gpio_desc *desc, const char *name,
2309 unsigned long lflags, enum gpiod_flags dflags)
2310{
2311 struct gpio_chip *chip;
2312 struct gpio_desc *local_desc;
2313 int hwnum;
2314 int status;
2315
2316 chip = gpiod_to_chip(desc);
2317 hwnum = gpio_chip_hwgpio(desc);
2318
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002319 gpiod_parse_flags(desc, lflags);
2320
Benoit Parrotf625d462015-02-02 11:44:44 -06002321 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2322 if (IS_ERR(local_desc)) {
Linus Walleija7138902015-06-05 11:36:10 +02002323 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2324 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002325 return PTR_ERR(local_desc);
2326 }
2327
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002328 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002329 if (status < 0) {
Linus Walleija7138902015-06-05 11:36:10 +02002330 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2331 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002332 gpiochip_free_own_desc(desc);
2333 return status;
2334 }
2335
2336 /* Mark GPIO as hogged so it can be identified and removed later */
2337 set_bit(FLAG_IS_HOGGED, &desc->flags);
2338
2339 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2340 desc_to_gpio(desc), name,
2341 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2342 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2343 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2344
2345 return 0;
2346}
2347
2348/**
2349 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2350 * @chip: gpio chip to act on
2351 *
2352 * This is only used by of_gpiochip_remove to free hogged gpios
2353 */
2354static void gpiochip_free_hogs(struct gpio_chip *chip)
2355{
2356 int id;
2357
2358 for (id = 0; id < chip->ngpio; id++) {
2359 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2360 gpiochip_free_own_desc(&chip->desc[id]);
2361 }
2362}
2363
2364/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002365 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2366 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2367 * @con_id: function within the GPIO consumer
2368 * @flags: optional GPIO initialization flags
2369 *
2370 * This function acquires all the GPIOs defined under a given function.
2371 *
2372 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2373 * no GPIO has been assigned to the requested function, or another IS_ERR()
2374 * code if an error occurred while trying to acquire the GPIOs.
2375 */
2376struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2377 const char *con_id,
2378 enum gpiod_flags flags)
2379{
2380 struct gpio_desc *desc;
2381 struct gpio_descs *descs;
2382 int count;
2383
2384 count = gpiod_count(dev, con_id);
2385 if (count < 0)
2386 return ERR_PTR(count);
2387
2388 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2389 GFP_KERNEL);
2390 if (!descs)
2391 return ERR_PTR(-ENOMEM);
2392
2393 for (descs->ndescs = 0; descs->ndescs < count; ) {
2394 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2395 if (IS_ERR(desc)) {
2396 gpiod_put_array(descs);
2397 return ERR_CAST(desc);
2398 }
2399 descs->desc[descs->ndescs] = desc;
2400 descs->ndescs++;
2401 }
2402 return descs;
2403}
2404EXPORT_SYMBOL_GPL(gpiod_get_array);
2405
2406/**
2407 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2408 * function
2409 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2410 * @con_id: function within the GPIO consumer
2411 * @flags: optional GPIO initialization flags
2412 *
2413 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2414 * assigned to the requested function it will return NULL.
2415 */
2416struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2417 const char *con_id,
2418 enum gpiod_flags flags)
2419{
2420 struct gpio_descs *descs;
2421
2422 descs = gpiod_get_array(dev, con_id, flags);
2423 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2424 return NULL;
2425
2426 return descs;
2427}
2428EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2429
2430/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002431 * gpiod_put - dispose of a GPIO descriptor
2432 * @desc: GPIO descriptor to dispose of
2433 *
2434 * No descriptor can be used after gpiod_put() has been called on it.
2435 */
2436void gpiod_put(struct gpio_desc *desc)
2437{
2438 gpiod_free(desc);
2439}
2440EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002441
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002442/**
2443 * gpiod_put_array - dispose of multiple GPIO descriptors
2444 * @descs: struct gpio_descs containing an array of descriptors
2445 */
2446void gpiod_put_array(struct gpio_descs *descs)
2447{
2448 unsigned int i;
2449
2450 for (i = 0; i < descs->ndescs; i++)
2451 gpiod_put(descs->desc[i]);
2452
2453 kfree(descs);
2454}
2455EXPORT_SYMBOL_GPL(gpiod_put_array);
2456
David Brownelld2876d02008-02-04 22:28:20 -08002457#ifdef CONFIG_DEBUG_FS
2458
2459static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2460{
2461 unsigned i;
2462 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002463 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002464 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002465 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002466
2467 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02002468 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2469 if (gdesc->name) {
2470 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2471 gpio, gdesc->name);
2472 }
David Brownelld2876d02008-02-04 22:28:20 -08002473 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02002474 }
David Brownelld2876d02008-02-04 22:28:20 -08002475
Alexandre Courbot372e7222013-02-03 01:29:29 +09002476 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002477 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002478 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02002479 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2480 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08002481 is_out ? "out" : "in ",
2482 chip->get
2483 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002484 : "? ",
2485 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002486 seq_printf(s, "\n");
2487 }
2488}
2489
Thierry Redingf9c4a312012-04-12 13:26:01 +02002490static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002491{
Grant Likely362432a2013-02-09 09:41:49 +00002492 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002493 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002494 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002495
2496 s->private = "";
2497
Grant Likely362432a2013-02-09 09:41:49 +00002498 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002499 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002500 if (index-- == 0) {
2501 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002502 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002503 }
2504 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002505
2506 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002507}
2508
2509static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2510{
Grant Likely362432a2013-02-09 09:41:49 +00002511 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002512 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002513 void *ret = NULL;
2514
Grant Likely362432a2013-02-09 09:41:49 +00002515 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002516 if (list_is_last(&chip->list, &gpio_chips))
2517 ret = NULL;
2518 else
2519 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002520 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002521
2522 s->private = "\n";
2523 ++*pos;
2524
2525 return ret;
2526}
2527
2528static void gpiolib_seq_stop(struct seq_file *s, void *v)
2529{
2530}
2531
2532static int gpiolib_seq_show(struct seq_file *s, void *v)
2533{
2534 struct gpio_chip *chip = v;
2535 struct device *dev;
2536
2537 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2538 chip->base, chip->base + chip->ngpio - 1);
Linus Walleij58383c72015-11-04 09:56:26 +01002539 dev = chip->parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002540 if (dev)
2541 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2542 dev_name(dev));
2543 if (chip->label)
2544 seq_printf(s, ", %s", chip->label);
2545 if (chip->can_sleep)
2546 seq_printf(s, ", can sleep");
2547 seq_printf(s, ":\n");
2548
2549 if (chip->dbg_show)
2550 chip->dbg_show(s, chip);
2551 else
2552 gpiolib_dbg_show(s, chip);
2553
David Brownelld2876d02008-02-04 22:28:20 -08002554 return 0;
2555}
2556
Thierry Redingf9c4a312012-04-12 13:26:01 +02002557static const struct seq_operations gpiolib_seq_ops = {
2558 .start = gpiolib_seq_start,
2559 .next = gpiolib_seq_next,
2560 .stop = gpiolib_seq_stop,
2561 .show = gpiolib_seq_show,
2562};
2563
David Brownelld2876d02008-02-04 22:28:20 -08002564static int gpiolib_open(struct inode *inode, struct file *file)
2565{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002566 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002567}
2568
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002569static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002570 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002571 .open = gpiolib_open,
2572 .read = seq_read,
2573 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002574 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002575};
2576
2577static int __init gpiolib_debugfs_init(void)
2578{
2579 /* /sys/kernel/debug/gpio */
2580 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2581 NULL, NULL, &gpiolib_operations);
2582 return 0;
2583}
2584subsys_initcall(gpiolib_debugfs_init);
2585
2586#endif /* DEBUG_FS */