blob: b816469823755ebccc0a629bd3d6c8b186338782 [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
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 Walleijf881bab02015-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 Walleijf881bab02015-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 Walleijf881bab02015-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 Walleijf881bab02015-09-23 16:20:43 -0700278
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100279 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700280 continue;
281
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100282 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700283 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100284 return desc;
Linus Walleijf881bab02015-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 Walleijf881bab02015-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 Walleijf881bab02015-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;
383 if (desc->flags & (FLAG_REQUESTED | FLAG_IS_HOGGED |
384 FLAG_USED_AS_IRQ | FLAG_EXPORT |
385 FLAG_SYSFS))
386 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
387 if (desc->flags & FLAG_IS_OUT)
388 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
389 if (desc->flags & FLAG_ACTIVE_LOW)
390 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
391 if (desc->flags & FLAG_OPEN_DRAIN)
392 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
393 if (desc->flags & FLAG_OPEN_SOURCE)
394 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
395
396 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
397 return -EFAULT;
398 return 0;
Linus Walleij3c702e92015-10-21 15:29:53 +0200399 }
400 return -EINVAL;
401}
402
403/**
404 * gpio_chrdev_open() - open the chardev for ioctl operations
405 * @inode: inode for this chardev
406 * @filp: file struct for storing private data
407 * Returns 0 on success
408 */
409static int gpio_chrdev_open(struct inode *inode, struct file *filp)
410{
411 struct gpio_device *gdev = container_of(inode->i_cdev,
412 struct gpio_device, chrdev);
413
414 /* Fail on open if the backing gpiochip is gone */
415 if (!gdev || !gdev->chip)
416 return -ENODEV;
417 get_device(&gdev->dev);
418 filp->private_data = gdev;
419 return 0;
420}
421
422/**
423 * gpio_chrdev_release() - close chardev after ioctl operations
424 * @inode: inode for this chardev
425 * @filp: file struct for storing private data
426 * Returns 0 on success
427 */
428static int gpio_chrdev_release(struct inode *inode, struct file *filp)
429{
430 struct gpio_device *gdev = container_of(inode->i_cdev,
431 struct gpio_device, chrdev);
432
433 if (!gdev)
434 return -ENODEV;
435 put_device(&gdev->dev);
436 return 0;
437}
438
439
440static const struct file_operations gpio_fileops = {
441 .release = gpio_chrdev_release,
442 .open = gpio_chrdev_open,
443 .owner = THIS_MODULE,
444 .llseek = noop_llseek,
445 .unlocked_ioctl = gpio_ioctl,
446 .compat_ioctl = gpio_ioctl,
447};
448
Linus Walleijff2b1352015-10-20 11:10:38 +0200449static void gpiodevice_release(struct device *dev)
450{
451 struct gpio_device *gdev = dev_get_drvdata(dev);
452
Linus Walleij3c702e92015-10-21 15:29:53 +0200453 cdev_del(&gdev->chrdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200454 list_del(&gdev->list);
455 ida_simple_remove(&gpio_ida, gdev->id);
Linus Walleij9efd9e62016-02-09 14:27:42 +0100456 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200457}
458
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700459/**
Linus Walleijb08ea352015-12-03 15:14:13 +0100460 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -0800461 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900462 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -0800463 *
464 * Returns a negative errno if the chip can't be registered, such as
465 * because the chip->base is invalid or already associated with a
466 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700467 *
Linus Walleijb08ea352015-12-03 15:14:13 +0100468 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800469 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d82008-07-25 01:46:07 -0700470 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
471 * for GPIOs will fail rudely.
472 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700473 * If chip->base is negative, this requests dynamic assignment of
474 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800475 */
Linus Walleijb08ea352015-12-03 15:14:13 +0100476int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -0800477{
478 unsigned long flags;
479 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +0200480 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700481 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +0200482 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -0800483
Linus Walleijff2b1352015-10-20 11:10:38 +0200484 /*
485 * First: allocate and populate the internal stat container, and
486 * set up the struct device.
487 */
Josh Cartwright969f07b2016-02-17 16:44:15 -0600488 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +0200489 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900490 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +0200491 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +0200492 gdev->chip = chip;
493 chip->gpiodev = gdev;
494 if (chip->parent) {
495 gdev->dev.parent = chip->parent;
496 gdev->dev.of_node = chip->parent->of_node;
497 } else {
498#ifdef CONFIG_OF_GPIO
499 /* If the gpiochip has an assigned OF node this takes precedence */
500 if (chip->of_node)
501 gdev->dev.of_node = chip->of_node;
502#endif
503 }
504 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
505 if (gdev->id < 0) {
506 status = gdev->id;
507 goto err_free_gdev;
508 }
509 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
510 device_initialize(&gdev->dev);
511 dev_set_drvdata(&gdev->dev, gdev);
512 if (chip->parent && chip->parent->driver)
513 gdev->owner = chip->parent->driver->owner;
514 else if (chip->owner)
515 /* TODO: remove chip->owner */
516 gdev->owner = chip->owner;
517 else
518 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -0800519
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100520 gdev->descs = devm_kcalloc(&gdev->dev, chip->ngpio,
521 sizeof(gdev->descs[0]), GFP_KERNEL);
522 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +0200523 status = -ENOMEM;
524 goto err_free_gdev;
525 }
526
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800527 if (chip->ngpio == 0) {
528 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +0200529 status = -EINVAL;
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100530 goto err_free_gdev;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800531 }
Linus Walleijdf4878e2016-02-12 14:48:23 +0100532
533 if (chip->label)
534 gdev->label = devm_kstrdup(&gdev->dev, chip->label, GFP_KERNEL);
535 else
536 gdev->label = devm_kstrdup(&gdev->dev, "unknown", GFP_KERNEL);
537 if (!gdev->label) {
538 status = -ENOMEM;
539 goto err_free_gdev;
540 }
541
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100542 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +0100543 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800544
David Brownelld2876d02008-02-04 22:28:20 -0800545 spin_lock_irqsave(&gpio_lock, flags);
546
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100547 /*
548 * TODO: this allocates a Linux GPIO number base in the global
549 * GPIO numberspace for this chip. In the long run we want to
550 * get *rid* of this numberspace and use only descriptors, but
551 * it may be a pipe dream. It will not happen before we get rid
552 * of the sysfs interface anyways.
553 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700554 if (base < 0) {
555 base = gpiochip_find_base(chip->ngpio);
556 if (base < 0) {
557 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100558 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100559 goto err_free_gdev;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700560 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100561 /*
562 * TODO: it should not be necessary to reflect the assigned
563 * base outside of the GPIO subsystem. Go over drivers and
564 * see if anyone makes use of this, else drop this and assign
565 * a poison instead.
566 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700567 chip->base = base;
568 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100569 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700570
Linus Walleijff2b1352015-10-20 11:10:38 +0200571 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +0100572 if (status) {
573 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100574 goto err_free_gdev;
Johan Hovold05aa5202015-01-12 17:12:26 +0100575 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900576
Linus Walleijff2b1352015-10-20 11:10:38 +0200577 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100578 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -0700579
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100580 desc->gdev = gdev;
Johan Hovold05aa5202015-01-12 17:12:26 +0100581
582 /* REVISIT: most hardware initializes GPIOs as inputs (often
583 * with pullups enabled) so power usage is minimized. Linux
584 * code should set the gpio direction first thing; but until
585 * it does, and in case chip->get_direction is not set, we may
586 * expose the wrong direction in sysfs.
587 */
588 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800589 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900590
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800591 spin_unlock_irqrestore(&gpio_lock, flags);
592
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530593#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +0100594 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530595#endif
596
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200597 status = gpiochip_set_desc_names(chip);
598 if (status)
599 goto err_remove_from_list;
600
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200601 status = of_gpiochip_add(chip);
602 if (status)
603 goto err_remove_chip;
604
Mika Westerberg664e3e52014-01-08 12:40:54 +0200605 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600606
Linus Walleij3c702e92015-10-21 15:29:53 +0200607 /*
608 * By first adding the chardev, and then adding the device,
609 * we get a device node entry in sysfs under
610 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
611 * coldplug of device nodes and other udev business.
612 */
613 cdev_init(&gdev->chrdev, &gpio_fileops);
614 gdev->chrdev.owner = THIS_MODULE;
615 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
616 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
617 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
618 if (status < 0)
619 chip_warn(chip, "failed to add char device %d:%d\n",
620 MAJOR(gpio_devt), gdev->id);
621 else
622 chip_dbg(chip, "added GPIO chardev (%d:%d)\n",
623 MAJOR(gpio_devt), gdev->id);
Linus Walleijff2b1352015-10-20 11:10:38 +0200624 status = device_add(&gdev->dev);
Johan Hovold225fce82015-01-12 17:12:25 +0100625 if (status)
Linus Walleij3c702e92015-10-21 15:29:53 +0200626 goto err_remove_chardev;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600627
Linus Walleijafbc4f32016-02-09 13:21:06 +0100628 status = gpiochip_sysfs_register(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200629 if (status)
630 goto err_remove_device;
631
632 /* From this point, the .release() function cleans up gpio_device */
633 gdev->dev.release = gpiodevice_release;
634 get_device(&gdev->dev);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100635 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
636 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
637 dev_name(&gdev->dev), chip->label ? : "generic");
Grant Likely64842aa2011-11-06 11:36:18 -0700638
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600639 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800640
Linus Walleijff2b1352015-10-20 11:10:38 +0200641err_remove_device:
642 device_del(&gdev->dev);
Linus Walleij3c702e92015-10-21 15:29:53 +0200643err_remove_chardev:
644 cdev_del(&gdev->chrdev);
Johan Hovold225fce82015-01-12 17:12:25 +0100645err_remove_chip:
646 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200647 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100648 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200649err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100650 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200651 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800652 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200653err_free_gdev:
654 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -0800655 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200656 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100657 gdev->base, gdev->base + gdev->ngpio - 1,
658 chip->label ? : "generic");
659 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -0800660 return status;
661}
Linus Walleijb08ea352015-12-03 15:14:13 +0100662EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -0800663
664/**
Linus Walleij43c54ec2016-02-11 11:37:48 +0100665 * gpiochip_get_data() - get per-subdriver data for the chip
666 */
667void *gpiochip_get_data(struct gpio_chip *chip)
668{
669 return chip->gpiodev->data;
670}
671EXPORT_SYMBOL_GPL(gpiochip_get_data);
672
673/**
David Brownelld2876d02008-02-04 22:28:20 -0800674 * gpiochip_remove() - unregister a gpio_chip
675 * @chip: the chip to unregister
676 *
677 * A gpio_chip with any GPIOs still requested may not be removed.
678 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200679void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800680{
Linus Walleijff2b1352015-10-20 11:10:38 +0200681 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200682 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800683 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100684 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200685 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800686
Linus Walleijff2b1352015-10-20 11:10:38 +0200687 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +0100688 gpiochip_sysfs_unregister(gdev);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +0800689 /* Numb the device, cancelling all outstanding operations */
690 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +0100691 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200692 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100693 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600694 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600695 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +0100696 /*
697 * We accept no more calls into the driver from this point, so
698 * NULL the driver data pointer
699 */
700 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -0600701
Johan Hovold6798aca2015-01-12 17:12:28 +0100702 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100703 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100704 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +0200705 if (test_bit(FLAG_REQUESTED, &desc->flags))
706 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800707 }
David Brownelld2876d02008-02-04 22:28:20 -0800708 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900709
Johan Hovoldfab28b82015-05-04 17:10:27 +0200710 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100711 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +0100712 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +0200713
Linus Walleijff2b1352015-10-20 11:10:38 +0200714 /*
715 * The gpiochip side puts its use of the device to rest here:
716 * if there are no userspace clients, the chardev and device will
717 * be removed, else it will be dangling until the last user is
718 * gone.
719 */
720 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -0800721}
722EXPORT_SYMBOL_GPL(gpiochip_remove);
723
Grant Likely594fa262010-06-08 07:48:16 -0600724/**
725 * gpiochip_find() - iterator for locating a specific gpio_chip
726 * @data: data to pass to match function
727 * @callback: Callback function to check gpio_chip
728 *
729 * Similar to bus_find_device. It returns a reference to a gpio_chip as
730 * determined by a user supplied @match callback. The callback should return
731 * 0 if the device doesn't match and non-zero if it does. If the callback is
732 * non-zero, this function will return to the caller and not iterate over any
733 * more gpio_chips.
734 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600735struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700736 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600737 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600738{
Linus Walleijff2b1352015-10-20 11:10:38 +0200739 struct gpio_device *gdev;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900740 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600741 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600742
743 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200744 list_for_each_entry(gdev, &gpio_devices, list)
745 if (match(gdev->chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600746 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900747
748 /* No match? */
Linus Walleijff2b1352015-10-20 11:10:38 +0200749 if (&gdev->list == &gpio_devices)
Alexandre Courbot125eef92013-02-03 01:29:26 +0900750 chip = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +0200751 else
752 chip = gdev->chip;
753
Grant Likely594fa262010-06-08 07:48:16 -0600754 spin_unlock_irqrestore(&gpio_lock, flags);
755
756 return chip;
757}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600758EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800759
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900760static int gpiochip_match_name(struct gpio_chip *chip, void *data)
761{
762 const char *name = data;
763
764 return !strcmp(chip->label, name);
765}
766
767static struct gpio_chip *find_chip_by_name(const char *name)
768{
769 return gpiochip_find((void *)name, gpiochip_match_name);
770}
771
Linus Walleij14250522014-03-25 10:40:18 +0100772#ifdef CONFIG_GPIOLIB_IRQCHIP
773
774/*
775 * The following is irqchip helper code for gpiochips.
776 */
777
778/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200779 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
780 * @gpiochip: the gpiochip to set the irqchip chain to
781 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100782 * @parent_irq: the irq number corresponding to the parent IRQ for this
783 * chained irqchip
784 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200785 * coming out of the gpiochip. If the interrupt is nested rather than
786 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100787 */
788void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
789 struct irq_chip *irqchip,
790 int parent_irq,
791 irq_flow_handler_t parent_handler)
792{
Linus Walleij83141a72014-09-26 13:50:12 +0200793 unsigned int offset;
794
Linus Walleij83141a72014-09-26 13:50:12 +0200795 if (!gpiochip->irqdomain) {
796 chip_err(gpiochip, "called %s before setting up irqchip\n",
797 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200798 return;
799 }
800
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200801 if (parent_handler) {
802 if (gpiochip->can_sleep) {
803 chip_err(gpiochip,
804 "you cannot have chained interrupts on a "
805 "chip that may sleep\n");
806 return;
807 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200808 /*
809 * The parent irqchip is already using the chip_data for this
810 * irqchip, so our callbacks simply use the handler_data.
811 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +0200812 irq_set_chained_handler_and_data(parent_irq, parent_handler,
813 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300814
815 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200816 }
Linus Walleij83141a72014-09-26 13:50:12 +0200817
818 /* Set the parent IRQ for all affected IRQs */
819 for (offset = 0; offset < gpiochip->ngpio; offset++)
820 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
821 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100822}
823EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
824
825/**
826 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
827 * @d: the irqdomain used by this irqchip
828 * @irq: the global irq number used by this GPIO irqchip irq
829 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
830 *
831 * This function will set up the mapping for a certain IRQ line on a
832 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
833 * stored inside the gpiochip.
834 */
835static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
836 irq_hw_number_t hwirq)
837{
838 struct gpio_chip *chip = d->host_data;
839
Linus Walleij14250522014-03-25 10:40:18 +0100840 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300841 /*
842 * This lock class tells lockdep that GPIO irqs are in a different
843 * category than their parents, so it won't report false recursion.
844 */
845 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +0200846 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200847 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300848 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200849 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100850 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -0500851
Linus Walleij1333b902014-04-23 16:45:12 +0200852 /*
853 * No set-up of the hardware will happen if IRQ_TYPE_NONE
854 * is passed as default type.
855 */
856 if (chip->irq_default_type != IRQ_TYPE_NONE)
857 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100858
859 return 0;
860}
861
Linus Walleijc3626fd2014-03-28 20:42:01 +0100862static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
863{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200864 struct gpio_chip *chip = d->host_data;
865
Linus Walleij1c8732b2014-04-09 13:34:39 +0200866 if (chip->can_sleep)
867 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100868 irq_set_chip_and_handler(irq, NULL, NULL);
869 irq_set_chip_data(irq, NULL);
870}
871
Linus Walleij14250522014-03-25 10:40:18 +0100872static const struct irq_domain_ops gpiochip_domain_ops = {
873 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100874 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100875 /* Virtually all GPIO irqchips are twocell:ed */
876 .xlate = irq_domain_xlate_twocell,
877};
878
879static int gpiochip_irq_reqres(struct irq_data *d)
880{
881 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
882
Linus Walleijff2b1352015-10-20 11:10:38 +0200883 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300884 return -ENODEV;
885
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900886 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100887 chip_err(chip,
888 "unable to lock HW IRQ %lu for IRQ\n",
889 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200890 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100891 return -EINVAL;
892 }
893 return 0;
894}
895
896static void gpiochip_irq_relres(struct irq_data *d)
897{
898 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
899
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900900 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200901 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100902}
903
904static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
905{
906 return irq_find_mapping(chip->irqdomain, offset);
907}
908
909/**
910 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
911 * @gpiochip: the gpiochip to remove the irqchip from
912 *
913 * This is called only from gpiochip_remove()
914 */
915static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
916{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100917 unsigned int offset;
918
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300919 acpi_gpiochip_free_interrupts(gpiochip);
920
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300921 if (gpiochip->irq_parent) {
922 irq_set_chained_handler(gpiochip->irq_parent, NULL);
923 irq_set_handler_data(gpiochip->irq_parent, NULL);
924 }
925
Linus Walleijc3626fd2014-03-28 20:42:01 +0100926 /* Remove all IRQ mappings and delete the domain */
927 if (gpiochip->irqdomain) {
928 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300929 irq_dispose_mapping(
930 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100931 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100932 }
Linus Walleij14250522014-03-25 10:40:18 +0100933
934 if (gpiochip->irqchip) {
935 gpiochip->irqchip->irq_request_resources = NULL;
936 gpiochip->irqchip->irq_release_resources = NULL;
937 gpiochip->irqchip = NULL;
938 }
939}
940
941/**
942 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
943 * @gpiochip: the gpiochip to add the irqchip to
944 * @irqchip: the irqchip to add to the gpiochip
945 * @first_irq: if not dynamically assigned, the base (first) IRQ to
946 * allocate gpiochip irqs from
947 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200948 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
949 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300950 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +0100951 *
952 * This function closely associates a certain irqchip with a certain
953 * gpiochip, providing an irq domain to translate the local IRQs to
954 * global irqs in the gpiolib core, and making sure that the gpiochip
955 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +0100956 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +0100957 * from the gpiochip passed as chip data. An irqdomain will be stored
958 * in the gpiochip that shall be used by the driver to handle IRQ number
959 * translation. The gpiochip will need to be initialized and registered
960 * before calling this function.
961 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100962 * This function will handle two cell:ed simple IRQs and assumes all
963 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100964 * need to be open coded.
965 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300966int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
967 struct irq_chip *irqchip,
968 unsigned int first_irq,
969 irq_flow_handler_t handler,
970 unsigned int type,
971 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +0100972{
973 struct device_node *of_node;
974 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100975 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100976
977 if (!gpiochip || !irqchip)
978 return -EINVAL;
979
Linus Walleij58383c782015-11-04 09:56:26 +0100980 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +0100981 pr_err("missing gpiochip .dev parent pointer\n");
982 return -EINVAL;
983 }
Linus Walleij58383c782015-11-04 09:56:26 +0100984 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +0100985#ifdef CONFIG_OF_GPIO
986 /*
Colin Cronin20a8a962015-05-18 11:41:43 -0700987 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800988 * FIXME: get rid of this and use gpiochip->parent->of_node
989 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +0100990 */
991 if (gpiochip->of_node)
992 of_node = gpiochip->of_node;
993#endif
994 gpiochip->irqchip = irqchip;
995 gpiochip->irq_handler = handler;
996 gpiochip->irq_default_type = type;
997 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300998 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +0100999 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1000 gpiochip->ngpio, first_irq,
1001 &gpiochip_domain_ops, gpiochip);
1002 if (!gpiochip->irqdomain) {
1003 gpiochip->irqchip = NULL;
1004 return -EINVAL;
1005 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001006
1007 /*
1008 * It is possible for a driver to override this, but only if the
1009 * alternative functions are both implemented.
1010 */
1011 if (!irqchip->irq_request_resources &&
1012 !irqchip->irq_release_resources) {
1013 irqchip->irq_request_resources = gpiochip_irq_reqres;
1014 irqchip->irq_release_resources = gpiochip_irq_relres;
1015 }
Linus Walleij14250522014-03-25 10:40:18 +01001016
1017 /*
1018 * Prepare the mapping since the irqchip shall be orthogonal to
1019 * any gpiochip calls. If the first_irq was zero, this is
1020 * necessary to allocate descriptors for all IRQs.
1021 */
Linus Walleijc3626fd2014-03-28 20:42:01 +01001022 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1023 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
1024 if (offset == 0)
1025 /*
1026 * Store the base into the gpiochip to be used when
1027 * unmapping the irqs.
1028 */
1029 gpiochip->irq_base = irq_base;
1030 }
Linus Walleij14250522014-03-25 10:40:18 +01001031
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001032 acpi_gpiochip_request_interrupts(gpiochip);
1033
Linus Walleij14250522014-03-25 10:40:18 +01001034 return 0;
1035}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001036EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +01001037
1038#else /* CONFIG_GPIOLIB_IRQCHIP */
1039
1040static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
1041
1042#endif /* CONFIG_GPIOLIB_IRQCHIP */
1043
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001044/**
1045 * gpiochip_generic_request() - request the gpio function for a pin
1046 * @chip: the gpiochip owning the GPIO
1047 * @offset: the offset of the GPIO to request for GPIO function
1048 */
1049int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1050{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001051 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001052}
1053EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1054
1055/**
1056 * gpiochip_generic_free() - free the gpio function from a pin
1057 * @chip: the gpiochip to request the gpio function for
1058 * @offset: the offset of the GPIO to free from GPIO function
1059 */
1060void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1061{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001062 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001063}
1064EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1065
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301066#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001067
Linus Walleij3f0f8672012-11-20 12:40:15 +01001068/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001069 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1070 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001071 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001072 * @gpio_offset: the start offset in the current gpio_chip number space
1073 * @pin_group: name of the pin group inside the pin controller
1074 */
1075int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1076 struct pinctrl_dev *pctldev,
1077 unsigned int gpio_offset, const char *pin_group)
1078{
1079 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001080 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001081 int ret;
1082
1083 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1084 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001085 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001086 return -ENOMEM;
1087 }
1088
1089 /* Use local offset as range ID */
1090 pin_range->range.id = gpio_offset;
1091 pin_range->range.gc = chip;
1092 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001093 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001094 pin_range->pctldev = pctldev;
1095
1096 ret = pinctrl_get_group_pins(pctldev, pin_group,
1097 &pin_range->range.pins,
1098 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001099 if (ret < 0) {
1100 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001101 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001102 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001103
1104 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1105
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001106 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1107 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001108 pinctrl_dev_get_devname(pctldev), pin_group);
1109
Linus Walleij20ec3e32016-02-11 11:03:06 +01001110 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001111
1112 return 0;
1113}
1114EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1115
1116/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001117 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1118 * @chip: the gpiochip to add the range for
1119 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001120 * @gpio_offset: the start offset in the current gpio_chip number space
1121 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001122 * @npins: the number of pins from the offset of each pin space (GPIO and
1123 * pin controller) to accumulate in this range
1124 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001125int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001126 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001127 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301128{
1129 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001130 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001131 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301132
Linus Walleij3f0f8672012-11-20 12:40:15 +01001133 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301134 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001135 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001136 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301137 }
1138
Linus Walleij3f0f8672012-11-20 12:40:15 +01001139 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001140 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001141 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301142 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001143 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001144 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301145 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001146 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301147 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001148 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001149 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001150 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001151 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001152 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001153 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001154 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1155 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001156 pinctl_name,
1157 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301158
Linus Walleij20ec3e32016-02-11 11:03:06 +01001159 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001160
1161 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301162}
Linus Walleij165adc92012-11-06 14:49:39 +01001163EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301164
Linus Walleij3f0f8672012-11-20 12:40:15 +01001165/**
1166 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1167 * @chip: the chip to remove all the mappings for
1168 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301169void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1170{
1171 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01001172 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301173
Linus Walleij20ec3e32016-02-11 11:03:06 +01001174 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301175 list_del(&pin_range->node);
1176 pinctrl_remove_gpio_range(pin_range->pctldev,
1177 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001178 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301179 }
1180}
Linus Walleij165adc92012-11-06 14:49:39 +01001181EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1182
1183#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301184
David Brownelld2876d02008-02-04 22:28:20 -08001185/* These "optional" allocation calls help prevent drivers from stomping
1186 * on each other, and help provide better diagnostics in debugfs.
1187 * They're called even less than the "set direction" calls.
1188 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001189static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001190{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001191 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001192 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001193 unsigned long flags;
1194
1195 spin_lock_irqsave(&gpio_lock, flags);
1196
David Brownelld2876d02008-02-04 22:28:20 -08001197 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001198 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001199 */
1200
1201 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1202 desc_set_label(desc, label ? : "?");
1203 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001204 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001205 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001206 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001207 }
1208
1209 if (chip->request) {
1210 /* chip->request may sleep */
1211 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001212 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001213 spin_lock_irqsave(&gpio_lock, flags);
1214
1215 if (status < 0) {
1216 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001217 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001218 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001219 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001220 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001221 if (chip->get_direction) {
1222 /* chip->get_direction may sleep */
1223 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001224 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001225 spin_lock_irqsave(&gpio_lock, flags);
1226 }
David Brownelld2876d02008-02-04 22:28:20 -08001227done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +03001228 if (status < 0) {
1229 /* Clear flags that might have been set by the caller before
1230 * requesting the GPIO.
1231 */
1232 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1233 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1234 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1235 }
Mika Westerberg77c2d792014-03-10 14:54:50 +02001236 spin_unlock_irqrestore(&gpio_lock, flags);
1237 return status;
1238}
1239
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001240/*
1241 * This descriptor validation needs to be inserted verbatim into each
1242 * function taking a descriptor, so we need to use a preprocessor
1243 * macro to avoid endless duplication.
1244 */
1245#define VALIDATE_DESC(desc) do { \
1246 if (!desc || !desc->gdev) { \
1247 pr_warn("%s: invalid GPIO\n", __func__); \
1248 return -EINVAL; \
1249 } \
1250 if ( !desc->gdev->chip ) { \
1251 dev_warn(&desc->gdev->dev, \
1252 "%s: backing chip is gone\n", __func__); \
1253 return 0; \
1254 } } while (0)
1255
1256#define VALIDATE_DESC_VOID(desc) do { \
1257 if (!desc || !desc->gdev) { \
1258 pr_warn("%s: invalid GPIO\n", __func__); \
1259 return; \
1260 } \
1261 if (!desc->gdev->chip) { \
1262 dev_warn(&desc->gdev->dev, \
1263 "%s: backing chip is gone\n", __func__); \
1264 return; \
1265 } } while (0)
1266
1267
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001268int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001269{
1270 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001271 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001272
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001273 VALIDATE_DESC(desc);
1274 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001275
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001276 if (try_module_get(gdev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001277 status = __gpiod_request(desc, label);
1278 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001279 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001280 else
1281 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001282 }
1283
David Brownelld2876d02008-02-04 22:28:20 -08001284 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001285 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001286
David Brownelld2876d02008-02-04 22:28:20 -08001287 return status;
1288}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001289
Mika Westerberg77c2d792014-03-10 14:54:50 +02001290static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001291{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001292 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001293 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001294 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001295
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001296 might_sleep();
1297
Alexandre Courbot372e7222013-02-03 01:29:29 +09001298 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07001299
David Brownelld2876d02008-02-04 22:28:20 -08001300 spin_lock_irqsave(&gpio_lock, flags);
1301
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001302 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07001303 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1304 if (chip->free) {
1305 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001306 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001307 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001308 spin_lock_irqsave(&gpio_lock, flags);
1309 }
David Brownelld2876d02008-02-04 22:28:20 -08001310 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001311 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001312 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301313 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301314 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001315 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001316 ret = true;
1317 }
David Brownelld2876d02008-02-04 22:28:20 -08001318
1319 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001320 return ret;
1321}
1322
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001323void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001324{
Linus Walleij33a68e82016-02-11 10:28:44 +01001325 if (desc && desc->gdev && __gpiod_free(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001326 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001327 put_device(&desc->gdev->dev);
1328 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001329 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01001330 }
David Brownelld2876d02008-02-04 22:28:20 -08001331}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001332
David Brownelld2876d02008-02-04 22:28:20 -08001333/**
1334 * gpiochip_is_requested - return string iff signal was requested
1335 * @chip: controller managing the signal
1336 * @offset: of signal within controller's 0..(ngpio - 1) range
1337 *
1338 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001339 * The string returned is the label passed to gpio_request(); if none has been
1340 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001341 *
1342 * This function is for use by GPIO controller drivers. The label can
1343 * help with diagnostics, and knowing that the signal is used as a GPIO
1344 * can help avoid accidentally multiplexing it to another controller.
1345 */
1346const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1347{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001348 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001349
Dirk Behme48b59532015-08-18 18:02:32 +02001350 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001351 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001352
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001353 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001354
Alexandre Courbot372e7222013-02-03 01:29:29 +09001355 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001356 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001357 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001358}
1359EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1360
Mika Westerberg77c2d792014-03-10 14:54:50 +02001361/**
1362 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1363 * @desc: GPIO descriptor to request
1364 * @label: label for the GPIO
1365 *
1366 * Function allows GPIO chip drivers to request and use their own GPIO
1367 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1368 * function will not increase reference count of the GPIO chip module. This
1369 * allows the GPIO chip module to be unloaded as needed (we assume that the
1370 * GPIO chip driver handles freeing the GPIOs it has requested).
1371 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001372struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1373 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001374{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001375 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1376 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001377
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001378 if (IS_ERR(desc)) {
1379 chip_err(chip, "failed to get GPIO descriptor\n");
1380 return desc;
1381 }
1382
1383 err = __gpiod_request(desc, label);
1384 if (err < 0)
1385 return ERR_PTR(err);
1386
1387 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001388}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001389EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001390
1391/**
1392 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1393 * @desc: GPIO descriptor to free
1394 *
1395 * Function frees the given GPIO requested previously with
1396 * gpiochip_request_own_desc().
1397 */
1398void gpiochip_free_own_desc(struct gpio_desc *desc)
1399{
1400 if (desc)
1401 __gpiod_free(desc);
1402}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001403EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08001404
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001405/*
1406 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08001407 * some cases this is done in early boot, before IRQs are enabled.
1408 *
1409 * As a rule these aren't called more than once (except for drivers
1410 * using the open-drain emulation idiom) so these are natural places
1411 * to accumulate extra debugging checks. Note that we can't (yet)
1412 * rely on gpio_request() having been called beforehand.
1413 */
1414
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001415/**
1416 * gpiod_direction_input - set the GPIO direction to input
1417 * @desc: GPIO to set to input
1418 *
1419 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1420 * be called safely on it.
1421 *
1422 * Return 0 in case of success, else an error code.
1423 */
1424int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001425{
David Brownelld2876d02008-02-04 22:28:20 -08001426 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001427 int status = -EINVAL;
1428
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001429 VALIDATE_DESC(desc);
1430 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001431
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001432 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001433 gpiod_warn(desc,
1434 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001435 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001436 return -EIO;
1437 }
1438
Alexandre Courbotd82da792014-07-22 16:17:43 +09001439 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001440 if (status == 0)
1441 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001442
Alexandre Courbot372e7222013-02-03 01:29:29 +09001443 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001444
David Brownelld2876d02008-02-04 22:28:20 -08001445 return status;
1446}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001447EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001448
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001449static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001450{
David Brownelld2876d02008-02-04 22:28:20 -08001451 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001452 int status = -EINVAL;
1453
Linus Walleijd468bf92013-09-24 11:54:38 +02001454 /* GPIOs used for IRQs shall not be set as output */
1455 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1456 gpiod_err(desc,
1457 "%s: tried to set a GPIO tied to an IRQ as output\n",
1458 __func__);
1459 return -EIO;
1460 }
1461
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301462 /* Open drain pin should not be driven to 1 */
1463 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001464 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301465
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301466 /* Open source pin should not be driven to 0 */
1467 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001468 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301469
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001470 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001471 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001472 gpiod_warn(desc,
1473 "%s: missing set() or direction_output() operations\n",
1474 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001475 return -EIO;
1476 }
1477
Alexandre Courbotd82da792014-07-22 16:17:43 +09001478 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001479 if (status == 0)
1480 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001481 trace_gpio_value(desc_to_gpio(desc), 0, value);
1482 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001483 return status;
1484}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001485
1486/**
1487 * gpiod_direction_output_raw - set the GPIO direction to output
1488 * @desc: GPIO to set to output
1489 * @value: initial output value of the GPIO
1490 *
1491 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1492 * be called safely on it. The initial value of the output must be specified
1493 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1494 *
1495 * Return 0 in case of success, else an error code.
1496 */
1497int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1498{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001499 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001500 return _gpiod_direction_output_raw(desc, value);
1501}
1502EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1503
1504/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301505 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001506 * @desc: GPIO to set to output
1507 * @value: initial output value of the GPIO
1508 *
1509 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1510 * be called safely on it. The initial value of the output must be specified
1511 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1512 * account.
1513 *
1514 * Return 0 in case of success, else an error code.
1515 */
1516int gpiod_direction_output(struct gpio_desc *desc, int value)
1517{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001518 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001519 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1520 value = !value;
1521 return _gpiod_direction_output_raw(desc, value);
1522}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001523EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001524
Felipe Balbic4b5be92010-05-26 14:42:23 -07001525/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001526 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001527 * @gpio: the gpio to set debounce time
1528 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001529 *
1530 * returns -ENOTSUPP if the controller does not support setting
1531 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001532 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001533int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001534{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001535 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001536
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001537 VALIDATE_DESC(desc);
1538 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001539 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001540 gpiod_dbg(desc,
1541 "%s: missing set() or set_debounce() operations\n",
1542 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001543 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001544 }
1545
Alexandre Courbotd82da792014-07-22 16:17:43 +09001546 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001547}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001548EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001549
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001550/**
1551 * gpiod_is_active_low - test whether a GPIO is active-low or not
1552 * @desc: the gpio descriptor to test
1553 *
1554 * Returns 1 if the GPIO is active-low, 0 otherwise.
1555 */
1556int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001557{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001558 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001559 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001560}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001561EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001562
1563/* I/O calls are only valid after configuration completed; the relevant
1564 * "is this a valid GPIO" error checks should already have been done.
1565 *
1566 * "Get" operations are often inlinable as reading a pin value register,
1567 * and masking the relevant bit in that register.
1568 *
1569 * When "set" operations are inlinable, they involve writing that mask to
1570 * one register to set a low value, or a different register to set it high.
1571 * Otherwise locking is needed, so there may be little value to inlining.
1572 *
1573 *------------------------------------------------------------------------
1574 *
1575 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1576 * have requested the GPIO. That can include implicit requesting by
1577 * a direction setting call. Marking a gpio as requested locks its chip
1578 * in memory, guaranteeing that these table lookups need no more locking
1579 * and that gpiochip_remove() will fail.
1580 *
1581 * REVISIT when debugging, consider adding some instrumentation to ensure
1582 * that the GPIO was actually requested.
1583 */
1584
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001585static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001586{
1587 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001588 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001589 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001590
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001591 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001592 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001593 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01001594 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001595 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001596 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001597}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001598
David Brownelld2876d02008-02-04 22:28:20 -08001599/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001600 * gpiod_get_raw_value() - return a gpio's raw value
1601 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001602 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001603 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001604 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001605 *
1606 * This function should be called from contexts where we cannot sleep, and will
1607 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001608 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001609int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001610{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001611 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001612 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001613 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001614 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001615}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001616EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001617
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001618/**
1619 * gpiod_get_value() - return a gpio's value
1620 * @desc: gpio whose value will be returned
1621 *
1622 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001623 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001624 *
1625 * This function should be called from contexts where we cannot sleep, and will
1626 * complain if the GPIO chip functions potentially sleep.
1627 */
1628int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001629{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001630 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001631
1632 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001633 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001634 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001635
1636 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001637 if (value < 0)
1638 return value;
1639
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001640 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1641 value = !value;
1642
1643 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001644}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001645EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001646
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301647/*
1648 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001649 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001650 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301651 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001652static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301653{
1654 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001655 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001656 int offset = gpio_chip_hwgpio(desc);
1657
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301658 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001659 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301660 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001661 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301662 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001663 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301664 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001665 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301666 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001667 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301668 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001669 gpiod_err(desc,
1670 "%s: Error in set_value for open drain err %d\n",
1671 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301672}
1673
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301674/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001675 * _gpio_set_open_source_value() - Set the open source gpio's value.
1676 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001677 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301678 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001679static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301680{
1681 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001682 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001683 int offset = gpio_chip_hwgpio(desc);
1684
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301685 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001686 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301687 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001688 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301689 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001690 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301691 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001692 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301693 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001694 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301695 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001696 gpiod_err(desc,
1697 "%s: Error in set_value for open source err %d\n",
1698 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301699}
1700
Alexandre Courbot23600962014-03-11 15:52:09 +09001701static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001702{
1703 struct gpio_chip *chip;
1704
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001705 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001706 trace_gpio_value(desc_to_gpio(desc), 0, value);
1707 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1708 _gpio_set_open_drain_value(desc, value);
1709 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1710 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301711 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001712 chip->set(chip, gpio_chip_hwgpio(desc), value);
1713}
1714
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001715/*
1716 * set multiple outputs on the same chip;
1717 * use the chip's set_multiple function if available;
1718 * otherwise set the outputs sequentially;
1719 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1720 * defines which outputs are to be changed
1721 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1722 * defines the values the outputs specified by mask are to be set to
1723 */
1724static void gpio_chip_set_multiple(struct gpio_chip *chip,
1725 unsigned long *mask, unsigned long *bits)
1726{
1727 if (chip->set_multiple) {
1728 chip->set_multiple(chip, mask, bits);
1729 } else {
1730 int i;
1731 for (i = 0; i < chip->ngpio; i++) {
1732 if (mask[BIT_WORD(i)] == 0) {
1733 /* no more set bits in this mask word;
1734 * skip ahead to the next word */
1735 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1736 continue;
1737 }
1738 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001739 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001740 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001741 }
1742 }
1743}
1744
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001745static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1746 unsigned int array_size,
1747 struct gpio_desc **desc_array,
1748 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001749{
1750 int i = 0;
1751
1752 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001753 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001754 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1755 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1756 int count = 0;
1757
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001758 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001759 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001760
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001761 memset(mask, 0, sizeof(mask));
1762 do {
1763 struct gpio_desc *desc = desc_array[i];
1764 int hwgpio = gpio_chip_hwgpio(desc);
1765 int value = value_array[i];
1766
1767 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1768 value = !value;
1769 trace_gpio_value(desc_to_gpio(desc), 0, value);
1770 /*
1771 * collect all normal outputs belonging to the same chip
1772 * open drain and open source outputs are set individually
1773 */
1774 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001775 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001776 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1777 _gpio_set_open_source_value(desc, value);
1778 } else {
1779 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001780 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001781 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001782 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001783 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001784 count++;
1785 }
1786 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001787 } while ((i < array_size) &&
1788 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001789 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001790 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001791 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001792 }
1793}
1794
David Brownelld2876d02008-02-04 22:28:20 -08001795/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001796 * gpiod_set_raw_value() - assign a gpio's raw value
1797 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001798 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001799 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001800 * Set the raw value of the GPIO, i.e. the value of its physical line without
1801 * regard for its ACTIVE_LOW status.
1802 *
1803 * This function should be called from contexts where we cannot sleep, and will
1804 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001805 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001806void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001807{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001808 VALIDATE_DESC_VOID(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001809 /* Should be using gpio_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001810 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001811 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001812}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001813EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001814
1815/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001816 * gpiod_set_value() - assign a gpio's value
1817 * @desc: gpio whose value will be assigned
1818 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001819 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001820 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1821 * account
1822 *
1823 * This function should be called from contexts where we cannot sleep, and will
1824 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001825 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001826void gpiod_set_value(struct gpio_desc *desc, int value)
1827{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001828 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001829 /* Should be using gpio_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001830 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001831 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1832 value = !value;
1833 _gpiod_set_raw_value(desc, value);
1834}
1835EXPORT_SYMBOL_GPL(gpiod_set_value);
1836
1837/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001838 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001839 * @array_size: number of elements in the descriptor / value arrays
1840 * @desc_array: array of GPIO descriptors whose values will be assigned
1841 * @value_array: array of values to assign
1842 *
1843 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1844 * without regard for their ACTIVE_LOW status.
1845 *
1846 * This function should be called from contexts where we cannot sleep, and will
1847 * complain if the GPIO chip functions potentially sleep.
1848 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001849void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001850 struct gpio_desc **desc_array, int *value_array)
1851{
1852 if (!desc_array)
1853 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001854 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1855 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001856}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001857EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001858
1859/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001860 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001861 * @array_size: number of elements in the descriptor / value arrays
1862 * @desc_array: array of GPIO descriptors whose values will be assigned
1863 * @value_array: array of values to assign
1864 *
1865 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1866 * into account.
1867 *
1868 * This function should be called from contexts where we cannot sleep, and will
1869 * complain if the GPIO chip functions potentially sleep.
1870 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001871void gpiod_set_array_value(unsigned int array_size,
1872 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001873{
1874 if (!desc_array)
1875 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001876 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1877 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001878}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001879EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001880
1881/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001882 * gpiod_cansleep() - report whether gpio value access may sleep
1883 * @desc: gpio to check
1884 *
1885 */
1886int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001887{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001888 VALIDATE_DESC(desc);
1889 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001890}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001891EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001892
David Brownell0f6d5042008-10-15 22:03:14 -07001893/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001894 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1895 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001896 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001897 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1898 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001899 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001900int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001901{
1902 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001903 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001904
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001905 VALIDATE_DESC(desc);
1906 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001907 offset = gpio_chip_hwgpio(desc);
1908 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1909}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001910EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001911
Linus Walleijd468bf92013-09-24 11:54:38 +02001912/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001913 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001914 * @chip: the chip the GPIO to lock belongs to
1915 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001916 *
1917 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001918 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001919 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001920int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001921{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001922 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001923 return -EINVAL;
1924
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001925 if (test_bit(FLAG_IS_OUT, &chip->gpiodev->descs[offset].flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001926 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001927 "%s: tried to flag a GPIO set as output for IRQ\n",
1928 __func__);
1929 return -EIO;
1930 }
1931
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001932 set_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001933 return 0;
1934}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001935EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001936
Linus Walleijd468bf92013-09-24 11:54:38 +02001937/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001938 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001939 * @chip: the chip the GPIO to lock belongs to
1940 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001941 *
1942 * This is used directly by GPIO drivers that want to indicate
1943 * that a certain GPIO is no longer used exclusively for IRQ.
1944 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001945void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001946{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001947 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001948 return;
1949
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001950 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001951}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001952EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001953
Linus Walleij6cee3822016-02-11 20:16:45 +01001954bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
1955{
1956 if (offset >= chip->ngpio)
1957 return false;
1958
1959 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
1960}
1961EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
1962
Linus Walleij143b65d2016-02-16 15:41:42 +01001963bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
1964{
1965 if (offset >= chip->ngpio)
1966 return false;
1967
1968 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
1969}
1970EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
1971
1972bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
1973{
1974 if (offset >= chip->ngpio)
1975 return false;
1976
1977 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
1978}
1979EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
1980
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001981/**
1982 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1983 * @desc: gpio whose value will be returned
1984 *
1985 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001986 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001987 *
1988 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001989 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001990int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001991{
David Brownelld2876d02008-02-04 22:28:20 -08001992 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001993 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001994 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001995}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001996EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001997
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001998/**
1999 * gpiod_get_value_cansleep() - return a gpio's value
2000 * @desc: gpio whose value will be returned
2001 *
2002 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002003 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002004 *
2005 * This function is to be called from contexts that can sleep.
2006 */
2007int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002008{
David Brownelld2876d02008-02-04 22:28:20 -08002009 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002010
2011 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002012 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002013 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002014 if (value < 0)
2015 return value;
2016
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002017 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2018 value = !value;
2019
David Brownelld2876d02008-02-04 22:28:20 -08002020 return value;
2021}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002022EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002023
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002024/**
2025 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2026 * @desc: gpio whose value will be assigned
2027 * @value: value to assign
2028 *
2029 * Set the raw value of the GPIO, i.e. the value of its physical line without
2030 * regard for its ACTIVE_LOW status.
2031 *
2032 * This function is to be called from contexts that can sleep.
2033 */
2034void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002035{
David Brownelld2876d02008-02-04 22:28:20 -08002036 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002037 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002038 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002039}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002040EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002041
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002042/**
2043 * gpiod_set_value_cansleep() - assign a gpio's value
2044 * @desc: gpio whose value will be assigned
2045 * @value: value to assign
2046 *
2047 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2048 * account
2049 *
2050 * This function is to be called from contexts that can sleep.
2051 */
2052void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002053{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002054 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002055 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002056 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2057 value = !value;
2058 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002059}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002060EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002061
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002062/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002063 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002064 * @array_size: number of elements in the descriptor / value arrays
2065 * @desc_array: array of GPIO descriptors whose values will be assigned
2066 * @value_array: array of values to assign
2067 *
2068 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2069 * without regard for their ACTIVE_LOW status.
2070 *
2071 * This function is to be called from contexts that can sleep.
2072 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002073void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2074 struct gpio_desc **desc_array,
2075 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002076{
2077 might_sleep_if(extra_checks);
2078 if (!desc_array)
2079 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002080 gpiod_set_array_value_priv(true, true, array_size, desc_array,
2081 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002082}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002083EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002084
2085/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002086 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002087 * @array_size: number of elements in the descriptor / value arrays
2088 * @desc_array: array of GPIO descriptors whose values will be assigned
2089 * @value_array: array of values to assign
2090 *
2091 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2092 * into account.
2093 *
2094 * This function is to be called from contexts that can sleep.
2095 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002096void gpiod_set_array_value_cansleep(unsigned int array_size,
2097 struct gpio_desc **desc_array,
2098 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002099{
2100 might_sleep_if(extra_checks);
2101 if (!desc_array)
2102 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002103 gpiod_set_array_value_priv(false, true, array_size, desc_array,
2104 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002105}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002106EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002107
2108/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002109 * gpiod_add_lookup_table() - register GPIO device consumers
2110 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002111 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002112void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002113{
2114 mutex_lock(&gpio_lookup_lock);
2115
Alexandre Courbotad824782013-12-03 12:20:11 +09002116 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002117
2118 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002119}
2120
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302121/**
2122 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2123 * @table: table of consumers to unregister
2124 */
2125void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2126{
2127 mutex_lock(&gpio_lookup_lock);
2128
2129 list_del(&table->list);
2130
2131 mutex_unlock(&gpio_lookup_lock);
2132}
2133
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002134static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002135 unsigned int idx,
2136 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002137{
2138 char prop_name[32]; /* 32 is max size of property name */
2139 enum of_gpio_flags of_flags;
2140 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02002141 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002142
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002143 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02002144 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01002145 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002146 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02002147 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01002148 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002149 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002150
Thierry Redingdd34c372014-04-23 17:28:09 +02002151 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2152 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07002153 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02002154 break;
2155 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002156
2157 if (IS_ERR(desc))
2158 return desc;
2159
2160 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002161 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002162
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002163 if (of_flags & OF_GPIO_SINGLE_ENDED) {
2164 if (of_flags & OF_GPIO_ACTIVE_LOW)
2165 *flags |= GPIO_OPEN_DRAIN;
2166 else
2167 *flags |= GPIO_OPEN_SOURCE;
2168 }
2169
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002170 return desc;
2171}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002172
Mika Westerberg81f59e92013-10-10 11:01:09 +03002173static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002174 unsigned int idx,
2175 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03002176{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002177 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03002178 struct acpi_gpio_info info;
2179 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002180 char propname[32];
2181 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03002182
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002183 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002184 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002185 if (con_id && strcmp(con_id, "gpios")) {
2186 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002187 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002188 } else {
2189 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002190 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002191 }
Mika Westerberge01f4402013-10-10 11:01:10 +03002192
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002193 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
2194 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2195 break;
2196 }
2197
2198 /* Then from plain _CRS GPIOs */
2199 if (IS_ERR(desc)) {
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08002200 if (!acpi_can_fallback_to_crs(adev, con_id))
2201 return ERR_PTR(-ENOENT);
2202
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002203 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2204 if (IS_ERR(desc))
2205 return desc;
2206 }
2207
Christophe RICARD52044722015-12-23 23:25:34 +01002208 if (info.polarity == GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002209 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002210
2211 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002212}
2213
Alexandre Courbotad824782013-12-03 12:20:11 +09002214static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2215{
2216 const char *dev_id = dev ? dev_name(dev) : NULL;
2217 struct gpiod_lookup_table *table;
2218
2219 mutex_lock(&gpio_lookup_lock);
2220
2221 list_for_each_entry(table, &gpio_lookup_list, list) {
2222 if (table->dev_id && dev_id) {
2223 /*
2224 * Valid strings on both ends, must be identical to have
2225 * a match
2226 */
2227 if (!strcmp(table->dev_id, dev_id))
2228 goto found;
2229 } else {
2230 /*
2231 * One of the pointers is NULL, so both must be to have
2232 * a match
2233 */
2234 if (dev_id == table->dev_id)
2235 goto found;
2236 }
2237 }
2238 table = NULL;
2239
2240found:
2241 mutex_unlock(&gpio_lookup_lock);
2242 return table;
2243}
2244
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002245static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002246 unsigned int idx,
2247 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002248{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002249 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002250 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002251 struct gpiod_lookup *p;
2252
Alexandre Courbotad824782013-12-03 12:20:11 +09002253 table = gpiod_find_lookup_table(dev);
2254 if (!table)
2255 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002256
Alexandre Courbotad824782013-12-03 12:20:11 +09002257 for (p = &table->table[0]; p->chip_label; p++) {
2258 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002259
Alexandre Courbotad824782013-12-03 12:20:11 +09002260 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002261 if (p->idx != idx)
2262 continue;
2263
Alexandre Courbotad824782013-12-03 12:20:11 +09002264 /* If the lookup entry has a con_id, require exact match */
2265 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2266 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002267
Alexandre Courbotad824782013-12-03 12:20:11 +09002268 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002269
Alexandre Courbotad824782013-12-03 12:20:11 +09002270 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002271 dev_err(dev, "cannot find GPIO chip %s\n",
2272 p->chip_label);
2273 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002274 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002275
Alexandre Courbotad824782013-12-03 12:20:11 +09002276 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002277 dev_err(dev,
2278 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2279 idx, chip->ngpio, chip->label);
2280 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09002281 }
2282
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09002283 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09002284 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002285
2286 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09002287 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002288
2289 return desc;
2290}
2291
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002292static int dt_gpio_count(struct device *dev, const char *con_id)
2293{
2294 int ret;
2295 char propname[32];
2296 unsigned int i;
2297
2298 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2299 if (con_id)
2300 snprintf(propname, sizeof(propname), "%s-%s",
2301 con_id, gpio_suffixes[i]);
2302 else
2303 snprintf(propname, sizeof(propname), "%s",
2304 gpio_suffixes[i]);
2305
2306 ret = of_gpio_named_count(dev->of_node, propname);
2307 if (ret >= 0)
2308 break;
2309 }
2310 return ret;
2311}
2312
2313static int platform_gpio_count(struct device *dev, const char *con_id)
2314{
2315 struct gpiod_lookup_table *table;
2316 struct gpiod_lookup *p;
2317 unsigned int count = 0;
2318
2319 table = gpiod_find_lookup_table(dev);
2320 if (!table)
2321 return -ENOENT;
2322
2323 for (p = &table->table[0]; p->chip_label; p++) {
2324 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2325 (!con_id && !p->con_id))
2326 count++;
2327 }
2328 if (!count)
2329 return -ENOENT;
2330
2331 return count;
2332}
2333
2334/**
2335 * gpiod_count - return the number of GPIOs associated with a device / function
2336 * or -ENOENT if no GPIO has been assigned to the requested function
2337 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2338 * @con_id: function within the GPIO consumer
2339 */
2340int gpiod_count(struct device *dev, const char *con_id)
2341{
2342 int count = -ENOENT;
2343
2344 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2345 count = dt_gpio_count(dev, con_id);
2346 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2347 count = acpi_gpio_count(dev, con_id);
2348
2349 if (count < 0)
2350 count = platform_gpio_count(dev, con_id);
2351
2352 return count;
2353}
2354EXPORT_SYMBOL_GPL(gpiod_count);
2355
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002356/**
Thierry Reding08791622014-04-25 16:54:22 +02002357 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002358 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002359 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002360 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002361 *
2362 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002363 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07002364 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002365 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002366struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002367 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002368{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002369 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002370}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002371EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002372
2373/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002374 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2375 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2376 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002377 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002378 *
2379 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2380 * the requested function it will return NULL. This is convenient for drivers
2381 * that need to handle optional GPIOs.
2382 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002383struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002384 const char *con_id,
2385 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002386{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002387 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002388}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002389EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002390
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002391/**
2392 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2393 * @desc: gpio to be setup
2394 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2395 * of_get_gpio_hog()
2396 *
2397 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2398 */
2399static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2400{
2401 if (lflags & GPIO_ACTIVE_LOW)
2402 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2403 if (lflags & GPIO_OPEN_DRAIN)
2404 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2405 if (lflags & GPIO_OPEN_SOURCE)
2406 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2407}
Benoit Parrotf625d462015-02-02 11:44:44 -06002408
2409/**
2410 * gpiod_configure_flags - helper function to configure a given GPIO
2411 * @desc: gpio whose value will be assigned
2412 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06002413 * @dflags: gpiod_flags - optional GPIO initialization flags
2414 *
2415 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2416 * requested function and/or index, or another IS_ERR() code if an error
2417 * occurred while trying to acquire the GPIO.
2418 */
2419static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002420 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06002421{
2422 int status;
2423
Benoit Parrotf625d462015-02-02 11:44:44 -06002424 /* No particular flag request, return here... */
2425 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2426 pr_debug("no flags found for %s\n", con_id);
2427 return 0;
2428 }
2429
2430 /* Process flags */
2431 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2432 status = gpiod_direction_output(desc,
2433 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2434 else
2435 status = gpiod_direction_input(desc);
2436
2437 return status;
2438}
2439
Thierry Reding29a1f2332014-04-25 17:10:06 +02002440/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002441 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002442 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002443 * @con_id: function within the GPIO consumer
2444 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002445 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002446 *
2447 * This variant of gpiod_get() allows to access GPIOs other than the first
2448 * defined one for functions that define several GPIOs.
2449 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002450 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2451 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07002452 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002453 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002454struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002455 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002456 unsigned int idx,
2457 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002458{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002459 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002460 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002461 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002462
2463 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2464
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002465 if (dev) {
2466 /* Using device tree? */
2467 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2468 dev_dbg(dev, "using device tree for GPIO lookup\n");
2469 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2470 } else if (ACPI_COMPANION(dev)) {
2471 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2472 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2473 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002474 }
2475
2476 /*
2477 * Either we are not using DT or ACPI, or their lookup did not return
2478 * a result. In that case, use platform lookup as a fallback.
2479 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002480 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002481 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002482 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002483 }
2484
2485 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002486 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002487 return desc;
2488 }
2489
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002490 gpiod_parse_flags(desc, lookupflags);
2491
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002492 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002493 if (status < 0)
2494 return ERR_PTR(status);
2495
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002496 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002497 if (status < 0) {
2498 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2499 gpiod_put(desc);
2500 return ERR_PTR(status);
2501 }
2502
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002503 return desc;
2504}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002505EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002506
2507/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002508 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2509 * @fwnode: handle of the firmware node
2510 * @propname: name of the firmware property representing the GPIO
2511 *
2512 * This function can be used for drivers that get their configuration
2513 * from firmware.
2514 *
2515 * Function properly finds the corresponding GPIO using whatever is the
2516 * underlying firmware interface and then makes sure that the GPIO
2517 * descriptor is requested before it is returned to the caller.
2518 *
2519 * In case of error an ERR_PTR() is returned.
2520 */
2521struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2522 const char *propname)
2523{
2524 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2525 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002526 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02002527 int ret;
2528
2529 if (!fwnode)
2530 return ERR_PTR(-EINVAL);
2531
2532 if (is_of_node(fwnode)) {
2533 enum of_gpio_flags flags;
2534
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002535 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002536 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002537 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02002538 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002539 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2540 }
Mika Westerberg40b73182014-10-21 13:33:59 +02002541 } else if (is_acpi_node(fwnode)) {
2542 struct acpi_gpio_info info;
2543
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02002544 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02002545 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01002546 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02002547 }
2548
2549 if (IS_ERR(desc))
2550 return desc;
2551
Mika Westerberg40b73182014-10-21 13:33:59 +02002552 if (active_low)
2553 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2554
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002555 if (single_ended) {
2556 if (active_low)
2557 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2558 else
2559 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2560 }
2561
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002562 ret = gpiod_request(desc, NULL);
2563 if (ret)
2564 return ERR_PTR(ret);
2565
Mika Westerberg40b73182014-10-21 13:33:59 +02002566 return desc;
2567}
2568EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2569
2570/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002571 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2572 * function
2573 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2574 * @con_id: function within the GPIO consumer
2575 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002576 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002577 *
2578 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2579 * specified index was assigned to the requested function it will return NULL.
2580 * This is convenient for drivers that need to handle optional GPIOs.
2581 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002582struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002583 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002584 unsigned int index,
2585 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002586{
2587 struct gpio_desc *desc;
2588
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002589 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002590 if (IS_ERR(desc)) {
2591 if (PTR_ERR(desc) == -ENOENT)
2592 return NULL;
2593 }
2594
2595 return desc;
2596}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002597EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002598
2599/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002600 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2601 * @desc: gpio whose value will be assigned
2602 * @name: gpio line name
2603 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2604 * of_get_gpio_hog()
2605 * @dflags: gpiod_flags - optional GPIO initialization flags
2606 */
2607int gpiod_hog(struct gpio_desc *desc, const char *name,
2608 unsigned long lflags, enum gpiod_flags dflags)
2609{
2610 struct gpio_chip *chip;
2611 struct gpio_desc *local_desc;
2612 int hwnum;
2613 int status;
2614
2615 chip = gpiod_to_chip(desc);
2616 hwnum = gpio_chip_hwgpio(desc);
2617
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002618 gpiod_parse_flags(desc, lflags);
2619
Benoit Parrotf625d462015-02-02 11:44:44 -06002620 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2621 if (IS_ERR(local_desc)) {
Linus Walleija7138902015-06-05 11:36:10 +02002622 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2623 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002624 return PTR_ERR(local_desc);
2625 }
2626
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002627 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002628 if (status < 0) {
Linus Walleija7138902015-06-05 11:36:10 +02002629 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2630 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002631 gpiochip_free_own_desc(desc);
2632 return status;
2633 }
2634
2635 /* Mark GPIO as hogged so it can be identified and removed later */
2636 set_bit(FLAG_IS_HOGGED, &desc->flags);
2637
2638 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2639 desc_to_gpio(desc), name,
2640 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2641 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2642 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2643
2644 return 0;
2645}
2646
2647/**
2648 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2649 * @chip: gpio chip to act on
2650 *
2651 * This is only used by of_gpiochip_remove to free hogged gpios
2652 */
2653static void gpiochip_free_hogs(struct gpio_chip *chip)
2654{
2655 int id;
2656
2657 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002658 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
2659 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06002660 }
2661}
2662
2663/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002664 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2665 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2666 * @con_id: function within the GPIO consumer
2667 * @flags: optional GPIO initialization flags
2668 *
2669 * This function acquires all the GPIOs defined under a given function.
2670 *
2671 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2672 * no GPIO has been assigned to the requested function, or another IS_ERR()
2673 * code if an error occurred while trying to acquire the GPIOs.
2674 */
2675struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2676 const char *con_id,
2677 enum gpiod_flags flags)
2678{
2679 struct gpio_desc *desc;
2680 struct gpio_descs *descs;
2681 int count;
2682
2683 count = gpiod_count(dev, con_id);
2684 if (count < 0)
2685 return ERR_PTR(count);
2686
2687 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2688 GFP_KERNEL);
2689 if (!descs)
2690 return ERR_PTR(-ENOMEM);
2691
2692 for (descs->ndescs = 0; descs->ndescs < count; ) {
2693 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2694 if (IS_ERR(desc)) {
2695 gpiod_put_array(descs);
2696 return ERR_CAST(desc);
2697 }
2698 descs->desc[descs->ndescs] = desc;
2699 descs->ndescs++;
2700 }
2701 return descs;
2702}
2703EXPORT_SYMBOL_GPL(gpiod_get_array);
2704
2705/**
2706 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2707 * function
2708 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2709 * @con_id: function within the GPIO consumer
2710 * @flags: optional GPIO initialization flags
2711 *
2712 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2713 * assigned to the requested function it will return NULL.
2714 */
2715struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2716 const char *con_id,
2717 enum gpiod_flags flags)
2718{
2719 struct gpio_descs *descs;
2720
2721 descs = gpiod_get_array(dev, con_id, flags);
2722 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2723 return NULL;
2724
2725 return descs;
2726}
2727EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2728
2729/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002730 * gpiod_put - dispose of a GPIO descriptor
2731 * @desc: GPIO descriptor to dispose of
2732 *
2733 * No descriptor can be used after gpiod_put() has been called on it.
2734 */
2735void gpiod_put(struct gpio_desc *desc)
2736{
2737 gpiod_free(desc);
2738}
2739EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002740
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002741/**
2742 * gpiod_put_array - dispose of multiple GPIO descriptors
2743 * @descs: struct gpio_descs containing an array of descriptors
2744 */
2745void gpiod_put_array(struct gpio_descs *descs)
2746{
2747 unsigned int i;
2748
2749 for (i = 0; i < descs->ndescs; i++)
2750 gpiod_put(descs->desc[i]);
2751
2752 kfree(descs);
2753}
2754EXPORT_SYMBOL_GPL(gpiod_put_array);
2755
Linus Walleij3c702e92015-10-21 15:29:53 +02002756static int __init gpiolib_dev_init(void)
2757{
2758 int ret;
2759
2760 /* Register GPIO sysfs bus */
2761 ret = bus_register(&gpio_bus_type);
2762 if (ret < 0) {
2763 pr_err("gpiolib: could not register GPIO bus type\n");
2764 return ret;
2765 }
2766
2767 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
2768 if (ret < 0) {
2769 pr_err("gpiolib: failed to allocate char dev region\n");
2770 bus_unregister(&gpio_bus_type);
2771 }
2772 return ret;
2773}
2774core_initcall(gpiolib_dev_init);
2775
David Brownelld2876d02008-02-04 22:28:20 -08002776#ifdef CONFIG_DEBUG_FS
2777
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002778static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08002779{
2780 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002781 struct gpio_chip *chip = gdev->chip;
2782 unsigned gpio = gdev->base;
2783 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08002784 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002785 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002786
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002787 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02002788 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2789 if (gdesc->name) {
2790 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2791 gpio, gdesc->name);
2792 }
David Brownelld2876d02008-02-04 22:28:20 -08002793 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02002794 }
David Brownelld2876d02008-02-04 22:28:20 -08002795
Alexandre Courbot372e7222013-02-03 01:29:29 +09002796 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002797 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002798 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02002799 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2800 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08002801 is_out ? "out" : "in ",
2802 chip->get
2803 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002804 : "? ",
2805 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002806 seq_printf(s, "\n");
2807 }
2808}
2809
Thierry Redingf9c4a312012-04-12 13:26:01 +02002810static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002811{
Grant Likely362432a2013-02-09 09:41:49 +00002812 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002813 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002814 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002815
2816 s->private = "";
2817
Grant Likely362432a2013-02-09 09:41:49 +00002818 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002819 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00002820 if (index-- == 0) {
2821 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002822 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00002823 }
2824 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002825
2826 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002827}
2828
2829static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2830{
Grant Likely362432a2013-02-09 09:41:49 +00002831 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002832 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002833 void *ret = NULL;
2834
Grant Likely362432a2013-02-09 09:41:49 +00002835 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002836 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002837 ret = NULL;
2838 else
Linus Walleijff2b1352015-10-20 11:10:38 +02002839 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00002840 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002841
2842 s->private = "\n";
2843 ++*pos;
2844
2845 return ret;
2846}
2847
2848static void gpiolib_seq_stop(struct seq_file *s, void *v)
2849{
2850}
2851
2852static int gpiolib_seq_show(struct seq_file *s, void *v)
2853{
Linus Walleijff2b1352015-10-20 11:10:38 +02002854 struct gpio_device *gdev = v;
2855 struct gpio_chip *chip = gdev->chip;
2856 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002857
Linus Walleijff2b1352015-10-20 11:10:38 +02002858 if (!chip) {
2859 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
2860 dev_name(&gdev->dev));
2861 return 0;
2862 }
2863
2864 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
2865 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002866 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02002867 parent = chip->parent;
2868 if (parent)
2869 seq_printf(s, ", parent: %s/%s",
2870 parent->bus ? parent->bus->name : "no-bus",
2871 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02002872 if (chip->label)
2873 seq_printf(s, ", %s", chip->label);
2874 if (chip->can_sleep)
2875 seq_printf(s, ", can sleep");
2876 seq_printf(s, ":\n");
2877
2878 if (chip->dbg_show)
2879 chip->dbg_show(s, chip);
2880 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002881 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002882
David Brownelld2876d02008-02-04 22:28:20 -08002883 return 0;
2884}
2885
Thierry Redingf9c4a312012-04-12 13:26:01 +02002886static const struct seq_operations gpiolib_seq_ops = {
2887 .start = gpiolib_seq_start,
2888 .next = gpiolib_seq_next,
2889 .stop = gpiolib_seq_stop,
2890 .show = gpiolib_seq_show,
2891};
2892
David Brownelld2876d02008-02-04 22:28:20 -08002893static int gpiolib_open(struct inode *inode, struct file *file)
2894{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002895 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002896}
2897
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002898static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002899 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002900 .open = gpiolib_open,
2901 .read = seq_read,
2902 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002903 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002904};
2905
2906static int __init gpiolib_debugfs_init(void)
2907{
2908 /* /sys/kernel/debug/gpio */
2909 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2910 NULL, NULL, &gpiolib_operations);
2911 return 0;
2912}
2913subsys_initcall(gpiolib_debugfs_init);
2914
2915#endif /* DEBUG_FS */