blob: 5a21a6acf8af98d577c429b5ea8e8f81d151b47b [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020018#include <linux/pinctrl/consumer.h>
Linus 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 Walleijf881bab02015-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 Walleijf881bab02015-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 Walleijf881bab02015-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 Walleijf881bab02015-09-23 16:20:43 -0700269
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100270 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700271 continue;
272
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100273 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700274 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100275 return desc;
Linus Walleijf881bab02015-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 Walleijf881bab02015-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 Walleijf881bab02015-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 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200477 /* Let i point at the last handle */
478 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200479 lh->numdescs = handlereq.lines;
480
481 fd = anon_inode_getfd("gpio-linehandle",
482 &linehandle_fileops,
483 lh,
484 O_RDONLY | O_CLOEXEC);
485 if (fd < 0) {
486 ret = fd;
487 goto out_free_descs;
488 }
489
490 handlereq.fd = fd;
491 if (copy_to_user(ip, &handlereq, sizeof(handlereq)))
492 return -EFAULT;
493
494 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
495 lh->numdescs);
496
497 return 0;
498
499out_free_descs:
500 for (; i >= 0; i--)
501 gpiod_free(lh->descs[i]);
502 kfree(lh->label);
503out_free_lh:
504 kfree(lh);
505 put_device(&gdev->dev);
506 return ret;
507}
508
Linus Walleij61f922d2016-06-02 11:30:15 +0200509/*
510 * GPIO line event management
511 */
512
513/**
514 * struct lineevent_state - contains the state of a userspace event
515 * @gdev: the GPIO device the event pertains to
516 * @label: consumer label used to tag descriptors
517 * @desc: the GPIO descriptor held by this event
518 * @eflags: the event flags this line was requested with
519 * @irq: the interrupt that trigger in response to events on this GPIO
520 * @wait: wait queue that handles blocking reads of events
521 * @events: KFIFO for the GPIO events
522 * @read_lock: mutex lock to protect reads from colliding with adding
523 * new events to the FIFO
524 */
525struct lineevent_state {
526 struct gpio_device *gdev;
527 const char *label;
528 struct gpio_desc *desc;
529 u32 eflags;
530 int irq;
531 wait_queue_head_t wait;
532 DECLARE_KFIFO(events, struct gpioevent_data, 16);
533 struct mutex read_lock;
534};
535
536static unsigned int lineevent_poll(struct file *filep,
537 struct poll_table_struct *wait)
538{
539 struct lineevent_state *le = filep->private_data;
540 unsigned int events = 0;
541
542 poll_wait(filep, &le->wait, wait);
543
544 if (!kfifo_is_empty(&le->events))
545 events = POLLIN | POLLRDNORM;
546
547 return events;
548}
549
550
551static ssize_t lineevent_read(struct file *filep,
552 char __user *buf,
553 size_t count,
554 loff_t *f_ps)
555{
556 struct lineevent_state *le = filep->private_data;
557 unsigned int copied;
558 int ret;
559
560 if (count < sizeof(struct gpioevent_data))
561 return -EINVAL;
562
563 do {
564 if (kfifo_is_empty(&le->events)) {
565 if (filep->f_flags & O_NONBLOCK)
566 return -EAGAIN;
567
568 ret = wait_event_interruptible(le->wait,
569 !kfifo_is_empty(&le->events));
570 if (ret)
571 return ret;
572 }
573
574 if (mutex_lock_interruptible(&le->read_lock))
575 return -ERESTARTSYS;
576 ret = kfifo_to_user(&le->events, buf, count, &copied);
577 mutex_unlock(&le->read_lock);
578
579 if (ret)
580 return ret;
581
582 /*
583 * If we couldn't read anything from the fifo (a different
584 * thread might have been faster) we either return -EAGAIN if
585 * the file descriptor is non-blocking, otherwise we go back to
586 * sleep and wait for more data to arrive.
587 */
588 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
589 return -EAGAIN;
590
591 } while (copied == 0);
592
593 return copied;
594}
595
596static int lineevent_release(struct inode *inode, struct file *filep)
597{
598 struct lineevent_state *le = filep->private_data;
599 struct gpio_device *gdev = le->gdev;
600
601 free_irq(le->irq, le);
602 gpiod_free(le->desc);
603 kfree(le->label);
604 kfree(le);
605 put_device(&gdev->dev);
606 return 0;
607}
608
609static long lineevent_ioctl(struct file *filep, unsigned int cmd,
610 unsigned long arg)
611{
612 struct lineevent_state *le = filep->private_data;
613 void __user *ip = (void __user *)arg;
614 struct gpiohandle_data ghd;
615
616 /*
617 * We can get the value for an event line but not set it,
618 * because it is input by definition.
619 */
620 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
621 int val;
622
623 val = gpiod_get_value_cansleep(le->desc);
624 if (val < 0)
625 return val;
626 ghd.values[0] = val;
627
628 if (copy_to_user(ip, &ghd, sizeof(ghd)))
629 return -EFAULT;
630
631 return 0;
632 }
633 return -EINVAL;
634}
635
636#ifdef CONFIG_COMPAT
637static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
638 unsigned long arg)
639{
640 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
641}
642#endif
643
644static const struct file_operations lineevent_fileops = {
645 .release = lineevent_release,
646 .read = lineevent_read,
647 .poll = lineevent_poll,
648 .owner = THIS_MODULE,
649 .llseek = noop_llseek,
650 .unlocked_ioctl = lineevent_ioctl,
651#ifdef CONFIG_COMPAT
652 .compat_ioctl = lineevent_ioctl_compat,
653#endif
654};
655
Ben Dooks33265b12016-06-17 16:03:13 +0100656static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200657{
658 struct lineevent_state *le = p;
659 struct gpioevent_data ge;
660 int ret;
661
662 ge.timestamp = ktime_get_real_ns();
663
664 if (le->eflags & GPIOEVENT_REQUEST_BOTH_EDGES) {
665 int level = gpiod_get_value_cansleep(le->desc);
666
667 if (level)
668 /* Emit low-to-high event */
669 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
670 else
671 /* Emit high-to-low event */
672 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
673 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
674 /* Emit low-to-high event */
675 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
676 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
677 /* Emit high-to-low event */
678 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200679 } else {
680 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200681 }
682
683 ret = kfifo_put(&le->events, ge);
684 if (ret != 0)
685 wake_up_poll(&le->wait, POLLIN);
686
687 return IRQ_HANDLED;
688}
689
690static int lineevent_create(struct gpio_device *gdev, void __user *ip)
691{
692 struct gpioevent_request eventreq;
693 struct lineevent_state *le;
694 struct gpio_desc *desc;
695 u32 offset;
696 u32 lflags;
697 u32 eflags;
698 int fd;
699 int ret;
700 int irqflags = 0;
701
702 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
703 return -EFAULT;
704
705 le = kzalloc(sizeof(*le), GFP_KERNEL);
706 if (!le)
707 return -ENOMEM;
708 le->gdev = gdev;
709 get_device(&gdev->dev);
710
711 /* Make sure this is terminated */
712 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
713 if (strlen(eventreq.consumer_label)) {
714 le->label = kstrdup(eventreq.consumer_label,
715 GFP_KERNEL);
716 if (!le->label) {
717 ret = -ENOMEM;
718 goto out_free_le;
719 }
720 }
721
722 offset = eventreq.lineoffset;
723 lflags = eventreq.handleflags;
724 eflags = eventreq.eventflags;
725
726 /* This is just wrong: we don't look for events on output lines */
727 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
728 ret = -EINVAL;
729 goto out_free_label;
730 }
731
732 desc = &gdev->descs[offset];
733 ret = gpiod_request(desc, le->label);
734 if (ret)
735 goto out_free_desc;
736 le->desc = desc;
737 le->eflags = eflags;
738
739 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
740 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
741 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
742 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
743 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
744 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
745
746 ret = gpiod_direction_input(desc);
747 if (ret)
748 goto out_free_desc;
749
750 le->irq = gpiod_to_irq(desc);
751 if (le->irq <= 0) {
752 ret = -ENODEV;
753 goto out_free_desc;
754 }
755
756 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
757 irqflags |= IRQF_TRIGGER_RISING;
758 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
759 irqflags |= IRQF_TRIGGER_FALLING;
760 irqflags |= IRQF_ONESHOT;
761 irqflags |= IRQF_SHARED;
762
763 INIT_KFIFO(le->events);
764 init_waitqueue_head(&le->wait);
765 mutex_init(&le->read_lock);
766
767 /* Request a thread to read the events */
768 ret = request_threaded_irq(le->irq,
769 NULL,
770 lineevent_irq_thread,
771 irqflags,
772 le->label,
773 le);
774 if (ret)
775 goto out_free_desc;
776
777 fd = anon_inode_getfd("gpio-event",
778 &lineevent_fileops,
779 le,
780 O_RDONLY | O_CLOEXEC);
781 if (fd < 0) {
782 ret = fd;
783 goto out_free_irq;
784 }
785
786 eventreq.fd = fd;
787 if (copy_to_user(ip, &eventreq, sizeof(eventreq)))
788 return -EFAULT;
789
790 return 0;
791
792out_free_irq:
793 free_irq(le->irq, le);
794out_free_desc:
795 gpiod_free(le->desc);
796out_free_label:
797 kfree(le->label);
798out_free_le:
799 kfree(le);
800 put_device(&gdev->dev);
801 return ret;
802}
803
Linus Walleij3c702e92015-10-21 15:29:53 +0200804/**
805 * gpio_ioctl() - ioctl handler for the GPIO chardev
806 */
807static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
808{
809 struct gpio_device *gdev = filp->private_data;
810 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200811 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200812
813 /* We fail any subsequent ioctl():s when the chip is gone */
814 if (!chip)
815 return -ENODEV;
816
Linus Walleij521a2ad2016-02-12 22:25:22 +0100817 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200818 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100819 struct gpiochip_info chipinfo;
820
Linus Walleij3c702e92015-10-21 15:29:53 +0200821 strncpy(chipinfo.name, dev_name(&gdev->dev),
822 sizeof(chipinfo.name));
823 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100824 strncpy(chipinfo.label, gdev->label,
825 sizeof(chipinfo.label));
826 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100827 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200828 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
829 return -EFAULT;
830 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100831 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
832 struct gpioline_info lineinfo;
833 struct gpio_desc *desc;
834
835 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
836 return -EFAULT;
837 if (lineinfo.line_offset > gdev->ngpio)
838 return -EINVAL;
839
840 desc = &gdev->descs[lineinfo.line_offset];
841 if (desc->name) {
842 strncpy(lineinfo.name, desc->name,
843 sizeof(lineinfo.name));
844 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
845 } else {
846 lineinfo.name[0] = '\0';
847 }
848 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100849 strncpy(lineinfo.consumer, desc->label,
850 sizeof(lineinfo.consumer));
851 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100852 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100853 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100854 }
855
856 /*
857 * Userspace only need to know that the kernel is using
858 * this GPIO so it can't use it.
859 */
860 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100861 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
862 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
863 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
864 test_bit(FLAG_EXPORT, &desc->flags) ||
865 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100866 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100867 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100868 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100869 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100870 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100871 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100872 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100873 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100874 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
875
876 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
877 return -EFAULT;
878 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200879 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
880 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +0200881 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
882 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +0200883 }
884 return -EINVAL;
885}
886
Linus Walleij8b92e172016-05-27 14:24:04 +0200887#ifdef CONFIG_COMPAT
888static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
889 unsigned long arg)
890{
891 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
892}
893#endif
894
Linus Walleij3c702e92015-10-21 15:29:53 +0200895/**
896 * gpio_chrdev_open() - open the chardev for ioctl operations
897 * @inode: inode for this chardev
898 * @filp: file struct for storing private data
899 * Returns 0 on success
900 */
901static int gpio_chrdev_open(struct inode *inode, struct file *filp)
902{
903 struct gpio_device *gdev = container_of(inode->i_cdev,
904 struct gpio_device, chrdev);
905
906 /* Fail on open if the backing gpiochip is gone */
907 if (!gdev || !gdev->chip)
908 return -ENODEV;
909 get_device(&gdev->dev);
910 filp->private_data = gdev;
911 return 0;
912}
913
914/**
915 * gpio_chrdev_release() - close chardev after ioctl operations
916 * @inode: inode for this chardev
917 * @filp: file struct for storing private data
918 * Returns 0 on success
919 */
920static int gpio_chrdev_release(struct inode *inode, struct file *filp)
921{
922 struct gpio_device *gdev = container_of(inode->i_cdev,
923 struct gpio_device, chrdev);
924
925 if (!gdev)
926 return -ENODEV;
927 put_device(&gdev->dev);
928 return 0;
929}
930
931
932static const struct file_operations gpio_fileops = {
933 .release = gpio_chrdev_release,
934 .open = gpio_chrdev_open,
935 .owner = THIS_MODULE,
936 .llseek = noop_llseek,
937 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +0200938#ifdef CONFIG_COMPAT
939 .compat_ioctl = gpio_ioctl_compat,
940#endif
Linus Walleij3c702e92015-10-21 15:29:53 +0200941};
942
Linus Walleijff2b1352015-10-20 11:10:38 +0200943static void gpiodevice_release(struct device *dev)
944{
945 struct gpio_device *gdev = dev_get_drvdata(dev);
946
Linus Walleij3c702e92015-10-21 15:29:53 +0200947 cdev_del(&gdev->chrdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200948 list_del(&gdev->list);
949 ida_simple_remove(&gpio_ida, gdev->id);
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700950 kfree(gdev->label);
951 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +0100952 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200953}
954
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700955static int gpiochip_setup_dev(struct gpio_device *gdev)
956{
957 int status;
958
959 cdev_init(&gdev->chrdev, &gpio_fileops);
960 gdev->chrdev.owner = THIS_MODULE;
961 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
962 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
963 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
964 if (status < 0)
965 chip_warn(gdev->chip, "failed to add char device %d:%d\n",
966 MAJOR(gpio_devt), gdev->id);
967 else
968 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
969 MAJOR(gpio_devt), gdev->id);
970 status = device_add(&gdev->dev);
971 if (status)
972 goto err_remove_chardev;
973
974 status = gpiochip_sysfs_register(gdev);
975 if (status)
976 goto err_remove_device;
977
978 /* From this point, the .release() function cleans up gpio_device */
979 gdev->dev.release = gpiodevice_release;
980 get_device(&gdev->dev);
981 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
982 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
983 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
984
985 return 0;
986
987err_remove_device:
988 device_del(&gdev->dev);
989err_remove_chardev:
990 cdev_del(&gdev->chrdev);
991 return status;
992}
993
994static void gpiochip_setup_devs(void)
995{
996 struct gpio_device *gdev;
997 int err;
998
999 list_for_each_entry(gdev, &gpio_devices, list) {
1000 err = gpiochip_setup_dev(gdev);
1001 if (err)
1002 pr_err("%s: Failed to initialize gpio device (%d)\n",
1003 dev_name(&gdev->dev), err);
1004 }
1005}
1006
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001007/**
Linus Walleijb08ea352015-12-03 15:14:13 +01001008 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -08001009 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001010 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -08001011 *
1012 * Returns a negative errno if the chip can't be registered, such as
1013 * because the chip->base is invalid or already associated with a
1014 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001015 *
Linus Walleijb08ea352015-12-03 15:14:13 +01001016 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001017 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d82008-07-25 01:46:07 -07001018 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1019 * for GPIOs will fail rudely.
1020 *
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001021 * gpiochip_add_data() must only be called after gpiolib initialization,
1022 * ie after core_initcall().
1023 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001024 * If chip->base is negative, this requests dynamic assignment of
1025 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001026 */
Linus Walleijb08ea352015-12-03 15:14:13 +01001027int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -08001028{
1029 unsigned long flags;
1030 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001031 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001032 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001033 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001034
Linus Walleijff2b1352015-10-20 11:10:38 +02001035 /*
1036 * First: allocate and populate the internal stat container, and
1037 * set up the struct device.
1038 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001039 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001040 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001041 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001042 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001043 gdev->chip = chip;
1044 chip->gpiodev = gdev;
1045 if (chip->parent) {
1046 gdev->dev.parent = chip->parent;
1047 gdev->dev.of_node = chip->parent->of_node;
1048 } else {
1049#ifdef CONFIG_OF_GPIO
1050 /* If the gpiochip has an assigned OF node this takes precedence */
1051 if (chip->of_node)
1052 gdev->dev.of_node = chip->of_node;
1053#endif
1054 }
1055 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1056 if (gdev->id < 0) {
1057 status = gdev->id;
1058 goto err_free_gdev;
1059 }
1060 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1061 device_initialize(&gdev->dev);
1062 dev_set_drvdata(&gdev->dev, gdev);
1063 if (chip->parent && chip->parent->driver)
1064 gdev->owner = chip->parent->driver->owner;
1065 else if (chip->owner)
1066 /* TODO: remove chip->owner */
1067 gdev->owner = chip->owner;
1068 else
1069 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001070
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001071 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001072 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001073 status = -ENOMEM;
1074 goto err_free_gdev;
1075 }
1076
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001077 if (chip->ngpio == 0) {
1078 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001079 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001080 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001081 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001082
1083 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001084 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001085 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001086 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001087 if (!gdev->label) {
1088 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001089 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001090 }
1091
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001092 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001093 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001094
David Brownelld2876d02008-02-04 22:28:20 -08001095 spin_lock_irqsave(&gpio_lock, flags);
1096
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001097 /*
1098 * TODO: this allocates a Linux GPIO number base in the global
1099 * GPIO numberspace for this chip. In the long run we want to
1100 * get *rid* of this numberspace and use only descriptors, but
1101 * it may be a pipe dream. It will not happen before we get rid
1102 * of the sysfs interface anyways.
1103 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001104 if (base < 0) {
1105 base = gpiochip_find_base(chip->ngpio);
1106 if (base < 0) {
1107 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001108 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001109 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001110 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001111 /*
1112 * TODO: it should not be necessary to reflect the assigned
1113 * base outside of the GPIO subsystem. Go over drivers and
1114 * see if anyone makes use of this, else drop this and assign
1115 * a poison instead.
1116 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001117 chip->base = base;
1118 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001119 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001120
Linus Walleijff2b1352015-10-20 11:10:38 +02001121 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001122 if (status) {
1123 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001124 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001125 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001126
Linus Walleij545ebd92016-05-30 17:11:59 +02001127 spin_unlock_irqrestore(&gpio_lock, flags);
1128
Linus Walleijff2b1352015-10-20 11:10:38 +02001129 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001130 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001131
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001132 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +02001133 /*
1134 * REVISIT: most hardware initializes GPIOs as inputs
1135 * (often with pullups enabled) so power usage is
1136 * minimized. Linux code should set the gpio direction
1137 * first thing; but until it does, and in case
1138 * chip->get_direction is not set, we may expose the
1139 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001140 */
Linus Walleij72d32002016-04-28 13:33:59 +02001141
1142 if (chip->get_direction) {
1143 /*
1144 * If we have .get_direction, set up the initial
1145 * direction flag from the hardware.
1146 */
1147 int dir = chip->get_direction(chip, i);
1148
1149 if (!dir)
1150 set_bit(FLAG_IS_OUT, &desc->flags);
1151 } else if (!chip->direction_input) {
1152 /*
1153 * If the chip lacks the .direction_input callback
1154 * we logically assume all lines are outputs.
1155 */
1156 set_bit(FLAG_IS_OUT, &desc->flags);
1157 }
David Brownelld2876d02008-02-04 22:28:20 -08001158 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001159
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301160#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001161 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301162#endif
1163
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001164 status = gpiochip_set_desc_names(chip);
1165 if (status)
1166 goto err_remove_from_list;
1167
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001168 status = of_gpiochip_add(chip);
1169 if (status)
1170 goto err_remove_chip;
1171
Mika Westerberg664e3e52014-01-08 12:40:54 +02001172 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001173
Linus Walleij3c702e92015-10-21 15:29:53 +02001174 /*
1175 * By first adding the chardev, and then adding the device,
1176 * we get a device node entry in sysfs under
1177 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1178 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001179 * We can do this only if gpiolib has been initialized.
1180 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001181 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001182 if (gpiolib_initialized) {
1183 status = gpiochip_setup_dev(gdev);
1184 if (status)
1185 goto err_remove_chip;
1186 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001187 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001188
Johan Hovold225fce82015-01-12 17:12:25 +01001189err_remove_chip:
1190 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001191 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001192 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001193err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001194 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001195 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001196 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001197err_free_label:
1198 kfree(gdev->label);
1199err_free_descs:
1200 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001201err_free_gdev:
1202 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001203 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001204 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001205 gdev->base, gdev->base + gdev->ngpio - 1,
1206 chip->label ? : "generic");
1207 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001208 return status;
1209}
Linus Walleijb08ea352015-12-03 15:14:13 +01001210EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -08001211
1212/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001213 * gpiochip_get_data() - get per-subdriver data for the chip
1214 */
1215void *gpiochip_get_data(struct gpio_chip *chip)
1216{
1217 return chip->gpiodev->data;
1218}
1219EXPORT_SYMBOL_GPL(gpiochip_get_data);
1220
1221/**
David Brownelld2876d02008-02-04 22:28:20 -08001222 * gpiochip_remove() - unregister a gpio_chip
1223 * @chip: the chip to unregister
1224 *
1225 * A gpio_chip with any GPIOs still requested may not be removed.
1226 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001227void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001228{
Linus Walleijff2b1352015-10-20 11:10:38 +02001229 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001230 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001231 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001232 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001233 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001234
Linus Walleijff2b1352015-10-20 11:10:38 +02001235 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001236 gpiochip_sysfs_unregister(gdev);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001237 /* Numb the device, cancelling all outstanding operations */
1238 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001239 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001240 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001241 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -06001242 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001243 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001244 /*
1245 * We accept no more calls into the driver from this point, so
1246 * NULL the driver data pointer
1247 */
1248 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001249
Johan Hovold6798aca2015-01-12 17:12:28 +01001250 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001251 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001252 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001253 if (test_bit(FLAG_REQUESTED, &desc->flags))
1254 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001255 }
David Brownelld2876d02008-02-04 22:28:20 -08001256 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001257
Johan Hovoldfab28b82015-05-04 17:10:27 +02001258 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001259 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001260 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001261
Linus Walleijff2b1352015-10-20 11:10:38 +02001262 /*
1263 * The gpiochip side puts its use of the device to rest here:
1264 * if there are no userspace clients, the chardev and device will
1265 * be removed, else it will be dangling until the last user is
1266 * gone.
1267 */
1268 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001269}
1270EXPORT_SYMBOL_GPL(gpiochip_remove);
1271
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301272static void devm_gpio_chip_release(struct device *dev, void *res)
1273{
1274 struct gpio_chip *chip = *(struct gpio_chip **)res;
1275
1276 gpiochip_remove(chip);
1277}
1278
1279static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1280
1281{
1282 struct gpio_chip **r = res;
1283
1284 if (!r || !*r) {
1285 WARN_ON(!r || !*r);
1286 return 0;
1287 }
1288
1289 return *r == data;
1290}
1291
1292/**
1293 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1294 * @dev: the device pointer on which irq_chip belongs to.
1295 * @chip: the chip to register, with chip->base initialized
1296 * Context: potentially before irqs will work
1297 *
1298 * Returns a negative errno if the chip can't be registered, such as
1299 * because the chip->base is invalid or already associated with a
1300 * different chip. Otherwise it returns zero as a success code.
1301 *
1302 * The gpio chip automatically be released when the device is unbound.
1303 */
1304int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1305 void *data)
1306{
1307 struct gpio_chip **ptr;
1308 int ret;
1309
1310 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1311 GFP_KERNEL);
1312 if (!ptr)
1313 return -ENOMEM;
1314
1315 ret = gpiochip_add_data(chip, data);
1316 if (ret < 0) {
1317 devres_free(ptr);
1318 return ret;
1319 }
1320
1321 *ptr = chip;
1322 devres_add(dev, ptr);
1323
1324 return 0;
1325}
1326EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1327
1328/**
1329 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1330 * @dev: device for which which resource was allocated
1331 * @chip: the chip to remove
1332 *
1333 * A gpio_chip with any GPIOs still requested may not be removed.
1334 */
1335void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1336{
1337 int ret;
1338
1339 ret = devres_release(dev, devm_gpio_chip_release,
1340 devm_gpio_chip_match, chip);
1341 if (!ret)
1342 WARN_ON(ret);
1343}
1344EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1345
Grant Likely594fa262010-06-08 07:48:16 -06001346/**
1347 * gpiochip_find() - iterator for locating a specific gpio_chip
1348 * @data: data to pass to match function
1349 * @callback: Callback function to check gpio_chip
1350 *
1351 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1352 * determined by a user supplied @match callback. The callback should return
1353 * 0 if the device doesn't match and non-zero if it does. If the callback is
1354 * non-zero, this function will return to the caller and not iterate over any
1355 * more gpio_chips.
1356 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001357struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001358 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001359 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001360{
Linus Walleijff2b1352015-10-20 11:10:38 +02001361 struct gpio_device *gdev;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001362 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001363 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001364
1365 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001366 list_for_each_entry(gdev, &gpio_devices, list)
1367 if (match(gdev->chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001368 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001369
1370 /* No match? */
Linus Walleijff2b1352015-10-20 11:10:38 +02001371 if (&gdev->list == &gpio_devices)
Alexandre Courbot125eef92013-02-03 01:29:26 +09001372 chip = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +02001373 else
1374 chip = gdev->chip;
1375
Grant Likely594fa262010-06-08 07:48:16 -06001376 spin_unlock_irqrestore(&gpio_lock, flags);
1377
1378 return chip;
1379}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001380EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001381
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001382static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1383{
1384 const char *name = data;
1385
1386 return !strcmp(chip->label, name);
1387}
1388
1389static struct gpio_chip *find_chip_by_name(const char *name)
1390{
1391 return gpiochip_find((void *)name, gpiochip_match_name);
1392}
1393
Linus Walleij14250522014-03-25 10:40:18 +01001394#ifdef CONFIG_GPIOLIB_IRQCHIP
1395
1396/*
1397 * The following is irqchip helper code for gpiochips.
1398 */
1399
1400/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001401 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
1402 * @gpiochip: the gpiochip to set the irqchip chain to
1403 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001404 * @parent_irq: the irq number corresponding to the parent IRQ for this
1405 * chained irqchip
1406 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001407 * coming out of the gpiochip. If the interrupt is nested rather than
1408 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001409 */
1410void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1411 struct irq_chip *irqchip,
1412 int parent_irq,
1413 irq_flow_handler_t parent_handler)
1414{
Linus Walleij83141a72014-09-26 13:50:12 +02001415 unsigned int offset;
1416
Linus Walleij83141a72014-09-26 13:50:12 +02001417 if (!gpiochip->irqdomain) {
1418 chip_err(gpiochip, "called %s before setting up irqchip\n",
1419 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001420 return;
1421 }
1422
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001423 if (parent_handler) {
1424 if (gpiochip->can_sleep) {
1425 chip_err(gpiochip,
1426 "you cannot have chained interrupts on a "
1427 "chip that may sleep\n");
1428 return;
1429 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001430 /*
1431 * The parent irqchip is already using the chip_data for this
1432 * irqchip, so our callbacks simply use the handler_data.
1433 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001434 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1435 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001436
1437 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001438 }
Linus Walleij83141a72014-09-26 13:50:12 +02001439
1440 /* Set the parent IRQ for all affected IRQs */
1441 for (offset = 0; offset < gpiochip->ngpio; offset++)
1442 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
1443 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +01001444}
1445EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1446
1447/**
1448 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1449 * @d: the irqdomain used by this irqchip
1450 * @irq: the global irq number used by this GPIO irqchip irq
1451 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1452 *
1453 * This function will set up the mapping for a certain IRQ line on a
1454 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1455 * stored inside the gpiochip.
1456 */
1457static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1458 irq_hw_number_t hwirq)
1459{
1460 struct gpio_chip *chip = d->host_data;
1461
Linus Walleij14250522014-03-25 10:40:18 +01001462 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001463 /*
1464 * This lock class tells lockdep that GPIO irqs are in a different
1465 * category than their parents, so it won't report false recursion.
1466 */
1467 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +02001468 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001469 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +03001470 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001471 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001472 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001473
Linus Walleij1333b902014-04-23 16:45:12 +02001474 /*
1475 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1476 * is passed as default type.
1477 */
1478 if (chip->irq_default_type != IRQ_TYPE_NONE)
1479 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001480
1481 return 0;
1482}
1483
Linus Walleijc3626fd2014-03-28 20:42:01 +01001484static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1485{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001486 struct gpio_chip *chip = d->host_data;
1487
Linus Walleij1c8732b2014-04-09 13:34:39 +02001488 if (chip->can_sleep)
1489 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001490 irq_set_chip_and_handler(irq, NULL, NULL);
1491 irq_set_chip_data(irq, NULL);
1492}
1493
Linus Walleij14250522014-03-25 10:40:18 +01001494static const struct irq_domain_ops gpiochip_domain_ops = {
1495 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001496 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001497 /* Virtually all GPIO irqchips are twocell:ed */
1498 .xlate = irq_domain_xlate_twocell,
1499};
1500
1501static int gpiochip_irq_reqres(struct irq_data *d)
1502{
1503 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1504
Linus Walleijff2b1352015-10-20 11:10:38 +02001505 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001506 return -ENODEV;
1507
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001508 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001509 chip_err(chip,
1510 "unable to lock HW IRQ %lu for IRQ\n",
1511 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001512 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001513 return -EINVAL;
1514 }
1515 return 0;
1516}
1517
1518static void gpiochip_irq_relres(struct irq_data *d)
1519{
1520 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1521
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001522 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001523 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001524}
1525
1526static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1527{
1528 return irq_find_mapping(chip->irqdomain, offset);
1529}
1530
1531/**
1532 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1533 * @gpiochip: the gpiochip to remove the irqchip from
1534 *
1535 * This is called only from gpiochip_remove()
1536 */
1537static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1538{
Linus Walleijc3626fd2014-03-28 20:42:01 +01001539 unsigned int offset;
1540
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001541 acpi_gpiochip_free_interrupts(gpiochip);
1542
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001543 if (gpiochip->irq_parent) {
1544 irq_set_chained_handler(gpiochip->irq_parent, NULL);
1545 irq_set_handler_data(gpiochip->irq_parent, NULL);
1546 }
1547
Linus Walleijc3626fd2014-03-28 20:42:01 +01001548 /* Remove all IRQ mappings and delete the domain */
1549 if (gpiochip->irqdomain) {
1550 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +03001551 irq_dispose_mapping(
1552 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +01001553 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001554 }
Linus Walleij14250522014-03-25 10:40:18 +01001555
1556 if (gpiochip->irqchip) {
1557 gpiochip->irqchip->irq_request_resources = NULL;
1558 gpiochip->irqchip->irq_release_resources = NULL;
1559 gpiochip->irqchip = NULL;
1560 }
1561}
1562
1563/**
1564 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
1565 * @gpiochip: the gpiochip to add the irqchip to
1566 * @irqchip: the irqchip to add to the gpiochip
1567 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1568 * allocate gpiochip irqs from
1569 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001570 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1571 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001572 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001573 *
1574 * This function closely associates a certain irqchip with a certain
1575 * gpiochip, providing an irq domain to translate the local IRQs to
1576 * global irqs in the gpiolib core, and making sure that the gpiochip
1577 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001578 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001579 * from the gpiochip passed as chip data. An irqdomain will be stored
1580 * in the gpiochip that shall be used by the driver to handle IRQ number
1581 * translation. The gpiochip will need to be initialized and registered
1582 * before calling this function.
1583 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001584 * This function will handle two cell:ed simple IRQs and assumes all
1585 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001586 * need to be open coded.
1587 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001588int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
1589 struct irq_chip *irqchip,
1590 unsigned int first_irq,
1591 irq_flow_handler_t handler,
1592 unsigned int type,
1593 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001594{
1595 struct device_node *of_node;
1596 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001597 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001598
1599 if (!gpiochip || !irqchip)
1600 return -EINVAL;
1601
Linus Walleij58383c782015-11-04 09:56:26 +01001602 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001603 pr_err("missing gpiochip .dev parent pointer\n");
1604 return -EINVAL;
1605 }
Linus Walleij58383c782015-11-04 09:56:26 +01001606 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001607#ifdef CONFIG_OF_GPIO
1608 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001609 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001610 * FIXME: get rid of this and use gpiochip->parent->of_node
1611 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001612 */
1613 if (gpiochip->of_node)
1614 of_node = gpiochip->of_node;
1615#endif
1616 gpiochip->irqchip = irqchip;
1617 gpiochip->irq_handler = handler;
1618 gpiochip->irq_default_type = type;
1619 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001620 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +01001621 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1622 gpiochip->ngpio, first_irq,
1623 &gpiochip_domain_ops, gpiochip);
1624 if (!gpiochip->irqdomain) {
1625 gpiochip->irqchip = NULL;
1626 return -EINVAL;
1627 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001628
1629 /*
1630 * It is possible for a driver to override this, but only if the
1631 * alternative functions are both implemented.
1632 */
1633 if (!irqchip->irq_request_resources &&
1634 !irqchip->irq_release_resources) {
1635 irqchip->irq_request_resources = gpiochip_irq_reqres;
1636 irqchip->irq_release_resources = gpiochip_irq_relres;
1637 }
Linus Walleij14250522014-03-25 10:40:18 +01001638
1639 /*
1640 * Prepare the mapping since the irqchip shall be orthogonal to
1641 * any gpiochip calls. If the first_irq was zero, this is
1642 * necessary to allocate descriptors for all IRQs.
1643 */
Linus Walleijc3626fd2014-03-28 20:42:01 +01001644 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1645 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
1646 if (offset == 0)
1647 /*
1648 * Store the base into the gpiochip to be used when
1649 * unmapping the irqs.
1650 */
1651 gpiochip->irq_base = irq_base;
1652 }
Linus Walleij14250522014-03-25 10:40:18 +01001653
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001654 acpi_gpiochip_request_interrupts(gpiochip);
1655
Linus Walleij14250522014-03-25 10:40:18 +01001656 return 0;
1657}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001658EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +01001659
1660#else /* CONFIG_GPIOLIB_IRQCHIP */
1661
1662static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
1663
1664#endif /* CONFIG_GPIOLIB_IRQCHIP */
1665
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001666/**
1667 * gpiochip_generic_request() - request the gpio function for a pin
1668 * @chip: the gpiochip owning the GPIO
1669 * @offset: the offset of the GPIO to request for GPIO function
1670 */
1671int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1672{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001673 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001674}
1675EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1676
1677/**
1678 * gpiochip_generic_free() - free the gpio function from a pin
1679 * @chip: the gpiochip to request the gpio function for
1680 * @offset: the offset of the GPIO to free from GPIO function
1681 */
1682void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1683{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001684 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001685}
1686EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1687
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301688#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001689
Linus Walleij3f0f8672012-11-20 12:40:15 +01001690/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001691 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1692 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001693 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001694 * @gpio_offset: the start offset in the current gpio_chip number space
1695 * @pin_group: name of the pin group inside the pin controller
1696 */
1697int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1698 struct pinctrl_dev *pctldev,
1699 unsigned int gpio_offset, const char *pin_group)
1700{
1701 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001702 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001703 int ret;
1704
1705 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1706 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001707 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001708 return -ENOMEM;
1709 }
1710
1711 /* Use local offset as range ID */
1712 pin_range->range.id = gpio_offset;
1713 pin_range->range.gc = chip;
1714 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001715 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001716 pin_range->pctldev = pctldev;
1717
1718 ret = pinctrl_get_group_pins(pctldev, pin_group,
1719 &pin_range->range.pins,
1720 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001721 if (ret < 0) {
1722 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001723 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001724 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001725
1726 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1727
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001728 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1729 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001730 pinctrl_dev_get_devname(pctldev), pin_group);
1731
Linus Walleij20ec3e32016-02-11 11:03:06 +01001732 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001733
1734 return 0;
1735}
1736EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1737
1738/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001739 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1740 * @chip: the gpiochip to add the range for
1741 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001742 * @gpio_offset: the start offset in the current gpio_chip number space
1743 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001744 * @npins: the number of pins from the offset of each pin space (GPIO and
1745 * pin controller) to accumulate in this range
1746 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001747int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001748 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001749 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301750{
1751 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001752 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001753 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301754
Linus Walleij3f0f8672012-11-20 12:40:15 +01001755 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301756 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001757 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001758 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301759 }
1760
Linus Walleij3f0f8672012-11-20 12:40:15 +01001761 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001762 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001763 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301764 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001765 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001766 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301767 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001768 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301769 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001770 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001771 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001772 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001773 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001774 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001775 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001776 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1777 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001778 pinctl_name,
1779 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301780
Linus Walleij20ec3e32016-02-11 11:03:06 +01001781 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001782
1783 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301784}
Linus Walleij165adc92012-11-06 14:49:39 +01001785EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301786
Linus Walleij3f0f8672012-11-20 12:40:15 +01001787/**
1788 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1789 * @chip: the chip to remove all the mappings for
1790 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301791void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1792{
1793 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01001794 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301795
Linus Walleij20ec3e32016-02-11 11:03:06 +01001796 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301797 list_del(&pin_range->node);
1798 pinctrl_remove_gpio_range(pin_range->pctldev,
1799 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001800 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301801 }
1802}
Linus Walleij165adc92012-11-06 14:49:39 +01001803EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1804
1805#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301806
David Brownelld2876d02008-02-04 22:28:20 -08001807/* These "optional" allocation calls help prevent drivers from stomping
1808 * on each other, and help provide better diagnostics in debugfs.
1809 * They're called even less than the "set direction" calls.
1810 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001811static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001812{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001813 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001814 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001815 unsigned long flags;
1816
1817 spin_lock_irqsave(&gpio_lock, flags);
1818
David Brownelld2876d02008-02-04 22:28:20 -08001819 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001820 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001821 */
1822
1823 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1824 desc_set_label(desc, label ? : "?");
1825 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001826 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001827 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001828 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001829 }
1830
1831 if (chip->request) {
1832 /* chip->request may sleep */
1833 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001834 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001835 spin_lock_irqsave(&gpio_lock, flags);
1836
1837 if (status < 0) {
1838 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001839 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001840 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001841 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001842 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001843 if (chip->get_direction) {
1844 /* chip->get_direction may sleep */
1845 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001846 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001847 spin_lock_irqsave(&gpio_lock, flags);
1848 }
David Brownelld2876d02008-02-04 22:28:20 -08001849done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +03001850 if (status < 0) {
1851 /* Clear flags that might have been set by the caller before
1852 * requesting the GPIO.
1853 */
1854 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1855 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1856 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1857 }
Mika Westerberg77c2d792014-03-10 14:54:50 +02001858 spin_unlock_irqrestore(&gpio_lock, flags);
1859 return status;
1860}
1861
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001862/*
1863 * This descriptor validation needs to be inserted verbatim into each
1864 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02001865 * macro to avoid endless duplication. If the desc is NULL it is an
1866 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001867 */
1868#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02001869 if (!desc) \
1870 return 0; \
1871 if (!desc->gdev) { \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001872 pr_warn("%s: invalid GPIO\n", __func__); \
1873 return -EINVAL; \
1874 } \
1875 if ( !desc->gdev->chip ) { \
1876 dev_warn(&desc->gdev->dev, \
1877 "%s: backing chip is gone\n", __func__); \
1878 return 0; \
1879 } } while (0)
1880
1881#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02001882 if (!desc) \
1883 return; \
1884 if (!desc->gdev) { \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001885 pr_warn("%s: invalid GPIO\n", __func__); \
1886 return; \
1887 } \
1888 if (!desc->gdev->chip) { \
1889 dev_warn(&desc->gdev->dev, \
1890 "%s: backing chip is gone\n", __func__); \
1891 return; \
1892 } } while (0)
1893
1894
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001895int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001896{
1897 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001898 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001899
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001900 VALIDATE_DESC(desc);
1901 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001902
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001903 if (try_module_get(gdev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001904 status = __gpiod_request(desc, label);
1905 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001906 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001907 else
1908 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001909 }
1910
David Brownelld2876d02008-02-04 22:28:20 -08001911 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001912 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001913
David Brownelld2876d02008-02-04 22:28:20 -08001914 return status;
1915}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001916
Mika Westerberg77c2d792014-03-10 14:54:50 +02001917static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001918{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001919 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001920 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001921 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001922
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001923 might_sleep();
1924
Alexandre Courbot372e7222013-02-03 01:29:29 +09001925 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07001926
David Brownelld2876d02008-02-04 22:28:20 -08001927 spin_lock_irqsave(&gpio_lock, flags);
1928
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001929 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07001930 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1931 if (chip->free) {
1932 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001933 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001934 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001935 spin_lock_irqsave(&gpio_lock, flags);
1936 }
David Brownelld2876d02008-02-04 22:28:20 -08001937 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001938 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001939 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301940 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301941 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001942 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001943 ret = true;
1944 }
David Brownelld2876d02008-02-04 22:28:20 -08001945
1946 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001947 return ret;
1948}
1949
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001950void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001951{
Linus Walleij33a68e82016-02-11 10:28:44 +01001952 if (desc && desc->gdev && __gpiod_free(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001953 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001954 put_device(&desc->gdev->dev);
1955 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001956 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01001957 }
David Brownelld2876d02008-02-04 22:28:20 -08001958}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001959
David Brownelld2876d02008-02-04 22:28:20 -08001960/**
1961 * gpiochip_is_requested - return string iff signal was requested
1962 * @chip: controller managing the signal
1963 * @offset: of signal within controller's 0..(ngpio - 1) range
1964 *
1965 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001966 * The string returned is the label passed to gpio_request(); if none has been
1967 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001968 *
1969 * This function is for use by GPIO controller drivers. The label can
1970 * help with diagnostics, and knowing that the signal is used as a GPIO
1971 * can help avoid accidentally multiplexing it to another controller.
1972 */
1973const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1974{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001975 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001976
Dirk Behme48b59532015-08-18 18:02:32 +02001977 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001978 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001979
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001980 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001981
Alexandre Courbot372e7222013-02-03 01:29:29 +09001982 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001983 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001984 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001985}
1986EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1987
Mika Westerberg77c2d792014-03-10 14:54:50 +02001988/**
1989 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1990 * @desc: GPIO descriptor to request
1991 * @label: label for the GPIO
1992 *
1993 * Function allows GPIO chip drivers to request and use their own GPIO
1994 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1995 * function will not increase reference count of the GPIO chip module. This
1996 * allows the GPIO chip module to be unloaded as needed (we assume that the
1997 * GPIO chip driver handles freeing the GPIOs it has requested).
1998 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001999struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2000 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002001{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002002 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2003 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002004
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002005 if (IS_ERR(desc)) {
2006 chip_err(chip, "failed to get GPIO descriptor\n");
2007 return desc;
2008 }
2009
2010 err = __gpiod_request(desc, label);
2011 if (err < 0)
2012 return ERR_PTR(err);
2013
2014 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002015}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002016EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002017
2018/**
2019 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2020 * @desc: GPIO descriptor to free
2021 *
2022 * Function frees the given GPIO requested previously with
2023 * gpiochip_request_own_desc().
2024 */
2025void gpiochip_free_own_desc(struct gpio_desc *desc)
2026{
2027 if (desc)
2028 __gpiod_free(desc);
2029}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002030EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002031
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002032/*
2033 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002034 * some cases this is done in early boot, before IRQs are enabled.
2035 *
2036 * As a rule these aren't called more than once (except for drivers
2037 * using the open-drain emulation idiom) so these are natural places
2038 * to accumulate extra debugging checks. Note that we can't (yet)
2039 * rely on gpio_request() having been called beforehand.
2040 */
2041
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002042/**
2043 * gpiod_direction_input - set the GPIO direction to input
2044 * @desc: GPIO to set to input
2045 *
2046 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2047 * be called safely on it.
2048 *
2049 * Return 0 in case of success, else an error code.
2050 */
2051int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002052{
David Brownelld2876d02008-02-04 22:28:20 -08002053 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002054 int status = -EINVAL;
2055
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002056 VALIDATE_DESC(desc);
2057 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002058
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002059 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002060 gpiod_warn(desc,
2061 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002062 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002063 return -EIO;
2064 }
2065
Alexandre Courbotd82da792014-07-22 16:17:43 +09002066 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002067 if (status == 0)
2068 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002069
Alexandre Courbot372e7222013-02-03 01:29:29 +09002070 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002071
David Brownelld2876d02008-02-04 22:28:20 -08002072 return status;
2073}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002074EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002075
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002076static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002077{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002078 struct gpio_chip *gc = desc->gdev->chip;
2079 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002080
Linus Walleijd468bf92013-09-24 11:54:38 +02002081 /* GPIOs used for IRQs shall not be set as output */
2082 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2083 gpiod_err(desc,
2084 "%s: tried to set a GPIO tied to an IRQ as output\n",
2085 __func__);
2086 return -EIO;
2087 }
2088
Linus Walleijc663e5f2016-03-22 10:51:16 +01002089 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2090 /* First see if we can enable open drain in hardware */
2091 if (gc->set_single_ended) {
2092 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2093 LINE_MODE_OPEN_DRAIN);
2094 if (!ret)
2095 goto set_output_value;
2096 }
2097 /* Emulate open drain by not actively driving the line high */
2098 if (value)
2099 return gpiod_direction_input(desc);
2100 }
2101 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2102 if (gc->set_single_ended) {
2103 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2104 LINE_MODE_OPEN_SOURCE);
2105 if (!ret)
2106 goto set_output_value;
2107 }
2108 /* Emulate open source by not actively driving the line low */
2109 if (!value)
2110 return gpiod_direction_input(desc);
2111 } else {
2112 /* Make sure to disable open drain/source hardware, if any */
2113 if (gc->set_single_ended)
2114 gc->set_single_ended(gc,
2115 gpio_chip_hwgpio(desc),
2116 LINE_MODE_PUSH_PULL);
2117 }
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302118
Linus Walleijc663e5f2016-03-22 10:51:16 +01002119set_output_value:
2120 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002121 gpiod_warn(desc,
2122 "%s: missing set() or direction_output() operations\n",
2123 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002124 return -EIO;
2125 }
2126
Linus Walleijc663e5f2016-03-22 10:51:16 +01002127 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), value);
2128 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002129 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002130 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002131 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2132 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002133}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002134
2135/**
2136 * gpiod_direction_output_raw - set the GPIO direction to output
2137 * @desc: GPIO to set to output
2138 * @value: initial output value of the GPIO
2139 *
2140 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2141 * be called safely on it. The initial value of the output must be specified
2142 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2143 *
2144 * Return 0 in case of success, else an error code.
2145 */
2146int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2147{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002148 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002149 return _gpiod_direction_output_raw(desc, value);
2150}
2151EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2152
2153/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302154 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002155 * @desc: GPIO to set to output
2156 * @value: initial output value of the GPIO
2157 *
2158 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2159 * be called safely on it. The initial value of the output must be specified
2160 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2161 * account.
2162 *
2163 * Return 0 in case of success, else an error code.
2164 */
2165int gpiod_direction_output(struct gpio_desc *desc, int value)
2166{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002167 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002168 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2169 value = !value;
2170 return _gpiod_direction_output_raw(desc, value);
2171}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002172EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002173
Felipe Balbic4b5be92010-05-26 14:42:23 -07002174/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002175 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07002176 * @gpio: the gpio to set debounce time
2177 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002178 *
2179 * returns -ENOTSUPP if the controller does not support setting
2180 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002181 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002182int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002183{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002184 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002185
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002186 VALIDATE_DESC(desc);
2187 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002188 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01002189 gpiod_dbg(desc,
2190 "%s: missing set() or set_debounce() operations\n",
2191 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002192 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002193 }
2194
Alexandre Courbotd82da792014-07-22 16:17:43 +09002195 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002196}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002197EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002198
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002199/**
2200 * gpiod_is_active_low - test whether a GPIO is active-low or not
2201 * @desc: the gpio descriptor to test
2202 *
2203 * Returns 1 if the GPIO is active-low, 0 otherwise.
2204 */
2205int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002206{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002207 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002208 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002209}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002210EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002211
2212/* I/O calls are only valid after configuration completed; the relevant
2213 * "is this a valid GPIO" error checks should already have been done.
2214 *
2215 * "Get" operations are often inlinable as reading a pin value register,
2216 * and masking the relevant bit in that register.
2217 *
2218 * When "set" operations are inlinable, they involve writing that mask to
2219 * one register to set a low value, or a different register to set it high.
2220 * Otherwise locking is needed, so there may be little value to inlining.
2221 *
2222 *------------------------------------------------------------------------
2223 *
2224 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2225 * have requested the GPIO. That can include implicit requesting by
2226 * a direction setting call. Marking a gpio as requested locks its chip
2227 * in memory, guaranteeing that these table lookups need no more locking
2228 * and that gpiochip_remove() will fail.
2229 *
2230 * REVISIT when debugging, consider adding some instrumentation to ensure
2231 * that the GPIO was actually requested.
2232 */
2233
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002234static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002235{
2236 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002237 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002238 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002239
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002240 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002241 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002242 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002243 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002244 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002245 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002246}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002247
David Brownelld2876d02008-02-04 22:28:20 -08002248/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002249 * gpiod_get_raw_value() - return a gpio's raw value
2250 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002251 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002252 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002253 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002254 *
2255 * This function should be called from contexts where we cannot sleep, and will
2256 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002257 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002258int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002259{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002260 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002261 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002262 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002263 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002264}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002265EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002266
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002267/**
2268 * gpiod_get_value() - return a gpio's value
2269 * @desc: gpio whose value will be returned
2270 *
2271 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002272 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002273 *
2274 * This function should be called from contexts where we cannot sleep, and will
2275 * complain if the GPIO chip functions potentially sleep.
2276 */
2277int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002278{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002279 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002280
2281 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002282 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002283 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002284
2285 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002286 if (value < 0)
2287 return value;
2288
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002289 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2290 value = !value;
2291
2292 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002293}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002294EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002295
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302296/*
2297 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002298 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002299 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302300 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002301static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302302{
2303 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002304 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002305 int offset = gpio_chip_hwgpio(desc);
2306
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302307 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002308 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302309 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002310 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302311 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002312 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302313 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002314 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302315 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002316 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302317 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002318 gpiod_err(desc,
2319 "%s: Error in set_value for open drain err %d\n",
2320 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302321}
2322
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302323/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002324 * _gpio_set_open_source_value() - Set the open source gpio's value.
2325 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002326 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302327 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002328static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302329{
2330 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002331 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002332 int offset = gpio_chip_hwgpio(desc);
2333
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302334 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002335 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302336 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002337 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302338 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002339 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302340 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002341 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302342 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002343 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302344 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002345 gpiod_err(desc,
2346 "%s: Error in set_value for open source err %d\n",
2347 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302348}
2349
Alexandre Courbot23600962014-03-11 15:52:09 +09002350static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002351{
2352 struct gpio_chip *chip;
2353
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002354 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002355 trace_gpio_value(desc_to_gpio(desc), 0, value);
2356 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2357 _gpio_set_open_drain_value(desc, value);
2358 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2359 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302360 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002361 chip->set(chip, gpio_chip_hwgpio(desc), value);
2362}
2363
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002364/*
2365 * set multiple outputs on the same chip;
2366 * use the chip's set_multiple function if available;
2367 * otherwise set the outputs sequentially;
2368 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2369 * defines which outputs are to be changed
2370 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2371 * defines the values the outputs specified by mask are to be set to
2372 */
2373static void gpio_chip_set_multiple(struct gpio_chip *chip,
2374 unsigned long *mask, unsigned long *bits)
2375{
2376 if (chip->set_multiple) {
2377 chip->set_multiple(chip, mask, bits);
2378 } else {
2379 int i;
2380 for (i = 0; i < chip->ngpio; i++) {
2381 if (mask[BIT_WORD(i)] == 0) {
2382 /* no more set bits in this mask word;
2383 * skip ahead to the next word */
2384 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
2385 continue;
2386 }
2387 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002388 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002389 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002390 }
2391 }
2392}
2393
Linus Walleij44c72882016-04-24 11:36:59 +02002394void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2395 unsigned int array_size,
2396 struct gpio_desc **desc_array,
2397 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002398{
2399 int i = 0;
2400
2401 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002402 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002403 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2404 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2405 int count = 0;
2406
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002407 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002408 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002409
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002410 memset(mask, 0, sizeof(mask));
2411 do {
2412 struct gpio_desc *desc = desc_array[i];
2413 int hwgpio = gpio_chip_hwgpio(desc);
2414 int value = value_array[i];
2415
2416 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2417 value = !value;
2418 trace_gpio_value(desc_to_gpio(desc), 0, value);
2419 /*
2420 * collect all normal outputs belonging to the same chip
2421 * open drain and open source outputs are set individually
2422 */
2423 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002424 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002425 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2426 _gpio_set_open_source_value(desc, value);
2427 } else {
2428 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002429 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002430 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002431 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002432 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002433 count++;
2434 }
2435 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002436 } while ((i < array_size) &&
2437 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002438 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002439 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002440 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002441 }
2442}
2443
David Brownelld2876d02008-02-04 22:28:20 -08002444/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002445 * gpiod_set_raw_value() - assign a gpio's raw value
2446 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002447 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002448 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002449 * Set the raw value of the GPIO, i.e. the value of its physical line without
2450 * regard for its ACTIVE_LOW status.
2451 *
2452 * This function should be called from contexts where we cannot sleep, and will
2453 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002454 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002455void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002456{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002457 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002458 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002459 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002460 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002461}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002462EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002463
2464/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002465 * gpiod_set_value() - assign a gpio's value
2466 * @desc: gpio whose value will be assigned
2467 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002468 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002469 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2470 * account
2471 *
2472 * This function should be called from contexts where we cannot sleep, and will
2473 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002474 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002475void gpiod_set_value(struct gpio_desc *desc, int value)
2476{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002477 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002478 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002479 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002480 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2481 value = !value;
2482 _gpiod_set_raw_value(desc, value);
2483}
2484EXPORT_SYMBOL_GPL(gpiod_set_value);
2485
2486/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002487 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002488 * @array_size: number of elements in the descriptor / value arrays
2489 * @desc_array: array of GPIO descriptors whose values will be assigned
2490 * @value_array: array of values to assign
2491 *
2492 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2493 * without regard for their ACTIVE_LOW status.
2494 *
2495 * This function should be called from contexts where we cannot sleep, and will
2496 * complain if the GPIO chip functions potentially sleep.
2497 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002498void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002499 struct gpio_desc **desc_array, int *value_array)
2500{
2501 if (!desc_array)
2502 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002503 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2504 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002505}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002506EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002507
2508/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002509 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002510 * @array_size: number of elements in the descriptor / value arrays
2511 * @desc_array: array of GPIO descriptors whose values will be assigned
2512 * @value_array: array of values to assign
2513 *
2514 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2515 * into account.
2516 *
2517 * This function should be called from contexts where we cannot sleep, and will
2518 * complain if the GPIO chip functions potentially sleep.
2519 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002520void gpiod_set_array_value(unsigned int array_size,
2521 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002522{
2523 if (!desc_array)
2524 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002525 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2526 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002527}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002528EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002529
2530/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002531 * gpiod_cansleep() - report whether gpio value access may sleep
2532 * @desc: gpio to check
2533 *
2534 */
2535int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002536{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002537 VALIDATE_DESC(desc);
2538 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002539}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002540EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002541
David Brownell0f6d5042008-10-15 22:03:14 -07002542/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002543 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2544 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002545 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002546 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2547 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002548 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002549int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002550{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002551 struct gpio_chip *chip;
2552 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002553
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002554 VALIDATE_DESC(desc);
2555 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002556 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02002557 if (chip->to_irq) {
2558 int retirq = chip->to_irq(chip, offset);
2559
2560 /* Zero means NO_IRQ */
2561 if (!retirq)
2562 return -ENXIO;
2563
2564 return retirq;
2565 }
2566 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002567}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002568EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002569
Linus Walleijd468bf92013-09-24 11:54:38 +02002570/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002571 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002572 * @chip: the chip the GPIO to lock belongs to
2573 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002574 *
2575 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08002576 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08002577 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002578int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08002579{
Linus Walleij9c102802016-05-25 10:56:03 +02002580 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02002581
Linus Walleij9c102802016-05-25 10:56:03 +02002582 desc = gpiochip_get_desc(chip, offset);
2583 if (IS_ERR(desc))
2584 return PTR_ERR(desc);
2585
2586 /* Flush direction if something changed behind our back */
2587 if (chip->get_direction) {
2588 int dir = chip->get_direction(chip, offset);
2589
2590 if (dir)
2591 clear_bit(FLAG_IS_OUT, &desc->flags);
2592 else
2593 set_bit(FLAG_IS_OUT, &desc->flags);
2594 }
2595
2596 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002597 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02002598 "%s: tried to flag a GPIO set as output for IRQ\n",
2599 __func__);
2600 return -EIO;
2601 }
2602
Linus Walleij9c102802016-05-25 10:56:03 +02002603 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002604 return 0;
2605}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002606EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002607
Linus Walleijd468bf92013-09-24 11:54:38 +02002608/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002609 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002610 * @chip: the chip the GPIO to lock belongs to
2611 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002612 *
2613 * This is used directly by GPIO drivers that want to indicate
2614 * that a certain GPIO is no longer used exclusively for IRQ.
2615 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002616void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02002617{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002618 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02002619 return;
2620
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002621 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002622}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002623EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002624
Linus Walleij6cee3822016-02-11 20:16:45 +01002625bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2626{
2627 if (offset >= chip->ngpio)
2628 return false;
2629
2630 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2631}
2632EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2633
Linus Walleij143b65d2016-02-16 15:41:42 +01002634bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2635{
2636 if (offset >= chip->ngpio)
2637 return false;
2638
2639 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2640}
2641EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2642
2643bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2644{
2645 if (offset >= chip->ngpio)
2646 return false;
2647
2648 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2649}
2650EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2651
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002652/**
2653 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2654 * @desc: gpio whose value will be returned
2655 *
2656 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002657 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002658 *
2659 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002660 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002661int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002662{
David Brownelld2876d02008-02-04 22:28:20 -08002663 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002664 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002665 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002666}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002667EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002668
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002669/**
2670 * gpiod_get_value_cansleep() - return a gpio's value
2671 * @desc: gpio whose value will be returned
2672 *
2673 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002674 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002675 *
2676 * This function is to be called from contexts that can sleep.
2677 */
2678int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002679{
David Brownelld2876d02008-02-04 22:28:20 -08002680 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002681
2682 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002683 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002684 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002685 if (value < 0)
2686 return value;
2687
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002688 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2689 value = !value;
2690
David Brownelld2876d02008-02-04 22:28:20 -08002691 return value;
2692}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002693EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002694
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002695/**
2696 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2697 * @desc: gpio whose value will be assigned
2698 * @value: value to assign
2699 *
2700 * Set the raw value of the GPIO, i.e. the value of its physical line without
2701 * regard for its ACTIVE_LOW status.
2702 *
2703 * This function is to be called from contexts that can sleep.
2704 */
2705void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002706{
David Brownelld2876d02008-02-04 22:28:20 -08002707 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002708 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002709 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002710}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002711EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002712
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002713/**
2714 * gpiod_set_value_cansleep() - assign a gpio's value
2715 * @desc: gpio whose value will be assigned
2716 * @value: value to assign
2717 *
2718 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2719 * account
2720 *
2721 * This function is to be called from contexts that can sleep.
2722 */
2723void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002724{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002725 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002726 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002727 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2728 value = !value;
2729 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002730}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002731EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002732
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002733/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002734 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002735 * @array_size: number of elements in the descriptor / value arrays
2736 * @desc_array: array of GPIO descriptors whose values will be assigned
2737 * @value_array: array of values to assign
2738 *
2739 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2740 * without regard for their ACTIVE_LOW status.
2741 *
2742 * This function is to be called from contexts that can sleep.
2743 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002744void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2745 struct gpio_desc **desc_array,
2746 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002747{
2748 might_sleep_if(extra_checks);
2749 if (!desc_array)
2750 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002751 gpiod_set_array_value_complex(true, true, array_size, desc_array,
2752 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002753}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002754EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002755
2756/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002757 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002758 * @array_size: number of elements in the descriptor / value arrays
2759 * @desc_array: array of GPIO descriptors whose values will be assigned
2760 * @value_array: array of values to assign
2761 *
2762 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2763 * into account.
2764 *
2765 * This function is to be called from contexts that can sleep.
2766 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002767void gpiod_set_array_value_cansleep(unsigned int array_size,
2768 struct gpio_desc **desc_array,
2769 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002770{
2771 might_sleep_if(extra_checks);
2772 if (!desc_array)
2773 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002774 gpiod_set_array_value_complex(false, true, array_size, desc_array,
2775 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002776}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002777EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002778
2779/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002780 * gpiod_add_lookup_table() - register GPIO device consumers
2781 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002782 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002783void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002784{
2785 mutex_lock(&gpio_lookup_lock);
2786
Alexandre Courbotad824782013-12-03 12:20:11 +09002787 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002788
2789 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002790}
2791
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302792/**
2793 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2794 * @table: table of consumers to unregister
2795 */
2796void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2797{
2798 mutex_lock(&gpio_lookup_lock);
2799
2800 list_del(&table->list);
2801
2802 mutex_unlock(&gpio_lookup_lock);
2803}
2804
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002805static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002806 unsigned int idx,
2807 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002808{
2809 char prop_name[32]; /* 32 is max size of property name */
2810 enum of_gpio_flags of_flags;
2811 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02002812 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002813
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002814 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02002815 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01002816 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002817 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02002818 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01002819 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002820 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002821
Thierry Redingdd34c372014-04-23 17:28:09 +02002822 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2823 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07002824 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02002825 break;
2826 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002827
2828 if (IS_ERR(desc))
2829 return desc;
2830
2831 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002832 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002833
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002834 if (of_flags & OF_GPIO_SINGLE_ENDED) {
2835 if (of_flags & OF_GPIO_ACTIVE_LOW)
2836 *flags |= GPIO_OPEN_DRAIN;
2837 else
2838 *flags |= GPIO_OPEN_SOURCE;
2839 }
2840
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002841 return desc;
2842}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002843
Dmitry Torokhov25487532016-03-24 10:50:25 -07002844static struct gpio_desc *acpi_find_gpio(struct device *dev,
2845 const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002846 unsigned int idx,
Dmitry Torokhov25487532016-03-24 10:50:25 -07002847 enum gpiod_flags flags,
2848 enum gpio_lookup_flags *lookupflags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03002849{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002850 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03002851 struct acpi_gpio_info info;
2852 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002853 char propname[32];
2854 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03002855
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002856 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002857 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002858 if (con_id && strcmp(con_id, "gpios")) {
2859 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002860 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002861 } else {
2862 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002863 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002864 }
Mika Westerberge01f4402013-10-10 11:01:10 +03002865
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002866 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
2867 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2868 break;
2869 }
2870
2871 /* Then from plain _CRS GPIOs */
2872 if (IS_ERR(desc)) {
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08002873 if (!acpi_can_fallback_to_crs(adev, con_id))
2874 return ERR_PTR(-ENOENT);
2875
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002876 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2877 if (IS_ERR(desc))
2878 return desc;
Dmitry Torokhov25487532016-03-24 10:50:25 -07002879
2880 if ((flags == GPIOD_OUT_LOW || flags == GPIOD_OUT_HIGH) &&
2881 info.gpioint) {
2882 dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
2883 return ERR_PTR(-ENOENT);
2884 }
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002885 }
2886
Christophe RICARD52044722015-12-23 23:25:34 +01002887 if (info.polarity == GPIO_ACTIVE_LOW)
Dmitry Torokhov25487532016-03-24 10:50:25 -07002888 *lookupflags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002889
2890 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002891}
2892
Alexandre Courbotad824782013-12-03 12:20:11 +09002893static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2894{
2895 const char *dev_id = dev ? dev_name(dev) : NULL;
2896 struct gpiod_lookup_table *table;
2897
2898 mutex_lock(&gpio_lookup_lock);
2899
2900 list_for_each_entry(table, &gpio_lookup_list, list) {
2901 if (table->dev_id && dev_id) {
2902 /*
2903 * Valid strings on both ends, must be identical to have
2904 * a match
2905 */
2906 if (!strcmp(table->dev_id, dev_id))
2907 goto found;
2908 } else {
2909 /*
2910 * One of the pointers is NULL, so both must be to have
2911 * a match
2912 */
2913 if (dev_id == table->dev_id)
2914 goto found;
2915 }
2916 }
2917 table = NULL;
2918
2919found:
2920 mutex_unlock(&gpio_lookup_lock);
2921 return table;
2922}
2923
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002924static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002925 unsigned int idx,
2926 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002927{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002928 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002929 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002930 struct gpiod_lookup *p;
2931
Alexandre Courbotad824782013-12-03 12:20:11 +09002932 table = gpiod_find_lookup_table(dev);
2933 if (!table)
2934 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002935
Alexandre Courbotad824782013-12-03 12:20:11 +09002936 for (p = &table->table[0]; p->chip_label; p++) {
2937 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002938
Alexandre Courbotad824782013-12-03 12:20:11 +09002939 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002940 if (p->idx != idx)
2941 continue;
2942
Alexandre Courbotad824782013-12-03 12:20:11 +09002943 /* If the lookup entry has a con_id, require exact match */
2944 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2945 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002946
Alexandre Courbotad824782013-12-03 12:20:11 +09002947 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002948
Alexandre Courbotad824782013-12-03 12:20:11 +09002949 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002950 dev_err(dev, "cannot find GPIO chip %s\n",
2951 p->chip_label);
2952 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002953 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002954
Alexandre Courbotad824782013-12-03 12:20:11 +09002955 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002956 dev_err(dev,
2957 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2958 idx, chip->ngpio, chip->label);
2959 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09002960 }
2961
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09002962 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09002963 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002964
2965 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09002966 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002967
2968 return desc;
2969}
2970
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002971static int dt_gpio_count(struct device *dev, const char *con_id)
2972{
2973 int ret;
2974 char propname[32];
2975 unsigned int i;
2976
2977 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2978 if (con_id)
2979 snprintf(propname, sizeof(propname), "%s-%s",
2980 con_id, gpio_suffixes[i]);
2981 else
2982 snprintf(propname, sizeof(propname), "%s",
2983 gpio_suffixes[i]);
2984
2985 ret = of_gpio_named_count(dev->of_node, propname);
2986 if (ret >= 0)
2987 break;
2988 }
2989 return ret;
2990}
2991
2992static int platform_gpio_count(struct device *dev, const char *con_id)
2993{
2994 struct gpiod_lookup_table *table;
2995 struct gpiod_lookup *p;
2996 unsigned int count = 0;
2997
2998 table = gpiod_find_lookup_table(dev);
2999 if (!table)
3000 return -ENOENT;
3001
3002 for (p = &table->table[0]; p->chip_label; p++) {
3003 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3004 (!con_id && !p->con_id))
3005 count++;
3006 }
3007 if (!count)
3008 return -ENOENT;
3009
3010 return count;
3011}
3012
3013/**
3014 * gpiod_count - return the number of GPIOs associated with a device / function
3015 * or -ENOENT if no GPIO has been assigned to the requested function
3016 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3017 * @con_id: function within the GPIO consumer
3018 */
3019int gpiod_count(struct device *dev, const char *con_id)
3020{
3021 int count = -ENOENT;
3022
3023 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3024 count = dt_gpio_count(dev, con_id);
3025 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3026 count = acpi_gpio_count(dev, con_id);
3027
3028 if (count < 0)
3029 count = platform_gpio_count(dev, con_id);
3030
3031 return count;
3032}
3033EXPORT_SYMBOL_GPL(gpiod_count);
3034
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003035/**
Thierry Reding08791622014-04-25 16:54:22 +02003036 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003037 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003038 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003039 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003040 *
3041 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003042 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003043 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003044 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003045struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003046 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003047{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003048 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003049}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003050EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003051
3052/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003053 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3054 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3055 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003056 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003057 *
3058 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3059 * the requested function it will return NULL. This is convenient for drivers
3060 * that need to handle optional GPIOs.
3061 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003062struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003063 const char *con_id,
3064 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003065{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003066 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003067}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003068EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003069
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003070/**
3071 * gpiod_parse_flags - helper function to parse GPIO lookup flags
3072 * @desc: gpio to be setup
3073 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3074 * of_get_gpio_hog()
3075 *
3076 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
3077 */
3078static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
3079{
3080 if (lflags & GPIO_ACTIVE_LOW)
3081 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3082 if (lflags & GPIO_OPEN_DRAIN)
3083 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3084 if (lflags & GPIO_OPEN_SOURCE)
3085 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3086}
Benoit Parrotf625d462015-02-02 11:44:44 -06003087
3088/**
3089 * gpiod_configure_flags - helper function to configure a given GPIO
3090 * @desc: gpio whose value will be assigned
3091 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06003092 * @dflags: gpiod_flags - optional GPIO initialization flags
3093 *
3094 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3095 * requested function and/or index, or another IS_ERR() code if an error
3096 * occurred while trying to acquire the GPIO.
3097 */
3098static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003099 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003100{
3101 int status;
3102
Benoit Parrotf625d462015-02-02 11:44:44 -06003103 /* No particular flag request, return here... */
3104 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3105 pr_debug("no flags found for %s\n", con_id);
3106 return 0;
3107 }
3108
3109 /* Process flags */
3110 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3111 status = gpiod_direction_output(desc,
3112 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
3113 else
3114 status = gpiod_direction_input(desc);
3115
3116 return status;
3117}
3118
Thierry Reding29a1f2332014-04-25 17:10:06 +02003119/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003120 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003121 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003122 * @con_id: function within the GPIO consumer
3123 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003124 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003125 *
3126 * This variant of gpiod_get() allows to access GPIOs other than the first
3127 * defined one for functions that define several GPIOs.
3128 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003129 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3130 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003131 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003132 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003133struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003134 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003135 unsigned int idx,
3136 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003137{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003138 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003139 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003140 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003141
3142 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3143
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003144 if (dev) {
3145 /* Using device tree? */
3146 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3147 dev_dbg(dev, "using device tree for GPIO lookup\n");
3148 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3149 } else if (ACPI_COMPANION(dev)) {
3150 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Dmitry Torokhov25487532016-03-24 10:50:25 -07003151 desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003152 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003153 }
3154
3155 /*
3156 * Either we are not using DT or ACPI, or their lookup did not return
3157 * a result. In that case, use platform lookup as a fallback.
3158 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003159 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003160 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003161 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003162 }
3163
3164 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003165 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003166 return desc;
3167 }
3168
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003169 gpiod_parse_flags(desc, lookupflags);
3170
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003171 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003172 if (status < 0)
3173 return ERR_PTR(status);
3174
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003175 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003176 if (status < 0) {
3177 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3178 gpiod_put(desc);
3179 return ERR_PTR(status);
3180 }
3181
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003182 return desc;
3183}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003184EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003185
3186/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003187 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3188 * @fwnode: handle of the firmware node
3189 * @propname: name of the firmware property representing the GPIO
3190 *
3191 * This function can be used for drivers that get their configuration
3192 * from firmware.
3193 *
3194 * Function properly finds the corresponding GPIO using whatever is the
3195 * underlying firmware interface and then makes sure that the GPIO
3196 * descriptor is requested before it is returned to the caller.
3197 *
3198 * In case of error an ERR_PTR() is returned.
3199 */
3200struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3201 const char *propname)
3202{
3203 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3204 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003205 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003206 int ret;
3207
3208 if (!fwnode)
3209 return ERR_PTR(-EINVAL);
3210
3211 if (is_of_node(fwnode)) {
3212 enum of_gpio_flags flags;
3213
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02003214 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02003215 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003216 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003217 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003218 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3219 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003220 } else if (is_acpi_node(fwnode)) {
3221 struct acpi_gpio_info info;
3222
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02003223 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02003224 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01003225 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003226 }
3227
3228 if (IS_ERR(desc))
3229 return desc;
3230
Mika Westerberg40b73182014-10-21 13:33:59 +02003231 if (active_low)
3232 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3233
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003234 if (single_ended) {
3235 if (active_low)
3236 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3237 else
3238 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3239 }
3240
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003241 ret = gpiod_request(desc, NULL);
3242 if (ret)
3243 return ERR_PTR(ret);
3244
Mika Westerberg40b73182014-10-21 13:33:59 +02003245 return desc;
3246}
3247EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3248
3249/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003250 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3251 * function
3252 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3253 * @con_id: function within the GPIO consumer
3254 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003255 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003256 *
3257 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3258 * specified index was assigned to the requested function it will return NULL.
3259 * This is convenient for drivers that need to handle optional GPIOs.
3260 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003261struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003262 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003263 unsigned int index,
3264 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003265{
3266 struct gpio_desc *desc;
3267
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003268 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003269 if (IS_ERR(desc)) {
3270 if (PTR_ERR(desc) == -ENOENT)
3271 return NULL;
3272 }
3273
3274 return desc;
3275}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003276EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003277
3278/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003279 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3280 * @desc: gpio whose value will be assigned
3281 * @name: gpio line name
3282 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3283 * of_get_gpio_hog()
3284 * @dflags: gpiod_flags - optional GPIO initialization flags
3285 */
3286int gpiod_hog(struct gpio_desc *desc, const char *name,
3287 unsigned long lflags, enum gpiod_flags dflags)
3288{
3289 struct gpio_chip *chip;
3290 struct gpio_desc *local_desc;
3291 int hwnum;
3292 int status;
3293
3294 chip = gpiod_to_chip(desc);
3295 hwnum = gpio_chip_hwgpio(desc);
3296
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003297 gpiod_parse_flags(desc, lflags);
3298
Benoit Parrotf625d462015-02-02 11:44:44 -06003299 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3300 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303301 status = PTR_ERR(local_desc);
3302 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3303 name, chip->label, hwnum, status);
3304 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003305 }
3306
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003307 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003308 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303309 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3310 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003311 gpiochip_free_own_desc(desc);
3312 return status;
3313 }
3314
3315 /* Mark GPIO as hogged so it can be identified and removed later */
3316 set_bit(FLAG_IS_HOGGED, &desc->flags);
3317
3318 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3319 desc_to_gpio(desc), name,
3320 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3321 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3322 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3323
3324 return 0;
3325}
3326
3327/**
3328 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3329 * @chip: gpio chip to act on
3330 *
3331 * This is only used by of_gpiochip_remove to free hogged gpios
3332 */
3333static void gpiochip_free_hogs(struct gpio_chip *chip)
3334{
3335 int id;
3336
3337 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003338 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3339 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003340 }
3341}
3342
3343/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003344 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3345 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3346 * @con_id: function within the GPIO consumer
3347 * @flags: optional GPIO initialization flags
3348 *
3349 * This function acquires all the GPIOs defined under a given function.
3350 *
3351 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3352 * no GPIO has been assigned to the requested function, or another IS_ERR()
3353 * code if an error occurred while trying to acquire the GPIOs.
3354 */
3355struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3356 const char *con_id,
3357 enum gpiod_flags flags)
3358{
3359 struct gpio_desc *desc;
3360 struct gpio_descs *descs;
3361 int count;
3362
3363 count = gpiod_count(dev, con_id);
3364 if (count < 0)
3365 return ERR_PTR(count);
3366
3367 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3368 GFP_KERNEL);
3369 if (!descs)
3370 return ERR_PTR(-ENOMEM);
3371
3372 for (descs->ndescs = 0; descs->ndescs < count; ) {
3373 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3374 if (IS_ERR(desc)) {
3375 gpiod_put_array(descs);
3376 return ERR_CAST(desc);
3377 }
3378 descs->desc[descs->ndescs] = desc;
3379 descs->ndescs++;
3380 }
3381 return descs;
3382}
3383EXPORT_SYMBOL_GPL(gpiod_get_array);
3384
3385/**
3386 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3387 * function
3388 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3389 * @con_id: function within the GPIO consumer
3390 * @flags: optional GPIO initialization flags
3391 *
3392 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3393 * assigned to the requested function it will return NULL.
3394 */
3395struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3396 const char *con_id,
3397 enum gpiod_flags flags)
3398{
3399 struct gpio_descs *descs;
3400
3401 descs = gpiod_get_array(dev, con_id, flags);
3402 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3403 return NULL;
3404
3405 return descs;
3406}
3407EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3408
3409/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003410 * gpiod_put - dispose of a GPIO descriptor
3411 * @desc: GPIO descriptor to dispose of
3412 *
3413 * No descriptor can be used after gpiod_put() has been called on it.
3414 */
3415void gpiod_put(struct gpio_desc *desc)
3416{
3417 gpiod_free(desc);
3418}
3419EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003420
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003421/**
3422 * gpiod_put_array - dispose of multiple GPIO descriptors
3423 * @descs: struct gpio_descs containing an array of descriptors
3424 */
3425void gpiod_put_array(struct gpio_descs *descs)
3426{
3427 unsigned int i;
3428
3429 for (i = 0; i < descs->ndescs; i++)
3430 gpiod_put(descs->desc[i]);
3431
3432 kfree(descs);
3433}
3434EXPORT_SYMBOL_GPL(gpiod_put_array);
3435
Linus Walleij3c702e92015-10-21 15:29:53 +02003436static int __init gpiolib_dev_init(void)
3437{
3438 int ret;
3439
3440 /* Register GPIO sysfs bus */
3441 ret = bus_register(&gpio_bus_type);
3442 if (ret < 0) {
3443 pr_err("gpiolib: could not register GPIO bus type\n");
3444 return ret;
3445 }
3446
3447 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3448 if (ret < 0) {
3449 pr_err("gpiolib: failed to allocate char dev region\n");
3450 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003451 } else {
3452 gpiolib_initialized = true;
3453 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003454 }
3455 return ret;
3456}
3457core_initcall(gpiolib_dev_init);
3458
David Brownelld2876d02008-02-04 22:28:20 -08003459#ifdef CONFIG_DEBUG_FS
3460
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003461static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003462{
3463 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003464 struct gpio_chip *chip = gdev->chip;
3465 unsigned gpio = gdev->base;
3466 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003467 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003468 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003469
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003470 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003471 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3472 if (gdesc->name) {
3473 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3474 gpio, gdesc->name);
3475 }
David Brownelld2876d02008-02-04 22:28:20 -08003476 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003477 }
David Brownelld2876d02008-02-04 22:28:20 -08003478
Alexandre Courbot372e7222013-02-03 01:29:29 +09003479 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003480 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003481 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003482 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3483 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003484 is_out ? "out" : "in ",
3485 chip->get
3486 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003487 : "? ",
3488 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003489 seq_printf(s, "\n");
3490 }
3491}
3492
Thierry Redingf9c4a312012-04-12 13:26:01 +02003493static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003494{
Grant Likely362432a2013-02-09 09:41:49 +00003495 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003496 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003497 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003498
3499 s->private = "";
3500
Grant Likely362432a2013-02-09 09:41:49 +00003501 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003502 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003503 if (index-- == 0) {
3504 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003505 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003506 }
3507 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003508
3509 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003510}
3511
3512static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3513{
Grant Likely362432a2013-02-09 09:41:49 +00003514 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003515 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003516 void *ret = NULL;
3517
Grant Likely362432a2013-02-09 09:41:49 +00003518 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003519 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003520 ret = NULL;
3521 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003522 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003523 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003524
3525 s->private = "\n";
3526 ++*pos;
3527
3528 return ret;
3529}
3530
3531static void gpiolib_seq_stop(struct seq_file *s, void *v)
3532{
3533}
3534
3535static int gpiolib_seq_show(struct seq_file *s, void *v)
3536{
Linus Walleijff2b1352015-10-20 11:10:38 +02003537 struct gpio_device *gdev = v;
3538 struct gpio_chip *chip = gdev->chip;
3539 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003540
Linus Walleijff2b1352015-10-20 11:10:38 +02003541 if (!chip) {
3542 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3543 dev_name(&gdev->dev));
3544 return 0;
3545 }
3546
3547 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3548 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003549 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02003550 parent = chip->parent;
3551 if (parent)
3552 seq_printf(s, ", parent: %s/%s",
3553 parent->bus ? parent->bus->name : "no-bus",
3554 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02003555 if (chip->label)
3556 seq_printf(s, ", %s", chip->label);
3557 if (chip->can_sleep)
3558 seq_printf(s, ", can sleep");
3559 seq_printf(s, ":\n");
3560
3561 if (chip->dbg_show)
3562 chip->dbg_show(s, chip);
3563 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003564 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003565
David Brownelld2876d02008-02-04 22:28:20 -08003566 return 0;
3567}
3568
Thierry Redingf9c4a312012-04-12 13:26:01 +02003569static const struct seq_operations gpiolib_seq_ops = {
3570 .start = gpiolib_seq_start,
3571 .next = gpiolib_seq_next,
3572 .stop = gpiolib_seq_stop,
3573 .show = gpiolib_seq_show,
3574};
3575
David Brownelld2876d02008-02-04 22:28:20 -08003576static int gpiolib_open(struct inode *inode, struct file *file)
3577{
Thierry Redingf9c4a312012-04-12 13:26:01 +02003578 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08003579}
3580
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003581static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02003582 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08003583 .open = gpiolib_open,
3584 .read = seq_read,
3585 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02003586 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08003587};
3588
3589static int __init gpiolib_debugfs_init(void)
3590{
3591 /* /sys/kernel/debug/gpio */
3592 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3593 NULL, NULL, &gpiolib_operations);
3594 return 0;
3595}
3596subsys_initcall(gpiolib_debugfs_init);
3597
3598#endif /* DEBUG_FS */