blob: 4f54ff45e09e5e3df0030d6e1ada08e516a999bf [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d2008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020018#include <linux/pinctrl/consumer.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020019#include <linux/cdev.h>
20#include <linux/fs.h>
21#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020022#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020023#include <linux/anon_inodes.h>
Lars-Peter Clausen953b9562016-10-24 13:59:15 +020024#include <linux/file.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);
Mika Westerberg79b804c2016-09-20 15:15:21 +030075static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
76static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020077
Guenter Roeck159f3cd2016-03-31 08:11:30 -070078static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020079
David Brownelld2876d02008-02-04 22:28:20 -080080static inline void desc_set_label(struct gpio_desc *d, const char *label)
81{
David Brownelld2876d02008-02-04 22:28:20 -080082 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080083}
84
Alexandre Courbot372e7222013-02-03 01:29:29 +090085/**
86 * Convert a GPIO number to its descriptor
87 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070088struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090089{
Linus Walleijff2b1352015-10-20 11:10:38 +020090 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +090091 unsigned long flags;
92
93 spin_lock_irqsave(&gpio_lock, flags);
94
Linus Walleijff2b1352015-10-20 11:10:38 +020095 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +010096 if (gdev->base <= gpio &&
97 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +090098 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +010099 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900100 }
101 }
102
103 spin_unlock_irqrestore(&gpio_lock, flags);
104
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900105 if (!gpio_is_valid(gpio))
106 WARN(1, "invalid GPIO %d\n", gpio);
107
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900108 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900109}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700110EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900111
112/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900113 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +0200114 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900115struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
116 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200117{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100118 struct gpio_device *gdev = chip->gpiodev;
119
120 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900121 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200122
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100123 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200124}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900125
126/**
127 * Convert a GPIO descriptor to the integer namespace.
128 * This should disappear in the future but is needed since we still
129 * use GPIO numbers for error messages and sysfs nodes
130 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700131int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900132{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100133 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900134}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700135EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900136
137
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700138/**
139 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
140 * @desc: descriptor to return the chip of
141 */
142struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900143{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100144 if (!desc || !desc->gdev || !desc->gdev->chip)
145 return NULL;
146 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900147}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700148EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800149
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700150/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
151static int gpiochip_find_base(int ngpio)
152{
Linus Walleijff2b1352015-10-20 11:10:38 +0200153 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900154 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700155
Linus Walleijff2b1352015-10-20 11:10:38 +0200156 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900157 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100158 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900159 break;
160 else
161 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100162 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700163 }
164
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900165 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700166 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900167 return base;
168 } else {
169 pr_err("%s: cannot find free range\n", __func__);
170 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700171 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700172}
173
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700174/**
175 * gpiod_get_direction - return the current direction of a GPIO
176 * @desc: GPIO to get the direction of
177 *
178 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
179 *
180 * This function may sleep if gpiod_cansleep() is true.
181 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900182int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300183{
184 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900185 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300186 int status = -EINVAL;
187
Alexandre Courbot372e7222013-02-03 01:29:29 +0900188 chip = gpiod_to_chip(desc);
189 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300190
191 if (!chip->get_direction)
192 return status;
193
Alexandre Courbot372e7222013-02-03 01:29:29 +0900194 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300195 if (status > 0) {
196 /* GPIOF_DIR_IN, or other positive */
197 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900198 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300199 }
200 if (status == 0) {
201 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900202 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300203 }
204 return status;
205}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700206EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300207
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900208/*
209 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800210 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900211 *
212 * Return -EBUSY if the new chip overlaps with some other chip's integer
213 * space.
214 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200215static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900216{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800217 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200218
219 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800220 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200221 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530222 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900223 }
224
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800225 next = list_entry(gpio_devices.next, struct gpio_device, list);
226 if (gdev->base + gdev->ngpio <= next->base) {
227 /* add before first entry */
228 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500229 return 0;
230 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900231
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800232 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
233 if (prev->base + prev->ngpio <= gdev->base) {
234 /* add behind last entry */
235 list_add_tail(&gdev->list, &gpio_devices);
236 return 0;
237 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800238
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800239 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
240 /* at the end of the list */
241 if (&next->list == &gpio_devices)
242 break;
243
244 /* add between prev and next */
245 if (prev->base + prev->ngpio <= gdev->base
246 && gdev->base + gdev->ngpio <= next->base) {
247 list_add(&gdev->list, &prev->list);
248 return 0;
249 }
250 }
251
252 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
253 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900254}
255
Linus Walleijf881bab2015-09-23 16:20:43 -0700256/**
257 * Convert a GPIO name to its descriptor
258 */
259static struct gpio_desc *gpio_name_to_desc(const char * const name)
260{
Linus Walleijff2b1352015-10-20 11:10:38 +0200261 struct gpio_device *gdev;
Linus Walleijf881bab2015-09-23 16:20:43 -0700262 unsigned long flags;
263
264 spin_lock_irqsave(&gpio_lock, flags);
265
Linus Walleijff2b1352015-10-20 11:10:38 +0200266 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700267 int i;
268
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100269 for (i = 0; i != gdev->ngpio; ++i) {
270 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab2015-09-23 16:20:43 -0700271
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100272 if (!desc->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700273 continue;
274
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100275 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700276 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100277 return desc;
Linus Walleijf881bab2015-09-23 16:20:43 -0700278 }
279 }
280 }
281
282 spin_unlock_irqrestore(&gpio_lock, flags);
283
284 return NULL;
285}
286
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200287/*
288 * Takes the names from gc->names and checks if they are all unique. If they
289 * are, they are assigned to their gpio descriptors.
290 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800291 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200292 */
293static int gpiochip_set_desc_names(struct gpio_chip *gc)
294{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100295 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200296 int i;
297
298 if (!gc->names)
299 return 0;
300
301 /* First check all names if they are unique */
302 for (i = 0; i != gc->ngpio; ++i) {
303 struct gpio_desc *gpio;
304
305 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700306 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100307 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200308 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab2015-09-23 16:20:43 -0700309 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200310 }
311
312 /* Then add all names to the GPIO descriptors */
313 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100314 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200315
316 return 0;
317}
318
Linus Walleijd7c51b42016-04-26 10:35:29 +0200319/*
320 * GPIO line handle management
321 */
322
323/**
324 * struct linehandle_state - contains the state of a userspace handle
325 * @gdev: the GPIO device the handle pertains to
326 * @label: consumer label used to tag descriptors
327 * @descs: the GPIO descriptors held by this handle
328 * @numdescs: the number of descriptors held in the descs array
329 */
330struct linehandle_state {
331 struct gpio_device *gdev;
332 const char *label;
333 struct gpio_desc *descs[GPIOHANDLES_MAX];
334 u32 numdescs;
335};
336
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200337#define GPIOHANDLE_REQUEST_VALID_FLAGS \
338 (GPIOHANDLE_REQUEST_INPUT | \
339 GPIOHANDLE_REQUEST_OUTPUT | \
340 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
341 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
342 GPIOHANDLE_REQUEST_OPEN_SOURCE)
343
Linus Walleijd7c51b42016-04-26 10:35:29 +0200344static long linehandle_ioctl(struct file *filep, unsigned int cmd,
345 unsigned long arg)
346{
347 struct linehandle_state *lh = filep->private_data;
348 void __user *ip = (void __user *)arg;
349 struct gpiohandle_data ghd;
350 int i;
351
352 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
353 int val;
354
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200355 memset(&ghd, 0, sizeof(ghd));
356
Linus Walleijd7c51b42016-04-26 10:35:29 +0200357 /* TODO: check if descriptors are really input */
358 for (i = 0; i < lh->numdescs; i++) {
359 val = gpiod_get_value_cansleep(lh->descs[i]);
360 if (val < 0)
361 return val;
362 ghd.values[i] = val;
363 }
364
365 if (copy_to_user(ip, &ghd, sizeof(ghd)))
366 return -EFAULT;
367
368 return 0;
369 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
370 int vals[GPIOHANDLES_MAX];
371
372 /* TODO: check if descriptors are really output */
373 if (copy_from_user(&ghd, ip, sizeof(ghd)))
374 return -EFAULT;
375
376 /* Clamp all values to [0,1] */
377 for (i = 0; i < lh->numdescs; i++)
378 vals[i] = !!ghd.values[i];
379
380 /* Reuse the array setting function */
381 gpiod_set_array_value_complex(false,
382 true,
383 lh->numdescs,
384 lh->descs,
385 vals);
386 return 0;
387 }
388 return -EINVAL;
389}
390
391#ifdef CONFIG_COMPAT
392static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
393 unsigned long arg)
394{
395 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
396}
397#endif
398
399static int linehandle_release(struct inode *inode, struct file *filep)
400{
401 struct linehandle_state *lh = filep->private_data;
402 struct gpio_device *gdev = lh->gdev;
403 int i;
404
405 for (i = 0; i < lh->numdescs; i++)
406 gpiod_free(lh->descs[i]);
407 kfree(lh->label);
408 kfree(lh);
409 put_device(&gdev->dev);
410 return 0;
411}
412
413static const struct file_operations linehandle_fileops = {
414 .release = linehandle_release,
415 .owner = THIS_MODULE,
416 .llseek = noop_llseek,
417 .unlocked_ioctl = linehandle_ioctl,
418#ifdef CONFIG_COMPAT
419 .compat_ioctl = linehandle_ioctl_compat,
420#endif
421};
422
423static int linehandle_create(struct gpio_device *gdev, void __user *ip)
424{
425 struct gpiohandle_request handlereq;
426 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200427 struct file *file;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200428 int fd, i, ret;
429
430 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
431 return -EFAULT;
432 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
433 return -EINVAL;
434
435 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
436 if (!lh)
437 return -ENOMEM;
438 lh->gdev = gdev;
439 get_device(&gdev->dev);
440
441 /* Make sure this is terminated */
442 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
443 if (strlen(handlereq.consumer_label)) {
444 lh->label = kstrdup(handlereq.consumer_label,
445 GFP_KERNEL);
446 if (!lh->label) {
447 ret = -ENOMEM;
448 goto out_free_lh;
449 }
450 }
451
452 /* Request each GPIO */
453 for (i = 0; i < handlereq.lines; i++) {
454 u32 offset = handlereq.lineoffsets[i];
455 u32 lflags = handlereq.flags;
456 struct gpio_desc *desc;
457
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200458 if (offset >= gdev->ngpio) {
459 ret = -EINVAL;
460 goto out_free_descs;
461 }
462
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200463 /* Return an error if a unknown flag is set */
464 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) {
465 ret = -EINVAL;
466 goto out_free_descs;
467 }
468
Linus Walleijd7c51b42016-04-26 10:35:29 +0200469 desc = &gdev->descs[offset];
470 ret = gpiod_request(desc, lh->label);
471 if (ret)
472 goto out_free_descs;
473 lh->descs[i] = desc;
474
475 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
476 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
477 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
478 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
479 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
480 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
481
482 /*
483 * Lines have to be requested explicitly for input
484 * or output, else the line will be treated "as is".
485 */
486 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
487 int val = !!handlereq.default_values[i];
488
489 ret = gpiod_direction_output(desc, val);
490 if (ret)
491 goto out_free_descs;
492 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
493 ret = gpiod_direction_input(desc);
494 if (ret)
495 goto out_free_descs;
496 }
497 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
498 offset);
499 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200500 /* Let i point at the last handle */
501 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200502 lh->numdescs = handlereq.lines;
503
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200504 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200505 if (fd < 0) {
506 ret = fd;
507 goto out_free_descs;
508 }
509
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200510 file = anon_inode_getfile("gpio-linehandle",
511 &linehandle_fileops,
512 lh,
513 O_RDONLY | O_CLOEXEC);
514 if (IS_ERR(file)) {
515 ret = PTR_ERR(file);
516 goto out_put_unused_fd;
517 }
518
Linus Walleijd7c51b42016-04-26 10:35:29 +0200519 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200520 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200521 /*
522 * fput() will trigger the release() callback, so do not go onto
523 * the regular error cleanup path here.
524 */
525 fput(file);
526 put_unused_fd(fd);
527 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200528 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200529
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200530 fd_install(fd, file);
531
Linus Walleijd7c51b42016-04-26 10:35:29 +0200532 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
533 lh->numdescs);
534
535 return 0;
536
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200537out_put_unused_fd:
538 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200539out_free_descs:
540 for (; i >= 0; i--)
541 gpiod_free(lh->descs[i]);
542 kfree(lh->label);
543out_free_lh:
544 kfree(lh);
545 put_device(&gdev->dev);
546 return ret;
547}
548
Linus Walleij61f922d2016-06-02 11:30:15 +0200549/*
550 * GPIO line event management
551 */
552
553/**
554 * struct lineevent_state - contains the state of a userspace event
555 * @gdev: the GPIO device the event pertains to
556 * @label: consumer label used to tag descriptors
557 * @desc: the GPIO descriptor held by this event
558 * @eflags: the event flags this line was requested with
559 * @irq: the interrupt that trigger in response to events on this GPIO
560 * @wait: wait queue that handles blocking reads of events
561 * @events: KFIFO for the GPIO events
562 * @read_lock: mutex lock to protect reads from colliding with adding
563 * new events to the FIFO
564 */
565struct lineevent_state {
566 struct gpio_device *gdev;
567 const char *label;
568 struct gpio_desc *desc;
569 u32 eflags;
570 int irq;
571 wait_queue_head_t wait;
572 DECLARE_KFIFO(events, struct gpioevent_data, 16);
573 struct mutex read_lock;
574};
575
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200576#define GPIOEVENT_REQUEST_VALID_FLAGS \
577 (GPIOEVENT_REQUEST_RISING_EDGE | \
578 GPIOEVENT_REQUEST_FALLING_EDGE)
579
Linus Walleij61f922d2016-06-02 11:30:15 +0200580static unsigned int lineevent_poll(struct file *filep,
581 struct poll_table_struct *wait)
582{
583 struct lineevent_state *le = filep->private_data;
584 unsigned int events = 0;
585
586 poll_wait(filep, &le->wait, wait);
587
588 if (!kfifo_is_empty(&le->events))
589 events = POLLIN | POLLRDNORM;
590
591 return events;
592}
593
594
595static ssize_t lineevent_read(struct file *filep,
596 char __user *buf,
597 size_t count,
598 loff_t *f_ps)
599{
600 struct lineevent_state *le = filep->private_data;
601 unsigned int copied;
602 int ret;
603
604 if (count < sizeof(struct gpioevent_data))
605 return -EINVAL;
606
607 do {
608 if (kfifo_is_empty(&le->events)) {
609 if (filep->f_flags & O_NONBLOCK)
610 return -EAGAIN;
611
612 ret = wait_event_interruptible(le->wait,
613 !kfifo_is_empty(&le->events));
614 if (ret)
615 return ret;
616 }
617
618 if (mutex_lock_interruptible(&le->read_lock))
619 return -ERESTARTSYS;
620 ret = kfifo_to_user(&le->events, buf, count, &copied);
621 mutex_unlock(&le->read_lock);
622
623 if (ret)
624 return ret;
625
626 /*
627 * If we couldn't read anything from the fifo (a different
628 * thread might have been faster) we either return -EAGAIN if
629 * the file descriptor is non-blocking, otherwise we go back to
630 * sleep and wait for more data to arrive.
631 */
632 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
633 return -EAGAIN;
634
635 } while (copied == 0);
636
637 return copied;
638}
639
640static int lineevent_release(struct inode *inode, struct file *filep)
641{
642 struct lineevent_state *le = filep->private_data;
643 struct gpio_device *gdev = le->gdev;
644
645 free_irq(le->irq, le);
646 gpiod_free(le->desc);
647 kfree(le->label);
648 kfree(le);
649 put_device(&gdev->dev);
650 return 0;
651}
652
653static long lineevent_ioctl(struct file *filep, unsigned int cmd,
654 unsigned long arg)
655{
656 struct lineevent_state *le = filep->private_data;
657 void __user *ip = (void __user *)arg;
658 struct gpiohandle_data ghd;
659
660 /*
661 * We can get the value for an event line but not set it,
662 * because it is input by definition.
663 */
664 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
665 int val;
666
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200667 memset(&ghd, 0, sizeof(ghd));
668
Linus Walleij61f922d2016-06-02 11:30:15 +0200669 val = gpiod_get_value_cansleep(le->desc);
670 if (val < 0)
671 return val;
672 ghd.values[0] = val;
673
674 if (copy_to_user(ip, &ghd, sizeof(ghd)))
675 return -EFAULT;
676
677 return 0;
678 }
679 return -EINVAL;
680}
681
682#ifdef CONFIG_COMPAT
683static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
684 unsigned long arg)
685{
686 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
687}
688#endif
689
690static const struct file_operations lineevent_fileops = {
691 .release = lineevent_release,
692 .read = lineevent_read,
693 .poll = lineevent_poll,
694 .owner = THIS_MODULE,
695 .llseek = noop_llseek,
696 .unlocked_ioctl = lineevent_ioctl,
697#ifdef CONFIG_COMPAT
698 .compat_ioctl = lineevent_ioctl_compat,
699#endif
700};
701
Ben Dooks33265b12016-06-17 16:03:13 +0100702static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200703{
704 struct lineevent_state *le = p;
705 struct gpioevent_data ge;
Bartosz Golaszewskib680e222017-07-03 11:12:03 +0200706 int ret, level;
Linus Walleij61f922d2016-06-02 11:30:15 +0200707
Linus Walleijcc1fa4a2018-01-22 13:19:28 +0100708 /* Do not leak kernel stack to userspace */
709 memset(&ge, 0, sizeof(ge));
710
Linus Walleij61f922d2016-06-02 11:30:15 +0200711 ge.timestamp = ktime_get_real_ns();
Bartosz Golaszewskib680e222017-07-03 11:12:03 +0200712 level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200713
Bartosz Golaszewski78c42442017-06-23 13:45:16 +0200714 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
715 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200716 if (level)
717 /* Emit low-to-high event */
718 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
719 else
720 /* Emit high-to-low event */
721 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Bartosz Golaszewskib680e222017-07-03 11:12:03 +0200722 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200723 /* Emit low-to-high event */
724 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Bartosz Golaszewskib680e222017-07-03 11:12:03 +0200725 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200726 /* Emit high-to-low event */
727 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200728 } else {
729 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200730 }
731
732 ret = kfifo_put(&le->events, ge);
733 if (ret != 0)
734 wake_up_poll(&le->wait, POLLIN);
735
736 return IRQ_HANDLED;
737}
738
739static int lineevent_create(struct gpio_device *gdev, void __user *ip)
740{
741 struct gpioevent_request eventreq;
742 struct lineevent_state *le;
743 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200744 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200745 u32 offset;
746 u32 lflags;
747 u32 eflags;
748 int fd;
749 int ret;
750 int irqflags = 0;
751
752 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
753 return -EFAULT;
754
755 le = kzalloc(sizeof(*le), GFP_KERNEL);
756 if (!le)
757 return -ENOMEM;
758 le->gdev = gdev;
759 get_device(&gdev->dev);
760
761 /* Make sure this is terminated */
762 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
763 if (strlen(eventreq.consumer_label)) {
764 le->label = kstrdup(eventreq.consumer_label,
765 GFP_KERNEL);
766 if (!le->label) {
767 ret = -ENOMEM;
768 goto out_free_le;
769 }
770 }
771
772 offset = eventreq.lineoffset;
773 lflags = eventreq.handleflags;
774 eflags = eventreq.eventflags;
775
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200776 if (offset >= gdev->ngpio) {
777 ret = -EINVAL;
778 goto out_free_label;
779 }
780
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200781 /* Return an error if a unknown flag is set */
782 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
783 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
784 ret = -EINVAL;
785 goto out_free_label;
786 }
787
Linus Walleij61f922d2016-06-02 11:30:15 +0200788 /* This is just wrong: we don't look for events on output lines */
789 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
790 ret = -EINVAL;
791 goto out_free_label;
792 }
793
794 desc = &gdev->descs[offset];
795 ret = gpiod_request(desc, le->label);
796 if (ret)
797 goto out_free_desc;
798 le->desc = desc;
799 le->eflags = eflags;
800
801 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
802 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
803 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
804 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
805 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
806 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
807
808 ret = gpiod_direction_input(desc);
809 if (ret)
810 goto out_free_desc;
811
812 le->irq = gpiod_to_irq(desc);
813 if (le->irq <= 0) {
814 ret = -ENODEV;
815 goto out_free_desc;
816 }
817
818 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
819 irqflags |= IRQF_TRIGGER_RISING;
820 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
821 irqflags |= IRQF_TRIGGER_FALLING;
822 irqflags |= IRQF_ONESHOT;
823 irqflags |= IRQF_SHARED;
824
825 INIT_KFIFO(le->events);
826 init_waitqueue_head(&le->wait);
827 mutex_init(&le->read_lock);
828
829 /* Request a thread to read the events */
830 ret = request_threaded_irq(le->irq,
831 NULL,
832 lineevent_irq_thread,
833 irqflags,
834 le->label,
835 le);
836 if (ret)
837 goto out_free_desc;
838
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200839 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200840 if (fd < 0) {
841 ret = fd;
842 goto out_free_irq;
843 }
844
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200845 file = anon_inode_getfile("gpio-event",
846 &lineevent_fileops,
847 le,
848 O_RDONLY | O_CLOEXEC);
849 if (IS_ERR(file)) {
850 ret = PTR_ERR(file);
851 goto out_put_unused_fd;
852 }
853
Linus Walleij61f922d2016-06-02 11:30:15 +0200854 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200855 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200856 /*
857 * fput() will trigger the release() callback, so do not go onto
858 * the regular error cleanup path here.
859 */
860 fput(file);
861 put_unused_fd(fd);
862 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200863 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200864
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200865 fd_install(fd, file);
866
Linus Walleij61f922d2016-06-02 11:30:15 +0200867 return 0;
868
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200869out_put_unused_fd:
870 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +0200871out_free_irq:
872 free_irq(le->irq, le);
873out_free_desc:
874 gpiod_free(le->desc);
875out_free_label:
876 kfree(le->label);
877out_free_le:
878 kfree(le);
879 put_device(&gdev->dev);
880 return ret;
881}
882
Linus Walleij3c702e92015-10-21 15:29:53 +0200883/**
884 * gpio_ioctl() - ioctl handler for the GPIO chardev
885 */
886static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
887{
888 struct gpio_device *gdev = filp->private_data;
889 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200890 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200891
892 /* We fail any subsequent ioctl():s when the chip is gone */
893 if (!chip)
894 return -ENODEV;
895
Linus Walleij521a2ad2016-02-12 22:25:22 +0100896 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200897 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100898 struct gpiochip_info chipinfo;
899
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +0200900 memset(&chipinfo, 0, sizeof(chipinfo));
901
Linus Walleij3c702e92015-10-21 15:29:53 +0200902 strncpy(chipinfo.name, dev_name(&gdev->dev),
903 sizeof(chipinfo.name));
904 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100905 strncpy(chipinfo.label, gdev->label,
906 sizeof(chipinfo.label));
907 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100908 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200909 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
910 return -EFAULT;
911 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100912 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
913 struct gpioline_info lineinfo;
914 struct gpio_desc *desc;
915
916 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
917 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +0200918 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +0100919 return -EINVAL;
920
921 desc = &gdev->descs[lineinfo.line_offset];
922 if (desc->name) {
923 strncpy(lineinfo.name, desc->name,
924 sizeof(lineinfo.name));
925 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
926 } else {
927 lineinfo.name[0] = '\0';
928 }
929 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100930 strncpy(lineinfo.consumer, desc->label,
931 sizeof(lineinfo.consumer));
932 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100933 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100934 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100935 }
936
937 /*
938 * Userspace only need to know that the kernel is using
939 * this GPIO so it can't use it.
940 */
941 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100942 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
943 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
944 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
945 test_bit(FLAG_EXPORT, &desc->flags) ||
946 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100947 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100948 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100949 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100950 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100951 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100952 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100953 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100954 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100955 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
956
957 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
958 return -EFAULT;
959 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200960 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
961 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +0200962 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
963 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +0200964 }
965 return -EINVAL;
966}
967
Linus Walleij8b92e172016-05-27 14:24:04 +0200968#ifdef CONFIG_COMPAT
969static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
970 unsigned long arg)
971{
972 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
973}
974#endif
975
Linus Walleij3c702e92015-10-21 15:29:53 +0200976/**
977 * gpio_chrdev_open() - open the chardev for ioctl operations
978 * @inode: inode for this chardev
979 * @filp: file struct for storing private data
980 * Returns 0 on success
981 */
982static int gpio_chrdev_open(struct inode *inode, struct file *filp)
983{
984 struct gpio_device *gdev = container_of(inode->i_cdev,
985 struct gpio_device, chrdev);
986
987 /* Fail on open if the backing gpiochip is gone */
988 if (!gdev || !gdev->chip)
989 return -ENODEV;
990 get_device(&gdev->dev);
991 filp->private_data = gdev;
Lars-Peter Clausen5f654072016-11-30 13:05:21 +0100992
993 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +0200994}
995
996/**
997 * gpio_chrdev_release() - close chardev after ioctl operations
998 * @inode: inode for this chardev
999 * @filp: file struct for storing private data
1000 * Returns 0 on success
1001 */
1002static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1003{
1004 struct gpio_device *gdev = container_of(inode->i_cdev,
1005 struct gpio_device, chrdev);
1006
1007 if (!gdev)
1008 return -ENODEV;
1009 put_device(&gdev->dev);
1010 return 0;
1011}
1012
1013
1014static const struct file_operations gpio_fileops = {
1015 .release = gpio_chrdev_release,
1016 .open = gpio_chrdev_open,
1017 .owner = THIS_MODULE,
Lars-Peter Clausen5f654072016-11-30 13:05:21 +01001018 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001019 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001020#ifdef CONFIG_COMPAT
1021 .compat_ioctl = gpio_ioctl_compat,
1022#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001023};
1024
Linus Walleijff2b1352015-10-20 11:10:38 +02001025static void gpiodevice_release(struct device *dev)
1026{
1027 struct gpio_device *gdev = dev_get_drvdata(dev);
1028
1029 list_del(&gdev->list);
1030 ida_simple_remove(&gpio_ida, gdev->id);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001031 kfree(gdev->label);
1032 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001033 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001034}
1035
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001036static int gpiochip_setup_dev(struct gpio_device *gdev)
1037{
1038 int status;
1039
1040 cdev_init(&gdev->chrdev, &gpio_fileops);
1041 gdev->chrdev.owner = THIS_MODULE;
1042 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
1043 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
1044 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
1045 if (status < 0)
1046 chip_warn(gdev->chip, "failed to add char device %d:%d\n",
1047 MAJOR(gpio_devt), gdev->id);
1048 else
1049 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1050 MAJOR(gpio_devt), gdev->id);
1051 status = device_add(&gdev->dev);
1052 if (status)
1053 goto err_remove_chardev;
1054
1055 status = gpiochip_sysfs_register(gdev);
1056 if (status)
1057 goto err_remove_device;
1058
1059 /* From this point, the .release() function cleans up gpio_device */
1060 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001061 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1062 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1063 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1064
1065 return 0;
1066
1067err_remove_device:
1068 device_del(&gdev->dev);
1069err_remove_chardev:
1070 cdev_del(&gdev->chrdev);
1071 return status;
1072}
1073
1074static void gpiochip_setup_devs(void)
1075{
1076 struct gpio_device *gdev;
1077 int err;
1078
1079 list_for_each_entry(gdev, &gpio_devices, list) {
1080 err = gpiochip_setup_dev(gdev);
1081 if (err)
1082 pr_err("%s: Failed to initialize gpio device (%d)\n",
1083 dev_name(&gdev->dev), err);
1084 }
1085}
1086
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001087/**
Linus Walleijb08ea352015-12-03 15:14:13 +01001088 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -08001089 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001090 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -08001091 *
1092 * Returns a negative errno if the chip can't be registered, such as
1093 * because the chip->base is invalid or already associated with a
1094 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001095 *
Linus Walleijb08ea352015-12-03 15:14:13 +01001096 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001097 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d2008-07-25 01:46:07 -07001098 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1099 * for GPIOs will fail rudely.
1100 *
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001101 * gpiochip_add_data() must only be called after gpiolib initialization,
1102 * ie after core_initcall().
1103 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001104 * If chip->base is negative, this requests dynamic assignment of
1105 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001106 */
Linus Walleijb08ea352015-12-03 15:14:13 +01001107int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -08001108{
1109 unsigned long flags;
1110 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001111 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001112 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001113 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001114
Linus Walleijff2b1352015-10-20 11:10:38 +02001115 /*
1116 * First: allocate and populate the internal stat container, and
1117 * set up the struct device.
1118 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001119 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001120 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001121 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001122 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001123 gdev->chip = chip;
1124 chip->gpiodev = gdev;
1125 if (chip->parent) {
1126 gdev->dev.parent = chip->parent;
1127 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001128 }
1129
Linus Walleijff2b1352015-10-20 11:10:38 +02001130#ifdef CONFIG_OF_GPIO
1131 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001132 if (chip->of_node)
1133 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001134#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001135
Linus Walleijff2b1352015-10-20 11:10:38 +02001136 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1137 if (gdev->id < 0) {
1138 status = gdev->id;
1139 goto err_free_gdev;
1140 }
1141 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1142 device_initialize(&gdev->dev);
1143 dev_set_drvdata(&gdev->dev, gdev);
1144 if (chip->parent && chip->parent->driver)
1145 gdev->owner = chip->parent->driver->owner;
1146 else if (chip->owner)
1147 /* TODO: remove chip->owner */
1148 gdev->owner = chip->owner;
1149 else
1150 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001151
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001152 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001153 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001154 status = -ENOMEM;
1155 goto err_free_gdev;
1156 }
1157
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001158 if (chip->ngpio == 0) {
1159 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001160 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001161 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001162 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001163
1164 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001165 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001166 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001167 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001168 if (!gdev->label) {
1169 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001170 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001171 }
1172
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001173 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001174 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001175
David Brownelld2876d02008-02-04 22:28:20 -08001176 spin_lock_irqsave(&gpio_lock, flags);
1177
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001178 /*
1179 * TODO: this allocates a Linux GPIO number base in the global
1180 * GPIO numberspace for this chip. In the long run we want to
1181 * get *rid* of this numberspace and use only descriptors, but
1182 * it may be a pipe dream. It will not happen before we get rid
1183 * of the sysfs interface anyways.
1184 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001185 if (base < 0) {
1186 base = gpiochip_find_base(chip->ngpio);
1187 if (base < 0) {
1188 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001189 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001190 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001191 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001192 /*
1193 * TODO: it should not be necessary to reflect the assigned
1194 * base outside of the GPIO subsystem. Go over drivers and
1195 * see if anyone makes use of this, else drop this and assign
1196 * a poison instead.
1197 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001198 chip->base = base;
1199 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001200 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001201
Linus Walleijff2b1352015-10-20 11:10:38 +02001202 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001203 if (status) {
1204 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001205 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001206 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001207
Linus Walleij545ebd92016-05-30 17:11:59 +02001208 spin_unlock_irqrestore(&gpio_lock, flags);
1209
Linus Walleijff2b1352015-10-20 11:10:38 +02001210 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001211 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d2008-07-25 01:46:07 -07001212
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001213 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +02001214 /*
1215 * REVISIT: most hardware initializes GPIOs as inputs
1216 * (often with pullups enabled) so power usage is
1217 * minimized. Linux code should set the gpio direction
1218 * first thing; but until it does, and in case
1219 * chip->get_direction is not set, we may expose the
1220 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001221 */
Linus Walleij72d32002016-04-28 13:33:59 +02001222
1223 if (chip->get_direction) {
1224 /*
1225 * If we have .get_direction, set up the initial
1226 * direction flag from the hardware.
1227 */
1228 int dir = chip->get_direction(chip, i);
1229
1230 if (!dir)
1231 set_bit(FLAG_IS_OUT, &desc->flags);
1232 } else if (!chip->direction_input) {
1233 /*
1234 * If the chip lacks the .direction_input callback
1235 * we logically assume all lines are outputs.
1236 */
1237 set_bit(FLAG_IS_OUT, &desc->flags);
1238 }
David Brownelld2876d02008-02-04 22:28:20 -08001239 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001240
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301241#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001242 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301243#endif
1244
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001245 status = gpiochip_set_desc_names(chip);
1246 if (status)
1247 goto err_remove_from_list;
1248
Mika Westerberg79b804c2016-09-20 15:15:21 +03001249 status = gpiochip_irqchip_init_valid_mask(chip);
1250 if (status)
1251 goto err_remove_from_list;
1252
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001253 status = of_gpiochip_add(chip);
1254 if (status)
1255 goto err_remove_chip;
1256
Mika Westerberg664e3e52014-01-08 12:40:54 +02001257 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001258
Linus Walleij3c702e92015-10-21 15:29:53 +02001259 /*
1260 * By first adding the chardev, and then adding the device,
1261 * we get a device node entry in sysfs under
1262 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1263 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001264 * We can do this only if gpiolib has been initialized.
1265 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001266 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001267 if (gpiolib_initialized) {
1268 status = gpiochip_setup_dev(gdev);
1269 if (status)
1270 goto err_remove_chip;
1271 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001272 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001273
Johan Hovold225fce82015-01-12 17:12:25 +01001274err_remove_chip:
1275 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001276 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001277 of_gpiochip_remove(chip);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001278 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001279err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001280 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001281 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001282 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001283err_free_label:
1284 kfree(gdev->label);
1285err_free_descs:
1286 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001287err_free_gdev:
1288 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001289 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001290 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001291 gdev->base, gdev->base + gdev->ngpio - 1,
1292 chip->label ? : "generic");
1293 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001294 return status;
1295}
Linus Walleijb08ea352015-12-03 15:14:13 +01001296EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -08001297
1298/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001299 * gpiochip_get_data() - get per-subdriver data for the chip
1300 */
1301void *gpiochip_get_data(struct gpio_chip *chip)
1302{
1303 return chip->gpiodev->data;
1304}
1305EXPORT_SYMBOL_GPL(gpiochip_get_data);
1306
1307/**
David Brownelld2876d02008-02-04 22:28:20 -08001308 * gpiochip_remove() - unregister a gpio_chip
1309 * @chip: the chip to unregister
1310 *
1311 * A gpio_chip with any GPIOs still requested may not be removed.
1312 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001313void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001314{
Linus Walleijff2b1352015-10-20 11:10:38 +02001315 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001316 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001317 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001318 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001319 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001320
Linus Walleijff2b1352015-10-20 11:10:38 +02001321 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001322 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven86673e92016-12-19 18:29:23 +01001323 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001324 /* Numb the device, cancelling all outstanding operations */
1325 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001326 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001327 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001328 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001329 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001330 /*
1331 * We accept no more calls into the driver from this point, so
1332 * NULL the driver data pointer
1333 */
1334 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001335
Johan Hovold6798aca2015-01-12 17:12:28 +01001336 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001337 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001338 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001339 if (test_bit(FLAG_REQUESTED, &desc->flags))
1340 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001341 }
David Brownelld2876d02008-02-04 22:28:20 -08001342 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001343
Johan Hovoldfab28b82015-05-04 17:10:27 +02001344 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001345 dev_crit(&gdev->dev,
Linus Walleij58383c72015-11-04 09:56:26 +01001346 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001347
Linus Walleijff2b1352015-10-20 11:10:38 +02001348 /*
1349 * The gpiochip side puts its use of the device to rest here:
1350 * if there are no userspace clients, the chardev and device will
1351 * be removed, else it will be dangling until the last user is
1352 * gone.
1353 */
Ricardo Ribalda Delgadof4833b82016-06-03 19:10:02 +02001354 cdev_del(&gdev->chrdev);
1355 device_del(&gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001356 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001357}
1358EXPORT_SYMBOL_GPL(gpiochip_remove);
1359
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301360static void devm_gpio_chip_release(struct device *dev, void *res)
1361{
1362 struct gpio_chip *chip = *(struct gpio_chip **)res;
1363
1364 gpiochip_remove(chip);
1365}
1366
1367static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1368
1369{
1370 struct gpio_chip **r = res;
1371
1372 if (!r || !*r) {
1373 WARN_ON(!r || !*r);
1374 return 0;
1375 }
1376
1377 return *r == data;
1378}
1379
1380/**
1381 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1382 * @dev: the device pointer on which irq_chip belongs to.
1383 * @chip: the chip to register, with chip->base initialized
1384 * Context: potentially before irqs will work
1385 *
1386 * Returns a negative errno if the chip can't be registered, such as
1387 * because the chip->base is invalid or already associated with a
1388 * different chip. Otherwise it returns zero as a success code.
1389 *
1390 * The gpio chip automatically be released when the device is unbound.
1391 */
1392int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1393 void *data)
1394{
1395 struct gpio_chip **ptr;
1396 int ret;
1397
1398 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1399 GFP_KERNEL);
1400 if (!ptr)
1401 return -ENOMEM;
1402
1403 ret = gpiochip_add_data(chip, data);
1404 if (ret < 0) {
1405 devres_free(ptr);
1406 return ret;
1407 }
1408
1409 *ptr = chip;
1410 devres_add(dev, ptr);
1411
1412 return 0;
1413}
1414EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1415
1416/**
1417 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1418 * @dev: device for which which resource was allocated
1419 * @chip: the chip to remove
1420 *
1421 * A gpio_chip with any GPIOs still requested may not be removed.
1422 */
1423void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1424{
1425 int ret;
1426
1427 ret = devres_release(dev, devm_gpio_chip_release,
1428 devm_gpio_chip_match, chip);
1429 if (!ret)
1430 WARN_ON(ret);
1431}
1432EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1433
Grant Likely594fa262010-06-08 07:48:16 -06001434/**
1435 * gpiochip_find() - iterator for locating a specific gpio_chip
1436 * @data: data to pass to match function
1437 * @callback: Callback function to check gpio_chip
1438 *
1439 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1440 * determined by a user supplied @match callback. The callback should return
1441 * 0 if the device doesn't match and non-zero if it does. If the callback is
1442 * non-zero, this function will return to the caller and not iterate over any
1443 * more gpio_chips.
1444 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001445struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001446 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001447 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001448{
Linus Walleijff2b1352015-10-20 11:10:38 +02001449 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001450 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001451 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001452
1453 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001454 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001455 if (gdev->chip && match(gdev->chip, data)) {
1456 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001457 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001458 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001459
Grant Likely594fa262010-06-08 07:48:16 -06001460 spin_unlock_irqrestore(&gpio_lock, flags);
1461
1462 return chip;
1463}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001464EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001465
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001466static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1467{
1468 const char *name = data;
1469
1470 return !strcmp(chip->label, name);
1471}
1472
1473static struct gpio_chip *find_chip_by_name(const char *name)
1474{
1475 return gpiochip_find((void *)name, gpiochip_match_name);
1476}
1477
Linus Walleij14250522014-03-25 10:40:18 +01001478#ifdef CONFIG_GPIOLIB_IRQCHIP
1479
1480/*
1481 * The following is irqchip helper code for gpiochips.
1482 */
1483
Mika Westerberg79b804c2016-09-20 15:15:21 +03001484static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1485{
1486 int i;
1487
1488 if (!gpiochip->irq_need_valid_mask)
1489 return 0;
1490
1491 gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
1492 sizeof(long), GFP_KERNEL);
1493 if (!gpiochip->irq_valid_mask)
1494 return -ENOMEM;
1495
1496 /* Assume by default all GPIOs are valid */
1497 for (i = 0; i < gpiochip->ngpio; i++)
1498 set_bit(i, gpiochip->irq_valid_mask);
1499
1500 return 0;
1501}
1502
1503static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1504{
1505 kfree(gpiochip->irq_valid_mask);
1506 gpiochip->irq_valid_mask = NULL;
1507}
1508
1509static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1510 unsigned int offset)
1511{
1512 /* No mask means all valid */
1513 if (likely(!gpiochip->irq_valid_mask))
1514 return true;
1515 return test_bit(offset, gpiochip->irq_valid_mask);
1516}
1517
Linus Walleij14250522014-03-25 10:40:18 +01001518/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001519 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
1520 * @gpiochip: the gpiochip to set the irqchip chain to
1521 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001522 * @parent_irq: the irq number corresponding to the parent IRQ for this
1523 * chained irqchip
1524 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001525 * coming out of the gpiochip. If the interrupt is nested rather than
1526 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001527 */
1528void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1529 struct irq_chip *irqchip,
1530 int parent_irq,
1531 irq_flow_handler_t parent_handler)
1532{
Linus Walleij83141a72014-09-26 13:50:12 +02001533 unsigned int offset;
1534
Linus Walleij83141a72014-09-26 13:50:12 +02001535 if (!gpiochip->irqdomain) {
1536 chip_err(gpiochip, "called %s before setting up irqchip\n",
1537 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001538 return;
1539 }
1540
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001541 if (parent_handler) {
1542 if (gpiochip->can_sleep) {
1543 chip_err(gpiochip,
1544 "you cannot have chained interrupts on a "
1545 "chip that may sleep\n");
1546 return;
1547 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001548 /*
1549 * The parent irqchip is already using the chip_data for this
1550 * irqchip, so our callbacks simply use the handler_data.
1551 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001552 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1553 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001554
1555 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001556 }
Linus Walleij83141a72014-09-26 13:50:12 +02001557
1558 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001559 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1560 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1561 continue;
Linus Walleij83141a72014-09-26 13:50:12 +02001562 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
1563 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001564 }
Linus Walleij14250522014-03-25 10:40:18 +01001565}
1566EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1567
1568/**
1569 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1570 * @d: the irqdomain used by this irqchip
1571 * @irq: the global irq number used by this GPIO irqchip irq
1572 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1573 *
1574 * This function will set up the mapping for a certain IRQ line on a
1575 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1576 * stored inside the gpiochip.
1577 */
1578static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1579 irq_hw_number_t hwirq)
1580{
1581 struct gpio_chip *chip = d->host_data;
1582
Linus Walleij14250522014-03-25 10:40:18 +01001583 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001584 /*
1585 * This lock class tells lockdep that GPIO irqs are in a different
1586 * category than their parents, so it won't report false recursion.
1587 */
1588 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +02001589 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001590 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +03001591 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001592 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001593 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001594
Linus Walleij1333b902014-04-23 16:45:12 +02001595 /*
1596 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1597 * is passed as default type.
1598 */
1599 if (chip->irq_default_type != IRQ_TYPE_NONE)
1600 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001601
1602 return 0;
1603}
1604
Linus Walleijc3626fd2014-03-28 20:42:01 +01001605static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1606{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001607 struct gpio_chip *chip = d->host_data;
1608
Linus Walleij1c8732b2014-04-09 13:34:39 +02001609 if (chip->can_sleep)
1610 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001611 irq_set_chip_and_handler(irq, NULL, NULL);
1612 irq_set_chip_data(irq, NULL);
1613}
1614
Linus Walleij14250522014-03-25 10:40:18 +01001615static const struct irq_domain_ops gpiochip_domain_ops = {
1616 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001617 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001618 /* Virtually all GPIO irqchips are twocell:ed */
1619 .xlate = irq_domain_xlate_twocell,
1620};
1621
1622static int gpiochip_irq_reqres(struct irq_data *d)
1623{
1624 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1625
Linus Walleijff2b1352015-10-20 11:10:38 +02001626 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001627 return -ENODEV;
1628
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001629 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001630 chip_err(chip,
1631 "unable to lock HW IRQ %lu for IRQ\n",
1632 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001633 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001634 return -EINVAL;
1635 }
1636 return 0;
1637}
1638
1639static void gpiochip_irq_relres(struct irq_data *d)
1640{
1641 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1642
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001643 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001644 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001645}
1646
1647static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1648{
1649 return irq_find_mapping(chip->irqdomain, offset);
1650}
1651
1652/**
1653 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1654 * @gpiochip: the gpiochip to remove the irqchip from
1655 *
1656 * This is called only from gpiochip_remove()
1657 */
1658static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1659{
Linus Walleijc3626fd2014-03-28 20:42:01 +01001660 unsigned int offset;
1661
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001662 acpi_gpiochip_free_interrupts(gpiochip);
1663
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001664 if (gpiochip->irq_parent) {
1665 irq_set_chained_handler(gpiochip->irq_parent, NULL);
1666 irq_set_handler_data(gpiochip->irq_parent, NULL);
1667 }
1668
Linus Walleijc3626fd2014-03-28 20:42:01 +01001669 /* Remove all IRQ mappings and delete the domain */
1670 if (gpiochip->irqdomain) {
Mika Westerberg79b804c2016-09-20 15:15:21 +03001671 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1672 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1673 continue;
Grygorii Strashkoe3893382014-09-25 19:09:23 +03001674 irq_dispose_mapping(
1675 irq_find_mapping(gpiochip->irqdomain, offset));
Mika Westerberg79b804c2016-09-20 15:15:21 +03001676 }
Linus Walleij14250522014-03-25 10:40:18 +01001677 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001678 }
Linus Walleij14250522014-03-25 10:40:18 +01001679
1680 if (gpiochip->irqchip) {
1681 gpiochip->irqchip->irq_request_resources = NULL;
1682 gpiochip->irqchip->irq_release_resources = NULL;
1683 gpiochip->irqchip = NULL;
1684 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001685
1686 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001687}
1688
1689/**
1690 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
1691 * @gpiochip: the gpiochip to add the irqchip to
1692 * @irqchip: the irqchip to add to the gpiochip
1693 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1694 * allocate gpiochip irqs from
1695 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001696 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1697 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001698 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001699 *
1700 * This function closely associates a certain irqchip with a certain
1701 * gpiochip, providing an irq domain to translate the local IRQs to
1702 * global irqs in the gpiolib core, and making sure that the gpiochip
1703 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001704 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001705 * from the gpiochip passed as chip data. An irqdomain will be stored
1706 * in the gpiochip that shall be used by the driver to handle IRQ number
1707 * translation. The gpiochip will need to be initialized and registered
1708 * before calling this function.
1709 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001710 * This function will handle two cell:ed simple IRQs and assumes all
1711 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001712 * need to be open coded.
1713 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001714int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
1715 struct irq_chip *irqchip,
1716 unsigned int first_irq,
1717 irq_flow_handler_t handler,
1718 unsigned int type,
1719 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001720{
1721 struct device_node *of_node;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001722 bool irq_base_set = false;
Linus Walleij14250522014-03-25 10:40:18 +01001723 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001724 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001725
1726 if (!gpiochip || !irqchip)
1727 return -EINVAL;
1728
Linus Walleij58383c72015-11-04 09:56:26 +01001729 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001730 pr_err("missing gpiochip .dev parent pointer\n");
1731 return -EINVAL;
1732 }
Linus Walleij58383c72015-11-04 09:56:26 +01001733 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001734#ifdef CONFIG_OF_GPIO
1735 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001736 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001737 * FIXME: get rid of this and use gpiochip->parent->of_node
1738 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001739 */
1740 if (gpiochip->of_node)
1741 of_node = gpiochip->of_node;
1742#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001743 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001744 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001745 * used to configure the interrupts, as you may end-up with
1746 * conflicting triggers. Tell the user, and reset to NONE.
1747 */
1748 if (WARN(of_node && type != IRQ_TYPE_NONE,
1749 "%s: Ignoring %d default trigger\n", of_node->full_name, type))
1750 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001751 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1752 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1753 "Ignoring %d default trigger\n", type);
1754 type = IRQ_TYPE_NONE;
1755 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001756
Linus Walleij14250522014-03-25 10:40:18 +01001757 gpiochip->irqchip = irqchip;
1758 gpiochip->irq_handler = handler;
1759 gpiochip->irq_default_type = type;
1760 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001761 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +01001762 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1763 gpiochip->ngpio, first_irq,
1764 &gpiochip_domain_ops, gpiochip);
1765 if (!gpiochip->irqdomain) {
1766 gpiochip->irqchip = NULL;
1767 return -EINVAL;
1768 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001769
1770 /*
1771 * It is possible for a driver to override this, but only if the
1772 * alternative functions are both implemented.
1773 */
1774 if (!irqchip->irq_request_resources &&
1775 !irqchip->irq_release_resources) {
1776 irqchip->irq_request_resources = gpiochip_irq_reqres;
1777 irqchip->irq_release_resources = gpiochip_irq_relres;
1778 }
Linus Walleij14250522014-03-25 10:40:18 +01001779
1780 /*
1781 * Prepare the mapping since the irqchip shall be orthogonal to
1782 * any gpiochip calls. If the first_irq was zero, this is
1783 * necessary to allocate descriptors for all IRQs.
1784 */
Linus Walleijc3626fd2014-03-28 20:42:01 +01001785 for (offset = 0; offset < gpiochip->ngpio; offset++) {
Mika Westerberg79b804c2016-09-20 15:15:21 +03001786 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1787 continue;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001788 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001789 if (!irq_base_set) {
Linus Walleijc3626fd2014-03-28 20:42:01 +01001790 /*
1791 * Store the base into the gpiochip to be used when
1792 * unmapping the irqs.
1793 */
1794 gpiochip->irq_base = irq_base;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001795 irq_base_set = true;
1796 }
Linus Walleijc3626fd2014-03-28 20:42:01 +01001797 }
Linus Walleij14250522014-03-25 10:40:18 +01001798
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001799 acpi_gpiochip_request_interrupts(gpiochip);
1800
Linus Walleij14250522014-03-25 10:40:18 +01001801 return 0;
1802}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001803EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +01001804
1805#else /* CONFIG_GPIOLIB_IRQCHIP */
1806
1807static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03001808static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1809{
1810 return 0;
1811}
1812static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1813{ }
Linus Walleij14250522014-03-25 10:40:18 +01001814
1815#endif /* CONFIG_GPIOLIB_IRQCHIP */
1816
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001817/**
1818 * gpiochip_generic_request() - request the gpio function for a pin
1819 * @chip: the gpiochip owning the GPIO
1820 * @offset: the offset of the GPIO to request for GPIO function
1821 */
1822int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1823{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001824 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001825}
1826EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1827
1828/**
1829 * gpiochip_generic_free() - free the gpio function from a pin
1830 * @chip: the gpiochip to request the gpio function for
1831 * @offset: the offset of the GPIO to free from GPIO function
1832 */
1833void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1834{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001835 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001836}
1837EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1838
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301839#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001840
Linus Walleij3f0f8672012-11-20 12:40:15 +01001841/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001842 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1843 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001844 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001845 * @gpio_offset: the start offset in the current gpio_chip number space
1846 * @pin_group: name of the pin group inside the pin controller
1847 */
1848int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1849 struct pinctrl_dev *pctldev,
1850 unsigned int gpio_offset, const char *pin_group)
1851{
1852 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001853 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001854 int ret;
1855
1856 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1857 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001858 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001859 return -ENOMEM;
1860 }
1861
1862 /* Use local offset as range ID */
1863 pin_range->range.id = gpio_offset;
1864 pin_range->range.gc = chip;
1865 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001866 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001867 pin_range->pctldev = pctldev;
1868
1869 ret = pinctrl_get_group_pins(pctldev, pin_group,
1870 &pin_range->range.pins,
1871 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001872 if (ret < 0) {
1873 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001874 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001875 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001876
1877 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1878
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001879 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1880 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001881 pinctrl_dev_get_devname(pctldev), pin_group);
1882
Linus Walleij20ec3e32016-02-11 11:03:06 +01001883 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001884
1885 return 0;
1886}
1887EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1888
1889/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001890 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1891 * @chip: the gpiochip to add the range for
1892 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001893 * @gpio_offset: the start offset in the current gpio_chip number space
1894 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001895 * @npins: the number of pins from the offset of each pin space (GPIO and
1896 * pin controller) to accumulate in this range
1897 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001898int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001899 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001900 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301901{
1902 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001903 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001904 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301905
Linus Walleij3f0f8672012-11-20 12:40:15 +01001906 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301907 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001908 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001909 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301910 }
1911
Linus Walleij3f0f8672012-11-20 12:40:15 +01001912 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001913 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001914 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301915 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001916 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001917 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301918 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001919 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301920 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001921 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001922 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001923 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001924 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001925 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001926 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001927 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1928 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001929 pinctl_name,
1930 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301931
Linus Walleij20ec3e32016-02-11 11:03:06 +01001932 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001933
1934 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301935}
Linus Walleij165adc92012-11-06 14:49:39 +01001936EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301937
Linus Walleij3f0f8672012-11-20 12:40:15 +01001938/**
1939 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1940 * @chip: the chip to remove all the mappings for
1941 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301942void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1943{
1944 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01001945 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301946
Linus Walleij20ec3e32016-02-11 11:03:06 +01001947 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301948 list_del(&pin_range->node);
1949 pinctrl_remove_gpio_range(pin_range->pctldev,
1950 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001951 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301952 }
1953}
Linus Walleij165adc92012-11-06 14:49:39 +01001954EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1955
1956#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301957
David Brownelld2876d02008-02-04 22:28:20 -08001958/* These "optional" allocation calls help prevent drivers from stomping
1959 * on each other, and help provide better diagnostics in debugfs.
1960 * They're called even less than the "set direction" calls.
1961 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001962static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001963{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001964 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001965 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001966 unsigned long flags;
1967
1968 spin_lock_irqsave(&gpio_lock, flags);
1969
David Brownelld2876d02008-02-04 22:28:20 -08001970 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001971 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001972 */
1973
1974 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1975 desc_set_label(desc, label ? : "?");
1976 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001977 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001978 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001979 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001980 }
1981
1982 if (chip->request) {
1983 /* chip->request may sleep */
1984 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001985 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001986 spin_lock_irqsave(&gpio_lock, flags);
1987
1988 if (status < 0) {
1989 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001990 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001991 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001992 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001993 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001994 if (chip->get_direction) {
1995 /* chip->get_direction may sleep */
1996 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001997 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001998 spin_lock_irqsave(&gpio_lock, flags);
1999 }
David Brownelld2876d02008-02-04 22:28:20 -08002000done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002001 spin_unlock_irqrestore(&gpio_lock, flags);
2002 return status;
2003}
2004
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002005/*
2006 * This descriptor validation needs to be inserted verbatim into each
2007 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002008 * macro to avoid endless duplication. If the desc is NULL it is an
2009 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002010 */
2011#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002012 if (!desc) \
2013 return 0; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002014 if (IS_ERR(desc)) { \
2015 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2016 return PTR_ERR(desc); \
2017 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002018 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002019 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002020 return -EINVAL; \
2021 } \
2022 if ( !desc->gdev->chip ) { \
2023 dev_warn(&desc->gdev->dev, \
2024 "%s: backing chip is gone\n", __func__); \
2025 return 0; \
2026 } } while (0)
2027
2028#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002029 if (!desc) \
2030 return; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002031 if (IS_ERR(desc)) { \
2032 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2033 return; \
2034 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002035 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002036 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002037 return; \
2038 } \
2039 if (!desc->gdev->chip) { \
2040 dev_warn(&desc->gdev->dev, \
2041 "%s: backing chip is gone\n", __func__); \
2042 return; \
2043 } } while (0)
2044
2045
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002046int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002047{
2048 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002049 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002050
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002051 VALIDATE_DESC(desc);
2052 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002053
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002054 if (try_module_get(gdev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002055 status = __gpiod_request(desc, label);
2056 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002057 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002058 else
2059 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002060 }
2061
David Brownelld2876d02008-02-04 22:28:20 -08002062 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002063 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002064
David Brownelld2876d02008-02-04 22:28:20 -08002065 return status;
2066}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002067
Mika Westerberg77c2d792014-03-10 14:54:50 +02002068static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002069{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002070 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002071 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002072 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002073
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002074 might_sleep();
2075
Alexandre Courbot372e7222013-02-03 01:29:29 +09002076 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07002077
David Brownelld2876d02008-02-04 22:28:20 -08002078 spin_lock_irqsave(&gpio_lock, flags);
2079
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002080 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002081 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2082 if (chip->free) {
2083 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002084 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002085 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002086 spin_lock_irqsave(&gpio_lock, flags);
2087 }
David Brownelld2876d02008-02-04 22:28:20 -08002088 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002089 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002090 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302091 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302092 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002093 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002094 ret = true;
2095 }
David Brownelld2876d02008-02-04 22:28:20 -08002096
2097 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002098 return ret;
2099}
2100
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002101void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002102{
Linus Walleij33a68e82016-02-11 10:28:44 +01002103 if (desc && desc->gdev && __gpiod_free(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002104 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002105 put_device(&desc->gdev->dev);
2106 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002107 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002108 }
David Brownelld2876d02008-02-04 22:28:20 -08002109}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002110
David Brownelld2876d02008-02-04 22:28:20 -08002111/**
2112 * gpiochip_is_requested - return string iff signal was requested
2113 * @chip: controller managing the signal
2114 * @offset: of signal within controller's 0..(ngpio - 1) range
2115 *
2116 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002117 * The string returned is the label passed to gpio_request(); if none has been
2118 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002119 *
2120 * This function is for use by GPIO controller drivers. The label can
2121 * help with diagnostics, and knowing that the signal is used as a GPIO
2122 * can help avoid accidentally multiplexing it to another controller.
2123 */
2124const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2125{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002126 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002127
Dirk Behme48b59532015-08-18 18:02:32 +02002128 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002129 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002130
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002131 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002132
Alexandre Courbot372e7222013-02-03 01:29:29 +09002133 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002134 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002135 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002136}
2137EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2138
Mika Westerberg77c2d792014-03-10 14:54:50 +02002139/**
2140 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2141 * @desc: GPIO descriptor to request
2142 * @label: label for the GPIO
2143 *
2144 * Function allows GPIO chip drivers to request and use their own GPIO
2145 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2146 * function will not increase reference count of the GPIO chip module. This
2147 * allows the GPIO chip module to be unloaded as needed (we assume that the
2148 * GPIO chip driver handles freeing the GPIOs it has requested).
2149 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002150struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2151 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002152{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002153 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2154 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002155
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002156 if (IS_ERR(desc)) {
2157 chip_err(chip, "failed to get GPIO descriptor\n");
2158 return desc;
2159 }
2160
2161 err = __gpiod_request(desc, label);
2162 if (err < 0)
2163 return ERR_PTR(err);
2164
2165 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002166}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002167EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002168
2169/**
2170 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2171 * @desc: GPIO descriptor to free
2172 *
2173 * Function frees the given GPIO requested previously with
2174 * gpiochip_request_own_desc().
2175 */
2176void gpiochip_free_own_desc(struct gpio_desc *desc)
2177{
2178 if (desc)
2179 __gpiod_free(desc);
2180}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002181EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002182
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002183/*
2184 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002185 * some cases this is done in early boot, before IRQs are enabled.
2186 *
2187 * As a rule these aren't called more than once (except for drivers
2188 * using the open-drain emulation idiom) so these are natural places
2189 * to accumulate extra debugging checks. Note that we can't (yet)
2190 * rely on gpio_request() having been called beforehand.
2191 */
2192
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002193/**
2194 * gpiod_direction_input - set the GPIO direction to input
2195 * @desc: GPIO to set to input
2196 *
2197 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2198 * be called safely on it.
2199 *
2200 * Return 0 in case of success, else an error code.
2201 */
2202int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002203{
David Brownelld2876d02008-02-04 22:28:20 -08002204 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002205 int status = -EINVAL;
2206
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002207 VALIDATE_DESC(desc);
2208 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002209
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002210 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002211 gpiod_warn(desc,
2212 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002213 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002214 return -EIO;
2215 }
2216
Alexandre Courbotd82da792014-07-22 16:17:43 +09002217 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002218 if (status == 0)
2219 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002220
Alexandre Courbot372e7222013-02-03 01:29:29 +09002221 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002222
David Brownelld2876d02008-02-04 22:28:20 -08002223 return status;
2224}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002225EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002226
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002227static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002228{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002229 struct gpio_chip *gc = desc->gdev->chip;
2230 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002231
Linus Walleijd468bf92013-09-24 11:54:38 +02002232 /* GPIOs used for IRQs shall not be set as output */
2233 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2234 gpiod_err(desc,
2235 "%s: tried to set a GPIO tied to an IRQ as output\n",
2236 __func__);
2237 return -EIO;
2238 }
2239
Linus Walleijc663e5f2016-03-22 10:51:16 +01002240 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2241 /* First see if we can enable open drain in hardware */
2242 if (gc->set_single_ended) {
2243 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2244 LINE_MODE_OPEN_DRAIN);
2245 if (!ret)
2246 goto set_output_value;
2247 }
2248 /* Emulate open drain by not actively driving the line high */
2249 if (value)
2250 return gpiod_direction_input(desc);
2251 }
2252 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2253 if (gc->set_single_ended) {
2254 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2255 LINE_MODE_OPEN_SOURCE);
2256 if (!ret)
2257 goto set_output_value;
2258 }
2259 /* Emulate open source by not actively driving the line low */
2260 if (!value)
2261 return gpiod_direction_input(desc);
2262 } else {
2263 /* Make sure to disable open drain/source hardware, if any */
2264 if (gc->set_single_ended)
2265 gc->set_single_ended(gc,
2266 gpio_chip_hwgpio(desc),
2267 LINE_MODE_PUSH_PULL);
2268 }
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302269
Linus Walleijc663e5f2016-03-22 10:51:16 +01002270set_output_value:
2271 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002272 gpiod_warn(desc,
2273 "%s: missing set() or direction_output() operations\n",
2274 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002275 return -EIO;
2276 }
2277
Linus Walleijc663e5f2016-03-22 10:51:16 +01002278 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), value);
2279 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002280 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002281 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002282 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2283 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002284}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002285
2286/**
2287 * gpiod_direction_output_raw - set the GPIO direction to output
2288 * @desc: GPIO to set to output
2289 * @value: initial output value of the GPIO
2290 *
2291 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2292 * be called safely on it. The initial value of the output must be specified
2293 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2294 *
2295 * Return 0 in case of success, else an error code.
2296 */
2297int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2298{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002299 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002300 return _gpiod_direction_output_raw(desc, value);
2301}
2302EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2303
2304/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302305 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002306 * @desc: GPIO to set to output
2307 * @value: initial output value of the GPIO
2308 *
2309 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2310 * be called safely on it. The initial value of the output must be specified
2311 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2312 * account.
2313 *
2314 * Return 0 in case of success, else an error code.
2315 */
2316int gpiod_direction_output(struct gpio_desc *desc, int value)
2317{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002318 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002319 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2320 value = !value;
2321 return _gpiod_direction_output_raw(desc, value);
2322}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002323EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002324
Felipe Balbic4b5be92010-05-26 14:42:23 -07002325/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002326 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07002327 * @gpio: the gpio to set debounce time
2328 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002329 *
2330 * returns -ENOTSUPP if the controller does not support setting
2331 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002332 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002333int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002334{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002335 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002336
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002337 VALIDATE_DESC(desc);
2338 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002339 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01002340 gpiod_dbg(desc,
2341 "%s: missing set() or set_debounce() operations\n",
2342 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002343 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002344 }
2345
Alexandre Courbotd82da792014-07-22 16:17:43 +09002346 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002347}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002348EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002349
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002350/**
2351 * gpiod_is_active_low - test whether a GPIO is active-low or not
2352 * @desc: the gpio descriptor to test
2353 *
2354 * Returns 1 if the GPIO is active-low, 0 otherwise.
2355 */
2356int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002357{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002358 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002359 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002360}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002361EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002362
2363/* I/O calls are only valid after configuration completed; the relevant
2364 * "is this a valid GPIO" error checks should already have been done.
2365 *
2366 * "Get" operations are often inlinable as reading a pin value register,
2367 * and masking the relevant bit in that register.
2368 *
2369 * When "set" operations are inlinable, they involve writing that mask to
2370 * one register to set a low value, or a different register to set it high.
2371 * Otherwise locking is needed, so there may be little value to inlining.
2372 *
2373 *------------------------------------------------------------------------
2374 *
2375 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2376 * have requested the GPIO. That can include implicit requesting by
2377 * a direction setting call. Marking a gpio as requested locks its chip
2378 * in memory, guaranteeing that these table lookups need no more locking
2379 * and that gpiochip_remove() will fail.
2380 *
2381 * REVISIT when debugging, consider adding some instrumentation to ensure
2382 * that the GPIO was actually requested.
2383 */
2384
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002385static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002386{
2387 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002388 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002389 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002390
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002391 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002392 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002393 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002394 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002395 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002396 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002397}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002398
David Brownelld2876d02008-02-04 22:28:20 -08002399/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002400 * gpiod_get_raw_value() - return a gpio's raw value
2401 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002402 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002403 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002404 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002405 *
2406 * This function should be called from contexts where we cannot sleep, and will
2407 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002408 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002409int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002410{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002411 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002412 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002413 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002414 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002415}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002416EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002417
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002418/**
2419 * gpiod_get_value() - return a gpio's value
2420 * @desc: gpio whose value will be returned
2421 *
2422 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002423 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002424 *
2425 * This function should be called from contexts where we cannot sleep, and will
2426 * complain if the GPIO chip functions potentially sleep.
2427 */
2428int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002429{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002430 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002431
2432 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002433 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002434 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002435
2436 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002437 if (value < 0)
2438 return value;
2439
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002440 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2441 value = !value;
2442
2443 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002444}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002445EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002446
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302447/*
2448 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002449 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002450 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302451 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002452static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302453{
2454 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002455 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002456 int offset = gpio_chip_hwgpio(desc);
2457
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302458 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002459 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302460 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002461 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302462 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002463 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302464 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002465 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302466 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002467 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302468 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002469 gpiod_err(desc,
2470 "%s: Error in set_value for open drain err %d\n",
2471 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302472}
2473
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302474/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002475 * _gpio_set_open_source_value() - Set the open source gpio's value.
2476 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002477 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302478 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002479static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302480{
2481 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002482 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002483 int offset = gpio_chip_hwgpio(desc);
2484
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302485 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002486 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302487 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002488 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302489 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002490 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302491 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002492 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302493 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002494 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302495 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002496 gpiod_err(desc,
2497 "%s: Error in set_value for open source err %d\n",
2498 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302499}
2500
Alexandre Courbot23600962014-03-11 15:52:09 +09002501static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002502{
2503 struct gpio_chip *chip;
2504
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002505 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002506 trace_gpio_value(desc_to_gpio(desc), 0, value);
2507 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2508 _gpio_set_open_drain_value(desc, value);
2509 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2510 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302511 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002512 chip->set(chip, gpio_chip_hwgpio(desc), value);
2513}
2514
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002515/*
2516 * set multiple outputs on the same chip;
2517 * use the chip's set_multiple function if available;
2518 * otherwise set the outputs sequentially;
2519 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2520 * defines which outputs are to be changed
2521 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2522 * defines the values the outputs specified by mask are to be set to
2523 */
2524static void gpio_chip_set_multiple(struct gpio_chip *chip,
2525 unsigned long *mask, unsigned long *bits)
2526{
2527 if (chip->set_multiple) {
2528 chip->set_multiple(chip, mask, bits);
2529 } else {
2530 int i;
2531 for (i = 0; i < chip->ngpio; i++) {
2532 if (mask[BIT_WORD(i)] == 0) {
2533 /* no more set bits in this mask word;
2534 * skip ahead to the next word */
2535 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
2536 continue;
2537 }
2538 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002539 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002540 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002541 }
2542 }
2543}
2544
Linus Walleij44c7288f2016-04-24 11:36:59 +02002545void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2546 unsigned int array_size,
2547 struct gpio_desc **desc_array,
2548 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002549{
2550 int i = 0;
2551
2552 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002553 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002554 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2555 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2556 int count = 0;
2557
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002558 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002559 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002560
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002561 memset(mask, 0, sizeof(mask));
2562 do {
2563 struct gpio_desc *desc = desc_array[i];
2564 int hwgpio = gpio_chip_hwgpio(desc);
2565 int value = value_array[i];
2566
2567 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2568 value = !value;
2569 trace_gpio_value(desc_to_gpio(desc), 0, value);
2570 /*
2571 * collect all normal outputs belonging to the same chip
2572 * open drain and open source outputs are set individually
2573 */
2574 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002575 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002576 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2577 _gpio_set_open_source_value(desc, value);
2578 } else {
2579 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002580 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002581 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002582 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002583 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002584 count++;
2585 }
2586 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002587 } while ((i < array_size) &&
2588 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002589 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002590 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002591 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002592 }
2593}
2594
David Brownelld2876d02008-02-04 22:28:20 -08002595/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002596 * gpiod_set_raw_value() - assign a gpio's raw value
2597 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002598 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002599 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002600 * Set the raw value of the GPIO, i.e. the value of its physical line without
2601 * regard for its ACTIVE_LOW status.
2602 *
2603 * This function should be called from contexts where we cannot sleep, and will
2604 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002605 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002606void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002607{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002608 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002609 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002610 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002611 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002612}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002613EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002614
2615/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002616 * gpiod_set_value() - assign a gpio's value
2617 * @desc: gpio whose value will be assigned
2618 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002619 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002620 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2621 * account
2622 *
2623 * This function should be called from contexts where we cannot sleep, and will
2624 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002625 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002626void gpiod_set_value(struct gpio_desc *desc, int value)
2627{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002628 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002629 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002630 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002631 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2632 value = !value;
2633 _gpiod_set_raw_value(desc, value);
2634}
2635EXPORT_SYMBOL_GPL(gpiod_set_value);
2636
2637/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002638 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002639 * @array_size: number of elements in the descriptor / value arrays
2640 * @desc_array: array of GPIO descriptors whose values will be assigned
2641 * @value_array: array of values to assign
2642 *
2643 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2644 * without regard for their ACTIVE_LOW status.
2645 *
2646 * This function should be called from contexts where we cannot sleep, and will
2647 * complain if the GPIO chip functions potentially sleep.
2648 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002649void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002650 struct gpio_desc **desc_array, int *value_array)
2651{
2652 if (!desc_array)
2653 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002654 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2655 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002656}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002657EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002658
2659/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002660 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002661 * @array_size: number of elements in the descriptor / value arrays
2662 * @desc_array: array of GPIO descriptors whose values will be assigned
2663 * @value_array: array of values to assign
2664 *
2665 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2666 * into account.
2667 *
2668 * This function should be called from contexts where we cannot sleep, and will
2669 * complain if the GPIO chip functions potentially sleep.
2670 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002671void gpiod_set_array_value(unsigned int array_size,
2672 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002673{
2674 if (!desc_array)
2675 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002676 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2677 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002678}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002679EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002680
2681/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002682 * gpiod_cansleep() - report whether gpio value access may sleep
2683 * @desc: gpio to check
2684 *
2685 */
2686int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002687{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002688 VALIDATE_DESC(desc);
2689 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002690}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002691EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002692
David Brownell0f6d5042008-10-15 22:03:14 -07002693/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002694 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2695 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002696 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002697 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2698 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002699 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002700int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002701{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002702 struct gpio_chip *chip;
2703 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002704
Linus Walleij79bb71b2016-06-15 22:57:38 +02002705 /*
2706 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
2707 * requires this function to not return zero on an invalid descriptor
2708 * but rather a negative error number.
2709 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02002710 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02002711 return -EINVAL;
2712
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002713 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002714 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02002715 if (chip->to_irq) {
2716 int retirq = chip->to_irq(chip, offset);
2717
2718 /* Zero means NO_IRQ */
2719 if (!retirq)
2720 return -ENXIO;
2721
2722 return retirq;
2723 }
2724 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002725}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002726EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002727
Linus Walleijd468bf92013-09-24 11:54:38 +02002728/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002729 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002730 * @chip: the chip the GPIO to lock belongs to
2731 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002732 *
2733 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08002734 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08002735 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002736int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08002737{
Linus Walleij9c102802016-05-25 10:56:03 +02002738 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02002739
Linus Walleij9c102802016-05-25 10:56:03 +02002740 desc = gpiochip_get_desc(chip, offset);
2741 if (IS_ERR(desc))
2742 return PTR_ERR(desc);
2743
Linus Walleij60f83392016-11-12 15:01:09 +01002744 /*
2745 * If it's fast: flush the direction setting if something changed
2746 * behind our back
2747 */
2748 if (!chip->can_sleep && chip->get_direction) {
Linus Walleij9c102802016-05-25 10:56:03 +02002749 int dir = chip->get_direction(chip, offset);
2750
2751 if (dir)
2752 clear_bit(FLAG_IS_OUT, &desc->flags);
2753 else
2754 set_bit(FLAG_IS_OUT, &desc->flags);
2755 }
2756
2757 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002758 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02002759 "%s: tried to flag a GPIO set as output for IRQ\n",
2760 __func__);
2761 return -EIO;
2762 }
2763
Linus Walleij9c102802016-05-25 10:56:03 +02002764 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002765 return 0;
2766}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002767EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002768
Linus Walleijd468bf92013-09-24 11:54:38 +02002769/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002770 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002771 * @chip: the chip the GPIO to lock belongs to
2772 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002773 *
2774 * This is used directly by GPIO drivers that want to indicate
2775 * that a certain GPIO is no longer used exclusively for IRQ.
2776 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002777void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02002778{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002779 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02002780 return;
2781
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002782 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002783}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002784EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002785
Linus Walleij6cee3822016-02-11 20:16:45 +01002786bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2787{
2788 if (offset >= chip->ngpio)
2789 return false;
2790
2791 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2792}
2793EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2794
Linus Walleij143b65d2016-02-16 15:41:42 +01002795bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2796{
2797 if (offset >= chip->ngpio)
2798 return false;
2799
2800 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2801}
2802EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2803
2804bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2805{
2806 if (offset >= chip->ngpio)
2807 return false;
2808
2809 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2810}
2811EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2812
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002813/**
2814 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2815 * @desc: gpio whose value will be returned
2816 *
2817 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002818 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002819 *
2820 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002821 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002822int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002823{
David Brownelld2876d02008-02-04 22:28:20 -08002824 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002825 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002826 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002827}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002828EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002829
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002830/**
2831 * gpiod_get_value_cansleep() - return a gpio's value
2832 * @desc: gpio whose value will be returned
2833 *
2834 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002835 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002836 *
2837 * This function is to be called from contexts that can sleep.
2838 */
2839int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002840{
David Brownelld2876d02008-02-04 22:28:20 -08002841 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002842
2843 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002844 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002845 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002846 if (value < 0)
2847 return value;
2848
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002849 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2850 value = !value;
2851
David Brownelld2876d02008-02-04 22:28:20 -08002852 return value;
2853}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002854EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002855
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002856/**
2857 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2858 * @desc: gpio whose value will be assigned
2859 * @value: value to assign
2860 *
2861 * Set the raw value of the GPIO, i.e. the value of its physical line without
2862 * regard for its ACTIVE_LOW status.
2863 *
2864 * This function is to be called from contexts that can sleep.
2865 */
2866void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002867{
David Brownelld2876d02008-02-04 22:28:20 -08002868 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002869 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002870 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002871}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002872EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002873
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002874/**
2875 * gpiod_set_value_cansleep() - assign a gpio's value
2876 * @desc: gpio whose value will be assigned
2877 * @value: value to assign
2878 *
2879 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2880 * account
2881 *
2882 * This function is to be called from contexts that can sleep.
2883 */
2884void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002885{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002886 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002887 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002888 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2889 value = !value;
2890 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002891}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002892EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002893
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002894/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002895 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002896 * @array_size: number of elements in the descriptor / value arrays
2897 * @desc_array: array of GPIO descriptors whose values will be assigned
2898 * @value_array: array of values to assign
2899 *
2900 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2901 * without regard for their ACTIVE_LOW status.
2902 *
2903 * This function is to be called from contexts that can sleep.
2904 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002905void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2906 struct gpio_desc **desc_array,
2907 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002908{
2909 might_sleep_if(extra_checks);
2910 if (!desc_array)
2911 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002912 gpiod_set_array_value_complex(true, true, array_size, desc_array,
2913 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002914}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002915EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002916
2917/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002918 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002919 * @array_size: number of elements in the descriptor / value arrays
2920 * @desc_array: array of GPIO descriptors whose values will be assigned
2921 * @value_array: array of values to assign
2922 *
2923 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2924 * into account.
2925 *
2926 * This function is to be called from contexts that can sleep.
2927 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002928void gpiod_set_array_value_cansleep(unsigned int array_size,
2929 struct gpio_desc **desc_array,
2930 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002931{
2932 might_sleep_if(extra_checks);
2933 if (!desc_array)
2934 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002935 gpiod_set_array_value_complex(false, true, array_size, desc_array,
2936 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002937}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002938EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002939
2940/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002941 * gpiod_add_lookup_table() - register GPIO device consumers
2942 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002943 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002944void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002945{
2946 mutex_lock(&gpio_lookup_lock);
2947
Alexandre Courbotad824782013-12-03 12:20:11 +09002948 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002949
2950 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002951}
2952
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302953/**
2954 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2955 * @table: table of consumers to unregister
2956 */
2957void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2958{
2959 mutex_lock(&gpio_lookup_lock);
2960
2961 list_del(&table->list);
2962
2963 mutex_unlock(&gpio_lookup_lock);
2964}
2965
Alexandre Courbotad824782013-12-03 12:20:11 +09002966static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2967{
2968 const char *dev_id = dev ? dev_name(dev) : NULL;
2969 struct gpiod_lookup_table *table;
2970
2971 mutex_lock(&gpio_lookup_lock);
2972
2973 list_for_each_entry(table, &gpio_lookup_list, list) {
2974 if (table->dev_id && dev_id) {
2975 /*
2976 * Valid strings on both ends, must be identical to have
2977 * a match
2978 */
2979 if (!strcmp(table->dev_id, dev_id))
2980 goto found;
2981 } else {
2982 /*
2983 * One of the pointers is NULL, so both must be to have
2984 * a match
2985 */
2986 if (dev_id == table->dev_id)
2987 goto found;
2988 }
2989 }
2990 table = NULL;
2991
2992found:
2993 mutex_unlock(&gpio_lookup_lock);
2994 return table;
2995}
2996
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002997static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002998 unsigned int idx,
2999 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003000{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003001 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003002 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003003 struct gpiod_lookup *p;
3004
Alexandre Courbotad824782013-12-03 12:20:11 +09003005 table = gpiod_find_lookup_table(dev);
3006 if (!table)
3007 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003008
Alexandre Courbotad824782013-12-03 12:20:11 +09003009 for (p = &table->table[0]; p->chip_label; p++) {
3010 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003011
Alexandre Courbotad824782013-12-03 12:20:11 +09003012 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003013 if (p->idx != idx)
3014 continue;
3015
Alexandre Courbotad824782013-12-03 12:20:11 +09003016 /* If the lookup entry has a con_id, require exact match */
3017 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3018 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003019
Alexandre Courbotad824782013-12-03 12:20:11 +09003020 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003021
Alexandre Courbotad824782013-12-03 12:20:11 +09003022 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003023 dev_err(dev, "cannot find GPIO chip %s\n",
3024 p->chip_label);
3025 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003026 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003027
Alexandre Courbotad824782013-12-03 12:20:11 +09003028 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003029 dev_err(dev,
3030 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3031 idx, chip->ngpio, chip->label);
3032 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003033 }
3034
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003035 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003036 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003037
3038 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003039 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003040
3041 return desc;
3042}
3043
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003044static int dt_gpio_count(struct device *dev, const char *con_id)
3045{
3046 int ret;
3047 char propname[32];
3048 unsigned int i;
3049
3050 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3051 if (con_id)
3052 snprintf(propname, sizeof(propname), "%s-%s",
3053 con_id, gpio_suffixes[i]);
3054 else
3055 snprintf(propname, sizeof(propname), "%s",
3056 gpio_suffixes[i]);
3057
3058 ret = of_gpio_named_count(dev->of_node, propname);
3059 if (ret >= 0)
3060 break;
3061 }
3062 return ret;
3063}
3064
3065static int platform_gpio_count(struct device *dev, const char *con_id)
3066{
3067 struct gpiod_lookup_table *table;
3068 struct gpiod_lookup *p;
3069 unsigned int count = 0;
3070
3071 table = gpiod_find_lookup_table(dev);
3072 if (!table)
3073 return -ENOENT;
3074
3075 for (p = &table->table[0]; p->chip_label; p++) {
3076 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3077 (!con_id && !p->con_id))
3078 count++;
3079 }
3080 if (!count)
3081 return -ENOENT;
3082
3083 return count;
3084}
3085
3086/**
3087 * gpiod_count - return the number of GPIOs associated with a device / function
3088 * or -ENOENT if no GPIO has been assigned to the requested function
3089 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3090 * @con_id: function within the GPIO consumer
3091 */
3092int gpiod_count(struct device *dev, const char *con_id)
3093{
3094 int count = -ENOENT;
3095
3096 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3097 count = dt_gpio_count(dev, con_id);
3098 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3099 count = acpi_gpio_count(dev, con_id);
3100
3101 if (count < 0)
3102 count = platform_gpio_count(dev, con_id);
3103
3104 return count;
3105}
3106EXPORT_SYMBOL_GPL(gpiod_count);
3107
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003108/**
Thierry Reding08791622014-04-25 16:54:22 +02003109 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003110 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003111 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003112 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003113 *
3114 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003115 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003116 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003117 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003118struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003119 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003120{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003121 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003122}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003123EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003124
3125/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003126 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3127 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3128 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003129 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003130 *
3131 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3132 * the requested function it will return NULL. This is convenient for drivers
3133 * that need to handle optional GPIOs.
3134 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003135struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003136 const char *con_id,
3137 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003138{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003139 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003140}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003141EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003142
Benoit Parrotf625d462015-02-02 11:44:44 -06003143
3144/**
3145 * gpiod_configure_flags - helper function to configure a given GPIO
3146 * @desc: gpio whose value will be assigned
3147 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003148 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3149 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003150 * @dflags: gpiod_flags - optional GPIO initialization flags
3151 *
3152 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3153 * requested function and/or index, or another IS_ERR() code if an error
3154 * occurred while trying to acquire the GPIO.
3155 */
3156static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003157 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003158{
3159 int status;
3160
Johan Hovold85b03b32016-07-03 18:32:05 +02003161 if (lflags & GPIO_ACTIVE_LOW)
3162 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3163 if (lflags & GPIO_OPEN_DRAIN)
3164 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3165 if (lflags & GPIO_OPEN_SOURCE)
3166 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3167
Benoit Parrotf625d462015-02-02 11:44:44 -06003168 /* No particular flag request, return here... */
3169 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3170 pr_debug("no flags found for %s\n", con_id);
3171 return 0;
3172 }
3173
3174 /* Process flags */
3175 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3176 status = gpiod_direction_output(desc,
3177 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
3178 else
3179 status = gpiod_direction_input(desc);
3180
3181 return status;
3182}
3183
Thierry Reding29a1f2332014-04-25 17:10:06 +02003184/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003185 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003186 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003187 * @con_id: function within the GPIO consumer
3188 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003189 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003190 *
3191 * This variant of gpiod_get() allows to access GPIOs other than the first
3192 * defined one for functions that define several GPIOs.
3193 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003194 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3195 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003196 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003197 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003198struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003199 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003200 unsigned int idx,
3201 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003202{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003203 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003204 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003205 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003206
3207 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3208
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003209 if (dev) {
3210 /* Using device tree? */
3211 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3212 dev_dbg(dev, "using device tree for GPIO lookup\n");
3213 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3214 } else if (ACPI_COMPANION(dev)) {
3215 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Dmitry Torokhov25487532016-03-24 10:50:25 -07003216 desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003217 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003218 }
3219
3220 /*
3221 * Either we are not using DT or ACPI, or their lookup did not return
3222 * a result. In that case, use platform lookup as a fallback.
3223 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003224 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003225 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003226 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003227 }
3228
3229 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003230 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003231 return desc;
3232 }
3233
Linus Walleijc5aa51c2018-01-04 22:31:11 +01003234 /* If a connection label was passed use that, else use the device name as label */
3235 status = gpiod_request(desc, con_id ? con_id : dev_name(dev));
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003236 if (status < 0)
3237 return ERR_PTR(status);
3238
Johan Hovold85b03b32016-07-03 18:32:05 +02003239 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003240 if (status < 0) {
3241 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3242 gpiod_put(desc);
3243 return ERR_PTR(status);
3244 }
3245
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003246 return desc;
3247}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003248EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003249
3250/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003251 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3252 * @fwnode: handle of the firmware node
3253 * @propname: name of the firmware property representing the GPIO
3254 *
3255 * This function can be used for drivers that get their configuration
3256 * from firmware.
3257 *
3258 * Function properly finds the corresponding GPIO using whatever is the
3259 * underlying firmware interface and then makes sure that the GPIO
3260 * descriptor is requested before it is returned to the caller.
3261 *
3262 * In case of error an ERR_PTR() is returned.
3263 */
3264struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3265 const char *propname)
3266{
3267 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3268 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003269 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003270 int ret;
3271
3272 if (!fwnode)
3273 return ERR_PTR(-EINVAL);
3274
3275 if (is_of_node(fwnode)) {
3276 enum of_gpio_flags flags;
3277
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02003278 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02003279 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003280 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003281 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003282 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3283 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003284 } else if (is_acpi_node(fwnode)) {
3285 struct acpi_gpio_info info;
3286
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02003287 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02003288 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01003289 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003290 }
3291
3292 if (IS_ERR(desc))
3293 return desc;
3294
Johan Hovold85b03b32016-07-03 18:32:05 +02003295 ret = gpiod_request(desc, NULL);
3296 if (ret)
3297 return ERR_PTR(ret);
3298
Mika Westerberg40b73182014-10-21 13:33:59 +02003299 if (active_low)
3300 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3301
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003302 if (single_ended) {
3303 if (active_low)
3304 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3305 else
3306 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3307 }
3308
Mika Westerberg40b73182014-10-21 13:33:59 +02003309 return desc;
3310}
3311EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3312
3313/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003314 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3315 * function
3316 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3317 * @con_id: function within the GPIO consumer
3318 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003319 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003320 *
3321 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3322 * specified index was assigned to the requested function it will return NULL.
3323 * This is convenient for drivers that need to handle optional GPIOs.
3324 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003325struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003326 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003327 unsigned int index,
3328 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003329{
3330 struct gpio_desc *desc;
3331
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003332 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003333 if (IS_ERR(desc)) {
3334 if (PTR_ERR(desc) == -ENOENT)
3335 return NULL;
3336 }
3337
3338 return desc;
3339}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003340EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003341
3342/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003343 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3344 * @desc: gpio whose value will be assigned
3345 * @name: gpio line name
3346 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3347 * of_get_gpio_hog()
3348 * @dflags: gpiod_flags - optional GPIO initialization flags
3349 */
3350int gpiod_hog(struct gpio_desc *desc, const char *name,
3351 unsigned long lflags, enum gpiod_flags dflags)
3352{
3353 struct gpio_chip *chip;
3354 struct gpio_desc *local_desc;
3355 int hwnum;
3356 int status;
3357
3358 chip = gpiod_to_chip(desc);
3359 hwnum = gpio_chip_hwgpio(desc);
3360
3361 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3362 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303363 status = PTR_ERR(local_desc);
3364 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3365 name, chip->label, hwnum, status);
3366 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003367 }
3368
Johan Hovold85b03b32016-07-03 18:32:05 +02003369 status = gpiod_configure_flags(desc, name, lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003370 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303371 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3372 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003373 gpiochip_free_own_desc(desc);
3374 return status;
3375 }
3376
3377 /* Mark GPIO as hogged so it can be identified and removed later */
3378 set_bit(FLAG_IS_HOGGED, &desc->flags);
3379
3380 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3381 desc_to_gpio(desc), name,
3382 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3383 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3384 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3385
3386 return 0;
3387}
3388
3389/**
3390 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3391 * @chip: gpio chip to act on
3392 *
3393 * This is only used by of_gpiochip_remove to free hogged gpios
3394 */
3395static void gpiochip_free_hogs(struct gpio_chip *chip)
3396{
3397 int id;
3398
3399 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003400 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3401 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003402 }
3403}
3404
3405/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003406 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3407 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3408 * @con_id: function within the GPIO consumer
3409 * @flags: optional GPIO initialization flags
3410 *
3411 * This function acquires all the GPIOs defined under a given function.
3412 *
3413 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3414 * no GPIO has been assigned to the requested function, or another IS_ERR()
3415 * code if an error occurred while trying to acquire the GPIOs.
3416 */
3417struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3418 const char *con_id,
3419 enum gpiod_flags flags)
3420{
3421 struct gpio_desc *desc;
3422 struct gpio_descs *descs;
3423 int count;
3424
3425 count = gpiod_count(dev, con_id);
3426 if (count < 0)
3427 return ERR_PTR(count);
3428
3429 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3430 GFP_KERNEL);
3431 if (!descs)
3432 return ERR_PTR(-ENOMEM);
3433
3434 for (descs->ndescs = 0; descs->ndescs < count; ) {
3435 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3436 if (IS_ERR(desc)) {
3437 gpiod_put_array(descs);
3438 return ERR_CAST(desc);
3439 }
3440 descs->desc[descs->ndescs] = desc;
3441 descs->ndescs++;
3442 }
3443 return descs;
3444}
3445EXPORT_SYMBOL_GPL(gpiod_get_array);
3446
3447/**
3448 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3449 * function
3450 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3451 * @con_id: function within the GPIO consumer
3452 * @flags: optional GPIO initialization flags
3453 *
3454 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3455 * assigned to the requested function it will return NULL.
3456 */
3457struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3458 const char *con_id,
3459 enum gpiod_flags flags)
3460{
3461 struct gpio_descs *descs;
3462
3463 descs = gpiod_get_array(dev, con_id, flags);
3464 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3465 return NULL;
3466
3467 return descs;
3468}
3469EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3470
3471/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003472 * gpiod_put - dispose of a GPIO descriptor
3473 * @desc: GPIO descriptor to dispose of
3474 *
3475 * No descriptor can be used after gpiod_put() has been called on it.
3476 */
3477void gpiod_put(struct gpio_desc *desc)
3478{
3479 gpiod_free(desc);
3480}
3481EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003482
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003483/**
3484 * gpiod_put_array - dispose of multiple GPIO descriptors
3485 * @descs: struct gpio_descs containing an array of descriptors
3486 */
3487void gpiod_put_array(struct gpio_descs *descs)
3488{
3489 unsigned int i;
3490
3491 for (i = 0; i < descs->ndescs; i++)
3492 gpiod_put(descs->desc[i]);
3493
3494 kfree(descs);
3495}
3496EXPORT_SYMBOL_GPL(gpiod_put_array);
3497
Linus Walleij3c702e92015-10-21 15:29:53 +02003498static int __init gpiolib_dev_init(void)
3499{
3500 int ret;
3501
3502 /* Register GPIO sysfs bus */
3503 ret = bus_register(&gpio_bus_type);
3504 if (ret < 0) {
3505 pr_err("gpiolib: could not register GPIO bus type\n");
3506 return ret;
3507 }
3508
3509 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3510 if (ret < 0) {
3511 pr_err("gpiolib: failed to allocate char dev region\n");
3512 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003513 } else {
3514 gpiolib_initialized = true;
3515 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003516 }
3517 return ret;
3518}
3519core_initcall(gpiolib_dev_init);
3520
David Brownelld2876d02008-02-04 22:28:20 -08003521#ifdef CONFIG_DEBUG_FS
3522
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003523static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003524{
3525 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003526 struct gpio_chip *chip = gdev->chip;
3527 unsigned gpio = gdev->base;
3528 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003529 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003530 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003531
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003532 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003533 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3534 if (gdesc->name) {
3535 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3536 gpio, gdesc->name);
3537 }
David Brownelld2876d02008-02-04 22:28:20 -08003538 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003539 }
David Brownelld2876d02008-02-04 22:28:20 -08003540
Alexandre Courbot372e7222013-02-03 01:29:29 +09003541 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003542 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003543 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003544 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3545 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003546 is_out ? "out" : "in ",
3547 chip->get
3548 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003549 : "? ",
3550 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003551 seq_printf(s, "\n");
3552 }
3553}
3554
Thierry Redingf9c4a312012-04-12 13:26:01 +02003555static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003556{
Grant Likely362432a2013-02-09 09:41:49 +00003557 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003558 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003559 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003560
3561 s->private = "";
3562
Grant Likely362432a2013-02-09 09:41:49 +00003563 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003564 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003565 if (index-- == 0) {
3566 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003567 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003568 }
3569 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003570
3571 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003572}
3573
3574static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3575{
Grant Likely362432a2013-02-09 09:41:49 +00003576 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003577 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003578 void *ret = NULL;
3579
Grant Likely362432a2013-02-09 09:41:49 +00003580 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003581 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003582 ret = NULL;
3583 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003584 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003585 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003586
3587 s->private = "\n";
3588 ++*pos;
3589
3590 return ret;
3591}
3592
3593static void gpiolib_seq_stop(struct seq_file *s, void *v)
3594{
3595}
3596
3597static int gpiolib_seq_show(struct seq_file *s, void *v)
3598{
Linus Walleijff2b1352015-10-20 11:10:38 +02003599 struct gpio_device *gdev = v;
3600 struct gpio_chip *chip = gdev->chip;
3601 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003602
Linus Walleijff2b1352015-10-20 11:10:38 +02003603 if (!chip) {
3604 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3605 dev_name(&gdev->dev));
3606 return 0;
3607 }
3608
3609 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3610 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003611 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02003612 parent = chip->parent;
3613 if (parent)
3614 seq_printf(s, ", parent: %s/%s",
3615 parent->bus ? parent->bus->name : "no-bus",
3616 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02003617 if (chip->label)
3618 seq_printf(s, ", %s", chip->label);
3619 if (chip->can_sleep)
3620 seq_printf(s, ", can sleep");
3621 seq_printf(s, ":\n");
3622
3623 if (chip->dbg_show)
3624 chip->dbg_show(s, chip);
3625 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003626 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003627
David Brownelld2876d02008-02-04 22:28:20 -08003628 return 0;
3629}
3630
Thierry Redingf9c4a312012-04-12 13:26:01 +02003631static const struct seq_operations gpiolib_seq_ops = {
3632 .start = gpiolib_seq_start,
3633 .next = gpiolib_seq_next,
3634 .stop = gpiolib_seq_stop,
3635 .show = gpiolib_seq_show,
3636};
3637
David Brownelld2876d02008-02-04 22:28:20 -08003638static int gpiolib_open(struct inode *inode, struct file *file)
3639{
Thierry Redingf9c4a312012-04-12 13:26:01 +02003640 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08003641}
3642
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003643static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02003644 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08003645 .open = gpiolib_open,
3646 .read = seq_read,
3647 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02003648 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08003649};
3650
3651static int __init gpiolib_debugfs_init(void)
3652{
3653 /* /sys/kernel/debug/gpio */
3654 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3655 NULL, NULL, &gpiolib_operations);
3656 return 0;
3657}
3658subsys_initcall(gpiolib_debugfs_init);
3659
3660#endif /* DEBUG_FS */