blob: 5763290f777c0eda303e4be0389781f61f2f35a4 [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d2008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020018#include <linux/pinctrl/consumer.h>
Linus Walleijff2b1352015-10-20 11:10:38 +020019#include <linux/idr.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020020#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
23#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080024
Mika Westerberg664e3e52014-01-08 12:40:54 +020025#include "gpiolib.h"
26
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060027#define CREATE_TRACE_POINTS
28#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080029
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070030/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080031 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070032 * The GPIO programming interface allows for inlining speed-critical
33 * get/set operations for common cases, so that access to SOC-integrated
34 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080035 */
36
37
38/* When debugging, extend minimal trust to callers and platform code.
39 * Also emit diagnostic messages that may help initial bringup, when
40 * board setup or driver bugs are most common.
41 *
42 * Otherwise, minimize overhead in what may be bitbanging codepaths.
43 */
44#ifdef DEBUG
45#define extra_checks 1
46#else
47#define extra_checks 0
48#endif
49
Linus Walleijff2b1352015-10-20 11:10:38 +020050/* Device and char device-related information */
51static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020052static dev_t gpio_devt;
53#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
54static struct bus_type gpio_bus_type = {
55 .name = "gpio",
56};
Linus Walleijff2b1352015-10-20 11:10:38 +020057
David Brownelld2876d02008-02-04 22:28:20 -080058/* gpio_lock prevents conflicts during gpio_desc[] table updates.
59 * While any GPIO is requested, its gpio_chip is not removable;
60 * each GPIO's "requested" flag serves as a lock and refcount.
61 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090062DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080063
Alexandre Courbotbae48da2013-10-17 10:21:38 -070064static DEFINE_MUTEX(gpio_lookup_lock);
65static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020066LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020067
68static void gpiochip_free_hogs(struct gpio_chip *chip);
69static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
70
71
David Brownelld2876d02008-02-04 22:28:20 -080072static inline void desc_set_label(struct gpio_desc *d, const char *label)
73{
David Brownelld2876d02008-02-04 22:28:20 -080074 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080075}
76
Alexandre Courbot372e7222013-02-03 01:29:29 +090077/**
78 * Convert a GPIO number to its descriptor
79 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070080struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090081{
Linus Walleijff2b1352015-10-20 11:10:38 +020082 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +090083 unsigned long flags;
84
85 spin_lock_irqsave(&gpio_lock, flags);
86
Linus Walleijff2b1352015-10-20 11:10:38 +020087 list_for_each_entry(gdev, &gpio_devices, list) {
88 if (gdev->chip->base <= gpio &&
89 gdev->chip->base + gdev->chip->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +090090 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +020091 return &gdev->chip->desc[gpio - gdev->chip->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +090092 }
93 }
94
95 spin_unlock_irqrestore(&gpio_lock, flags);
96
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +090097 if (!gpio_is_valid(gpio))
98 WARN(1, "invalid GPIO %d\n", gpio);
99
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900100 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900101}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700102EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900103
104/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900105 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +0200106 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900107struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
108 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200109{
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900110 if (hwnum >= chip->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900111 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200112
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900113 return &chip->desc[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200114}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900115
116/**
117 * Convert a GPIO descriptor to the integer namespace.
118 * This should disappear in the future but is needed since we still
119 * use GPIO numbers for error messages and sysfs nodes
120 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700121int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900122{
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900123 return desc->chip->base + (desc - &desc->chip->desc[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900124}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700125EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900126
127
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700128/**
129 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
130 * @desc: descriptor to return the chip of
131 */
132struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900133{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900134 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900135}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700136EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800137
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700138/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
139static int gpiochip_find_base(int ngpio)
140{
Linus Walleijff2b1352015-10-20 11:10:38 +0200141 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900142 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700143
Linus Walleijff2b1352015-10-20 11:10:38 +0200144 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900145 /* found a free space? */
Linus Walleijff2b1352015-10-20 11:10:38 +0200146 if (gdev->chip->base + gdev->chip->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900147 break;
148 else
149 /* nope, check the space right before the chip */
Linus Walleijff2b1352015-10-20 11:10:38 +0200150 base = gdev->chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700151 }
152
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900153 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700154 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900155 return base;
156 } else {
157 pr_err("%s: cannot find free range\n", __func__);
158 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700159 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700160}
161
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700162/**
163 * gpiod_get_direction - return the current direction of a GPIO
164 * @desc: GPIO to get the direction of
165 *
166 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
167 *
168 * This function may sleep if gpiod_cansleep() is true.
169 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900170int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300171{
172 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900173 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300174 int status = -EINVAL;
175
Alexandre Courbot372e7222013-02-03 01:29:29 +0900176 chip = gpiod_to_chip(desc);
177 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300178
179 if (!chip->get_direction)
180 return status;
181
Alexandre Courbot372e7222013-02-03 01:29:29 +0900182 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300183 if (status > 0) {
184 /* GPIOF_DIR_IN, or other positive */
185 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900186 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300187 }
188 if (status == 0) {
189 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900190 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300191 }
192 return status;
193}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700194EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300195
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900196/*
197 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800198 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900199 *
200 * Return -EBUSY if the new chip overlaps with some other chip's integer
201 * space.
202 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200203static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900204{
Linus Walleijff2b1352015-10-20 11:10:38 +0200205 struct gpio_device *iterator;
206 struct gpio_device *previous = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900207
Linus Walleijff2b1352015-10-20 11:10:38 +0200208 if (!gdev->chip)
209 return -EINVAL;
210
211 if (list_empty(&gpio_devices)) {
212 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530213 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900214 }
215
Linus Walleijff2b1352015-10-20 11:10:38 +0200216 list_for_each_entry(iterator, &gpio_devices, list) {
217 /*
218 * The list may contain dangling GPIO devices with no
219 * live chip assigned.
220 */
221 if (!iterator->chip)
222 continue;
223 if (iterator->chip->base >=
224 gdev->chip->base + gdev->chip->ngpio) {
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800225 /*
226 * Iterator is the first GPIO chip so there is no
227 * previous one
228 */
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530229 if (!previous) {
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800230 goto found;
231 } else {
232 /*
233 * We found a valid range(means
234 * [base, base + ngpio - 1]) between previous
235 * and iterator chip.
236 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200237 if (previous->chip->base + previous->chip->ngpio
238 <= gdev->chip->base)
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800239 goto found;
240 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900241 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800242 previous = iterator;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900243 }
244
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530245 /*
246 * We are beyond the last chip in the list and iterator now
247 * points to the head.
248 * Let iterator point to the last chip in the list.
249 */
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900250
Linus Walleijff2b1352015-10-20 11:10:38 +0200251 iterator = list_last_entry(&gpio_devices, struct gpio_device, list);
252 if (iterator->chip->base + iterator->chip->ngpio <= gdev->chip->base) {
253 list_add(&gdev->list, &iterator->list);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500254 return 0;
255 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900256
Linus Walleijff2b1352015-10-20 11:10:38 +0200257 dev_err(&gdev->dev,
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800258 "GPIO integer space overlap, cannot add chip\n");
259 return -EBUSY;
260
261found:
Linus Walleijff2b1352015-10-20 11:10:38 +0200262 list_add_tail(&gdev->list, &iterator->list);
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800263 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900264}
265
Linus Walleijf881bab2015-09-23 16:20:43 -0700266/**
267 * Convert a GPIO name to its descriptor
268 */
269static struct gpio_desc *gpio_name_to_desc(const char * const name)
270{
Linus Walleijff2b1352015-10-20 11:10:38 +0200271 struct gpio_device *gdev;
Linus Walleijf881bab2015-09-23 16:20:43 -0700272 unsigned long flags;
273
274 spin_lock_irqsave(&gpio_lock, flags);
275
Linus Walleijff2b1352015-10-20 11:10:38 +0200276 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700277 int i;
278
Linus Walleijff2b1352015-10-20 11:10:38 +0200279 for (i = 0; i != gdev->chip->ngpio; ++i) {
280 struct gpio_desc *gpio = &gdev->chip->desc[i];
Linus Walleijf881bab2015-09-23 16:20:43 -0700281
Vladimir Zapolskiyd06165b2015-11-11 14:36:53 +0200282 if (!gpio->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700283 continue;
284
285 if (!strcmp(gpio->name, name)) {
286 spin_unlock_irqrestore(&gpio_lock, flags);
287 return gpio;
288 }
289 }
290 }
291
292 spin_unlock_irqrestore(&gpio_lock, flags);
293
294 return NULL;
295}
296
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200297/*
298 * Takes the names from gc->names and checks if they are all unique. If they
299 * are, they are assigned to their gpio descriptors.
300 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800301 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200302 */
303static int gpiochip_set_desc_names(struct gpio_chip *gc)
304{
305 int i;
306
307 if (!gc->names)
308 return 0;
309
310 /* First check all names if they are unique */
311 for (i = 0; i != gc->ngpio; ++i) {
312 struct gpio_desc *gpio;
313
314 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700315 if (gpio)
Linus Walleij34ffd852015-10-20 11:31:54 +0200316 dev_warn(&gc->gpiodev->dev,
317 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab2015-09-23 16:20:43 -0700318 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200319 }
320
321 /* Then add all names to the GPIO descriptors */
322 for (i = 0; i != gc->ngpio; ++i)
323 gc->desc[i].name = gc->names[i];
324
325 return 0;
326}
327
Linus Walleij3c702e92015-10-21 15:29:53 +0200328/**
329 * gpio_ioctl() - ioctl handler for the GPIO chardev
330 */
331static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
332{
333 struct gpio_device *gdev = filp->private_data;
334 struct gpio_chip *chip = gdev->chip;
335 int __user *ip = (int __user *)arg;
336 struct gpiochip_info chipinfo;
337
338 /* We fail any subsequent ioctl():s when the chip is gone */
339 if (!chip)
340 return -ENODEV;
341
342 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
343 /* Fill in the struct and pass to userspace */
344 strncpy(chipinfo.name, dev_name(&gdev->dev),
345 sizeof(chipinfo.name));
346 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
347 chipinfo.lines = chip->ngpio;
348 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
349 return -EFAULT;
350 return 0;
351 }
352 return -EINVAL;
353}
354
355/**
356 * gpio_chrdev_open() - open the chardev for ioctl operations
357 * @inode: inode for this chardev
358 * @filp: file struct for storing private data
359 * Returns 0 on success
360 */
361static int gpio_chrdev_open(struct inode *inode, struct file *filp)
362{
363 struct gpio_device *gdev = container_of(inode->i_cdev,
364 struct gpio_device, chrdev);
365
366 /* Fail on open if the backing gpiochip is gone */
367 if (!gdev || !gdev->chip)
368 return -ENODEV;
369 get_device(&gdev->dev);
370 filp->private_data = gdev;
371 return 0;
372}
373
374/**
375 * gpio_chrdev_release() - close chardev after ioctl operations
376 * @inode: inode for this chardev
377 * @filp: file struct for storing private data
378 * Returns 0 on success
379 */
380static int gpio_chrdev_release(struct inode *inode, struct file *filp)
381{
382 struct gpio_device *gdev = container_of(inode->i_cdev,
383 struct gpio_device, chrdev);
384
385 if (!gdev)
386 return -ENODEV;
387 put_device(&gdev->dev);
388 return 0;
389}
390
391
392static const struct file_operations gpio_fileops = {
393 .release = gpio_chrdev_release,
394 .open = gpio_chrdev_open,
395 .owner = THIS_MODULE,
396 .llseek = noop_llseek,
397 .unlocked_ioctl = gpio_ioctl,
398 .compat_ioctl = gpio_ioctl,
399};
400
Linus Walleijff2b1352015-10-20 11:10:38 +0200401static void gpiodevice_release(struct device *dev)
402{
403 struct gpio_device *gdev = dev_get_drvdata(dev);
404
Linus Walleij3c702e92015-10-21 15:29:53 +0200405 cdev_del(&gdev->chrdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200406 list_del(&gdev->list);
407 ida_simple_remove(&gpio_ida, gdev->id);
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;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900434 struct gpio_desc *descs;
Linus Walleijff2b1352015-10-20 11:10:38 +0200435 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -0800436
Linus Walleijff2b1352015-10-20 11:10:38 +0200437 /*
438 * First: allocate and populate the internal stat container, and
439 * set up the struct device.
440 */
441 gdev = kmalloc(sizeof(*gdev), GFP_KERNEL);
442 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900443 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +0200444 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +0200445 gdev->chip = chip;
446 chip->gpiodev = gdev;
447 if (chip->parent) {
448 gdev->dev.parent = chip->parent;
449 gdev->dev.of_node = chip->parent->of_node;
450 } else {
451#ifdef CONFIG_OF_GPIO
452 /* If the gpiochip has an assigned OF node this takes precedence */
453 if (chip->of_node)
454 gdev->dev.of_node = chip->of_node;
455#endif
456 }
457 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
458 if (gdev->id < 0) {
459 status = gdev->id;
460 goto err_free_gdev;
461 }
462 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
463 device_initialize(&gdev->dev);
464 dev_set_drvdata(&gdev->dev, gdev);
465 if (chip->parent && chip->parent->driver)
466 gdev->owner = chip->parent->driver->owner;
467 else if (chip->owner)
468 /* TODO: remove chip->owner */
469 gdev->owner = chip->owner;
470 else
471 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -0800472
Linus Walleijff2b1352015-10-20 11:10:38 +0200473 /* FIXME: devm_kcalloc() these and move to gpio_device */
474 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
475 if (!descs) {
476 status = -ENOMEM;
477 goto err_free_gdev;
478 }
479
480 /* FIXME: move driver data into gpio_device dev_set_drvdata() */
Linus Walleijb08ea352015-12-03 15:14:13 +0100481 chip->data = data;
482
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800483 if (chip->ngpio == 0) {
484 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +0200485 status = -EINVAL;
486 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800487 }
488
David Brownelld2876d02008-02-04 22:28:20 -0800489 spin_lock_irqsave(&gpio_lock, flags);
490
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700491 if (base < 0) {
492 base = gpiochip_find_base(chip->ngpio);
493 if (base < 0) {
494 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100495 spin_unlock_irqrestore(&gpio_lock, flags);
496 goto err_free_descs;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700497 }
498 chip->base = base;
499 }
500
Linus Walleijff2b1352015-10-20 11:10:38 +0200501 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +0100502 if (status) {
503 spin_unlock_irqrestore(&gpio_lock, flags);
504 goto err_free_descs;
505 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900506
Linus Walleijff2b1352015-10-20 11:10:38 +0200507 for (i = 0; i < chip->ngpio; i++) {
508 struct gpio_desc *desc = &descs[i];
David Brownelld8f388d2008-07-25 01:46:07 -0700509
Linus Walleijff2b1352015-10-20 11:10:38 +0200510 /* REVISIT: maybe a pointer to gpio_device is better */
Johan Hovold05aa5202015-01-12 17:12:26 +0100511 desc->chip = chip;
512
513 /* REVISIT: most hardware initializes GPIOs as inputs (often
514 * with pullups enabled) so power usage is minimized. Linux
515 * code should set the gpio direction first thing; but until
516 * it does, and in case chip->get_direction is not set, we may
517 * expose the wrong direction in sysfs.
518 */
519 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800520 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900521 chip->desc = descs;
522
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800523 spin_unlock_irqrestore(&gpio_lock, flags);
524
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530525#ifdef CONFIG_PINCTRL
Linus Walleijff2b1352015-10-20 11:10:38 +0200526 /* FIXME: move pin ranges to gpio_device */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530527 INIT_LIST_HEAD(&chip->pin_ranges);
528#endif
529
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200530 status = gpiochip_set_desc_names(chip);
531 if (status)
532 goto err_remove_from_list;
533
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200534 status = of_gpiochip_add(chip);
535 if (status)
536 goto err_remove_chip;
537
Mika Westerberg664e3e52014-01-08 12:40:54 +0200538 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600539
Linus Walleij3c702e92015-10-21 15:29:53 +0200540 /*
541 * By first adding the chardev, and then adding the device,
542 * we get a device node entry in sysfs under
543 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
544 * coldplug of device nodes and other udev business.
545 */
546 cdev_init(&gdev->chrdev, &gpio_fileops);
547 gdev->chrdev.owner = THIS_MODULE;
548 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
549 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
550 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
551 if (status < 0)
552 chip_warn(chip, "failed to add char device %d:%d\n",
553 MAJOR(gpio_devt), gdev->id);
554 else
555 chip_dbg(chip, "added GPIO chardev (%d:%d)\n",
556 MAJOR(gpio_devt), gdev->id);
Linus Walleijff2b1352015-10-20 11:10:38 +0200557 status = device_add(&gdev->dev);
Johan Hovold225fce82015-01-12 17:12:25 +0100558 if (status)
Linus Walleij3c702e92015-10-21 15:29:53 +0200559 goto err_remove_chardev;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600560
Linus Walleijafbc4f32016-02-09 13:21:06 +0100561 status = gpiochip_sysfs_register(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200562 if (status)
563 goto err_remove_device;
564
565 /* From this point, the .release() function cleans up gpio_device */
566 gdev->dev.release = gpiodevice_release;
567 get_device(&gdev->dev);
Andy Shevchenko7589e592013-12-05 11:26:23 +0200568 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700569 chip->base, chip->base + chip->ngpio - 1,
570 chip->label ? : "generic");
571
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600572 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800573
Linus Walleijff2b1352015-10-20 11:10:38 +0200574err_remove_device:
575 device_del(&gdev->dev);
Linus Walleij3c702e92015-10-21 15:29:53 +0200576err_remove_chardev:
577 cdev_del(&gdev->chrdev);
Johan Hovold225fce82015-01-12 17:12:25 +0100578err_remove_chip:
579 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200580 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100581 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200582err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100583 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200584 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800585 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold05aa5202015-01-12 17:12:26 +0100586 chip->desc = NULL;
Johan Hovold225fce82015-01-12 17:12:25 +0100587err_free_descs:
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900588 kfree(descs);
Linus Walleijff2b1352015-10-20 11:10:38 +0200589err_free_gdev:
590 ida_simple_remove(&gpio_ida, gdev->id);
591 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -0800592 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200593 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600594 chip->base, chip->base + chip->ngpio - 1,
595 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800596 return status;
597}
Linus Walleijb08ea352015-12-03 15:14:13 +0100598EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -0800599
600/**
601 * gpiochip_remove() - unregister a gpio_chip
602 * @chip: the chip to unregister
603 *
604 * A gpio_chip with any GPIOs still requested may not be removed.
605 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200606void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800607{
Linus Walleijff2b1352015-10-20 11:10:38 +0200608 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200609 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800610 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800611 unsigned id;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200612 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800613
Linus Walleijff2b1352015-10-20 11:10:38 +0200614 /* Numb the device, cancelling all outstanding operations */
615 gdev->chip = NULL;
616
617 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +0100618 gpiochip_sysfs_unregister(gdev);
Johan Hovold00acc3d2015-01-12 17:12:27 +0100619 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200620 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100621 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600622 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600623 of_gpiochip_remove(chip);
624
Johan Hovold6798aca2015-01-12 17:12:28 +0100625 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900626 for (id = 0; id < chip->ngpio; id++) {
Johan Hovoldfab28b82015-05-04 17:10:27 +0200627 desc = &chip->desc[id];
628 desc->chip = NULL;
629 if (test_bit(FLAG_REQUESTED, &desc->flags))
630 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800631 }
David Brownelld2876d02008-02-04 22:28:20 -0800632 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900633
Johan Hovoldfab28b82015-05-04 17:10:27 +0200634 if (requested)
Linus Walleij34ffd852015-10-20 11:31:54 +0200635 dev_crit(&chip->gpiodev->dev,
Linus Walleij58383c72015-11-04 09:56:26 +0100636 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +0200637
Linus Walleijff2b1352015-10-20 11:10:38 +0200638 /* FIXME: need to be moved to gpio_device and held there */
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900639 kfree(chip->desc);
640 chip->desc = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +0200641
642 /*
643 * The gpiochip side puts its use of the device to rest here:
644 * if there are no userspace clients, the chardev and device will
645 * be removed, else it will be dangling until the last user is
646 * gone.
647 */
648 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -0800649}
650EXPORT_SYMBOL_GPL(gpiochip_remove);
651
Grant Likely594fa262010-06-08 07:48:16 -0600652/**
653 * gpiochip_find() - iterator for locating a specific gpio_chip
654 * @data: data to pass to match function
655 * @callback: Callback function to check gpio_chip
656 *
657 * Similar to bus_find_device. It returns a reference to a gpio_chip as
658 * determined by a user supplied @match callback. The callback should return
659 * 0 if the device doesn't match and non-zero if it does. If the callback is
660 * non-zero, this function will return to the caller and not iterate over any
661 * more gpio_chips.
662 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600663struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700664 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600665 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600666{
Linus Walleijff2b1352015-10-20 11:10:38 +0200667 struct gpio_device *gdev;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900668 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600669 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600670
671 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200672 list_for_each_entry(gdev, &gpio_devices, list)
673 if (match(gdev->chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600674 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900675
676 /* No match? */
Linus Walleijff2b1352015-10-20 11:10:38 +0200677 if (&gdev->list == &gpio_devices)
Alexandre Courbot125eef92013-02-03 01:29:26 +0900678 chip = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +0200679 else
680 chip = gdev->chip;
681
Grant Likely594fa262010-06-08 07:48:16 -0600682 spin_unlock_irqrestore(&gpio_lock, flags);
683
684 return chip;
685}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600686EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800687
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900688static int gpiochip_match_name(struct gpio_chip *chip, void *data)
689{
690 const char *name = data;
691
692 return !strcmp(chip->label, name);
693}
694
695static struct gpio_chip *find_chip_by_name(const char *name)
696{
697 return gpiochip_find((void *)name, gpiochip_match_name);
698}
699
Linus Walleij14250522014-03-25 10:40:18 +0100700#ifdef CONFIG_GPIOLIB_IRQCHIP
701
702/*
703 * The following is irqchip helper code for gpiochips.
704 */
705
706/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200707 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
708 * @gpiochip: the gpiochip to set the irqchip chain to
709 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100710 * @parent_irq: the irq number corresponding to the parent IRQ for this
711 * chained irqchip
712 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200713 * coming out of the gpiochip. If the interrupt is nested rather than
714 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100715 */
716void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
717 struct irq_chip *irqchip,
718 int parent_irq,
719 irq_flow_handler_t parent_handler)
720{
Linus Walleij83141a72014-09-26 13:50:12 +0200721 unsigned int offset;
722
Linus Walleij83141a72014-09-26 13:50:12 +0200723 if (!gpiochip->irqdomain) {
724 chip_err(gpiochip, "called %s before setting up irqchip\n",
725 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200726 return;
727 }
728
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200729 if (parent_handler) {
730 if (gpiochip->can_sleep) {
731 chip_err(gpiochip,
732 "you cannot have chained interrupts on a "
733 "chip that may sleep\n");
734 return;
735 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200736 /*
737 * The parent irqchip is already using the chip_data for this
738 * irqchip, so our callbacks simply use the handler_data.
739 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +0200740 irq_set_chained_handler_and_data(parent_irq, parent_handler,
741 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300742
743 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200744 }
Linus Walleij83141a72014-09-26 13:50:12 +0200745
746 /* Set the parent IRQ for all affected IRQs */
747 for (offset = 0; offset < gpiochip->ngpio; offset++)
748 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
749 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100750}
751EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
752
753/**
754 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
755 * @d: the irqdomain used by this irqchip
756 * @irq: the global irq number used by this GPIO irqchip irq
757 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
758 *
759 * This function will set up the mapping for a certain IRQ line on a
760 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
761 * stored inside the gpiochip.
762 */
763static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
764 irq_hw_number_t hwirq)
765{
766 struct gpio_chip *chip = d->host_data;
767
Linus Walleij14250522014-03-25 10:40:18 +0100768 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300769 /*
770 * This lock class tells lockdep that GPIO irqs are in a different
771 * category than their parents, so it won't report false recursion.
772 */
773 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +0200774 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200775 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300776 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200777 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100778 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -0500779
Linus Walleij1333b902014-04-23 16:45:12 +0200780 /*
781 * No set-up of the hardware will happen if IRQ_TYPE_NONE
782 * is passed as default type.
783 */
784 if (chip->irq_default_type != IRQ_TYPE_NONE)
785 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100786
787 return 0;
788}
789
Linus Walleijc3626fd2014-03-28 20:42:01 +0100790static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
791{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200792 struct gpio_chip *chip = d->host_data;
793
Linus Walleij1c8732b2014-04-09 13:34:39 +0200794 if (chip->can_sleep)
795 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100796 irq_set_chip_and_handler(irq, NULL, NULL);
797 irq_set_chip_data(irq, NULL);
798}
799
Linus Walleij14250522014-03-25 10:40:18 +0100800static const struct irq_domain_ops gpiochip_domain_ops = {
801 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100802 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100803 /* Virtually all GPIO irqchips are twocell:ed */
804 .xlate = irq_domain_xlate_twocell,
805};
806
807static int gpiochip_irq_reqres(struct irq_data *d)
808{
809 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
810
Linus Walleijff2b1352015-10-20 11:10:38 +0200811 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300812 return -ENODEV;
813
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900814 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100815 chip_err(chip,
816 "unable to lock HW IRQ %lu for IRQ\n",
817 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200818 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100819 return -EINVAL;
820 }
821 return 0;
822}
823
824static void gpiochip_irq_relres(struct irq_data *d)
825{
826 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
827
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900828 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200829 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100830}
831
832static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
833{
834 return irq_find_mapping(chip->irqdomain, offset);
835}
836
837/**
838 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
839 * @gpiochip: the gpiochip to remove the irqchip from
840 *
841 * This is called only from gpiochip_remove()
842 */
843static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
844{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100845 unsigned int offset;
846
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300847 acpi_gpiochip_free_interrupts(gpiochip);
848
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300849 if (gpiochip->irq_parent) {
850 irq_set_chained_handler(gpiochip->irq_parent, NULL);
851 irq_set_handler_data(gpiochip->irq_parent, NULL);
852 }
853
Linus Walleijc3626fd2014-03-28 20:42:01 +0100854 /* Remove all IRQ mappings and delete the domain */
855 if (gpiochip->irqdomain) {
856 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300857 irq_dispose_mapping(
858 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100859 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100860 }
Linus Walleij14250522014-03-25 10:40:18 +0100861
862 if (gpiochip->irqchip) {
863 gpiochip->irqchip->irq_request_resources = NULL;
864 gpiochip->irqchip->irq_release_resources = NULL;
865 gpiochip->irqchip = NULL;
866 }
867}
868
869/**
870 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
871 * @gpiochip: the gpiochip to add the irqchip to
872 * @irqchip: the irqchip to add to the gpiochip
873 * @first_irq: if not dynamically assigned, the base (first) IRQ to
874 * allocate gpiochip irqs from
875 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200876 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
877 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300878 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +0100879 *
880 * This function closely associates a certain irqchip with a certain
881 * gpiochip, providing an irq domain to translate the local IRQs to
882 * global irqs in the gpiolib core, and making sure that the gpiochip
883 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +0100884 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +0100885 * from the gpiochip passed as chip data. An irqdomain will be stored
886 * in the gpiochip that shall be used by the driver to handle IRQ number
887 * translation. The gpiochip will need to be initialized and registered
888 * before calling this function.
889 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100890 * This function will handle two cell:ed simple IRQs and assumes all
891 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100892 * need to be open coded.
893 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300894int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
895 struct irq_chip *irqchip,
896 unsigned int first_irq,
897 irq_flow_handler_t handler,
898 unsigned int type,
899 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +0100900{
901 struct device_node *of_node;
902 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100903 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100904
905 if (!gpiochip || !irqchip)
906 return -EINVAL;
907
Linus Walleij58383c72015-11-04 09:56:26 +0100908 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +0100909 pr_err("missing gpiochip .dev parent pointer\n");
910 return -EINVAL;
911 }
Linus Walleij58383c72015-11-04 09:56:26 +0100912 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +0100913#ifdef CONFIG_OF_GPIO
914 /*
Colin Cronin20a8a962015-05-18 11:41:43 -0700915 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800916 * FIXME: get rid of this and use gpiochip->parent->of_node
917 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +0100918 */
919 if (gpiochip->of_node)
920 of_node = gpiochip->of_node;
921#endif
922 gpiochip->irqchip = irqchip;
923 gpiochip->irq_handler = handler;
924 gpiochip->irq_default_type = type;
925 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300926 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +0100927 gpiochip->irqdomain = irq_domain_add_simple(of_node,
928 gpiochip->ngpio, first_irq,
929 &gpiochip_domain_ops, gpiochip);
930 if (!gpiochip->irqdomain) {
931 gpiochip->irqchip = NULL;
932 return -EINVAL;
933 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +0200934
935 /*
936 * It is possible for a driver to override this, but only if the
937 * alternative functions are both implemented.
938 */
939 if (!irqchip->irq_request_resources &&
940 !irqchip->irq_release_resources) {
941 irqchip->irq_request_resources = gpiochip_irq_reqres;
942 irqchip->irq_release_resources = gpiochip_irq_relres;
943 }
Linus Walleij14250522014-03-25 10:40:18 +0100944
945 /*
946 * Prepare the mapping since the irqchip shall be orthogonal to
947 * any gpiochip calls. If the first_irq was zero, this is
948 * necessary to allocate descriptors for all IRQs.
949 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100950 for (offset = 0; offset < gpiochip->ngpio; offset++) {
951 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
952 if (offset == 0)
953 /*
954 * Store the base into the gpiochip to be used when
955 * unmapping the irqs.
956 */
957 gpiochip->irq_base = irq_base;
958 }
Linus Walleij14250522014-03-25 10:40:18 +0100959
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300960 acpi_gpiochip_request_interrupts(gpiochip);
961
Linus Walleij14250522014-03-25 10:40:18 +0100962 return 0;
963}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300964EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +0100965
966#else /* CONFIG_GPIOLIB_IRQCHIP */
967
968static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
969
970#endif /* CONFIG_GPIOLIB_IRQCHIP */
971
Jonas Gorskic771c2f2015-10-11 17:34:15 +0200972/**
973 * gpiochip_generic_request() - request the gpio function for a pin
974 * @chip: the gpiochip owning the GPIO
975 * @offset: the offset of the GPIO to request for GPIO function
976 */
977int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
978{
979 return pinctrl_request_gpio(chip->base + offset);
980}
981EXPORT_SYMBOL_GPL(gpiochip_generic_request);
982
983/**
984 * gpiochip_generic_free() - free the gpio function from a pin
985 * @chip: the gpiochip to request the gpio function for
986 * @offset: the offset of the GPIO to free from GPIO function
987 */
988void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
989{
990 pinctrl_free_gpio(chip->base + offset);
991}
992EXPORT_SYMBOL_GPL(gpiochip_generic_free);
993
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530994#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100995
Linus Walleij3f0f8672012-11-20 12:40:15 +0100996/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200997 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
998 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +0200999 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001000 * @gpio_offset: the start offset in the current gpio_chip number space
1001 * @pin_group: name of the pin group inside the pin controller
1002 */
1003int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1004 struct pinctrl_dev *pctldev,
1005 unsigned int gpio_offset, const char *pin_group)
1006{
1007 struct gpio_pin_range *pin_range;
1008 int ret;
1009
1010 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1011 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001012 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001013 return -ENOMEM;
1014 }
1015
1016 /* Use local offset as range ID */
1017 pin_range->range.id = gpio_offset;
1018 pin_range->range.gc = chip;
1019 pin_range->range.name = chip->label;
1020 pin_range->range.base = chip->base + gpio_offset;
1021 pin_range->pctldev = pctldev;
1022
1023 ret = pinctrl_get_group_pins(pctldev, pin_group,
1024 &pin_range->range.pins,
1025 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001026 if (ret < 0) {
1027 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001028 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001029 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001030
1031 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1032
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001033 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1034 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001035 pinctrl_dev_get_devname(pctldev), pin_group);
1036
1037 list_add_tail(&pin_range->node, &chip->pin_ranges);
1038
1039 return 0;
1040}
1041EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1042
1043/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001044 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1045 * @chip: the gpiochip to add the range for
1046 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001047 * @gpio_offset: the start offset in the current gpio_chip number space
1048 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001049 * @npins: the number of pins from the offset of each pin space (GPIO and
1050 * pin controller) to accumulate in this range
1051 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001052int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001053 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001054 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301055{
1056 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001057 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301058
Linus Walleij3f0f8672012-11-20 12:40:15 +01001059 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301060 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001061 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001062 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301063 }
1064
Linus Walleij3f0f8672012-11-20 12:40:15 +01001065 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001066 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001067 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301068 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +01001069 pin_range->range.base = chip->base + gpio_offset;
1070 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301071 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001072 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301073 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001074 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001075 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001076 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001077 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001078 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001079 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001080 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1081 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001082 pinctl_name,
1083 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301084
1085 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001086
1087 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301088}
Linus Walleij165adc92012-11-06 14:49:39 +01001089EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301090
Linus Walleij3f0f8672012-11-20 12:40:15 +01001091/**
1092 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1093 * @chip: the chip to remove all the mappings for
1094 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301095void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1096{
1097 struct gpio_pin_range *pin_range, *tmp;
1098
1099 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1100 list_del(&pin_range->node);
1101 pinctrl_remove_gpio_range(pin_range->pctldev,
1102 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001103 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301104 }
1105}
Linus Walleij165adc92012-11-06 14:49:39 +01001106EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1107
1108#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301109
David Brownelld2876d02008-02-04 22:28:20 -08001110/* These "optional" allocation calls help prevent drivers from stomping
1111 * on each other, and help provide better diagnostics in debugfs.
1112 * They're called even less than the "set direction" calls.
1113 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001114static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001115{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001116 struct gpio_chip *chip = desc->chip;
1117 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001118 unsigned long flags;
1119
1120 spin_lock_irqsave(&gpio_lock, flags);
1121
David Brownelld2876d02008-02-04 22:28:20 -08001122 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001123 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001124 */
1125
1126 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1127 desc_set_label(desc, label ? : "?");
1128 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001129 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001130 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001131 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001132 }
1133
1134 if (chip->request) {
1135 /* chip->request may sleep */
1136 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001137 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001138 spin_lock_irqsave(&gpio_lock, flags);
1139
1140 if (status < 0) {
1141 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001142 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001143 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001144 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001145 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001146 if (chip->get_direction) {
1147 /* chip->get_direction may sleep */
1148 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001149 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001150 spin_lock_irqsave(&gpio_lock, flags);
1151 }
David Brownelld2876d02008-02-04 22:28:20 -08001152done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +03001153 if (status < 0) {
1154 /* Clear flags that might have been set by the caller before
1155 * requesting the GPIO.
1156 */
1157 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1158 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1159 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1160 }
Mika Westerberg77c2d792014-03-10 14:54:50 +02001161 spin_unlock_irqrestore(&gpio_lock, flags);
1162 return status;
1163}
1164
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001165int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001166{
1167 int status = -EPROBE_DEFER;
1168 struct gpio_chip *chip;
1169
1170 if (!desc) {
1171 pr_warn("%s: invalid GPIO\n", __func__);
1172 return -EINVAL;
1173 }
1174
1175 chip = desc->chip;
1176 if (!chip)
1177 goto done;
1178
Linus Walleijff2b1352015-10-20 11:10:38 +02001179 if (try_module_get(chip->gpiodev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001180 status = __gpiod_request(desc, label);
1181 if (status < 0)
Linus Walleijff2b1352015-10-20 11:10:38 +02001182 module_put(chip->gpiodev->owner);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001183 }
1184
1185done:
David Brownelld2876d02008-02-04 22:28:20 -08001186 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001187 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001188
David Brownelld2876d02008-02-04 22:28:20 -08001189 return status;
1190}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001191
Mika Westerberg77c2d792014-03-10 14:54:50 +02001192static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001193{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001194 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001195 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001196 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001197
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001198 might_sleep();
1199
Alexandre Courbot372e7222013-02-03 01:29:29 +09001200 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001201
David Brownelld2876d02008-02-04 22:28:20 -08001202 spin_lock_irqsave(&gpio_lock, flags);
1203
David Brownell35e8bb52008-10-15 22:03:16 -07001204 chip = desc->chip;
1205 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1206 if (chip->free) {
1207 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001208 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001209 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001210 spin_lock_irqsave(&gpio_lock, flags);
1211 }
David Brownelld2876d02008-02-04 22:28:20 -08001212 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001213 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001214 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301215 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301216 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001217 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001218 ret = true;
1219 }
David Brownelld2876d02008-02-04 22:28:20 -08001220
1221 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001222 return ret;
1223}
1224
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001225void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001226{
1227 if (desc && __gpiod_free(desc))
Linus Walleijff2b1352015-10-20 11:10:38 +02001228 module_put(desc->chip->gpiodev->owner);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001229 else
1230 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -08001231}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001232
David Brownelld2876d02008-02-04 22:28:20 -08001233/**
1234 * gpiochip_is_requested - return string iff signal was requested
1235 * @chip: controller managing the signal
1236 * @offset: of signal within controller's 0..(ngpio - 1) range
1237 *
1238 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001239 * The string returned is the label passed to gpio_request(); if none has been
1240 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001241 *
1242 * This function is for use by GPIO controller drivers. The label can
1243 * help with diagnostics, and knowing that the signal is used as a GPIO
1244 * can help avoid accidentally multiplexing it to another controller.
1245 */
1246const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1247{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001248 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001249
Dirk Behme48b59532015-08-18 18:02:32 +02001250 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001251 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001252
1253 desc = &chip->desc[offset];
1254
Alexandre Courbot372e7222013-02-03 01:29:29 +09001255 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001256 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001257 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001258}
1259EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1260
Mika Westerberg77c2d792014-03-10 14:54:50 +02001261/**
1262 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1263 * @desc: GPIO descriptor to request
1264 * @label: label for the GPIO
1265 *
1266 * Function allows GPIO chip drivers to request and use their own GPIO
1267 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1268 * function will not increase reference count of the GPIO chip module. This
1269 * allows the GPIO chip module to be unloaded as needed (we assume that the
1270 * GPIO chip driver handles freeing the GPIOs it has requested).
1271 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001272struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1273 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001274{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001275 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1276 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001277
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001278 if (IS_ERR(desc)) {
1279 chip_err(chip, "failed to get GPIO descriptor\n");
1280 return desc;
1281 }
1282
1283 err = __gpiod_request(desc, label);
1284 if (err < 0)
1285 return ERR_PTR(err);
1286
1287 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001288}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001289EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001290
1291/**
1292 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1293 * @desc: GPIO descriptor to free
1294 *
1295 * Function frees the given GPIO requested previously with
1296 * gpiochip_request_own_desc().
1297 */
1298void gpiochip_free_own_desc(struct gpio_desc *desc)
1299{
1300 if (desc)
1301 __gpiod_free(desc);
1302}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001303EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08001304
1305/* Drivers MUST set GPIO direction before making get/set calls. In
1306 * some cases this is done in early boot, before IRQs are enabled.
1307 *
1308 * As a rule these aren't called more than once (except for drivers
1309 * using the open-drain emulation idiom) so these are natural places
1310 * to accumulate extra debugging checks. Note that we can't (yet)
1311 * rely on gpio_request() having been called beforehand.
1312 */
1313
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001314/**
1315 * gpiod_direction_input - set the GPIO direction to input
1316 * @desc: GPIO to set to input
1317 *
1318 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1319 * be called safely on it.
1320 *
1321 * Return 0 in case of success, else an error code.
1322 */
1323int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001324{
David Brownelld2876d02008-02-04 22:28:20 -08001325 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001326 int status = -EINVAL;
1327
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001328 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001329 pr_warn("%s: invalid GPIO\n", __func__);
1330 return -EINVAL;
1331 }
1332
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001333 chip = desc->chip;
1334 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001335 gpiod_warn(desc,
1336 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001337 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001338 return -EIO;
1339 }
1340
Alexandre Courbotd82da792014-07-22 16:17:43 +09001341 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001342 if (status == 0)
1343 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001344
Alexandre Courbot372e7222013-02-03 01:29:29 +09001345 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001346
David Brownelld2876d02008-02-04 22:28:20 -08001347 return status;
1348}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001349EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001350
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001351static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001352{
David Brownelld2876d02008-02-04 22:28:20 -08001353 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001354 int status = -EINVAL;
1355
Linus Walleijd468bf92013-09-24 11:54:38 +02001356 /* GPIOs used for IRQs shall not be set as output */
1357 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1358 gpiod_err(desc,
1359 "%s: tried to set a GPIO tied to an IRQ as output\n",
1360 __func__);
1361 return -EIO;
1362 }
1363
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301364 /* Open drain pin should not be driven to 1 */
1365 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001366 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301367
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301368 /* Open source pin should not be driven to 0 */
1369 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001370 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301371
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001372 chip = desc->chip;
1373 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001374 gpiod_warn(desc,
1375 "%s: missing set() or direction_output() operations\n",
1376 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001377 return -EIO;
1378 }
1379
Alexandre Courbotd82da792014-07-22 16:17:43 +09001380 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001381 if (status == 0)
1382 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001383 trace_gpio_value(desc_to_gpio(desc), 0, value);
1384 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001385 return status;
1386}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001387
1388/**
1389 * gpiod_direction_output_raw - set the GPIO direction to output
1390 * @desc: GPIO to set to output
1391 * @value: initial output value of the GPIO
1392 *
1393 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1394 * be called safely on it. The initial value of the output must be specified
1395 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1396 *
1397 * Return 0 in case of success, else an error code.
1398 */
1399int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1400{
1401 if (!desc || !desc->chip) {
1402 pr_warn("%s: invalid GPIO\n", __func__);
1403 return -EINVAL;
1404 }
1405 return _gpiod_direction_output_raw(desc, value);
1406}
1407EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1408
1409/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301410 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001411 * @desc: GPIO to set to output
1412 * @value: initial output value of the GPIO
1413 *
1414 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1415 * be called safely on it. The initial value of the output must be specified
1416 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1417 * account.
1418 *
1419 * Return 0 in case of success, else an error code.
1420 */
1421int gpiod_direction_output(struct gpio_desc *desc, int value)
1422{
1423 if (!desc || !desc->chip) {
1424 pr_warn("%s: invalid GPIO\n", __func__);
1425 return -EINVAL;
1426 }
1427 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1428 value = !value;
1429 return _gpiod_direction_output_raw(desc, value);
1430}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001431EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001432
Felipe Balbic4b5be92010-05-26 14:42:23 -07001433/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001434 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001435 * @gpio: the gpio to set debounce time
1436 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001437 *
1438 * returns -ENOTSUPP if the controller does not support setting
1439 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001440 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001441int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001442{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001443 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001444
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001445 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001446 pr_warn("%s: invalid GPIO\n", __func__);
1447 return -EINVAL;
1448 }
1449
Felipe Balbic4b5be92010-05-26 14:42:23 -07001450 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001451 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001452 gpiod_dbg(desc,
1453 "%s: missing set() or set_debounce() operations\n",
1454 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001455 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001456 }
1457
Alexandre Courbotd82da792014-07-22 16:17:43 +09001458 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001459}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001460EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001461
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001462/**
1463 * gpiod_is_active_low - test whether a GPIO is active-low or not
1464 * @desc: the gpio descriptor to test
1465 *
1466 * Returns 1 if the GPIO is active-low, 0 otherwise.
1467 */
1468int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001469{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001470 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001471}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001472EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001473
1474/* I/O calls are only valid after configuration completed; the relevant
1475 * "is this a valid GPIO" error checks should already have been done.
1476 *
1477 * "Get" operations are often inlinable as reading a pin value register,
1478 * and masking the relevant bit in that register.
1479 *
1480 * When "set" operations are inlinable, they involve writing that mask to
1481 * one register to set a low value, or a different register to set it high.
1482 * Otherwise locking is needed, so there may be little value to inlining.
1483 *
1484 *------------------------------------------------------------------------
1485 *
1486 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1487 * have requested the GPIO. That can include implicit requesting by
1488 * a direction setting call. Marking a gpio as requested locks its chip
1489 * in memory, guaranteeing that these table lookups need no more locking
1490 * and that gpiochip_remove() will fail.
1491 *
1492 * REVISIT when debugging, consider adding some instrumentation to ensure
1493 * that the GPIO was actually requested.
1494 */
1495
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001496static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001497{
1498 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001499 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001500 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001501
Alexandre Courbot372e7222013-02-03 01:29:29 +09001502 chip = desc->chip;
1503 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001504 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01001505 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001506 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001507 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001508}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001509
David Brownelld2876d02008-02-04 22:28:20 -08001510/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001511 * gpiod_get_raw_value() - return a gpio's raw value
1512 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001513 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001514 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001515 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001516 *
1517 * This function should be called from contexts where we cannot sleep, and will
1518 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001519 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001520int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001521{
David Brownelld2876d02008-02-04 22:28:20 -08001522 if (!desc)
1523 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001524 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001525 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001526 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001527}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001528EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001529
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001530/**
1531 * gpiod_get_value() - return a gpio's value
1532 * @desc: gpio whose value will be returned
1533 *
1534 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001535 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001536 *
1537 * This function should be called from contexts where we cannot sleep, and will
1538 * complain if the GPIO chip functions potentially sleep.
1539 */
1540int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001541{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001542 int value;
1543 if (!desc)
1544 return 0;
1545 /* Should be using gpio_get_value_cansleep() */
1546 WARN_ON(desc->chip->can_sleep);
1547
1548 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001549 if (value < 0)
1550 return value;
1551
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001552 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1553 value = !value;
1554
1555 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001556}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001557EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001558
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301559/*
1560 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001561 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001562 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301563 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001564static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301565{
1566 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001567 struct gpio_chip *chip = desc->chip;
1568 int offset = gpio_chip_hwgpio(desc);
1569
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301570 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001571 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301572 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001573 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301574 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001575 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301576 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001577 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301578 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001579 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301580 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001581 gpiod_err(desc,
1582 "%s: Error in set_value for open drain err %d\n",
1583 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301584}
1585
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301586/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001587 * _gpio_set_open_source_value() - Set the open source gpio's value.
1588 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001589 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301590 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001591static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301592{
1593 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001594 struct gpio_chip *chip = desc->chip;
1595 int offset = gpio_chip_hwgpio(desc);
1596
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301597 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001598 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301599 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001600 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301601 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001602 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301603 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001604 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301605 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001606 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301607 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001608 gpiod_err(desc,
1609 "%s: Error in set_value for open source err %d\n",
1610 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301611}
1612
Alexandre Courbot23600962014-03-11 15:52:09 +09001613static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001614{
1615 struct gpio_chip *chip;
1616
Alexandre Courbot372e7222013-02-03 01:29:29 +09001617 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001618 trace_gpio_value(desc_to_gpio(desc), 0, value);
1619 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1620 _gpio_set_open_drain_value(desc, value);
1621 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1622 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301623 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001624 chip->set(chip, gpio_chip_hwgpio(desc), value);
1625}
1626
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001627/*
1628 * set multiple outputs on the same chip;
1629 * use the chip's set_multiple function if available;
1630 * otherwise set the outputs sequentially;
1631 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1632 * defines which outputs are to be changed
1633 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1634 * defines the values the outputs specified by mask are to be set to
1635 */
1636static void gpio_chip_set_multiple(struct gpio_chip *chip,
1637 unsigned long *mask, unsigned long *bits)
1638{
1639 if (chip->set_multiple) {
1640 chip->set_multiple(chip, mask, bits);
1641 } else {
1642 int i;
1643 for (i = 0; i < chip->ngpio; i++) {
1644 if (mask[BIT_WORD(i)] == 0) {
1645 /* no more set bits in this mask word;
1646 * skip ahead to the next word */
1647 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1648 continue;
1649 }
1650 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001651 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001652 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001653 }
1654 }
1655}
1656
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001657static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1658 unsigned int array_size,
1659 struct gpio_desc **desc_array,
1660 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001661{
1662 int i = 0;
1663
1664 while (i < array_size) {
1665 struct gpio_chip *chip = desc_array[i]->chip;
1666 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1667 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1668 int count = 0;
1669
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001670 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001671 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001672
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001673 memset(mask, 0, sizeof(mask));
1674 do {
1675 struct gpio_desc *desc = desc_array[i];
1676 int hwgpio = gpio_chip_hwgpio(desc);
1677 int value = value_array[i];
1678
1679 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1680 value = !value;
1681 trace_gpio_value(desc_to_gpio(desc), 0, value);
1682 /*
1683 * collect all normal outputs belonging to the same chip
1684 * open drain and open source outputs are set individually
1685 */
1686 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001687 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001688 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1689 _gpio_set_open_source_value(desc, value);
1690 } else {
1691 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001692 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001693 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001694 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001695 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001696 count++;
1697 }
1698 i++;
1699 } while ((i < array_size) && (desc_array[i]->chip == chip));
1700 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001701 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001702 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001703 }
1704}
1705
David Brownelld2876d02008-02-04 22:28:20 -08001706/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001707 * gpiod_set_raw_value() - assign a gpio's raw value
1708 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001709 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001710 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001711 * Set the raw value of the GPIO, i.e. the value of its physical line without
1712 * regard for its ACTIVE_LOW status.
1713 *
1714 * This function should be called from contexts where we cannot sleep, and will
1715 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001716 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001717void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001718{
David Brownelld2876d02008-02-04 22:28:20 -08001719 if (!desc)
1720 return;
David Brownelld2876d02008-02-04 22:28:20 -08001721 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001722 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001723 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001724}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001725EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001726
1727/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001728 * gpiod_set_value() - assign a gpio's value
1729 * @desc: gpio whose value will be assigned
1730 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001731 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001732 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1733 * account
1734 *
1735 * This function should be called from contexts where we cannot sleep, and will
1736 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001737 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001738void gpiod_set_value(struct gpio_desc *desc, int value)
1739{
1740 if (!desc)
1741 return;
1742 /* Should be using gpio_set_value_cansleep() */
1743 WARN_ON(desc->chip->can_sleep);
1744 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1745 value = !value;
1746 _gpiod_set_raw_value(desc, value);
1747}
1748EXPORT_SYMBOL_GPL(gpiod_set_value);
1749
1750/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001751 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001752 * @array_size: number of elements in the descriptor / value arrays
1753 * @desc_array: array of GPIO descriptors whose values will be assigned
1754 * @value_array: array of values to assign
1755 *
1756 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1757 * without regard for their ACTIVE_LOW status.
1758 *
1759 * This function should be called from contexts where we cannot sleep, and will
1760 * complain if the GPIO chip functions potentially sleep.
1761 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001762void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001763 struct gpio_desc **desc_array, int *value_array)
1764{
1765 if (!desc_array)
1766 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001767 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1768 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001769}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001770EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001771
1772/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001773 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001774 * @array_size: number of elements in the descriptor / value arrays
1775 * @desc_array: array of GPIO descriptors whose values will be assigned
1776 * @value_array: array of values to assign
1777 *
1778 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1779 * into account.
1780 *
1781 * This function should be called from contexts where we cannot sleep, and will
1782 * complain if the GPIO chip functions potentially sleep.
1783 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001784void gpiod_set_array_value(unsigned int array_size,
1785 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001786{
1787 if (!desc_array)
1788 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001789 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1790 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001791}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001792EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001793
1794/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001795 * gpiod_cansleep() - report whether gpio value access may sleep
1796 * @desc: gpio to check
1797 *
1798 */
1799int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001800{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001801 if (!desc)
1802 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001803 return desc->chip->can_sleep;
1804}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001805EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001806
David Brownell0f6d5042008-10-15 22:03:14 -07001807/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001808 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1809 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001810 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001811 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1812 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001813 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001814int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001815{
1816 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001817 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001818
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001819 if (!desc)
1820 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001821 chip = desc->chip;
1822 offset = gpio_chip_hwgpio(desc);
1823 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1824}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001825EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001826
Linus Walleijd468bf92013-09-24 11:54:38 +02001827/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001828 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001829 * @chip: the chip the GPIO to lock belongs to
1830 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001831 *
1832 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001833 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001834 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001835int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001836{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001837 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001838 return -EINVAL;
1839
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001840 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1841 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001842 "%s: tried to flag a GPIO set as output for IRQ\n",
1843 __func__);
1844 return -EIO;
1845 }
1846
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001847 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001848 return 0;
1849}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001850EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001851
Linus Walleijd468bf92013-09-24 11:54:38 +02001852/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001853 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001854 * @chip: the chip the GPIO to lock belongs to
1855 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001856 *
1857 * This is used directly by GPIO drivers that want to indicate
1858 * that a certain GPIO is no longer used exclusively for IRQ.
1859 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001860void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001861{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001862 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001863 return;
1864
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001865 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001866}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001867EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001868
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001869/**
1870 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1871 * @desc: gpio whose value will be returned
1872 *
1873 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001874 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001875 *
1876 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001877 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001878int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001879{
David Brownelld2876d02008-02-04 22:28:20 -08001880 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001881 if (!desc)
1882 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001883 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001884}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001885EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001886
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001887/**
1888 * gpiod_get_value_cansleep() - return a gpio's value
1889 * @desc: gpio whose value will be returned
1890 *
1891 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001892 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001893 *
1894 * This function is to be called from contexts that can sleep.
1895 */
1896int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001897{
David Brownelld2876d02008-02-04 22:28:20 -08001898 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001899
1900 might_sleep_if(extra_checks);
1901 if (!desc)
1902 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001903
1904 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001905 if (value < 0)
1906 return value;
1907
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001908 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1909 value = !value;
1910
David Brownelld2876d02008-02-04 22:28:20 -08001911 return value;
1912}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001913EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001914
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001915/**
1916 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1917 * @desc: gpio whose value will be assigned
1918 * @value: value to assign
1919 *
1920 * Set the raw value of the GPIO, i.e. the value of its physical line without
1921 * regard for its ACTIVE_LOW status.
1922 *
1923 * This function is to be called from contexts that can sleep.
1924 */
1925void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001926{
David Brownelld2876d02008-02-04 22:28:20 -08001927 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001928 if (!desc)
1929 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001930 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001931}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001932EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001933
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001934/**
1935 * gpiod_set_value_cansleep() - assign a gpio's value
1936 * @desc: gpio whose value will be assigned
1937 * @value: value to assign
1938 *
1939 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1940 * account
1941 *
1942 * This function is to be called from contexts that can sleep.
1943 */
1944void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001945{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001946 might_sleep_if(extra_checks);
1947 if (!desc)
1948 return;
1949
1950 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1951 value = !value;
1952 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001953}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001954EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001955
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001956/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001957 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001958 * @array_size: number of elements in the descriptor / value arrays
1959 * @desc_array: array of GPIO descriptors whose values will be assigned
1960 * @value_array: array of values to assign
1961 *
1962 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1963 * without regard for their ACTIVE_LOW status.
1964 *
1965 * This function is to be called from contexts that can sleep.
1966 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001967void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1968 struct gpio_desc **desc_array,
1969 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001970{
1971 might_sleep_if(extra_checks);
1972 if (!desc_array)
1973 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001974 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1975 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001976}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001977EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001978
1979/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001980 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001981 * @array_size: number of elements in the descriptor / value arrays
1982 * @desc_array: array of GPIO descriptors whose values will be assigned
1983 * @value_array: array of values to assign
1984 *
1985 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1986 * into account.
1987 *
1988 * This function is to be called from contexts that can sleep.
1989 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001990void gpiod_set_array_value_cansleep(unsigned int array_size,
1991 struct gpio_desc **desc_array,
1992 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001993{
1994 might_sleep_if(extra_checks);
1995 if (!desc_array)
1996 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001997 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1998 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001999}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002000EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002001
2002/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002003 * gpiod_add_lookup_table() - register GPIO device consumers
2004 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002005 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002006void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002007{
2008 mutex_lock(&gpio_lookup_lock);
2009
Alexandre Courbotad824782013-12-03 12:20:11 +09002010 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002011
2012 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002013}
2014
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302015/**
2016 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2017 * @table: table of consumers to unregister
2018 */
2019void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2020{
2021 mutex_lock(&gpio_lookup_lock);
2022
2023 list_del(&table->list);
2024
2025 mutex_unlock(&gpio_lookup_lock);
2026}
2027
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002028static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002029 unsigned int idx,
2030 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002031{
2032 char prop_name[32]; /* 32 is max size of property name */
2033 enum of_gpio_flags of_flags;
2034 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02002035 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002036
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002037 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02002038 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01002039 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002040 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02002041 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01002042 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002043 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002044
Thierry Redingdd34c372014-04-23 17:28:09 +02002045 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2046 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07002047 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02002048 break;
2049 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002050
2051 if (IS_ERR(desc))
2052 return desc;
2053
2054 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002055 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002056
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002057 if (of_flags & OF_GPIO_SINGLE_ENDED) {
2058 if (of_flags & OF_GPIO_ACTIVE_LOW)
2059 *flags |= GPIO_OPEN_DRAIN;
2060 else
2061 *flags |= GPIO_OPEN_SOURCE;
2062 }
2063
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002064 return desc;
2065}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002066
Mika Westerberg81f59e92013-10-10 11:01:09 +03002067static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002068 unsigned int idx,
2069 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03002070{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002071 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03002072 struct acpi_gpio_info info;
2073 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002074 char propname[32];
2075 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03002076
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002077 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002078 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002079 if (con_id && strcmp(con_id, "gpios")) {
2080 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002081 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002082 } else {
2083 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002084 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002085 }
Mika Westerberge01f4402013-10-10 11:01:10 +03002086
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002087 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
2088 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2089 break;
2090 }
2091
2092 /* Then from plain _CRS GPIOs */
2093 if (IS_ERR(desc)) {
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08002094 if (!acpi_can_fallback_to_crs(adev, con_id))
2095 return ERR_PTR(-ENOENT);
2096
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002097 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2098 if (IS_ERR(desc))
2099 return desc;
2100 }
2101
Christophe RICARD52044722015-12-23 23:25:34 +01002102 if (info.polarity == GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002103 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002104
2105 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002106}
2107
Alexandre Courbotad824782013-12-03 12:20:11 +09002108static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2109{
2110 const char *dev_id = dev ? dev_name(dev) : NULL;
2111 struct gpiod_lookup_table *table;
2112
2113 mutex_lock(&gpio_lookup_lock);
2114
2115 list_for_each_entry(table, &gpio_lookup_list, list) {
2116 if (table->dev_id && dev_id) {
2117 /*
2118 * Valid strings on both ends, must be identical to have
2119 * a match
2120 */
2121 if (!strcmp(table->dev_id, dev_id))
2122 goto found;
2123 } else {
2124 /*
2125 * One of the pointers is NULL, so both must be to have
2126 * a match
2127 */
2128 if (dev_id == table->dev_id)
2129 goto found;
2130 }
2131 }
2132 table = NULL;
2133
2134found:
2135 mutex_unlock(&gpio_lookup_lock);
2136 return table;
2137}
2138
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002139static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002140 unsigned int idx,
2141 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002142{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002143 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002144 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002145 struct gpiod_lookup *p;
2146
Alexandre Courbotad824782013-12-03 12:20:11 +09002147 table = gpiod_find_lookup_table(dev);
2148 if (!table)
2149 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002150
Alexandre Courbotad824782013-12-03 12:20:11 +09002151 for (p = &table->table[0]; p->chip_label; p++) {
2152 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002153
Alexandre Courbotad824782013-12-03 12:20:11 +09002154 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002155 if (p->idx != idx)
2156 continue;
2157
Alexandre Courbotad824782013-12-03 12:20:11 +09002158 /* If the lookup entry has a con_id, require exact match */
2159 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2160 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002161
Alexandre Courbotad824782013-12-03 12:20:11 +09002162 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002163
Alexandre Courbotad824782013-12-03 12:20:11 +09002164 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002165 dev_err(dev, "cannot find GPIO chip %s\n",
2166 p->chip_label);
2167 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002168 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002169
Alexandre Courbotad824782013-12-03 12:20:11 +09002170 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002171 dev_err(dev,
2172 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2173 idx, chip->ngpio, chip->label);
2174 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09002175 }
2176
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09002177 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09002178 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002179
2180 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09002181 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002182
2183 return desc;
2184}
2185
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002186static int dt_gpio_count(struct device *dev, const char *con_id)
2187{
2188 int ret;
2189 char propname[32];
2190 unsigned int i;
2191
2192 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2193 if (con_id)
2194 snprintf(propname, sizeof(propname), "%s-%s",
2195 con_id, gpio_suffixes[i]);
2196 else
2197 snprintf(propname, sizeof(propname), "%s",
2198 gpio_suffixes[i]);
2199
2200 ret = of_gpio_named_count(dev->of_node, propname);
2201 if (ret >= 0)
2202 break;
2203 }
2204 return ret;
2205}
2206
2207static int platform_gpio_count(struct device *dev, const char *con_id)
2208{
2209 struct gpiod_lookup_table *table;
2210 struct gpiod_lookup *p;
2211 unsigned int count = 0;
2212
2213 table = gpiod_find_lookup_table(dev);
2214 if (!table)
2215 return -ENOENT;
2216
2217 for (p = &table->table[0]; p->chip_label; p++) {
2218 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2219 (!con_id && !p->con_id))
2220 count++;
2221 }
2222 if (!count)
2223 return -ENOENT;
2224
2225 return count;
2226}
2227
2228/**
2229 * gpiod_count - return the number of GPIOs associated with a device / function
2230 * or -ENOENT if no GPIO has been assigned to the requested function
2231 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2232 * @con_id: function within the GPIO consumer
2233 */
2234int gpiod_count(struct device *dev, const char *con_id)
2235{
2236 int count = -ENOENT;
2237
2238 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2239 count = dt_gpio_count(dev, con_id);
2240 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2241 count = acpi_gpio_count(dev, con_id);
2242
2243 if (count < 0)
2244 count = platform_gpio_count(dev, con_id);
2245
2246 return count;
2247}
2248EXPORT_SYMBOL_GPL(gpiod_count);
2249
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002250/**
Thierry Reding08791622014-04-25 16:54:22 +02002251 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002252 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002253 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002254 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002255 *
2256 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002257 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07002258 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002259 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002260struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002261 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002262{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002263 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002264}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002265EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002266
2267/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002268 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2269 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2270 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002271 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002272 *
2273 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2274 * the requested function it will return NULL. This is convenient for drivers
2275 * that need to handle optional GPIOs.
2276 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002277struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002278 const char *con_id,
2279 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002280{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002281 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002282}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002283EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002284
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002285/**
2286 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2287 * @desc: gpio to be setup
2288 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2289 * of_get_gpio_hog()
2290 *
2291 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2292 */
2293static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2294{
2295 if (lflags & GPIO_ACTIVE_LOW)
2296 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2297 if (lflags & GPIO_OPEN_DRAIN)
2298 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2299 if (lflags & GPIO_OPEN_SOURCE)
2300 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2301}
Benoit Parrotf625d462015-02-02 11:44:44 -06002302
2303/**
2304 * gpiod_configure_flags - helper function to configure a given GPIO
2305 * @desc: gpio whose value will be assigned
2306 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06002307 * @dflags: gpiod_flags - optional GPIO initialization flags
2308 *
2309 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2310 * requested function and/or index, or another IS_ERR() code if an error
2311 * occurred while trying to acquire the GPIO.
2312 */
2313static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002314 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06002315{
2316 int status;
2317
Benoit Parrotf625d462015-02-02 11:44:44 -06002318 /* No particular flag request, return here... */
2319 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2320 pr_debug("no flags found for %s\n", con_id);
2321 return 0;
2322 }
2323
2324 /* Process flags */
2325 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2326 status = gpiod_direction_output(desc,
2327 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2328 else
2329 status = gpiod_direction_input(desc);
2330
2331 return status;
2332}
2333
Thierry Reding29a1f2332014-04-25 17:10:06 +02002334/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002335 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002336 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002337 * @con_id: function within the GPIO consumer
2338 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002339 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002340 *
2341 * This variant of gpiod_get() allows to access GPIOs other than the first
2342 * defined one for functions that define several GPIOs.
2343 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002344 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2345 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07002346 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002347 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002348struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002349 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002350 unsigned int idx,
2351 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002352{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002353 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002354 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002355 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002356
2357 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2358
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002359 if (dev) {
2360 /* Using device tree? */
2361 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2362 dev_dbg(dev, "using device tree for GPIO lookup\n");
2363 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2364 } else if (ACPI_COMPANION(dev)) {
2365 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2366 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2367 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002368 }
2369
2370 /*
2371 * Either we are not using DT or ACPI, or their lookup did not return
2372 * a result. In that case, use platform lookup as a fallback.
2373 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002374 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002375 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002376 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002377 }
2378
2379 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002380 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002381 return desc;
2382 }
2383
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002384 gpiod_parse_flags(desc, lookupflags);
2385
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002386 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002387 if (status < 0)
2388 return ERR_PTR(status);
2389
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002390 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002391 if (status < 0) {
2392 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2393 gpiod_put(desc);
2394 return ERR_PTR(status);
2395 }
2396
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002397 return desc;
2398}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002399EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002400
2401/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002402 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2403 * @fwnode: handle of the firmware node
2404 * @propname: name of the firmware property representing the GPIO
2405 *
2406 * This function can be used for drivers that get their configuration
2407 * from firmware.
2408 *
2409 * Function properly finds the corresponding GPIO using whatever is the
2410 * underlying firmware interface and then makes sure that the GPIO
2411 * descriptor is requested before it is returned to the caller.
2412 *
2413 * In case of error an ERR_PTR() is returned.
2414 */
2415struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2416 const char *propname)
2417{
2418 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2419 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002420 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02002421 int ret;
2422
2423 if (!fwnode)
2424 return ERR_PTR(-EINVAL);
2425
2426 if (is_of_node(fwnode)) {
2427 enum of_gpio_flags flags;
2428
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002429 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002430 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002431 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02002432 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002433 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2434 }
Mika Westerberg40b73182014-10-21 13:33:59 +02002435 } else if (is_acpi_node(fwnode)) {
2436 struct acpi_gpio_info info;
2437
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02002438 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02002439 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01002440 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02002441 }
2442
2443 if (IS_ERR(desc))
2444 return desc;
2445
Mika Westerberg40b73182014-10-21 13:33:59 +02002446 if (active_low)
2447 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2448
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002449 if (single_ended) {
2450 if (active_low)
2451 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2452 else
2453 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2454 }
2455
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002456 ret = gpiod_request(desc, NULL);
2457 if (ret)
2458 return ERR_PTR(ret);
2459
Mika Westerberg40b73182014-10-21 13:33:59 +02002460 return desc;
2461}
2462EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2463
2464/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002465 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2466 * function
2467 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2468 * @con_id: function within the GPIO consumer
2469 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002470 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002471 *
2472 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2473 * specified index was assigned to the requested function it will return NULL.
2474 * This is convenient for drivers that need to handle optional GPIOs.
2475 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002476struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002477 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002478 unsigned int index,
2479 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002480{
2481 struct gpio_desc *desc;
2482
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002483 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002484 if (IS_ERR(desc)) {
2485 if (PTR_ERR(desc) == -ENOENT)
2486 return NULL;
2487 }
2488
2489 return desc;
2490}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002491EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002492
2493/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002494 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2495 * @desc: gpio whose value will be assigned
2496 * @name: gpio line name
2497 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2498 * of_get_gpio_hog()
2499 * @dflags: gpiod_flags - optional GPIO initialization flags
2500 */
2501int gpiod_hog(struct gpio_desc *desc, const char *name,
2502 unsigned long lflags, enum gpiod_flags dflags)
2503{
2504 struct gpio_chip *chip;
2505 struct gpio_desc *local_desc;
2506 int hwnum;
2507 int status;
2508
2509 chip = gpiod_to_chip(desc);
2510 hwnum = gpio_chip_hwgpio(desc);
2511
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002512 gpiod_parse_flags(desc, lflags);
2513
Benoit Parrotf625d462015-02-02 11:44:44 -06002514 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2515 if (IS_ERR(local_desc)) {
Linus Walleija7138902015-06-05 11:36:10 +02002516 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2517 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002518 return PTR_ERR(local_desc);
2519 }
2520
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002521 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002522 if (status < 0) {
Linus Walleija7138902015-06-05 11:36:10 +02002523 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2524 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002525 gpiochip_free_own_desc(desc);
2526 return status;
2527 }
2528
2529 /* Mark GPIO as hogged so it can be identified and removed later */
2530 set_bit(FLAG_IS_HOGGED, &desc->flags);
2531
2532 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2533 desc_to_gpio(desc), name,
2534 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2535 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2536 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2537
2538 return 0;
2539}
2540
2541/**
2542 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2543 * @chip: gpio chip to act on
2544 *
2545 * This is only used by of_gpiochip_remove to free hogged gpios
2546 */
2547static void gpiochip_free_hogs(struct gpio_chip *chip)
2548{
2549 int id;
2550
2551 for (id = 0; id < chip->ngpio; id++) {
2552 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2553 gpiochip_free_own_desc(&chip->desc[id]);
2554 }
2555}
2556
2557/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002558 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2559 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2560 * @con_id: function within the GPIO consumer
2561 * @flags: optional GPIO initialization flags
2562 *
2563 * This function acquires all the GPIOs defined under a given function.
2564 *
2565 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2566 * no GPIO has been assigned to the requested function, or another IS_ERR()
2567 * code if an error occurred while trying to acquire the GPIOs.
2568 */
2569struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2570 const char *con_id,
2571 enum gpiod_flags flags)
2572{
2573 struct gpio_desc *desc;
2574 struct gpio_descs *descs;
2575 int count;
2576
2577 count = gpiod_count(dev, con_id);
2578 if (count < 0)
2579 return ERR_PTR(count);
2580
2581 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2582 GFP_KERNEL);
2583 if (!descs)
2584 return ERR_PTR(-ENOMEM);
2585
2586 for (descs->ndescs = 0; descs->ndescs < count; ) {
2587 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2588 if (IS_ERR(desc)) {
2589 gpiod_put_array(descs);
2590 return ERR_CAST(desc);
2591 }
2592 descs->desc[descs->ndescs] = desc;
2593 descs->ndescs++;
2594 }
2595 return descs;
2596}
2597EXPORT_SYMBOL_GPL(gpiod_get_array);
2598
2599/**
2600 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2601 * function
2602 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2603 * @con_id: function within the GPIO consumer
2604 * @flags: optional GPIO initialization flags
2605 *
2606 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2607 * assigned to the requested function it will return NULL.
2608 */
2609struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2610 const char *con_id,
2611 enum gpiod_flags flags)
2612{
2613 struct gpio_descs *descs;
2614
2615 descs = gpiod_get_array(dev, con_id, flags);
2616 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2617 return NULL;
2618
2619 return descs;
2620}
2621EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2622
2623/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002624 * gpiod_put - dispose of a GPIO descriptor
2625 * @desc: GPIO descriptor to dispose of
2626 *
2627 * No descriptor can be used after gpiod_put() has been called on it.
2628 */
2629void gpiod_put(struct gpio_desc *desc)
2630{
2631 gpiod_free(desc);
2632}
2633EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002634
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002635/**
2636 * gpiod_put_array - dispose of multiple GPIO descriptors
2637 * @descs: struct gpio_descs containing an array of descriptors
2638 */
2639void gpiod_put_array(struct gpio_descs *descs)
2640{
2641 unsigned int i;
2642
2643 for (i = 0; i < descs->ndescs; i++)
2644 gpiod_put(descs->desc[i]);
2645
2646 kfree(descs);
2647}
2648EXPORT_SYMBOL_GPL(gpiod_put_array);
2649
Linus Walleij3c702e92015-10-21 15:29:53 +02002650static int __init gpiolib_dev_init(void)
2651{
2652 int ret;
2653
2654 /* Register GPIO sysfs bus */
2655 ret = bus_register(&gpio_bus_type);
2656 if (ret < 0) {
2657 pr_err("gpiolib: could not register GPIO bus type\n");
2658 return ret;
2659 }
2660
2661 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
2662 if (ret < 0) {
2663 pr_err("gpiolib: failed to allocate char dev region\n");
2664 bus_unregister(&gpio_bus_type);
2665 }
2666 return ret;
2667}
2668core_initcall(gpiolib_dev_init);
2669
David Brownelld2876d02008-02-04 22:28:20 -08002670#ifdef CONFIG_DEBUG_FS
2671
2672static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2673{
2674 unsigned i;
2675 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002676 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002677 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002678 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002679
2680 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02002681 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2682 if (gdesc->name) {
2683 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2684 gpio, gdesc->name);
2685 }
David Brownelld2876d02008-02-04 22:28:20 -08002686 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02002687 }
David Brownelld2876d02008-02-04 22:28:20 -08002688
Alexandre Courbot372e7222013-02-03 01:29:29 +09002689 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002690 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002691 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02002692 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2693 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08002694 is_out ? "out" : "in ",
2695 chip->get
2696 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002697 : "? ",
2698 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002699 seq_printf(s, "\n");
2700 }
2701}
2702
Thierry Redingf9c4a312012-04-12 13:26:01 +02002703static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002704{
Grant Likely362432a2013-02-09 09:41:49 +00002705 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002706 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002707 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002708
2709 s->private = "";
2710
Grant Likely362432a2013-02-09 09:41:49 +00002711 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002712 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00002713 if (index-- == 0) {
2714 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002715 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00002716 }
2717 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002718
2719 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002720}
2721
2722static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2723{
Grant Likely362432a2013-02-09 09:41:49 +00002724 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002725 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002726 void *ret = NULL;
2727
Grant Likely362432a2013-02-09 09:41:49 +00002728 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002729 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002730 ret = NULL;
2731 else
Linus Walleijff2b1352015-10-20 11:10:38 +02002732 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00002733 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002734
2735 s->private = "\n";
2736 ++*pos;
2737
2738 return ret;
2739}
2740
2741static void gpiolib_seq_stop(struct seq_file *s, void *v)
2742{
2743}
2744
2745static int gpiolib_seq_show(struct seq_file *s, void *v)
2746{
Linus Walleijff2b1352015-10-20 11:10:38 +02002747 struct gpio_device *gdev = v;
2748 struct gpio_chip *chip = gdev->chip;
2749 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002750
Linus Walleijff2b1352015-10-20 11:10:38 +02002751 if (!chip) {
2752 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
2753 dev_name(&gdev->dev));
2754 return 0;
2755 }
2756
2757 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
2758 dev_name(&gdev->dev),
2759 chip->base, chip->base + chip->ngpio - 1);
2760 parent = chip->parent;
2761 if (parent)
2762 seq_printf(s, ", parent: %s/%s",
2763 parent->bus ? parent->bus->name : "no-bus",
2764 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02002765 if (chip->label)
2766 seq_printf(s, ", %s", chip->label);
2767 if (chip->can_sleep)
2768 seq_printf(s, ", can sleep");
2769 seq_printf(s, ":\n");
2770
2771 if (chip->dbg_show)
2772 chip->dbg_show(s, chip);
2773 else
2774 gpiolib_dbg_show(s, chip);
2775
David Brownelld2876d02008-02-04 22:28:20 -08002776 return 0;
2777}
2778
Thierry Redingf9c4a312012-04-12 13:26:01 +02002779static const struct seq_operations gpiolib_seq_ops = {
2780 .start = gpiolib_seq_start,
2781 .next = gpiolib_seq_next,
2782 .stop = gpiolib_seq_stop,
2783 .show = gpiolib_seq_show,
2784};
2785
David Brownelld2876d02008-02-04 22:28:20 -08002786static int gpiolib_open(struct inode *inode, struct file *file)
2787{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002788 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002789}
2790
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002791static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002792 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002793 .open = gpiolib_open,
2794 .read = seq_read,
2795 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002796 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002797};
2798
2799static int __init gpiolib_debugfs_init(void)
2800{
2801 /* /sys/kernel/debug/gpio */
2802 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2803 NULL, NULL, &gpiolib_operations);
2804 return 0;
2805}
2806subsys_initcall(gpiolib_debugfs_init);
2807
2808#endif /* DEBUG_FS */