blob: 37f919dc2cb47832455e09a8c282e21616607c1c [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 Brownelld8f388d82008-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 Brownelld8f388d82008-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 Brownelld8f388d82008-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
Johan Hovold00acc3d2015-01-12 17:12:27 +0100330 gpiochip_irqchip_remove(chip);
331
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200332 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100333 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600334 of_gpiochip_remove(chip);
335
Johan Hovold6798aca2015-01-12 17:12:28 +0100336 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900337 for (id = 0; id < chip->ngpio; id++) {
abdoulaye berthee1db1702014-07-05 18:28:50 +0200338 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags))
339 dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
David Brownelld2876d02008-02-04 22:28:20 -0800340 }
abdoulaye berthee1db1702014-07-05 18:28:50 +0200341 for (id = 0; id < chip->ngpio; id++)
342 chip->desc[id].chip = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900343
abdoulaye berthee1db1702014-07-05 18:28:50 +0200344 list_del(&chip->list);
David Brownelld2876d02008-02-04 22:28:20 -0800345 spin_unlock_irqrestore(&gpio_lock, flags);
abdoulaye berthee1db1702014-07-05 18:28:50 +0200346 gpiochip_unexport(chip);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900347
348 kfree(chip->desc);
349 chip->desc = NULL;
David Brownelld2876d02008-02-04 22:28:20 -0800350}
351EXPORT_SYMBOL_GPL(gpiochip_remove);
352
Grant Likely594fa262010-06-08 07:48:16 -0600353/**
354 * gpiochip_find() - iterator for locating a specific gpio_chip
355 * @data: data to pass to match function
356 * @callback: Callback function to check gpio_chip
357 *
358 * Similar to bus_find_device. It returns a reference to a gpio_chip as
359 * determined by a user supplied @match callback. The callback should return
360 * 0 if the device doesn't match and non-zero if it does. If the callback is
361 * non-zero, this function will return to the caller and not iterate over any
362 * more gpio_chips.
363 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600364struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700365 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600366 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600367{
Alexandre Courbot125eef92013-02-03 01:29:26 +0900368 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600369 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600370
371 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot125eef92013-02-03 01:29:26 +0900372 list_for_each_entry(chip, &gpio_chips, list)
373 if (match(chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600374 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900375
376 /* No match? */
377 if (&chip->list == &gpio_chips)
378 chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600379 spin_unlock_irqrestore(&gpio_lock, flags);
380
381 return chip;
382}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600383EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800384
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900385static int gpiochip_match_name(struct gpio_chip *chip, void *data)
386{
387 const char *name = data;
388
389 return !strcmp(chip->label, name);
390}
391
392static struct gpio_chip *find_chip_by_name(const char *name)
393{
394 return gpiochip_find((void *)name, gpiochip_match_name);
395}
396
Linus Walleij14250522014-03-25 10:40:18 +0100397#ifdef CONFIG_GPIOLIB_IRQCHIP
398
399/*
400 * The following is irqchip helper code for gpiochips.
401 */
402
403/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200404 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
405 * @gpiochip: the gpiochip to set the irqchip chain to
406 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100407 * @parent_irq: the irq number corresponding to the parent IRQ for this
408 * chained irqchip
409 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200410 * coming out of the gpiochip. If the interrupt is nested rather than
411 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100412 */
413void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
414 struct irq_chip *irqchip,
415 int parent_irq,
416 irq_flow_handler_t parent_handler)
417{
Linus Walleij83141a72014-09-26 13:50:12 +0200418 unsigned int offset;
419
Linus Walleij83141a72014-09-26 13:50:12 +0200420 if (!gpiochip->irqdomain) {
421 chip_err(gpiochip, "called %s before setting up irqchip\n",
422 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200423 return;
424 }
425
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200426 if (parent_handler) {
427 if (gpiochip->can_sleep) {
428 chip_err(gpiochip,
429 "you cannot have chained interrupts on a "
430 "chip that may sleep\n");
431 return;
432 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200433 /*
434 * The parent irqchip is already using the chip_data for this
435 * irqchip, so our callbacks simply use the handler_data.
436 */
437 irq_set_handler_data(parent_irq, gpiochip);
Linus Torvaldsea584592014-10-09 14:58:15 -0400438 irq_set_chained_handler(parent_irq, parent_handler);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200439 }
Linus Walleij83141a72014-09-26 13:50:12 +0200440
441 /* Set the parent IRQ for all affected IRQs */
442 for (offset = 0; offset < gpiochip->ngpio; offset++)
443 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
444 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100445}
446EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
447
Linus Walleije45d1c82014-04-22 14:01:46 +0200448/*
449 * This lock class tells lockdep that GPIO irqs are in a different
450 * category than their parents, so it won't report false recursion.
451 */
452static struct lock_class_key gpiochip_irq_lock_class;
453
Linus Walleij14250522014-03-25 10:40:18 +0100454/**
455 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
456 * @d: the irqdomain used by this irqchip
457 * @irq: the global irq number used by this GPIO irqchip irq
458 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
459 *
460 * This function will set up the mapping for a certain IRQ line on a
461 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
462 * stored inside the gpiochip.
463 */
464static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
465 irq_hw_number_t hwirq)
466{
467 struct gpio_chip *chip = d->host_data;
468
Linus Walleij14250522014-03-25 10:40:18 +0100469 irq_set_chip_data(irq, chip);
Linus Walleije45d1c82014-04-22 14:01:46 +0200470 irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
Linus Walleij7633fb92014-04-09 13:20:38 +0200471 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200472 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300473 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200474 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100475#ifdef CONFIG_ARM
476 set_irq_flags(irq, IRQF_VALID);
477#else
478 irq_set_noprobe(irq);
479#endif
Linus Walleij1333b902014-04-23 16:45:12 +0200480 /*
481 * No set-up of the hardware will happen if IRQ_TYPE_NONE
482 * is passed as default type.
483 */
484 if (chip->irq_default_type != IRQ_TYPE_NONE)
485 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100486
487 return 0;
488}
489
Linus Walleijc3626fd2014-03-28 20:42:01 +0100490static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
491{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200492 struct gpio_chip *chip = d->host_data;
493
Linus Walleijc3626fd2014-03-28 20:42:01 +0100494#ifdef CONFIG_ARM
495 set_irq_flags(irq, 0);
496#endif
Linus Walleij1c8732b2014-04-09 13:34:39 +0200497 if (chip->can_sleep)
498 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100499 irq_set_chip_and_handler(irq, NULL, NULL);
500 irq_set_chip_data(irq, NULL);
501}
502
Linus Walleij14250522014-03-25 10:40:18 +0100503static const struct irq_domain_ops gpiochip_domain_ops = {
504 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100505 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100506 /* Virtually all GPIO irqchips are twocell:ed */
507 .xlate = irq_domain_xlate_twocell,
508};
509
510static int gpiochip_irq_reqres(struct irq_data *d)
511{
512 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
513
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900514 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100515 chip_err(chip,
516 "unable to lock HW IRQ %lu for IRQ\n",
517 d->hwirq);
518 return -EINVAL;
519 }
520 return 0;
521}
522
523static void gpiochip_irq_relres(struct irq_data *d)
524{
525 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
526
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900527 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleij14250522014-03-25 10:40:18 +0100528}
529
530static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
531{
532 return irq_find_mapping(chip->irqdomain, offset);
533}
534
535/**
536 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
537 * @gpiochip: the gpiochip to remove the irqchip from
538 *
539 * This is called only from gpiochip_remove()
540 */
541static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
542{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100543 unsigned int offset;
544
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300545 acpi_gpiochip_free_interrupts(gpiochip);
546
Linus Walleijc3626fd2014-03-28 20:42:01 +0100547 /* Remove all IRQ mappings and delete the domain */
548 if (gpiochip->irqdomain) {
549 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300550 irq_dispose_mapping(
551 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100552 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100553 }
Linus Walleij14250522014-03-25 10:40:18 +0100554
555 if (gpiochip->irqchip) {
556 gpiochip->irqchip->irq_request_resources = NULL;
557 gpiochip->irqchip->irq_release_resources = NULL;
558 gpiochip->irqchip = NULL;
559 }
560}
561
562/**
563 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
564 * @gpiochip: the gpiochip to add the irqchip to
565 * @irqchip: the irqchip to add to the gpiochip
566 * @first_irq: if not dynamically assigned, the base (first) IRQ to
567 * allocate gpiochip irqs from
568 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200569 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
570 * to have the core avoid setting up any default type in the hardware.
Linus Walleij14250522014-03-25 10:40:18 +0100571 *
572 * This function closely associates a certain irqchip with a certain
573 * gpiochip, providing an irq domain to translate the local IRQs to
574 * global irqs in the gpiolib core, and making sure that the gpiochip
575 * is passed as chip data to all related functions. Driver callbacks
576 * need to use container_of() to get their local state containers back
577 * from the gpiochip passed as chip data. An irqdomain will be stored
578 * in the gpiochip that shall be used by the driver to handle IRQ number
579 * translation. The gpiochip will need to be initialized and registered
580 * before calling this function.
581 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100582 * This function will handle two cell:ed simple IRQs and assumes all
583 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100584 * need to be open coded.
585 */
586int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
587 struct irq_chip *irqchip,
588 unsigned int first_irq,
589 irq_flow_handler_t handler,
590 unsigned int type)
591{
592 struct device_node *of_node;
593 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100594 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100595
596 if (!gpiochip || !irqchip)
597 return -EINVAL;
598
599 if (!gpiochip->dev) {
600 pr_err("missing gpiochip .dev parent pointer\n");
601 return -EINVAL;
602 }
603 of_node = gpiochip->dev->of_node;
604#ifdef CONFIG_OF_GPIO
605 /*
606 * If the gpiochip has an assigned OF node this takes precendence
607 * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
608 */
609 if (gpiochip->of_node)
610 of_node = gpiochip->of_node;
611#endif
612 gpiochip->irqchip = irqchip;
613 gpiochip->irq_handler = handler;
614 gpiochip->irq_default_type = type;
615 gpiochip->to_irq = gpiochip_to_irq;
616 gpiochip->irqdomain = irq_domain_add_simple(of_node,
617 gpiochip->ngpio, first_irq,
618 &gpiochip_domain_ops, gpiochip);
619 if (!gpiochip->irqdomain) {
620 gpiochip->irqchip = NULL;
621 return -EINVAL;
622 }
623 irqchip->irq_request_resources = gpiochip_irq_reqres;
624 irqchip->irq_release_resources = gpiochip_irq_relres;
625
626 /*
627 * Prepare the mapping since the irqchip shall be orthogonal to
628 * any gpiochip calls. If the first_irq was zero, this is
629 * necessary to allocate descriptors for all IRQs.
630 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100631 for (offset = 0; offset < gpiochip->ngpio; offset++) {
632 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
633 if (offset == 0)
634 /*
635 * Store the base into the gpiochip to be used when
636 * unmapping the irqs.
637 */
638 gpiochip->irq_base = irq_base;
639 }
Linus Walleij14250522014-03-25 10:40:18 +0100640
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300641 acpi_gpiochip_request_interrupts(gpiochip);
642
Linus Walleij14250522014-03-25 10:40:18 +0100643 return 0;
644}
645EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
646
647#else /* CONFIG_GPIOLIB_IRQCHIP */
648
649static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
650
651#endif /* CONFIG_GPIOLIB_IRQCHIP */
652
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530653#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100654
Linus Walleij3f0f8672012-11-20 12:40:15 +0100655/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200656 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
657 * @chip: the gpiochip to add the range for
658 * @pinctrl: the dev_name() of the pin controller to map to
659 * @gpio_offset: the start offset in the current gpio_chip number space
660 * @pin_group: name of the pin group inside the pin controller
661 */
662int gpiochip_add_pingroup_range(struct gpio_chip *chip,
663 struct pinctrl_dev *pctldev,
664 unsigned int gpio_offset, const char *pin_group)
665{
666 struct gpio_pin_range *pin_range;
667 int ret;
668
669 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
670 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200671 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200672 return -ENOMEM;
673 }
674
675 /* Use local offset as range ID */
676 pin_range->range.id = gpio_offset;
677 pin_range->range.gc = chip;
678 pin_range->range.name = chip->label;
679 pin_range->range.base = chip->base + gpio_offset;
680 pin_range->pctldev = pctldev;
681
682 ret = pinctrl_get_group_pins(pctldev, pin_group,
683 &pin_range->range.pins,
684 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100685 if (ret < 0) {
686 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200687 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100688 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200689
690 pinctrl_add_gpio_range(pctldev, &pin_range->range);
691
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200692 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
693 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200694 pinctrl_dev_get_devname(pctldev), pin_group);
695
696 list_add_tail(&pin_range->node, &chip->pin_ranges);
697
698 return 0;
699}
700EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
701
702/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100703 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
704 * @chip: the gpiochip to add the range for
705 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100706 * @gpio_offset: the start offset in the current gpio_chip number space
707 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100708 * @npins: the number of pins from the offset of each pin space (GPIO and
709 * pin controller) to accumulate in this range
710 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100711int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100712 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100713 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530714{
715 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800716 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530717
Linus Walleij3f0f8672012-11-20 12:40:15 +0100718 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530719 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200720 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100721 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530722 }
723
Linus Walleij3f0f8672012-11-20 12:40:15 +0100724 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100725 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100726 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530727 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100728 pin_range->range.base = chip->base + gpio_offset;
729 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530730 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100731 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530732 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100733 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800734 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200735 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100736 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800737 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100738 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200739 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
740 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100741 pinctl_name,
742 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530743
744 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100745
746 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530747}
Linus Walleij165adc92012-11-06 14:49:39 +0100748EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530749
Linus Walleij3f0f8672012-11-20 12:40:15 +0100750/**
751 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
752 * @chip: the chip to remove all the mappings for
753 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530754void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
755{
756 struct gpio_pin_range *pin_range, *tmp;
757
758 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
759 list_del(&pin_range->node);
760 pinctrl_remove_gpio_range(pin_range->pctldev,
761 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100762 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530763 }
764}
Linus Walleij165adc92012-11-06 14:49:39 +0100765EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
766
767#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530768
David Brownelld2876d02008-02-04 22:28:20 -0800769/* These "optional" allocation calls help prevent drivers from stomping
770 * on each other, and help provide better diagnostics in debugfs.
771 * They're called even less than the "set direction" calls.
772 */
Mika Westerberg77c2d792014-03-10 14:54:50 +0200773static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -0800774{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200775 struct gpio_chip *chip = desc->chip;
776 int status;
David Brownelld2876d02008-02-04 22:28:20 -0800777 unsigned long flags;
778
779 spin_lock_irqsave(&gpio_lock, flags);
780
David Brownelld2876d02008-02-04 22:28:20 -0800781 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -0700782 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800783 */
784
785 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
786 desc_set_label(desc, label ? : "?");
787 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700788 } else {
David Brownelld2876d02008-02-04 22:28:20 -0800789 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -0800790 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700791 }
792
793 if (chip->request) {
794 /* chip->request may sleep */
795 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900796 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700797 spin_lock_irqsave(&gpio_lock, flags);
798
799 if (status < 0) {
800 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -0700801 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300802 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -0700803 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -0700804 }
Mathias Nyman80b0a602012-10-24 17:25:27 +0300805 if (chip->get_direction) {
806 /* chip->get_direction may sleep */
807 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900808 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300809 spin_lock_irqsave(&gpio_lock, flags);
810 }
David Brownelld2876d02008-02-04 22:28:20 -0800811done:
Mika Westerberg77c2d792014-03-10 14:54:50 +0200812 spin_unlock_irqrestore(&gpio_lock, flags);
813 return status;
814}
815
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900816int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200817{
818 int status = -EPROBE_DEFER;
819 struct gpio_chip *chip;
820
821 if (!desc) {
822 pr_warn("%s: invalid GPIO\n", __func__);
823 return -EINVAL;
824 }
825
826 chip = desc->chip;
827 if (!chip)
828 goto done;
829
830 if (try_module_get(chip->owner)) {
831 status = __gpiod_request(desc, label);
832 if (status < 0)
833 module_put(chip->owner);
834 }
835
836done:
David Brownelld2876d02008-02-04 22:28:20 -0800837 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +0200838 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200839
David Brownelld2876d02008-02-04 22:28:20 -0800840 return status;
841}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900842
Mika Westerberg77c2d792014-03-10 14:54:50 +0200843static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800844{
Mika Westerberg77c2d792014-03-10 14:54:50 +0200845 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -0800846 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -0700847 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800848
Uwe Kleine-König3d599d12008-10-15 22:03:12 -0700849 might_sleep();
850
Alexandre Courbot372e7222013-02-03 01:29:29 +0900851 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -0700852
David Brownelld2876d02008-02-04 22:28:20 -0800853 spin_lock_irqsave(&gpio_lock, flags);
854
David Brownell35e8bb52008-10-15 22:03:16 -0700855 chip = desc->chip;
856 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
857 if (chip->free) {
858 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -0700859 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900860 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -0700861 spin_lock_irqsave(&gpio_lock, flags);
862 }
David Brownelld2876d02008-02-04 22:28:20 -0800863 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -0800864 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -0700865 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +0530866 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +0530867 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200868 ret = true;
869 }
David Brownelld2876d02008-02-04 22:28:20 -0800870
871 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200872 return ret;
873}
874
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900875void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200876{
877 if (desc && __gpiod_free(desc))
878 module_put(desc->chip->owner);
879 else
880 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -0800881}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900882
David Brownelld2876d02008-02-04 22:28:20 -0800883/**
884 * gpiochip_is_requested - return string iff signal was requested
885 * @chip: controller managing the signal
886 * @offset: of signal within controller's 0..(ngpio - 1) range
887 *
888 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +0900889 * The string returned is the label passed to gpio_request(); if none has been
890 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -0800891 *
892 * This function is for use by GPIO controller drivers. The label can
893 * help with diagnostics, and knowing that the signal is used as a GPIO
894 * can help avoid accidentally multiplexing it to another controller.
895 */
896const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
897{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900898 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800899
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900900 if (!GPIO_OFFSET_VALID(chip, offset))
David Brownelld2876d02008-02-04 22:28:20 -0800901 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900902
903 desc = &chip->desc[offset];
904
Alexandre Courbot372e7222013-02-03 01:29:29 +0900905 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -0800906 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900907 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -0800908}
909EXPORT_SYMBOL_GPL(gpiochip_is_requested);
910
Mika Westerberg77c2d792014-03-10 14:54:50 +0200911/**
912 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
913 * @desc: GPIO descriptor to request
914 * @label: label for the GPIO
915 *
916 * Function allows GPIO chip drivers to request and use their own GPIO
917 * descriptors via gpiolib API. Difference to gpiod_request() is that this
918 * function will not increase reference count of the GPIO chip module. This
919 * allows the GPIO chip module to be unloaded as needed (we assume that the
920 * GPIO chip driver handles freeing the GPIOs it has requested).
921 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700922struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
923 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +0200924{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700925 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
926 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200927
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700928 if (IS_ERR(desc)) {
929 chip_err(chip, "failed to get GPIO descriptor\n");
930 return desc;
931 }
932
933 err = __gpiod_request(desc, label);
934 if (err < 0)
935 return ERR_PTR(err);
936
937 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +0200938}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700939EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +0200940
941/**
942 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
943 * @desc: GPIO descriptor to free
944 *
945 * Function frees the given GPIO requested previously with
946 * gpiochip_request_own_desc().
947 */
948void gpiochip_free_own_desc(struct gpio_desc *desc)
949{
950 if (desc)
951 __gpiod_free(desc);
952}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -0700953EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -0800954
955/* Drivers MUST set GPIO direction before making get/set calls. In
956 * some cases this is done in early boot, before IRQs are enabled.
957 *
958 * As a rule these aren't called more than once (except for drivers
959 * using the open-drain emulation idiom) so these are natural places
960 * to accumulate extra debugging checks. Note that we can't (yet)
961 * rely on gpio_request() having been called beforehand.
962 */
963
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700964/**
965 * gpiod_direction_input - set the GPIO direction to input
966 * @desc: GPIO to set to input
967 *
968 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
969 * be called safely on it.
970 *
971 * Return 0 in case of success, else an error code.
972 */
973int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -0800974{
David Brownelld2876d02008-02-04 22:28:20 -0800975 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -0800976 int status = -EINVAL;
977
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200978 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900979 pr_warn("%s: invalid GPIO\n", __func__);
980 return -EINVAL;
981 }
982
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200983 chip = desc->chip;
984 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +0100985 gpiod_warn(desc,
986 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +0200987 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +0200988 return -EIO;
989 }
990
Alexandre Courbotd82da792014-07-22 16:17:43 +0900991 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -0800992 if (status == 0)
993 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -0600994
Alexandre Courbot372e7222013-02-03 01:29:29 +0900995 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +0900996
David Brownelld2876d02008-02-04 22:28:20 -0800997 return status;
998}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700999EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001000
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001001static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001002{
David Brownelld2876d02008-02-04 22:28:20 -08001003 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001004 int status = -EINVAL;
1005
Linus Walleijd468bf92013-09-24 11:54:38 +02001006 /* GPIOs used for IRQs shall not be set as output */
1007 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1008 gpiod_err(desc,
1009 "%s: tried to set a GPIO tied to an IRQ as output\n",
1010 __func__);
1011 return -EIO;
1012 }
1013
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301014 /* Open drain pin should not be driven to 1 */
1015 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001016 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301017
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301018 /* Open source pin should not be driven to 0 */
1019 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001020 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301021
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001022 chip = desc->chip;
1023 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001024 gpiod_warn(desc,
1025 "%s: missing set() or direction_output() operations\n",
1026 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001027 return -EIO;
1028 }
1029
Alexandre Courbotd82da792014-07-22 16:17:43 +09001030 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001031 if (status == 0)
1032 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001033 trace_gpio_value(desc_to_gpio(desc), 0, value);
1034 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001035 return status;
1036}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001037
1038/**
1039 * gpiod_direction_output_raw - set the GPIO direction to output
1040 * @desc: GPIO to set to output
1041 * @value: initial output value of the GPIO
1042 *
1043 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1044 * be called safely on it. The initial value of the output must be specified
1045 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1046 *
1047 * Return 0 in case of success, else an error code.
1048 */
1049int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1050{
1051 if (!desc || !desc->chip) {
1052 pr_warn("%s: invalid GPIO\n", __func__);
1053 return -EINVAL;
1054 }
1055 return _gpiod_direction_output_raw(desc, value);
1056}
1057EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1058
1059/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301060 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001061 * @desc: GPIO to set to output
1062 * @value: initial output value of the GPIO
1063 *
1064 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1065 * be called safely on it. The initial value of the output must be specified
1066 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1067 * account.
1068 *
1069 * Return 0 in case of success, else an error code.
1070 */
1071int gpiod_direction_output(struct gpio_desc *desc, int value)
1072{
1073 if (!desc || !desc->chip) {
1074 pr_warn("%s: invalid GPIO\n", __func__);
1075 return -EINVAL;
1076 }
1077 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1078 value = !value;
1079 return _gpiod_direction_output_raw(desc, value);
1080}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001081EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001082
Felipe Balbic4b5be92010-05-26 14:42:23 -07001083/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001084 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001085 * @gpio: the gpio to set debounce time
1086 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001087 *
1088 * returns -ENOTSUPP if the controller does not support setting
1089 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001090 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001091int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001092{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001093 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001094
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001095 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001096 pr_warn("%s: invalid GPIO\n", __func__);
1097 return -EINVAL;
1098 }
1099
Felipe Balbic4b5be92010-05-26 14:42:23 -07001100 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001101 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001102 gpiod_dbg(desc,
1103 "%s: missing set() or set_debounce() operations\n",
1104 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001105 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001106 }
1107
Alexandre Courbotd82da792014-07-22 16:17:43 +09001108 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001109}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001110EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001111
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001112/**
1113 * gpiod_is_active_low - test whether a GPIO is active-low or not
1114 * @desc: the gpio descriptor to test
1115 *
1116 * Returns 1 if the GPIO is active-low, 0 otherwise.
1117 */
1118int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001119{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001120 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001121}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001122EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001123
1124/* I/O calls are only valid after configuration completed; the relevant
1125 * "is this a valid GPIO" error checks should already have been done.
1126 *
1127 * "Get" operations are often inlinable as reading a pin value register,
1128 * and masking the relevant bit in that register.
1129 *
1130 * When "set" operations are inlinable, they involve writing that mask to
1131 * one register to set a low value, or a different register to set it high.
1132 * Otherwise locking is needed, so there may be little value to inlining.
1133 *
1134 *------------------------------------------------------------------------
1135 *
1136 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1137 * have requested the GPIO. That can include implicit requesting by
1138 * a direction setting call. Marking a gpio as requested locks its chip
1139 * in memory, guaranteeing that these table lookups need no more locking
1140 * and that gpiochip_remove() will fail.
1141 *
1142 * REVISIT when debugging, consider adding some instrumentation to ensure
1143 * that the GPIO was actually requested.
1144 */
1145
Alexandre Courbot23600962014-03-11 15:52:09 +09001146static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001147{
1148 struct gpio_chip *chip;
Alexandre Courbot23600962014-03-11 15:52:09 +09001149 bool value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001150 int offset;
David Brownelld2876d02008-02-04 22:28:20 -08001151
Alexandre Courbot372e7222013-02-03 01:29:29 +09001152 chip = desc->chip;
1153 offset = gpio_chip_hwgpio(desc);
Alexandre Courbot23600962014-03-11 15:52:09 +09001154 value = chip->get ? chip->get(chip, offset) : false;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001155 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001156 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001157}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001158
David Brownelld2876d02008-02-04 22:28:20 -08001159/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001160 * gpiod_get_raw_value() - return a gpio's raw value
1161 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001162 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001163 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1164 * its ACTIVE_LOW status.
1165 *
1166 * This function should be called from contexts where we cannot sleep, and will
1167 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001168 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001169int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001170{
David Brownelld2876d02008-02-04 22:28:20 -08001171 if (!desc)
1172 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001173 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001174 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001175 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001176}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001177EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001178
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001179/**
1180 * gpiod_get_value() - return a gpio's value
1181 * @desc: gpio whose value will be returned
1182 *
1183 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1184 * account.
1185 *
1186 * This function should be called from contexts where we cannot sleep, and will
1187 * complain if the GPIO chip functions potentially sleep.
1188 */
1189int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001190{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001191 int value;
1192 if (!desc)
1193 return 0;
1194 /* Should be using gpio_get_value_cansleep() */
1195 WARN_ON(desc->chip->can_sleep);
1196
1197 value = _gpiod_get_raw_value(desc);
1198 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1199 value = !value;
1200
1201 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001202}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001203EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001204
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301205/*
1206 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001207 * @desc: gpio descriptor whose state need to be set.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301208 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1209 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001210static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301211{
1212 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001213 struct gpio_chip *chip = desc->chip;
1214 int offset = gpio_chip_hwgpio(desc);
1215
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301216 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001217 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301218 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001219 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301220 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001221 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301222 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001223 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301224 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001225 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301226 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001227 gpiod_err(desc,
1228 "%s: Error in set_value for open drain err %d\n",
1229 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301230}
1231
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301232/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001233 * _gpio_set_open_source_value() - Set the open source gpio's value.
1234 * @desc: gpio descriptor whose state need to be set.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301235 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1236 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001237static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301238{
1239 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001240 struct gpio_chip *chip = desc->chip;
1241 int offset = gpio_chip_hwgpio(desc);
1242
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301243 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001244 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301245 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001246 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301247 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001248 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301249 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001250 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301251 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001252 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301253 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001254 gpiod_err(desc,
1255 "%s: Error in set_value for open source err %d\n",
1256 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301257}
1258
Alexandre Courbot23600962014-03-11 15:52:09 +09001259static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001260{
1261 struct gpio_chip *chip;
1262
Alexandre Courbot372e7222013-02-03 01:29:29 +09001263 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001264 trace_gpio_value(desc_to_gpio(desc), 0, value);
1265 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1266 _gpio_set_open_drain_value(desc, value);
1267 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1268 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301269 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001270 chip->set(chip, gpio_chip_hwgpio(desc), value);
1271}
1272
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001273/*
1274 * set multiple outputs on the same chip;
1275 * use the chip's set_multiple function if available;
1276 * otherwise set the outputs sequentially;
1277 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1278 * defines which outputs are to be changed
1279 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1280 * defines the values the outputs specified by mask are to be set to
1281 */
1282static void gpio_chip_set_multiple(struct gpio_chip *chip,
1283 unsigned long *mask, unsigned long *bits)
1284{
1285 if (chip->set_multiple) {
1286 chip->set_multiple(chip, mask, bits);
1287 } else {
1288 int i;
1289 for (i = 0; i < chip->ngpio; i++) {
1290 if (mask[BIT_WORD(i)] == 0) {
1291 /* no more set bits in this mask word;
1292 * skip ahead to the next word */
1293 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1294 continue;
1295 }
1296 /* set outputs if the corresponding mask bit is set */
1297 if (__test_and_clear_bit(i, mask)) {
1298 chip->set(chip, i, test_bit(i, bits));
1299 }
1300 }
1301 }
1302}
1303
1304static void gpiod_set_array_priv(bool raw, bool can_sleep,
1305 unsigned int array_size,
1306 struct gpio_desc **desc_array,
1307 int *value_array)
1308{
1309 int i = 0;
1310
1311 while (i < array_size) {
1312 struct gpio_chip *chip = desc_array[i]->chip;
1313 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1314 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1315 int count = 0;
1316
1317 if (!can_sleep) {
1318 WARN_ON(chip->can_sleep);
1319 }
1320 memset(mask, 0, sizeof(mask));
1321 do {
1322 struct gpio_desc *desc = desc_array[i];
1323 int hwgpio = gpio_chip_hwgpio(desc);
1324 int value = value_array[i];
1325
1326 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1327 value = !value;
1328 trace_gpio_value(desc_to_gpio(desc), 0, value);
1329 /*
1330 * collect all normal outputs belonging to the same chip
1331 * open drain and open source outputs are set individually
1332 */
1333 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
1334 _gpio_set_open_drain_value(desc,value);
1335 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1336 _gpio_set_open_source_value(desc, value);
1337 } else {
1338 __set_bit(hwgpio, mask);
1339 if (value) {
1340 __set_bit(hwgpio, bits);
1341 } else {
1342 __clear_bit(hwgpio, bits);
1343 }
1344 count++;
1345 }
1346 i++;
1347 } while ((i < array_size) && (desc_array[i]->chip == chip));
1348 /* push collected bits to outputs */
1349 if (count != 0) {
1350 gpio_chip_set_multiple(chip, mask, bits);
1351 }
1352 }
1353}
1354
David Brownelld2876d02008-02-04 22:28:20 -08001355/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001356 * gpiod_set_raw_value() - assign a gpio's raw value
1357 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001358 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001359 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001360 * Set the raw value of the GPIO, i.e. the value of its physical line without
1361 * regard for its ACTIVE_LOW status.
1362 *
1363 * This function should be called from contexts where we cannot sleep, and will
1364 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001365 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001366void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001367{
David Brownelld2876d02008-02-04 22:28:20 -08001368 if (!desc)
1369 return;
David Brownelld2876d02008-02-04 22:28:20 -08001370 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001371 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001372 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001373}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001374EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001375
1376/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001377 * gpiod_set_value() - assign a gpio's value
1378 * @desc: gpio whose value will be assigned
1379 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001380 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001381 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1382 * account
1383 *
1384 * This function should be called from contexts where we cannot sleep, and will
1385 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001386 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001387void gpiod_set_value(struct gpio_desc *desc, int value)
1388{
1389 if (!desc)
1390 return;
1391 /* Should be using gpio_set_value_cansleep() */
1392 WARN_ON(desc->chip->can_sleep);
1393 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1394 value = !value;
1395 _gpiod_set_raw_value(desc, value);
1396}
1397EXPORT_SYMBOL_GPL(gpiod_set_value);
1398
1399/**
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001400 * gpiod_set_raw_array() - assign values to an array of GPIOs
1401 * @array_size: number of elements in the descriptor / value arrays
1402 * @desc_array: array of GPIO descriptors whose values will be assigned
1403 * @value_array: array of values to assign
1404 *
1405 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1406 * without regard for their ACTIVE_LOW status.
1407 *
1408 * This function should be called from contexts where we cannot sleep, and will
1409 * complain if the GPIO chip functions potentially sleep.
1410 */
1411void gpiod_set_raw_array(unsigned int array_size,
1412 struct gpio_desc **desc_array, int *value_array)
1413{
1414 if (!desc_array)
1415 return;
1416 gpiod_set_array_priv(true, false, array_size, desc_array, value_array);
1417}
1418EXPORT_SYMBOL_GPL(gpiod_set_raw_array);
1419
1420/**
1421 * gpiod_set_array() - assign values to an array of GPIOs
1422 * @array_size: number of elements in the descriptor / value arrays
1423 * @desc_array: array of GPIO descriptors whose values will be assigned
1424 * @value_array: array of values to assign
1425 *
1426 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1427 * into account.
1428 *
1429 * This function should be called from contexts where we cannot sleep, and will
1430 * complain if the GPIO chip functions potentially sleep.
1431 */
1432void gpiod_set_array(unsigned int array_size,
1433 struct gpio_desc **desc_array, int *value_array)
1434{
1435 if (!desc_array)
1436 return;
1437 gpiod_set_array_priv(false, false, array_size, desc_array, value_array);
1438}
1439EXPORT_SYMBOL_GPL(gpiod_set_array);
1440
1441/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001442 * gpiod_cansleep() - report whether gpio value access may sleep
1443 * @desc: gpio to check
1444 *
1445 */
1446int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001447{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001448 if (!desc)
1449 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001450 return desc->chip->can_sleep;
1451}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001452EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001453
David Brownell0f6d5042008-10-15 22:03:14 -07001454/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001455 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1456 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001457 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001458 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1459 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001460 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001461int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001462{
1463 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001464 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001465
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001466 if (!desc)
1467 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001468 chip = desc->chip;
1469 offset = gpio_chip_hwgpio(desc);
1470 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1471}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001472EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001473
Linus Walleijd468bf92013-09-24 11:54:38 +02001474/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001475 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001476 * @chip: the chip the GPIO to lock belongs to
1477 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001478 *
1479 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001480 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001481 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001482int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001483{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001484 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001485 return -EINVAL;
1486
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001487 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1488 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001489 "%s: tried to flag a GPIO set as output for IRQ\n",
1490 __func__);
1491 return -EIO;
1492 }
1493
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001494 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001495 return 0;
1496}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001497EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001498
Linus Walleijd468bf92013-09-24 11:54:38 +02001499/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001500 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001501 * @chip: the chip the GPIO to lock belongs to
1502 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001503 *
1504 * This is used directly by GPIO drivers that want to indicate
1505 * that a certain GPIO is no longer used exclusively for IRQ.
1506 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001507void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001508{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001509 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001510 return;
1511
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001512 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001513}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001514EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001515
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001516/**
1517 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1518 * @desc: gpio whose value will be returned
1519 *
1520 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1521 * its ACTIVE_LOW status.
1522 *
1523 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001524 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001525int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001526{
David Brownelld2876d02008-02-04 22:28:20 -08001527 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001528 if (!desc)
1529 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001530 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001531}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001532EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001533
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001534/**
1535 * gpiod_get_value_cansleep() - return a gpio's value
1536 * @desc: gpio whose value will be returned
1537 *
1538 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1539 * account.
1540 *
1541 * This function is to be called from contexts that can sleep.
1542 */
1543int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001544{
David Brownelld2876d02008-02-04 22:28:20 -08001545 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001546
1547 might_sleep_if(extra_checks);
1548 if (!desc)
1549 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001550
1551 value = _gpiod_get_raw_value(desc);
1552 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1553 value = !value;
1554
David Brownelld2876d02008-02-04 22:28:20 -08001555 return value;
1556}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001557EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001558
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001559/**
1560 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1561 * @desc: gpio whose value will be assigned
1562 * @value: value to assign
1563 *
1564 * Set the raw value of the GPIO, i.e. the value of its physical line without
1565 * regard for its ACTIVE_LOW status.
1566 *
1567 * This function is to be called from contexts that can sleep.
1568 */
1569void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001570{
David Brownelld2876d02008-02-04 22:28:20 -08001571 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001572 if (!desc)
1573 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001574 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001575}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001576EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001577
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001578/**
1579 * gpiod_set_value_cansleep() - assign a gpio's value
1580 * @desc: gpio whose value will be assigned
1581 * @value: value to assign
1582 *
1583 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1584 * account
1585 *
1586 * This function is to be called from contexts that can sleep.
1587 */
1588void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001589{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001590 might_sleep_if(extra_checks);
1591 if (!desc)
1592 return;
1593
1594 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1595 value = !value;
1596 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001597}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001598EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001599
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001600/**
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001601 * gpiod_set_raw_array_cansleep() - assign values to an array of GPIOs
1602 * @array_size: number of elements in the descriptor / value arrays
1603 * @desc_array: array of GPIO descriptors whose values will be assigned
1604 * @value_array: array of values to assign
1605 *
1606 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1607 * without regard for their ACTIVE_LOW status.
1608 *
1609 * This function is to be called from contexts that can sleep.
1610 */
1611void gpiod_set_raw_array_cansleep(unsigned int array_size,
1612 struct gpio_desc **desc_array,
1613 int *value_array)
1614{
1615 might_sleep_if(extra_checks);
1616 if (!desc_array)
1617 return;
1618 gpiod_set_array_priv(true, true, array_size, desc_array, value_array);
1619}
1620EXPORT_SYMBOL_GPL(gpiod_set_raw_array_cansleep);
1621
1622/**
1623 * gpiod_set_array_cansleep() - assign values to an array of GPIOs
1624 * @array_size: number of elements in the descriptor / value arrays
1625 * @desc_array: array of GPIO descriptors whose values will be assigned
1626 * @value_array: array of values to assign
1627 *
1628 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1629 * into account.
1630 *
1631 * This function is to be called from contexts that can sleep.
1632 */
1633void gpiod_set_array_cansleep(unsigned int array_size,
1634 struct gpio_desc **desc_array,
1635 int *value_array)
1636{
1637 might_sleep_if(extra_checks);
1638 if (!desc_array)
1639 return;
1640 gpiod_set_array_priv(false, true, array_size, desc_array, value_array);
1641}
1642EXPORT_SYMBOL_GPL(gpiod_set_array_cansleep);
1643
1644/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001645 * gpiod_add_lookup_table() - register GPIO device consumers
1646 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001647 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001648void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001649{
1650 mutex_lock(&gpio_lookup_lock);
1651
Alexandre Courbotad824782013-12-03 12:20:11 +09001652 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001653
1654 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001655}
1656
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001657static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001658 unsigned int idx,
1659 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001660{
Thierry Redingdd34c372014-04-23 17:28:09 +02001661 static const char *suffixes[] = { "gpios", "gpio" };
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001662 char prop_name[32]; /* 32 is max size of property name */
1663 enum of_gpio_flags of_flags;
1664 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001665 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001666
Thierry Redingdd34c372014-04-23 17:28:09 +02001667 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1668 if (con_id)
1669 snprintf(prop_name, 32, "%s-%s", con_id, suffixes[i]);
1670 else
1671 snprintf(prop_name, 32, "%s", suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001672
Thierry Redingdd34c372014-04-23 17:28:09 +02001673 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1674 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001675 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001676 break;
1677 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001678
1679 if (IS_ERR(desc))
1680 return desc;
1681
1682 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001683 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001684
1685 return desc;
1686}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001687
Mika Westerberg81f59e92013-10-10 11:01:09 +03001688static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001689 unsigned int idx,
1690 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001691{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001692 static const char * const suffixes[] = { "gpios", "gpio" };
1693 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03001694 struct acpi_gpio_info info;
1695 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001696 char propname[32];
1697 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03001698
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001699 /* Try first from _DSD */
1700 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1701 if (con_id && strcmp(con_id, "gpios")) {
1702 snprintf(propname, sizeof(propname), "%s-%s",
1703 con_id, suffixes[i]);
1704 } else {
1705 snprintf(propname, sizeof(propname), "%s",
1706 suffixes[i]);
1707 }
Mika Westerberge01f4402013-10-10 11:01:10 +03001708
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001709 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1710 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1711 break;
1712 }
1713
1714 /* Then from plain _CRS GPIOs */
1715 if (IS_ERR(desc)) {
1716 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1717 if (IS_ERR(desc))
1718 return desc;
1719 }
1720
1721 if (info.active_low)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001722 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03001723
1724 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03001725}
1726
Alexandre Courbotad824782013-12-03 12:20:11 +09001727static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
1728{
1729 const char *dev_id = dev ? dev_name(dev) : NULL;
1730 struct gpiod_lookup_table *table;
1731
1732 mutex_lock(&gpio_lookup_lock);
1733
1734 list_for_each_entry(table, &gpio_lookup_list, list) {
1735 if (table->dev_id && dev_id) {
1736 /*
1737 * Valid strings on both ends, must be identical to have
1738 * a match
1739 */
1740 if (!strcmp(table->dev_id, dev_id))
1741 goto found;
1742 } else {
1743 /*
1744 * One of the pointers is NULL, so both must be to have
1745 * a match
1746 */
1747 if (dev_id == table->dev_id)
1748 goto found;
1749 }
1750 }
1751 table = NULL;
1752
1753found:
1754 mutex_unlock(&gpio_lookup_lock);
1755 return table;
1756}
1757
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001758static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001759 unsigned int idx,
1760 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001761{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001762 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09001763 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001764 struct gpiod_lookup *p;
1765
Alexandre Courbotad824782013-12-03 12:20:11 +09001766 table = gpiod_find_lookup_table(dev);
1767 if (!table)
1768 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001769
Alexandre Courbotad824782013-12-03 12:20:11 +09001770 for (p = &table->table[0]; p->chip_label; p++) {
1771 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001772
Alexandre Courbotad824782013-12-03 12:20:11 +09001773 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001774 if (p->idx != idx)
1775 continue;
1776
Alexandre Courbotad824782013-12-03 12:20:11 +09001777 /* If the lookup entry has a con_id, require exact match */
1778 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
1779 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001780
Alexandre Courbotad824782013-12-03 12:20:11 +09001781 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001782
Alexandre Courbotad824782013-12-03 12:20:11 +09001783 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001784 dev_err(dev, "cannot find GPIO chip %s\n",
1785 p->chip_label);
1786 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001787 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001788
Alexandre Courbotad824782013-12-03 12:20:11 +09001789 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001790 dev_err(dev,
1791 "requested GPIO %d is out of range [0..%d] for chip %s\n",
1792 idx, chip->ngpio, chip->label);
1793 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09001794 }
1795
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09001796 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09001797 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001798
1799 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09001800 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001801
1802 return desc;
1803}
1804
1805/**
Thierry Reding08791622014-04-25 16:54:22 +02001806 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09001807 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001808 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001809 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001810 *
1811 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001812 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1813 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001814 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001815struct gpio_desc *__must_check __gpiod_get(struct device *dev, const char *con_id,
1816 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001817{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001818 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001819}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001820EXPORT_SYMBOL_GPL(__gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001821
1822/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02001823 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1824 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1825 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001826 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02001827 *
1828 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1829 * the requested function it will return NULL. This is convenient for drivers
1830 * that need to handle optional GPIOs.
1831 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001832struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
1833 const char *con_id,
1834 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02001835{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001836 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001837}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001838EXPORT_SYMBOL_GPL(__gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001839
1840/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001841 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02001842 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001843 * @con_id: function within the GPIO consumer
1844 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001845 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001846 *
1847 * This variant of gpiod_get() allows to access GPIOs other than the first
1848 * defined one for functions that define several GPIOs.
1849 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001850 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
1851 * requested function and/or index, or another IS_ERR() code if an error
1852 * occured while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001853 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001854struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001855 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001856 unsigned int idx,
1857 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001858{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001859 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001860 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001861 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001862
1863 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
1864
1865 /* Using device tree? */
1866 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
1867 dev_dbg(dev, "using device tree for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001868 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
Mika Westerberg81f59e92013-10-10 11:01:09 +03001869 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
1870 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001871 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09001872 }
1873
1874 /*
1875 * Either we are not using DT or ACPI, or their lookup did not return
1876 * a result. In that case, use platform lookup as a fallback.
1877 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09001878 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04001879 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001880 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001881 }
1882
1883 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02001884 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001885 return desc;
1886 }
1887
1888 status = gpiod_request(desc, con_id);
1889
1890 if (status < 0)
1891 return ERR_PTR(status);
1892
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001893 if (lookupflags & GPIO_ACTIVE_LOW)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001894 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001895 if (lookupflags & GPIO_OPEN_DRAIN)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001896 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001897 if (lookupflags & GPIO_OPEN_SOURCE)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001898 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001899
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001900 /* No particular flag request, return here... */
Adrian Hunter72f908c2014-09-22 11:01:16 +03001901 if (!(flags & GPIOD_FLAGS_BIT_DIR_SET))
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001902 return desc;
1903
1904 /* Process flags */
1905 if (flags & GPIOD_FLAGS_BIT_DIR_OUT)
1906 status = gpiod_direction_output(desc,
1907 flags & GPIOD_FLAGS_BIT_DIR_VAL);
1908 else
1909 status = gpiod_direction_input(desc);
1910
1911 if (status < 0) {
1912 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
1913 gpiod_put(desc);
1914 return ERR_PTR(status);
1915 }
1916
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001917 return desc;
1918}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001919EXPORT_SYMBOL_GPL(__gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001920
1921/**
Mika Westerberg40b73182014-10-21 13:33:59 +02001922 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
1923 * @fwnode: handle of the firmware node
1924 * @propname: name of the firmware property representing the GPIO
1925 *
1926 * This function can be used for drivers that get their configuration
1927 * from firmware.
1928 *
1929 * Function properly finds the corresponding GPIO using whatever is the
1930 * underlying firmware interface and then makes sure that the GPIO
1931 * descriptor is requested before it is returned to the caller.
1932 *
1933 * In case of error an ERR_PTR() is returned.
1934 */
1935struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
1936 const char *propname)
1937{
1938 struct gpio_desc *desc = ERR_PTR(-ENODEV);
1939 bool active_low = false;
1940 int ret;
1941
1942 if (!fwnode)
1943 return ERR_PTR(-EINVAL);
1944
1945 if (is_of_node(fwnode)) {
1946 enum of_gpio_flags flags;
1947
1948 desc = of_get_named_gpiod_flags(of_node(fwnode), propname, 0,
1949 &flags);
1950 if (!IS_ERR(desc))
1951 active_low = flags & OF_GPIO_ACTIVE_LOW;
1952 } else if (is_acpi_node(fwnode)) {
1953 struct acpi_gpio_info info;
1954
1955 desc = acpi_get_gpiod_by_index(acpi_node(fwnode), propname, 0,
1956 &info);
1957 if (!IS_ERR(desc))
1958 active_low = info.active_low;
1959 }
1960
1961 if (IS_ERR(desc))
1962 return desc;
1963
1964 ret = gpiod_request(desc, NULL);
1965 if (ret)
1966 return ERR_PTR(ret);
1967
1968 /* Only value flag can be set from both DT and ACPI is active_low */
1969 if (active_low)
1970 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
1971
1972 return desc;
1973}
1974EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
1975
1976/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02001977 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
1978 * function
1979 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1980 * @con_id: function within the GPIO consumer
1981 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001982 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02001983 *
1984 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
1985 * specified index was assigned to the requested function it will return NULL.
1986 * This is convenient for drivers that need to handle optional GPIOs.
1987 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001988struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02001989 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001990 unsigned int index,
1991 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02001992{
1993 struct gpio_desc *desc;
1994
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09001995 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02001996 if (IS_ERR(desc)) {
1997 if (PTR_ERR(desc) == -ENOENT)
1998 return NULL;
1999 }
2000
2001 return desc;
2002}
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002003EXPORT_SYMBOL_GPL(__gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002004
2005/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002006 * gpiod_put - dispose of a GPIO descriptor
2007 * @desc: GPIO descriptor to dispose of
2008 *
2009 * No descriptor can be used after gpiod_put() has been called on it.
2010 */
2011void gpiod_put(struct gpio_desc *desc)
2012{
2013 gpiod_free(desc);
2014}
2015EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002016
David Brownelld2876d02008-02-04 22:28:20 -08002017#ifdef CONFIG_DEBUG_FS
2018
2019static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2020{
2021 unsigned i;
2022 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002023 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002024 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002025 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002026
2027 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2028 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2029 continue;
2030
Alexandre Courbot372e7222013-02-03 01:29:29 +09002031 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002032 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002033 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2034 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
David Brownelld2876d02008-02-04 22:28:20 -08002035 gpio, gdesc->label,
2036 is_out ? "out" : "in ",
2037 chip->get
2038 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002039 : "? ",
2040 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002041 seq_printf(s, "\n");
2042 }
2043}
2044
Thierry Redingf9c4a312012-04-12 13:26:01 +02002045static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002046{
Grant Likely362432a2013-02-09 09:41:49 +00002047 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002048 struct gpio_chip *chip = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002049 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002050
2051 s->private = "";
2052
Grant Likely362432a2013-02-09 09:41:49 +00002053 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002054 list_for_each_entry(chip, &gpio_chips, list)
Grant Likely362432a2013-02-09 09:41:49 +00002055 if (index-- == 0) {
2056 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002057 return chip;
Grant Likely362432a2013-02-09 09:41:49 +00002058 }
2059 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002060
2061 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002062}
2063
2064static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2065{
Grant Likely362432a2013-02-09 09:41:49 +00002066 unsigned long flags;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002067 struct gpio_chip *chip = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002068 void *ret = NULL;
2069
Grant Likely362432a2013-02-09 09:41:49 +00002070 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002071 if (list_is_last(&chip->list, &gpio_chips))
2072 ret = NULL;
2073 else
2074 ret = list_entry(chip->list.next, struct gpio_chip, list);
Grant Likely362432a2013-02-09 09:41:49 +00002075 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002076
2077 s->private = "\n";
2078 ++*pos;
2079
2080 return ret;
2081}
2082
2083static void gpiolib_seq_stop(struct seq_file *s, void *v)
2084{
2085}
2086
2087static int gpiolib_seq_show(struct seq_file *s, void *v)
2088{
2089 struct gpio_chip *chip = v;
2090 struct device *dev;
2091
2092 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2093 chip->base, chip->base + chip->ngpio - 1);
2094 dev = chip->dev;
2095 if (dev)
2096 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2097 dev_name(dev));
2098 if (chip->label)
2099 seq_printf(s, ", %s", chip->label);
2100 if (chip->can_sleep)
2101 seq_printf(s, ", can sleep");
2102 seq_printf(s, ":\n");
2103
2104 if (chip->dbg_show)
2105 chip->dbg_show(s, chip);
2106 else
2107 gpiolib_dbg_show(s, chip);
2108
David Brownelld2876d02008-02-04 22:28:20 -08002109 return 0;
2110}
2111
Thierry Redingf9c4a312012-04-12 13:26:01 +02002112static const struct seq_operations gpiolib_seq_ops = {
2113 .start = gpiolib_seq_start,
2114 .next = gpiolib_seq_next,
2115 .stop = gpiolib_seq_stop,
2116 .show = gpiolib_seq_show,
2117};
2118
David Brownelld2876d02008-02-04 22:28:20 -08002119static int gpiolib_open(struct inode *inode, struct file *file)
2120{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002121 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002122}
2123
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002124static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002125 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002126 .open = gpiolib_open,
2127 .read = seq_read,
2128 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002129 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002130};
2131
2132static int __init gpiolib_debugfs_init(void)
2133{
2134 /* /sys/kernel/debug/gpio */
2135 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2136 NULL, NULL, &gpiolib_operations);
2137 return 0;
2138}
2139subsys_initcall(gpiolib_debugfs_init);
2140
2141#endif /* DEBUG_FS */