blob: 4efb92ca346311ea3821a2ee6752f7882b4de219 [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
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600288 status = gpiochip_export(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);
318
David Brownelld2876d02008-02-04 22:28:20 -0800319/**
320 * gpiochip_remove() - unregister a gpio_chip
321 * @chip: the chip to unregister
322 *
323 * A gpio_chip with any GPIOs still requested may not be removed.
324 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200325void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800326{
327 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800328 unsigned id;
329
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200330 acpi_gpiochip_remove(chip);
331
David Brownelld2876d02008-02-04 22:28:20 -0800332 spin_lock_irqsave(&gpio_lock, flags);
333
Linus Walleij14250522014-03-25 10:40:18 +0100334 gpiochip_irqchip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100335 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600336 of_gpiochip_remove(chip);
337
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900338 for (id = 0; id < chip->ngpio; id++) {
abdoulaye berthee1db1702014-07-05 18:28:50 +0200339 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags))
340 dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
David Brownelld2876d02008-02-04 22:28:20 -0800341 }
abdoulaye berthee1db1702014-07-05 18:28:50 +0200342 for (id = 0; id < chip->ngpio; id++)
343 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900344
abdoulaye berthee1db1702014-07-05 18:28:50 +0200345 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800346 spin_unlock_irqrestore(&gpio_lock, flags);
abdoulaye berthee1db1702014-07-05 18:28:50 +0200347 gpiochip_unexport(chip);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900348
349 kfree(chip->desc);
350 chip->desc = NULL;
David Brownelld2876d02008-02-04 22:28:20 -0800351}
352EXPORT_SYMBOL_GPL(gpiochip_remove);
353
Grant Likely594fa262010-06-08 07:48:16 -0600354/**
355 * gpiochip_find() - iterator for locating a specific gpio_chip
356 * @data: data to pass to match function
357 * @callback: Callback function to check gpio_chip
358 *
359 * Similar to bus_find_device. It returns a reference to a gpio_chip as
360 * determined by a user supplied @match callback. The callback should return
361 * 0 if the device doesn't match and non-zero if it does. If the callback is
362 * non-zero, this function will return to the caller and not iterate over any
363 * more gpio_chips.
364 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600365struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700366 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600367 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600368{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900369 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600370 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600371
372 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900373 list_for_each_entry(chip, &gpio_chips, list)
374 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600375 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900376
377 /* No match? */
378 if (&chip->list == &gpio_chips)
379 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600380 spin_unlock_irqrestore(&gpio_lock, flags);
381
382 return chip;
383}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600384EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800385
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900386static int gpiochip_match_name(struct gpio_chip *chip, void *data)
387{
388 const char *name = data;
389
390 return !strcmp(chip->label, name);
391}
392
393static struct gpio_chip *find_chip_by_name(const char *name)
394{
395 return gpiochip_find((void *)name, gpiochip_match_name);
396}
397
Linus Walleij14250522014-03-25 10:40:18 +0100398#ifdef CONFIG_GPIOLIB_IRQCHIP
399
400/*
401 * The following is irqchip helper code for gpiochips.
402 */
403
404/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200405 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
406 * @gpiochip: the gpiochip to set the irqchip chain to
407 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100408 * @parent_irq: the irq number corresponding to the parent IRQ for this
409 * chained irqchip
410 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200411 * coming out of the gpiochip. If the interrupt is nested rather than
412 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100413 */
414void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
415 struct irq_chip *irqchip,
416 int parent_irq,
417 irq_flow_handler_t parent_handler)
418{
Linus Walleij83141a72014-09-26 13:50:12 +0200419 unsigned int offset;
420
Linus Walleij83141a72014-09-26 13:50:12 +0200421 if (!gpiochip->irqdomain) {
422 chip_err(gpiochip, "called %s before setting up irqchip\n",
423 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200424 return;
425 }
426
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200427 if (parent_handler) {
428 if (gpiochip->can_sleep) {
429 chip_err(gpiochip,
430 "you cannot have chained interrupts on a "
431 "chip that may sleep\n");
432 return;
433 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200434 /*
435 * The parent irqchip is already using the chip_data for this
436 * irqchip, so our callbacks simply use the handler_data.
437 */
438 irq_set_handler_data(parent_irq, gpiochip);
Linus Torvaldsea584592014-10-09 14:58:15 -0400439 irq_set_chained_handler(parent_irq, parent_handler);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200440 }
Linus Walleij83141a72014-09-26 13:50:12 +0200441
442 /* Set the parent IRQ for all affected IRQs */
443 for (offset = 0; offset < gpiochip->ngpio; offset++)
444 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
445 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100446}
447EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
448
Linus Walleije45d1c82014-04-22 14:01:46 +0200449/*
450 * This lock class tells lockdep that GPIO irqs are in a different
451 * category than their parents, so it won't report false recursion.
452 */
453static struct lock_class_key gpiochip_irq_lock_class;
454
Linus Walleij14250522014-03-25 10:40:18 +0100455/**
456 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
457 * @d: the irqdomain used by this irqchip
458 * @irq: the global irq number used by this GPIO irqchip irq
459 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
460 *
461 * This function will set up the mapping for a certain IRQ line on a
462 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
463 * stored inside the gpiochip.
464 */
465static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
466 irq_hw_number_t hwirq)
467{
468 struct gpio_chip *chip = d->host_data;
469
Linus Walleij14250522014-03-25 10:40:18 +0100470 irq_set_chip_data(irq, chip);
Linus Walleije45d1c82014-04-22 14:01:46 +0200471 irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
Linus Walleij7633fb92014-04-09 13:20:38 +0200472 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200473 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300474 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200475 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100476#ifdef CONFIG_ARM
477 set_irq_flags(irq, IRQF_VALID);
478#else
479 irq_set_noprobe(irq);
480#endif
Linus Walleij1333b902014-04-23 16:45:12 +0200481 /*
482 * No set-up of the hardware will happen if IRQ_TYPE_NONE
483 * is passed as default type.
484 */
485 if (chip->irq_default_type != IRQ_TYPE_NONE)
486 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100487
488 return 0;
489}
490
Linus Walleijc3626fd2014-03-28 20:42:01 +0100491static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
492{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200493 struct gpio_chip *chip = d->host_data;
494
Linus Walleijc3626fd2014-03-28 20:42:01 +0100495#ifdef CONFIG_ARM
496 set_irq_flags(irq, 0);
497#endif
Linus Walleij1c8732b2014-04-09 13:34:39 +0200498 if (chip->can_sleep)
499 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100500 irq_set_chip_and_handler(irq, NULL, NULL);
501 irq_set_chip_data(irq, NULL);
502}
503
Linus Walleij14250522014-03-25 10:40:18 +0100504static const struct irq_domain_ops gpiochip_domain_ops = {
505 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100506 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100507 /* Virtually all GPIO irqchips are twocell:ed */
508 .xlate = irq_domain_xlate_twocell,
509};
510
511static int gpiochip_irq_reqres(struct irq_data *d)
512{
513 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
514
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900515 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100516 chip_err(chip,
517 "unable to lock HW IRQ %lu for IRQ\n",
518 d->hwirq);
519 return -EINVAL;
520 }
521 return 0;
522}
523
524static void gpiochip_irq_relres(struct irq_data *d)
525{
526 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
527
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900528 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleij14250522014-03-25 10:40:18 +0100529}
530
531static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
532{
533 return irq_find_mapping(chip->irqdomain, offset);
534}
535
536/**
537 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
538 * @gpiochip: the gpiochip to remove the irqchip from
539 *
540 * This is called only from gpiochip_remove()
541 */
542static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
543{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100544 unsigned int offset;
545
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300546 acpi_gpiochip_free_interrupts(gpiochip);
547
Linus Walleijc3626fd2014-03-28 20:42:01 +0100548 /* Remove all IRQ mappings and delete the domain */
549 if (gpiochip->irqdomain) {
550 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300551 irq_dispose_mapping(
552 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100553 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100554 }
Linus Walleij14250522014-03-25 10:40:18 +0100555
556 if (gpiochip->irqchip) {
557 gpiochip->irqchip->irq_request_resources = NULL;
558 gpiochip->irqchip->irq_release_resources = NULL;
559 gpiochip->irqchip = NULL;
560 }
561}
562
563/**
564 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
565 * @gpiochip: the gpiochip to add the irqchip to
566 * @irqchip: the irqchip to add to the gpiochip
567 * @first_irq: if not dynamically assigned, the base (first) IRQ to
568 * allocate gpiochip irqs from
569 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200570 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
571 * to have the core avoid setting up any default type in the hardware.
Linus Walleij14250522014-03-25 10:40:18 +0100572 *
573 * This function closely associates a certain irqchip with a certain
574 * gpiochip, providing an irq domain to translate the local IRQs to
575 * global irqs in the gpiolib core, and making sure that the gpiochip
576 * is passed as chip data to all related functions. Driver callbacks
577 * need to use container_of() to get their local state containers back
578 * from the gpiochip passed as chip data. An irqdomain will be stored
579 * in the gpiochip that shall be used by the driver to handle IRQ number
580 * translation. The gpiochip will need to be initialized and registered
581 * before calling this function.
582 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100583 * This function will handle two cell:ed simple IRQs and assumes all
584 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100585 * need to be open coded.
586 */
587int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
588 struct irq_chip *irqchip,
589 unsigned int first_irq,
590 irq_flow_handler_t handler,
591 unsigned int type)
592{
593 struct device_node *of_node;
594 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100595 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100596
597 if (!gpiochip || !irqchip)
598 return -EINVAL;
599
600 if (!gpiochip->dev) {
601 pr_err("missing gpiochip .dev parent pointer\n");
602 return -EINVAL;
603 }
604 of_node = gpiochip->dev->of_node;
605#ifdef CONFIG_OF_GPIO
606 /*
607 * If the gpiochip has an assigned OF node this takes precendence
608 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
609 */
610 if (gpiochip->of_node)
611 of_node = gpiochip->of_node;
612#endif
613 gpiochip->irqchip = irqchip;
614 gpiochip->irq_handler = handler;
615 gpiochip->irq_default_type = type;
616 gpiochip->to_irq = gpiochip_to_irq;
617 gpiochip->irqdomain = irq_domain_add_simple(of_node,
618 gpiochip->ngpio, first_irq,
619 &gpiochip_domain_ops, gpiochip);
620 if (!gpiochip->irqdomain) {
621 gpiochip->irqchip = NULL;
622 return -EINVAL;
623 }
624 irqchip->irq_request_resources = gpiochip_irq_reqres;
625 irqchip->irq_release_resources = gpiochip_irq_relres;
626
627 /*
628 * Prepare the mapping since the irqchip shall be orthogonal to
629 * any gpiochip calls. If the first_irq was zero, this is
630 * necessary to allocate descriptors for all IRQs.
631 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100632 for (offset = 0; offset < gpiochip->ngpio; offset++) {
633 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
634 if (offset == 0)
635 /*
636 * Store the base into the gpiochip to be used when
637 * unmapping the irqs.
638 */
639 gpiochip->irq_base = irq_base;
640 }
Linus Walleij14250522014-03-25 10:40:18 +0100641
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300642 acpi_gpiochip_request_interrupts(gpiochip);
643
Linus Walleij14250522014-03-25 10:40:18 +0100644 return 0;
645}
646EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
647
648#else /* CONFIG_GPIOLIB_IRQCHIP */
649
650static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
651
652#endif /* CONFIG_GPIOLIB_IRQCHIP */
653
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530654#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100655
Linus Walleij3f0f8672012-11-20 12:40:15 +0100656/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200657 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
658 * @chip: the gpiochip to add the range for
659 * @pinctrl: the dev_name() of the pin controller to map to
660 * @gpio_offset: the start offset in the current gpio_chip number space
661 * @pin_group: name of the pin group inside the pin controller
662 */
663int gpiochip_add_pingroup_range(struct gpio_chip *chip,
664 struct pinctrl_dev *pctldev,
665 unsigned int gpio_offset, const char *pin_group)
666{
667 struct gpio_pin_range *pin_range;
668 int ret;
669
670 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
671 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200672 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200673 return -ENOMEM;
674 }
675
676 /* Use local offset as range ID */
677 pin_range->range.id = gpio_offset;
678 pin_range->range.gc = chip;
679 pin_range->range.name = chip->label;
680 pin_range->range.base = chip->base + gpio_offset;
681 pin_range->pctldev = pctldev;
682
683 ret = pinctrl_get_group_pins(pctldev, pin_group,
684 &pin_range->range.pins,
685 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100686 if (ret < 0) {
687 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200688 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100689 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200690
691 pinctrl_add_gpio_range(pctldev, &pin_range->range);
692
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200693 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
694 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200695 pinctrl_dev_get_devname(pctldev), pin_group);
696
697 list_add_tail(&pin_range->node, &chip->pin_ranges);
698
699 return 0;
700}
701EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
702
703/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100704 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
705 * @chip: the gpiochip to add the range for
706 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100707 * @gpio_offset: the start offset in the current gpio_chip number space
708 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100709 * @npins: the number of pins from the offset of each pin space (GPIO and
710 * pin controller) to accumulate in this range
711 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100712int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100713 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100714 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530715{
716 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800717 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530718
Linus Walleij3f0f8672012-11-20 12:40:15 +0100719 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530720 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200721 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100722 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530723 }
724
Linus Walleij3f0f8672012-11-20 12:40:15 +0100725 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100726 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100727 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530728 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100729 pin_range->range.base = chip->base + gpio_offset;
730 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530731 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100732 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530733 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100734 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800735 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200736 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100737 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800738 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100739 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200740 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
741 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100742 pinctl_name,
743 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530744
745 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100746
747 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530748}
Linus Walleij165adc92012-11-06 14:49:39 +0100749EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530750
Linus Walleij3f0f8672012-11-20 12:40:15 +0100751/**
752 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
753 * @chip: the chip to remove all the mappings for
754 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530755void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
756{
757 struct gpio_pin_range *pin_range, *tmp;
758
759 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
760 list_del(&pin_range->node);
761 pinctrl_remove_gpio_range(pin_range->pctldev,
762 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100763 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530764 }
765}
Linus Walleij165adc92012-11-06 14:49:39 +0100766EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
767
768#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530769
David Brownelld2876d02008-02-04 22:28:20 -0800770/* These "optional" allocation calls help prevent drivers from stomping
771 * on each other, and help provide better diagnostics in debugfs.
772 * They're called even less than the "set direction" calls.
773 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200774static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800775{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200776 struct gpio_chip *chip = desc->chip;
777 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800778 unsigned long flags;
779
780 spin_lock_irqsave(&gpio_lock, flags);
781
David Brownelld2876d02008-02-04 22:28:20 -0800782 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700783 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800784 */
785
786 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
787 desc_set_label(desc, label ? : "?");
788 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700789 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800790 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800791 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700792 }
793
794 if (chip->request) {
795 /* chip->request may sleep */
796 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900797 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700798 spin_lock_irqsave(&gpio_lock, flags);
799
800 if (status < 0) {
801 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700802 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300803 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700804 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700805 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300806 if (chip->get_direction) {
807 /* chip->get_direction may sleep */
808 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900809 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300810 spin_lock_irqsave(&gpio_lock, flags);
811 }
David Brownelld2876d02008-02-04 22:28:20 -0800812done:
Mika Westerberg77c2d792014-03-10 14:54:50 +0200813 spin_unlock_irqrestore(&gpio_lock, flags);
814 return status;
815}
816
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900817int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200818{
819 int status = -EPROBE_DEFER;
820 struct gpio_chip *chip;
821
822 if (!desc) {
823 pr_warn("%s: invalid GPIO\n", __func__);
824 return -EINVAL;
825 }
826
827 chip = desc->chip;
828 if (!chip)
829 goto done;
830
831 if (try_module_get(chip->owner)) {
832 status = __gpiod_request(desc, label);
833 if (status < 0)
834 module_put(chip->owner);
835 }
836
837done:
David Brownelld2876d02008-02-04 22:28:20 -0800838 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200839 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200840
David Brownelld2876d02008-02-04 22:28:20 -0800841 return status;
842}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900843
Mika Westerberg77c2d792014-03-10 14:54:50 +0200844static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800845{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200846 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -0800847 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -0700848 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800849
Uwe Kleine-König3d599d12008-10-15 22:03:12 -0700850 might_sleep();
851
Alexandre Courbot372e7222013-02-03 01:29:29 +0900852 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -0700853
David Brownelld2876d02008-02-04 22:28:20 -0800854 spin_lock_irqsave(&gpio_lock, flags);
855
David Brownell35e8bb52008-10-15 22:03:16 -0700856 chip = desc->chip;
857 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
858 if (chip->free) {
859 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -0700860 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900861 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700862 spin_lock_irqsave(&gpio_lock, flags);
863 }
David Brownelld2876d02008-02-04 22:28:20 -0800864 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -0800865 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -0700866 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530867 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +0530868 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200869 ret = true;
870 }
David Brownelld2876d02008-02-04 22:28:20 -0800871
872 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200873 return ret;
874}
875
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900876void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200877{
878 if (desc && __gpiod_free(desc))
879 module_put(desc->chip->owner);
880 else
881 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -0800882}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900883
David Brownelld2876d02008-02-04 22:28:20 -0800884/**
885 * gpiochip_is_requested - return string iff signal was requested
886 * @chip: controller managing the signal
887 * @offset: of signal within controller's 0..(ngpio - 1) range
888 *
889 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +0900890 * The string returned is the label passed to gpio_request(); if none has been
891 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -0800892 *
893 * This function is for use by GPIO controller drivers. The label can
894 * help with diagnostics, and knowing that the signal is used as a GPIO
895 * can help avoid accidentally multiplexing it to another controller.
896 */
897const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
898{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900899 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800900
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900901 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -0800902 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900903
904 desc = &chip->desc[offset];
905
Alexandre Courbot372e7222013-02-03 01:29:29 +0900906 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -0800907 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900908 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -0800909}
910EXPORT_SYMBOL_GPL(gpiochip_is_requested);
911
Mika Westerberg77c2d792014-03-10 14:54:50 +0200912/**
913 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
914 * @desc: GPIO descriptor to request
915 * @label: label for the GPIO
916 *
917 * Function allows GPIO chip drivers to request and use their own GPIO
918 * descriptors via gpiolib API. Difference to gpiod_request() is that this
919 * function will not increase reference count of the GPIO chip module. This
920 * allows the GPIO chip module to be unloaded as needed (we assume that the
921 * GPIO chip driver handles freeing the GPIOs it has requested).
922 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700923struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
924 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200925{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700926 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
927 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200928
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700929 if (IS_ERR(desc)) {
930 chip_err(chip, "failed to get GPIO descriptor\n");
931 return desc;
932 }
933
934 err = __gpiod_request(desc, label);
935 if (err < 0)
936 return ERR_PTR(err);
937
938 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200939}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700940EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200941
942/**
943 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
944 * @desc: GPIO descriptor to free
945 *
946 * Function frees the given GPIO requested previously with
947 * gpiochip_request_own_desc().
948 */
949void gpiochip_free_own_desc(struct gpio_desc *desc)
950{
951 if (desc)
952 __gpiod_free(desc);
953}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700954EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -0800955
956/* Drivers MUST set GPIO direction before making get/set calls. In
957 * some cases this is done in early boot, before IRQs are enabled.
958 *
959 * As a rule these aren't called more than once (except for drivers
960 * using the open-drain emulation idiom) so these are natural places
961 * to accumulate extra debugging checks. Note that we can't (yet)
962 * rely on gpio_request() having been called beforehand.
963 */
964
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700965/**
966 * gpiod_direction_input - set the GPIO direction to input
967 * @desc: GPIO to set to input
968 *
969 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
970 * be called safely on it.
971 *
972 * Return 0 in case of success, else an error code.
973 */
974int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800975{
David Brownelld2876d02008-02-04 22:28:20 -0800976 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800977 int status = -EINVAL;
978
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200979 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900980 pr_warn("%s: invalid GPIO\n", __func__);
981 return -EINVAL;
982 }
983
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200984 chip = desc->chip;
985 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +0100986 gpiod_warn(desc,
987 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +0200988 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200989 return -EIO;
990 }
991
Alexandre Courbotd82da792014-07-22 16:17:43 +0900992 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -0800993 if (status == 0)
994 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -0600995
Alexandre Courbot372e7222013-02-03 01:29:29 +0900996 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +0900997
David Brownelld2876d02008-02-04 22:28:20 -0800998 return status;
999}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001000EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001001
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001002static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001003{
David Brownelld2876d02008-02-04 22:28:20 -08001004 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001005 int status = -EINVAL;
1006
Linus Walleijd468bf92013-09-24 11:54:38 +02001007 /* GPIOs used for IRQs shall not be set as output */
1008 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1009 gpiod_err(desc,
1010 "%s: tried to set a GPIO tied to an IRQ as output\n",
1011 __func__);
1012 return -EIO;
1013 }
1014
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301015 /* Open drain pin should not be driven to 1 */
1016 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001017 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301018
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301019 /* Open source pin should not be driven to 0 */
1020 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001021 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301022
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001023 chip = desc->chip;
1024 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001025 gpiod_warn(desc,
1026 "%s: missing set() or direction_output() operations\n",
1027 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001028 return -EIO;
1029 }
1030
Alexandre Courbotd82da792014-07-22 16:17:43 +09001031 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001032 if (status == 0)
1033 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001034 trace_gpio_value(desc_to_gpio(desc), 0, value);
1035 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001036 return status;
1037}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001038
1039/**
1040 * gpiod_direction_output_raw - set the GPIO direction to output
1041 * @desc: GPIO to set to output
1042 * @value: initial output value of the GPIO
1043 *
1044 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1045 * be called safely on it. The initial value of the output must be specified
1046 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1047 *
1048 * Return 0 in case of success, else an error code.
1049 */
1050int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1051{
1052 if (!desc || !desc->chip) {
1053 pr_warn("%s: invalid GPIO\n", __func__);
1054 return -EINVAL;
1055 }
1056 return _gpiod_direction_output_raw(desc, value);
1057}
1058EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1059
1060/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301061 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001062 * @desc: GPIO to set to output
1063 * @value: initial output value of the GPIO
1064 *
1065 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1066 * be called safely on it. The initial value of the output must be specified
1067 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1068 * account.
1069 *
1070 * Return 0 in case of success, else an error code.
1071 */
1072int gpiod_direction_output(struct gpio_desc *desc, int value)
1073{
1074 if (!desc || !desc->chip) {
1075 pr_warn("%s: invalid GPIO\n", __func__);
1076 return -EINVAL;
1077 }
1078 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1079 value = !value;
1080 return _gpiod_direction_output_raw(desc, value);
1081}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001082EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001083
Felipe Balbic4b5be92010-05-26 14:42:23 -07001084/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001085 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001086 * @gpio: the gpio to set debounce time
1087 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001088 *
1089 * returns -ENOTSUPP if the controller does not support setting
1090 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001091 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001092int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001093{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001094 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001095
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001096 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001097 pr_warn("%s: invalid GPIO\n", __func__);
1098 return -EINVAL;
1099 }
1100
Felipe Balbic4b5be92010-05-26 14:42:23 -07001101 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001102 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001103 gpiod_dbg(desc,
1104 "%s: missing set() or set_debounce() operations\n",
1105 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001106 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001107 }
1108
Alexandre Courbotd82da792014-07-22 16:17:43 +09001109 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001110}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001111EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001112
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001113/**
1114 * gpiod_is_active_low - test whether a GPIO is active-low or not
1115 * @desc: the gpio descriptor to test
1116 *
1117 * Returns 1 if the GPIO is active-low, 0 otherwise.
1118 */
1119int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001120{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001121 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001122}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001123EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001124
1125/* I/O calls are only valid after configuration completed; the relevant
1126 * "is this a valid GPIO" error checks should already have been done.
1127 *
1128 * "Get" operations are often inlinable as reading a pin value register,
1129 * and masking the relevant bit in that register.
1130 *
1131 * When "set" operations are inlinable, they involve writing that mask to
1132 * one register to set a low value, or a different register to set it high.
1133 * Otherwise locking is needed, so there may be little value to inlining.
1134 *
1135 *------------------------------------------------------------------------
1136 *
1137 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1138 * have requested the GPIO. That can include implicit requesting by
1139 * a direction setting call. Marking a gpio as requested locks its chip
1140 * in memory, guaranteeing that these table lookups need no more locking
1141 * and that gpiochip_remove() will fail.
1142 *
1143 * REVISIT when debugging, consider adding some instrumentation to ensure
1144 * that the GPIO was actually requested.
1145 */
1146
Alexandre Courbot23600962014-03-11 15:52:09 +09001147static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001148{
1149 struct gpio_chip *chip;
Alexandre Courbot23600962014-03-11 15:52:09 +09001150 bool value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001151 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001152
Alexandre Courbot372e7222013-02-03 01:29:29 +09001153 chip = desc->chip;
1154 offset = gpio_chip_hwgpio(desc);
Alexandre Courbot23600962014-03-11 15:52:09 +09001155 value = chip->get ? chip->get(chip, offset) : false;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001156 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001157 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001158}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001159
David Brownelld2876d02008-02-04 22:28:20 -08001160/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001161 * gpiod_get_raw_value() - return a gpio's raw value
1162 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001163 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001164 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1165 * its ACTIVE_LOW status.
1166 *
1167 * This function should be called from contexts where we cannot sleep, and will
1168 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001169 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001170int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001171{
David Brownelld2876d02008-02-04 22:28:20 -08001172 if (!desc)
1173 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001174 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001175 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001176 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001177}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001178EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001179
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001180/**
1181 * gpiod_get_value() - return a gpio's value
1182 * @desc: gpio whose value will be returned
1183 *
1184 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1185 * account.
1186 *
1187 * This function should be called from contexts where we cannot sleep, and will
1188 * complain if the GPIO chip functions potentially sleep.
1189 */
1190int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001191{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001192 int value;
1193 if (!desc)
1194 return 0;
1195 /* Should be using gpio_get_value_cansleep() */
1196 WARN_ON(desc->chip->can_sleep);
1197
1198 value = _gpiod_get_raw_value(desc);
1199 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1200 value = !value;
1201
1202 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001203}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001204EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001205
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301206/*
1207 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001208 * @desc: gpio descriptor whose state need to be set.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301209 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1210 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001211static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301212{
1213 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001214 struct gpio_chip *chip = desc->chip;
1215 int offset = gpio_chip_hwgpio(desc);
1216
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301217 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001218 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301219 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001220 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301221 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001222 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301223 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001224 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301225 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001226 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301227 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001228 gpiod_err(desc,
1229 "%s: Error in set_value for open drain err %d\n",
1230 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301231}
1232
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301233/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001234 * _gpio_set_open_source_value() - Set the open source gpio's value.
1235 * @desc: gpio descriptor whose state need to be set.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301236 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1237 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001238static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301239{
1240 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001241 struct gpio_chip *chip = desc->chip;
1242 int offset = gpio_chip_hwgpio(desc);
1243
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301244 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001245 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301246 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001247 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301248 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001249 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301250 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001251 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301252 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001253 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301254 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001255 gpiod_err(desc,
1256 "%s: Error in set_value for open source err %d\n",
1257 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301258}
1259
Alexandre Courbot23600962014-03-11 15:52:09 +09001260static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001261{
1262 struct gpio_chip *chip;
1263
Alexandre Courbot372e7222013-02-03 01:29:29 +09001264 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001265 trace_gpio_value(desc_to_gpio(desc), 0, value);
1266 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1267 _gpio_set_open_drain_value(desc, value);
1268 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1269 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301270 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001271 chip->set(chip, gpio_chip_hwgpio(desc), value);
1272}
1273
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001274/*
1275 * set multiple outputs on the same chip;
1276 * use the chip's set_multiple function if available;
1277 * otherwise set the outputs sequentially;
1278 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1279 * defines which outputs are to be changed
1280 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1281 * defines the values the outputs specified by mask are to be set to
1282 */
1283static void gpio_chip_set_multiple(struct gpio_chip *chip,
1284 unsigned long *mask, unsigned long *bits)
1285{
1286 if (chip->set_multiple) {
1287 chip->set_multiple(chip, mask, bits);
1288 } else {
1289 int i;
1290 for (i = 0; i < chip->ngpio; i++) {
1291 if (mask[BIT_WORD(i)] == 0) {
1292 /* no more set bits in this mask word;
1293 * skip ahead to the next word */
1294 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1295 continue;
1296 }
1297 /* set outputs if the corresponding mask bit is set */
1298 if (__test_and_clear_bit(i, mask)) {
1299 chip->set(chip, i, test_bit(i, bits));
1300 }
1301 }
1302 }
1303}
1304
1305static void gpiod_set_array_priv(bool raw, bool can_sleep,
1306 unsigned int array_size,
1307 struct gpio_desc **desc_array,
1308 int *value_array)
1309{
1310 int i = 0;
1311
1312 while (i < array_size) {
1313 struct gpio_chip *chip = desc_array[i]->chip;
1314 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1315 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1316 int count = 0;
1317
1318 if (!can_sleep) {
1319 WARN_ON(chip->can_sleep);
1320 }
1321 memset(mask, 0, sizeof(mask));
1322 do {
1323 struct gpio_desc *desc = desc_array[i];
1324 int hwgpio = gpio_chip_hwgpio(desc);
1325 int value = value_array[i];
1326
1327 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1328 value = !value;
1329 trace_gpio_value(desc_to_gpio(desc), 0, value);
1330 /*
1331 * collect all normal outputs belonging to the same chip
1332 * open drain and open source outputs are set individually
1333 */
1334 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
1335 _gpio_set_open_drain_value(desc,value);
1336 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1337 _gpio_set_open_source_value(desc, value);
1338 } else {
1339 __set_bit(hwgpio, mask);
1340 if (value) {
1341 __set_bit(hwgpio, bits);
1342 } else {
1343 __clear_bit(hwgpio, bits);
1344 }
1345 count++;
1346 }
1347 i++;
1348 } while ((i < array_size) && (desc_array[i]->chip == chip));
1349 /* push collected bits to outputs */
1350 if (count != 0) {
1351 gpio_chip_set_multiple(chip, mask, bits);
1352 }
1353 }
1354}
1355
David Brownelld2876d02008-02-04 22:28:20 -08001356/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001357 * gpiod_set_raw_value() - assign a gpio's raw value
1358 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001359 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001360 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001361 * Set the raw value of the GPIO, i.e. the value of its physical line without
1362 * regard for its ACTIVE_LOW status.
1363 *
1364 * This function should be called from contexts where we cannot sleep, and will
1365 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001366 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001367void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001368{
David Brownelld2876d02008-02-04 22:28:20 -08001369 if (!desc)
1370 return;
David Brownelld2876d02008-02-04 22:28:20 -08001371 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001372 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001373 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001374}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001375EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001376
1377/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001378 * gpiod_set_value() - assign a gpio's value
1379 * @desc: gpio whose value will be assigned
1380 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001381 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001382 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1383 * account
1384 *
1385 * This function should be called from contexts where we cannot sleep, and will
1386 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001387 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001388void gpiod_set_value(struct gpio_desc *desc, int value)
1389{
1390 if (!desc)
1391 return;
1392 /* Should be using gpio_set_value_cansleep() */
1393 WARN_ON(desc->chip->can_sleep);
1394 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1395 value = !value;
1396 _gpiod_set_raw_value(desc, value);
1397}
1398EXPORT_SYMBOL_GPL(gpiod_set_value);
1399
1400/**
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001401 * gpiod_set_raw_array() - assign values to an array of GPIOs
1402 * @array_size: number of elements in the descriptor / value arrays
1403 * @desc_array: array of GPIO descriptors whose values will be assigned
1404 * @value_array: array of values to assign
1405 *
1406 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1407 * without regard for their ACTIVE_LOW status.
1408 *
1409 * This function should be called from contexts where we cannot sleep, and will
1410 * complain if the GPIO chip functions potentially sleep.
1411 */
1412void gpiod_set_raw_array(unsigned int array_size,
1413 struct gpio_desc **desc_array, int *value_array)
1414{
1415 if (!desc_array)
1416 return;
1417 gpiod_set_array_priv(true, false, array_size, desc_array, value_array);
1418}
1419EXPORT_SYMBOL_GPL(gpiod_set_raw_array);
1420
1421/**
1422 * gpiod_set_array() - assign values to an array of GPIOs
1423 * @array_size: number of elements in the descriptor / value arrays
1424 * @desc_array: array of GPIO descriptors whose values will be assigned
1425 * @value_array: array of values to assign
1426 *
1427 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1428 * into account.
1429 *
1430 * This function should be called from contexts where we cannot sleep, and will
1431 * complain if the GPIO chip functions potentially sleep.
1432 */
1433void gpiod_set_array(unsigned int array_size,
1434 struct gpio_desc **desc_array, int *value_array)
1435{
1436 if (!desc_array)
1437 return;
1438 gpiod_set_array_priv(false, false, array_size, desc_array, value_array);
1439}
1440EXPORT_SYMBOL_GPL(gpiod_set_array);
1441
1442/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001443 * gpiod_cansleep() - report whether gpio value access may sleep
1444 * @desc: gpio to check
1445 *
1446 */
1447int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001448{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001449 if (!desc)
1450 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001451 return desc->chip->can_sleep;
1452}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001453EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001454
David Brownell0f6d5042008-10-15 22:03:14 -07001455/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001456 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1457 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001458 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001459 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1460 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001461 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001462int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001463{
1464 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001465 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001466
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001467 if (!desc)
1468 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001469 chip = desc->chip;
1470 offset = gpio_chip_hwgpio(desc);
1471 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1472}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001473EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001474
Linus Walleijd468bf92013-09-24 11:54:38 +02001475/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001476 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001477 * @chip: the chip the GPIO to lock belongs to
1478 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001479 *
1480 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001481 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001482 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001483int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001484{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001485 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001486 return -EINVAL;
1487
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001488 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1489 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001490 "%s: tried to flag a GPIO set as output for IRQ\n",
1491 __func__);
1492 return -EIO;
1493 }
1494
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001495 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001496 return 0;
1497}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001498EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001499
Linus Walleijd468bf92013-09-24 11:54:38 +02001500/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001501 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001502 * @chip: the chip the GPIO to lock belongs to
1503 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001504 *
1505 * This is used directly by GPIO drivers that want to indicate
1506 * that a certain GPIO is no longer used exclusively for IRQ.
1507 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001508void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001509{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001510 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001511 return;
1512
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001513 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001514}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001515EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001516
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001517/**
1518 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1519 * @desc: gpio whose value will be returned
1520 *
1521 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1522 * its ACTIVE_LOW status.
1523 *
1524 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001525 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001526int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001527{
David Brownelld2876d02008-02-04 22:28:20 -08001528 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001529 if (!desc)
1530 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001531 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001532}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001533EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001534
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001535/**
1536 * gpiod_get_value_cansleep() - return a gpio's value
1537 * @desc: gpio whose value will be returned
1538 *
1539 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1540 * account.
1541 *
1542 * This function is to be called from contexts that can sleep.
1543 */
1544int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001545{
David Brownelld2876d02008-02-04 22:28:20 -08001546 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001547
1548 might_sleep_if(extra_checks);
1549 if (!desc)
1550 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001551
1552 value = _gpiod_get_raw_value(desc);
1553 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1554 value = !value;
1555
David Brownelld2876d02008-02-04 22:28:20 -08001556 return value;
1557}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001558EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001559
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001560/**
1561 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1562 * @desc: gpio whose value will be assigned
1563 * @value: value to assign
1564 *
1565 * Set the raw value of the GPIO, i.e. the value of its physical line without
1566 * regard for its ACTIVE_LOW status.
1567 *
1568 * This function is to be called from contexts that can sleep.
1569 */
1570void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001571{
David Brownelld2876d02008-02-04 22:28:20 -08001572 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001573 if (!desc)
1574 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001575 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001576}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001577EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001578
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001579/**
1580 * gpiod_set_value_cansleep() - assign a gpio's value
1581 * @desc: gpio whose value will be assigned
1582 * @value: value to assign
1583 *
1584 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1585 * account
1586 *
1587 * This function is to be called from contexts that can sleep.
1588 */
1589void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001590{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001591 might_sleep_if(extra_checks);
1592 if (!desc)
1593 return;
1594
1595 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1596 value = !value;
1597 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001598}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001599EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001600
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001601/**
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001602 * gpiod_set_raw_array_cansleep() - assign values to an array of GPIOs
1603 * @array_size: number of elements in the descriptor / value arrays
1604 * @desc_array: array of GPIO descriptors whose values will be assigned
1605 * @value_array: array of values to assign
1606 *
1607 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1608 * without regard for their ACTIVE_LOW status.
1609 *
1610 * This function is to be called from contexts that can sleep.
1611 */
1612void gpiod_set_raw_array_cansleep(unsigned int array_size,
1613 struct gpio_desc **desc_array,
1614 int *value_array)
1615{
1616 might_sleep_if(extra_checks);
1617 if (!desc_array)
1618 return;
1619 gpiod_set_array_priv(true, true, array_size, desc_array, value_array);
1620}
1621EXPORT_SYMBOL_GPL(gpiod_set_raw_array_cansleep);
1622
1623/**
1624 * gpiod_set_array_cansleep() - assign values to an array of GPIOs
1625 * @array_size: number of elements in the descriptor / value arrays
1626 * @desc_array: array of GPIO descriptors whose values will be assigned
1627 * @value_array: array of values to assign
1628 *
1629 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1630 * into account.
1631 *
1632 * This function is to be called from contexts that can sleep.
1633 */
1634void gpiod_set_array_cansleep(unsigned int array_size,
1635 struct gpio_desc **desc_array,
1636 int *value_array)
1637{
1638 might_sleep_if(extra_checks);
1639 if (!desc_array)
1640 return;
1641 gpiod_set_array_priv(false, true, array_size, desc_array, value_array);
1642}
1643EXPORT_SYMBOL_GPL(gpiod_set_array_cansleep);
1644
1645/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001646 * gpiod_add_lookup_table() - register GPIO device consumers
1647 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001648 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001649void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001650{
1651 mutex_lock(&gpio_lookup_lock);
1652
Alexandre Courbotad824782013-12-03 12:20:11 +09001653 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001654
1655 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001656}
1657
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001658static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001659 unsigned int idx,
1660 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001661{
Thierry Redingdd34c372014-04-23 17:28:09 +02001662 static const char *suffixes[] = { "gpios", "gpio" };
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001663 char prop_name[32]; /* 32 is max size of property name */
1664 enum of_gpio_flags of_flags;
1665 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001666 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001667
Thierry Redingdd34c372014-04-23 17:28:09 +02001668 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1669 if (con_id)
1670 snprintf(prop_name, 32, "%s-%s", con_id, suffixes[i]);
1671 else
1672 snprintf(prop_name, 32, "%s", suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001673
Thierry Redingdd34c372014-04-23 17:28:09 +02001674 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1675 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001676 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001677 break;
1678 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001679
1680 if (IS_ERR(desc))
1681 return desc;
1682
1683 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001684 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001685
1686 return desc;
1687}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001688
Mika Westerberg81f59e92013-10-10 11:01:09 +03001689static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001690 unsigned int idx,
1691 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001692{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001693 static const char * const suffixes[] = { "gpios", "gpio" };
1694 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03001695 struct acpi_gpio_info info;
1696 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001697 char propname[32];
1698 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03001699
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001700 /* Try first from _DSD */
1701 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1702 if (con_id && strcmp(con_id, "gpios")) {
1703 snprintf(propname, sizeof(propname), "%s-%s",
1704 con_id, suffixes[i]);
1705 } else {
1706 snprintf(propname, sizeof(propname), "%s",
1707 suffixes[i]);
1708 }
Mika Westerberge01f4402013-10-10 11:01:10 +03001709
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001710 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1711 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1712 break;
1713 }
1714
1715 /* Then from plain _CRS GPIOs */
1716 if (IS_ERR(desc)) {
1717 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1718 if (IS_ERR(desc))
1719 return desc;
1720 }
1721
1722 if (info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001723 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001724
1725 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001726}
1727
Alexandre Courbotad824782013-12-03 12:20:11 +09001728static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1729{
1730 const char *dev_id = dev ? dev_name(dev) : NULL;
1731 struct gpiod_lookup_table *table;
1732
1733 mutex_lock(&gpio_lookup_lock);
1734
1735 list_for_each_entry(table, &gpio_lookup_list, list) {
1736 if (table->dev_id && dev_id) {
1737 /*
1738 * Valid strings on both ends, must be identical to have
1739 * a match
1740 */
1741 if (!strcmp(table->dev_id, dev_id))
1742 goto found;
1743 } else {
1744 /*
1745 * One of the pointers is NULL, so both must be to have
1746 * a match
1747 */
1748 if (dev_id == table->dev_id)
1749 goto found;
1750 }
1751 }
1752 table = NULL;
1753
1754found:
1755 mutex_unlock(&gpio_lookup_lock);
1756 return table;
1757}
1758
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001759static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001760 unsigned int idx,
1761 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001762{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001763 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001764 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001765 struct gpiod_lookup *p;
1766
Alexandre Courbotad824782013-12-03 12:20:11 +09001767 table = gpiod_find_lookup_table(dev);
1768 if (!table)
1769 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001770
Alexandre Courbotad824782013-12-03 12:20:11 +09001771 for (p = &table->table[0]; p->chip_label; p++) {
1772 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001773
Alexandre Courbotad824782013-12-03 12:20:11 +09001774 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001775 if (p->idx != idx)
1776 continue;
1777
Alexandre Courbotad824782013-12-03 12:20:11 +09001778 /* If the lookup entry has a con_id, require exact match */
1779 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1780 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001781
Alexandre Courbotad824782013-12-03 12:20:11 +09001782 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001783
Alexandre Courbotad824782013-12-03 12:20:11 +09001784 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001785 dev_err(dev, "cannot find GPIO chip %s\n",
1786 p->chip_label);
1787 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001788 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001789
Alexandre Courbotad824782013-12-03 12:20:11 +09001790 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001791 dev_err(dev,
1792 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1793 idx, chip->ngpio, chip->label);
1794 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001795 }
1796
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001797 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001798 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001799
1800 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001801 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001802
1803 return desc;
1804}
1805
1806/**
Thierry Reding08791622014-04-25 16:54:22 +02001807 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09001808 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001809 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001810 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001811 *
1812 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001813 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1814 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001815 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001816struct gpio_desc *__must_check __gpiod_get(struct device *dev, const char *con_id,
1817 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001818{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001819 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001820}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001821EXPORT_SYMBOL_GPL(__gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001822
1823/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02001824 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1825 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1826 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001827 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02001828 *
1829 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1830 * the requested function it will return NULL. This is convenient for drivers
1831 * that need to handle optional GPIOs.
1832 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001833struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
1834 const char *con_id,
1835 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02001836{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001837 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001838}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001839EXPORT_SYMBOL_GPL(__gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001840
1841/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001842 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02001843 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001844 * @con_id: function within the GPIO consumer
1845 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001846 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001847 *
1848 * This variant of gpiod_get() allows to access GPIOs other than the first
1849 * defined one for functions that define several GPIOs.
1850 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001851 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1852 * requested function and/or index, or another IS_ERR() code if an error
1853 * occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001854 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001855struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001856 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001857 unsigned int idx,
1858 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001859{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001860 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001861 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001862 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001863
1864 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
1865
1866 /* Using device tree? */
1867 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
1868 dev_dbg(dev, "using device tree for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001869 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
Mika Westerberg81f59e92013-10-10 11:01:09 +03001870 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
1871 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001872 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001873 }
1874
1875 /*
1876 * Either we are not using DT or ACPI, or their lookup did not return
1877 * a result. In that case, use platform lookup as a fallback.
1878 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001879 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04001880 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001881 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001882 }
1883
1884 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02001885 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001886 return desc;
1887 }
1888
1889 status = gpiod_request(desc, con_id);
1890
1891 if (status < 0)
1892 return ERR_PTR(status);
1893
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001894 if (lookupflags & GPIO_ACTIVE_LOW)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001895 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001896 if (lookupflags & GPIO_OPEN_DRAIN)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001897 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001898 if (lookupflags & GPIO_OPEN_SOURCE)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001899 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001900
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001901 /* No particular flag request, return here... */
Adrian Hunter72f908c2014-09-22 11:01:16 +03001902 if (!(flags & GPIOD_FLAGS_BIT_DIR_SET))
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001903 return desc;
1904
1905 /* Process flags */
1906 if (flags & GPIOD_FLAGS_BIT_DIR_OUT)
1907 status = gpiod_direction_output(desc,
1908 flags & GPIOD_FLAGS_BIT_DIR_VAL);
1909 else
1910 status = gpiod_direction_input(desc);
1911
1912 if (status < 0) {
1913 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
1914 gpiod_put(desc);
1915 return ERR_PTR(status);
1916 }
1917
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001918 return desc;
1919}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001920EXPORT_SYMBOL_GPL(__gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001921
1922/**
Mika Westerberg40b73182014-10-21 13:33:59 +02001923 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
1924 * @fwnode: handle of the firmware node
1925 * @propname: name of the firmware property representing the GPIO
1926 *
1927 * This function can be used for drivers that get their configuration
1928 * from firmware.
1929 *
1930 * Function properly finds the corresponding GPIO using whatever is the
1931 * underlying firmware interface and then makes sure that the GPIO
1932 * descriptor is requested before it is returned to the caller.
1933 *
1934 * In case of error an ERR_PTR() is returned.
1935 */
1936struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
1937 const char *propname)
1938{
1939 struct gpio_desc *desc = ERR_PTR(-ENODEV);
1940 bool active_low = false;
1941 int ret;
1942
1943 if (!fwnode)
1944 return ERR_PTR(-EINVAL);
1945
1946 if (is_of_node(fwnode)) {
1947 enum of_gpio_flags flags;
1948
1949 desc = of_get_named_gpiod_flags(of_node(fwnode), propname, 0,
1950 &flags);
1951 if (!IS_ERR(desc))
1952 active_low = flags & OF_GPIO_ACTIVE_LOW;
1953 } else if (is_acpi_node(fwnode)) {
1954 struct acpi_gpio_info info;
1955
1956 desc = acpi_get_gpiod_by_index(acpi_node(fwnode), propname, 0,
1957 &info);
1958 if (!IS_ERR(desc))
1959 active_low = info.active_low;
1960 }
1961
1962 if (IS_ERR(desc))
1963 return desc;
1964
1965 ret = gpiod_request(desc, NULL);
1966 if (ret)
1967 return ERR_PTR(ret);
1968
1969 /* Only value flag can be set from both DT and ACPI is active_low */
1970 if (active_low)
1971 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
1972
1973 return desc;
1974}
1975EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
1976
1977/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02001978 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
1979 * function
1980 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1981 * @con_id: function within the GPIO consumer
1982 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001983 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02001984 *
1985 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
1986 * specified index was assigned to the requested function it will return NULL.
1987 * This is convenient for drivers that need to handle optional GPIOs.
1988 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001989struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02001990 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001991 unsigned int index,
1992 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02001993{
1994 struct gpio_desc *desc;
1995
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001996 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001997 if (IS_ERR(desc)) {
1998 if (PTR_ERR(desc) == -ENOENT)
1999 return NULL;
2000 }
2001
2002 return desc;
2003}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002004EXPORT_SYMBOL_GPL(__gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002005
2006/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002007 * gpiod_put - dispose of a GPIO descriptor
2008 * @desc: GPIO descriptor to dispose of
2009 *
2010 * No descriptor can be used after gpiod_put() has been called on it.
2011 */
2012void gpiod_put(struct gpio_desc *desc)
2013{
2014 gpiod_free(desc);
2015}
2016EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002017
David Brownelld2876d02008-02-04 22:28:20 -08002018#ifdef CONFIG_DEBUG_FS
2019
2020static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2021{
2022 unsigned i;
2023 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002024 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002025 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002026 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002027
2028 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2029 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2030 continue;
2031
Alexandre Courbot372e7222013-02-03 01:29:29 +09002032 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002033 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002034 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2035 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002036 gpio, gdesc->label,
2037 is_out ? "out" : "in ",
2038 chip->get
2039 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002040 : "? ",
2041 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002042 seq_printf(s, "\n");
2043 }
2044}
2045
Thierry Redingf9c4a312012-04-12 13:26:01 +02002046static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002047{
Grant Likely362432a2013-02-09 09:41:49 +00002048 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002049 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002050 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002051
2052 s->private = "";
2053
Grant Likely362432a2013-02-09 09:41:49 +00002054 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002055 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002056 if (index-- == 0) {
2057 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002058 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002059 }
2060 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002061
2062 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002063}
2064
2065static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2066{
Grant Likely362432a2013-02-09 09:41:49 +00002067 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002068 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002069 void *ret = NULL;
2070
Grant Likely362432a2013-02-09 09:41:49 +00002071 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002072 if (list_is_last(&chip->list, &gpio_chips))
2073 ret = NULL;
2074 else
2075 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002076 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002077
2078 s->private = "\n";
2079 ++*pos;
2080
2081 return ret;
2082}
2083
2084static void gpiolib_seq_stop(struct seq_file *s, void *v)
2085{
2086}
2087
2088static int gpiolib_seq_show(struct seq_file *s, void *v)
2089{
2090 struct gpio_chip *chip = v;
2091 struct device *dev;
2092
2093 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2094 chip->base, chip->base + chip->ngpio - 1);
2095 dev = chip->dev;
2096 if (dev)
2097 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2098 dev_name(dev));
2099 if (chip->label)
2100 seq_printf(s, ", %s", chip->label);
2101 if (chip->can_sleep)
2102 seq_printf(s, ", can sleep");
2103 seq_printf(s, ":\n");
2104
2105 if (chip->dbg_show)
2106 chip->dbg_show(s, chip);
2107 else
2108 gpiolib_dbg_show(s, chip);
2109
David Brownelld2876d02008-02-04 22:28:20 -08002110 return 0;
2111}
2112
Thierry Redingf9c4a312012-04-12 13:26:01 +02002113static const struct seq_operations gpiolib_seq_ops = {
2114 .start = gpiolib_seq_start,
2115 .next = gpiolib_seq_next,
2116 .stop = gpiolib_seq_stop,
2117 .show = gpiolib_seq_show,
2118};
2119
David Brownelld2876d02008-02-04 22:28:20 -08002120static int gpiolib_open(struct inode *inode, struct file *file)
2121{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002122 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002123}
2124
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002125static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002126 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002127 .open = gpiolib_open,
2128 .read = seq_read,
2129 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002130 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002131};
2132
2133static int __init gpiolib_debugfs_init(void)
2134{
2135 /* /sys/kernel/debug/gpio */
2136 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2137 NULL, NULL, &gpiolib_operations);
2138 return 0;
2139}
2140subsys_initcall(gpiolib_debugfs_init);
2141
2142#endif /* DEBUG_FS */