blob: c826844abdebe91ea39b3c75a88d060f207b38f0 [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d2008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020018#include <linux/pinctrl/consumer.h>
Linus Walleijff2b1352015-10-20 11:10:38 +020019#include <linux/idr.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020020#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020023#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020024#include <linux/anon_inodes.h>
Linus Walleij61f922d2016-06-02 11:30:15 +020025#include <linux/kfifo.h>
26#include <linux/poll.h>
27#include <linux/timekeeping.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020028#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080029
Mika Westerberg664e3e52014-01-08 12:40:54 +020030#include "gpiolib.h"
31
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060032#define CREATE_TRACE_POINTS
33#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080034
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070035/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080036 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070037 * The GPIO programming interface allows for inlining speed-critical
38 * get/set operations for common cases, so that access to SOC-integrated
39 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080040 */
41
42
43/* When debugging, extend minimal trust to callers and platform code.
44 * Also emit diagnostic messages that may help initial bringup, when
45 * board setup or driver bugs are most common.
46 *
47 * Otherwise, minimize overhead in what may be bitbanging codepaths.
48 */
49#ifdef DEBUG
50#define extra_checks 1
51#else
52#define extra_checks 0
53#endif
54
Linus Walleijff2b1352015-10-20 11:10:38 +020055/* Device and char device-related information */
56static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020057static dev_t gpio_devt;
58#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
59static struct bus_type gpio_bus_type = {
60 .name = "gpio",
61};
Linus Walleijff2b1352015-10-20 11:10:38 +020062
David Brownelld2876d02008-02-04 22:28:20 -080063/* gpio_lock prevents conflicts during gpio_desc[] table updates.
64 * While any GPIO is requested, its gpio_chip is not removable;
65 * each GPIO's "requested" flag serves as a lock and refcount.
66 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090067DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080068
Alexandre Courbotbae48da2013-10-17 10:21:38 -070069static DEFINE_MUTEX(gpio_lookup_lock);
70static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020071LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020072
73static void gpiochip_free_hogs(struct gpio_chip *chip);
74static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
75
Guenter Roeck159f3cd2016-03-31 08:11:30 -070076static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020077
David Brownelld2876d02008-02-04 22:28:20 -080078static inline void desc_set_label(struct gpio_desc *d, const char *label)
79{
David Brownelld2876d02008-02-04 22:28:20 -080080 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080081}
82
Alexandre Courbot372e7222013-02-03 01:29:29 +090083/**
84 * Convert a GPIO number to its descriptor
85 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070086struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090087{
Linus Walleijff2b1352015-10-20 11:10:38 +020088 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +090089 unsigned long flags;
90
91 spin_lock_irqsave(&gpio_lock, flags);
92
Linus Walleijff2b1352015-10-20 11:10:38 +020093 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +010094 if (gdev->base <= gpio &&
95 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +090096 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +010097 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +090098 }
99 }
100
101 spin_unlock_irqrestore(&gpio_lock, flags);
102
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900103 if (!gpio_is_valid(gpio))
104 WARN(1, "invalid GPIO %d\n", gpio);
105
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900106 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900107}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700108EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900109
110/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900111 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +0200112 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900113struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
114 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200115{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100116 struct gpio_device *gdev = chip->gpiodev;
117
118 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900119 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200120
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100121 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200122}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900123
124/**
125 * Convert a GPIO descriptor to the integer namespace.
126 * This should disappear in the future but is needed since we still
127 * use GPIO numbers for error messages and sysfs nodes
128 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700129int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900130{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100131 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900132}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700133EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900134
135
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700136/**
137 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
138 * @desc: descriptor to return the chip of
139 */
140struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900141{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100142 if (!desc || !desc->gdev || !desc->gdev->chip)
143 return NULL;
144 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900145}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700146EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800147
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700148/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
149static int gpiochip_find_base(int ngpio)
150{
Linus Walleijff2b1352015-10-20 11:10:38 +0200151 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900152 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700153
Linus Walleijff2b1352015-10-20 11:10:38 +0200154 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900155 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100156 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900157 break;
158 else
159 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100160 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700161 }
162
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900163 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700164 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900165 return base;
166 } else {
167 pr_err("%s: cannot find free range\n", __func__);
168 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700169 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700170}
171
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700172/**
173 * gpiod_get_direction - return the current direction of a GPIO
174 * @desc: GPIO to get the direction of
175 *
176 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
177 *
178 * This function may sleep if gpiod_cansleep() is true.
179 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900180int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300181{
182 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900183 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300184 int status = -EINVAL;
185
Alexandre Courbot372e7222013-02-03 01:29:29 +0900186 chip = gpiod_to_chip(desc);
187 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300188
189 if (!chip->get_direction)
190 return status;
191
Alexandre Courbot372e7222013-02-03 01:29:29 +0900192 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300193 if (status > 0) {
194 /* GPIOF_DIR_IN, or other positive */
195 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900196 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300197 }
198 if (status == 0) {
199 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900200 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300201 }
202 return status;
203}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700204EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300205
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900206/*
207 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800208 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900209 *
210 * Return -EBUSY if the new chip overlaps with some other chip's integer
211 * space.
212 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200213static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900214{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800215 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200216
217 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800218 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200219 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530220 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900221 }
222
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800223 next = list_entry(gpio_devices.next, struct gpio_device, list);
224 if (gdev->base + gdev->ngpio <= next->base) {
225 /* add before first entry */
226 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500227 return 0;
228 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900229
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800230 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
231 if (prev->base + prev->ngpio <= gdev->base) {
232 /* add behind last entry */
233 list_add_tail(&gdev->list, &gpio_devices);
234 return 0;
235 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800236
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800237 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
238 /* at the end of the list */
239 if (&next->list == &gpio_devices)
240 break;
241
242 /* add between prev and next */
243 if (prev->base + prev->ngpio <= gdev->base
244 && gdev->base + gdev->ngpio <= next->base) {
245 list_add(&gdev->list, &prev->list);
246 return 0;
247 }
248 }
249
250 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
251 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900252}
253
Linus Walleijf881bab2015-09-23 16:20:43 -0700254/**
255 * Convert a GPIO name to its descriptor
256 */
257static struct gpio_desc *gpio_name_to_desc(const char * const name)
258{
Linus Walleijff2b1352015-10-20 11:10:38 +0200259 struct gpio_device *gdev;
Linus Walleijf881bab2015-09-23 16:20:43 -0700260 unsigned long flags;
261
262 spin_lock_irqsave(&gpio_lock, flags);
263
Linus Walleijff2b1352015-10-20 11:10:38 +0200264 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700265 int i;
266
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100267 for (i = 0; i != gdev->ngpio; ++i) {
268 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab2015-09-23 16:20:43 -0700269
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100270 if (!desc->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700271 continue;
272
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100273 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700274 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100275 return desc;
Linus Walleijf881bab2015-09-23 16:20:43 -0700276 }
277 }
278 }
279
280 spin_unlock_irqrestore(&gpio_lock, flags);
281
282 return NULL;
283}
284
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200285/*
286 * Takes the names from gc->names and checks if they are all unique. If they
287 * are, they are assigned to their gpio descriptors.
288 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800289 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200290 */
291static int gpiochip_set_desc_names(struct gpio_chip *gc)
292{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100293 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200294 int i;
295
296 if (!gc->names)
297 return 0;
298
299 /* First check all names if they are unique */
300 for (i = 0; i != gc->ngpio; ++i) {
301 struct gpio_desc *gpio;
302
303 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700304 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100305 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200306 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab2015-09-23 16:20:43 -0700307 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200308 }
309
310 /* Then add all names to the GPIO descriptors */
311 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100312 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200313
314 return 0;
315}
316
Linus Walleijd7c51b42016-04-26 10:35:29 +0200317/*
318 * GPIO line handle management
319 */
320
321/**
322 * struct linehandle_state - contains the state of a userspace handle
323 * @gdev: the GPIO device the handle pertains to
324 * @label: consumer label used to tag descriptors
325 * @descs: the GPIO descriptors held by this handle
326 * @numdescs: the number of descriptors held in the descs array
327 */
328struct linehandle_state {
329 struct gpio_device *gdev;
330 const char *label;
331 struct gpio_desc *descs[GPIOHANDLES_MAX];
332 u32 numdescs;
333};
334
335static long linehandle_ioctl(struct file *filep, unsigned int cmd,
336 unsigned long arg)
337{
338 struct linehandle_state *lh = filep->private_data;
339 void __user *ip = (void __user *)arg;
340 struct gpiohandle_data ghd;
341 int i;
342
343 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
344 int val;
345
346 /* TODO: check if descriptors are really input */
347 for (i = 0; i < lh->numdescs; i++) {
348 val = gpiod_get_value_cansleep(lh->descs[i]);
349 if (val < 0)
350 return val;
351 ghd.values[i] = val;
352 }
353
354 if (copy_to_user(ip, &ghd, sizeof(ghd)))
355 return -EFAULT;
356
357 return 0;
358 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
359 int vals[GPIOHANDLES_MAX];
360
361 /* TODO: check if descriptors are really output */
362 if (copy_from_user(&ghd, ip, sizeof(ghd)))
363 return -EFAULT;
364
365 /* Clamp all values to [0,1] */
366 for (i = 0; i < lh->numdescs; i++)
367 vals[i] = !!ghd.values[i];
368
369 /* Reuse the array setting function */
370 gpiod_set_array_value_complex(false,
371 true,
372 lh->numdescs,
373 lh->descs,
374 vals);
375 return 0;
376 }
377 return -EINVAL;
378}
379
380#ifdef CONFIG_COMPAT
381static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
382 unsigned long arg)
383{
384 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
385}
386#endif
387
388static int linehandle_release(struct inode *inode, struct file *filep)
389{
390 struct linehandle_state *lh = filep->private_data;
391 struct gpio_device *gdev = lh->gdev;
392 int i;
393
394 for (i = 0; i < lh->numdescs; i++)
395 gpiod_free(lh->descs[i]);
396 kfree(lh->label);
397 kfree(lh);
398 put_device(&gdev->dev);
399 return 0;
400}
401
402static const struct file_operations linehandle_fileops = {
403 .release = linehandle_release,
404 .owner = THIS_MODULE,
405 .llseek = noop_llseek,
406 .unlocked_ioctl = linehandle_ioctl,
407#ifdef CONFIG_COMPAT
408 .compat_ioctl = linehandle_ioctl_compat,
409#endif
410};
411
412static int linehandle_create(struct gpio_device *gdev, void __user *ip)
413{
414 struct gpiohandle_request handlereq;
415 struct linehandle_state *lh;
416 int fd, i, ret;
417
418 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
419 return -EFAULT;
420 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
421 return -EINVAL;
422
423 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
424 if (!lh)
425 return -ENOMEM;
426 lh->gdev = gdev;
427 get_device(&gdev->dev);
428
429 /* Make sure this is terminated */
430 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
431 if (strlen(handlereq.consumer_label)) {
432 lh->label = kstrdup(handlereq.consumer_label,
433 GFP_KERNEL);
434 if (!lh->label) {
435 ret = -ENOMEM;
436 goto out_free_lh;
437 }
438 }
439
440 /* Request each GPIO */
441 for (i = 0; i < handlereq.lines; i++) {
442 u32 offset = handlereq.lineoffsets[i];
443 u32 lflags = handlereq.flags;
444 struct gpio_desc *desc;
445
446 desc = &gdev->descs[offset];
447 ret = gpiod_request(desc, lh->label);
448 if (ret)
449 goto out_free_descs;
450 lh->descs[i] = desc;
451
452 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
453 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
454 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
455 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
456 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
457 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
458
459 /*
460 * Lines have to be requested explicitly for input
461 * or output, else the line will be treated "as is".
462 */
463 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
464 int val = !!handlereq.default_values[i];
465
466 ret = gpiod_direction_output(desc, val);
467 if (ret)
468 goto out_free_descs;
469 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
470 ret = gpiod_direction_input(desc);
471 if (ret)
472 goto out_free_descs;
473 }
474 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
475 offset);
476 }
477 lh->numdescs = handlereq.lines;
478
479 fd = anon_inode_getfd("gpio-linehandle",
480 &linehandle_fileops,
481 lh,
482 O_RDONLY | O_CLOEXEC);
483 if (fd < 0) {
484 ret = fd;
485 goto out_free_descs;
486 }
487
488 handlereq.fd = fd;
489 if (copy_to_user(ip, &handlereq, sizeof(handlereq)))
490 return -EFAULT;
491
492 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
493 lh->numdescs);
494
495 return 0;
496
497out_free_descs:
498 for (; i >= 0; i--)
499 gpiod_free(lh->descs[i]);
500 kfree(lh->label);
501out_free_lh:
502 kfree(lh);
503 put_device(&gdev->dev);
504 return ret;
505}
506
Linus Walleij61f922d2016-06-02 11:30:15 +0200507/*
508 * GPIO line event management
509 */
510
511/**
512 * struct lineevent_state - contains the state of a userspace event
513 * @gdev: the GPIO device the event pertains to
514 * @label: consumer label used to tag descriptors
515 * @desc: the GPIO descriptor held by this event
516 * @eflags: the event flags this line was requested with
517 * @irq: the interrupt that trigger in response to events on this GPIO
518 * @wait: wait queue that handles blocking reads of events
519 * @events: KFIFO for the GPIO events
520 * @read_lock: mutex lock to protect reads from colliding with adding
521 * new events to the FIFO
522 */
523struct lineevent_state {
524 struct gpio_device *gdev;
525 const char *label;
526 struct gpio_desc *desc;
527 u32 eflags;
528 int irq;
529 wait_queue_head_t wait;
530 DECLARE_KFIFO(events, struct gpioevent_data, 16);
531 struct mutex read_lock;
532};
533
534static unsigned int lineevent_poll(struct file *filep,
535 struct poll_table_struct *wait)
536{
537 struct lineevent_state *le = filep->private_data;
538 unsigned int events = 0;
539
540 poll_wait(filep, &le->wait, wait);
541
542 if (!kfifo_is_empty(&le->events))
543 events = POLLIN | POLLRDNORM;
544
545 return events;
546}
547
548
549static ssize_t lineevent_read(struct file *filep,
550 char __user *buf,
551 size_t count,
552 loff_t *f_ps)
553{
554 struct lineevent_state *le = filep->private_data;
555 unsigned int copied;
556 int ret;
557
558 if (count < sizeof(struct gpioevent_data))
559 return -EINVAL;
560
561 do {
562 if (kfifo_is_empty(&le->events)) {
563 if (filep->f_flags & O_NONBLOCK)
564 return -EAGAIN;
565
566 ret = wait_event_interruptible(le->wait,
567 !kfifo_is_empty(&le->events));
568 if (ret)
569 return ret;
570 }
571
572 if (mutex_lock_interruptible(&le->read_lock))
573 return -ERESTARTSYS;
574 ret = kfifo_to_user(&le->events, buf, count, &copied);
575 mutex_unlock(&le->read_lock);
576
577 if (ret)
578 return ret;
579
580 /*
581 * If we couldn't read anything from the fifo (a different
582 * thread might have been faster) we either return -EAGAIN if
583 * the file descriptor is non-blocking, otherwise we go back to
584 * sleep and wait for more data to arrive.
585 */
586 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
587 return -EAGAIN;
588
589 } while (copied == 0);
590
591 return copied;
592}
593
594static int lineevent_release(struct inode *inode, struct file *filep)
595{
596 struct lineevent_state *le = filep->private_data;
597 struct gpio_device *gdev = le->gdev;
598
599 free_irq(le->irq, le);
600 gpiod_free(le->desc);
601 kfree(le->label);
602 kfree(le);
603 put_device(&gdev->dev);
604 return 0;
605}
606
607static long lineevent_ioctl(struct file *filep, unsigned int cmd,
608 unsigned long arg)
609{
610 struct lineevent_state *le = filep->private_data;
611 void __user *ip = (void __user *)arg;
612 struct gpiohandle_data ghd;
613
614 /*
615 * We can get the value for an event line but not set it,
616 * because it is input by definition.
617 */
618 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
619 int val;
620
621 val = gpiod_get_value_cansleep(le->desc);
622 if (val < 0)
623 return val;
624 ghd.values[0] = val;
625
626 if (copy_to_user(ip, &ghd, sizeof(ghd)))
627 return -EFAULT;
628
629 return 0;
630 }
631 return -EINVAL;
632}
633
634#ifdef CONFIG_COMPAT
635static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
636 unsigned long arg)
637{
638 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
639}
640#endif
641
642static const struct file_operations lineevent_fileops = {
643 .release = lineevent_release,
644 .read = lineevent_read,
645 .poll = lineevent_poll,
646 .owner = THIS_MODULE,
647 .llseek = noop_llseek,
648 .unlocked_ioctl = lineevent_ioctl,
649#ifdef CONFIG_COMPAT
650 .compat_ioctl = lineevent_ioctl_compat,
651#endif
652};
653
654irqreturn_t lineevent_irq_thread(int irq, void *p)
655{
656 struct lineevent_state *le = p;
657 struct gpioevent_data ge;
658 int ret;
659
660 ge.timestamp = ktime_get_real_ns();
661
662 if (le->eflags & GPIOEVENT_REQUEST_BOTH_EDGES) {
663 int level = gpiod_get_value_cansleep(le->desc);
664
665 if (level)
666 /* Emit low-to-high event */
667 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
668 else
669 /* Emit high-to-low event */
670 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
671 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
672 /* Emit low-to-high event */
673 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
674 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
675 /* Emit high-to-low event */
676 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200677 } else {
678 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200679 }
680
681 ret = kfifo_put(&le->events, ge);
682 if (ret != 0)
683 wake_up_poll(&le->wait, POLLIN);
684
685 return IRQ_HANDLED;
686}
687
688static int lineevent_create(struct gpio_device *gdev, void __user *ip)
689{
690 struct gpioevent_request eventreq;
691 struct lineevent_state *le;
692 struct gpio_desc *desc;
693 u32 offset;
694 u32 lflags;
695 u32 eflags;
696 int fd;
697 int ret;
698 int irqflags = 0;
699
700 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
701 return -EFAULT;
702
703 le = kzalloc(sizeof(*le), GFP_KERNEL);
704 if (!le)
705 return -ENOMEM;
706 le->gdev = gdev;
707 get_device(&gdev->dev);
708
709 /* Make sure this is terminated */
710 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
711 if (strlen(eventreq.consumer_label)) {
712 le->label = kstrdup(eventreq.consumer_label,
713 GFP_KERNEL);
714 if (!le->label) {
715 ret = -ENOMEM;
716 goto out_free_le;
717 }
718 }
719
720 offset = eventreq.lineoffset;
721 lflags = eventreq.handleflags;
722 eflags = eventreq.eventflags;
723
724 /* This is just wrong: we don't look for events on output lines */
725 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
726 ret = -EINVAL;
727 goto out_free_label;
728 }
729
730 desc = &gdev->descs[offset];
731 ret = gpiod_request(desc, le->label);
732 if (ret)
733 goto out_free_desc;
734 le->desc = desc;
735 le->eflags = eflags;
736
737 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
738 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
739 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
740 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
741 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
742 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
743
744 ret = gpiod_direction_input(desc);
745 if (ret)
746 goto out_free_desc;
747
748 le->irq = gpiod_to_irq(desc);
749 if (le->irq <= 0) {
750 ret = -ENODEV;
751 goto out_free_desc;
752 }
753
754 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
755 irqflags |= IRQF_TRIGGER_RISING;
756 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
757 irqflags |= IRQF_TRIGGER_FALLING;
758 irqflags |= IRQF_ONESHOT;
759 irqflags |= IRQF_SHARED;
760
761 INIT_KFIFO(le->events);
762 init_waitqueue_head(&le->wait);
763 mutex_init(&le->read_lock);
764
765 /* Request a thread to read the events */
766 ret = request_threaded_irq(le->irq,
767 NULL,
768 lineevent_irq_thread,
769 irqflags,
770 le->label,
771 le);
772 if (ret)
773 goto out_free_desc;
774
775 fd = anon_inode_getfd("gpio-event",
776 &lineevent_fileops,
777 le,
778 O_RDONLY | O_CLOEXEC);
779 if (fd < 0) {
780 ret = fd;
781 goto out_free_irq;
782 }
783
784 eventreq.fd = fd;
785 if (copy_to_user(ip, &eventreq, sizeof(eventreq)))
786 return -EFAULT;
787
788 return 0;
789
790out_free_irq:
791 free_irq(le->irq, le);
792out_free_desc:
793 gpiod_free(le->desc);
794out_free_label:
795 kfree(le->label);
796out_free_le:
797 kfree(le);
798 put_device(&gdev->dev);
799 return ret;
800}
801
Linus Walleij3c702e92015-10-21 15:29:53 +0200802/**
803 * gpio_ioctl() - ioctl handler for the GPIO chardev
804 */
805static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
806{
807 struct gpio_device *gdev = filp->private_data;
808 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200809 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200810
811 /* We fail any subsequent ioctl():s when the chip is gone */
812 if (!chip)
813 return -ENODEV;
814
Linus Walleij521a2ad2016-02-12 22:25:22 +0100815 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200816 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100817 struct gpiochip_info chipinfo;
818
Linus Walleij3c702e92015-10-21 15:29:53 +0200819 strncpy(chipinfo.name, dev_name(&gdev->dev),
820 sizeof(chipinfo.name));
821 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100822 strncpy(chipinfo.label, gdev->label,
823 sizeof(chipinfo.label));
824 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100825 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200826 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
827 return -EFAULT;
828 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100829 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
830 struct gpioline_info lineinfo;
831 struct gpio_desc *desc;
832
833 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
834 return -EFAULT;
835 if (lineinfo.line_offset > gdev->ngpio)
836 return -EINVAL;
837
838 desc = &gdev->descs[lineinfo.line_offset];
839 if (desc->name) {
840 strncpy(lineinfo.name, desc->name,
841 sizeof(lineinfo.name));
842 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
843 } else {
844 lineinfo.name[0] = '\0';
845 }
846 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100847 strncpy(lineinfo.consumer, desc->label,
848 sizeof(lineinfo.consumer));
849 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100850 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100851 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100852 }
853
854 /*
855 * Userspace only need to know that the kernel is using
856 * this GPIO so it can't use it.
857 */
858 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100859 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
860 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
861 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
862 test_bit(FLAG_EXPORT, &desc->flags) ||
863 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100864 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100865 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100866 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100867 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100868 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100869 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100870 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100871 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100872 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
873
874 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
875 return -EFAULT;
876 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200877 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
878 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +0200879 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
880 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +0200881 }
882 return -EINVAL;
883}
884
Linus Walleij8b92e172016-05-27 14:24:04 +0200885#ifdef CONFIG_COMPAT
886static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
887 unsigned long arg)
888{
889 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
890}
891#endif
892
Linus Walleij3c702e92015-10-21 15:29:53 +0200893/**
894 * gpio_chrdev_open() - open the chardev for ioctl operations
895 * @inode: inode for this chardev
896 * @filp: file struct for storing private data
897 * Returns 0 on success
898 */
899static int gpio_chrdev_open(struct inode *inode, struct file *filp)
900{
901 struct gpio_device *gdev = container_of(inode->i_cdev,
902 struct gpio_device, chrdev);
903
904 /* Fail on open if the backing gpiochip is gone */
905 if (!gdev || !gdev->chip)
906 return -ENODEV;
907 get_device(&gdev->dev);
908 filp->private_data = gdev;
909 return 0;
910}
911
912/**
913 * gpio_chrdev_release() - close chardev after ioctl operations
914 * @inode: inode for this chardev
915 * @filp: file struct for storing private data
916 * Returns 0 on success
917 */
918static int gpio_chrdev_release(struct inode *inode, struct file *filp)
919{
920 struct gpio_device *gdev = container_of(inode->i_cdev,
921 struct gpio_device, chrdev);
922
923 if (!gdev)
924 return -ENODEV;
925 put_device(&gdev->dev);
926 return 0;
927}
928
929
930static const struct file_operations gpio_fileops = {
931 .release = gpio_chrdev_release,
932 .open = gpio_chrdev_open,
933 .owner = THIS_MODULE,
934 .llseek = noop_llseek,
935 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +0200936#ifdef CONFIG_COMPAT
937 .compat_ioctl = gpio_ioctl_compat,
938#endif
Linus Walleij3c702e92015-10-21 15:29:53 +0200939};
940
Linus Walleijff2b1352015-10-20 11:10:38 +0200941static void gpiodevice_release(struct device *dev)
942{
943 struct gpio_device *gdev = dev_get_drvdata(dev);
944
Linus Walleij3c702e92015-10-21 15:29:53 +0200945 cdev_del(&gdev->chrdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200946 list_del(&gdev->list);
947 ida_simple_remove(&gpio_ida, gdev->id);
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700948 kfree(gdev->label);
949 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +0100950 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200951}
952
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700953static int gpiochip_setup_dev(struct gpio_device *gdev)
954{
955 int status;
956
957 cdev_init(&gdev->chrdev, &gpio_fileops);
958 gdev->chrdev.owner = THIS_MODULE;
959 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
960 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
961 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
962 if (status < 0)
963 chip_warn(gdev->chip, "failed to add char device %d:%d\n",
964 MAJOR(gpio_devt), gdev->id);
965 else
966 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
967 MAJOR(gpio_devt), gdev->id);
968 status = device_add(&gdev->dev);
969 if (status)
970 goto err_remove_chardev;
971
972 status = gpiochip_sysfs_register(gdev);
973 if (status)
974 goto err_remove_device;
975
976 /* From this point, the .release() function cleans up gpio_device */
977 gdev->dev.release = gpiodevice_release;
978 get_device(&gdev->dev);
979 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
980 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
981 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
982
983 return 0;
984
985err_remove_device:
986 device_del(&gdev->dev);
987err_remove_chardev:
988 cdev_del(&gdev->chrdev);
989 return status;
990}
991
992static void gpiochip_setup_devs(void)
993{
994 struct gpio_device *gdev;
995 int err;
996
997 list_for_each_entry(gdev, &gpio_devices, list) {
998 err = gpiochip_setup_dev(gdev);
999 if (err)
1000 pr_err("%s: Failed to initialize gpio device (%d)\n",
1001 dev_name(&gdev->dev), err);
1002 }
1003}
1004
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001005/**
Linus Walleijb08ea352015-12-03 15:14:13 +01001006 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -08001007 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001008 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -08001009 *
1010 * Returns a negative errno if the chip can't be registered, such as
1011 * because the chip->base is invalid or already associated with a
1012 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001013 *
Linus Walleijb08ea352015-12-03 15:14:13 +01001014 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001015 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d2008-07-25 01:46:07 -07001016 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1017 * for GPIOs will fail rudely.
1018 *
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001019 * gpiochip_add_data() must only be called after gpiolib initialization,
1020 * ie after core_initcall().
1021 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001022 * If chip->base is negative, this requests dynamic assignment of
1023 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001024 */
Linus Walleijb08ea352015-12-03 15:14:13 +01001025int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -08001026{
1027 unsigned long flags;
1028 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001029 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001030 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001031 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001032
Linus Walleijff2b1352015-10-20 11:10:38 +02001033 /*
1034 * First: allocate and populate the internal stat container, and
1035 * set up the struct device.
1036 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001037 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001038 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001039 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001040 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001041 gdev->chip = chip;
1042 chip->gpiodev = gdev;
1043 if (chip->parent) {
1044 gdev->dev.parent = chip->parent;
1045 gdev->dev.of_node = chip->parent->of_node;
1046 } else {
1047#ifdef CONFIG_OF_GPIO
1048 /* If the gpiochip has an assigned OF node this takes precedence */
1049 if (chip->of_node)
1050 gdev->dev.of_node = chip->of_node;
1051#endif
1052 }
1053 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1054 if (gdev->id < 0) {
1055 status = gdev->id;
1056 goto err_free_gdev;
1057 }
1058 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1059 device_initialize(&gdev->dev);
1060 dev_set_drvdata(&gdev->dev, gdev);
1061 if (chip->parent && chip->parent->driver)
1062 gdev->owner = chip->parent->driver->owner;
1063 else if (chip->owner)
1064 /* TODO: remove chip->owner */
1065 gdev->owner = chip->owner;
1066 else
1067 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001068
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001069 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001070 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001071 status = -ENOMEM;
1072 goto err_free_gdev;
1073 }
1074
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001075 if (chip->ngpio == 0) {
1076 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001077 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001078 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001079 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001080
1081 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001082 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001083 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001084 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001085 if (!gdev->label) {
1086 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001087 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001088 }
1089
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001090 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001091 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001092
David Brownelld2876d02008-02-04 22:28:20 -08001093 spin_lock_irqsave(&gpio_lock, flags);
1094
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001095 /*
1096 * TODO: this allocates a Linux GPIO number base in the global
1097 * GPIO numberspace for this chip. In the long run we want to
1098 * get *rid* of this numberspace and use only descriptors, but
1099 * it may be a pipe dream. It will not happen before we get rid
1100 * of the sysfs interface anyways.
1101 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001102 if (base < 0) {
1103 base = gpiochip_find_base(chip->ngpio);
1104 if (base < 0) {
1105 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001106 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001107 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001108 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001109 /*
1110 * TODO: it should not be necessary to reflect the assigned
1111 * base outside of the GPIO subsystem. Go over drivers and
1112 * see if anyone makes use of this, else drop this and assign
1113 * a poison instead.
1114 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001115 chip->base = base;
1116 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001117 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001118
Linus Walleijff2b1352015-10-20 11:10:38 +02001119 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001120 if (status) {
1121 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001122 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001123 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001124
Linus Walleij545ebd92016-05-30 17:11:59 +02001125 spin_unlock_irqrestore(&gpio_lock, flags);
1126
Linus Walleijff2b1352015-10-20 11:10:38 +02001127 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001128 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d2008-07-25 01:46:07 -07001129
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001130 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +02001131 /*
1132 * REVISIT: most hardware initializes GPIOs as inputs
1133 * (often with pullups enabled) so power usage is
1134 * minimized. Linux code should set the gpio direction
1135 * first thing; but until it does, and in case
1136 * chip->get_direction is not set, we may expose the
1137 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001138 */
Linus Walleij72d32002016-04-28 13:33:59 +02001139
1140 if (chip->get_direction) {
1141 /*
1142 * If we have .get_direction, set up the initial
1143 * direction flag from the hardware.
1144 */
1145 int dir = chip->get_direction(chip, i);
1146
1147 if (!dir)
1148 set_bit(FLAG_IS_OUT, &desc->flags);
1149 } else if (!chip->direction_input) {
1150 /*
1151 * If the chip lacks the .direction_input callback
1152 * we logically assume all lines are outputs.
1153 */
1154 set_bit(FLAG_IS_OUT, &desc->flags);
1155 }
David Brownelld2876d02008-02-04 22:28:20 -08001156 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001157
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301158#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001159 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301160#endif
1161
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001162 status = gpiochip_set_desc_names(chip);
1163 if (status)
1164 goto err_remove_from_list;
1165
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001166 status = of_gpiochip_add(chip);
1167 if (status)
1168 goto err_remove_chip;
1169
Mika Westerberg664e3e52014-01-08 12:40:54 +02001170 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001171
Linus Walleij3c702e92015-10-21 15:29:53 +02001172 /*
1173 * By first adding the chardev, and then adding the device,
1174 * we get a device node entry in sysfs under
1175 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1176 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001177 * We can do this only if gpiolib has been initialized.
1178 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001179 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001180 if (gpiolib_initialized) {
1181 status = gpiochip_setup_dev(gdev);
1182 if (status)
1183 goto err_remove_chip;
1184 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001185 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001186
Johan Hovold225fce82015-01-12 17:12:25 +01001187err_remove_chip:
1188 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001189 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001190 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001191err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001192 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001193 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001194 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001195err_free_label:
1196 kfree(gdev->label);
1197err_free_descs:
1198 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001199err_free_gdev:
1200 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001201 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001202 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001203 gdev->base, gdev->base + gdev->ngpio - 1,
1204 chip->label ? : "generic");
1205 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001206 return status;
1207}
Linus Walleijb08ea352015-12-03 15:14:13 +01001208EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -08001209
1210/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001211 * gpiochip_get_data() - get per-subdriver data for the chip
1212 */
1213void *gpiochip_get_data(struct gpio_chip *chip)
1214{
1215 return chip->gpiodev->data;
1216}
1217EXPORT_SYMBOL_GPL(gpiochip_get_data);
1218
1219/**
David Brownelld2876d02008-02-04 22:28:20 -08001220 * gpiochip_remove() - unregister a gpio_chip
1221 * @chip: the chip to unregister
1222 *
1223 * A gpio_chip with any GPIOs still requested may not be removed.
1224 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001225void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001226{
Linus Walleijff2b1352015-10-20 11:10:38 +02001227 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001228 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001229 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001230 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001231 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001232
Linus Walleijff2b1352015-10-20 11:10:38 +02001233 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001234 gpiochip_sysfs_unregister(gdev);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001235 /* Numb the device, cancelling all outstanding operations */
1236 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001237 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001238 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001239 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -06001240 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001241 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001242 /*
1243 * We accept no more calls into the driver from this point, so
1244 * NULL the driver data pointer
1245 */
1246 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001247
Johan Hovold6798aca2015-01-12 17:12:28 +01001248 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001249 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001250 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001251 if (test_bit(FLAG_REQUESTED, &desc->flags))
1252 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001253 }
David Brownelld2876d02008-02-04 22:28:20 -08001254 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001255
Johan Hovoldfab28b82015-05-04 17:10:27 +02001256 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001257 dev_crit(&gdev->dev,
Linus Walleij58383c72015-11-04 09:56:26 +01001258 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001259
Linus Walleijff2b1352015-10-20 11:10:38 +02001260 /*
1261 * The gpiochip side puts its use of the device to rest here:
1262 * if there are no userspace clients, the chardev and device will
1263 * be removed, else it will be dangling until the last user is
1264 * gone.
1265 */
1266 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001267}
1268EXPORT_SYMBOL_GPL(gpiochip_remove);
1269
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301270static void devm_gpio_chip_release(struct device *dev, void *res)
1271{
1272 struct gpio_chip *chip = *(struct gpio_chip **)res;
1273
1274 gpiochip_remove(chip);
1275}
1276
1277static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1278
1279{
1280 struct gpio_chip **r = res;
1281
1282 if (!r || !*r) {
1283 WARN_ON(!r || !*r);
1284 return 0;
1285 }
1286
1287 return *r == data;
1288}
1289
1290/**
1291 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1292 * @dev: the device pointer on which irq_chip belongs to.
1293 * @chip: the chip to register, with chip->base initialized
1294 * Context: potentially before irqs will work
1295 *
1296 * Returns a negative errno if the chip can't be registered, such as
1297 * because the chip->base is invalid or already associated with a
1298 * different chip. Otherwise it returns zero as a success code.
1299 *
1300 * The gpio chip automatically be released when the device is unbound.
1301 */
1302int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1303 void *data)
1304{
1305 struct gpio_chip **ptr;
1306 int ret;
1307
1308 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1309 GFP_KERNEL);
1310 if (!ptr)
1311 return -ENOMEM;
1312
1313 ret = gpiochip_add_data(chip, data);
1314 if (ret < 0) {
1315 devres_free(ptr);
1316 return ret;
1317 }
1318
1319 *ptr = chip;
1320 devres_add(dev, ptr);
1321
1322 return 0;
1323}
1324EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1325
1326/**
1327 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1328 * @dev: device for which which resource was allocated
1329 * @chip: the chip to remove
1330 *
1331 * A gpio_chip with any GPIOs still requested may not be removed.
1332 */
1333void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1334{
1335 int ret;
1336
1337 ret = devres_release(dev, devm_gpio_chip_release,
1338 devm_gpio_chip_match, chip);
1339 if (!ret)
1340 WARN_ON(ret);
1341}
1342EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1343
Grant Likely594fa262010-06-08 07:48:16 -06001344/**
1345 * gpiochip_find() - iterator for locating a specific gpio_chip
1346 * @data: data to pass to match function
1347 * @callback: Callback function to check gpio_chip
1348 *
1349 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1350 * determined by a user supplied @match callback. The callback should return
1351 * 0 if the device doesn't match and non-zero if it does. If the callback is
1352 * non-zero, this function will return to the caller and not iterate over any
1353 * more gpio_chips.
1354 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001355struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001356 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001357 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001358{
Linus Walleijff2b1352015-10-20 11:10:38 +02001359 struct gpio_device *gdev;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001360 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001361 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001362
1363 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001364 list_for_each_entry(gdev, &gpio_devices, list)
1365 if (match(gdev->chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001366 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001367
1368 /* No match? */
Linus Walleijff2b1352015-10-20 11:10:38 +02001369 if (&gdev->list == &gpio_devices)
Alexandre Courbot125eef92013-02-03 01:29:26 +09001370 chip = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +02001371 else
1372 chip = gdev->chip;
1373
Grant Likely594fa262010-06-08 07:48:16 -06001374 spin_unlock_irqrestore(&gpio_lock, flags);
1375
1376 return chip;
1377}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001378EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001379
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001380static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1381{
1382 const char *name = data;
1383
1384 return !strcmp(chip->label, name);
1385}
1386
1387static struct gpio_chip *find_chip_by_name(const char *name)
1388{
1389 return gpiochip_find((void *)name, gpiochip_match_name);
1390}
1391
Linus Walleij14250522014-03-25 10:40:18 +01001392#ifdef CONFIG_GPIOLIB_IRQCHIP
1393
1394/*
1395 * The following is irqchip helper code for gpiochips.
1396 */
1397
1398/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001399 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
1400 * @gpiochip: the gpiochip to set the irqchip chain to
1401 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001402 * @parent_irq: the irq number corresponding to the parent IRQ for this
1403 * chained irqchip
1404 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001405 * coming out of the gpiochip. If the interrupt is nested rather than
1406 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001407 */
1408void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1409 struct irq_chip *irqchip,
1410 int parent_irq,
1411 irq_flow_handler_t parent_handler)
1412{
Linus Walleij83141a72014-09-26 13:50:12 +02001413 unsigned int offset;
1414
Linus Walleij83141a72014-09-26 13:50:12 +02001415 if (!gpiochip->irqdomain) {
1416 chip_err(gpiochip, "called %s before setting up irqchip\n",
1417 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001418 return;
1419 }
1420
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001421 if (parent_handler) {
1422 if (gpiochip->can_sleep) {
1423 chip_err(gpiochip,
1424 "you cannot have chained interrupts on a "
1425 "chip that may sleep\n");
1426 return;
1427 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001428 /*
1429 * The parent irqchip is already using the chip_data for this
1430 * irqchip, so our callbacks simply use the handler_data.
1431 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001432 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1433 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001434
1435 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001436 }
Linus Walleij83141a72014-09-26 13:50:12 +02001437
1438 /* Set the parent IRQ for all affected IRQs */
1439 for (offset = 0; offset < gpiochip->ngpio; offset++)
1440 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
1441 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +01001442}
1443EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1444
1445/**
1446 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1447 * @d: the irqdomain used by this irqchip
1448 * @irq: the global irq number used by this GPIO irqchip irq
1449 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1450 *
1451 * This function will set up the mapping for a certain IRQ line on a
1452 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1453 * stored inside the gpiochip.
1454 */
1455static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1456 irq_hw_number_t hwirq)
1457{
1458 struct gpio_chip *chip = d->host_data;
1459
Linus Walleij14250522014-03-25 10:40:18 +01001460 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001461 /*
1462 * This lock class tells lockdep that GPIO irqs are in a different
1463 * category than their parents, so it won't report false recursion.
1464 */
1465 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +02001466 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001467 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +03001468 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001469 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001470 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001471
Linus Walleij1333b902014-04-23 16:45:12 +02001472 /*
1473 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1474 * is passed as default type.
1475 */
1476 if (chip->irq_default_type != IRQ_TYPE_NONE)
1477 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001478
1479 return 0;
1480}
1481
Linus Walleijc3626fd2014-03-28 20:42:01 +01001482static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1483{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001484 struct gpio_chip *chip = d->host_data;
1485
Linus Walleij1c8732b2014-04-09 13:34:39 +02001486 if (chip->can_sleep)
1487 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001488 irq_set_chip_and_handler(irq, NULL, NULL);
1489 irq_set_chip_data(irq, NULL);
1490}
1491
Linus Walleij14250522014-03-25 10:40:18 +01001492static const struct irq_domain_ops gpiochip_domain_ops = {
1493 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001494 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001495 /* Virtually all GPIO irqchips are twocell:ed */
1496 .xlate = irq_domain_xlate_twocell,
1497};
1498
1499static int gpiochip_irq_reqres(struct irq_data *d)
1500{
1501 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1502
Linus Walleijff2b1352015-10-20 11:10:38 +02001503 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001504 return -ENODEV;
1505
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001506 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001507 chip_err(chip,
1508 "unable to lock HW IRQ %lu for IRQ\n",
1509 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001510 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001511 return -EINVAL;
1512 }
1513 return 0;
1514}
1515
1516static void gpiochip_irq_relres(struct irq_data *d)
1517{
1518 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1519
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001520 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001521 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001522}
1523
1524static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1525{
1526 return irq_find_mapping(chip->irqdomain, offset);
1527}
1528
1529/**
1530 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1531 * @gpiochip: the gpiochip to remove the irqchip from
1532 *
1533 * This is called only from gpiochip_remove()
1534 */
1535static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1536{
Linus Walleijc3626fd2014-03-28 20:42:01 +01001537 unsigned int offset;
1538
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001539 acpi_gpiochip_free_interrupts(gpiochip);
1540
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001541 if (gpiochip->irq_parent) {
1542 irq_set_chained_handler(gpiochip->irq_parent, NULL);
1543 irq_set_handler_data(gpiochip->irq_parent, NULL);
1544 }
1545
Linus Walleijc3626fd2014-03-28 20:42:01 +01001546 /* Remove all IRQ mappings and delete the domain */
1547 if (gpiochip->irqdomain) {
1548 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +03001549 irq_dispose_mapping(
1550 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +01001551 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001552 }
Linus Walleij14250522014-03-25 10:40:18 +01001553
1554 if (gpiochip->irqchip) {
1555 gpiochip->irqchip->irq_request_resources = NULL;
1556 gpiochip->irqchip->irq_release_resources = NULL;
1557 gpiochip->irqchip = NULL;
1558 }
1559}
1560
1561/**
1562 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
1563 * @gpiochip: the gpiochip to add the irqchip to
1564 * @irqchip: the irqchip to add to the gpiochip
1565 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1566 * allocate gpiochip irqs from
1567 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001568 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1569 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001570 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001571 *
1572 * This function closely associates a certain irqchip with a certain
1573 * gpiochip, providing an irq domain to translate the local IRQs to
1574 * global irqs in the gpiolib core, and making sure that the gpiochip
1575 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001576 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001577 * from the gpiochip passed as chip data. An irqdomain will be stored
1578 * in the gpiochip that shall be used by the driver to handle IRQ number
1579 * translation. The gpiochip will need to be initialized and registered
1580 * before calling this function.
1581 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001582 * This function will handle two cell:ed simple IRQs and assumes all
1583 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001584 * need to be open coded.
1585 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001586int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
1587 struct irq_chip *irqchip,
1588 unsigned int first_irq,
1589 irq_flow_handler_t handler,
1590 unsigned int type,
1591 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001592{
1593 struct device_node *of_node;
1594 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001595 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001596
1597 if (!gpiochip || !irqchip)
1598 return -EINVAL;
1599
Linus Walleij58383c72015-11-04 09:56:26 +01001600 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001601 pr_err("missing gpiochip .dev parent pointer\n");
1602 return -EINVAL;
1603 }
Linus Walleij58383c72015-11-04 09:56:26 +01001604 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001605#ifdef CONFIG_OF_GPIO
1606 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001607 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001608 * FIXME: get rid of this and use gpiochip->parent->of_node
1609 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001610 */
1611 if (gpiochip->of_node)
1612 of_node = gpiochip->of_node;
1613#endif
1614 gpiochip->irqchip = irqchip;
1615 gpiochip->irq_handler = handler;
1616 gpiochip->irq_default_type = type;
1617 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001618 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +01001619 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1620 gpiochip->ngpio, first_irq,
1621 &gpiochip_domain_ops, gpiochip);
1622 if (!gpiochip->irqdomain) {
1623 gpiochip->irqchip = NULL;
1624 return -EINVAL;
1625 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001626
1627 /*
1628 * It is possible for a driver to override this, but only if the
1629 * alternative functions are both implemented.
1630 */
1631 if (!irqchip->irq_request_resources &&
1632 !irqchip->irq_release_resources) {
1633 irqchip->irq_request_resources = gpiochip_irq_reqres;
1634 irqchip->irq_release_resources = gpiochip_irq_relres;
1635 }
Linus Walleij14250522014-03-25 10:40:18 +01001636
1637 /*
1638 * Prepare the mapping since the irqchip shall be orthogonal to
1639 * any gpiochip calls. If the first_irq was zero, this is
1640 * necessary to allocate descriptors for all IRQs.
1641 */
Linus Walleijc3626fd2014-03-28 20:42:01 +01001642 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1643 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
1644 if (offset == 0)
1645 /*
1646 * Store the base into the gpiochip to be used when
1647 * unmapping the irqs.
1648 */
1649 gpiochip->irq_base = irq_base;
1650 }
Linus Walleij14250522014-03-25 10:40:18 +01001651
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001652 acpi_gpiochip_request_interrupts(gpiochip);
1653
Linus Walleij14250522014-03-25 10:40:18 +01001654 return 0;
1655}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001656EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +01001657
1658#else /* CONFIG_GPIOLIB_IRQCHIP */
1659
1660static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
1661
1662#endif /* CONFIG_GPIOLIB_IRQCHIP */
1663
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001664/**
1665 * gpiochip_generic_request() - request the gpio function for a pin
1666 * @chip: the gpiochip owning the GPIO
1667 * @offset: the offset of the GPIO to request for GPIO function
1668 */
1669int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1670{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001671 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001672}
1673EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1674
1675/**
1676 * gpiochip_generic_free() - free the gpio function from a pin
1677 * @chip: the gpiochip to request the gpio function for
1678 * @offset: the offset of the GPIO to free from GPIO function
1679 */
1680void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1681{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001682 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001683}
1684EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1685
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301686#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001687
Linus Walleij3f0f8672012-11-20 12:40:15 +01001688/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001689 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1690 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001691 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001692 * @gpio_offset: the start offset in the current gpio_chip number space
1693 * @pin_group: name of the pin group inside the pin controller
1694 */
1695int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1696 struct pinctrl_dev *pctldev,
1697 unsigned int gpio_offset, const char *pin_group)
1698{
1699 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001700 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001701 int ret;
1702
1703 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1704 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001705 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001706 return -ENOMEM;
1707 }
1708
1709 /* Use local offset as range ID */
1710 pin_range->range.id = gpio_offset;
1711 pin_range->range.gc = chip;
1712 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001713 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001714 pin_range->pctldev = pctldev;
1715
1716 ret = pinctrl_get_group_pins(pctldev, pin_group,
1717 &pin_range->range.pins,
1718 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001719 if (ret < 0) {
1720 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001721 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001722 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001723
1724 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1725
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001726 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1727 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001728 pinctrl_dev_get_devname(pctldev), pin_group);
1729
Linus Walleij20ec3e32016-02-11 11:03:06 +01001730 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001731
1732 return 0;
1733}
1734EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1735
1736/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001737 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1738 * @chip: the gpiochip to add the range for
1739 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001740 * @gpio_offset: the start offset in the current gpio_chip number space
1741 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001742 * @npins: the number of pins from the offset of each pin space (GPIO and
1743 * pin controller) to accumulate in this range
1744 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001745int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001746 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001747 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301748{
1749 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001750 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001751 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301752
Linus Walleij3f0f8672012-11-20 12:40:15 +01001753 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301754 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001755 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001756 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301757 }
1758
Linus Walleij3f0f8672012-11-20 12:40:15 +01001759 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001760 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001761 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301762 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001763 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001764 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301765 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001766 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301767 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001768 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001769 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001770 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001771 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001772 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001773 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001774 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1775 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001776 pinctl_name,
1777 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301778
Linus Walleij20ec3e32016-02-11 11:03:06 +01001779 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001780
1781 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301782}
Linus Walleij165adc92012-11-06 14:49:39 +01001783EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301784
Linus Walleij3f0f8672012-11-20 12:40:15 +01001785/**
1786 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1787 * @chip: the chip to remove all the mappings for
1788 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301789void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1790{
1791 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01001792 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301793
Linus Walleij20ec3e32016-02-11 11:03:06 +01001794 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301795 list_del(&pin_range->node);
1796 pinctrl_remove_gpio_range(pin_range->pctldev,
1797 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001798 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301799 }
1800}
Linus Walleij165adc92012-11-06 14:49:39 +01001801EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1802
1803#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301804
David Brownelld2876d02008-02-04 22:28:20 -08001805/* These "optional" allocation calls help prevent drivers from stomping
1806 * on each other, and help provide better diagnostics in debugfs.
1807 * They're called even less than the "set direction" calls.
1808 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001809static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001810{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001811 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001812 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001813 unsigned long flags;
1814
1815 spin_lock_irqsave(&gpio_lock, flags);
1816
David Brownelld2876d02008-02-04 22:28:20 -08001817 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001818 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001819 */
1820
1821 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1822 desc_set_label(desc, label ? : "?");
1823 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001824 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001825 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001826 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001827 }
1828
1829 if (chip->request) {
1830 /* chip->request may sleep */
1831 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001832 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001833 spin_lock_irqsave(&gpio_lock, flags);
1834
1835 if (status < 0) {
1836 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001837 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001838 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001839 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001840 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001841 if (chip->get_direction) {
1842 /* chip->get_direction may sleep */
1843 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001844 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001845 spin_lock_irqsave(&gpio_lock, flags);
1846 }
David Brownelld2876d02008-02-04 22:28:20 -08001847done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +03001848 if (status < 0) {
1849 /* Clear flags that might have been set by the caller before
1850 * requesting the GPIO.
1851 */
1852 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1853 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1854 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1855 }
Mika Westerberg77c2d792014-03-10 14:54:50 +02001856 spin_unlock_irqrestore(&gpio_lock, flags);
1857 return status;
1858}
1859
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001860/*
1861 * This descriptor validation needs to be inserted verbatim into each
1862 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02001863 * macro to avoid endless duplication. If the desc is NULL it is an
1864 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001865 */
1866#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02001867 if (!desc) \
1868 return 0; \
1869 if (!desc->gdev) { \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001870 pr_warn("%s: invalid GPIO\n", __func__); \
1871 return -EINVAL; \
1872 } \
1873 if ( !desc->gdev->chip ) { \
1874 dev_warn(&desc->gdev->dev, \
1875 "%s: backing chip is gone\n", __func__); \
1876 return 0; \
1877 } } while (0)
1878
1879#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02001880 if (!desc) \
1881 return; \
1882 if (!desc->gdev) { \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001883 pr_warn("%s: invalid GPIO\n", __func__); \
1884 return; \
1885 } \
1886 if (!desc->gdev->chip) { \
1887 dev_warn(&desc->gdev->dev, \
1888 "%s: backing chip is gone\n", __func__); \
1889 return; \
1890 } } while (0)
1891
1892
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001893int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001894{
1895 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001896 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001897
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001898 VALIDATE_DESC(desc);
1899 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001900
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001901 if (try_module_get(gdev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001902 status = __gpiod_request(desc, label);
1903 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001904 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001905 else
1906 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001907 }
1908
David Brownelld2876d02008-02-04 22:28:20 -08001909 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001910 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001911
David Brownelld2876d02008-02-04 22:28:20 -08001912 return status;
1913}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001914
Mika Westerberg77c2d792014-03-10 14:54:50 +02001915static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001916{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001917 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001918 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001919 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001920
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001921 might_sleep();
1922
Alexandre Courbot372e7222013-02-03 01:29:29 +09001923 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001924
David Brownelld2876d02008-02-04 22:28:20 -08001925 spin_lock_irqsave(&gpio_lock, flags);
1926
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001927 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07001928 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1929 if (chip->free) {
1930 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001931 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001932 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001933 spin_lock_irqsave(&gpio_lock, flags);
1934 }
David Brownelld2876d02008-02-04 22:28:20 -08001935 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001936 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001937 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301938 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301939 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001940 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001941 ret = true;
1942 }
David Brownelld2876d02008-02-04 22:28:20 -08001943
1944 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001945 return ret;
1946}
1947
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001948void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001949{
Linus Walleij33a68e82016-02-11 10:28:44 +01001950 if (desc && desc->gdev && __gpiod_free(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001951 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001952 put_device(&desc->gdev->dev);
1953 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001954 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01001955 }
David Brownelld2876d02008-02-04 22:28:20 -08001956}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001957
David Brownelld2876d02008-02-04 22:28:20 -08001958/**
1959 * gpiochip_is_requested - return string iff signal was requested
1960 * @chip: controller managing the signal
1961 * @offset: of signal within controller's 0..(ngpio - 1) range
1962 *
1963 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001964 * The string returned is the label passed to gpio_request(); if none has been
1965 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001966 *
1967 * This function is for use by GPIO controller drivers. The label can
1968 * help with diagnostics, and knowing that the signal is used as a GPIO
1969 * can help avoid accidentally multiplexing it to another controller.
1970 */
1971const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1972{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001973 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001974
Dirk Behme48b59532015-08-18 18:02:32 +02001975 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001976 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001977
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001978 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001979
Alexandre Courbot372e7222013-02-03 01:29:29 +09001980 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001981 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001982 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001983}
1984EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1985
Mika Westerberg77c2d792014-03-10 14:54:50 +02001986/**
1987 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1988 * @desc: GPIO descriptor to request
1989 * @label: label for the GPIO
1990 *
1991 * Function allows GPIO chip drivers to request and use their own GPIO
1992 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1993 * function will not increase reference count of the GPIO chip module. This
1994 * allows the GPIO chip module to be unloaded as needed (we assume that the
1995 * GPIO chip driver handles freeing the GPIOs it has requested).
1996 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001997struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1998 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001999{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002000 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2001 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002002
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002003 if (IS_ERR(desc)) {
2004 chip_err(chip, "failed to get GPIO descriptor\n");
2005 return desc;
2006 }
2007
2008 err = __gpiod_request(desc, label);
2009 if (err < 0)
2010 return ERR_PTR(err);
2011
2012 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002013}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002014EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002015
2016/**
2017 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2018 * @desc: GPIO descriptor to free
2019 *
2020 * Function frees the given GPIO requested previously with
2021 * gpiochip_request_own_desc().
2022 */
2023void gpiochip_free_own_desc(struct gpio_desc *desc)
2024{
2025 if (desc)
2026 __gpiod_free(desc);
2027}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002028EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002029
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002030/*
2031 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002032 * some cases this is done in early boot, before IRQs are enabled.
2033 *
2034 * As a rule these aren't called more than once (except for drivers
2035 * using the open-drain emulation idiom) so these are natural places
2036 * to accumulate extra debugging checks. Note that we can't (yet)
2037 * rely on gpio_request() having been called beforehand.
2038 */
2039
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002040/**
2041 * gpiod_direction_input - set the GPIO direction to input
2042 * @desc: GPIO to set to input
2043 *
2044 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2045 * be called safely on it.
2046 *
2047 * Return 0 in case of success, else an error code.
2048 */
2049int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002050{
David Brownelld2876d02008-02-04 22:28:20 -08002051 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002052 int status = -EINVAL;
2053
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002054 VALIDATE_DESC(desc);
2055 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002056
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002057 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002058 gpiod_warn(desc,
2059 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002060 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002061 return -EIO;
2062 }
2063
Alexandre Courbotd82da792014-07-22 16:17:43 +09002064 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002065 if (status == 0)
2066 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002067
Alexandre Courbot372e7222013-02-03 01:29:29 +09002068 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002069
David Brownelld2876d02008-02-04 22:28:20 -08002070 return status;
2071}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002072EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002073
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002074static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002075{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002076 struct gpio_chip *gc = desc->gdev->chip;
2077 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002078
Linus Walleijd468bf92013-09-24 11:54:38 +02002079 /* GPIOs used for IRQs shall not be set as output */
2080 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2081 gpiod_err(desc,
2082 "%s: tried to set a GPIO tied to an IRQ as output\n",
2083 __func__);
2084 return -EIO;
2085 }
2086
Linus Walleijc663e5f2016-03-22 10:51:16 +01002087 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2088 /* First see if we can enable open drain in hardware */
2089 if (gc->set_single_ended) {
2090 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2091 LINE_MODE_OPEN_DRAIN);
2092 if (!ret)
2093 goto set_output_value;
2094 }
2095 /* Emulate open drain by not actively driving the line high */
2096 if (value)
2097 return gpiod_direction_input(desc);
2098 }
2099 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2100 if (gc->set_single_ended) {
2101 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2102 LINE_MODE_OPEN_SOURCE);
2103 if (!ret)
2104 goto set_output_value;
2105 }
2106 /* Emulate open source by not actively driving the line low */
2107 if (!value)
2108 return gpiod_direction_input(desc);
2109 } else {
2110 /* Make sure to disable open drain/source hardware, if any */
2111 if (gc->set_single_ended)
2112 gc->set_single_ended(gc,
2113 gpio_chip_hwgpio(desc),
2114 LINE_MODE_PUSH_PULL);
2115 }
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302116
Linus Walleijc663e5f2016-03-22 10:51:16 +01002117set_output_value:
2118 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002119 gpiod_warn(desc,
2120 "%s: missing set() or direction_output() operations\n",
2121 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002122 return -EIO;
2123 }
2124
Linus Walleijc663e5f2016-03-22 10:51:16 +01002125 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), value);
2126 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002127 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002128 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002129 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2130 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002131}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002132
2133/**
2134 * gpiod_direction_output_raw - set the GPIO direction to output
2135 * @desc: GPIO to set to output
2136 * @value: initial output value of the GPIO
2137 *
2138 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2139 * be called safely on it. The initial value of the output must be specified
2140 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2141 *
2142 * Return 0 in case of success, else an error code.
2143 */
2144int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2145{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002146 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002147 return _gpiod_direction_output_raw(desc, value);
2148}
2149EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2150
2151/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302152 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002153 * @desc: GPIO to set to output
2154 * @value: initial output value of the GPIO
2155 *
2156 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2157 * be called safely on it. The initial value of the output must be specified
2158 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2159 * account.
2160 *
2161 * Return 0 in case of success, else an error code.
2162 */
2163int gpiod_direction_output(struct gpio_desc *desc, int value)
2164{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002165 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002166 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2167 value = !value;
2168 return _gpiod_direction_output_raw(desc, value);
2169}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002170EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002171
Felipe Balbic4b5be92010-05-26 14:42:23 -07002172/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002173 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07002174 * @gpio: the gpio to set debounce time
2175 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002176 *
2177 * returns -ENOTSUPP if the controller does not support setting
2178 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002179 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002180int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002181{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002182 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002183
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002184 VALIDATE_DESC(desc);
2185 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002186 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01002187 gpiod_dbg(desc,
2188 "%s: missing set() or set_debounce() operations\n",
2189 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002190 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002191 }
2192
Alexandre Courbotd82da792014-07-22 16:17:43 +09002193 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002194}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002195EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002196
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002197/**
2198 * gpiod_is_active_low - test whether a GPIO is active-low or not
2199 * @desc: the gpio descriptor to test
2200 *
2201 * Returns 1 if the GPIO is active-low, 0 otherwise.
2202 */
2203int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002204{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002205 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002206 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002207}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002208EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002209
2210/* I/O calls are only valid after configuration completed; the relevant
2211 * "is this a valid GPIO" error checks should already have been done.
2212 *
2213 * "Get" operations are often inlinable as reading a pin value register,
2214 * and masking the relevant bit in that register.
2215 *
2216 * When "set" operations are inlinable, they involve writing that mask to
2217 * one register to set a low value, or a different register to set it high.
2218 * Otherwise locking is needed, so there may be little value to inlining.
2219 *
2220 *------------------------------------------------------------------------
2221 *
2222 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2223 * have requested the GPIO. That can include implicit requesting by
2224 * a direction setting call. Marking a gpio as requested locks its chip
2225 * in memory, guaranteeing that these table lookups need no more locking
2226 * and that gpiochip_remove() will fail.
2227 *
2228 * REVISIT when debugging, consider adding some instrumentation to ensure
2229 * that the GPIO was actually requested.
2230 */
2231
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002232static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002233{
2234 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002235 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002236 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002237
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002238 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002239 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002240 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002241 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002242 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002243 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002244}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002245
David Brownelld2876d02008-02-04 22:28:20 -08002246/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002247 * gpiod_get_raw_value() - return a gpio's raw value
2248 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002249 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002250 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002251 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002252 *
2253 * This function should be called from contexts where we cannot sleep, and will
2254 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002255 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002256int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002257{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002258 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002259 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002260 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002261 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002262}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002263EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002264
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002265/**
2266 * gpiod_get_value() - return a gpio's value
2267 * @desc: gpio whose value will be returned
2268 *
2269 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002270 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002271 *
2272 * This function should be called from contexts where we cannot sleep, and will
2273 * complain if the GPIO chip functions potentially sleep.
2274 */
2275int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002276{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002277 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002278
2279 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002280 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002281 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002282
2283 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002284 if (value < 0)
2285 return value;
2286
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002287 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2288 value = !value;
2289
2290 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002291}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002292EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002293
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302294/*
2295 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002296 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002297 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302298 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002299static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302300{
2301 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002302 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002303 int offset = gpio_chip_hwgpio(desc);
2304
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302305 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002306 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302307 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002308 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302309 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002310 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302311 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002312 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302313 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002314 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302315 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002316 gpiod_err(desc,
2317 "%s: Error in set_value for open drain err %d\n",
2318 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302319}
2320
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302321/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002322 * _gpio_set_open_source_value() - Set the open source gpio's value.
2323 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002324 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302325 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002326static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302327{
2328 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002329 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002330 int offset = gpio_chip_hwgpio(desc);
2331
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302332 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002333 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302334 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002335 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302336 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002337 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302338 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002339 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302340 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002341 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302342 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002343 gpiod_err(desc,
2344 "%s: Error in set_value for open source err %d\n",
2345 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302346}
2347
Alexandre Courbot23600962014-03-11 15:52:09 +09002348static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002349{
2350 struct gpio_chip *chip;
2351
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002352 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002353 trace_gpio_value(desc_to_gpio(desc), 0, value);
2354 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2355 _gpio_set_open_drain_value(desc, value);
2356 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2357 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302358 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002359 chip->set(chip, gpio_chip_hwgpio(desc), value);
2360}
2361
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002362/*
2363 * set multiple outputs on the same chip;
2364 * use the chip's set_multiple function if available;
2365 * otherwise set the outputs sequentially;
2366 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2367 * defines which outputs are to be changed
2368 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2369 * defines the values the outputs specified by mask are to be set to
2370 */
2371static void gpio_chip_set_multiple(struct gpio_chip *chip,
2372 unsigned long *mask, unsigned long *bits)
2373{
2374 if (chip->set_multiple) {
2375 chip->set_multiple(chip, mask, bits);
2376 } else {
2377 int i;
2378 for (i = 0; i < chip->ngpio; i++) {
2379 if (mask[BIT_WORD(i)] == 0) {
2380 /* no more set bits in this mask word;
2381 * skip ahead to the next word */
2382 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
2383 continue;
2384 }
2385 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002386 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002387 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002388 }
2389 }
2390}
2391
Linus Walleij44c7288f2016-04-24 11:36:59 +02002392void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2393 unsigned int array_size,
2394 struct gpio_desc **desc_array,
2395 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002396{
2397 int i = 0;
2398
2399 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002400 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002401 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2402 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2403 int count = 0;
2404
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002405 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002406 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002407
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002408 memset(mask, 0, sizeof(mask));
2409 do {
2410 struct gpio_desc *desc = desc_array[i];
2411 int hwgpio = gpio_chip_hwgpio(desc);
2412 int value = value_array[i];
2413
2414 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2415 value = !value;
2416 trace_gpio_value(desc_to_gpio(desc), 0, value);
2417 /*
2418 * collect all normal outputs belonging to the same chip
2419 * open drain and open source outputs are set individually
2420 */
2421 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002422 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002423 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2424 _gpio_set_open_source_value(desc, value);
2425 } else {
2426 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002427 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002428 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002429 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002430 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002431 count++;
2432 }
2433 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002434 } while ((i < array_size) &&
2435 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002436 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002437 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002438 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002439 }
2440}
2441
David Brownelld2876d02008-02-04 22:28:20 -08002442/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002443 * gpiod_set_raw_value() - assign a gpio's raw value
2444 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002445 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002446 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002447 * Set the raw value of the GPIO, i.e. the value of its physical line without
2448 * regard for its ACTIVE_LOW status.
2449 *
2450 * This function should be called from contexts where we cannot sleep, and will
2451 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002452 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002453void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002454{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002455 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002456 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002457 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002458 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002459}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002460EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002461
2462/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002463 * gpiod_set_value() - assign a gpio's value
2464 * @desc: gpio whose value will be assigned
2465 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002466 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002467 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2468 * account
2469 *
2470 * This function should be called from contexts where we cannot sleep, and will
2471 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002472 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002473void gpiod_set_value(struct gpio_desc *desc, int value)
2474{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002475 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002476 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002477 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002478 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2479 value = !value;
2480 _gpiod_set_raw_value(desc, value);
2481}
2482EXPORT_SYMBOL_GPL(gpiod_set_value);
2483
2484/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002485 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002486 * @array_size: number of elements in the descriptor / value arrays
2487 * @desc_array: array of GPIO descriptors whose values will be assigned
2488 * @value_array: array of values to assign
2489 *
2490 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2491 * without regard for their ACTIVE_LOW status.
2492 *
2493 * This function should be called from contexts where we cannot sleep, and will
2494 * complain if the GPIO chip functions potentially sleep.
2495 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002496void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002497 struct gpio_desc **desc_array, int *value_array)
2498{
2499 if (!desc_array)
2500 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002501 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2502 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002503}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002504EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002505
2506/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002507 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002508 * @array_size: number of elements in the descriptor / value arrays
2509 * @desc_array: array of GPIO descriptors whose values will be assigned
2510 * @value_array: array of values to assign
2511 *
2512 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2513 * into account.
2514 *
2515 * This function should be called from contexts where we cannot sleep, and will
2516 * complain if the GPIO chip functions potentially sleep.
2517 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002518void gpiod_set_array_value(unsigned int array_size,
2519 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002520{
2521 if (!desc_array)
2522 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002523 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2524 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002525}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002526EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002527
2528/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002529 * gpiod_cansleep() - report whether gpio value access may sleep
2530 * @desc: gpio to check
2531 *
2532 */
2533int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002534{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002535 VALIDATE_DESC(desc);
2536 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002537}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002538EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002539
David Brownell0f6d5042008-10-15 22:03:14 -07002540/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002541 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2542 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002543 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002544 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2545 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002546 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002547int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002548{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002549 struct gpio_chip *chip;
2550 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002551
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002552 VALIDATE_DESC(desc);
2553 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002554 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02002555 if (chip->to_irq) {
2556 int retirq = chip->to_irq(chip, offset);
2557
2558 /* Zero means NO_IRQ */
2559 if (!retirq)
2560 return -ENXIO;
2561
2562 return retirq;
2563 }
2564 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002565}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002566EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002567
Linus Walleijd468bf92013-09-24 11:54:38 +02002568/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002569 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002570 * @chip: the chip the GPIO to lock belongs to
2571 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002572 *
2573 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08002574 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08002575 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002576int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08002577{
Linus Walleij9c102802016-05-25 10:56:03 +02002578 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02002579
Linus Walleij9c102802016-05-25 10:56:03 +02002580 desc = gpiochip_get_desc(chip, offset);
2581 if (IS_ERR(desc))
2582 return PTR_ERR(desc);
2583
2584 /* Flush direction if something changed behind our back */
2585 if (chip->get_direction) {
2586 int dir = chip->get_direction(chip, offset);
2587
2588 if (dir)
2589 clear_bit(FLAG_IS_OUT, &desc->flags);
2590 else
2591 set_bit(FLAG_IS_OUT, &desc->flags);
2592 }
2593
2594 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002595 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02002596 "%s: tried to flag a GPIO set as output for IRQ\n",
2597 __func__);
2598 return -EIO;
2599 }
2600
Linus Walleij9c102802016-05-25 10:56:03 +02002601 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002602 return 0;
2603}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002604EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002605
Linus Walleijd468bf92013-09-24 11:54:38 +02002606/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002607 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002608 * @chip: the chip the GPIO to lock belongs to
2609 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002610 *
2611 * This is used directly by GPIO drivers that want to indicate
2612 * that a certain GPIO is no longer used exclusively for IRQ.
2613 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002614void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02002615{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002616 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02002617 return;
2618
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002619 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002620}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002621EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002622
Linus Walleij6cee3822016-02-11 20:16:45 +01002623bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2624{
2625 if (offset >= chip->ngpio)
2626 return false;
2627
2628 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2629}
2630EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2631
Linus Walleij143b65d2016-02-16 15:41:42 +01002632bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2633{
2634 if (offset >= chip->ngpio)
2635 return false;
2636
2637 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2638}
2639EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2640
2641bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2642{
2643 if (offset >= chip->ngpio)
2644 return false;
2645
2646 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2647}
2648EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2649
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002650/**
2651 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2652 * @desc: gpio whose value will be returned
2653 *
2654 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002655 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002656 *
2657 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002658 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002659int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002660{
David Brownelld2876d02008-02-04 22:28:20 -08002661 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002662 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002663 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002664}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002665EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002666
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002667/**
2668 * gpiod_get_value_cansleep() - return a gpio's value
2669 * @desc: gpio whose value will be returned
2670 *
2671 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002672 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002673 *
2674 * This function is to be called from contexts that can sleep.
2675 */
2676int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002677{
David Brownelld2876d02008-02-04 22:28:20 -08002678 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002679
2680 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002681 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002682 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002683 if (value < 0)
2684 return value;
2685
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002686 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2687 value = !value;
2688
David Brownelld2876d02008-02-04 22:28:20 -08002689 return value;
2690}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002691EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002692
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002693/**
2694 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2695 * @desc: gpio whose value will be assigned
2696 * @value: value to assign
2697 *
2698 * Set the raw value of the GPIO, i.e. the value of its physical line without
2699 * regard for its ACTIVE_LOW status.
2700 *
2701 * This function is to be called from contexts that can sleep.
2702 */
2703void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002704{
David Brownelld2876d02008-02-04 22:28:20 -08002705 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002706 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002707 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002708}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002709EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002710
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002711/**
2712 * gpiod_set_value_cansleep() - assign a gpio's value
2713 * @desc: gpio whose value will be assigned
2714 * @value: value to assign
2715 *
2716 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2717 * account
2718 *
2719 * This function is to be called from contexts that can sleep.
2720 */
2721void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002722{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002723 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002724 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002725 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2726 value = !value;
2727 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002728}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002729EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002730
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002731/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002732 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002733 * @array_size: number of elements in the descriptor / value arrays
2734 * @desc_array: array of GPIO descriptors whose values will be assigned
2735 * @value_array: array of values to assign
2736 *
2737 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2738 * without regard for their ACTIVE_LOW status.
2739 *
2740 * This function is to be called from contexts that can sleep.
2741 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002742void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2743 struct gpio_desc **desc_array,
2744 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002745{
2746 might_sleep_if(extra_checks);
2747 if (!desc_array)
2748 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002749 gpiod_set_array_value_complex(true, true, array_size, desc_array,
2750 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002751}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002752EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002753
2754/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002755 * gpiod_set_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 logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2761 * into account.
2762 *
2763 * This function is to be called from contexts that can sleep.
2764 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002765void gpiod_set_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 Walleij44c7288f2016-04-24 11:36:59 +02002772 gpiod_set_array_value_complex(false, 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_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002776
2777/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002778 * gpiod_add_lookup_table() - register GPIO device consumers
2779 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002780 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002781void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002782{
2783 mutex_lock(&gpio_lookup_lock);
2784
Alexandre Courbotad824782013-12-03 12:20:11 +09002785 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002786
2787 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002788}
2789
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302790/**
2791 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2792 * @table: table of consumers to unregister
2793 */
2794void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2795{
2796 mutex_lock(&gpio_lookup_lock);
2797
2798 list_del(&table->list);
2799
2800 mutex_unlock(&gpio_lookup_lock);
2801}
2802
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002803static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002804 unsigned int idx,
2805 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002806{
2807 char prop_name[32]; /* 32 is max size of property name */
2808 enum of_gpio_flags of_flags;
2809 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02002810 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002811
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002812 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02002813 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01002814 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002815 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02002816 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01002817 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002818 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002819
Thierry Redingdd34c372014-04-23 17:28:09 +02002820 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2821 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07002822 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02002823 break;
2824 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002825
2826 if (IS_ERR(desc))
2827 return desc;
2828
2829 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002830 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002831
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002832 if (of_flags & OF_GPIO_SINGLE_ENDED) {
2833 if (of_flags & OF_GPIO_ACTIVE_LOW)
2834 *flags |= GPIO_OPEN_DRAIN;
2835 else
2836 *flags |= GPIO_OPEN_SOURCE;
2837 }
2838
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002839 return desc;
2840}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002841
Dmitry Torokhov25487532016-03-24 10:50:25 -07002842static struct gpio_desc *acpi_find_gpio(struct device *dev,
2843 const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002844 unsigned int idx,
Dmitry Torokhov25487532016-03-24 10:50:25 -07002845 enum gpiod_flags flags,
2846 enum gpio_lookup_flags *lookupflags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03002847{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002848 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03002849 struct acpi_gpio_info info;
2850 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002851 char propname[32];
2852 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03002853
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002854 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002855 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002856 if (con_id && strcmp(con_id, "gpios")) {
2857 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002858 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002859 } else {
2860 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002861 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002862 }
Mika Westerberge01f4402013-10-10 11:01:10 +03002863
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002864 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
2865 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2866 break;
2867 }
2868
2869 /* Then from plain _CRS GPIOs */
2870 if (IS_ERR(desc)) {
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08002871 if (!acpi_can_fallback_to_crs(adev, con_id))
2872 return ERR_PTR(-ENOENT);
2873
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002874 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2875 if (IS_ERR(desc))
2876 return desc;
Dmitry Torokhov25487532016-03-24 10:50:25 -07002877
2878 if ((flags == GPIOD_OUT_LOW || flags == GPIOD_OUT_HIGH) &&
2879 info.gpioint) {
2880 dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
2881 return ERR_PTR(-ENOENT);
2882 }
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002883 }
2884
Christophe RICARD52044722015-12-23 23:25:34 +01002885 if (info.polarity == GPIO_ACTIVE_LOW)
Dmitry Torokhov25487532016-03-24 10:50:25 -07002886 *lookupflags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002887
2888 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002889}
2890
Alexandre Courbotad824782013-12-03 12:20:11 +09002891static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2892{
2893 const char *dev_id = dev ? dev_name(dev) : NULL;
2894 struct gpiod_lookup_table *table;
2895
2896 mutex_lock(&gpio_lookup_lock);
2897
2898 list_for_each_entry(table, &gpio_lookup_list, list) {
2899 if (table->dev_id && dev_id) {
2900 /*
2901 * Valid strings on both ends, must be identical to have
2902 * a match
2903 */
2904 if (!strcmp(table->dev_id, dev_id))
2905 goto found;
2906 } else {
2907 /*
2908 * One of the pointers is NULL, so both must be to have
2909 * a match
2910 */
2911 if (dev_id == table->dev_id)
2912 goto found;
2913 }
2914 }
2915 table = NULL;
2916
2917found:
2918 mutex_unlock(&gpio_lookup_lock);
2919 return table;
2920}
2921
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002922static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002923 unsigned int idx,
2924 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002925{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002926 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002927 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002928 struct gpiod_lookup *p;
2929
Alexandre Courbotad824782013-12-03 12:20:11 +09002930 table = gpiod_find_lookup_table(dev);
2931 if (!table)
2932 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002933
Alexandre Courbotad824782013-12-03 12:20:11 +09002934 for (p = &table->table[0]; p->chip_label; p++) {
2935 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002936
Alexandre Courbotad824782013-12-03 12:20:11 +09002937 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002938 if (p->idx != idx)
2939 continue;
2940
Alexandre Courbotad824782013-12-03 12:20:11 +09002941 /* If the lookup entry has a con_id, require exact match */
2942 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2943 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002944
Alexandre Courbotad824782013-12-03 12:20:11 +09002945 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002946
Alexandre Courbotad824782013-12-03 12:20:11 +09002947 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002948 dev_err(dev, "cannot find GPIO chip %s\n",
2949 p->chip_label);
2950 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002951 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002952
Alexandre Courbotad824782013-12-03 12:20:11 +09002953 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002954 dev_err(dev,
2955 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2956 idx, chip->ngpio, chip->label);
2957 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09002958 }
2959
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09002960 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09002961 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002962
2963 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09002964 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002965
2966 return desc;
2967}
2968
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002969static int dt_gpio_count(struct device *dev, const char *con_id)
2970{
2971 int ret;
2972 char propname[32];
2973 unsigned int i;
2974
2975 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2976 if (con_id)
2977 snprintf(propname, sizeof(propname), "%s-%s",
2978 con_id, gpio_suffixes[i]);
2979 else
2980 snprintf(propname, sizeof(propname), "%s",
2981 gpio_suffixes[i]);
2982
2983 ret = of_gpio_named_count(dev->of_node, propname);
2984 if (ret >= 0)
2985 break;
2986 }
2987 return ret;
2988}
2989
2990static int platform_gpio_count(struct device *dev, const char *con_id)
2991{
2992 struct gpiod_lookup_table *table;
2993 struct gpiod_lookup *p;
2994 unsigned int count = 0;
2995
2996 table = gpiod_find_lookup_table(dev);
2997 if (!table)
2998 return -ENOENT;
2999
3000 for (p = &table->table[0]; p->chip_label; p++) {
3001 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3002 (!con_id && !p->con_id))
3003 count++;
3004 }
3005 if (!count)
3006 return -ENOENT;
3007
3008 return count;
3009}
3010
3011/**
3012 * gpiod_count - return the number of GPIOs associated with a device / function
3013 * or -ENOENT if no GPIO has been assigned to the requested function
3014 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3015 * @con_id: function within the GPIO consumer
3016 */
3017int gpiod_count(struct device *dev, const char *con_id)
3018{
3019 int count = -ENOENT;
3020
3021 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3022 count = dt_gpio_count(dev, con_id);
3023 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3024 count = acpi_gpio_count(dev, con_id);
3025
3026 if (count < 0)
3027 count = platform_gpio_count(dev, con_id);
3028
3029 return count;
3030}
3031EXPORT_SYMBOL_GPL(gpiod_count);
3032
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003033/**
Thierry Reding08791622014-04-25 16:54:22 +02003034 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003035 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003036 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003037 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003038 *
3039 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003040 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003041 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003042 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003043struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003044 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003045{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003046 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003047}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003048EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003049
3050/**
Thierry Reding29a1f232014-04-25 17:10:06 +02003051 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3052 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3053 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003054 * @flags: optional GPIO initialization flags
Thierry Reding29a1f232014-04-25 17:10:06 +02003055 *
3056 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3057 * the requested function it will return NULL. This is convenient for drivers
3058 * that need to handle optional GPIOs.
3059 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003060struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003061 const char *con_id,
3062 enum gpiod_flags flags)
Thierry Reding29a1f232014-04-25 17:10:06 +02003063{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003064 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f232014-04-25 17:10:06 +02003065}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003066EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f232014-04-25 17:10:06 +02003067
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003068/**
3069 * gpiod_parse_flags - helper function to parse GPIO lookup flags
3070 * @desc: gpio to be setup
3071 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3072 * of_get_gpio_hog()
3073 *
3074 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
3075 */
3076static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
3077{
3078 if (lflags & GPIO_ACTIVE_LOW)
3079 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3080 if (lflags & GPIO_OPEN_DRAIN)
3081 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3082 if (lflags & GPIO_OPEN_SOURCE)
3083 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3084}
Benoit Parrotf625d462015-02-02 11:44:44 -06003085
3086/**
3087 * gpiod_configure_flags - helper function to configure a given GPIO
3088 * @desc: gpio whose value will be assigned
3089 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06003090 * @dflags: gpiod_flags - optional GPIO initialization flags
3091 *
3092 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3093 * requested function and/or index, or another IS_ERR() code if an error
3094 * occurred while trying to acquire the GPIO.
3095 */
3096static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003097 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003098{
3099 int status;
3100
Benoit Parrotf625d462015-02-02 11:44:44 -06003101 /* No particular flag request, return here... */
3102 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3103 pr_debug("no flags found for %s\n", con_id);
3104 return 0;
3105 }
3106
3107 /* Process flags */
3108 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3109 status = gpiod_direction_output(desc,
3110 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
3111 else
3112 status = gpiod_direction_input(desc);
3113
3114 return status;
3115}
3116
Thierry Reding29a1f232014-04-25 17:10:06 +02003117/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003118 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003119 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003120 * @con_id: function within the GPIO consumer
3121 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003122 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003123 *
3124 * This variant of gpiod_get() allows to access GPIOs other than the first
3125 * defined one for functions that define several GPIOs.
3126 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003127 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3128 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003129 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003130 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003131struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003132 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003133 unsigned int idx,
3134 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003135{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003136 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003137 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003138 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003139
3140 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3141
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003142 if (dev) {
3143 /* Using device tree? */
3144 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3145 dev_dbg(dev, "using device tree for GPIO lookup\n");
3146 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3147 } else if (ACPI_COMPANION(dev)) {
3148 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Dmitry Torokhov25487532016-03-24 10:50:25 -07003149 desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003150 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003151 }
3152
3153 /*
3154 * Either we are not using DT or ACPI, or their lookup did not return
3155 * a result. In that case, use platform lookup as a fallback.
3156 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003157 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003158 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003159 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003160 }
3161
3162 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003163 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003164 return desc;
3165 }
3166
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003167 gpiod_parse_flags(desc, lookupflags);
3168
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003169 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003170 if (status < 0)
3171 return ERR_PTR(status);
3172
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003173 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003174 if (status < 0) {
3175 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3176 gpiod_put(desc);
3177 return ERR_PTR(status);
3178 }
3179
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003180 return desc;
3181}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003182EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003183
3184/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003185 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3186 * @fwnode: handle of the firmware node
3187 * @propname: name of the firmware property representing the GPIO
3188 *
3189 * This function can be used for drivers that get their configuration
3190 * from firmware.
3191 *
3192 * Function properly finds the corresponding GPIO using whatever is the
3193 * underlying firmware interface and then makes sure that the GPIO
3194 * descriptor is requested before it is returned to the caller.
3195 *
3196 * In case of error an ERR_PTR() is returned.
3197 */
3198struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3199 const char *propname)
3200{
3201 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3202 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003203 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003204 int ret;
3205
3206 if (!fwnode)
3207 return ERR_PTR(-EINVAL);
3208
3209 if (is_of_node(fwnode)) {
3210 enum of_gpio_flags flags;
3211
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02003212 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02003213 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003214 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003215 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003216 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3217 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003218 } else if (is_acpi_node(fwnode)) {
3219 struct acpi_gpio_info info;
3220
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02003221 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02003222 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01003223 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003224 }
3225
3226 if (IS_ERR(desc))
3227 return desc;
3228
Mika Westerberg40b73182014-10-21 13:33:59 +02003229 if (active_low)
3230 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3231
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003232 if (single_ended) {
3233 if (active_low)
3234 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3235 else
3236 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3237 }
3238
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003239 ret = gpiod_request(desc, NULL);
3240 if (ret)
3241 return ERR_PTR(ret);
3242
Mika Westerberg40b73182014-10-21 13:33:59 +02003243 return desc;
3244}
3245EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3246
3247/**
Thierry Reding29a1f232014-04-25 17:10:06 +02003248 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3249 * function
3250 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3251 * @con_id: function within the GPIO consumer
3252 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003253 * @flags: optional GPIO initialization flags
Thierry Reding29a1f232014-04-25 17:10:06 +02003254 *
3255 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3256 * specified index was assigned to the requested function it will return NULL.
3257 * This is convenient for drivers that need to handle optional GPIOs.
3258 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003259struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f232014-04-25 17:10:06 +02003260 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003261 unsigned int index,
3262 enum gpiod_flags flags)
Thierry Reding29a1f232014-04-25 17:10:06 +02003263{
3264 struct gpio_desc *desc;
3265
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003266 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f232014-04-25 17:10:06 +02003267 if (IS_ERR(desc)) {
3268 if (PTR_ERR(desc) == -ENOENT)
3269 return NULL;
3270 }
3271
3272 return desc;
3273}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003274EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f232014-04-25 17:10:06 +02003275
3276/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003277 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3278 * @desc: gpio whose value will be assigned
3279 * @name: gpio line name
3280 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3281 * of_get_gpio_hog()
3282 * @dflags: gpiod_flags - optional GPIO initialization flags
3283 */
3284int gpiod_hog(struct gpio_desc *desc, const char *name,
3285 unsigned long lflags, enum gpiod_flags dflags)
3286{
3287 struct gpio_chip *chip;
3288 struct gpio_desc *local_desc;
3289 int hwnum;
3290 int status;
3291
3292 chip = gpiod_to_chip(desc);
3293 hwnum = gpio_chip_hwgpio(desc);
3294
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003295 gpiod_parse_flags(desc, lflags);
3296
Benoit Parrotf625d462015-02-02 11:44:44 -06003297 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3298 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303299 status = PTR_ERR(local_desc);
3300 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3301 name, chip->label, hwnum, status);
3302 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003303 }
3304
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003305 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003306 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303307 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3308 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003309 gpiochip_free_own_desc(desc);
3310 return status;
3311 }
3312
3313 /* Mark GPIO as hogged so it can be identified and removed later */
3314 set_bit(FLAG_IS_HOGGED, &desc->flags);
3315
3316 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3317 desc_to_gpio(desc), name,
3318 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3319 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3320 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3321
3322 return 0;
3323}
3324
3325/**
3326 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3327 * @chip: gpio chip to act on
3328 *
3329 * This is only used by of_gpiochip_remove to free hogged gpios
3330 */
3331static void gpiochip_free_hogs(struct gpio_chip *chip)
3332{
3333 int id;
3334
3335 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003336 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3337 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003338 }
3339}
3340
3341/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003342 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3343 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3344 * @con_id: function within the GPIO consumer
3345 * @flags: optional GPIO initialization flags
3346 *
3347 * This function acquires all the GPIOs defined under a given function.
3348 *
3349 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3350 * no GPIO has been assigned to the requested function, or another IS_ERR()
3351 * code if an error occurred while trying to acquire the GPIOs.
3352 */
3353struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3354 const char *con_id,
3355 enum gpiod_flags flags)
3356{
3357 struct gpio_desc *desc;
3358 struct gpio_descs *descs;
3359 int count;
3360
3361 count = gpiod_count(dev, con_id);
3362 if (count < 0)
3363 return ERR_PTR(count);
3364
3365 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3366 GFP_KERNEL);
3367 if (!descs)
3368 return ERR_PTR(-ENOMEM);
3369
3370 for (descs->ndescs = 0; descs->ndescs < count; ) {
3371 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3372 if (IS_ERR(desc)) {
3373 gpiod_put_array(descs);
3374 return ERR_CAST(desc);
3375 }
3376 descs->desc[descs->ndescs] = desc;
3377 descs->ndescs++;
3378 }
3379 return descs;
3380}
3381EXPORT_SYMBOL_GPL(gpiod_get_array);
3382
3383/**
3384 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3385 * function
3386 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3387 * @con_id: function within the GPIO consumer
3388 * @flags: optional GPIO initialization flags
3389 *
3390 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3391 * assigned to the requested function it will return NULL.
3392 */
3393struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3394 const char *con_id,
3395 enum gpiod_flags flags)
3396{
3397 struct gpio_descs *descs;
3398
3399 descs = gpiod_get_array(dev, con_id, flags);
3400 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3401 return NULL;
3402
3403 return descs;
3404}
3405EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3406
3407/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003408 * gpiod_put - dispose of a GPIO descriptor
3409 * @desc: GPIO descriptor to dispose of
3410 *
3411 * No descriptor can be used after gpiod_put() has been called on it.
3412 */
3413void gpiod_put(struct gpio_desc *desc)
3414{
3415 gpiod_free(desc);
3416}
3417EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003418
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003419/**
3420 * gpiod_put_array - dispose of multiple GPIO descriptors
3421 * @descs: struct gpio_descs containing an array of descriptors
3422 */
3423void gpiod_put_array(struct gpio_descs *descs)
3424{
3425 unsigned int i;
3426
3427 for (i = 0; i < descs->ndescs; i++)
3428 gpiod_put(descs->desc[i]);
3429
3430 kfree(descs);
3431}
3432EXPORT_SYMBOL_GPL(gpiod_put_array);
3433
Linus Walleij3c702e92015-10-21 15:29:53 +02003434static int __init gpiolib_dev_init(void)
3435{
3436 int ret;
3437
3438 /* Register GPIO sysfs bus */
3439 ret = bus_register(&gpio_bus_type);
3440 if (ret < 0) {
3441 pr_err("gpiolib: could not register GPIO bus type\n");
3442 return ret;
3443 }
3444
3445 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3446 if (ret < 0) {
3447 pr_err("gpiolib: failed to allocate char dev region\n");
3448 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003449 } else {
3450 gpiolib_initialized = true;
3451 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003452 }
3453 return ret;
3454}
3455core_initcall(gpiolib_dev_init);
3456
David Brownelld2876d02008-02-04 22:28:20 -08003457#ifdef CONFIG_DEBUG_FS
3458
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003459static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003460{
3461 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003462 struct gpio_chip *chip = gdev->chip;
3463 unsigned gpio = gdev->base;
3464 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003465 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003466 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003467
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003468 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003469 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3470 if (gdesc->name) {
3471 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3472 gpio, gdesc->name);
3473 }
David Brownelld2876d02008-02-04 22:28:20 -08003474 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003475 }
David Brownelld2876d02008-02-04 22:28:20 -08003476
Alexandre Courbot372e7222013-02-03 01:29:29 +09003477 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003478 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003479 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003480 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3481 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003482 is_out ? "out" : "in ",
3483 chip->get
3484 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003485 : "? ",
3486 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003487 seq_printf(s, "\n");
3488 }
3489}
3490
Thierry Redingf9c4a312012-04-12 13:26:01 +02003491static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003492{
Grant Likely362432a2013-02-09 09:41:49 +00003493 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003494 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003495 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003496
3497 s->private = "";
3498
Grant Likely362432a2013-02-09 09:41:49 +00003499 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003500 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003501 if (index-- == 0) {
3502 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003503 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003504 }
3505 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003506
3507 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003508}
3509
3510static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3511{
Grant Likely362432a2013-02-09 09:41:49 +00003512 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003513 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003514 void *ret = NULL;
3515
Grant Likely362432a2013-02-09 09:41:49 +00003516 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003517 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003518 ret = NULL;
3519 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003520 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003521 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003522
3523 s->private = "\n";
3524 ++*pos;
3525
3526 return ret;
3527}
3528
3529static void gpiolib_seq_stop(struct seq_file *s, void *v)
3530{
3531}
3532
3533static int gpiolib_seq_show(struct seq_file *s, void *v)
3534{
Linus Walleijff2b1352015-10-20 11:10:38 +02003535 struct gpio_device *gdev = v;
3536 struct gpio_chip *chip = gdev->chip;
3537 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003538
Linus Walleijff2b1352015-10-20 11:10:38 +02003539 if (!chip) {
3540 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3541 dev_name(&gdev->dev));
3542 return 0;
3543 }
3544
3545 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3546 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003547 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02003548 parent = chip->parent;
3549 if (parent)
3550 seq_printf(s, ", parent: %s/%s",
3551 parent->bus ? parent->bus->name : "no-bus",
3552 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02003553 if (chip->label)
3554 seq_printf(s, ", %s", chip->label);
3555 if (chip->can_sleep)
3556 seq_printf(s, ", can sleep");
3557 seq_printf(s, ":\n");
3558
3559 if (chip->dbg_show)
3560 chip->dbg_show(s, chip);
3561 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003562 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003563
David Brownelld2876d02008-02-04 22:28:20 -08003564 return 0;
3565}
3566
Thierry Redingf9c4a312012-04-12 13:26:01 +02003567static const struct seq_operations gpiolib_seq_ops = {
3568 .start = gpiolib_seq_start,
3569 .next = gpiolib_seq_next,
3570 .stop = gpiolib_seq_stop,
3571 .show = gpiolib_seq_show,
3572};
3573
David Brownelld2876d02008-02-04 22:28:20 -08003574static int gpiolib_open(struct inode *inode, struct file *file)
3575{
Thierry Redingf9c4a312012-04-12 13:26:01 +02003576 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08003577}
3578
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003579static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02003580 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08003581 .open = gpiolib_open,
3582 .read = seq_read,
3583 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02003584 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08003585};
3586
3587static int __init gpiolib_debugfs_init(void)
3588{
3589 /* /sys/kernel/debug/gpio */
3590 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3591 NULL, NULL, &gpiolib_operations);
3592 return 0;
3593}
3594subsys_initcall(gpiolib_debugfs_init);
3595
3596#endif /* DEBUG_FS */