blob: 69efe278f74d5e7d01c987e13116c2da0e63cbbf [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;
Linus Walleijd932cd42016-07-04 13:13:04 +0200491 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
492 ret = -EFAULT;
493 goto out_free_descs;
494 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200495
496 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
497 lh->numdescs);
498
499 return 0;
500
501out_free_descs:
502 for (; i >= 0; i--)
503 gpiod_free(lh->descs[i]);
504 kfree(lh->label);
505out_free_lh:
506 kfree(lh);
507 put_device(&gdev->dev);
508 return ret;
509}
510
Linus Walleij61f922d2016-06-02 11:30:15 +0200511/*
512 * GPIO line event management
513 */
514
515/**
516 * struct lineevent_state - contains the state of a userspace event
517 * @gdev: the GPIO device the event pertains to
518 * @label: consumer label used to tag descriptors
519 * @desc: the GPIO descriptor held by this event
520 * @eflags: the event flags this line was requested with
521 * @irq: the interrupt that trigger in response to events on this GPIO
522 * @wait: wait queue that handles blocking reads of events
523 * @events: KFIFO for the GPIO events
524 * @read_lock: mutex lock to protect reads from colliding with adding
525 * new events to the FIFO
526 */
527struct lineevent_state {
528 struct gpio_device *gdev;
529 const char *label;
530 struct gpio_desc *desc;
531 u32 eflags;
532 int irq;
533 wait_queue_head_t wait;
534 DECLARE_KFIFO(events, struct gpioevent_data, 16);
535 struct mutex read_lock;
536};
537
538static unsigned int lineevent_poll(struct file *filep,
539 struct poll_table_struct *wait)
540{
541 struct lineevent_state *le = filep->private_data;
542 unsigned int events = 0;
543
544 poll_wait(filep, &le->wait, wait);
545
546 if (!kfifo_is_empty(&le->events))
547 events = POLLIN | POLLRDNORM;
548
549 return events;
550}
551
552
553static ssize_t lineevent_read(struct file *filep,
554 char __user *buf,
555 size_t count,
556 loff_t *f_ps)
557{
558 struct lineevent_state *le = filep->private_data;
559 unsigned int copied;
560 int ret;
561
562 if (count < sizeof(struct gpioevent_data))
563 return -EINVAL;
564
565 do {
566 if (kfifo_is_empty(&le->events)) {
567 if (filep->f_flags & O_NONBLOCK)
568 return -EAGAIN;
569
570 ret = wait_event_interruptible(le->wait,
571 !kfifo_is_empty(&le->events));
572 if (ret)
573 return ret;
574 }
575
576 if (mutex_lock_interruptible(&le->read_lock))
577 return -ERESTARTSYS;
578 ret = kfifo_to_user(&le->events, buf, count, &copied);
579 mutex_unlock(&le->read_lock);
580
581 if (ret)
582 return ret;
583
584 /*
585 * If we couldn't read anything from the fifo (a different
586 * thread might have been faster) we either return -EAGAIN if
587 * the file descriptor is non-blocking, otherwise we go back to
588 * sleep and wait for more data to arrive.
589 */
590 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
591 return -EAGAIN;
592
593 } while (copied == 0);
594
595 return copied;
596}
597
598static int lineevent_release(struct inode *inode, struct file *filep)
599{
600 struct lineevent_state *le = filep->private_data;
601 struct gpio_device *gdev = le->gdev;
602
603 free_irq(le->irq, le);
604 gpiod_free(le->desc);
605 kfree(le->label);
606 kfree(le);
607 put_device(&gdev->dev);
608 return 0;
609}
610
611static long lineevent_ioctl(struct file *filep, unsigned int cmd,
612 unsigned long arg)
613{
614 struct lineevent_state *le = filep->private_data;
615 void __user *ip = (void __user *)arg;
616 struct gpiohandle_data ghd;
617
618 /*
619 * We can get the value for an event line but not set it,
620 * because it is input by definition.
621 */
622 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
623 int val;
624
625 val = gpiod_get_value_cansleep(le->desc);
626 if (val < 0)
627 return val;
628 ghd.values[0] = val;
629
630 if (copy_to_user(ip, &ghd, sizeof(ghd)))
631 return -EFAULT;
632
633 return 0;
634 }
635 return -EINVAL;
636}
637
638#ifdef CONFIG_COMPAT
639static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
640 unsigned long arg)
641{
642 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
643}
644#endif
645
646static const struct file_operations lineevent_fileops = {
647 .release = lineevent_release,
648 .read = lineevent_read,
649 .poll = lineevent_poll,
650 .owner = THIS_MODULE,
651 .llseek = noop_llseek,
652 .unlocked_ioctl = lineevent_ioctl,
653#ifdef CONFIG_COMPAT
654 .compat_ioctl = lineevent_ioctl_compat,
655#endif
656};
657
Ben Dooks33265b12016-06-17 16:03:13 +0100658static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200659{
660 struct lineevent_state *le = p;
661 struct gpioevent_data ge;
662 int ret;
663
664 ge.timestamp = ktime_get_real_ns();
665
666 if (le->eflags & GPIOEVENT_REQUEST_BOTH_EDGES) {
667 int level = gpiod_get_value_cansleep(le->desc);
668
669 if (level)
670 /* Emit low-to-high event */
671 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
672 else
673 /* Emit high-to-low event */
674 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
675 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
676 /* Emit low-to-high event */
677 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
678 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
679 /* Emit high-to-low event */
680 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200681 } else {
682 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200683 }
684
685 ret = kfifo_put(&le->events, ge);
686 if (ret != 0)
687 wake_up_poll(&le->wait, POLLIN);
688
689 return IRQ_HANDLED;
690}
691
692static int lineevent_create(struct gpio_device *gdev, void __user *ip)
693{
694 struct gpioevent_request eventreq;
695 struct lineevent_state *le;
696 struct gpio_desc *desc;
697 u32 offset;
698 u32 lflags;
699 u32 eflags;
700 int fd;
701 int ret;
702 int irqflags = 0;
703
704 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
705 return -EFAULT;
706
707 le = kzalloc(sizeof(*le), GFP_KERNEL);
708 if (!le)
709 return -ENOMEM;
710 le->gdev = gdev;
711 get_device(&gdev->dev);
712
713 /* Make sure this is terminated */
714 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
715 if (strlen(eventreq.consumer_label)) {
716 le->label = kstrdup(eventreq.consumer_label,
717 GFP_KERNEL);
718 if (!le->label) {
719 ret = -ENOMEM;
720 goto out_free_le;
721 }
722 }
723
724 offset = eventreq.lineoffset;
725 lflags = eventreq.handleflags;
726 eflags = eventreq.eventflags;
727
728 /* This is just wrong: we don't look for events on output lines */
729 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
730 ret = -EINVAL;
731 goto out_free_label;
732 }
733
734 desc = &gdev->descs[offset];
735 ret = gpiod_request(desc, le->label);
736 if (ret)
737 goto out_free_desc;
738 le->desc = desc;
739 le->eflags = eflags;
740
741 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
742 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
743 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
744 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
745 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
746 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
747
748 ret = gpiod_direction_input(desc);
749 if (ret)
750 goto out_free_desc;
751
752 le->irq = gpiod_to_irq(desc);
753 if (le->irq <= 0) {
754 ret = -ENODEV;
755 goto out_free_desc;
756 }
757
758 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
759 irqflags |= IRQF_TRIGGER_RISING;
760 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
761 irqflags |= IRQF_TRIGGER_FALLING;
762 irqflags |= IRQF_ONESHOT;
763 irqflags |= IRQF_SHARED;
764
765 INIT_KFIFO(le->events);
766 init_waitqueue_head(&le->wait);
767 mutex_init(&le->read_lock);
768
769 /* Request a thread to read the events */
770 ret = request_threaded_irq(le->irq,
771 NULL,
772 lineevent_irq_thread,
773 irqflags,
774 le->label,
775 le);
776 if (ret)
777 goto out_free_desc;
778
779 fd = anon_inode_getfd("gpio-event",
780 &lineevent_fileops,
781 le,
782 O_RDONLY | O_CLOEXEC);
783 if (fd < 0) {
784 ret = fd;
785 goto out_free_irq;
786 }
787
788 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200789 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
790 ret = -EFAULT;
791 goto out_free_irq;
792 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200793
794 return 0;
795
796out_free_irq:
797 free_irq(le->irq, le);
798out_free_desc:
799 gpiod_free(le->desc);
800out_free_label:
801 kfree(le->label);
802out_free_le:
803 kfree(le);
804 put_device(&gdev->dev);
805 return ret;
806}
807
Linus Walleij3c702e92015-10-21 15:29:53 +0200808/**
809 * gpio_ioctl() - ioctl handler for the GPIO chardev
810 */
811static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
812{
813 struct gpio_device *gdev = filp->private_data;
814 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200815 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200816
817 /* We fail any subsequent ioctl():s when the chip is gone */
818 if (!chip)
819 return -ENODEV;
820
Linus Walleij521a2ad2016-02-12 22:25:22 +0100821 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200822 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100823 struct gpiochip_info chipinfo;
824
Linus Walleij3c702e92015-10-21 15:29:53 +0200825 strncpy(chipinfo.name, dev_name(&gdev->dev),
826 sizeof(chipinfo.name));
827 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100828 strncpy(chipinfo.label, gdev->label,
829 sizeof(chipinfo.label));
830 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100831 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200832 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
833 return -EFAULT;
834 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100835 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
836 struct gpioline_info lineinfo;
837 struct gpio_desc *desc;
838
839 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
840 return -EFAULT;
841 if (lineinfo.line_offset > gdev->ngpio)
842 return -EINVAL;
843
844 desc = &gdev->descs[lineinfo.line_offset];
845 if (desc->name) {
846 strncpy(lineinfo.name, desc->name,
847 sizeof(lineinfo.name));
848 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
849 } else {
850 lineinfo.name[0] = '\0';
851 }
852 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100853 strncpy(lineinfo.consumer, desc->label,
854 sizeof(lineinfo.consumer));
855 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100856 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100857 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100858 }
859
860 /*
861 * Userspace only need to know that the kernel is using
862 * this GPIO so it can't use it.
863 */
864 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100865 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
866 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
867 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
868 test_bit(FLAG_EXPORT, &desc->flags) ||
869 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100870 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100871 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100872 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100873 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100874 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100875 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100876 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100877 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100878 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
879
880 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
881 return -EFAULT;
882 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200883 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
884 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +0200885 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
886 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +0200887 }
888 return -EINVAL;
889}
890
Linus Walleij8b92e172016-05-27 14:24:04 +0200891#ifdef CONFIG_COMPAT
892static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
893 unsigned long arg)
894{
895 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
896}
897#endif
898
Linus Walleij3c702e92015-10-21 15:29:53 +0200899/**
900 * gpio_chrdev_open() - open the chardev for ioctl operations
901 * @inode: inode for this chardev
902 * @filp: file struct for storing private data
903 * Returns 0 on success
904 */
905static int gpio_chrdev_open(struct inode *inode, struct file *filp)
906{
907 struct gpio_device *gdev = container_of(inode->i_cdev,
908 struct gpio_device, chrdev);
909
910 /* Fail on open if the backing gpiochip is gone */
911 if (!gdev || !gdev->chip)
912 return -ENODEV;
913 get_device(&gdev->dev);
914 filp->private_data = gdev;
915 return 0;
916}
917
918/**
919 * gpio_chrdev_release() - close chardev after ioctl operations
920 * @inode: inode for this chardev
921 * @filp: file struct for storing private data
922 * Returns 0 on success
923 */
924static int gpio_chrdev_release(struct inode *inode, struct file *filp)
925{
926 struct gpio_device *gdev = container_of(inode->i_cdev,
927 struct gpio_device, chrdev);
928
929 if (!gdev)
930 return -ENODEV;
931 put_device(&gdev->dev);
932 return 0;
933}
934
935
936static const struct file_operations gpio_fileops = {
937 .release = gpio_chrdev_release,
938 .open = gpio_chrdev_open,
939 .owner = THIS_MODULE,
940 .llseek = noop_llseek,
941 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +0200942#ifdef CONFIG_COMPAT
943 .compat_ioctl = gpio_ioctl_compat,
944#endif
Linus Walleij3c702e92015-10-21 15:29:53 +0200945};
946
Linus Walleijff2b1352015-10-20 11:10:38 +0200947static void gpiodevice_release(struct device *dev)
948{
949 struct gpio_device *gdev = dev_get_drvdata(dev);
950
Linus Walleij3c702e92015-10-21 15:29:53 +0200951 cdev_del(&gdev->chrdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200952 list_del(&gdev->list);
953 ida_simple_remove(&gpio_ida, gdev->id);
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700954 kfree(gdev->label);
955 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +0100956 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200957}
958
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700959static int gpiochip_setup_dev(struct gpio_device *gdev)
960{
961 int status;
962
963 cdev_init(&gdev->chrdev, &gpio_fileops);
964 gdev->chrdev.owner = THIS_MODULE;
965 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
966 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
967 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
968 if (status < 0)
969 chip_warn(gdev->chip, "failed to add char device %d:%d\n",
970 MAJOR(gpio_devt), gdev->id);
971 else
972 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
973 MAJOR(gpio_devt), gdev->id);
974 status = device_add(&gdev->dev);
975 if (status)
976 goto err_remove_chardev;
977
978 status = gpiochip_sysfs_register(gdev);
979 if (status)
980 goto err_remove_device;
981
982 /* From this point, the .release() function cleans up gpio_device */
983 gdev->dev.release = gpiodevice_release;
984 get_device(&gdev->dev);
985 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
986 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
987 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
988
989 return 0;
990
991err_remove_device:
992 device_del(&gdev->dev);
993err_remove_chardev:
994 cdev_del(&gdev->chrdev);
995 return status;
996}
997
998static void gpiochip_setup_devs(void)
999{
1000 struct gpio_device *gdev;
1001 int err;
1002
1003 list_for_each_entry(gdev, &gpio_devices, list) {
1004 err = gpiochip_setup_dev(gdev);
1005 if (err)
1006 pr_err("%s: Failed to initialize gpio device (%d)\n",
1007 dev_name(&gdev->dev), err);
1008 }
1009}
1010
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001011/**
Linus Walleijb08ea352015-12-03 15:14:13 +01001012 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -08001013 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001014 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -08001015 *
1016 * Returns a negative errno if the chip can't be registered, such as
1017 * because the chip->base is invalid or already associated with a
1018 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001019 *
Linus Walleijb08ea352015-12-03 15:14:13 +01001020 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001021 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d82008-07-25 01:46:07 -07001022 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1023 * for GPIOs will fail rudely.
1024 *
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001025 * gpiochip_add_data() must only be called after gpiolib initialization,
1026 * ie after core_initcall().
1027 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001028 * If chip->base is negative, this requests dynamic assignment of
1029 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001030 */
Linus Walleijb08ea352015-12-03 15:14:13 +01001031int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -08001032{
1033 unsigned long flags;
1034 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001035 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001036 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001037 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001038
Linus Walleijff2b1352015-10-20 11:10:38 +02001039 /*
1040 * First: allocate and populate the internal stat container, and
1041 * set up the struct device.
1042 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001043 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001044 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001045 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001046 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001047 gdev->chip = chip;
1048 chip->gpiodev = gdev;
1049 if (chip->parent) {
1050 gdev->dev.parent = chip->parent;
1051 gdev->dev.of_node = chip->parent->of_node;
1052 } else {
1053#ifdef CONFIG_OF_GPIO
1054 /* If the gpiochip has an assigned OF node this takes precedence */
1055 if (chip->of_node)
1056 gdev->dev.of_node = chip->of_node;
1057#endif
1058 }
1059 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1060 if (gdev->id < 0) {
1061 status = gdev->id;
1062 goto err_free_gdev;
1063 }
1064 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1065 device_initialize(&gdev->dev);
1066 dev_set_drvdata(&gdev->dev, gdev);
1067 if (chip->parent && chip->parent->driver)
1068 gdev->owner = chip->parent->driver->owner;
1069 else if (chip->owner)
1070 /* TODO: remove chip->owner */
1071 gdev->owner = chip->owner;
1072 else
1073 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001074
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001075 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001076 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001077 status = -ENOMEM;
1078 goto err_free_gdev;
1079 }
1080
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001081 if (chip->ngpio == 0) {
1082 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001083 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001084 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001085 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001086
1087 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001088 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001089 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001090 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001091 if (!gdev->label) {
1092 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001093 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001094 }
1095
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001096 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001097 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001098
David Brownelld2876d02008-02-04 22:28:20 -08001099 spin_lock_irqsave(&gpio_lock, flags);
1100
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001101 /*
1102 * TODO: this allocates a Linux GPIO number base in the global
1103 * GPIO numberspace for this chip. In the long run we want to
1104 * get *rid* of this numberspace and use only descriptors, but
1105 * it may be a pipe dream. It will not happen before we get rid
1106 * of the sysfs interface anyways.
1107 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001108 if (base < 0) {
1109 base = gpiochip_find_base(chip->ngpio);
1110 if (base < 0) {
1111 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001112 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001113 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001114 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001115 /*
1116 * TODO: it should not be necessary to reflect the assigned
1117 * base outside of the GPIO subsystem. Go over drivers and
1118 * see if anyone makes use of this, else drop this and assign
1119 * a poison instead.
1120 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001121 chip->base = base;
1122 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001123 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001124
Linus Walleijff2b1352015-10-20 11:10:38 +02001125 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001126 if (status) {
1127 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001128 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001129 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001130
Linus Walleij545ebd92016-05-30 17:11:59 +02001131 spin_unlock_irqrestore(&gpio_lock, flags);
1132
Linus Walleijff2b1352015-10-20 11:10:38 +02001133 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001134 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001135
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001136 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +02001137 /*
1138 * REVISIT: most hardware initializes GPIOs as inputs
1139 * (often with pullups enabled) so power usage is
1140 * minimized. Linux code should set the gpio direction
1141 * first thing; but until it does, and in case
1142 * chip->get_direction is not set, we may expose the
1143 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001144 */
Linus Walleij72d32002016-04-28 13:33:59 +02001145
1146 if (chip->get_direction) {
1147 /*
1148 * If we have .get_direction, set up the initial
1149 * direction flag from the hardware.
1150 */
1151 int dir = chip->get_direction(chip, i);
1152
1153 if (!dir)
1154 set_bit(FLAG_IS_OUT, &desc->flags);
1155 } else if (!chip->direction_input) {
1156 /*
1157 * If the chip lacks the .direction_input callback
1158 * we logically assume all lines are outputs.
1159 */
1160 set_bit(FLAG_IS_OUT, &desc->flags);
1161 }
David Brownelld2876d02008-02-04 22:28:20 -08001162 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001163
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301164#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001165 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301166#endif
1167
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001168 status = gpiochip_set_desc_names(chip);
1169 if (status)
1170 goto err_remove_from_list;
1171
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001172 status = of_gpiochip_add(chip);
1173 if (status)
1174 goto err_remove_chip;
1175
Mika Westerberg664e3e52014-01-08 12:40:54 +02001176 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001177
Linus Walleij3c702e92015-10-21 15:29:53 +02001178 /*
1179 * By first adding the chardev, and then adding the device,
1180 * we get a device node entry in sysfs under
1181 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1182 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001183 * We can do this only if gpiolib has been initialized.
1184 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001185 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001186 if (gpiolib_initialized) {
1187 status = gpiochip_setup_dev(gdev);
1188 if (status)
1189 goto err_remove_chip;
1190 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001191 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001192
Johan Hovold225fce82015-01-12 17:12:25 +01001193err_remove_chip:
1194 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001195 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001196 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001197err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001198 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001199 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001200 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001201err_free_label:
1202 kfree(gdev->label);
1203err_free_descs:
1204 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001205err_free_gdev:
1206 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001207 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001208 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001209 gdev->base, gdev->base + gdev->ngpio - 1,
1210 chip->label ? : "generic");
1211 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001212 return status;
1213}
Linus Walleijb08ea352015-12-03 15:14:13 +01001214EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -08001215
1216/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001217 * gpiochip_get_data() - get per-subdriver data for the chip
1218 */
1219void *gpiochip_get_data(struct gpio_chip *chip)
1220{
1221 return chip->gpiodev->data;
1222}
1223EXPORT_SYMBOL_GPL(gpiochip_get_data);
1224
1225/**
David Brownelld2876d02008-02-04 22:28:20 -08001226 * gpiochip_remove() - unregister a gpio_chip
1227 * @chip: the chip to unregister
1228 *
1229 * A gpio_chip with any GPIOs still requested may not be removed.
1230 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001231void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001232{
Linus Walleijff2b1352015-10-20 11:10:38 +02001233 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001234 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001235 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001236 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001237 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001238
Linus Walleijff2b1352015-10-20 11:10:38 +02001239 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001240 gpiochip_sysfs_unregister(gdev);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001241 /* Numb the device, cancelling all outstanding operations */
1242 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001243 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001244 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001245 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -06001246 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001247 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001248 /*
1249 * We accept no more calls into the driver from this point, so
1250 * NULL the driver data pointer
1251 */
1252 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001253
Johan Hovold6798aca2015-01-12 17:12:28 +01001254 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001255 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001256 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001257 if (test_bit(FLAG_REQUESTED, &desc->flags))
1258 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001259 }
David Brownelld2876d02008-02-04 22:28:20 -08001260 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001261
Johan Hovoldfab28b82015-05-04 17:10:27 +02001262 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001263 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001264 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001265
Linus Walleijff2b1352015-10-20 11:10:38 +02001266 /*
1267 * The gpiochip side puts its use of the device to rest here:
1268 * if there are no userspace clients, the chardev and device will
1269 * be removed, else it will be dangling until the last user is
1270 * gone.
1271 */
1272 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001273}
1274EXPORT_SYMBOL_GPL(gpiochip_remove);
1275
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301276static void devm_gpio_chip_release(struct device *dev, void *res)
1277{
1278 struct gpio_chip *chip = *(struct gpio_chip **)res;
1279
1280 gpiochip_remove(chip);
1281}
1282
1283static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1284
1285{
1286 struct gpio_chip **r = res;
1287
1288 if (!r || !*r) {
1289 WARN_ON(!r || !*r);
1290 return 0;
1291 }
1292
1293 return *r == data;
1294}
1295
1296/**
1297 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1298 * @dev: the device pointer on which irq_chip belongs to.
1299 * @chip: the chip to register, with chip->base initialized
1300 * Context: potentially before irqs will work
1301 *
1302 * Returns a negative errno if the chip can't be registered, such as
1303 * because the chip->base is invalid or already associated with a
1304 * different chip. Otherwise it returns zero as a success code.
1305 *
1306 * The gpio chip automatically be released when the device is unbound.
1307 */
1308int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1309 void *data)
1310{
1311 struct gpio_chip **ptr;
1312 int ret;
1313
1314 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1315 GFP_KERNEL);
1316 if (!ptr)
1317 return -ENOMEM;
1318
1319 ret = gpiochip_add_data(chip, data);
1320 if (ret < 0) {
1321 devres_free(ptr);
1322 return ret;
1323 }
1324
1325 *ptr = chip;
1326 devres_add(dev, ptr);
1327
1328 return 0;
1329}
1330EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1331
1332/**
1333 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1334 * @dev: device for which which resource was allocated
1335 * @chip: the chip to remove
1336 *
1337 * A gpio_chip with any GPIOs still requested may not be removed.
1338 */
1339void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1340{
1341 int ret;
1342
1343 ret = devres_release(dev, devm_gpio_chip_release,
1344 devm_gpio_chip_match, chip);
1345 if (!ret)
1346 WARN_ON(ret);
1347}
1348EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1349
Grant Likely594fa262010-06-08 07:48:16 -06001350/**
1351 * gpiochip_find() - iterator for locating a specific gpio_chip
1352 * @data: data to pass to match function
1353 * @callback: Callback function to check gpio_chip
1354 *
1355 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1356 * determined by a user supplied @match callback. The callback should return
1357 * 0 if the device doesn't match and non-zero if it does. If the callback is
1358 * non-zero, this function will return to the caller and not iterate over any
1359 * more gpio_chips.
1360 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001361struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001362 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001363 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001364{
Linus Walleijff2b1352015-10-20 11:10:38 +02001365 struct gpio_device *gdev;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001366 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001367 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001368
1369 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001370 list_for_each_entry(gdev, &gpio_devices, list)
1371 if (match(gdev->chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001372 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001373
1374 /* No match? */
Linus Walleijff2b1352015-10-20 11:10:38 +02001375 if (&gdev->list == &gpio_devices)
Alexandre Courbot125eef92013-02-03 01:29:26 +09001376 chip = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +02001377 else
1378 chip = gdev->chip;
1379
Grant Likely594fa262010-06-08 07:48:16 -06001380 spin_unlock_irqrestore(&gpio_lock, flags);
1381
1382 return chip;
1383}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001384EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001385
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001386static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1387{
1388 const char *name = data;
1389
1390 return !strcmp(chip->label, name);
1391}
1392
1393static struct gpio_chip *find_chip_by_name(const char *name)
1394{
1395 return gpiochip_find((void *)name, gpiochip_match_name);
1396}
1397
Linus Walleij14250522014-03-25 10:40:18 +01001398#ifdef CONFIG_GPIOLIB_IRQCHIP
1399
1400/*
1401 * The following is irqchip helper code for gpiochips.
1402 */
1403
1404/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001405 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
1406 * @gpiochip: the gpiochip to set the irqchip chain to
1407 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001408 * @parent_irq: the irq number corresponding to the parent IRQ for this
1409 * chained irqchip
1410 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001411 * coming out of the gpiochip. If the interrupt is nested rather than
1412 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001413 */
1414void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1415 struct irq_chip *irqchip,
1416 int parent_irq,
1417 irq_flow_handler_t parent_handler)
1418{
Linus Walleij83141a72014-09-26 13:50:12 +02001419 unsigned int offset;
1420
Linus Walleij83141a72014-09-26 13:50:12 +02001421 if (!gpiochip->irqdomain) {
1422 chip_err(gpiochip, "called %s before setting up irqchip\n",
1423 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001424 return;
1425 }
1426
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001427 if (parent_handler) {
1428 if (gpiochip->can_sleep) {
1429 chip_err(gpiochip,
1430 "you cannot have chained interrupts on a "
1431 "chip that may sleep\n");
1432 return;
1433 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001434 /*
1435 * The parent irqchip is already using the chip_data for this
1436 * irqchip, so our callbacks simply use the handler_data.
1437 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001438 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1439 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001440
1441 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001442 }
Linus Walleij83141a72014-09-26 13:50:12 +02001443
1444 /* Set the parent IRQ for all affected IRQs */
1445 for (offset = 0; offset < gpiochip->ngpio; offset++)
1446 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
1447 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +01001448}
1449EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1450
1451/**
1452 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1453 * @d: the irqdomain used by this irqchip
1454 * @irq: the global irq number used by this GPIO irqchip irq
1455 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1456 *
1457 * This function will set up the mapping for a certain IRQ line on a
1458 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1459 * stored inside the gpiochip.
1460 */
1461static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1462 irq_hw_number_t hwirq)
1463{
1464 struct gpio_chip *chip = d->host_data;
1465
Linus Walleij14250522014-03-25 10:40:18 +01001466 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001467 /*
1468 * This lock class tells lockdep that GPIO irqs are in a different
1469 * category than their parents, so it won't report false recursion.
1470 */
1471 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +02001472 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001473 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +03001474 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001475 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001476 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001477
Linus Walleij1333b902014-04-23 16:45:12 +02001478 /*
1479 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1480 * is passed as default type.
1481 */
1482 if (chip->irq_default_type != IRQ_TYPE_NONE)
1483 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001484
1485 return 0;
1486}
1487
Linus Walleijc3626fd2014-03-28 20:42:01 +01001488static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1489{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001490 struct gpio_chip *chip = d->host_data;
1491
Linus Walleij1c8732b2014-04-09 13:34:39 +02001492 if (chip->can_sleep)
1493 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001494 irq_set_chip_and_handler(irq, NULL, NULL);
1495 irq_set_chip_data(irq, NULL);
1496}
1497
Linus Walleij14250522014-03-25 10:40:18 +01001498static const struct irq_domain_ops gpiochip_domain_ops = {
1499 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001500 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001501 /* Virtually all GPIO irqchips are twocell:ed */
1502 .xlate = irq_domain_xlate_twocell,
1503};
1504
1505static int gpiochip_irq_reqres(struct irq_data *d)
1506{
1507 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1508
Linus Walleijff2b1352015-10-20 11:10:38 +02001509 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001510 return -ENODEV;
1511
Linus Walleij7e7c0592016-06-22 16:31:54 +02001512 /*
1513 * If it is possible to switch this GPIO to an input
1514 * this is a good time to do it.
1515 */
1516 if (chip->direction_input) {
1517 struct gpio_desc *desc;
1518 int ret;
1519
1520 desc = gpiochip_get_desc(chip, d->hwirq);
1521 if (IS_ERR(desc))
1522 return PTR_ERR(desc);
1523
1524 ret = chip->direction_input(chip, d->hwirq);
1525 if (ret)
1526 return ret;
1527
1528 clear_bit(FLAG_IS_OUT, &desc->flags);
1529 }
1530
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001531 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001532 chip_err(chip,
1533 "unable to lock HW IRQ %lu for IRQ\n",
1534 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001535 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001536 return -EINVAL;
1537 }
1538 return 0;
1539}
1540
1541static void gpiochip_irq_relres(struct irq_data *d)
1542{
1543 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1544
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001545 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001546 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001547}
1548
1549static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1550{
1551 return irq_find_mapping(chip->irqdomain, offset);
1552}
1553
1554/**
1555 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1556 * @gpiochip: the gpiochip to remove the irqchip from
1557 *
1558 * This is called only from gpiochip_remove()
1559 */
1560static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1561{
Linus Walleijc3626fd2014-03-28 20:42:01 +01001562 unsigned int offset;
1563
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001564 acpi_gpiochip_free_interrupts(gpiochip);
1565
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001566 if (gpiochip->irq_parent) {
1567 irq_set_chained_handler(gpiochip->irq_parent, NULL);
1568 irq_set_handler_data(gpiochip->irq_parent, NULL);
1569 }
1570
Linus Walleijc3626fd2014-03-28 20:42:01 +01001571 /* Remove all IRQ mappings and delete the domain */
1572 if (gpiochip->irqdomain) {
1573 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +03001574 irq_dispose_mapping(
1575 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +01001576 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001577 }
Linus Walleij14250522014-03-25 10:40:18 +01001578
1579 if (gpiochip->irqchip) {
1580 gpiochip->irqchip->irq_request_resources = NULL;
1581 gpiochip->irqchip->irq_release_resources = NULL;
1582 gpiochip->irqchip = NULL;
1583 }
1584}
1585
1586/**
1587 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
1588 * @gpiochip: the gpiochip to add the irqchip to
1589 * @irqchip: the irqchip to add to the gpiochip
1590 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1591 * allocate gpiochip irqs from
1592 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001593 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1594 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001595 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001596 *
1597 * This function closely associates a certain irqchip with a certain
1598 * gpiochip, providing an irq domain to translate the local IRQs to
1599 * global irqs in the gpiolib core, and making sure that the gpiochip
1600 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001601 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001602 * from the gpiochip passed as chip data. An irqdomain will be stored
1603 * in the gpiochip that shall be used by the driver to handle IRQ number
1604 * translation. The gpiochip will need to be initialized and registered
1605 * before calling this function.
1606 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001607 * This function will handle two cell:ed simple IRQs and assumes all
1608 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001609 * need to be open coded.
1610 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001611int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
1612 struct irq_chip *irqchip,
1613 unsigned int first_irq,
1614 irq_flow_handler_t handler,
1615 unsigned int type,
1616 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001617{
1618 struct device_node *of_node;
1619 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001620 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001621
1622 if (!gpiochip || !irqchip)
1623 return -EINVAL;
1624
Linus Walleij58383c782015-11-04 09:56:26 +01001625 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001626 pr_err("missing gpiochip .dev parent pointer\n");
1627 return -EINVAL;
1628 }
Linus Walleij58383c782015-11-04 09:56:26 +01001629 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001630#ifdef CONFIG_OF_GPIO
1631 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001632 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001633 * FIXME: get rid of this and use gpiochip->parent->of_node
1634 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001635 */
1636 if (gpiochip->of_node)
1637 of_node = gpiochip->of_node;
1638#endif
1639 gpiochip->irqchip = irqchip;
1640 gpiochip->irq_handler = handler;
1641 gpiochip->irq_default_type = type;
1642 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001643 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +01001644 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1645 gpiochip->ngpio, first_irq,
1646 &gpiochip_domain_ops, gpiochip);
1647 if (!gpiochip->irqdomain) {
1648 gpiochip->irqchip = NULL;
1649 return -EINVAL;
1650 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001651
1652 /*
1653 * It is possible for a driver to override this, but only if the
1654 * alternative functions are both implemented.
1655 */
1656 if (!irqchip->irq_request_resources &&
1657 !irqchip->irq_release_resources) {
1658 irqchip->irq_request_resources = gpiochip_irq_reqres;
1659 irqchip->irq_release_resources = gpiochip_irq_relres;
1660 }
Linus Walleij14250522014-03-25 10:40:18 +01001661
1662 /*
1663 * Prepare the mapping since the irqchip shall be orthogonal to
1664 * any gpiochip calls. If the first_irq was zero, this is
1665 * necessary to allocate descriptors for all IRQs.
1666 */
Linus Walleijc3626fd2014-03-28 20:42:01 +01001667 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1668 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
1669 if (offset == 0)
1670 /*
1671 * Store the base into the gpiochip to be used when
1672 * unmapping the irqs.
1673 */
1674 gpiochip->irq_base = irq_base;
1675 }
Linus Walleij14250522014-03-25 10:40:18 +01001676
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001677 acpi_gpiochip_request_interrupts(gpiochip);
1678
Linus Walleij14250522014-03-25 10:40:18 +01001679 return 0;
1680}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001681EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +01001682
1683#else /* CONFIG_GPIOLIB_IRQCHIP */
1684
1685static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
1686
1687#endif /* CONFIG_GPIOLIB_IRQCHIP */
1688
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001689/**
1690 * gpiochip_generic_request() - request the gpio function for a pin
1691 * @chip: the gpiochip owning the GPIO
1692 * @offset: the offset of the GPIO to request for GPIO function
1693 */
1694int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1695{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001696 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001697}
1698EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1699
1700/**
1701 * gpiochip_generic_free() - free the gpio function from a pin
1702 * @chip: the gpiochip to request the gpio function for
1703 * @offset: the offset of the GPIO to free from GPIO function
1704 */
1705void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1706{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001707 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001708}
1709EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1710
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301711#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001712
Linus Walleij3f0f8672012-11-20 12:40:15 +01001713/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001714 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1715 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001716 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001717 * @gpio_offset: the start offset in the current gpio_chip number space
1718 * @pin_group: name of the pin group inside the pin controller
1719 */
1720int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1721 struct pinctrl_dev *pctldev,
1722 unsigned int gpio_offset, const char *pin_group)
1723{
1724 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001725 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001726 int ret;
1727
1728 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1729 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001730 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001731 return -ENOMEM;
1732 }
1733
1734 /* Use local offset as range ID */
1735 pin_range->range.id = gpio_offset;
1736 pin_range->range.gc = chip;
1737 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001738 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001739 pin_range->pctldev = pctldev;
1740
1741 ret = pinctrl_get_group_pins(pctldev, pin_group,
1742 &pin_range->range.pins,
1743 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001744 if (ret < 0) {
1745 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001746 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001747 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001748
1749 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1750
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001751 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1752 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001753 pinctrl_dev_get_devname(pctldev), pin_group);
1754
Linus Walleij20ec3e32016-02-11 11:03:06 +01001755 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001756
1757 return 0;
1758}
1759EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1760
1761/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001762 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1763 * @chip: the gpiochip to add the range for
1764 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001765 * @gpio_offset: the start offset in the current gpio_chip number space
1766 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001767 * @npins: the number of pins from the offset of each pin space (GPIO and
1768 * pin controller) to accumulate in this range
1769 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001770int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001771 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001772 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301773{
1774 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001775 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001776 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301777
Linus Walleij3f0f8672012-11-20 12:40:15 +01001778 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301779 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001780 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001781 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301782 }
1783
Linus Walleij3f0f8672012-11-20 12:40:15 +01001784 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001785 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001786 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301787 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001788 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001789 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301790 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001791 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301792 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001793 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001794 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001795 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001796 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001797 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001798 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001799 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1800 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001801 pinctl_name,
1802 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301803
Linus Walleij20ec3e32016-02-11 11:03:06 +01001804 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001805
1806 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301807}
Linus Walleij165adc92012-11-06 14:49:39 +01001808EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301809
Linus Walleij3f0f8672012-11-20 12:40:15 +01001810/**
1811 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1812 * @chip: the chip to remove all the mappings for
1813 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301814void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1815{
1816 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01001817 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301818
Linus Walleij20ec3e32016-02-11 11:03:06 +01001819 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301820 list_del(&pin_range->node);
1821 pinctrl_remove_gpio_range(pin_range->pctldev,
1822 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001823 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301824 }
1825}
Linus Walleij165adc92012-11-06 14:49:39 +01001826EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1827
1828#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301829
David Brownelld2876d02008-02-04 22:28:20 -08001830/* These "optional" allocation calls help prevent drivers from stomping
1831 * on each other, and help provide better diagnostics in debugfs.
1832 * They're called even less than the "set direction" calls.
1833 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001834static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001835{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001836 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001837 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001838 unsigned long flags;
1839
1840 spin_lock_irqsave(&gpio_lock, flags);
1841
David Brownelld2876d02008-02-04 22:28:20 -08001842 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001843 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001844 */
1845
1846 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1847 desc_set_label(desc, label ? : "?");
1848 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001849 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001850 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001851 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001852 }
1853
1854 if (chip->request) {
1855 /* chip->request may sleep */
1856 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001857 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001858 spin_lock_irqsave(&gpio_lock, flags);
1859
1860 if (status < 0) {
1861 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001862 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001863 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001864 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001865 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001866 if (chip->get_direction) {
1867 /* chip->get_direction may sleep */
1868 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001869 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001870 spin_lock_irqsave(&gpio_lock, flags);
1871 }
David Brownelld2876d02008-02-04 22:28:20 -08001872done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +03001873 if (status < 0) {
1874 /* Clear flags that might have been set by the caller before
1875 * requesting the GPIO.
1876 */
1877 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1878 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1879 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1880 }
Mika Westerberg77c2d792014-03-10 14:54:50 +02001881 spin_unlock_irqrestore(&gpio_lock, flags);
1882 return status;
1883}
1884
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001885/*
1886 * This descriptor validation needs to be inserted verbatim into each
1887 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02001888 * macro to avoid endless duplication. If the desc is NULL it is an
1889 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001890 */
1891#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02001892 if (!desc) \
1893 return 0; \
1894 if (!desc->gdev) { \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001895 pr_warn("%s: invalid GPIO\n", __func__); \
1896 return -EINVAL; \
1897 } \
1898 if ( !desc->gdev->chip ) { \
1899 dev_warn(&desc->gdev->dev, \
1900 "%s: backing chip is gone\n", __func__); \
1901 return 0; \
1902 } } while (0)
1903
1904#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02001905 if (!desc) \
1906 return; \
1907 if (!desc->gdev) { \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001908 pr_warn("%s: invalid GPIO\n", __func__); \
1909 return; \
1910 } \
1911 if (!desc->gdev->chip) { \
1912 dev_warn(&desc->gdev->dev, \
1913 "%s: backing chip is gone\n", __func__); \
1914 return; \
1915 } } while (0)
1916
1917
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001918int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001919{
1920 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001921 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001922
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001923 VALIDATE_DESC(desc);
1924 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001925
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001926 if (try_module_get(gdev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001927 status = __gpiod_request(desc, label);
1928 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001929 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001930 else
1931 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001932 }
1933
David Brownelld2876d02008-02-04 22:28:20 -08001934 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001935 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001936
David Brownelld2876d02008-02-04 22:28:20 -08001937 return status;
1938}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001939
Mika Westerberg77c2d792014-03-10 14:54:50 +02001940static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001941{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001942 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001943 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001944 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001945
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001946 might_sleep();
1947
Alexandre Courbot372e7222013-02-03 01:29:29 +09001948 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07001949
David Brownelld2876d02008-02-04 22:28:20 -08001950 spin_lock_irqsave(&gpio_lock, flags);
1951
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001952 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07001953 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1954 if (chip->free) {
1955 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001956 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001957 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001958 spin_lock_irqsave(&gpio_lock, flags);
1959 }
David Brownelld2876d02008-02-04 22:28:20 -08001960 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001961 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001962 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301963 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301964 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001965 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001966 ret = true;
1967 }
David Brownelld2876d02008-02-04 22:28:20 -08001968
1969 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001970 return ret;
1971}
1972
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001973void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001974{
Linus Walleij33a68e82016-02-11 10:28:44 +01001975 if (desc && desc->gdev && __gpiod_free(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001976 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001977 put_device(&desc->gdev->dev);
1978 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001979 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01001980 }
David Brownelld2876d02008-02-04 22:28:20 -08001981}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001982
David Brownelld2876d02008-02-04 22:28:20 -08001983/**
1984 * gpiochip_is_requested - return string iff signal was requested
1985 * @chip: controller managing the signal
1986 * @offset: of signal within controller's 0..(ngpio - 1) range
1987 *
1988 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001989 * The string returned is the label passed to gpio_request(); if none has been
1990 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001991 *
1992 * This function is for use by GPIO controller drivers. The label can
1993 * help with diagnostics, and knowing that the signal is used as a GPIO
1994 * can help avoid accidentally multiplexing it to another controller.
1995 */
1996const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1997{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001998 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001999
Dirk Behme48b59532015-08-18 18:02:32 +02002000 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002001 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002002
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002003 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002004
Alexandre Courbot372e7222013-02-03 01:29:29 +09002005 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002006 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002007 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002008}
2009EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2010
Mika Westerberg77c2d792014-03-10 14:54:50 +02002011/**
2012 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2013 * @desc: GPIO descriptor to request
2014 * @label: label for the GPIO
2015 *
2016 * Function allows GPIO chip drivers to request and use their own GPIO
2017 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2018 * function will not increase reference count of the GPIO chip module. This
2019 * allows the GPIO chip module to be unloaded as needed (we assume that the
2020 * GPIO chip driver handles freeing the GPIOs it has requested).
2021 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002022struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2023 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002024{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002025 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2026 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002027
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002028 if (IS_ERR(desc)) {
2029 chip_err(chip, "failed to get GPIO descriptor\n");
2030 return desc;
2031 }
2032
2033 err = __gpiod_request(desc, label);
2034 if (err < 0)
2035 return ERR_PTR(err);
2036
2037 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002038}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002039EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002040
2041/**
2042 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2043 * @desc: GPIO descriptor to free
2044 *
2045 * Function frees the given GPIO requested previously with
2046 * gpiochip_request_own_desc().
2047 */
2048void gpiochip_free_own_desc(struct gpio_desc *desc)
2049{
2050 if (desc)
2051 __gpiod_free(desc);
2052}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002053EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002054
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002055/*
2056 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002057 * some cases this is done in early boot, before IRQs are enabled.
2058 *
2059 * As a rule these aren't called more than once (except for drivers
2060 * using the open-drain emulation idiom) so these are natural places
2061 * to accumulate extra debugging checks. Note that we can't (yet)
2062 * rely on gpio_request() having been called beforehand.
2063 */
2064
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002065/**
2066 * gpiod_direction_input - set the GPIO direction to input
2067 * @desc: GPIO to set to input
2068 *
2069 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2070 * be called safely on it.
2071 *
2072 * Return 0 in case of success, else an error code.
2073 */
2074int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002075{
David Brownelld2876d02008-02-04 22:28:20 -08002076 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002077 int status = -EINVAL;
2078
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002079 VALIDATE_DESC(desc);
2080 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002081
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002082 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002083 gpiod_warn(desc,
2084 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002085 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002086 return -EIO;
2087 }
2088
Alexandre Courbotd82da792014-07-22 16:17:43 +09002089 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002090 if (status == 0)
2091 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002092
Alexandre Courbot372e7222013-02-03 01:29:29 +09002093 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002094
David Brownelld2876d02008-02-04 22:28:20 -08002095 return status;
2096}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002097EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002098
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002099static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002100{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002101 struct gpio_chip *gc = desc->gdev->chip;
2102 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002103
Linus Walleijd468bf92013-09-24 11:54:38 +02002104 /* GPIOs used for IRQs shall not be set as output */
2105 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2106 gpiod_err(desc,
2107 "%s: tried to set a GPIO tied to an IRQ as output\n",
2108 __func__);
2109 return -EIO;
2110 }
2111
Linus Walleijc663e5f2016-03-22 10:51:16 +01002112 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2113 /* First see if we can enable open drain in hardware */
2114 if (gc->set_single_ended) {
2115 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2116 LINE_MODE_OPEN_DRAIN);
2117 if (!ret)
2118 goto set_output_value;
2119 }
2120 /* Emulate open drain by not actively driving the line high */
2121 if (value)
2122 return gpiod_direction_input(desc);
2123 }
2124 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2125 if (gc->set_single_ended) {
2126 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2127 LINE_MODE_OPEN_SOURCE);
2128 if (!ret)
2129 goto set_output_value;
2130 }
2131 /* Emulate open source by not actively driving the line low */
2132 if (!value)
2133 return gpiod_direction_input(desc);
2134 } else {
2135 /* Make sure to disable open drain/source hardware, if any */
2136 if (gc->set_single_ended)
2137 gc->set_single_ended(gc,
2138 gpio_chip_hwgpio(desc),
2139 LINE_MODE_PUSH_PULL);
2140 }
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302141
Linus Walleijc663e5f2016-03-22 10:51:16 +01002142set_output_value:
2143 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002144 gpiod_warn(desc,
2145 "%s: missing set() or direction_output() operations\n",
2146 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002147 return -EIO;
2148 }
2149
Linus Walleijc663e5f2016-03-22 10:51:16 +01002150 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), value);
2151 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002152 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002153 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002154 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2155 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002156}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002157
2158/**
2159 * gpiod_direction_output_raw - set the GPIO direction to output
2160 * @desc: GPIO to set to output
2161 * @value: initial output value of the GPIO
2162 *
2163 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2164 * be called safely on it. The initial value of the output must be specified
2165 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2166 *
2167 * Return 0 in case of success, else an error code.
2168 */
2169int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2170{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002171 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002172 return _gpiod_direction_output_raw(desc, value);
2173}
2174EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2175
2176/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302177 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002178 * @desc: GPIO to set to output
2179 * @value: initial output value of the GPIO
2180 *
2181 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2182 * be called safely on it. The initial value of the output must be specified
2183 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2184 * account.
2185 *
2186 * Return 0 in case of success, else an error code.
2187 */
2188int gpiod_direction_output(struct gpio_desc *desc, int value)
2189{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002190 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002191 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2192 value = !value;
2193 return _gpiod_direction_output_raw(desc, value);
2194}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002195EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002196
Felipe Balbic4b5be92010-05-26 14:42:23 -07002197/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002198 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07002199 * @gpio: the gpio to set debounce time
2200 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002201 *
2202 * returns -ENOTSUPP if the controller does not support setting
2203 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002204 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002205int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002206{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002207 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002208
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002209 VALIDATE_DESC(desc);
2210 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002211 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01002212 gpiod_dbg(desc,
2213 "%s: missing set() or set_debounce() operations\n",
2214 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002215 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002216 }
2217
Alexandre Courbotd82da792014-07-22 16:17:43 +09002218 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002219}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002220EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002221
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002222/**
2223 * gpiod_is_active_low - test whether a GPIO is active-low or not
2224 * @desc: the gpio descriptor to test
2225 *
2226 * Returns 1 if the GPIO is active-low, 0 otherwise.
2227 */
2228int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002229{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002230 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002231 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002232}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002233EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002234
2235/* I/O calls are only valid after configuration completed; the relevant
2236 * "is this a valid GPIO" error checks should already have been done.
2237 *
2238 * "Get" operations are often inlinable as reading a pin value register,
2239 * and masking the relevant bit in that register.
2240 *
2241 * When "set" operations are inlinable, they involve writing that mask to
2242 * one register to set a low value, or a different register to set it high.
2243 * Otherwise locking is needed, so there may be little value to inlining.
2244 *
2245 *------------------------------------------------------------------------
2246 *
2247 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2248 * have requested the GPIO. That can include implicit requesting by
2249 * a direction setting call. Marking a gpio as requested locks its chip
2250 * in memory, guaranteeing that these table lookups need no more locking
2251 * and that gpiochip_remove() will fail.
2252 *
2253 * REVISIT when debugging, consider adding some instrumentation to ensure
2254 * that the GPIO was actually requested.
2255 */
2256
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002257static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002258{
2259 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002260 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002261 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002262
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002263 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002264 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002265 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002266 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002267 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002268 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002269}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002270
David Brownelld2876d02008-02-04 22:28:20 -08002271/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002272 * gpiod_get_raw_value() - return a gpio's raw value
2273 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002274 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002275 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002276 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002277 *
2278 * This function should be called from contexts where we cannot sleep, and will
2279 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002280 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002281int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002282{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002283 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002284 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002285 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002286 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002287}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002288EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002289
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002290/**
2291 * gpiod_get_value() - return a gpio's value
2292 * @desc: gpio whose value will be returned
2293 *
2294 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002295 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002296 *
2297 * This function should be called from contexts where we cannot sleep, and will
2298 * complain if the GPIO chip functions potentially sleep.
2299 */
2300int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002301{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002302 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002303
2304 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002305 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002306 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002307
2308 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002309 if (value < 0)
2310 return value;
2311
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002312 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2313 value = !value;
2314
2315 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002316}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002317EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002318
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302319/*
2320 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002321 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002322 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302323 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002324static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302325{
2326 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002327 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002328 int offset = gpio_chip_hwgpio(desc);
2329
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302330 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002331 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302332 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002333 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302334 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002335 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302336 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002337 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302338 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002339 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302340 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002341 gpiod_err(desc,
2342 "%s: Error in set_value for open drain err %d\n",
2343 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302344}
2345
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302346/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002347 * _gpio_set_open_source_value() - Set the open source gpio's value.
2348 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002349 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302350 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002351static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302352{
2353 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002354 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002355 int offset = gpio_chip_hwgpio(desc);
2356
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302357 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002358 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302359 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002360 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302361 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002362 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302363 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002364 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302365 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002366 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302367 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002368 gpiod_err(desc,
2369 "%s: Error in set_value for open source err %d\n",
2370 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302371}
2372
Alexandre Courbot23600962014-03-11 15:52:09 +09002373static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002374{
2375 struct gpio_chip *chip;
2376
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002377 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002378 trace_gpio_value(desc_to_gpio(desc), 0, value);
2379 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2380 _gpio_set_open_drain_value(desc, value);
2381 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2382 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302383 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002384 chip->set(chip, gpio_chip_hwgpio(desc), value);
2385}
2386
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002387/*
2388 * set multiple outputs on the same chip;
2389 * use the chip's set_multiple function if available;
2390 * otherwise set the outputs sequentially;
2391 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2392 * defines which outputs are to be changed
2393 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2394 * defines the values the outputs specified by mask are to be set to
2395 */
2396static void gpio_chip_set_multiple(struct gpio_chip *chip,
2397 unsigned long *mask, unsigned long *bits)
2398{
2399 if (chip->set_multiple) {
2400 chip->set_multiple(chip, mask, bits);
2401 } else {
2402 int i;
2403 for (i = 0; i < chip->ngpio; i++) {
2404 if (mask[BIT_WORD(i)] == 0) {
2405 /* no more set bits in this mask word;
2406 * skip ahead to the next word */
2407 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
2408 continue;
2409 }
2410 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002411 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002412 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002413 }
2414 }
2415}
2416
Linus Walleij44c72882016-04-24 11:36:59 +02002417void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2418 unsigned int array_size,
2419 struct gpio_desc **desc_array,
2420 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002421{
2422 int i = 0;
2423
2424 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002425 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002426 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2427 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2428 int count = 0;
2429
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002430 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002431 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002432
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002433 memset(mask, 0, sizeof(mask));
2434 do {
2435 struct gpio_desc *desc = desc_array[i];
2436 int hwgpio = gpio_chip_hwgpio(desc);
2437 int value = value_array[i];
2438
2439 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2440 value = !value;
2441 trace_gpio_value(desc_to_gpio(desc), 0, value);
2442 /*
2443 * collect all normal outputs belonging to the same chip
2444 * open drain and open source outputs are set individually
2445 */
2446 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002447 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002448 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2449 _gpio_set_open_source_value(desc, value);
2450 } else {
2451 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002452 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002453 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002454 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002455 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002456 count++;
2457 }
2458 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002459 } while ((i < array_size) &&
2460 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002461 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002462 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002463 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002464 }
2465}
2466
David Brownelld2876d02008-02-04 22:28:20 -08002467/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002468 * gpiod_set_raw_value() - assign a gpio's raw value
2469 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002470 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002471 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002472 * Set the raw value of the GPIO, i.e. the value of its physical line without
2473 * regard for its ACTIVE_LOW status.
2474 *
2475 * This function should be called from contexts where we cannot sleep, and will
2476 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002477 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002478void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002479{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002480 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002481 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002482 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002483 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002484}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002485EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002486
2487/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002488 * gpiod_set_value() - assign a gpio's value
2489 * @desc: gpio whose value will be assigned
2490 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002491 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002492 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2493 * account
2494 *
2495 * This function should be called from contexts where we cannot sleep, and will
2496 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002497 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002498void gpiod_set_value(struct gpio_desc *desc, int value)
2499{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002500 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002501 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002502 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002503 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2504 value = !value;
2505 _gpiod_set_raw_value(desc, value);
2506}
2507EXPORT_SYMBOL_GPL(gpiod_set_value);
2508
2509/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002510 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002511 * @array_size: number of elements in the descriptor / value arrays
2512 * @desc_array: array of GPIO descriptors whose values will be assigned
2513 * @value_array: array of values to assign
2514 *
2515 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2516 * without regard for their ACTIVE_LOW status.
2517 *
2518 * This function should be called from contexts where we cannot sleep, and will
2519 * complain if the GPIO chip functions potentially sleep.
2520 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002521void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002522 struct gpio_desc **desc_array, int *value_array)
2523{
2524 if (!desc_array)
2525 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002526 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2527 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002528}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002529EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002530
2531/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002532 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002533 * @array_size: number of elements in the descriptor / value arrays
2534 * @desc_array: array of GPIO descriptors whose values will be assigned
2535 * @value_array: array of values to assign
2536 *
2537 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2538 * into account.
2539 *
2540 * This function should be called from contexts where we cannot sleep, and will
2541 * complain if the GPIO chip functions potentially sleep.
2542 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002543void gpiod_set_array_value(unsigned int array_size,
2544 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002545{
2546 if (!desc_array)
2547 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002548 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2549 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002550}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002551EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002552
2553/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002554 * gpiod_cansleep() - report whether gpio value access may sleep
2555 * @desc: gpio to check
2556 *
2557 */
2558int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002559{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002560 VALIDATE_DESC(desc);
2561 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002562}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002563EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002564
David Brownell0f6d5042008-10-15 22:03:14 -07002565/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002566 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2567 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002568 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002569 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2570 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002571 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002572int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002573{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002574 struct gpio_chip *chip;
2575 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002576
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002577 VALIDATE_DESC(desc);
2578 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002579 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02002580 if (chip->to_irq) {
2581 int retirq = chip->to_irq(chip, offset);
2582
2583 /* Zero means NO_IRQ */
2584 if (!retirq)
2585 return -ENXIO;
2586
2587 return retirq;
2588 }
2589 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002590}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002591EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002592
Linus Walleijd468bf92013-09-24 11:54:38 +02002593/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002594 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002595 * @chip: the chip the GPIO to lock belongs to
2596 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002597 *
2598 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08002599 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08002600 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002601int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08002602{
Linus Walleij9c102802016-05-25 10:56:03 +02002603 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02002604
Linus Walleij9c102802016-05-25 10:56:03 +02002605 desc = gpiochip_get_desc(chip, offset);
2606 if (IS_ERR(desc))
2607 return PTR_ERR(desc);
2608
2609 /* Flush direction if something changed behind our back */
2610 if (chip->get_direction) {
2611 int dir = chip->get_direction(chip, offset);
2612
2613 if (dir)
2614 clear_bit(FLAG_IS_OUT, &desc->flags);
2615 else
2616 set_bit(FLAG_IS_OUT, &desc->flags);
2617 }
2618
2619 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002620 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02002621 "%s: tried to flag a GPIO set as output for IRQ\n",
2622 __func__);
2623 return -EIO;
2624 }
2625
Linus Walleij9c102802016-05-25 10:56:03 +02002626 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002627 return 0;
2628}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002629EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002630
Linus Walleijd468bf92013-09-24 11:54:38 +02002631/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002632 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002633 * @chip: the chip the GPIO to lock belongs to
2634 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002635 *
2636 * This is used directly by GPIO drivers that want to indicate
2637 * that a certain GPIO is no longer used exclusively for IRQ.
2638 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002639void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02002640{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002641 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02002642 return;
2643
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002644 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002645}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002646EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002647
Linus Walleij6cee3822016-02-11 20:16:45 +01002648bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2649{
2650 if (offset >= chip->ngpio)
2651 return false;
2652
2653 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2654}
2655EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2656
Linus Walleij143b65d2016-02-16 15:41:42 +01002657bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2658{
2659 if (offset >= chip->ngpio)
2660 return false;
2661
2662 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2663}
2664EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2665
2666bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2667{
2668 if (offset >= chip->ngpio)
2669 return false;
2670
2671 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2672}
2673EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2674
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002675/**
2676 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2677 * @desc: gpio whose value will be returned
2678 *
2679 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002680 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002681 *
2682 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002683 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002684int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002685{
David Brownelld2876d02008-02-04 22:28:20 -08002686 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002687 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002688 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002689}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002690EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002691
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002692/**
2693 * gpiod_get_value_cansleep() - return a gpio's value
2694 * @desc: gpio whose value will be returned
2695 *
2696 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002697 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002698 *
2699 * This function is to be called from contexts that can sleep.
2700 */
2701int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002702{
David Brownelld2876d02008-02-04 22:28:20 -08002703 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002704
2705 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002706 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002707 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002708 if (value < 0)
2709 return value;
2710
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002711 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2712 value = !value;
2713
David Brownelld2876d02008-02-04 22:28:20 -08002714 return value;
2715}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002716EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002717
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002718/**
2719 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2720 * @desc: gpio whose value will be assigned
2721 * @value: value to assign
2722 *
2723 * Set the raw value of the GPIO, i.e. the value of its physical line without
2724 * regard for its ACTIVE_LOW status.
2725 *
2726 * This function is to be called from contexts that can sleep.
2727 */
2728void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002729{
David Brownelld2876d02008-02-04 22:28:20 -08002730 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002731 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002732 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002733}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002734EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002735
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002736/**
2737 * gpiod_set_value_cansleep() - assign a gpio's value
2738 * @desc: gpio whose value will be assigned
2739 * @value: value to assign
2740 *
2741 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2742 * account
2743 *
2744 * This function is to be called from contexts that can sleep.
2745 */
2746void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002747{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002748 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002749 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002750 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2751 value = !value;
2752 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002753}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002754EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002755
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002756/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002757 * gpiod_set_raw_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 raw values of the GPIOs, i.e. the values of the physical lines
2763 * without regard for their ACTIVE_LOW status.
2764 *
2765 * This function is to be called from contexts that can sleep.
2766 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002767void gpiod_set_raw_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(true, 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_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002778
2779/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002780 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002781 * @array_size: number of elements in the descriptor / value arrays
2782 * @desc_array: array of GPIO descriptors whose values will be assigned
2783 * @value_array: array of values to assign
2784 *
2785 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2786 * into account.
2787 *
2788 * This function is to be called from contexts that can sleep.
2789 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002790void gpiod_set_array_value_cansleep(unsigned int array_size,
2791 struct gpio_desc **desc_array,
2792 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002793{
2794 might_sleep_if(extra_checks);
2795 if (!desc_array)
2796 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002797 gpiod_set_array_value_complex(false, true, array_size, desc_array,
2798 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002799}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002800EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002801
2802/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002803 * gpiod_add_lookup_table() - register GPIO device consumers
2804 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002805 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002806void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002807{
2808 mutex_lock(&gpio_lookup_lock);
2809
Alexandre Courbotad824782013-12-03 12:20:11 +09002810 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002811
2812 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002813}
2814
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302815/**
2816 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2817 * @table: table of consumers to unregister
2818 */
2819void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2820{
2821 mutex_lock(&gpio_lookup_lock);
2822
2823 list_del(&table->list);
2824
2825 mutex_unlock(&gpio_lookup_lock);
2826}
2827
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002828static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002829 unsigned int idx,
2830 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002831{
2832 char prop_name[32]; /* 32 is max size of property name */
2833 enum of_gpio_flags of_flags;
2834 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02002835 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002836
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002837 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02002838 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01002839 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002840 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02002841 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01002842 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002843 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002844
Thierry Redingdd34c372014-04-23 17:28:09 +02002845 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2846 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07002847 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02002848 break;
2849 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002850
2851 if (IS_ERR(desc))
2852 return desc;
2853
2854 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002855 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002856
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002857 if (of_flags & OF_GPIO_SINGLE_ENDED) {
2858 if (of_flags & OF_GPIO_ACTIVE_LOW)
2859 *flags |= GPIO_OPEN_DRAIN;
2860 else
2861 *flags |= GPIO_OPEN_SOURCE;
2862 }
2863
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002864 return desc;
2865}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002866
Dmitry Torokhov25487532016-03-24 10:50:25 -07002867static struct gpio_desc *acpi_find_gpio(struct device *dev,
2868 const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002869 unsigned int idx,
Dmitry Torokhov25487532016-03-24 10:50:25 -07002870 enum gpiod_flags flags,
2871 enum gpio_lookup_flags *lookupflags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03002872{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002873 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03002874 struct acpi_gpio_info info;
2875 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002876 char propname[32];
2877 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03002878
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002879 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002880 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002881 if (con_id && strcmp(con_id, "gpios")) {
2882 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002883 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002884 } else {
2885 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002886 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002887 }
Mika Westerberge01f4402013-10-10 11:01:10 +03002888
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002889 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
2890 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2891 break;
2892 }
2893
2894 /* Then from plain _CRS GPIOs */
2895 if (IS_ERR(desc)) {
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08002896 if (!acpi_can_fallback_to_crs(adev, con_id))
2897 return ERR_PTR(-ENOENT);
2898
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002899 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2900 if (IS_ERR(desc))
2901 return desc;
Dmitry Torokhov25487532016-03-24 10:50:25 -07002902
2903 if ((flags == GPIOD_OUT_LOW || flags == GPIOD_OUT_HIGH) &&
2904 info.gpioint) {
2905 dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
2906 return ERR_PTR(-ENOENT);
2907 }
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002908 }
2909
Christophe RICARD52044722015-12-23 23:25:34 +01002910 if (info.polarity == GPIO_ACTIVE_LOW)
Dmitry Torokhov25487532016-03-24 10:50:25 -07002911 *lookupflags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002912
2913 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002914}
2915
Alexandre Courbotad824782013-12-03 12:20:11 +09002916static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2917{
2918 const char *dev_id = dev ? dev_name(dev) : NULL;
2919 struct gpiod_lookup_table *table;
2920
2921 mutex_lock(&gpio_lookup_lock);
2922
2923 list_for_each_entry(table, &gpio_lookup_list, list) {
2924 if (table->dev_id && dev_id) {
2925 /*
2926 * Valid strings on both ends, must be identical to have
2927 * a match
2928 */
2929 if (!strcmp(table->dev_id, dev_id))
2930 goto found;
2931 } else {
2932 /*
2933 * One of the pointers is NULL, so both must be to have
2934 * a match
2935 */
2936 if (dev_id == table->dev_id)
2937 goto found;
2938 }
2939 }
2940 table = NULL;
2941
2942found:
2943 mutex_unlock(&gpio_lookup_lock);
2944 return table;
2945}
2946
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002947static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002948 unsigned int idx,
2949 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002950{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002951 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002952 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002953 struct gpiod_lookup *p;
2954
Alexandre Courbotad824782013-12-03 12:20:11 +09002955 table = gpiod_find_lookup_table(dev);
2956 if (!table)
2957 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002958
Alexandre Courbotad824782013-12-03 12:20:11 +09002959 for (p = &table->table[0]; p->chip_label; p++) {
2960 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002961
Alexandre Courbotad824782013-12-03 12:20:11 +09002962 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002963 if (p->idx != idx)
2964 continue;
2965
Alexandre Courbotad824782013-12-03 12:20:11 +09002966 /* If the lookup entry has a con_id, require exact match */
2967 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2968 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002969
Alexandre Courbotad824782013-12-03 12:20:11 +09002970 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002971
Alexandre Courbotad824782013-12-03 12:20:11 +09002972 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002973 dev_err(dev, "cannot find GPIO chip %s\n",
2974 p->chip_label);
2975 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002976 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002977
Alexandre Courbotad824782013-12-03 12:20:11 +09002978 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002979 dev_err(dev,
2980 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2981 idx, chip->ngpio, chip->label);
2982 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09002983 }
2984
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09002985 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09002986 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002987
2988 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09002989 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002990
2991 return desc;
2992}
2993
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002994static int dt_gpio_count(struct device *dev, const char *con_id)
2995{
2996 int ret;
2997 char propname[32];
2998 unsigned int i;
2999
3000 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3001 if (con_id)
3002 snprintf(propname, sizeof(propname), "%s-%s",
3003 con_id, gpio_suffixes[i]);
3004 else
3005 snprintf(propname, sizeof(propname), "%s",
3006 gpio_suffixes[i]);
3007
3008 ret = of_gpio_named_count(dev->of_node, propname);
3009 if (ret >= 0)
3010 break;
3011 }
3012 return ret;
3013}
3014
3015static int platform_gpio_count(struct device *dev, const char *con_id)
3016{
3017 struct gpiod_lookup_table *table;
3018 struct gpiod_lookup *p;
3019 unsigned int count = 0;
3020
3021 table = gpiod_find_lookup_table(dev);
3022 if (!table)
3023 return -ENOENT;
3024
3025 for (p = &table->table[0]; p->chip_label; p++) {
3026 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3027 (!con_id && !p->con_id))
3028 count++;
3029 }
3030 if (!count)
3031 return -ENOENT;
3032
3033 return count;
3034}
3035
3036/**
3037 * gpiod_count - return the number of GPIOs associated with a device / function
3038 * or -ENOENT if no GPIO has been assigned to the requested function
3039 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3040 * @con_id: function within the GPIO consumer
3041 */
3042int gpiod_count(struct device *dev, const char *con_id)
3043{
3044 int count = -ENOENT;
3045
3046 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3047 count = dt_gpio_count(dev, con_id);
3048 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3049 count = acpi_gpio_count(dev, con_id);
3050
3051 if (count < 0)
3052 count = platform_gpio_count(dev, con_id);
3053
3054 return count;
3055}
3056EXPORT_SYMBOL_GPL(gpiod_count);
3057
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003058/**
Thierry Reding08791622014-04-25 16:54:22 +02003059 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003060 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003061 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003062 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003063 *
3064 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003065 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003066 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003067 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003068struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003069 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003070{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003071 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003072}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003073EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003074
3075/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003076 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3077 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3078 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003079 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003080 *
3081 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3082 * the requested function it will return NULL. This is convenient for drivers
3083 * that need to handle optional GPIOs.
3084 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003085struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003086 const char *con_id,
3087 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003088{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003089 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003090}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003091EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003092
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003093/**
3094 * gpiod_parse_flags - helper function to parse GPIO lookup flags
3095 * @desc: gpio to be setup
3096 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3097 * of_get_gpio_hog()
3098 *
3099 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
3100 */
3101static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
3102{
3103 if (lflags & GPIO_ACTIVE_LOW)
3104 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3105 if (lflags & GPIO_OPEN_DRAIN)
3106 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3107 if (lflags & GPIO_OPEN_SOURCE)
3108 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3109}
Benoit Parrotf625d462015-02-02 11:44:44 -06003110
3111/**
3112 * gpiod_configure_flags - helper function to configure a given GPIO
3113 * @desc: gpio whose value will be assigned
3114 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06003115 * @dflags: gpiod_flags - optional GPIO initialization flags
3116 *
3117 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3118 * requested function and/or index, or another IS_ERR() code if an error
3119 * occurred while trying to acquire the GPIO.
3120 */
3121static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003122 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003123{
3124 int status;
3125
Benoit Parrotf625d462015-02-02 11:44:44 -06003126 /* No particular flag request, return here... */
3127 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3128 pr_debug("no flags found for %s\n", con_id);
3129 return 0;
3130 }
3131
3132 /* Process flags */
3133 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3134 status = gpiod_direction_output(desc,
3135 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
3136 else
3137 status = gpiod_direction_input(desc);
3138
3139 return status;
3140}
3141
Thierry Reding29a1f2332014-04-25 17:10:06 +02003142/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003143 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003144 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003145 * @con_id: function within the GPIO consumer
3146 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003147 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003148 *
3149 * This variant of gpiod_get() allows to access GPIOs other than the first
3150 * defined one for functions that define several GPIOs.
3151 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003152 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3153 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003154 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003155 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003156struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003157 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003158 unsigned int idx,
3159 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003160{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003161 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003162 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003163 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003164
3165 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3166
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003167 if (dev) {
3168 /* Using device tree? */
3169 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3170 dev_dbg(dev, "using device tree for GPIO lookup\n");
3171 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3172 } else if (ACPI_COMPANION(dev)) {
3173 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Dmitry Torokhov25487532016-03-24 10:50:25 -07003174 desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003175 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003176 }
3177
3178 /*
3179 * Either we are not using DT or ACPI, or their lookup did not return
3180 * a result. In that case, use platform lookup as a fallback.
3181 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003182 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003183 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003184 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003185 }
3186
3187 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003188 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003189 return desc;
3190 }
3191
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003192 gpiod_parse_flags(desc, lookupflags);
3193
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003194 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003195 if (status < 0)
3196 return ERR_PTR(status);
3197
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003198 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003199 if (status < 0) {
3200 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3201 gpiod_put(desc);
3202 return ERR_PTR(status);
3203 }
3204
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003205 return desc;
3206}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003207EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003208
3209/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003210 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3211 * @fwnode: handle of the firmware node
3212 * @propname: name of the firmware property representing the GPIO
3213 *
3214 * This function can be used for drivers that get their configuration
3215 * from firmware.
3216 *
3217 * Function properly finds the corresponding GPIO using whatever is the
3218 * underlying firmware interface and then makes sure that the GPIO
3219 * descriptor is requested before it is returned to the caller.
3220 *
3221 * In case of error an ERR_PTR() is returned.
3222 */
3223struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3224 const char *propname)
3225{
3226 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3227 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003228 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003229 int ret;
3230
3231 if (!fwnode)
3232 return ERR_PTR(-EINVAL);
3233
3234 if (is_of_node(fwnode)) {
3235 enum of_gpio_flags flags;
3236
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02003237 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02003238 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003239 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003240 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003241 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3242 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003243 } else if (is_acpi_node(fwnode)) {
3244 struct acpi_gpio_info info;
3245
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02003246 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02003247 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01003248 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003249 }
3250
3251 if (IS_ERR(desc))
3252 return desc;
3253
Mika Westerberg40b73182014-10-21 13:33:59 +02003254 if (active_low)
3255 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3256
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003257 if (single_ended) {
3258 if (active_low)
3259 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3260 else
3261 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3262 }
3263
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003264 ret = gpiod_request(desc, NULL);
3265 if (ret)
3266 return ERR_PTR(ret);
3267
Mika Westerberg40b73182014-10-21 13:33:59 +02003268 return desc;
3269}
3270EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3271
3272/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003273 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3274 * function
3275 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3276 * @con_id: function within the GPIO consumer
3277 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003278 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003279 *
3280 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3281 * specified index was assigned to the requested function it will return NULL.
3282 * This is convenient for drivers that need to handle optional GPIOs.
3283 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003284struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003285 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003286 unsigned int index,
3287 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003288{
3289 struct gpio_desc *desc;
3290
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003291 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003292 if (IS_ERR(desc)) {
3293 if (PTR_ERR(desc) == -ENOENT)
3294 return NULL;
3295 }
3296
3297 return desc;
3298}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003299EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003300
3301/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003302 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3303 * @desc: gpio whose value will be assigned
3304 * @name: gpio line name
3305 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3306 * of_get_gpio_hog()
3307 * @dflags: gpiod_flags - optional GPIO initialization flags
3308 */
3309int gpiod_hog(struct gpio_desc *desc, const char *name,
3310 unsigned long lflags, enum gpiod_flags dflags)
3311{
3312 struct gpio_chip *chip;
3313 struct gpio_desc *local_desc;
3314 int hwnum;
3315 int status;
3316
3317 chip = gpiod_to_chip(desc);
3318 hwnum = gpio_chip_hwgpio(desc);
3319
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003320 gpiod_parse_flags(desc, lflags);
3321
Benoit Parrotf625d462015-02-02 11:44:44 -06003322 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3323 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303324 status = PTR_ERR(local_desc);
3325 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3326 name, chip->label, hwnum, status);
3327 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003328 }
3329
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003330 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003331 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303332 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3333 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003334 gpiochip_free_own_desc(desc);
3335 return status;
3336 }
3337
3338 /* Mark GPIO as hogged so it can be identified and removed later */
3339 set_bit(FLAG_IS_HOGGED, &desc->flags);
3340
3341 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3342 desc_to_gpio(desc), name,
3343 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3344 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3345 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3346
3347 return 0;
3348}
3349
3350/**
3351 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3352 * @chip: gpio chip to act on
3353 *
3354 * This is only used by of_gpiochip_remove to free hogged gpios
3355 */
3356static void gpiochip_free_hogs(struct gpio_chip *chip)
3357{
3358 int id;
3359
3360 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003361 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3362 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003363 }
3364}
3365
3366/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003367 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3368 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3369 * @con_id: function within the GPIO consumer
3370 * @flags: optional GPIO initialization flags
3371 *
3372 * This function acquires all the GPIOs defined under a given function.
3373 *
3374 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3375 * no GPIO has been assigned to the requested function, or another IS_ERR()
3376 * code if an error occurred while trying to acquire the GPIOs.
3377 */
3378struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3379 const char *con_id,
3380 enum gpiod_flags flags)
3381{
3382 struct gpio_desc *desc;
3383 struct gpio_descs *descs;
3384 int count;
3385
3386 count = gpiod_count(dev, con_id);
3387 if (count < 0)
3388 return ERR_PTR(count);
3389
3390 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3391 GFP_KERNEL);
3392 if (!descs)
3393 return ERR_PTR(-ENOMEM);
3394
3395 for (descs->ndescs = 0; descs->ndescs < count; ) {
3396 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3397 if (IS_ERR(desc)) {
3398 gpiod_put_array(descs);
3399 return ERR_CAST(desc);
3400 }
3401 descs->desc[descs->ndescs] = desc;
3402 descs->ndescs++;
3403 }
3404 return descs;
3405}
3406EXPORT_SYMBOL_GPL(gpiod_get_array);
3407
3408/**
3409 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3410 * function
3411 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3412 * @con_id: function within the GPIO consumer
3413 * @flags: optional GPIO initialization flags
3414 *
3415 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3416 * assigned to the requested function it will return NULL.
3417 */
3418struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3419 const char *con_id,
3420 enum gpiod_flags flags)
3421{
3422 struct gpio_descs *descs;
3423
3424 descs = gpiod_get_array(dev, con_id, flags);
3425 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3426 return NULL;
3427
3428 return descs;
3429}
3430EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3431
3432/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003433 * gpiod_put - dispose of a GPIO descriptor
3434 * @desc: GPIO descriptor to dispose of
3435 *
3436 * No descriptor can be used after gpiod_put() has been called on it.
3437 */
3438void gpiod_put(struct gpio_desc *desc)
3439{
3440 gpiod_free(desc);
3441}
3442EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003443
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003444/**
3445 * gpiod_put_array - dispose of multiple GPIO descriptors
3446 * @descs: struct gpio_descs containing an array of descriptors
3447 */
3448void gpiod_put_array(struct gpio_descs *descs)
3449{
3450 unsigned int i;
3451
3452 for (i = 0; i < descs->ndescs; i++)
3453 gpiod_put(descs->desc[i]);
3454
3455 kfree(descs);
3456}
3457EXPORT_SYMBOL_GPL(gpiod_put_array);
3458
Linus Walleij3c702e92015-10-21 15:29:53 +02003459static int __init gpiolib_dev_init(void)
3460{
3461 int ret;
3462
3463 /* Register GPIO sysfs bus */
3464 ret = bus_register(&gpio_bus_type);
3465 if (ret < 0) {
3466 pr_err("gpiolib: could not register GPIO bus type\n");
3467 return ret;
3468 }
3469
3470 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3471 if (ret < 0) {
3472 pr_err("gpiolib: failed to allocate char dev region\n");
3473 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003474 } else {
3475 gpiolib_initialized = true;
3476 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003477 }
3478 return ret;
3479}
3480core_initcall(gpiolib_dev_init);
3481
David Brownelld2876d02008-02-04 22:28:20 -08003482#ifdef CONFIG_DEBUG_FS
3483
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003484static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003485{
3486 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003487 struct gpio_chip *chip = gdev->chip;
3488 unsigned gpio = gdev->base;
3489 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003490 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003491 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003492
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003493 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003494 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3495 if (gdesc->name) {
3496 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3497 gpio, gdesc->name);
3498 }
David Brownelld2876d02008-02-04 22:28:20 -08003499 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003500 }
David Brownelld2876d02008-02-04 22:28:20 -08003501
Alexandre Courbot372e7222013-02-03 01:29:29 +09003502 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003503 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003504 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003505 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3506 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003507 is_out ? "out" : "in ",
3508 chip->get
3509 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003510 : "? ",
3511 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003512 seq_printf(s, "\n");
3513 }
3514}
3515
Thierry Redingf9c4a312012-04-12 13:26:01 +02003516static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003517{
Grant Likely362432a2013-02-09 09:41:49 +00003518 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003519 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003520 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003521
3522 s->private = "";
3523
Grant Likely362432a2013-02-09 09:41:49 +00003524 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003525 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003526 if (index-- == 0) {
3527 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003528 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003529 }
3530 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003531
3532 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003533}
3534
3535static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3536{
Grant Likely362432a2013-02-09 09:41:49 +00003537 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003538 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003539 void *ret = NULL;
3540
Grant Likely362432a2013-02-09 09:41:49 +00003541 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003542 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003543 ret = NULL;
3544 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003545 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003546 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003547
3548 s->private = "\n";
3549 ++*pos;
3550
3551 return ret;
3552}
3553
3554static void gpiolib_seq_stop(struct seq_file *s, void *v)
3555{
3556}
3557
3558static int gpiolib_seq_show(struct seq_file *s, void *v)
3559{
Linus Walleijff2b1352015-10-20 11:10:38 +02003560 struct gpio_device *gdev = v;
3561 struct gpio_chip *chip = gdev->chip;
3562 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003563
Linus Walleijff2b1352015-10-20 11:10:38 +02003564 if (!chip) {
3565 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3566 dev_name(&gdev->dev));
3567 return 0;
3568 }
3569
3570 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3571 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003572 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02003573 parent = chip->parent;
3574 if (parent)
3575 seq_printf(s, ", parent: %s/%s",
3576 parent->bus ? parent->bus->name : "no-bus",
3577 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02003578 if (chip->label)
3579 seq_printf(s, ", %s", chip->label);
3580 if (chip->can_sleep)
3581 seq_printf(s, ", can sleep");
3582 seq_printf(s, ":\n");
3583
3584 if (chip->dbg_show)
3585 chip->dbg_show(s, chip);
3586 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003587 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003588
David Brownelld2876d02008-02-04 22:28:20 -08003589 return 0;
3590}
3591
Thierry Redingf9c4a312012-04-12 13:26:01 +02003592static const struct seq_operations gpiolib_seq_ops = {
3593 .start = gpiolib_seq_start,
3594 .next = gpiolib_seq_next,
3595 .stop = gpiolib_seq_stop,
3596 .show = gpiolib_seq_show,
3597};
3598
David Brownelld2876d02008-02-04 22:28:20 -08003599static int gpiolib_open(struct inode *inode, struct file *file)
3600{
Thierry Redingf9c4a312012-04-12 13:26:01 +02003601 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08003602}
3603
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003604static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02003605 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08003606 .open = gpiolib_open,
3607 .read = seq_read,
3608 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02003609 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08003610};
3611
3612static int __init gpiolib_debugfs_init(void)
3613{
3614 /* /sys/kernel/debug/gpio */
3615 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3616 NULL, NULL, &gpiolib_operations);
3617 return 0;
3618}
3619subsys_initcall(gpiolib_debugfs_init);
3620
3621#endif /* DEBUG_FS */