blob: 646dea4f96ffbd374638da3d6e28fdd37f317f7f [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d2008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020018#include <linux/pinctrl/consumer.h>
Linus Walleijff2b1352015-10-20 11:10:38 +020019#include <linux/idr.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020020#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
23#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080024
Mika Westerberg664e3e52014-01-08 12:40:54 +020025#include "gpiolib.h"
26
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060027#define CREATE_TRACE_POINTS
28#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080029
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070030/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080031 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070032 * The GPIO programming interface allows for inlining speed-critical
33 * get/set operations for common cases, so that access to SOC-integrated
34 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080035 */
36
37
38/* When debugging, extend minimal trust to callers and platform code.
39 * Also emit diagnostic messages that may help initial bringup, when
40 * board setup or driver bugs are most common.
41 *
42 * Otherwise, minimize overhead in what may be bitbanging codepaths.
43 */
44#ifdef DEBUG
45#define extra_checks 1
46#else
47#define extra_checks 0
48#endif
49
Linus Walleijff2b1352015-10-20 11:10:38 +020050/* Device and char device-related information */
51static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020052static dev_t gpio_devt;
53#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
54static struct bus_type gpio_bus_type = {
55 .name = "gpio",
56};
Linus Walleijff2b1352015-10-20 11:10:38 +020057
David Brownelld2876d02008-02-04 22:28:20 -080058/* gpio_lock prevents conflicts during gpio_desc[] table updates.
59 * While any GPIO is requested, its gpio_chip is not removable;
60 * each GPIO's "requested" flag serves as a lock and refcount.
61 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090062DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080063
Alexandre Courbotbae48da2013-10-17 10:21:38 -070064static DEFINE_MUTEX(gpio_lookup_lock);
65static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020066LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020067
68static void gpiochip_free_hogs(struct gpio_chip *chip);
69static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
70
71
David Brownelld2876d02008-02-04 22:28:20 -080072static inline void desc_set_label(struct gpio_desc *d, const char *label)
73{
David Brownelld2876d02008-02-04 22:28:20 -080074 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080075}
76
Alexandre Courbot372e7222013-02-03 01:29:29 +090077/**
78 * Convert a GPIO number to its descriptor
79 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070080struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090081{
Linus Walleijff2b1352015-10-20 11:10:38 +020082 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +090083 unsigned long flags;
84
85 spin_lock_irqsave(&gpio_lock, flags);
86
Linus Walleijff2b1352015-10-20 11:10:38 +020087 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +010088 if (gdev->base <= gpio &&
89 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +090090 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +010091 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +090092 }
93 }
94
95 spin_unlock_irqrestore(&gpio_lock, flags);
96
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +090097 if (!gpio_is_valid(gpio))
98 WARN(1, "invalid GPIO %d\n", gpio);
99
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900100 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900101}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700102EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900103
104/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900105 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +0200106 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900107struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
108 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200109{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100110 struct gpio_device *gdev = chip->gpiodev;
111
112 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900113 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200114
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100115 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200116}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900117
118/**
119 * Convert a GPIO descriptor to the integer namespace.
120 * This should disappear in the future but is needed since we still
121 * use GPIO numbers for error messages and sysfs nodes
122 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700123int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900124{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100125 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900126}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700127EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900128
129
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700130/**
131 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
132 * @desc: descriptor to return the chip of
133 */
134struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900135{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100136 if (!desc || !desc->gdev || !desc->gdev->chip)
137 return NULL;
138 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900139}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700140EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800141
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700142/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
143static int gpiochip_find_base(int ngpio)
144{
Linus Walleijff2b1352015-10-20 11:10:38 +0200145 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900146 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700147
Linus Walleijff2b1352015-10-20 11:10:38 +0200148 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900149 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100150 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900151 break;
152 else
153 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100154 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700155 }
156
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900157 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700158 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900159 return base;
160 } else {
161 pr_err("%s: cannot find free range\n", __func__);
162 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700163 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700164}
165
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700166/**
167 * gpiod_get_direction - return the current direction of a GPIO
168 * @desc: GPIO to get the direction of
169 *
170 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
171 *
172 * This function may sleep if gpiod_cansleep() is true.
173 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900174int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300175{
176 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900177 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300178 int status = -EINVAL;
179
Alexandre Courbot372e7222013-02-03 01:29:29 +0900180 chip = gpiod_to_chip(desc);
181 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300182
183 if (!chip->get_direction)
184 return status;
185
Alexandre Courbot372e7222013-02-03 01:29:29 +0900186 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300187 if (status > 0) {
188 /* GPIOF_DIR_IN, or other positive */
189 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900190 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300191 }
192 if (status == 0) {
193 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900194 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300195 }
196 return status;
197}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700198EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300199
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900200/*
201 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800202 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900203 *
204 * Return -EBUSY if the new chip overlaps with some other chip's integer
205 * space.
206 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200207static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900208{
Linus Walleijff2b1352015-10-20 11:10:38 +0200209 struct gpio_device *iterator;
210 struct gpio_device *previous = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900211
Linus Walleijff2b1352015-10-20 11:10:38 +0200212 if (!gdev->chip)
213 return -EINVAL;
214
215 if (list_empty(&gpio_devices)) {
216 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530217 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900218 }
219
Linus Walleijff2b1352015-10-20 11:10:38 +0200220 list_for_each_entry(iterator, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100221 if (iterator->base >= gdev->base + gdev->ngpio) {
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800222 /*
223 * Iterator is the first GPIO chip so there is no
224 * previous one
225 */
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530226 if (!previous) {
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800227 goto found;
228 } else {
229 /*
230 * We found a valid range(means
231 * [base, base + ngpio - 1]) between previous
232 * and iterator chip.
233 */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100234 if (previous->base + previous->ngpio
235 <= gdev->base)
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800236 goto found;
237 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900238 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800239 previous = iterator;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900240 }
241
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530242 /*
243 * We are beyond the last chip in the list and iterator now
244 * points to the head.
245 * Let iterator point to the last chip in the list.
246 */
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900247
Linus Walleijff2b1352015-10-20 11:10:38 +0200248 iterator = list_last_entry(&gpio_devices, struct gpio_device, list);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100249 if (iterator->base + iterator->ngpio <= gdev->base) {
Linus Walleijff2b1352015-10-20 11:10:38 +0200250 list_add(&gdev->list, &iterator->list);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500251 return 0;
252 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900253
Linus Walleijff2b1352015-10-20 11:10:38 +0200254 dev_err(&gdev->dev,
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800255 "GPIO integer space overlap, cannot add chip\n");
256 return -EBUSY;
257
258found:
Linus Walleijff2b1352015-10-20 11:10:38 +0200259 list_add_tail(&gdev->list, &iterator->list);
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800260 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900261}
262
Linus Walleijf881bab2015-09-23 16:20:43 -0700263/**
264 * Convert a GPIO name to its descriptor
265 */
266static struct gpio_desc *gpio_name_to_desc(const char * const name)
267{
Linus Walleijff2b1352015-10-20 11:10:38 +0200268 struct gpio_device *gdev;
Linus Walleijf881bab2015-09-23 16:20:43 -0700269 unsigned long flags;
270
271 spin_lock_irqsave(&gpio_lock, flags);
272
Linus Walleijff2b1352015-10-20 11:10:38 +0200273 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700274 int i;
275
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100276 for (i = 0; i != gdev->ngpio; ++i) {
277 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab2015-09-23 16:20:43 -0700278
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100279 if (!desc->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700280 continue;
281
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100282 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700283 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100284 return desc;
Linus Walleijf881bab2015-09-23 16:20:43 -0700285 }
286 }
287 }
288
289 spin_unlock_irqrestore(&gpio_lock, flags);
290
291 return NULL;
292}
293
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200294/*
295 * Takes the names from gc->names and checks if they are all unique. If they
296 * are, they are assigned to their gpio descriptors.
297 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800298 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200299 */
300static int gpiochip_set_desc_names(struct gpio_chip *gc)
301{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100302 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200303 int i;
304
305 if (!gc->names)
306 return 0;
307
308 /* First check all names if they are unique */
309 for (i = 0; i != gc->ngpio; ++i) {
310 struct gpio_desc *gpio;
311
312 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700313 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100314 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200315 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab2015-09-23 16:20:43 -0700316 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200317 }
318
319 /* Then add all names to the GPIO descriptors */
320 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100321 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200322
323 return 0;
324}
325
Linus Walleij3c702e92015-10-21 15:29:53 +0200326/**
327 * gpio_ioctl() - ioctl handler for the GPIO chardev
328 */
329static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
330{
331 struct gpio_device *gdev = filp->private_data;
332 struct gpio_chip *chip = gdev->chip;
333 int __user *ip = (int __user *)arg;
334 struct gpiochip_info chipinfo;
335
336 /* We fail any subsequent ioctl():s when the chip is gone */
337 if (!chip)
338 return -ENODEV;
339
340 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
341 /* Fill in the struct and pass to userspace */
342 strncpy(chipinfo.name, dev_name(&gdev->dev),
343 sizeof(chipinfo.name));
344 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100345 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200346 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
347 return -EFAULT;
348 return 0;
349 }
350 return -EINVAL;
351}
352
353/**
354 * gpio_chrdev_open() - open the chardev for ioctl operations
355 * @inode: inode for this chardev
356 * @filp: file struct for storing private data
357 * Returns 0 on success
358 */
359static int gpio_chrdev_open(struct inode *inode, struct file *filp)
360{
361 struct gpio_device *gdev = container_of(inode->i_cdev,
362 struct gpio_device, chrdev);
363
364 /* Fail on open if the backing gpiochip is gone */
365 if (!gdev || !gdev->chip)
366 return -ENODEV;
367 get_device(&gdev->dev);
368 filp->private_data = gdev;
369 return 0;
370}
371
372/**
373 * gpio_chrdev_release() - close chardev after ioctl operations
374 * @inode: inode for this chardev
375 * @filp: file struct for storing private data
376 * Returns 0 on success
377 */
378static int gpio_chrdev_release(struct inode *inode, struct file *filp)
379{
380 struct gpio_device *gdev = container_of(inode->i_cdev,
381 struct gpio_device, chrdev);
382
383 if (!gdev)
384 return -ENODEV;
385 put_device(&gdev->dev);
386 return 0;
387}
388
389
390static const struct file_operations gpio_fileops = {
391 .release = gpio_chrdev_release,
392 .open = gpio_chrdev_open,
393 .owner = THIS_MODULE,
394 .llseek = noop_llseek,
395 .unlocked_ioctl = gpio_ioctl,
396 .compat_ioctl = gpio_ioctl,
397};
398
Linus Walleijff2b1352015-10-20 11:10:38 +0200399static void gpiodevice_release(struct device *dev)
400{
401 struct gpio_device *gdev = dev_get_drvdata(dev);
402
Linus Walleij3c702e92015-10-21 15:29:53 +0200403 cdev_del(&gdev->chrdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200404 list_del(&gdev->list);
405 ida_simple_remove(&gpio_ida, gdev->id);
Linus Walleij9efd9e62016-02-09 14:27:42 +0100406 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200407}
408
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700409/**
Linus Walleijb08ea352015-12-03 15:14:13 +0100410 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -0800411 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900412 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -0800413 *
414 * Returns a negative errno if the chip can't be registered, such as
415 * because the chip->base is invalid or already associated with a
416 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700417 *
Linus Walleijb08ea352015-12-03 15:14:13 +0100418 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800419 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d2008-07-25 01:46:07 -0700420 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
421 * for GPIOs will fail rudely.
422 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700423 * If chip->base is negative, this requests dynamic assignment of
424 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800425 */
Linus Walleijb08ea352015-12-03 15:14:13 +0100426int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -0800427{
428 unsigned long flags;
429 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +0200430 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700431 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +0200432 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -0800433
Linus Walleijff2b1352015-10-20 11:10:38 +0200434 /*
435 * First: allocate and populate the internal stat container, and
436 * set up the struct device.
437 */
438 gdev = kmalloc(sizeof(*gdev), GFP_KERNEL);
439 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900440 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +0200441 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +0200442 gdev->chip = chip;
443 chip->gpiodev = gdev;
444 if (chip->parent) {
445 gdev->dev.parent = chip->parent;
446 gdev->dev.of_node = chip->parent->of_node;
447 } else {
448#ifdef CONFIG_OF_GPIO
449 /* If the gpiochip has an assigned OF node this takes precedence */
450 if (chip->of_node)
451 gdev->dev.of_node = chip->of_node;
452#endif
453 }
454 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
455 if (gdev->id < 0) {
456 status = gdev->id;
457 goto err_free_gdev;
458 }
459 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
460 device_initialize(&gdev->dev);
461 dev_set_drvdata(&gdev->dev, gdev);
462 if (chip->parent && chip->parent->driver)
463 gdev->owner = chip->parent->driver->owner;
464 else if (chip->owner)
465 /* TODO: remove chip->owner */
466 gdev->owner = chip->owner;
467 else
468 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -0800469
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100470 gdev->descs = devm_kcalloc(&gdev->dev, chip->ngpio,
471 sizeof(gdev->descs[0]), GFP_KERNEL);
472 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +0200473 status = -ENOMEM;
474 goto err_free_gdev;
475 }
476
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800477 if (chip->ngpio == 0) {
478 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +0200479 status = -EINVAL;
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100480 goto err_free_gdev;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800481 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100482 gdev->ngpio = chip->ngpio;
483 /* FIXME: move driver data into gpio_device dev_set_drvdata() */
484 chip->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800485
David Brownelld2876d02008-02-04 22:28:20 -0800486 spin_lock_irqsave(&gpio_lock, flags);
487
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100488 /*
489 * TODO: this allocates a Linux GPIO number base in the global
490 * GPIO numberspace for this chip. In the long run we want to
491 * get *rid* of this numberspace and use only descriptors, but
492 * it may be a pipe dream. It will not happen before we get rid
493 * of the sysfs interface anyways.
494 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700495 if (base < 0) {
496 base = gpiochip_find_base(chip->ngpio);
497 if (base < 0) {
498 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100499 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100500 goto err_free_gdev;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700501 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100502 /*
503 * TODO: it should not be necessary to reflect the assigned
504 * base outside of the GPIO subsystem. Go over drivers and
505 * see if anyone makes use of this, else drop this and assign
506 * a poison instead.
507 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700508 chip->base = base;
509 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100510 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700511
Linus Walleijff2b1352015-10-20 11:10:38 +0200512 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +0100513 if (status) {
514 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100515 goto err_free_gdev;
Johan Hovold05aa5202015-01-12 17:12:26 +0100516 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900517
Linus Walleijff2b1352015-10-20 11:10:38 +0200518 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100519 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d2008-07-25 01:46:07 -0700520
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100521 desc->gdev = gdev;
Johan Hovold05aa5202015-01-12 17:12:26 +0100522
523 /* REVISIT: most hardware initializes GPIOs as inputs (often
524 * with pullups enabled) so power usage is minimized. Linux
525 * code should set the gpio direction first thing; but until
526 * it does, and in case chip->get_direction is not set, we may
527 * expose the wrong direction in sysfs.
528 */
529 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800530 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900531
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800532 spin_unlock_irqrestore(&gpio_lock, flags);
533
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530534#ifdef CONFIG_PINCTRL
Linus Walleijff2b1352015-10-20 11:10:38 +0200535 /* FIXME: move pin ranges to gpio_device */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530536 INIT_LIST_HEAD(&chip->pin_ranges);
537#endif
538
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200539 status = gpiochip_set_desc_names(chip);
540 if (status)
541 goto err_remove_from_list;
542
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200543 status = of_gpiochip_add(chip);
544 if (status)
545 goto err_remove_chip;
546
Mika Westerberg664e3e52014-01-08 12:40:54 +0200547 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600548
Linus Walleij3c702e92015-10-21 15:29:53 +0200549 /*
550 * By first adding the chardev, and then adding the device,
551 * we get a device node entry in sysfs under
552 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
553 * coldplug of device nodes and other udev business.
554 */
555 cdev_init(&gdev->chrdev, &gpio_fileops);
556 gdev->chrdev.owner = THIS_MODULE;
557 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
558 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
559 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
560 if (status < 0)
561 chip_warn(chip, "failed to add char device %d:%d\n",
562 MAJOR(gpio_devt), gdev->id);
563 else
564 chip_dbg(chip, "added GPIO chardev (%d:%d)\n",
565 MAJOR(gpio_devt), gdev->id);
Linus Walleijff2b1352015-10-20 11:10:38 +0200566 status = device_add(&gdev->dev);
Johan Hovold225fce82015-01-12 17:12:25 +0100567 if (status)
Linus Walleij3c702e92015-10-21 15:29:53 +0200568 goto err_remove_chardev;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600569
Linus Walleijafbc4f32016-02-09 13:21:06 +0100570 status = gpiochip_sysfs_register(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200571 if (status)
572 goto err_remove_device;
573
574 /* From this point, the .release() function cleans up gpio_device */
575 gdev->dev.release = gpiodevice_release;
576 get_device(&gdev->dev);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100577 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
578 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
579 dev_name(&gdev->dev), chip->label ? : "generic");
Grant Likely64842aa2011-11-06 11:36:18 -0700580
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600581 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800582
Linus Walleijff2b1352015-10-20 11:10:38 +0200583err_remove_device:
584 device_del(&gdev->dev);
Linus Walleij3c702e92015-10-21 15:29:53 +0200585err_remove_chardev:
586 cdev_del(&gdev->chrdev);
Johan Hovold225fce82015-01-12 17:12:25 +0100587err_remove_chip:
588 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200589 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100590 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200591err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100592 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200593 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800594 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200595err_free_gdev:
596 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -0800597 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200598 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100599 gdev->base, gdev->base + gdev->ngpio - 1,
600 chip->label ? : "generic");
601 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -0800602 return status;
603}
Linus Walleijb08ea352015-12-03 15:14:13 +0100604EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -0800605
606/**
607 * gpiochip_remove() - unregister a gpio_chip
608 * @chip: the chip to unregister
609 *
610 * A gpio_chip with any GPIOs still requested may not be removed.
611 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200612void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800613{
Linus Walleijff2b1352015-10-20 11:10:38 +0200614 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200615 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800616 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100617 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200618 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800619
Linus Walleijff2b1352015-10-20 11:10:38 +0200620 /* Numb the device, cancelling all outstanding operations */
621 gdev->chip = NULL;
622
623 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +0100624 gpiochip_sysfs_unregister(gdev);
Johan Hovold00acc3d2015-01-12 17:12:27 +0100625 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200626 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100627 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600628 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600629 of_gpiochip_remove(chip);
630
Johan Hovold6798aca2015-01-12 17:12:28 +0100631 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100632 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100633 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +0200634 if (test_bit(FLAG_REQUESTED, &desc->flags))
635 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800636 }
David Brownelld2876d02008-02-04 22:28:20 -0800637 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900638
Johan Hovoldfab28b82015-05-04 17:10:27 +0200639 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100640 dev_crit(&gdev->dev,
Linus Walleij58383c72015-11-04 09:56:26 +0100641 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +0200642
Linus Walleijff2b1352015-10-20 11:10:38 +0200643 /*
644 * The gpiochip side puts its use of the device to rest here:
645 * if there are no userspace clients, the chardev and device will
646 * be removed, else it will be dangling until the last user is
647 * gone.
648 */
649 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -0800650}
651EXPORT_SYMBOL_GPL(gpiochip_remove);
652
Grant Likely594fa262010-06-08 07:48:16 -0600653/**
654 * gpiochip_find() - iterator for locating a specific gpio_chip
655 * @data: data to pass to match function
656 * @callback: Callback function to check gpio_chip
657 *
658 * Similar to bus_find_device. It returns a reference to a gpio_chip as
659 * determined by a user supplied @match callback. The callback should return
660 * 0 if the device doesn't match and non-zero if it does. If the callback is
661 * non-zero, this function will return to the caller and not iterate over any
662 * more gpio_chips.
663 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600664struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700665 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600666 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600667{
Linus Walleijff2b1352015-10-20 11:10:38 +0200668 struct gpio_device *gdev;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900669 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600670 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600671
672 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200673 list_for_each_entry(gdev, &gpio_devices, list)
674 if (match(gdev->chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600675 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900676
677 /* No match? */
Linus Walleijff2b1352015-10-20 11:10:38 +0200678 if (&gdev->list == &gpio_devices)
Alexandre Courbot125eef92013-02-03 01:29:26 +0900679 chip = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +0200680 else
681 chip = gdev->chip;
682
Grant Likely594fa262010-06-08 07:48:16 -0600683 spin_unlock_irqrestore(&gpio_lock, flags);
684
685 return chip;
686}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600687EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800688
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900689static int gpiochip_match_name(struct gpio_chip *chip, void *data)
690{
691 const char *name = data;
692
693 return !strcmp(chip->label, name);
694}
695
696static struct gpio_chip *find_chip_by_name(const char *name)
697{
698 return gpiochip_find((void *)name, gpiochip_match_name);
699}
700
Linus Walleij14250522014-03-25 10:40:18 +0100701#ifdef CONFIG_GPIOLIB_IRQCHIP
702
703/*
704 * The following is irqchip helper code for gpiochips.
705 */
706
707/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200708 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
709 * @gpiochip: the gpiochip to set the irqchip chain to
710 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100711 * @parent_irq: the irq number corresponding to the parent IRQ for this
712 * chained irqchip
713 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200714 * coming out of the gpiochip. If the interrupt is nested rather than
715 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100716 */
717void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
718 struct irq_chip *irqchip,
719 int parent_irq,
720 irq_flow_handler_t parent_handler)
721{
Linus Walleij83141a72014-09-26 13:50:12 +0200722 unsigned int offset;
723
Linus Walleij83141a72014-09-26 13:50:12 +0200724 if (!gpiochip->irqdomain) {
725 chip_err(gpiochip, "called %s before setting up irqchip\n",
726 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200727 return;
728 }
729
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200730 if (parent_handler) {
731 if (gpiochip->can_sleep) {
732 chip_err(gpiochip,
733 "you cannot have chained interrupts on a "
734 "chip that may sleep\n");
735 return;
736 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200737 /*
738 * The parent irqchip is already using the chip_data for this
739 * irqchip, so our callbacks simply use the handler_data.
740 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +0200741 irq_set_chained_handler_and_data(parent_irq, parent_handler,
742 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300743
744 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200745 }
Linus Walleij83141a72014-09-26 13:50:12 +0200746
747 /* Set the parent IRQ for all affected IRQs */
748 for (offset = 0; offset < gpiochip->ngpio; offset++)
749 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
750 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100751}
752EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
753
754/**
755 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
756 * @d: the irqdomain used by this irqchip
757 * @irq: the global irq number used by this GPIO irqchip irq
758 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
759 *
760 * This function will set up the mapping for a certain IRQ line on a
761 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
762 * stored inside the gpiochip.
763 */
764static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
765 irq_hw_number_t hwirq)
766{
767 struct gpio_chip *chip = d->host_data;
768
Linus Walleij14250522014-03-25 10:40:18 +0100769 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300770 /*
771 * This lock class tells lockdep that GPIO irqs are in a different
772 * category than their parents, so it won't report false recursion.
773 */
774 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +0200775 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200776 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300777 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200778 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100779 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -0500780
Linus Walleij1333b902014-04-23 16:45:12 +0200781 /*
782 * No set-up of the hardware will happen if IRQ_TYPE_NONE
783 * is passed as default type.
784 */
785 if (chip->irq_default_type != IRQ_TYPE_NONE)
786 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100787
788 return 0;
789}
790
Linus Walleijc3626fd2014-03-28 20:42:01 +0100791static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
792{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200793 struct gpio_chip *chip = d->host_data;
794
Linus Walleij1c8732b2014-04-09 13:34:39 +0200795 if (chip->can_sleep)
796 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100797 irq_set_chip_and_handler(irq, NULL, NULL);
798 irq_set_chip_data(irq, NULL);
799}
800
Linus Walleij14250522014-03-25 10:40:18 +0100801static const struct irq_domain_ops gpiochip_domain_ops = {
802 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100803 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100804 /* Virtually all GPIO irqchips are twocell:ed */
805 .xlate = irq_domain_xlate_twocell,
806};
807
808static int gpiochip_irq_reqres(struct irq_data *d)
809{
810 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
811
Linus Walleijff2b1352015-10-20 11:10:38 +0200812 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300813 return -ENODEV;
814
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900815 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100816 chip_err(chip,
817 "unable to lock HW IRQ %lu for IRQ\n",
818 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200819 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100820 return -EINVAL;
821 }
822 return 0;
823}
824
825static void gpiochip_irq_relres(struct irq_data *d)
826{
827 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
828
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900829 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200830 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100831}
832
833static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
834{
835 return irq_find_mapping(chip->irqdomain, offset);
836}
837
838/**
839 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
840 * @gpiochip: the gpiochip to remove the irqchip from
841 *
842 * This is called only from gpiochip_remove()
843 */
844static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
845{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100846 unsigned int offset;
847
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300848 acpi_gpiochip_free_interrupts(gpiochip);
849
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300850 if (gpiochip->irq_parent) {
851 irq_set_chained_handler(gpiochip->irq_parent, NULL);
852 irq_set_handler_data(gpiochip->irq_parent, NULL);
853 }
854
Linus Walleijc3626fd2014-03-28 20:42:01 +0100855 /* Remove all IRQ mappings and delete the domain */
856 if (gpiochip->irqdomain) {
857 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300858 irq_dispose_mapping(
859 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100860 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100861 }
Linus Walleij14250522014-03-25 10:40:18 +0100862
863 if (gpiochip->irqchip) {
864 gpiochip->irqchip->irq_request_resources = NULL;
865 gpiochip->irqchip->irq_release_resources = NULL;
866 gpiochip->irqchip = NULL;
867 }
868}
869
870/**
871 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
872 * @gpiochip: the gpiochip to add the irqchip to
873 * @irqchip: the irqchip to add to the gpiochip
874 * @first_irq: if not dynamically assigned, the base (first) IRQ to
875 * allocate gpiochip irqs from
876 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200877 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
878 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300879 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +0100880 *
881 * This function closely associates a certain irqchip with a certain
882 * gpiochip, providing an irq domain to translate the local IRQs to
883 * global irqs in the gpiolib core, and making sure that the gpiochip
884 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +0100885 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +0100886 * from the gpiochip passed as chip data. An irqdomain will be stored
887 * in the gpiochip that shall be used by the driver to handle IRQ number
888 * translation. The gpiochip will need to be initialized and registered
889 * before calling this function.
890 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100891 * This function will handle two cell:ed simple IRQs and assumes all
892 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100893 * need to be open coded.
894 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300895int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
896 struct irq_chip *irqchip,
897 unsigned int first_irq,
898 irq_flow_handler_t handler,
899 unsigned int type,
900 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +0100901{
902 struct device_node *of_node;
903 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100904 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100905
906 if (!gpiochip || !irqchip)
907 return -EINVAL;
908
Linus Walleij58383c72015-11-04 09:56:26 +0100909 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +0100910 pr_err("missing gpiochip .dev parent pointer\n");
911 return -EINVAL;
912 }
Linus Walleij58383c72015-11-04 09:56:26 +0100913 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +0100914#ifdef CONFIG_OF_GPIO
915 /*
Colin Cronin20a8a962015-05-18 11:41:43 -0700916 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800917 * FIXME: get rid of this and use gpiochip->parent->of_node
918 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +0100919 */
920 if (gpiochip->of_node)
921 of_node = gpiochip->of_node;
922#endif
923 gpiochip->irqchip = irqchip;
924 gpiochip->irq_handler = handler;
925 gpiochip->irq_default_type = type;
926 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300927 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +0100928 gpiochip->irqdomain = irq_domain_add_simple(of_node,
929 gpiochip->ngpio, first_irq,
930 &gpiochip_domain_ops, gpiochip);
931 if (!gpiochip->irqdomain) {
932 gpiochip->irqchip = NULL;
933 return -EINVAL;
934 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +0200935
936 /*
937 * It is possible for a driver to override this, but only if the
938 * alternative functions are both implemented.
939 */
940 if (!irqchip->irq_request_resources &&
941 !irqchip->irq_release_resources) {
942 irqchip->irq_request_resources = gpiochip_irq_reqres;
943 irqchip->irq_release_resources = gpiochip_irq_relres;
944 }
Linus Walleij14250522014-03-25 10:40:18 +0100945
946 /*
947 * Prepare the mapping since the irqchip shall be orthogonal to
948 * any gpiochip calls. If the first_irq was zero, this is
949 * necessary to allocate descriptors for all IRQs.
950 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100951 for (offset = 0; offset < gpiochip->ngpio; offset++) {
952 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
953 if (offset == 0)
954 /*
955 * Store the base into the gpiochip to be used when
956 * unmapping the irqs.
957 */
958 gpiochip->irq_base = irq_base;
959 }
Linus Walleij14250522014-03-25 10:40:18 +0100960
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300961 acpi_gpiochip_request_interrupts(gpiochip);
962
Linus Walleij14250522014-03-25 10:40:18 +0100963 return 0;
964}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300965EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +0100966
967#else /* CONFIG_GPIOLIB_IRQCHIP */
968
969static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
970
971#endif /* CONFIG_GPIOLIB_IRQCHIP */
972
Jonas Gorskic771c2f2015-10-11 17:34:15 +0200973/**
974 * gpiochip_generic_request() - request the gpio function for a pin
975 * @chip: the gpiochip owning the GPIO
976 * @offset: the offset of the GPIO to request for GPIO function
977 */
978int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
979{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100980 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +0200981}
982EXPORT_SYMBOL_GPL(gpiochip_generic_request);
983
984/**
985 * gpiochip_generic_free() - free the gpio function from a pin
986 * @chip: the gpiochip to request the gpio function for
987 * @offset: the offset of the GPIO to free from GPIO function
988 */
989void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
990{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100991 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +0200992}
993EXPORT_SYMBOL_GPL(gpiochip_generic_free);
994
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530995#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100996
Linus Walleij3f0f8672012-11-20 12:40:15 +0100997/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200998 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
999 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001000 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001001 * @gpio_offset: the start offset in the current gpio_chip number space
1002 * @pin_group: name of the pin group inside the pin controller
1003 */
1004int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1005 struct pinctrl_dev *pctldev,
1006 unsigned int gpio_offset, const char *pin_group)
1007{
1008 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001009 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001010 int ret;
1011
1012 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1013 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001014 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001015 return -ENOMEM;
1016 }
1017
1018 /* Use local offset as range ID */
1019 pin_range->range.id = gpio_offset;
1020 pin_range->range.gc = chip;
1021 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001022 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001023 pin_range->pctldev = pctldev;
1024
1025 ret = pinctrl_get_group_pins(pctldev, pin_group,
1026 &pin_range->range.pins,
1027 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001028 if (ret < 0) {
1029 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001030 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001031 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001032
1033 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1034
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001035 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1036 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001037 pinctrl_dev_get_devname(pctldev), pin_group);
1038
1039 list_add_tail(&pin_range->node, &chip->pin_ranges);
1040
1041 return 0;
1042}
1043EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1044
1045/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001046 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1047 * @chip: the gpiochip to add the range for
1048 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001049 * @gpio_offset: the start offset in the current gpio_chip number space
1050 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001051 * @npins: the number of pins from the offset of each pin space (GPIO and
1052 * pin controller) to accumulate in this range
1053 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001054int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001055 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001056 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301057{
1058 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001059 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001060 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301061
Linus Walleij3f0f8672012-11-20 12:40:15 +01001062 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301063 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001064 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001065 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301066 }
1067
Linus Walleij3f0f8672012-11-20 12:40:15 +01001068 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001069 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001070 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301071 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001072 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001073 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301074 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001075 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301076 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001077 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001078 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001079 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001080 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001081 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001082 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001083 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1084 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001085 pinctl_name,
1086 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301087
1088 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001089
1090 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301091}
Linus Walleij165adc92012-11-06 14:49:39 +01001092EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301093
Linus Walleij3f0f8672012-11-20 12:40:15 +01001094/**
1095 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1096 * @chip: the chip to remove all the mappings for
1097 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301098void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1099{
1100 struct gpio_pin_range *pin_range, *tmp;
1101
1102 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1103 list_del(&pin_range->node);
1104 pinctrl_remove_gpio_range(pin_range->pctldev,
1105 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001106 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301107 }
1108}
Linus Walleij165adc92012-11-06 14:49:39 +01001109EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1110
1111#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301112
David Brownelld2876d02008-02-04 22:28:20 -08001113/* These "optional" allocation calls help prevent drivers from stomping
1114 * on each other, and help provide better diagnostics in debugfs.
1115 * They're called even less than the "set direction" calls.
1116 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001117static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001118{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001119 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001120 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001121 unsigned long flags;
1122
1123 spin_lock_irqsave(&gpio_lock, flags);
1124
David Brownelld2876d02008-02-04 22:28:20 -08001125 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001126 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001127 */
1128
1129 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1130 desc_set_label(desc, label ? : "?");
1131 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001132 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001133 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001134 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001135 }
1136
1137 if (chip->request) {
1138 /* chip->request may sleep */
1139 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001140 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001141 spin_lock_irqsave(&gpio_lock, flags);
1142
1143 if (status < 0) {
1144 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001145 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001146 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001147 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001148 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001149 if (chip->get_direction) {
1150 /* chip->get_direction may sleep */
1151 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001152 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001153 spin_lock_irqsave(&gpio_lock, flags);
1154 }
David Brownelld2876d02008-02-04 22:28:20 -08001155done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +03001156 if (status < 0) {
1157 /* Clear flags that might have been set by the caller before
1158 * requesting the GPIO.
1159 */
1160 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1161 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1162 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1163 }
Mika Westerberg77c2d792014-03-10 14:54:50 +02001164 spin_unlock_irqrestore(&gpio_lock, flags);
1165 return status;
1166}
1167
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001168/*
1169 * This descriptor validation needs to be inserted verbatim into each
1170 * function taking a descriptor, so we need to use a preprocessor
1171 * macro to avoid endless duplication.
1172 */
1173#define VALIDATE_DESC(desc) do { \
1174 if (!desc || !desc->gdev) { \
1175 pr_warn("%s: invalid GPIO\n", __func__); \
1176 return -EINVAL; \
1177 } \
1178 if ( !desc->gdev->chip ) { \
1179 dev_warn(&desc->gdev->dev, \
1180 "%s: backing chip is gone\n", __func__); \
1181 return 0; \
1182 } } while (0)
1183
1184#define VALIDATE_DESC_VOID(desc) do { \
1185 if (!desc || !desc->gdev) { \
1186 pr_warn("%s: invalid GPIO\n", __func__); \
1187 return; \
1188 } \
1189 if (!desc->gdev->chip) { \
1190 dev_warn(&desc->gdev->dev, \
1191 "%s: backing chip is gone\n", __func__); \
1192 return; \
1193 } } while (0)
1194
1195
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001196int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001197{
1198 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001199 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001200
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001201 VALIDATE_DESC(desc);
1202 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001203
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001204 if (try_module_get(gdev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001205 status = __gpiod_request(desc, label);
1206 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001207 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001208 else
1209 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001210 }
1211
David Brownelld2876d02008-02-04 22:28:20 -08001212 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001213 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001214
David Brownelld2876d02008-02-04 22:28:20 -08001215 return status;
1216}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001217
Mika Westerberg77c2d792014-03-10 14:54:50 +02001218static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001219{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001220 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001221 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001222 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001223
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001224 might_sleep();
1225
Alexandre Courbot372e7222013-02-03 01:29:29 +09001226 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001227
David Brownelld2876d02008-02-04 22:28:20 -08001228 spin_lock_irqsave(&gpio_lock, flags);
1229
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001230 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07001231 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1232 if (chip->free) {
1233 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001234 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001235 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001236 spin_lock_irqsave(&gpio_lock, flags);
1237 }
David Brownelld2876d02008-02-04 22:28:20 -08001238 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001239 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001240 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301241 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301242 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001243 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001244 ret = true;
1245 }
David Brownelld2876d02008-02-04 22:28:20 -08001246
1247 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001248 return ret;
1249}
1250
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001251void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001252{
Linus Walleij33a68e82016-02-11 10:28:44 +01001253 if (desc && desc->gdev && __gpiod_free(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001254 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001255 put_device(&desc->gdev->dev);
1256 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001257 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01001258 }
David Brownelld2876d02008-02-04 22:28:20 -08001259}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001260
David Brownelld2876d02008-02-04 22:28:20 -08001261/**
1262 * gpiochip_is_requested - return string iff signal was requested
1263 * @chip: controller managing the signal
1264 * @offset: of signal within controller's 0..(ngpio - 1) range
1265 *
1266 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001267 * The string returned is the label passed to gpio_request(); if none has been
1268 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001269 *
1270 * This function is for use by GPIO controller drivers. The label can
1271 * help with diagnostics, and knowing that the signal is used as a GPIO
1272 * can help avoid accidentally multiplexing it to another controller.
1273 */
1274const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1275{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001276 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001277
Dirk Behme48b59532015-08-18 18:02:32 +02001278 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001279 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001280
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001281 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001282
Alexandre Courbot372e7222013-02-03 01:29:29 +09001283 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001284 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001285 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001286}
1287EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1288
Mika Westerberg77c2d792014-03-10 14:54:50 +02001289/**
1290 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1291 * @desc: GPIO descriptor to request
1292 * @label: label for the GPIO
1293 *
1294 * Function allows GPIO chip drivers to request and use their own GPIO
1295 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1296 * function will not increase reference count of the GPIO chip module. This
1297 * allows the GPIO chip module to be unloaded as needed (we assume that the
1298 * GPIO chip driver handles freeing the GPIOs it has requested).
1299 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001300struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1301 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001302{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001303 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1304 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001305
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001306 if (IS_ERR(desc)) {
1307 chip_err(chip, "failed to get GPIO descriptor\n");
1308 return desc;
1309 }
1310
1311 err = __gpiod_request(desc, label);
1312 if (err < 0)
1313 return ERR_PTR(err);
1314
1315 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001316}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001317EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001318
1319/**
1320 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1321 * @desc: GPIO descriptor to free
1322 *
1323 * Function frees the given GPIO requested previously with
1324 * gpiochip_request_own_desc().
1325 */
1326void gpiochip_free_own_desc(struct gpio_desc *desc)
1327{
1328 if (desc)
1329 __gpiod_free(desc);
1330}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001331EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08001332
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001333/*
1334 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08001335 * some cases this is done in early boot, before IRQs are enabled.
1336 *
1337 * As a rule these aren't called more than once (except for drivers
1338 * using the open-drain emulation idiom) so these are natural places
1339 * to accumulate extra debugging checks. Note that we can't (yet)
1340 * rely on gpio_request() having been called beforehand.
1341 */
1342
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001343/**
1344 * gpiod_direction_input - set the GPIO direction to input
1345 * @desc: GPIO to set to input
1346 *
1347 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1348 * be called safely on it.
1349 *
1350 * Return 0 in case of success, else an error code.
1351 */
1352int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001353{
David Brownelld2876d02008-02-04 22:28:20 -08001354 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001355 int status = -EINVAL;
1356
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001357 VALIDATE_DESC(desc);
1358 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001359
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001360 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001361 gpiod_warn(desc,
1362 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001363 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001364 return -EIO;
1365 }
1366
Alexandre Courbotd82da792014-07-22 16:17:43 +09001367 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001368 if (status == 0)
1369 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001370
Alexandre Courbot372e7222013-02-03 01:29:29 +09001371 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001372
David Brownelld2876d02008-02-04 22:28:20 -08001373 return status;
1374}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001375EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001376
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001377static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001378{
David Brownelld2876d02008-02-04 22:28:20 -08001379 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001380 int status = -EINVAL;
1381
Linus Walleijd468bf92013-09-24 11:54:38 +02001382 /* GPIOs used for IRQs shall not be set as output */
1383 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1384 gpiod_err(desc,
1385 "%s: tried to set a GPIO tied to an IRQ as output\n",
1386 __func__);
1387 return -EIO;
1388 }
1389
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301390 /* Open drain pin should not be driven to 1 */
1391 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001392 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301393
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301394 /* Open source pin should not be driven to 0 */
1395 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001396 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301397
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001398 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001399 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001400 gpiod_warn(desc,
1401 "%s: missing set() or direction_output() operations\n",
1402 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001403 return -EIO;
1404 }
1405
Alexandre Courbotd82da792014-07-22 16:17:43 +09001406 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001407 if (status == 0)
1408 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001409 trace_gpio_value(desc_to_gpio(desc), 0, value);
1410 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001411 return status;
1412}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001413
1414/**
1415 * gpiod_direction_output_raw - set the GPIO direction to output
1416 * @desc: GPIO to set to output
1417 * @value: initial output value of the GPIO
1418 *
1419 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1420 * be called safely on it. The initial value of the output must be specified
1421 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1422 *
1423 * Return 0 in case of success, else an error code.
1424 */
1425int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1426{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001427 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001428 return _gpiod_direction_output_raw(desc, value);
1429}
1430EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1431
1432/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301433 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001434 * @desc: GPIO to set to output
1435 * @value: initial output value of the GPIO
1436 *
1437 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1438 * be called safely on it. The initial value of the output must be specified
1439 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1440 * account.
1441 *
1442 * Return 0 in case of success, else an error code.
1443 */
1444int gpiod_direction_output(struct gpio_desc *desc, int value)
1445{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001446 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001447 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1448 value = !value;
1449 return _gpiod_direction_output_raw(desc, value);
1450}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001451EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001452
Felipe Balbic4b5be92010-05-26 14:42:23 -07001453/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001454 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001455 * @gpio: the gpio to set debounce time
1456 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001457 *
1458 * returns -ENOTSUPP if the controller does not support setting
1459 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001460 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001461int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001462{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001463 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001464
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001465 VALIDATE_DESC(desc);
1466 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001467 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001468 gpiod_dbg(desc,
1469 "%s: missing set() or set_debounce() operations\n",
1470 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001471 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001472 }
1473
Alexandre Courbotd82da792014-07-22 16:17:43 +09001474 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001475}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001476EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001477
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001478/**
1479 * gpiod_is_active_low - test whether a GPIO is active-low or not
1480 * @desc: the gpio descriptor to test
1481 *
1482 * Returns 1 if the GPIO is active-low, 0 otherwise.
1483 */
1484int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001485{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001486 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001487 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001488}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001489EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001490
1491/* I/O calls are only valid after configuration completed; the relevant
1492 * "is this a valid GPIO" error checks should already have been done.
1493 *
1494 * "Get" operations are often inlinable as reading a pin value register,
1495 * and masking the relevant bit in that register.
1496 *
1497 * When "set" operations are inlinable, they involve writing that mask to
1498 * one register to set a low value, or a different register to set it high.
1499 * Otherwise locking is needed, so there may be little value to inlining.
1500 *
1501 *------------------------------------------------------------------------
1502 *
1503 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1504 * have requested the GPIO. That can include implicit requesting by
1505 * a direction setting call. Marking a gpio as requested locks its chip
1506 * in memory, guaranteeing that these table lookups need no more locking
1507 * and that gpiochip_remove() will fail.
1508 *
1509 * REVISIT when debugging, consider adding some instrumentation to ensure
1510 * that the GPIO was actually requested.
1511 */
1512
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001513static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001514{
1515 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001516 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001517 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001518
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001519 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001520 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001521 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01001522 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001523 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001524 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001525}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001526
David Brownelld2876d02008-02-04 22:28:20 -08001527/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001528 * gpiod_get_raw_value() - return a gpio's raw value
1529 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001530 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001531 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001532 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001533 *
1534 * This function should be called from contexts where we cannot sleep, and will
1535 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001536 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001537int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001538{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001539 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001540 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001541 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001542 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001543}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001544EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001545
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001546/**
1547 * gpiod_get_value() - return a gpio's value
1548 * @desc: gpio whose value will be returned
1549 *
1550 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001551 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001552 *
1553 * This function should be called from contexts where we cannot sleep, and will
1554 * complain if the GPIO chip functions potentially sleep.
1555 */
1556int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001557{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001558 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001559
1560 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001561 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001562 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001563
1564 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001565 if (value < 0)
1566 return value;
1567
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001568 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1569 value = !value;
1570
1571 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001572}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001573EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001574
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301575/*
1576 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001577 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001578 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301579 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001580static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301581{
1582 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001583 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001584 int offset = gpio_chip_hwgpio(desc);
1585
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301586 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001587 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301588 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001589 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301590 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001591 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301592 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001593 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301594 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001595 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301596 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001597 gpiod_err(desc,
1598 "%s: Error in set_value for open drain err %d\n",
1599 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301600}
1601
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301602/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001603 * _gpio_set_open_source_value() - Set the open source gpio's value.
1604 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001605 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301606 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001607static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301608{
1609 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001610 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001611 int offset = gpio_chip_hwgpio(desc);
1612
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301613 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001614 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301615 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001616 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301617 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001618 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301619 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001620 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301621 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001622 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301623 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001624 gpiod_err(desc,
1625 "%s: Error in set_value for open source err %d\n",
1626 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301627}
1628
Alexandre Courbot23600962014-03-11 15:52:09 +09001629static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001630{
1631 struct gpio_chip *chip;
1632
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001633 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001634 trace_gpio_value(desc_to_gpio(desc), 0, value);
1635 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1636 _gpio_set_open_drain_value(desc, value);
1637 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1638 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301639 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001640 chip->set(chip, gpio_chip_hwgpio(desc), value);
1641}
1642
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001643/*
1644 * set multiple outputs on the same chip;
1645 * use the chip's set_multiple function if available;
1646 * otherwise set the outputs sequentially;
1647 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1648 * defines which outputs are to be changed
1649 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1650 * defines the values the outputs specified by mask are to be set to
1651 */
1652static void gpio_chip_set_multiple(struct gpio_chip *chip,
1653 unsigned long *mask, unsigned long *bits)
1654{
1655 if (chip->set_multiple) {
1656 chip->set_multiple(chip, mask, bits);
1657 } else {
1658 int i;
1659 for (i = 0; i < chip->ngpio; i++) {
1660 if (mask[BIT_WORD(i)] == 0) {
1661 /* no more set bits in this mask word;
1662 * skip ahead to the next word */
1663 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1664 continue;
1665 }
1666 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001667 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001668 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001669 }
1670 }
1671}
1672
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001673static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1674 unsigned int array_size,
1675 struct gpio_desc **desc_array,
1676 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001677{
1678 int i = 0;
1679
1680 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001681 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001682 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1683 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1684 int count = 0;
1685
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001686 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001687 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001688
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001689 memset(mask, 0, sizeof(mask));
1690 do {
1691 struct gpio_desc *desc = desc_array[i];
1692 int hwgpio = gpio_chip_hwgpio(desc);
1693 int value = value_array[i];
1694
1695 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1696 value = !value;
1697 trace_gpio_value(desc_to_gpio(desc), 0, value);
1698 /*
1699 * collect all normal outputs belonging to the same chip
1700 * open drain and open source outputs are set individually
1701 */
1702 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001703 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001704 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1705 _gpio_set_open_source_value(desc, value);
1706 } else {
1707 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001708 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001709 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001710 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001711 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001712 count++;
1713 }
1714 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001715 } while ((i < array_size) &&
1716 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001717 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001718 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001719 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001720 }
1721}
1722
David Brownelld2876d02008-02-04 22:28:20 -08001723/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001724 * gpiod_set_raw_value() - assign a gpio's raw value
1725 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001726 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001727 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001728 * Set the raw value of the GPIO, i.e. the value of its physical line without
1729 * regard for its ACTIVE_LOW status.
1730 *
1731 * This function should be called from contexts where we cannot sleep, and will
1732 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001733 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001734void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001735{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001736 VALIDATE_DESC_VOID(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001737 /* Should be using gpio_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001738 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001739 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001740}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001741EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001742
1743/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001744 * gpiod_set_value() - assign a gpio's value
1745 * @desc: gpio whose value will be assigned
1746 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001747 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001748 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1749 * account
1750 *
1751 * This function should be called from contexts where we cannot sleep, and will
1752 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001753 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001754void gpiod_set_value(struct gpio_desc *desc, int value)
1755{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001756 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001757 /* Should be using gpio_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001758 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001759 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1760 value = !value;
1761 _gpiod_set_raw_value(desc, value);
1762}
1763EXPORT_SYMBOL_GPL(gpiod_set_value);
1764
1765/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001766 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001767 * @array_size: number of elements in the descriptor / value arrays
1768 * @desc_array: array of GPIO descriptors whose values will be assigned
1769 * @value_array: array of values to assign
1770 *
1771 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1772 * without regard for their ACTIVE_LOW status.
1773 *
1774 * This function should be called from contexts where we cannot sleep, and will
1775 * complain if the GPIO chip functions potentially sleep.
1776 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001777void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001778 struct gpio_desc **desc_array, int *value_array)
1779{
1780 if (!desc_array)
1781 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001782 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1783 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001784}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001785EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001786
1787/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001788 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001789 * @array_size: number of elements in the descriptor / value arrays
1790 * @desc_array: array of GPIO descriptors whose values will be assigned
1791 * @value_array: array of values to assign
1792 *
1793 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1794 * into account.
1795 *
1796 * This function should be called from contexts where we cannot sleep, and will
1797 * complain if the GPIO chip functions potentially sleep.
1798 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001799void gpiod_set_array_value(unsigned int array_size,
1800 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001801{
1802 if (!desc_array)
1803 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001804 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1805 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001806}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001807EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001808
1809/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001810 * gpiod_cansleep() - report whether gpio value access may sleep
1811 * @desc: gpio to check
1812 *
1813 */
1814int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001815{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001816 VALIDATE_DESC(desc);
1817 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001818}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001819EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001820
David Brownell0f6d5042008-10-15 22:03:14 -07001821/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001822 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1823 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001824 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001825 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1826 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001827 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001828int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001829{
1830 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001831 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001832
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001833 VALIDATE_DESC(desc);
1834 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001835 offset = gpio_chip_hwgpio(desc);
1836 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1837}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001838EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001839
Linus Walleijd468bf92013-09-24 11:54:38 +02001840/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001841 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001842 * @chip: the chip the GPIO to lock belongs to
1843 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001844 *
1845 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001846 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001847 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001848int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001849{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001850 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001851 return -EINVAL;
1852
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001853 if (test_bit(FLAG_IS_OUT, &chip->gpiodev->descs[offset].flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001854 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001855 "%s: tried to flag a GPIO set as output for IRQ\n",
1856 __func__);
1857 return -EIO;
1858 }
1859
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001860 set_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001861 return 0;
1862}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001863EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001864
Linus Walleijd468bf92013-09-24 11:54:38 +02001865/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001866 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001867 * @chip: the chip the GPIO to lock belongs to
1868 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001869 *
1870 * This is used directly by GPIO drivers that want to indicate
1871 * that a certain GPIO is no longer used exclusively for IRQ.
1872 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001873void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001874{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001875 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001876 return;
1877
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001878 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001879}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001880EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001881
Linus Walleij6cee3822016-02-11 20:16:45 +01001882bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
1883{
1884 if (offset >= chip->ngpio)
1885 return false;
1886
1887 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
1888}
1889EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
1890
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001891/**
1892 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1893 * @desc: gpio whose value will be returned
1894 *
1895 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001896 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001897 *
1898 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001899 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001900int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001901{
David Brownelld2876d02008-02-04 22:28:20 -08001902 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001903 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001904 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001905}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001906EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001907
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001908/**
1909 * gpiod_get_value_cansleep() - return a gpio's value
1910 * @desc: gpio whose value will be returned
1911 *
1912 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001913 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001914 *
1915 * This function is to be called from contexts that can sleep.
1916 */
1917int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001918{
David Brownelld2876d02008-02-04 22:28:20 -08001919 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001920
1921 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001922 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001923 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001924 if (value < 0)
1925 return value;
1926
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001927 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1928 value = !value;
1929
David Brownelld2876d02008-02-04 22:28:20 -08001930 return value;
1931}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001932EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001933
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001934/**
1935 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1936 * @desc: gpio whose value will be assigned
1937 * @value: value to assign
1938 *
1939 * Set the raw value of the GPIO, i.e. the value of its physical line without
1940 * regard for its ACTIVE_LOW status.
1941 *
1942 * This function is to be called from contexts that can sleep.
1943 */
1944void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001945{
David Brownelld2876d02008-02-04 22:28:20 -08001946 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001947 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001948 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001949}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001950EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001951
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001952/**
1953 * gpiod_set_value_cansleep() - assign a gpio's value
1954 * @desc: gpio whose value will be assigned
1955 * @value: value to assign
1956 *
1957 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1958 * account
1959 *
1960 * This function is to be called from contexts that can sleep.
1961 */
1962void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001963{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001964 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001965 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001966 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1967 value = !value;
1968 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001969}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001970EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001971
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001972/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001973 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001974 * @array_size: number of elements in the descriptor / value arrays
1975 * @desc_array: array of GPIO descriptors whose values will be assigned
1976 * @value_array: array of values to assign
1977 *
1978 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1979 * without regard for their ACTIVE_LOW status.
1980 *
1981 * This function is to be called from contexts that can sleep.
1982 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001983void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1984 struct gpio_desc **desc_array,
1985 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001986{
1987 might_sleep_if(extra_checks);
1988 if (!desc_array)
1989 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001990 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1991 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001992}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001993EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001994
1995/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001996 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001997 * @array_size: number of elements in the descriptor / value arrays
1998 * @desc_array: array of GPIO descriptors whose values will be assigned
1999 * @value_array: array of values to assign
2000 *
2001 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2002 * into account.
2003 *
2004 * This function is to be called from contexts that can sleep.
2005 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002006void gpiod_set_array_value_cansleep(unsigned int array_size,
2007 struct gpio_desc **desc_array,
2008 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002009{
2010 might_sleep_if(extra_checks);
2011 if (!desc_array)
2012 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002013 gpiod_set_array_value_priv(false, true, array_size, desc_array,
2014 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002015}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002016EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002017
2018/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002019 * gpiod_add_lookup_table() - register GPIO device consumers
2020 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002021 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002022void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002023{
2024 mutex_lock(&gpio_lookup_lock);
2025
Alexandre Courbotad824782013-12-03 12:20:11 +09002026 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002027
2028 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002029}
2030
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302031/**
2032 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2033 * @table: table of consumers to unregister
2034 */
2035void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2036{
2037 mutex_lock(&gpio_lookup_lock);
2038
2039 list_del(&table->list);
2040
2041 mutex_unlock(&gpio_lookup_lock);
2042}
2043
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002044static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002045 unsigned int idx,
2046 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002047{
2048 char prop_name[32]; /* 32 is max size of property name */
2049 enum of_gpio_flags of_flags;
2050 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02002051 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002052
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002053 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02002054 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01002055 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002056 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02002057 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01002058 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002059 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002060
Thierry Redingdd34c372014-04-23 17:28:09 +02002061 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2062 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07002063 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02002064 break;
2065 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002066
2067 if (IS_ERR(desc))
2068 return desc;
2069
2070 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002071 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002072
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002073 if (of_flags & OF_GPIO_SINGLE_ENDED) {
2074 if (of_flags & OF_GPIO_ACTIVE_LOW)
2075 *flags |= GPIO_OPEN_DRAIN;
2076 else
2077 *flags |= GPIO_OPEN_SOURCE;
2078 }
2079
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002080 return desc;
2081}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002082
Mika Westerberg81f59e92013-10-10 11:01:09 +03002083static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002084 unsigned int idx,
2085 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03002086{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002087 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03002088 struct acpi_gpio_info info;
2089 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002090 char propname[32];
2091 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03002092
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002093 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002094 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002095 if (con_id && strcmp(con_id, "gpios")) {
2096 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002097 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002098 } else {
2099 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002100 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002101 }
Mika Westerberge01f4402013-10-10 11:01:10 +03002102
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002103 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
2104 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2105 break;
2106 }
2107
2108 /* Then from plain _CRS GPIOs */
2109 if (IS_ERR(desc)) {
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08002110 if (!acpi_can_fallback_to_crs(adev, con_id))
2111 return ERR_PTR(-ENOENT);
2112
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002113 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2114 if (IS_ERR(desc))
2115 return desc;
2116 }
2117
Christophe RICARD52044722015-12-23 23:25:34 +01002118 if (info.polarity == GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002119 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002120
2121 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002122}
2123
Alexandre Courbotad824782013-12-03 12:20:11 +09002124static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2125{
2126 const char *dev_id = dev ? dev_name(dev) : NULL;
2127 struct gpiod_lookup_table *table;
2128
2129 mutex_lock(&gpio_lookup_lock);
2130
2131 list_for_each_entry(table, &gpio_lookup_list, list) {
2132 if (table->dev_id && dev_id) {
2133 /*
2134 * Valid strings on both ends, must be identical to have
2135 * a match
2136 */
2137 if (!strcmp(table->dev_id, dev_id))
2138 goto found;
2139 } else {
2140 /*
2141 * One of the pointers is NULL, so both must be to have
2142 * a match
2143 */
2144 if (dev_id == table->dev_id)
2145 goto found;
2146 }
2147 }
2148 table = NULL;
2149
2150found:
2151 mutex_unlock(&gpio_lookup_lock);
2152 return table;
2153}
2154
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002155static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002156 unsigned int idx,
2157 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002158{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002159 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002160 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002161 struct gpiod_lookup *p;
2162
Alexandre Courbotad824782013-12-03 12:20:11 +09002163 table = gpiod_find_lookup_table(dev);
2164 if (!table)
2165 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002166
Alexandre Courbotad824782013-12-03 12:20:11 +09002167 for (p = &table->table[0]; p->chip_label; p++) {
2168 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002169
Alexandre Courbotad824782013-12-03 12:20:11 +09002170 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002171 if (p->idx != idx)
2172 continue;
2173
Alexandre Courbotad824782013-12-03 12:20:11 +09002174 /* If the lookup entry has a con_id, require exact match */
2175 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2176 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002177
Alexandre Courbotad824782013-12-03 12:20:11 +09002178 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002179
Alexandre Courbotad824782013-12-03 12:20:11 +09002180 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002181 dev_err(dev, "cannot find GPIO chip %s\n",
2182 p->chip_label);
2183 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002184 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002185
Alexandre Courbotad824782013-12-03 12:20:11 +09002186 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002187 dev_err(dev,
2188 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2189 idx, chip->ngpio, chip->label);
2190 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09002191 }
2192
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09002193 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09002194 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002195
2196 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09002197 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002198
2199 return desc;
2200}
2201
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002202static int dt_gpio_count(struct device *dev, const char *con_id)
2203{
2204 int ret;
2205 char propname[32];
2206 unsigned int i;
2207
2208 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2209 if (con_id)
2210 snprintf(propname, sizeof(propname), "%s-%s",
2211 con_id, gpio_suffixes[i]);
2212 else
2213 snprintf(propname, sizeof(propname), "%s",
2214 gpio_suffixes[i]);
2215
2216 ret = of_gpio_named_count(dev->of_node, propname);
2217 if (ret >= 0)
2218 break;
2219 }
2220 return ret;
2221}
2222
2223static int platform_gpio_count(struct device *dev, const char *con_id)
2224{
2225 struct gpiod_lookup_table *table;
2226 struct gpiod_lookup *p;
2227 unsigned int count = 0;
2228
2229 table = gpiod_find_lookup_table(dev);
2230 if (!table)
2231 return -ENOENT;
2232
2233 for (p = &table->table[0]; p->chip_label; p++) {
2234 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2235 (!con_id && !p->con_id))
2236 count++;
2237 }
2238 if (!count)
2239 return -ENOENT;
2240
2241 return count;
2242}
2243
2244/**
2245 * gpiod_count - return the number of GPIOs associated with a device / function
2246 * or -ENOENT if no GPIO has been assigned to the requested function
2247 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2248 * @con_id: function within the GPIO consumer
2249 */
2250int gpiod_count(struct device *dev, const char *con_id)
2251{
2252 int count = -ENOENT;
2253
2254 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2255 count = dt_gpio_count(dev, con_id);
2256 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2257 count = acpi_gpio_count(dev, con_id);
2258
2259 if (count < 0)
2260 count = platform_gpio_count(dev, con_id);
2261
2262 return count;
2263}
2264EXPORT_SYMBOL_GPL(gpiod_count);
2265
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002266/**
Thierry Reding08791622014-04-25 16:54:22 +02002267 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002268 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002269 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002270 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002271 *
2272 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002273 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07002274 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002275 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002276struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002277 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002278{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002279 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002280}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002281EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002282
2283/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002284 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2285 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2286 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002287 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002288 *
2289 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2290 * the requested function it will return NULL. This is convenient for drivers
2291 * that need to handle optional GPIOs.
2292 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002293struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002294 const char *con_id,
2295 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002296{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002297 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002298}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002299EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002300
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002301/**
2302 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2303 * @desc: gpio to be setup
2304 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2305 * of_get_gpio_hog()
2306 *
2307 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2308 */
2309static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2310{
2311 if (lflags & GPIO_ACTIVE_LOW)
2312 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2313 if (lflags & GPIO_OPEN_DRAIN)
2314 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2315 if (lflags & GPIO_OPEN_SOURCE)
2316 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2317}
Benoit Parrotf625d462015-02-02 11:44:44 -06002318
2319/**
2320 * gpiod_configure_flags - helper function to configure a given GPIO
2321 * @desc: gpio whose value will be assigned
2322 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06002323 * @dflags: gpiod_flags - optional GPIO initialization flags
2324 *
2325 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2326 * requested function and/or index, or another IS_ERR() code if an error
2327 * occurred while trying to acquire the GPIO.
2328 */
2329static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002330 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06002331{
2332 int status;
2333
Benoit Parrotf625d462015-02-02 11:44:44 -06002334 /* No particular flag request, return here... */
2335 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2336 pr_debug("no flags found for %s\n", con_id);
2337 return 0;
2338 }
2339
2340 /* Process flags */
2341 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2342 status = gpiod_direction_output(desc,
2343 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2344 else
2345 status = gpiod_direction_input(desc);
2346
2347 return status;
2348}
2349
Thierry Reding29a1f2332014-04-25 17:10:06 +02002350/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002351 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002352 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002353 * @con_id: function within the GPIO consumer
2354 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002355 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002356 *
2357 * This variant of gpiod_get() allows to access GPIOs other than the first
2358 * defined one for functions that define several GPIOs.
2359 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002360 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2361 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07002362 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002363 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002364struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002365 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002366 unsigned int idx,
2367 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002368{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002369 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002370 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002371 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002372
2373 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2374
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002375 if (dev) {
2376 /* Using device tree? */
2377 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2378 dev_dbg(dev, "using device tree for GPIO lookup\n");
2379 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2380 } else if (ACPI_COMPANION(dev)) {
2381 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2382 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2383 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002384 }
2385
2386 /*
2387 * Either we are not using DT or ACPI, or their lookup did not return
2388 * a result. In that case, use platform lookup as a fallback.
2389 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002390 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002391 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002392 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002393 }
2394
2395 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002396 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002397 return desc;
2398 }
2399
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002400 gpiod_parse_flags(desc, lookupflags);
2401
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002402 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002403 if (status < 0)
2404 return ERR_PTR(status);
2405
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002406 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002407 if (status < 0) {
2408 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2409 gpiod_put(desc);
2410 return ERR_PTR(status);
2411 }
2412
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002413 return desc;
2414}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002415EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002416
2417/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002418 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2419 * @fwnode: handle of the firmware node
2420 * @propname: name of the firmware property representing the GPIO
2421 *
2422 * This function can be used for drivers that get their configuration
2423 * from firmware.
2424 *
2425 * Function properly finds the corresponding GPIO using whatever is the
2426 * underlying firmware interface and then makes sure that the GPIO
2427 * descriptor is requested before it is returned to the caller.
2428 *
2429 * In case of error an ERR_PTR() is returned.
2430 */
2431struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2432 const char *propname)
2433{
2434 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2435 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002436 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02002437 int ret;
2438
2439 if (!fwnode)
2440 return ERR_PTR(-EINVAL);
2441
2442 if (is_of_node(fwnode)) {
2443 enum of_gpio_flags flags;
2444
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002445 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002446 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002447 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02002448 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002449 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2450 }
Mika Westerberg40b73182014-10-21 13:33:59 +02002451 } else if (is_acpi_node(fwnode)) {
2452 struct acpi_gpio_info info;
2453
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02002454 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02002455 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01002456 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02002457 }
2458
2459 if (IS_ERR(desc))
2460 return desc;
2461
Mika Westerberg40b73182014-10-21 13:33:59 +02002462 if (active_low)
2463 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2464
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002465 if (single_ended) {
2466 if (active_low)
2467 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2468 else
2469 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2470 }
2471
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002472 ret = gpiod_request(desc, NULL);
2473 if (ret)
2474 return ERR_PTR(ret);
2475
Mika Westerberg40b73182014-10-21 13:33:59 +02002476 return desc;
2477}
2478EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2479
2480/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002481 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2482 * function
2483 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2484 * @con_id: function within the GPIO consumer
2485 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002486 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002487 *
2488 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2489 * specified index was assigned to the requested function it will return NULL.
2490 * This is convenient for drivers that need to handle optional GPIOs.
2491 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002492struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002493 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002494 unsigned int index,
2495 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002496{
2497 struct gpio_desc *desc;
2498
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002499 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002500 if (IS_ERR(desc)) {
2501 if (PTR_ERR(desc) == -ENOENT)
2502 return NULL;
2503 }
2504
2505 return desc;
2506}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002507EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002508
2509/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002510 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2511 * @desc: gpio whose value will be assigned
2512 * @name: gpio line name
2513 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2514 * of_get_gpio_hog()
2515 * @dflags: gpiod_flags - optional GPIO initialization flags
2516 */
2517int gpiod_hog(struct gpio_desc *desc, const char *name,
2518 unsigned long lflags, enum gpiod_flags dflags)
2519{
2520 struct gpio_chip *chip;
2521 struct gpio_desc *local_desc;
2522 int hwnum;
2523 int status;
2524
2525 chip = gpiod_to_chip(desc);
2526 hwnum = gpio_chip_hwgpio(desc);
2527
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002528 gpiod_parse_flags(desc, lflags);
2529
Benoit Parrotf625d462015-02-02 11:44:44 -06002530 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2531 if (IS_ERR(local_desc)) {
Linus Walleija7138902015-06-05 11:36:10 +02002532 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2533 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002534 return PTR_ERR(local_desc);
2535 }
2536
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002537 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002538 if (status < 0) {
Linus Walleija7138902015-06-05 11:36:10 +02002539 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2540 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002541 gpiochip_free_own_desc(desc);
2542 return status;
2543 }
2544
2545 /* Mark GPIO as hogged so it can be identified and removed later */
2546 set_bit(FLAG_IS_HOGGED, &desc->flags);
2547
2548 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2549 desc_to_gpio(desc), name,
2550 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2551 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2552 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2553
2554 return 0;
2555}
2556
2557/**
2558 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2559 * @chip: gpio chip to act on
2560 *
2561 * This is only used by of_gpiochip_remove to free hogged gpios
2562 */
2563static void gpiochip_free_hogs(struct gpio_chip *chip)
2564{
2565 int id;
2566
2567 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002568 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
2569 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06002570 }
2571}
2572
2573/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002574 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2575 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2576 * @con_id: function within the GPIO consumer
2577 * @flags: optional GPIO initialization flags
2578 *
2579 * This function acquires all the GPIOs defined under a given function.
2580 *
2581 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2582 * no GPIO has been assigned to the requested function, or another IS_ERR()
2583 * code if an error occurred while trying to acquire the GPIOs.
2584 */
2585struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2586 const char *con_id,
2587 enum gpiod_flags flags)
2588{
2589 struct gpio_desc *desc;
2590 struct gpio_descs *descs;
2591 int count;
2592
2593 count = gpiod_count(dev, con_id);
2594 if (count < 0)
2595 return ERR_PTR(count);
2596
2597 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2598 GFP_KERNEL);
2599 if (!descs)
2600 return ERR_PTR(-ENOMEM);
2601
2602 for (descs->ndescs = 0; descs->ndescs < count; ) {
2603 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2604 if (IS_ERR(desc)) {
2605 gpiod_put_array(descs);
2606 return ERR_CAST(desc);
2607 }
2608 descs->desc[descs->ndescs] = desc;
2609 descs->ndescs++;
2610 }
2611 return descs;
2612}
2613EXPORT_SYMBOL_GPL(gpiod_get_array);
2614
2615/**
2616 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2617 * function
2618 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2619 * @con_id: function within the GPIO consumer
2620 * @flags: optional GPIO initialization flags
2621 *
2622 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2623 * assigned to the requested function it will return NULL.
2624 */
2625struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2626 const char *con_id,
2627 enum gpiod_flags flags)
2628{
2629 struct gpio_descs *descs;
2630
2631 descs = gpiod_get_array(dev, con_id, flags);
2632 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2633 return NULL;
2634
2635 return descs;
2636}
2637EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2638
2639/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002640 * gpiod_put - dispose of a GPIO descriptor
2641 * @desc: GPIO descriptor to dispose of
2642 *
2643 * No descriptor can be used after gpiod_put() has been called on it.
2644 */
2645void gpiod_put(struct gpio_desc *desc)
2646{
2647 gpiod_free(desc);
2648}
2649EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002650
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002651/**
2652 * gpiod_put_array - dispose of multiple GPIO descriptors
2653 * @descs: struct gpio_descs containing an array of descriptors
2654 */
2655void gpiod_put_array(struct gpio_descs *descs)
2656{
2657 unsigned int i;
2658
2659 for (i = 0; i < descs->ndescs; i++)
2660 gpiod_put(descs->desc[i]);
2661
2662 kfree(descs);
2663}
2664EXPORT_SYMBOL_GPL(gpiod_put_array);
2665
Linus Walleij3c702e92015-10-21 15:29:53 +02002666static int __init gpiolib_dev_init(void)
2667{
2668 int ret;
2669
2670 /* Register GPIO sysfs bus */
2671 ret = bus_register(&gpio_bus_type);
2672 if (ret < 0) {
2673 pr_err("gpiolib: could not register GPIO bus type\n");
2674 return ret;
2675 }
2676
2677 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
2678 if (ret < 0) {
2679 pr_err("gpiolib: failed to allocate char dev region\n");
2680 bus_unregister(&gpio_bus_type);
2681 }
2682 return ret;
2683}
2684core_initcall(gpiolib_dev_init);
2685
David Brownelld2876d02008-02-04 22:28:20 -08002686#ifdef CONFIG_DEBUG_FS
2687
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002688static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08002689{
2690 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002691 struct gpio_chip *chip = gdev->chip;
2692 unsigned gpio = gdev->base;
2693 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08002694 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002695 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002696
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002697 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02002698 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2699 if (gdesc->name) {
2700 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2701 gpio, gdesc->name);
2702 }
David Brownelld2876d02008-02-04 22:28:20 -08002703 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02002704 }
David Brownelld2876d02008-02-04 22:28:20 -08002705
Alexandre Courbot372e7222013-02-03 01:29:29 +09002706 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002707 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002708 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02002709 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2710 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08002711 is_out ? "out" : "in ",
2712 chip->get
2713 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002714 : "? ",
2715 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002716 seq_printf(s, "\n");
2717 }
2718}
2719
Thierry Redingf9c4a312012-04-12 13:26:01 +02002720static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002721{
Grant Likely362432a2013-02-09 09:41:49 +00002722 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002723 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002724 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002725
2726 s->private = "";
2727
Grant Likely362432a2013-02-09 09:41:49 +00002728 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002729 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00002730 if (index-- == 0) {
2731 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002732 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00002733 }
2734 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002735
2736 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002737}
2738
2739static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2740{
Grant Likely362432a2013-02-09 09:41:49 +00002741 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002742 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002743 void *ret = NULL;
2744
Grant Likely362432a2013-02-09 09:41:49 +00002745 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002746 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002747 ret = NULL;
2748 else
Linus Walleijff2b1352015-10-20 11:10:38 +02002749 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00002750 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002751
2752 s->private = "\n";
2753 ++*pos;
2754
2755 return ret;
2756}
2757
2758static void gpiolib_seq_stop(struct seq_file *s, void *v)
2759{
2760}
2761
2762static int gpiolib_seq_show(struct seq_file *s, void *v)
2763{
Linus Walleijff2b1352015-10-20 11:10:38 +02002764 struct gpio_device *gdev = v;
2765 struct gpio_chip *chip = gdev->chip;
2766 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002767
Linus Walleijff2b1352015-10-20 11:10:38 +02002768 if (!chip) {
2769 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
2770 dev_name(&gdev->dev));
2771 return 0;
2772 }
2773
2774 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
2775 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002776 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02002777 parent = chip->parent;
2778 if (parent)
2779 seq_printf(s, ", parent: %s/%s",
2780 parent->bus ? parent->bus->name : "no-bus",
2781 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02002782 if (chip->label)
2783 seq_printf(s, ", %s", chip->label);
2784 if (chip->can_sleep)
2785 seq_printf(s, ", can sleep");
2786 seq_printf(s, ":\n");
2787
2788 if (chip->dbg_show)
2789 chip->dbg_show(s, chip);
2790 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002791 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002792
David Brownelld2876d02008-02-04 22:28:20 -08002793 return 0;
2794}
2795
Thierry Redingf9c4a312012-04-12 13:26:01 +02002796static const struct seq_operations gpiolib_seq_ops = {
2797 .start = gpiolib_seq_start,
2798 .next = gpiolib_seq_next,
2799 .stop = gpiolib_seq_stop,
2800 .show = gpiolib_seq_show,
2801};
2802
David Brownelld2876d02008-02-04 22:28:20 -08002803static int gpiolib_open(struct inode *inode, struct file *file)
2804{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002805 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002806}
2807
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002808static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002809 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002810 .open = gpiolib_open,
2811 .read = seq_read,
2812 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002813 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002814};
2815
2816static int __init gpiolib_debugfs_init(void)
2817{
2818 /* /sys/kernel/debug/gpio */
2819 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2820 NULL, NULL, &gpiolib_operations);
2821 return 0;
2822}
2823subsys_initcall(gpiolib_debugfs_init);
2824
2825#endif /* DEBUG_FS */