blob: 043128d3e831daf391e544b67c2f3db23f8c2146 [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020018#include <linux/pinctrl/consumer.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020019#include <linux/cdev.h>
20#include <linux/fs.h>
21#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020022#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020023#include <linux/anon_inodes.h>
Linus Walleij61f922d2016-06-02 11:30:15 +020024#include <linux/kfifo.h>
25#include <linux/poll.h>
26#include <linux/timekeeping.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020027#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080028
Mika Westerberg664e3e52014-01-08 12:40:54 +020029#include "gpiolib.h"
30
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060031#define CREATE_TRACE_POINTS
32#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080033
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070034/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080035 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070036 * The GPIO programming interface allows for inlining speed-critical
37 * get/set operations for common cases, so that access to SOC-integrated
38 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080039 */
40
41
42/* When debugging, extend minimal trust to callers and platform code.
43 * Also emit diagnostic messages that may help initial bringup, when
44 * board setup or driver bugs are most common.
45 *
46 * Otherwise, minimize overhead in what may be bitbanging codepaths.
47 */
48#ifdef DEBUG
49#define extra_checks 1
50#else
51#define extra_checks 0
52#endif
53
Linus Walleijff2b1352015-10-20 11:10:38 +020054/* Device and char device-related information */
55static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020056static dev_t gpio_devt;
57#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
58static struct bus_type gpio_bus_type = {
59 .name = "gpio",
60};
Linus Walleijff2b1352015-10-20 11:10:38 +020061
David Brownelld2876d02008-02-04 22:28:20 -080062/* gpio_lock prevents conflicts during gpio_desc[] table updates.
63 * While any GPIO is requested, its gpio_chip is not removable;
64 * each GPIO's "requested" flag serves as a lock and refcount.
65 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090066DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080067
Alexandre Courbotbae48da2013-10-17 10:21:38 -070068static DEFINE_MUTEX(gpio_lookup_lock);
69static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020070LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020071
72static void gpiochip_free_hogs(struct gpio_chip *chip);
73static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
74
Guenter Roeck159f3cd2016-03-31 08:11:30 -070075static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020076
David Brownelld2876d02008-02-04 22:28:20 -080077static inline void desc_set_label(struct gpio_desc *d, const char *label)
78{
David Brownelld2876d02008-02-04 22:28:20 -080079 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080080}
81
Alexandre Courbot372e7222013-02-03 01:29:29 +090082/**
83 * Convert a GPIO number to its descriptor
84 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070085struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090086{
Linus Walleijff2b1352015-10-20 11:10:38 +020087 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +090088 unsigned long flags;
89
90 spin_lock_irqsave(&gpio_lock, flags);
91
Linus Walleijff2b1352015-10-20 11:10:38 +020092 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +010093 if (gdev->base <= gpio &&
94 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +090095 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +010096 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +090097 }
98 }
99
100 spin_unlock_irqrestore(&gpio_lock, flags);
101
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900102 if (!gpio_is_valid(gpio))
103 WARN(1, "invalid GPIO %d\n", gpio);
104
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900105 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900106}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700107EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900108
109/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900110 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +0200111 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900112struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
113 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200114{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100115 struct gpio_device *gdev = chip->gpiodev;
116
117 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900118 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200119
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100120 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200121}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900122
123/**
124 * Convert a GPIO descriptor to the integer namespace.
125 * This should disappear in the future but is needed since we still
126 * use GPIO numbers for error messages and sysfs nodes
127 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700128int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900129{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100130 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900131}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700132EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900133
134
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700135/**
136 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
137 * @desc: descriptor to return the chip of
138 */
139struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900140{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100141 if (!desc || !desc->gdev || !desc->gdev->chip)
142 return NULL;
143 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900144}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700145EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800146
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700147/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
148static int gpiochip_find_base(int ngpio)
149{
Linus Walleijff2b1352015-10-20 11:10:38 +0200150 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900151 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700152
Linus Walleijff2b1352015-10-20 11:10:38 +0200153 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900154 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100155 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900156 break;
157 else
158 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100159 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700160 }
161
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900162 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700163 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900164 return base;
165 } else {
166 pr_err("%s: cannot find free range\n", __func__);
167 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700168 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700169}
170
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700171/**
172 * gpiod_get_direction - return the current direction of a GPIO
173 * @desc: GPIO to get the direction of
174 *
175 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
176 *
177 * This function may sleep if gpiod_cansleep() is true.
178 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900179int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300180{
181 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900182 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300183 int status = -EINVAL;
184
Alexandre Courbot372e7222013-02-03 01:29:29 +0900185 chip = gpiod_to_chip(desc);
186 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300187
188 if (!chip->get_direction)
189 return status;
190
Alexandre Courbot372e7222013-02-03 01:29:29 +0900191 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300192 if (status > 0) {
193 /* GPIOF_DIR_IN, or other positive */
194 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900195 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300196 }
197 if (status == 0) {
198 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900199 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300200 }
201 return status;
202}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700203EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300204
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900205/*
206 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800207 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900208 *
209 * Return -EBUSY if the new chip overlaps with some other chip's integer
210 * space.
211 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200212static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900213{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800214 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200215
216 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800217 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200218 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530219 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900220 }
221
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800222 next = list_entry(gpio_devices.next, struct gpio_device, list);
223 if (gdev->base + gdev->ngpio <= next->base) {
224 /* add before first entry */
225 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500226 return 0;
227 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900228
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800229 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
230 if (prev->base + prev->ngpio <= gdev->base) {
231 /* add behind last entry */
232 list_add_tail(&gdev->list, &gpio_devices);
233 return 0;
234 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800235
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800236 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
237 /* at the end of the list */
238 if (&next->list == &gpio_devices)
239 break;
240
241 /* add between prev and next */
242 if (prev->base + prev->ngpio <= gdev->base
243 && gdev->base + gdev->ngpio <= next->base) {
244 list_add(&gdev->list, &prev->list);
245 return 0;
246 }
247 }
248
249 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
250 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900251}
252
Linus Walleijf881bab02015-09-23 16:20:43 -0700253/**
254 * Convert a GPIO name to its descriptor
255 */
256static struct gpio_desc *gpio_name_to_desc(const char * const name)
257{
Linus Walleijff2b1352015-10-20 11:10:38 +0200258 struct gpio_device *gdev;
Linus Walleijf881bab02015-09-23 16:20:43 -0700259 unsigned long flags;
260
261 spin_lock_irqsave(&gpio_lock, flags);
262
Linus Walleijff2b1352015-10-20 11:10:38 +0200263 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700264 int i;
265
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100266 for (i = 0; i != gdev->ngpio; ++i) {
267 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab02015-09-23 16:20:43 -0700268
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100269 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700270 continue;
271
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100272 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700273 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100274 return desc;
Linus Walleijf881bab02015-09-23 16:20:43 -0700275 }
276 }
277 }
278
279 spin_unlock_irqrestore(&gpio_lock, flags);
280
281 return NULL;
282}
283
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200284/*
285 * Takes the names from gc->names and checks if they are all unique. If they
286 * are, they are assigned to their gpio descriptors.
287 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800288 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200289 */
290static int gpiochip_set_desc_names(struct gpio_chip *gc)
291{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100292 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200293 int i;
294
295 if (!gc->names)
296 return 0;
297
298 /* First check all names if they are unique */
299 for (i = 0; i != gc->ngpio; ++i) {
300 struct gpio_desc *gpio;
301
302 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab02015-09-23 16:20:43 -0700303 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100304 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200305 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab02015-09-23 16:20:43 -0700306 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200307 }
308
309 /* Then add all names to the GPIO descriptors */
310 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100311 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200312
313 return 0;
314}
315
Linus Walleijd7c51b42016-04-26 10:35:29 +0200316/*
317 * GPIO line handle management
318 */
319
320/**
321 * struct linehandle_state - contains the state of a userspace handle
322 * @gdev: the GPIO device the handle pertains to
323 * @label: consumer label used to tag descriptors
324 * @descs: the GPIO descriptors held by this handle
325 * @numdescs: the number of descriptors held in the descs array
326 */
327struct linehandle_state {
328 struct gpio_device *gdev;
329 const char *label;
330 struct gpio_desc *descs[GPIOHANDLES_MAX];
331 u32 numdescs;
332};
333
334static long linehandle_ioctl(struct file *filep, unsigned int cmd,
335 unsigned long arg)
336{
337 struct linehandle_state *lh = filep->private_data;
338 void __user *ip = (void __user *)arg;
339 struct gpiohandle_data ghd;
340 int i;
341
342 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
343 int val;
344
345 /* TODO: check if descriptors are really input */
346 for (i = 0; i < lh->numdescs; i++) {
347 val = gpiod_get_value_cansleep(lh->descs[i]);
348 if (val < 0)
349 return val;
350 ghd.values[i] = val;
351 }
352
353 if (copy_to_user(ip, &ghd, sizeof(ghd)))
354 return -EFAULT;
355
356 return 0;
357 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
358 int vals[GPIOHANDLES_MAX];
359
360 /* TODO: check if descriptors are really output */
361 if (copy_from_user(&ghd, ip, sizeof(ghd)))
362 return -EFAULT;
363
364 /* Clamp all values to [0,1] */
365 for (i = 0; i < lh->numdescs; i++)
366 vals[i] = !!ghd.values[i];
367
368 /* Reuse the array setting function */
369 gpiod_set_array_value_complex(false,
370 true,
371 lh->numdescs,
372 lh->descs,
373 vals);
374 return 0;
375 }
376 return -EINVAL;
377}
378
379#ifdef CONFIG_COMPAT
380static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
381 unsigned long arg)
382{
383 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
384}
385#endif
386
387static int linehandle_release(struct inode *inode, struct file *filep)
388{
389 struct linehandle_state *lh = filep->private_data;
390 struct gpio_device *gdev = lh->gdev;
391 int i;
392
393 for (i = 0; i < lh->numdescs; i++)
394 gpiod_free(lh->descs[i]);
395 kfree(lh->label);
396 kfree(lh);
397 put_device(&gdev->dev);
398 return 0;
399}
400
401static const struct file_operations linehandle_fileops = {
402 .release = linehandle_release,
403 .owner = THIS_MODULE,
404 .llseek = noop_llseek,
405 .unlocked_ioctl = linehandle_ioctl,
406#ifdef CONFIG_COMPAT
407 .compat_ioctl = linehandle_ioctl_compat,
408#endif
409};
410
411static int linehandle_create(struct gpio_device *gdev, void __user *ip)
412{
413 struct gpiohandle_request handlereq;
414 struct linehandle_state *lh;
415 int fd, i, ret;
416
417 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
418 return -EFAULT;
419 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
420 return -EINVAL;
421
422 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
423 if (!lh)
424 return -ENOMEM;
425 lh->gdev = gdev;
426 get_device(&gdev->dev);
427
428 /* Make sure this is terminated */
429 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
430 if (strlen(handlereq.consumer_label)) {
431 lh->label = kstrdup(handlereq.consumer_label,
432 GFP_KERNEL);
433 if (!lh->label) {
434 ret = -ENOMEM;
435 goto out_free_lh;
436 }
437 }
438
439 /* Request each GPIO */
440 for (i = 0; i < handlereq.lines; i++) {
441 u32 offset = handlereq.lineoffsets[i];
442 u32 lflags = handlereq.flags;
443 struct gpio_desc *desc;
444
445 desc = &gdev->descs[offset];
446 ret = gpiod_request(desc, lh->label);
447 if (ret)
448 goto out_free_descs;
449 lh->descs[i] = desc;
450
451 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
452 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
453 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
454 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
455 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
456 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
457
458 /*
459 * Lines have to be requested explicitly for input
460 * or output, else the line will be treated "as is".
461 */
462 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
463 int val = !!handlereq.default_values[i];
464
465 ret = gpiod_direction_output(desc, val);
466 if (ret)
467 goto out_free_descs;
468 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
469 ret = gpiod_direction_input(desc);
470 if (ret)
471 goto out_free_descs;
472 }
473 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
474 offset);
475 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200476 /* Let i point at the last handle */
477 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200478 lh->numdescs = handlereq.lines;
479
480 fd = anon_inode_getfd("gpio-linehandle",
481 &linehandle_fileops,
482 lh,
483 O_RDONLY | O_CLOEXEC);
484 if (fd < 0) {
485 ret = fd;
486 goto out_free_descs;
487 }
488
489 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200490 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
491 ret = -EFAULT;
492 goto out_free_descs;
493 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200494
495 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
496 lh->numdescs);
497
498 return 0;
499
500out_free_descs:
501 for (; i >= 0; i--)
502 gpiod_free(lh->descs[i]);
503 kfree(lh->label);
504out_free_lh:
505 kfree(lh);
506 put_device(&gdev->dev);
507 return ret;
508}
509
Linus Walleij61f922d2016-06-02 11:30:15 +0200510/*
511 * GPIO line event management
512 */
513
514/**
515 * struct lineevent_state - contains the state of a userspace event
516 * @gdev: the GPIO device the event pertains to
517 * @label: consumer label used to tag descriptors
518 * @desc: the GPIO descriptor held by this event
519 * @eflags: the event flags this line was requested with
520 * @irq: the interrupt that trigger in response to events on this GPIO
521 * @wait: wait queue that handles blocking reads of events
522 * @events: KFIFO for the GPIO events
523 * @read_lock: mutex lock to protect reads from colliding with adding
524 * new events to the FIFO
525 */
526struct lineevent_state {
527 struct gpio_device *gdev;
528 const char *label;
529 struct gpio_desc *desc;
530 u32 eflags;
531 int irq;
532 wait_queue_head_t wait;
533 DECLARE_KFIFO(events, struct gpioevent_data, 16);
534 struct mutex read_lock;
535};
536
537static unsigned int lineevent_poll(struct file *filep,
538 struct poll_table_struct *wait)
539{
540 struct lineevent_state *le = filep->private_data;
541 unsigned int events = 0;
542
543 poll_wait(filep, &le->wait, wait);
544
545 if (!kfifo_is_empty(&le->events))
546 events = POLLIN | POLLRDNORM;
547
548 return events;
549}
550
551
552static ssize_t lineevent_read(struct file *filep,
553 char __user *buf,
554 size_t count,
555 loff_t *f_ps)
556{
557 struct lineevent_state *le = filep->private_data;
558 unsigned int copied;
559 int ret;
560
561 if (count < sizeof(struct gpioevent_data))
562 return -EINVAL;
563
564 do {
565 if (kfifo_is_empty(&le->events)) {
566 if (filep->f_flags & O_NONBLOCK)
567 return -EAGAIN;
568
569 ret = wait_event_interruptible(le->wait,
570 !kfifo_is_empty(&le->events));
571 if (ret)
572 return ret;
573 }
574
575 if (mutex_lock_interruptible(&le->read_lock))
576 return -ERESTARTSYS;
577 ret = kfifo_to_user(&le->events, buf, count, &copied);
578 mutex_unlock(&le->read_lock);
579
580 if (ret)
581 return ret;
582
583 /*
584 * If we couldn't read anything from the fifo (a different
585 * thread might have been faster) we either return -EAGAIN if
586 * the file descriptor is non-blocking, otherwise we go back to
587 * sleep and wait for more data to arrive.
588 */
589 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
590 return -EAGAIN;
591
592 } while (copied == 0);
593
594 return copied;
595}
596
597static int lineevent_release(struct inode *inode, struct file *filep)
598{
599 struct lineevent_state *le = filep->private_data;
600 struct gpio_device *gdev = le->gdev;
601
602 free_irq(le->irq, le);
603 gpiod_free(le->desc);
604 kfree(le->label);
605 kfree(le);
606 put_device(&gdev->dev);
607 return 0;
608}
609
610static long lineevent_ioctl(struct file *filep, unsigned int cmd,
611 unsigned long arg)
612{
613 struct lineevent_state *le = filep->private_data;
614 void __user *ip = (void __user *)arg;
615 struct gpiohandle_data ghd;
616
617 /*
618 * We can get the value for an event line but not set it,
619 * because it is input by definition.
620 */
621 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
622 int val;
623
624 val = gpiod_get_value_cansleep(le->desc);
625 if (val < 0)
626 return val;
627 ghd.values[0] = val;
628
629 if (copy_to_user(ip, &ghd, sizeof(ghd)))
630 return -EFAULT;
631
632 return 0;
633 }
634 return -EINVAL;
635}
636
637#ifdef CONFIG_COMPAT
638static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
639 unsigned long arg)
640{
641 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
642}
643#endif
644
645static const struct file_operations lineevent_fileops = {
646 .release = lineevent_release,
647 .read = lineevent_read,
648 .poll = lineevent_poll,
649 .owner = THIS_MODULE,
650 .llseek = noop_llseek,
651 .unlocked_ioctl = lineevent_ioctl,
652#ifdef CONFIG_COMPAT
653 .compat_ioctl = lineevent_ioctl_compat,
654#endif
655};
656
Ben Dooks33265b12016-06-17 16:03:13 +0100657static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200658{
659 struct lineevent_state *le = p;
660 struct gpioevent_data ge;
661 int ret;
662
663 ge.timestamp = ktime_get_real_ns();
664
665 if (le->eflags & GPIOEVENT_REQUEST_BOTH_EDGES) {
666 int level = gpiod_get_value_cansleep(le->desc);
667
668 if (level)
669 /* Emit low-to-high event */
670 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
671 else
672 /* Emit high-to-low event */
673 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
674 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
675 /* Emit low-to-high event */
676 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
677 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
678 /* Emit high-to-low event */
679 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200680 } else {
681 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200682 }
683
684 ret = kfifo_put(&le->events, ge);
685 if (ret != 0)
686 wake_up_poll(&le->wait, POLLIN);
687
688 return IRQ_HANDLED;
689}
690
691static int lineevent_create(struct gpio_device *gdev, void __user *ip)
692{
693 struct gpioevent_request eventreq;
694 struct lineevent_state *le;
695 struct gpio_desc *desc;
696 u32 offset;
697 u32 lflags;
698 u32 eflags;
699 int fd;
700 int ret;
701 int irqflags = 0;
702
703 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
704 return -EFAULT;
705
706 le = kzalloc(sizeof(*le), GFP_KERNEL);
707 if (!le)
708 return -ENOMEM;
709 le->gdev = gdev;
710 get_device(&gdev->dev);
711
712 /* Make sure this is terminated */
713 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
714 if (strlen(eventreq.consumer_label)) {
715 le->label = kstrdup(eventreq.consumer_label,
716 GFP_KERNEL);
717 if (!le->label) {
718 ret = -ENOMEM;
719 goto out_free_le;
720 }
721 }
722
723 offset = eventreq.lineoffset;
724 lflags = eventreq.handleflags;
725 eflags = eventreq.eventflags;
726
727 /* This is just wrong: we don't look for events on output lines */
728 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
729 ret = -EINVAL;
730 goto out_free_label;
731 }
732
733 desc = &gdev->descs[offset];
734 ret = gpiod_request(desc, le->label);
735 if (ret)
736 goto out_free_desc;
737 le->desc = desc;
738 le->eflags = eflags;
739
740 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
741 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
742 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
743 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
744 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
745 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
746
747 ret = gpiod_direction_input(desc);
748 if (ret)
749 goto out_free_desc;
750
751 le->irq = gpiod_to_irq(desc);
752 if (le->irq <= 0) {
753 ret = -ENODEV;
754 goto out_free_desc;
755 }
756
757 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
758 irqflags |= IRQF_TRIGGER_RISING;
759 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
760 irqflags |= IRQF_TRIGGER_FALLING;
761 irqflags |= IRQF_ONESHOT;
762 irqflags |= IRQF_SHARED;
763
764 INIT_KFIFO(le->events);
765 init_waitqueue_head(&le->wait);
766 mutex_init(&le->read_lock);
767
768 /* Request a thread to read the events */
769 ret = request_threaded_irq(le->irq,
770 NULL,
771 lineevent_irq_thread,
772 irqflags,
773 le->label,
774 le);
775 if (ret)
776 goto out_free_desc;
777
778 fd = anon_inode_getfd("gpio-event",
779 &lineevent_fileops,
780 le,
781 O_RDONLY | O_CLOEXEC);
782 if (fd < 0) {
783 ret = fd;
784 goto out_free_irq;
785 }
786
787 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200788 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
789 ret = -EFAULT;
790 goto out_free_irq;
791 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200792
793 return 0;
794
795out_free_irq:
796 free_irq(le->irq, le);
797out_free_desc:
798 gpiod_free(le->desc);
799out_free_label:
800 kfree(le->label);
801out_free_le:
802 kfree(le);
803 put_device(&gdev->dev);
804 return ret;
805}
806
Linus Walleij3c702e92015-10-21 15:29:53 +0200807/**
808 * gpio_ioctl() - ioctl handler for the GPIO chardev
809 */
810static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
811{
812 struct gpio_device *gdev = filp->private_data;
813 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200814 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200815
816 /* We fail any subsequent ioctl():s when the chip is gone */
817 if (!chip)
818 return -ENODEV;
819
Linus Walleij521a2ad2016-02-12 22:25:22 +0100820 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200821 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100822 struct gpiochip_info chipinfo;
823
Linus Walleij3c702e92015-10-21 15:29:53 +0200824 strncpy(chipinfo.name, dev_name(&gdev->dev),
825 sizeof(chipinfo.name));
826 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100827 strncpy(chipinfo.label, gdev->label,
828 sizeof(chipinfo.label));
829 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100830 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200831 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
832 return -EFAULT;
833 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100834 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
835 struct gpioline_info lineinfo;
836 struct gpio_desc *desc;
837
838 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
839 return -EFAULT;
840 if (lineinfo.line_offset > gdev->ngpio)
841 return -EINVAL;
842
843 desc = &gdev->descs[lineinfo.line_offset];
844 if (desc->name) {
845 strncpy(lineinfo.name, desc->name,
846 sizeof(lineinfo.name));
847 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
848 } else {
849 lineinfo.name[0] = '\0';
850 }
851 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100852 strncpy(lineinfo.consumer, desc->label,
853 sizeof(lineinfo.consumer));
854 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100855 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100856 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100857 }
858
859 /*
860 * Userspace only need to know that the kernel is using
861 * this GPIO so it can't use it.
862 */
863 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100864 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
865 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
866 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
867 test_bit(FLAG_EXPORT, &desc->flags) ||
868 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100869 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100870 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100871 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100872 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100873 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100874 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100875 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100876 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100877 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
878
879 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
880 return -EFAULT;
881 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200882 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
883 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +0200884 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
885 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +0200886 }
887 return -EINVAL;
888}
889
Linus Walleij8b92e172016-05-27 14:24:04 +0200890#ifdef CONFIG_COMPAT
891static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
892 unsigned long arg)
893{
894 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
895}
896#endif
897
Linus Walleij3c702e92015-10-21 15:29:53 +0200898/**
899 * gpio_chrdev_open() - open the chardev for ioctl operations
900 * @inode: inode for this chardev
901 * @filp: file struct for storing private data
902 * Returns 0 on success
903 */
904static int gpio_chrdev_open(struct inode *inode, struct file *filp)
905{
906 struct gpio_device *gdev = container_of(inode->i_cdev,
907 struct gpio_device, chrdev);
908
909 /* Fail on open if the backing gpiochip is gone */
910 if (!gdev || !gdev->chip)
911 return -ENODEV;
912 get_device(&gdev->dev);
913 filp->private_data = gdev;
914 return 0;
915}
916
917/**
918 * gpio_chrdev_release() - close chardev after ioctl operations
919 * @inode: inode for this chardev
920 * @filp: file struct for storing private data
921 * Returns 0 on success
922 */
923static int gpio_chrdev_release(struct inode *inode, struct file *filp)
924{
925 struct gpio_device *gdev = container_of(inode->i_cdev,
926 struct gpio_device, chrdev);
927
928 if (!gdev)
929 return -ENODEV;
930 put_device(&gdev->dev);
931 return 0;
932}
933
934
935static const struct file_operations gpio_fileops = {
936 .release = gpio_chrdev_release,
937 .open = gpio_chrdev_open,
938 .owner = THIS_MODULE,
939 .llseek = noop_llseek,
940 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +0200941#ifdef CONFIG_COMPAT
942 .compat_ioctl = gpio_ioctl_compat,
943#endif
Linus Walleij3c702e92015-10-21 15:29:53 +0200944};
945
Linus Walleijff2b1352015-10-20 11:10:38 +0200946static void gpiodevice_release(struct device *dev)
947{
948 struct gpio_device *gdev = dev_get_drvdata(dev);
949
950 list_del(&gdev->list);
951 ida_simple_remove(&gpio_ida, gdev->id);
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700952 kfree(gdev->label);
953 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +0100954 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200955}
956
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700957static int gpiochip_setup_dev(struct gpio_device *gdev)
958{
959 int status;
960
961 cdev_init(&gdev->chrdev, &gpio_fileops);
962 gdev->chrdev.owner = THIS_MODULE;
963 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
964 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
965 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
966 if (status < 0)
967 chip_warn(gdev->chip, "failed to add char device %d:%d\n",
968 MAJOR(gpio_devt), gdev->id);
969 else
970 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
971 MAJOR(gpio_devt), gdev->id);
972 status = device_add(&gdev->dev);
973 if (status)
974 goto err_remove_chardev;
975
976 status = gpiochip_sysfs_register(gdev);
977 if (status)
978 goto err_remove_device;
979
980 /* From this point, the .release() function cleans up gpio_device */
981 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700982 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
983 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
984 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
985
986 return 0;
987
988err_remove_device:
989 device_del(&gdev->dev);
990err_remove_chardev:
991 cdev_del(&gdev->chrdev);
992 return status;
993}
994
995static void gpiochip_setup_devs(void)
996{
997 struct gpio_device *gdev;
998 int err;
999
1000 list_for_each_entry(gdev, &gpio_devices, list) {
1001 err = gpiochip_setup_dev(gdev);
1002 if (err)
1003 pr_err("%s: Failed to initialize gpio device (%d)\n",
1004 dev_name(&gdev->dev), err);
1005 }
1006}
1007
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001008/**
Linus Walleijb08ea352015-12-03 15:14:13 +01001009 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -08001010 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001011 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -08001012 *
1013 * Returns a negative errno if the chip can't be registered, such as
1014 * because the chip->base is invalid or already associated with a
1015 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001016 *
Linus Walleijb08ea352015-12-03 15:14:13 +01001017 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001018 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d82008-07-25 01:46:07 -07001019 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1020 * for GPIOs will fail rudely.
1021 *
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001022 * gpiochip_add_data() must only be called after gpiolib initialization,
1023 * ie after core_initcall().
1024 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001025 * If chip->base is negative, this requests dynamic assignment of
1026 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001027 */
Linus Walleijb08ea352015-12-03 15:14:13 +01001028int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -08001029{
1030 unsigned long flags;
1031 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001032 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001033 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001034 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001035
Linus Walleijff2b1352015-10-20 11:10:38 +02001036 /*
1037 * First: allocate and populate the internal stat container, and
1038 * set up the struct device.
1039 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001040 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001041 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001042 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001043 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001044 gdev->chip = chip;
1045 chip->gpiodev = gdev;
1046 if (chip->parent) {
1047 gdev->dev.parent = chip->parent;
1048 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001049 }
1050
Linus Walleijff2b1352015-10-20 11:10:38 +02001051#ifdef CONFIG_OF_GPIO
1052 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001053 if (chip->of_node)
1054 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001055#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001056
Linus Walleijff2b1352015-10-20 11:10:38 +02001057 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1058 if (gdev->id < 0) {
1059 status = gdev->id;
1060 goto err_free_gdev;
1061 }
1062 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1063 device_initialize(&gdev->dev);
1064 dev_set_drvdata(&gdev->dev, gdev);
1065 if (chip->parent && chip->parent->driver)
1066 gdev->owner = chip->parent->driver->owner;
1067 else if (chip->owner)
1068 /* TODO: remove chip->owner */
1069 gdev->owner = chip->owner;
1070 else
1071 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001072
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001073 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001074 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001075 status = -ENOMEM;
1076 goto err_free_gdev;
1077 }
1078
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001079 if (chip->ngpio == 0) {
1080 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001081 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001082 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001083 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001084
1085 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001086 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001087 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001088 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001089 if (!gdev->label) {
1090 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001091 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001092 }
1093
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001094 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001095 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001096
David Brownelld2876d02008-02-04 22:28:20 -08001097 spin_lock_irqsave(&gpio_lock, flags);
1098
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001099 /*
1100 * TODO: this allocates a Linux GPIO number base in the global
1101 * GPIO numberspace for this chip. In the long run we want to
1102 * get *rid* of this numberspace and use only descriptors, but
1103 * it may be a pipe dream. It will not happen before we get rid
1104 * of the sysfs interface anyways.
1105 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001106 if (base < 0) {
1107 base = gpiochip_find_base(chip->ngpio);
1108 if (base < 0) {
1109 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001110 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001111 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001112 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001113 /*
1114 * TODO: it should not be necessary to reflect the assigned
1115 * base outside of the GPIO subsystem. Go over drivers and
1116 * see if anyone makes use of this, else drop this and assign
1117 * a poison instead.
1118 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001119 chip->base = base;
1120 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001121 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001122
Linus Walleijff2b1352015-10-20 11:10:38 +02001123 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001124 if (status) {
1125 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001126 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001127 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001128
Linus Walleij545ebd92016-05-30 17:11:59 +02001129 spin_unlock_irqrestore(&gpio_lock, flags);
1130
Linus Walleijff2b1352015-10-20 11:10:38 +02001131 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001132 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001133
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001134 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +02001135 /*
1136 * REVISIT: most hardware initializes GPIOs as inputs
1137 * (often with pullups enabled) so power usage is
1138 * minimized. Linux code should set the gpio direction
1139 * first thing; but until it does, and in case
1140 * chip->get_direction is not set, we may expose the
1141 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001142 */
Linus Walleij72d32002016-04-28 13:33:59 +02001143
1144 if (chip->get_direction) {
1145 /*
1146 * If we have .get_direction, set up the initial
1147 * direction flag from the hardware.
1148 */
1149 int dir = chip->get_direction(chip, i);
1150
1151 if (!dir)
1152 set_bit(FLAG_IS_OUT, &desc->flags);
1153 } else if (!chip->direction_input) {
1154 /*
1155 * If the chip lacks the .direction_input callback
1156 * we logically assume all lines are outputs.
1157 */
1158 set_bit(FLAG_IS_OUT, &desc->flags);
1159 }
David Brownelld2876d02008-02-04 22:28:20 -08001160 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001161
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301162#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001163 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301164#endif
1165
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001166 status = gpiochip_set_desc_names(chip);
1167 if (status)
1168 goto err_remove_from_list;
1169
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001170 status = of_gpiochip_add(chip);
1171 if (status)
1172 goto err_remove_chip;
1173
Mika Westerberg664e3e52014-01-08 12:40:54 +02001174 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001175
Linus Walleij3c702e92015-10-21 15:29:53 +02001176 /*
1177 * By first adding the chardev, and then adding the device,
1178 * we get a device node entry in sysfs under
1179 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1180 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001181 * We can do this only if gpiolib has been initialized.
1182 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001183 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001184 if (gpiolib_initialized) {
1185 status = gpiochip_setup_dev(gdev);
1186 if (status)
1187 goto err_remove_chip;
1188 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001189 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001190
Johan Hovold225fce82015-01-12 17:12:25 +01001191err_remove_chip:
1192 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001193 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001194 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001195err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001196 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001197 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001198 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001199err_free_label:
1200 kfree(gdev->label);
1201err_free_descs:
1202 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001203err_free_gdev:
1204 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001205 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001206 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001207 gdev->base, gdev->base + gdev->ngpio - 1,
1208 chip->label ? : "generic");
1209 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001210 return status;
1211}
Linus Walleijb08ea352015-12-03 15:14:13 +01001212EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -08001213
1214/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001215 * gpiochip_get_data() - get per-subdriver data for the chip
1216 */
1217void *gpiochip_get_data(struct gpio_chip *chip)
1218{
1219 return chip->gpiodev->data;
1220}
1221EXPORT_SYMBOL_GPL(gpiochip_get_data);
1222
1223/**
David Brownelld2876d02008-02-04 22:28:20 -08001224 * gpiochip_remove() - unregister a gpio_chip
1225 * @chip: the chip to unregister
1226 *
1227 * A gpio_chip with any GPIOs still requested may not be removed.
1228 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001229void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001230{
Linus Walleijff2b1352015-10-20 11:10:38 +02001231 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001232 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001233 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001234 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001235 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001236
Linus Walleijff2b1352015-10-20 11:10:38 +02001237 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001238 gpiochip_sysfs_unregister(gdev);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001239 /* Numb the device, cancelling all outstanding operations */
1240 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001241 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001242 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001243 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -06001244 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001245 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001246 /*
1247 * We accept no more calls into the driver from this point, so
1248 * NULL the driver data pointer
1249 */
1250 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001251
Johan Hovold6798aca2015-01-12 17:12:28 +01001252 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001253 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001254 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001255 if (test_bit(FLAG_REQUESTED, &desc->flags))
1256 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001257 }
David Brownelld2876d02008-02-04 22:28:20 -08001258 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001259
Johan Hovoldfab28b82015-05-04 17:10:27 +02001260 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001261 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001262 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001263
Linus Walleijff2b1352015-10-20 11:10:38 +02001264 /*
1265 * The gpiochip side puts its use of the device to rest here:
1266 * if there are no userspace clients, the chardev and device will
1267 * be removed, else it will be dangling until the last user is
1268 * gone.
1269 */
Ricardo Ribalda Delgadof4833b82016-06-03 19:10:02 +02001270 cdev_del(&gdev->chrdev);
1271 device_del(&gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001272 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001273}
1274EXPORT_SYMBOL_GPL(gpiochip_remove);
1275
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301276static void devm_gpio_chip_release(struct device *dev, void *res)
1277{
1278 struct gpio_chip *chip = *(struct gpio_chip **)res;
1279
1280 gpiochip_remove(chip);
1281}
1282
1283static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1284
1285{
1286 struct gpio_chip **r = res;
1287
1288 if (!r || !*r) {
1289 WARN_ON(!r || !*r);
1290 return 0;
1291 }
1292
1293 return *r == data;
1294}
1295
1296/**
1297 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1298 * @dev: the device pointer on which irq_chip belongs to.
1299 * @chip: the chip to register, with chip->base initialized
1300 * Context: potentially before irqs will work
1301 *
1302 * Returns a negative errno if the chip can't be registered, such as
1303 * because the chip->base is invalid or already associated with a
1304 * different chip. Otherwise it returns zero as a success code.
1305 *
1306 * The gpio chip automatically be released when the device is unbound.
1307 */
1308int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1309 void *data)
1310{
1311 struct gpio_chip **ptr;
1312 int ret;
1313
1314 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1315 GFP_KERNEL);
1316 if (!ptr)
1317 return -ENOMEM;
1318
1319 ret = gpiochip_add_data(chip, data);
1320 if (ret < 0) {
1321 devres_free(ptr);
1322 return ret;
1323 }
1324
1325 *ptr = chip;
1326 devres_add(dev, ptr);
1327
1328 return 0;
1329}
1330EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1331
1332/**
1333 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1334 * @dev: device for which which resource was allocated
1335 * @chip: the chip to remove
1336 *
1337 * A gpio_chip with any GPIOs still requested may not be removed.
1338 */
1339void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1340{
1341 int ret;
1342
1343 ret = devres_release(dev, devm_gpio_chip_release,
1344 devm_gpio_chip_match, chip);
1345 if (!ret)
1346 WARN_ON(ret);
1347}
1348EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1349
Grant Likely594fa262010-06-08 07:48:16 -06001350/**
1351 * gpiochip_find() - iterator for locating a specific gpio_chip
1352 * @data: data to pass to match function
1353 * @callback: Callback function to check gpio_chip
1354 *
1355 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1356 * determined by a user supplied @match callback. The callback should return
1357 * 0 if the device doesn't match and non-zero if it does. If the callback is
1358 * non-zero, this function will return to the caller and not iterate over any
1359 * more gpio_chips.
1360 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001361struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001362 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001363 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001364{
Linus Walleijff2b1352015-10-20 11:10:38 +02001365 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001366 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001367 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001368
1369 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001370 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001371 if (gdev->chip && match(gdev->chip, data)) {
1372 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001373 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001374 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001375
Grant Likely594fa262010-06-08 07:48:16 -06001376 spin_unlock_irqrestore(&gpio_lock, flags);
1377
1378 return chip;
1379}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001380EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001381
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001382static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1383{
1384 const char *name = data;
1385
1386 return !strcmp(chip->label, name);
1387}
1388
1389static struct gpio_chip *find_chip_by_name(const char *name)
1390{
1391 return gpiochip_find((void *)name, gpiochip_match_name);
1392}
1393
Linus Walleij14250522014-03-25 10:40:18 +01001394#ifdef CONFIG_GPIOLIB_IRQCHIP
1395
1396/*
1397 * The following is irqchip helper code for gpiochips.
1398 */
1399
1400/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001401 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
1402 * @gpiochip: the gpiochip to set the irqchip chain to
1403 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001404 * @parent_irq: the irq number corresponding to the parent IRQ for this
1405 * chained irqchip
1406 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001407 * coming out of the gpiochip. If the interrupt is nested rather than
1408 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001409 */
1410void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1411 struct irq_chip *irqchip,
1412 int parent_irq,
1413 irq_flow_handler_t parent_handler)
1414{
Linus Walleij83141a72014-09-26 13:50:12 +02001415 unsigned int offset;
1416
Linus Walleij83141a72014-09-26 13:50:12 +02001417 if (!gpiochip->irqdomain) {
1418 chip_err(gpiochip, "called %s before setting up irqchip\n",
1419 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001420 return;
1421 }
1422
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001423 if (parent_handler) {
1424 if (gpiochip->can_sleep) {
1425 chip_err(gpiochip,
1426 "you cannot have chained interrupts on a "
1427 "chip that may sleep\n");
1428 return;
1429 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001430 /*
1431 * The parent irqchip is already using the chip_data for this
1432 * irqchip, so our callbacks simply use the handler_data.
1433 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001434 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1435 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001436
1437 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001438 }
Linus Walleij83141a72014-09-26 13:50:12 +02001439
1440 /* Set the parent IRQ for all affected IRQs */
1441 for (offset = 0; offset < gpiochip->ngpio; offset++)
1442 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
1443 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +01001444}
1445EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1446
1447/**
1448 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1449 * @d: the irqdomain used by this irqchip
1450 * @irq: the global irq number used by this GPIO irqchip irq
1451 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1452 *
1453 * This function will set up the mapping for a certain IRQ line on a
1454 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1455 * stored inside the gpiochip.
1456 */
1457static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1458 irq_hw_number_t hwirq)
1459{
1460 struct gpio_chip *chip = d->host_data;
1461
Linus Walleij14250522014-03-25 10:40:18 +01001462 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001463 /*
1464 * This lock class tells lockdep that GPIO irqs are in a different
1465 * category than their parents, so it won't report false recursion.
1466 */
1467 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +02001468 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001469 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +03001470 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001471 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001472 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001473
Linus Walleij1333b902014-04-23 16:45:12 +02001474 /*
1475 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1476 * is passed as default type.
1477 */
1478 if (chip->irq_default_type != IRQ_TYPE_NONE)
1479 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001480
1481 return 0;
1482}
1483
Linus Walleijc3626fd2014-03-28 20:42:01 +01001484static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1485{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001486 struct gpio_chip *chip = d->host_data;
1487
Linus Walleij1c8732b2014-04-09 13:34:39 +02001488 if (chip->can_sleep)
1489 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001490 irq_set_chip_and_handler(irq, NULL, NULL);
1491 irq_set_chip_data(irq, NULL);
1492}
1493
Linus Walleij14250522014-03-25 10:40:18 +01001494static const struct irq_domain_ops gpiochip_domain_ops = {
1495 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001496 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001497 /* Virtually all GPIO irqchips are twocell:ed */
1498 .xlate = irq_domain_xlate_twocell,
1499};
1500
1501static int gpiochip_irq_reqres(struct irq_data *d)
1502{
1503 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1504
Linus Walleijff2b1352015-10-20 11:10:38 +02001505 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001506 return -ENODEV;
1507
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001508 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001509 chip_err(chip,
1510 "unable to lock HW IRQ %lu for IRQ\n",
1511 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001512 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001513 return -EINVAL;
1514 }
1515 return 0;
1516}
1517
1518static void gpiochip_irq_relres(struct irq_data *d)
1519{
1520 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1521
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001522 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001523 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001524}
1525
1526static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1527{
1528 return irq_find_mapping(chip->irqdomain, offset);
1529}
1530
1531/**
1532 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1533 * @gpiochip: the gpiochip to remove the irqchip from
1534 *
1535 * This is called only from gpiochip_remove()
1536 */
1537static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1538{
Linus Walleijc3626fd2014-03-28 20:42:01 +01001539 unsigned int offset;
1540
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001541 acpi_gpiochip_free_interrupts(gpiochip);
1542
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001543 if (gpiochip->irq_parent) {
1544 irq_set_chained_handler(gpiochip->irq_parent, NULL);
1545 irq_set_handler_data(gpiochip->irq_parent, NULL);
1546 }
1547
Linus Walleijc3626fd2014-03-28 20:42:01 +01001548 /* Remove all IRQ mappings and delete the domain */
1549 if (gpiochip->irqdomain) {
1550 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +03001551 irq_dispose_mapping(
1552 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +01001553 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001554 }
Linus Walleij14250522014-03-25 10:40:18 +01001555
1556 if (gpiochip->irqchip) {
1557 gpiochip->irqchip->irq_request_resources = NULL;
1558 gpiochip->irqchip->irq_release_resources = NULL;
1559 gpiochip->irqchip = NULL;
1560 }
1561}
1562
1563/**
1564 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
1565 * @gpiochip: the gpiochip to add the irqchip to
1566 * @irqchip: the irqchip to add to the gpiochip
1567 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1568 * allocate gpiochip irqs from
1569 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001570 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1571 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001572 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001573 *
1574 * This function closely associates a certain irqchip with a certain
1575 * gpiochip, providing an irq domain to translate the local IRQs to
1576 * global irqs in the gpiolib core, and making sure that the gpiochip
1577 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001578 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001579 * from the gpiochip passed as chip data. An irqdomain will be stored
1580 * in the gpiochip that shall be used by the driver to handle IRQ number
1581 * translation. The gpiochip will need to be initialized and registered
1582 * before calling this function.
1583 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001584 * This function will handle two cell:ed simple IRQs and assumes all
1585 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001586 * need to be open coded.
1587 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001588int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
1589 struct irq_chip *irqchip,
1590 unsigned int first_irq,
1591 irq_flow_handler_t handler,
1592 unsigned int type,
1593 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001594{
1595 struct device_node *of_node;
1596 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001597 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001598
1599 if (!gpiochip || !irqchip)
1600 return -EINVAL;
1601
Linus Walleij58383c782015-11-04 09:56:26 +01001602 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001603 pr_err("missing gpiochip .dev parent pointer\n");
1604 return -EINVAL;
1605 }
Linus Walleij58383c782015-11-04 09:56:26 +01001606 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001607#ifdef CONFIG_OF_GPIO
1608 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001609 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001610 * FIXME: get rid of this and use gpiochip->parent->of_node
1611 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001612 */
1613 if (gpiochip->of_node)
1614 of_node = gpiochip->of_node;
1615#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001616 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001617 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001618 * used to configure the interrupts, as you may end-up with
1619 * conflicting triggers. Tell the user, and reset to NONE.
1620 */
1621 if (WARN(of_node && type != IRQ_TYPE_NONE,
1622 "%s: Ignoring %d default trigger\n", of_node->full_name, type))
1623 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001624 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1625 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1626 "Ignoring %d default trigger\n", type);
1627 type = IRQ_TYPE_NONE;
1628 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001629
Linus Walleij14250522014-03-25 10:40:18 +01001630 gpiochip->irqchip = irqchip;
1631 gpiochip->irq_handler = handler;
1632 gpiochip->irq_default_type = type;
1633 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001634 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +01001635 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1636 gpiochip->ngpio, first_irq,
1637 &gpiochip_domain_ops, gpiochip);
1638 if (!gpiochip->irqdomain) {
1639 gpiochip->irqchip = NULL;
1640 return -EINVAL;
1641 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001642
1643 /*
1644 * It is possible for a driver to override this, but only if the
1645 * alternative functions are both implemented.
1646 */
1647 if (!irqchip->irq_request_resources &&
1648 !irqchip->irq_release_resources) {
1649 irqchip->irq_request_resources = gpiochip_irq_reqres;
1650 irqchip->irq_release_resources = gpiochip_irq_relres;
1651 }
Linus Walleij14250522014-03-25 10:40:18 +01001652
1653 /*
1654 * Prepare the mapping since the irqchip shall be orthogonal to
1655 * any gpiochip calls. If the first_irq was zero, this is
1656 * necessary to allocate descriptors for all IRQs.
1657 */
Linus Walleijc3626fd2014-03-28 20:42:01 +01001658 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1659 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
1660 if (offset == 0)
1661 /*
1662 * Store the base into the gpiochip to be used when
1663 * unmapping the irqs.
1664 */
1665 gpiochip->irq_base = irq_base;
1666 }
Linus Walleij14250522014-03-25 10:40:18 +01001667
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001668 acpi_gpiochip_request_interrupts(gpiochip);
1669
Linus Walleij14250522014-03-25 10:40:18 +01001670 return 0;
1671}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001672EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +01001673
1674#else /* CONFIG_GPIOLIB_IRQCHIP */
1675
1676static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
1677
1678#endif /* CONFIG_GPIOLIB_IRQCHIP */
1679
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001680/**
1681 * gpiochip_generic_request() - request the gpio function for a pin
1682 * @chip: the gpiochip owning the GPIO
1683 * @offset: the offset of the GPIO to request for GPIO function
1684 */
1685int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1686{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001687 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001688}
1689EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1690
1691/**
1692 * gpiochip_generic_free() - free the gpio function from a pin
1693 * @chip: the gpiochip to request the gpio function for
1694 * @offset: the offset of the GPIO to free from GPIO function
1695 */
1696void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1697{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001698 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001699}
1700EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1701
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301702#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001703
Linus Walleij3f0f8672012-11-20 12:40:15 +01001704/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001705 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1706 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001707 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001708 * @gpio_offset: the start offset in the current gpio_chip number space
1709 * @pin_group: name of the pin group inside the pin controller
1710 */
1711int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1712 struct pinctrl_dev *pctldev,
1713 unsigned int gpio_offset, const char *pin_group)
1714{
1715 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001716 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001717 int ret;
1718
1719 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1720 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001721 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001722 return -ENOMEM;
1723 }
1724
1725 /* Use local offset as range ID */
1726 pin_range->range.id = gpio_offset;
1727 pin_range->range.gc = chip;
1728 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001729 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001730 pin_range->pctldev = pctldev;
1731
1732 ret = pinctrl_get_group_pins(pctldev, pin_group,
1733 &pin_range->range.pins,
1734 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001735 if (ret < 0) {
1736 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001737 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001738 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001739
1740 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1741
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001742 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1743 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001744 pinctrl_dev_get_devname(pctldev), pin_group);
1745
Linus Walleij20ec3e32016-02-11 11:03:06 +01001746 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001747
1748 return 0;
1749}
1750EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1751
1752/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001753 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1754 * @chip: the gpiochip to add the range for
1755 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001756 * @gpio_offset: the start offset in the current gpio_chip number space
1757 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001758 * @npins: the number of pins from the offset of each pin space (GPIO and
1759 * pin controller) to accumulate in this range
1760 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001761int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001762 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001763 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301764{
1765 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001766 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001767 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301768
Linus Walleij3f0f8672012-11-20 12:40:15 +01001769 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301770 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001771 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001772 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301773 }
1774
Linus Walleij3f0f8672012-11-20 12:40:15 +01001775 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001776 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001777 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301778 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001779 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001780 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301781 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001782 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301783 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001784 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001785 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001786 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001787 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001788 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001789 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001790 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1791 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001792 pinctl_name,
1793 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301794
Linus Walleij20ec3e32016-02-11 11:03:06 +01001795 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001796
1797 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301798}
Linus Walleij165adc92012-11-06 14:49:39 +01001799EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301800
Linus Walleij3f0f8672012-11-20 12:40:15 +01001801/**
1802 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1803 * @chip: the chip to remove all the mappings for
1804 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301805void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1806{
1807 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01001808 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301809
Linus Walleij20ec3e32016-02-11 11:03:06 +01001810 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301811 list_del(&pin_range->node);
1812 pinctrl_remove_gpio_range(pin_range->pctldev,
1813 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001814 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301815 }
1816}
Linus Walleij165adc92012-11-06 14:49:39 +01001817EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1818
1819#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301820
David Brownelld2876d02008-02-04 22:28:20 -08001821/* These "optional" allocation calls help prevent drivers from stomping
1822 * on each other, and help provide better diagnostics in debugfs.
1823 * They're called even less than the "set direction" calls.
1824 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001825static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001826{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001827 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001828 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001829 unsigned long flags;
1830
1831 spin_lock_irqsave(&gpio_lock, flags);
1832
David Brownelld2876d02008-02-04 22:28:20 -08001833 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001834 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001835 */
1836
1837 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1838 desc_set_label(desc, label ? : "?");
1839 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001840 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001841 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001842 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001843 }
1844
1845 if (chip->request) {
1846 /* chip->request may sleep */
1847 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001848 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001849 spin_lock_irqsave(&gpio_lock, flags);
1850
1851 if (status < 0) {
1852 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001853 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001854 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001855 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001856 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001857 if (chip->get_direction) {
1858 /* chip->get_direction may sleep */
1859 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001860 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001861 spin_lock_irqsave(&gpio_lock, flags);
1862 }
David Brownelld2876d02008-02-04 22:28:20 -08001863done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02001864 spin_unlock_irqrestore(&gpio_lock, flags);
1865 return status;
1866}
1867
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001868/*
1869 * This descriptor validation needs to be inserted verbatim into each
1870 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02001871 * macro to avoid endless duplication. If the desc is NULL it is an
1872 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001873 */
1874#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02001875 if (!desc) \
1876 return 0; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02001877 if (IS_ERR(desc)) { \
1878 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
1879 return PTR_ERR(desc); \
1880 } \
Linus Walleij54d77192016-05-30 16:48:39 +02001881 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02001882 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001883 return -EINVAL; \
1884 } \
1885 if ( !desc->gdev->chip ) { \
1886 dev_warn(&desc->gdev->dev, \
1887 "%s: backing chip is gone\n", __func__); \
1888 return 0; \
1889 } } while (0)
1890
1891#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02001892 if (!desc) \
1893 return; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02001894 if (IS_ERR(desc)) { \
1895 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
1896 return; \
1897 } \
Linus Walleij54d77192016-05-30 16:48:39 +02001898 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02001899 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001900 return; \
1901 } \
1902 if (!desc->gdev->chip) { \
1903 dev_warn(&desc->gdev->dev, \
1904 "%s: backing chip is gone\n", __func__); \
1905 return; \
1906 } } while (0)
1907
1908
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001909int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001910{
1911 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001912 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001913
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001914 VALIDATE_DESC(desc);
1915 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001916
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001917 if (try_module_get(gdev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001918 status = __gpiod_request(desc, label);
1919 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001920 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001921 else
1922 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001923 }
1924
David Brownelld2876d02008-02-04 22:28:20 -08001925 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001926 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001927
David Brownelld2876d02008-02-04 22:28:20 -08001928 return status;
1929}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001930
Mika Westerberg77c2d792014-03-10 14:54:50 +02001931static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001932{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001933 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001934 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001935 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001936
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001937 might_sleep();
1938
Alexandre Courbot372e7222013-02-03 01:29:29 +09001939 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07001940
David Brownelld2876d02008-02-04 22:28:20 -08001941 spin_lock_irqsave(&gpio_lock, flags);
1942
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001943 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07001944 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1945 if (chip->free) {
1946 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001947 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001948 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001949 spin_lock_irqsave(&gpio_lock, flags);
1950 }
David Brownelld2876d02008-02-04 22:28:20 -08001951 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001952 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001953 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301954 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301955 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001956 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001957 ret = true;
1958 }
David Brownelld2876d02008-02-04 22:28:20 -08001959
1960 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001961 return ret;
1962}
1963
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001964void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001965{
Linus Walleij33a68e82016-02-11 10:28:44 +01001966 if (desc && desc->gdev && __gpiod_free(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001967 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001968 put_device(&desc->gdev->dev);
1969 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001970 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01001971 }
David Brownelld2876d02008-02-04 22:28:20 -08001972}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001973
David Brownelld2876d02008-02-04 22:28:20 -08001974/**
1975 * gpiochip_is_requested - return string iff signal was requested
1976 * @chip: controller managing the signal
1977 * @offset: of signal within controller's 0..(ngpio - 1) range
1978 *
1979 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001980 * The string returned is the label passed to gpio_request(); if none has been
1981 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001982 *
1983 * This function is for use by GPIO controller drivers. The label can
1984 * help with diagnostics, and knowing that the signal is used as a GPIO
1985 * can help avoid accidentally multiplexing it to another controller.
1986 */
1987const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1988{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001989 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001990
Dirk Behme48b59532015-08-18 18:02:32 +02001991 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001992 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001993
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001994 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001995
Alexandre Courbot372e7222013-02-03 01:29:29 +09001996 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001997 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001998 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001999}
2000EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2001
Mika Westerberg77c2d792014-03-10 14:54:50 +02002002/**
2003 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2004 * @desc: GPIO descriptor to request
2005 * @label: label for the GPIO
2006 *
2007 * Function allows GPIO chip drivers to request and use their own GPIO
2008 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2009 * function will not increase reference count of the GPIO chip module. This
2010 * allows the GPIO chip module to be unloaded as needed (we assume that the
2011 * GPIO chip driver handles freeing the GPIOs it has requested).
2012 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002013struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2014 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002015{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002016 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2017 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002018
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002019 if (IS_ERR(desc)) {
2020 chip_err(chip, "failed to get GPIO descriptor\n");
2021 return desc;
2022 }
2023
2024 err = __gpiod_request(desc, label);
2025 if (err < 0)
2026 return ERR_PTR(err);
2027
2028 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002029}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002030EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002031
2032/**
2033 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2034 * @desc: GPIO descriptor to free
2035 *
2036 * Function frees the given GPIO requested previously with
2037 * gpiochip_request_own_desc().
2038 */
2039void gpiochip_free_own_desc(struct gpio_desc *desc)
2040{
2041 if (desc)
2042 __gpiod_free(desc);
2043}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002044EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002045
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002046/*
2047 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002048 * some cases this is done in early boot, before IRQs are enabled.
2049 *
2050 * As a rule these aren't called more than once (except for drivers
2051 * using the open-drain emulation idiom) so these are natural places
2052 * to accumulate extra debugging checks. Note that we can't (yet)
2053 * rely on gpio_request() having been called beforehand.
2054 */
2055
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002056/**
2057 * gpiod_direction_input - set the GPIO direction to input
2058 * @desc: GPIO to set to input
2059 *
2060 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2061 * be called safely on it.
2062 *
2063 * Return 0 in case of success, else an error code.
2064 */
2065int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002066{
David Brownelld2876d02008-02-04 22:28:20 -08002067 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002068 int status = -EINVAL;
2069
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002070 VALIDATE_DESC(desc);
2071 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002072
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002073 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002074 gpiod_warn(desc,
2075 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002076 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002077 return -EIO;
2078 }
2079
Alexandre Courbotd82da792014-07-22 16:17:43 +09002080 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002081 if (status == 0)
2082 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002083
Alexandre Courbot372e7222013-02-03 01:29:29 +09002084 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002085
David Brownelld2876d02008-02-04 22:28:20 -08002086 return status;
2087}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002088EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002089
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002090static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002091{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002092 struct gpio_chip *gc = desc->gdev->chip;
2093 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002094
Linus Walleijd468bf92013-09-24 11:54:38 +02002095 /* GPIOs used for IRQs shall not be set as output */
2096 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2097 gpiod_err(desc,
2098 "%s: tried to set a GPIO tied to an IRQ as output\n",
2099 __func__);
2100 return -EIO;
2101 }
2102
Linus Walleijc663e5f2016-03-22 10:51:16 +01002103 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2104 /* First see if we can enable open drain in hardware */
2105 if (gc->set_single_ended) {
2106 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2107 LINE_MODE_OPEN_DRAIN);
2108 if (!ret)
2109 goto set_output_value;
2110 }
2111 /* Emulate open drain by not actively driving the line high */
2112 if (value)
2113 return gpiod_direction_input(desc);
2114 }
2115 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2116 if (gc->set_single_ended) {
2117 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2118 LINE_MODE_OPEN_SOURCE);
2119 if (!ret)
2120 goto set_output_value;
2121 }
2122 /* Emulate open source by not actively driving the line low */
2123 if (!value)
2124 return gpiod_direction_input(desc);
2125 } else {
2126 /* Make sure to disable open drain/source hardware, if any */
2127 if (gc->set_single_ended)
2128 gc->set_single_ended(gc,
2129 gpio_chip_hwgpio(desc),
2130 LINE_MODE_PUSH_PULL);
2131 }
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302132
Linus Walleijc663e5f2016-03-22 10:51:16 +01002133set_output_value:
2134 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002135 gpiod_warn(desc,
2136 "%s: missing set() or direction_output() operations\n",
2137 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002138 return -EIO;
2139 }
2140
Linus Walleijc663e5f2016-03-22 10:51:16 +01002141 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), value);
2142 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002143 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002144 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002145 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2146 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002147}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002148
2149/**
2150 * gpiod_direction_output_raw - set the GPIO direction to output
2151 * @desc: GPIO to set to output
2152 * @value: initial output value of the GPIO
2153 *
2154 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2155 * be called safely on it. The initial value of the output must be specified
2156 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2157 *
2158 * Return 0 in case of success, else an error code.
2159 */
2160int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2161{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002162 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002163 return _gpiod_direction_output_raw(desc, value);
2164}
2165EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2166
2167/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302168 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002169 * @desc: GPIO to set to output
2170 * @value: initial output value of the GPIO
2171 *
2172 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2173 * be called safely on it. The initial value of the output must be specified
2174 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2175 * account.
2176 *
2177 * Return 0 in case of success, else an error code.
2178 */
2179int gpiod_direction_output(struct gpio_desc *desc, int value)
2180{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002181 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002182 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2183 value = !value;
2184 return _gpiod_direction_output_raw(desc, value);
2185}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002186EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002187
Felipe Balbic4b5be92010-05-26 14:42:23 -07002188/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002189 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07002190 * @gpio: the gpio to set debounce time
2191 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002192 *
2193 * returns -ENOTSUPP if the controller does not support setting
2194 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002195 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002196int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002197{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002198 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002199
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002200 VALIDATE_DESC(desc);
2201 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002202 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01002203 gpiod_dbg(desc,
2204 "%s: missing set() or set_debounce() operations\n",
2205 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002206 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002207 }
2208
Alexandre Courbotd82da792014-07-22 16:17:43 +09002209 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002210}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002211EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002212
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002213/**
2214 * gpiod_is_active_low - test whether a GPIO is active-low or not
2215 * @desc: the gpio descriptor to test
2216 *
2217 * Returns 1 if the GPIO is active-low, 0 otherwise.
2218 */
2219int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002220{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002221 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002222 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002223}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002224EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002225
2226/* I/O calls are only valid after configuration completed; the relevant
2227 * "is this a valid GPIO" error checks should already have been done.
2228 *
2229 * "Get" operations are often inlinable as reading a pin value register,
2230 * and masking the relevant bit in that register.
2231 *
2232 * When "set" operations are inlinable, they involve writing that mask to
2233 * one register to set a low value, or a different register to set it high.
2234 * Otherwise locking is needed, so there may be little value to inlining.
2235 *
2236 *------------------------------------------------------------------------
2237 *
2238 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2239 * have requested the GPIO. That can include implicit requesting by
2240 * a direction setting call. Marking a gpio as requested locks its chip
2241 * in memory, guaranteeing that these table lookups need no more locking
2242 * and that gpiochip_remove() will fail.
2243 *
2244 * REVISIT when debugging, consider adding some instrumentation to ensure
2245 * that the GPIO was actually requested.
2246 */
2247
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002248static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002249{
2250 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002251 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002252 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002253
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002254 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002255 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002256 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002257 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002258 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002259 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002260}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002261
David Brownelld2876d02008-02-04 22:28:20 -08002262/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002263 * gpiod_get_raw_value() - return a gpio's raw value
2264 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002265 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002266 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002267 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002268 *
2269 * This function should be called from contexts where we cannot sleep, and will
2270 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002271 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002272int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002273{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002274 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002275 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002276 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002277 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002278}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002279EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002280
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002281/**
2282 * gpiod_get_value() - return a gpio's value
2283 * @desc: gpio whose value will be returned
2284 *
2285 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002286 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002287 *
2288 * This function should be called from contexts where we cannot sleep, and will
2289 * complain if the GPIO chip functions potentially sleep.
2290 */
2291int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002292{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002293 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002294
2295 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002296 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002297 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002298
2299 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002300 if (value < 0)
2301 return value;
2302
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002303 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2304 value = !value;
2305
2306 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002307}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002308EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002309
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302310/*
2311 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002312 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002313 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302314 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002315static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302316{
2317 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002318 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002319 int offset = gpio_chip_hwgpio(desc);
2320
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302321 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002322 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302323 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002324 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302325 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002326 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302327 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002328 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302329 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002330 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302331 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002332 gpiod_err(desc,
2333 "%s: Error in set_value for open drain err %d\n",
2334 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302335}
2336
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302337/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002338 * _gpio_set_open_source_value() - Set the open source gpio's value.
2339 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002340 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302341 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002342static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302343{
2344 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002345 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002346 int offset = gpio_chip_hwgpio(desc);
2347
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302348 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002349 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302350 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002351 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302352 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002353 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302354 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002355 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302356 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002357 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302358 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002359 gpiod_err(desc,
2360 "%s: Error in set_value for open source err %d\n",
2361 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302362}
2363
Alexandre Courbot23600962014-03-11 15:52:09 +09002364static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002365{
2366 struct gpio_chip *chip;
2367
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002368 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002369 trace_gpio_value(desc_to_gpio(desc), 0, value);
2370 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2371 _gpio_set_open_drain_value(desc, value);
2372 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2373 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302374 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002375 chip->set(chip, gpio_chip_hwgpio(desc), value);
2376}
2377
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002378/*
2379 * set multiple outputs on the same chip;
2380 * use the chip's set_multiple function if available;
2381 * otherwise set the outputs sequentially;
2382 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2383 * defines which outputs are to be changed
2384 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2385 * defines the values the outputs specified by mask are to be set to
2386 */
2387static void gpio_chip_set_multiple(struct gpio_chip *chip,
2388 unsigned long *mask, unsigned long *bits)
2389{
2390 if (chip->set_multiple) {
2391 chip->set_multiple(chip, mask, bits);
2392 } else {
2393 int i;
2394 for (i = 0; i < chip->ngpio; i++) {
2395 if (mask[BIT_WORD(i)] == 0) {
2396 /* no more set bits in this mask word;
2397 * skip ahead to the next word */
2398 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
2399 continue;
2400 }
2401 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002402 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002403 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002404 }
2405 }
2406}
2407
Linus Walleij44c72882016-04-24 11:36:59 +02002408void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2409 unsigned int array_size,
2410 struct gpio_desc **desc_array,
2411 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002412{
2413 int i = 0;
2414
2415 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002416 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002417 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2418 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2419 int count = 0;
2420
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002421 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002422 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002423
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002424 memset(mask, 0, sizeof(mask));
2425 do {
2426 struct gpio_desc *desc = desc_array[i];
2427 int hwgpio = gpio_chip_hwgpio(desc);
2428 int value = value_array[i];
2429
2430 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2431 value = !value;
2432 trace_gpio_value(desc_to_gpio(desc), 0, value);
2433 /*
2434 * collect all normal outputs belonging to the same chip
2435 * open drain and open source outputs are set individually
2436 */
2437 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002438 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002439 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2440 _gpio_set_open_source_value(desc, value);
2441 } else {
2442 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002443 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002444 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002445 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002446 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002447 count++;
2448 }
2449 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002450 } while ((i < array_size) &&
2451 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002452 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002453 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002454 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002455 }
2456}
2457
David Brownelld2876d02008-02-04 22:28:20 -08002458/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002459 * gpiod_set_raw_value() - assign a gpio's raw value
2460 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002461 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002462 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002463 * Set the raw value of the GPIO, i.e. the value of its physical line without
2464 * regard for its ACTIVE_LOW status.
2465 *
2466 * This function should be called from contexts where we cannot sleep, and will
2467 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002468 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002469void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002470{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002471 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002472 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002473 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002474 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002475}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002476EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002477
2478/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002479 * gpiod_set_value() - assign a gpio's value
2480 * @desc: gpio whose value will be assigned
2481 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002482 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002483 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2484 * account
2485 *
2486 * This function should be called from contexts where we cannot sleep, and will
2487 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002488 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002489void gpiod_set_value(struct gpio_desc *desc, int value)
2490{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002491 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002492 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002493 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002494 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2495 value = !value;
2496 _gpiod_set_raw_value(desc, value);
2497}
2498EXPORT_SYMBOL_GPL(gpiod_set_value);
2499
2500/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002501 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002502 * @array_size: number of elements in the descriptor / value arrays
2503 * @desc_array: array of GPIO descriptors whose values will be assigned
2504 * @value_array: array of values to assign
2505 *
2506 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2507 * without regard for their ACTIVE_LOW status.
2508 *
2509 * This function should be called from contexts where we cannot sleep, and will
2510 * complain if the GPIO chip functions potentially sleep.
2511 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002512void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002513 struct gpio_desc **desc_array, int *value_array)
2514{
2515 if (!desc_array)
2516 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002517 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2518 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002519}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002520EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002521
2522/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002523 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002524 * @array_size: number of elements in the descriptor / value arrays
2525 * @desc_array: array of GPIO descriptors whose values will be assigned
2526 * @value_array: array of values to assign
2527 *
2528 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2529 * into account.
2530 *
2531 * This function should be called from contexts where we cannot sleep, and will
2532 * complain if the GPIO chip functions potentially sleep.
2533 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002534void gpiod_set_array_value(unsigned int array_size,
2535 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002536{
2537 if (!desc_array)
2538 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002539 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2540 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002541}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002542EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002543
2544/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002545 * gpiod_cansleep() - report whether gpio value access may sleep
2546 * @desc: gpio to check
2547 *
2548 */
2549int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002550{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002551 VALIDATE_DESC(desc);
2552 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002553}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002554EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002555
David Brownell0f6d5042008-10-15 22:03:14 -07002556/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002557 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2558 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002559 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002560 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2561 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002562 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002563int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002564{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002565 struct gpio_chip *chip;
2566 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002567
Linus Walleij79bb71b2016-06-15 22:57:38 +02002568 /*
2569 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
2570 * requires this function to not return zero on an invalid descriptor
2571 * but rather a negative error number.
2572 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02002573 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02002574 return -EINVAL;
2575
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002576 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002577 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02002578 if (chip->to_irq) {
2579 int retirq = chip->to_irq(chip, offset);
2580
2581 /* Zero means NO_IRQ */
2582 if (!retirq)
2583 return -ENXIO;
2584
2585 return retirq;
2586 }
2587 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002588}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002589EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002590
Linus Walleijd468bf92013-09-24 11:54:38 +02002591/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002592 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002593 * @chip: the chip the GPIO to lock belongs to
2594 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002595 *
2596 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08002597 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08002598 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002599int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08002600{
Linus Walleij9c102802016-05-25 10:56:03 +02002601 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02002602
Linus Walleij9c102802016-05-25 10:56:03 +02002603 desc = gpiochip_get_desc(chip, offset);
2604 if (IS_ERR(desc))
2605 return PTR_ERR(desc);
2606
2607 /* Flush direction if something changed behind our back */
2608 if (chip->get_direction) {
2609 int dir = chip->get_direction(chip, offset);
2610
2611 if (dir)
2612 clear_bit(FLAG_IS_OUT, &desc->flags);
2613 else
2614 set_bit(FLAG_IS_OUT, &desc->flags);
2615 }
2616
2617 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002618 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02002619 "%s: tried to flag a GPIO set as output for IRQ\n",
2620 __func__);
2621 return -EIO;
2622 }
2623
Linus Walleij9c102802016-05-25 10:56:03 +02002624 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002625 return 0;
2626}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002627EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002628
Linus Walleijd468bf92013-09-24 11:54:38 +02002629/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002630 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002631 * @chip: the chip the GPIO to lock belongs to
2632 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002633 *
2634 * This is used directly by GPIO drivers that want to indicate
2635 * that a certain GPIO is no longer used exclusively for IRQ.
2636 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002637void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02002638{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002639 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02002640 return;
2641
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002642 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002643}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002644EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002645
Linus Walleij6cee3822016-02-11 20:16:45 +01002646bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2647{
2648 if (offset >= chip->ngpio)
2649 return false;
2650
2651 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2652}
2653EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2654
Linus Walleij143b65d2016-02-16 15:41:42 +01002655bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2656{
2657 if (offset >= chip->ngpio)
2658 return false;
2659
2660 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2661}
2662EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2663
2664bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2665{
2666 if (offset >= chip->ngpio)
2667 return false;
2668
2669 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2670}
2671EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2672
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002673/**
2674 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2675 * @desc: gpio whose value will be returned
2676 *
2677 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002678 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002679 *
2680 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002681 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002682int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002683{
David Brownelld2876d02008-02-04 22:28:20 -08002684 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002685 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002686 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002687}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002688EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002689
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002690/**
2691 * gpiod_get_value_cansleep() - return a gpio's value
2692 * @desc: gpio whose value will be returned
2693 *
2694 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002695 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002696 *
2697 * This function is to be called from contexts that can sleep.
2698 */
2699int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002700{
David Brownelld2876d02008-02-04 22:28:20 -08002701 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002702
2703 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002704 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002705 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002706 if (value < 0)
2707 return value;
2708
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002709 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2710 value = !value;
2711
David Brownelld2876d02008-02-04 22:28:20 -08002712 return value;
2713}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002714EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002715
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002716/**
2717 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2718 * @desc: gpio whose value will be assigned
2719 * @value: value to assign
2720 *
2721 * Set the raw value of the GPIO, i.e. the value of its physical line without
2722 * regard for its ACTIVE_LOW status.
2723 *
2724 * This function is to be called from contexts that can sleep.
2725 */
2726void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002727{
David Brownelld2876d02008-02-04 22:28:20 -08002728 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002729 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002730 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002731}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002732EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002733
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002734/**
2735 * gpiod_set_value_cansleep() - assign a gpio's value
2736 * @desc: gpio whose value will be assigned
2737 * @value: value to assign
2738 *
2739 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2740 * account
2741 *
2742 * This function is to be called from contexts that can sleep.
2743 */
2744void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002745{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002746 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002747 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002748 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2749 value = !value;
2750 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002751}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002752EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002753
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002754/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002755 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002756 * @array_size: number of elements in the descriptor / value arrays
2757 * @desc_array: array of GPIO descriptors whose values will be assigned
2758 * @value_array: array of values to assign
2759 *
2760 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2761 * without regard for their ACTIVE_LOW status.
2762 *
2763 * This function is to be called from contexts that can sleep.
2764 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002765void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2766 struct gpio_desc **desc_array,
2767 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002768{
2769 might_sleep_if(extra_checks);
2770 if (!desc_array)
2771 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002772 gpiod_set_array_value_complex(true, true, array_size, desc_array,
2773 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002774}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002775EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002776
2777/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002778 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002779 * @array_size: number of elements in the descriptor / value arrays
2780 * @desc_array: array of GPIO descriptors whose values will be assigned
2781 * @value_array: array of values to assign
2782 *
2783 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2784 * into account.
2785 *
2786 * This function is to be called from contexts that can sleep.
2787 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002788void gpiod_set_array_value_cansleep(unsigned int array_size,
2789 struct gpio_desc **desc_array,
2790 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002791{
2792 might_sleep_if(extra_checks);
2793 if (!desc_array)
2794 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002795 gpiod_set_array_value_complex(false, true, array_size, desc_array,
2796 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002797}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002798EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002799
2800/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002801 * gpiod_add_lookup_table() - register GPIO device consumers
2802 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002803 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002804void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002805{
2806 mutex_lock(&gpio_lookup_lock);
2807
Alexandre Courbotad824782013-12-03 12:20:11 +09002808 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002809
2810 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002811}
2812
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302813/**
2814 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2815 * @table: table of consumers to unregister
2816 */
2817void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2818{
2819 mutex_lock(&gpio_lookup_lock);
2820
2821 list_del(&table->list);
2822
2823 mutex_unlock(&gpio_lookup_lock);
2824}
2825
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002826static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002827 unsigned int idx,
2828 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002829{
2830 char prop_name[32]; /* 32 is max size of property name */
2831 enum of_gpio_flags of_flags;
2832 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02002833 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002834
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002835 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02002836 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01002837 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002838 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02002839 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01002840 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002841 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002842
Thierry Redingdd34c372014-04-23 17:28:09 +02002843 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2844 &of_flags);
Lars-Peter Clausenda17f8a2016-07-01 17:40:09 +02002845 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
Thierry Redingdd34c372014-04-23 17:28:09 +02002846 break;
2847 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002848
2849 if (IS_ERR(desc))
2850 return desc;
2851
2852 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002853 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002854
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002855 if (of_flags & OF_GPIO_SINGLE_ENDED) {
2856 if (of_flags & OF_GPIO_ACTIVE_LOW)
2857 *flags |= GPIO_OPEN_DRAIN;
2858 else
2859 *flags |= GPIO_OPEN_SOURCE;
2860 }
2861
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002862 return desc;
2863}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002864
Dmitry Torokhov25487532016-03-24 10:50:25 -07002865static struct gpio_desc *acpi_find_gpio(struct device *dev,
2866 const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002867 unsigned int idx,
Dmitry Torokhov25487532016-03-24 10:50:25 -07002868 enum gpiod_flags flags,
2869 enum gpio_lookup_flags *lookupflags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03002870{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002871 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03002872 struct acpi_gpio_info info;
2873 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002874 char propname[32];
2875 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03002876
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002877 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002878 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002879 if (con_id && strcmp(con_id, "gpios")) {
2880 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002881 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002882 } else {
2883 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002884 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002885 }
Mika Westerberge01f4402013-10-10 11:01:10 +03002886
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002887 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
2888 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2889 break;
2890 }
2891
2892 /* Then from plain _CRS GPIOs */
2893 if (IS_ERR(desc)) {
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08002894 if (!acpi_can_fallback_to_crs(adev, con_id))
2895 return ERR_PTR(-ENOENT);
2896
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002897 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2898 if (IS_ERR(desc))
2899 return desc;
Dmitry Torokhov25487532016-03-24 10:50:25 -07002900
2901 if ((flags == GPIOD_OUT_LOW || flags == GPIOD_OUT_HIGH) &&
2902 info.gpioint) {
2903 dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
2904 return ERR_PTR(-ENOENT);
2905 }
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002906 }
2907
Christophe RICARD52044722015-12-23 23:25:34 +01002908 if (info.polarity == GPIO_ACTIVE_LOW)
Dmitry Torokhov25487532016-03-24 10:50:25 -07002909 *lookupflags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002910
2911 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002912}
2913
Alexandre Courbotad824782013-12-03 12:20:11 +09002914static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2915{
2916 const char *dev_id = dev ? dev_name(dev) : NULL;
2917 struct gpiod_lookup_table *table;
2918
2919 mutex_lock(&gpio_lookup_lock);
2920
2921 list_for_each_entry(table, &gpio_lookup_list, list) {
2922 if (table->dev_id && dev_id) {
2923 /*
2924 * Valid strings on both ends, must be identical to have
2925 * a match
2926 */
2927 if (!strcmp(table->dev_id, dev_id))
2928 goto found;
2929 } else {
2930 /*
2931 * One of the pointers is NULL, so both must be to have
2932 * a match
2933 */
2934 if (dev_id == table->dev_id)
2935 goto found;
2936 }
2937 }
2938 table = NULL;
2939
2940found:
2941 mutex_unlock(&gpio_lookup_lock);
2942 return table;
2943}
2944
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002945static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002946 unsigned int idx,
2947 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002948{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002949 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002950 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002951 struct gpiod_lookup *p;
2952
Alexandre Courbotad824782013-12-03 12:20:11 +09002953 table = gpiod_find_lookup_table(dev);
2954 if (!table)
2955 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002956
Alexandre Courbotad824782013-12-03 12:20:11 +09002957 for (p = &table->table[0]; p->chip_label; p++) {
2958 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002959
Alexandre Courbotad824782013-12-03 12:20:11 +09002960 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002961 if (p->idx != idx)
2962 continue;
2963
Alexandre Courbotad824782013-12-03 12:20:11 +09002964 /* If the lookup entry has a con_id, require exact match */
2965 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2966 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002967
Alexandre Courbotad824782013-12-03 12:20:11 +09002968 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002969
Alexandre Courbotad824782013-12-03 12:20:11 +09002970 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002971 dev_err(dev, "cannot find GPIO chip %s\n",
2972 p->chip_label);
2973 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002974 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002975
Alexandre Courbotad824782013-12-03 12:20:11 +09002976 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002977 dev_err(dev,
2978 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2979 idx, chip->ngpio, chip->label);
2980 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09002981 }
2982
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09002983 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09002984 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002985
2986 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09002987 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002988
2989 return desc;
2990}
2991
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002992static int dt_gpio_count(struct device *dev, const char *con_id)
2993{
2994 int ret;
2995 char propname[32];
2996 unsigned int i;
2997
2998 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2999 if (con_id)
3000 snprintf(propname, sizeof(propname), "%s-%s",
3001 con_id, gpio_suffixes[i]);
3002 else
3003 snprintf(propname, sizeof(propname), "%s",
3004 gpio_suffixes[i]);
3005
3006 ret = of_gpio_named_count(dev->of_node, propname);
3007 if (ret >= 0)
3008 break;
3009 }
3010 return ret;
3011}
3012
3013static int platform_gpio_count(struct device *dev, const char *con_id)
3014{
3015 struct gpiod_lookup_table *table;
3016 struct gpiod_lookup *p;
3017 unsigned int count = 0;
3018
3019 table = gpiod_find_lookup_table(dev);
3020 if (!table)
3021 return -ENOENT;
3022
3023 for (p = &table->table[0]; p->chip_label; p++) {
3024 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3025 (!con_id && !p->con_id))
3026 count++;
3027 }
3028 if (!count)
3029 return -ENOENT;
3030
3031 return count;
3032}
3033
3034/**
3035 * gpiod_count - return the number of GPIOs associated with a device / function
3036 * or -ENOENT if no GPIO has been assigned to the requested function
3037 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3038 * @con_id: function within the GPIO consumer
3039 */
3040int gpiod_count(struct device *dev, const char *con_id)
3041{
3042 int count = -ENOENT;
3043
3044 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3045 count = dt_gpio_count(dev, con_id);
3046 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3047 count = acpi_gpio_count(dev, con_id);
3048
3049 if (count < 0)
3050 count = platform_gpio_count(dev, con_id);
3051
3052 return count;
3053}
3054EXPORT_SYMBOL_GPL(gpiod_count);
3055
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003056/**
Thierry Reding08791622014-04-25 16:54:22 +02003057 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003058 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003059 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003060 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003061 *
3062 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003063 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003064 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003065 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003066struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003067 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003068{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003069 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003070}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003071EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003072
3073/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003074 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3075 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3076 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003077 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003078 *
3079 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3080 * the requested function it will return NULL. This is convenient for drivers
3081 * that need to handle optional GPIOs.
3082 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003083struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003084 const char *con_id,
3085 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003086{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003087 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003088}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003089EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003090
Benoit Parrotf625d462015-02-02 11:44:44 -06003091
3092/**
3093 * gpiod_configure_flags - helper function to configure a given GPIO
3094 * @desc: gpio whose value will be assigned
3095 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003096 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3097 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003098 * @dflags: gpiod_flags - optional GPIO initialization flags
3099 *
3100 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3101 * requested function and/or index, or another IS_ERR() code if an error
3102 * occurred while trying to acquire the GPIO.
3103 */
3104static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003105 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003106{
3107 int status;
3108
Johan Hovold85b03b32016-07-03 18:32:05 +02003109 if (lflags & GPIO_ACTIVE_LOW)
3110 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3111 if (lflags & GPIO_OPEN_DRAIN)
3112 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3113 if (lflags & GPIO_OPEN_SOURCE)
3114 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3115
Benoit Parrotf625d462015-02-02 11:44:44 -06003116 /* No particular flag request, return here... */
3117 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3118 pr_debug("no flags found for %s\n", con_id);
3119 return 0;
3120 }
3121
3122 /* Process flags */
3123 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3124 status = gpiod_direction_output(desc,
3125 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
3126 else
3127 status = gpiod_direction_input(desc);
3128
3129 return status;
3130}
3131
Thierry Reding29a1f2332014-04-25 17:10:06 +02003132/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003133 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003134 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003135 * @con_id: function within the GPIO consumer
3136 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003137 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003138 *
3139 * This variant of gpiod_get() allows to access GPIOs other than the first
3140 * defined one for functions that define several GPIOs.
3141 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003142 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3143 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003144 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003145 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003146struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003147 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003148 unsigned int idx,
3149 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003150{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003151 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003152 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003153 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003154
3155 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3156
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003157 if (dev) {
3158 /* Using device tree? */
3159 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3160 dev_dbg(dev, "using device tree for GPIO lookup\n");
3161 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3162 } else if (ACPI_COMPANION(dev)) {
3163 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Dmitry Torokhov25487532016-03-24 10:50:25 -07003164 desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003165 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003166 }
3167
3168 /*
3169 * Either we are not using DT or ACPI, or their lookup did not return
3170 * a result. In that case, use platform lookup as a fallback.
3171 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003172 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003173 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003174 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003175 }
3176
3177 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003178 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003179 return desc;
3180 }
3181
3182 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003183 if (status < 0)
3184 return ERR_PTR(status);
3185
Johan Hovold85b03b32016-07-03 18:32:05 +02003186 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003187 if (status < 0) {
3188 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3189 gpiod_put(desc);
3190 return ERR_PTR(status);
3191 }
3192
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003193 return desc;
3194}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003195EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003196
3197/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003198 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3199 * @fwnode: handle of the firmware node
3200 * @propname: name of the firmware property representing the GPIO
3201 *
3202 * This function can be used for drivers that get their configuration
3203 * from firmware.
3204 *
3205 * Function properly finds the corresponding GPIO using whatever is the
3206 * underlying firmware interface and then makes sure that the GPIO
3207 * descriptor is requested before it is returned to the caller.
3208 *
3209 * In case of error an ERR_PTR() is returned.
3210 */
3211struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3212 const char *propname)
3213{
3214 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3215 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003216 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003217 int ret;
3218
3219 if (!fwnode)
3220 return ERR_PTR(-EINVAL);
3221
3222 if (is_of_node(fwnode)) {
3223 enum of_gpio_flags flags;
3224
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02003225 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02003226 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003227 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003228 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003229 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3230 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003231 } else if (is_acpi_node(fwnode)) {
3232 struct acpi_gpio_info info;
3233
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02003234 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02003235 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01003236 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003237 }
3238
3239 if (IS_ERR(desc))
3240 return desc;
3241
Johan Hovold85b03b32016-07-03 18:32:05 +02003242 ret = gpiod_request(desc, NULL);
3243 if (ret)
3244 return ERR_PTR(ret);
3245
Mika Westerberg40b73182014-10-21 13:33:59 +02003246 if (active_low)
3247 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3248
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003249 if (single_ended) {
3250 if (active_low)
3251 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3252 else
3253 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3254 }
3255
Mika Westerberg40b73182014-10-21 13:33:59 +02003256 return desc;
3257}
3258EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3259
3260/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003261 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3262 * function
3263 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3264 * @con_id: function within the GPIO consumer
3265 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003266 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003267 *
3268 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3269 * specified index was assigned to the requested function it will return NULL.
3270 * This is convenient for drivers that need to handle optional GPIOs.
3271 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003272struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003273 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003274 unsigned int index,
3275 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003276{
3277 struct gpio_desc *desc;
3278
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003279 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003280 if (IS_ERR(desc)) {
3281 if (PTR_ERR(desc) == -ENOENT)
3282 return NULL;
3283 }
3284
3285 return desc;
3286}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003287EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003288
3289/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003290 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3291 * @desc: gpio whose value will be assigned
3292 * @name: gpio line name
3293 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3294 * of_get_gpio_hog()
3295 * @dflags: gpiod_flags - optional GPIO initialization flags
3296 */
3297int gpiod_hog(struct gpio_desc *desc, const char *name,
3298 unsigned long lflags, enum gpiod_flags dflags)
3299{
3300 struct gpio_chip *chip;
3301 struct gpio_desc *local_desc;
3302 int hwnum;
3303 int status;
3304
3305 chip = gpiod_to_chip(desc);
3306 hwnum = gpio_chip_hwgpio(desc);
3307
3308 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3309 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303310 status = PTR_ERR(local_desc);
3311 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3312 name, chip->label, hwnum, status);
3313 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003314 }
3315
Johan Hovold85b03b32016-07-03 18:32:05 +02003316 status = gpiod_configure_flags(desc, name, lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003317 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303318 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3319 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003320 gpiochip_free_own_desc(desc);
3321 return status;
3322 }
3323
3324 /* Mark GPIO as hogged so it can be identified and removed later */
3325 set_bit(FLAG_IS_HOGGED, &desc->flags);
3326
3327 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3328 desc_to_gpio(desc), name,
3329 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3330 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3331 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3332
3333 return 0;
3334}
3335
3336/**
3337 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3338 * @chip: gpio chip to act on
3339 *
3340 * This is only used by of_gpiochip_remove to free hogged gpios
3341 */
3342static void gpiochip_free_hogs(struct gpio_chip *chip)
3343{
3344 int id;
3345
3346 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003347 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3348 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003349 }
3350}
3351
3352/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003353 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3354 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3355 * @con_id: function within the GPIO consumer
3356 * @flags: optional GPIO initialization flags
3357 *
3358 * This function acquires all the GPIOs defined under a given function.
3359 *
3360 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3361 * no GPIO has been assigned to the requested function, or another IS_ERR()
3362 * code if an error occurred while trying to acquire the GPIOs.
3363 */
3364struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3365 const char *con_id,
3366 enum gpiod_flags flags)
3367{
3368 struct gpio_desc *desc;
3369 struct gpio_descs *descs;
3370 int count;
3371
3372 count = gpiod_count(dev, con_id);
3373 if (count < 0)
3374 return ERR_PTR(count);
3375
3376 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3377 GFP_KERNEL);
3378 if (!descs)
3379 return ERR_PTR(-ENOMEM);
3380
3381 for (descs->ndescs = 0; descs->ndescs < count; ) {
3382 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3383 if (IS_ERR(desc)) {
3384 gpiod_put_array(descs);
3385 return ERR_CAST(desc);
3386 }
3387 descs->desc[descs->ndescs] = desc;
3388 descs->ndescs++;
3389 }
3390 return descs;
3391}
3392EXPORT_SYMBOL_GPL(gpiod_get_array);
3393
3394/**
3395 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3396 * function
3397 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3398 * @con_id: function within the GPIO consumer
3399 * @flags: optional GPIO initialization flags
3400 *
3401 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3402 * assigned to the requested function it will return NULL.
3403 */
3404struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3405 const char *con_id,
3406 enum gpiod_flags flags)
3407{
3408 struct gpio_descs *descs;
3409
3410 descs = gpiod_get_array(dev, con_id, flags);
3411 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3412 return NULL;
3413
3414 return descs;
3415}
3416EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3417
3418/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003419 * gpiod_put - dispose of a GPIO descriptor
3420 * @desc: GPIO descriptor to dispose of
3421 *
3422 * No descriptor can be used after gpiod_put() has been called on it.
3423 */
3424void gpiod_put(struct gpio_desc *desc)
3425{
3426 gpiod_free(desc);
3427}
3428EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003429
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003430/**
3431 * gpiod_put_array - dispose of multiple GPIO descriptors
3432 * @descs: struct gpio_descs containing an array of descriptors
3433 */
3434void gpiod_put_array(struct gpio_descs *descs)
3435{
3436 unsigned int i;
3437
3438 for (i = 0; i < descs->ndescs; i++)
3439 gpiod_put(descs->desc[i]);
3440
3441 kfree(descs);
3442}
3443EXPORT_SYMBOL_GPL(gpiod_put_array);
3444
Linus Walleij3c702e92015-10-21 15:29:53 +02003445static int __init gpiolib_dev_init(void)
3446{
3447 int ret;
3448
3449 /* Register GPIO sysfs bus */
3450 ret = bus_register(&gpio_bus_type);
3451 if (ret < 0) {
3452 pr_err("gpiolib: could not register GPIO bus type\n");
3453 return ret;
3454 }
3455
3456 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3457 if (ret < 0) {
3458 pr_err("gpiolib: failed to allocate char dev region\n");
3459 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003460 } else {
3461 gpiolib_initialized = true;
3462 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003463 }
3464 return ret;
3465}
3466core_initcall(gpiolib_dev_init);
3467
David Brownelld2876d02008-02-04 22:28:20 -08003468#ifdef CONFIG_DEBUG_FS
3469
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003470static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003471{
3472 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003473 struct gpio_chip *chip = gdev->chip;
3474 unsigned gpio = gdev->base;
3475 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003476 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003477 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003478
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003479 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003480 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3481 if (gdesc->name) {
3482 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3483 gpio, gdesc->name);
3484 }
David Brownelld2876d02008-02-04 22:28:20 -08003485 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003486 }
David Brownelld2876d02008-02-04 22:28:20 -08003487
Alexandre Courbot372e7222013-02-03 01:29:29 +09003488 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003489 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003490 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003491 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3492 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003493 is_out ? "out" : "in ",
3494 chip->get
3495 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003496 : "? ",
3497 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003498 seq_printf(s, "\n");
3499 }
3500}
3501
Thierry Redingf9c4a312012-04-12 13:26:01 +02003502static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003503{
Grant Likely362432a2013-02-09 09:41:49 +00003504 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003505 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003506 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003507
3508 s->private = "";
3509
Grant Likely362432a2013-02-09 09:41:49 +00003510 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003511 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003512 if (index-- == 0) {
3513 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003514 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003515 }
3516 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003517
3518 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003519}
3520
3521static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3522{
Grant Likely362432a2013-02-09 09:41:49 +00003523 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003524 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003525 void *ret = NULL;
3526
Grant Likely362432a2013-02-09 09:41:49 +00003527 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003528 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003529 ret = NULL;
3530 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003531 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003532 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003533
3534 s->private = "\n";
3535 ++*pos;
3536
3537 return ret;
3538}
3539
3540static void gpiolib_seq_stop(struct seq_file *s, void *v)
3541{
3542}
3543
3544static int gpiolib_seq_show(struct seq_file *s, void *v)
3545{
Linus Walleijff2b1352015-10-20 11:10:38 +02003546 struct gpio_device *gdev = v;
3547 struct gpio_chip *chip = gdev->chip;
3548 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003549
Linus Walleijff2b1352015-10-20 11:10:38 +02003550 if (!chip) {
3551 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3552 dev_name(&gdev->dev));
3553 return 0;
3554 }
3555
3556 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3557 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003558 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02003559 parent = chip->parent;
3560 if (parent)
3561 seq_printf(s, ", parent: %s/%s",
3562 parent->bus ? parent->bus->name : "no-bus",
3563 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02003564 if (chip->label)
3565 seq_printf(s, ", %s", chip->label);
3566 if (chip->can_sleep)
3567 seq_printf(s, ", can sleep");
3568 seq_printf(s, ":\n");
3569
3570 if (chip->dbg_show)
3571 chip->dbg_show(s, chip);
3572 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003573 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003574
David Brownelld2876d02008-02-04 22:28:20 -08003575 return 0;
3576}
3577
Thierry Redingf9c4a312012-04-12 13:26:01 +02003578static const struct seq_operations gpiolib_seq_ops = {
3579 .start = gpiolib_seq_start,
3580 .next = gpiolib_seq_next,
3581 .stop = gpiolib_seq_stop,
3582 .show = gpiolib_seq_show,
3583};
3584
David Brownelld2876d02008-02-04 22:28:20 -08003585static int gpiolib_open(struct inode *inode, struct file *file)
3586{
Thierry Redingf9c4a312012-04-12 13:26:01 +02003587 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08003588}
3589
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003590static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02003591 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08003592 .open = gpiolib_open,
3593 .read = seq_read,
3594 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02003595 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08003596};
3597
3598static int __init gpiolib_debugfs_init(void)
3599{
3600 /* /sys/kernel/debug/gpio */
3601 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3602 NULL, NULL, &gpiolib_operations);
3603 return 0;
3604}
3605subsys_initcall(gpiolib_debugfs_init);
3606
3607#endif /* DEBUG_FS */