blob: ca6630207c66c5ee7736347aa2968170657f353a [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/**
David Brownelld2876d02008-02-04 22:28:20 -0800304 * gpiochip_add() - register a gpio_chip
305 * @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 *
David Brownelld8f388d2008-07-25 01:46:07 -0700312 * When gpiochip_add() 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 */
320int gpiochip_add(struct gpio_chip *chip)
321{
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
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800332 if (chip->ngpio == 0) {
333 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
334 return -EINVAL;
335 }
336
David Brownelld2876d02008-02-04 22:28:20 -0800337 spin_lock_irqsave(&gpio_lock, flags);
338
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700339 if (base < 0) {
340 base = gpiochip_find_base(chip->ngpio);
341 if (base < 0) {
342 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100343 spin_unlock_irqrestore(&gpio_lock, flags);
344 goto err_free_descs;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700345 }
346 chip->base = base;
347 }
348
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900349 status = gpiochip_add_to_list(chip);
Johan Hovold05aa5202015-01-12 17:12:26 +0100350 if (status) {
351 spin_unlock_irqrestore(&gpio_lock, flags);
352 goto err_free_descs;
353 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900354
Johan Hovold05aa5202015-01-12 17:12:26 +0100355 for (id = 0; id < chip->ngpio; id++) {
356 struct gpio_desc *desc = &descs[id];
David Brownelld8f388d2008-07-25 01:46:07 -0700357
Johan Hovold05aa5202015-01-12 17:12:26 +0100358 desc->chip = chip;
359
360 /* REVISIT: most hardware initializes GPIOs as inputs (often
361 * with pullups enabled) so power usage is minimized. Linux
362 * code should set the gpio direction first thing; but until
363 * it does, and in case chip->get_direction is not set, we may
364 * expose the wrong direction in sysfs.
365 */
366 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800367 }
368
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900369 chip->desc = descs;
370
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800371 spin_unlock_irqrestore(&gpio_lock, flags);
372
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530373#ifdef CONFIG_PINCTRL
374 INIT_LIST_HEAD(&chip->pin_ranges);
375#endif
376
Linus Walleij58383c72015-11-04 09:56:26 +0100377 if (!chip->owner && chip->parent && chip->parent->driver)
378 chip->owner = chip->parent->driver->owner;
Grygorii Strashko37269602015-06-25 20:30:51 +0300379
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200380 status = gpiochip_set_desc_names(chip);
381 if (status)
382 goto err_remove_from_list;
383
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200384 status = of_gpiochip_add(chip);
385 if (status)
386 goto err_remove_chip;
387
Mika Westerberg664e3e52014-01-08 12:40:54 +0200388 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600389
Johan Hovold426577b2015-05-04 17:10:32 +0200390 status = gpiochip_sysfs_register(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100391 if (status)
392 goto err_remove_chip;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600393
Andy Shevchenko7589e592013-12-05 11:26:23 +0200394 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700395 chip->base, chip->base + chip->ngpio - 1,
396 chip->label ? : "generic");
397
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600398 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800399
Johan Hovold225fce82015-01-12 17:12:25 +0100400err_remove_chip:
401 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200402 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100403 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200404err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100405 spin_lock_irqsave(&gpio_lock, flags);
406 list_del(&chip->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800407 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold05aa5202015-01-12 17:12:26 +0100408 chip->desc = NULL;
Johan Hovold225fce82015-01-12 17:12:25 +0100409err_free_descs:
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900410 kfree(descs);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900411
David Brownelld2876d02008-02-04 22:28:20 -0800412 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200413 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600414 chip->base, chip->base + chip->ngpio - 1,
415 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800416 return status;
417}
418EXPORT_SYMBOL_GPL(gpiochip_add);
419
420/**
421 * gpiochip_remove() - unregister a gpio_chip
422 * @chip: the chip to unregister
423 *
424 * A gpio_chip with any GPIOs still requested may not be removed.
425 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200426void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800427{
Johan Hovoldfab28b82015-05-04 17:10:27 +0200428 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800429 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800430 unsigned id;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200431 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800432
Johan Hovold426577b2015-05-04 17:10:32 +0200433 gpiochip_sysfs_unregister(chip);
Johan Hovold01cca932015-01-12 17:12:29 +0100434
Johan Hovold00acc3d2015-01-12 17:12:27 +0100435 gpiochip_irqchip_remove(chip);
436
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200437 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100438 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600439 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600440 of_gpiochip_remove(chip);
441
Johan Hovold6798aca2015-01-12 17:12:28 +0100442 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900443 for (id = 0; id < chip->ngpio; id++) {
Johan Hovoldfab28b82015-05-04 17:10:27 +0200444 desc = &chip->desc[id];
445 desc->chip = NULL;
446 if (test_bit(FLAG_REQUESTED, &desc->flags))
447 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800448 }
abdoulaye berthee1db1702014-07-05 18:28:50 +0200449 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800450 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900451
Johan Hovoldfab28b82015-05-04 17:10:27 +0200452 if (requested)
Linus Walleij58383c72015-11-04 09:56:26 +0100453 dev_crit(chip->parent,
454 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +0200455
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900456 kfree(chip->desc);
457 chip->desc = NULL;
David Brownelld2876d02008-02-04 22:28:20 -0800458}
459EXPORT_SYMBOL_GPL(gpiochip_remove);
460
Grant Likely594fa262010-06-08 07:48:16 -0600461/**
462 * gpiochip_find() - iterator for locating a specific gpio_chip
463 * @data: data to pass to match function
464 * @callback: Callback function to check gpio_chip
465 *
466 * Similar to bus_find_device. It returns a reference to a gpio_chip as
467 * determined by a user supplied @match callback. The callback should return
468 * 0 if the device doesn't match and non-zero if it does. If the callback is
469 * non-zero, this function will return to the caller and not iterate over any
470 * more gpio_chips.
471 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600472struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700473 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600474 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600475{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900476 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600477 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600478
479 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900480 list_for_each_entry(chip, &gpio_chips, list)
481 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600482 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900483
484 /* No match? */
485 if (&chip->list == &gpio_chips)
486 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600487 spin_unlock_irqrestore(&gpio_lock, flags);
488
489 return chip;
490}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600491EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800492
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900493static int gpiochip_match_name(struct gpio_chip *chip, void *data)
494{
495 const char *name = data;
496
497 return !strcmp(chip->label, name);
498}
499
500static struct gpio_chip *find_chip_by_name(const char *name)
501{
502 return gpiochip_find((void *)name, gpiochip_match_name);
503}
504
Linus Walleij14250522014-03-25 10:40:18 +0100505#ifdef CONFIG_GPIOLIB_IRQCHIP
506
507/*
508 * The following is irqchip helper code for gpiochips.
509 */
510
511/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200512 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
513 * @gpiochip: the gpiochip to set the irqchip chain to
514 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100515 * @parent_irq: the irq number corresponding to the parent IRQ for this
516 * chained irqchip
517 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200518 * coming out of the gpiochip. If the interrupt is nested rather than
519 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100520 */
521void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
522 struct irq_chip *irqchip,
523 int parent_irq,
524 irq_flow_handler_t parent_handler)
525{
Linus Walleij83141a72014-09-26 13:50:12 +0200526 unsigned int offset;
527
Linus Walleij83141a72014-09-26 13:50:12 +0200528 if (!gpiochip->irqdomain) {
529 chip_err(gpiochip, "called %s before setting up irqchip\n",
530 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200531 return;
532 }
533
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200534 if (parent_handler) {
535 if (gpiochip->can_sleep) {
536 chip_err(gpiochip,
537 "you cannot have chained interrupts on a "
538 "chip that may sleep\n");
539 return;
540 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200541 /*
542 * The parent irqchip is already using the chip_data for this
543 * irqchip, so our callbacks simply use the handler_data.
544 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +0200545 irq_set_chained_handler_and_data(parent_irq, parent_handler,
546 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300547
548 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200549 }
Linus Walleij83141a72014-09-26 13:50:12 +0200550
551 /* Set the parent IRQ for all affected IRQs */
552 for (offset = 0; offset < gpiochip->ngpio; offset++)
553 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
554 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100555}
556EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
557
558/**
559 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
560 * @d: the irqdomain used by this irqchip
561 * @irq: the global irq number used by this GPIO irqchip irq
562 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
563 *
564 * This function will set up the mapping for a certain IRQ line on a
565 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
566 * stored inside the gpiochip.
567 */
568static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
569 irq_hw_number_t hwirq)
570{
571 struct gpio_chip *chip = d->host_data;
572
Linus Walleij14250522014-03-25 10:40:18 +0100573 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300574 /*
575 * This lock class tells lockdep that GPIO irqs are in a different
576 * category than their parents, so it won't report false recursion.
577 */
578 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +0200579 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200580 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300581 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200582 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100583 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -0500584
Linus Walleij1333b902014-04-23 16:45:12 +0200585 /*
586 * No set-up of the hardware will happen if IRQ_TYPE_NONE
587 * is passed as default type.
588 */
589 if (chip->irq_default_type != IRQ_TYPE_NONE)
590 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100591
592 return 0;
593}
594
Linus Walleijc3626fd2014-03-28 20:42:01 +0100595static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
596{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200597 struct gpio_chip *chip = d->host_data;
598
Linus Walleij1c8732b2014-04-09 13:34:39 +0200599 if (chip->can_sleep)
600 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100601 irq_set_chip_and_handler(irq, NULL, NULL);
602 irq_set_chip_data(irq, NULL);
603}
604
Linus Walleij14250522014-03-25 10:40:18 +0100605static const struct irq_domain_ops gpiochip_domain_ops = {
606 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100607 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100608 /* Virtually all GPIO irqchips are twocell:ed */
609 .xlate = irq_domain_xlate_twocell,
610};
611
612static int gpiochip_irq_reqres(struct irq_data *d)
613{
614 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
615
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300616 if (!try_module_get(chip->owner))
617 return -ENODEV;
618
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900619 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100620 chip_err(chip,
621 "unable to lock HW IRQ %lu for IRQ\n",
622 d->hwirq);
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300623 module_put(chip->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100624 return -EINVAL;
625 }
626 return 0;
627}
628
629static void gpiochip_irq_relres(struct irq_data *d)
630{
631 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
632
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900633 gpiochip_unlock_as_irq(chip, d->hwirq);
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300634 module_put(chip->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100635}
636
637static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
638{
639 return irq_find_mapping(chip->irqdomain, offset);
640}
641
642/**
643 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
644 * @gpiochip: the gpiochip to remove the irqchip from
645 *
646 * This is called only from gpiochip_remove()
647 */
648static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
649{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100650 unsigned int offset;
651
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300652 acpi_gpiochip_free_interrupts(gpiochip);
653
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300654 if (gpiochip->irq_parent) {
655 irq_set_chained_handler(gpiochip->irq_parent, NULL);
656 irq_set_handler_data(gpiochip->irq_parent, NULL);
657 }
658
Linus Walleijc3626fd2014-03-28 20:42:01 +0100659 /* Remove all IRQ mappings and delete the domain */
660 if (gpiochip->irqdomain) {
661 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300662 irq_dispose_mapping(
663 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100664 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100665 }
Linus Walleij14250522014-03-25 10:40:18 +0100666
667 if (gpiochip->irqchip) {
668 gpiochip->irqchip->irq_request_resources = NULL;
669 gpiochip->irqchip->irq_release_resources = NULL;
670 gpiochip->irqchip = NULL;
671 }
672}
673
674/**
675 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
676 * @gpiochip: the gpiochip to add the irqchip to
677 * @irqchip: the irqchip to add to the gpiochip
678 * @first_irq: if not dynamically assigned, the base (first) IRQ to
679 * allocate gpiochip irqs from
680 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200681 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
682 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300683 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +0100684 *
685 * This function closely associates a certain irqchip with a certain
686 * gpiochip, providing an irq domain to translate the local IRQs to
687 * global irqs in the gpiolib core, and making sure that the gpiochip
688 * is passed as chip data to all related functions. Driver callbacks
689 * need to use container_of() to get their local state containers back
690 * from the gpiochip passed as chip data. An irqdomain will be stored
691 * in the gpiochip that shall be used by the driver to handle IRQ number
692 * translation. The gpiochip will need to be initialized and registered
693 * before calling this function.
694 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100695 * This function will handle two cell:ed simple IRQs and assumes all
696 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100697 * need to be open coded.
698 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300699int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
700 struct irq_chip *irqchip,
701 unsigned int first_irq,
702 irq_flow_handler_t handler,
703 unsigned int type,
704 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +0100705{
706 struct device_node *of_node;
707 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100708 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100709
710 if (!gpiochip || !irqchip)
711 return -EINVAL;
712
Linus Walleij58383c72015-11-04 09:56:26 +0100713 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +0100714 pr_err("missing gpiochip .dev parent pointer\n");
715 return -EINVAL;
716 }
Linus Walleij58383c72015-11-04 09:56:26 +0100717 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +0100718#ifdef CONFIG_OF_GPIO
719 /*
Colin Cronin20a8a962015-05-18 11:41:43 -0700720 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800721 * FIXME: get rid of this and use gpiochip->parent->of_node
722 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +0100723 */
724 if (gpiochip->of_node)
725 of_node = gpiochip->of_node;
726#endif
727 gpiochip->irqchip = irqchip;
728 gpiochip->irq_handler = handler;
729 gpiochip->irq_default_type = type;
730 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300731 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +0100732 gpiochip->irqdomain = irq_domain_add_simple(of_node,
733 gpiochip->ngpio, first_irq,
734 &gpiochip_domain_ops, gpiochip);
735 if (!gpiochip->irqdomain) {
736 gpiochip->irqchip = NULL;
737 return -EINVAL;
738 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +0200739
740 /*
741 * It is possible for a driver to override this, but only if the
742 * alternative functions are both implemented.
743 */
744 if (!irqchip->irq_request_resources &&
745 !irqchip->irq_release_resources) {
746 irqchip->irq_request_resources = gpiochip_irq_reqres;
747 irqchip->irq_release_resources = gpiochip_irq_relres;
748 }
Linus Walleij14250522014-03-25 10:40:18 +0100749
750 /*
751 * Prepare the mapping since the irqchip shall be orthogonal to
752 * any gpiochip calls. If the first_irq was zero, this is
753 * necessary to allocate descriptors for all IRQs.
754 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100755 for (offset = 0; offset < gpiochip->ngpio; offset++) {
756 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
757 if (offset == 0)
758 /*
759 * Store the base into the gpiochip to be used when
760 * unmapping the irqs.
761 */
762 gpiochip->irq_base = irq_base;
763 }
Linus Walleij14250522014-03-25 10:40:18 +0100764
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300765 acpi_gpiochip_request_interrupts(gpiochip);
766
Linus Walleij14250522014-03-25 10:40:18 +0100767 return 0;
768}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300769EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +0100770
771#else /* CONFIG_GPIOLIB_IRQCHIP */
772
773static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
774
775#endif /* CONFIG_GPIOLIB_IRQCHIP */
776
Jonas Gorskic771c2f2015-10-11 17:34:15 +0200777/**
778 * gpiochip_generic_request() - request the gpio function for a pin
779 * @chip: the gpiochip owning the GPIO
780 * @offset: the offset of the GPIO to request for GPIO function
781 */
782int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
783{
784 return pinctrl_request_gpio(chip->base + offset);
785}
786EXPORT_SYMBOL_GPL(gpiochip_generic_request);
787
788/**
789 * gpiochip_generic_free() - free the gpio function from a pin
790 * @chip: the gpiochip to request the gpio function for
791 * @offset: the offset of the GPIO to free from GPIO function
792 */
793void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
794{
795 pinctrl_free_gpio(chip->base + offset);
796}
797EXPORT_SYMBOL_GPL(gpiochip_generic_free);
798
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530799#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100800
Linus Walleij3f0f8672012-11-20 12:40:15 +0100801/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200802 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
803 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +0200804 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +0200805 * @gpio_offset: the start offset in the current gpio_chip number space
806 * @pin_group: name of the pin group inside the pin controller
807 */
808int gpiochip_add_pingroup_range(struct gpio_chip *chip,
809 struct pinctrl_dev *pctldev,
810 unsigned int gpio_offset, const char *pin_group)
811{
812 struct gpio_pin_range *pin_range;
813 int ret;
814
815 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
816 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200817 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200818 return -ENOMEM;
819 }
820
821 /* Use local offset as range ID */
822 pin_range->range.id = gpio_offset;
823 pin_range->range.gc = chip;
824 pin_range->range.name = chip->label;
825 pin_range->range.base = chip->base + gpio_offset;
826 pin_range->pctldev = pctldev;
827
828 ret = pinctrl_get_group_pins(pctldev, pin_group,
829 &pin_range->range.pins,
830 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100831 if (ret < 0) {
832 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200833 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100834 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200835
836 pinctrl_add_gpio_range(pctldev, &pin_range->range);
837
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200838 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
839 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200840 pinctrl_dev_get_devname(pctldev), pin_group);
841
842 list_add_tail(&pin_range->node, &chip->pin_ranges);
843
844 return 0;
845}
846EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
847
848/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100849 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
850 * @chip: the gpiochip to add the range for
851 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100852 * @gpio_offset: the start offset in the current gpio_chip number space
853 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100854 * @npins: the number of pins from the offset of each pin space (GPIO and
855 * pin controller) to accumulate in this range
856 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100857int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100858 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100859 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530860{
861 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800862 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530863
Linus Walleij3f0f8672012-11-20 12:40:15 +0100864 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530865 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200866 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100867 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530868 }
869
Linus Walleij3f0f8672012-11-20 12:40:15 +0100870 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100871 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100872 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530873 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100874 pin_range->range.base = chip->base + gpio_offset;
875 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530876 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100877 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530878 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100879 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800880 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200881 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100882 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800883 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100884 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200885 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
886 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100887 pinctl_name,
888 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530889
890 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100891
892 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530893}
Linus Walleij165adc92012-11-06 14:49:39 +0100894EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530895
Linus Walleij3f0f8672012-11-20 12:40:15 +0100896/**
897 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
898 * @chip: the chip to remove all the mappings for
899 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530900void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
901{
902 struct gpio_pin_range *pin_range, *tmp;
903
904 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
905 list_del(&pin_range->node);
906 pinctrl_remove_gpio_range(pin_range->pctldev,
907 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100908 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530909 }
910}
Linus Walleij165adc92012-11-06 14:49:39 +0100911EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
912
913#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530914
David Brownelld2876d02008-02-04 22:28:20 -0800915/* These "optional" allocation calls help prevent drivers from stomping
916 * on each other, and help provide better diagnostics in debugfs.
917 * They're called even less than the "set direction" calls.
918 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200919static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800920{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200921 struct gpio_chip *chip = desc->chip;
922 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800923 unsigned long flags;
924
925 spin_lock_irqsave(&gpio_lock, flags);
926
David Brownelld2876d02008-02-04 22:28:20 -0800927 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700928 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800929 */
930
931 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
932 desc_set_label(desc, label ? : "?");
933 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700934 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800935 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800936 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700937 }
938
939 if (chip->request) {
940 /* chip->request may sleep */
941 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900942 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700943 spin_lock_irqsave(&gpio_lock, flags);
944
945 if (status < 0) {
946 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700947 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300948 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700949 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700950 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300951 if (chip->get_direction) {
952 /* chip->get_direction may sleep */
953 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900954 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300955 spin_lock_irqsave(&gpio_lock, flags);
956 }
David Brownelld2876d02008-02-04 22:28:20 -0800957done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +0300958 if (status < 0) {
959 /* Clear flags that might have been set by the caller before
960 * requesting the GPIO.
961 */
962 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
963 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
964 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
965 }
Mika Westerberg77c2d792014-03-10 14:54:50 +0200966 spin_unlock_irqrestore(&gpio_lock, flags);
967 return status;
968}
969
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900970int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200971{
972 int status = -EPROBE_DEFER;
973 struct gpio_chip *chip;
974
975 if (!desc) {
976 pr_warn("%s: invalid GPIO\n", __func__);
977 return -EINVAL;
978 }
979
980 chip = desc->chip;
981 if (!chip)
982 goto done;
983
984 if (try_module_get(chip->owner)) {
985 status = __gpiod_request(desc, label);
986 if (status < 0)
987 module_put(chip->owner);
988 }
989
990done:
David Brownelld2876d02008-02-04 22:28:20 -0800991 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200992 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200993
David Brownelld2876d02008-02-04 22:28:20 -0800994 return status;
995}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900996
Mika Westerberg77c2d792014-03-10 14:54:50 +0200997static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800998{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200999 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001000 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001001 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001002
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001003 might_sleep();
1004
Alexandre Courbot372e7222013-02-03 01:29:29 +09001005 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001006
David Brownelld2876d02008-02-04 22:28:20 -08001007 spin_lock_irqsave(&gpio_lock, flags);
1008
David Brownell35e8bb52008-10-15 22:03:16 -07001009 chip = desc->chip;
1010 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1011 if (chip->free) {
1012 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001013 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001014 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001015 spin_lock_irqsave(&gpio_lock, flags);
1016 }
David Brownelld2876d02008-02-04 22:28:20 -08001017 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001018 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001019 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301020 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301021 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001022 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001023 ret = true;
1024 }
David Brownelld2876d02008-02-04 22:28:20 -08001025
1026 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001027 return ret;
1028}
1029
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001030void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001031{
1032 if (desc && __gpiod_free(desc))
1033 module_put(desc->chip->owner);
1034 else
1035 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -08001036}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001037
David Brownelld2876d02008-02-04 22:28:20 -08001038/**
1039 * gpiochip_is_requested - return string iff signal was requested
1040 * @chip: controller managing the signal
1041 * @offset: of signal within controller's 0..(ngpio - 1) range
1042 *
1043 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001044 * The string returned is the label passed to gpio_request(); if none has been
1045 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001046 *
1047 * This function is for use by GPIO controller drivers. The label can
1048 * help with diagnostics, and knowing that the signal is used as a GPIO
1049 * can help avoid accidentally multiplexing it to another controller.
1050 */
1051const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1052{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001053 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001054
Dirk Behme48b59532015-08-18 18:02:32 +02001055 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001056 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001057
1058 desc = &chip->desc[offset];
1059
Alexandre Courbot372e7222013-02-03 01:29:29 +09001060 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001061 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001062 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001063}
1064EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1065
Mika Westerberg77c2d792014-03-10 14:54:50 +02001066/**
1067 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1068 * @desc: GPIO descriptor to request
1069 * @label: label for the GPIO
1070 *
1071 * Function allows GPIO chip drivers to request and use their own GPIO
1072 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1073 * function will not increase reference count of the GPIO chip module. This
1074 * allows the GPIO chip module to be unloaded as needed (we assume that the
1075 * GPIO chip driver handles freeing the GPIOs it has requested).
1076 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001077struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1078 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001079{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001080 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1081 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001082
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001083 if (IS_ERR(desc)) {
1084 chip_err(chip, "failed to get GPIO descriptor\n");
1085 return desc;
1086 }
1087
1088 err = __gpiod_request(desc, label);
1089 if (err < 0)
1090 return ERR_PTR(err);
1091
1092 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001093}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001094EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001095
1096/**
1097 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1098 * @desc: GPIO descriptor to free
1099 *
1100 * Function frees the given GPIO requested previously with
1101 * gpiochip_request_own_desc().
1102 */
1103void gpiochip_free_own_desc(struct gpio_desc *desc)
1104{
1105 if (desc)
1106 __gpiod_free(desc);
1107}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001108EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08001109
1110/* Drivers MUST set GPIO direction before making get/set calls. In
1111 * some cases this is done in early boot, before IRQs are enabled.
1112 *
1113 * As a rule these aren't called more than once (except for drivers
1114 * using the open-drain emulation idiom) so these are natural places
1115 * to accumulate extra debugging checks. Note that we can't (yet)
1116 * rely on gpio_request() having been called beforehand.
1117 */
1118
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001119/**
1120 * gpiod_direction_input - set the GPIO direction to input
1121 * @desc: GPIO to set to input
1122 *
1123 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1124 * be called safely on it.
1125 *
1126 * Return 0 in case of success, else an error code.
1127 */
1128int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001129{
David Brownelld2876d02008-02-04 22:28:20 -08001130 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001131 int status = -EINVAL;
1132
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001133 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001134 pr_warn("%s: invalid GPIO\n", __func__);
1135 return -EINVAL;
1136 }
1137
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001138 chip = desc->chip;
1139 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001140 gpiod_warn(desc,
1141 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001142 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001143 return -EIO;
1144 }
1145
Alexandre Courbotd82da792014-07-22 16:17:43 +09001146 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001147 if (status == 0)
1148 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001149
Alexandre Courbot372e7222013-02-03 01:29:29 +09001150 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001151
David Brownelld2876d02008-02-04 22:28:20 -08001152 return status;
1153}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001154EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001155
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001156static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001157{
David Brownelld2876d02008-02-04 22:28:20 -08001158 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001159 int status = -EINVAL;
1160
Linus Walleijd468bf92013-09-24 11:54:38 +02001161 /* GPIOs used for IRQs shall not be set as output */
1162 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1163 gpiod_err(desc,
1164 "%s: tried to set a GPIO tied to an IRQ as output\n",
1165 __func__);
1166 return -EIO;
1167 }
1168
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301169 /* Open drain pin should not be driven to 1 */
1170 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001171 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301172
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301173 /* Open source pin should not be driven to 0 */
1174 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001175 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301176
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001177 chip = desc->chip;
1178 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001179 gpiod_warn(desc,
1180 "%s: missing set() or direction_output() operations\n",
1181 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001182 return -EIO;
1183 }
1184
Alexandre Courbotd82da792014-07-22 16:17:43 +09001185 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001186 if (status == 0)
1187 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001188 trace_gpio_value(desc_to_gpio(desc), 0, value);
1189 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001190 return status;
1191}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001192
1193/**
1194 * gpiod_direction_output_raw - set the GPIO direction to output
1195 * @desc: GPIO to set to output
1196 * @value: initial output value of the GPIO
1197 *
1198 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1199 * be called safely on it. The initial value of the output must be specified
1200 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1201 *
1202 * Return 0 in case of success, else an error code.
1203 */
1204int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1205{
1206 if (!desc || !desc->chip) {
1207 pr_warn("%s: invalid GPIO\n", __func__);
1208 return -EINVAL;
1209 }
1210 return _gpiod_direction_output_raw(desc, value);
1211}
1212EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1213
1214/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301215 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001216 * @desc: GPIO to set to output
1217 * @value: initial output value of the GPIO
1218 *
1219 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1220 * be called safely on it. The initial value of the output must be specified
1221 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1222 * account.
1223 *
1224 * Return 0 in case of success, else an error code.
1225 */
1226int gpiod_direction_output(struct gpio_desc *desc, int value)
1227{
1228 if (!desc || !desc->chip) {
1229 pr_warn("%s: invalid GPIO\n", __func__);
1230 return -EINVAL;
1231 }
1232 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1233 value = !value;
1234 return _gpiod_direction_output_raw(desc, value);
1235}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001236EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001237
Felipe Balbic4b5be92010-05-26 14:42:23 -07001238/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001239 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001240 * @gpio: the gpio to set debounce time
1241 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001242 *
1243 * returns -ENOTSUPP if the controller does not support setting
1244 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001245 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001246int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001247{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001248 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001249
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001250 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001251 pr_warn("%s: invalid GPIO\n", __func__);
1252 return -EINVAL;
1253 }
1254
Felipe Balbic4b5be92010-05-26 14:42:23 -07001255 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001256 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001257 gpiod_dbg(desc,
1258 "%s: missing set() or set_debounce() operations\n",
1259 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001260 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001261 }
1262
Alexandre Courbotd82da792014-07-22 16:17:43 +09001263 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001264}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001265EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001266
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001267/**
1268 * gpiod_is_active_low - test whether a GPIO is active-low or not
1269 * @desc: the gpio descriptor to test
1270 *
1271 * Returns 1 if the GPIO is active-low, 0 otherwise.
1272 */
1273int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001274{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001275 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001276}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001277EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001278
1279/* I/O calls are only valid after configuration completed; the relevant
1280 * "is this a valid GPIO" error checks should already have been done.
1281 *
1282 * "Get" operations are often inlinable as reading a pin value register,
1283 * and masking the relevant bit in that register.
1284 *
1285 * When "set" operations are inlinable, they involve writing that mask to
1286 * one register to set a low value, or a different register to set it high.
1287 * Otherwise locking is needed, so there may be little value to inlining.
1288 *
1289 *------------------------------------------------------------------------
1290 *
1291 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1292 * have requested the GPIO. That can include implicit requesting by
1293 * a direction setting call. Marking a gpio as requested locks its chip
1294 * in memory, guaranteeing that these table lookups need no more locking
1295 * and that gpiochip_remove() will fail.
1296 *
1297 * REVISIT when debugging, consider adding some instrumentation to ensure
1298 * that the GPIO was actually requested.
1299 */
1300
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001301static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001302{
1303 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001304 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001305 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001306
Alexandre Courbot372e7222013-02-03 01:29:29 +09001307 chip = desc->chip;
1308 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001309 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01001310 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001311 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001312 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001313}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001314
David Brownelld2876d02008-02-04 22:28:20 -08001315/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001316 * gpiod_get_raw_value() - return a gpio's raw value
1317 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001318 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001319 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001320 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001321 *
1322 * This function should be called from contexts where we cannot sleep, and will
1323 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001324 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001325int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001326{
David Brownelld2876d02008-02-04 22:28:20 -08001327 if (!desc)
1328 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001329 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001330 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001331 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001332}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001333EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001334
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001335/**
1336 * gpiod_get_value() - return a gpio's value
1337 * @desc: gpio whose value will be returned
1338 *
1339 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001340 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001341 *
1342 * This function should be called from contexts where we cannot sleep, and will
1343 * complain if the GPIO chip functions potentially sleep.
1344 */
1345int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001346{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001347 int value;
1348 if (!desc)
1349 return 0;
1350 /* Should be using gpio_get_value_cansleep() */
1351 WARN_ON(desc->chip->can_sleep);
1352
1353 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001354 if (value < 0)
1355 return value;
1356
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001357 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1358 value = !value;
1359
1360 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001361}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001362EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001363
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301364/*
1365 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001366 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001367 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301368 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001369static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301370{
1371 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001372 struct gpio_chip *chip = desc->chip;
1373 int offset = gpio_chip_hwgpio(desc);
1374
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301375 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001376 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301377 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001378 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301379 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001380 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301381 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001382 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301383 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001384 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301385 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001386 gpiod_err(desc,
1387 "%s: Error in set_value for open drain err %d\n",
1388 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301389}
1390
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301391/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001392 * _gpio_set_open_source_value() - Set the open source gpio's value.
1393 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001394 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301395 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001396static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301397{
1398 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001399 struct gpio_chip *chip = desc->chip;
1400 int offset = gpio_chip_hwgpio(desc);
1401
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301402 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001403 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301404 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001405 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301406 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001407 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301408 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001409 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301410 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001411 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301412 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001413 gpiod_err(desc,
1414 "%s: Error in set_value for open source err %d\n",
1415 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301416}
1417
Alexandre Courbot23600962014-03-11 15:52:09 +09001418static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001419{
1420 struct gpio_chip *chip;
1421
Alexandre Courbot372e7222013-02-03 01:29:29 +09001422 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001423 trace_gpio_value(desc_to_gpio(desc), 0, value);
1424 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1425 _gpio_set_open_drain_value(desc, value);
1426 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1427 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301428 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001429 chip->set(chip, gpio_chip_hwgpio(desc), value);
1430}
1431
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001432/*
1433 * set multiple outputs on the same chip;
1434 * use the chip's set_multiple function if available;
1435 * otherwise set the outputs sequentially;
1436 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1437 * defines which outputs are to be changed
1438 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1439 * defines the values the outputs specified by mask are to be set to
1440 */
1441static void gpio_chip_set_multiple(struct gpio_chip *chip,
1442 unsigned long *mask, unsigned long *bits)
1443{
1444 if (chip->set_multiple) {
1445 chip->set_multiple(chip, mask, bits);
1446 } else {
1447 int i;
1448 for (i = 0; i < chip->ngpio; i++) {
1449 if (mask[BIT_WORD(i)] == 0) {
1450 /* no more set bits in this mask word;
1451 * skip ahead to the next word */
1452 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1453 continue;
1454 }
1455 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001456 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001457 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001458 }
1459 }
1460}
1461
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001462static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1463 unsigned int array_size,
1464 struct gpio_desc **desc_array,
1465 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001466{
1467 int i = 0;
1468
1469 while (i < array_size) {
1470 struct gpio_chip *chip = desc_array[i]->chip;
1471 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1472 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1473 int count = 0;
1474
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001475 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001476 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001477
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001478 memset(mask, 0, sizeof(mask));
1479 do {
1480 struct gpio_desc *desc = desc_array[i];
1481 int hwgpio = gpio_chip_hwgpio(desc);
1482 int value = value_array[i];
1483
1484 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1485 value = !value;
1486 trace_gpio_value(desc_to_gpio(desc), 0, value);
1487 /*
1488 * collect all normal outputs belonging to the same chip
1489 * open drain and open source outputs are set individually
1490 */
1491 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001492 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001493 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1494 _gpio_set_open_source_value(desc, value);
1495 } else {
1496 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001497 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001498 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001499 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001500 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001501 count++;
1502 }
1503 i++;
1504 } while ((i < array_size) && (desc_array[i]->chip == chip));
1505 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001506 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001507 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001508 }
1509}
1510
David Brownelld2876d02008-02-04 22:28:20 -08001511/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001512 * gpiod_set_raw_value() - assign a gpio's raw value
1513 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001514 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001515 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001516 * Set the raw value of the GPIO, i.e. the value of its physical line without
1517 * regard for its ACTIVE_LOW status.
1518 *
1519 * This function should be called from contexts where we cannot sleep, and will
1520 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001521 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001522void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001523{
David Brownelld2876d02008-02-04 22:28:20 -08001524 if (!desc)
1525 return;
David Brownelld2876d02008-02-04 22:28:20 -08001526 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001527 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001528 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001529}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001530EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001531
1532/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001533 * gpiod_set_value() - assign a gpio's value
1534 * @desc: gpio whose value will be assigned
1535 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001536 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001537 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1538 * account
1539 *
1540 * This function should be called from contexts where we cannot sleep, and will
1541 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001542 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001543void gpiod_set_value(struct gpio_desc *desc, int value)
1544{
1545 if (!desc)
1546 return;
1547 /* Should be using gpio_set_value_cansleep() */
1548 WARN_ON(desc->chip->can_sleep);
1549 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1550 value = !value;
1551 _gpiod_set_raw_value(desc, value);
1552}
1553EXPORT_SYMBOL_GPL(gpiod_set_value);
1554
1555/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001556 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001557 * @array_size: number of elements in the descriptor / value arrays
1558 * @desc_array: array of GPIO descriptors whose values will be assigned
1559 * @value_array: array of values to assign
1560 *
1561 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1562 * without regard for their ACTIVE_LOW status.
1563 *
1564 * This function should be called from contexts where we cannot sleep, and will
1565 * complain if the GPIO chip functions potentially sleep.
1566 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001567void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001568 struct gpio_desc **desc_array, int *value_array)
1569{
1570 if (!desc_array)
1571 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001572 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1573 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001574}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001575EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001576
1577/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001578 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001579 * @array_size: number of elements in the descriptor / value arrays
1580 * @desc_array: array of GPIO descriptors whose values will be assigned
1581 * @value_array: array of values to assign
1582 *
1583 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1584 * into account.
1585 *
1586 * This function should be called from contexts where we cannot sleep, and will
1587 * complain if the GPIO chip functions potentially sleep.
1588 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001589void gpiod_set_array_value(unsigned int array_size,
1590 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001591{
1592 if (!desc_array)
1593 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001594 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1595 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001596}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001597EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001598
1599/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001600 * gpiod_cansleep() - report whether gpio value access may sleep
1601 * @desc: gpio to check
1602 *
1603 */
1604int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001605{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001606 if (!desc)
1607 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001608 return desc->chip->can_sleep;
1609}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001610EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001611
David Brownell0f6d5042008-10-15 22:03:14 -07001612/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001613 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1614 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001615 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001616 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1617 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001618 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001619int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001620{
1621 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001622 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001623
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001624 if (!desc)
1625 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001626 chip = desc->chip;
1627 offset = gpio_chip_hwgpio(desc);
1628 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1629}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001630EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001631
Linus Walleijd468bf92013-09-24 11:54:38 +02001632/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001633 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001634 * @chip: the chip the GPIO to lock belongs to
1635 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001636 *
1637 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001638 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001639 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001640int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001641{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001642 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001643 return -EINVAL;
1644
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001645 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1646 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001647 "%s: tried to flag a GPIO set as output for IRQ\n",
1648 __func__);
1649 return -EIO;
1650 }
1651
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001652 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001653 return 0;
1654}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001655EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001656
Linus Walleijd468bf92013-09-24 11:54:38 +02001657/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001658 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001659 * @chip: the chip the GPIO to lock belongs to
1660 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001661 *
1662 * This is used directly by GPIO drivers that want to indicate
1663 * that a certain GPIO is no longer used exclusively for IRQ.
1664 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001665void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001666{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001667 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001668 return;
1669
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001670 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001671}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001672EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001673
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001674/**
1675 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1676 * @desc: gpio whose value will be returned
1677 *
1678 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001679 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001680 *
1681 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001682 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001683int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001684{
David Brownelld2876d02008-02-04 22:28:20 -08001685 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001686 if (!desc)
1687 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001688 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001689}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001690EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001691
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001692/**
1693 * gpiod_get_value_cansleep() - return a gpio's value
1694 * @desc: gpio whose value will be returned
1695 *
1696 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001697 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001698 *
1699 * This function is to be called from contexts that can sleep.
1700 */
1701int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001702{
David Brownelld2876d02008-02-04 22:28:20 -08001703 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001704
1705 might_sleep_if(extra_checks);
1706 if (!desc)
1707 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001708
1709 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001710 if (value < 0)
1711 return value;
1712
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001713 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1714 value = !value;
1715
David Brownelld2876d02008-02-04 22:28:20 -08001716 return value;
1717}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001718EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001719
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001720/**
1721 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1722 * @desc: gpio whose value will be assigned
1723 * @value: value to assign
1724 *
1725 * Set the raw value of the GPIO, i.e. the value of its physical line without
1726 * regard for its ACTIVE_LOW status.
1727 *
1728 * This function is to be called from contexts that can sleep.
1729 */
1730void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001731{
David Brownelld2876d02008-02-04 22:28:20 -08001732 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001733 if (!desc)
1734 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001735 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001736}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001737EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001738
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001739/**
1740 * gpiod_set_value_cansleep() - assign a gpio's value
1741 * @desc: gpio whose value will be assigned
1742 * @value: value to assign
1743 *
1744 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1745 * account
1746 *
1747 * This function is to be called from contexts that can sleep.
1748 */
1749void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001750{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001751 might_sleep_if(extra_checks);
1752 if (!desc)
1753 return;
1754
1755 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1756 value = !value;
1757 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001758}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001759EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001760
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001761/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001762 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001763 * @array_size: number of elements in the descriptor / value arrays
1764 * @desc_array: array of GPIO descriptors whose values will be assigned
1765 * @value_array: array of values to assign
1766 *
1767 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1768 * without regard for their ACTIVE_LOW status.
1769 *
1770 * This function is to be called from contexts that can sleep.
1771 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001772void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1773 struct gpio_desc **desc_array,
1774 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001775{
1776 might_sleep_if(extra_checks);
1777 if (!desc_array)
1778 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001779 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1780 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001781}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001782EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001783
1784/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001785 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001786 * @array_size: number of elements in the descriptor / value arrays
1787 * @desc_array: array of GPIO descriptors whose values will be assigned
1788 * @value_array: array of values to assign
1789 *
1790 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1791 * into account.
1792 *
1793 * This function is to be called from contexts that can sleep.
1794 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001795void gpiod_set_array_value_cansleep(unsigned int array_size,
1796 struct gpio_desc **desc_array,
1797 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001798{
1799 might_sleep_if(extra_checks);
1800 if (!desc_array)
1801 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001802 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1803 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001804}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001805EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001806
1807/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001808 * gpiod_add_lookup_table() - register GPIO device consumers
1809 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001810 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001811void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001812{
1813 mutex_lock(&gpio_lookup_lock);
1814
Alexandre Courbotad824782013-12-03 12:20:11 +09001815 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001816
1817 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001818}
1819
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05301820/**
1821 * gpiod_remove_lookup_table() - unregister GPIO device consumers
1822 * @table: table of consumers to unregister
1823 */
1824void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
1825{
1826 mutex_lock(&gpio_lookup_lock);
1827
1828 list_del(&table->list);
1829
1830 mutex_unlock(&gpio_lookup_lock);
1831}
1832
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001833static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001834 unsigned int idx,
1835 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001836{
1837 char prop_name[32]; /* 32 is max size of property name */
1838 enum of_gpio_flags of_flags;
1839 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001840 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001841
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001842 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02001843 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01001844 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001845 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02001846 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01001847 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001848 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001849
Thierry Redingdd34c372014-04-23 17:28:09 +02001850 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1851 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001852 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001853 break;
1854 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001855
1856 if (IS_ERR(desc))
1857 return desc;
1858
1859 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001860 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001861
Laurent Pinchart90b665f2015-10-13 00:20:21 +03001862 if (of_flags & OF_GPIO_SINGLE_ENDED) {
1863 if (of_flags & OF_GPIO_ACTIVE_LOW)
1864 *flags |= GPIO_OPEN_DRAIN;
1865 else
1866 *flags |= GPIO_OPEN_SOURCE;
1867 }
1868
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001869 return desc;
1870}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001871
Mika Westerberg81f59e92013-10-10 11:01:09 +03001872static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001873 unsigned int idx,
1874 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001875{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001876 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03001877 struct acpi_gpio_info info;
1878 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001879 char propname[32];
1880 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03001881
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001882 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001883 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001884 if (con_id && strcmp(con_id, "gpios")) {
1885 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001886 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001887 } else {
1888 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001889 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001890 }
Mika Westerberge01f4402013-10-10 11:01:10 +03001891
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001892 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1893 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1894 break;
1895 }
1896
1897 /* Then from plain _CRS GPIOs */
1898 if (IS_ERR(desc)) {
Dmitry Torokhov9c3c9bc2015-11-11 11:45:30 -08001899 if (!acpi_can_fallback_to_crs(adev, con_id))
1900 return ERR_PTR(-ENOENT);
1901
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001902 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1903 if (IS_ERR(desc))
1904 return desc;
1905 }
1906
1907 if (info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001908 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001909
1910 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001911}
1912
Alexandre Courbotad824782013-12-03 12:20:11 +09001913static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1914{
1915 const char *dev_id = dev ? dev_name(dev) : NULL;
1916 struct gpiod_lookup_table *table;
1917
1918 mutex_lock(&gpio_lookup_lock);
1919
1920 list_for_each_entry(table, &gpio_lookup_list, list) {
1921 if (table->dev_id && dev_id) {
1922 /*
1923 * Valid strings on both ends, must be identical to have
1924 * a match
1925 */
1926 if (!strcmp(table->dev_id, dev_id))
1927 goto found;
1928 } else {
1929 /*
1930 * One of the pointers is NULL, so both must be to have
1931 * a match
1932 */
1933 if (dev_id == table->dev_id)
1934 goto found;
1935 }
1936 }
1937 table = NULL;
1938
1939found:
1940 mutex_unlock(&gpio_lookup_lock);
1941 return table;
1942}
1943
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001944static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001945 unsigned int idx,
1946 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001947{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001948 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001949 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001950 struct gpiod_lookup *p;
1951
Alexandre Courbotad824782013-12-03 12:20:11 +09001952 table = gpiod_find_lookup_table(dev);
1953 if (!table)
1954 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001955
Alexandre Courbotad824782013-12-03 12:20:11 +09001956 for (p = &table->table[0]; p->chip_label; p++) {
1957 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001958
Alexandre Courbotad824782013-12-03 12:20:11 +09001959 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001960 if (p->idx != idx)
1961 continue;
1962
Alexandre Courbotad824782013-12-03 12:20:11 +09001963 /* If the lookup entry has a con_id, require exact match */
1964 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1965 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001966
Alexandre Courbotad824782013-12-03 12:20:11 +09001967 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001968
Alexandre Courbotad824782013-12-03 12:20:11 +09001969 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001970 dev_err(dev, "cannot find GPIO chip %s\n",
1971 p->chip_label);
1972 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001973 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001974
Alexandre Courbotad824782013-12-03 12:20:11 +09001975 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001976 dev_err(dev,
1977 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1978 idx, chip->ngpio, chip->label);
1979 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001980 }
1981
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001982 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001983 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001984
1985 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001986 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001987
1988 return desc;
1989}
1990
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001991static int dt_gpio_count(struct device *dev, const char *con_id)
1992{
1993 int ret;
1994 char propname[32];
1995 unsigned int i;
1996
1997 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1998 if (con_id)
1999 snprintf(propname, sizeof(propname), "%s-%s",
2000 con_id, gpio_suffixes[i]);
2001 else
2002 snprintf(propname, sizeof(propname), "%s",
2003 gpio_suffixes[i]);
2004
2005 ret = of_gpio_named_count(dev->of_node, propname);
2006 if (ret >= 0)
2007 break;
2008 }
2009 return ret;
2010}
2011
2012static int platform_gpio_count(struct device *dev, const char *con_id)
2013{
2014 struct gpiod_lookup_table *table;
2015 struct gpiod_lookup *p;
2016 unsigned int count = 0;
2017
2018 table = gpiod_find_lookup_table(dev);
2019 if (!table)
2020 return -ENOENT;
2021
2022 for (p = &table->table[0]; p->chip_label; p++) {
2023 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2024 (!con_id && !p->con_id))
2025 count++;
2026 }
2027 if (!count)
2028 return -ENOENT;
2029
2030 return count;
2031}
2032
2033/**
2034 * gpiod_count - return the number of GPIOs associated with a device / function
2035 * or -ENOENT if no GPIO has been assigned to the requested function
2036 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2037 * @con_id: function within the GPIO consumer
2038 */
2039int gpiod_count(struct device *dev, const char *con_id)
2040{
2041 int count = -ENOENT;
2042
2043 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2044 count = dt_gpio_count(dev, con_id);
2045 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2046 count = acpi_gpio_count(dev, con_id);
2047
2048 if (count < 0)
2049 count = platform_gpio_count(dev, con_id);
2050
2051 return count;
2052}
2053EXPORT_SYMBOL_GPL(gpiod_count);
2054
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002055/**
Thierry Reding08791622014-04-25 16:54:22 +02002056 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002057 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002058 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002059 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002060 *
2061 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002062 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07002063 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002064 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002065struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002066 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002067{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002068 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002069}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002070EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002071
2072/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002073 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2074 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2075 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002076 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002077 *
2078 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2079 * the requested function it will return NULL. This is convenient for drivers
2080 * that need to handle optional GPIOs.
2081 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002082struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002083 const char *con_id,
2084 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002085{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002086 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002087}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002088EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002089
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002090/**
2091 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2092 * @desc: gpio to be setup
2093 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2094 * of_get_gpio_hog()
2095 *
2096 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2097 */
2098static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2099{
2100 if (lflags & GPIO_ACTIVE_LOW)
2101 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2102 if (lflags & GPIO_OPEN_DRAIN)
2103 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2104 if (lflags & GPIO_OPEN_SOURCE)
2105 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2106}
Benoit Parrotf625d462015-02-02 11:44:44 -06002107
2108/**
2109 * gpiod_configure_flags - helper function to configure a given GPIO
2110 * @desc: gpio whose value will be assigned
2111 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06002112 * @dflags: gpiod_flags - optional GPIO initialization flags
2113 *
2114 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2115 * requested function and/or index, or another IS_ERR() code if an error
2116 * occurred while trying to acquire the GPIO.
2117 */
2118static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002119 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06002120{
2121 int status;
2122
Benoit Parrotf625d462015-02-02 11:44:44 -06002123 /* No particular flag request, return here... */
2124 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2125 pr_debug("no flags found for %s\n", con_id);
2126 return 0;
2127 }
2128
2129 /* Process flags */
2130 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2131 status = gpiod_direction_output(desc,
2132 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2133 else
2134 status = gpiod_direction_input(desc);
2135
2136 return status;
2137}
2138
Thierry Reding29a1f2332014-04-25 17:10:06 +02002139/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002140 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002141 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002142 * @con_id: function within the GPIO consumer
2143 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002144 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002145 *
2146 * This variant of gpiod_get() allows to access GPIOs other than the first
2147 * defined one for functions that define several GPIOs.
2148 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002149 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2150 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07002151 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002152 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002153struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002154 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002155 unsigned int idx,
2156 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002157{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002158 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002159 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002160 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002161
2162 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2163
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002164 if (dev) {
2165 /* Using device tree? */
2166 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2167 dev_dbg(dev, "using device tree for GPIO lookup\n");
2168 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2169 } else if (ACPI_COMPANION(dev)) {
2170 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2171 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2172 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002173 }
2174
2175 /*
2176 * Either we are not using DT or ACPI, or their lookup did not return
2177 * a result. In that case, use platform lookup as a fallback.
2178 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002179 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002180 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002181 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002182 }
2183
2184 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002185 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002186 return desc;
2187 }
2188
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002189 gpiod_parse_flags(desc, lookupflags);
2190
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002191 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002192 if (status < 0)
2193 return ERR_PTR(status);
2194
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002195 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002196 if (status < 0) {
2197 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2198 gpiod_put(desc);
2199 return ERR_PTR(status);
2200 }
2201
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002202 return desc;
2203}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002204EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002205
2206/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002207 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2208 * @fwnode: handle of the firmware node
2209 * @propname: name of the firmware property representing the GPIO
2210 *
2211 * This function can be used for drivers that get their configuration
2212 * from firmware.
2213 *
2214 * Function properly finds the corresponding GPIO using whatever is the
2215 * underlying firmware interface and then makes sure that the GPIO
2216 * descriptor is requested before it is returned to the caller.
2217 *
2218 * In case of error an ERR_PTR() is returned.
2219 */
2220struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2221 const char *propname)
2222{
2223 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2224 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002225 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02002226 int ret;
2227
2228 if (!fwnode)
2229 return ERR_PTR(-EINVAL);
2230
2231 if (is_of_node(fwnode)) {
2232 enum of_gpio_flags flags;
2233
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002234 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002235 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002236 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02002237 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002238 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2239 }
Mika Westerberg40b73182014-10-21 13:33:59 +02002240 } else if (is_acpi_node(fwnode)) {
2241 struct acpi_gpio_info info;
2242
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02002243 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02002244 if (!IS_ERR(desc))
2245 active_low = info.active_low;
2246 }
2247
2248 if (IS_ERR(desc))
2249 return desc;
2250
Mika Westerberg40b73182014-10-21 13:33:59 +02002251 if (active_low)
2252 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2253
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002254 if (single_ended) {
2255 if (active_low)
2256 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2257 else
2258 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2259 }
2260
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002261 ret = gpiod_request(desc, NULL);
2262 if (ret)
2263 return ERR_PTR(ret);
2264
Mika Westerberg40b73182014-10-21 13:33:59 +02002265 return desc;
2266}
2267EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2268
2269/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002270 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2271 * function
2272 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2273 * @con_id: function within the GPIO consumer
2274 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002275 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002276 *
2277 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2278 * specified index was assigned to the requested function it will return NULL.
2279 * This is convenient for drivers that need to handle optional GPIOs.
2280 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002281struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002282 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002283 unsigned int index,
2284 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002285{
2286 struct gpio_desc *desc;
2287
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002288 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002289 if (IS_ERR(desc)) {
2290 if (PTR_ERR(desc) == -ENOENT)
2291 return NULL;
2292 }
2293
2294 return desc;
2295}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002296EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002297
2298/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002299 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2300 * @desc: gpio whose value will be assigned
2301 * @name: gpio line name
2302 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2303 * of_get_gpio_hog()
2304 * @dflags: gpiod_flags - optional GPIO initialization flags
2305 */
2306int gpiod_hog(struct gpio_desc *desc, const char *name,
2307 unsigned long lflags, enum gpiod_flags dflags)
2308{
2309 struct gpio_chip *chip;
2310 struct gpio_desc *local_desc;
2311 int hwnum;
2312 int status;
2313
2314 chip = gpiod_to_chip(desc);
2315 hwnum = gpio_chip_hwgpio(desc);
2316
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002317 gpiod_parse_flags(desc, lflags);
2318
Benoit Parrotf625d462015-02-02 11:44:44 -06002319 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2320 if (IS_ERR(local_desc)) {
Linus Walleija7138902015-06-05 11:36:10 +02002321 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2322 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002323 return PTR_ERR(local_desc);
2324 }
2325
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002326 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002327 if (status < 0) {
Linus Walleija7138902015-06-05 11:36:10 +02002328 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2329 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002330 gpiochip_free_own_desc(desc);
2331 return status;
2332 }
2333
2334 /* Mark GPIO as hogged so it can be identified and removed later */
2335 set_bit(FLAG_IS_HOGGED, &desc->flags);
2336
2337 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2338 desc_to_gpio(desc), name,
2339 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2340 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2341 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2342
2343 return 0;
2344}
2345
2346/**
2347 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2348 * @chip: gpio chip to act on
2349 *
2350 * This is only used by of_gpiochip_remove to free hogged gpios
2351 */
2352static void gpiochip_free_hogs(struct gpio_chip *chip)
2353{
2354 int id;
2355
2356 for (id = 0; id < chip->ngpio; id++) {
2357 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2358 gpiochip_free_own_desc(&chip->desc[id]);
2359 }
2360}
2361
2362/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002363 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2364 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2365 * @con_id: function within the GPIO consumer
2366 * @flags: optional GPIO initialization flags
2367 *
2368 * This function acquires all the GPIOs defined under a given function.
2369 *
2370 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2371 * no GPIO has been assigned to the requested function, or another IS_ERR()
2372 * code if an error occurred while trying to acquire the GPIOs.
2373 */
2374struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2375 const char *con_id,
2376 enum gpiod_flags flags)
2377{
2378 struct gpio_desc *desc;
2379 struct gpio_descs *descs;
2380 int count;
2381
2382 count = gpiod_count(dev, con_id);
2383 if (count < 0)
2384 return ERR_PTR(count);
2385
2386 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2387 GFP_KERNEL);
2388 if (!descs)
2389 return ERR_PTR(-ENOMEM);
2390
2391 for (descs->ndescs = 0; descs->ndescs < count; ) {
2392 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2393 if (IS_ERR(desc)) {
2394 gpiod_put_array(descs);
2395 return ERR_CAST(desc);
2396 }
2397 descs->desc[descs->ndescs] = desc;
2398 descs->ndescs++;
2399 }
2400 return descs;
2401}
2402EXPORT_SYMBOL_GPL(gpiod_get_array);
2403
2404/**
2405 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2406 * function
2407 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2408 * @con_id: function within the GPIO consumer
2409 * @flags: optional GPIO initialization flags
2410 *
2411 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2412 * assigned to the requested function it will return NULL.
2413 */
2414struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2415 const char *con_id,
2416 enum gpiod_flags flags)
2417{
2418 struct gpio_descs *descs;
2419
2420 descs = gpiod_get_array(dev, con_id, flags);
2421 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2422 return NULL;
2423
2424 return descs;
2425}
2426EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2427
2428/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002429 * gpiod_put - dispose of a GPIO descriptor
2430 * @desc: GPIO descriptor to dispose of
2431 *
2432 * No descriptor can be used after gpiod_put() has been called on it.
2433 */
2434void gpiod_put(struct gpio_desc *desc)
2435{
2436 gpiod_free(desc);
2437}
2438EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002439
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002440/**
2441 * gpiod_put_array - dispose of multiple GPIO descriptors
2442 * @descs: struct gpio_descs containing an array of descriptors
2443 */
2444void gpiod_put_array(struct gpio_descs *descs)
2445{
2446 unsigned int i;
2447
2448 for (i = 0; i < descs->ndescs; i++)
2449 gpiod_put(descs->desc[i]);
2450
2451 kfree(descs);
2452}
2453EXPORT_SYMBOL_GPL(gpiod_put_array);
2454
David Brownelld2876d02008-02-04 22:28:20 -08002455#ifdef CONFIG_DEBUG_FS
2456
2457static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2458{
2459 unsigned i;
2460 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002461 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002462 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002463 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002464
2465 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02002466 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2467 if (gdesc->name) {
2468 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2469 gpio, gdesc->name);
2470 }
David Brownelld2876d02008-02-04 22:28:20 -08002471 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02002472 }
David Brownelld2876d02008-02-04 22:28:20 -08002473
Alexandre Courbot372e7222013-02-03 01:29:29 +09002474 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002475 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002476 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02002477 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2478 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08002479 is_out ? "out" : "in ",
2480 chip->get
2481 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002482 : "? ",
2483 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002484 seq_printf(s, "\n");
2485 }
2486}
2487
Thierry Redingf9c4a312012-04-12 13:26:01 +02002488static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002489{
Grant Likely362432a2013-02-09 09:41:49 +00002490 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002491 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002492 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002493
2494 s->private = "";
2495
Grant Likely362432a2013-02-09 09:41:49 +00002496 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002497 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002498 if (index-- == 0) {
2499 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002500 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002501 }
2502 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002503
2504 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002505}
2506
2507static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2508{
Grant Likely362432a2013-02-09 09:41:49 +00002509 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002510 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002511 void *ret = NULL;
2512
Grant Likely362432a2013-02-09 09:41:49 +00002513 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002514 if (list_is_last(&chip->list, &gpio_chips))
2515 ret = NULL;
2516 else
2517 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002518 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002519
2520 s->private = "\n";
2521 ++*pos;
2522
2523 return ret;
2524}
2525
2526static void gpiolib_seq_stop(struct seq_file *s, void *v)
2527{
2528}
2529
2530static int gpiolib_seq_show(struct seq_file *s, void *v)
2531{
2532 struct gpio_chip *chip = v;
2533 struct device *dev;
2534
2535 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2536 chip->base, chip->base + chip->ngpio - 1);
Linus Walleij58383c72015-11-04 09:56:26 +01002537 dev = chip->parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002538 if (dev)
2539 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2540 dev_name(dev));
2541 if (chip->label)
2542 seq_printf(s, ", %s", chip->label);
2543 if (chip->can_sleep)
2544 seq_printf(s, ", can sleep");
2545 seq_printf(s, ":\n");
2546
2547 if (chip->dbg_show)
2548 chip->dbg_show(s, chip);
2549 else
2550 gpiolib_dbg_show(s, chip);
2551
David Brownelld2876d02008-02-04 22:28:20 -08002552 return 0;
2553}
2554
Thierry Redingf9c4a312012-04-12 13:26:01 +02002555static const struct seq_operations gpiolib_seq_ops = {
2556 .start = gpiolib_seq_start,
2557 .next = gpiolib_seq_next,
2558 .stop = gpiolib_seq_stop,
2559 .show = gpiolib_seq_show,
2560};
2561
David Brownelld2876d02008-02-04 22:28:20 -08002562static int gpiolib_open(struct inode *inode, struct file *file)
2563{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002564 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002565}
2566
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002567static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002568 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002569 .open = gpiolib_open,
2570 .read = seq_read,
2571 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002572 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002573};
2574
2575static int __init gpiolib_debugfs_init(void)
2576{
2577 /* /sys/kernel/debug/gpio */
2578 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2579 NULL, NULL, &gpiolib_operations);
2580 return 0;
2581}
2582subsys_initcall(gpiolib_debugfs_init);
2583
2584#endif /* DEBUG_FS */