blob: 797c790aa7506c42ea748e2c030b5f692f82f2e8 [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 Walleijdf4878e2016-02-12 14:48:23 +0100345 strncpy(chipinfo.label, gdev->label,
346 sizeof(chipinfo.label));
347 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100348 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200349 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
350 return -EFAULT;
351 return 0;
352 }
353 return -EINVAL;
354}
355
356/**
357 * gpio_chrdev_open() - open the chardev for ioctl operations
358 * @inode: inode for this chardev
359 * @filp: file struct for storing private data
360 * Returns 0 on success
361 */
362static int gpio_chrdev_open(struct inode *inode, struct file *filp)
363{
364 struct gpio_device *gdev = container_of(inode->i_cdev,
365 struct gpio_device, chrdev);
366
367 /* Fail on open if the backing gpiochip is gone */
368 if (!gdev || !gdev->chip)
369 return -ENODEV;
370 get_device(&gdev->dev);
371 filp->private_data = gdev;
372 return 0;
373}
374
375/**
376 * gpio_chrdev_release() - close chardev after ioctl operations
377 * @inode: inode for this chardev
378 * @filp: file struct for storing private data
379 * Returns 0 on success
380 */
381static int gpio_chrdev_release(struct inode *inode, struct file *filp)
382{
383 struct gpio_device *gdev = container_of(inode->i_cdev,
384 struct gpio_device, chrdev);
385
386 if (!gdev)
387 return -ENODEV;
388 put_device(&gdev->dev);
389 return 0;
390}
391
392
393static const struct file_operations gpio_fileops = {
394 .release = gpio_chrdev_release,
395 .open = gpio_chrdev_open,
396 .owner = THIS_MODULE,
397 .llseek = noop_llseek,
398 .unlocked_ioctl = gpio_ioctl,
399 .compat_ioctl = gpio_ioctl,
400};
401
Linus Walleijff2b1352015-10-20 11:10:38 +0200402static void gpiodevice_release(struct device *dev)
403{
404 struct gpio_device *gdev = dev_get_drvdata(dev);
405
Linus Walleij3c702e92015-10-21 15:29:53 +0200406 cdev_del(&gdev->chrdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200407 list_del(&gdev->list);
408 ida_simple_remove(&gpio_ida, gdev->id);
Linus Walleij9efd9e62016-02-09 14:27:42 +0100409 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200410}
411
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700412/**
Linus Walleijb08ea352015-12-03 15:14:13 +0100413 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -0800414 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900415 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -0800416 *
417 * Returns a negative errno if the chip can't be registered, such as
418 * because the chip->base is invalid or already associated with a
419 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700420 *
Linus Walleijb08ea352015-12-03 15:14:13 +0100421 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800422 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d2008-07-25 01:46:07 -0700423 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
424 * for GPIOs will fail rudely.
425 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700426 * If chip->base is negative, this requests dynamic assignment of
427 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800428 */
Linus Walleijb08ea352015-12-03 15:14:13 +0100429int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -0800430{
431 unsigned long flags;
432 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +0200433 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700434 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +0200435 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -0800436
Linus Walleijff2b1352015-10-20 11:10:38 +0200437 /*
438 * First: allocate and populate the internal stat container, and
439 * set up the struct device.
440 */
Josh Cartwright969f07b2016-02-17 16:44:15 -0600441 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +0200442 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900443 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +0200444 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +0200445 gdev->chip = chip;
446 chip->gpiodev = gdev;
447 if (chip->parent) {
448 gdev->dev.parent = chip->parent;
449 gdev->dev.of_node = chip->parent->of_node;
450 } else {
451#ifdef CONFIG_OF_GPIO
452 /* If the gpiochip has an assigned OF node this takes precedence */
453 if (chip->of_node)
454 gdev->dev.of_node = chip->of_node;
455#endif
456 }
457 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
458 if (gdev->id < 0) {
459 status = gdev->id;
460 goto err_free_gdev;
461 }
462 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
463 device_initialize(&gdev->dev);
464 dev_set_drvdata(&gdev->dev, gdev);
465 if (chip->parent && chip->parent->driver)
466 gdev->owner = chip->parent->driver->owner;
467 else if (chip->owner)
468 /* TODO: remove chip->owner */
469 gdev->owner = chip->owner;
470 else
471 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -0800472
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100473 gdev->descs = devm_kcalloc(&gdev->dev, chip->ngpio,
474 sizeof(gdev->descs[0]), GFP_KERNEL);
475 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +0200476 status = -ENOMEM;
477 goto err_free_gdev;
478 }
479
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800480 if (chip->ngpio == 0) {
481 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +0200482 status = -EINVAL;
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100483 goto err_free_gdev;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800484 }
Linus Walleijdf4878e2016-02-12 14:48:23 +0100485
486 if (chip->label)
487 gdev->label = devm_kstrdup(&gdev->dev, chip->label, GFP_KERNEL);
488 else
489 gdev->label = devm_kstrdup(&gdev->dev, "unknown", GFP_KERNEL);
490 if (!gdev->label) {
491 status = -ENOMEM;
492 goto err_free_gdev;
493 }
494
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100495 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +0100496 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800497
David Brownelld2876d02008-02-04 22:28:20 -0800498 spin_lock_irqsave(&gpio_lock, flags);
499
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100500 /*
501 * TODO: this allocates a Linux GPIO number base in the global
502 * GPIO numberspace for this chip. In the long run we want to
503 * get *rid* of this numberspace and use only descriptors, but
504 * it may be a pipe dream. It will not happen before we get rid
505 * of the sysfs interface anyways.
506 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700507 if (base < 0) {
508 base = gpiochip_find_base(chip->ngpio);
509 if (base < 0) {
510 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100511 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100512 goto err_free_gdev;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700513 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100514 /*
515 * TODO: it should not be necessary to reflect the assigned
516 * base outside of the GPIO subsystem. Go over drivers and
517 * see if anyone makes use of this, else drop this and assign
518 * a poison instead.
519 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700520 chip->base = base;
521 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100522 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700523
Linus Walleijff2b1352015-10-20 11:10:38 +0200524 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +0100525 if (status) {
526 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100527 goto err_free_gdev;
Johan Hovold05aa5202015-01-12 17:12:26 +0100528 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900529
Linus Walleijff2b1352015-10-20 11:10:38 +0200530 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100531 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d2008-07-25 01:46:07 -0700532
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100533 desc->gdev = gdev;
Johan Hovold05aa5202015-01-12 17:12:26 +0100534
535 /* REVISIT: most hardware initializes GPIOs as inputs (often
536 * with pullups enabled) so power usage is minimized. Linux
537 * code should set the gpio direction first thing; but until
538 * it does, and in case chip->get_direction is not set, we may
539 * expose the wrong direction in sysfs.
540 */
541 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800542 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900543
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800544 spin_unlock_irqrestore(&gpio_lock, flags);
545
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530546#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +0100547 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530548#endif
549
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200550 status = gpiochip_set_desc_names(chip);
551 if (status)
552 goto err_remove_from_list;
553
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200554 status = of_gpiochip_add(chip);
555 if (status)
556 goto err_remove_chip;
557
Mika Westerberg664e3e52014-01-08 12:40:54 +0200558 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600559
Linus Walleij3c702e92015-10-21 15:29:53 +0200560 /*
561 * By first adding the chardev, and then adding the device,
562 * we get a device node entry in sysfs under
563 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
564 * coldplug of device nodes and other udev business.
565 */
566 cdev_init(&gdev->chrdev, &gpio_fileops);
567 gdev->chrdev.owner = THIS_MODULE;
568 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
569 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
570 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
571 if (status < 0)
572 chip_warn(chip, "failed to add char device %d:%d\n",
573 MAJOR(gpio_devt), gdev->id);
574 else
575 chip_dbg(chip, "added GPIO chardev (%d:%d)\n",
576 MAJOR(gpio_devt), gdev->id);
Linus Walleijff2b1352015-10-20 11:10:38 +0200577 status = device_add(&gdev->dev);
Johan Hovold225fce82015-01-12 17:12:25 +0100578 if (status)
Linus Walleij3c702e92015-10-21 15:29:53 +0200579 goto err_remove_chardev;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600580
Linus Walleijafbc4f32016-02-09 13:21:06 +0100581 status = gpiochip_sysfs_register(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200582 if (status)
583 goto err_remove_device;
584
585 /* From this point, the .release() function cleans up gpio_device */
586 gdev->dev.release = gpiodevice_release;
587 get_device(&gdev->dev);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100588 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
589 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
590 dev_name(&gdev->dev), chip->label ? : "generic");
Grant Likely64842aa2011-11-06 11:36:18 -0700591
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600592 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800593
Linus Walleijff2b1352015-10-20 11:10:38 +0200594err_remove_device:
595 device_del(&gdev->dev);
Linus Walleij3c702e92015-10-21 15:29:53 +0200596err_remove_chardev:
597 cdev_del(&gdev->chrdev);
Johan Hovold225fce82015-01-12 17:12:25 +0100598err_remove_chip:
599 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200600 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100601 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200602err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100603 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200604 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800605 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200606err_free_gdev:
607 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -0800608 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200609 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100610 gdev->base, gdev->base + gdev->ngpio - 1,
611 chip->label ? : "generic");
612 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -0800613 return status;
614}
Linus Walleijb08ea352015-12-03 15:14:13 +0100615EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -0800616
617/**
Linus Walleij43c54ec2016-02-11 11:37:48 +0100618 * gpiochip_get_data() - get per-subdriver data for the chip
619 */
620void *gpiochip_get_data(struct gpio_chip *chip)
621{
622 return chip->gpiodev->data;
623}
624EXPORT_SYMBOL_GPL(gpiochip_get_data);
625
626/**
David Brownelld2876d02008-02-04 22:28:20 -0800627 * gpiochip_remove() - unregister a gpio_chip
628 * @chip: the chip to unregister
629 *
630 * A gpio_chip with any GPIOs still requested may not be removed.
631 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200632void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800633{
Linus Walleijff2b1352015-10-20 11:10:38 +0200634 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200635 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800636 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100637 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200638 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800639
Linus Walleijff2b1352015-10-20 11:10:38 +0200640 /* Numb the device, cancelling all outstanding operations */
641 gdev->chip = NULL;
642
643 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +0100644 gpiochip_sysfs_unregister(gdev);
Johan Hovold00acc3d2015-01-12 17:12:27 +0100645 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200646 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100647 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600648 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600649 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +0100650 /*
651 * We accept no more calls into the driver from this point, so
652 * NULL the driver data pointer
653 */
654 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -0600655
Johan Hovold6798aca2015-01-12 17:12:28 +0100656 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100657 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100658 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +0200659 if (test_bit(FLAG_REQUESTED, &desc->flags))
660 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800661 }
David Brownelld2876d02008-02-04 22:28:20 -0800662 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900663
Johan Hovoldfab28b82015-05-04 17:10:27 +0200664 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100665 dev_crit(&gdev->dev,
Linus Walleij58383c72015-11-04 09:56:26 +0100666 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +0200667
Linus Walleijff2b1352015-10-20 11:10:38 +0200668 /*
669 * The gpiochip side puts its use of the device to rest here:
670 * if there are no userspace clients, the chardev and device will
671 * be removed, else it will be dangling until the last user is
672 * gone.
673 */
674 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -0800675}
676EXPORT_SYMBOL_GPL(gpiochip_remove);
677
Grant Likely594fa262010-06-08 07:48:16 -0600678/**
679 * gpiochip_find() - iterator for locating a specific gpio_chip
680 * @data: data to pass to match function
681 * @callback: Callback function to check gpio_chip
682 *
683 * Similar to bus_find_device. It returns a reference to a gpio_chip as
684 * determined by a user supplied @match callback. The callback should return
685 * 0 if the device doesn't match and non-zero if it does. If the callback is
686 * non-zero, this function will return to the caller and not iterate over any
687 * more gpio_chips.
688 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600689struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700690 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600691 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600692{
Linus Walleijff2b1352015-10-20 11:10:38 +0200693 struct gpio_device *gdev;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900694 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600695 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600696
697 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200698 list_for_each_entry(gdev, &gpio_devices, list)
699 if (match(gdev->chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600700 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900701
702 /* No match? */
Linus Walleijff2b1352015-10-20 11:10:38 +0200703 if (&gdev->list == &gpio_devices)
Alexandre Courbot125eef92013-02-03 01:29:26 +0900704 chip = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +0200705 else
706 chip = gdev->chip;
707
Grant Likely594fa262010-06-08 07:48:16 -0600708 spin_unlock_irqrestore(&gpio_lock, flags);
709
710 return chip;
711}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600712EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800713
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900714static int gpiochip_match_name(struct gpio_chip *chip, void *data)
715{
716 const char *name = data;
717
718 return !strcmp(chip->label, name);
719}
720
721static struct gpio_chip *find_chip_by_name(const char *name)
722{
723 return gpiochip_find((void *)name, gpiochip_match_name);
724}
725
Linus Walleij14250522014-03-25 10:40:18 +0100726#ifdef CONFIG_GPIOLIB_IRQCHIP
727
728/*
729 * The following is irqchip helper code for gpiochips.
730 */
731
732/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200733 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
734 * @gpiochip: the gpiochip to set the irqchip chain to
735 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100736 * @parent_irq: the irq number corresponding to the parent IRQ for this
737 * chained irqchip
738 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200739 * coming out of the gpiochip. If the interrupt is nested rather than
740 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100741 */
742void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
743 struct irq_chip *irqchip,
744 int parent_irq,
745 irq_flow_handler_t parent_handler)
746{
Linus Walleij83141a72014-09-26 13:50:12 +0200747 unsigned int offset;
748
Linus Walleij83141a72014-09-26 13:50:12 +0200749 if (!gpiochip->irqdomain) {
750 chip_err(gpiochip, "called %s before setting up irqchip\n",
751 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200752 return;
753 }
754
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200755 if (parent_handler) {
756 if (gpiochip->can_sleep) {
757 chip_err(gpiochip,
758 "you cannot have chained interrupts on a "
759 "chip that may sleep\n");
760 return;
761 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200762 /*
763 * The parent irqchip is already using the chip_data for this
764 * irqchip, so our callbacks simply use the handler_data.
765 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +0200766 irq_set_chained_handler_and_data(parent_irq, parent_handler,
767 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300768
769 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200770 }
Linus Walleij83141a72014-09-26 13:50:12 +0200771
772 /* Set the parent IRQ for all affected IRQs */
773 for (offset = 0; offset < gpiochip->ngpio; offset++)
774 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
775 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100776}
777EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
778
779/**
780 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
781 * @d: the irqdomain used by this irqchip
782 * @irq: the global irq number used by this GPIO irqchip irq
783 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
784 *
785 * This function will set up the mapping for a certain IRQ line on a
786 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
787 * stored inside the gpiochip.
788 */
789static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
790 irq_hw_number_t hwirq)
791{
792 struct gpio_chip *chip = d->host_data;
793
Linus Walleij14250522014-03-25 10:40:18 +0100794 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300795 /*
796 * This lock class tells lockdep that GPIO irqs are in a different
797 * category than their parents, so it won't report false recursion.
798 */
799 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +0200800 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200801 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300802 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200803 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100804 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -0500805
Linus Walleij1333b902014-04-23 16:45:12 +0200806 /*
807 * No set-up of the hardware will happen if IRQ_TYPE_NONE
808 * is passed as default type.
809 */
810 if (chip->irq_default_type != IRQ_TYPE_NONE)
811 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100812
813 return 0;
814}
815
Linus Walleijc3626fd2014-03-28 20:42:01 +0100816static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
817{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200818 struct gpio_chip *chip = d->host_data;
819
Linus Walleij1c8732b2014-04-09 13:34:39 +0200820 if (chip->can_sleep)
821 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100822 irq_set_chip_and_handler(irq, NULL, NULL);
823 irq_set_chip_data(irq, NULL);
824}
825
Linus Walleij14250522014-03-25 10:40:18 +0100826static const struct irq_domain_ops gpiochip_domain_ops = {
827 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100828 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100829 /* Virtually all GPIO irqchips are twocell:ed */
830 .xlate = irq_domain_xlate_twocell,
831};
832
833static int gpiochip_irq_reqres(struct irq_data *d)
834{
835 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
836
Linus Walleijff2b1352015-10-20 11:10:38 +0200837 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300838 return -ENODEV;
839
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900840 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100841 chip_err(chip,
842 "unable to lock HW IRQ %lu for IRQ\n",
843 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200844 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100845 return -EINVAL;
846 }
847 return 0;
848}
849
850static void gpiochip_irq_relres(struct irq_data *d)
851{
852 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
853
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900854 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200855 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100856}
857
858static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
859{
860 return irq_find_mapping(chip->irqdomain, offset);
861}
862
863/**
864 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
865 * @gpiochip: the gpiochip to remove the irqchip from
866 *
867 * This is called only from gpiochip_remove()
868 */
869static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
870{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100871 unsigned int offset;
872
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300873 acpi_gpiochip_free_interrupts(gpiochip);
874
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300875 if (gpiochip->irq_parent) {
876 irq_set_chained_handler(gpiochip->irq_parent, NULL);
877 irq_set_handler_data(gpiochip->irq_parent, NULL);
878 }
879
Linus Walleijc3626fd2014-03-28 20:42:01 +0100880 /* Remove all IRQ mappings and delete the domain */
881 if (gpiochip->irqdomain) {
882 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300883 irq_dispose_mapping(
884 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100885 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100886 }
Linus Walleij14250522014-03-25 10:40:18 +0100887
888 if (gpiochip->irqchip) {
889 gpiochip->irqchip->irq_request_resources = NULL;
890 gpiochip->irqchip->irq_release_resources = NULL;
891 gpiochip->irqchip = NULL;
892 }
893}
894
895/**
896 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
897 * @gpiochip: the gpiochip to add the irqchip to
898 * @irqchip: the irqchip to add to the gpiochip
899 * @first_irq: if not dynamically assigned, the base (first) IRQ to
900 * allocate gpiochip irqs from
901 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200902 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
903 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300904 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +0100905 *
906 * This function closely associates a certain irqchip with a certain
907 * gpiochip, providing an irq domain to translate the local IRQs to
908 * global irqs in the gpiolib core, and making sure that the gpiochip
909 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +0100910 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +0100911 * from the gpiochip passed as chip data. An irqdomain will be stored
912 * in the gpiochip that shall be used by the driver to handle IRQ number
913 * translation. The gpiochip will need to be initialized and registered
914 * before calling this function.
915 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100916 * This function will handle two cell:ed simple IRQs and assumes all
917 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100918 * need to be open coded.
919 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300920int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
921 struct irq_chip *irqchip,
922 unsigned int first_irq,
923 irq_flow_handler_t handler,
924 unsigned int type,
925 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +0100926{
927 struct device_node *of_node;
928 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100929 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100930
931 if (!gpiochip || !irqchip)
932 return -EINVAL;
933
Linus Walleij58383c72015-11-04 09:56:26 +0100934 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +0100935 pr_err("missing gpiochip .dev parent pointer\n");
936 return -EINVAL;
937 }
Linus Walleij58383c72015-11-04 09:56:26 +0100938 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +0100939#ifdef CONFIG_OF_GPIO
940 /*
Colin Cronin20a8a962015-05-18 11:41:43 -0700941 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800942 * FIXME: get rid of this and use gpiochip->parent->of_node
943 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +0100944 */
945 if (gpiochip->of_node)
946 of_node = gpiochip->of_node;
947#endif
948 gpiochip->irqchip = irqchip;
949 gpiochip->irq_handler = handler;
950 gpiochip->irq_default_type = type;
951 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300952 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +0100953 gpiochip->irqdomain = irq_domain_add_simple(of_node,
954 gpiochip->ngpio, first_irq,
955 &gpiochip_domain_ops, gpiochip);
956 if (!gpiochip->irqdomain) {
957 gpiochip->irqchip = NULL;
958 return -EINVAL;
959 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +0200960
961 /*
962 * It is possible for a driver to override this, but only if the
963 * alternative functions are both implemented.
964 */
965 if (!irqchip->irq_request_resources &&
966 !irqchip->irq_release_resources) {
967 irqchip->irq_request_resources = gpiochip_irq_reqres;
968 irqchip->irq_release_resources = gpiochip_irq_relres;
969 }
Linus Walleij14250522014-03-25 10:40:18 +0100970
971 /*
972 * Prepare the mapping since the irqchip shall be orthogonal to
973 * any gpiochip calls. If the first_irq was zero, this is
974 * necessary to allocate descriptors for all IRQs.
975 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100976 for (offset = 0; offset < gpiochip->ngpio; offset++) {
977 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
978 if (offset == 0)
979 /*
980 * Store the base into the gpiochip to be used when
981 * unmapping the irqs.
982 */
983 gpiochip->irq_base = irq_base;
984 }
Linus Walleij14250522014-03-25 10:40:18 +0100985
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300986 acpi_gpiochip_request_interrupts(gpiochip);
987
Linus Walleij14250522014-03-25 10:40:18 +0100988 return 0;
989}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300990EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +0100991
992#else /* CONFIG_GPIOLIB_IRQCHIP */
993
994static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
995
996#endif /* CONFIG_GPIOLIB_IRQCHIP */
997
Jonas Gorskic771c2f2015-10-11 17:34:15 +0200998/**
999 * gpiochip_generic_request() - request the gpio function for a pin
1000 * @chip: the gpiochip owning the GPIO
1001 * @offset: the offset of the GPIO to request for GPIO function
1002 */
1003int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1004{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001005 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001006}
1007EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1008
1009/**
1010 * gpiochip_generic_free() - free the gpio function from a pin
1011 * @chip: the gpiochip to request the gpio function for
1012 * @offset: the offset of the GPIO to free from GPIO function
1013 */
1014void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1015{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001016 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001017}
1018EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1019
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301020#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001021
Linus Walleij3f0f8672012-11-20 12:40:15 +01001022/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001023 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1024 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001025 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001026 * @gpio_offset: the start offset in the current gpio_chip number space
1027 * @pin_group: name of the pin group inside the pin controller
1028 */
1029int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1030 struct pinctrl_dev *pctldev,
1031 unsigned int gpio_offset, const char *pin_group)
1032{
1033 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001034 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001035 int ret;
1036
1037 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1038 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001039 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001040 return -ENOMEM;
1041 }
1042
1043 /* Use local offset as range ID */
1044 pin_range->range.id = gpio_offset;
1045 pin_range->range.gc = chip;
1046 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001047 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001048 pin_range->pctldev = pctldev;
1049
1050 ret = pinctrl_get_group_pins(pctldev, pin_group,
1051 &pin_range->range.pins,
1052 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001053 if (ret < 0) {
1054 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001055 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001056 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001057
1058 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1059
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001060 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1061 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001062 pinctrl_dev_get_devname(pctldev), pin_group);
1063
Linus Walleij20ec3e32016-02-11 11:03:06 +01001064 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001065
1066 return 0;
1067}
1068EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1069
1070/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001071 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1072 * @chip: the gpiochip to add the range for
1073 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001074 * @gpio_offset: the start offset in the current gpio_chip number space
1075 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001076 * @npins: the number of pins from the offset of each pin space (GPIO and
1077 * pin controller) to accumulate in this range
1078 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001079int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001080 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001081 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301082{
1083 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001084 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001085 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301086
Linus Walleij3f0f8672012-11-20 12:40:15 +01001087 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301088 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001089 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001090 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301091 }
1092
Linus Walleij3f0f8672012-11-20 12:40:15 +01001093 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001094 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001095 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301096 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001097 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001098 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301099 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001100 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301101 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001102 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001103 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001104 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001105 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001106 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001107 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001108 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1109 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001110 pinctl_name,
1111 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301112
Linus Walleij20ec3e32016-02-11 11:03:06 +01001113 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001114
1115 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301116}
Linus Walleij165adc92012-11-06 14:49:39 +01001117EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301118
Linus Walleij3f0f8672012-11-20 12:40:15 +01001119/**
1120 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1121 * @chip: the chip to remove all the mappings for
1122 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301123void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1124{
1125 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01001126 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301127
Linus Walleij20ec3e32016-02-11 11:03:06 +01001128 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301129 list_del(&pin_range->node);
1130 pinctrl_remove_gpio_range(pin_range->pctldev,
1131 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001132 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301133 }
1134}
Linus Walleij165adc92012-11-06 14:49:39 +01001135EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1136
1137#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301138
David Brownelld2876d02008-02-04 22:28:20 -08001139/* These "optional" allocation calls help prevent drivers from stomping
1140 * on each other, and help provide better diagnostics in debugfs.
1141 * They're called even less than the "set direction" calls.
1142 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001143static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001144{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001145 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001146 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001147 unsigned long flags;
1148
1149 spin_lock_irqsave(&gpio_lock, flags);
1150
David Brownelld2876d02008-02-04 22:28:20 -08001151 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001152 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001153 */
1154
1155 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1156 desc_set_label(desc, label ? : "?");
1157 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001158 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001159 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001160 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001161 }
1162
1163 if (chip->request) {
1164 /* chip->request may sleep */
1165 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001166 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001167 spin_lock_irqsave(&gpio_lock, flags);
1168
1169 if (status < 0) {
1170 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001171 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001172 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001173 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001174 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001175 if (chip->get_direction) {
1176 /* chip->get_direction may sleep */
1177 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001178 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001179 spin_lock_irqsave(&gpio_lock, flags);
1180 }
David Brownelld2876d02008-02-04 22:28:20 -08001181done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +03001182 if (status < 0) {
1183 /* Clear flags that might have been set by the caller before
1184 * requesting the GPIO.
1185 */
1186 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1187 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1188 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1189 }
Mika Westerberg77c2d792014-03-10 14:54:50 +02001190 spin_unlock_irqrestore(&gpio_lock, flags);
1191 return status;
1192}
1193
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001194/*
1195 * This descriptor validation needs to be inserted verbatim into each
1196 * function taking a descriptor, so we need to use a preprocessor
1197 * macro to avoid endless duplication.
1198 */
1199#define VALIDATE_DESC(desc) do { \
1200 if (!desc || !desc->gdev) { \
1201 pr_warn("%s: invalid GPIO\n", __func__); \
1202 return -EINVAL; \
1203 } \
1204 if ( !desc->gdev->chip ) { \
1205 dev_warn(&desc->gdev->dev, \
1206 "%s: backing chip is gone\n", __func__); \
1207 return 0; \
1208 } } while (0)
1209
1210#define VALIDATE_DESC_VOID(desc) do { \
1211 if (!desc || !desc->gdev) { \
1212 pr_warn("%s: invalid GPIO\n", __func__); \
1213 return; \
1214 } \
1215 if (!desc->gdev->chip) { \
1216 dev_warn(&desc->gdev->dev, \
1217 "%s: backing chip is gone\n", __func__); \
1218 return; \
1219 } } while (0)
1220
1221
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001222int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001223{
1224 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001225 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001226
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001227 VALIDATE_DESC(desc);
1228 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001229
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001230 if (try_module_get(gdev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001231 status = __gpiod_request(desc, label);
1232 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001233 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001234 else
1235 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001236 }
1237
David Brownelld2876d02008-02-04 22:28:20 -08001238 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001239 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001240
David Brownelld2876d02008-02-04 22:28:20 -08001241 return status;
1242}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001243
Mika Westerberg77c2d792014-03-10 14:54:50 +02001244static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001245{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001246 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001247 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001248 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001249
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001250 might_sleep();
1251
Alexandre Courbot372e7222013-02-03 01:29:29 +09001252 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001253
David Brownelld2876d02008-02-04 22:28:20 -08001254 spin_lock_irqsave(&gpio_lock, flags);
1255
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001256 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07001257 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1258 if (chip->free) {
1259 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001260 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001261 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001262 spin_lock_irqsave(&gpio_lock, flags);
1263 }
David Brownelld2876d02008-02-04 22:28:20 -08001264 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001265 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001266 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301267 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301268 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001269 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001270 ret = true;
1271 }
David Brownelld2876d02008-02-04 22:28:20 -08001272
1273 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001274 return ret;
1275}
1276
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001277void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001278{
Linus Walleij33a68e82016-02-11 10:28:44 +01001279 if (desc && desc->gdev && __gpiod_free(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001280 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001281 put_device(&desc->gdev->dev);
1282 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001283 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01001284 }
David Brownelld2876d02008-02-04 22:28:20 -08001285}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001286
David Brownelld2876d02008-02-04 22:28:20 -08001287/**
1288 * gpiochip_is_requested - return string iff signal was requested
1289 * @chip: controller managing the signal
1290 * @offset: of signal within controller's 0..(ngpio - 1) range
1291 *
1292 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001293 * The string returned is the label passed to gpio_request(); if none has been
1294 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001295 *
1296 * This function is for use by GPIO controller drivers. The label can
1297 * help with diagnostics, and knowing that the signal is used as a GPIO
1298 * can help avoid accidentally multiplexing it to another controller.
1299 */
1300const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1301{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001302 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001303
Dirk Behme48b59532015-08-18 18:02:32 +02001304 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001305 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001306
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001307 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001308
Alexandre Courbot372e7222013-02-03 01:29:29 +09001309 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001310 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001311 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001312}
1313EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1314
Mika Westerberg77c2d792014-03-10 14:54:50 +02001315/**
1316 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1317 * @desc: GPIO descriptor to request
1318 * @label: label for the GPIO
1319 *
1320 * Function allows GPIO chip drivers to request and use their own GPIO
1321 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1322 * function will not increase reference count of the GPIO chip module. This
1323 * allows the GPIO chip module to be unloaded as needed (we assume that the
1324 * GPIO chip driver handles freeing the GPIOs it has requested).
1325 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001326struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1327 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001328{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001329 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1330 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001331
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001332 if (IS_ERR(desc)) {
1333 chip_err(chip, "failed to get GPIO descriptor\n");
1334 return desc;
1335 }
1336
1337 err = __gpiod_request(desc, label);
1338 if (err < 0)
1339 return ERR_PTR(err);
1340
1341 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001342}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001343EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001344
1345/**
1346 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1347 * @desc: GPIO descriptor to free
1348 *
1349 * Function frees the given GPIO requested previously with
1350 * gpiochip_request_own_desc().
1351 */
1352void gpiochip_free_own_desc(struct gpio_desc *desc)
1353{
1354 if (desc)
1355 __gpiod_free(desc);
1356}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001357EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08001358
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001359/*
1360 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08001361 * some cases this is done in early boot, before IRQs are enabled.
1362 *
1363 * As a rule these aren't called more than once (except for drivers
1364 * using the open-drain emulation idiom) so these are natural places
1365 * to accumulate extra debugging checks. Note that we can't (yet)
1366 * rely on gpio_request() having been called beforehand.
1367 */
1368
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001369/**
1370 * gpiod_direction_input - set the GPIO direction to input
1371 * @desc: GPIO to set to input
1372 *
1373 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1374 * be called safely on it.
1375 *
1376 * Return 0 in case of success, else an error code.
1377 */
1378int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001379{
David Brownelld2876d02008-02-04 22:28:20 -08001380 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001381 int status = -EINVAL;
1382
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001383 VALIDATE_DESC(desc);
1384 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001385
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001386 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001387 gpiod_warn(desc,
1388 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001389 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001390 return -EIO;
1391 }
1392
Alexandre Courbotd82da792014-07-22 16:17:43 +09001393 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001394 if (status == 0)
1395 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001396
Alexandre Courbot372e7222013-02-03 01:29:29 +09001397 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001398
David Brownelld2876d02008-02-04 22:28:20 -08001399 return status;
1400}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001401EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001402
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001403static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001404{
David Brownelld2876d02008-02-04 22:28:20 -08001405 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001406 int status = -EINVAL;
1407
Linus Walleijd468bf92013-09-24 11:54:38 +02001408 /* GPIOs used for IRQs shall not be set as output */
1409 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1410 gpiod_err(desc,
1411 "%s: tried to set a GPIO tied to an IRQ as output\n",
1412 __func__);
1413 return -EIO;
1414 }
1415
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301416 /* Open drain pin should not be driven to 1 */
1417 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001418 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301419
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301420 /* Open source pin should not be driven to 0 */
1421 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001422 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301423
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001424 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001425 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001426 gpiod_warn(desc,
1427 "%s: missing set() or direction_output() operations\n",
1428 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001429 return -EIO;
1430 }
1431
Alexandre Courbotd82da792014-07-22 16:17:43 +09001432 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001433 if (status == 0)
1434 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001435 trace_gpio_value(desc_to_gpio(desc), 0, value);
1436 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001437 return status;
1438}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001439
1440/**
1441 * gpiod_direction_output_raw - set the GPIO direction to output
1442 * @desc: GPIO to set to output
1443 * @value: initial output value of the GPIO
1444 *
1445 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1446 * be called safely on it. The initial value of the output must be specified
1447 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1448 *
1449 * Return 0 in case of success, else an error code.
1450 */
1451int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1452{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001453 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001454 return _gpiod_direction_output_raw(desc, value);
1455}
1456EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1457
1458/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301459 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001460 * @desc: GPIO to set to output
1461 * @value: initial output value of the GPIO
1462 *
1463 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1464 * be called safely on it. The initial value of the output must be specified
1465 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1466 * account.
1467 *
1468 * Return 0 in case of success, else an error code.
1469 */
1470int gpiod_direction_output(struct gpio_desc *desc, int value)
1471{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001472 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001473 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1474 value = !value;
1475 return _gpiod_direction_output_raw(desc, value);
1476}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001477EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001478
Felipe Balbic4b5be92010-05-26 14:42:23 -07001479/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001480 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001481 * @gpio: the gpio to set debounce time
1482 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001483 *
1484 * returns -ENOTSUPP if the controller does not support setting
1485 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001486 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001487int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001488{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001489 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001490
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001491 VALIDATE_DESC(desc);
1492 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001493 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001494 gpiod_dbg(desc,
1495 "%s: missing set() or set_debounce() operations\n",
1496 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001497 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001498 }
1499
Alexandre Courbotd82da792014-07-22 16:17:43 +09001500 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001501}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001502EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001503
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001504/**
1505 * gpiod_is_active_low - test whether a GPIO is active-low or not
1506 * @desc: the gpio descriptor to test
1507 *
1508 * Returns 1 if the GPIO is active-low, 0 otherwise.
1509 */
1510int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001511{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001512 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001513 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001514}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001515EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001516
1517/* I/O calls are only valid after configuration completed; the relevant
1518 * "is this a valid GPIO" error checks should already have been done.
1519 *
1520 * "Get" operations are often inlinable as reading a pin value register,
1521 * and masking the relevant bit in that register.
1522 *
1523 * When "set" operations are inlinable, they involve writing that mask to
1524 * one register to set a low value, or a different register to set it high.
1525 * Otherwise locking is needed, so there may be little value to inlining.
1526 *
1527 *------------------------------------------------------------------------
1528 *
1529 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1530 * have requested the GPIO. That can include implicit requesting by
1531 * a direction setting call. Marking a gpio as requested locks its chip
1532 * in memory, guaranteeing that these table lookups need no more locking
1533 * and that gpiochip_remove() will fail.
1534 *
1535 * REVISIT when debugging, consider adding some instrumentation to ensure
1536 * that the GPIO was actually requested.
1537 */
1538
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001539static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001540{
1541 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001542 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001543 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001544
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001545 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001546 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001547 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01001548 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001549 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001550 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001551}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001552
David Brownelld2876d02008-02-04 22:28:20 -08001553/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001554 * gpiod_get_raw_value() - return a gpio's raw value
1555 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001556 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001557 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001558 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001559 *
1560 * This function should be called from contexts where we cannot sleep, and will
1561 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001562 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001563int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001564{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001565 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001566 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001567 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001568 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001569}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001570EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001571
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001572/**
1573 * gpiod_get_value() - return a gpio's value
1574 * @desc: gpio whose value will be returned
1575 *
1576 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001577 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001578 *
1579 * This function should be called from contexts where we cannot sleep, and will
1580 * complain if the GPIO chip functions potentially sleep.
1581 */
1582int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001583{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001584 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001585
1586 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001587 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001588 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001589
1590 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001591 if (value < 0)
1592 return value;
1593
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001594 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1595 value = !value;
1596
1597 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001598}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001599EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001600
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301601/*
1602 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001603 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001604 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301605 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001606static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301607{
1608 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001609 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001610 int offset = gpio_chip_hwgpio(desc);
1611
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301612 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001613 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301614 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001615 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301616 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001617 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301618 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001619 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301620 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001621 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301622 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001623 gpiod_err(desc,
1624 "%s: Error in set_value for open drain err %d\n",
1625 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301626}
1627
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301628/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001629 * _gpio_set_open_source_value() - Set the open source gpio's value.
1630 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001631 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301632 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001633static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301634{
1635 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001636 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001637 int offset = gpio_chip_hwgpio(desc);
1638
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301639 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001640 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301641 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001642 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301643 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001644 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301645 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001646 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301647 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001648 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301649 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001650 gpiod_err(desc,
1651 "%s: Error in set_value for open source err %d\n",
1652 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301653}
1654
Alexandre Courbot23600962014-03-11 15:52:09 +09001655static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001656{
1657 struct gpio_chip *chip;
1658
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001659 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001660 trace_gpio_value(desc_to_gpio(desc), 0, value);
1661 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1662 _gpio_set_open_drain_value(desc, value);
1663 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1664 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301665 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001666 chip->set(chip, gpio_chip_hwgpio(desc), value);
1667}
1668
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001669/*
1670 * set multiple outputs on the same chip;
1671 * use the chip's set_multiple function if available;
1672 * otherwise set the outputs sequentially;
1673 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1674 * defines which outputs are to be changed
1675 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1676 * defines the values the outputs specified by mask are to be set to
1677 */
1678static void gpio_chip_set_multiple(struct gpio_chip *chip,
1679 unsigned long *mask, unsigned long *bits)
1680{
1681 if (chip->set_multiple) {
1682 chip->set_multiple(chip, mask, bits);
1683 } else {
1684 int i;
1685 for (i = 0; i < chip->ngpio; i++) {
1686 if (mask[BIT_WORD(i)] == 0) {
1687 /* no more set bits in this mask word;
1688 * skip ahead to the next word */
1689 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1690 continue;
1691 }
1692 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001693 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001694 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001695 }
1696 }
1697}
1698
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001699static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1700 unsigned int array_size,
1701 struct gpio_desc **desc_array,
1702 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001703{
1704 int i = 0;
1705
1706 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001707 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001708 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1709 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1710 int count = 0;
1711
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001712 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001713 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001714
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001715 memset(mask, 0, sizeof(mask));
1716 do {
1717 struct gpio_desc *desc = desc_array[i];
1718 int hwgpio = gpio_chip_hwgpio(desc);
1719 int value = value_array[i];
1720
1721 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1722 value = !value;
1723 trace_gpio_value(desc_to_gpio(desc), 0, value);
1724 /*
1725 * collect all normal outputs belonging to the same chip
1726 * open drain and open source outputs are set individually
1727 */
1728 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001729 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001730 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1731 _gpio_set_open_source_value(desc, value);
1732 } else {
1733 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001734 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001735 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001736 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001737 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001738 count++;
1739 }
1740 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001741 } while ((i < array_size) &&
1742 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001743 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001744 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001745 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001746 }
1747}
1748
David Brownelld2876d02008-02-04 22:28:20 -08001749/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001750 * gpiod_set_raw_value() - assign a gpio's raw value
1751 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001752 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001753 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001754 * Set the raw value of the GPIO, i.e. the value of its physical line without
1755 * regard for its ACTIVE_LOW status.
1756 *
1757 * This function should be called from contexts where we cannot sleep, and will
1758 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001759 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001760void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001761{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001762 VALIDATE_DESC_VOID(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001763 /* Should be using gpio_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001764 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001765 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001766}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001767EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001768
1769/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001770 * gpiod_set_value() - assign a gpio's value
1771 * @desc: gpio whose value will be assigned
1772 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001773 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001774 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1775 * account
1776 *
1777 * This function should be called from contexts where we cannot sleep, and will
1778 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001779 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001780void gpiod_set_value(struct gpio_desc *desc, int value)
1781{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001782 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001783 /* Should be using gpio_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001784 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001785 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1786 value = !value;
1787 _gpiod_set_raw_value(desc, value);
1788}
1789EXPORT_SYMBOL_GPL(gpiod_set_value);
1790
1791/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001792 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001793 * @array_size: number of elements in the descriptor / value arrays
1794 * @desc_array: array of GPIO descriptors whose values will be assigned
1795 * @value_array: array of values to assign
1796 *
1797 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1798 * without regard for their ACTIVE_LOW status.
1799 *
1800 * This function should be called from contexts where we cannot sleep, and will
1801 * complain if the GPIO chip functions potentially sleep.
1802 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001803void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001804 struct gpio_desc **desc_array, int *value_array)
1805{
1806 if (!desc_array)
1807 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001808 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1809 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001810}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001811EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001812
1813/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001814 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001815 * @array_size: number of elements in the descriptor / value arrays
1816 * @desc_array: array of GPIO descriptors whose values will be assigned
1817 * @value_array: array of values to assign
1818 *
1819 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1820 * into account.
1821 *
1822 * This function should be called from contexts where we cannot sleep, and will
1823 * complain if the GPIO chip functions potentially sleep.
1824 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001825void gpiod_set_array_value(unsigned int array_size,
1826 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001827{
1828 if (!desc_array)
1829 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001830 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1831 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001832}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001833EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001834
1835/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001836 * gpiod_cansleep() - report whether gpio value access may sleep
1837 * @desc: gpio to check
1838 *
1839 */
1840int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001841{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001842 VALIDATE_DESC(desc);
1843 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001844}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001845EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001846
David Brownell0f6d5042008-10-15 22:03:14 -07001847/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001848 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1849 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001850 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001851 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1852 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001853 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001854int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001855{
1856 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001857 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001858
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001859 VALIDATE_DESC(desc);
1860 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001861 offset = gpio_chip_hwgpio(desc);
1862 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1863}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001864EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001865
Linus Walleijd468bf92013-09-24 11:54:38 +02001866/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001867 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001868 * @chip: the chip the GPIO to lock belongs to
1869 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001870 *
1871 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001872 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001873 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001874int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001875{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001876 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001877 return -EINVAL;
1878
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001879 if (test_bit(FLAG_IS_OUT, &chip->gpiodev->descs[offset].flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001880 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001881 "%s: tried to flag a GPIO set as output for IRQ\n",
1882 __func__);
1883 return -EIO;
1884 }
1885
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001886 set_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001887 return 0;
1888}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001889EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001890
Linus Walleijd468bf92013-09-24 11:54:38 +02001891/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001892 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001893 * @chip: the chip the GPIO to lock belongs to
1894 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001895 *
1896 * This is used directly by GPIO drivers that want to indicate
1897 * that a certain GPIO is no longer used exclusively for IRQ.
1898 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001899void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001900{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001901 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001902 return;
1903
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001904 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001905}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001906EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001907
Linus Walleij6cee3822016-02-11 20:16:45 +01001908bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
1909{
1910 if (offset >= chip->ngpio)
1911 return false;
1912
1913 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
1914}
1915EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
1916
Linus Walleij143b65d2016-02-16 15:41:42 +01001917bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
1918{
1919 if (offset >= chip->ngpio)
1920 return false;
1921
1922 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
1923}
1924EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
1925
1926bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
1927{
1928 if (offset >= chip->ngpio)
1929 return false;
1930
1931 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
1932}
1933EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
1934
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001935/**
1936 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1937 * @desc: gpio whose value will be returned
1938 *
1939 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001940 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001941 *
1942 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001943 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001944int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001945{
David Brownelld2876d02008-02-04 22:28:20 -08001946 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001947 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001948 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001949}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001950EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001951
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001952/**
1953 * gpiod_get_value_cansleep() - return a gpio's value
1954 * @desc: gpio whose value will be returned
1955 *
1956 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001957 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001958 *
1959 * This function is to be called from contexts that can sleep.
1960 */
1961int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001962{
David Brownelld2876d02008-02-04 22:28:20 -08001963 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001964
1965 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001966 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001967 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001968 if (value < 0)
1969 return value;
1970
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001971 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1972 value = !value;
1973
David Brownelld2876d02008-02-04 22:28:20 -08001974 return value;
1975}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001976EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001977
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001978/**
1979 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1980 * @desc: gpio whose value will be assigned
1981 * @value: value to assign
1982 *
1983 * Set the raw value of the GPIO, i.e. the value of its physical line without
1984 * regard for its ACTIVE_LOW status.
1985 *
1986 * This function is to be called from contexts that can sleep.
1987 */
1988void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001989{
David Brownelld2876d02008-02-04 22:28:20 -08001990 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001991 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001992 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001993}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001994EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001995
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001996/**
1997 * gpiod_set_value_cansleep() - assign a gpio's value
1998 * @desc: gpio whose value will be assigned
1999 * @value: value to assign
2000 *
2001 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2002 * account
2003 *
2004 * This function is to be called from contexts that can sleep.
2005 */
2006void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002007{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002008 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002009 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002010 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2011 value = !value;
2012 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002013}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002014EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002015
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002016/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002017 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002018 * @array_size: number of elements in the descriptor / value arrays
2019 * @desc_array: array of GPIO descriptors whose values will be assigned
2020 * @value_array: array of values to assign
2021 *
2022 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2023 * without regard for their ACTIVE_LOW status.
2024 *
2025 * This function is to be called from contexts that can sleep.
2026 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002027void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2028 struct gpio_desc **desc_array,
2029 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002030{
2031 might_sleep_if(extra_checks);
2032 if (!desc_array)
2033 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002034 gpiod_set_array_value_priv(true, true, array_size, desc_array,
2035 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002036}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002037EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002038
2039/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002040 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002041 * @array_size: number of elements in the descriptor / value arrays
2042 * @desc_array: array of GPIO descriptors whose values will be assigned
2043 * @value_array: array of values to assign
2044 *
2045 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2046 * into account.
2047 *
2048 * This function is to be called from contexts that can sleep.
2049 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002050void gpiod_set_array_value_cansleep(unsigned int array_size,
2051 struct gpio_desc **desc_array,
2052 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002053{
2054 might_sleep_if(extra_checks);
2055 if (!desc_array)
2056 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002057 gpiod_set_array_value_priv(false, true, array_size, desc_array,
2058 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002059}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002060EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002061
2062/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002063 * gpiod_add_lookup_table() - register GPIO device consumers
2064 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002065 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002066void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002067{
2068 mutex_lock(&gpio_lookup_lock);
2069
Alexandre Courbotad824782013-12-03 12:20:11 +09002070 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002071
2072 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002073}
2074
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302075/**
2076 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2077 * @table: table of consumers to unregister
2078 */
2079void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2080{
2081 mutex_lock(&gpio_lookup_lock);
2082
2083 list_del(&table->list);
2084
2085 mutex_unlock(&gpio_lookup_lock);
2086}
2087
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002088static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002089 unsigned int idx,
2090 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002091{
2092 char prop_name[32]; /* 32 is max size of property name */
2093 enum of_gpio_flags of_flags;
2094 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02002095 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002096
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002097 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02002098 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01002099 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002100 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02002101 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01002102 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002103 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002104
Thierry Redingdd34c372014-04-23 17:28:09 +02002105 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2106 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07002107 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02002108 break;
2109 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002110
2111 if (IS_ERR(desc))
2112 return desc;
2113
2114 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002115 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002116
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002117 if (of_flags & OF_GPIO_SINGLE_ENDED) {
2118 if (of_flags & OF_GPIO_ACTIVE_LOW)
2119 *flags |= GPIO_OPEN_DRAIN;
2120 else
2121 *flags |= GPIO_OPEN_SOURCE;
2122 }
2123
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002124 return desc;
2125}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002126
Mika Westerberg81f59e92013-10-10 11:01:09 +03002127static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002128 unsigned int idx,
2129 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03002130{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002131 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03002132 struct acpi_gpio_info info;
2133 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002134 char propname[32];
2135 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03002136
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002137 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002138 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002139 if (con_id && strcmp(con_id, "gpios")) {
2140 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002141 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002142 } else {
2143 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002144 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002145 }
Mika Westerberge01f4402013-10-10 11:01:10 +03002146
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002147 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
2148 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2149 break;
2150 }
2151
2152 /* Then from plain _CRS GPIOs */
2153 if (IS_ERR(desc)) {
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08002154 if (!acpi_can_fallback_to_crs(adev, con_id))
2155 return ERR_PTR(-ENOENT);
2156
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002157 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2158 if (IS_ERR(desc))
2159 return desc;
2160 }
2161
Christophe RICARD52044722015-12-23 23:25:34 +01002162 if (info.polarity == GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002163 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002164
2165 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002166}
2167
Alexandre Courbotad824782013-12-03 12:20:11 +09002168static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2169{
2170 const char *dev_id = dev ? dev_name(dev) : NULL;
2171 struct gpiod_lookup_table *table;
2172
2173 mutex_lock(&gpio_lookup_lock);
2174
2175 list_for_each_entry(table, &gpio_lookup_list, list) {
2176 if (table->dev_id && dev_id) {
2177 /*
2178 * Valid strings on both ends, must be identical to have
2179 * a match
2180 */
2181 if (!strcmp(table->dev_id, dev_id))
2182 goto found;
2183 } else {
2184 /*
2185 * One of the pointers is NULL, so both must be to have
2186 * a match
2187 */
2188 if (dev_id == table->dev_id)
2189 goto found;
2190 }
2191 }
2192 table = NULL;
2193
2194found:
2195 mutex_unlock(&gpio_lookup_lock);
2196 return table;
2197}
2198
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002199static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002200 unsigned int idx,
2201 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002202{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002203 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002204 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002205 struct gpiod_lookup *p;
2206
Alexandre Courbotad824782013-12-03 12:20:11 +09002207 table = gpiod_find_lookup_table(dev);
2208 if (!table)
2209 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002210
Alexandre Courbotad824782013-12-03 12:20:11 +09002211 for (p = &table->table[0]; p->chip_label; p++) {
2212 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002213
Alexandre Courbotad824782013-12-03 12:20:11 +09002214 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002215 if (p->idx != idx)
2216 continue;
2217
Alexandre Courbotad824782013-12-03 12:20:11 +09002218 /* If the lookup entry has a con_id, require exact match */
2219 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2220 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002221
Alexandre Courbotad824782013-12-03 12:20:11 +09002222 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002223
Alexandre Courbotad824782013-12-03 12:20:11 +09002224 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002225 dev_err(dev, "cannot find GPIO chip %s\n",
2226 p->chip_label);
2227 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002228 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002229
Alexandre Courbotad824782013-12-03 12:20:11 +09002230 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002231 dev_err(dev,
2232 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2233 idx, chip->ngpio, chip->label);
2234 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09002235 }
2236
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09002237 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09002238 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002239
2240 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09002241 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002242
2243 return desc;
2244}
2245
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002246static int dt_gpio_count(struct device *dev, const char *con_id)
2247{
2248 int ret;
2249 char propname[32];
2250 unsigned int i;
2251
2252 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2253 if (con_id)
2254 snprintf(propname, sizeof(propname), "%s-%s",
2255 con_id, gpio_suffixes[i]);
2256 else
2257 snprintf(propname, sizeof(propname), "%s",
2258 gpio_suffixes[i]);
2259
2260 ret = of_gpio_named_count(dev->of_node, propname);
2261 if (ret >= 0)
2262 break;
2263 }
2264 return ret;
2265}
2266
2267static int platform_gpio_count(struct device *dev, const char *con_id)
2268{
2269 struct gpiod_lookup_table *table;
2270 struct gpiod_lookup *p;
2271 unsigned int count = 0;
2272
2273 table = gpiod_find_lookup_table(dev);
2274 if (!table)
2275 return -ENOENT;
2276
2277 for (p = &table->table[0]; p->chip_label; p++) {
2278 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2279 (!con_id && !p->con_id))
2280 count++;
2281 }
2282 if (!count)
2283 return -ENOENT;
2284
2285 return count;
2286}
2287
2288/**
2289 * gpiod_count - return the number of GPIOs associated with a device / function
2290 * or -ENOENT if no GPIO has been assigned to the requested function
2291 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2292 * @con_id: function within the GPIO consumer
2293 */
2294int gpiod_count(struct device *dev, const char *con_id)
2295{
2296 int count = -ENOENT;
2297
2298 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2299 count = dt_gpio_count(dev, con_id);
2300 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2301 count = acpi_gpio_count(dev, con_id);
2302
2303 if (count < 0)
2304 count = platform_gpio_count(dev, con_id);
2305
2306 return count;
2307}
2308EXPORT_SYMBOL_GPL(gpiod_count);
2309
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002310/**
Thierry Reding08791622014-04-25 16:54:22 +02002311 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002312 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002313 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002314 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002315 *
2316 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002317 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07002318 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002319 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002320struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002321 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002322{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002323 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002324}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002325EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002326
2327/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002328 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2329 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2330 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002331 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002332 *
2333 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2334 * the requested function it will return NULL. This is convenient for drivers
2335 * that need to handle optional GPIOs.
2336 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002337struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002338 const char *con_id,
2339 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002340{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002341 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002342}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002343EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002344
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002345/**
2346 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2347 * @desc: gpio to be setup
2348 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2349 * of_get_gpio_hog()
2350 *
2351 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2352 */
2353static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2354{
2355 if (lflags & GPIO_ACTIVE_LOW)
2356 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2357 if (lflags & GPIO_OPEN_DRAIN)
2358 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2359 if (lflags & GPIO_OPEN_SOURCE)
2360 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2361}
Benoit Parrotf625d462015-02-02 11:44:44 -06002362
2363/**
2364 * gpiod_configure_flags - helper function to configure a given GPIO
2365 * @desc: gpio whose value will be assigned
2366 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06002367 * @dflags: gpiod_flags - optional GPIO initialization flags
2368 *
2369 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2370 * requested function and/or index, or another IS_ERR() code if an error
2371 * occurred while trying to acquire the GPIO.
2372 */
2373static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002374 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06002375{
2376 int status;
2377
Benoit Parrotf625d462015-02-02 11:44:44 -06002378 /* No particular flag request, return here... */
2379 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2380 pr_debug("no flags found for %s\n", con_id);
2381 return 0;
2382 }
2383
2384 /* Process flags */
2385 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2386 status = gpiod_direction_output(desc,
2387 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2388 else
2389 status = gpiod_direction_input(desc);
2390
2391 return status;
2392}
2393
Thierry Reding29a1f2332014-04-25 17:10:06 +02002394/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002395 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002396 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002397 * @con_id: function within the GPIO consumer
2398 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002399 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002400 *
2401 * This variant of gpiod_get() allows to access GPIOs other than the first
2402 * defined one for functions that define several GPIOs.
2403 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002404 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2405 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07002406 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002407 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002408struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002409 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002410 unsigned int idx,
2411 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002412{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002413 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002414 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002415 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002416
2417 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2418
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002419 if (dev) {
2420 /* Using device tree? */
2421 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2422 dev_dbg(dev, "using device tree for GPIO lookup\n");
2423 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2424 } else if (ACPI_COMPANION(dev)) {
2425 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2426 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2427 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002428 }
2429
2430 /*
2431 * Either we are not using DT or ACPI, or their lookup did not return
2432 * a result. In that case, use platform lookup as a fallback.
2433 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002434 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002435 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002436 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002437 }
2438
2439 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002440 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002441 return desc;
2442 }
2443
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002444 gpiod_parse_flags(desc, lookupflags);
2445
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002446 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002447 if (status < 0)
2448 return ERR_PTR(status);
2449
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002450 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002451 if (status < 0) {
2452 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2453 gpiod_put(desc);
2454 return ERR_PTR(status);
2455 }
2456
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002457 return desc;
2458}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002459EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002460
2461/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002462 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2463 * @fwnode: handle of the firmware node
2464 * @propname: name of the firmware property representing the GPIO
2465 *
2466 * This function can be used for drivers that get their configuration
2467 * from firmware.
2468 *
2469 * Function properly finds the corresponding GPIO using whatever is the
2470 * underlying firmware interface and then makes sure that the GPIO
2471 * descriptor is requested before it is returned to the caller.
2472 *
2473 * In case of error an ERR_PTR() is returned.
2474 */
2475struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2476 const char *propname)
2477{
2478 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2479 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002480 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02002481 int ret;
2482
2483 if (!fwnode)
2484 return ERR_PTR(-EINVAL);
2485
2486 if (is_of_node(fwnode)) {
2487 enum of_gpio_flags flags;
2488
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002489 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002490 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002491 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02002492 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002493 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2494 }
Mika Westerberg40b73182014-10-21 13:33:59 +02002495 } else if (is_acpi_node(fwnode)) {
2496 struct acpi_gpio_info info;
2497
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02002498 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02002499 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01002500 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02002501 }
2502
2503 if (IS_ERR(desc))
2504 return desc;
2505
Mika Westerberg40b73182014-10-21 13:33:59 +02002506 if (active_low)
2507 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2508
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002509 if (single_ended) {
2510 if (active_low)
2511 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2512 else
2513 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2514 }
2515
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002516 ret = gpiod_request(desc, NULL);
2517 if (ret)
2518 return ERR_PTR(ret);
2519
Mika Westerberg40b73182014-10-21 13:33:59 +02002520 return desc;
2521}
2522EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2523
2524/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002525 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2526 * function
2527 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2528 * @con_id: function within the GPIO consumer
2529 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002530 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002531 *
2532 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2533 * specified index was assigned to the requested function it will return NULL.
2534 * This is convenient for drivers that need to handle optional GPIOs.
2535 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002536struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002537 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002538 unsigned int index,
2539 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002540{
2541 struct gpio_desc *desc;
2542
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002543 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002544 if (IS_ERR(desc)) {
2545 if (PTR_ERR(desc) == -ENOENT)
2546 return NULL;
2547 }
2548
2549 return desc;
2550}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002551EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002552
2553/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002554 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2555 * @desc: gpio whose value will be assigned
2556 * @name: gpio line name
2557 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2558 * of_get_gpio_hog()
2559 * @dflags: gpiod_flags - optional GPIO initialization flags
2560 */
2561int gpiod_hog(struct gpio_desc *desc, const char *name,
2562 unsigned long lflags, enum gpiod_flags dflags)
2563{
2564 struct gpio_chip *chip;
2565 struct gpio_desc *local_desc;
2566 int hwnum;
2567 int status;
2568
2569 chip = gpiod_to_chip(desc);
2570 hwnum = gpio_chip_hwgpio(desc);
2571
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002572 gpiod_parse_flags(desc, lflags);
2573
Benoit Parrotf625d462015-02-02 11:44:44 -06002574 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2575 if (IS_ERR(local_desc)) {
Linus Walleija7138902015-06-05 11:36:10 +02002576 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2577 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002578 return PTR_ERR(local_desc);
2579 }
2580
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002581 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002582 if (status < 0) {
Linus Walleija7138902015-06-05 11:36:10 +02002583 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2584 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002585 gpiochip_free_own_desc(desc);
2586 return status;
2587 }
2588
2589 /* Mark GPIO as hogged so it can be identified and removed later */
2590 set_bit(FLAG_IS_HOGGED, &desc->flags);
2591
2592 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2593 desc_to_gpio(desc), name,
2594 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2595 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2596 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2597
2598 return 0;
2599}
2600
2601/**
2602 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2603 * @chip: gpio chip to act on
2604 *
2605 * This is only used by of_gpiochip_remove to free hogged gpios
2606 */
2607static void gpiochip_free_hogs(struct gpio_chip *chip)
2608{
2609 int id;
2610
2611 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002612 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
2613 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06002614 }
2615}
2616
2617/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002618 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2619 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2620 * @con_id: function within the GPIO consumer
2621 * @flags: optional GPIO initialization flags
2622 *
2623 * This function acquires all the GPIOs defined under a given function.
2624 *
2625 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2626 * no GPIO has been assigned to the requested function, or another IS_ERR()
2627 * code if an error occurred while trying to acquire the GPIOs.
2628 */
2629struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2630 const char *con_id,
2631 enum gpiod_flags flags)
2632{
2633 struct gpio_desc *desc;
2634 struct gpio_descs *descs;
2635 int count;
2636
2637 count = gpiod_count(dev, con_id);
2638 if (count < 0)
2639 return ERR_PTR(count);
2640
2641 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2642 GFP_KERNEL);
2643 if (!descs)
2644 return ERR_PTR(-ENOMEM);
2645
2646 for (descs->ndescs = 0; descs->ndescs < count; ) {
2647 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2648 if (IS_ERR(desc)) {
2649 gpiod_put_array(descs);
2650 return ERR_CAST(desc);
2651 }
2652 descs->desc[descs->ndescs] = desc;
2653 descs->ndescs++;
2654 }
2655 return descs;
2656}
2657EXPORT_SYMBOL_GPL(gpiod_get_array);
2658
2659/**
2660 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2661 * function
2662 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2663 * @con_id: function within the GPIO consumer
2664 * @flags: optional GPIO initialization flags
2665 *
2666 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2667 * assigned to the requested function it will return NULL.
2668 */
2669struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2670 const char *con_id,
2671 enum gpiod_flags flags)
2672{
2673 struct gpio_descs *descs;
2674
2675 descs = gpiod_get_array(dev, con_id, flags);
2676 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2677 return NULL;
2678
2679 return descs;
2680}
2681EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2682
2683/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002684 * gpiod_put - dispose of a GPIO descriptor
2685 * @desc: GPIO descriptor to dispose of
2686 *
2687 * No descriptor can be used after gpiod_put() has been called on it.
2688 */
2689void gpiod_put(struct gpio_desc *desc)
2690{
2691 gpiod_free(desc);
2692}
2693EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002694
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002695/**
2696 * gpiod_put_array - dispose of multiple GPIO descriptors
2697 * @descs: struct gpio_descs containing an array of descriptors
2698 */
2699void gpiod_put_array(struct gpio_descs *descs)
2700{
2701 unsigned int i;
2702
2703 for (i = 0; i < descs->ndescs; i++)
2704 gpiod_put(descs->desc[i]);
2705
2706 kfree(descs);
2707}
2708EXPORT_SYMBOL_GPL(gpiod_put_array);
2709
Linus Walleij3c702e92015-10-21 15:29:53 +02002710static int __init gpiolib_dev_init(void)
2711{
2712 int ret;
2713
2714 /* Register GPIO sysfs bus */
2715 ret = bus_register(&gpio_bus_type);
2716 if (ret < 0) {
2717 pr_err("gpiolib: could not register GPIO bus type\n");
2718 return ret;
2719 }
2720
2721 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
2722 if (ret < 0) {
2723 pr_err("gpiolib: failed to allocate char dev region\n");
2724 bus_unregister(&gpio_bus_type);
2725 }
2726 return ret;
2727}
2728core_initcall(gpiolib_dev_init);
2729
David Brownelld2876d02008-02-04 22:28:20 -08002730#ifdef CONFIG_DEBUG_FS
2731
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002732static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08002733{
2734 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002735 struct gpio_chip *chip = gdev->chip;
2736 unsigned gpio = gdev->base;
2737 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08002738 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002739 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002740
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002741 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02002742 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2743 if (gdesc->name) {
2744 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2745 gpio, gdesc->name);
2746 }
David Brownelld2876d02008-02-04 22:28:20 -08002747 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02002748 }
David Brownelld2876d02008-02-04 22:28:20 -08002749
Alexandre Courbot372e7222013-02-03 01:29:29 +09002750 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002751 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002752 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02002753 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2754 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08002755 is_out ? "out" : "in ",
2756 chip->get
2757 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002758 : "? ",
2759 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002760 seq_printf(s, "\n");
2761 }
2762}
2763
Thierry Redingf9c4a312012-04-12 13:26:01 +02002764static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002765{
Grant Likely362432a2013-02-09 09:41:49 +00002766 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002767 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002768 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002769
2770 s->private = "";
2771
Grant Likely362432a2013-02-09 09:41:49 +00002772 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002773 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00002774 if (index-- == 0) {
2775 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002776 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00002777 }
2778 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002779
2780 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002781}
2782
2783static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2784{
Grant Likely362432a2013-02-09 09:41:49 +00002785 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002786 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002787 void *ret = NULL;
2788
Grant Likely362432a2013-02-09 09:41:49 +00002789 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002790 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002791 ret = NULL;
2792 else
Linus Walleijff2b1352015-10-20 11:10:38 +02002793 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00002794 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002795
2796 s->private = "\n";
2797 ++*pos;
2798
2799 return ret;
2800}
2801
2802static void gpiolib_seq_stop(struct seq_file *s, void *v)
2803{
2804}
2805
2806static int gpiolib_seq_show(struct seq_file *s, void *v)
2807{
Linus Walleijff2b1352015-10-20 11:10:38 +02002808 struct gpio_device *gdev = v;
2809 struct gpio_chip *chip = gdev->chip;
2810 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002811
Linus Walleijff2b1352015-10-20 11:10:38 +02002812 if (!chip) {
2813 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
2814 dev_name(&gdev->dev));
2815 return 0;
2816 }
2817
2818 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
2819 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002820 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02002821 parent = chip->parent;
2822 if (parent)
2823 seq_printf(s, ", parent: %s/%s",
2824 parent->bus ? parent->bus->name : "no-bus",
2825 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02002826 if (chip->label)
2827 seq_printf(s, ", %s", chip->label);
2828 if (chip->can_sleep)
2829 seq_printf(s, ", can sleep");
2830 seq_printf(s, ":\n");
2831
2832 if (chip->dbg_show)
2833 chip->dbg_show(s, chip);
2834 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002835 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002836
David Brownelld2876d02008-02-04 22:28:20 -08002837 return 0;
2838}
2839
Thierry Redingf9c4a312012-04-12 13:26:01 +02002840static const struct seq_operations gpiolib_seq_ops = {
2841 .start = gpiolib_seq_start,
2842 .next = gpiolib_seq_next,
2843 .stop = gpiolib_seq_stop,
2844 .show = gpiolib_seq_show,
2845};
2846
David Brownelld2876d02008-02-04 22:28:20 -08002847static int gpiolib_open(struct inode *inode, struct file *file)
2848{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002849 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002850}
2851
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002852static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002853 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002854 .open = gpiolib_open,
2855 .read = seq_read,
2856 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002857 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002858};
2859
2860static int __init gpiolib_debugfs_init(void)
2861{
2862 /* /sys/kernel/debug/gpio */
2863 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2864 NULL, NULL, &gpiolib_operations);
2865 return 0;
2866}
2867subsys_initcall(gpiolib_debugfs_init);
2868
2869#endif /* DEBUG_FS */