blob: 6bc612b8a49fcf859261173e00d0e7389d7d2b05 [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>
David Brownelld2876d02008-02-04 22:28:20 -080018
Mika Westerberg664e3e52014-01-08 12:40:54 +020019#include "gpiolib.h"
20
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060021#define CREATE_TRACE_POINTS
22#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080023
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070024/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080025 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070026 * The GPIO programming interface allows for inlining speed-critical
27 * get/set operations for common cases, so that access to SOC-integrated
28 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080029 */
30
31
32/* When debugging, extend minimal trust to callers and platform code.
33 * Also emit diagnostic messages that may help initial bringup, when
34 * board setup or driver bugs are most common.
35 *
36 * Otherwise, minimize overhead in what may be bitbanging codepaths.
37 */
38#ifdef DEBUG
39#define extra_checks 1
40#else
41#define extra_checks 0
42#endif
43
44/* gpio_lock prevents conflicts during gpio_desc[] table updates.
45 * While any GPIO is requested, its gpio_chip is not removable;
46 * each GPIO's "requested" flag serves as a lock and refcount.
47 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090048DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080049
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +090050#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
51
Alexandre Courbotbae48da2013-10-17 10:21:38 -070052static DEFINE_MUTEX(gpio_lookup_lock);
53static LIST_HEAD(gpio_lookup_list);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090054LIST_HEAD(gpio_chips);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +020055
Johan Hovold6d867502015-05-04 17:23:25 +020056
57static void gpiochip_free_hogs(struct gpio_chip *chip);
58static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
59
60
David Brownelld2876d02008-02-04 22:28:20 -080061static inline void desc_set_label(struct gpio_desc *d, const char *label)
62{
David Brownelld2876d02008-02-04 22:28:20 -080063 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080064}
65
Alexandre Courbot372e7222013-02-03 01:29:29 +090066/**
67 * Convert a GPIO number to its descriptor
68 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070069struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090070{
Alexandre Courbot14e85c02014-11-19 16:51:27 +090071 struct gpio_chip *chip;
72 unsigned long flags;
73
74 spin_lock_irqsave(&gpio_lock, flags);
75
76 list_for_each_entry(chip, &gpio_chips, list) {
77 if (chip->base <= gpio && chip->base + chip->ngpio > gpio) {
78 spin_unlock_irqrestore(&gpio_lock, flags);
79 return &chip->desc[gpio - chip->base];
80 }
81 }
82
83 spin_unlock_irqrestore(&gpio_lock, flags);
84
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +090085 if (!gpio_is_valid(gpio))
86 WARN(1, "invalid GPIO %d\n", gpio);
87
Alexandre Courbot14e85c02014-11-19 16:51:27 +090088 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +090089}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070090EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090091
92/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090093 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +020094 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090095struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
96 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +020097{
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090098 if (hwnum >= chip->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +090099 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200100
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900101 return &chip->desc[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200102}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900103
104/**
105 * Convert a GPIO descriptor to the integer namespace.
106 * This should disappear in the future but is needed since we still
107 * use GPIO numbers for error messages and sysfs nodes
108 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700109int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900110{
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900111 return desc->chip->base + (desc - &desc->chip->desc[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900112}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700113EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900114
115
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700116/**
117 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
118 * @desc: descriptor to return the chip of
119 */
120struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900121{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900122 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900123}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700124EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800125
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700126/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
127static int gpiochip_find_base(int ngpio)
128{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900129 struct gpio_chip *chip;
130 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700131
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900132 list_for_each_entry_reverse(chip, &gpio_chips, list) {
133 /* found a free space? */
134 if (chip->base + chip->ngpio <= base)
135 break;
136 else
137 /* nope, check the space right before the chip */
138 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700139 }
140
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900141 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700142 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900143 return base;
144 } else {
145 pr_err("%s: cannot find free range\n", __func__);
146 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700147 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700148}
149
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700150/**
151 * gpiod_get_direction - return the current direction of a GPIO
152 * @desc: GPIO to get the direction of
153 *
154 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
155 *
156 * This function may sleep if gpiod_cansleep() is true.
157 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900158int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300159{
160 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900161 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300162 int status = -EINVAL;
163
Alexandre Courbot372e7222013-02-03 01:29:29 +0900164 chip = gpiod_to_chip(desc);
165 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300166
167 if (!chip->get_direction)
168 return status;
169
Alexandre Courbot372e7222013-02-03 01:29:29 +0900170 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300171 if (status > 0) {
172 /* GPIOF_DIR_IN, or other positive */
173 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900174 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300175 }
176 if (status == 0) {
177 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900178 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300179 }
180 return status;
181}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700182EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300183
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900184/*
185 * Add a new chip to the global chips list, keeping the list of chips sorted
186 * by base order.
187 *
188 * Return -EBUSY if the new chip overlaps with some other chip's integer
189 * space.
190 */
191static int gpiochip_add_to_list(struct gpio_chip *chip)
192{
193 struct list_head *pos = &gpio_chips;
194 struct gpio_chip *_chip;
195 int err = 0;
196
197 /* find where to insert our chip */
198 list_for_each(pos, &gpio_chips) {
199 _chip = list_entry(pos, struct gpio_chip, list);
200 /* shall we insert before _chip? */
201 if (_chip->base >= chip->base + chip->ngpio)
202 break;
203 }
204
205 /* are we stepping on the chip right before? */
206 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
207 _chip = list_entry(pos->prev, struct gpio_chip, list);
208 if (_chip->base + _chip->ngpio > chip->base) {
209 dev_err(chip->dev,
210 "GPIO integer space overlap, cannot add chip\n");
211 err = -EBUSY;
212 }
213 }
214
215 if (!err)
216 list_add_tail(&chip->list, pos);
217
218 return err;
219}
220
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700221/**
David Brownelld2876d02008-02-04 22:28:20 -0800222 * gpiochip_add() - register a gpio_chip
223 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900224 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -0800225 *
226 * Returns a negative errno if the chip can't be registered, such as
227 * because the chip->base is invalid or already associated with a
228 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700229 *
David Brownelld8f388d2008-07-25 01:46:07 -0700230 * When gpiochip_add() is called very early during boot, so that GPIOs
231 * can be freely used, the chip->dev device must be registered before
232 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
233 * for GPIOs will fail rudely.
234 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700235 * If chip->base is negative, this requests dynamic assignment of
236 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800237 */
238int gpiochip_add(struct gpio_chip *chip)
239{
240 unsigned long flags;
241 int status = 0;
242 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700243 int base = chip->base;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900244 struct gpio_desc *descs;
David Brownelld2876d02008-02-04 22:28:20 -0800245
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900246 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
247 if (!descs)
248 return -ENOMEM;
David Brownelld2876d02008-02-04 22:28:20 -0800249
250 spin_lock_irqsave(&gpio_lock, flags);
251
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700252 if (base < 0) {
253 base = gpiochip_find_base(chip->ngpio);
254 if (base < 0) {
255 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100256 spin_unlock_irqrestore(&gpio_lock, flags);
257 goto err_free_descs;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700258 }
259 chip->base = base;
260 }
261
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900262 status = gpiochip_add_to_list(chip);
Johan Hovold05aa5202015-01-12 17:12:26 +0100263 if (status) {
264 spin_unlock_irqrestore(&gpio_lock, flags);
265 goto err_free_descs;
266 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900267
Johan Hovold05aa5202015-01-12 17:12:26 +0100268 for (id = 0; id < chip->ngpio; id++) {
269 struct gpio_desc *desc = &descs[id];
David Brownelld8f388d2008-07-25 01:46:07 -0700270
Johan Hovold05aa5202015-01-12 17:12:26 +0100271 desc->chip = chip;
272
273 /* REVISIT: most hardware initializes GPIOs as inputs (often
274 * with pullups enabled) so power usage is minimized. Linux
275 * code should set the gpio direction first thing; but until
276 * it does, and in case chip->get_direction is not set, we may
277 * expose the wrong direction in sysfs.
278 */
279 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800280 }
281
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900282 chip->desc = descs;
283
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800284 spin_unlock_irqrestore(&gpio_lock, flags);
285
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530286#ifdef CONFIG_PINCTRL
287 INIT_LIST_HEAD(&chip->pin_ranges);
288#endif
289
Anton Vorontsov391c9702010-06-08 07:48:17 -0600290 of_gpiochip_add(chip);
Mika Westerberg664e3e52014-01-08 12:40:54 +0200291 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600292
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600293 status = gpiochip_export(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100294 if (status)
295 goto err_remove_chip;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600296
Andy Shevchenko7589e592013-12-05 11:26:23 +0200297 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700298 chip->base, chip->base + chip->ngpio - 1,
299 chip->label ? : "generic");
300
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600301 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800302
Johan Hovold225fce82015-01-12 17:12:25 +0100303err_remove_chip:
304 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200305 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100306 of_gpiochip_remove(chip);
307 spin_lock_irqsave(&gpio_lock, flags);
308 list_del(&chip->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800309 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold05aa5202015-01-12 17:12:26 +0100310 chip->desc = NULL;
Johan Hovold225fce82015-01-12 17:12:25 +0100311err_free_descs:
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900312 kfree(descs);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900313
David Brownelld2876d02008-02-04 22:28:20 -0800314 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200315 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600316 chip->base, chip->base + chip->ngpio - 1,
317 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800318 return status;
319}
320EXPORT_SYMBOL_GPL(gpiochip_add);
321
322/**
323 * gpiochip_remove() - unregister a gpio_chip
324 * @chip: the chip to unregister
325 *
326 * A gpio_chip with any GPIOs still requested may not be removed.
327 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200328void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800329{
330 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800331 unsigned id;
332
Johan Hovold01cca932015-01-12 17:12:29 +0100333 gpiochip_unexport(chip);
334
Johan Hovold00acc3d2015-01-12 17:12:27 +0100335 gpiochip_irqchip_remove(chip);
336
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200337 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100338 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600339 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600340 of_gpiochip_remove(chip);
341
Johan Hovold6798aca2015-01-12 17:12:28 +0100342 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900343 for (id = 0; id < chip->ngpio; id++) {
abdoulaye berthee1db1702014-07-05 18:28:50 +0200344 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags))
345 dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
David Brownelld2876d02008-02-04 22:28:20 -0800346 }
abdoulaye berthee1db1702014-07-05 18:28:50 +0200347 for (id = 0; id < chip->ngpio; id++)
348 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900349
abdoulaye berthee1db1702014-07-05 18:28:50 +0200350 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800351 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900352
353 kfree(chip->desc);
354 chip->desc = NULL;
David Brownelld2876d02008-02-04 22:28:20 -0800355}
356EXPORT_SYMBOL_GPL(gpiochip_remove);
357
Grant Likely594fa262010-06-08 07:48:16 -0600358/**
359 * gpiochip_find() - iterator for locating a specific gpio_chip
360 * @data: data to pass to match function
361 * @callback: Callback function to check gpio_chip
362 *
363 * Similar to bus_find_device. It returns a reference to a gpio_chip as
364 * determined by a user supplied @match callback. The callback should return
365 * 0 if the device doesn't match and non-zero if it does. If the callback is
366 * non-zero, this function will return to the caller and not iterate over any
367 * more gpio_chips.
368 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600369struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700370 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600371 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600372{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900373 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600374 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600375
376 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900377 list_for_each_entry(chip, &gpio_chips, list)
378 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600379 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900380
381 /* No match? */
382 if (&chip->list == &gpio_chips)
383 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600384 spin_unlock_irqrestore(&gpio_lock, flags);
385
386 return chip;
387}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600388EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800389
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900390static int gpiochip_match_name(struct gpio_chip *chip, void *data)
391{
392 const char *name = data;
393
394 return !strcmp(chip->label, name);
395}
396
397static struct gpio_chip *find_chip_by_name(const char *name)
398{
399 return gpiochip_find((void *)name, gpiochip_match_name);
400}
401
Linus Walleij14250522014-03-25 10:40:18 +0100402#ifdef CONFIG_GPIOLIB_IRQCHIP
403
404/*
405 * The following is irqchip helper code for gpiochips.
406 */
407
408/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200409 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
410 * @gpiochip: the gpiochip to set the irqchip chain to
411 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100412 * @parent_irq: the irq number corresponding to the parent IRQ for this
413 * chained irqchip
414 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200415 * coming out of the gpiochip. If the interrupt is nested rather than
416 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100417 */
418void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
419 struct irq_chip *irqchip,
420 int parent_irq,
421 irq_flow_handler_t parent_handler)
422{
Linus Walleij83141a72014-09-26 13:50:12 +0200423 unsigned int offset;
424
Linus Walleij83141a72014-09-26 13:50:12 +0200425 if (!gpiochip->irqdomain) {
426 chip_err(gpiochip, "called %s before setting up irqchip\n",
427 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200428 return;
429 }
430
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200431 if (parent_handler) {
432 if (gpiochip->can_sleep) {
433 chip_err(gpiochip,
434 "you cannot have chained interrupts on a "
435 "chip that may sleep\n");
436 return;
437 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200438 /*
439 * The parent irqchip is already using the chip_data for this
440 * irqchip, so our callbacks simply use the handler_data.
441 */
442 irq_set_handler_data(parent_irq, gpiochip);
Linus Torvaldsea584592014-10-09 14:58:15 -0400443 irq_set_chained_handler(parent_irq, parent_handler);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200444 }
Linus Walleij83141a72014-09-26 13:50:12 +0200445
446 /* Set the parent IRQ for all affected IRQs */
447 for (offset = 0; offset < gpiochip->ngpio; offset++)
448 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
449 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100450}
451EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
452
Linus Walleije45d1c82014-04-22 14:01:46 +0200453/*
454 * This lock class tells lockdep that GPIO irqs are in a different
455 * category than their parents, so it won't report false recursion.
456 */
457static struct lock_class_key gpiochip_irq_lock_class;
458
Linus Walleij14250522014-03-25 10:40:18 +0100459/**
460 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
461 * @d: the irqdomain used by this irqchip
462 * @irq: the global irq number used by this GPIO irqchip irq
463 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
464 *
465 * This function will set up the mapping for a certain IRQ line on a
466 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
467 * stored inside the gpiochip.
468 */
469static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
470 irq_hw_number_t hwirq)
471{
472 struct gpio_chip *chip = d->host_data;
473
Linus Walleij14250522014-03-25 10:40:18 +0100474 irq_set_chip_data(irq, chip);
Linus Walleije45d1c82014-04-22 14:01:46 +0200475 irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
Linus Walleij7633fb92014-04-09 13:20:38 +0200476 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200477 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300478 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200479 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100480#ifdef CONFIG_ARM
481 set_irq_flags(irq, IRQF_VALID);
482#else
483 irq_set_noprobe(irq);
484#endif
Linus Walleij1333b902014-04-23 16:45:12 +0200485 /*
486 * No set-up of the hardware will happen if IRQ_TYPE_NONE
487 * is passed as default type.
488 */
489 if (chip->irq_default_type != IRQ_TYPE_NONE)
490 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100491
492 return 0;
493}
494
Linus Walleijc3626fd2014-03-28 20:42:01 +0100495static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
496{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200497 struct gpio_chip *chip = d->host_data;
498
Linus Walleijc3626fd2014-03-28 20:42:01 +0100499#ifdef CONFIG_ARM
500 set_irq_flags(irq, 0);
501#endif
Linus Walleij1c8732b2014-04-09 13:34:39 +0200502 if (chip->can_sleep)
503 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100504 irq_set_chip_and_handler(irq, NULL, NULL);
505 irq_set_chip_data(irq, NULL);
506}
507
Linus Walleij14250522014-03-25 10:40:18 +0100508static const struct irq_domain_ops gpiochip_domain_ops = {
509 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100510 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100511 /* Virtually all GPIO irqchips are twocell:ed */
512 .xlate = irq_domain_xlate_twocell,
513};
514
515static int gpiochip_irq_reqres(struct irq_data *d)
516{
517 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
518
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900519 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100520 chip_err(chip,
521 "unable to lock HW IRQ %lu for IRQ\n",
522 d->hwirq);
523 return -EINVAL;
524 }
525 return 0;
526}
527
528static void gpiochip_irq_relres(struct irq_data *d)
529{
530 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
531
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900532 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleij14250522014-03-25 10:40:18 +0100533}
534
535static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
536{
537 return irq_find_mapping(chip->irqdomain, offset);
538}
539
540/**
541 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
542 * @gpiochip: the gpiochip to remove the irqchip from
543 *
544 * This is called only from gpiochip_remove()
545 */
546static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
547{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100548 unsigned int offset;
549
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300550 acpi_gpiochip_free_interrupts(gpiochip);
551
Linus Walleijc3626fd2014-03-28 20:42:01 +0100552 /* Remove all IRQ mappings and delete the domain */
553 if (gpiochip->irqdomain) {
554 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300555 irq_dispose_mapping(
556 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100557 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100558 }
Linus Walleij14250522014-03-25 10:40:18 +0100559
560 if (gpiochip->irqchip) {
561 gpiochip->irqchip->irq_request_resources = NULL;
562 gpiochip->irqchip->irq_release_resources = NULL;
563 gpiochip->irqchip = NULL;
564 }
565}
566
567/**
568 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
569 * @gpiochip: the gpiochip to add the irqchip to
570 * @irqchip: the irqchip to add to the gpiochip
571 * @first_irq: if not dynamically assigned, the base (first) IRQ to
572 * allocate gpiochip irqs from
573 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200574 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
575 * to have the core avoid setting up any default type in the hardware.
Linus Walleij14250522014-03-25 10:40:18 +0100576 *
577 * This function closely associates a certain irqchip with a certain
578 * gpiochip, providing an irq domain to translate the local IRQs to
579 * global irqs in the gpiolib core, and making sure that the gpiochip
580 * is passed as chip data to all related functions. Driver callbacks
581 * need to use container_of() to get their local state containers back
582 * from the gpiochip passed as chip data. An irqdomain will be stored
583 * in the gpiochip that shall be used by the driver to handle IRQ number
584 * translation. The gpiochip will need to be initialized and registered
585 * before calling this function.
586 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100587 * This function will handle two cell:ed simple IRQs and assumes all
588 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100589 * need to be open coded.
590 */
591int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
592 struct irq_chip *irqchip,
593 unsigned int first_irq,
594 irq_flow_handler_t handler,
595 unsigned int type)
596{
597 struct device_node *of_node;
598 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100599 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100600
601 if (!gpiochip || !irqchip)
602 return -EINVAL;
603
604 if (!gpiochip->dev) {
605 pr_err("missing gpiochip .dev parent pointer\n");
606 return -EINVAL;
607 }
608 of_node = gpiochip->dev->of_node;
609#ifdef CONFIG_OF_GPIO
610 /*
611 * If the gpiochip has an assigned OF node this takes precendence
612 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
613 */
614 if (gpiochip->of_node)
615 of_node = gpiochip->of_node;
616#endif
617 gpiochip->irqchip = irqchip;
618 gpiochip->irq_handler = handler;
619 gpiochip->irq_default_type = type;
620 gpiochip->to_irq = gpiochip_to_irq;
621 gpiochip->irqdomain = irq_domain_add_simple(of_node,
622 gpiochip->ngpio, first_irq,
623 &gpiochip_domain_ops, gpiochip);
624 if (!gpiochip->irqdomain) {
625 gpiochip->irqchip = NULL;
626 return -EINVAL;
627 }
628 irqchip->irq_request_resources = gpiochip_irq_reqres;
629 irqchip->irq_release_resources = gpiochip_irq_relres;
630
631 /*
632 * Prepare the mapping since the irqchip shall be orthogonal to
633 * any gpiochip calls. If the first_irq was zero, this is
634 * necessary to allocate descriptors for all IRQs.
635 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100636 for (offset = 0; offset < gpiochip->ngpio; offset++) {
637 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
638 if (offset == 0)
639 /*
640 * Store the base into the gpiochip to be used when
641 * unmapping the irqs.
642 */
643 gpiochip->irq_base = irq_base;
644 }
Linus Walleij14250522014-03-25 10:40:18 +0100645
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300646 acpi_gpiochip_request_interrupts(gpiochip);
647
Linus Walleij14250522014-03-25 10:40:18 +0100648 return 0;
649}
650EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
651
652#else /* CONFIG_GPIOLIB_IRQCHIP */
653
654static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
655
656#endif /* CONFIG_GPIOLIB_IRQCHIP */
657
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530658#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100659
Linus Walleij3f0f8672012-11-20 12:40:15 +0100660/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200661 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
662 * @chip: the gpiochip to add the range for
663 * @pinctrl: the dev_name() of the pin controller to map to
664 * @gpio_offset: the start offset in the current gpio_chip number space
665 * @pin_group: name of the pin group inside the pin controller
666 */
667int gpiochip_add_pingroup_range(struct gpio_chip *chip,
668 struct pinctrl_dev *pctldev,
669 unsigned int gpio_offset, const char *pin_group)
670{
671 struct gpio_pin_range *pin_range;
672 int ret;
673
674 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
675 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200676 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200677 return -ENOMEM;
678 }
679
680 /* Use local offset as range ID */
681 pin_range->range.id = gpio_offset;
682 pin_range->range.gc = chip;
683 pin_range->range.name = chip->label;
684 pin_range->range.base = chip->base + gpio_offset;
685 pin_range->pctldev = pctldev;
686
687 ret = pinctrl_get_group_pins(pctldev, pin_group,
688 &pin_range->range.pins,
689 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100690 if (ret < 0) {
691 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200692 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100693 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200694
695 pinctrl_add_gpio_range(pctldev, &pin_range->range);
696
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200697 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
698 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200699 pinctrl_dev_get_devname(pctldev), pin_group);
700
701 list_add_tail(&pin_range->node, &chip->pin_ranges);
702
703 return 0;
704}
705EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
706
707/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100708 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
709 * @chip: the gpiochip to add the range for
710 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100711 * @gpio_offset: the start offset in the current gpio_chip number space
712 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100713 * @npins: the number of pins from the offset of each pin space (GPIO and
714 * pin controller) to accumulate in this range
715 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100716int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100717 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100718 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530719{
720 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800721 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530722
Linus Walleij3f0f8672012-11-20 12:40:15 +0100723 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530724 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200725 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100726 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530727 }
728
Linus Walleij3f0f8672012-11-20 12:40:15 +0100729 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100730 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100731 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530732 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100733 pin_range->range.base = chip->base + gpio_offset;
734 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530735 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100736 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530737 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100738 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800739 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200740 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100741 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800742 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100743 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200744 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
745 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100746 pinctl_name,
747 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530748
749 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100750
751 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530752}
Linus Walleij165adc92012-11-06 14:49:39 +0100753EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530754
Linus Walleij3f0f8672012-11-20 12:40:15 +0100755/**
756 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
757 * @chip: the chip to remove all the mappings for
758 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530759void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
760{
761 struct gpio_pin_range *pin_range, *tmp;
762
763 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
764 list_del(&pin_range->node);
765 pinctrl_remove_gpio_range(pin_range->pctldev,
766 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100767 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530768 }
769}
Linus Walleij165adc92012-11-06 14:49:39 +0100770EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
771
772#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530773
David Brownelld2876d02008-02-04 22:28:20 -0800774/* These "optional" allocation calls help prevent drivers from stomping
775 * on each other, and help provide better diagnostics in debugfs.
776 * They're called even less than the "set direction" calls.
777 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200778static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800779{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200780 struct gpio_chip *chip = desc->chip;
781 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800782 unsigned long flags;
783
784 spin_lock_irqsave(&gpio_lock, flags);
785
David Brownelld2876d02008-02-04 22:28:20 -0800786 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700787 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800788 */
789
790 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
791 desc_set_label(desc, label ? : "?");
792 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700793 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800794 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800795 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700796 }
797
798 if (chip->request) {
799 /* chip->request may sleep */
800 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900801 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700802 spin_lock_irqsave(&gpio_lock, flags);
803
804 if (status < 0) {
805 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700806 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300807 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700808 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700809 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300810 if (chip->get_direction) {
811 /* chip->get_direction may sleep */
812 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900813 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300814 spin_lock_irqsave(&gpio_lock, flags);
815 }
David Brownelld2876d02008-02-04 22:28:20 -0800816done:
Mika Westerberg77c2d792014-03-10 14:54:50 +0200817 spin_unlock_irqrestore(&gpio_lock, flags);
818 return status;
819}
820
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900821int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200822{
823 int status = -EPROBE_DEFER;
824 struct gpio_chip *chip;
825
826 if (!desc) {
827 pr_warn("%s: invalid GPIO\n", __func__);
828 return -EINVAL;
829 }
830
831 chip = desc->chip;
832 if (!chip)
833 goto done;
834
835 if (try_module_get(chip->owner)) {
836 status = __gpiod_request(desc, label);
837 if (status < 0)
838 module_put(chip->owner);
839 }
840
841done:
David Brownelld2876d02008-02-04 22:28:20 -0800842 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200843 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200844
David Brownelld2876d02008-02-04 22:28:20 -0800845 return status;
846}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900847
Mika Westerberg77c2d792014-03-10 14:54:50 +0200848static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800849{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200850 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -0800851 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -0700852 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800853
Uwe Kleine-König3d599d12008-10-15 22:03:12 -0700854 might_sleep();
855
Alexandre Courbot372e7222013-02-03 01:29:29 +0900856 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700857
David Brownelld2876d02008-02-04 22:28:20 -0800858 spin_lock_irqsave(&gpio_lock, flags);
859
David Brownell35e8bb52008-10-15 22:03:16 -0700860 chip = desc->chip;
861 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
862 if (chip->free) {
863 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -0700864 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900865 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700866 spin_lock_irqsave(&gpio_lock, flags);
867 }
David Brownelld2876d02008-02-04 22:28:20 -0800868 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -0800869 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -0700870 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530871 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +0530872 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -0600873 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200874 ret = true;
875 }
David Brownelld2876d02008-02-04 22:28:20 -0800876
877 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200878 return ret;
879}
880
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900881void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200882{
883 if (desc && __gpiod_free(desc))
884 module_put(desc->chip->owner);
885 else
886 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -0800887}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900888
David Brownelld2876d02008-02-04 22:28:20 -0800889/**
890 * gpiochip_is_requested - return string iff signal was requested
891 * @chip: controller managing the signal
892 * @offset: of signal within controller's 0..(ngpio - 1) range
893 *
894 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +0900895 * The string returned is the label passed to gpio_request(); if none has been
896 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -0800897 *
898 * This function is for use by GPIO controller drivers. The label can
899 * help with diagnostics, and knowing that the signal is used as a GPIO
900 * can help avoid accidentally multiplexing it to another controller.
901 */
902const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
903{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900904 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800905
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900906 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -0800907 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900908
909 desc = &chip->desc[offset];
910
Alexandre Courbot372e7222013-02-03 01:29:29 +0900911 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -0800912 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900913 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -0800914}
915EXPORT_SYMBOL_GPL(gpiochip_is_requested);
916
Mika Westerberg77c2d792014-03-10 14:54:50 +0200917/**
918 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
919 * @desc: GPIO descriptor to request
920 * @label: label for the GPIO
921 *
922 * Function allows GPIO chip drivers to request and use their own GPIO
923 * descriptors via gpiolib API. Difference to gpiod_request() is that this
924 * function will not increase reference count of the GPIO chip module. This
925 * allows the GPIO chip module to be unloaded as needed (we assume that the
926 * GPIO chip driver handles freeing the GPIOs it has requested).
927 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700928struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
929 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200930{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700931 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
932 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200933
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700934 if (IS_ERR(desc)) {
935 chip_err(chip, "failed to get GPIO descriptor\n");
936 return desc;
937 }
938
939 err = __gpiod_request(desc, label);
940 if (err < 0)
941 return ERR_PTR(err);
942
943 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200944}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700945EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200946
947/**
948 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
949 * @desc: GPIO descriptor to free
950 *
951 * Function frees the given GPIO requested previously with
952 * gpiochip_request_own_desc().
953 */
954void gpiochip_free_own_desc(struct gpio_desc *desc)
955{
956 if (desc)
957 __gpiod_free(desc);
958}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700959EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -0800960
961/* Drivers MUST set GPIO direction before making get/set calls. In
962 * some cases this is done in early boot, before IRQs are enabled.
963 *
964 * As a rule these aren't called more than once (except for drivers
965 * using the open-drain emulation idiom) so these are natural places
966 * to accumulate extra debugging checks. Note that we can't (yet)
967 * rely on gpio_request() having been called beforehand.
968 */
969
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700970/**
971 * gpiod_direction_input - set the GPIO direction to input
972 * @desc: GPIO to set to input
973 *
974 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
975 * be called safely on it.
976 *
977 * Return 0 in case of success, else an error code.
978 */
979int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800980{
David Brownelld2876d02008-02-04 22:28:20 -0800981 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800982 int status = -EINVAL;
983
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200984 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900985 pr_warn("%s: invalid GPIO\n", __func__);
986 return -EINVAL;
987 }
988
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200989 chip = desc->chip;
990 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +0100991 gpiod_warn(desc,
992 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +0200993 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200994 return -EIO;
995 }
996
Alexandre Courbotd82da792014-07-22 16:17:43 +0900997 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -0800998 if (status == 0)
999 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001000
Alexandre Courbot372e7222013-02-03 01:29:29 +09001001 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001002
David Brownelld2876d02008-02-04 22:28:20 -08001003 return status;
1004}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001005EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001006
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001007static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001008{
David Brownelld2876d02008-02-04 22:28:20 -08001009 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001010 int status = -EINVAL;
1011
Linus Walleijd468bf92013-09-24 11:54:38 +02001012 /* GPIOs used for IRQs shall not be set as output */
1013 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1014 gpiod_err(desc,
1015 "%s: tried to set a GPIO tied to an IRQ as output\n",
1016 __func__);
1017 return -EIO;
1018 }
1019
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301020 /* Open drain pin should not be driven to 1 */
1021 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001022 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301023
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301024 /* Open source pin should not be driven to 0 */
1025 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001026 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301027
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001028 chip = desc->chip;
1029 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001030 gpiod_warn(desc,
1031 "%s: missing set() or direction_output() operations\n",
1032 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001033 return -EIO;
1034 }
1035
Alexandre Courbotd82da792014-07-22 16:17:43 +09001036 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001037 if (status == 0)
1038 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001039 trace_gpio_value(desc_to_gpio(desc), 0, value);
1040 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001041 return status;
1042}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001043
1044/**
1045 * gpiod_direction_output_raw - set the GPIO direction to output
1046 * @desc: GPIO to set to output
1047 * @value: initial output value of the GPIO
1048 *
1049 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1050 * be called safely on it. The initial value of the output must be specified
1051 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1052 *
1053 * Return 0 in case of success, else an error code.
1054 */
1055int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1056{
1057 if (!desc || !desc->chip) {
1058 pr_warn("%s: invalid GPIO\n", __func__);
1059 return -EINVAL;
1060 }
1061 return _gpiod_direction_output_raw(desc, value);
1062}
1063EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1064
1065/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301066 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001067 * @desc: GPIO to set to output
1068 * @value: initial output value of the GPIO
1069 *
1070 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1071 * be called safely on it. The initial value of the output must be specified
1072 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1073 * account.
1074 *
1075 * Return 0 in case of success, else an error code.
1076 */
1077int gpiod_direction_output(struct gpio_desc *desc, int value)
1078{
1079 if (!desc || !desc->chip) {
1080 pr_warn("%s: invalid GPIO\n", __func__);
1081 return -EINVAL;
1082 }
1083 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1084 value = !value;
1085 return _gpiod_direction_output_raw(desc, value);
1086}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001087EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001088
Felipe Balbic4b5be92010-05-26 14:42:23 -07001089/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001090 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001091 * @gpio: the gpio to set debounce time
1092 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001093 *
1094 * returns -ENOTSUPP if the controller does not support setting
1095 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001096 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001097int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001098{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001099 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001100
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001101 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001102 pr_warn("%s: invalid GPIO\n", __func__);
1103 return -EINVAL;
1104 }
1105
Felipe Balbic4b5be92010-05-26 14:42:23 -07001106 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001107 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001108 gpiod_dbg(desc,
1109 "%s: missing set() or set_debounce() operations\n",
1110 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001111 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001112 }
1113
Alexandre Courbotd82da792014-07-22 16:17:43 +09001114 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001115}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001116EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001117
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001118/**
1119 * gpiod_is_active_low - test whether a GPIO is active-low or not
1120 * @desc: the gpio descriptor to test
1121 *
1122 * Returns 1 if the GPIO is active-low, 0 otherwise.
1123 */
1124int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001125{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001126 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001127}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001128EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001129
1130/* I/O calls are only valid after configuration completed; the relevant
1131 * "is this a valid GPIO" error checks should already have been done.
1132 *
1133 * "Get" operations are often inlinable as reading a pin value register,
1134 * and masking the relevant bit in that register.
1135 *
1136 * When "set" operations are inlinable, they involve writing that mask to
1137 * one register to set a low value, or a different register to set it high.
1138 * Otherwise locking is needed, so there may be little value to inlining.
1139 *
1140 *------------------------------------------------------------------------
1141 *
1142 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1143 * have requested the GPIO. That can include implicit requesting by
1144 * a direction setting call. Marking a gpio as requested locks its chip
1145 * in memory, guaranteeing that these table lookups need no more locking
1146 * and that gpiochip_remove() will fail.
1147 *
1148 * REVISIT when debugging, consider adding some instrumentation to ensure
1149 * that the GPIO was actually requested.
1150 */
1151
Alexandre Courbot23600962014-03-11 15:52:09 +09001152static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001153{
1154 struct gpio_chip *chip;
Alexandre Courbot23600962014-03-11 15:52:09 +09001155 bool value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001156 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001157
Alexandre Courbot372e7222013-02-03 01:29:29 +09001158 chip = desc->chip;
1159 offset = gpio_chip_hwgpio(desc);
Alexandre Courbot23600962014-03-11 15:52:09 +09001160 value = chip->get ? chip->get(chip, offset) : false;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001161 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001162 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001163}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001164
David Brownelld2876d02008-02-04 22:28:20 -08001165/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001166 * gpiod_get_raw_value() - return a gpio's raw value
1167 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001168 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001169 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1170 * its ACTIVE_LOW status.
1171 *
1172 * This function should be called from contexts where we cannot sleep, and will
1173 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001174 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001175int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001176{
David Brownelld2876d02008-02-04 22:28:20 -08001177 if (!desc)
1178 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001179 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001180 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001181 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001182}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001183EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001184
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001185/**
1186 * gpiod_get_value() - return a gpio's value
1187 * @desc: gpio whose value will be returned
1188 *
1189 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1190 * account.
1191 *
1192 * This function should be called from contexts where we cannot sleep, and will
1193 * complain if the GPIO chip functions potentially sleep.
1194 */
1195int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001196{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001197 int value;
1198 if (!desc)
1199 return 0;
1200 /* Should be using gpio_get_value_cansleep() */
1201 WARN_ON(desc->chip->can_sleep);
1202
1203 value = _gpiod_get_raw_value(desc);
1204 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1205 value = !value;
1206
1207 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001208}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001209EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001210
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301211/*
1212 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001213 * @desc: gpio descriptor whose state need to be set.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301214 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1215 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001216static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301217{
1218 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001219 struct gpio_chip *chip = desc->chip;
1220 int offset = gpio_chip_hwgpio(desc);
1221
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301222 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001223 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301224 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001225 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301226 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001227 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301228 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001229 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301230 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001231 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301232 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001233 gpiod_err(desc,
1234 "%s: Error in set_value for open drain err %d\n",
1235 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301236}
1237
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301238/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001239 * _gpio_set_open_source_value() - Set the open source gpio's value.
1240 * @desc: gpio descriptor whose state need to be set.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301241 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1242 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001243static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301244{
1245 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001246 struct gpio_chip *chip = desc->chip;
1247 int offset = gpio_chip_hwgpio(desc);
1248
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301249 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001250 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301251 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001252 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301253 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001254 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301255 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001256 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301257 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001258 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301259 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001260 gpiod_err(desc,
1261 "%s: Error in set_value for open source err %d\n",
1262 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301263}
1264
Alexandre Courbot23600962014-03-11 15:52:09 +09001265static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001266{
1267 struct gpio_chip *chip;
1268
Alexandre Courbot372e7222013-02-03 01:29:29 +09001269 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001270 trace_gpio_value(desc_to_gpio(desc), 0, value);
1271 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1272 _gpio_set_open_drain_value(desc, value);
1273 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1274 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301275 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001276 chip->set(chip, gpio_chip_hwgpio(desc), value);
1277}
1278
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001279/*
1280 * set multiple outputs on the same chip;
1281 * use the chip's set_multiple function if available;
1282 * otherwise set the outputs sequentially;
1283 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1284 * defines which outputs are to be changed
1285 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1286 * defines the values the outputs specified by mask are to be set to
1287 */
1288static void gpio_chip_set_multiple(struct gpio_chip *chip,
1289 unsigned long *mask, unsigned long *bits)
1290{
1291 if (chip->set_multiple) {
1292 chip->set_multiple(chip, mask, bits);
1293 } else {
1294 int i;
1295 for (i = 0; i < chip->ngpio; i++) {
1296 if (mask[BIT_WORD(i)] == 0) {
1297 /* no more set bits in this mask word;
1298 * skip ahead to the next word */
1299 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1300 continue;
1301 }
1302 /* set outputs if the corresponding mask bit is set */
1303 if (__test_and_clear_bit(i, mask)) {
1304 chip->set(chip, i, test_bit(i, bits));
1305 }
1306 }
1307 }
1308}
1309
1310static void gpiod_set_array_priv(bool raw, bool can_sleep,
1311 unsigned int array_size,
1312 struct gpio_desc **desc_array,
1313 int *value_array)
1314{
1315 int i = 0;
1316
1317 while (i < array_size) {
1318 struct gpio_chip *chip = desc_array[i]->chip;
1319 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1320 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1321 int count = 0;
1322
1323 if (!can_sleep) {
1324 WARN_ON(chip->can_sleep);
1325 }
1326 memset(mask, 0, sizeof(mask));
1327 do {
1328 struct gpio_desc *desc = desc_array[i];
1329 int hwgpio = gpio_chip_hwgpio(desc);
1330 int value = value_array[i];
1331
1332 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1333 value = !value;
1334 trace_gpio_value(desc_to_gpio(desc), 0, value);
1335 /*
1336 * collect all normal outputs belonging to the same chip
1337 * open drain and open source outputs are set individually
1338 */
1339 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
1340 _gpio_set_open_drain_value(desc,value);
1341 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1342 _gpio_set_open_source_value(desc, value);
1343 } else {
1344 __set_bit(hwgpio, mask);
1345 if (value) {
1346 __set_bit(hwgpio, bits);
1347 } else {
1348 __clear_bit(hwgpio, bits);
1349 }
1350 count++;
1351 }
1352 i++;
1353 } while ((i < array_size) && (desc_array[i]->chip == chip));
1354 /* push collected bits to outputs */
1355 if (count != 0) {
1356 gpio_chip_set_multiple(chip, mask, bits);
1357 }
1358 }
1359}
1360
David Brownelld2876d02008-02-04 22:28:20 -08001361/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001362 * gpiod_set_raw_value() - assign a gpio's raw value
1363 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001364 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001365 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001366 * Set the raw value of the GPIO, i.e. the value of its physical line without
1367 * regard for its ACTIVE_LOW status.
1368 *
1369 * This function should be called from contexts where we cannot sleep, and will
1370 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001371 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001372void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001373{
David Brownelld2876d02008-02-04 22:28:20 -08001374 if (!desc)
1375 return;
David Brownelld2876d02008-02-04 22:28:20 -08001376 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001377 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001378 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001379}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001380EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001381
1382/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001383 * gpiod_set_value() - assign a gpio's value
1384 * @desc: gpio whose value will be assigned
1385 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001386 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001387 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1388 * account
1389 *
1390 * This function should be called from contexts where we cannot sleep, and will
1391 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001392 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001393void gpiod_set_value(struct gpio_desc *desc, int value)
1394{
1395 if (!desc)
1396 return;
1397 /* Should be using gpio_set_value_cansleep() */
1398 WARN_ON(desc->chip->can_sleep);
1399 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1400 value = !value;
1401 _gpiod_set_raw_value(desc, value);
1402}
1403EXPORT_SYMBOL_GPL(gpiod_set_value);
1404
1405/**
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001406 * gpiod_set_raw_array() - assign values to an array of GPIOs
1407 * @array_size: number of elements in the descriptor / value arrays
1408 * @desc_array: array of GPIO descriptors whose values will be assigned
1409 * @value_array: array of values to assign
1410 *
1411 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1412 * without regard for their ACTIVE_LOW status.
1413 *
1414 * This function should be called from contexts where we cannot sleep, and will
1415 * complain if the GPIO chip functions potentially sleep.
1416 */
1417void gpiod_set_raw_array(unsigned int array_size,
1418 struct gpio_desc **desc_array, int *value_array)
1419{
1420 if (!desc_array)
1421 return;
1422 gpiod_set_array_priv(true, false, array_size, desc_array, value_array);
1423}
1424EXPORT_SYMBOL_GPL(gpiod_set_raw_array);
1425
1426/**
1427 * gpiod_set_array() - assign values to an array of GPIOs
1428 * @array_size: number of elements in the descriptor / value arrays
1429 * @desc_array: array of GPIO descriptors whose values will be assigned
1430 * @value_array: array of values to assign
1431 *
1432 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1433 * into account.
1434 *
1435 * This function should be called from contexts where we cannot sleep, and will
1436 * complain if the GPIO chip functions potentially sleep.
1437 */
1438void gpiod_set_array(unsigned int array_size,
1439 struct gpio_desc **desc_array, int *value_array)
1440{
1441 if (!desc_array)
1442 return;
1443 gpiod_set_array_priv(false, false, array_size, desc_array, value_array);
1444}
1445EXPORT_SYMBOL_GPL(gpiod_set_array);
1446
1447/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001448 * gpiod_cansleep() - report whether gpio value access may sleep
1449 * @desc: gpio to check
1450 *
1451 */
1452int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001453{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001454 if (!desc)
1455 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001456 return desc->chip->can_sleep;
1457}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001458EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001459
David Brownell0f6d5042008-10-15 22:03:14 -07001460/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001461 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1462 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001463 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001464 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1465 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001466 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001467int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001468{
1469 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001470 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001471
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001472 if (!desc)
1473 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001474 chip = desc->chip;
1475 offset = gpio_chip_hwgpio(desc);
1476 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1477}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001478EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001479
Linus Walleijd468bf92013-09-24 11:54:38 +02001480/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001481 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001482 * @chip: the chip the GPIO to lock belongs to
1483 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001484 *
1485 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001486 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001487 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001488int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001489{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001490 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001491 return -EINVAL;
1492
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001493 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1494 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001495 "%s: tried to flag a GPIO set as output for IRQ\n",
1496 __func__);
1497 return -EIO;
1498 }
1499
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001500 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001501 return 0;
1502}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001503EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001504
Linus Walleijd468bf92013-09-24 11:54:38 +02001505/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001506 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001507 * @chip: the chip the GPIO to lock belongs to
1508 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001509 *
1510 * This is used directly by GPIO drivers that want to indicate
1511 * that a certain GPIO is no longer used exclusively for IRQ.
1512 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001513void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001514{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001515 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001516 return;
1517
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001518 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001519}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001520EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001521
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001522/**
1523 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1524 * @desc: gpio whose value will be returned
1525 *
1526 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1527 * its ACTIVE_LOW status.
1528 *
1529 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001530 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001531int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001532{
David Brownelld2876d02008-02-04 22:28:20 -08001533 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001534 if (!desc)
1535 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001536 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001537}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001538EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001539
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001540/**
1541 * gpiod_get_value_cansleep() - return a gpio's value
1542 * @desc: gpio whose value will be returned
1543 *
1544 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1545 * account.
1546 *
1547 * This function is to be called from contexts that can sleep.
1548 */
1549int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001550{
David Brownelld2876d02008-02-04 22:28:20 -08001551 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001552
1553 might_sleep_if(extra_checks);
1554 if (!desc)
1555 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001556
1557 value = _gpiod_get_raw_value(desc);
1558 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1559 value = !value;
1560
David Brownelld2876d02008-02-04 22:28:20 -08001561 return value;
1562}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001563EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001564
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001565/**
1566 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1567 * @desc: gpio whose value will be assigned
1568 * @value: value to assign
1569 *
1570 * Set the raw value of the GPIO, i.e. the value of its physical line without
1571 * regard for its ACTIVE_LOW status.
1572 *
1573 * This function is to be called from contexts that can sleep.
1574 */
1575void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001576{
David Brownelld2876d02008-02-04 22:28:20 -08001577 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001578 if (!desc)
1579 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001580 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001581}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001582EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001583
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001584/**
1585 * gpiod_set_value_cansleep() - assign a gpio's value
1586 * @desc: gpio whose value will be assigned
1587 * @value: value to assign
1588 *
1589 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1590 * account
1591 *
1592 * This function is to be called from contexts that can sleep.
1593 */
1594void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001595{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001596 might_sleep_if(extra_checks);
1597 if (!desc)
1598 return;
1599
1600 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1601 value = !value;
1602 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001603}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001604EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001605
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001606/**
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001607 * gpiod_set_raw_array_cansleep() - assign values to an array of GPIOs
1608 * @array_size: number of elements in the descriptor / value arrays
1609 * @desc_array: array of GPIO descriptors whose values will be assigned
1610 * @value_array: array of values to assign
1611 *
1612 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1613 * without regard for their ACTIVE_LOW status.
1614 *
1615 * This function is to be called from contexts that can sleep.
1616 */
1617void gpiod_set_raw_array_cansleep(unsigned int array_size,
1618 struct gpio_desc **desc_array,
1619 int *value_array)
1620{
1621 might_sleep_if(extra_checks);
1622 if (!desc_array)
1623 return;
1624 gpiod_set_array_priv(true, true, array_size, desc_array, value_array);
1625}
1626EXPORT_SYMBOL_GPL(gpiod_set_raw_array_cansleep);
1627
1628/**
1629 * gpiod_set_array_cansleep() - assign values to an array of GPIOs
1630 * @array_size: number of elements in the descriptor / value arrays
1631 * @desc_array: array of GPIO descriptors whose values will be assigned
1632 * @value_array: array of values to assign
1633 *
1634 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1635 * into account.
1636 *
1637 * This function is to be called from contexts that can sleep.
1638 */
1639void gpiod_set_array_cansleep(unsigned int array_size,
1640 struct gpio_desc **desc_array,
1641 int *value_array)
1642{
1643 might_sleep_if(extra_checks);
1644 if (!desc_array)
1645 return;
1646 gpiod_set_array_priv(false, true, array_size, desc_array, value_array);
1647}
1648EXPORT_SYMBOL_GPL(gpiod_set_array_cansleep);
1649
1650/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001651 * gpiod_add_lookup_table() - register GPIO device consumers
1652 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001653 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001654void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001655{
1656 mutex_lock(&gpio_lookup_lock);
1657
Alexandre Courbotad824782013-12-03 12:20:11 +09001658 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001659
1660 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001661}
1662
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001663static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001664 unsigned int idx,
1665 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001666{
1667 char prop_name[32]; /* 32 is max size of property name */
1668 enum of_gpio_flags of_flags;
1669 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001670 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001671
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001672 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02001673 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01001674 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001675 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02001676 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01001677 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001678 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001679
Thierry Redingdd34c372014-04-23 17:28:09 +02001680 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1681 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001682 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001683 break;
1684 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001685
1686 if (IS_ERR(desc))
1687 return desc;
1688
1689 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001690 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001691
1692 return desc;
1693}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001694
Mika Westerberg81f59e92013-10-10 11:01:09 +03001695static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001696 unsigned int idx,
1697 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001698{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001699 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03001700 struct acpi_gpio_info info;
1701 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001702 char propname[32];
1703 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03001704
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001705 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001706 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001707 if (con_id && strcmp(con_id, "gpios")) {
1708 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001709 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001710 } else {
1711 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001712 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001713 }
Mika Westerberge01f4402013-10-10 11:01:10 +03001714
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001715 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1716 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1717 break;
1718 }
1719
1720 /* Then from plain _CRS GPIOs */
1721 if (IS_ERR(desc)) {
1722 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1723 if (IS_ERR(desc))
1724 return desc;
1725 }
1726
1727 if (info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001728 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001729
1730 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001731}
1732
Alexandre Courbotad824782013-12-03 12:20:11 +09001733static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1734{
1735 const char *dev_id = dev ? dev_name(dev) : NULL;
1736 struct gpiod_lookup_table *table;
1737
1738 mutex_lock(&gpio_lookup_lock);
1739
1740 list_for_each_entry(table, &gpio_lookup_list, list) {
1741 if (table->dev_id && dev_id) {
1742 /*
1743 * Valid strings on both ends, must be identical to have
1744 * a match
1745 */
1746 if (!strcmp(table->dev_id, dev_id))
1747 goto found;
1748 } else {
1749 /*
1750 * One of the pointers is NULL, so both must be to have
1751 * a match
1752 */
1753 if (dev_id == table->dev_id)
1754 goto found;
1755 }
1756 }
1757 table = NULL;
1758
1759found:
1760 mutex_unlock(&gpio_lookup_lock);
1761 return table;
1762}
1763
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001764static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001765 unsigned int idx,
1766 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001767{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001768 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001769 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001770 struct gpiod_lookup *p;
1771
Alexandre Courbotad824782013-12-03 12:20:11 +09001772 table = gpiod_find_lookup_table(dev);
1773 if (!table)
1774 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001775
Alexandre Courbotad824782013-12-03 12:20:11 +09001776 for (p = &table->table[0]; p->chip_label; p++) {
1777 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001778
Alexandre Courbotad824782013-12-03 12:20:11 +09001779 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001780 if (p->idx != idx)
1781 continue;
1782
Alexandre Courbotad824782013-12-03 12:20:11 +09001783 /* If the lookup entry has a con_id, require exact match */
1784 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1785 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001786
Alexandre Courbotad824782013-12-03 12:20:11 +09001787 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001788
Alexandre Courbotad824782013-12-03 12:20:11 +09001789 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001790 dev_err(dev, "cannot find GPIO chip %s\n",
1791 p->chip_label);
1792 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001793 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001794
Alexandre Courbotad824782013-12-03 12:20:11 +09001795 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001796 dev_err(dev,
1797 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1798 idx, chip->ngpio, chip->label);
1799 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001800 }
1801
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001802 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001803 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001804
1805 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001806 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001807
1808 return desc;
1809}
1810
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001811static int dt_gpio_count(struct device *dev, const char *con_id)
1812{
1813 int ret;
1814 char propname[32];
1815 unsigned int i;
1816
1817 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1818 if (con_id)
1819 snprintf(propname, sizeof(propname), "%s-%s",
1820 con_id, gpio_suffixes[i]);
1821 else
1822 snprintf(propname, sizeof(propname), "%s",
1823 gpio_suffixes[i]);
1824
1825 ret = of_gpio_named_count(dev->of_node, propname);
1826 if (ret >= 0)
1827 break;
1828 }
1829 return ret;
1830}
1831
1832static int platform_gpio_count(struct device *dev, const char *con_id)
1833{
1834 struct gpiod_lookup_table *table;
1835 struct gpiod_lookup *p;
1836 unsigned int count = 0;
1837
1838 table = gpiod_find_lookup_table(dev);
1839 if (!table)
1840 return -ENOENT;
1841
1842 for (p = &table->table[0]; p->chip_label; p++) {
1843 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
1844 (!con_id && !p->con_id))
1845 count++;
1846 }
1847 if (!count)
1848 return -ENOENT;
1849
1850 return count;
1851}
1852
1853/**
1854 * gpiod_count - return the number of GPIOs associated with a device / function
1855 * or -ENOENT if no GPIO has been assigned to the requested function
1856 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1857 * @con_id: function within the GPIO consumer
1858 */
1859int gpiod_count(struct device *dev, const char *con_id)
1860{
1861 int count = -ENOENT;
1862
1863 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
1864 count = dt_gpio_count(dev, con_id);
1865 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
1866 count = acpi_gpio_count(dev, con_id);
1867
1868 if (count < 0)
1869 count = platform_gpio_count(dev, con_id);
1870
1871 return count;
1872}
1873EXPORT_SYMBOL_GPL(gpiod_count);
1874
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001875/**
Thierry Reding08791622014-04-25 16:54:22 +02001876 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09001877 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001878 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001879 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001880 *
1881 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001882 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1883 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001884 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001885struct gpio_desc *__must_check __gpiod_get(struct device *dev, const char *con_id,
1886 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001887{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001888 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001889}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001890EXPORT_SYMBOL_GPL(__gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001891
1892/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02001893 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1894 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1895 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001896 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02001897 *
1898 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1899 * the requested function it will return NULL. This is convenient for drivers
1900 * that need to handle optional GPIOs.
1901 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001902struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
1903 const char *con_id,
1904 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02001905{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001906 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001907}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001908EXPORT_SYMBOL_GPL(__gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001909
Benoit Parrotf625d462015-02-02 11:44:44 -06001910
1911/**
1912 * gpiod_configure_flags - helper function to configure a given GPIO
1913 * @desc: gpio whose value will be assigned
1914 * @con_id: function within the GPIO consumer
1915 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
1916 * of_get_gpio_hog()
1917 * @dflags: gpiod_flags - optional GPIO initialization flags
1918 *
1919 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
1920 * requested function and/or index, or another IS_ERR() code if an error
1921 * occurred while trying to acquire the GPIO.
1922 */
1923static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
1924 unsigned long lflags, enum gpiod_flags dflags)
1925{
1926 int status;
1927
1928 if (lflags & GPIO_ACTIVE_LOW)
1929 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
1930 if (lflags & GPIO_OPEN_DRAIN)
1931 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1932 if (lflags & GPIO_OPEN_SOURCE)
1933 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
1934
1935 /* No particular flag request, return here... */
1936 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
1937 pr_debug("no flags found for %s\n", con_id);
1938 return 0;
1939 }
1940
1941 /* Process flags */
1942 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
1943 status = gpiod_direction_output(desc,
1944 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
1945 else
1946 status = gpiod_direction_input(desc);
1947
1948 return status;
1949}
1950
Thierry Reding29a1f2332014-04-25 17:10:06 +02001951/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001952 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02001953 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001954 * @con_id: function within the GPIO consumer
1955 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001956 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001957 *
1958 * This variant of gpiod_get() allows to access GPIOs other than the first
1959 * defined one for functions that define several GPIOs.
1960 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001961 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1962 * requested function and/or index, or another IS_ERR() code if an error
1963 * occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001964 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001965struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001966 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001967 unsigned int idx,
1968 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001969{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001970 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001971 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001972 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001973
1974 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
1975
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01001976 if (dev) {
1977 /* Using device tree? */
1978 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1979 dev_dbg(dev, "using device tree for GPIO lookup\n");
1980 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
1981 } else if (ACPI_COMPANION(dev)) {
1982 dev_dbg(dev, "using ACPI for GPIO lookup\n");
1983 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
1984 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001985 }
1986
1987 /*
1988 * Either we are not using DT or ACPI, or their lookup did not return
1989 * a result. In that case, use platform lookup as a fallback.
1990 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001991 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04001992 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001993 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001994 }
1995
1996 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02001997 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001998 return desc;
1999 }
2000
2001 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002002 if (status < 0)
2003 return ERR_PTR(status);
2004
Benoit Parrotf625d462015-02-02 11:44:44 -06002005 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002006 if (status < 0) {
2007 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2008 gpiod_put(desc);
2009 return ERR_PTR(status);
2010 }
2011
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002012 return desc;
2013}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002014EXPORT_SYMBOL_GPL(__gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002015
2016/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002017 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2018 * @fwnode: handle of the firmware node
2019 * @propname: name of the firmware property representing the GPIO
2020 *
2021 * This function can be used for drivers that get their configuration
2022 * from firmware.
2023 *
2024 * Function properly finds the corresponding GPIO using whatever is the
2025 * underlying firmware interface and then makes sure that the GPIO
2026 * descriptor is requested before it is returned to the caller.
2027 *
2028 * In case of error an ERR_PTR() is returned.
2029 */
2030struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2031 const char *propname)
2032{
2033 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2034 bool active_low = false;
2035 int ret;
2036
2037 if (!fwnode)
2038 return ERR_PTR(-EINVAL);
2039
2040 if (is_of_node(fwnode)) {
2041 enum of_gpio_flags flags;
2042
2043 desc = of_get_named_gpiod_flags(of_node(fwnode), propname, 0,
2044 &flags);
2045 if (!IS_ERR(desc))
2046 active_low = flags & OF_GPIO_ACTIVE_LOW;
2047 } else if (is_acpi_node(fwnode)) {
2048 struct acpi_gpio_info info;
2049
2050 desc = acpi_get_gpiod_by_index(acpi_node(fwnode), propname, 0,
2051 &info);
2052 if (!IS_ERR(desc))
2053 active_low = info.active_low;
2054 }
2055
2056 if (IS_ERR(desc))
2057 return desc;
2058
2059 ret = gpiod_request(desc, NULL);
2060 if (ret)
2061 return ERR_PTR(ret);
2062
2063 /* Only value flag can be set from both DT and ACPI is active_low */
2064 if (active_low)
2065 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2066
2067 return desc;
2068}
2069EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2070
2071/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002072 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2073 * function
2074 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2075 * @con_id: function within the GPIO consumer
2076 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002077 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002078 *
2079 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2080 * specified index was assigned to the requested function it will return NULL.
2081 * This is convenient for drivers that need to handle optional GPIOs.
2082 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002083struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002084 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002085 unsigned int index,
2086 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002087{
2088 struct gpio_desc *desc;
2089
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002090 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002091 if (IS_ERR(desc)) {
2092 if (PTR_ERR(desc) == -ENOENT)
2093 return NULL;
2094 }
2095
2096 return desc;
2097}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002098EXPORT_SYMBOL_GPL(__gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002099
2100/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002101 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2102 * @desc: gpio whose value will be assigned
2103 * @name: gpio line name
2104 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2105 * of_get_gpio_hog()
2106 * @dflags: gpiod_flags - optional GPIO initialization flags
2107 */
2108int gpiod_hog(struct gpio_desc *desc, const char *name,
2109 unsigned long lflags, enum gpiod_flags dflags)
2110{
2111 struct gpio_chip *chip;
2112 struct gpio_desc *local_desc;
2113 int hwnum;
2114 int status;
2115
2116 chip = gpiod_to_chip(desc);
2117 hwnum = gpio_chip_hwgpio(desc);
2118
2119 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2120 if (IS_ERR(local_desc)) {
2121 pr_debug("requesting own GPIO %s failed\n", name);
2122 return PTR_ERR(local_desc);
2123 }
2124
2125 status = gpiod_configure_flags(desc, name, lflags, dflags);
2126 if (status < 0) {
2127 pr_debug("setup of GPIO %s failed\n", name);
2128 gpiochip_free_own_desc(desc);
2129 return status;
2130 }
2131
2132 /* Mark GPIO as hogged so it can be identified and removed later */
2133 set_bit(FLAG_IS_HOGGED, &desc->flags);
2134
2135 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2136 desc_to_gpio(desc), name,
2137 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2138 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2139 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2140
2141 return 0;
2142}
2143
2144/**
2145 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2146 * @chip: gpio chip to act on
2147 *
2148 * This is only used by of_gpiochip_remove to free hogged gpios
2149 */
2150static void gpiochip_free_hogs(struct gpio_chip *chip)
2151{
2152 int id;
2153
2154 for (id = 0; id < chip->ngpio; id++) {
2155 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2156 gpiochip_free_own_desc(&chip->desc[id]);
2157 }
2158}
2159
2160/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002161 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2162 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2163 * @con_id: function within the GPIO consumer
2164 * @flags: optional GPIO initialization flags
2165 *
2166 * This function acquires all the GPIOs defined under a given function.
2167 *
2168 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2169 * no GPIO has been assigned to the requested function, or another IS_ERR()
2170 * code if an error occurred while trying to acquire the GPIOs.
2171 */
2172struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2173 const char *con_id,
2174 enum gpiod_flags flags)
2175{
2176 struct gpio_desc *desc;
2177 struct gpio_descs *descs;
2178 int count;
2179
2180 count = gpiod_count(dev, con_id);
2181 if (count < 0)
2182 return ERR_PTR(count);
2183
2184 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2185 GFP_KERNEL);
2186 if (!descs)
2187 return ERR_PTR(-ENOMEM);
2188
2189 for (descs->ndescs = 0; descs->ndescs < count; ) {
2190 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2191 if (IS_ERR(desc)) {
2192 gpiod_put_array(descs);
2193 return ERR_CAST(desc);
2194 }
2195 descs->desc[descs->ndescs] = desc;
2196 descs->ndescs++;
2197 }
2198 return descs;
2199}
2200EXPORT_SYMBOL_GPL(gpiod_get_array);
2201
2202/**
2203 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2204 * function
2205 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2206 * @con_id: function within the GPIO consumer
2207 * @flags: optional GPIO initialization flags
2208 *
2209 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2210 * assigned to the requested function it will return NULL.
2211 */
2212struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2213 const char *con_id,
2214 enum gpiod_flags flags)
2215{
2216 struct gpio_descs *descs;
2217
2218 descs = gpiod_get_array(dev, con_id, flags);
2219 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2220 return NULL;
2221
2222 return descs;
2223}
2224EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2225
2226/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002227 * gpiod_put - dispose of a GPIO descriptor
2228 * @desc: GPIO descriptor to dispose of
2229 *
2230 * No descriptor can be used after gpiod_put() has been called on it.
2231 */
2232void gpiod_put(struct gpio_desc *desc)
2233{
2234 gpiod_free(desc);
2235}
2236EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002237
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002238/**
2239 * gpiod_put_array - dispose of multiple GPIO descriptors
2240 * @descs: struct gpio_descs containing an array of descriptors
2241 */
2242void gpiod_put_array(struct gpio_descs *descs)
2243{
2244 unsigned int i;
2245
2246 for (i = 0; i < descs->ndescs; i++)
2247 gpiod_put(descs->desc[i]);
2248
2249 kfree(descs);
2250}
2251EXPORT_SYMBOL_GPL(gpiod_put_array);
2252
David Brownelld2876d02008-02-04 22:28:20 -08002253#ifdef CONFIG_DEBUG_FS
2254
2255static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2256{
2257 unsigned i;
2258 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002259 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002260 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002261 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002262
2263 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2264 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2265 continue;
2266
Alexandre Courbot372e7222013-02-03 01:29:29 +09002267 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002268 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002269 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2270 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002271 gpio, gdesc->label,
2272 is_out ? "out" : "in ",
2273 chip->get
2274 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002275 : "? ",
2276 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002277 seq_printf(s, "\n");
2278 }
2279}
2280
Thierry Redingf9c4a312012-04-12 13:26:01 +02002281static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002282{
Grant Likely362432a2013-02-09 09:41:49 +00002283 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002284 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002285 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002286
2287 s->private = "";
2288
Grant Likely362432a2013-02-09 09:41:49 +00002289 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002290 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002291 if (index-- == 0) {
2292 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002293 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002294 }
2295 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002296
2297 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002298}
2299
2300static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2301{
Grant Likely362432a2013-02-09 09:41:49 +00002302 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002303 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002304 void *ret = NULL;
2305
Grant Likely362432a2013-02-09 09:41:49 +00002306 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002307 if (list_is_last(&chip->list, &gpio_chips))
2308 ret = NULL;
2309 else
2310 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002311 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002312
2313 s->private = "\n";
2314 ++*pos;
2315
2316 return ret;
2317}
2318
2319static void gpiolib_seq_stop(struct seq_file *s, void *v)
2320{
2321}
2322
2323static int gpiolib_seq_show(struct seq_file *s, void *v)
2324{
2325 struct gpio_chip *chip = v;
2326 struct device *dev;
2327
2328 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2329 chip->base, chip->base + chip->ngpio - 1);
2330 dev = chip->dev;
2331 if (dev)
2332 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2333 dev_name(dev));
2334 if (chip->label)
2335 seq_printf(s, ", %s", chip->label);
2336 if (chip->can_sleep)
2337 seq_printf(s, ", can sleep");
2338 seq_printf(s, ":\n");
2339
2340 if (chip->dbg_show)
2341 chip->dbg_show(s, chip);
2342 else
2343 gpiolib_dbg_show(s, chip);
2344
David Brownelld2876d02008-02-04 22:28:20 -08002345 return 0;
2346}
2347
Thierry Redingf9c4a312012-04-12 13:26:01 +02002348static const struct seq_operations gpiolib_seq_ops = {
2349 .start = gpiolib_seq_start,
2350 .next = gpiolib_seq_next,
2351 .stop = gpiolib_seq_stop,
2352 .show = gpiolib_seq_show,
2353};
2354
David Brownelld2876d02008-02-04 22:28:20 -08002355static int gpiolib_open(struct inode *inode, struct file *file)
2356{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002357 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002358}
2359
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002360static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002361 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002362 .open = gpiolib_open,
2363 .read = seq_read,
2364 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002365 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002366};
2367
2368static int __init gpiolib_debugfs_init(void)
2369{
2370 /* /sys/kernel/debug/gpio */
2371 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2372 NULL, NULL, &gpiolib_operations);
2373 return 0;
2374}
2375subsys_initcall(gpiolib_debugfs_init);
2376
2377#endif /* DEBUG_FS */