blob: f3fcd415a77b7ed4a358271c6995510b8fbc05d9 [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 Walleij1c3cdb12016-02-09 13:51:59 +010091 return &gdev->descs[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
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100113 return &chip->gpiodev->descs[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{
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100123 return desc->chip->base + (desc - &desc->chip->gpiodev->descs[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) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100280 struct gpio_desc *gpio = &gdev->descs[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)
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100323 gc->gpiodev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200324
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);
Linus Walleij9efd9e62016-02-09 14:27:42 +0100408 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200409}
410
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700411/**
Linus Walleijb08ea352015-12-03 15:14:13 +0100412 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -0800413 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900414 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -0800415 *
416 * Returns a negative errno if the chip can't be registered, such as
417 * because the chip->base is invalid or already associated with a
418 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700419 *
Linus Walleijb08ea352015-12-03 15:14:13 +0100420 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800421 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d2008-07-25 01:46:07 -0700422 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
423 * for GPIOs will fail rudely.
424 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700425 * If chip->base is negative, this requests dynamic assignment of
426 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800427 */
Linus Walleijb08ea352015-12-03 15:14:13 +0100428int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -0800429{
430 unsigned long flags;
431 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +0200432 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700433 int base = chip->base;
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 Walleij1c3cdb12016-02-09 13:51:59 +0100472 gdev->descs = devm_kcalloc(&gdev->dev, chip->ngpio,
473 sizeof(gdev->descs[0]), GFP_KERNEL);
474 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +0200475 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;
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100485 goto err_free_gdev;
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);
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100495 goto err_free_gdev;
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);
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100503 goto err_free_gdev;
Johan Hovold05aa5202015-01-12 17:12:26 +0100504 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900505
Linus Walleijff2b1352015-10-20 11:10:38 +0200506 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100507 struct gpio_desc *desc = &gdev->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
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800521 spin_unlock_irqrestore(&gpio_lock, flags);
522
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530523#ifdef CONFIG_PINCTRL
Linus Walleijff2b1352015-10-20 11:10:38 +0200524 /* FIXME: move pin ranges to gpio_device */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530525 INIT_LIST_HEAD(&chip->pin_ranges);
526#endif
527
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200528 status = gpiochip_set_desc_names(chip);
529 if (status)
530 goto err_remove_from_list;
531
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200532 status = of_gpiochip_add(chip);
533 if (status)
534 goto err_remove_chip;
535
Mika Westerberg664e3e52014-01-08 12:40:54 +0200536 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600537
Linus Walleij3c702e92015-10-21 15:29:53 +0200538 /*
539 * By first adding the chardev, and then adding the device,
540 * we get a device node entry in sysfs under
541 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
542 * coldplug of device nodes and other udev business.
543 */
544 cdev_init(&gdev->chrdev, &gpio_fileops);
545 gdev->chrdev.owner = THIS_MODULE;
546 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
547 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
548 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
549 if (status < 0)
550 chip_warn(chip, "failed to add char device %d:%d\n",
551 MAJOR(gpio_devt), gdev->id);
552 else
553 chip_dbg(chip, "added GPIO chardev (%d:%d)\n",
554 MAJOR(gpio_devt), gdev->id);
Linus Walleijff2b1352015-10-20 11:10:38 +0200555 status = device_add(&gdev->dev);
Johan Hovold225fce82015-01-12 17:12:25 +0100556 if (status)
Linus Walleij3c702e92015-10-21 15:29:53 +0200557 goto err_remove_chardev;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600558
Linus Walleijafbc4f32016-02-09 13:21:06 +0100559 status = gpiochip_sysfs_register(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200560 if (status)
561 goto err_remove_device;
562
563 /* From this point, the .release() function cleans up gpio_device */
564 gdev->dev.release = gpiodevice_release;
565 get_device(&gdev->dev);
Andy Shevchenko7589e592013-12-05 11:26:23 +0200566 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700567 chip->base, chip->base + chip->ngpio - 1,
568 chip->label ? : "generic");
569
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600570 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800571
Linus Walleijff2b1352015-10-20 11:10:38 +0200572err_remove_device:
573 device_del(&gdev->dev);
Linus Walleij3c702e92015-10-21 15:29:53 +0200574err_remove_chardev:
575 cdev_del(&gdev->chrdev);
Johan Hovold225fce82015-01-12 17:12:25 +0100576err_remove_chip:
577 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200578 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100579 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200580err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100581 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200582 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800583 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200584err_free_gdev:
585 ida_simple_remove(&gpio_ida, gdev->id);
586 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -0800587 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200588 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600589 chip->base, chip->base + chip->ngpio - 1,
590 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800591 return status;
592}
Linus Walleijb08ea352015-12-03 15:14:13 +0100593EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -0800594
595/**
596 * gpiochip_remove() - unregister a gpio_chip
597 * @chip: the chip to unregister
598 *
599 * A gpio_chip with any GPIOs still requested may not be removed.
600 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200601void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800602{
Linus Walleijff2b1352015-10-20 11:10:38 +0200603 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200604 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800605 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100606 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200607 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800608
Linus Walleijff2b1352015-10-20 11:10:38 +0200609 /* Numb the device, cancelling all outstanding operations */
610 gdev->chip = NULL;
611
612 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +0100613 gpiochip_sysfs_unregister(gdev);
Johan Hovold00acc3d2015-01-12 17:12:27 +0100614 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200615 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100616 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600617 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600618 of_gpiochip_remove(chip);
619
Johan Hovold6798aca2015-01-12 17:12:28 +0100620 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100621 for (i = 0; i < chip->ngpio; i++) {
622 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +0200623 desc->chip = NULL;
624 if (test_bit(FLAG_REQUESTED, &desc->flags))
625 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800626 }
David Brownelld2876d02008-02-04 22:28:20 -0800627 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900628
Johan Hovoldfab28b82015-05-04 17:10:27 +0200629 if (requested)
Linus Walleij34ffd852015-10-20 11:31:54 +0200630 dev_crit(&chip->gpiodev->dev,
Linus Walleij58383c72015-11-04 09:56:26 +0100631 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +0200632
Linus Walleijff2b1352015-10-20 11:10:38 +0200633 /*
634 * The gpiochip side puts its use of the device to rest here:
635 * if there are no userspace clients, the chardev and device will
636 * be removed, else it will be dangling until the last user is
637 * gone.
638 */
639 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -0800640}
641EXPORT_SYMBOL_GPL(gpiochip_remove);
642
Grant Likely594fa262010-06-08 07:48:16 -0600643/**
644 * gpiochip_find() - iterator for locating a specific gpio_chip
645 * @data: data to pass to match function
646 * @callback: Callback function to check gpio_chip
647 *
648 * Similar to bus_find_device. It returns a reference to a gpio_chip as
649 * determined by a user supplied @match callback. The callback should return
650 * 0 if the device doesn't match and non-zero if it does. If the callback is
651 * non-zero, this function will return to the caller and not iterate over any
652 * more gpio_chips.
653 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600654struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700655 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600656 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600657{
Linus Walleijff2b1352015-10-20 11:10:38 +0200658 struct gpio_device *gdev;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900659 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600660 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600661
662 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200663 list_for_each_entry(gdev, &gpio_devices, list)
664 if (match(gdev->chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600665 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900666
667 /* No match? */
Linus Walleijff2b1352015-10-20 11:10:38 +0200668 if (&gdev->list == &gpio_devices)
Alexandre Courbot125eef92013-02-03 01:29:26 +0900669 chip = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +0200670 else
671 chip = gdev->chip;
672
Grant Likely594fa262010-06-08 07:48:16 -0600673 spin_unlock_irqrestore(&gpio_lock, flags);
674
675 return chip;
676}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600677EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800678
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900679static int gpiochip_match_name(struct gpio_chip *chip, void *data)
680{
681 const char *name = data;
682
683 return !strcmp(chip->label, name);
684}
685
686static struct gpio_chip *find_chip_by_name(const char *name)
687{
688 return gpiochip_find((void *)name, gpiochip_match_name);
689}
690
Linus Walleij14250522014-03-25 10:40:18 +0100691#ifdef CONFIG_GPIOLIB_IRQCHIP
692
693/*
694 * The following is irqchip helper code for gpiochips.
695 */
696
697/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200698 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
699 * @gpiochip: the gpiochip to set the irqchip chain to
700 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100701 * @parent_irq: the irq number corresponding to the parent IRQ for this
702 * chained irqchip
703 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200704 * coming out of the gpiochip. If the interrupt is nested rather than
705 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100706 */
707void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
708 struct irq_chip *irqchip,
709 int parent_irq,
710 irq_flow_handler_t parent_handler)
711{
Linus Walleij83141a72014-09-26 13:50:12 +0200712 unsigned int offset;
713
Linus Walleij83141a72014-09-26 13:50:12 +0200714 if (!gpiochip->irqdomain) {
715 chip_err(gpiochip, "called %s before setting up irqchip\n",
716 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200717 return;
718 }
719
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200720 if (parent_handler) {
721 if (gpiochip->can_sleep) {
722 chip_err(gpiochip,
723 "you cannot have chained interrupts on a "
724 "chip that may sleep\n");
725 return;
726 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200727 /*
728 * The parent irqchip is already using the chip_data for this
729 * irqchip, so our callbacks simply use the handler_data.
730 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +0200731 irq_set_chained_handler_and_data(parent_irq, parent_handler,
732 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300733
734 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200735 }
Linus Walleij83141a72014-09-26 13:50:12 +0200736
737 /* Set the parent IRQ for all affected IRQs */
738 for (offset = 0; offset < gpiochip->ngpio; offset++)
739 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
740 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100741}
742EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
743
744/**
745 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
746 * @d: the irqdomain used by this irqchip
747 * @irq: the global irq number used by this GPIO irqchip irq
748 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
749 *
750 * This function will set up the mapping for a certain IRQ line on a
751 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
752 * stored inside the gpiochip.
753 */
754static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
755 irq_hw_number_t hwirq)
756{
757 struct gpio_chip *chip = d->host_data;
758
Linus Walleij14250522014-03-25 10:40:18 +0100759 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300760 /*
761 * This lock class tells lockdep that GPIO irqs are in a different
762 * category than their parents, so it won't report false recursion.
763 */
764 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +0200765 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200766 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300767 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200768 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100769 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -0500770
Linus Walleij1333b902014-04-23 16:45:12 +0200771 /*
772 * No set-up of the hardware will happen if IRQ_TYPE_NONE
773 * is passed as default type.
774 */
775 if (chip->irq_default_type != IRQ_TYPE_NONE)
776 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100777
778 return 0;
779}
780
Linus Walleijc3626fd2014-03-28 20:42:01 +0100781static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
782{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200783 struct gpio_chip *chip = d->host_data;
784
Linus Walleij1c8732b2014-04-09 13:34:39 +0200785 if (chip->can_sleep)
786 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100787 irq_set_chip_and_handler(irq, NULL, NULL);
788 irq_set_chip_data(irq, NULL);
789}
790
Linus Walleij14250522014-03-25 10:40:18 +0100791static const struct irq_domain_ops gpiochip_domain_ops = {
792 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100793 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100794 /* Virtually all GPIO irqchips are twocell:ed */
795 .xlate = irq_domain_xlate_twocell,
796};
797
798static int gpiochip_irq_reqres(struct irq_data *d)
799{
800 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
801
Linus Walleijff2b1352015-10-20 11:10:38 +0200802 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300803 return -ENODEV;
804
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900805 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100806 chip_err(chip,
807 "unable to lock HW IRQ %lu for IRQ\n",
808 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200809 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100810 return -EINVAL;
811 }
812 return 0;
813}
814
815static void gpiochip_irq_relres(struct irq_data *d)
816{
817 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
818
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900819 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200820 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100821}
822
823static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
824{
825 return irq_find_mapping(chip->irqdomain, offset);
826}
827
828/**
829 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
830 * @gpiochip: the gpiochip to remove the irqchip from
831 *
832 * This is called only from gpiochip_remove()
833 */
834static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
835{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100836 unsigned int offset;
837
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300838 acpi_gpiochip_free_interrupts(gpiochip);
839
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300840 if (gpiochip->irq_parent) {
841 irq_set_chained_handler(gpiochip->irq_parent, NULL);
842 irq_set_handler_data(gpiochip->irq_parent, NULL);
843 }
844
Linus Walleijc3626fd2014-03-28 20:42:01 +0100845 /* Remove all IRQ mappings and delete the domain */
846 if (gpiochip->irqdomain) {
847 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300848 irq_dispose_mapping(
849 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100850 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100851 }
Linus Walleij14250522014-03-25 10:40:18 +0100852
853 if (gpiochip->irqchip) {
854 gpiochip->irqchip->irq_request_resources = NULL;
855 gpiochip->irqchip->irq_release_resources = NULL;
856 gpiochip->irqchip = NULL;
857 }
858}
859
860/**
861 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
862 * @gpiochip: the gpiochip to add the irqchip to
863 * @irqchip: the irqchip to add to the gpiochip
864 * @first_irq: if not dynamically assigned, the base (first) IRQ to
865 * allocate gpiochip irqs from
866 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200867 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
868 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300869 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +0100870 *
871 * This function closely associates a certain irqchip with a certain
872 * gpiochip, providing an irq domain to translate the local IRQs to
873 * global irqs in the gpiolib core, and making sure that the gpiochip
874 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +0100875 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +0100876 * from the gpiochip passed as chip data. An irqdomain will be stored
877 * in the gpiochip that shall be used by the driver to handle IRQ number
878 * translation. The gpiochip will need to be initialized and registered
879 * before calling this function.
880 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100881 * This function will handle two cell:ed simple IRQs and assumes all
882 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100883 * need to be open coded.
884 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300885int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
886 struct irq_chip *irqchip,
887 unsigned int first_irq,
888 irq_flow_handler_t handler,
889 unsigned int type,
890 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +0100891{
892 struct device_node *of_node;
893 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100894 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100895
896 if (!gpiochip || !irqchip)
897 return -EINVAL;
898
Linus Walleij58383c72015-11-04 09:56:26 +0100899 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +0100900 pr_err("missing gpiochip .dev parent pointer\n");
901 return -EINVAL;
902 }
Linus Walleij58383c72015-11-04 09:56:26 +0100903 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +0100904#ifdef CONFIG_OF_GPIO
905 /*
Colin Cronin20a8a962015-05-18 11:41:43 -0700906 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800907 * FIXME: get rid of this and use gpiochip->parent->of_node
908 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +0100909 */
910 if (gpiochip->of_node)
911 of_node = gpiochip->of_node;
912#endif
913 gpiochip->irqchip = irqchip;
914 gpiochip->irq_handler = handler;
915 gpiochip->irq_default_type = type;
916 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300917 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +0100918 gpiochip->irqdomain = irq_domain_add_simple(of_node,
919 gpiochip->ngpio, first_irq,
920 &gpiochip_domain_ops, gpiochip);
921 if (!gpiochip->irqdomain) {
922 gpiochip->irqchip = NULL;
923 return -EINVAL;
924 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +0200925
926 /*
927 * It is possible for a driver to override this, but only if the
928 * alternative functions are both implemented.
929 */
930 if (!irqchip->irq_request_resources &&
931 !irqchip->irq_release_resources) {
932 irqchip->irq_request_resources = gpiochip_irq_reqres;
933 irqchip->irq_release_resources = gpiochip_irq_relres;
934 }
Linus Walleij14250522014-03-25 10:40:18 +0100935
936 /*
937 * Prepare the mapping since the irqchip shall be orthogonal to
938 * any gpiochip calls. If the first_irq was zero, this is
939 * necessary to allocate descriptors for all IRQs.
940 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100941 for (offset = 0; offset < gpiochip->ngpio; offset++) {
942 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
943 if (offset == 0)
944 /*
945 * Store the base into the gpiochip to be used when
946 * unmapping the irqs.
947 */
948 gpiochip->irq_base = irq_base;
949 }
Linus Walleij14250522014-03-25 10:40:18 +0100950
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300951 acpi_gpiochip_request_interrupts(gpiochip);
952
Linus Walleij14250522014-03-25 10:40:18 +0100953 return 0;
954}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300955EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +0100956
957#else /* CONFIG_GPIOLIB_IRQCHIP */
958
959static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
960
961#endif /* CONFIG_GPIOLIB_IRQCHIP */
962
Jonas Gorskic771c2f2015-10-11 17:34:15 +0200963/**
964 * gpiochip_generic_request() - request the gpio function for a pin
965 * @chip: the gpiochip owning the GPIO
966 * @offset: the offset of the GPIO to request for GPIO function
967 */
968int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
969{
970 return pinctrl_request_gpio(chip->base + offset);
971}
972EXPORT_SYMBOL_GPL(gpiochip_generic_request);
973
974/**
975 * gpiochip_generic_free() - free the gpio function from a pin
976 * @chip: the gpiochip to request the gpio function for
977 * @offset: the offset of the GPIO to free from GPIO function
978 */
979void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
980{
981 pinctrl_free_gpio(chip->base + offset);
982}
983EXPORT_SYMBOL_GPL(gpiochip_generic_free);
984
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530985#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100986
Linus Walleij3f0f8672012-11-20 12:40:15 +0100987/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200988 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
989 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +0200990 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +0200991 * @gpio_offset: the start offset in the current gpio_chip number space
992 * @pin_group: name of the pin group inside the pin controller
993 */
994int gpiochip_add_pingroup_range(struct gpio_chip *chip,
995 struct pinctrl_dev *pctldev,
996 unsigned int gpio_offset, const char *pin_group)
997{
998 struct gpio_pin_range *pin_range;
999 int ret;
1000
1001 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1002 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001003 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001004 return -ENOMEM;
1005 }
1006
1007 /* Use local offset as range ID */
1008 pin_range->range.id = gpio_offset;
1009 pin_range->range.gc = chip;
1010 pin_range->range.name = chip->label;
1011 pin_range->range.base = chip->base + gpio_offset;
1012 pin_range->pctldev = pctldev;
1013
1014 ret = pinctrl_get_group_pins(pctldev, pin_group,
1015 &pin_range->range.pins,
1016 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001017 if (ret < 0) {
1018 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001019 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001020 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001021
1022 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1023
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001024 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1025 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001026 pinctrl_dev_get_devname(pctldev), pin_group);
1027
1028 list_add_tail(&pin_range->node, &chip->pin_ranges);
1029
1030 return 0;
1031}
1032EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1033
1034/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001035 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1036 * @chip: the gpiochip to add the range for
1037 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001038 * @gpio_offset: the start offset in the current gpio_chip number space
1039 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001040 * @npins: the number of pins from the offset of each pin space (GPIO and
1041 * pin controller) to accumulate in this range
1042 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001043int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001044 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001045 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301046{
1047 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001048 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301049
Linus Walleij3f0f8672012-11-20 12:40:15 +01001050 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301051 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001052 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001053 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301054 }
1055
Linus Walleij3f0f8672012-11-20 12:40:15 +01001056 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001057 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001058 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301059 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001060 pin_range->range.base = chip->base + gpio_offset;
1061 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301062 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001063 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301064 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001065 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001066 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001067 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001068 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001069 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001070 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001071 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1072 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001073 pinctl_name,
1074 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301075
1076 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001077
1078 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301079}
Linus Walleij165adc92012-11-06 14:49:39 +01001080EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301081
Linus Walleij3f0f8672012-11-20 12:40:15 +01001082/**
1083 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1084 * @chip: the chip to remove all the mappings for
1085 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301086void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1087{
1088 struct gpio_pin_range *pin_range, *tmp;
1089
1090 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1091 list_del(&pin_range->node);
1092 pinctrl_remove_gpio_range(pin_range->pctldev,
1093 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001094 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301095 }
1096}
Linus Walleij165adc92012-11-06 14:49:39 +01001097EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1098
1099#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301100
David Brownelld2876d02008-02-04 22:28:20 -08001101/* These "optional" allocation calls help prevent drivers from stomping
1102 * on each other, and help provide better diagnostics in debugfs.
1103 * They're called even less than the "set direction" calls.
1104 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001105static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001106{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001107 struct gpio_chip *chip = desc->chip;
1108 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001109 unsigned long flags;
1110
1111 spin_lock_irqsave(&gpio_lock, flags);
1112
David Brownelld2876d02008-02-04 22:28:20 -08001113 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001114 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001115 */
1116
1117 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1118 desc_set_label(desc, label ? : "?");
1119 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001120 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001121 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001122 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001123 }
1124
1125 if (chip->request) {
1126 /* chip->request may sleep */
1127 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001128 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001129 spin_lock_irqsave(&gpio_lock, flags);
1130
1131 if (status < 0) {
1132 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001133 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001134 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001135 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001136 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001137 if (chip->get_direction) {
1138 /* chip->get_direction may sleep */
1139 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001140 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001141 spin_lock_irqsave(&gpio_lock, flags);
1142 }
David Brownelld2876d02008-02-04 22:28:20 -08001143done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +03001144 if (status < 0) {
1145 /* Clear flags that might have been set by the caller before
1146 * requesting the GPIO.
1147 */
1148 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1149 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1150 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1151 }
Mika Westerberg77c2d792014-03-10 14:54:50 +02001152 spin_unlock_irqrestore(&gpio_lock, flags);
1153 return status;
1154}
1155
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001156int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001157{
1158 int status = -EPROBE_DEFER;
1159 struct gpio_chip *chip;
1160
1161 if (!desc) {
1162 pr_warn("%s: invalid GPIO\n", __func__);
1163 return -EINVAL;
1164 }
1165
1166 chip = desc->chip;
1167 if (!chip)
1168 goto done;
1169
Linus Walleijff2b1352015-10-20 11:10:38 +02001170 if (try_module_get(chip->gpiodev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001171 status = __gpiod_request(desc, label);
1172 if (status < 0)
Linus Walleijff2b1352015-10-20 11:10:38 +02001173 module_put(chip->gpiodev->owner);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001174 }
1175
1176done:
David Brownelld2876d02008-02-04 22:28:20 -08001177 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001178 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001179
David Brownelld2876d02008-02-04 22:28:20 -08001180 return status;
1181}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001182
Mika Westerberg77c2d792014-03-10 14:54:50 +02001183static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001184{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001185 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001186 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001187 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001188
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001189 might_sleep();
1190
Alexandre Courbot372e7222013-02-03 01:29:29 +09001191 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001192
David Brownelld2876d02008-02-04 22:28:20 -08001193 spin_lock_irqsave(&gpio_lock, flags);
1194
David Brownell35e8bb52008-10-15 22:03:16 -07001195 chip = desc->chip;
1196 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1197 if (chip->free) {
1198 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001199 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001200 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001201 spin_lock_irqsave(&gpio_lock, flags);
1202 }
David Brownelld2876d02008-02-04 22:28:20 -08001203 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001204 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001205 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301206 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301207 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001208 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001209 ret = true;
1210 }
David Brownelld2876d02008-02-04 22:28:20 -08001211
1212 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001213 return ret;
1214}
1215
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001216void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001217{
1218 if (desc && __gpiod_free(desc))
Linus Walleijff2b1352015-10-20 11:10:38 +02001219 module_put(desc->chip->gpiodev->owner);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001220 else
1221 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -08001222}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001223
David Brownelld2876d02008-02-04 22:28:20 -08001224/**
1225 * gpiochip_is_requested - return string iff signal was requested
1226 * @chip: controller managing the signal
1227 * @offset: of signal within controller's 0..(ngpio - 1) range
1228 *
1229 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001230 * The string returned is the label passed to gpio_request(); if none has been
1231 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001232 *
1233 * This function is for use by GPIO controller drivers. The label can
1234 * help with diagnostics, and knowing that the signal is used as a GPIO
1235 * can help avoid accidentally multiplexing it to another controller.
1236 */
1237const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1238{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001239 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001240
Dirk Behme48b59532015-08-18 18:02:32 +02001241 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001242 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001243
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001244 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001245
Alexandre Courbot372e7222013-02-03 01:29:29 +09001246 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001247 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001248 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001249}
1250EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1251
Mika Westerberg77c2d792014-03-10 14:54:50 +02001252/**
1253 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1254 * @desc: GPIO descriptor to request
1255 * @label: label for the GPIO
1256 *
1257 * Function allows GPIO chip drivers to request and use their own GPIO
1258 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1259 * function will not increase reference count of the GPIO chip module. This
1260 * allows the GPIO chip module to be unloaded as needed (we assume that the
1261 * GPIO chip driver handles freeing the GPIOs it has requested).
1262 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001263struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1264 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001265{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001266 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1267 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001268
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001269 if (IS_ERR(desc)) {
1270 chip_err(chip, "failed to get GPIO descriptor\n");
1271 return desc;
1272 }
1273
1274 err = __gpiod_request(desc, label);
1275 if (err < 0)
1276 return ERR_PTR(err);
1277
1278 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001279}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001280EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001281
1282/**
1283 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1284 * @desc: GPIO descriptor to free
1285 *
1286 * Function frees the given GPIO requested previously with
1287 * gpiochip_request_own_desc().
1288 */
1289void gpiochip_free_own_desc(struct gpio_desc *desc)
1290{
1291 if (desc)
1292 __gpiod_free(desc);
1293}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001294EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08001295
1296/* Drivers MUST set GPIO direction before making get/set calls. In
1297 * some cases this is done in early boot, before IRQs are enabled.
1298 *
1299 * As a rule these aren't called more than once (except for drivers
1300 * using the open-drain emulation idiom) so these are natural places
1301 * to accumulate extra debugging checks. Note that we can't (yet)
1302 * rely on gpio_request() having been called beforehand.
1303 */
1304
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001305/**
1306 * gpiod_direction_input - set the GPIO direction to input
1307 * @desc: GPIO to set to input
1308 *
1309 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1310 * be called safely on it.
1311 *
1312 * Return 0 in case of success, else an error code.
1313 */
1314int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001315{
David Brownelld2876d02008-02-04 22:28:20 -08001316 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001317 int status = -EINVAL;
1318
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001319 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001320 pr_warn("%s: invalid GPIO\n", __func__);
1321 return -EINVAL;
1322 }
1323
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001324 chip = desc->chip;
1325 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001326 gpiod_warn(desc,
1327 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001328 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001329 return -EIO;
1330 }
1331
Alexandre Courbotd82da792014-07-22 16:17:43 +09001332 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001333 if (status == 0)
1334 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001335
Alexandre Courbot372e7222013-02-03 01:29:29 +09001336 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001337
David Brownelld2876d02008-02-04 22:28:20 -08001338 return status;
1339}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001340EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001341
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001342static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001343{
David Brownelld2876d02008-02-04 22:28:20 -08001344 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001345 int status = -EINVAL;
1346
Linus Walleijd468bf92013-09-24 11:54:38 +02001347 /* GPIOs used for IRQs shall not be set as output */
1348 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1349 gpiod_err(desc,
1350 "%s: tried to set a GPIO tied to an IRQ as output\n",
1351 __func__);
1352 return -EIO;
1353 }
1354
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301355 /* Open drain pin should not be driven to 1 */
1356 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001357 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301358
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301359 /* Open source pin should not be driven to 0 */
1360 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001361 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301362
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001363 chip = desc->chip;
1364 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001365 gpiod_warn(desc,
1366 "%s: missing set() or direction_output() operations\n",
1367 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001368 return -EIO;
1369 }
1370
Alexandre Courbotd82da792014-07-22 16:17:43 +09001371 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001372 if (status == 0)
1373 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001374 trace_gpio_value(desc_to_gpio(desc), 0, value);
1375 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001376 return status;
1377}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001378
1379/**
1380 * gpiod_direction_output_raw - set the GPIO direction to output
1381 * @desc: GPIO to set to output
1382 * @value: initial output value of the GPIO
1383 *
1384 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1385 * be called safely on it. The initial value of the output must be specified
1386 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1387 *
1388 * Return 0 in case of success, else an error code.
1389 */
1390int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1391{
1392 if (!desc || !desc->chip) {
1393 pr_warn("%s: invalid GPIO\n", __func__);
1394 return -EINVAL;
1395 }
1396 return _gpiod_direction_output_raw(desc, value);
1397}
1398EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1399
1400/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301401 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001402 * @desc: GPIO to set to output
1403 * @value: initial output value of the GPIO
1404 *
1405 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1406 * be called safely on it. The initial value of the output must be specified
1407 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1408 * account.
1409 *
1410 * Return 0 in case of success, else an error code.
1411 */
1412int gpiod_direction_output(struct gpio_desc *desc, int value)
1413{
1414 if (!desc || !desc->chip) {
1415 pr_warn("%s: invalid GPIO\n", __func__);
1416 return -EINVAL;
1417 }
1418 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1419 value = !value;
1420 return _gpiod_direction_output_raw(desc, value);
1421}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001422EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001423
Felipe Balbic4b5be92010-05-26 14:42:23 -07001424/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001425 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001426 * @gpio: the gpio to set debounce time
1427 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001428 *
1429 * returns -ENOTSUPP if the controller does not support setting
1430 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001431 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001432int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001433{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001434 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001435
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001436 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001437 pr_warn("%s: invalid GPIO\n", __func__);
1438 return -EINVAL;
1439 }
1440
Felipe Balbic4b5be92010-05-26 14:42:23 -07001441 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001442 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001443 gpiod_dbg(desc,
1444 "%s: missing set() or set_debounce() operations\n",
1445 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001446 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001447 }
1448
Alexandre Courbotd82da792014-07-22 16:17:43 +09001449 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001450}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001451EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001452
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001453/**
1454 * gpiod_is_active_low - test whether a GPIO is active-low or not
1455 * @desc: the gpio descriptor to test
1456 *
1457 * Returns 1 if the GPIO is active-low, 0 otherwise.
1458 */
1459int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001460{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001461 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001462}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001463EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001464
1465/* I/O calls are only valid after configuration completed; the relevant
1466 * "is this a valid GPIO" error checks should already have been done.
1467 *
1468 * "Get" operations are often inlinable as reading a pin value register,
1469 * and masking the relevant bit in that register.
1470 *
1471 * When "set" operations are inlinable, they involve writing that mask to
1472 * one register to set a low value, or a different register to set it high.
1473 * Otherwise locking is needed, so there may be little value to inlining.
1474 *
1475 *------------------------------------------------------------------------
1476 *
1477 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1478 * have requested the GPIO. That can include implicit requesting by
1479 * a direction setting call. Marking a gpio as requested locks its chip
1480 * in memory, guaranteeing that these table lookups need no more locking
1481 * and that gpiochip_remove() will fail.
1482 *
1483 * REVISIT when debugging, consider adding some instrumentation to ensure
1484 * that the GPIO was actually requested.
1485 */
1486
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001487static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001488{
1489 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001490 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001491 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001492
Alexandre Courbot372e7222013-02-03 01:29:29 +09001493 chip = desc->chip;
1494 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001495 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01001496 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001497 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001498 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001499}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001500
David Brownelld2876d02008-02-04 22:28:20 -08001501/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001502 * gpiod_get_raw_value() - return a gpio's raw value
1503 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001504 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001505 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001506 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001507 *
1508 * This function should be called from contexts where we cannot sleep, and will
1509 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001510 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001511int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001512{
David Brownelld2876d02008-02-04 22:28:20 -08001513 if (!desc)
1514 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001515 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001516 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001517 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001518}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001519EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001520
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001521/**
1522 * gpiod_get_value() - return a gpio's value
1523 * @desc: gpio whose value will be returned
1524 *
1525 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001526 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001527 *
1528 * This function should be called from contexts where we cannot sleep, and will
1529 * complain if the GPIO chip functions potentially sleep.
1530 */
1531int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001532{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001533 int value;
1534 if (!desc)
1535 return 0;
1536 /* Should be using gpio_get_value_cansleep() */
1537 WARN_ON(desc->chip->can_sleep);
1538
1539 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001540 if (value < 0)
1541 return value;
1542
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001543 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1544 value = !value;
1545
1546 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001547}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001548EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001549
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301550/*
1551 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001552 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001553 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301554 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001555static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301556{
1557 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001558 struct gpio_chip *chip = desc->chip;
1559 int offset = gpio_chip_hwgpio(desc);
1560
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301561 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001562 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301563 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001564 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301565 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001566 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301567 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001568 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301569 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001570 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301571 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001572 gpiod_err(desc,
1573 "%s: Error in set_value for open drain err %d\n",
1574 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301575}
1576
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301577/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001578 * _gpio_set_open_source_value() - Set the open source gpio's value.
1579 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001580 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301581 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001582static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301583{
1584 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001585 struct gpio_chip *chip = desc->chip;
1586 int offset = gpio_chip_hwgpio(desc);
1587
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301588 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001589 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301590 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001591 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301592 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001593 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301594 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001595 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301596 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001597 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301598 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001599 gpiod_err(desc,
1600 "%s: Error in set_value for open source err %d\n",
1601 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301602}
1603
Alexandre Courbot23600962014-03-11 15:52:09 +09001604static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001605{
1606 struct gpio_chip *chip;
1607
Alexandre Courbot372e7222013-02-03 01:29:29 +09001608 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001609 trace_gpio_value(desc_to_gpio(desc), 0, value);
1610 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1611 _gpio_set_open_drain_value(desc, value);
1612 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1613 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301614 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001615 chip->set(chip, gpio_chip_hwgpio(desc), value);
1616}
1617
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001618/*
1619 * set multiple outputs on the same chip;
1620 * use the chip's set_multiple function if available;
1621 * otherwise set the outputs sequentially;
1622 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1623 * defines which outputs are to be changed
1624 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1625 * defines the values the outputs specified by mask are to be set to
1626 */
1627static void gpio_chip_set_multiple(struct gpio_chip *chip,
1628 unsigned long *mask, unsigned long *bits)
1629{
1630 if (chip->set_multiple) {
1631 chip->set_multiple(chip, mask, bits);
1632 } else {
1633 int i;
1634 for (i = 0; i < chip->ngpio; i++) {
1635 if (mask[BIT_WORD(i)] == 0) {
1636 /* no more set bits in this mask word;
1637 * skip ahead to the next word */
1638 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1639 continue;
1640 }
1641 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001642 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001643 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001644 }
1645 }
1646}
1647
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001648static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1649 unsigned int array_size,
1650 struct gpio_desc **desc_array,
1651 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001652{
1653 int i = 0;
1654
1655 while (i < array_size) {
1656 struct gpio_chip *chip = desc_array[i]->chip;
1657 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1658 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1659 int count = 0;
1660
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001661 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001662 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001663
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001664 memset(mask, 0, sizeof(mask));
1665 do {
1666 struct gpio_desc *desc = desc_array[i];
1667 int hwgpio = gpio_chip_hwgpio(desc);
1668 int value = value_array[i];
1669
1670 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1671 value = !value;
1672 trace_gpio_value(desc_to_gpio(desc), 0, value);
1673 /*
1674 * collect all normal outputs belonging to the same chip
1675 * open drain and open source outputs are set individually
1676 */
1677 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001678 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001679 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1680 _gpio_set_open_source_value(desc, value);
1681 } else {
1682 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001683 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001684 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001685 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001686 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001687 count++;
1688 }
1689 i++;
1690 } while ((i < array_size) && (desc_array[i]->chip == chip));
1691 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001692 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001693 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001694 }
1695}
1696
David Brownelld2876d02008-02-04 22:28:20 -08001697/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001698 * gpiod_set_raw_value() - assign a gpio's raw value
1699 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001700 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001701 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001702 * Set the raw value of the GPIO, i.e. the value of its physical line without
1703 * regard for its ACTIVE_LOW status.
1704 *
1705 * This function should be called from contexts where we cannot sleep, and will
1706 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001707 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001708void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001709{
David Brownelld2876d02008-02-04 22:28:20 -08001710 if (!desc)
1711 return;
David Brownelld2876d02008-02-04 22:28:20 -08001712 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001713 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001714 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001715}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001716EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001717
1718/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001719 * gpiod_set_value() - assign a gpio's value
1720 * @desc: gpio whose value will be assigned
1721 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001722 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001723 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1724 * account
1725 *
1726 * This function should be called from contexts where we cannot sleep, and will
1727 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001728 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001729void gpiod_set_value(struct gpio_desc *desc, int value)
1730{
1731 if (!desc)
1732 return;
1733 /* Should be using gpio_set_value_cansleep() */
1734 WARN_ON(desc->chip->can_sleep);
1735 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1736 value = !value;
1737 _gpiod_set_raw_value(desc, value);
1738}
1739EXPORT_SYMBOL_GPL(gpiod_set_value);
1740
1741/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001742 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001743 * @array_size: number of elements in the descriptor / value arrays
1744 * @desc_array: array of GPIO descriptors whose values will be assigned
1745 * @value_array: array of values to assign
1746 *
1747 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1748 * without regard for their ACTIVE_LOW status.
1749 *
1750 * This function should be called from contexts where we cannot sleep, and will
1751 * complain if the GPIO chip functions potentially sleep.
1752 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001753void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001754 struct gpio_desc **desc_array, int *value_array)
1755{
1756 if (!desc_array)
1757 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001758 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1759 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001760}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001761EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001762
1763/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001764 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001765 * @array_size: number of elements in the descriptor / value arrays
1766 * @desc_array: array of GPIO descriptors whose values will be assigned
1767 * @value_array: array of values to assign
1768 *
1769 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1770 * into account.
1771 *
1772 * This function should be called from contexts where we cannot sleep, and will
1773 * complain if the GPIO chip functions potentially sleep.
1774 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001775void gpiod_set_array_value(unsigned int array_size,
1776 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001777{
1778 if (!desc_array)
1779 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001780 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1781 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001782}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001783EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001784
1785/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001786 * gpiod_cansleep() - report whether gpio value access may sleep
1787 * @desc: gpio to check
1788 *
1789 */
1790int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001791{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001792 if (!desc)
1793 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001794 return desc->chip->can_sleep;
1795}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001796EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001797
David Brownell0f6d5042008-10-15 22:03:14 -07001798/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001799 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1800 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001801 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001802 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1803 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001804 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001805int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001806{
1807 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001808 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001809
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001810 if (!desc)
1811 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001812 chip = desc->chip;
1813 offset = gpio_chip_hwgpio(desc);
1814 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1815}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001816EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001817
Linus Walleijd468bf92013-09-24 11:54:38 +02001818/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001819 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001820 * @chip: the chip the GPIO to lock belongs to
1821 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001822 *
1823 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001824 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001825 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001826int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001827{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001828 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001829 return -EINVAL;
1830
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001831 if (test_bit(FLAG_IS_OUT, &chip->gpiodev->descs[offset].flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001832 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001833 "%s: tried to flag a GPIO set as output for IRQ\n",
1834 __func__);
1835 return -EIO;
1836 }
1837
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001838 set_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001839 return 0;
1840}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001841EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001842
Linus Walleijd468bf92013-09-24 11:54:38 +02001843/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001844 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001845 * @chip: the chip the GPIO to lock belongs to
1846 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001847 *
1848 * This is used directly by GPIO drivers that want to indicate
1849 * that a certain GPIO is no longer used exclusively for IRQ.
1850 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001851void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001852{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001853 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001854 return;
1855
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001856 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001857}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001858EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001859
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001860/**
1861 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1862 * @desc: gpio whose value will be returned
1863 *
1864 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001865 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001866 *
1867 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001868 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001869int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001870{
David Brownelld2876d02008-02-04 22:28:20 -08001871 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001872 if (!desc)
1873 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001874 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001875}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001876EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001877
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001878/**
1879 * gpiod_get_value_cansleep() - return a gpio's value
1880 * @desc: gpio whose value will be returned
1881 *
1882 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001883 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001884 *
1885 * This function is to be called from contexts that can sleep.
1886 */
1887int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001888{
David Brownelld2876d02008-02-04 22:28:20 -08001889 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001890
1891 might_sleep_if(extra_checks);
1892 if (!desc)
1893 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001894
1895 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001896 if (value < 0)
1897 return value;
1898
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001899 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1900 value = !value;
1901
David Brownelld2876d02008-02-04 22:28:20 -08001902 return value;
1903}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001904EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001905
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001906/**
1907 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1908 * @desc: gpio whose value will be assigned
1909 * @value: value to assign
1910 *
1911 * Set the raw value of the GPIO, i.e. the value of its physical line without
1912 * regard for its ACTIVE_LOW status.
1913 *
1914 * This function is to be called from contexts that can sleep.
1915 */
1916void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001917{
David Brownelld2876d02008-02-04 22:28:20 -08001918 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001919 if (!desc)
1920 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001921 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001922}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001923EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001924
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001925/**
1926 * gpiod_set_value_cansleep() - assign a gpio's value
1927 * @desc: gpio whose value will be assigned
1928 * @value: value to assign
1929 *
1930 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1931 * account
1932 *
1933 * This function is to be called from contexts that can sleep.
1934 */
1935void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001936{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001937 might_sleep_if(extra_checks);
1938 if (!desc)
1939 return;
1940
1941 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1942 value = !value;
1943 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001944}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001945EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001946
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001947/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001948 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001949 * @array_size: number of elements in the descriptor / value arrays
1950 * @desc_array: array of GPIO descriptors whose values will be assigned
1951 * @value_array: array of values to assign
1952 *
1953 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1954 * without regard for their ACTIVE_LOW status.
1955 *
1956 * This function is to be called from contexts that can sleep.
1957 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001958void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1959 struct gpio_desc **desc_array,
1960 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001961{
1962 might_sleep_if(extra_checks);
1963 if (!desc_array)
1964 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001965 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1966 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001967}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001968EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001969
1970/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001971 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001972 * @array_size: number of elements in the descriptor / value arrays
1973 * @desc_array: array of GPIO descriptors whose values will be assigned
1974 * @value_array: array of values to assign
1975 *
1976 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1977 * into account.
1978 *
1979 * This function is to be called from contexts that can sleep.
1980 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001981void gpiod_set_array_value_cansleep(unsigned int array_size,
1982 struct gpio_desc **desc_array,
1983 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001984{
1985 might_sleep_if(extra_checks);
1986 if (!desc_array)
1987 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001988 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1989 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001990}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001991EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001992
1993/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001994 * gpiod_add_lookup_table() - register GPIO device consumers
1995 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001996 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001997void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001998{
1999 mutex_lock(&gpio_lookup_lock);
2000
Alexandre Courbotad824782013-12-03 12:20:11 +09002001 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002002
2003 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002004}
2005
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302006/**
2007 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2008 * @table: table of consumers to unregister
2009 */
2010void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2011{
2012 mutex_lock(&gpio_lookup_lock);
2013
2014 list_del(&table->list);
2015
2016 mutex_unlock(&gpio_lookup_lock);
2017}
2018
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002019static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002020 unsigned int idx,
2021 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002022{
2023 char prop_name[32]; /* 32 is max size of property name */
2024 enum of_gpio_flags of_flags;
2025 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02002026 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002027
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002028 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02002029 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01002030 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002031 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02002032 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01002033 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002034 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002035
Thierry Redingdd34c372014-04-23 17:28:09 +02002036 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2037 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07002038 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02002039 break;
2040 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002041
2042 if (IS_ERR(desc))
2043 return desc;
2044
2045 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002046 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002047
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002048 if (of_flags & OF_GPIO_SINGLE_ENDED) {
2049 if (of_flags & OF_GPIO_ACTIVE_LOW)
2050 *flags |= GPIO_OPEN_DRAIN;
2051 else
2052 *flags |= GPIO_OPEN_SOURCE;
2053 }
2054
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002055 return desc;
2056}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002057
Mika Westerberg81f59e92013-10-10 11:01:09 +03002058static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002059 unsigned int idx,
2060 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03002061{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002062 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03002063 struct acpi_gpio_info info;
2064 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002065 char propname[32];
2066 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03002067
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002068 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002069 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002070 if (con_id && strcmp(con_id, "gpios")) {
2071 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002072 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002073 } else {
2074 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002075 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002076 }
Mika Westerberge01f4402013-10-10 11:01:10 +03002077
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002078 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
2079 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2080 break;
2081 }
2082
2083 /* Then from plain _CRS GPIOs */
2084 if (IS_ERR(desc)) {
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08002085 if (!acpi_can_fallback_to_crs(adev, con_id))
2086 return ERR_PTR(-ENOENT);
2087
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002088 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2089 if (IS_ERR(desc))
2090 return desc;
2091 }
2092
Christophe RICARD52044722015-12-23 23:25:34 +01002093 if (info.polarity == GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002094 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002095
2096 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002097}
2098
Alexandre Courbotad824782013-12-03 12:20:11 +09002099static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2100{
2101 const char *dev_id = dev ? dev_name(dev) : NULL;
2102 struct gpiod_lookup_table *table;
2103
2104 mutex_lock(&gpio_lookup_lock);
2105
2106 list_for_each_entry(table, &gpio_lookup_list, list) {
2107 if (table->dev_id && dev_id) {
2108 /*
2109 * Valid strings on both ends, must be identical to have
2110 * a match
2111 */
2112 if (!strcmp(table->dev_id, dev_id))
2113 goto found;
2114 } else {
2115 /*
2116 * One of the pointers is NULL, so both must be to have
2117 * a match
2118 */
2119 if (dev_id == table->dev_id)
2120 goto found;
2121 }
2122 }
2123 table = NULL;
2124
2125found:
2126 mutex_unlock(&gpio_lookup_lock);
2127 return table;
2128}
2129
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002130static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002131 unsigned int idx,
2132 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002133{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002134 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002135 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002136 struct gpiod_lookup *p;
2137
Alexandre Courbotad824782013-12-03 12:20:11 +09002138 table = gpiod_find_lookup_table(dev);
2139 if (!table)
2140 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002141
Alexandre Courbotad824782013-12-03 12:20:11 +09002142 for (p = &table->table[0]; p->chip_label; p++) {
2143 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002144
Alexandre Courbotad824782013-12-03 12:20:11 +09002145 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002146 if (p->idx != idx)
2147 continue;
2148
Alexandre Courbotad824782013-12-03 12:20:11 +09002149 /* If the lookup entry has a con_id, require exact match */
2150 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2151 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002152
Alexandre Courbotad824782013-12-03 12:20:11 +09002153 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002154
Alexandre Courbotad824782013-12-03 12:20:11 +09002155 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002156 dev_err(dev, "cannot find GPIO chip %s\n",
2157 p->chip_label);
2158 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002159 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002160
Alexandre Courbotad824782013-12-03 12:20:11 +09002161 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002162 dev_err(dev,
2163 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2164 idx, chip->ngpio, chip->label);
2165 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09002166 }
2167
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09002168 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09002169 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002170
2171 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09002172 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002173
2174 return desc;
2175}
2176
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002177static int dt_gpio_count(struct device *dev, const char *con_id)
2178{
2179 int ret;
2180 char propname[32];
2181 unsigned int i;
2182
2183 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2184 if (con_id)
2185 snprintf(propname, sizeof(propname), "%s-%s",
2186 con_id, gpio_suffixes[i]);
2187 else
2188 snprintf(propname, sizeof(propname), "%s",
2189 gpio_suffixes[i]);
2190
2191 ret = of_gpio_named_count(dev->of_node, propname);
2192 if (ret >= 0)
2193 break;
2194 }
2195 return ret;
2196}
2197
2198static int platform_gpio_count(struct device *dev, const char *con_id)
2199{
2200 struct gpiod_lookup_table *table;
2201 struct gpiod_lookup *p;
2202 unsigned int count = 0;
2203
2204 table = gpiod_find_lookup_table(dev);
2205 if (!table)
2206 return -ENOENT;
2207
2208 for (p = &table->table[0]; p->chip_label; p++) {
2209 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2210 (!con_id && !p->con_id))
2211 count++;
2212 }
2213 if (!count)
2214 return -ENOENT;
2215
2216 return count;
2217}
2218
2219/**
2220 * gpiod_count - return the number of GPIOs associated with a device / function
2221 * or -ENOENT if no GPIO has been assigned to the requested function
2222 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2223 * @con_id: function within the GPIO consumer
2224 */
2225int gpiod_count(struct device *dev, const char *con_id)
2226{
2227 int count = -ENOENT;
2228
2229 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2230 count = dt_gpio_count(dev, con_id);
2231 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2232 count = acpi_gpio_count(dev, con_id);
2233
2234 if (count < 0)
2235 count = platform_gpio_count(dev, con_id);
2236
2237 return count;
2238}
2239EXPORT_SYMBOL_GPL(gpiod_count);
2240
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002241/**
Thierry Reding08791622014-04-25 16:54:22 +02002242 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002243 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002244 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002245 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002246 *
2247 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002248 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07002249 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002250 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002251struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002252 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002253{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002254 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002255}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002256EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002257
2258/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002259 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2260 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2261 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002262 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002263 *
2264 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2265 * the requested function it will return NULL. This is convenient for drivers
2266 * that need to handle optional GPIOs.
2267 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002268struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002269 const char *con_id,
2270 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002271{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002272 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002273}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002274EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002275
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002276/**
2277 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2278 * @desc: gpio to be setup
2279 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2280 * of_get_gpio_hog()
2281 *
2282 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2283 */
2284static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2285{
2286 if (lflags & GPIO_ACTIVE_LOW)
2287 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2288 if (lflags & GPIO_OPEN_DRAIN)
2289 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2290 if (lflags & GPIO_OPEN_SOURCE)
2291 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2292}
Benoit Parrotf625d462015-02-02 11:44:44 -06002293
2294/**
2295 * gpiod_configure_flags - helper function to configure a given GPIO
2296 * @desc: gpio whose value will be assigned
2297 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06002298 * @dflags: gpiod_flags - optional GPIO initialization flags
2299 *
2300 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2301 * requested function and/or index, or another IS_ERR() code if an error
2302 * occurred while trying to acquire the GPIO.
2303 */
2304static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002305 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06002306{
2307 int status;
2308
Benoit Parrotf625d462015-02-02 11:44:44 -06002309 /* No particular flag request, return here... */
2310 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2311 pr_debug("no flags found for %s\n", con_id);
2312 return 0;
2313 }
2314
2315 /* Process flags */
2316 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2317 status = gpiod_direction_output(desc,
2318 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2319 else
2320 status = gpiod_direction_input(desc);
2321
2322 return status;
2323}
2324
Thierry Reding29a1f2332014-04-25 17:10:06 +02002325/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002326 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002327 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002328 * @con_id: function within the GPIO consumer
2329 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002330 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002331 *
2332 * This variant of gpiod_get() allows to access GPIOs other than the first
2333 * defined one for functions that define several GPIOs.
2334 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002335 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2336 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07002337 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002338 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002339struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002340 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002341 unsigned int idx,
2342 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002343{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002344 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002345 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002346 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002347
2348 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2349
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002350 if (dev) {
2351 /* Using device tree? */
2352 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2353 dev_dbg(dev, "using device tree for GPIO lookup\n");
2354 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2355 } else if (ACPI_COMPANION(dev)) {
2356 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2357 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2358 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002359 }
2360
2361 /*
2362 * Either we are not using DT or ACPI, or their lookup did not return
2363 * a result. In that case, use platform lookup as a fallback.
2364 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002365 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002366 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002367 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002368 }
2369
2370 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002371 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002372 return desc;
2373 }
2374
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002375 gpiod_parse_flags(desc, lookupflags);
2376
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002377 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002378 if (status < 0)
2379 return ERR_PTR(status);
2380
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002381 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002382 if (status < 0) {
2383 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2384 gpiod_put(desc);
2385 return ERR_PTR(status);
2386 }
2387
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002388 return desc;
2389}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002390EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002391
2392/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002393 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2394 * @fwnode: handle of the firmware node
2395 * @propname: name of the firmware property representing the GPIO
2396 *
2397 * This function can be used for drivers that get their configuration
2398 * from firmware.
2399 *
2400 * Function properly finds the corresponding GPIO using whatever is the
2401 * underlying firmware interface and then makes sure that the GPIO
2402 * descriptor is requested before it is returned to the caller.
2403 *
2404 * In case of error an ERR_PTR() is returned.
2405 */
2406struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2407 const char *propname)
2408{
2409 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2410 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002411 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02002412 int ret;
2413
2414 if (!fwnode)
2415 return ERR_PTR(-EINVAL);
2416
2417 if (is_of_node(fwnode)) {
2418 enum of_gpio_flags flags;
2419
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002420 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002421 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002422 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02002423 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002424 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2425 }
Mika Westerberg40b73182014-10-21 13:33:59 +02002426 } else if (is_acpi_node(fwnode)) {
2427 struct acpi_gpio_info info;
2428
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02002429 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02002430 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01002431 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02002432 }
2433
2434 if (IS_ERR(desc))
2435 return desc;
2436
Mika Westerberg40b73182014-10-21 13:33:59 +02002437 if (active_low)
2438 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2439
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002440 if (single_ended) {
2441 if (active_low)
2442 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2443 else
2444 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2445 }
2446
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002447 ret = gpiod_request(desc, NULL);
2448 if (ret)
2449 return ERR_PTR(ret);
2450
Mika Westerberg40b73182014-10-21 13:33:59 +02002451 return desc;
2452}
2453EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2454
2455/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002456 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2457 * function
2458 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2459 * @con_id: function within the GPIO consumer
2460 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002461 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002462 *
2463 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2464 * specified index was assigned to the requested function it will return NULL.
2465 * This is convenient for drivers that need to handle optional GPIOs.
2466 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002467struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002468 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002469 unsigned int index,
2470 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002471{
2472 struct gpio_desc *desc;
2473
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002474 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002475 if (IS_ERR(desc)) {
2476 if (PTR_ERR(desc) == -ENOENT)
2477 return NULL;
2478 }
2479
2480 return desc;
2481}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002482EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002483
2484/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002485 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2486 * @desc: gpio whose value will be assigned
2487 * @name: gpio line name
2488 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2489 * of_get_gpio_hog()
2490 * @dflags: gpiod_flags - optional GPIO initialization flags
2491 */
2492int gpiod_hog(struct gpio_desc *desc, const char *name,
2493 unsigned long lflags, enum gpiod_flags dflags)
2494{
2495 struct gpio_chip *chip;
2496 struct gpio_desc *local_desc;
2497 int hwnum;
2498 int status;
2499
2500 chip = gpiod_to_chip(desc);
2501 hwnum = gpio_chip_hwgpio(desc);
2502
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002503 gpiod_parse_flags(desc, lflags);
2504
Benoit Parrotf625d462015-02-02 11:44:44 -06002505 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2506 if (IS_ERR(local_desc)) {
Linus Walleija7138902015-06-05 11:36:10 +02002507 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2508 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002509 return PTR_ERR(local_desc);
2510 }
2511
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002512 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002513 if (status < 0) {
Linus Walleija7138902015-06-05 11:36:10 +02002514 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2515 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002516 gpiochip_free_own_desc(desc);
2517 return status;
2518 }
2519
2520 /* Mark GPIO as hogged so it can be identified and removed later */
2521 set_bit(FLAG_IS_HOGGED, &desc->flags);
2522
2523 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2524 desc_to_gpio(desc), name,
2525 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2526 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2527 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2528
2529 return 0;
2530}
2531
2532/**
2533 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2534 * @chip: gpio chip to act on
2535 *
2536 * This is only used by of_gpiochip_remove to free hogged gpios
2537 */
2538static void gpiochip_free_hogs(struct gpio_chip *chip)
2539{
2540 int id;
2541
2542 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002543 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
2544 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06002545 }
2546}
2547
2548/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002549 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2550 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2551 * @con_id: function within the GPIO consumer
2552 * @flags: optional GPIO initialization flags
2553 *
2554 * This function acquires all the GPIOs defined under a given function.
2555 *
2556 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2557 * no GPIO has been assigned to the requested function, or another IS_ERR()
2558 * code if an error occurred while trying to acquire the GPIOs.
2559 */
2560struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2561 const char *con_id,
2562 enum gpiod_flags flags)
2563{
2564 struct gpio_desc *desc;
2565 struct gpio_descs *descs;
2566 int count;
2567
2568 count = gpiod_count(dev, con_id);
2569 if (count < 0)
2570 return ERR_PTR(count);
2571
2572 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2573 GFP_KERNEL);
2574 if (!descs)
2575 return ERR_PTR(-ENOMEM);
2576
2577 for (descs->ndescs = 0; descs->ndescs < count; ) {
2578 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2579 if (IS_ERR(desc)) {
2580 gpiod_put_array(descs);
2581 return ERR_CAST(desc);
2582 }
2583 descs->desc[descs->ndescs] = desc;
2584 descs->ndescs++;
2585 }
2586 return descs;
2587}
2588EXPORT_SYMBOL_GPL(gpiod_get_array);
2589
2590/**
2591 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2592 * function
2593 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2594 * @con_id: function within the GPIO consumer
2595 * @flags: optional GPIO initialization flags
2596 *
2597 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2598 * assigned to the requested function it will return NULL.
2599 */
2600struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2601 const char *con_id,
2602 enum gpiod_flags flags)
2603{
2604 struct gpio_descs *descs;
2605
2606 descs = gpiod_get_array(dev, con_id, flags);
2607 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2608 return NULL;
2609
2610 return descs;
2611}
2612EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2613
2614/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002615 * gpiod_put - dispose of a GPIO descriptor
2616 * @desc: GPIO descriptor to dispose of
2617 *
2618 * No descriptor can be used after gpiod_put() has been called on it.
2619 */
2620void gpiod_put(struct gpio_desc *desc)
2621{
2622 gpiod_free(desc);
2623}
2624EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002625
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002626/**
2627 * gpiod_put_array - dispose of multiple GPIO descriptors
2628 * @descs: struct gpio_descs containing an array of descriptors
2629 */
2630void gpiod_put_array(struct gpio_descs *descs)
2631{
2632 unsigned int i;
2633
2634 for (i = 0; i < descs->ndescs; i++)
2635 gpiod_put(descs->desc[i]);
2636
2637 kfree(descs);
2638}
2639EXPORT_SYMBOL_GPL(gpiod_put_array);
2640
Linus Walleij3c702e92015-10-21 15:29:53 +02002641static int __init gpiolib_dev_init(void)
2642{
2643 int ret;
2644
2645 /* Register GPIO sysfs bus */
2646 ret = bus_register(&gpio_bus_type);
2647 if (ret < 0) {
2648 pr_err("gpiolib: could not register GPIO bus type\n");
2649 return ret;
2650 }
2651
2652 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
2653 if (ret < 0) {
2654 pr_err("gpiolib: failed to allocate char dev region\n");
2655 bus_unregister(&gpio_bus_type);
2656 }
2657 return ret;
2658}
2659core_initcall(gpiolib_dev_init);
2660
David Brownelld2876d02008-02-04 22:28:20 -08002661#ifdef CONFIG_DEBUG_FS
2662
2663static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2664{
2665 unsigned i;
2666 unsigned gpio = chip->base;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002667 struct gpio_desc *gdesc = &chip->gpiodev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08002668 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002669 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002670
2671 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02002672 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2673 if (gdesc->name) {
2674 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2675 gpio, gdesc->name);
2676 }
David Brownelld2876d02008-02-04 22:28:20 -08002677 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02002678 }
David Brownelld2876d02008-02-04 22:28:20 -08002679
Alexandre Courbot372e7222013-02-03 01:29:29 +09002680 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002681 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002682 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02002683 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2684 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08002685 is_out ? "out" : "in ",
2686 chip->get
2687 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002688 : "? ",
2689 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002690 seq_printf(s, "\n");
2691 }
2692}
2693
Thierry Redingf9c4a312012-04-12 13:26:01 +02002694static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002695{
Grant Likely362432a2013-02-09 09:41:49 +00002696 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002697 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002698 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002699
2700 s->private = "";
2701
Grant Likely362432a2013-02-09 09:41:49 +00002702 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002703 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00002704 if (index-- == 0) {
2705 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002706 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00002707 }
2708 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002709
2710 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002711}
2712
2713static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2714{
Grant Likely362432a2013-02-09 09:41:49 +00002715 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002716 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002717 void *ret = NULL;
2718
Grant Likely362432a2013-02-09 09:41:49 +00002719 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002720 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002721 ret = NULL;
2722 else
Linus Walleijff2b1352015-10-20 11:10:38 +02002723 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00002724 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002725
2726 s->private = "\n";
2727 ++*pos;
2728
2729 return ret;
2730}
2731
2732static void gpiolib_seq_stop(struct seq_file *s, void *v)
2733{
2734}
2735
2736static int gpiolib_seq_show(struct seq_file *s, void *v)
2737{
Linus Walleijff2b1352015-10-20 11:10:38 +02002738 struct gpio_device *gdev = v;
2739 struct gpio_chip *chip = gdev->chip;
2740 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002741
Linus Walleijff2b1352015-10-20 11:10:38 +02002742 if (!chip) {
2743 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
2744 dev_name(&gdev->dev));
2745 return 0;
2746 }
2747
2748 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
2749 dev_name(&gdev->dev),
2750 chip->base, chip->base + chip->ngpio - 1);
2751 parent = chip->parent;
2752 if (parent)
2753 seq_printf(s, ", parent: %s/%s",
2754 parent->bus ? parent->bus->name : "no-bus",
2755 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02002756 if (chip->label)
2757 seq_printf(s, ", %s", chip->label);
2758 if (chip->can_sleep)
2759 seq_printf(s, ", can sleep");
2760 seq_printf(s, ":\n");
2761
2762 if (chip->dbg_show)
2763 chip->dbg_show(s, chip);
2764 else
2765 gpiolib_dbg_show(s, chip);
2766
David Brownelld2876d02008-02-04 22:28:20 -08002767 return 0;
2768}
2769
Thierry Redingf9c4a312012-04-12 13:26:01 +02002770static const struct seq_operations gpiolib_seq_ops = {
2771 .start = gpiolib_seq_start,
2772 .next = gpiolib_seq_next,
2773 .stop = gpiolib_seq_stop,
2774 .show = gpiolib_seq_show,
2775};
2776
David Brownelld2876d02008-02-04 22:28:20 -08002777static int gpiolib_open(struct inode *inode, struct file *file)
2778{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002779 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002780}
2781
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002782static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002783 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002784 .open = gpiolib_open,
2785 .read = seq_read,
2786 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002787 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002788};
2789
2790static int __init gpiolib_debugfs_init(void)
2791{
2792 /* /sys/kernel/debug/gpio */
2793 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2794 NULL, NULL, &gpiolib_operations);
2795 return 0;
2796}
2797subsys_initcall(gpiolib_debugfs_init);
2798
2799#endif /* DEBUG_FS */