blob: 872774a404f1752549753975082298e2381315c7 [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;
Linus Walleij3c702e92015-10-21 15:29:53 +0200334
335 /* We fail any subsequent ioctl():s when the chip is gone */
336 if (!chip)
337 return -ENODEV;
338
Linus Walleij521a2ad2016-02-12 22:25:22 +0100339 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200340 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100341 struct gpiochip_info chipinfo;
342
Linus Walleij3c702e92015-10-21 15:29:53 +0200343 strncpy(chipinfo.name, dev_name(&gdev->dev),
344 sizeof(chipinfo.name));
345 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100346 strncpy(chipinfo.label, gdev->label,
347 sizeof(chipinfo.label));
348 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100349 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200350 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
351 return -EFAULT;
352 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100353 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
354 struct gpioline_info lineinfo;
355 struct gpio_desc *desc;
356
357 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
358 return -EFAULT;
359 if (lineinfo.line_offset > gdev->ngpio)
360 return -EINVAL;
361
362 desc = &gdev->descs[lineinfo.line_offset];
363 if (desc->name) {
364 strncpy(lineinfo.name, desc->name,
365 sizeof(lineinfo.name));
366 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
367 } else {
368 lineinfo.name[0] = '\0';
369 }
370 if (desc->label) {
371 strncpy(lineinfo.label, desc->label,
372 sizeof(lineinfo.label));
373 lineinfo.label[sizeof(lineinfo.label)-1] = '\0';
374 } else {
375 lineinfo.label[0] = '\0';
376 }
377
378 /*
379 * Userspace only need to know that the kernel is using
380 * this GPIO so it can't use it.
381 */
382 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100383 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
384 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
385 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
386 test_bit(FLAG_EXPORT, &desc->flags) ||
387 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100388 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100389 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100390 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100391 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100392 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100393 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100394 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100395 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100396 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
397
398 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
399 return -EFAULT;
400 return 0;
Linus Walleij3c702e92015-10-21 15:29:53 +0200401 }
402 return -EINVAL;
403}
404
405/**
406 * gpio_chrdev_open() - open the chardev for ioctl operations
407 * @inode: inode for this chardev
408 * @filp: file struct for storing private data
409 * Returns 0 on success
410 */
411static int gpio_chrdev_open(struct inode *inode, struct file *filp)
412{
413 struct gpio_device *gdev = container_of(inode->i_cdev,
414 struct gpio_device, chrdev);
415
416 /* Fail on open if the backing gpiochip is gone */
417 if (!gdev || !gdev->chip)
418 return -ENODEV;
419 get_device(&gdev->dev);
420 filp->private_data = gdev;
421 return 0;
422}
423
424/**
425 * gpio_chrdev_release() - close chardev after ioctl operations
426 * @inode: inode for this chardev
427 * @filp: file struct for storing private data
428 * Returns 0 on success
429 */
430static int gpio_chrdev_release(struct inode *inode, struct file *filp)
431{
432 struct gpio_device *gdev = container_of(inode->i_cdev,
433 struct gpio_device, chrdev);
434
435 if (!gdev)
436 return -ENODEV;
437 put_device(&gdev->dev);
438 return 0;
439}
440
441
442static const struct file_operations gpio_fileops = {
443 .release = gpio_chrdev_release,
444 .open = gpio_chrdev_open,
445 .owner = THIS_MODULE,
446 .llseek = noop_llseek,
447 .unlocked_ioctl = gpio_ioctl,
448 .compat_ioctl = gpio_ioctl,
449};
450
Linus Walleijff2b1352015-10-20 11:10:38 +0200451static void gpiodevice_release(struct device *dev)
452{
453 struct gpio_device *gdev = dev_get_drvdata(dev);
454
Linus Walleij3c702e92015-10-21 15:29:53 +0200455 cdev_del(&gdev->chrdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200456 list_del(&gdev->list);
457 ida_simple_remove(&gpio_ida, gdev->id);
Linus Walleij9efd9e62016-02-09 14:27:42 +0100458 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200459}
460
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700461/**
Linus Walleijb08ea352015-12-03 15:14:13 +0100462 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -0800463 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900464 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -0800465 *
466 * Returns a negative errno if the chip can't be registered, such as
467 * because the chip->base is invalid or already associated with a
468 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700469 *
Linus Walleijb08ea352015-12-03 15:14:13 +0100470 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800471 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d2008-07-25 01:46:07 -0700472 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
473 * for GPIOs will fail rudely.
474 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700475 * If chip->base is negative, this requests dynamic assignment of
476 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800477 */
Linus Walleijb08ea352015-12-03 15:14:13 +0100478int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -0800479{
480 unsigned long flags;
481 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +0200482 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700483 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +0200484 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -0800485
Linus Walleijff2b1352015-10-20 11:10:38 +0200486 /*
487 * First: allocate and populate the internal stat container, and
488 * set up the struct device.
489 */
Josh Cartwright969f07b2016-02-17 16:44:15 -0600490 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +0200491 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900492 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +0200493 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +0200494 gdev->chip = chip;
495 chip->gpiodev = gdev;
496 if (chip->parent) {
497 gdev->dev.parent = chip->parent;
498 gdev->dev.of_node = chip->parent->of_node;
499 } else {
500#ifdef CONFIG_OF_GPIO
501 /* If the gpiochip has an assigned OF node this takes precedence */
502 if (chip->of_node)
503 gdev->dev.of_node = chip->of_node;
504#endif
505 }
506 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
507 if (gdev->id < 0) {
508 status = gdev->id;
509 goto err_free_gdev;
510 }
511 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
512 device_initialize(&gdev->dev);
513 dev_set_drvdata(&gdev->dev, gdev);
514 if (chip->parent && chip->parent->driver)
515 gdev->owner = chip->parent->driver->owner;
516 else if (chip->owner)
517 /* TODO: remove chip->owner */
518 gdev->owner = chip->owner;
519 else
520 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -0800521
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100522 gdev->descs = devm_kcalloc(&gdev->dev, chip->ngpio,
523 sizeof(gdev->descs[0]), GFP_KERNEL);
524 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +0200525 status = -ENOMEM;
526 goto err_free_gdev;
527 }
528
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800529 if (chip->ngpio == 0) {
530 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +0200531 status = -EINVAL;
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100532 goto err_free_gdev;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800533 }
Linus Walleijdf4878e2016-02-12 14:48:23 +0100534
535 if (chip->label)
536 gdev->label = devm_kstrdup(&gdev->dev, chip->label, GFP_KERNEL);
537 else
538 gdev->label = devm_kstrdup(&gdev->dev, "unknown", GFP_KERNEL);
539 if (!gdev->label) {
540 status = -ENOMEM;
541 goto err_free_gdev;
542 }
543
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100544 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +0100545 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800546
David Brownelld2876d02008-02-04 22:28:20 -0800547 spin_lock_irqsave(&gpio_lock, flags);
548
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100549 /*
550 * TODO: this allocates a Linux GPIO number base in the global
551 * GPIO numberspace for this chip. In the long run we want to
552 * get *rid* of this numberspace and use only descriptors, but
553 * it may be a pipe dream. It will not happen before we get rid
554 * of the sysfs interface anyways.
555 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700556 if (base < 0) {
557 base = gpiochip_find_base(chip->ngpio);
558 if (base < 0) {
559 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100560 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100561 goto err_free_gdev;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700562 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100563 /*
564 * TODO: it should not be necessary to reflect the assigned
565 * base outside of the GPIO subsystem. Go over drivers and
566 * see if anyone makes use of this, else drop this and assign
567 * a poison instead.
568 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700569 chip->base = base;
570 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100571 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700572
Linus Walleijff2b1352015-10-20 11:10:38 +0200573 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +0100574 if (status) {
575 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100576 goto err_free_gdev;
Johan Hovold05aa5202015-01-12 17:12:26 +0100577 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900578
Linus Walleijff2b1352015-10-20 11:10:38 +0200579 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100580 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d2008-07-25 01:46:07 -0700581
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100582 desc->gdev = gdev;
Johan Hovold05aa5202015-01-12 17:12:26 +0100583
584 /* REVISIT: most hardware initializes GPIOs as inputs (often
585 * with pullups enabled) so power usage is minimized. Linux
586 * code should set the gpio direction first thing; but until
587 * it does, and in case chip->get_direction is not set, we may
588 * expose the wrong direction in sysfs.
589 */
590 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800591 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900592
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800593 spin_unlock_irqrestore(&gpio_lock, flags);
594
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530595#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +0100596 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530597#endif
598
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200599 status = gpiochip_set_desc_names(chip);
600 if (status)
601 goto err_remove_from_list;
602
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200603 status = of_gpiochip_add(chip);
604 if (status)
605 goto err_remove_chip;
606
Mika Westerberg664e3e52014-01-08 12:40:54 +0200607 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600608
Linus Walleij3c702e92015-10-21 15:29:53 +0200609 /*
610 * By first adding the chardev, and then adding the device,
611 * we get a device node entry in sysfs under
612 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
613 * coldplug of device nodes and other udev business.
614 */
615 cdev_init(&gdev->chrdev, &gpio_fileops);
616 gdev->chrdev.owner = THIS_MODULE;
617 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
618 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
619 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
620 if (status < 0)
621 chip_warn(chip, "failed to add char device %d:%d\n",
622 MAJOR(gpio_devt), gdev->id);
623 else
624 chip_dbg(chip, "added GPIO chardev (%d:%d)\n",
625 MAJOR(gpio_devt), gdev->id);
Linus Walleijff2b1352015-10-20 11:10:38 +0200626 status = device_add(&gdev->dev);
Johan Hovold225fce82015-01-12 17:12:25 +0100627 if (status)
Linus Walleij3c702e92015-10-21 15:29:53 +0200628 goto err_remove_chardev;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600629
Linus Walleijafbc4f32016-02-09 13:21:06 +0100630 status = gpiochip_sysfs_register(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200631 if (status)
632 goto err_remove_device;
633
634 /* From this point, the .release() function cleans up gpio_device */
635 gdev->dev.release = gpiodevice_release;
636 get_device(&gdev->dev);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100637 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
638 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
639 dev_name(&gdev->dev), chip->label ? : "generic");
Grant Likely64842aa2011-11-06 11:36:18 -0700640
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600641 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800642
Linus Walleijff2b1352015-10-20 11:10:38 +0200643err_remove_device:
644 device_del(&gdev->dev);
Linus Walleij3c702e92015-10-21 15:29:53 +0200645err_remove_chardev:
646 cdev_del(&gdev->chrdev);
Johan Hovold225fce82015-01-12 17:12:25 +0100647err_remove_chip:
648 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200649 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100650 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200651err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100652 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200653 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800654 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200655err_free_gdev:
656 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -0800657 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200658 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100659 gdev->base, gdev->base + gdev->ngpio - 1,
660 chip->label ? : "generic");
661 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -0800662 return status;
663}
Linus Walleijb08ea352015-12-03 15:14:13 +0100664EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -0800665
666/**
Linus Walleij43c54ec2016-02-11 11:37:48 +0100667 * gpiochip_get_data() - get per-subdriver data for the chip
668 */
669void *gpiochip_get_data(struct gpio_chip *chip)
670{
671 return chip->gpiodev->data;
672}
673EXPORT_SYMBOL_GPL(gpiochip_get_data);
674
675/**
David Brownelld2876d02008-02-04 22:28:20 -0800676 * gpiochip_remove() - unregister a gpio_chip
677 * @chip: the chip to unregister
678 *
679 * A gpio_chip with any GPIOs still requested may not be removed.
680 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200681void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800682{
Linus Walleijff2b1352015-10-20 11:10:38 +0200683 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200684 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800685 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100686 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200687 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800688
Linus Walleijff2b1352015-10-20 11:10:38 +0200689 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +0100690 gpiochip_sysfs_unregister(gdev);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +0800691 /* Numb the device, cancelling all outstanding operations */
692 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +0100693 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200694 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100695 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600696 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600697 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +0100698 /*
699 * We accept no more calls into the driver from this point, so
700 * NULL the driver data pointer
701 */
702 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -0600703
Johan Hovold6798aca2015-01-12 17:12:28 +0100704 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100705 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100706 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +0200707 if (test_bit(FLAG_REQUESTED, &desc->flags))
708 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800709 }
David Brownelld2876d02008-02-04 22:28:20 -0800710 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900711
Johan Hovoldfab28b82015-05-04 17:10:27 +0200712 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100713 dev_crit(&gdev->dev,
Linus Walleij58383c72015-11-04 09:56:26 +0100714 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +0200715
Linus Walleijff2b1352015-10-20 11:10:38 +0200716 /*
717 * The gpiochip side puts its use of the device to rest here:
718 * if there are no userspace clients, the chardev and device will
719 * be removed, else it will be dangling until the last user is
720 * gone.
721 */
722 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -0800723}
724EXPORT_SYMBOL_GPL(gpiochip_remove);
725
Grant Likely594fa262010-06-08 07:48:16 -0600726/**
727 * gpiochip_find() - iterator for locating a specific gpio_chip
728 * @data: data to pass to match function
729 * @callback: Callback function to check gpio_chip
730 *
731 * Similar to bus_find_device. It returns a reference to a gpio_chip as
732 * determined by a user supplied @match callback. The callback should return
733 * 0 if the device doesn't match and non-zero if it does. If the callback is
734 * non-zero, this function will return to the caller and not iterate over any
735 * more gpio_chips.
736 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600737struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700738 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600739 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600740{
Linus Walleijff2b1352015-10-20 11:10:38 +0200741 struct gpio_device *gdev;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900742 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600743 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600744
745 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200746 list_for_each_entry(gdev, &gpio_devices, list)
747 if (match(gdev->chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600748 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900749
750 /* No match? */
Linus Walleijff2b1352015-10-20 11:10:38 +0200751 if (&gdev->list == &gpio_devices)
Alexandre Courbot125eef92013-02-03 01:29:26 +0900752 chip = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +0200753 else
754 chip = gdev->chip;
755
Grant Likely594fa262010-06-08 07:48:16 -0600756 spin_unlock_irqrestore(&gpio_lock, flags);
757
758 return chip;
759}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600760EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800761
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900762static int gpiochip_match_name(struct gpio_chip *chip, void *data)
763{
764 const char *name = data;
765
766 return !strcmp(chip->label, name);
767}
768
769static struct gpio_chip *find_chip_by_name(const char *name)
770{
771 return gpiochip_find((void *)name, gpiochip_match_name);
772}
773
Linus Walleij14250522014-03-25 10:40:18 +0100774#ifdef CONFIG_GPIOLIB_IRQCHIP
775
776/*
777 * The following is irqchip helper code for gpiochips.
778 */
779
780/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200781 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
782 * @gpiochip: the gpiochip to set the irqchip chain to
783 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100784 * @parent_irq: the irq number corresponding to the parent IRQ for this
785 * chained irqchip
786 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200787 * coming out of the gpiochip. If the interrupt is nested rather than
788 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100789 */
790void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
791 struct irq_chip *irqchip,
792 int parent_irq,
793 irq_flow_handler_t parent_handler)
794{
Linus Walleij83141a72014-09-26 13:50:12 +0200795 unsigned int offset;
796
Linus Walleij83141a72014-09-26 13:50:12 +0200797 if (!gpiochip->irqdomain) {
798 chip_err(gpiochip, "called %s before setting up irqchip\n",
799 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200800 return;
801 }
802
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200803 if (parent_handler) {
804 if (gpiochip->can_sleep) {
805 chip_err(gpiochip,
806 "you cannot have chained interrupts on a "
807 "chip that may sleep\n");
808 return;
809 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200810 /*
811 * The parent irqchip is already using the chip_data for this
812 * irqchip, so our callbacks simply use the handler_data.
813 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +0200814 irq_set_chained_handler_and_data(parent_irq, parent_handler,
815 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300816
817 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200818 }
Linus Walleij83141a72014-09-26 13:50:12 +0200819
820 /* Set the parent IRQ for all affected IRQs */
821 for (offset = 0; offset < gpiochip->ngpio; offset++)
822 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
823 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100824}
825EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
826
827/**
828 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
829 * @d: the irqdomain used by this irqchip
830 * @irq: the global irq number used by this GPIO irqchip irq
831 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
832 *
833 * This function will set up the mapping for a certain IRQ line on a
834 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
835 * stored inside the gpiochip.
836 */
837static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
838 irq_hw_number_t hwirq)
839{
840 struct gpio_chip *chip = d->host_data;
841
Linus Walleij14250522014-03-25 10:40:18 +0100842 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300843 /*
844 * This lock class tells lockdep that GPIO irqs are in a different
845 * category than their parents, so it won't report false recursion.
846 */
847 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +0200848 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200849 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300850 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200851 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100852 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -0500853
Linus Walleij1333b902014-04-23 16:45:12 +0200854 /*
855 * No set-up of the hardware will happen if IRQ_TYPE_NONE
856 * is passed as default type.
857 */
858 if (chip->irq_default_type != IRQ_TYPE_NONE)
859 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100860
861 return 0;
862}
863
Linus Walleijc3626fd2014-03-28 20:42:01 +0100864static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
865{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200866 struct gpio_chip *chip = d->host_data;
867
Linus Walleij1c8732b2014-04-09 13:34:39 +0200868 if (chip->can_sleep)
869 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100870 irq_set_chip_and_handler(irq, NULL, NULL);
871 irq_set_chip_data(irq, NULL);
872}
873
Linus Walleij14250522014-03-25 10:40:18 +0100874static const struct irq_domain_ops gpiochip_domain_ops = {
875 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100876 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100877 /* Virtually all GPIO irqchips are twocell:ed */
878 .xlate = irq_domain_xlate_twocell,
879};
880
881static int gpiochip_irq_reqres(struct irq_data *d)
882{
883 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
884
Linus Walleijff2b1352015-10-20 11:10:38 +0200885 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300886 return -ENODEV;
887
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900888 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100889 chip_err(chip,
890 "unable to lock HW IRQ %lu for IRQ\n",
891 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200892 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100893 return -EINVAL;
894 }
895 return 0;
896}
897
898static void gpiochip_irq_relres(struct irq_data *d)
899{
900 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
901
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900902 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200903 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100904}
905
906static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
907{
908 return irq_find_mapping(chip->irqdomain, offset);
909}
910
911/**
912 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
913 * @gpiochip: the gpiochip to remove the irqchip from
914 *
915 * This is called only from gpiochip_remove()
916 */
917static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
918{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100919 unsigned int offset;
920
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300921 acpi_gpiochip_free_interrupts(gpiochip);
922
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300923 if (gpiochip->irq_parent) {
924 irq_set_chained_handler(gpiochip->irq_parent, NULL);
925 irq_set_handler_data(gpiochip->irq_parent, NULL);
926 }
927
Linus Walleijc3626fd2014-03-28 20:42:01 +0100928 /* Remove all IRQ mappings and delete the domain */
929 if (gpiochip->irqdomain) {
930 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300931 irq_dispose_mapping(
932 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100933 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100934 }
Linus Walleij14250522014-03-25 10:40:18 +0100935
936 if (gpiochip->irqchip) {
937 gpiochip->irqchip->irq_request_resources = NULL;
938 gpiochip->irqchip->irq_release_resources = NULL;
939 gpiochip->irqchip = NULL;
940 }
941}
942
943/**
944 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
945 * @gpiochip: the gpiochip to add the irqchip to
946 * @irqchip: the irqchip to add to the gpiochip
947 * @first_irq: if not dynamically assigned, the base (first) IRQ to
948 * allocate gpiochip irqs from
949 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200950 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
951 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300952 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +0100953 *
954 * This function closely associates a certain irqchip with a certain
955 * gpiochip, providing an irq domain to translate the local IRQs to
956 * global irqs in the gpiolib core, and making sure that the gpiochip
957 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +0100958 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +0100959 * from the gpiochip passed as chip data. An irqdomain will be stored
960 * in the gpiochip that shall be used by the driver to handle IRQ number
961 * translation. The gpiochip will need to be initialized and registered
962 * before calling this function.
963 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100964 * This function will handle two cell:ed simple IRQs and assumes all
965 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100966 * need to be open coded.
967 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300968int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
969 struct irq_chip *irqchip,
970 unsigned int first_irq,
971 irq_flow_handler_t handler,
972 unsigned int type,
973 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +0100974{
975 struct device_node *of_node;
976 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100977 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100978
979 if (!gpiochip || !irqchip)
980 return -EINVAL;
981
Linus Walleij58383c72015-11-04 09:56:26 +0100982 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +0100983 pr_err("missing gpiochip .dev parent pointer\n");
984 return -EINVAL;
985 }
Linus Walleij58383c72015-11-04 09:56:26 +0100986 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +0100987#ifdef CONFIG_OF_GPIO
988 /*
Colin Cronin20a8a962015-05-18 11:41:43 -0700989 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800990 * FIXME: get rid of this and use gpiochip->parent->of_node
991 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +0100992 */
993 if (gpiochip->of_node)
994 of_node = gpiochip->of_node;
995#endif
996 gpiochip->irqchip = irqchip;
997 gpiochip->irq_handler = handler;
998 gpiochip->irq_default_type = type;
999 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001000 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +01001001 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1002 gpiochip->ngpio, first_irq,
1003 &gpiochip_domain_ops, gpiochip);
1004 if (!gpiochip->irqdomain) {
1005 gpiochip->irqchip = NULL;
1006 return -EINVAL;
1007 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001008
1009 /*
1010 * It is possible for a driver to override this, but only if the
1011 * alternative functions are both implemented.
1012 */
1013 if (!irqchip->irq_request_resources &&
1014 !irqchip->irq_release_resources) {
1015 irqchip->irq_request_resources = gpiochip_irq_reqres;
1016 irqchip->irq_release_resources = gpiochip_irq_relres;
1017 }
Linus Walleij14250522014-03-25 10:40:18 +01001018
1019 /*
1020 * Prepare the mapping since the irqchip shall be orthogonal to
1021 * any gpiochip calls. If the first_irq was zero, this is
1022 * necessary to allocate descriptors for all IRQs.
1023 */
Linus Walleijc3626fd2014-03-28 20:42:01 +01001024 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1025 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
1026 if (offset == 0)
1027 /*
1028 * Store the base into the gpiochip to be used when
1029 * unmapping the irqs.
1030 */
1031 gpiochip->irq_base = irq_base;
1032 }
Linus Walleij14250522014-03-25 10:40:18 +01001033
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001034 acpi_gpiochip_request_interrupts(gpiochip);
1035
Linus Walleij14250522014-03-25 10:40:18 +01001036 return 0;
1037}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001038EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +01001039
1040#else /* CONFIG_GPIOLIB_IRQCHIP */
1041
1042static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
1043
1044#endif /* CONFIG_GPIOLIB_IRQCHIP */
1045
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001046/**
1047 * gpiochip_generic_request() - request the gpio function for a pin
1048 * @chip: the gpiochip owning the GPIO
1049 * @offset: the offset of the GPIO to request for GPIO function
1050 */
1051int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1052{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001053 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001054}
1055EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1056
1057/**
1058 * gpiochip_generic_free() - free the gpio function from a pin
1059 * @chip: the gpiochip to request the gpio function for
1060 * @offset: the offset of the GPIO to free from GPIO function
1061 */
1062void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1063{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001064 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001065}
1066EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1067
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301068#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001069
Linus Walleij3f0f8672012-11-20 12:40:15 +01001070/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001071 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1072 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001073 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001074 * @gpio_offset: the start offset in the current gpio_chip number space
1075 * @pin_group: name of the pin group inside the pin controller
1076 */
1077int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1078 struct pinctrl_dev *pctldev,
1079 unsigned int gpio_offset, const char *pin_group)
1080{
1081 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001082 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001083 int ret;
1084
1085 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1086 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001087 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001088 return -ENOMEM;
1089 }
1090
1091 /* Use local offset as range ID */
1092 pin_range->range.id = gpio_offset;
1093 pin_range->range.gc = chip;
1094 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001095 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001096 pin_range->pctldev = pctldev;
1097
1098 ret = pinctrl_get_group_pins(pctldev, pin_group,
1099 &pin_range->range.pins,
1100 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001101 if (ret < 0) {
1102 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001103 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001104 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001105
1106 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1107
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001108 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1109 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001110 pinctrl_dev_get_devname(pctldev), pin_group);
1111
Linus Walleij20ec3e32016-02-11 11:03:06 +01001112 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001113
1114 return 0;
1115}
1116EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1117
1118/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001119 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1120 * @chip: the gpiochip to add the range for
1121 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001122 * @gpio_offset: the start offset in the current gpio_chip number space
1123 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001124 * @npins: the number of pins from the offset of each pin space (GPIO and
1125 * pin controller) to accumulate in this range
1126 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001127int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001128 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001129 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301130{
1131 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001132 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001133 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301134
Linus Walleij3f0f8672012-11-20 12:40:15 +01001135 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301136 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001137 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001138 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301139 }
1140
Linus Walleij3f0f8672012-11-20 12:40:15 +01001141 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001142 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001143 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301144 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001145 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001146 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301147 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001148 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301149 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001150 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001151 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001152 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001153 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001154 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001155 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001156 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1157 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001158 pinctl_name,
1159 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301160
Linus Walleij20ec3e32016-02-11 11:03:06 +01001161 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001162
1163 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301164}
Linus Walleij165adc92012-11-06 14:49:39 +01001165EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301166
Linus Walleij3f0f8672012-11-20 12:40:15 +01001167/**
1168 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1169 * @chip: the chip to remove all the mappings for
1170 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301171void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1172{
1173 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01001174 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301175
Linus Walleij20ec3e32016-02-11 11:03:06 +01001176 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301177 list_del(&pin_range->node);
1178 pinctrl_remove_gpio_range(pin_range->pctldev,
1179 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001180 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301181 }
1182}
Linus Walleij165adc92012-11-06 14:49:39 +01001183EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1184
1185#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301186
David Brownelld2876d02008-02-04 22:28:20 -08001187/* These "optional" allocation calls help prevent drivers from stomping
1188 * on each other, and help provide better diagnostics in debugfs.
1189 * They're called even less than the "set direction" calls.
1190 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001191static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001192{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001193 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001194 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001195 unsigned long flags;
1196
1197 spin_lock_irqsave(&gpio_lock, flags);
1198
David Brownelld2876d02008-02-04 22:28:20 -08001199 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001200 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001201 */
1202
1203 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1204 desc_set_label(desc, label ? : "?");
1205 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001206 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001207 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001208 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001209 }
1210
1211 if (chip->request) {
1212 /* chip->request may sleep */
1213 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001214 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001215 spin_lock_irqsave(&gpio_lock, flags);
1216
1217 if (status < 0) {
1218 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001219 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001220 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001221 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001222 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001223 if (chip->get_direction) {
1224 /* chip->get_direction may sleep */
1225 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001226 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001227 spin_lock_irqsave(&gpio_lock, flags);
1228 }
David Brownelld2876d02008-02-04 22:28:20 -08001229done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +03001230 if (status < 0) {
1231 /* Clear flags that might have been set by the caller before
1232 * requesting the GPIO.
1233 */
1234 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1235 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1236 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1237 }
Mika Westerberg77c2d792014-03-10 14:54:50 +02001238 spin_unlock_irqrestore(&gpio_lock, flags);
1239 return status;
1240}
1241
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001242/*
1243 * This descriptor validation needs to be inserted verbatim into each
1244 * function taking a descriptor, so we need to use a preprocessor
1245 * macro to avoid endless duplication.
1246 */
1247#define VALIDATE_DESC(desc) do { \
1248 if (!desc || !desc->gdev) { \
1249 pr_warn("%s: invalid GPIO\n", __func__); \
1250 return -EINVAL; \
1251 } \
1252 if ( !desc->gdev->chip ) { \
1253 dev_warn(&desc->gdev->dev, \
1254 "%s: backing chip is gone\n", __func__); \
1255 return 0; \
1256 } } while (0)
1257
1258#define VALIDATE_DESC_VOID(desc) do { \
1259 if (!desc || !desc->gdev) { \
1260 pr_warn("%s: invalid GPIO\n", __func__); \
1261 return; \
1262 } \
1263 if (!desc->gdev->chip) { \
1264 dev_warn(&desc->gdev->dev, \
1265 "%s: backing chip is gone\n", __func__); \
1266 return; \
1267 } } while (0)
1268
1269
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001270int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001271{
1272 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001273 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001274
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001275 VALIDATE_DESC(desc);
1276 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001277
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001278 if (try_module_get(gdev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001279 status = __gpiod_request(desc, label);
1280 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001281 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001282 else
1283 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001284 }
1285
David Brownelld2876d02008-02-04 22:28:20 -08001286 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001287 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001288
David Brownelld2876d02008-02-04 22:28:20 -08001289 return status;
1290}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001291
Mika Westerberg77c2d792014-03-10 14:54:50 +02001292static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001293{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001294 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001295 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001296 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001297
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001298 might_sleep();
1299
Alexandre Courbot372e7222013-02-03 01:29:29 +09001300 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001301
David Brownelld2876d02008-02-04 22:28:20 -08001302 spin_lock_irqsave(&gpio_lock, flags);
1303
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001304 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07001305 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1306 if (chip->free) {
1307 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001308 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001309 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001310 spin_lock_irqsave(&gpio_lock, flags);
1311 }
David Brownelld2876d02008-02-04 22:28:20 -08001312 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001313 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001314 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301315 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301316 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001317 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001318 ret = true;
1319 }
David Brownelld2876d02008-02-04 22:28:20 -08001320
1321 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001322 return ret;
1323}
1324
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001325void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001326{
Linus Walleij33a68e82016-02-11 10:28:44 +01001327 if (desc && desc->gdev && __gpiod_free(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001328 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001329 put_device(&desc->gdev->dev);
1330 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001331 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01001332 }
David Brownelld2876d02008-02-04 22:28:20 -08001333}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001334
David Brownelld2876d02008-02-04 22:28:20 -08001335/**
1336 * gpiochip_is_requested - return string iff signal was requested
1337 * @chip: controller managing the signal
1338 * @offset: of signal within controller's 0..(ngpio - 1) range
1339 *
1340 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001341 * The string returned is the label passed to gpio_request(); if none has been
1342 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001343 *
1344 * This function is for use by GPIO controller drivers. The label can
1345 * help with diagnostics, and knowing that the signal is used as a GPIO
1346 * can help avoid accidentally multiplexing it to another controller.
1347 */
1348const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1349{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001350 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001351
Dirk Behme48b59532015-08-18 18:02:32 +02001352 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001353 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001354
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001355 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001356
Alexandre Courbot372e7222013-02-03 01:29:29 +09001357 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001358 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001359 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001360}
1361EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1362
Mika Westerberg77c2d792014-03-10 14:54:50 +02001363/**
1364 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1365 * @desc: GPIO descriptor to request
1366 * @label: label for the GPIO
1367 *
1368 * Function allows GPIO chip drivers to request and use their own GPIO
1369 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1370 * function will not increase reference count of the GPIO chip module. This
1371 * allows the GPIO chip module to be unloaded as needed (we assume that the
1372 * GPIO chip driver handles freeing the GPIOs it has requested).
1373 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001374struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1375 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001376{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001377 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1378 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001379
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001380 if (IS_ERR(desc)) {
1381 chip_err(chip, "failed to get GPIO descriptor\n");
1382 return desc;
1383 }
1384
1385 err = __gpiod_request(desc, label);
1386 if (err < 0)
1387 return ERR_PTR(err);
1388
1389 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001390}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001391EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001392
1393/**
1394 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1395 * @desc: GPIO descriptor to free
1396 *
1397 * Function frees the given GPIO requested previously with
1398 * gpiochip_request_own_desc().
1399 */
1400void gpiochip_free_own_desc(struct gpio_desc *desc)
1401{
1402 if (desc)
1403 __gpiod_free(desc);
1404}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001405EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08001406
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001407/*
1408 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08001409 * some cases this is done in early boot, before IRQs are enabled.
1410 *
1411 * As a rule these aren't called more than once (except for drivers
1412 * using the open-drain emulation idiom) so these are natural places
1413 * to accumulate extra debugging checks. Note that we can't (yet)
1414 * rely on gpio_request() having been called beforehand.
1415 */
1416
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001417/**
1418 * gpiod_direction_input - set the GPIO direction to input
1419 * @desc: GPIO to set to input
1420 *
1421 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1422 * be called safely on it.
1423 *
1424 * Return 0 in case of success, else an error code.
1425 */
1426int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001427{
David Brownelld2876d02008-02-04 22:28:20 -08001428 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001429 int status = -EINVAL;
1430
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001431 VALIDATE_DESC(desc);
1432 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001433
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001434 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001435 gpiod_warn(desc,
1436 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001437 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001438 return -EIO;
1439 }
1440
Alexandre Courbotd82da792014-07-22 16:17:43 +09001441 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001442 if (status == 0)
1443 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001444
Alexandre Courbot372e7222013-02-03 01:29:29 +09001445 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001446
David Brownelld2876d02008-02-04 22:28:20 -08001447 return status;
1448}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001449EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001450
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001451static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001452{
David Brownelld2876d02008-02-04 22:28:20 -08001453 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001454 int status = -EINVAL;
1455
Linus Walleijd468bf92013-09-24 11:54:38 +02001456 /* GPIOs used for IRQs shall not be set as output */
1457 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1458 gpiod_err(desc,
1459 "%s: tried to set a GPIO tied to an IRQ as output\n",
1460 __func__);
1461 return -EIO;
1462 }
1463
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301464 /* Open drain pin should not be driven to 1 */
1465 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001466 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301467
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301468 /* Open source pin should not be driven to 0 */
1469 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001470 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301471
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001472 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001473 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001474 gpiod_warn(desc,
1475 "%s: missing set() or direction_output() operations\n",
1476 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001477 return -EIO;
1478 }
1479
Alexandre Courbotd82da792014-07-22 16:17:43 +09001480 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001481 if (status == 0)
1482 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001483 trace_gpio_value(desc_to_gpio(desc), 0, value);
1484 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001485 return status;
1486}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001487
1488/**
1489 * gpiod_direction_output_raw - set the GPIO direction to output
1490 * @desc: GPIO to set to output
1491 * @value: initial output value of the GPIO
1492 *
1493 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1494 * be called safely on it. The initial value of the output must be specified
1495 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1496 *
1497 * Return 0 in case of success, else an error code.
1498 */
1499int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1500{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001501 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001502 return _gpiod_direction_output_raw(desc, value);
1503}
1504EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1505
1506/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301507 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001508 * @desc: GPIO to set to output
1509 * @value: initial output value of the GPIO
1510 *
1511 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1512 * be called safely on it. The initial value of the output must be specified
1513 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1514 * account.
1515 *
1516 * Return 0 in case of success, else an error code.
1517 */
1518int gpiod_direction_output(struct gpio_desc *desc, int value)
1519{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001520 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001521 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1522 value = !value;
1523 return _gpiod_direction_output_raw(desc, value);
1524}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001525EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001526
Felipe Balbic4b5be92010-05-26 14:42:23 -07001527/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001528 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001529 * @gpio: the gpio to set debounce time
1530 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001531 *
1532 * returns -ENOTSUPP if the controller does not support setting
1533 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001534 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001535int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001536{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001537 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001538
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001539 VALIDATE_DESC(desc);
1540 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001541 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001542 gpiod_dbg(desc,
1543 "%s: missing set() or set_debounce() operations\n",
1544 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001545 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001546 }
1547
Alexandre Courbotd82da792014-07-22 16:17:43 +09001548 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001549}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001550EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001551
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001552/**
1553 * gpiod_is_active_low - test whether a GPIO is active-low or not
1554 * @desc: the gpio descriptor to test
1555 *
1556 * Returns 1 if the GPIO is active-low, 0 otherwise.
1557 */
1558int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001559{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001560 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001561 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001562}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001563EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001564
1565/* I/O calls are only valid after configuration completed; the relevant
1566 * "is this a valid GPIO" error checks should already have been done.
1567 *
1568 * "Get" operations are often inlinable as reading a pin value register,
1569 * and masking the relevant bit in that register.
1570 *
1571 * When "set" operations are inlinable, they involve writing that mask to
1572 * one register to set a low value, or a different register to set it high.
1573 * Otherwise locking is needed, so there may be little value to inlining.
1574 *
1575 *------------------------------------------------------------------------
1576 *
1577 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1578 * have requested the GPIO. That can include implicit requesting by
1579 * a direction setting call. Marking a gpio as requested locks its chip
1580 * in memory, guaranteeing that these table lookups need no more locking
1581 * and that gpiochip_remove() will fail.
1582 *
1583 * REVISIT when debugging, consider adding some instrumentation to ensure
1584 * that the GPIO was actually requested.
1585 */
1586
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001587static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001588{
1589 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001590 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001591 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001592
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001593 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001594 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001595 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01001596 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001597 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001598 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001599}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001600
David Brownelld2876d02008-02-04 22:28:20 -08001601/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001602 * gpiod_get_raw_value() - return a gpio's raw value
1603 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001604 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001605 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001606 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001607 *
1608 * This function should be called from contexts where we cannot sleep, and will
1609 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001610 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001611int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001612{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001613 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001614 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001615 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001616 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001617}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001618EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001619
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001620/**
1621 * gpiod_get_value() - return a gpio's value
1622 * @desc: gpio whose value will be returned
1623 *
1624 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001625 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001626 *
1627 * This function should be called from contexts where we cannot sleep, and will
1628 * complain if the GPIO chip functions potentially sleep.
1629 */
1630int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001631{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001632 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001633
1634 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001635 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001636 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001637
1638 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001639 if (value < 0)
1640 return value;
1641
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001642 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1643 value = !value;
1644
1645 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001646}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001647EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001648
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301649/*
1650 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001651 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001652 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301653 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001654static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301655{
1656 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001657 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001658 int offset = gpio_chip_hwgpio(desc);
1659
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301660 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001661 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301662 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001663 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301664 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001665 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301666 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001667 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301668 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001669 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301670 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001671 gpiod_err(desc,
1672 "%s: Error in set_value for open drain err %d\n",
1673 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301674}
1675
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301676/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001677 * _gpio_set_open_source_value() - Set the open source gpio's value.
1678 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001679 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301680 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001681static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301682{
1683 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001684 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001685 int offset = gpio_chip_hwgpio(desc);
1686
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301687 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001688 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301689 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001690 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301691 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001692 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301693 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001694 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301695 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001696 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301697 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001698 gpiod_err(desc,
1699 "%s: Error in set_value for open source err %d\n",
1700 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301701}
1702
Alexandre Courbot23600962014-03-11 15:52:09 +09001703static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001704{
1705 struct gpio_chip *chip;
1706
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001707 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001708 trace_gpio_value(desc_to_gpio(desc), 0, value);
1709 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1710 _gpio_set_open_drain_value(desc, value);
1711 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1712 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301713 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001714 chip->set(chip, gpio_chip_hwgpio(desc), value);
1715}
1716
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001717/*
1718 * set multiple outputs on the same chip;
1719 * use the chip's set_multiple function if available;
1720 * otherwise set the outputs sequentially;
1721 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1722 * defines which outputs are to be changed
1723 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1724 * defines the values the outputs specified by mask are to be set to
1725 */
1726static void gpio_chip_set_multiple(struct gpio_chip *chip,
1727 unsigned long *mask, unsigned long *bits)
1728{
1729 if (chip->set_multiple) {
1730 chip->set_multiple(chip, mask, bits);
1731 } else {
1732 int i;
1733 for (i = 0; i < chip->ngpio; i++) {
1734 if (mask[BIT_WORD(i)] == 0) {
1735 /* no more set bits in this mask word;
1736 * skip ahead to the next word */
1737 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1738 continue;
1739 }
1740 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001741 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001742 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001743 }
1744 }
1745}
1746
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001747static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1748 unsigned int array_size,
1749 struct gpio_desc **desc_array,
1750 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001751{
1752 int i = 0;
1753
1754 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001755 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001756 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1757 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1758 int count = 0;
1759
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001760 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001761 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001762
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001763 memset(mask, 0, sizeof(mask));
1764 do {
1765 struct gpio_desc *desc = desc_array[i];
1766 int hwgpio = gpio_chip_hwgpio(desc);
1767 int value = value_array[i];
1768
1769 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1770 value = !value;
1771 trace_gpio_value(desc_to_gpio(desc), 0, value);
1772 /*
1773 * collect all normal outputs belonging to the same chip
1774 * open drain and open source outputs are set individually
1775 */
1776 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001777 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001778 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1779 _gpio_set_open_source_value(desc, value);
1780 } else {
1781 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001782 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001783 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001784 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001785 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001786 count++;
1787 }
1788 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001789 } while ((i < array_size) &&
1790 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001791 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001792 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001793 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001794 }
1795}
1796
David Brownelld2876d02008-02-04 22:28:20 -08001797/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001798 * gpiod_set_raw_value() - assign a gpio's raw value
1799 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001800 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001801 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001802 * Set the raw value of the GPIO, i.e. the value of its physical line without
1803 * regard for its ACTIVE_LOW status.
1804 *
1805 * This function should be called from contexts where we cannot sleep, and will
1806 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001807 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001808void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001809{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001810 VALIDATE_DESC_VOID(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001811 /* Should be using gpio_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001812 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001813 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001814}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001815EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001816
1817/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001818 * gpiod_set_value() - assign a gpio's value
1819 * @desc: gpio whose value will be assigned
1820 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001821 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001822 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1823 * account
1824 *
1825 * This function should be called from contexts where we cannot sleep, and will
1826 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001827 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001828void gpiod_set_value(struct gpio_desc *desc, int value)
1829{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001830 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001831 /* Should be using gpio_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001832 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001833 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1834 value = !value;
1835 _gpiod_set_raw_value(desc, value);
1836}
1837EXPORT_SYMBOL_GPL(gpiod_set_value);
1838
1839/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001840 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001841 * @array_size: number of elements in the descriptor / value arrays
1842 * @desc_array: array of GPIO descriptors whose values will be assigned
1843 * @value_array: array of values to assign
1844 *
1845 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1846 * without regard for their ACTIVE_LOW status.
1847 *
1848 * This function should be called from contexts where we cannot sleep, and will
1849 * complain if the GPIO chip functions potentially sleep.
1850 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001851void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001852 struct gpio_desc **desc_array, int *value_array)
1853{
1854 if (!desc_array)
1855 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001856 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1857 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001858}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001859EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001860
1861/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001862 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001863 * @array_size: number of elements in the descriptor / value arrays
1864 * @desc_array: array of GPIO descriptors whose values will be assigned
1865 * @value_array: array of values to assign
1866 *
1867 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1868 * into account.
1869 *
1870 * This function should be called from contexts where we cannot sleep, and will
1871 * complain if the GPIO chip functions potentially sleep.
1872 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001873void gpiod_set_array_value(unsigned int array_size,
1874 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001875{
1876 if (!desc_array)
1877 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001878 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1879 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001880}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001881EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001882
1883/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001884 * gpiod_cansleep() - report whether gpio value access may sleep
1885 * @desc: gpio to check
1886 *
1887 */
1888int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001889{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001890 VALIDATE_DESC(desc);
1891 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001892}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001893EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001894
David Brownell0f6d5042008-10-15 22:03:14 -07001895/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001896 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1897 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001898 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001899 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1900 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001901 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001902int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001903{
1904 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001905 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001906
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001907 VALIDATE_DESC(desc);
1908 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001909 offset = gpio_chip_hwgpio(desc);
1910 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1911}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001912EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001913
Linus Walleijd468bf92013-09-24 11:54:38 +02001914/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001915 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001916 * @chip: the chip the GPIO to lock belongs to
1917 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001918 *
1919 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001920 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001921 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001922int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001923{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001924 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001925 return -EINVAL;
1926
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001927 if (test_bit(FLAG_IS_OUT, &chip->gpiodev->descs[offset].flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001928 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001929 "%s: tried to flag a GPIO set as output for IRQ\n",
1930 __func__);
1931 return -EIO;
1932 }
1933
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001934 set_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001935 return 0;
1936}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001937EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001938
Linus Walleijd468bf92013-09-24 11:54:38 +02001939/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001940 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001941 * @chip: the chip the GPIO to lock belongs to
1942 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001943 *
1944 * This is used directly by GPIO drivers that want to indicate
1945 * that a certain GPIO is no longer used exclusively for IRQ.
1946 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001947void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001948{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001949 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001950 return;
1951
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001952 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001953}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001954EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001955
Linus Walleij6cee3822016-02-11 20:16:45 +01001956bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
1957{
1958 if (offset >= chip->ngpio)
1959 return false;
1960
1961 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
1962}
1963EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
1964
Linus Walleij143b65d2016-02-16 15:41:42 +01001965bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
1966{
1967 if (offset >= chip->ngpio)
1968 return false;
1969
1970 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
1971}
1972EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
1973
1974bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
1975{
1976 if (offset >= chip->ngpio)
1977 return false;
1978
1979 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
1980}
1981EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
1982
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001983/**
1984 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1985 * @desc: gpio whose value will be returned
1986 *
1987 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001988 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001989 *
1990 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001991 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001992int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001993{
David Brownelld2876d02008-02-04 22:28:20 -08001994 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001995 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001996 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001997}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001998EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001999
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002000/**
2001 * gpiod_get_value_cansleep() - return a gpio's value
2002 * @desc: gpio whose value will be returned
2003 *
2004 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002005 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002006 *
2007 * This function is to be called from contexts that can sleep.
2008 */
2009int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002010{
David Brownelld2876d02008-02-04 22:28:20 -08002011 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002012
2013 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002014 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002015 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002016 if (value < 0)
2017 return value;
2018
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002019 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2020 value = !value;
2021
David Brownelld2876d02008-02-04 22:28:20 -08002022 return value;
2023}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002024EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002025
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002026/**
2027 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2028 * @desc: gpio whose value will be assigned
2029 * @value: value to assign
2030 *
2031 * Set the raw value of the GPIO, i.e. the value of its physical line without
2032 * regard for its ACTIVE_LOW status.
2033 *
2034 * This function is to be called from contexts that can sleep.
2035 */
2036void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002037{
David Brownelld2876d02008-02-04 22:28:20 -08002038 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002039 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002040 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002041}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002042EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002043
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002044/**
2045 * gpiod_set_value_cansleep() - assign a gpio's value
2046 * @desc: gpio whose value will be assigned
2047 * @value: value to assign
2048 *
2049 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2050 * account
2051 *
2052 * This function is to be called from contexts that can sleep.
2053 */
2054void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002055{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002056 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002057 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002058 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2059 value = !value;
2060 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002061}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002062EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002063
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002064/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002065 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002066 * @array_size: number of elements in the descriptor / value arrays
2067 * @desc_array: array of GPIO descriptors whose values will be assigned
2068 * @value_array: array of values to assign
2069 *
2070 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2071 * without regard for their ACTIVE_LOW status.
2072 *
2073 * This function is to be called from contexts that can sleep.
2074 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002075void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2076 struct gpio_desc **desc_array,
2077 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002078{
2079 might_sleep_if(extra_checks);
2080 if (!desc_array)
2081 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002082 gpiod_set_array_value_priv(true, true, array_size, desc_array,
2083 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002084}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002085EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002086
2087/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002088 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002089 * @array_size: number of elements in the descriptor / value arrays
2090 * @desc_array: array of GPIO descriptors whose values will be assigned
2091 * @value_array: array of values to assign
2092 *
2093 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2094 * into account.
2095 *
2096 * This function is to be called from contexts that can sleep.
2097 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002098void gpiod_set_array_value_cansleep(unsigned int array_size,
2099 struct gpio_desc **desc_array,
2100 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002101{
2102 might_sleep_if(extra_checks);
2103 if (!desc_array)
2104 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002105 gpiod_set_array_value_priv(false, true, array_size, desc_array,
2106 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002107}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002108EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002109
2110/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002111 * gpiod_add_lookup_table() - register GPIO device consumers
2112 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002113 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002114void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002115{
2116 mutex_lock(&gpio_lookup_lock);
2117
Alexandre Courbotad824782013-12-03 12:20:11 +09002118 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002119
2120 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002121}
2122
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302123/**
2124 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2125 * @table: table of consumers to unregister
2126 */
2127void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2128{
2129 mutex_lock(&gpio_lookup_lock);
2130
2131 list_del(&table->list);
2132
2133 mutex_unlock(&gpio_lookup_lock);
2134}
2135
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002136static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002137 unsigned int idx,
2138 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002139{
2140 char prop_name[32]; /* 32 is max size of property name */
2141 enum of_gpio_flags of_flags;
2142 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02002143 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002144
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002145 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02002146 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01002147 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002148 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02002149 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01002150 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002151 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002152
Thierry Redingdd34c372014-04-23 17:28:09 +02002153 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2154 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07002155 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02002156 break;
2157 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002158
2159 if (IS_ERR(desc))
2160 return desc;
2161
2162 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002163 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002164
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002165 if (of_flags & OF_GPIO_SINGLE_ENDED) {
2166 if (of_flags & OF_GPIO_ACTIVE_LOW)
2167 *flags |= GPIO_OPEN_DRAIN;
2168 else
2169 *flags |= GPIO_OPEN_SOURCE;
2170 }
2171
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002172 return desc;
2173}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002174
Mika Westerberg81f59e92013-10-10 11:01:09 +03002175static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002176 unsigned int idx,
2177 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03002178{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002179 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03002180 struct acpi_gpio_info info;
2181 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002182 char propname[32];
2183 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03002184
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002185 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002186 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002187 if (con_id && strcmp(con_id, "gpios")) {
2188 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002189 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002190 } else {
2191 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002192 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002193 }
Mika Westerberge01f4402013-10-10 11:01:10 +03002194
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002195 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
2196 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2197 break;
2198 }
2199
2200 /* Then from plain _CRS GPIOs */
2201 if (IS_ERR(desc)) {
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08002202 if (!acpi_can_fallback_to_crs(adev, con_id))
2203 return ERR_PTR(-ENOENT);
2204
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002205 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2206 if (IS_ERR(desc))
2207 return desc;
2208 }
2209
Christophe RICARD52044722015-12-23 23:25:34 +01002210 if (info.polarity == GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002211 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002212
2213 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002214}
2215
Alexandre Courbotad824782013-12-03 12:20:11 +09002216static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2217{
2218 const char *dev_id = dev ? dev_name(dev) : NULL;
2219 struct gpiod_lookup_table *table;
2220
2221 mutex_lock(&gpio_lookup_lock);
2222
2223 list_for_each_entry(table, &gpio_lookup_list, list) {
2224 if (table->dev_id && dev_id) {
2225 /*
2226 * Valid strings on both ends, must be identical to have
2227 * a match
2228 */
2229 if (!strcmp(table->dev_id, dev_id))
2230 goto found;
2231 } else {
2232 /*
2233 * One of the pointers is NULL, so both must be to have
2234 * a match
2235 */
2236 if (dev_id == table->dev_id)
2237 goto found;
2238 }
2239 }
2240 table = NULL;
2241
2242found:
2243 mutex_unlock(&gpio_lookup_lock);
2244 return table;
2245}
2246
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002247static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002248 unsigned int idx,
2249 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002250{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002251 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002252 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002253 struct gpiod_lookup *p;
2254
Alexandre Courbotad824782013-12-03 12:20:11 +09002255 table = gpiod_find_lookup_table(dev);
2256 if (!table)
2257 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002258
Alexandre Courbotad824782013-12-03 12:20:11 +09002259 for (p = &table->table[0]; p->chip_label; p++) {
2260 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002261
Alexandre Courbotad824782013-12-03 12:20:11 +09002262 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002263 if (p->idx != idx)
2264 continue;
2265
Alexandre Courbotad824782013-12-03 12:20:11 +09002266 /* If the lookup entry has a con_id, require exact match */
2267 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2268 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002269
Alexandre Courbotad824782013-12-03 12:20:11 +09002270 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002271
Alexandre Courbotad824782013-12-03 12:20:11 +09002272 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002273 dev_err(dev, "cannot find GPIO chip %s\n",
2274 p->chip_label);
2275 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002276 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002277
Alexandre Courbotad824782013-12-03 12:20:11 +09002278 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002279 dev_err(dev,
2280 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2281 idx, chip->ngpio, chip->label);
2282 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09002283 }
2284
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09002285 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09002286 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002287
2288 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09002289 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002290
2291 return desc;
2292}
2293
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002294static int dt_gpio_count(struct device *dev, const char *con_id)
2295{
2296 int ret;
2297 char propname[32];
2298 unsigned int i;
2299
2300 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2301 if (con_id)
2302 snprintf(propname, sizeof(propname), "%s-%s",
2303 con_id, gpio_suffixes[i]);
2304 else
2305 snprintf(propname, sizeof(propname), "%s",
2306 gpio_suffixes[i]);
2307
2308 ret = of_gpio_named_count(dev->of_node, propname);
2309 if (ret >= 0)
2310 break;
2311 }
2312 return ret;
2313}
2314
2315static int platform_gpio_count(struct device *dev, const char *con_id)
2316{
2317 struct gpiod_lookup_table *table;
2318 struct gpiod_lookup *p;
2319 unsigned int count = 0;
2320
2321 table = gpiod_find_lookup_table(dev);
2322 if (!table)
2323 return -ENOENT;
2324
2325 for (p = &table->table[0]; p->chip_label; p++) {
2326 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2327 (!con_id && !p->con_id))
2328 count++;
2329 }
2330 if (!count)
2331 return -ENOENT;
2332
2333 return count;
2334}
2335
2336/**
2337 * gpiod_count - return the number of GPIOs associated with a device / function
2338 * or -ENOENT if no GPIO has been assigned to the requested function
2339 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2340 * @con_id: function within the GPIO consumer
2341 */
2342int gpiod_count(struct device *dev, const char *con_id)
2343{
2344 int count = -ENOENT;
2345
2346 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2347 count = dt_gpio_count(dev, con_id);
2348 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2349 count = acpi_gpio_count(dev, con_id);
2350
2351 if (count < 0)
2352 count = platform_gpio_count(dev, con_id);
2353
2354 return count;
2355}
2356EXPORT_SYMBOL_GPL(gpiod_count);
2357
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002358/**
Thierry Reding08791622014-04-25 16:54:22 +02002359 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002360 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002361 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002362 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002363 *
2364 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002365 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07002366 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002367 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002368struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002369 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002370{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002371 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002372}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002373EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002374
2375/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002376 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2377 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2378 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002379 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002380 *
2381 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2382 * the requested function it will return NULL. This is convenient for drivers
2383 * that need to handle optional GPIOs.
2384 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002385struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002386 const char *con_id,
2387 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002388{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002389 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002390}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002391EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002392
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002393/**
2394 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2395 * @desc: gpio to be setup
2396 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2397 * of_get_gpio_hog()
2398 *
2399 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2400 */
2401static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2402{
2403 if (lflags & GPIO_ACTIVE_LOW)
2404 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2405 if (lflags & GPIO_OPEN_DRAIN)
2406 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2407 if (lflags & GPIO_OPEN_SOURCE)
2408 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2409}
Benoit Parrotf625d462015-02-02 11:44:44 -06002410
2411/**
2412 * gpiod_configure_flags - helper function to configure a given GPIO
2413 * @desc: gpio whose value will be assigned
2414 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06002415 * @dflags: gpiod_flags - optional GPIO initialization flags
2416 *
2417 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2418 * requested function and/or index, or another IS_ERR() code if an error
2419 * occurred while trying to acquire the GPIO.
2420 */
2421static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002422 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06002423{
2424 int status;
2425
Benoit Parrotf625d462015-02-02 11:44:44 -06002426 /* No particular flag request, return here... */
2427 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2428 pr_debug("no flags found for %s\n", con_id);
2429 return 0;
2430 }
2431
2432 /* Process flags */
2433 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2434 status = gpiod_direction_output(desc,
2435 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2436 else
2437 status = gpiod_direction_input(desc);
2438
2439 return status;
2440}
2441
Thierry Reding29a1f2332014-04-25 17:10:06 +02002442/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002443 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002444 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002445 * @con_id: function within the GPIO consumer
2446 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002447 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002448 *
2449 * This variant of gpiod_get() allows to access GPIOs other than the first
2450 * defined one for functions that define several GPIOs.
2451 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002452 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2453 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07002454 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002455 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002456struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002457 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002458 unsigned int idx,
2459 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002460{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002461 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002462 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002463 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002464
2465 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2466
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002467 if (dev) {
2468 /* Using device tree? */
2469 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2470 dev_dbg(dev, "using device tree for GPIO lookup\n");
2471 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2472 } else if (ACPI_COMPANION(dev)) {
2473 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2474 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2475 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002476 }
2477
2478 /*
2479 * Either we are not using DT or ACPI, or their lookup did not return
2480 * a result. In that case, use platform lookup as a fallback.
2481 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002482 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002483 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002484 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002485 }
2486
2487 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002488 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002489 return desc;
2490 }
2491
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002492 gpiod_parse_flags(desc, lookupflags);
2493
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002494 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002495 if (status < 0)
2496 return ERR_PTR(status);
2497
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002498 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002499 if (status < 0) {
2500 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2501 gpiod_put(desc);
2502 return ERR_PTR(status);
2503 }
2504
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002505 return desc;
2506}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002507EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002508
2509/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002510 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2511 * @fwnode: handle of the firmware node
2512 * @propname: name of the firmware property representing the GPIO
2513 *
2514 * This function can be used for drivers that get their configuration
2515 * from firmware.
2516 *
2517 * Function properly finds the corresponding GPIO using whatever is the
2518 * underlying firmware interface and then makes sure that the GPIO
2519 * descriptor is requested before it is returned to the caller.
2520 *
2521 * In case of error an ERR_PTR() is returned.
2522 */
2523struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2524 const char *propname)
2525{
2526 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2527 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002528 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02002529 int ret;
2530
2531 if (!fwnode)
2532 return ERR_PTR(-EINVAL);
2533
2534 if (is_of_node(fwnode)) {
2535 enum of_gpio_flags flags;
2536
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002537 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002538 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002539 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02002540 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002541 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2542 }
Mika Westerberg40b73182014-10-21 13:33:59 +02002543 } else if (is_acpi_node(fwnode)) {
2544 struct acpi_gpio_info info;
2545
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02002546 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02002547 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01002548 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02002549 }
2550
2551 if (IS_ERR(desc))
2552 return desc;
2553
Mika Westerberg40b73182014-10-21 13:33:59 +02002554 if (active_low)
2555 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2556
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002557 if (single_ended) {
2558 if (active_low)
2559 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2560 else
2561 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2562 }
2563
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002564 ret = gpiod_request(desc, NULL);
2565 if (ret)
2566 return ERR_PTR(ret);
2567
Mika Westerberg40b73182014-10-21 13:33:59 +02002568 return desc;
2569}
2570EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2571
2572/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002573 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2574 * function
2575 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2576 * @con_id: function within the GPIO consumer
2577 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002578 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002579 *
2580 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2581 * specified index was assigned to the requested function it will return NULL.
2582 * This is convenient for drivers that need to handle optional GPIOs.
2583 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002584struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002585 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002586 unsigned int index,
2587 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002588{
2589 struct gpio_desc *desc;
2590
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002591 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002592 if (IS_ERR(desc)) {
2593 if (PTR_ERR(desc) == -ENOENT)
2594 return NULL;
2595 }
2596
2597 return desc;
2598}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002599EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002600
2601/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002602 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2603 * @desc: gpio whose value will be assigned
2604 * @name: gpio line name
2605 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2606 * of_get_gpio_hog()
2607 * @dflags: gpiod_flags - optional GPIO initialization flags
2608 */
2609int gpiod_hog(struct gpio_desc *desc, const char *name,
2610 unsigned long lflags, enum gpiod_flags dflags)
2611{
2612 struct gpio_chip *chip;
2613 struct gpio_desc *local_desc;
2614 int hwnum;
2615 int status;
2616
2617 chip = gpiod_to_chip(desc);
2618 hwnum = gpio_chip_hwgpio(desc);
2619
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002620 gpiod_parse_flags(desc, lflags);
2621
Benoit Parrotf625d462015-02-02 11:44:44 -06002622 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2623 if (IS_ERR(local_desc)) {
Linus Walleija7138902015-06-05 11:36:10 +02002624 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2625 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002626 return PTR_ERR(local_desc);
2627 }
2628
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002629 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002630 if (status < 0) {
Linus Walleija7138902015-06-05 11:36:10 +02002631 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2632 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002633 gpiochip_free_own_desc(desc);
2634 return status;
2635 }
2636
2637 /* Mark GPIO as hogged so it can be identified and removed later */
2638 set_bit(FLAG_IS_HOGGED, &desc->flags);
2639
2640 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2641 desc_to_gpio(desc), name,
2642 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2643 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2644 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2645
2646 return 0;
2647}
2648
2649/**
2650 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2651 * @chip: gpio chip to act on
2652 *
2653 * This is only used by of_gpiochip_remove to free hogged gpios
2654 */
2655static void gpiochip_free_hogs(struct gpio_chip *chip)
2656{
2657 int id;
2658
2659 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002660 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
2661 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06002662 }
2663}
2664
2665/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002666 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2667 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2668 * @con_id: function within the GPIO consumer
2669 * @flags: optional GPIO initialization flags
2670 *
2671 * This function acquires all the GPIOs defined under a given function.
2672 *
2673 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2674 * no GPIO has been assigned to the requested function, or another IS_ERR()
2675 * code if an error occurred while trying to acquire the GPIOs.
2676 */
2677struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2678 const char *con_id,
2679 enum gpiod_flags flags)
2680{
2681 struct gpio_desc *desc;
2682 struct gpio_descs *descs;
2683 int count;
2684
2685 count = gpiod_count(dev, con_id);
2686 if (count < 0)
2687 return ERR_PTR(count);
2688
2689 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2690 GFP_KERNEL);
2691 if (!descs)
2692 return ERR_PTR(-ENOMEM);
2693
2694 for (descs->ndescs = 0; descs->ndescs < count; ) {
2695 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2696 if (IS_ERR(desc)) {
2697 gpiod_put_array(descs);
2698 return ERR_CAST(desc);
2699 }
2700 descs->desc[descs->ndescs] = desc;
2701 descs->ndescs++;
2702 }
2703 return descs;
2704}
2705EXPORT_SYMBOL_GPL(gpiod_get_array);
2706
2707/**
2708 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2709 * function
2710 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2711 * @con_id: function within the GPIO consumer
2712 * @flags: optional GPIO initialization flags
2713 *
2714 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2715 * assigned to the requested function it will return NULL.
2716 */
2717struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2718 const char *con_id,
2719 enum gpiod_flags flags)
2720{
2721 struct gpio_descs *descs;
2722
2723 descs = gpiod_get_array(dev, con_id, flags);
2724 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2725 return NULL;
2726
2727 return descs;
2728}
2729EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2730
2731/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002732 * gpiod_put - dispose of a GPIO descriptor
2733 * @desc: GPIO descriptor to dispose of
2734 *
2735 * No descriptor can be used after gpiod_put() has been called on it.
2736 */
2737void gpiod_put(struct gpio_desc *desc)
2738{
2739 gpiod_free(desc);
2740}
2741EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002742
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002743/**
2744 * gpiod_put_array - dispose of multiple GPIO descriptors
2745 * @descs: struct gpio_descs containing an array of descriptors
2746 */
2747void gpiod_put_array(struct gpio_descs *descs)
2748{
2749 unsigned int i;
2750
2751 for (i = 0; i < descs->ndescs; i++)
2752 gpiod_put(descs->desc[i]);
2753
2754 kfree(descs);
2755}
2756EXPORT_SYMBOL_GPL(gpiod_put_array);
2757
Linus Walleij3c702e92015-10-21 15:29:53 +02002758static int __init gpiolib_dev_init(void)
2759{
2760 int ret;
2761
2762 /* Register GPIO sysfs bus */
2763 ret = bus_register(&gpio_bus_type);
2764 if (ret < 0) {
2765 pr_err("gpiolib: could not register GPIO bus type\n");
2766 return ret;
2767 }
2768
2769 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
2770 if (ret < 0) {
2771 pr_err("gpiolib: failed to allocate char dev region\n");
2772 bus_unregister(&gpio_bus_type);
2773 }
2774 return ret;
2775}
2776core_initcall(gpiolib_dev_init);
2777
David Brownelld2876d02008-02-04 22:28:20 -08002778#ifdef CONFIG_DEBUG_FS
2779
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002780static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08002781{
2782 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002783 struct gpio_chip *chip = gdev->chip;
2784 unsigned gpio = gdev->base;
2785 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08002786 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002787 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002788
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002789 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02002790 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2791 if (gdesc->name) {
2792 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2793 gpio, gdesc->name);
2794 }
David Brownelld2876d02008-02-04 22:28:20 -08002795 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02002796 }
David Brownelld2876d02008-02-04 22:28:20 -08002797
Alexandre Courbot372e7222013-02-03 01:29:29 +09002798 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002799 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002800 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02002801 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2802 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08002803 is_out ? "out" : "in ",
2804 chip->get
2805 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002806 : "? ",
2807 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002808 seq_printf(s, "\n");
2809 }
2810}
2811
Thierry Redingf9c4a312012-04-12 13:26:01 +02002812static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002813{
Grant Likely362432a2013-02-09 09:41:49 +00002814 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002815 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002816 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002817
2818 s->private = "";
2819
Grant Likely362432a2013-02-09 09:41:49 +00002820 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002821 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00002822 if (index-- == 0) {
2823 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002824 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00002825 }
2826 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002827
2828 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002829}
2830
2831static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2832{
Grant Likely362432a2013-02-09 09:41:49 +00002833 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002834 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002835 void *ret = NULL;
2836
Grant Likely362432a2013-02-09 09:41:49 +00002837 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002838 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002839 ret = NULL;
2840 else
Linus Walleijff2b1352015-10-20 11:10:38 +02002841 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00002842 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002843
2844 s->private = "\n";
2845 ++*pos;
2846
2847 return ret;
2848}
2849
2850static void gpiolib_seq_stop(struct seq_file *s, void *v)
2851{
2852}
2853
2854static int gpiolib_seq_show(struct seq_file *s, void *v)
2855{
Linus Walleijff2b1352015-10-20 11:10:38 +02002856 struct gpio_device *gdev = v;
2857 struct gpio_chip *chip = gdev->chip;
2858 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002859
Linus Walleijff2b1352015-10-20 11:10:38 +02002860 if (!chip) {
2861 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
2862 dev_name(&gdev->dev));
2863 return 0;
2864 }
2865
2866 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
2867 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002868 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02002869 parent = chip->parent;
2870 if (parent)
2871 seq_printf(s, ", parent: %s/%s",
2872 parent->bus ? parent->bus->name : "no-bus",
2873 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02002874 if (chip->label)
2875 seq_printf(s, ", %s", chip->label);
2876 if (chip->can_sleep)
2877 seq_printf(s, ", can sleep");
2878 seq_printf(s, ":\n");
2879
2880 if (chip->dbg_show)
2881 chip->dbg_show(s, chip);
2882 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002883 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002884
David Brownelld2876d02008-02-04 22:28:20 -08002885 return 0;
2886}
2887
Thierry Redingf9c4a312012-04-12 13:26:01 +02002888static const struct seq_operations gpiolib_seq_ops = {
2889 .start = gpiolib_seq_start,
2890 .next = gpiolib_seq_next,
2891 .stop = gpiolib_seq_stop,
2892 .show = gpiolib_seq_show,
2893};
2894
David Brownelld2876d02008-02-04 22:28:20 -08002895static int gpiolib_open(struct inode *inode, struct file *file)
2896{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002897 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002898}
2899
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002900static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002901 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002902 .open = gpiolib_open,
2903 .read = seq_read,
2904 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002905 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002906};
2907
2908static int __init gpiolib_debugfs_init(void)
2909{
2910 /* /sys/kernel/debug/gpio */
2911 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2912 NULL, NULL, &gpiolib_operations);
2913 return 0;
2914}
2915subsys_initcall(gpiolib_debugfs_init);
2916
2917#endif /* DEBUG_FS */