blob: e31d0a1e6f7c941677440525cb70cf2703c50716 [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d2008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020018#include <linux/pinctrl/consumer.h>
Linus Walleijff2b1352015-10-20 11:10:38 +020019#include <linux/idr.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020020#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
23#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080024
Mika Westerberg664e3e52014-01-08 12:40:54 +020025#include "gpiolib.h"
26
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060027#define CREATE_TRACE_POINTS
28#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080029
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070030/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080031 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070032 * The GPIO programming interface allows for inlining speed-critical
33 * get/set operations for common cases, so that access to SOC-integrated
34 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080035 */
36
37
38/* When debugging, extend minimal trust to callers and platform code.
39 * Also emit diagnostic messages that may help initial bringup, when
40 * board setup or driver bugs are most common.
41 *
42 * Otherwise, minimize overhead in what may be bitbanging codepaths.
43 */
44#ifdef DEBUG
45#define extra_checks 1
46#else
47#define extra_checks 0
48#endif
49
Linus Walleijff2b1352015-10-20 11:10:38 +020050/* Device and char device-related information */
51static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020052static dev_t gpio_devt;
53#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
54static struct bus_type gpio_bus_type = {
55 .name = "gpio",
56};
Linus Walleijff2b1352015-10-20 11:10:38 +020057
David Brownelld2876d02008-02-04 22:28:20 -080058/* gpio_lock prevents conflicts during gpio_desc[] table updates.
59 * While any GPIO is requested, its gpio_chip is not removable;
60 * each GPIO's "requested" flag serves as a lock and refcount.
61 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090062DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080063
Alexandre Courbotbae48da2013-10-17 10:21:38 -070064static DEFINE_MUTEX(gpio_lookup_lock);
65static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020066LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020067
68static void gpiochip_free_hogs(struct gpio_chip *chip);
69static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
70
71
David Brownelld2876d02008-02-04 22:28:20 -080072static inline void desc_set_label(struct gpio_desc *d, const char *label)
73{
David Brownelld2876d02008-02-04 22:28:20 -080074 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080075}
76
Alexandre Courbot372e7222013-02-03 01:29:29 +090077/**
78 * Convert a GPIO number to its descriptor
79 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070080struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090081{
Linus Walleijff2b1352015-10-20 11:10:38 +020082 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +090083 unsigned long flags;
84
85 spin_lock_irqsave(&gpio_lock, flags);
86
Linus Walleijff2b1352015-10-20 11:10:38 +020087 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +010088 if (gdev->base <= gpio &&
89 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +090090 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +010091 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +090092 }
93 }
94
95 spin_unlock_irqrestore(&gpio_lock, flags);
96
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +090097 if (!gpio_is_valid(gpio))
98 WARN(1, "invalid GPIO %d\n", gpio);
99
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900100 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900101}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700102EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900103
104/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900105 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +0200106 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900107struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
108 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200109{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100110 struct gpio_device *gdev = chip->gpiodev;
111
112 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900113 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200114
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100115 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200116}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900117
118/**
119 * Convert a GPIO descriptor to the integer namespace.
120 * This should disappear in the future but is needed since we still
121 * use GPIO numbers for error messages and sysfs nodes
122 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700123int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900124{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100125 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900126}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700127EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900128
129
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700130/**
131 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
132 * @desc: descriptor to return the chip of
133 */
134struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900135{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100136 if (!desc || !desc->gdev || !desc->gdev->chip)
137 return NULL;
138 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900139}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700140EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800141
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700142/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
143static int gpiochip_find_base(int ngpio)
144{
Linus Walleijff2b1352015-10-20 11:10:38 +0200145 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900146 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700147
Linus Walleijff2b1352015-10-20 11:10:38 +0200148 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900149 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100150 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900151 break;
152 else
153 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100154 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700155 }
156
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900157 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700158 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900159 return base;
160 } else {
161 pr_err("%s: cannot find free range\n", __func__);
162 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700163 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700164}
165
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700166/**
167 * gpiod_get_direction - return the current direction of a GPIO
168 * @desc: GPIO to get the direction of
169 *
170 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
171 *
172 * This function may sleep if gpiod_cansleep() is true.
173 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900174int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300175{
176 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900177 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300178 int status = -EINVAL;
179
Alexandre Courbot372e7222013-02-03 01:29:29 +0900180 chip = gpiod_to_chip(desc);
181 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300182
183 if (!chip->get_direction)
184 return status;
185
Alexandre Courbot372e7222013-02-03 01:29:29 +0900186 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300187 if (status > 0) {
188 /* GPIOF_DIR_IN, or other positive */
189 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900190 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300191 }
192 if (status == 0) {
193 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900194 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300195 }
196 return status;
197}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700198EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300199
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900200/*
201 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800202 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900203 *
204 * Return -EBUSY if the new chip overlaps with some other chip's integer
205 * space.
206 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200207static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900208{
Linus Walleijff2b1352015-10-20 11:10:38 +0200209 struct gpio_device *iterator;
210 struct gpio_device *previous = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900211
Linus Walleijff2b1352015-10-20 11:10:38 +0200212 if (!gdev->chip)
213 return -EINVAL;
214
215 if (list_empty(&gpio_devices)) {
216 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530217 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900218 }
219
Linus Walleijff2b1352015-10-20 11:10:38 +0200220 list_for_each_entry(iterator, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100221 if (iterator->base >= gdev->base + gdev->ngpio) {
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800222 /*
223 * Iterator is the first GPIO chip so there is no
224 * previous one
225 */
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530226 if (!previous) {
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800227 goto found;
228 } else {
229 /*
230 * We found a valid range(means
231 * [base, base + ngpio - 1]) between previous
232 * and iterator chip.
233 */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100234 if (previous->base + previous->ngpio
235 <= gdev->base)
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800236 goto found;
237 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900238 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800239 previous = iterator;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900240 }
241
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530242 /*
243 * We are beyond the last chip in the list and iterator now
244 * points to the head.
245 * Let iterator point to the last chip in the list.
246 */
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900247
Linus Walleijff2b1352015-10-20 11:10:38 +0200248 iterator = list_last_entry(&gpio_devices, struct gpio_device, list);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100249 if (iterator->base + iterator->ngpio <= gdev->base) {
Linus Walleijff2b1352015-10-20 11:10:38 +0200250 list_add(&gdev->list, &iterator->list);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500251 return 0;
252 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900253
Linus Walleijff2b1352015-10-20 11:10:38 +0200254 dev_err(&gdev->dev,
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800255 "GPIO integer space overlap, cannot add chip\n");
256 return -EBUSY;
257
258found:
Linus Walleijff2b1352015-10-20 11:10:38 +0200259 list_add_tail(&gdev->list, &iterator->list);
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800260 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900261}
262
Linus Walleijf881bab2015-09-23 16:20:43 -0700263/**
264 * Convert a GPIO name to its descriptor
265 */
266static struct gpio_desc *gpio_name_to_desc(const char * const name)
267{
Linus Walleijff2b1352015-10-20 11:10:38 +0200268 struct gpio_device *gdev;
Linus Walleijf881bab2015-09-23 16:20:43 -0700269 unsigned long flags;
270
271 spin_lock_irqsave(&gpio_lock, flags);
272
Linus Walleijff2b1352015-10-20 11:10:38 +0200273 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700274 int i;
275
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100276 for (i = 0; i != gdev->ngpio; ++i) {
277 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab2015-09-23 16:20:43 -0700278
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100279 if (!desc->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700280 continue;
281
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100282 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700283 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100284 return desc;
Linus Walleijf881bab2015-09-23 16:20:43 -0700285 }
286 }
287 }
288
289 spin_unlock_irqrestore(&gpio_lock, flags);
290
291 return NULL;
292}
293
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200294/*
295 * Takes the names from gc->names and checks if they are all unique. If they
296 * are, they are assigned to their gpio descriptors.
297 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800298 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200299 */
300static int gpiochip_set_desc_names(struct gpio_chip *gc)
301{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100302 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200303 int i;
304
305 if (!gc->names)
306 return 0;
307
308 /* First check all names if they are unique */
309 for (i = 0; i != gc->ngpio; ++i) {
310 struct gpio_desc *gpio;
311
312 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700313 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100314 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200315 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab2015-09-23 16:20:43 -0700316 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200317 }
318
319 /* Then add all names to the GPIO descriptors */
320 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100321 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200322
323 return 0;
324}
325
Linus Walleij3c702e92015-10-21 15:29:53 +0200326/**
327 * gpio_ioctl() - ioctl handler for the GPIO chardev
328 */
329static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
330{
331 struct gpio_device *gdev = filp->private_data;
332 struct gpio_chip *chip = gdev->chip;
333 int __user *ip = (int __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200334
335 /* We fail any subsequent ioctl():s when the chip is gone */
336 if (!chip)
337 return -ENODEV;
338
Linus Walleij521a2ad2016-02-12 22:25:22 +0100339 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200340 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100341 struct gpiochip_info chipinfo;
342
Linus Walleij3c702e92015-10-21 15:29:53 +0200343 strncpy(chipinfo.name, dev_name(&gdev->dev),
344 sizeof(chipinfo.name));
345 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100346 strncpy(chipinfo.label, gdev->label,
347 sizeof(chipinfo.label));
348 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100349 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200350 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
351 return -EFAULT;
352 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100353 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
354 struct gpioline_info lineinfo;
355 struct gpio_desc *desc;
356
357 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
358 return -EFAULT;
359 if (lineinfo.line_offset > gdev->ngpio)
360 return -EINVAL;
361
362 desc = &gdev->descs[lineinfo.line_offset];
363 if (desc->name) {
364 strncpy(lineinfo.name, desc->name,
365 sizeof(lineinfo.name));
366 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
367 } else {
368 lineinfo.name[0] = '\0';
369 }
370 if (desc->label) {
371 strncpy(lineinfo.label, desc->label,
372 sizeof(lineinfo.label));
373 lineinfo.label[sizeof(lineinfo.label)-1] = '\0';
374 } else {
375 lineinfo.label[0] = '\0';
376 }
377
378 /*
379 * Userspace only need to know that the kernel is using
380 * this GPIO so it can't use it.
381 */
382 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100383 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
384 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
385 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
386 test_bit(FLAG_EXPORT, &desc->flags) ||
387 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100388 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100389 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100390 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100391 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100392 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100393 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100394 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100395 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100396 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
397
398 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
399 return -EFAULT;
400 return 0;
Linus Walleij3c702e92015-10-21 15:29:53 +0200401 }
402 return -EINVAL;
403}
404
405/**
406 * gpio_chrdev_open() - open the chardev for ioctl operations
407 * @inode: inode for this chardev
408 * @filp: file struct for storing private data
409 * Returns 0 on success
410 */
411static int gpio_chrdev_open(struct inode *inode, struct file *filp)
412{
413 struct gpio_device *gdev = container_of(inode->i_cdev,
414 struct gpio_device, chrdev);
415
416 /* Fail on open if the backing gpiochip is gone */
417 if (!gdev || !gdev->chip)
418 return -ENODEV;
419 get_device(&gdev->dev);
420 filp->private_data = gdev;
421 return 0;
422}
423
424/**
425 * gpio_chrdev_release() - close chardev after ioctl operations
426 * @inode: inode for this chardev
427 * @filp: file struct for storing private data
428 * Returns 0 on success
429 */
430static int gpio_chrdev_release(struct inode *inode, struct file *filp)
431{
432 struct gpio_device *gdev = container_of(inode->i_cdev,
433 struct gpio_device, chrdev);
434
435 if (!gdev)
436 return -ENODEV;
437 put_device(&gdev->dev);
438 return 0;
439}
440
441
442static const struct file_operations gpio_fileops = {
443 .release = gpio_chrdev_release,
444 .open = gpio_chrdev_open,
445 .owner = THIS_MODULE,
446 .llseek = noop_llseek,
447 .unlocked_ioctl = gpio_ioctl,
448 .compat_ioctl = gpio_ioctl,
449};
450
Linus Walleijff2b1352015-10-20 11:10:38 +0200451static void gpiodevice_release(struct device *dev)
452{
453 struct gpio_device *gdev = dev_get_drvdata(dev);
454
Linus Walleij3c702e92015-10-21 15:29:53 +0200455 cdev_del(&gdev->chrdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200456 list_del(&gdev->list);
457 ida_simple_remove(&gpio_ida, gdev->id);
Linus Walleij9efd9e62016-02-09 14:27:42 +0100458 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200459}
460
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700461/**
Linus Walleijb08ea352015-12-03 15:14:13 +0100462 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -0800463 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900464 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -0800465 *
466 * Returns a negative errno if the chip can't be registered, such as
467 * because the chip->base is invalid or already associated with a
468 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700469 *
Linus Walleijb08ea352015-12-03 15:14:13 +0100470 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800471 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d2008-07-25 01:46:07 -0700472 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
473 * for GPIOs will fail rudely.
474 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700475 * If chip->base is negative, this requests dynamic assignment of
476 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800477 */
Linus Walleijb08ea352015-12-03 15:14:13 +0100478int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -0800479{
480 unsigned long flags;
481 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +0200482 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700483 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +0200484 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -0800485
Linus Walleijff2b1352015-10-20 11:10:38 +0200486 /*
487 * First: allocate and populate the internal stat container, and
488 * set up the struct device.
489 */
Josh Cartwright969f07b2016-02-17 16:44:15 -0600490 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +0200491 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900492 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +0200493 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +0200494 gdev->chip = chip;
495 chip->gpiodev = gdev;
496 if (chip->parent) {
497 gdev->dev.parent = chip->parent;
498 gdev->dev.of_node = chip->parent->of_node;
499 } else {
500#ifdef CONFIG_OF_GPIO
501 /* If the gpiochip has an assigned OF node this takes precedence */
502 if (chip->of_node)
503 gdev->dev.of_node = chip->of_node;
504#endif
505 }
506 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
507 if (gdev->id < 0) {
508 status = gdev->id;
509 goto err_free_gdev;
510 }
511 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
512 device_initialize(&gdev->dev);
513 dev_set_drvdata(&gdev->dev, gdev);
514 if (chip->parent && chip->parent->driver)
515 gdev->owner = chip->parent->driver->owner;
516 else if (chip->owner)
517 /* TODO: remove chip->owner */
518 gdev->owner = chip->owner;
519 else
520 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -0800521
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100522 gdev->descs = devm_kcalloc(&gdev->dev, chip->ngpio,
523 sizeof(gdev->descs[0]), GFP_KERNEL);
524 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +0200525 status = -ENOMEM;
526 goto err_free_gdev;
527 }
528
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800529 if (chip->ngpio == 0) {
530 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +0200531 status = -EINVAL;
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100532 goto err_free_gdev;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800533 }
Linus Walleijdf4878e2016-02-12 14:48:23 +0100534
535 if (chip->label)
536 gdev->label = devm_kstrdup(&gdev->dev, chip->label, GFP_KERNEL);
537 else
538 gdev->label = devm_kstrdup(&gdev->dev, "unknown", GFP_KERNEL);
539 if (!gdev->label) {
540 status = -ENOMEM;
541 goto err_free_gdev;
542 }
543
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100544 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +0100545 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800546
David Brownelld2876d02008-02-04 22:28:20 -0800547 spin_lock_irqsave(&gpio_lock, flags);
548
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100549 /*
550 * TODO: this allocates a Linux GPIO number base in the global
551 * GPIO numberspace for this chip. In the long run we want to
552 * get *rid* of this numberspace and use only descriptors, but
553 * it may be a pipe dream. It will not happen before we get rid
554 * of the sysfs interface anyways.
555 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700556 if (base < 0) {
557 base = gpiochip_find_base(chip->ngpio);
558 if (base < 0) {
559 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100560 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100561 goto err_free_gdev;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700562 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100563 /*
564 * TODO: it should not be necessary to reflect the assigned
565 * base outside of the GPIO subsystem. Go over drivers and
566 * see if anyone makes use of this, else drop this and assign
567 * a poison instead.
568 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700569 chip->base = base;
570 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100571 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700572
Linus Walleijff2b1352015-10-20 11:10:38 +0200573 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +0100574 if (status) {
575 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100576 goto err_free_gdev;
Johan Hovold05aa5202015-01-12 17:12:26 +0100577 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900578
Linus Walleijff2b1352015-10-20 11:10:38 +0200579 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100580 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d2008-07-25 01:46:07 -0700581
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100582 desc->gdev = gdev;
Johan Hovold05aa5202015-01-12 17:12:26 +0100583
584 /* REVISIT: most hardware initializes GPIOs as inputs (often
585 * with pullups enabled) so power usage is minimized. Linux
586 * code should set the gpio direction first thing; but until
587 * it does, and in case chip->get_direction is not set, we may
588 * expose the wrong direction in sysfs.
589 */
590 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800591 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900592
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800593 spin_unlock_irqrestore(&gpio_lock, flags);
594
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530595#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +0100596 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530597#endif
598
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200599 status = gpiochip_set_desc_names(chip);
600 if (status)
601 goto err_remove_from_list;
602
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200603 status = of_gpiochip_add(chip);
604 if (status)
605 goto err_remove_chip;
606
Mika Westerberg664e3e52014-01-08 12:40:54 +0200607 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600608
Linus Walleij3c702e92015-10-21 15:29:53 +0200609 /*
610 * By first adding the chardev, and then adding the device,
611 * we get a device node entry in sysfs under
612 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
613 * coldplug of device nodes and other udev business.
614 */
615 cdev_init(&gdev->chrdev, &gpio_fileops);
616 gdev->chrdev.owner = THIS_MODULE;
617 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
618 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
619 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
620 if (status < 0)
621 chip_warn(chip, "failed to add char device %d:%d\n",
622 MAJOR(gpio_devt), gdev->id);
623 else
624 chip_dbg(chip, "added GPIO chardev (%d:%d)\n",
625 MAJOR(gpio_devt), gdev->id);
Linus Walleijff2b1352015-10-20 11:10:38 +0200626 status = device_add(&gdev->dev);
Johan Hovold225fce82015-01-12 17:12:25 +0100627 if (status)
Linus Walleij3c702e92015-10-21 15:29:53 +0200628 goto err_remove_chardev;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600629
Linus Walleijafbc4f32016-02-09 13:21:06 +0100630 status = gpiochip_sysfs_register(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200631 if (status)
632 goto err_remove_device;
633
634 /* From this point, the .release() function cleans up gpio_device */
635 gdev->dev.release = gpiodevice_release;
636 get_device(&gdev->dev);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100637 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
638 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
639 dev_name(&gdev->dev), chip->label ? : "generic");
Grant Likely64842aa2011-11-06 11:36:18 -0700640
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600641 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800642
Linus Walleijff2b1352015-10-20 11:10:38 +0200643err_remove_device:
644 device_del(&gdev->dev);
Linus Walleij3c702e92015-10-21 15:29:53 +0200645err_remove_chardev:
646 cdev_del(&gdev->chrdev);
Johan Hovold225fce82015-01-12 17:12:25 +0100647err_remove_chip:
648 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200649 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100650 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200651err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100652 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200653 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800654 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200655err_free_gdev:
656 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -0800657 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200658 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100659 gdev->base, gdev->base + gdev->ngpio - 1,
660 chip->label ? : "generic");
661 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -0800662 return status;
663}
Linus Walleijb08ea352015-12-03 15:14:13 +0100664EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -0800665
666/**
Linus Walleij43c54ec2016-02-11 11:37:48 +0100667 * gpiochip_get_data() - get per-subdriver data for the chip
668 */
669void *gpiochip_get_data(struct gpio_chip *chip)
670{
671 return chip->gpiodev->data;
672}
673EXPORT_SYMBOL_GPL(gpiochip_get_data);
674
675/**
David Brownelld2876d02008-02-04 22:28:20 -0800676 * gpiochip_remove() - unregister a gpio_chip
677 * @chip: the chip to unregister
678 *
679 * A gpio_chip with any GPIOs still requested may not be removed.
680 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200681void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800682{
Linus Walleijff2b1352015-10-20 11:10:38 +0200683 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200684 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800685 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100686 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200687 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800688
Linus Walleijff2b1352015-10-20 11:10:38 +0200689 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +0100690 gpiochip_sysfs_unregister(gdev);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +0800691 /* Numb the device, cancelling all outstanding operations */
692 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +0100693 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200694 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100695 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600696 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600697 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +0100698 /*
699 * We accept no more calls into the driver from this point, so
700 * NULL the driver data pointer
701 */
702 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -0600703
Johan Hovold6798aca2015-01-12 17:12:28 +0100704 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100705 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100706 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +0200707 if (test_bit(FLAG_REQUESTED, &desc->flags))
708 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800709 }
David Brownelld2876d02008-02-04 22:28:20 -0800710 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900711
Johan Hovoldfab28b82015-05-04 17:10:27 +0200712 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100713 dev_crit(&gdev->dev,
Linus Walleij58383c72015-11-04 09:56:26 +0100714 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +0200715
Linus Walleijff2b1352015-10-20 11:10:38 +0200716 /*
717 * The gpiochip side puts its use of the device to rest here:
718 * if there are no userspace clients, the chardev and device will
719 * be removed, else it will be dangling until the last user is
720 * gone.
721 */
722 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -0800723}
724EXPORT_SYMBOL_GPL(gpiochip_remove);
725
Laxman Dewangan0cf32922016-02-15 16:32:09 +0530726static void devm_gpio_chip_release(struct device *dev, void *res)
727{
728 struct gpio_chip *chip = *(struct gpio_chip **)res;
729
730 gpiochip_remove(chip);
731}
732
733static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
734
735{
736 struct gpio_chip **r = res;
737
738 if (!r || !*r) {
739 WARN_ON(!r || !*r);
740 return 0;
741 }
742
743 return *r == data;
744}
745
746/**
747 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
748 * @dev: the device pointer on which irq_chip belongs to.
749 * @chip: the chip to register, with chip->base initialized
750 * Context: potentially before irqs will work
751 *
752 * Returns a negative errno if the chip can't be registered, such as
753 * because the chip->base is invalid or already associated with a
754 * different chip. Otherwise it returns zero as a success code.
755 *
756 * The gpio chip automatically be released when the device is unbound.
757 */
758int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
759 void *data)
760{
761 struct gpio_chip **ptr;
762 int ret;
763
764 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
765 GFP_KERNEL);
766 if (!ptr)
767 return -ENOMEM;
768
769 ret = gpiochip_add_data(chip, data);
770 if (ret < 0) {
771 devres_free(ptr);
772 return ret;
773 }
774
775 *ptr = chip;
776 devres_add(dev, ptr);
777
778 return 0;
779}
780EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
781
782/**
783 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
784 * @dev: device for which which resource was allocated
785 * @chip: the chip to remove
786 *
787 * A gpio_chip with any GPIOs still requested may not be removed.
788 */
789void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
790{
791 int ret;
792
793 ret = devres_release(dev, devm_gpio_chip_release,
794 devm_gpio_chip_match, chip);
795 if (!ret)
796 WARN_ON(ret);
797}
798EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
799
Grant Likely594fa262010-06-08 07:48:16 -0600800/**
801 * gpiochip_find() - iterator for locating a specific gpio_chip
802 * @data: data to pass to match function
803 * @callback: Callback function to check gpio_chip
804 *
805 * Similar to bus_find_device. It returns a reference to a gpio_chip as
806 * determined by a user supplied @match callback. The callback should return
807 * 0 if the device doesn't match and non-zero if it does. If the callback is
808 * non-zero, this function will return to the caller and not iterate over any
809 * more gpio_chips.
810 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600811struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700812 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600813 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600814{
Linus Walleijff2b1352015-10-20 11:10:38 +0200815 struct gpio_device *gdev;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900816 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600817 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600818
819 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200820 list_for_each_entry(gdev, &gpio_devices, list)
821 if (match(gdev->chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600822 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900823
824 /* No match? */
Linus Walleijff2b1352015-10-20 11:10:38 +0200825 if (&gdev->list == &gpio_devices)
Alexandre Courbot125eef92013-02-03 01:29:26 +0900826 chip = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +0200827 else
828 chip = gdev->chip;
829
Grant Likely594fa262010-06-08 07:48:16 -0600830 spin_unlock_irqrestore(&gpio_lock, flags);
831
832 return chip;
833}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600834EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800835
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900836static int gpiochip_match_name(struct gpio_chip *chip, void *data)
837{
838 const char *name = data;
839
840 return !strcmp(chip->label, name);
841}
842
843static struct gpio_chip *find_chip_by_name(const char *name)
844{
845 return gpiochip_find((void *)name, gpiochip_match_name);
846}
847
Linus Walleij14250522014-03-25 10:40:18 +0100848#ifdef CONFIG_GPIOLIB_IRQCHIP
849
850/*
851 * The following is irqchip helper code for gpiochips.
852 */
853
854/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200855 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
856 * @gpiochip: the gpiochip to set the irqchip chain to
857 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100858 * @parent_irq: the irq number corresponding to the parent IRQ for this
859 * chained irqchip
860 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200861 * coming out of the gpiochip. If the interrupt is nested rather than
862 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100863 */
864void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
865 struct irq_chip *irqchip,
866 int parent_irq,
867 irq_flow_handler_t parent_handler)
868{
Linus Walleij83141a72014-09-26 13:50:12 +0200869 unsigned int offset;
870
Linus Walleij83141a72014-09-26 13:50:12 +0200871 if (!gpiochip->irqdomain) {
872 chip_err(gpiochip, "called %s before setting up irqchip\n",
873 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200874 return;
875 }
876
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200877 if (parent_handler) {
878 if (gpiochip->can_sleep) {
879 chip_err(gpiochip,
880 "you cannot have chained interrupts on a "
881 "chip that may sleep\n");
882 return;
883 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200884 /*
885 * The parent irqchip is already using the chip_data for this
886 * irqchip, so our callbacks simply use the handler_data.
887 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +0200888 irq_set_chained_handler_and_data(parent_irq, parent_handler,
889 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300890
891 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200892 }
Linus Walleij83141a72014-09-26 13:50:12 +0200893
894 /* Set the parent IRQ for all affected IRQs */
895 for (offset = 0; offset < gpiochip->ngpio; offset++)
896 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
897 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100898}
899EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
900
901/**
902 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
903 * @d: the irqdomain used by this irqchip
904 * @irq: the global irq number used by this GPIO irqchip irq
905 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
906 *
907 * This function will set up the mapping for a certain IRQ line on a
908 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
909 * stored inside the gpiochip.
910 */
911static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
912 irq_hw_number_t hwirq)
913{
914 struct gpio_chip *chip = d->host_data;
915
Linus Walleij14250522014-03-25 10:40:18 +0100916 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300917 /*
918 * This lock class tells lockdep that GPIO irqs are in a different
919 * category than their parents, so it won't report false recursion.
920 */
921 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +0200922 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200923 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300924 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200925 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100926 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -0500927
Linus Walleij1333b902014-04-23 16:45:12 +0200928 /*
929 * No set-up of the hardware will happen if IRQ_TYPE_NONE
930 * is passed as default type.
931 */
932 if (chip->irq_default_type != IRQ_TYPE_NONE)
933 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100934
935 return 0;
936}
937
Linus Walleijc3626fd2014-03-28 20:42:01 +0100938static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
939{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200940 struct gpio_chip *chip = d->host_data;
941
Linus Walleij1c8732b2014-04-09 13:34:39 +0200942 if (chip->can_sleep)
943 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100944 irq_set_chip_and_handler(irq, NULL, NULL);
945 irq_set_chip_data(irq, NULL);
946}
947
Linus Walleij14250522014-03-25 10:40:18 +0100948static const struct irq_domain_ops gpiochip_domain_ops = {
949 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100950 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100951 /* Virtually all GPIO irqchips are twocell:ed */
952 .xlate = irq_domain_xlate_twocell,
953};
954
955static int gpiochip_irq_reqres(struct irq_data *d)
956{
957 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
958
Linus Walleijff2b1352015-10-20 11:10:38 +0200959 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300960 return -ENODEV;
961
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900962 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100963 chip_err(chip,
964 "unable to lock HW IRQ %lu for IRQ\n",
965 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200966 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100967 return -EINVAL;
968 }
969 return 0;
970}
971
972static void gpiochip_irq_relres(struct irq_data *d)
973{
974 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
975
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900976 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200977 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100978}
979
980static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
981{
982 return irq_find_mapping(chip->irqdomain, offset);
983}
984
985/**
986 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
987 * @gpiochip: the gpiochip to remove the irqchip from
988 *
989 * This is called only from gpiochip_remove()
990 */
991static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
992{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100993 unsigned int offset;
994
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300995 acpi_gpiochip_free_interrupts(gpiochip);
996
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300997 if (gpiochip->irq_parent) {
998 irq_set_chained_handler(gpiochip->irq_parent, NULL);
999 irq_set_handler_data(gpiochip->irq_parent, NULL);
1000 }
1001
Linus Walleijc3626fd2014-03-28 20:42:01 +01001002 /* Remove all IRQ mappings and delete the domain */
1003 if (gpiochip->irqdomain) {
1004 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +03001005 irq_dispose_mapping(
1006 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +01001007 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001008 }
Linus Walleij14250522014-03-25 10:40:18 +01001009
1010 if (gpiochip->irqchip) {
1011 gpiochip->irqchip->irq_request_resources = NULL;
1012 gpiochip->irqchip->irq_release_resources = NULL;
1013 gpiochip->irqchip = NULL;
1014 }
1015}
1016
1017/**
1018 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
1019 * @gpiochip: the gpiochip to add the irqchip to
1020 * @irqchip: the irqchip to add to the gpiochip
1021 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1022 * allocate gpiochip irqs from
1023 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001024 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1025 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001026 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001027 *
1028 * This function closely associates a certain irqchip with a certain
1029 * gpiochip, providing an irq domain to translate the local IRQs to
1030 * global irqs in the gpiolib core, and making sure that the gpiochip
1031 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001032 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001033 * from the gpiochip passed as chip data. An irqdomain will be stored
1034 * in the gpiochip that shall be used by the driver to handle IRQ number
1035 * translation. The gpiochip will need to be initialized and registered
1036 * before calling this function.
1037 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001038 * This function will handle two cell:ed simple IRQs and assumes all
1039 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001040 * need to be open coded.
1041 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001042int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
1043 struct irq_chip *irqchip,
1044 unsigned int first_irq,
1045 irq_flow_handler_t handler,
1046 unsigned int type,
1047 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001048{
1049 struct device_node *of_node;
1050 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001051 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001052
1053 if (!gpiochip || !irqchip)
1054 return -EINVAL;
1055
Linus Walleij58383c72015-11-04 09:56:26 +01001056 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001057 pr_err("missing gpiochip .dev parent pointer\n");
1058 return -EINVAL;
1059 }
Linus Walleij58383c72015-11-04 09:56:26 +01001060 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001061#ifdef CONFIG_OF_GPIO
1062 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001063 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001064 * FIXME: get rid of this and use gpiochip->parent->of_node
1065 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001066 */
1067 if (gpiochip->of_node)
1068 of_node = gpiochip->of_node;
1069#endif
1070 gpiochip->irqchip = irqchip;
1071 gpiochip->irq_handler = handler;
1072 gpiochip->irq_default_type = type;
1073 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001074 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +01001075 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1076 gpiochip->ngpio, first_irq,
1077 &gpiochip_domain_ops, gpiochip);
1078 if (!gpiochip->irqdomain) {
1079 gpiochip->irqchip = NULL;
1080 return -EINVAL;
1081 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001082
1083 /*
1084 * It is possible for a driver to override this, but only if the
1085 * alternative functions are both implemented.
1086 */
1087 if (!irqchip->irq_request_resources &&
1088 !irqchip->irq_release_resources) {
1089 irqchip->irq_request_resources = gpiochip_irq_reqres;
1090 irqchip->irq_release_resources = gpiochip_irq_relres;
1091 }
Linus Walleij14250522014-03-25 10:40:18 +01001092
1093 /*
1094 * Prepare the mapping since the irqchip shall be orthogonal to
1095 * any gpiochip calls. If the first_irq was zero, this is
1096 * necessary to allocate descriptors for all IRQs.
1097 */
Linus Walleijc3626fd2014-03-28 20:42:01 +01001098 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1099 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
1100 if (offset == 0)
1101 /*
1102 * Store the base into the gpiochip to be used when
1103 * unmapping the irqs.
1104 */
1105 gpiochip->irq_base = irq_base;
1106 }
Linus Walleij14250522014-03-25 10:40:18 +01001107
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001108 acpi_gpiochip_request_interrupts(gpiochip);
1109
Linus Walleij14250522014-03-25 10:40:18 +01001110 return 0;
1111}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001112EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +01001113
1114#else /* CONFIG_GPIOLIB_IRQCHIP */
1115
1116static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
1117
1118#endif /* CONFIG_GPIOLIB_IRQCHIP */
1119
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001120/**
1121 * gpiochip_generic_request() - request the gpio function for a pin
1122 * @chip: the gpiochip owning the GPIO
1123 * @offset: the offset of the GPIO to request for GPIO function
1124 */
1125int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1126{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001127 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001128}
1129EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1130
1131/**
1132 * gpiochip_generic_free() - free the gpio function from a pin
1133 * @chip: the gpiochip to request the gpio function for
1134 * @offset: the offset of the GPIO to free from GPIO function
1135 */
1136void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1137{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001138 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001139}
1140EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1141
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301142#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001143
Linus Walleij3f0f8672012-11-20 12:40:15 +01001144/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001145 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1146 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001147 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001148 * @gpio_offset: the start offset in the current gpio_chip number space
1149 * @pin_group: name of the pin group inside the pin controller
1150 */
1151int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1152 struct pinctrl_dev *pctldev,
1153 unsigned int gpio_offset, const char *pin_group)
1154{
1155 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001156 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001157 int ret;
1158
1159 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1160 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001161 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001162 return -ENOMEM;
1163 }
1164
1165 /* Use local offset as range ID */
1166 pin_range->range.id = gpio_offset;
1167 pin_range->range.gc = chip;
1168 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001169 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001170 pin_range->pctldev = pctldev;
1171
1172 ret = pinctrl_get_group_pins(pctldev, pin_group,
1173 &pin_range->range.pins,
1174 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001175 if (ret < 0) {
1176 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001177 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001178 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001179
1180 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1181
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001182 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1183 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001184 pinctrl_dev_get_devname(pctldev), pin_group);
1185
Linus Walleij20ec3e32016-02-11 11:03:06 +01001186 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001187
1188 return 0;
1189}
1190EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1191
1192/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001193 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1194 * @chip: the gpiochip to add the range for
1195 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001196 * @gpio_offset: the start offset in the current gpio_chip number space
1197 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001198 * @npins: the number of pins from the offset of each pin space (GPIO and
1199 * pin controller) to accumulate in this range
1200 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001201int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001202 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001203 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301204{
1205 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001206 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001207 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301208
Linus Walleij3f0f8672012-11-20 12:40:15 +01001209 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301210 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001211 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001212 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301213 }
1214
Linus Walleij3f0f8672012-11-20 12:40:15 +01001215 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001216 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001217 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301218 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001219 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001220 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301221 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001222 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301223 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001224 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001225 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001226 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001227 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001228 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001229 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001230 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1231 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001232 pinctl_name,
1233 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301234
Linus Walleij20ec3e32016-02-11 11:03:06 +01001235 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001236
1237 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301238}
Linus Walleij165adc92012-11-06 14:49:39 +01001239EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301240
Linus Walleij3f0f8672012-11-20 12:40:15 +01001241/**
1242 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1243 * @chip: the chip to remove all the mappings for
1244 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301245void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1246{
1247 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01001248 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301249
Linus Walleij20ec3e32016-02-11 11:03:06 +01001250 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301251 list_del(&pin_range->node);
1252 pinctrl_remove_gpio_range(pin_range->pctldev,
1253 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001254 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301255 }
1256}
Linus Walleij165adc92012-11-06 14:49:39 +01001257EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1258
1259#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301260
David Brownelld2876d02008-02-04 22:28:20 -08001261/* These "optional" allocation calls help prevent drivers from stomping
1262 * on each other, and help provide better diagnostics in debugfs.
1263 * They're called even less than the "set direction" calls.
1264 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001265static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001266{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001267 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001268 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001269 unsigned long flags;
1270
1271 spin_lock_irqsave(&gpio_lock, flags);
1272
David Brownelld2876d02008-02-04 22:28:20 -08001273 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001274 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001275 */
1276
1277 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1278 desc_set_label(desc, label ? : "?");
1279 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001280 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001281 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001282 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001283 }
1284
1285 if (chip->request) {
1286 /* chip->request may sleep */
1287 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001288 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001289 spin_lock_irqsave(&gpio_lock, flags);
1290
1291 if (status < 0) {
1292 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001293 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001294 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001295 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001296 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001297 if (chip->get_direction) {
1298 /* chip->get_direction may sleep */
1299 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001300 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001301 spin_lock_irqsave(&gpio_lock, flags);
1302 }
David Brownelld2876d02008-02-04 22:28:20 -08001303done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +03001304 if (status < 0) {
1305 /* Clear flags that might have been set by the caller before
1306 * requesting the GPIO.
1307 */
1308 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1309 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1310 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1311 }
Mika Westerberg77c2d792014-03-10 14:54:50 +02001312 spin_unlock_irqrestore(&gpio_lock, flags);
1313 return status;
1314}
1315
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001316/*
1317 * This descriptor validation needs to be inserted verbatim into each
1318 * function taking a descriptor, so we need to use a preprocessor
1319 * macro to avoid endless duplication.
1320 */
1321#define VALIDATE_DESC(desc) do { \
1322 if (!desc || !desc->gdev) { \
1323 pr_warn("%s: invalid GPIO\n", __func__); \
1324 return -EINVAL; \
1325 } \
1326 if ( !desc->gdev->chip ) { \
1327 dev_warn(&desc->gdev->dev, \
1328 "%s: backing chip is gone\n", __func__); \
1329 return 0; \
1330 } } while (0)
1331
1332#define VALIDATE_DESC_VOID(desc) do { \
1333 if (!desc || !desc->gdev) { \
1334 pr_warn("%s: invalid GPIO\n", __func__); \
1335 return; \
1336 } \
1337 if (!desc->gdev->chip) { \
1338 dev_warn(&desc->gdev->dev, \
1339 "%s: backing chip is gone\n", __func__); \
1340 return; \
1341 } } while (0)
1342
1343
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001344int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001345{
1346 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001347 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001348
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001349 VALIDATE_DESC(desc);
1350 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001351
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001352 if (try_module_get(gdev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001353 status = __gpiod_request(desc, label);
1354 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001355 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001356 else
1357 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001358 }
1359
David Brownelld2876d02008-02-04 22:28:20 -08001360 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001361 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001362
David Brownelld2876d02008-02-04 22:28:20 -08001363 return status;
1364}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001365
Mika Westerberg77c2d792014-03-10 14:54:50 +02001366static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001367{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001368 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001369 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001370 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001371
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001372 might_sleep();
1373
Alexandre Courbot372e7222013-02-03 01:29:29 +09001374 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001375
David Brownelld2876d02008-02-04 22:28:20 -08001376 spin_lock_irqsave(&gpio_lock, flags);
1377
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001378 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07001379 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1380 if (chip->free) {
1381 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001382 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001383 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001384 spin_lock_irqsave(&gpio_lock, flags);
1385 }
David Brownelld2876d02008-02-04 22:28:20 -08001386 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001387 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001388 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301389 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301390 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001391 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001392 ret = true;
1393 }
David Brownelld2876d02008-02-04 22:28:20 -08001394
1395 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001396 return ret;
1397}
1398
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001399void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001400{
Linus Walleij33a68e82016-02-11 10:28:44 +01001401 if (desc && desc->gdev && __gpiod_free(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001402 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001403 put_device(&desc->gdev->dev);
1404 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001405 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01001406 }
David Brownelld2876d02008-02-04 22:28:20 -08001407}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001408
David Brownelld2876d02008-02-04 22:28:20 -08001409/**
1410 * gpiochip_is_requested - return string iff signal was requested
1411 * @chip: controller managing the signal
1412 * @offset: of signal within controller's 0..(ngpio - 1) range
1413 *
1414 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001415 * The string returned is the label passed to gpio_request(); if none has been
1416 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001417 *
1418 * This function is for use by GPIO controller drivers. The label can
1419 * help with diagnostics, and knowing that the signal is used as a GPIO
1420 * can help avoid accidentally multiplexing it to another controller.
1421 */
1422const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1423{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001424 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001425
Dirk Behme48b59532015-08-18 18:02:32 +02001426 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001427 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001428
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001429 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001430
Alexandre Courbot372e7222013-02-03 01:29:29 +09001431 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001432 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001433 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001434}
1435EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1436
Mika Westerberg77c2d792014-03-10 14:54:50 +02001437/**
1438 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1439 * @desc: GPIO descriptor to request
1440 * @label: label for the GPIO
1441 *
1442 * Function allows GPIO chip drivers to request and use their own GPIO
1443 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1444 * function will not increase reference count of the GPIO chip module. This
1445 * allows the GPIO chip module to be unloaded as needed (we assume that the
1446 * GPIO chip driver handles freeing the GPIOs it has requested).
1447 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001448struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1449 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001450{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001451 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1452 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001453
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001454 if (IS_ERR(desc)) {
1455 chip_err(chip, "failed to get GPIO descriptor\n");
1456 return desc;
1457 }
1458
1459 err = __gpiod_request(desc, label);
1460 if (err < 0)
1461 return ERR_PTR(err);
1462
1463 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001464}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001465EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001466
1467/**
1468 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1469 * @desc: GPIO descriptor to free
1470 *
1471 * Function frees the given GPIO requested previously with
1472 * gpiochip_request_own_desc().
1473 */
1474void gpiochip_free_own_desc(struct gpio_desc *desc)
1475{
1476 if (desc)
1477 __gpiod_free(desc);
1478}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001479EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08001480
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001481/*
1482 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08001483 * some cases this is done in early boot, before IRQs are enabled.
1484 *
1485 * As a rule these aren't called more than once (except for drivers
1486 * using the open-drain emulation idiom) so these are natural places
1487 * to accumulate extra debugging checks. Note that we can't (yet)
1488 * rely on gpio_request() having been called beforehand.
1489 */
1490
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001491/**
1492 * gpiod_direction_input - set the GPIO direction to input
1493 * @desc: GPIO to set to input
1494 *
1495 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1496 * be called safely on it.
1497 *
1498 * Return 0 in case of success, else an error code.
1499 */
1500int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001501{
David Brownelld2876d02008-02-04 22:28:20 -08001502 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001503 int status = -EINVAL;
1504
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001505 VALIDATE_DESC(desc);
1506 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001507
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001508 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001509 gpiod_warn(desc,
1510 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001511 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001512 return -EIO;
1513 }
1514
Alexandre Courbotd82da792014-07-22 16:17:43 +09001515 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001516 if (status == 0)
1517 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001518
Alexandre Courbot372e7222013-02-03 01:29:29 +09001519 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001520
David Brownelld2876d02008-02-04 22:28:20 -08001521 return status;
1522}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001523EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001524
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001525static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001526{
David Brownelld2876d02008-02-04 22:28:20 -08001527 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001528 int status = -EINVAL;
1529
Linus Walleijd468bf92013-09-24 11:54:38 +02001530 /* GPIOs used for IRQs shall not be set as output */
1531 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1532 gpiod_err(desc,
1533 "%s: tried to set a GPIO tied to an IRQ as output\n",
1534 __func__);
1535 return -EIO;
1536 }
1537
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301538 /* Open drain pin should not be driven to 1 */
1539 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001540 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301541
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301542 /* Open source pin should not be driven to 0 */
1543 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001544 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301545
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001546 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001547 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001548 gpiod_warn(desc,
1549 "%s: missing set() or direction_output() operations\n",
1550 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001551 return -EIO;
1552 }
1553
Alexandre Courbotd82da792014-07-22 16:17:43 +09001554 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001555 if (status == 0)
1556 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001557 trace_gpio_value(desc_to_gpio(desc), 0, value);
1558 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001559 return status;
1560}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001561
1562/**
1563 * gpiod_direction_output_raw - set the GPIO direction to output
1564 * @desc: GPIO to set to output
1565 * @value: initial output value of the GPIO
1566 *
1567 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1568 * be called safely on it. The initial value of the output must be specified
1569 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1570 *
1571 * Return 0 in case of success, else an error code.
1572 */
1573int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1574{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001575 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001576 return _gpiod_direction_output_raw(desc, value);
1577}
1578EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1579
1580/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301581 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001582 * @desc: GPIO to set to output
1583 * @value: initial output value of the GPIO
1584 *
1585 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1586 * be called safely on it. The initial value of the output must be specified
1587 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1588 * account.
1589 *
1590 * Return 0 in case of success, else an error code.
1591 */
1592int gpiod_direction_output(struct gpio_desc *desc, int value)
1593{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001594 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001595 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1596 value = !value;
1597 return _gpiod_direction_output_raw(desc, value);
1598}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001599EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001600
Felipe Balbic4b5be92010-05-26 14:42:23 -07001601/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001602 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001603 * @gpio: the gpio to set debounce time
1604 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001605 *
1606 * returns -ENOTSUPP if the controller does not support setting
1607 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001608 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001609int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001610{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001611 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001612
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001613 VALIDATE_DESC(desc);
1614 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001615 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001616 gpiod_dbg(desc,
1617 "%s: missing set() or set_debounce() operations\n",
1618 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001619 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001620 }
1621
Alexandre Courbotd82da792014-07-22 16:17:43 +09001622 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001623}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001624EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001625
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001626/**
1627 * gpiod_is_active_low - test whether a GPIO is active-low or not
1628 * @desc: the gpio descriptor to test
1629 *
1630 * Returns 1 if the GPIO is active-low, 0 otherwise.
1631 */
1632int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001633{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001634 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001635 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001636}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001637EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001638
1639/* I/O calls are only valid after configuration completed; the relevant
1640 * "is this a valid GPIO" error checks should already have been done.
1641 *
1642 * "Get" operations are often inlinable as reading a pin value register,
1643 * and masking the relevant bit in that register.
1644 *
1645 * When "set" operations are inlinable, they involve writing that mask to
1646 * one register to set a low value, or a different register to set it high.
1647 * Otherwise locking is needed, so there may be little value to inlining.
1648 *
1649 *------------------------------------------------------------------------
1650 *
1651 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1652 * have requested the GPIO. That can include implicit requesting by
1653 * a direction setting call. Marking a gpio as requested locks its chip
1654 * in memory, guaranteeing that these table lookups need no more locking
1655 * and that gpiochip_remove() will fail.
1656 *
1657 * REVISIT when debugging, consider adding some instrumentation to ensure
1658 * that the GPIO was actually requested.
1659 */
1660
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001661static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001662{
1663 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001664 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001665 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001666
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001667 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001668 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001669 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01001670 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001671 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001672 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001673}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001674
David Brownelld2876d02008-02-04 22:28:20 -08001675/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001676 * gpiod_get_raw_value() - return a gpio's raw value
1677 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001678 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001679 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001680 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001681 *
1682 * This function should be called from contexts where we cannot sleep, and will
1683 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001684 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001685int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001686{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001687 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001688 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001689 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001690 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001691}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001692EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001693
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001694/**
1695 * gpiod_get_value() - return a gpio's value
1696 * @desc: gpio whose value will be returned
1697 *
1698 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001699 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001700 *
1701 * This function should be called from contexts where we cannot sleep, and will
1702 * complain if the GPIO chip functions potentially sleep.
1703 */
1704int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001705{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001706 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001707
1708 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001709 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001710 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001711
1712 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001713 if (value < 0)
1714 return value;
1715
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001716 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1717 value = !value;
1718
1719 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001720}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001721EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001722
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301723/*
1724 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001725 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001726 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301727 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001728static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301729{
1730 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001731 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001732 int offset = gpio_chip_hwgpio(desc);
1733
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301734 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001735 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301736 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001737 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301738 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001739 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301740 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001741 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301742 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001743 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301744 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001745 gpiod_err(desc,
1746 "%s: Error in set_value for open drain err %d\n",
1747 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301748}
1749
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301750/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001751 * _gpio_set_open_source_value() - Set the open source gpio's value.
1752 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001753 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301754 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001755static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301756{
1757 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001758 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001759 int offset = gpio_chip_hwgpio(desc);
1760
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301761 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001762 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301763 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001764 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301765 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001766 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301767 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001768 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301769 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001770 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301771 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001772 gpiod_err(desc,
1773 "%s: Error in set_value for open source err %d\n",
1774 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301775}
1776
Alexandre Courbot23600962014-03-11 15:52:09 +09001777static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001778{
1779 struct gpio_chip *chip;
1780
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001781 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001782 trace_gpio_value(desc_to_gpio(desc), 0, value);
1783 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1784 _gpio_set_open_drain_value(desc, value);
1785 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1786 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301787 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001788 chip->set(chip, gpio_chip_hwgpio(desc), value);
1789}
1790
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001791/*
1792 * set multiple outputs on the same chip;
1793 * use the chip's set_multiple function if available;
1794 * otherwise set the outputs sequentially;
1795 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1796 * defines which outputs are to be changed
1797 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1798 * defines the values the outputs specified by mask are to be set to
1799 */
1800static void gpio_chip_set_multiple(struct gpio_chip *chip,
1801 unsigned long *mask, unsigned long *bits)
1802{
1803 if (chip->set_multiple) {
1804 chip->set_multiple(chip, mask, bits);
1805 } else {
1806 int i;
1807 for (i = 0; i < chip->ngpio; i++) {
1808 if (mask[BIT_WORD(i)] == 0) {
1809 /* no more set bits in this mask word;
1810 * skip ahead to the next word */
1811 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1812 continue;
1813 }
1814 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001815 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001816 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001817 }
1818 }
1819}
1820
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001821static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1822 unsigned int array_size,
1823 struct gpio_desc **desc_array,
1824 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001825{
1826 int i = 0;
1827
1828 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001829 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001830 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1831 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1832 int count = 0;
1833
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001834 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001835 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001836
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001837 memset(mask, 0, sizeof(mask));
1838 do {
1839 struct gpio_desc *desc = desc_array[i];
1840 int hwgpio = gpio_chip_hwgpio(desc);
1841 int value = value_array[i];
1842
1843 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1844 value = !value;
1845 trace_gpio_value(desc_to_gpio(desc), 0, value);
1846 /*
1847 * collect all normal outputs belonging to the same chip
1848 * open drain and open source outputs are set individually
1849 */
1850 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001851 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001852 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1853 _gpio_set_open_source_value(desc, value);
1854 } else {
1855 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001856 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001857 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001858 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001859 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001860 count++;
1861 }
1862 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001863 } while ((i < array_size) &&
1864 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001865 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001866 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001867 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001868 }
1869}
1870
David Brownelld2876d02008-02-04 22:28:20 -08001871/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001872 * gpiod_set_raw_value() - assign a gpio's raw value
1873 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001874 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001875 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001876 * Set the raw value of the GPIO, i.e. the value of its physical line without
1877 * regard for its ACTIVE_LOW status.
1878 *
1879 * This function should be called from contexts where we cannot sleep, and will
1880 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001881 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001882void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001883{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001884 VALIDATE_DESC_VOID(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001885 /* Should be using gpio_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001886 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001887 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001888}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001889EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001890
1891/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001892 * gpiod_set_value() - assign a gpio's value
1893 * @desc: gpio whose value will be assigned
1894 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001895 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001896 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1897 * account
1898 *
1899 * This function should be called from contexts where we cannot sleep, and will
1900 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001901 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001902void gpiod_set_value(struct gpio_desc *desc, int value)
1903{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001904 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001905 /* Should be using gpio_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001906 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001907 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1908 value = !value;
1909 _gpiod_set_raw_value(desc, value);
1910}
1911EXPORT_SYMBOL_GPL(gpiod_set_value);
1912
1913/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001914 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001915 * @array_size: number of elements in the descriptor / value arrays
1916 * @desc_array: array of GPIO descriptors whose values will be assigned
1917 * @value_array: array of values to assign
1918 *
1919 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1920 * without regard for their ACTIVE_LOW status.
1921 *
1922 * This function should be called from contexts where we cannot sleep, and will
1923 * complain if the GPIO chip functions potentially sleep.
1924 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001925void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001926 struct gpio_desc **desc_array, int *value_array)
1927{
1928 if (!desc_array)
1929 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001930 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1931 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001932}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001933EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001934
1935/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001936 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001937 * @array_size: number of elements in the descriptor / value arrays
1938 * @desc_array: array of GPIO descriptors whose values will be assigned
1939 * @value_array: array of values to assign
1940 *
1941 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1942 * into account.
1943 *
1944 * This function should be called from contexts where we cannot sleep, and will
1945 * complain if the GPIO chip functions potentially sleep.
1946 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001947void gpiod_set_array_value(unsigned int array_size,
1948 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001949{
1950 if (!desc_array)
1951 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001952 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1953 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001954}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001955EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001956
1957/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001958 * gpiod_cansleep() - report whether gpio value access may sleep
1959 * @desc: gpio to check
1960 *
1961 */
1962int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001963{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001964 VALIDATE_DESC(desc);
1965 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001966}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001967EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001968
David Brownell0f6d5042008-10-15 22:03:14 -07001969/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001970 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1971 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001972 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001973 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1974 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001975 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001976int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001977{
1978 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001979 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001980
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001981 VALIDATE_DESC(desc);
1982 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001983 offset = gpio_chip_hwgpio(desc);
1984 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1985}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001986EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001987
Linus Walleijd468bf92013-09-24 11:54:38 +02001988/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001989 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001990 * @chip: the chip the GPIO to lock belongs to
1991 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001992 *
1993 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001994 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001995 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001996int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001997{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001998 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001999 return -EINVAL;
2000
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002001 if (test_bit(FLAG_IS_OUT, &chip->gpiodev->descs[offset].flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002002 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02002003 "%s: tried to flag a GPIO set as output for IRQ\n",
2004 __func__);
2005 return -EIO;
2006 }
2007
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002008 set_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002009 return 0;
2010}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002011EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002012
Linus Walleijd468bf92013-09-24 11:54:38 +02002013/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002014 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002015 * @chip: the chip the GPIO to lock belongs to
2016 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002017 *
2018 * This is used directly by GPIO drivers that want to indicate
2019 * that a certain GPIO is no longer used exclusively for IRQ.
2020 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002021void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02002022{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002023 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02002024 return;
2025
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002026 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002027}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002028EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002029
Linus Walleij6cee3822016-02-11 20:16:45 +01002030bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2031{
2032 if (offset >= chip->ngpio)
2033 return false;
2034
2035 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2036}
2037EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2038
Linus Walleij143b65d2016-02-16 15:41:42 +01002039bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2040{
2041 if (offset >= chip->ngpio)
2042 return false;
2043
2044 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2045}
2046EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2047
2048bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2049{
2050 if (offset >= chip->ngpio)
2051 return false;
2052
2053 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2054}
2055EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2056
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002057/**
2058 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2059 * @desc: gpio whose value will be returned
2060 *
2061 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002062 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002063 *
2064 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002065 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002066int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002067{
David Brownelld2876d02008-02-04 22:28:20 -08002068 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002069 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002070 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002071}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002072EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002073
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002074/**
2075 * gpiod_get_value_cansleep() - return a gpio's value
2076 * @desc: gpio whose value will be returned
2077 *
2078 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002079 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002080 *
2081 * This function is to be called from contexts that can sleep.
2082 */
2083int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002084{
David Brownelld2876d02008-02-04 22:28:20 -08002085 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002086
2087 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002088 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002089 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002090 if (value < 0)
2091 return value;
2092
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002093 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2094 value = !value;
2095
David Brownelld2876d02008-02-04 22:28:20 -08002096 return value;
2097}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002098EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002099
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002100/**
2101 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2102 * @desc: gpio whose value will be assigned
2103 * @value: value to assign
2104 *
2105 * Set the raw value of the GPIO, i.e. the value of its physical line without
2106 * regard for its ACTIVE_LOW status.
2107 *
2108 * This function is to be called from contexts that can sleep.
2109 */
2110void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002111{
David Brownelld2876d02008-02-04 22:28:20 -08002112 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002113 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002114 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002115}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002116EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002117
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002118/**
2119 * gpiod_set_value_cansleep() - assign a gpio's value
2120 * @desc: gpio whose value will be assigned
2121 * @value: value to assign
2122 *
2123 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2124 * account
2125 *
2126 * This function is to be called from contexts that can sleep.
2127 */
2128void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002129{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002130 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002131 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002132 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2133 value = !value;
2134 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002135}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002136EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002137
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002138/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002139 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002140 * @array_size: number of elements in the descriptor / value arrays
2141 * @desc_array: array of GPIO descriptors whose values will be assigned
2142 * @value_array: array of values to assign
2143 *
2144 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2145 * without regard for their ACTIVE_LOW status.
2146 *
2147 * This function is to be called from contexts that can sleep.
2148 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002149void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2150 struct gpio_desc **desc_array,
2151 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002152{
2153 might_sleep_if(extra_checks);
2154 if (!desc_array)
2155 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002156 gpiod_set_array_value_priv(true, true, array_size, desc_array,
2157 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002158}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002159EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002160
2161/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002162 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002163 * @array_size: number of elements in the descriptor / value arrays
2164 * @desc_array: array of GPIO descriptors whose values will be assigned
2165 * @value_array: array of values to assign
2166 *
2167 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2168 * into account.
2169 *
2170 * This function is to be called from contexts that can sleep.
2171 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002172void gpiod_set_array_value_cansleep(unsigned int array_size,
2173 struct gpio_desc **desc_array,
2174 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002175{
2176 might_sleep_if(extra_checks);
2177 if (!desc_array)
2178 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002179 gpiod_set_array_value_priv(false, true, array_size, desc_array,
2180 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002181}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002182EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002183
2184/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002185 * gpiod_add_lookup_table() - register GPIO device consumers
2186 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002187 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002188void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002189{
2190 mutex_lock(&gpio_lookup_lock);
2191
Alexandre Courbotad824782013-12-03 12:20:11 +09002192 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002193
2194 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002195}
2196
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302197/**
2198 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2199 * @table: table of consumers to unregister
2200 */
2201void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2202{
2203 mutex_lock(&gpio_lookup_lock);
2204
2205 list_del(&table->list);
2206
2207 mutex_unlock(&gpio_lookup_lock);
2208}
2209
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002210static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002211 unsigned int idx,
2212 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002213{
2214 char prop_name[32]; /* 32 is max size of property name */
2215 enum of_gpio_flags of_flags;
2216 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02002217 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002218
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002219 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02002220 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01002221 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002222 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02002223 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01002224 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002225 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002226
Thierry Redingdd34c372014-04-23 17:28:09 +02002227 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2228 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07002229 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02002230 break;
2231 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002232
2233 if (IS_ERR(desc))
2234 return desc;
2235
2236 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002237 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002238
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002239 if (of_flags & OF_GPIO_SINGLE_ENDED) {
2240 if (of_flags & OF_GPIO_ACTIVE_LOW)
2241 *flags |= GPIO_OPEN_DRAIN;
2242 else
2243 *flags |= GPIO_OPEN_SOURCE;
2244 }
2245
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002246 return desc;
2247}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002248
Mika Westerberg81f59e92013-10-10 11:01:09 +03002249static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002250 unsigned int idx,
2251 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03002252{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002253 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03002254 struct acpi_gpio_info info;
2255 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002256 char propname[32];
2257 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03002258
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002259 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002260 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002261 if (con_id && strcmp(con_id, "gpios")) {
2262 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002263 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002264 } else {
2265 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002266 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002267 }
Mika Westerberge01f4402013-10-10 11:01:10 +03002268
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002269 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
2270 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2271 break;
2272 }
2273
2274 /* Then from plain _CRS GPIOs */
2275 if (IS_ERR(desc)) {
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08002276 if (!acpi_can_fallback_to_crs(adev, con_id))
2277 return ERR_PTR(-ENOENT);
2278
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002279 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2280 if (IS_ERR(desc))
2281 return desc;
2282 }
2283
Christophe RICARD52044722015-12-23 23:25:34 +01002284 if (info.polarity == GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002285 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002286
2287 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002288}
2289
Alexandre Courbotad824782013-12-03 12:20:11 +09002290static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2291{
2292 const char *dev_id = dev ? dev_name(dev) : NULL;
2293 struct gpiod_lookup_table *table;
2294
2295 mutex_lock(&gpio_lookup_lock);
2296
2297 list_for_each_entry(table, &gpio_lookup_list, list) {
2298 if (table->dev_id && dev_id) {
2299 /*
2300 * Valid strings on both ends, must be identical to have
2301 * a match
2302 */
2303 if (!strcmp(table->dev_id, dev_id))
2304 goto found;
2305 } else {
2306 /*
2307 * One of the pointers is NULL, so both must be to have
2308 * a match
2309 */
2310 if (dev_id == table->dev_id)
2311 goto found;
2312 }
2313 }
2314 table = NULL;
2315
2316found:
2317 mutex_unlock(&gpio_lookup_lock);
2318 return table;
2319}
2320
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002321static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002322 unsigned int idx,
2323 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002324{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002325 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002326 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002327 struct gpiod_lookup *p;
2328
Alexandre Courbotad824782013-12-03 12:20:11 +09002329 table = gpiod_find_lookup_table(dev);
2330 if (!table)
2331 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002332
Alexandre Courbotad824782013-12-03 12:20:11 +09002333 for (p = &table->table[0]; p->chip_label; p++) {
2334 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002335
Alexandre Courbotad824782013-12-03 12:20:11 +09002336 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002337 if (p->idx != idx)
2338 continue;
2339
Alexandre Courbotad824782013-12-03 12:20:11 +09002340 /* If the lookup entry has a con_id, require exact match */
2341 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2342 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002343
Alexandre Courbotad824782013-12-03 12:20:11 +09002344 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002345
Alexandre Courbotad824782013-12-03 12:20:11 +09002346 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002347 dev_err(dev, "cannot find GPIO chip %s\n",
2348 p->chip_label);
2349 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002350 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002351
Alexandre Courbotad824782013-12-03 12:20:11 +09002352 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002353 dev_err(dev,
2354 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2355 idx, chip->ngpio, chip->label);
2356 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09002357 }
2358
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09002359 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09002360 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002361
2362 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09002363 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002364
2365 return desc;
2366}
2367
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002368static int dt_gpio_count(struct device *dev, const char *con_id)
2369{
2370 int ret;
2371 char propname[32];
2372 unsigned int i;
2373
2374 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2375 if (con_id)
2376 snprintf(propname, sizeof(propname), "%s-%s",
2377 con_id, gpio_suffixes[i]);
2378 else
2379 snprintf(propname, sizeof(propname), "%s",
2380 gpio_suffixes[i]);
2381
2382 ret = of_gpio_named_count(dev->of_node, propname);
2383 if (ret >= 0)
2384 break;
2385 }
2386 return ret;
2387}
2388
2389static int platform_gpio_count(struct device *dev, const char *con_id)
2390{
2391 struct gpiod_lookup_table *table;
2392 struct gpiod_lookup *p;
2393 unsigned int count = 0;
2394
2395 table = gpiod_find_lookup_table(dev);
2396 if (!table)
2397 return -ENOENT;
2398
2399 for (p = &table->table[0]; p->chip_label; p++) {
2400 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2401 (!con_id && !p->con_id))
2402 count++;
2403 }
2404 if (!count)
2405 return -ENOENT;
2406
2407 return count;
2408}
2409
2410/**
2411 * gpiod_count - return the number of GPIOs associated with a device / function
2412 * or -ENOENT if no GPIO has been assigned to the requested function
2413 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2414 * @con_id: function within the GPIO consumer
2415 */
2416int gpiod_count(struct device *dev, const char *con_id)
2417{
2418 int count = -ENOENT;
2419
2420 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2421 count = dt_gpio_count(dev, con_id);
2422 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2423 count = acpi_gpio_count(dev, con_id);
2424
2425 if (count < 0)
2426 count = platform_gpio_count(dev, con_id);
2427
2428 return count;
2429}
2430EXPORT_SYMBOL_GPL(gpiod_count);
2431
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002432/**
Thierry Reding08791622014-04-25 16:54:22 +02002433 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002434 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002435 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002436 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002437 *
2438 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002439 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07002440 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002441 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002442struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002443 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002444{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002445 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002446}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002447EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002448
2449/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002450 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2451 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2452 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002453 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002454 *
2455 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2456 * the requested function it will return NULL. This is convenient for drivers
2457 * that need to handle optional GPIOs.
2458 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002459struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002460 const char *con_id,
2461 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002462{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002463 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002464}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002465EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002466
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002467/**
2468 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2469 * @desc: gpio to be setup
2470 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2471 * of_get_gpio_hog()
2472 *
2473 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2474 */
2475static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2476{
2477 if (lflags & GPIO_ACTIVE_LOW)
2478 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2479 if (lflags & GPIO_OPEN_DRAIN)
2480 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2481 if (lflags & GPIO_OPEN_SOURCE)
2482 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2483}
Benoit Parrotf625d462015-02-02 11:44:44 -06002484
2485/**
2486 * gpiod_configure_flags - helper function to configure a given GPIO
2487 * @desc: gpio whose value will be assigned
2488 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06002489 * @dflags: gpiod_flags - optional GPIO initialization flags
2490 *
2491 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2492 * requested function and/or index, or another IS_ERR() code if an error
2493 * occurred while trying to acquire the GPIO.
2494 */
2495static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002496 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06002497{
2498 int status;
2499
Benoit Parrotf625d462015-02-02 11:44:44 -06002500 /* No particular flag request, return here... */
2501 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2502 pr_debug("no flags found for %s\n", con_id);
2503 return 0;
2504 }
2505
2506 /* Process flags */
2507 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2508 status = gpiod_direction_output(desc,
2509 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2510 else
2511 status = gpiod_direction_input(desc);
2512
2513 return status;
2514}
2515
Thierry Reding29a1f2332014-04-25 17:10:06 +02002516/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002517 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002518 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002519 * @con_id: function within the GPIO consumer
2520 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002521 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002522 *
2523 * This variant of gpiod_get() allows to access GPIOs other than the first
2524 * defined one for functions that define several GPIOs.
2525 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002526 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2527 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07002528 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002529 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002530struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002531 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002532 unsigned int idx,
2533 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002534{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002535 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002536 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002537 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002538
2539 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2540
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002541 if (dev) {
2542 /* Using device tree? */
2543 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2544 dev_dbg(dev, "using device tree for GPIO lookup\n");
2545 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2546 } else if (ACPI_COMPANION(dev)) {
2547 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2548 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2549 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002550 }
2551
2552 /*
2553 * Either we are not using DT or ACPI, or their lookup did not return
2554 * a result. In that case, use platform lookup as a fallback.
2555 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002556 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002557 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002558 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002559 }
2560
2561 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002562 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002563 return desc;
2564 }
2565
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002566 gpiod_parse_flags(desc, lookupflags);
2567
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002568 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002569 if (status < 0)
2570 return ERR_PTR(status);
2571
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002572 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002573 if (status < 0) {
2574 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2575 gpiod_put(desc);
2576 return ERR_PTR(status);
2577 }
2578
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002579 return desc;
2580}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002581EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002582
2583/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002584 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2585 * @fwnode: handle of the firmware node
2586 * @propname: name of the firmware property representing the GPIO
2587 *
2588 * This function can be used for drivers that get their configuration
2589 * from firmware.
2590 *
2591 * Function properly finds the corresponding GPIO using whatever is the
2592 * underlying firmware interface and then makes sure that the GPIO
2593 * descriptor is requested before it is returned to the caller.
2594 *
2595 * In case of error an ERR_PTR() is returned.
2596 */
2597struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2598 const char *propname)
2599{
2600 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2601 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002602 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02002603 int ret;
2604
2605 if (!fwnode)
2606 return ERR_PTR(-EINVAL);
2607
2608 if (is_of_node(fwnode)) {
2609 enum of_gpio_flags flags;
2610
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002611 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002612 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002613 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02002614 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002615 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2616 }
Mika Westerberg40b73182014-10-21 13:33:59 +02002617 } else if (is_acpi_node(fwnode)) {
2618 struct acpi_gpio_info info;
2619
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02002620 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02002621 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01002622 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02002623 }
2624
2625 if (IS_ERR(desc))
2626 return desc;
2627
Mika Westerberg40b73182014-10-21 13:33:59 +02002628 if (active_low)
2629 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2630
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002631 if (single_ended) {
2632 if (active_low)
2633 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2634 else
2635 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2636 }
2637
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002638 ret = gpiod_request(desc, NULL);
2639 if (ret)
2640 return ERR_PTR(ret);
2641
Mika Westerberg40b73182014-10-21 13:33:59 +02002642 return desc;
2643}
2644EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2645
2646/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002647 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2648 * function
2649 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2650 * @con_id: function within the GPIO consumer
2651 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002652 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002653 *
2654 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2655 * specified index was assigned to the requested function it will return NULL.
2656 * This is convenient for drivers that need to handle optional GPIOs.
2657 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002658struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002659 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002660 unsigned int index,
2661 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002662{
2663 struct gpio_desc *desc;
2664
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002665 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002666 if (IS_ERR(desc)) {
2667 if (PTR_ERR(desc) == -ENOENT)
2668 return NULL;
2669 }
2670
2671 return desc;
2672}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002673EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002674
2675/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002676 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2677 * @desc: gpio whose value will be assigned
2678 * @name: gpio line name
2679 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2680 * of_get_gpio_hog()
2681 * @dflags: gpiod_flags - optional GPIO initialization flags
2682 */
2683int gpiod_hog(struct gpio_desc *desc, const char *name,
2684 unsigned long lflags, enum gpiod_flags dflags)
2685{
2686 struct gpio_chip *chip;
2687 struct gpio_desc *local_desc;
2688 int hwnum;
2689 int status;
2690
2691 chip = gpiod_to_chip(desc);
2692 hwnum = gpio_chip_hwgpio(desc);
2693
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002694 gpiod_parse_flags(desc, lflags);
2695
Benoit Parrotf625d462015-02-02 11:44:44 -06002696 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2697 if (IS_ERR(local_desc)) {
Linus Walleija7138902015-06-05 11:36:10 +02002698 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2699 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002700 return PTR_ERR(local_desc);
2701 }
2702
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002703 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002704 if (status < 0) {
Linus Walleija7138902015-06-05 11:36:10 +02002705 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2706 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002707 gpiochip_free_own_desc(desc);
2708 return status;
2709 }
2710
2711 /* Mark GPIO as hogged so it can be identified and removed later */
2712 set_bit(FLAG_IS_HOGGED, &desc->flags);
2713
2714 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2715 desc_to_gpio(desc), name,
2716 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2717 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2718 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2719
2720 return 0;
2721}
2722
2723/**
2724 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2725 * @chip: gpio chip to act on
2726 *
2727 * This is only used by of_gpiochip_remove to free hogged gpios
2728 */
2729static void gpiochip_free_hogs(struct gpio_chip *chip)
2730{
2731 int id;
2732
2733 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002734 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
2735 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06002736 }
2737}
2738
2739/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002740 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2741 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2742 * @con_id: function within the GPIO consumer
2743 * @flags: optional GPIO initialization flags
2744 *
2745 * This function acquires all the GPIOs defined under a given function.
2746 *
2747 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2748 * no GPIO has been assigned to the requested function, or another IS_ERR()
2749 * code if an error occurred while trying to acquire the GPIOs.
2750 */
2751struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2752 const char *con_id,
2753 enum gpiod_flags flags)
2754{
2755 struct gpio_desc *desc;
2756 struct gpio_descs *descs;
2757 int count;
2758
2759 count = gpiod_count(dev, con_id);
2760 if (count < 0)
2761 return ERR_PTR(count);
2762
2763 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2764 GFP_KERNEL);
2765 if (!descs)
2766 return ERR_PTR(-ENOMEM);
2767
2768 for (descs->ndescs = 0; descs->ndescs < count; ) {
2769 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2770 if (IS_ERR(desc)) {
2771 gpiod_put_array(descs);
2772 return ERR_CAST(desc);
2773 }
2774 descs->desc[descs->ndescs] = desc;
2775 descs->ndescs++;
2776 }
2777 return descs;
2778}
2779EXPORT_SYMBOL_GPL(gpiod_get_array);
2780
2781/**
2782 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2783 * function
2784 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2785 * @con_id: function within the GPIO consumer
2786 * @flags: optional GPIO initialization flags
2787 *
2788 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2789 * assigned to the requested function it will return NULL.
2790 */
2791struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2792 const char *con_id,
2793 enum gpiod_flags flags)
2794{
2795 struct gpio_descs *descs;
2796
2797 descs = gpiod_get_array(dev, con_id, flags);
2798 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2799 return NULL;
2800
2801 return descs;
2802}
2803EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2804
2805/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002806 * gpiod_put - dispose of a GPIO descriptor
2807 * @desc: GPIO descriptor to dispose of
2808 *
2809 * No descriptor can be used after gpiod_put() has been called on it.
2810 */
2811void gpiod_put(struct gpio_desc *desc)
2812{
2813 gpiod_free(desc);
2814}
2815EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002816
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002817/**
2818 * gpiod_put_array - dispose of multiple GPIO descriptors
2819 * @descs: struct gpio_descs containing an array of descriptors
2820 */
2821void gpiod_put_array(struct gpio_descs *descs)
2822{
2823 unsigned int i;
2824
2825 for (i = 0; i < descs->ndescs; i++)
2826 gpiod_put(descs->desc[i]);
2827
2828 kfree(descs);
2829}
2830EXPORT_SYMBOL_GPL(gpiod_put_array);
2831
Linus Walleij3c702e92015-10-21 15:29:53 +02002832static int __init gpiolib_dev_init(void)
2833{
2834 int ret;
2835
2836 /* Register GPIO sysfs bus */
2837 ret = bus_register(&gpio_bus_type);
2838 if (ret < 0) {
2839 pr_err("gpiolib: could not register GPIO bus type\n");
2840 return ret;
2841 }
2842
2843 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
2844 if (ret < 0) {
2845 pr_err("gpiolib: failed to allocate char dev region\n");
2846 bus_unregister(&gpio_bus_type);
2847 }
2848 return ret;
2849}
2850core_initcall(gpiolib_dev_init);
2851
David Brownelld2876d02008-02-04 22:28:20 -08002852#ifdef CONFIG_DEBUG_FS
2853
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002854static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08002855{
2856 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002857 struct gpio_chip *chip = gdev->chip;
2858 unsigned gpio = gdev->base;
2859 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08002860 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002861 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002862
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002863 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02002864 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2865 if (gdesc->name) {
2866 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2867 gpio, gdesc->name);
2868 }
David Brownelld2876d02008-02-04 22:28:20 -08002869 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02002870 }
David Brownelld2876d02008-02-04 22:28:20 -08002871
Alexandre Courbot372e7222013-02-03 01:29:29 +09002872 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002873 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002874 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02002875 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2876 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08002877 is_out ? "out" : "in ",
2878 chip->get
2879 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002880 : "? ",
2881 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002882 seq_printf(s, "\n");
2883 }
2884}
2885
Thierry Redingf9c4a312012-04-12 13:26:01 +02002886static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002887{
Grant Likely362432a2013-02-09 09:41:49 +00002888 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002889 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002890 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002891
2892 s->private = "";
2893
Grant Likely362432a2013-02-09 09:41:49 +00002894 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002895 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00002896 if (index-- == 0) {
2897 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002898 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00002899 }
2900 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002901
2902 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002903}
2904
2905static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2906{
Grant Likely362432a2013-02-09 09:41:49 +00002907 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002908 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002909 void *ret = NULL;
2910
Grant Likely362432a2013-02-09 09:41:49 +00002911 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002912 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002913 ret = NULL;
2914 else
Linus Walleijff2b1352015-10-20 11:10:38 +02002915 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00002916 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002917
2918 s->private = "\n";
2919 ++*pos;
2920
2921 return ret;
2922}
2923
2924static void gpiolib_seq_stop(struct seq_file *s, void *v)
2925{
2926}
2927
2928static int gpiolib_seq_show(struct seq_file *s, void *v)
2929{
Linus Walleijff2b1352015-10-20 11:10:38 +02002930 struct gpio_device *gdev = v;
2931 struct gpio_chip *chip = gdev->chip;
2932 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002933
Linus Walleijff2b1352015-10-20 11:10:38 +02002934 if (!chip) {
2935 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
2936 dev_name(&gdev->dev));
2937 return 0;
2938 }
2939
2940 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
2941 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002942 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02002943 parent = chip->parent;
2944 if (parent)
2945 seq_printf(s, ", parent: %s/%s",
2946 parent->bus ? parent->bus->name : "no-bus",
2947 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02002948 if (chip->label)
2949 seq_printf(s, ", %s", chip->label);
2950 if (chip->can_sleep)
2951 seq_printf(s, ", can sleep");
2952 seq_printf(s, ":\n");
2953
2954 if (chip->dbg_show)
2955 chip->dbg_show(s, chip);
2956 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002957 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002958
David Brownelld2876d02008-02-04 22:28:20 -08002959 return 0;
2960}
2961
Thierry Redingf9c4a312012-04-12 13:26:01 +02002962static const struct seq_operations gpiolib_seq_ops = {
2963 .start = gpiolib_seq_start,
2964 .next = gpiolib_seq_next,
2965 .stop = gpiolib_seq_stop,
2966 .show = gpiolib_seq_show,
2967};
2968
David Brownelld2876d02008-02-04 22:28:20 -08002969static int gpiolib_open(struct inode *inode, struct file *file)
2970{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002971 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002972}
2973
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002974static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002975 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002976 .open = gpiolib_open,
2977 .read = seq_read,
2978 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002979 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002980};
2981
2982static int __init gpiolib_debugfs_init(void)
2983{
2984 /* /sys/kernel/debug/gpio */
2985 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2986 NULL, NULL, &gpiolib_operations);
2987 return 0;
2988}
2989subsys_initcall(gpiolib_debugfs_init);
2990
2991#endif /* DEBUG_FS */