blob: f25dc880b0073edd2c2449b9ad9ace77781c18c7 [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
Johan Hovold426577b2015-05-04 17:10:32 +0200293 status = gpiochip_sysfs_register(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{
Johan Hovoldfab28b82015-05-04 17:10:27 +0200330 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800331 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800332 unsigned id;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200333 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800334
Johan Hovold426577b2015-05-04 17:10:32 +0200335 gpiochip_sysfs_unregister(chip);
Johan Hovold01cca932015-01-12 17:12:29 +0100336
Johan Hovold00acc3d2015-01-12 17:12:27 +0100337 gpiochip_irqchip_remove(chip);
338
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200339 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100340 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600341 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600342 of_gpiochip_remove(chip);
343
Johan Hovold6798aca2015-01-12 17:12:28 +0100344 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900345 for (id = 0; id < chip->ngpio; id++) {
Johan Hovoldfab28b82015-05-04 17:10:27 +0200346 desc = &chip->desc[id];
347 desc->chip = NULL;
348 if (test_bit(FLAG_REQUESTED, &desc->flags))
349 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800350 }
abdoulaye berthee1db1702014-07-05 18:28:50 +0200351 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800352 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900353
Johan Hovoldfab28b82015-05-04 17:10:27 +0200354 if (requested)
355 dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
356
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900357 kfree(chip->desc);
358 chip->desc = NULL;
David Brownelld2876d02008-02-04 22:28:20 -0800359}
360EXPORT_SYMBOL_GPL(gpiochip_remove);
361
Grant Likely594fa262010-06-08 07:48:16 -0600362/**
363 * gpiochip_find() - iterator for locating a specific gpio_chip
364 * @data: data to pass to match function
365 * @callback: Callback function to check gpio_chip
366 *
367 * Similar to bus_find_device. It returns a reference to a gpio_chip as
368 * determined by a user supplied @match callback. The callback should return
369 * 0 if the device doesn't match and non-zero if it does. If the callback is
370 * non-zero, this function will return to the caller and not iterate over any
371 * more gpio_chips.
372 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600373struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700374 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600375 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600376{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900377 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600378 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600379
380 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900381 list_for_each_entry(chip, &gpio_chips, list)
382 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600383 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900384
385 /* No match? */
386 if (&chip->list == &gpio_chips)
387 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600388 spin_unlock_irqrestore(&gpio_lock, flags);
389
390 return chip;
391}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600392EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800393
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900394static int gpiochip_match_name(struct gpio_chip *chip, void *data)
395{
396 const char *name = data;
397
398 return !strcmp(chip->label, name);
399}
400
401static struct gpio_chip *find_chip_by_name(const char *name)
402{
403 return gpiochip_find((void *)name, gpiochip_match_name);
404}
405
Linus Walleij14250522014-03-25 10:40:18 +0100406#ifdef CONFIG_GPIOLIB_IRQCHIP
407
408/*
409 * The following is irqchip helper code for gpiochips.
410 */
411
412/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200413 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
414 * @gpiochip: the gpiochip to set the irqchip chain to
415 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100416 * @parent_irq: the irq number corresponding to the parent IRQ for this
417 * chained irqchip
418 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200419 * coming out of the gpiochip. If the interrupt is nested rather than
420 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100421 */
422void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
423 struct irq_chip *irqchip,
424 int parent_irq,
425 irq_flow_handler_t parent_handler)
426{
Linus Walleij83141a72014-09-26 13:50:12 +0200427 unsigned int offset;
428
Linus Walleij83141a72014-09-26 13:50:12 +0200429 if (!gpiochip->irqdomain) {
430 chip_err(gpiochip, "called %s before setting up irqchip\n",
431 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200432 return;
433 }
434
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200435 if (parent_handler) {
436 if (gpiochip->can_sleep) {
437 chip_err(gpiochip,
438 "you cannot have chained interrupts on a "
439 "chip that may sleep\n");
440 return;
441 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200442 /*
443 * The parent irqchip is already using the chip_data for this
444 * irqchip, so our callbacks simply use the handler_data.
445 */
446 irq_set_handler_data(parent_irq, gpiochip);
Linus Torvaldsea584592014-10-09 14:58:15 -0400447 irq_set_chained_handler(parent_irq, parent_handler);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300448
449 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200450 }
Linus Walleij83141a72014-09-26 13:50:12 +0200451
452 /* Set the parent IRQ for all affected IRQs */
453 for (offset = 0; offset < gpiochip->ngpio; offset++)
454 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
455 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100456}
457EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
458
Linus Walleije45d1c82014-04-22 14:01:46 +0200459/*
460 * This lock class tells lockdep that GPIO irqs are in a different
461 * category than their parents, so it won't report false recursion.
462 */
463static struct lock_class_key gpiochip_irq_lock_class;
464
Linus Walleij14250522014-03-25 10:40:18 +0100465/**
466 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
467 * @d: the irqdomain used by this irqchip
468 * @irq: the global irq number used by this GPIO irqchip irq
469 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
470 *
471 * This function will set up the mapping for a certain IRQ line on a
472 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
473 * stored inside the gpiochip.
474 */
475static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
476 irq_hw_number_t hwirq)
477{
478 struct gpio_chip *chip = d->host_data;
479
Linus Walleij14250522014-03-25 10:40:18 +0100480 irq_set_chip_data(irq, chip);
Linus Walleije45d1c82014-04-22 14:01:46 +0200481 irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
Linus Walleij7633fb92014-04-09 13:20:38 +0200482 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200483 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300484 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200485 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100486#ifdef CONFIG_ARM
487 set_irq_flags(irq, IRQF_VALID);
488#else
489 irq_set_noprobe(irq);
490#endif
Linus Walleij1333b902014-04-23 16:45:12 +0200491 /*
492 * No set-up of the hardware will happen if IRQ_TYPE_NONE
493 * is passed as default type.
494 */
495 if (chip->irq_default_type != IRQ_TYPE_NONE)
496 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100497
498 return 0;
499}
500
Linus Walleijc3626fd2014-03-28 20:42:01 +0100501static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
502{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200503 struct gpio_chip *chip = d->host_data;
504
Linus Walleijc3626fd2014-03-28 20:42:01 +0100505#ifdef CONFIG_ARM
506 set_irq_flags(irq, 0);
507#endif
Linus Walleij1c8732b2014-04-09 13:34:39 +0200508 if (chip->can_sleep)
509 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100510 irq_set_chip_and_handler(irq, NULL, NULL);
511 irq_set_chip_data(irq, NULL);
512}
513
Linus Walleij14250522014-03-25 10:40:18 +0100514static const struct irq_domain_ops gpiochip_domain_ops = {
515 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100516 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100517 /* Virtually all GPIO irqchips are twocell:ed */
518 .xlate = irq_domain_xlate_twocell,
519};
520
521static int gpiochip_irq_reqres(struct irq_data *d)
522{
523 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
524
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900525 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100526 chip_err(chip,
527 "unable to lock HW IRQ %lu for IRQ\n",
528 d->hwirq);
529 return -EINVAL;
530 }
531 return 0;
532}
533
534static void gpiochip_irq_relres(struct irq_data *d)
535{
536 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
537
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900538 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleij14250522014-03-25 10:40:18 +0100539}
540
541static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
542{
543 return irq_find_mapping(chip->irqdomain, offset);
544}
545
546/**
547 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
548 * @gpiochip: the gpiochip to remove the irqchip from
549 *
550 * This is called only from gpiochip_remove()
551 */
552static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
553{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100554 unsigned int offset;
555
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300556 acpi_gpiochip_free_interrupts(gpiochip);
557
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300558 if (gpiochip->irq_parent) {
559 irq_set_chained_handler(gpiochip->irq_parent, NULL);
560 irq_set_handler_data(gpiochip->irq_parent, NULL);
561 }
562
Linus Walleijc3626fd2014-03-28 20:42:01 +0100563 /* Remove all IRQ mappings and delete the domain */
564 if (gpiochip->irqdomain) {
565 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300566 irq_dispose_mapping(
567 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100568 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100569 }
Linus Walleij14250522014-03-25 10:40:18 +0100570
571 if (gpiochip->irqchip) {
572 gpiochip->irqchip->irq_request_resources = NULL;
573 gpiochip->irqchip->irq_release_resources = NULL;
574 gpiochip->irqchip = NULL;
575 }
576}
577
578/**
579 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
580 * @gpiochip: the gpiochip to add the irqchip to
581 * @irqchip: the irqchip to add to the gpiochip
582 * @first_irq: if not dynamically assigned, the base (first) IRQ to
583 * allocate gpiochip irqs from
584 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200585 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
586 * to have the core avoid setting up any default type in the hardware.
Linus Walleij14250522014-03-25 10:40:18 +0100587 *
588 * This function closely associates a certain irqchip with a certain
589 * gpiochip, providing an irq domain to translate the local IRQs to
590 * global irqs in the gpiolib core, and making sure that the gpiochip
591 * is passed as chip data to all related functions. Driver callbacks
592 * need to use container_of() to get their local state containers back
593 * from the gpiochip passed as chip data. An irqdomain will be stored
594 * in the gpiochip that shall be used by the driver to handle IRQ number
595 * translation. The gpiochip will need to be initialized and registered
596 * before calling this function.
597 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100598 * This function will handle two cell:ed simple IRQs and assumes all
599 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100600 * need to be open coded.
601 */
602int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
603 struct irq_chip *irqchip,
604 unsigned int first_irq,
605 irq_flow_handler_t handler,
606 unsigned int type)
607{
608 struct device_node *of_node;
609 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100610 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100611
612 if (!gpiochip || !irqchip)
613 return -EINVAL;
614
615 if (!gpiochip->dev) {
616 pr_err("missing gpiochip .dev parent pointer\n");
617 return -EINVAL;
618 }
619 of_node = gpiochip->dev->of_node;
620#ifdef CONFIG_OF_GPIO
621 /*
Colin Cronin20a8a962015-05-18 11:41:43 -0700622 * If the gpiochip has an assigned OF node this takes precedence
Linus Walleij14250522014-03-25 10:40:18 +0100623 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
624 */
625 if (gpiochip->of_node)
626 of_node = gpiochip->of_node;
627#endif
628 gpiochip->irqchip = irqchip;
629 gpiochip->irq_handler = handler;
630 gpiochip->irq_default_type = type;
631 gpiochip->to_irq = gpiochip_to_irq;
632 gpiochip->irqdomain = irq_domain_add_simple(of_node,
633 gpiochip->ngpio, first_irq,
634 &gpiochip_domain_ops, gpiochip);
635 if (!gpiochip->irqdomain) {
636 gpiochip->irqchip = NULL;
637 return -EINVAL;
638 }
639 irqchip->irq_request_resources = gpiochip_irq_reqres;
640 irqchip->irq_release_resources = gpiochip_irq_relres;
641
642 /*
643 * Prepare the mapping since the irqchip shall be orthogonal to
644 * any gpiochip calls. If the first_irq was zero, this is
645 * necessary to allocate descriptors for all IRQs.
646 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100647 for (offset = 0; offset < gpiochip->ngpio; offset++) {
648 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
649 if (offset == 0)
650 /*
651 * Store the base into the gpiochip to be used when
652 * unmapping the irqs.
653 */
654 gpiochip->irq_base = irq_base;
655 }
Linus Walleij14250522014-03-25 10:40:18 +0100656
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300657 acpi_gpiochip_request_interrupts(gpiochip);
658
Linus Walleij14250522014-03-25 10:40:18 +0100659 return 0;
660}
661EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
662
663#else /* CONFIG_GPIOLIB_IRQCHIP */
664
665static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
666
667#endif /* CONFIG_GPIOLIB_IRQCHIP */
668
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530669#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100670
Linus Walleij3f0f8672012-11-20 12:40:15 +0100671/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200672 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
673 * @chip: the gpiochip to add the range for
674 * @pinctrl: the dev_name() of the pin controller to map to
675 * @gpio_offset: the start offset in the current gpio_chip number space
676 * @pin_group: name of the pin group inside the pin controller
677 */
678int gpiochip_add_pingroup_range(struct gpio_chip *chip,
679 struct pinctrl_dev *pctldev,
680 unsigned int gpio_offset, const char *pin_group)
681{
682 struct gpio_pin_range *pin_range;
683 int ret;
684
685 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
686 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200687 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200688 return -ENOMEM;
689 }
690
691 /* Use local offset as range ID */
692 pin_range->range.id = gpio_offset;
693 pin_range->range.gc = chip;
694 pin_range->range.name = chip->label;
695 pin_range->range.base = chip->base + gpio_offset;
696 pin_range->pctldev = pctldev;
697
698 ret = pinctrl_get_group_pins(pctldev, pin_group,
699 &pin_range->range.pins,
700 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100701 if (ret < 0) {
702 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200703 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100704 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200705
706 pinctrl_add_gpio_range(pctldev, &pin_range->range);
707
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200708 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
709 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200710 pinctrl_dev_get_devname(pctldev), pin_group);
711
712 list_add_tail(&pin_range->node, &chip->pin_ranges);
713
714 return 0;
715}
716EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
717
718/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100719 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
720 * @chip: the gpiochip to add the range for
721 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100722 * @gpio_offset: the start offset in the current gpio_chip number space
723 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100724 * @npins: the number of pins from the offset of each pin space (GPIO and
725 * pin controller) to accumulate in this range
726 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100727int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100728 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100729 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530730{
731 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800732 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530733
Linus Walleij3f0f8672012-11-20 12:40:15 +0100734 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530735 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200736 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100737 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530738 }
739
Linus Walleij3f0f8672012-11-20 12:40:15 +0100740 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100741 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100742 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530743 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100744 pin_range->range.base = chip->base + gpio_offset;
745 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530746 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100747 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530748 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100749 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800750 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200751 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100752 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800753 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100754 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200755 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
756 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100757 pinctl_name,
758 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530759
760 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100761
762 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530763}
Linus Walleij165adc92012-11-06 14:49:39 +0100764EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530765
Linus Walleij3f0f8672012-11-20 12:40:15 +0100766/**
767 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
768 * @chip: the chip to remove all the mappings for
769 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530770void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
771{
772 struct gpio_pin_range *pin_range, *tmp;
773
774 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
775 list_del(&pin_range->node);
776 pinctrl_remove_gpio_range(pin_range->pctldev,
777 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100778 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530779 }
780}
Linus Walleij165adc92012-11-06 14:49:39 +0100781EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
782
783#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530784
David Brownelld2876d02008-02-04 22:28:20 -0800785/* These "optional" allocation calls help prevent drivers from stomping
786 * on each other, and help provide better diagnostics in debugfs.
787 * They're called even less than the "set direction" calls.
788 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200789static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800790{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200791 struct gpio_chip *chip = desc->chip;
792 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800793 unsigned long flags;
794
795 spin_lock_irqsave(&gpio_lock, flags);
796
David Brownelld2876d02008-02-04 22:28:20 -0800797 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700798 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800799 */
800
801 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
802 desc_set_label(desc, label ? : "?");
803 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700804 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800805 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800806 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700807 }
808
809 if (chip->request) {
810 /* chip->request may sleep */
811 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900812 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700813 spin_lock_irqsave(&gpio_lock, flags);
814
815 if (status < 0) {
816 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700817 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300818 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700819 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700820 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300821 if (chip->get_direction) {
822 /* chip->get_direction may sleep */
823 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900824 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300825 spin_lock_irqsave(&gpio_lock, flags);
826 }
David Brownelld2876d02008-02-04 22:28:20 -0800827done:
Mika Westerberg77c2d792014-03-10 14:54:50 +0200828 spin_unlock_irqrestore(&gpio_lock, flags);
829 return status;
830}
831
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900832int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200833{
834 int status = -EPROBE_DEFER;
835 struct gpio_chip *chip;
836
837 if (!desc) {
838 pr_warn("%s: invalid GPIO\n", __func__);
839 return -EINVAL;
840 }
841
842 chip = desc->chip;
843 if (!chip)
844 goto done;
845
846 if (try_module_get(chip->owner)) {
847 status = __gpiod_request(desc, label);
848 if (status < 0)
849 module_put(chip->owner);
850 }
851
852done:
David Brownelld2876d02008-02-04 22:28:20 -0800853 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200854 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200855
David Brownelld2876d02008-02-04 22:28:20 -0800856 return status;
857}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900858
Mika Westerberg77c2d792014-03-10 14:54:50 +0200859static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800860{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200861 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -0800862 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -0700863 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800864
Uwe Kleine-König3d599d12008-10-15 22:03:12 -0700865 might_sleep();
866
Alexandre Courbot372e7222013-02-03 01:29:29 +0900867 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700868
David Brownelld2876d02008-02-04 22:28:20 -0800869 spin_lock_irqsave(&gpio_lock, flags);
870
David Brownell35e8bb52008-10-15 22:03:16 -0700871 chip = desc->chip;
872 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
873 if (chip->free) {
874 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -0700875 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900876 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700877 spin_lock_irqsave(&gpio_lock, flags);
878 }
David Brownelld2876d02008-02-04 22:28:20 -0800879 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -0800880 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -0700881 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530882 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +0530883 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -0600884 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200885 ret = true;
886 }
David Brownelld2876d02008-02-04 22:28:20 -0800887
888 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200889 return ret;
890}
891
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900892void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200893{
894 if (desc && __gpiod_free(desc))
895 module_put(desc->chip->owner);
896 else
897 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -0800898}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900899
David Brownelld2876d02008-02-04 22:28:20 -0800900/**
901 * gpiochip_is_requested - return string iff signal was requested
902 * @chip: controller managing the signal
903 * @offset: of signal within controller's 0..(ngpio - 1) range
904 *
905 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +0900906 * The string returned is the label passed to gpio_request(); if none has been
907 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -0800908 *
909 * This function is for use by GPIO controller drivers. The label can
910 * help with diagnostics, and knowing that the signal is used as a GPIO
911 * can help avoid accidentally multiplexing it to another controller.
912 */
913const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
914{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900915 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800916
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900917 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -0800918 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900919
920 desc = &chip->desc[offset];
921
Alexandre Courbot372e7222013-02-03 01:29:29 +0900922 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -0800923 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900924 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -0800925}
926EXPORT_SYMBOL_GPL(gpiochip_is_requested);
927
Mika Westerberg77c2d792014-03-10 14:54:50 +0200928/**
929 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
930 * @desc: GPIO descriptor to request
931 * @label: label for the GPIO
932 *
933 * Function allows GPIO chip drivers to request and use their own GPIO
934 * descriptors via gpiolib API. Difference to gpiod_request() is that this
935 * function will not increase reference count of the GPIO chip module. This
936 * allows the GPIO chip module to be unloaded as needed (we assume that the
937 * GPIO chip driver handles freeing the GPIOs it has requested).
938 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700939struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
940 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200941{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700942 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
943 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200944
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700945 if (IS_ERR(desc)) {
946 chip_err(chip, "failed to get GPIO descriptor\n");
947 return desc;
948 }
949
950 err = __gpiod_request(desc, label);
951 if (err < 0)
952 return ERR_PTR(err);
953
954 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200955}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700956EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200957
958/**
959 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
960 * @desc: GPIO descriptor to free
961 *
962 * Function frees the given GPIO requested previously with
963 * gpiochip_request_own_desc().
964 */
965void gpiochip_free_own_desc(struct gpio_desc *desc)
966{
967 if (desc)
968 __gpiod_free(desc);
969}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700970EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -0800971
972/* Drivers MUST set GPIO direction before making get/set calls. In
973 * some cases this is done in early boot, before IRQs are enabled.
974 *
975 * As a rule these aren't called more than once (except for drivers
976 * using the open-drain emulation idiom) so these are natural places
977 * to accumulate extra debugging checks. Note that we can't (yet)
978 * rely on gpio_request() having been called beforehand.
979 */
980
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700981/**
982 * gpiod_direction_input - set the GPIO direction to input
983 * @desc: GPIO to set to input
984 *
985 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
986 * be called safely on it.
987 *
988 * Return 0 in case of success, else an error code.
989 */
990int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800991{
David Brownelld2876d02008-02-04 22:28:20 -0800992 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800993 int status = -EINVAL;
994
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200995 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900996 pr_warn("%s: invalid GPIO\n", __func__);
997 return -EINVAL;
998 }
999
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001000 chip = desc->chip;
1001 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001002 gpiod_warn(desc,
1003 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001004 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001005 return -EIO;
1006 }
1007
Alexandre Courbotd82da792014-07-22 16:17:43 +09001008 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001009 if (status == 0)
1010 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001011
Alexandre Courbot372e7222013-02-03 01:29:29 +09001012 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001013
David Brownelld2876d02008-02-04 22:28:20 -08001014 return status;
1015}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001016EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001017
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001018static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001019{
David Brownelld2876d02008-02-04 22:28:20 -08001020 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001021 int status = -EINVAL;
1022
Linus Walleijd468bf92013-09-24 11:54:38 +02001023 /* GPIOs used for IRQs shall not be set as output */
1024 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1025 gpiod_err(desc,
1026 "%s: tried to set a GPIO tied to an IRQ as output\n",
1027 __func__);
1028 return -EIO;
1029 }
1030
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301031 /* Open drain pin should not be driven to 1 */
1032 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001033 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301034
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301035 /* Open source pin should not be driven to 0 */
1036 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001037 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301038
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001039 chip = desc->chip;
1040 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001041 gpiod_warn(desc,
1042 "%s: missing set() or direction_output() operations\n",
1043 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001044 return -EIO;
1045 }
1046
Alexandre Courbotd82da792014-07-22 16:17:43 +09001047 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001048 if (status == 0)
1049 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001050 trace_gpio_value(desc_to_gpio(desc), 0, value);
1051 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001052 return status;
1053}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001054
1055/**
1056 * gpiod_direction_output_raw - set the GPIO direction to output
1057 * @desc: GPIO to set to output
1058 * @value: initial output value of the GPIO
1059 *
1060 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1061 * be called safely on it. The initial value of the output must be specified
1062 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1063 *
1064 * Return 0 in case of success, else an error code.
1065 */
1066int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1067{
1068 if (!desc || !desc->chip) {
1069 pr_warn("%s: invalid GPIO\n", __func__);
1070 return -EINVAL;
1071 }
1072 return _gpiod_direction_output_raw(desc, value);
1073}
1074EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1075
1076/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301077 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001078 * @desc: GPIO to set to output
1079 * @value: initial output value of the GPIO
1080 *
1081 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1082 * be called safely on it. The initial value of the output must be specified
1083 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1084 * account.
1085 *
1086 * Return 0 in case of success, else an error code.
1087 */
1088int gpiod_direction_output(struct gpio_desc *desc, int value)
1089{
1090 if (!desc || !desc->chip) {
1091 pr_warn("%s: invalid GPIO\n", __func__);
1092 return -EINVAL;
1093 }
1094 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1095 value = !value;
1096 return _gpiod_direction_output_raw(desc, value);
1097}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001098EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001099
Felipe Balbic4b5be92010-05-26 14:42:23 -07001100/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001101 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001102 * @gpio: the gpio to set debounce time
1103 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001104 *
1105 * returns -ENOTSUPP if the controller does not support setting
1106 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001107 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001108int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001109{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001110 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001111
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001112 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001113 pr_warn("%s: invalid GPIO\n", __func__);
1114 return -EINVAL;
1115 }
1116
Felipe Balbic4b5be92010-05-26 14:42:23 -07001117 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001118 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001119 gpiod_dbg(desc,
1120 "%s: missing set() or set_debounce() operations\n",
1121 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001122 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001123 }
1124
Alexandre Courbotd82da792014-07-22 16:17:43 +09001125 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001126}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001127EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001128
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001129/**
1130 * gpiod_is_active_low - test whether a GPIO is active-low or not
1131 * @desc: the gpio descriptor to test
1132 *
1133 * Returns 1 if the GPIO is active-low, 0 otherwise.
1134 */
1135int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001136{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001137 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001138}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001139EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001140
1141/* I/O calls are only valid after configuration completed; the relevant
1142 * "is this a valid GPIO" error checks should already have been done.
1143 *
1144 * "Get" operations are often inlinable as reading a pin value register,
1145 * and masking the relevant bit in that register.
1146 *
1147 * When "set" operations are inlinable, they involve writing that mask to
1148 * one register to set a low value, or a different register to set it high.
1149 * Otherwise locking is needed, so there may be little value to inlining.
1150 *
1151 *------------------------------------------------------------------------
1152 *
1153 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1154 * have requested the GPIO. That can include implicit requesting by
1155 * a direction setting call. Marking a gpio as requested locks its chip
1156 * in memory, guaranteeing that these table lookups need no more locking
1157 * and that gpiochip_remove() will fail.
1158 *
1159 * REVISIT when debugging, consider adding some instrumentation to ensure
1160 * that the GPIO was actually requested.
1161 */
1162
Alexandre Courbot23600962014-03-11 15:52:09 +09001163static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001164{
1165 struct gpio_chip *chip;
Alexandre Courbot23600962014-03-11 15:52:09 +09001166 bool value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001167 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001168
Alexandre Courbot372e7222013-02-03 01:29:29 +09001169 chip = desc->chip;
1170 offset = gpio_chip_hwgpio(desc);
Alexandre Courbot23600962014-03-11 15:52:09 +09001171 value = chip->get ? chip->get(chip, offset) : false;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001172 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001173 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001174}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001175
David Brownelld2876d02008-02-04 22:28:20 -08001176/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001177 * gpiod_get_raw_value() - return a gpio's raw value
1178 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001179 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001180 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1181 * its ACTIVE_LOW status.
1182 *
1183 * This function should be called from contexts where we cannot sleep, and will
1184 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001185 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001186int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001187{
David Brownelld2876d02008-02-04 22:28:20 -08001188 if (!desc)
1189 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001190 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001191 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001192 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001193}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001194EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001195
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001196/**
1197 * gpiod_get_value() - return a gpio's value
1198 * @desc: gpio whose value will be returned
1199 *
1200 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1201 * account.
1202 *
1203 * This function should be called from contexts where we cannot sleep, and will
1204 * complain if the GPIO chip functions potentially sleep.
1205 */
1206int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001207{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001208 int value;
1209 if (!desc)
1210 return 0;
1211 /* Should be using gpio_get_value_cansleep() */
1212 WARN_ON(desc->chip->can_sleep);
1213
1214 value = _gpiod_get_raw_value(desc);
1215 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1216 value = !value;
1217
1218 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001219}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001220EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001221
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301222/*
1223 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001224 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001225 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301226 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001227static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301228{
1229 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001230 struct gpio_chip *chip = desc->chip;
1231 int offset = gpio_chip_hwgpio(desc);
1232
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301233 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001234 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301235 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001236 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301237 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001238 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301239 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001240 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301241 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001242 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301243 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001244 gpiod_err(desc,
1245 "%s: Error in set_value for open drain err %d\n",
1246 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301247}
1248
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301249/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001250 * _gpio_set_open_source_value() - Set the open source gpio's value.
1251 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001252 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301253 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001254static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301255{
1256 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001257 struct gpio_chip *chip = desc->chip;
1258 int offset = gpio_chip_hwgpio(desc);
1259
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301260 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001261 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301262 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001263 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301264 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001265 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301266 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001267 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301268 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001269 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301270 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001271 gpiod_err(desc,
1272 "%s: Error in set_value for open source err %d\n",
1273 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301274}
1275
Alexandre Courbot23600962014-03-11 15:52:09 +09001276static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001277{
1278 struct gpio_chip *chip;
1279
Alexandre Courbot372e7222013-02-03 01:29:29 +09001280 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001281 trace_gpio_value(desc_to_gpio(desc), 0, value);
1282 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1283 _gpio_set_open_drain_value(desc, value);
1284 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1285 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301286 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001287 chip->set(chip, gpio_chip_hwgpio(desc), value);
1288}
1289
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001290/*
1291 * set multiple outputs on the same chip;
1292 * use the chip's set_multiple function if available;
1293 * otherwise set the outputs sequentially;
1294 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1295 * defines which outputs are to be changed
1296 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1297 * defines the values the outputs specified by mask are to be set to
1298 */
1299static void gpio_chip_set_multiple(struct gpio_chip *chip,
1300 unsigned long *mask, unsigned long *bits)
1301{
1302 if (chip->set_multiple) {
1303 chip->set_multiple(chip, mask, bits);
1304 } else {
1305 int i;
1306 for (i = 0; i < chip->ngpio; i++) {
1307 if (mask[BIT_WORD(i)] == 0) {
1308 /* no more set bits in this mask word;
1309 * skip ahead to the next word */
1310 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1311 continue;
1312 }
1313 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001314 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001315 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001316 }
1317 }
1318}
1319
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001320static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1321 unsigned int array_size,
1322 struct gpio_desc **desc_array,
1323 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001324{
1325 int i = 0;
1326
1327 while (i < array_size) {
1328 struct gpio_chip *chip = desc_array[i]->chip;
1329 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1330 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1331 int count = 0;
1332
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001333 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001334 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001335
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001336 memset(mask, 0, sizeof(mask));
1337 do {
1338 struct gpio_desc *desc = desc_array[i];
1339 int hwgpio = gpio_chip_hwgpio(desc);
1340 int value = value_array[i];
1341
1342 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1343 value = !value;
1344 trace_gpio_value(desc_to_gpio(desc), 0, value);
1345 /*
1346 * collect all normal outputs belonging to the same chip
1347 * open drain and open source outputs are set individually
1348 */
1349 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001350 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001351 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1352 _gpio_set_open_source_value(desc, value);
1353 } else {
1354 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001355 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001356 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001357 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001358 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001359 count++;
1360 }
1361 i++;
1362 } while ((i < array_size) && (desc_array[i]->chip == chip));
1363 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001364 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001365 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001366 }
1367}
1368
David Brownelld2876d02008-02-04 22:28:20 -08001369/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001370 * gpiod_set_raw_value() - assign a gpio's raw value
1371 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001372 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001373 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001374 * Set the raw value of the GPIO, i.e. the value of its physical line without
1375 * regard for its ACTIVE_LOW status.
1376 *
1377 * This function should be called from contexts where we cannot sleep, and will
1378 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001379 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001380void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001381{
David Brownelld2876d02008-02-04 22:28:20 -08001382 if (!desc)
1383 return;
David Brownelld2876d02008-02-04 22:28:20 -08001384 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001385 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001386 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001387}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001388EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001389
1390/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001391 * gpiod_set_value() - assign a gpio's value
1392 * @desc: gpio whose value will be assigned
1393 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001394 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001395 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1396 * account
1397 *
1398 * This function should be called from contexts where we cannot sleep, and will
1399 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001400 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001401void gpiod_set_value(struct gpio_desc *desc, int value)
1402{
1403 if (!desc)
1404 return;
1405 /* Should be using gpio_set_value_cansleep() */
1406 WARN_ON(desc->chip->can_sleep);
1407 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1408 value = !value;
1409 _gpiod_set_raw_value(desc, value);
1410}
1411EXPORT_SYMBOL_GPL(gpiod_set_value);
1412
1413/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001414 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001415 * @array_size: number of elements in the descriptor / value arrays
1416 * @desc_array: array of GPIO descriptors whose values will be assigned
1417 * @value_array: array of values to assign
1418 *
1419 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1420 * without regard for their ACTIVE_LOW status.
1421 *
1422 * This function should be called from contexts where we cannot sleep, and will
1423 * complain if the GPIO chip functions potentially sleep.
1424 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001425void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001426 struct gpio_desc **desc_array, int *value_array)
1427{
1428 if (!desc_array)
1429 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001430 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1431 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001432}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001433EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001434
1435/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001436 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001437 * @array_size: number of elements in the descriptor / value arrays
1438 * @desc_array: array of GPIO descriptors whose values will be assigned
1439 * @value_array: array of values to assign
1440 *
1441 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1442 * into account.
1443 *
1444 * This function should be called from contexts where we cannot sleep, and will
1445 * complain if the GPIO chip functions potentially sleep.
1446 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001447void gpiod_set_array_value(unsigned int array_size,
1448 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001449{
1450 if (!desc_array)
1451 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001452 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1453 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001454}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001455EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001456
1457/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001458 * gpiod_cansleep() - report whether gpio value access may sleep
1459 * @desc: gpio to check
1460 *
1461 */
1462int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001463{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001464 if (!desc)
1465 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001466 return desc->chip->can_sleep;
1467}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001468EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001469
David Brownell0f6d5042008-10-15 22:03:14 -07001470/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001471 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1472 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001473 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001474 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1475 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001476 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001477int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001478{
1479 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001480 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001481
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001482 if (!desc)
1483 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001484 chip = desc->chip;
1485 offset = gpio_chip_hwgpio(desc);
1486 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1487}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001488EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001489
Linus Walleijd468bf92013-09-24 11:54:38 +02001490/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001491 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001492 * @chip: the chip the GPIO to lock belongs to
1493 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001494 *
1495 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001496 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001497 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001498int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001499{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001500 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001501 return -EINVAL;
1502
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001503 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1504 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001505 "%s: tried to flag a GPIO set as output for IRQ\n",
1506 __func__);
1507 return -EIO;
1508 }
1509
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001510 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001511 return 0;
1512}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001513EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001514
Linus Walleijd468bf92013-09-24 11:54:38 +02001515/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001516 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001517 * @chip: the chip the GPIO to lock belongs to
1518 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001519 *
1520 * This is used directly by GPIO drivers that want to indicate
1521 * that a certain GPIO is no longer used exclusively for IRQ.
1522 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001523void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001524{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001525 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001526 return;
1527
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001528 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001529}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001530EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001531
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001532/**
1533 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1534 * @desc: gpio whose value will be returned
1535 *
1536 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1537 * its ACTIVE_LOW status.
1538 *
1539 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001540 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001541int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001542{
David Brownelld2876d02008-02-04 22:28:20 -08001543 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001544 if (!desc)
1545 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001546 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001547}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001548EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001549
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001550/**
1551 * gpiod_get_value_cansleep() - return a gpio's value
1552 * @desc: gpio whose value will be returned
1553 *
1554 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1555 * account.
1556 *
1557 * This function is to be called from contexts that can sleep.
1558 */
1559int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001560{
David Brownelld2876d02008-02-04 22:28:20 -08001561 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001562
1563 might_sleep_if(extra_checks);
1564 if (!desc)
1565 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001566
1567 value = _gpiod_get_raw_value(desc);
1568 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1569 value = !value;
1570
David Brownelld2876d02008-02-04 22:28:20 -08001571 return value;
1572}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001573EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001574
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001575/**
1576 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1577 * @desc: gpio whose value will be assigned
1578 * @value: value to assign
1579 *
1580 * Set the raw value of the GPIO, i.e. the value of its physical line without
1581 * regard for its ACTIVE_LOW status.
1582 *
1583 * This function is to be called from contexts that can sleep.
1584 */
1585void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001586{
David Brownelld2876d02008-02-04 22:28:20 -08001587 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001588 if (!desc)
1589 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001590 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001591}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001592EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001593
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001594/**
1595 * gpiod_set_value_cansleep() - assign a gpio's value
1596 * @desc: gpio whose value will be assigned
1597 * @value: value to assign
1598 *
1599 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1600 * account
1601 *
1602 * This function is to be called from contexts that can sleep.
1603 */
1604void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001605{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001606 might_sleep_if(extra_checks);
1607 if (!desc)
1608 return;
1609
1610 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1611 value = !value;
1612 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001613}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001614EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001615
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001616/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001617 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001618 * @array_size: number of elements in the descriptor / value arrays
1619 * @desc_array: array of GPIO descriptors whose values will be assigned
1620 * @value_array: array of values to assign
1621 *
1622 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1623 * without regard for their ACTIVE_LOW status.
1624 *
1625 * This function is to be called from contexts that can sleep.
1626 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001627void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1628 struct gpio_desc **desc_array,
1629 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001630{
1631 might_sleep_if(extra_checks);
1632 if (!desc_array)
1633 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001634 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1635 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001636}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001637EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001638
1639/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001640 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001641 * @array_size: number of elements in the descriptor / value arrays
1642 * @desc_array: array of GPIO descriptors whose values will be assigned
1643 * @value_array: array of values to assign
1644 *
1645 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1646 * into account.
1647 *
1648 * This function is to be called from contexts that can sleep.
1649 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001650void gpiod_set_array_value_cansleep(unsigned int array_size,
1651 struct gpio_desc **desc_array,
1652 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001653{
1654 might_sleep_if(extra_checks);
1655 if (!desc_array)
1656 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001657 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1658 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001659}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001660EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001661
1662/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001663 * gpiod_add_lookup_table() - register GPIO device consumers
1664 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001665 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001666void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001667{
1668 mutex_lock(&gpio_lookup_lock);
1669
Alexandre Courbotad824782013-12-03 12:20:11 +09001670 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001671
1672 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001673}
1674
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05301675/**
1676 * gpiod_remove_lookup_table() - unregister GPIO device consumers
1677 * @table: table of consumers to unregister
1678 */
1679void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
1680{
1681 mutex_lock(&gpio_lookup_lock);
1682
1683 list_del(&table->list);
1684
1685 mutex_unlock(&gpio_lookup_lock);
1686}
1687
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001688static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001689 unsigned int idx,
1690 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001691{
1692 char prop_name[32]; /* 32 is max size of property name */
1693 enum of_gpio_flags of_flags;
1694 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001695 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001696
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001697 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02001698 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01001699 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001700 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02001701 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01001702 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001703 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001704
Thierry Redingdd34c372014-04-23 17:28:09 +02001705 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1706 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001707 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001708 break;
1709 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001710
1711 if (IS_ERR(desc))
1712 return desc;
1713
1714 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001715 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001716
1717 return desc;
1718}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001719
Mika Westerberg81f59e92013-10-10 11:01:09 +03001720static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001721 unsigned int idx,
1722 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001723{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001724 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03001725 struct acpi_gpio_info info;
1726 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001727 char propname[32];
1728 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03001729
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001730 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001731 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001732 if (con_id && strcmp(con_id, "gpios")) {
1733 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001734 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001735 } else {
1736 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001737 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001738 }
Mika Westerberge01f4402013-10-10 11:01:10 +03001739
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001740 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1741 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1742 break;
1743 }
1744
1745 /* Then from plain _CRS GPIOs */
1746 if (IS_ERR(desc)) {
1747 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1748 if (IS_ERR(desc))
1749 return desc;
1750 }
1751
1752 if (info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001753 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001754
1755 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001756}
1757
Alexandre Courbotad824782013-12-03 12:20:11 +09001758static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1759{
1760 const char *dev_id = dev ? dev_name(dev) : NULL;
1761 struct gpiod_lookup_table *table;
1762
1763 mutex_lock(&gpio_lookup_lock);
1764
1765 list_for_each_entry(table, &gpio_lookup_list, list) {
1766 if (table->dev_id && dev_id) {
1767 /*
1768 * Valid strings on both ends, must be identical to have
1769 * a match
1770 */
1771 if (!strcmp(table->dev_id, dev_id))
1772 goto found;
1773 } else {
1774 /*
1775 * One of the pointers is NULL, so both must be to have
1776 * a match
1777 */
1778 if (dev_id == table->dev_id)
1779 goto found;
1780 }
1781 }
1782 table = NULL;
1783
1784found:
1785 mutex_unlock(&gpio_lookup_lock);
1786 return table;
1787}
1788
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001789static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001790 unsigned int idx,
1791 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001792{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001793 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001794 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001795 struct gpiod_lookup *p;
1796
Alexandre Courbotad824782013-12-03 12:20:11 +09001797 table = gpiod_find_lookup_table(dev);
1798 if (!table)
1799 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001800
Alexandre Courbotad824782013-12-03 12:20:11 +09001801 for (p = &table->table[0]; p->chip_label; p++) {
1802 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001803
Alexandre Courbotad824782013-12-03 12:20:11 +09001804 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001805 if (p->idx != idx)
1806 continue;
1807
Alexandre Courbotad824782013-12-03 12:20:11 +09001808 /* If the lookup entry has a con_id, require exact match */
1809 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1810 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001811
Alexandre Courbotad824782013-12-03 12:20:11 +09001812 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001813
Alexandre Courbotad824782013-12-03 12:20:11 +09001814 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001815 dev_err(dev, "cannot find GPIO chip %s\n",
1816 p->chip_label);
1817 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001818 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001819
Alexandre Courbotad824782013-12-03 12:20:11 +09001820 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001821 dev_err(dev,
1822 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1823 idx, chip->ngpio, chip->label);
1824 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001825 }
1826
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001827 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001828 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001829
1830 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001831 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001832
1833 return desc;
1834}
1835
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001836static int dt_gpio_count(struct device *dev, const char *con_id)
1837{
1838 int ret;
1839 char propname[32];
1840 unsigned int i;
1841
1842 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1843 if (con_id)
1844 snprintf(propname, sizeof(propname), "%s-%s",
1845 con_id, gpio_suffixes[i]);
1846 else
1847 snprintf(propname, sizeof(propname), "%s",
1848 gpio_suffixes[i]);
1849
1850 ret = of_gpio_named_count(dev->of_node, propname);
1851 if (ret >= 0)
1852 break;
1853 }
1854 return ret;
1855}
1856
1857static int platform_gpio_count(struct device *dev, const char *con_id)
1858{
1859 struct gpiod_lookup_table *table;
1860 struct gpiod_lookup *p;
1861 unsigned int count = 0;
1862
1863 table = gpiod_find_lookup_table(dev);
1864 if (!table)
1865 return -ENOENT;
1866
1867 for (p = &table->table[0]; p->chip_label; p++) {
1868 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
1869 (!con_id && !p->con_id))
1870 count++;
1871 }
1872 if (!count)
1873 return -ENOENT;
1874
1875 return count;
1876}
1877
1878/**
1879 * gpiod_count - return the number of GPIOs associated with a device / function
1880 * or -ENOENT if no GPIO has been assigned to the requested function
1881 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1882 * @con_id: function within the GPIO consumer
1883 */
1884int gpiod_count(struct device *dev, const char *con_id)
1885{
1886 int count = -ENOENT;
1887
1888 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
1889 count = dt_gpio_count(dev, con_id);
1890 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
1891 count = acpi_gpio_count(dev, con_id);
1892
1893 if (count < 0)
1894 count = platform_gpio_count(dev, con_id);
1895
1896 return count;
1897}
1898EXPORT_SYMBOL_GPL(gpiod_count);
1899
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001900/**
Thierry Reding08791622014-04-25 16:54:22 +02001901 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09001902 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001903 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001904 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001905 *
1906 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001907 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07001908 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001909 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001910struct gpio_desc *__must_check __gpiod_get(struct device *dev, const char *con_id,
1911 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001912{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001913 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001914}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001915EXPORT_SYMBOL_GPL(__gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001916
1917/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02001918 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1919 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1920 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001921 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02001922 *
1923 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1924 * the requested function it will return NULL. This is convenient for drivers
1925 * that need to handle optional GPIOs.
1926 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001927struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
1928 const char *con_id,
1929 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02001930{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001931 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001932}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001933EXPORT_SYMBOL_GPL(__gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001934
Benoit Parrotf625d462015-02-02 11:44:44 -06001935
1936/**
1937 * gpiod_configure_flags - helper function to configure a given GPIO
1938 * @desc: gpio whose value will be assigned
1939 * @con_id: function within the GPIO consumer
1940 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
1941 * of_get_gpio_hog()
1942 * @dflags: gpiod_flags - optional GPIO initialization flags
1943 *
1944 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
1945 * requested function and/or index, or another IS_ERR() code if an error
1946 * occurred while trying to acquire the GPIO.
1947 */
1948static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
1949 unsigned long lflags, enum gpiod_flags dflags)
1950{
1951 int status;
1952
1953 if (lflags & GPIO_ACTIVE_LOW)
1954 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
1955 if (lflags & GPIO_OPEN_DRAIN)
1956 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1957 if (lflags & GPIO_OPEN_SOURCE)
1958 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
1959
1960 /* No particular flag request, return here... */
1961 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
1962 pr_debug("no flags found for %s\n", con_id);
1963 return 0;
1964 }
1965
1966 /* Process flags */
1967 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
1968 status = gpiod_direction_output(desc,
1969 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
1970 else
1971 status = gpiod_direction_input(desc);
1972
1973 return status;
1974}
1975
Thierry Reding29a1f2332014-04-25 17:10:06 +02001976/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001977 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02001978 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001979 * @con_id: function within the GPIO consumer
1980 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001981 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001982 *
1983 * This variant of gpiod_get() allows to access GPIOs other than the first
1984 * defined one for functions that define several GPIOs.
1985 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001986 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1987 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07001988 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001989 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001990struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001991 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001992 unsigned int idx,
1993 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001994{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001995 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001996 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001997 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001998
1999 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2000
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002001 if (dev) {
2002 /* Using device tree? */
2003 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2004 dev_dbg(dev, "using device tree for GPIO lookup\n");
2005 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2006 } else if (ACPI_COMPANION(dev)) {
2007 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2008 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2009 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002010 }
2011
2012 /*
2013 * Either we are not using DT or ACPI, or their lookup did not return
2014 * a result. In that case, use platform lookup as a fallback.
2015 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002016 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002017 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002018 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002019 }
2020
2021 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002022 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002023 return desc;
2024 }
2025
2026 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002027 if (status < 0)
2028 return ERR_PTR(status);
2029
Benoit Parrotf625d462015-02-02 11:44:44 -06002030 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002031 if (status < 0) {
2032 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2033 gpiod_put(desc);
2034 return ERR_PTR(status);
2035 }
2036
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002037 return desc;
2038}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002039EXPORT_SYMBOL_GPL(__gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002040
2041/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002042 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2043 * @fwnode: handle of the firmware node
2044 * @propname: name of the firmware property representing the GPIO
2045 *
2046 * This function can be used for drivers that get their configuration
2047 * from firmware.
2048 *
2049 * Function properly finds the corresponding GPIO using whatever is the
2050 * underlying firmware interface and then makes sure that the GPIO
2051 * descriptor is requested before it is returned to the caller.
2052 *
2053 * In case of error an ERR_PTR() is returned.
2054 */
2055struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2056 const char *propname)
2057{
2058 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2059 bool active_low = false;
2060 int ret;
2061
2062 if (!fwnode)
2063 return ERR_PTR(-EINVAL);
2064
2065 if (is_of_node(fwnode)) {
2066 enum of_gpio_flags flags;
2067
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002068 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002069 &flags);
2070 if (!IS_ERR(desc))
2071 active_low = flags & OF_GPIO_ACTIVE_LOW;
2072 } else if (is_acpi_node(fwnode)) {
2073 struct acpi_gpio_info info;
2074
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002075 desc = acpi_get_gpiod_by_index(to_acpi_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002076 &info);
2077 if (!IS_ERR(desc))
2078 active_low = info.active_low;
2079 }
2080
2081 if (IS_ERR(desc))
2082 return desc;
2083
2084 ret = gpiod_request(desc, NULL);
2085 if (ret)
2086 return ERR_PTR(ret);
2087
2088 /* Only value flag can be set from both DT and ACPI is active_low */
2089 if (active_low)
2090 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2091
2092 return desc;
2093}
2094EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2095
2096/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002097 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2098 * function
2099 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2100 * @con_id: function within the GPIO consumer
2101 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002102 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002103 *
2104 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2105 * specified index was assigned to the requested function it will return NULL.
2106 * This is convenient for drivers that need to handle optional GPIOs.
2107 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002108struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002109 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002110 unsigned int index,
2111 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002112{
2113 struct gpio_desc *desc;
2114
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002115 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002116 if (IS_ERR(desc)) {
2117 if (PTR_ERR(desc) == -ENOENT)
2118 return NULL;
2119 }
2120
2121 return desc;
2122}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002123EXPORT_SYMBOL_GPL(__gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002124
2125/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002126 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2127 * @desc: gpio whose value will be assigned
2128 * @name: gpio line name
2129 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2130 * of_get_gpio_hog()
2131 * @dflags: gpiod_flags - optional GPIO initialization flags
2132 */
2133int gpiod_hog(struct gpio_desc *desc, const char *name,
2134 unsigned long lflags, enum gpiod_flags dflags)
2135{
2136 struct gpio_chip *chip;
2137 struct gpio_desc *local_desc;
2138 int hwnum;
2139 int status;
2140
2141 chip = gpiod_to_chip(desc);
2142 hwnum = gpio_chip_hwgpio(desc);
2143
2144 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2145 if (IS_ERR(local_desc)) {
Linus Walleija7138902015-06-05 11:36:10 +02002146 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2147 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002148 return PTR_ERR(local_desc);
2149 }
2150
2151 status = gpiod_configure_flags(desc, name, lflags, dflags);
2152 if (status < 0) {
Linus Walleija7138902015-06-05 11:36:10 +02002153 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2154 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002155 gpiochip_free_own_desc(desc);
2156 return status;
2157 }
2158
2159 /* Mark GPIO as hogged so it can be identified and removed later */
2160 set_bit(FLAG_IS_HOGGED, &desc->flags);
2161
2162 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2163 desc_to_gpio(desc), name,
2164 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2165 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2166 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2167
2168 return 0;
2169}
2170
2171/**
2172 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2173 * @chip: gpio chip to act on
2174 *
2175 * This is only used by of_gpiochip_remove to free hogged gpios
2176 */
2177static void gpiochip_free_hogs(struct gpio_chip *chip)
2178{
2179 int id;
2180
2181 for (id = 0; id < chip->ngpio; id++) {
2182 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2183 gpiochip_free_own_desc(&chip->desc[id]);
2184 }
2185}
2186
2187/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002188 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2189 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2190 * @con_id: function within the GPIO consumer
2191 * @flags: optional GPIO initialization flags
2192 *
2193 * This function acquires all the GPIOs defined under a given function.
2194 *
2195 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2196 * no GPIO has been assigned to the requested function, or another IS_ERR()
2197 * code if an error occurred while trying to acquire the GPIOs.
2198 */
2199struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2200 const char *con_id,
2201 enum gpiod_flags flags)
2202{
2203 struct gpio_desc *desc;
2204 struct gpio_descs *descs;
2205 int count;
2206
2207 count = gpiod_count(dev, con_id);
2208 if (count < 0)
2209 return ERR_PTR(count);
2210
2211 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2212 GFP_KERNEL);
2213 if (!descs)
2214 return ERR_PTR(-ENOMEM);
2215
2216 for (descs->ndescs = 0; descs->ndescs < count; ) {
2217 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2218 if (IS_ERR(desc)) {
2219 gpiod_put_array(descs);
2220 return ERR_CAST(desc);
2221 }
2222 descs->desc[descs->ndescs] = desc;
2223 descs->ndescs++;
2224 }
2225 return descs;
2226}
2227EXPORT_SYMBOL_GPL(gpiod_get_array);
2228
2229/**
2230 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2231 * function
2232 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2233 * @con_id: function within the GPIO consumer
2234 * @flags: optional GPIO initialization flags
2235 *
2236 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2237 * assigned to the requested function it will return NULL.
2238 */
2239struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2240 const char *con_id,
2241 enum gpiod_flags flags)
2242{
2243 struct gpio_descs *descs;
2244
2245 descs = gpiod_get_array(dev, con_id, flags);
2246 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2247 return NULL;
2248
2249 return descs;
2250}
2251EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2252
2253/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002254 * gpiod_put - dispose of a GPIO descriptor
2255 * @desc: GPIO descriptor to dispose of
2256 *
2257 * No descriptor can be used after gpiod_put() has been called on it.
2258 */
2259void gpiod_put(struct gpio_desc *desc)
2260{
2261 gpiod_free(desc);
2262}
2263EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002264
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002265/**
2266 * gpiod_put_array - dispose of multiple GPIO descriptors
2267 * @descs: struct gpio_descs containing an array of descriptors
2268 */
2269void gpiod_put_array(struct gpio_descs *descs)
2270{
2271 unsigned int i;
2272
2273 for (i = 0; i < descs->ndescs; i++)
2274 gpiod_put(descs->desc[i]);
2275
2276 kfree(descs);
2277}
2278EXPORT_SYMBOL_GPL(gpiod_put_array);
2279
David Brownelld2876d02008-02-04 22:28:20 -08002280#ifdef CONFIG_DEBUG_FS
2281
2282static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2283{
2284 unsigned i;
2285 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002286 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002287 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002288 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002289
2290 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2291 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2292 continue;
2293
Alexandre Courbot372e7222013-02-03 01:29:29 +09002294 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002295 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002296 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2297 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002298 gpio, gdesc->label,
2299 is_out ? "out" : "in ",
2300 chip->get
2301 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002302 : "? ",
2303 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002304 seq_printf(s, "\n");
2305 }
2306}
2307
Thierry Redingf9c4a312012-04-12 13:26:01 +02002308static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002309{
Grant Likely362432a2013-02-09 09:41:49 +00002310 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002311 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002312 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002313
2314 s->private = "";
2315
Grant Likely362432a2013-02-09 09:41:49 +00002316 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002317 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002318 if (index-- == 0) {
2319 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002320 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002321 }
2322 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002323
2324 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002325}
2326
2327static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2328{
Grant Likely362432a2013-02-09 09:41:49 +00002329 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002330 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002331 void *ret = NULL;
2332
Grant Likely362432a2013-02-09 09:41:49 +00002333 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002334 if (list_is_last(&chip->list, &gpio_chips))
2335 ret = NULL;
2336 else
2337 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002338 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002339
2340 s->private = "\n";
2341 ++*pos;
2342
2343 return ret;
2344}
2345
2346static void gpiolib_seq_stop(struct seq_file *s, void *v)
2347{
2348}
2349
2350static int gpiolib_seq_show(struct seq_file *s, void *v)
2351{
2352 struct gpio_chip *chip = v;
2353 struct device *dev;
2354
2355 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2356 chip->base, chip->base + chip->ngpio - 1);
2357 dev = chip->dev;
2358 if (dev)
2359 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2360 dev_name(dev));
2361 if (chip->label)
2362 seq_printf(s, ", %s", chip->label);
2363 if (chip->can_sleep)
2364 seq_printf(s, ", can sleep");
2365 seq_printf(s, ":\n");
2366
2367 if (chip->dbg_show)
2368 chip->dbg_show(s, chip);
2369 else
2370 gpiolib_dbg_show(s, chip);
2371
David Brownelld2876d02008-02-04 22:28:20 -08002372 return 0;
2373}
2374
Thierry Redingf9c4a312012-04-12 13:26:01 +02002375static const struct seq_operations gpiolib_seq_ops = {
2376 .start = gpiolib_seq_start,
2377 .next = gpiolib_seq_next,
2378 .stop = gpiolib_seq_stop,
2379 .show = gpiolib_seq_show,
2380};
2381
David Brownelld2876d02008-02-04 22:28:20 -08002382static int gpiolib_open(struct inode *inode, struct file *file)
2383{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002384 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002385}
2386
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002387static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002388 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002389 .open = gpiolib_open,
2390 .read = seq_read,
2391 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002392 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002393};
2394
2395static int __init gpiolib_debugfs_init(void)
2396{
2397 /* /sys/kernel/debug/gpio */
2398 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2399 NULL, NULL, &gpiolib_operations);
2400 return 0;
2401}
2402subsys_initcall(gpiolib_debugfs_init);
2403
2404#endif /* DEBUG_FS */