blob: 70e0fff0a8a7b951a767d86ac354471ccc5a9431 [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) {
88 if (gdev->chip->base <= gpio &&
89 gdev->chip->base + gdev->chip->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +090090 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +020091 return &gdev->chip->desc[gpio - gdev->chip->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{
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900110 if (hwnum >= chip->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900111 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200112
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900113 return &chip->desc[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200114}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900115
116/**
117 * Convert a GPIO descriptor to the integer namespace.
118 * This should disappear in the future but is needed since we still
119 * use GPIO numbers for error messages and sysfs nodes
120 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700121int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900122{
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900123 return desc->chip->base + (desc - &desc->chip->desc[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900124}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700125EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900126
127
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700128/**
129 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
130 * @desc: descriptor to return the chip of
131 */
132struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900133{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900134 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900135}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700136EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800137
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700138/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
139static int gpiochip_find_base(int ngpio)
140{
Linus Walleijff2b1352015-10-20 11:10:38 +0200141 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900142 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700143
Linus Walleijff2b1352015-10-20 11:10:38 +0200144 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900145 /* found a free space? */
Linus Walleijff2b1352015-10-20 11:10:38 +0200146 if (gdev->chip->base + gdev->chip->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900147 break;
148 else
149 /* nope, check the space right before the chip */
Linus Walleijff2b1352015-10-20 11:10:38 +0200150 base = gdev->chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700151 }
152
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900153 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700154 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900155 return base;
156 } else {
157 pr_err("%s: cannot find free range\n", __func__);
158 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700159 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700160}
161
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700162/**
163 * gpiod_get_direction - return the current direction of a GPIO
164 * @desc: GPIO to get the direction of
165 *
166 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
167 *
168 * This function may sleep if gpiod_cansleep() is true.
169 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900170int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300171{
172 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900173 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300174 int status = -EINVAL;
175
Alexandre Courbot372e7222013-02-03 01:29:29 +0900176 chip = gpiod_to_chip(desc);
177 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300178
179 if (!chip->get_direction)
180 return status;
181
Alexandre Courbot372e7222013-02-03 01:29:29 +0900182 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300183 if (status > 0) {
184 /* GPIOF_DIR_IN, or other positive */
185 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900186 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300187 }
188 if (status == 0) {
189 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900190 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300191 }
192 return status;
193}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700194EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300195
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900196/*
197 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800198 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900199 *
200 * Return -EBUSY if the new chip overlaps with some other chip's integer
201 * space.
202 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200203static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900204{
Linus Walleijff2b1352015-10-20 11:10:38 +0200205 struct gpio_device *iterator;
206 struct gpio_device *previous = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900207
Linus Walleijff2b1352015-10-20 11:10:38 +0200208 if (!gdev->chip)
209 return -EINVAL;
210
211 if (list_empty(&gpio_devices)) {
212 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530213 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900214 }
215
Linus Walleijff2b1352015-10-20 11:10:38 +0200216 list_for_each_entry(iterator, &gpio_devices, list) {
217 /*
218 * The list may contain dangling GPIO devices with no
219 * live chip assigned.
220 */
221 if (!iterator->chip)
222 continue;
223 if (iterator->chip->base >=
224 gdev->chip->base + gdev->chip->ngpio) {
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800225 /*
226 * Iterator is the first GPIO chip so there is no
227 * previous one
228 */
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530229 if (!previous) {
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800230 goto found;
231 } else {
232 /*
233 * We found a valid range(means
234 * [base, base + ngpio - 1]) between previous
235 * and iterator chip.
236 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200237 if (previous->chip->base + previous->chip->ngpio
238 <= gdev->chip->base)
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800239 goto found;
240 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900241 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800242 previous = iterator;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900243 }
244
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530245 /*
246 * We are beyond the last chip in the list and iterator now
247 * points to the head.
248 * Let iterator point to the last chip in the list.
249 */
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900250
Linus Walleijff2b1352015-10-20 11:10:38 +0200251 iterator = list_last_entry(&gpio_devices, struct gpio_device, list);
252 if (iterator->chip->base + iterator->chip->ngpio <= gdev->chip->base) {
253 list_add(&gdev->list, &iterator->list);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500254 return 0;
255 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900256
Linus Walleijff2b1352015-10-20 11:10:38 +0200257 dev_err(&gdev->dev,
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800258 "GPIO integer space overlap, cannot add chip\n");
259 return -EBUSY;
260
261found:
Linus Walleijff2b1352015-10-20 11:10:38 +0200262 list_add_tail(&gdev->list, &iterator->list);
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800263 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900264}
265
Linus Walleijf881bab2015-09-23 16:20:43 -0700266/**
267 * Convert a GPIO name to its descriptor
268 */
269static struct gpio_desc *gpio_name_to_desc(const char * const name)
270{
Linus Walleijff2b1352015-10-20 11:10:38 +0200271 struct gpio_device *gdev;
Linus Walleijf881bab2015-09-23 16:20:43 -0700272 unsigned long flags;
273
274 spin_lock_irqsave(&gpio_lock, flags);
275
Linus Walleijff2b1352015-10-20 11:10:38 +0200276 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700277 int i;
278
Linus Walleijff2b1352015-10-20 11:10:38 +0200279 for (i = 0; i != gdev->chip->ngpio; ++i) {
280 struct gpio_desc *gpio = &gdev->chip->desc[i];
Linus Walleijf881bab2015-09-23 16:20:43 -0700281
Vladimir Zapolskiyd06165b2015-11-11 14:36:53 +0200282 if (!gpio->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700283 continue;
284
285 if (!strcmp(gpio->name, name)) {
286 spin_unlock_irqrestore(&gpio_lock, flags);
287 return gpio;
288 }
289 }
290 }
291
292 spin_unlock_irqrestore(&gpio_lock, flags);
293
294 return NULL;
295}
296
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200297/*
298 * Takes the names from gc->names and checks if they are all unique. If they
299 * are, they are assigned to their gpio descriptors.
300 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800301 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200302 */
303static int gpiochip_set_desc_names(struct gpio_chip *gc)
304{
305 int i;
306
307 if (!gc->names)
308 return 0;
309
310 /* First check all names if they are unique */
311 for (i = 0; i != gc->ngpio; ++i) {
312 struct gpio_desc *gpio;
313
314 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700315 if (gpio)
Linus Walleij34ffd852015-10-20 11:31:54 +0200316 dev_warn(&gc->gpiodev->dev,
317 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab2015-09-23 16:20:43 -0700318 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200319 }
320
321 /* Then add all names to the GPIO descriptors */
322 for (i = 0; i != gc->ngpio; ++i)
323 gc->desc[i].name = gc->names[i];
324
325 return 0;
326}
327
Linus Walleij3c702e92015-10-21 15:29:53 +0200328/**
329 * gpio_ioctl() - ioctl handler for the GPIO chardev
330 */
331static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
332{
333 struct gpio_device *gdev = filp->private_data;
334 struct gpio_chip *chip = gdev->chip;
335 int __user *ip = (int __user *)arg;
336 struct gpiochip_info chipinfo;
337
338 /* We fail any subsequent ioctl():s when the chip is gone */
339 if (!chip)
340 return -ENODEV;
341
342 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
343 /* Fill in the struct and pass to userspace */
344 strncpy(chipinfo.name, dev_name(&gdev->dev),
345 sizeof(chipinfo.name));
346 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
347 chipinfo.lines = chip->ngpio;
348 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
349 return -EFAULT;
350 return 0;
351 }
352 return -EINVAL;
353}
354
355/**
356 * gpio_chrdev_open() - open the chardev for ioctl operations
357 * @inode: inode for this chardev
358 * @filp: file struct for storing private data
359 * Returns 0 on success
360 */
361static int gpio_chrdev_open(struct inode *inode, struct file *filp)
362{
363 struct gpio_device *gdev = container_of(inode->i_cdev,
364 struct gpio_device, chrdev);
365
366 /* Fail on open if the backing gpiochip is gone */
367 if (!gdev || !gdev->chip)
368 return -ENODEV;
369 get_device(&gdev->dev);
370 filp->private_data = gdev;
371 return 0;
372}
373
374/**
375 * gpio_chrdev_release() - close chardev after ioctl operations
376 * @inode: inode for this chardev
377 * @filp: file struct for storing private data
378 * Returns 0 on success
379 */
380static int gpio_chrdev_release(struct inode *inode, struct file *filp)
381{
382 struct gpio_device *gdev = container_of(inode->i_cdev,
383 struct gpio_device, chrdev);
384
385 if (!gdev)
386 return -ENODEV;
387 put_device(&gdev->dev);
388 return 0;
389}
390
391
392static const struct file_operations gpio_fileops = {
393 .release = gpio_chrdev_release,
394 .open = gpio_chrdev_open,
395 .owner = THIS_MODULE,
396 .llseek = noop_llseek,
397 .unlocked_ioctl = gpio_ioctl,
398 .compat_ioctl = gpio_ioctl,
399};
400
Linus Walleijff2b1352015-10-20 11:10:38 +0200401static void gpiodevice_release(struct device *dev)
402{
403 struct gpio_device *gdev = dev_get_drvdata(dev);
404
Linus Walleij3c702e92015-10-21 15:29:53 +0200405 cdev_del(&gdev->chrdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200406 list_del(&gdev->list);
407 ida_simple_remove(&gpio_ida, gdev->id);
408}
409
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700410/**
Linus Walleijb08ea352015-12-03 15:14:13 +0100411 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -0800412 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900413 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -0800414 *
415 * Returns a negative errno if the chip can't be registered, such as
416 * because the chip->base is invalid or already associated with a
417 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700418 *
Linus Walleijb08ea352015-12-03 15:14:13 +0100419 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800420 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d2008-07-25 01:46:07 -0700421 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
422 * for GPIOs will fail rudely.
423 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700424 * If chip->base is negative, this requests dynamic assignment of
425 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800426 */
Linus Walleijb08ea352015-12-03 15:14:13 +0100427int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -0800428{
429 unsigned long flags;
430 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +0200431 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700432 int base = chip->base;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900433 struct gpio_desc *descs;
Linus Walleijff2b1352015-10-20 11:10:38 +0200434 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -0800435
Linus Walleijff2b1352015-10-20 11:10:38 +0200436 /*
437 * First: allocate and populate the internal stat container, and
438 * set up the struct device.
439 */
440 gdev = kmalloc(sizeof(*gdev), GFP_KERNEL);
441 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900442 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +0200443 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +0200444 gdev->chip = chip;
445 chip->gpiodev = gdev;
446 if (chip->parent) {
447 gdev->dev.parent = chip->parent;
448 gdev->dev.of_node = chip->parent->of_node;
449 } else {
450#ifdef CONFIG_OF_GPIO
451 /* If the gpiochip has an assigned OF node this takes precedence */
452 if (chip->of_node)
453 gdev->dev.of_node = chip->of_node;
454#endif
455 }
456 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
457 if (gdev->id < 0) {
458 status = gdev->id;
459 goto err_free_gdev;
460 }
461 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
462 device_initialize(&gdev->dev);
463 dev_set_drvdata(&gdev->dev, gdev);
464 if (chip->parent && chip->parent->driver)
465 gdev->owner = chip->parent->driver->owner;
466 else if (chip->owner)
467 /* TODO: remove chip->owner */
468 gdev->owner = chip->owner;
469 else
470 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -0800471
Linus Walleijff2b1352015-10-20 11:10:38 +0200472 /* FIXME: devm_kcalloc() these and move to gpio_device */
473 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
474 if (!descs) {
475 status = -ENOMEM;
476 goto err_free_gdev;
477 }
478
479 /* FIXME: move driver data into gpio_device dev_set_drvdata() */
Linus Walleijb08ea352015-12-03 15:14:13 +0100480 chip->data = data;
481
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800482 if (chip->ngpio == 0) {
483 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +0200484 status = -EINVAL;
485 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800486 }
487
David Brownelld2876d02008-02-04 22:28:20 -0800488 spin_lock_irqsave(&gpio_lock, flags);
489
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700490 if (base < 0) {
491 base = gpiochip_find_base(chip->ngpio);
492 if (base < 0) {
493 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100494 spin_unlock_irqrestore(&gpio_lock, flags);
495 goto err_free_descs;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700496 }
497 chip->base = base;
498 }
499
Linus Walleijff2b1352015-10-20 11:10:38 +0200500 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +0100501 if (status) {
502 spin_unlock_irqrestore(&gpio_lock, flags);
503 goto err_free_descs;
504 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900505
Linus Walleijff2b1352015-10-20 11:10:38 +0200506 for (i = 0; i < chip->ngpio; i++) {
507 struct gpio_desc *desc = &descs[i];
David Brownelld8f388d2008-07-25 01:46:07 -0700508
Linus Walleijff2b1352015-10-20 11:10:38 +0200509 /* REVISIT: maybe a pointer to gpio_device is better */
Johan Hovold05aa5202015-01-12 17:12:26 +0100510 desc->chip = chip;
511
512 /* REVISIT: most hardware initializes GPIOs as inputs (often
513 * with pullups enabled) so power usage is minimized. Linux
514 * code should set the gpio direction first thing; but until
515 * it does, and in case chip->get_direction is not set, we may
516 * expose the wrong direction in sysfs.
517 */
518 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800519 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900520 chip->desc = descs;
521
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800522 spin_unlock_irqrestore(&gpio_lock, flags);
523
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530524#ifdef CONFIG_PINCTRL
Linus Walleijff2b1352015-10-20 11:10:38 +0200525 /* FIXME: move pin ranges to gpio_device */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530526 INIT_LIST_HEAD(&chip->pin_ranges);
527#endif
528
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200529 status = gpiochip_set_desc_names(chip);
530 if (status)
531 goto err_remove_from_list;
532
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200533 status = of_gpiochip_add(chip);
534 if (status)
535 goto err_remove_chip;
536
Mika Westerberg664e3e52014-01-08 12:40:54 +0200537 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600538
Linus Walleij3c702e92015-10-21 15:29:53 +0200539 /*
540 * By first adding the chardev, and then adding the device,
541 * we get a device node entry in sysfs under
542 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
543 * coldplug of device nodes and other udev business.
544 */
545 cdev_init(&gdev->chrdev, &gpio_fileops);
546 gdev->chrdev.owner = THIS_MODULE;
547 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
548 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
549 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
550 if (status < 0)
551 chip_warn(chip, "failed to add char device %d:%d\n",
552 MAJOR(gpio_devt), gdev->id);
553 else
554 chip_dbg(chip, "added GPIO chardev (%d:%d)\n",
555 MAJOR(gpio_devt), gdev->id);
Linus Walleijff2b1352015-10-20 11:10:38 +0200556 status = device_add(&gdev->dev);
Johan Hovold225fce82015-01-12 17:12:25 +0100557 if (status)
Linus Walleij3c702e92015-10-21 15:29:53 +0200558 goto err_remove_chardev;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600559
Linus Walleijff2b1352015-10-20 11:10:38 +0200560 status = gpiochip_sysfs_register(chip);
561 if (status)
562 goto err_remove_device;
563
564 /* From this point, the .release() function cleans up gpio_device */
565 gdev->dev.release = gpiodevice_release;
566 get_device(&gdev->dev);
Andy Shevchenko7589e592013-12-05 11:26:23 +0200567 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700568 chip->base, chip->base + chip->ngpio - 1,
569 chip->label ? : "generic");
570
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600571 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800572
Linus Walleijff2b1352015-10-20 11:10:38 +0200573err_remove_device:
574 device_del(&gdev->dev);
Linus Walleij3c702e92015-10-21 15:29:53 +0200575err_remove_chardev:
576 cdev_del(&gdev->chrdev);
Johan Hovold225fce82015-01-12 17:12:25 +0100577err_remove_chip:
578 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200579 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100580 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200581err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100582 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200583 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800584 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold05aa5202015-01-12 17:12:26 +0100585 chip->desc = NULL;
Johan Hovold225fce82015-01-12 17:12:25 +0100586err_free_descs:
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900587 kfree(descs);
Linus Walleijff2b1352015-10-20 11:10:38 +0200588err_free_gdev:
589 ida_simple_remove(&gpio_ida, gdev->id);
590 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -0800591 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200592 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600593 chip->base, chip->base + chip->ngpio - 1,
594 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800595 return status;
596}
Linus Walleijb08ea352015-12-03 15:14:13 +0100597EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -0800598
599/**
600 * gpiochip_remove() - unregister a gpio_chip
601 * @chip: the chip to unregister
602 *
603 * A gpio_chip with any GPIOs still requested may not be removed.
604 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200605void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800606{
Linus Walleijff2b1352015-10-20 11:10:38 +0200607 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200608 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800609 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800610 unsigned id;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200611 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800612
Linus Walleijff2b1352015-10-20 11:10:38 +0200613 /* Numb the device, cancelling all outstanding operations */
614 gdev->chip = NULL;
615
616 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Johan Hovold426577b2015-05-04 17:10:32 +0200617 gpiochip_sysfs_unregister(chip);
Johan Hovold00acc3d2015-01-12 17:12:27 +0100618 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200619 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100620 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600621 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600622 of_gpiochip_remove(chip);
623
Johan Hovold6798aca2015-01-12 17:12:28 +0100624 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900625 for (id = 0; id < chip->ngpio; id++) {
Johan Hovoldfab28b82015-05-04 17:10:27 +0200626 desc = &chip->desc[id];
627 desc->chip = NULL;
628 if (test_bit(FLAG_REQUESTED, &desc->flags))
629 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800630 }
David Brownelld2876d02008-02-04 22:28:20 -0800631 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900632
Johan Hovoldfab28b82015-05-04 17:10:27 +0200633 if (requested)
Linus Walleij34ffd852015-10-20 11:31:54 +0200634 dev_crit(&chip->gpiodev->dev,
Linus Walleij58383c72015-11-04 09:56:26 +0100635 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +0200636
Linus Walleijff2b1352015-10-20 11:10:38 +0200637 /* FIXME: need to be moved to gpio_device and held there */
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900638 kfree(chip->desc);
639 chip->desc = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +0200640
641 /*
642 * The gpiochip side puts its use of the device to rest here:
643 * if there are no userspace clients, the chardev and device will
644 * be removed, else it will be dangling until the last user is
645 * gone.
646 */
647 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -0800648}
649EXPORT_SYMBOL_GPL(gpiochip_remove);
650
Grant Likely594fa262010-06-08 07:48:16 -0600651/**
652 * gpiochip_find() - iterator for locating a specific gpio_chip
653 * @data: data to pass to match function
654 * @callback: Callback function to check gpio_chip
655 *
656 * Similar to bus_find_device. It returns a reference to a gpio_chip as
657 * determined by a user supplied @match callback. The callback should return
658 * 0 if the device doesn't match and non-zero if it does. If the callback is
659 * non-zero, this function will return to the caller and not iterate over any
660 * more gpio_chips.
661 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600662struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700663 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600664 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600665{
Linus Walleijff2b1352015-10-20 11:10:38 +0200666 struct gpio_device *gdev;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900667 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600668 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600669
670 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200671 list_for_each_entry(gdev, &gpio_devices, list)
672 if (match(gdev->chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600673 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900674
675 /* No match? */
Linus Walleijff2b1352015-10-20 11:10:38 +0200676 if (&gdev->list == &gpio_devices)
Alexandre Courbot125eef92013-02-03 01:29:26 +0900677 chip = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +0200678 else
679 chip = gdev->chip;
680
Grant Likely594fa262010-06-08 07:48:16 -0600681 spin_unlock_irqrestore(&gpio_lock, flags);
682
683 return chip;
684}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600685EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800686
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900687static int gpiochip_match_name(struct gpio_chip *chip, void *data)
688{
689 const char *name = data;
690
691 return !strcmp(chip->label, name);
692}
693
694static struct gpio_chip *find_chip_by_name(const char *name)
695{
696 return gpiochip_find((void *)name, gpiochip_match_name);
697}
698
Linus Walleij14250522014-03-25 10:40:18 +0100699#ifdef CONFIG_GPIOLIB_IRQCHIP
700
701/*
702 * The following is irqchip helper code for gpiochips.
703 */
704
705/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200706 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
707 * @gpiochip: the gpiochip to set the irqchip chain to
708 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100709 * @parent_irq: the irq number corresponding to the parent IRQ for this
710 * chained irqchip
711 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200712 * coming out of the gpiochip. If the interrupt is nested rather than
713 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100714 */
715void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
716 struct irq_chip *irqchip,
717 int parent_irq,
718 irq_flow_handler_t parent_handler)
719{
Linus Walleij83141a72014-09-26 13:50:12 +0200720 unsigned int offset;
721
Linus Walleij83141a72014-09-26 13:50:12 +0200722 if (!gpiochip->irqdomain) {
723 chip_err(gpiochip, "called %s before setting up irqchip\n",
724 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200725 return;
726 }
727
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200728 if (parent_handler) {
729 if (gpiochip->can_sleep) {
730 chip_err(gpiochip,
731 "you cannot have chained interrupts on a "
732 "chip that may sleep\n");
733 return;
734 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200735 /*
736 * The parent irqchip is already using the chip_data for this
737 * irqchip, so our callbacks simply use the handler_data.
738 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +0200739 irq_set_chained_handler_and_data(parent_irq, parent_handler,
740 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300741
742 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200743 }
Linus Walleij83141a72014-09-26 13:50:12 +0200744
745 /* Set the parent IRQ for all affected IRQs */
746 for (offset = 0; offset < gpiochip->ngpio; offset++)
747 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
748 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100749}
750EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
751
752/**
753 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
754 * @d: the irqdomain used by this irqchip
755 * @irq: the global irq number used by this GPIO irqchip irq
756 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
757 *
758 * This function will set up the mapping for a certain IRQ line on a
759 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
760 * stored inside the gpiochip.
761 */
762static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
763 irq_hw_number_t hwirq)
764{
765 struct gpio_chip *chip = d->host_data;
766
Linus Walleij14250522014-03-25 10:40:18 +0100767 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300768 /*
769 * This lock class tells lockdep that GPIO irqs are in a different
770 * category than their parents, so it won't report false recursion.
771 */
772 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +0200773 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200774 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300775 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200776 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100777 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -0500778
Linus Walleij1333b902014-04-23 16:45:12 +0200779 /*
780 * No set-up of the hardware will happen if IRQ_TYPE_NONE
781 * is passed as default type.
782 */
783 if (chip->irq_default_type != IRQ_TYPE_NONE)
784 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100785
786 return 0;
787}
788
Linus Walleijc3626fd2014-03-28 20:42:01 +0100789static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
790{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200791 struct gpio_chip *chip = d->host_data;
792
Linus Walleij1c8732b2014-04-09 13:34:39 +0200793 if (chip->can_sleep)
794 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100795 irq_set_chip_and_handler(irq, NULL, NULL);
796 irq_set_chip_data(irq, NULL);
797}
798
Linus Walleij14250522014-03-25 10:40:18 +0100799static const struct irq_domain_ops gpiochip_domain_ops = {
800 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100801 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100802 /* Virtually all GPIO irqchips are twocell:ed */
803 .xlate = irq_domain_xlate_twocell,
804};
805
806static int gpiochip_irq_reqres(struct irq_data *d)
807{
808 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
809
Linus Walleijff2b1352015-10-20 11:10:38 +0200810 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300811 return -ENODEV;
812
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900813 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100814 chip_err(chip,
815 "unable to lock HW IRQ %lu for IRQ\n",
816 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200817 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100818 return -EINVAL;
819 }
820 return 0;
821}
822
823static void gpiochip_irq_relres(struct irq_data *d)
824{
825 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
826
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900827 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200828 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100829}
830
831static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
832{
833 return irq_find_mapping(chip->irqdomain, offset);
834}
835
836/**
837 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
838 * @gpiochip: the gpiochip to remove the irqchip from
839 *
840 * This is called only from gpiochip_remove()
841 */
842static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
843{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100844 unsigned int offset;
845
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300846 acpi_gpiochip_free_interrupts(gpiochip);
847
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300848 if (gpiochip->irq_parent) {
849 irq_set_chained_handler(gpiochip->irq_parent, NULL);
850 irq_set_handler_data(gpiochip->irq_parent, NULL);
851 }
852
Linus Walleijc3626fd2014-03-28 20:42:01 +0100853 /* Remove all IRQ mappings and delete the domain */
854 if (gpiochip->irqdomain) {
855 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300856 irq_dispose_mapping(
857 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100858 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100859 }
Linus Walleij14250522014-03-25 10:40:18 +0100860
861 if (gpiochip->irqchip) {
862 gpiochip->irqchip->irq_request_resources = NULL;
863 gpiochip->irqchip->irq_release_resources = NULL;
864 gpiochip->irqchip = NULL;
865 }
866}
867
868/**
869 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
870 * @gpiochip: the gpiochip to add the irqchip to
871 * @irqchip: the irqchip to add to the gpiochip
872 * @first_irq: if not dynamically assigned, the base (first) IRQ to
873 * allocate gpiochip irqs from
874 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200875 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
876 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300877 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +0100878 *
879 * This function closely associates a certain irqchip with a certain
880 * gpiochip, providing an irq domain to translate the local IRQs to
881 * global irqs in the gpiolib core, and making sure that the gpiochip
882 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +0100883 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +0100884 * from the gpiochip passed as chip data. An irqdomain will be stored
885 * in the gpiochip that shall be used by the driver to handle IRQ number
886 * translation. The gpiochip will need to be initialized and registered
887 * before calling this function.
888 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100889 * This function will handle two cell:ed simple IRQs and assumes all
890 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100891 * need to be open coded.
892 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300893int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
894 struct irq_chip *irqchip,
895 unsigned int first_irq,
896 irq_flow_handler_t handler,
897 unsigned int type,
898 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +0100899{
900 struct device_node *of_node;
901 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100902 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100903
904 if (!gpiochip || !irqchip)
905 return -EINVAL;
906
Linus Walleij58383c72015-11-04 09:56:26 +0100907 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +0100908 pr_err("missing gpiochip .dev parent pointer\n");
909 return -EINVAL;
910 }
Linus Walleij58383c72015-11-04 09:56:26 +0100911 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +0100912#ifdef CONFIG_OF_GPIO
913 /*
Colin Cronin20a8a962015-05-18 11:41:43 -0700914 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800915 * FIXME: get rid of this and use gpiochip->parent->of_node
916 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +0100917 */
918 if (gpiochip->of_node)
919 of_node = gpiochip->of_node;
920#endif
921 gpiochip->irqchip = irqchip;
922 gpiochip->irq_handler = handler;
923 gpiochip->irq_default_type = type;
924 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300925 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +0100926 gpiochip->irqdomain = irq_domain_add_simple(of_node,
927 gpiochip->ngpio, first_irq,
928 &gpiochip_domain_ops, gpiochip);
929 if (!gpiochip->irqdomain) {
930 gpiochip->irqchip = NULL;
931 return -EINVAL;
932 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +0200933
934 /*
935 * It is possible for a driver to override this, but only if the
936 * alternative functions are both implemented.
937 */
938 if (!irqchip->irq_request_resources &&
939 !irqchip->irq_release_resources) {
940 irqchip->irq_request_resources = gpiochip_irq_reqres;
941 irqchip->irq_release_resources = gpiochip_irq_relres;
942 }
Linus Walleij14250522014-03-25 10:40:18 +0100943
944 /*
945 * Prepare the mapping since the irqchip shall be orthogonal to
946 * any gpiochip calls. If the first_irq was zero, this is
947 * necessary to allocate descriptors for all IRQs.
948 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100949 for (offset = 0; offset < gpiochip->ngpio; offset++) {
950 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
951 if (offset == 0)
952 /*
953 * Store the base into the gpiochip to be used when
954 * unmapping the irqs.
955 */
956 gpiochip->irq_base = irq_base;
957 }
Linus Walleij14250522014-03-25 10:40:18 +0100958
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300959 acpi_gpiochip_request_interrupts(gpiochip);
960
Linus Walleij14250522014-03-25 10:40:18 +0100961 return 0;
962}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300963EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +0100964
965#else /* CONFIG_GPIOLIB_IRQCHIP */
966
967static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
968
969#endif /* CONFIG_GPIOLIB_IRQCHIP */
970
Jonas Gorskic771c2f2015-10-11 17:34:15 +0200971/**
972 * gpiochip_generic_request() - request the gpio function for a pin
973 * @chip: the gpiochip owning the GPIO
974 * @offset: the offset of the GPIO to request for GPIO function
975 */
976int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
977{
978 return pinctrl_request_gpio(chip->base + offset);
979}
980EXPORT_SYMBOL_GPL(gpiochip_generic_request);
981
982/**
983 * gpiochip_generic_free() - free the gpio function from a pin
984 * @chip: the gpiochip to request the gpio function for
985 * @offset: the offset of the GPIO to free from GPIO function
986 */
987void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
988{
989 pinctrl_free_gpio(chip->base + offset);
990}
991EXPORT_SYMBOL_GPL(gpiochip_generic_free);
992
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530993#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100994
Linus Walleij3f0f8672012-11-20 12:40:15 +0100995/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200996 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
997 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +0200998 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +0200999 * @gpio_offset: the start offset in the current gpio_chip number space
1000 * @pin_group: name of the pin group inside the pin controller
1001 */
1002int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1003 struct pinctrl_dev *pctldev,
1004 unsigned int gpio_offset, const char *pin_group)
1005{
1006 struct gpio_pin_range *pin_range;
1007 int ret;
1008
1009 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1010 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001011 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001012 return -ENOMEM;
1013 }
1014
1015 /* Use local offset as range ID */
1016 pin_range->range.id = gpio_offset;
1017 pin_range->range.gc = chip;
1018 pin_range->range.name = chip->label;
1019 pin_range->range.base = chip->base + gpio_offset;
1020 pin_range->pctldev = pctldev;
1021
1022 ret = pinctrl_get_group_pins(pctldev, pin_group,
1023 &pin_range->range.pins,
1024 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001025 if (ret < 0) {
1026 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001027 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001028 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001029
1030 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1031
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001032 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1033 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001034 pinctrl_dev_get_devname(pctldev), pin_group);
1035
1036 list_add_tail(&pin_range->node, &chip->pin_ranges);
1037
1038 return 0;
1039}
1040EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1041
1042/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001043 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1044 * @chip: the gpiochip to add the range for
1045 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001046 * @gpio_offset: the start offset in the current gpio_chip number space
1047 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001048 * @npins: the number of pins from the offset of each pin space (GPIO and
1049 * pin controller) to accumulate in this range
1050 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001051int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001052 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001053 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301054{
1055 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001056 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301057
Linus Walleij3f0f8672012-11-20 12:40:15 +01001058 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301059 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001060 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001061 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301062 }
1063
Linus Walleij3f0f8672012-11-20 12:40:15 +01001064 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001065 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001066 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301067 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001068 pin_range->range.base = chip->base + gpio_offset;
1069 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301070 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001071 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301072 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001073 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001074 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001075 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001076 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001077 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001078 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001079 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1080 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001081 pinctl_name,
1082 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301083
1084 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001085
1086 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301087}
Linus Walleij165adc92012-11-06 14:49:39 +01001088EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301089
Linus Walleij3f0f8672012-11-20 12:40:15 +01001090/**
1091 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1092 * @chip: the chip to remove all the mappings for
1093 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301094void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1095{
1096 struct gpio_pin_range *pin_range, *tmp;
1097
1098 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1099 list_del(&pin_range->node);
1100 pinctrl_remove_gpio_range(pin_range->pctldev,
1101 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001102 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301103 }
1104}
Linus Walleij165adc92012-11-06 14:49:39 +01001105EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1106
1107#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301108
David Brownelld2876d02008-02-04 22:28:20 -08001109/* These "optional" allocation calls help prevent drivers from stomping
1110 * on each other, and help provide better diagnostics in debugfs.
1111 * They're called even less than the "set direction" calls.
1112 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001113static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001114{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001115 struct gpio_chip *chip = desc->chip;
1116 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001117 unsigned long flags;
1118
1119 spin_lock_irqsave(&gpio_lock, flags);
1120
David Brownelld2876d02008-02-04 22:28:20 -08001121 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001122 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001123 */
1124
1125 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1126 desc_set_label(desc, label ? : "?");
1127 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001128 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001129 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001130 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001131 }
1132
1133 if (chip->request) {
1134 /* chip->request may sleep */
1135 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001136 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001137 spin_lock_irqsave(&gpio_lock, flags);
1138
1139 if (status < 0) {
1140 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001141 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001142 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001143 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001144 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001145 if (chip->get_direction) {
1146 /* chip->get_direction may sleep */
1147 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001148 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001149 spin_lock_irqsave(&gpio_lock, flags);
1150 }
David Brownelld2876d02008-02-04 22:28:20 -08001151done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +03001152 if (status < 0) {
1153 /* Clear flags that might have been set by the caller before
1154 * requesting the GPIO.
1155 */
1156 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1157 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1158 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1159 }
Mika Westerberg77c2d792014-03-10 14:54:50 +02001160 spin_unlock_irqrestore(&gpio_lock, flags);
1161 return status;
1162}
1163
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001164int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001165{
1166 int status = -EPROBE_DEFER;
1167 struct gpio_chip *chip;
1168
1169 if (!desc) {
1170 pr_warn("%s: invalid GPIO\n", __func__);
1171 return -EINVAL;
1172 }
1173
1174 chip = desc->chip;
1175 if (!chip)
1176 goto done;
1177
Linus Walleijff2b1352015-10-20 11:10:38 +02001178 if (try_module_get(chip->gpiodev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001179 status = __gpiod_request(desc, label);
1180 if (status < 0)
Linus Walleijff2b1352015-10-20 11:10:38 +02001181 module_put(chip->gpiodev->owner);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001182 }
1183
1184done:
David Brownelld2876d02008-02-04 22:28:20 -08001185 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001186 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001187
David Brownelld2876d02008-02-04 22:28:20 -08001188 return status;
1189}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001190
Mika Westerberg77c2d792014-03-10 14:54:50 +02001191static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001192{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001193 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001194 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001195 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001196
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001197 might_sleep();
1198
Alexandre Courbot372e7222013-02-03 01:29:29 +09001199 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001200
David Brownelld2876d02008-02-04 22:28:20 -08001201 spin_lock_irqsave(&gpio_lock, flags);
1202
David Brownell35e8bb52008-10-15 22:03:16 -07001203 chip = desc->chip;
1204 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1205 if (chip->free) {
1206 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001207 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001208 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001209 spin_lock_irqsave(&gpio_lock, flags);
1210 }
David Brownelld2876d02008-02-04 22:28:20 -08001211 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001212 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001213 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301214 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301215 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001216 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001217 ret = true;
1218 }
David Brownelld2876d02008-02-04 22:28:20 -08001219
1220 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001221 return ret;
1222}
1223
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001224void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001225{
1226 if (desc && __gpiod_free(desc))
Linus Walleijff2b1352015-10-20 11:10:38 +02001227 module_put(desc->chip->gpiodev->owner);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001228 else
1229 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -08001230}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001231
David Brownelld2876d02008-02-04 22:28:20 -08001232/**
1233 * gpiochip_is_requested - return string iff signal was requested
1234 * @chip: controller managing the signal
1235 * @offset: of signal within controller's 0..(ngpio - 1) range
1236 *
1237 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001238 * The string returned is the label passed to gpio_request(); if none has been
1239 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001240 *
1241 * This function is for use by GPIO controller drivers. The label can
1242 * help with diagnostics, and knowing that the signal is used as a GPIO
1243 * can help avoid accidentally multiplexing it to another controller.
1244 */
1245const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1246{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001247 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001248
Dirk Behme48b59532015-08-18 18:02:32 +02001249 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001250 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001251
1252 desc = &chip->desc[offset];
1253
Alexandre Courbot372e7222013-02-03 01:29:29 +09001254 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001255 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001256 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001257}
1258EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1259
Mika Westerberg77c2d792014-03-10 14:54:50 +02001260/**
1261 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1262 * @desc: GPIO descriptor to request
1263 * @label: label for the GPIO
1264 *
1265 * Function allows GPIO chip drivers to request and use their own GPIO
1266 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1267 * function will not increase reference count of the GPIO chip module. This
1268 * allows the GPIO chip module to be unloaded as needed (we assume that the
1269 * GPIO chip driver handles freeing the GPIOs it has requested).
1270 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001271struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1272 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001273{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001274 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1275 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001276
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001277 if (IS_ERR(desc)) {
1278 chip_err(chip, "failed to get GPIO descriptor\n");
1279 return desc;
1280 }
1281
1282 err = __gpiod_request(desc, label);
1283 if (err < 0)
1284 return ERR_PTR(err);
1285
1286 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001287}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001288EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001289
1290/**
1291 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1292 * @desc: GPIO descriptor to free
1293 *
1294 * Function frees the given GPIO requested previously with
1295 * gpiochip_request_own_desc().
1296 */
1297void gpiochip_free_own_desc(struct gpio_desc *desc)
1298{
1299 if (desc)
1300 __gpiod_free(desc);
1301}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001302EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08001303
1304/* Drivers MUST set GPIO direction before making get/set calls. In
1305 * some cases this is done in early boot, before IRQs are enabled.
1306 *
1307 * As a rule these aren't called more than once (except for drivers
1308 * using the open-drain emulation idiom) so these are natural places
1309 * to accumulate extra debugging checks. Note that we can't (yet)
1310 * rely on gpio_request() having been called beforehand.
1311 */
1312
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001313/**
1314 * gpiod_direction_input - set the GPIO direction to input
1315 * @desc: GPIO to set to input
1316 *
1317 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1318 * be called safely on it.
1319 *
1320 * Return 0 in case of success, else an error code.
1321 */
1322int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001323{
David Brownelld2876d02008-02-04 22:28:20 -08001324 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001325 int status = -EINVAL;
1326
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001327 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001328 pr_warn("%s: invalid GPIO\n", __func__);
1329 return -EINVAL;
1330 }
1331
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001332 chip = desc->chip;
1333 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001334 gpiod_warn(desc,
1335 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001336 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001337 return -EIO;
1338 }
1339
Alexandre Courbotd82da792014-07-22 16:17:43 +09001340 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001341 if (status == 0)
1342 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001343
Alexandre Courbot372e7222013-02-03 01:29:29 +09001344 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001345
David Brownelld2876d02008-02-04 22:28:20 -08001346 return status;
1347}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001348EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001349
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001350static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001351{
David Brownelld2876d02008-02-04 22:28:20 -08001352 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001353 int status = -EINVAL;
1354
Linus Walleijd468bf92013-09-24 11:54:38 +02001355 /* GPIOs used for IRQs shall not be set as output */
1356 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1357 gpiod_err(desc,
1358 "%s: tried to set a GPIO tied to an IRQ as output\n",
1359 __func__);
1360 return -EIO;
1361 }
1362
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301363 /* Open drain pin should not be driven to 1 */
1364 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001365 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301366
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301367 /* Open source pin should not be driven to 0 */
1368 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001369 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301370
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001371 chip = desc->chip;
1372 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001373 gpiod_warn(desc,
1374 "%s: missing set() or direction_output() operations\n",
1375 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001376 return -EIO;
1377 }
1378
Alexandre Courbotd82da792014-07-22 16:17:43 +09001379 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001380 if (status == 0)
1381 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001382 trace_gpio_value(desc_to_gpio(desc), 0, value);
1383 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001384 return status;
1385}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001386
1387/**
1388 * gpiod_direction_output_raw - set the GPIO direction to output
1389 * @desc: GPIO to set to output
1390 * @value: initial output value of the GPIO
1391 *
1392 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1393 * be called safely on it. The initial value of the output must be specified
1394 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1395 *
1396 * Return 0 in case of success, else an error code.
1397 */
1398int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1399{
1400 if (!desc || !desc->chip) {
1401 pr_warn("%s: invalid GPIO\n", __func__);
1402 return -EINVAL;
1403 }
1404 return _gpiod_direction_output_raw(desc, value);
1405}
1406EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1407
1408/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301409 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001410 * @desc: GPIO to set to output
1411 * @value: initial output value of the GPIO
1412 *
1413 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1414 * be called safely on it. The initial value of the output must be specified
1415 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1416 * account.
1417 *
1418 * Return 0 in case of success, else an error code.
1419 */
1420int gpiod_direction_output(struct gpio_desc *desc, int value)
1421{
1422 if (!desc || !desc->chip) {
1423 pr_warn("%s: invalid GPIO\n", __func__);
1424 return -EINVAL;
1425 }
1426 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1427 value = !value;
1428 return _gpiod_direction_output_raw(desc, value);
1429}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001430EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001431
Felipe Balbic4b5be92010-05-26 14:42:23 -07001432/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001433 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001434 * @gpio: the gpio to set debounce time
1435 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001436 *
1437 * returns -ENOTSUPP if the controller does not support setting
1438 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001439 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001440int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001441{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001442 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001443
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001444 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001445 pr_warn("%s: invalid GPIO\n", __func__);
1446 return -EINVAL;
1447 }
1448
Felipe Balbic4b5be92010-05-26 14:42:23 -07001449 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001450 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001451 gpiod_dbg(desc,
1452 "%s: missing set() or set_debounce() operations\n",
1453 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001454 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001455 }
1456
Alexandre Courbotd82da792014-07-22 16:17:43 +09001457 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001458}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001459EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001460
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001461/**
1462 * gpiod_is_active_low - test whether a GPIO is active-low or not
1463 * @desc: the gpio descriptor to test
1464 *
1465 * Returns 1 if the GPIO is active-low, 0 otherwise.
1466 */
1467int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001468{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001469 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001470}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001471EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001472
1473/* I/O calls are only valid after configuration completed; the relevant
1474 * "is this a valid GPIO" error checks should already have been done.
1475 *
1476 * "Get" operations are often inlinable as reading a pin value register,
1477 * and masking the relevant bit in that register.
1478 *
1479 * When "set" operations are inlinable, they involve writing that mask to
1480 * one register to set a low value, or a different register to set it high.
1481 * Otherwise locking is needed, so there may be little value to inlining.
1482 *
1483 *------------------------------------------------------------------------
1484 *
1485 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1486 * have requested the GPIO. That can include implicit requesting by
1487 * a direction setting call. Marking a gpio as requested locks its chip
1488 * in memory, guaranteeing that these table lookups need no more locking
1489 * and that gpiochip_remove() will fail.
1490 *
1491 * REVISIT when debugging, consider adding some instrumentation to ensure
1492 * that the GPIO was actually requested.
1493 */
1494
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001495static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001496{
1497 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001498 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001499 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001500
Alexandre Courbot372e7222013-02-03 01:29:29 +09001501 chip = desc->chip;
1502 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001503 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01001504 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001505 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001506 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001507}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001508
David Brownelld2876d02008-02-04 22:28:20 -08001509/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001510 * gpiod_get_raw_value() - return a gpio's raw value
1511 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001512 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001513 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001514 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001515 *
1516 * This function should be called from contexts where we cannot sleep, and will
1517 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001518 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001519int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001520{
David Brownelld2876d02008-02-04 22:28:20 -08001521 if (!desc)
1522 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001523 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001524 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001525 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001526}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001527EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001528
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001529/**
1530 * gpiod_get_value() - return a gpio's value
1531 * @desc: gpio whose value will be returned
1532 *
1533 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001534 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001535 *
1536 * This function should be called from contexts where we cannot sleep, and will
1537 * complain if the GPIO chip functions potentially sleep.
1538 */
1539int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001540{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001541 int value;
1542 if (!desc)
1543 return 0;
1544 /* Should be using gpio_get_value_cansleep() */
1545 WARN_ON(desc->chip->can_sleep);
1546
1547 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001548 if (value < 0)
1549 return value;
1550
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001551 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1552 value = !value;
1553
1554 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001555}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001556EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001557
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301558/*
1559 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001560 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001561 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301562 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001563static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301564{
1565 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001566 struct gpio_chip *chip = desc->chip;
1567 int offset = gpio_chip_hwgpio(desc);
1568
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301569 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001570 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301571 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001572 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301573 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001574 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301575 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001576 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301577 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001578 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301579 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001580 gpiod_err(desc,
1581 "%s: Error in set_value for open drain err %d\n",
1582 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301583}
1584
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301585/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001586 * _gpio_set_open_source_value() - Set the open source gpio's value.
1587 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001588 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301589 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001590static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301591{
1592 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001593 struct gpio_chip *chip = desc->chip;
1594 int offset = gpio_chip_hwgpio(desc);
1595
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301596 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001597 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301598 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001599 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301600 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001601 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301602 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001603 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301604 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001605 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301606 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001607 gpiod_err(desc,
1608 "%s: Error in set_value for open source err %d\n",
1609 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301610}
1611
Alexandre Courbot23600962014-03-11 15:52:09 +09001612static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001613{
1614 struct gpio_chip *chip;
1615
Alexandre Courbot372e7222013-02-03 01:29:29 +09001616 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001617 trace_gpio_value(desc_to_gpio(desc), 0, value);
1618 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1619 _gpio_set_open_drain_value(desc, value);
1620 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1621 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301622 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001623 chip->set(chip, gpio_chip_hwgpio(desc), value);
1624}
1625
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001626/*
1627 * set multiple outputs on the same chip;
1628 * use the chip's set_multiple function if available;
1629 * otherwise set the outputs sequentially;
1630 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1631 * defines which outputs are to be changed
1632 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1633 * defines the values the outputs specified by mask are to be set to
1634 */
1635static void gpio_chip_set_multiple(struct gpio_chip *chip,
1636 unsigned long *mask, unsigned long *bits)
1637{
1638 if (chip->set_multiple) {
1639 chip->set_multiple(chip, mask, bits);
1640 } else {
1641 int i;
1642 for (i = 0; i < chip->ngpio; i++) {
1643 if (mask[BIT_WORD(i)] == 0) {
1644 /* no more set bits in this mask word;
1645 * skip ahead to the next word */
1646 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1647 continue;
1648 }
1649 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001650 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001651 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001652 }
1653 }
1654}
1655
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001656static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1657 unsigned int array_size,
1658 struct gpio_desc **desc_array,
1659 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001660{
1661 int i = 0;
1662
1663 while (i < array_size) {
1664 struct gpio_chip *chip = desc_array[i]->chip;
1665 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1666 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1667 int count = 0;
1668
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001669 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001670 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001671
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001672 memset(mask, 0, sizeof(mask));
1673 do {
1674 struct gpio_desc *desc = desc_array[i];
1675 int hwgpio = gpio_chip_hwgpio(desc);
1676 int value = value_array[i];
1677
1678 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1679 value = !value;
1680 trace_gpio_value(desc_to_gpio(desc), 0, value);
1681 /*
1682 * collect all normal outputs belonging to the same chip
1683 * open drain and open source outputs are set individually
1684 */
1685 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001686 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001687 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1688 _gpio_set_open_source_value(desc, value);
1689 } else {
1690 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001691 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001692 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001693 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001694 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001695 count++;
1696 }
1697 i++;
1698 } while ((i < array_size) && (desc_array[i]->chip == chip));
1699 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001700 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001701 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001702 }
1703}
1704
David Brownelld2876d02008-02-04 22:28:20 -08001705/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001706 * gpiod_set_raw_value() - assign a gpio's raw value
1707 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001708 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001709 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001710 * Set the raw value of the GPIO, i.e. the value of its physical line without
1711 * regard for its ACTIVE_LOW status.
1712 *
1713 * This function should be called from contexts where we cannot sleep, and will
1714 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001715 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001716void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001717{
David Brownelld2876d02008-02-04 22:28:20 -08001718 if (!desc)
1719 return;
David Brownelld2876d02008-02-04 22:28:20 -08001720 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001721 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001722 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001723}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001724EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001725
1726/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001727 * gpiod_set_value() - assign a gpio's value
1728 * @desc: gpio whose value will be assigned
1729 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001730 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001731 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1732 * account
1733 *
1734 * This function should be called from contexts where we cannot sleep, and will
1735 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001736 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001737void gpiod_set_value(struct gpio_desc *desc, int value)
1738{
1739 if (!desc)
1740 return;
1741 /* Should be using gpio_set_value_cansleep() */
1742 WARN_ON(desc->chip->can_sleep);
1743 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1744 value = !value;
1745 _gpiod_set_raw_value(desc, value);
1746}
1747EXPORT_SYMBOL_GPL(gpiod_set_value);
1748
1749/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001750 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001751 * @array_size: number of elements in the descriptor / value arrays
1752 * @desc_array: array of GPIO descriptors whose values will be assigned
1753 * @value_array: array of values to assign
1754 *
1755 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1756 * without regard for their ACTIVE_LOW status.
1757 *
1758 * This function should be called from contexts where we cannot sleep, and will
1759 * complain if the GPIO chip functions potentially sleep.
1760 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001761void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001762 struct gpio_desc **desc_array, int *value_array)
1763{
1764 if (!desc_array)
1765 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001766 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1767 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001768}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001769EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001770
1771/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001772 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001773 * @array_size: number of elements in the descriptor / value arrays
1774 * @desc_array: array of GPIO descriptors whose values will be assigned
1775 * @value_array: array of values to assign
1776 *
1777 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1778 * into account.
1779 *
1780 * This function should be called from contexts where we cannot sleep, and will
1781 * complain if the GPIO chip functions potentially sleep.
1782 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001783void gpiod_set_array_value(unsigned int array_size,
1784 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001785{
1786 if (!desc_array)
1787 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001788 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1789 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001790}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001791EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001792
1793/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001794 * gpiod_cansleep() - report whether gpio value access may sleep
1795 * @desc: gpio to check
1796 *
1797 */
1798int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001799{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001800 if (!desc)
1801 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001802 return desc->chip->can_sleep;
1803}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001804EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001805
David Brownell0f6d5042008-10-15 22:03:14 -07001806/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001807 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1808 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001809 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001810 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1811 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001812 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001813int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001814{
1815 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001816 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001817
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001818 if (!desc)
1819 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001820 chip = desc->chip;
1821 offset = gpio_chip_hwgpio(desc);
1822 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1823}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001824EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001825
Linus Walleijd468bf92013-09-24 11:54:38 +02001826/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001827 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001828 * @chip: the chip the GPIO to lock belongs to
1829 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001830 *
1831 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001832 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001833 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001834int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001835{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001836 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001837 return -EINVAL;
1838
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001839 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1840 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001841 "%s: tried to flag a GPIO set as output for IRQ\n",
1842 __func__);
1843 return -EIO;
1844 }
1845
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001846 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001847 return 0;
1848}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001849EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001850
Linus Walleijd468bf92013-09-24 11:54:38 +02001851/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001852 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001853 * @chip: the chip the GPIO to lock belongs to
1854 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001855 *
1856 * This is used directly by GPIO drivers that want to indicate
1857 * that a certain GPIO is no longer used exclusively for IRQ.
1858 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001859void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001860{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001861 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001862 return;
1863
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001864 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001865}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001866EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001867
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001868/**
1869 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1870 * @desc: gpio whose value will be returned
1871 *
1872 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001873 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001874 *
1875 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001876 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001877int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001878{
David Brownelld2876d02008-02-04 22:28:20 -08001879 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001880 if (!desc)
1881 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001882 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001883}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001884EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001885
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001886/**
1887 * gpiod_get_value_cansleep() - return a gpio's value
1888 * @desc: gpio whose value will be returned
1889 *
1890 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001891 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001892 *
1893 * This function is to be called from contexts that can sleep.
1894 */
1895int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001896{
David Brownelld2876d02008-02-04 22:28:20 -08001897 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001898
1899 might_sleep_if(extra_checks);
1900 if (!desc)
1901 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001902
1903 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001904 if (value < 0)
1905 return value;
1906
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001907 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1908 value = !value;
1909
David Brownelld2876d02008-02-04 22:28:20 -08001910 return value;
1911}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001912EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001913
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001914/**
1915 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1916 * @desc: gpio whose value will be assigned
1917 * @value: value to assign
1918 *
1919 * Set the raw value of the GPIO, i.e. the value of its physical line without
1920 * regard for its ACTIVE_LOW status.
1921 *
1922 * This function is to be called from contexts that can sleep.
1923 */
1924void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001925{
David Brownelld2876d02008-02-04 22:28:20 -08001926 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001927 if (!desc)
1928 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001929 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001930}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001931EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001932
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001933/**
1934 * gpiod_set_value_cansleep() - assign a gpio's value
1935 * @desc: gpio whose value will be assigned
1936 * @value: value to assign
1937 *
1938 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1939 * account
1940 *
1941 * This function is to be called from contexts that can sleep.
1942 */
1943void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001944{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001945 might_sleep_if(extra_checks);
1946 if (!desc)
1947 return;
1948
1949 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1950 value = !value;
1951 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001952}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001953EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001954
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001955/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001956 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001957 * @array_size: number of elements in the descriptor / value arrays
1958 * @desc_array: array of GPIO descriptors whose values will be assigned
1959 * @value_array: array of values to assign
1960 *
1961 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1962 * without regard for their ACTIVE_LOW status.
1963 *
1964 * This function is to be called from contexts that can sleep.
1965 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001966void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1967 struct gpio_desc **desc_array,
1968 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001969{
1970 might_sleep_if(extra_checks);
1971 if (!desc_array)
1972 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001973 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1974 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001975}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001976EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001977
1978/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001979 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001980 * @array_size: number of elements in the descriptor / value arrays
1981 * @desc_array: array of GPIO descriptors whose values will be assigned
1982 * @value_array: array of values to assign
1983 *
1984 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1985 * into account.
1986 *
1987 * This function is to be called from contexts that can sleep.
1988 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001989void gpiod_set_array_value_cansleep(unsigned int array_size,
1990 struct gpio_desc **desc_array,
1991 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001992{
1993 might_sleep_if(extra_checks);
1994 if (!desc_array)
1995 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001996 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1997 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001998}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001999EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002000
2001/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002002 * gpiod_add_lookup_table() - register GPIO device consumers
2003 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002004 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002005void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002006{
2007 mutex_lock(&gpio_lookup_lock);
2008
Alexandre Courbotad824782013-12-03 12:20:11 +09002009 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002010
2011 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002012}
2013
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302014/**
2015 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2016 * @table: table of consumers to unregister
2017 */
2018void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2019{
2020 mutex_lock(&gpio_lookup_lock);
2021
2022 list_del(&table->list);
2023
2024 mutex_unlock(&gpio_lookup_lock);
2025}
2026
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002027static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002028 unsigned int idx,
2029 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002030{
2031 char prop_name[32]; /* 32 is max size of property name */
2032 enum of_gpio_flags of_flags;
2033 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02002034 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002035
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002036 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02002037 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01002038 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002039 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02002040 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01002041 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002042 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002043
Thierry Redingdd34c372014-04-23 17:28:09 +02002044 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2045 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07002046 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02002047 break;
2048 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002049
2050 if (IS_ERR(desc))
2051 return desc;
2052
2053 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002054 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002055
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002056 if (of_flags & OF_GPIO_SINGLE_ENDED) {
2057 if (of_flags & OF_GPIO_ACTIVE_LOW)
2058 *flags |= GPIO_OPEN_DRAIN;
2059 else
2060 *flags |= GPIO_OPEN_SOURCE;
2061 }
2062
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002063 return desc;
2064}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002065
Mika Westerberg81f59e92013-10-10 11:01:09 +03002066static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002067 unsigned int idx,
2068 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03002069{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002070 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03002071 struct acpi_gpio_info info;
2072 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002073 char propname[32];
2074 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03002075
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002076 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002077 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002078 if (con_id && strcmp(con_id, "gpios")) {
2079 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002080 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002081 } else {
2082 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002083 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002084 }
Mika Westerberge01f4402013-10-10 11:01:10 +03002085
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002086 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
2087 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2088 break;
2089 }
2090
2091 /* Then from plain _CRS GPIOs */
2092 if (IS_ERR(desc)) {
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08002093 if (!acpi_can_fallback_to_crs(adev, con_id))
2094 return ERR_PTR(-ENOENT);
2095
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002096 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2097 if (IS_ERR(desc))
2098 return desc;
2099 }
2100
Christophe RICARD52044722015-12-23 23:25:34 +01002101 if (info.polarity == GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002102 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002103
2104 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002105}
2106
Alexandre Courbotad824782013-12-03 12:20:11 +09002107static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2108{
2109 const char *dev_id = dev ? dev_name(dev) : NULL;
2110 struct gpiod_lookup_table *table;
2111
2112 mutex_lock(&gpio_lookup_lock);
2113
2114 list_for_each_entry(table, &gpio_lookup_list, list) {
2115 if (table->dev_id && dev_id) {
2116 /*
2117 * Valid strings on both ends, must be identical to have
2118 * a match
2119 */
2120 if (!strcmp(table->dev_id, dev_id))
2121 goto found;
2122 } else {
2123 /*
2124 * One of the pointers is NULL, so both must be to have
2125 * a match
2126 */
2127 if (dev_id == table->dev_id)
2128 goto found;
2129 }
2130 }
2131 table = NULL;
2132
2133found:
2134 mutex_unlock(&gpio_lookup_lock);
2135 return table;
2136}
2137
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002138static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002139 unsigned int idx,
2140 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002141{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002142 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002143 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002144 struct gpiod_lookup *p;
2145
Alexandre Courbotad824782013-12-03 12:20:11 +09002146 table = gpiod_find_lookup_table(dev);
2147 if (!table)
2148 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002149
Alexandre Courbotad824782013-12-03 12:20:11 +09002150 for (p = &table->table[0]; p->chip_label; p++) {
2151 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002152
Alexandre Courbotad824782013-12-03 12:20:11 +09002153 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002154 if (p->idx != idx)
2155 continue;
2156
Alexandre Courbotad824782013-12-03 12:20:11 +09002157 /* If the lookup entry has a con_id, require exact match */
2158 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2159 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002160
Alexandre Courbotad824782013-12-03 12:20:11 +09002161 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002162
Alexandre Courbotad824782013-12-03 12:20:11 +09002163 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002164 dev_err(dev, "cannot find GPIO chip %s\n",
2165 p->chip_label);
2166 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002167 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002168
Alexandre Courbotad824782013-12-03 12:20:11 +09002169 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002170 dev_err(dev,
2171 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2172 idx, chip->ngpio, chip->label);
2173 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09002174 }
2175
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09002176 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09002177 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002178
2179 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09002180 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002181
2182 return desc;
2183}
2184
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002185static int dt_gpio_count(struct device *dev, const char *con_id)
2186{
2187 int ret;
2188 char propname[32];
2189 unsigned int i;
2190
2191 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2192 if (con_id)
2193 snprintf(propname, sizeof(propname), "%s-%s",
2194 con_id, gpio_suffixes[i]);
2195 else
2196 snprintf(propname, sizeof(propname), "%s",
2197 gpio_suffixes[i]);
2198
2199 ret = of_gpio_named_count(dev->of_node, propname);
2200 if (ret >= 0)
2201 break;
2202 }
2203 return ret;
2204}
2205
2206static int platform_gpio_count(struct device *dev, const char *con_id)
2207{
2208 struct gpiod_lookup_table *table;
2209 struct gpiod_lookup *p;
2210 unsigned int count = 0;
2211
2212 table = gpiod_find_lookup_table(dev);
2213 if (!table)
2214 return -ENOENT;
2215
2216 for (p = &table->table[0]; p->chip_label; p++) {
2217 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2218 (!con_id && !p->con_id))
2219 count++;
2220 }
2221 if (!count)
2222 return -ENOENT;
2223
2224 return count;
2225}
2226
2227/**
2228 * gpiod_count - return the number of GPIOs associated with a device / function
2229 * or -ENOENT if no GPIO has been assigned to the requested function
2230 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2231 * @con_id: function within the GPIO consumer
2232 */
2233int gpiod_count(struct device *dev, const char *con_id)
2234{
2235 int count = -ENOENT;
2236
2237 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2238 count = dt_gpio_count(dev, con_id);
2239 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2240 count = acpi_gpio_count(dev, con_id);
2241
2242 if (count < 0)
2243 count = platform_gpio_count(dev, con_id);
2244
2245 return count;
2246}
2247EXPORT_SYMBOL_GPL(gpiod_count);
2248
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002249/**
Thierry Reding08791622014-04-25 16:54:22 +02002250 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002251 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002252 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002253 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002254 *
2255 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002256 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07002257 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002258 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002259struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002260 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002261{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002262 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002263}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002264EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002265
2266/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002267 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2268 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2269 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002270 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002271 *
2272 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2273 * the requested function it will return NULL. This is convenient for drivers
2274 * that need to handle optional GPIOs.
2275 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002276struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002277 const char *con_id,
2278 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002279{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002280 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002281}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002282EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002283
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002284/**
2285 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2286 * @desc: gpio to be setup
2287 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2288 * of_get_gpio_hog()
2289 *
2290 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2291 */
2292static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2293{
2294 if (lflags & GPIO_ACTIVE_LOW)
2295 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2296 if (lflags & GPIO_OPEN_DRAIN)
2297 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2298 if (lflags & GPIO_OPEN_SOURCE)
2299 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2300}
Benoit Parrotf625d462015-02-02 11:44:44 -06002301
2302/**
2303 * gpiod_configure_flags - helper function to configure a given GPIO
2304 * @desc: gpio whose value will be assigned
2305 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06002306 * @dflags: gpiod_flags - optional GPIO initialization flags
2307 *
2308 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2309 * requested function and/or index, or another IS_ERR() code if an error
2310 * occurred while trying to acquire the GPIO.
2311 */
2312static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002313 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06002314{
2315 int status;
2316
Benoit Parrotf625d462015-02-02 11:44:44 -06002317 /* No particular flag request, return here... */
2318 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2319 pr_debug("no flags found for %s\n", con_id);
2320 return 0;
2321 }
2322
2323 /* Process flags */
2324 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2325 status = gpiod_direction_output(desc,
2326 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2327 else
2328 status = gpiod_direction_input(desc);
2329
2330 return status;
2331}
2332
Thierry Reding29a1f2332014-04-25 17:10:06 +02002333/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002334 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002335 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002336 * @con_id: function within the GPIO consumer
2337 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002338 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002339 *
2340 * This variant of gpiod_get() allows to access GPIOs other than the first
2341 * defined one for functions that define several GPIOs.
2342 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002343 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2344 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07002345 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002346 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002347struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002348 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002349 unsigned int idx,
2350 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002351{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002352 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002353 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002354 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002355
2356 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2357
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002358 if (dev) {
2359 /* Using device tree? */
2360 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2361 dev_dbg(dev, "using device tree for GPIO lookup\n");
2362 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2363 } else if (ACPI_COMPANION(dev)) {
2364 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2365 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2366 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002367 }
2368
2369 /*
2370 * Either we are not using DT or ACPI, or their lookup did not return
2371 * a result. In that case, use platform lookup as a fallback.
2372 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002373 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002374 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002375 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002376 }
2377
2378 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002379 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002380 return desc;
2381 }
2382
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002383 gpiod_parse_flags(desc, lookupflags);
2384
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002385 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002386 if (status < 0)
2387 return ERR_PTR(status);
2388
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002389 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002390 if (status < 0) {
2391 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2392 gpiod_put(desc);
2393 return ERR_PTR(status);
2394 }
2395
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002396 return desc;
2397}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002398EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002399
2400/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002401 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2402 * @fwnode: handle of the firmware node
2403 * @propname: name of the firmware property representing the GPIO
2404 *
2405 * This function can be used for drivers that get their configuration
2406 * from firmware.
2407 *
2408 * Function properly finds the corresponding GPIO using whatever is the
2409 * underlying firmware interface and then makes sure that the GPIO
2410 * descriptor is requested before it is returned to the caller.
2411 *
2412 * In case of error an ERR_PTR() is returned.
2413 */
2414struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2415 const char *propname)
2416{
2417 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2418 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002419 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02002420 int ret;
2421
2422 if (!fwnode)
2423 return ERR_PTR(-EINVAL);
2424
2425 if (is_of_node(fwnode)) {
2426 enum of_gpio_flags flags;
2427
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002428 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002429 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002430 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02002431 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002432 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2433 }
Mika Westerberg40b73182014-10-21 13:33:59 +02002434 } else if (is_acpi_node(fwnode)) {
2435 struct acpi_gpio_info info;
2436
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02002437 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02002438 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01002439 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02002440 }
2441
2442 if (IS_ERR(desc))
2443 return desc;
2444
Mika Westerberg40b73182014-10-21 13:33:59 +02002445 if (active_low)
2446 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2447
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002448 if (single_ended) {
2449 if (active_low)
2450 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2451 else
2452 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2453 }
2454
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002455 ret = gpiod_request(desc, NULL);
2456 if (ret)
2457 return ERR_PTR(ret);
2458
Mika Westerberg40b73182014-10-21 13:33:59 +02002459 return desc;
2460}
2461EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2462
2463/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002464 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2465 * function
2466 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2467 * @con_id: function within the GPIO consumer
2468 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002469 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002470 *
2471 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2472 * specified index was assigned to the requested function it will return NULL.
2473 * This is convenient for drivers that need to handle optional GPIOs.
2474 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002475struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002476 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002477 unsigned int index,
2478 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002479{
2480 struct gpio_desc *desc;
2481
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002482 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002483 if (IS_ERR(desc)) {
2484 if (PTR_ERR(desc) == -ENOENT)
2485 return NULL;
2486 }
2487
2488 return desc;
2489}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002490EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002491
2492/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002493 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2494 * @desc: gpio whose value will be assigned
2495 * @name: gpio line name
2496 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2497 * of_get_gpio_hog()
2498 * @dflags: gpiod_flags - optional GPIO initialization flags
2499 */
2500int gpiod_hog(struct gpio_desc *desc, const char *name,
2501 unsigned long lflags, enum gpiod_flags dflags)
2502{
2503 struct gpio_chip *chip;
2504 struct gpio_desc *local_desc;
2505 int hwnum;
2506 int status;
2507
2508 chip = gpiod_to_chip(desc);
2509 hwnum = gpio_chip_hwgpio(desc);
2510
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002511 gpiod_parse_flags(desc, lflags);
2512
Benoit Parrotf625d462015-02-02 11:44:44 -06002513 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2514 if (IS_ERR(local_desc)) {
Linus Walleija7138902015-06-05 11:36:10 +02002515 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2516 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002517 return PTR_ERR(local_desc);
2518 }
2519
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002520 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002521 if (status < 0) {
Linus Walleija7138902015-06-05 11:36:10 +02002522 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2523 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002524 gpiochip_free_own_desc(desc);
2525 return status;
2526 }
2527
2528 /* Mark GPIO as hogged so it can be identified and removed later */
2529 set_bit(FLAG_IS_HOGGED, &desc->flags);
2530
2531 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2532 desc_to_gpio(desc), name,
2533 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2534 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2535 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2536
2537 return 0;
2538}
2539
2540/**
2541 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2542 * @chip: gpio chip to act on
2543 *
2544 * This is only used by of_gpiochip_remove to free hogged gpios
2545 */
2546static void gpiochip_free_hogs(struct gpio_chip *chip)
2547{
2548 int id;
2549
2550 for (id = 0; id < chip->ngpio; id++) {
2551 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2552 gpiochip_free_own_desc(&chip->desc[id]);
2553 }
2554}
2555
2556/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002557 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2558 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2559 * @con_id: function within the GPIO consumer
2560 * @flags: optional GPIO initialization flags
2561 *
2562 * This function acquires all the GPIOs defined under a given function.
2563 *
2564 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2565 * no GPIO has been assigned to the requested function, or another IS_ERR()
2566 * code if an error occurred while trying to acquire the GPIOs.
2567 */
2568struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2569 const char *con_id,
2570 enum gpiod_flags flags)
2571{
2572 struct gpio_desc *desc;
2573 struct gpio_descs *descs;
2574 int count;
2575
2576 count = gpiod_count(dev, con_id);
2577 if (count < 0)
2578 return ERR_PTR(count);
2579
2580 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2581 GFP_KERNEL);
2582 if (!descs)
2583 return ERR_PTR(-ENOMEM);
2584
2585 for (descs->ndescs = 0; descs->ndescs < count; ) {
2586 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2587 if (IS_ERR(desc)) {
2588 gpiod_put_array(descs);
2589 return ERR_CAST(desc);
2590 }
2591 descs->desc[descs->ndescs] = desc;
2592 descs->ndescs++;
2593 }
2594 return descs;
2595}
2596EXPORT_SYMBOL_GPL(gpiod_get_array);
2597
2598/**
2599 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2600 * function
2601 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2602 * @con_id: function within the GPIO consumer
2603 * @flags: optional GPIO initialization flags
2604 *
2605 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2606 * assigned to the requested function it will return NULL.
2607 */
2608struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2609 const char *con_id,
2610 enum gpiod_flags flags)
2611{
2612 struct gpio_descs *descs;
2613
2614 descs = gpiod_get_array(dev, con_id, flags);
2615 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2616 return NULL;
2617
2618 return descs;
2619}
2620EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2621
2622/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002623 * gpiod_put - dispose of a GPIO descriptor
2624 * @desc: GPIO descriptor to dispose of
2625 *
2626 * No descriptor can be used after gpiod_put() has been called on it.
2627 */
2628void gpiod_put(struct gpio_desc *desc)
2629{
2630 gpiod_free(desc);
2631}
2632EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002633
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002634/**
2635 * gpiod_put_array - dispose of multiple GPIO descriptors
2636 * @descs: struct gpio_descs containing an array of descriptors
2637 */
2638void gpiod_put_array(struct gpio_descs *descs)
2639{
2640 unsigned int i;
2641
2642 for (i = 0; i < descs->ndescs; i++)
2643 gpiod_put(descs->desc[i]);
2644
2645 kfree(descs);
2646}
2647EXPORT_SYMBOL_GPL(gpiod_put_array);
2648
Linus Walleij3c702e92015-10-21 15:29:53 +02002649static int __init gpiolib_dev_init(void)
2650{
2651 int ret;
2652
2653 /* Register GPIO sysfs bus */
2654 ret = bus_register(&gpio_bus_type);
2655 if (ret < 0) {
2656 pr_err("gpiolib: could not register GPIO bus type\n");
2657 return ret;
2658 }
2659
2660 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
2661 if (ret < 0) {
2662 pr_err("gpiolib: failed to allocate char dev region\n");
2663 bus_unregister(&gpio_bus_type);
2664 }
2665 return ret;
2666}
2667core_initcall(gpiolib_dev_init);
2668
David Brownelld2876d02008-02-04 22:28:20 -08002669#ifdef CONFIG_DEBUG_FS
2670
2671static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2672{
2673 unsigned i;
2674 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002675 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002676 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002677 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002678
2679 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02002680 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2681 if (gdesc->name) {
2682 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2683 gpio, gdesc->name);
2684 }
David Brownelld2876d02008-02-04 22:28:20 -08002685 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02002686 }
David Brownelld2876d02008-02-04 22:28:20 -08002687
Alexandre Courbot372e7222013-02-03 01:29:29 +09002688 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002689 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002690 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02002691 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2692 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08002693 is_out ? "out" : "in ",
2694 chip->get
2695 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002696 : "? ",
2697 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002698 seq_printf(s, "\n");
2699 }
2700}
2701
Thierry Redingf9c4a312012-04-12 13:26:01 +02002702static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002703{
Grant Likely362432a2013-02-09 09:41:49 +00002704 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002705 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002706 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002707
2708 s->private = "";
2709
Grant Likely362432a2013-02-09 09:41:49 +00002710 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002711 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00002712 if (index-- == 0) {
2713 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002714 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00002715 }
2716 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002717
2718 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002719}
2720
2721static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2722{
Grant Likely362432a2013-02-09 09:41:49 +00002723 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002724 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002725 void *ret = NULL;
2726
Grant Likely362432a2013-02-09 09:41:49 +00002727 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002728 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002729 ret = NULL;
2730 else
Linus Walleijff2b1352015-10-20 11:10:38 +02002731 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00002732 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002733
2734 s->private = "\n";
2735 ++*pos;
2736
2737 return ret;
2738}
2739
2740static void gpiolib_seq_stop(struct seq_file *s, void *v)
2741{
2742}
2743
2744static int gpiolib_seq_show(struct seq_file *s, void *v)
2745{
Linus Walleijff2b1352015-10-20 11:10:38 +02002746 struct gpio_device *gdev = v;
2747 struct gpio_chip *chip = gdev->chip;
2748 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002749
Linus Walleijff2b1352015-10-20 11:10:38 +02002750 if (!chip) {
2751 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
2752 dev_name(&gdev->dev));
2753 return 0;
2754 }
2755
2756 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
2757 dev_name(&gdev->dev),
2758 chip->base, chip->base + chip->ngpio - 1);
2759 parent = chip->parent;
2760 if (parent)
2761 seq_printf(s, ", parent: %s/%s",
2762 parent->bus ? parent->bus->name : "no-bus",
2763 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02002764 if (chip->label)
2765 seq_printf(s, ", %s", chip->label);
2766 if (chip->can_sleep)
2767 seq_printf(s, ", can sleep");
2768 seq_printf(s, ":\n");
2769
2770 if (chip->dbg_show)
2771 chip->dbg_show(s, chip);
2772 else
2773 gpiolib_dbg_show(s, chip);
2774
David Brownelld2876d02008-02-04 22:28:20 -08002775 return 0;
2776}
2777
Thierry Redingf9c4a312012-04-12 13:26:01 +02002778static const struct seq_operations gpiolib_seq_ops = {
2779 .start = gpiolib_seq_start,
2780 .next = gpiolib_seq_next,
2781 .stop = gpiolib_seq_stop,
2782 .show = gpiolib_seq_show,
2783};
2784
David Brownelld2876d02008-02-04 22:28:20 -08002785static int gpiolib_open(struct inode *inode, struct file *file)
2786{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002787 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002788}
2789
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002790static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002791 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002792 .open = gpiolib_open,
2793 .read = seq_read,
2794 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002795 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002796};
2797
2798static int __init gpiolib_debugfs_init(void)
2799{
2800 /* /sys/kernel/debug/gpio */
2801 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2802 NULL, NULL, &gpiolib_operations);
2803 return 0;
2804}
2805subsys_initcall(gpiolib_debugfs_init);
2806
2807#endif /* DEBUG_FS */