blob: 8dfb54f9259428baebadb9519202f5977f8e8d39 [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
David Brownelld2876d02008-02-04 22:28:20 -080056static inline void desc_set_label(struct gpio_desc *d, const char *label)
57{
David Brownelld2876d02008-02-04 22:28:20 -080058 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080059}
60
Alexandre Courbot372e7222013-02-03 01:29:29 +090061/**
62 * Convert a GPIO number to its descriptor
63 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070064struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090065{
Alexandre Courbot14e85c02014-11-19 16:51:27 +090066 struct gpio_chip *chip;
67 unsigned long flags;
68
69 spin_lock_irqsave(&gpio_lock, flags);
70
71 list_for_each_entry(chip, &gpio_chips, list) {
72 if (chip->base <= gpio && chip->base + chip->ngpio > gpio) {
73 spin_unlock_irqrestore(&gpio_lock, flags);
74 return &chip->desc[gpio - chip->base];
75 }
76 }
77
78 spin_unlock_irqrestore(&gpio_lock, flags);
79
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +090080 if (!gpio_is_valid(gpio))
81 WARN(1, "invalid GPIO %d\n", gpio);
82
Alexandre Courbot14e85c02014-11-19 16:51:27 +090083 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +090084}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070085EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090086
87/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090088 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +020089 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090090struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
91 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +020092{
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090093 if (hwnum >= chip->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +090094 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +020095
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090096 return &chip->desc[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +020097}
Alexandre Courbot372e7222013-02-03 01:29:29 +090098
99/**
100 * Convert a GPIO descriptor to the integer namespace.
101 * This should disappear in the future but is needed since we still
102 * use GPIO numbers for error messages and sysfs nodes
103 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700104int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900105{
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900106 return desc->chip->base + (desc - &desc->chip->desc[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900107}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700108EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900109
110
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700111/**
112 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
113 * @desc: descriptor to return the chip of
114 */
115struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900116{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900117 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900118}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700119EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800120
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700121/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
122static int gpiochip_find_base(int ngpio)
123{
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900124 struct gpio_chip *chip;
125 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700126
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900127 list_for_each_entry_reverse(chip, &gpio_chips, list) {
128 /* found a free space? */
129 if (chip->base + chip->ngpio <= base)
130 break;
131 else
132 /* nope, check the space right before the chip */
133 base = chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700134 }
135
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900136 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700137 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900138 return base;
139 } else {
140 pr_err("%s: cannot find free range\n", __func__);
141 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700142 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700143}
144
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700145/**
146 * gpiod_get_direction - return the current direction of a GPIO
147 * @desc: GPIO to get the direction of
148 *
149 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
150 *
151 * This function may sleep if gpiod_cansleep() is true.
152 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900153int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300154{
155 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900156 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300157 int status = -EINVAL;
158
Alexandre Courbot372e7222013-02-03 01:29:29 +0900159 chip = gpiod_to_chip(desc);
160 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300161
162 if (!chip->get_direction)
163 return status;
164
Alexandre Courbot372e7222013-02-03 01:29:29 +0900165 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300166 if (status > 0) {
167 /* GPIOF_DIR_IN, or other positive */
168 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900169 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300170 }
171 if (status == 0) {
172 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900173 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300174 }
175 return status;
176}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700177EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300178
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900179/*
180 * Add a new chip to the global chips list, keeping the list of chips sorted
181 * by base order.
182 *
183 * Return -EBUSY if the new chip overlaps with some other chip's integer
184 * space.
185 */
186static int gpiochip_add_to_list(struct gpio_chip *chip)
187{
188 struct list_head *pos = &gpio_chips;
189 struct gpio_chip *_chip;
190 int err = 0;
191
192 /* find where to insert our chip */
193 list_for_each(pos, &gpio_chips) {
194 _chip = list_entry(pos, struct gpio_chip, list);
195 /* shall we insert before _chip? */
196 if (_chip->base >= chip->base + chip->ngpio)
197 break;
198 }
199
200 /* are we stepping on the chip right before? */
201 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
202 _chip = list_entry(pos->prev, struct gpio_chip, list);
203 if (_chip->base + _chip->ngpio > chip->base) {
204 dev_err(chip->dev,
205 "GPIO integer space overlap, cannot add chip\n");
206 err = -EBUSY;
207 }
208 }
209
210 if (!err)
211 list_add_tail(&chip->list, pos);
212
213 return err;
214}
215
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700216/**
David Brownelld2876d02008-02-04 22:28:20 -0800217 * gpiochip_add() - register a gpio_chip
218 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900219 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -0800220 *
221 * Returns a negative errno if the chip can't be registered, such as
222 * because the chip->base is invalid or already associated with a
223 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700224 *
David Brownelld8f388d2008-07-25 01:46:07 -0700225 * When gpiochip_add() is called very early during boot, so that GPIOs
226 * can be freely used, the chip->dev device must be registered before
227 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
228 * for GPIOs will fail rudely.
229 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700230 * If chip->base is negative, this requests dynamic assignment of
231 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800232 */
233int gpiochip_add(struct gpio_chip *chip)
234{
235 unsigned long flags;
236 int status = 0;
237 unsigned id;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700238 int base = chip->base;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900239 struct gpio_desc *descs;
David Brownelld2876d02008-02-04 22:28:20 -0800240
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900241 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
242 if (!descs)
243 return -ENOMEM;
David Brownelld2876d02008-02-04 22:28:20 -0800244
245 spin_lock_irqsave(&gpio_lock, flags);
246
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700247 if (base < 0) {
248 base = gpiochip_find_base(chip->ngpio);
249 if (base < 0) {
250 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100251 spin_unlock_irqrestore(&gpio_lock, flags);
252 goto err_free_descs;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700253 }
254 chip->base = base;
255 }
256
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900257 status = gpiochip_add_to_list(chip);
Johan Hovold05aa5202015-01-12 17:12:26 +0100258 if (status) {
259 spin_unlock_irqrestore(&gpio_lock, flags);
260 goto err_free_descs;
261 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900262
Johan Hovold05aa5202015-01-12 17:12:26 +0100263 for (id = 0; id < chip->ngpio; id++) {
264 struct gpio_desc *desc = &descs[id];
David Brownelld8f388d2008-07-25 01:46:07 -0700265
Johan Hovold05aa5202015-01-12 17:12:26 +0100266 desc->chip = chip;
267
268 /* REVISIT: most hardware initializes GPIOs as inputs (often
269 * with pullups enabled) so power usage is minimized. Linux
270 * code should set the gpio direction first thing; but until
271 * it does, and in case chip->get_direction is not set, we may
272 * expose the wrong direction in sysfs.
273 */
274 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800275 }
276
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900277 chip->desc = descs;
278
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800279 spin_unlock_irqrestore(&gpio_lock, flags);
280
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530281#ifdef CONFIG_PINCTRL
282 INIT_LIST_HEAD(&chip->pin_ranges);
283#endif
284
Anton Vorontsov391c9702010-06-08 07:48:17 -0600285 of_gpiochip_add(chip);
Mika Westerberg664e3e52014-01-08 12:40:54 +0200286 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600287
Johan Hovold426577b2015-05-04 17:10:32 +0200288 status = gpiochip_sysfs_register(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100289 if (status)
290 goto err_remove_chip;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600291
Andy Shevchenko7589e592013-12-05 11:26:23 +0200292 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700293 chip->base, chip->base + chip->ngpio - 1,
294 chip->label ? : "generic");
295
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600296 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800297
Johan Hovold225fce82015-01-12 17:12:25 +0100298err_remove_chip:
299 acpi_gpiochip_remove(chip);
300 of_gpiochip_remove(chip);
301 spin_lock_irqsave(&gpio_lock, flags);
302 list_del(&chip->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800303 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold05aa5202015-01-12 17:12:26 +0100304 chip->desc = NULL;
Johan Hovold225fce82015-01-12 17:12:25 +0100305err_free_descs:
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900306 kfree(descs);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900307
David Brownelld2876d02008-02-04 22:28:20 -0800308 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200309 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600310 chip->base, chip->base + chip->ngpio - 1,
311 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800312 return status;
313}
314EXPORT_SYMBOL_GPL(gpiochip_add);
315
Linus Walleij14250522014-03-25 10:40:18 +0100316/* Forward-declaration */
317static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600318static void gpiochip_free_hogs(struct gpio_chip *chip);
Linus Walleij14250522014-03-25 10:40:18 +0100319
David Brownelld2876d02008-02-04 22:28:20 -0800320/**
321 * gpiochip_remove() - unregister a gpio_chip
322 * @chip: the chip to unregister
323 *
324 * A gpio_chip with any GPIOs still requested may not be removed.
325 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200326void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800327{
Johan Hovoldfab28b82015-05-04 17:10:27 +0200328 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800329 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800330 unsigned id;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200331 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800332
Johan Hovold426577b2015-05-04 17:10:32 +0200333 gpiochip_sysfs_unregister(chip);
Johan Hovold01cca932015-01-12 17:12:29 +0100334
Johan Hovold00acc3d2015-01-12 17:12:27 +0100335 gpiochip_irqchip_remove(chip);
336
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200337 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100338 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600339 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600340 of_gpiochip_remove(chip);
341
Johan Hovold6798aca2015-01-12 17:12:28 +0100342 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900343 for (id = 0; id < chip->ngpio; id++) {
Johan Hovoldfab28b82015-05-04 17:10:27 +0200344 desc = &chip->desc[id];
345 desc->chip = NULL;
346 if (test_bit(FLAG_REQUESTED, &desc->flags))
347 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800348 }
abdoulaye berthee1db1702014-07-05 18:28:50 +0200349 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800350 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900351
Johan Hovoldfab28b82015-05-04 17:10:27 +0200352 if (requested)
353 dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
354
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900355 kfree(chip->desc);
356 chip->desc = NULL;
David Brownelld2876d02008-02-04 22:28:20 -0800357}
358EXPORT_SYMBOL_GPL(gpiochip_remove);
359
Grant Likely594fa262010-06-08 07:48:16 -0600360/**
361 * gpiochip_find() - iterator for locating a specific gpio_chip
362 * @data: data to pass to match function
363 * @callback: Callback function to check gpio_chip
364 *
365 * Similar to bus_find_device. It returns a reference to a gpio_chip as
366 * determined by a user supplied @match callback. The callback should return
367 * 0 if the device doesn't match and non-zero if it does. If the callback is
368 * non-zero, this function will return to the caller and not iterate over any
369 * more gpio_chips.
370 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600371struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700372 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600373 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600374{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900375 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600376 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600377
378 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900379 list_for_each_entry(chip, &gpio_chips, list)
380 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600381 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900382
383 /* No match? */
384 if (&chip->list == &gpio_chips)
385 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600386 spin_unlock_irqrestore(&gpio_lock, flags);
387
388 return chip;
389}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600390EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800391
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900392static int gpiochip_match_name(struct gpio_chip *chip, void *data)
393{
394 const char *name = data;
395
396 return !strcmp(chip->label, name);
397}
398
399static struct gpio_chip *find_chip_by_name(const char *name)
400{
401 return gpiochip_find((void *)name, gpiochip_match_name);
402}
403
Linus Walleij14250522014-03-25 10:40:18 +0100404#ifdef CONFIG_GPIOLIB_IRQCHIP
405
406/*
407 * The following is irqchip helper code for gpiochips.
408 */
409
410/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200411 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
412 * @gpiochip: the gpiochip to set the irqchip chain to
413 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100414 * @parent_irq: the irq number corresponding to the parent IRQ for this
415 * chained irqchip
416 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200417 * coming out of the gpiochip. If the interrupt is nested rather than
418 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100419 */
420void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
421 struct irq_chip *irqchip,
422 int parent_irq,
423 irq_flow_handler_t parent_handler)
424{
Linus Walleij83141a72014-09-26 13:50:12 +0200425 unsigned int offset;
426
Linus Walleij83141a72014-09-26 13:50:12 +0200427 if (!gpiochip->irqdomain) {
428 chip_err(gpiochip, "called %s before setting up irqchip\n",
429 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200430 return;
431 }
432
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200433 if (parent_handler) {
434 if (gpiochip->can_sleep) {
435 chip_err(gpiochip,
436 "you cannot have chained interrupts on a "
437 "chip that may sleep\n");
438 return;
439 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200440 /*
441 * The parent irqchip is already using the chip_data for this
442 * irqchip, so our callbacks simply use the handler_data.
443 */
444 irq_set_handler_data(parent_irq, gpiochip);
Linus Torvaldsea584592014-10-09 14:58:15 -0400445 irq_set_chained_handler(parent_irq, parent_handler);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300446
447 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200448 }
Linus Walleij83141a72014-09-26 13:50:12 +0200449
450 /* Set the parent IRQ for all affected IRQs */
451 for (offset = 0; offset < gpiochip->ngpio; offset++)
452 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
453 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100454}
455EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
456
Linus Walleije45d1c82014-04-22 14:01:46 +0200457/*
458 * This lock class tells lockdep that GPIO irqs are in a different
459 * category than their parents, so it won't report false recursion.
460 */
461static struct lock_class_key gpiochip_irq_lock_class;
462
Linus Walleij14250522014-03-25 10:40:18 +0100463/**
464 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
465 * @d: the irqdomain used by this irqchip
466 * @irq: the global irq number used by this GPIO irqchip irq
467 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
468 *
469 * This function will set up the mapping for a certain IRQ line on a
470 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
471 * stored inside the gpiochip.
472 */
473static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
474 irq_hw_number_t hwirq)
475{
476 struct gpio_chip *chip = d->host_data;
477
Linus Walleij14250522014-03-25 10:40:18 +0100478 irq_set_chip_data(irq, chip);
Linus Walleije45d1c82014-04-22 14:01:46 +0200479 irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
Linus Walleij7633fb92014-04-09 13:20:38 +0200480 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200481 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300482 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200483 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100484#ifdef CONFIG_ARM
485 set_irq_flags(irq, IRQF_VALID);
486#else
487 irq_set_noprobe(irq);
488#endif
Linus Walleij1333b902014-04-23 16:45:12 +0200489 /*
490 * No set-up of the hardware will happen if IRQ_TYPE_NONE
491 * is passed as default type.
492 */
493 if (chip->irq_default_type != IRQ_TYPE_NONE)
494 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100495
496 return 0;
497}
498
Linus Walleijc3626fd2014-03-28 20:42:01 +0100499static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
500{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200501 struct gpio_chip *chip = d->host_data;
502
Linus Walleijc3626fd2014-03-28 20:42:01 +0100503#ifdef CONFIG_ARM
504 set_irq_flags(irq, 0);
505#endif
Linus Walleij1c8732b2014-04-09 13:34:39 +0200506 if (chip->can_sleep)
507 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100508 irq_set_chip_and_handler(irq, NULL, NULL);
509 irq_set_chip_data(irq, NULL);
510}
511
Linus Walleij14250522014-03-25 10:40:18 +0100512static const struct irq_domain_ops gpiochip_domain_ops = {
513 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100514 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100515 /* Virtually all GPIO irqchips are twocell:ed */
516 .xlate = irq_domain_xlate_twocell,
517};
518
519static int gpiochip_irq_reqres(struct irq_data *d)
520{
521 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
522
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900523 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100524 chip_err(chip,
525 "unable to lock HW IRQ %lu for IRQ\n",
526 d->hwirq);
527 return -EINVAL;
528 }
529 return 0;
530}
531
532static void gpiochip_irq_relres(struct irq_data *d)
533{
534 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
535
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900536 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleij14250522014-03-25 10:40:18 +0100537}
538
539static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
540{
541 return irq_find_mapping(chip->irqdomain, offset);
542}
543
544/**
545 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
546 * @gpiochip: the gpiochip to remove the irqchip from
547 *
548 * This is called only from gpiochip_remove()
549 */
550static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
551{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100552 unsigned int offset;
553
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300554 acpi_gpiochip_free_interrupts(gpiochip);
555
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300556 if (gpiochip->irq_parent) {
557 irq_set_chained_handler(gpiochip->irq_parent, NULL);
558 irq_set_handler_data(gpiochip->irq_parent, NULL);
559 }
560
Linus Walleijc3626fd2014-03-28 20:42:01 +0100561 /* Remove all IRQ mappings and delete the domain */
562 if (gpiochip->irqdomain) {
563 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300564 irq_dispose_mapping(
565 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100566 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100567 }
Linus Walleij14250522014-03-25 10:40:18 +0100568
569 if (gpiochip->irqchip) {
570 gpiochip->irqchip->irq_request_resources = NULL;
571 gpiochip->irqchip->irq_release_resources = NULL;
572 gpiochip->irqchip = NULL;
573 }
574}
575
576/**
577 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
578 * @gpiochip: the gpiochip to add the irqchip to
579 * @irqchip: the irqchip to add to the gpiochip
580 * @first_irq: if not dynamically assigned, the base (first) IRQ to
581 * allocate gpiochip irqs from
582 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200583 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
584 * to have the core avoid setting up any default type in the hardware.
Linus Walleij14250522014-03-25 10:40:18 +0100585 *
586 * This function closely associates a certain irqchip with a certain
587 * gpiochip, providing an irq domain to translate the local IRQs to
588 * global irqs in the gpiolib core, and making sure that the gpiochip
589 * is passed as chip data to all related functions. Driver callbacks
590 * need to use container_of() to get their local state containers back
591 * from the gpiochip passed as chip data. An irqdomain will be stored
592 * in the gpiochip that shall be used by the driver to handle IRQ number
593 * translation. The gpiochip will need to be initialized and registered
594 * before calling this function.
595 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100596 * This function will handle two cell:ed simple IRQs and assumes all
597 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100598 * need to be open coded.
599 */
600int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
601 struct irq_chip *irqchip,
602 unsigned int first_irq,
603 irq_flow_handler_t handler,
604 unsigned int type)
605{
606 struct device_node *of_node;
607 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100608 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100609
610 if (!gpiochip || !irqchip)
611 return -EINVAL;
612
613 if (!gpiochip->dev) {
614 pr_err("missing gpiochip .dev parent pointer\n");
615 return -EINVAL;
616 }
617 of_node = gpiochip->dev->of_node;
618#ifdef CONFIG_OF_GPIO
619 /*
Colin Cronin20a8a962015-05-18 11:41:43 -0700620 * If the gpiochip has an assigned OF node this takes precedence
Linus Walleij14250522014-03-25 10:40:18 +0100621 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
622 */
623 if (gpiochip->of_node)
624 of_node = gpiochip->of_node;
625#endif
626 gpiochip->irqchip = irqchip;
627 gpiochip->irq_handler = handler;
628 gpiochip->irq_default_type = type;
629 gpiochip->to_irq = gpiochip_to_irq;
630 gpiochip->irqdomain = irq_domain_add_simple(of_node,
631 gpiochip->ngpio, first_irq,
632 &gpiochip_domain_ops, gpiochip);
633 if (!gpiochip->irqdomain) {
634 gpiochip->irqchip = NULL;
635 return -EINVAL;
636 }
637 irqchip->irq_request_resources = gpiochip_irq_reqres;
638 irqchip->irq_release_resources = gpiochip_irq_relres;
639
640 /*
641 * Prepare the mapping since the irqchip shall be orthogonal to
642 * any gpiochip calls. If the first_irq was zero, this is
643 * necessary to allocate descriptors for all IRQs.
644 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100645 for (offset = 0; offset < gpiochip->ngpio; offset++) {
646 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
647 if (offset == 0)
648 /*
649 * Store the base into the gpiochip to be used when
650 * unmapping the irqs.
651 */
652 gpiochip->irq_base = irq_base;
653 }
Linus Walleij14250522014-03-25 10:40:18 +0100654
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300655 acpi_gpiochip_request_interrupts(gpiochip);
656
Linus Walleij14250522014-03-25 10:40:18 +0100657 return 0;
658}
659EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
660
661#else /* CONFIG_GPIOLIB_IRQCHIP */
662
663static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
664
665#endif /* CONFIG_GPIOLIB_IRQCHIP */
666
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530667#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100668
Linus Walleij3f0f8672012-11-20 12:40:15 +0100669/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200670 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
671 * @chip: the gpiochip to add the range for
672 * @pinctrl: the dev_name() of the pin controller to map to
673 * @gpio_offset: the start offset in the current gpio_chip number space
674 * @pin_group: name of the pin group inside the pin controller
675 */
676int gpiochip_add_pingroup_range(struct gpio_chip *chip,
677 struct pinctrl_dev *pctldev,
678 unsigned int gpio_offset, const char *pin_group)
679{
680 struct gpio_pin_range *pin_range;
681 int ret;
682
683 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
684 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200685 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200686 return -ENOMEM;
687 }
688
689 /* Use local offset as range ID */
690 pin_range->range.id = gpio_offset;
691 pin_range->range.gc = chip;
692 pin_range->range.name = chip->label;
693 pin_range->range.base = chip->base + gpio_offset;
694 pin_range->pctldev = pctldev;
695
696 ret = pinctrl_get_group_pins(pctldev, pin_group,
697 &pin_range->range.pins,
698 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100699 if (ret < 0) {
700 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200701 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100702 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200703
704 pinctrl_add_gpio_range(pctldev, &pin_range->range);
705
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200706 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
707 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200708 pinctrl_dev_get_devname(pctldev), pin_group);
709
710 list_add_tail(&pin_range->node, &chip->pin_ranges);
711
712 return 0;
713}
714EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
715
716/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100717 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
718 * @chip: the gpiochip to add the range for
719 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100720 * @gpio_offset: the start offset in the current gpio_chip number space
721 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100722 * @npins: the number of pins from the offset of each pin space (GPIO and
723 * pin controller) to accumulate in this range
724 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100725int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100726 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100727 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530728{
729 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800730 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530731
Linus Walleij3f0f8672012-11-20 12:40:15 +0100732 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530733 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200734 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100735 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530736 }
737
Linus Walleij3f0f8672012-11-20 12:40:15 +0100738 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100739 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100740 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530741 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100742 pin_range->range.base = chip->base + gpio_offset;
743 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530744 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100745 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530746 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100747 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800748 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200749 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100750 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800751 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100752 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200753 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
754 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100755 pinctl_name,
756 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530757
758 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100759
760 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530761}
Linus Walleij165adc92012-11-06 14:49:39 +0100762EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530763
Linus Walleij3f0f8672012-11-20 12:40:15 +0100764/**
765 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
766 * @chip: the chip to remove all the mappings for
767 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530768void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
769{
770 struct gpio_pin_range *pin_range, *tmp;
771
772 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
773 list_del(&pin_range->node);
774 pinctrl_remove_gpio_range(pin_range->pctldev,
775 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100776 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530777 }
778}
Linus Walleij165adc92012-11-06 14:49:39 +0100779EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
780
781#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530782
David Brownelld2876d02008-02-04 22:28:20 -0800783/* These "optional" allocation calls help prevent drivers from stomping
784 * on each other, and help provide better diagnostics in debugfs.
785 * They're called even less than the "set direction" calls.
786 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200787static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800788{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200789 struct gpio_chip *chip = desc->chip;
790 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800791 unsigned long flags;
792
793 spin_lock_irqsave(&gpio_lock, flags);
794
David Brownelld2876d02008-02-04 22:28:20 -0800795 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700796 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800797 */
798
799 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
800 desc_set_label(desc, label ? : "?");
801 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700802 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800803 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800804 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700805 }
806
807 if (chip->request) {
808 /* chip->request may sleep */
809 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900810 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700811 spin_lock_irqsave(&gpio_lock, flags);
812
813 if (status < 0) {
814 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700815 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300816 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700817 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700818 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300819 if (chip->get_direction) {
820 /* chip->get_direction may sleep */
821 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900822 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300823 spin_lock_irqsave(&gpio_lock, flags);
824 }
David Brownelld2876d02008-02-04 22:28:20 -0800825done:
Mika Westerberg77c2d792014-03-10 14:54:50 +0200826 spin_unlock_irqrestore(&gpio_lock, flags);
827 return status;
828}
829
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900830int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200831{
832 int status = -EPROBE_DEFER;
833 struct gpio_chip *chip;
834
835 if (!desc) {
836 pr_warn("%s: invalid GPIO\n", __func__);
837 return -EINVAL;
838 }
839
840 chip = desc->chip;
841 if (!chip)
842 goto done;
843
844 if (try_module_get(chip->owner)) {
845 status = __gpiod_request(desc, label);
846 if (status < 0)
847 module_put(chip->owner);
848 }
849
850done:
David Brownelld2876d02008-02-04 22:28:20 -0800851 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200852 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200853
David Brownelld2876d02008-02-04 22:28:20 -0800854 return status;
855}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900856
Mika Westerberg77c2d792014-03-10 14:54:50 +0200857static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800858{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200859 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -0800860 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -0700861 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800862
Uwe Kleine-König3d599d12008-10-15 22:03:12 -0700863 might_sleep();
864
Alexandre Courbot372e7222013-02-03 01:29:29 +0900865 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700866
David Brownelld2876d02008-02-04 22:28:20 -0800867 spin_lock_irqsave(&gpio_lock, flags);
868
David Brownell35e8bb52008-10-15 22:03:16 -0700869 chip = desc->chip;
870 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
871 if (chip->free) {
872 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -0700873 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900874 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700875 spin_lock_irqsave(&gpio_lock, flags);
876 }
David Brownelld2876d02008-02-04 22:28:20 -0800877 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -0800878 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -0700879 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530880 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +0530881 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -0600882 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200883 ret = true;
884 }
David Brownelld2876d02008-02-04 22:28:20 -0800885
886 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200887 return ret;
888}
889
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900890void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200891{
892 if (desc && __gpiod_free(desc))
893 module_put(desc->chip->owner);
894 else
895 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -0800896}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900897
David Brownelld2876d02008-02-04 22:28:20 -0800898/**
899 * gpiochip_is_requested - return string iff signal was requested
900 * @chip: controller managing the signal
901 * @offset: of signal within controller's 0..(ngpio - 1) range
902 *
903 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +0900904 * The string returned is the label passed to gpio_request(); if none has been
905 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -0800906 *
907 * This function is for use by GPIO controller drivers. The label can
908 * help with diagnostics, and knowing that the signal is used as a GPIO
909 * can help avoid accidentally multiplexing it to another controller.
910 */
911const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
912{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900913 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800914
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900915 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -0800916 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900917
918 desc = &chip->desc[offset];
919
Alexandre Courbot372e7222013-02-03 01:29:29 +0900920 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -0800921 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900922 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -0800923}
924EXPORT_SYMBOL_GPL(gpiochip_is_requested);
925
Mika Westerberg77c2d792014-03-10 14:54:50 +0200926/**
927 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
928 * @desc: GPIO descriptor to request
929 * @label: label for the GPIO
930 *
931 * Function allows GPIO chip drivers to request and use their own GPIO
932 * descriptors via gpiolib API. Difference to gpiod_request() is that this
933 * function will not increase reference count of the GPIO chip module. This
934 * allows the GPIO chip module to be unloaded as needed (we assume that the
935 * GPIO chip driver handles freeing the GPIOs it has requested).
936 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700937struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
938 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200939{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700940 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
941 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200942
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700943 if (IS_ERR(desc)) {
944 chip_err(chip, "failed to get GPIO descriptor\n");
945 return desc;
946 }
947
948 err = __gpiod_request(desc, label);
949 if (err < 0)
950 return ERR_PTR(err);
951
952 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200953}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700954EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200955
956/**
957 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
958 * @desc: GPIO descriptor to free
959 *
960 * Function frees the given GPIO requested previously with
961 * gpiochip_request_own_desc().
962 */
963void gpiochip_free_own_desc(struct gpio_desc *desc)
964{
965 if (desc)
966 __gpiod_free(desc);
967}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700968EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -0800969
970/* Drivers MUST set GPIO direction before making get/set calls. In
971 * some cases this is done in early boot, before IRQs are enabled.
972 *
973 * As a rule these aren't called more than once (except for drivers
974 * using the open-drain emulation idiom) so these are natural places
975 * to accumulate extra debugging checks. Note that we can't (yet)
976 * rely on gpio_request() having been called beforehand.
977 */
978
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700979/**
980 * gpiod_direction_input - set the GPIO direction to input
981 * @desc: GPIO to set to input
982 *
983 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
984 * be called safely on it.
985 *
986 * Return 0 in case of success, else an error code.
987 */
988int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800989{
David Brownelld2876d02008-02-04 22:28:20 -0800990 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800991 int status = -EINVAL;
992
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200993 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900994 pr_warn("%s: invalid GPIO\n", __func__);
995 return -EINVAL;
996 }
997
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200998 chip = desc->chip;
999 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001000 gpiod_warn(desc,
1001 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001002 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001003 return -EIO;
1004 }
1005
Alexandre Courbotd82da792014-07-22 16:17:43 +09001006 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001007 if (status == 0)
1008 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001009
Alexandre Courbot372e7222013-02-03 01:29:29 +09001010 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001011
David Brownelld2876d02008-02-04 22:28:20 -08001012 return status;
1013}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001014EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001015
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001016static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001017{
David Brownelld2876d02008-02-04 22:28:20 -08001018 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001019 int status = -EINVAL;
1020
Linus Walleijd468bf92013-09-24 11:54:38 +02001021 /* GPIOs used for IRQs shall not be set as output */
1022 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1023 gpiod_err(desc,
1024 "%s: tried to set a GPIO tied to an IRQ as output\n",
1025 __func__);
1026 return -EIO;
1027 }
1028
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301029 /* Open drain pin should not be driven to 1 */
1030 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001031 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301032
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301033 /* Open source pin should not be driven to 0 */
1034 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001035 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301036
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001037 chip = desc->chip;
1038 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001039 gpiod_warn(desc,
1040 "%s: missing set() or direction_output() operations\n",
1041 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001042 return -EIO;
1043 }
1044
Alexandre Courbotd82da792014-07-22 16:17:43 +09001045 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001046 if (status == 0)
1047 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001048 trace_gpio_value(desc_to_gpio(desc), 0, value);
1049 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001050 return status;
1051}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001052
1053/**
1054 * gpiod_direction_output_raw - set the GPIO direction to output
1055 * @desc: GPIO to set to output
1056 * @value: initial output value of the GPIO
1057 *
1058 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1059 * be called safely on it. The initial value of the output must be specified
1060 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1061 *
1062 * Return 0 in case of success, else an error code.
1063 */
1064int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1065{
1066 if (!desc || !desc->chip) {
1067 pr_warn("%s: invalid GPIO\n", __func__);
1068 return -EINVAL;
1069 }
1070 return _gpiod_direction_output_raw(desc, value);
1071}
1072EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1073
1074/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301075 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001076 * @desc: GPIO to set to output
1077 * @value: initial output value of the GPIO
1078 *
1079 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1080 * be called safely on it. The initial value of the output must be specified
1081 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1082 * account.
1083 *
1084 * Return 0 in case of success, else an error code.
1085 */
1086int gpiod_direction_output(struct gpio_desc *desc, int value)
1087{
1088 if (!desc || !desc->chip) {
1089 pr_warn("%s: invalid GPIO\n", __func__);
1090 return -EINVAL;
1091 }
1092 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1093 value = !value;
1094 return _gpiod_direction_output_raw(desc, value);
1095}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001096EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001097
Felipe Balbic4b5be92010-05-26 14:42:23 -07001098/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001099 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001100 * @gpio: the gpio to set debounce time
1101 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001102 *
1103 * returns -ENOTSUPP if the controller does not support setting
1104 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001105 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001106int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001107{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001108 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001109
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001110 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001111 pr_warn("%s: invalid GPIO\n", __func__);
1112 return -EINVAL;
1113 }
1114
Felipe Balbic4b5be92010-05-26 14:42:23 -07001115 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001116 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001117 gpiod_dbg(desc,
1118 "%s: missing set() or set_debounce() operations\n",
1119 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001120 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001121 }
1122
Alexandre Courbotd82da792014-07-22 16:17:43 +09001123 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001124}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001125EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001126
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001127/**
1128 * gpiod_is_active_low - test whether a GPIO is active-low or not
1129 * @desc: the gpio descriptor to test
1130 *
1131 * Returns 1 if the GPIO is active-low, 0 otherwise.
1132 */
1133int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001134{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001135 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001136}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001137EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001138
1139/* I/O calls are only valid after configuration completed; the relevant
1140 * "is this a valid GPIO" error checks should already have been done.
1141 *
1142 * "Get" operations are often inlinable as reading a pin value register,
1143 * and masking the relevant bit in that register.
1144 *
1145 * When "set" operations are inlinable, they involve writing that mask to
1146 * one register to set a low value, or a different register to set it high.
1147 * Otherwise locking is needed, so there may be little value to inlining.
1148 *
1149 *------------------------------------------------------------------------
1150 *
1151 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1152 * have requested the GPIO. That can include implicit requesting by
1153 * a direction setting call. Marking a gpio as requested locks its chip
1154 * in memory, guaranteeing that these table lookups need no more locking
1155 * and that gpiochip_remove() will fail.
1156 *
1157 * REVISIT when debugging, consider adding some instrumentation to ensure
1158 * that the GPIO was actually requested.
1159 */
1160
Alexandre Courbot23600962014-03-11 15:52:09 +09001161static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001162{
1163 struct gpio_chip *chip;
Alexandre Courbot23600962014-03-11 15:52:09 +09001164 bool value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001165 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001166
Alexandre Courbot372e7222013-02-03 01:29:29 +09001167 chip = desc->chip;
1168 offset = gpio_chip_hwgpio(desc);
Alexandre Courbot23600962014-03-11 15:52:09 +09001169 value = chip->get ? chip->get(chip, offset) : false;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001170 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001171 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001172}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001173
David Brownelld2876d02008-02-04 22:28:20 -08001174/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001175 * gpiod_get_raw_value() - return a gpio's raw value
1176 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001177 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001178 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1179 * its ACTIVE_LOW status.
1180 *
1181 * This function should be called from contexts where we cannot sleep, and will
1182 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001183 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001184int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001185{
David Brownelld2876d02008-02-04 22:28:20 -08001186 if (!desc)
1187 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001188 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001189 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001190 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001191}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001192EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001193
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001194/**
1195 * gpiod_get_value() - return a gpio's value
1196 * @desc: gpio whose value will be returned
1197 *
1198 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1199 * account.
1200 *
1201 * This function should be called from contexts where we cannot sleep, and will
1202 * complain if the GPIO chip functions potentially sleep.
1203 */
1204int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001205{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001206 int value;
1207 if (!desc)
1208 return 0;
1209 /* Should be using gpio_get_value_cansleep() */
1210 WARN_ON(desc->chip->can_sleep);
1211
1212 value = _gpiod_get_raw_value(desc);
1213 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1214 value = !value;
1215
1216 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001217}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001218EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001219
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301220/*
1221 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001222 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001223 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301224 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001225static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301226{
1227 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001228 struct gpio_chip *chip = desc->chip;
1229 int offset = gpio_chip_hwgpio(desc);
1230
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301231 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001232 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301233 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001234 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301235 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001236 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301237 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001238 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301239 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001240 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301241 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001242 gpiod_err(desc,
1243 "%s: Error in set_value for open drain err %d\n",
1244 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301245}
1246
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301247/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001248 * _gpio_set_open_source_value() - Set the open source gpio's value.
1249 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001250 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301251 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001252static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301253{
1254 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001255 struct gpio_chip *chip = desc->chip;
1256 int offset = gpio_chip_hwgpio(desc);
1257
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301258 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001259 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301260 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001261 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301262 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001263 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301264 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001265 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301266 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001267 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301268 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001269 gpiod_err(desc,
1270 "%s: Error in set_value for open source err %d\n",
1271 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301272}
1273
Alexandre Courbot23600962014-03-11 15:52:09 +09001274static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001275{
1276 struct gpio_chip *chip;
1277
Alexandre Courbot372e7222013-02-03 01:29:29 +09001278 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001279 trace_gpio_value(desc_to_gpio(desc), 0, value);
1280 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1281 _gpio_set_open_drain_value(desc, value);
1282 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1283 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301284 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001285 chip->set(chip, gpio_chip_hwgpio(desc), value);
1286}
1287
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001288/*
1289 * set multiple outputs on the same chip;
1290 * use the chip's set_multiple function if available;
1291 * otherwise set the outputs sequentially;
1292 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1293 * defines which outputs are to be changed
1294 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1295 * defines the values the outputs specified by mask are to be set to
1296 */
1297static void gpio_chip_set_multiple(struct gpio_chip *chip,
1298 unsigned long *mask, unsigned long *bits)
1299{
1300 if (chip->set_multiple) {
1301 chip->set_multiple(chip, mask, bits);
1302 } else {
1303 int i;
1304 for (i = 0; i < chip->ngpio; i++) {
1305 if (mask[BIT_WORD(i)] == 0) {
1306 /* no more set bits in this mask word;
1307 * skip ahead to the next word */
1308 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1309 continue;
1310 }
1311 /* set outputs if the corresponding mask bit is set */
1312 if (__test_and_clear_bit(i, mask)) {
1313 chip->set(chip, i, test_bit(i, bits));
1314 }
1315 }
1316 }
1317}
1318
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001319static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1320 unsigned int array_size,
1321 struct gpio_desc **desc_array,
1322 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001323{
1324 int i = 0;
1325
1326 while (i < array_size) {
1327 struct gpio_chip *chip = desc_array[i]->chip;
1328 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1329 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1330 int count = 0;
1331
1332 if (!can_sleep) {
1333 WARN_ON(chip->can_sleep);
1334 }
1335 memset(mask, 0, sizeof(mask));
1336 do {
1337 struct gpio_desc *desc = desc_array[i];
1338 int hwgpio = gpio_chip_hwgpio(desc);
1339 int value = value_array[i];
1340
1341 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1342 value = !value;
1343 trace_gpio_value(desc_to_gpio(desc), 0, value);
1344 /*
1345 * collect all normal outputs belonging to the same chip
1346 * open drain and open source outputs are set individually
1347 */
1348 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
1349 _gpio_set_open_drain_value(desc,value);
1350 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1351 _gpio_set_open_source_value(desc, value);
1352 } else {
1353 __set_bit(hwgpio, mask);
1354 if (value) {
1355 __set_bit(hwgpio, bits);
1356 } else {
1357 __clear_bit(hwgpio, bits);
1358 }
1359 count++;
1360 }
1361 i++;
1362 } while ((i < array_size) && (desc_array[i]->chip == chip));
1363 /* push collected bits to outputs */
1364 if (count != 0) {
1365 gpio_chip_set_multiple(chip, mask, bits);
1366 }
1367 }
1368}
1369
David Brownelld2876d02008-02-04 22:28:20 -08001370/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001371 * gpiod_set_raw_value() - assign a gpio's raw value
1372 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001373 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001374 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001375 * Set the raw value of the GPIO, i.e. the value of its physical line without
1376 * regard for its ACTIVE_LOW status.
1377 *
1378 * This function should be called from contexts where we cannot sleep, and will
1379 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001380 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001381void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001382{
David Brownelld2876d02008-02-04 22:28:20 -08001383 if (!desc)
1384 return;
David Brownelld2876d02008-02-04 22:28:20 -08001385 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001386 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001387 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001388}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001389EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001390
1391/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001392 * gpiod_set_value() - assign a gpio's value
1393 * @desc: gpio whose value will be assigned
1394 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001395 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001396 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1397 * account
1398 *
1399 * This function should be called from contexts where we cannot sleep, and will
1400 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001401 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001402void gpiod_set_value(struct gpio_desc *desc, int value)
1403{
1404 if (!desc)
1405 return;
1406 /* Should be using gpio_set_value_cansleep() */
1407 WARN_ON(desc->chip->can_sleep);
1408 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1409 value = !value;
1410 _gpiod_set_raw_value(desc, value);
1411}
1412EXPORT_SYMBOL_GPL(gpiod_set_value);
1413
1414/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001415 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001416 * @array_size: number of elements in the descriptor / value arrays
1417 * @desc_array: array of GPIO descriptors whose values will be assigned
1418 * @value_array: array of values to assign
1419 *
1420 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1421 * without regard for their ACTIVE_LOW status.
1422 *
1423 * This function should be called from contexts where we cannot sleep, and will
1424 * complain if the GPIO chip functions potentially sleep.
1425 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001426void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001427 struct gpio_desc **desc_array, int *value_array)
1428{
1429 if (!desc_array)
1430 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001431 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1432 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001433}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001434EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001435
1436/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001437 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001438 * @array_size: number of elements in the descriptor / value arrays
1439 * @desc_array: array of GPIO descriptors whose values will be assigned
1440 * @value_array: array of values to assign
1441 *
1442 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1443 * into account.
1444 *
1445 * This function should be called from contexts where we cannot sleep, and will
1446 * complain if the GPIO chip functions potentially sleep.
1447 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001448void gpiod_set_array_value(unsigned int array_size,
1449 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001450{
1451 if (!desc_array)
1452 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001453 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1454 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001455}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001456EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001457
1458/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001459 * gpiod_cansleep() - report whether gpio value access may sleep
1460 * @desc: gpio to check
1461 *
1462 */
1463int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001464{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001465 if (!desc)
1466 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001467 return desc->chip->can_sleep;
1468}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001469EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001470
David Brownell0f6d5042008-10-15 22:03:14 -07001471/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001472 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1473 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001474 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001475 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1476 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001477 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001478int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001479{
1480 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001481 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001482
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001483 if (!desc)
1484 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001485 chip = desc->chip;
1486 offset = gpio_chip_hwgpio(desc);
1487 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1488}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001489EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001490
Linus Walleijd468bf92013-09-24 11:54:38 +02001491/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001492 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001493 * @chip: the chip the GPIO to lock belongs to
1494 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001495 *
1496 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001497 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001498 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001499int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001500{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001501 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001502 return -EINVAL;
1503
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001504 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1505 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001506 "%s: tried to flag a GPIO set as output for IRQ\n",
1507 __func__);
1508 return -EIO;
1509 }
1510
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001511 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001512 return 0;
1513}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001514EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001515
Linus Walleijd468bf92013-09-24 11:54:38 +02001516/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001517 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001518 * @chip: the chip the GPIO to lock belongs to
1519 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001520 *
1521 * This is used directly by GPIO drivers that want to indicate
1522 * that a certain GPIO is no longer used exclusively for IRQ.
1523 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001524void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001525{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001526 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001527 return;
1528
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001529 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001530}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001531EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001532
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001533/**
1534 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1535 * @desc: gpio whose value will be returned
1536 *
1537 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1538 * its ACTIVE_LOW status.
1539 *
1540 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001541 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001542int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001543{
David Brownelld2876d02008-02-04 22:28:20 -08001544 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001545 if (!desc)
1546 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001547 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001548}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001549EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001550
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001551/**
1552 * gpiod_get_value_cansleep() - return a gpio's value
1553 * @desc: gpio whose value will be returned
1554 *
1555 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1556 * account.
1557 *
1558 * This function is to be called from contexts that can sleep.
1559 */
1560int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001561{
David Brownelld2876d02008-02-04 22:28:20 -08001562 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001563
1564 might_sleep_if(extra_checks);
1565 if (!desc)
1566 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001567
1568 value = _gpiod_get_raw_value(desc);
1569 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1570 value = !value;
1571
David Brownelld2876d02008-02-04 22:28:20 -08001572 return value;
1573}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001574EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001575
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001576/**
1577 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1578 * @desc: gpio whose value will be assigned
1579 * @value: value to assign
1580 *
1581 * Set the raw value of the GPIO, i.e. the value of its physical line without
1582 * regard for its ACTIVE_LOW status.
1583 *
1584 * This function is to be called from contexts that can sleep.
1585 */
1586void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001587{
David Brownelld2876d02008-02-04 22:28:20 -08001588 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001589 if (!desc)
1590 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001591 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001592}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001593EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001594
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001595/**
1596 * gpiod_set_value_cansleep() - assign a gpio's value
1597 * @desc: gpio whose value will be assigned
1598 * @value: value to assign
1599 *
1600 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1601 * account
1602 *
1603 * This function is to be called from contexts that can sleep.
1604 */
1605void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001606{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001607 might_sleep_if(extra_checks);
1608 if (!desc)
1609 return;
1610
1611 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1612 value = !value;
1613 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001614}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001615EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001616
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001617/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001618 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001619 * @array_size: number of elements in the descriptor / value arrays
1620 * @desc_array: array of GPIO descriptors whose values will be assigned
1621 * @value_array: array of values to assign
1622 *
1623 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1624 * without regard for their ACTIVE_LOW status.
1625 *
1626 * This function is to be called from contexts that can sleep.
1627 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001628void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1629 struct gpio_desc **desc_array,
1630 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001631{
1632 might_sleep_if(extra_checks);
1633 if (!desc_array)
1634 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001635 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1636 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001637}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001638EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001639
1640/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001641 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001642 * @array_size: number of elements in the descriptor / value arrays
1643 * @desc_array: array of GPIO descriptors whose values will be assigned
1644 * @value_array: array of values to assign
1645 *
1646 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1647 * into account.
1648 *
1649 * This function is to be called from contexts that can sleep.
1650 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001651void gpiod_set_array_value_cansleep(unsigned int array_size,
1652 struct gpio_desc **desc_array,
1653 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001654{
1655 might_sleep_if(extra_checks);
1656 if (!desc_array)
1657 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001658 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1659 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001660}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001661EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001662
1663/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001664 * gpiod_add_lookup_table() - register GPIO device consumers
1665 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001666 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001667void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001668{
1669 mutex_lock(&gpio_lookup_lock);
1670
Alexandre Courbotad824782013-12-03 12:20:11 +09001671 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001672
1673 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001674}
1675
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001676static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001677 unsigned int idx,
1678 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001679{
1680 char prop_name[32]; /* 32 is max size of property name */
1681 enum of_gpio_flags of_flags;
1682 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001683 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001684
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001685 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02001686 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01001687 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001688 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02001689 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01001690 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001691 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001692
Thierry Redingdd34c372014-04-23 17:28:09 +02001693 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1694 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001695 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001696 break;
1697 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001698
1699 if (IS_ERR(desc))
1700 return desc;
1701
1702 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001703 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001704
1705 return desc;
1706}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001707
Mika Westerberg81f59e92013-10-10 11:01:09 +03001708static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001709 unsigned int idx,
1710 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001711{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001712 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03001713 struct acpi_gpio_info info;
1714 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001715 char propname[32];
1716 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03001717
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001718 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001719 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001720 if (con_id && strcmp(con_id, "gpios")) {
1721 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001722 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001723 } else {
1724 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001725 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001726 }
Mika Westerberge01f4402013-10-10 11:01:10 +03001727
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001728 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1729 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1730 break;
1731 }
1732
1733 /* Then from plain _CRS GPIOs */
1734 if (IS_ERR(desc)) {
1735 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1736 if (IS_ERR(desc))
1737 return desc;
1738 }
1739
1740 if (info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001741 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001742
1743 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001744}
1745
Alexandre Courbotad824782013-12-03 12:20:11 +09001746static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1747{
1748 const char *dev_id = dev ? dev_name(dev) : NULL;
1749 struct gpiod_lookup_table *table;
1750
1751 mutex_lock(&gpio_lookup_lock);
1752
1753 list_for_each_entry(table, &gpio_lookup_list, list) {
1754 if (table->dev_id && dev_id) {
1755 /*
1756 * Valid strings on both ends, must be identical to have
1757 * a match
1758 */
1759 if (!strcmp(table->dev_id, dev_id))
1760 goto found;
1761 } else {
1762 /*
1763 * One of the pointers is NULL, so both must be to have
1764 * a match
1765 */
1766 if (dev_id == table->dev_id)
1767 goto found;
1768 }
1769 }
1770 table = NULL;
1771
1772found:
1773 mutex_unlock(&gpio_lookup_lock);
1774 return table;
1775}
1776
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001777static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001778 unsigned int idx,
1779 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001780{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001781 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001782 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001783 struct gpiod_lookup *p;
1784
Alexandre Courbotad824782013-12-03 12:20:11 +09001785 table = gpiod_find_lookup_table(dev);
1786 if (!table)
1787 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001788
Alexandre Courbotad824782013-12-03 12:20:11 +09001789 for (p = &table->table[0]; p->chip_label; p++) {
1790 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001791
Alexandre Courbotad824782013-12-03 12:20:11 +09001792 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001793 if (p->idx != idx)
1794 continue;
1795
Alexandre Courbotad824782013-12-03 12:20:11 +09001796 /* If the lookup entry has a con_id, require exact match */
1797 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1798 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001799
Alexandre Courbotad824782013-12-03 12:20:11 +09001800 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001801
Alexandre Courbotad824782013-12-03 12:20:11 +09001802 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001803 dev_err(dev, "cannot find GPIO chip %s\n",
1804 p->chip_label);
1805 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001806 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001807
Alexandre Courbotad824782013-12-03 12:20:11 +09001808 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001809 dev_err(dev,
1810 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1811 idx, chip->ngpio, chip->label);
1812 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001813 }
1814
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001815 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001816 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001817
1818 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001819 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001820
1821 return desc;
1822}
1823
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001824static int dt_gpio_count(struct device *dev, const char *con_id)
1825{
1826 int ret;
1827 char propname[32];
1828 unsigned int i;
1829
1830 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
1831 if (con_id)
1832 snprintf(propname, sizeof(propname), "%s-%s",
1833 con_id, gpio_suffixes[i]);
1834 else
1835 snprintf(propname, sizeof(propname), "%s",
1836 gpio_suffixes[i]);
1837
1838 ret = of_gpio_named_count(dev->of_node, propname);
1839 if (ret >= 0)
1840 break;
1841 }
1842 return ret;
1843}
1844
1845static int platform_gpio_count(struct device *dev, const char *con_id)
1846{
1847 struct gpiod_lookup_table *table;
1848 struct gpiod_lookup *p;
1849 unsigned int count = 0;
1850
1851 table = gpiod_find_lookup_table(dev);
1852 if (!table)
1853 return -ENOENT;
1854
1855 for (p = &table->table[0]; p->chip_label; p++) {
1856 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
1857 (!con_id && !p->con_id))
1858 count++;
1859 }
1860 if (!count)
1861 return -ENOENT;
1862
1863 return count;
1864}
1865
1866/**
1867 * gpiod_count - return the number of GPIOs associated with a device / function
1868 * or -ENOENT if no GPIO has been assigned to the requested function
1869 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1870 * @con_id: function within the GPIO consumer
1871 */
1872int gpiod_count(struct device *dev, const char *con_id)
1873{
1874 int count = -ENOENT;
1875
1876 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
1877 count = dt_gpio_count(dev, con_id);
1878 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
1879 count = acpi_gpio_count(dev, con_id);
1880
1881 if (count < 0)
1882 count = platform_gpio_count(dev, con_id);
1883
1884 return count;
1885}
1886EXPORT_SYMBOL_GPL(gpiod_count);
1887
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001888/**
Thierry Reding08791622014-04-25 16:54:22 +02001889 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09001890 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001891 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001892 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001893 *
1894 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001895 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07001896 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001897 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001898struct gpio_desc *__must_check __gpiod_get(struct device *dev, const char *con_id,
1899 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001900{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001901 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001902}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001903EXPORT_SYMBOL_GPL(__gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001904
1905/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02001906 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1907 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1908 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001909 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02001910 *
1911 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1912 * the requested function it will return NULL. This is convenient for drivers
1913 * that need to handle optional GPIOs.
1914 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001915struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
1916 const char *con_id,
1917 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02001918{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001919 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001920}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001921EXPORT_SYMBOL_GPL(__gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001922
Benoit Parrotf625d462015-02-02 11:44:44 -06001923
1924/**
1925 * gpiod_configure_flags - helper function to configure a given GPIO
1926 * @desc: gpio whose value will be assigned
1927 * @con_id: function within the GPIO consumer
1928 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
1929 * of_get_gpio_hog()
1930 * @dflags: gpiod_flags - optional GPIO initialization flags
1931 *
1932 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
1933 * requested function and/or index, or another IS_ERR() code if an error
1934 * occurred while trying to acquire the GPIO.
1935 */
1936static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
1937 unsigned long lflags, enum gpiod_flags dflags)
1938{
1939 int status;
1940
1941 if (lflags & GPIO_ACTIVE_LOW)
1942 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
1943 if (lflags & GPIO_OPEN_DRAIN)
1944 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1945 if (lflags & GPIO_OPEN_SOURCE)
1946 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
1947
1948 /* No particular flag request, return here... */
1949 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
1950 pr_debug("no flags found for %s\n", con_id);
1951 return 0;
1952 }
1953
1954 /* Process flags */
1955 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
1956 status = gpiod_direction_output(desc,
1957 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
1958 else
1959 status = gpiod_direction_input(desc);
1960
1961 return status;
1962}
1963
Thierry Reding29a1f2332014-04-25 17:10:06 +02001964/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001965 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02001966 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001967 * @con_id: function within the GPIO consumer
1968 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001969 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001970 *
1971 * This variant of gpiod_get() allows to access GPIOs other than the first
1972 * defined one for functions that define several GPIOs.
1973 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001974 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1975 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07001976 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001977 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001978struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001979 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001980 unsigned int idx,
1981 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001982{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001983 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001984 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001985 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001986
1987 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
1988
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01001989 if (dev) {
1990 /* Using device tree? */
1991 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1992 dev_dbg(dev, "using device tree for GPIO lookup\n");
1993 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
1994 } else if (ACPI_COMPANION(dev)) {
1995 dev_dbg(dev, "using ACPI for GPIO lookup\n");
1996 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
1997 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001998 }
1999
2000 /*
2001 * Either we are not using DT or ACPI, or their lookup did not return
2002 * a result. In that case, use platform lookup as a fallback.
2003 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002004 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002005 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002006 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002007 }
2008
2009 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002010 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002011 return desc;
2012 }
2013
2014 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002015 if (status < 0)
2016 return ERR_PTR(status);
2017
Benoit Parrotf625d462015-02-02 11:44:44 -06002018 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002019 if (status < 0) {
2020 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2021 gpiod_put(desc);
2022 return ERR_PTR(status);
2023 }
2024
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002025 return desc;
2026}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002027EXPORT_SYMBOL_GPL(__gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002028
2029/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002030 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2031 * @fwnode: handle of the firmware node
2032 * @propname: name of the firmware property representing the GPIO
2033 *
2034 * This function can be used for drivers that get their configuration
2035 * from firmware.
2036 *
2037 * Function properly finds the corresponding GPIO using whatever is the
2038 * underlying firmware interface and then makes sure that the GPIO
2039 * descriptor is requested before it is returned to the caller.
2040 *
2041 * In case of error an ERR_PTR() is returned.
2042 */
2043struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2044 const char *propname)
2045{
2046 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2047 bool active_low = false;
2048 int ret;
2049
2050 if (!fwnode)
2051 return ERR_PTR(-EINVAL);
2052
2053 if (is_of_node(fwnode)) {
2054 enum of_gpio_flags flags;
2055
2056 desc = of_get_named_gpiod_flags(of_node(fwnode), propname, 0,
2057 &flags);
2058 if (!IS_ERR(desc))
2059 active_low = flags & OF_GPIO_ACTIVE_LOW;
2060 } else if (is_acpi_node(fwnode)) {
2061 struct acpi_gpio_info info;
2062
2063 desc = acpi_get_gpiod_by_index(acpi_node(fwnode), propname, 0,
2064 &info);
2065 if (!IS_ERR(desc))
2066 active_low = info.active_low;
2067 }
2068
2069 if (IS_ERR(desc))
2070 return desc;
2071
2072 ret = gpiod_request(desc, NULL);
2073 if (ret)
2074 return ERR_PTR(ret);
2075
2076 /* Only value flag can be set from both DT and ACPI is active_low */
2077 if (active_low)
2078 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2079
2080 return desc;
2081}
2082EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2083
2084/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002085 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2086 * function
2087 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2088 * @con_id: function within the GPIO consumer
2089 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002090 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002091 *
2092 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2093 * specified index was assigned to the requested function it will return NULL.
2094 * This is convenient for drivers that need to handle optional GPIOs.
2095 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002096struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002097 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002098 unsigned int index,
2099 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002100{
2101 struct gpio_desc *desc;
2102
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002103 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002104 if (IS_ERR(desc)) {
2105 if (PTR_ERR(desc) == -ENOENT)
2106 return NULL;
2107 }
2108
2109 return desc;
2110}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002111EXPORT_SYMBOL_GPL(__gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002112
2113/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002114 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2115 * @desc: gpio whose value will be assigned
2116 * @name: gpio line name
2117 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2118 * of_get_gpio_hog()
2119 * @dflags: gpiod_flags - optional GPIO initialization flags
2120 */
2121int gpiod_hog(struct gpio_desc *desc, const char *name,
2122 unsigned long lflags, enum gpiod_flags dflags)
2123{
2124 struct gpio_chip *chip;
2125 struct gpio_desc *local_desc;
2126 int hwnum;
2127 int status;
2128
2129 chip = gpiod_to_chip(desc);
2130 hwnum = gpio_chip_hwgpio(desc);
2131
2132 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2133 if (IS_ERR(local_desc)) {
2134 pr_debug("requesting own GPIO %s failed\n", name);
2135 return PTR_ERR(local_desc);
2136 }
2137
2138 status = gpiod_configure_flags(desc, name, lflags, dflags);
2139 if (status < 0) {
2140 pr_debug("setup of GPIO %s failed\n", name);
2141 gpiochip_free_own_desc(desc);
2142 return status;
2143 }
2144
2145 /* Mark GPIO as hogged so it can be identified and removed later */
2146 set_bit(FLAG_IS_HOGGED, &desc->flags);
2147
2148 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2149 desc_to_gpio(desc), name,
2150 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2151 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2152 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2153
2154 return 0;
2155}
2156
2157/**
2158 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2159 * @chip: gpio chip to act on
2160 *
2161 * This is only used by of_gpiochip_remove to free hogged gpios
2162 */
2163static void gpiochip_free_hogs(struct gpio_chip *chip)
2164{
2165 int id;
2166
2167 for (id = 0; id < chip->ngpio; id++) {
2168 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2169 gpiochip_free_own_desc(&chip->desc[id]);
2170 }
2171}
2172
2173/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002174 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2175 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2176 * @con_id: function within the GPIO consumer
2177 * @flags: optional GPIO initialization flags
2178 *
2179 * This function acquires all the GPIOs defined under a given function.
2180 *
2181 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2182 * no GPIO has been assigned to the requested function, or another IS_ERR()
2183 * code if an error occurred while trying to acquire the GPIOs.
2184 */
2185struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2186 const char *con_id,
2187 enum gpiod_flags flags)
2188{
2189 struct gpio_desc *desc;
2190 struct gpio_descs *descs;
2191 int count;
2192
2193 count = gpiod_count(dev, con_id);
2194 if (count < 0)
2195 return ERR_PTR(count);
2196
2197 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2198 GFP_KERNEL);
2199 if (!descs)
2200 return ERR_PTR(-ENOMEM);
2201
2202 for (descs->ndescs = 0; descs->ndescs < count; ) {
2203 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2204 if (IS_ERR(desc)) {
2205 gpiod_put_array(descs);
2206 return ERR_CAST(desc);
2207 }
2208 descs->desc[descs->ndescs] = desc;
2209 descs->ndescs++;
2210 }
2211 return descs;
2212}
2213EXPORT_SYMBOL_GPL(gpiod_get_array);
2214
2215/**
2216 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2217 * function
2218 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2219 * @con_id: function within the GPIO consumer
2220 * @flags: optional GPIO initialization flags
2221 *
2222 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2223 * assigned to the requested function it will return NULL.
2224 */
2225struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2226 const char *con_id,
2227 enum gpiod_flags flags)
2228{
2229 struct gpio_descs *descs;
2230
2231 descs = gpiod_get_array(dev, con_id, flags);
2232 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2233 return NULL;
2234
2235 return descs;
2236}
2237EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2238
2239/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002240 * gpiod_put - dispose of a GPIO descriptor
2241 * @desc: GPIO descriptor to dispose of
2242 *
2243 * No descriptor can be used after gpiod_put() has been called on it.
2244 */
2245void gpiod_put(struct gpio_desc *desc)
2246{
2247 gpiod_free(desc);
2248}
2249EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002250
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002251/**
2252 * gpiod_put_array - dispose of multiple GPIO descriptors
2253 * @descs: struct gpio_descs containing an array of descriptors
2254 */
2255void gpiod_put_array(struct gpio_descs *descs)
2256{
2257 unsigned int i;
2258
2259 for (i = 0; i < descs->ndescs; i++)
2260 gpiod_put(descs->desc[i]);
2261
2262 kfree(descs);
2263}
2264EXPORT_SYMBOL_GPL(gpiod_put_array);
2265
David Brownelld2876d02008-02-04 22:28:20 -08002266#ifdef CONFIG_DEBUG_FS
2267
2268static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2269{
2270 unsigned i;
2271 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002272 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002273 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002274 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002275
2276 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2277 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2278 continue;
2279
Alexandre Courbot372e7222013-02-03 01:29:29 +09002280 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002281 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002282 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2283 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002284 gpio, gdesc->label,
2285 is_out ? "out" : "in ",
2286 chip->get
2287 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002288 : "? ",
2289 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002290 seq_printf(s, "\n");
2291 }
2292}
2293
Thierry Redingf9c4a312012-04-12 13:26:01 +02002294static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002295{
Grant Likely362432a2013-02-09 09:41:49 +00002296 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002297 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002298 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002299
2300 s->private = "";
2301
Grant Likely362432a2013-02-09 09:41:49 +00002302 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002303 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002304 if (index-- == 0) {
2305 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002306 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002307 }
2308 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002309
2310 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002311}
2312
2313static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2314{
Grant Likely362432a2013-02-09 09:41:49 +00002315 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002316 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002317 void *ret = NULL;
2318
Grant Likely362432a2013-02-09 09:41:49 +00002319 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002320 if (list_is_last(&chip->list, &gpio_chips))
2321 ret = NULL;
2322 else
2323 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002324 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002325
2326 s->private = "\n";
2327 ++*pos;
2328
2329 return ret;
2330}
2331
2332static void gpiolib_seq_stop(struct seq_file *s, void *v)
2333{
2334}
2335
2336static int gpiolib_seq_show(struct seq_file *s, void *v)
2337{
2338 struct gpio_chip *chip = v;
2339 struct device *dev;
2340
2341 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2342 chip->base, chip->base + chip->ngpio - 1);
2343 dev = chip->dev;
2344 if (dev)
2345 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2346 dev_name(dev));
2347 if (chip->label)
2348 seq_printf(s, ", %s", chip->label);
2349 if (chip->can_sleep)
2350 seq_printf(s, ", can sleep");
2351 seq_printf(s, ":\n");
2352
2353 if (chip->dbg_show)
2354 chip->dbg_show(s, chip);
2355 else
2356 gpiolib_dbg_show(s, chip);
2357
David Brownelld2876d02008-02-04 22:28:20 -08002358 return 0;
2359}
2360
Thierry Redingf9c4a312012-04-12 13:26:01 +02002361static const struct seq_operations gpiolib_seq_ops = {
2362 .start = gpiolib_seq_start,
2363 .next = gpiolib_seq_next,
2364 .stop = gpiolib_seq_stop,
2365 .show = gpiolib_seq_show,
2366};
2367
David Brownelld2876d02008-02-04 22:28:20 -08002368static int gpiolib_open(struct inode *inode, struct file *file)
2369{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002370 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002371}
2372
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002373static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002374 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002375 .open = gpiolib_open,
2376 .read = seq_read,
2377 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002378 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002379};
2380
2381static int __init gpiolib_debugfs_init(void)
2382{
2383 /* /sys/kernel/debug/gpio */
2384 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2385 NULL, NULL, &gpiolib_operations);
2386 return 0;
2387}
2388subsys_initcall(gpiolib_debugfs_init);
2389
2390#endif /* DEBUG_FS */