blob: 9e2fe12c28584c0bcb2acacd6cf2386fba77e830 [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;
Timur Tabi31d04ca2018-03-29 13:29:12 -0500428 int fd, i, count = 0, ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200429
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;
Ricardo Ribalda Delgadoe88ca672018-09-13 15:37:04 +0200474 count = i + 1;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200475
476 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
477 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
478 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
479 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
480 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
481 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
482
483 /*
484 * Lines have to be requested explicitly for input
485 * or output, else the line will be treated "as is".
486 */
487 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
488 int val = !!handlereq.default_values[i];
489
490 ret = gpiod_direction_output(desc, val);
491 if (ret)
492 goto out_free_descs;
493 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
494 ret = gpiod_direction_input(desc);
495 if (ret)
496 goto out_free_descs;
497 }
498 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
499 offset);
500 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200501 /* Let i point at the last handle */
502 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200503 lh->numdescs = handlereq.lines;
504
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200505 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200506 if (fd < 0) {
507 ret = fd;
508 goto out_free_descs;
509 }
510
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200511 file = anon_inode_getfile("gpio-linehandle",
512 &linehandle_fileops,
513 lh,
514 O_RDONLY | O_CLOEXEC);
515 if (IS_ERR(file)) {
516 ret = PTR_ERR(file);
517 goto out_put_unused_fd;
518 }
519
Linus Walleijd7c51b42016-04-26 10:35:29 +0200520 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200521 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200522 /*
523 * fput() will trigger the release() callback, so do not go onto
524 * the regular error cleanup path here.
525 */
526 fput(file);
527 put_unused_fd(fd);
528 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200529 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200530
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200531 fd_install(fd, file);
532
Linus Walleijd7c51b42016-04-26 10:35:29 +0200533 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
534 lh->numdescs);
535
536 return 0;
537
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200538out_put_unused_fd:
539 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200540out_free_descs:
Timur Tabi31d04ca2018-03-29 13:29:12 -0500541 for (i = 0; i < count; i++)
Linus Walleijd7c51b42016-04-26 10:35:29 +0200542 gpiod_free(lh->descs[i]);
543 kfree(lh->label);
544out_free_lh:
545 kfree(lh);
546 put_device(&gdev->dev);
547 return ret;
548}
549
Linus Walleij61f922d2016-06-02 11:30:15 +0200550/*
551 * GPIO line event management
552 */
553
554/**
555 * struct lineevent_state - contains the state of a userspace event
556 * @gdev: the GPIO device the event pertains to
557 * @label: consumer label used to tag descriptors
558 * @desc: the GPIO descriptor held by this event
559 * @eflags: the event flags this line was requested with
560 * @irq: the interrupt that trigger in response to events on this GPIO
561 * @wait: wait queue that handles blocking reads of events
562 * @events: KFIFO for the GPIO events
563 * @read_lock: mutex lock to protect reads from colliding with adding
564 * new events to the FIFO
565 */
566struct lineevent_state {
567 struct gpio_device *gdev;
568 const char *label;
569 struct gpio_desc *desc;
570 u32 eflags;
571 int irq;
572 wait_queue_head_t wait;
573 DECLARE_KFIFO(events, struct gpioevent_data, 16);
574 struct mutex read_lock;
575};
576
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200577#define GPIOEVENT_REQUEST_VALID_FLAGS \
578 (GPIOEVENT_REQUEST_RISING_EDGE | \
579 GPIOEVENT_REQUEST_FALLING_EDGE)
580
Linus Walleij61f922d2016-06-02 11:30:15 +0200581static unsigned int lineevent_poll(struct file *filep,
582 struct poll_table_struct *wait)
583{
584 struct lineevent_state *le = filep->private_data;
585 unsigned int events = 0;
586
587 poll_wait(filep, &le->wait, wait);
588
589 if (!kfifo_is_empty(&le->events))
590 events = POLLIN | POLLRDNORM;
591
592 return events;
593}
594
595
596static ssize_t lineevent_read(struct file *filep,
597 char __user *buf,
598 size_t count,
599 loff_t *f_ps)
600{
601 struct lineevent_state *le = filep->private_data;
602 unsigned int copied;
603 int ret;
604
605 if (count < sizeof(struct gpioevent_data))
606 return -EINVAL;
607
608 do {
609 if (kfifo_is_empty(&le->events)) {
610 if (filep->f_flags & O_NONBLOCK)
611 return -EAGAIN;
612
613 ret = wait_event_interruptible(le->wait,
614 !kfifo_is_empty(&le->events));
615 if (ret)
616 return ret;
617 }
618
619 if (mutex_lock_interruptible(&le->read_lock))
620 return -ERESTARTSYS;
621 ret = kfifo_to_user(&le->events, buf, count, &copied);
622 mutex_unlock(&le->read_lock);
623
624 if (ret)
625 return ret;
626
627 /*
628 * If we couldn't read anything from the fifo (a different
629 * thread might have been faster) we either return -EAGAIN if
630 * the file descriptor is non-blocking, otherwise we go back to
631 * sleep and wait for more data to arrive.
632 */
633 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
634 return -EAGAIN;
635
636 } while (copied == 0);
637
638 return copied;
639}
640
641static int lineevent_release(struct inode *inode, struct file *filep)
642{
643 struct lineevent_state *le = filep->private_data;
644 struct gpio_device *gdev = le->gdev;
645
646 free_irq(le->irq, le);
647 gpiod_free(le->desc);
648 kfree(le->label);
649 kfree(le);
650 put_device(&gdev->dev);
651 return 0;
652}
653
654static long lineevent_ioctl(struct file *filep, unsigned int cmd,
655 unsigned long arg)
656{
657 struct lineevent_state *le = filep->private_data;
658 void __user *ip = (void __user *)arg;
659 struct gpiohandle_data ghd;
660
661 /*
662 * We can get the value for an event line but not set it,
663 * because it is input by definition.
664 */
665 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
666 int val;
667
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200668 memset(&ghd, 0, sizeof(ghd));
669
Linus Walleij61f922d2016-06-02 11:30:15 +0200670 val = gpiod_get_value_cansleep(le->desc);
671 if (val < 0)
672 return val;
673 ghd.values[0] = val;
674
675 if (copy_to_user(ip, &ghd, sizeof(ghd)))
676 return -EFAULT;
677
678 return 0;
679 }
680 return -EINVAL;
681}
682
683#ifdef CONFIG_COMPAT
684static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
685 unsigned long arg)
686{
687 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
688}
689#endif
690
691static const struct file_operations lineevent_fileops = {
692 .release = lineevent_release,
693 .read = lineevent_read,
694 .poll = lineevent_poll,
695 .owner = THIS_MODULE,
696 .llseek = noop_llseek,
697 .unlocked_ioctl = lineevent_ioctl,
698#ifdef CONFIG_COMPAT
699 .compat_ioctl = lineevent_ioctl_compat,
700#endif
701};
702
Ben Dooks33265b12016-06-17 16:03:13 +0100703static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200704{
705 struct lineevent_state *le = p;
706 struct gpioevent_data ge;
Bartosz Golaszewskib680e222017-07-03 11:12:03 +0200707 int ret, level;
Linus Walleij61f922d2016-06-02 11:30:15 +0200708
Linus Walleijcc1fa4a2018-01-22 13:19:28 +0100709 /* Do not leak kernel stack to userspace */
710 memset(&ge, 0, sizeof(ge));
711
Linus Walleij61f922d2016-06-02 11:30:15 +0200712 ge.timestamp = ktime_get_real_ns();
Bartosz Golaszewskib680e222017-07-03 11:12:03 +0200713 level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200714
Bartosz Golaszewski78c42442017-06-23 13:45:16 +0200715 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
716 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200717 if (level)
718 /* Emit low-to-high event */
719 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
720 else
721 /* Emit high-to-low event */
722 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Bartosz Golaszewskib680e222017-07-03 11:12:03 +0200723 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200724 /* Emit low-to-high event */
725 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Bartosz Golaszewskib680e222017-07-03 11:12:03 +0200726 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200727 /* Emit high-to-low event */
728 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200729 } else {
730 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200731 }
732
733 ret = kfifo_put(&le->events, ge);
734 if (ret != 0)
735 wake_up_poll(&le->wait, POLLIN);
736
737 return IRQ_HANDLED;
738}
739
740static int lineevent_create(struct gpio_device *gdev, void __user *ip)
741{
742 struct gpioevent_request eventreq;
743 struct lineevent_state *le;
744 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200745 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200746 u32 offset;
747 u32 lflags;
748 u32 eflags;
749 int fd;
750 int ret;
751 int irqflags = 0;
752
753 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
754 return -EFAULT;
755
756 le = kzalloc(sizeof(*le), GFP_KERNEL);
757 if (!le)
758 return -ENOMEM;
759 le->gdev = gdev;
760 get_device(&gdev->dev);
761
762 /* Make sure this is terminated */
763 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
764 if (strlen(eventreq.consumer_label)) {
765 le->label = kstrdup(eventreq.consumer_label,
766 GFP_KERNEL);
767 if (!le->label) {
768 ret = -ENOMEM;
769 goto out_free_le;
770 }
771 }
772
773 offset = eventreq.lineoffset;
774 lflags = eventreq.handleflags;
775 eflags = eventreq.eventflags;
776
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200777 if (offset >= gdev->ngpio) {
778 ret = -EINVAL;
779 goto out_free_label;
780 }
781
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200782 /* Return an error if a unknown flag is set */
783 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
784 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
785 ret = -EINVAL;
786 goto out_free_label;
787 }
788
Linus Walleij61f922d2016-06-02 11:30:15 +0200789 /* This is just wrong: we don't look for events on output lines */
790 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
791 ret = -EINVAL;
792 goto out_free_label;
793 }
794
795 desc = &gdev->descs[offset];
796 ret = gpiod_request(desc, le->label);
797 if (ret)
Uwe Kleine-König63e2ae92018-04-16 13:17:53 +0200798 goto out_free_label;
Linus Walleij61f922d2016-06-02 11:30:15 +0200799 le->desc = desc;
800 le->eflags = eflags;
801
802 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
803 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
804 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
805 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
806 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
807 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
808
809 ret = gpiod_direction_input(desc);
810 if (ret)
811 goto out_free_desc;
812
813 le->irq = gpiod_to_irq(desc);
814 if (le->irq <= 0) {
815 ret = -ENODEV;
816 goto out_free_desc;
817 }
818
819 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
820 irqflags |= IRQF_TRIGGER_RISING;
821 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
822 irqflags |= IRQF_TRIGGER_FALLING;
823 irqflags |= IRQF_ONESHOT;
824 irqflags |= IRQF_SHARED;
825
826 INIT_KFIFO(le->events);
827 init_waitqueue_head(&le->wait);
828 mutex_init(&le->read_lock);
829
830 /* Request a thread to read the events */
831 ret = request_threaded_irq(le->irq,
832 NULL,
833 lineevent_irq_thread,
834 irqflags,
835 le->label,
836 le);
837 if (ret)
838 goto out_free_desc;
839
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200840 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200841 if (fd < 0) {
842 ret = fd;
843 goto out_free_irq;
844 }
845
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200846 file = anon_inode_getfile("gpio-event",
847 &lineevent_fileops,
848 le,
849 O_RDONLY | O_CLOEXEC);
850 if (IS_ERR(file)) {
851 ret = PTR_ERR(file);
852 goto out_put_unused_fd;
853 }
854
Linus Walleij61f922d2016-06-02 11:30:15 +0200855 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200856 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200857 /*
858 * fput() will trigger the release() callback, so do not go onto
859 * the regular error cleanup path here.
860 */
861 fput(file);
862 put_unused_fd(fd);
863 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200864 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200865
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200866 fd_install(fd, file);
867
Linus Walleij61f922d2016-06-02 11:30:15 +0200868 return 0;
869
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200870out_put_unused_fd:
871 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +0200872out_free_irq:
873 free_irq(le->irq, le);
874out_free_desc:
875 gpiod_free(le->desc);
876out_free_label:
877 kfree(le->label);
878out_free_le:
879 kfree(le);
880 put_device(&gdev->dev);
881 return ret;
882}
883
Linus Walleij3c702e92015-10-21 15:29:53 +0200884/**
885 * gpio_ioctl() - ioctl handler for the GPIO chardev
886 */
887static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
888{
889 struct gpio_device *gdev = filp->private_data;
890 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200891 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200892
893 /* We fail any subsequent ioctl():s when the chip is gone */
894 if (!chip)
895 return -ENODEV;
896
Linus Walleij521a2ad2016-02-12 22:25:22 +0100897 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200898 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100899 struct gpiochip_info chipinfo;
900
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +0200901 memset(&chipinfo, 0, sizeof(chipinfo));
902
Linus Walleij3c702e92015-10-21 15:29:53 +0200903 strncpy(chipinfo.name, dev_name(&gdev->dev),
904 sizeof(chipinfo.name));
905 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100906 strncpy(chipinfo.label, gdev->label,
907 sizeof(chipinfo.label));
908 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100909 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200910 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
911 return -EFAULT;
912 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100913 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
914 struct gpioline_info lineinfo;
915 struct gpio_desc *desc;
916
917 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
918 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +0200919 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +0100920 return -EINVAL;
921
922 desc = &gdev->descs[lineinfo.line_offset];
923 if (desc->name) {
924 strncpy(lineinfo.name, desc->name,
925 sizeof(lineinfo.name));
926 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
927 } else {
928 lineinfo.name[0] = '\0';
929 }
930 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100931 strncpy(lineinfo.consumer, desc->label,
932 sizeof(lineinfo.consumer));
933 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100934 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100935 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100936 }
937
938 /*
939 * Userspace only need to know that the kernel is using
940 * this GPIO so it can't use it.
941 */
942 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100943 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
944 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
945 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
946 test_bit(FLAG_EXPORT, &desc->flags) ||
947 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100948 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100949 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100950 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100951 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100952 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100953 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100954 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100955 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100956 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
957
958 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
959 return -EFAULT;
960 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200961 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
962 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +0200963 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
964 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +0200965 }
966 return -EINVAL;
967}
968
Linus Walleij8b92e172016-05-27 14:24:04 +0200969#ifdef CONFIG_COMPAT
970static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
971 unsigned long arg)
972{
973 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
974}
975#endif
976
Linus Walleij3c702e92015-10-21 15:29:53 +0200977/**
978 * gpio_chrdev_open() - open the chardev for ioctl operations
979 * @inode: inode for this chardev
980 * @filp: file struct for storing private data
981 * Returns 0 on success
982 */
983static int gpio_chrdev_open(struct inode *inode, struct file *filp)
984{
985 struct gpio_device *gdev = container_of(inode->i_cdev,
986 struct gpio_device, chrdev);
987
988 /* Fail on open if the backing gpiochip is gone */
989 if (!gdev || !gdev->chip)
990 return -ENODEV;
991 get_device(&gdev->dev);
992 filp->private_data = gdev;
Lars-Peter Clausen5f654072016-11-30 13:05:21 +0100993
994 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +0200995}
996
997/**
998 * gpio_chrdev_release() - close chardev after ioctl operations
999 * @inode: inode for this chardev
1000 * @filp: file struct for storing private data
1001 * Returns 0 on success
1002 */
1003static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1004{
1005 struct gpio_device *gdev = container_of(inode->i_cdev,
1006 struct gpio_device, chrdev);
1007
1008 if (!gdev)
1009 return -ENODEV;
1010 put_device(&gdev->dev);
1011 return 0;
1012}
1013
1014
1015static const struct file_operations gpio_fileops = {
1016 .release = gpio_chrdev_release,
1017 .open = gpio_chrdev_open,
1018 .owner = THIS_MODULE,
Lars-Peter Clausen5f654072016-11-30 13:05:21 +01001019 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001020 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001021#ifdef CONFIG_COMPAT
1022 .compat_ioctl = gpio_ioctl_compat,
1023#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001024};
1025
Linus Walleijff2b1352015-10-20 11:10:38 +02001026static void gpiodevice_release(struct device *dev)
1027{
1028 struct gpio_device *gdev = dev_get_drvdata(dev);
1029
1030 list_del(&gdev->list);
1031 ida_simple_remove(&gpio_ida, gdev->id);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001032 kfree(gdev->label);
1033 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001034 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001035}
1036
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001037static int gpiochip_setup_dev(struct gpio_device *gdev)
1038{
1039 int status;
1040
1041 cdev_init(&gdev->chrdev, &gpio_fileops);
1042 gdev->chrdev.owner = THIS_MODULE;
1043 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
1044 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
1045 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
1046 if (status < 0)
1047 chip_warn(gdev->chip, "failed to add char device %d:%d\n",
1048 MAJOR(gpio_devt), gdev->id);
1049 else
1050 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1051 MAJOR(gpio_devt), gdev->id);
1052 status = device_add(&gdev->dev);
1053 if (status)
1054 goto err_remove_chardev;
1055
1056 status = gpiochip_sysfs_register(gdev);
1057 if (status)
1058 goto err_remove_device;
1059
1060 /* From this point, the .release() function cleans up gpio_device */
1061 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001062 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1063 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1064 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1065
1066 return 0;
1067
1068err_remove_device:
1069 device_del(&gdev->dev);
1070err_remove_chardev:
1071 cdev_del(&gdev->chrdev);
1072 return status;
1073}
1074
1075static void gpiochip_setup_devs(void)
1076{
1077 struct gpio_device *gdev;
1078 int err;
1079
1080 list_for_each_entry(gdev, &gpio_devices, list) {
1081 err = gpiochip_setup_dev(gdev);
1082 if (err)
1083 pr_err("%s: Failed to initialize gpio device (%d)\n",
1084 dev_name(&gdev->dev), err);
1085 }
1086}
1087
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001088/**
Linus Walleijb08ea352015-12-03 15:14:13 +01001089 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -08001090 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001091 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -08001092 *
1093 * Returns a negative errno if the chip can't be registered, such as
1094 * because the chip->base is invalid or already associated with a
1095 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001096 *
Linus Walleijb08ea352015-12-03 15:14:13 +01001097 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001098 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d2008-07-25 01:46:07 -07001099 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1100 * for GPIOs will fail rudely.
1101 *
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001102 * gpiochip_add_data() must only be called after gpiolib initialization,
1103 * ie after core_initcall().
1104 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001105 * If chip->base is negative, this requests dynamic assignment of
1106 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001107 */
Linus Walleijb08ea352015-12-03 15:14:13 +01001108int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -08001109{
1110 unsigned long flags;
1111 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001112 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001113 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001114 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001115
Linus Walleijff2b1352015-10-20 11:10:38 +02001116 /*
1117 * First: allocate and populate the internal stat container, and
1118 * set up the struct device.
1119 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001120 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001121 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001122 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001123 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001124 gdev->chip = chip;
1125 chip->gpiodev = gdev;
1126 if (chip->parent) {
1127 gdev->dev.parent = chip->parent;
1128 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001129 }
1130
Linus Walleijff2b1352015-10-20 11:10:38 +02001131#ifdef CONFIG_OF_GPIO
1132 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001133 if (chip->of_node)
1134 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001135#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001136
Linus Walleijff2b1352015-10-20 11:10:38 +02001137 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1138 if (gdev->id < 0) {
1139 status = gdev->id;
1140 goto err_free_gdev;
1141 }
1142 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1143 device_initialize(&gdev->dev);
1144 dev_set_drvdata(&gdev->dev, gdev);
1145 if (chip->parent && chip->parent->driver)
1146 gdev->owner = chip->parent->driver->owner;
1147 else if (chip->owner)
1148 /* TODO: remove chip->owner */
1149 gdev->owner = chip->owner;
1150 else
1151 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001152
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001153 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001154 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001155 status = -ENOMEM;
Vladimir Zapolskiy2e668d72018-11-02 15:39:43 +02001156 goto err_free_ida;
Linus Walleijff2b1352015-10-20 11:10:38 +02001157 }
1158
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001159 if (chip->ngpio == 0) {
1160 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001161 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001162 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001163 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001164
1165 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001166 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001167 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001168 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001169 if (!gdev->label) {
1170 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001171 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001172 }
1173
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001174 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001175 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001176
David Brownelld2876d02008-02-04 22:28:20 -08001177 spin_lock_irqsave(&gpio_lock, flags);
1178
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001179 /*
1180 * TODO: this allocates a Linux GPIO number base in the global
1181 * GPIO numberspace for this chip. In the long run we want to
1182 * get *rid* of this numberspace and use only descriptors, but
1183 * it may be a pipe dream. It will not happen before we get rid
1184 * of the sysfs interface anyways.
1185 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001186 if (base < 0) {
1187 base = gpiochip_find_base(chip->ngpio);
1188 if (base < 0) {
1189 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001190 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001191 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001192 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001193 /*
1194 * TODO: it should not be necessary to reflect the assigned
1195 * base outside of the GPIO subsystem. Go over drivers and
1196 * see if anyone makes use of this, else drop this and assign
1197 * a poison instead.
1198 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001199 chip->base = base;
1200 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001201 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001202
Linus Walleijff2b1352015-10-20 11:10:38 +02001203 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001204 if (status) {
1205 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001206 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001207 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001208
Linus Walleij545ebd92016-05-30 17:11:59 +02001209 spin_unlock_irqrestore(&gpio_lock, flags);
1210
Linus Walleijff2b1352015-10-20 11:10:38 +02001211 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001212 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d2008-07-25 01:46:07 -07001213
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001214 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +02001215 /*
1216 * REVISIT: most hardware initializes GPIOs as inputs
1217 * (often with pullups enabled) so power usage is
1218 * minimized. Linux code should set the gpio direction
1219 * first thing; but until it does, and in case
1220 * chip->get_direction is not set, we may expose the
1221 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001222 */
Linus Walleij72d32002016-04-28 13:33:59 +02001223
1224 if (chip->get_direction) {
1225 /*
1226 * If we have .get_direction, set up the initial
1227 * direction flag from the hardware.
1228 */
1229 int dir = chip->get_direction(chip, i);
1230
1231 if (!dir)
1232 set_bit(FLAG_IS_OUT, &desc->flags);
1233 } else if (!chip->direction_input) {
1234 /*
1235 * If the chip lacks the .direction_input callback
1236 * we logically assume all lines are outputs.
1237 */
1238 set_bit(FLAG_IS_OUT, &desc->flags);
1239 }
David Brownelld2876d02008-02-04 22:28:20 -08001240 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001241
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301242#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001243 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301244#endif
1245
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001246 status = gpiochip_set_desc_names(chip);
1247 if (status)
1248 goto err_remove_from_list;
1249
Mika Westerberg79b804c2016-09-20 15:15:21 +03001250 status = gpiochip_irqchip_init_valid_mask(chip);
1251 if (status)
1252 goto err_remove_from_list;
1253
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001254 status = of_gpiochip_add(chip);
1255 if (status)
1256 goto err_remove_chip;
1257
Mika Westerberg664e3e52014-01-08 12:40:54 +02001258 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001259
Linus Walleij3c702e92015-10-21 15:29:53 +02001260 /*
1261 * By first adding the chardev, and then adding the device,
1262 * we get a device node entry in sysfs under
1263 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1264 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001265 * We can do this only if gpiolib has been initialized.
1266 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001267 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001268 if (gpiolib_initialized) {
1269 status = gpiochip_setup_dev(gdev);
1270 if (status)
1271 goto err_remove_chip;
1272 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001273 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001274
Johan Hovold225fce82015-01-12 17:12:25 +01001275err_remove_chip:
1276 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001277 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001278 of_gpiochip_remove(chip);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001279 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001280err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001281 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001282 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001283 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001284err_free_label:
1285 kfree(gdev->label);
1286err_free_descs:
1287 kfree(gdev->descs);
Vladimir Zapolskiy2e668d72018-11-02 15:39:43 +02001288err_free_ida:
Linus Walleijff2b1352015-10-20 11:10:38 +02001289 ida_simple_remove(&gpio_ida, gdev->id);
Vladimir Zapolskiy2e668d72018-11-02 15:39:43 +02001290err_free_gdev:
David Brownelld2876d02008-02-04 22:28:20 -08001291 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001292 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001293 gdev->base, gdev->base + gdev->ngpio - 1,
1294 chip->label ? : "generic");
1295 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001296 return status;
1297}
Linus Walleijb08ea352015-12-03 15:14:13 +01001298EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -08001299
1300/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001301 * gpiochip_get_data() - get per-subdriver data for the chip
1302 */
1303void *gpiochip_get_data(struct gpio_chip *chip)
1304{
1305 return chip->gpiodev->data;
1306}
1307EXPORT_SYMBOL_GPL(gpiochip_get_data);
1308
1309/**
David Brownelld2876d02008-02-04 22:28:20 -08001310 * gpiochip_remove() - unregister a gpio_chip
1311 * @chip: the chip to unregister
1312 *
1313 * A gpio_chip with any GPIOs still requested may not be removed.
1314 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001315void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001316{
Linus Walleijff2b1352015-10-20 11:10:38 +02001317 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001318 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001319 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001320 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001321 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001322
Linus Walleijff2b1352015-10-20 11:10:38 +02001323 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001324 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven86673e92016-12-19 18:29:23 +01001325 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001326 /* Numb the device, cancelling all outstanding operations */
1327 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001328 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001329 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001330 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001331 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001332 /*
1333 * We accept no more calls into the driver from this point, so
1334 * NULL the driver data pointer
1335 */
1336 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001337
Johan Hovold6798aca2015-01-12 17:12:28 +01001338 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001339 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001340 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001341 if (test_bit(FLAG_REQUESTED, &desc->flags))
1342 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001343 }
David Brownelld2876d02008-02-04 22:28:20 -08001344 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001345
Johan Hovoldfab28b82015-05-04 17:10:27 +02001346 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001347 dev_crit(&gdev->dev,
Linus Walleij58383c72015-11-04 09:56:26 +01001348 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001349
Linus Walleijff2b1352015-10-20 11:10:38 +02001350 /*
1351 * The gpiochip side puts its use of the device to rest here:
1352 * if there are no userspace clients, the chardev and device will
1353 * be removed, else it will be dangling until the last user is
1354 * gone.
1355 */
Ricardo Ribalda Delgadof4833b82016-06-03 19:10:02 +02001356 cdev_del(&gdev->chrdev);
1357 device_del(&gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001358 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001359}
1360EXPORT_SYMBOL_GPL(gpiochip_remove);
1361
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301362static void devm_gpio_chip_release(struct device *dev, void *res)
1363{
1364 struct gpio_chip *chip = *(struct gpio_chip **)res;
1365
1366 gpiochip_remove(chip);
1367}
1368
1369static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1370
1371{
1372 struct gpio_chip **r = res;
1373
1374 if (!r || !*r) {
1375 WARN_ON(!r || !*r);
1376 return 0;
1377 }
1378
1379 return *r == data;
1380}
1381
1382/**
1383 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1384 * @dev: the device pointer on which irq_chip belongs to.
1385 * @chip: the chip to register, with chip->base initialized
1386 * Context: potentially before irqs will work
1387 *
1388 * Returns a negative errno if the chip can't be registered, such as
1389 * because the chip->base is invalid or already associated with a
1390 * different chip. Otherwise it returns zero as a success code.
1391 *
1392 * The gpio chip automatically be released when the device is unbound.
1393 */
1394int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1395 void *data)
1396{
1397 struct gpio_chip **ptr;
1398 int ret;
1399
1400 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1401 GFP_KERNEL);
1402 if (!ptr)
1403 return -ENOMEM;
1404
1405 ret = gpiochip_add_data(chip, data);
1406 if (ret < 0) {
1407 devres_free(ptr);
1408 return ret;
1409 }
1410
1411 *ptr = chip;
1412 devres_add(dev, ptr);
1413
1414 return 0;
1415}
1416EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1417
1418/**
1419 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1420 * @dev: device for which which resource was allocated
1421 * @chip: the chip to remove
1422 *
1423 * A gpio_chip with any GPIOs still requested may not be removed.
1424 */
1425void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1426{
1427 int ret;
1428
1429 ret = devres_release(dev, devm_gpio_chip_release,
1430 devm_gpio_chip_match, chip);
1431 if (!ret)
1432 WARN_ON(ret);
1433}
1434EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1435
Grant Likely594fa262010-06-08 07:48:16 -06001436/**
1437 * gpiochip_find() - iterator for locating a specific gpio_chip
1438 * @data: data to pass to match function
1439 * @callback: Callback function to check gpio_chip
1440 *
1441 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1442 * determined by a user supplied @match callback. The callback should return
1443 * 0 if the device doesn't match and non-zero if it does. If the callback is
1444 * non-zero, this function will return to the caller and not iterate over any
1445 * more gpio_chips.
1446 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001447struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001448 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001449 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001450{
Linus Walleijff2b1352015-10-20 11:10:38 +02001451 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001452 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001453 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001454
1455 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001456 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001457 if (gdev->chip && match(gdev->chip, data)) {
1458 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001459 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001460 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001461
Grant Likely594fa262010-06-08 07:48:16 -06001462 spin_unlock_irqrestore(&gpio_lock, flags);
1463
1464 return chip;
1465}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001466EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001467
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001468static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1469{
1470 const char *name = data;
1471
1472 return !strcmp(chip->label, name);
1473}
1474
1475static struct gpio_chip *find_chip_by_name(const char *name)
1476{
1477 return gpiochip_find((void *)name, gpiochip_match_name);
1478}
1479
Linus Walleij14250522014-03-25 10:40:18 +01001480#ifdef CONFIG_GPIOLIB_IRQCHIP
1481
1482/*
1483 * The following is irqchip helper code for gpiochips.
1484 */
1485
Mika Westerberg79b804c2016-09-20 15:15:21 +03001486static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1487{
1488 int i;
1489
1490 if (!gpiochip->irq_need_valid_mask)
1491 return 0;
1492
1493 gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
1494 sizeof(long), GFP_KERNEL);
1495 if (!gpiochip->irq_valid_mask)
1496 return -ENOMEM;
1497
1498 /* Assume by default all GPIOs are valid */
1499 for (i = 0; i < gpiochip->ngpio; i++)
1500 set_bit(i, gpiochip->irq_valid_mask);
1501
1502 return 0;
1503}
1504
1505static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1506{
1507 kfree(gpiochip->irq_valid_mask);
1508 gpiochip->irq_valid_mask = NULL;
1509}
1510
1511static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1512 unsigned int offset)
1513{
1514 /* No mask means all valid */
1515 if (likely(!gpiochip->irq_valid_mask))
1516 return true;
1517 return test_bit(offset, gpiochip->irq_valid_mask);
1518}
1519
Linus Walleij14250522014-03-25 10:40:18 +01001520/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001521 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
1522 * @gpiochip: the gpiochip to set the irqchip chain to
1523 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001524 * @parent_irq: the irq number corresponding to the parent IRQ for this
1525 * chained irqchip
1526 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001527 * coming out of the gpiochip. If the interrupt is nested rather than
1528 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001529 */
1530void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1531 struct irq_chip *irqchip,
1532 int parent_irq,
1533 irq_flow_handler_t parent_handler)
1534{
Linus Walleij83141a72014-09-26 13:50:12 +02001535 unsigned int offset;
1536
Linus Walleij83141a72014-09-26 13:50:12 +02001537 if (!gpiochip->irqdomain) {
1538 chip_err(gpiochip, "called %s before setting up irqchip\n",
1539 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001540 return;
1541 }
1542
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001543 if (parent_handler) {
1544 if (gpiochip->can_sleep) {
1545 chip_err(gpiochip,
1546 "you cannot have chained interrupts on a "
1547 "chip that may sleep\n");
1548 return;
1549 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001550 /*
1551 * The parent irqchip is already using the chip_data for this
1552 * irqchip, so our callbacks simply use the handler_data.
1553 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001554 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1555 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001556
1557 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001558 }
Linus Walleij83141a72014-09-26 13:50:12 +02001559
1560 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001561 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1562 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1563 continue;
Linus Walleij83141a72014-09-26 13:50:12 +02001564 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
1565 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001566 }
Linus Walleij14250522014-03-25 10:40:18 +01001567}
1568EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1569
1570/**
1571 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1572 * @d: the irqdomain used by this irqchip
1573 * @irq: the global irq number used by this GPIO irqchip irq
1574 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1575 *
1576 * This function will set up the mapping for a certain IRQ line on a
1577 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1578 * stored inside the gpiochip.
1579 */
1580static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1581 irq_hw_number_t hwirq)
1582{
1583 struct gpio_chip *chip = d->host_data;
1584
Linus Walleij14250522014-03-25 10:40:18 +01001585 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001586 /*
1587 * This lock class tells lockdep that GPIO irqs are in a different
1588 * category than their parents, so it won't report false recursion.
1589 */
1590 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +02001591 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001592 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +03001593 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001594 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001595 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001596
Linus Walleij1333b902014-04-23 16:45:12 +02001597 /*
1598 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1599 * is passed as default type.
1600 */
1601 if (chip->irq_default_type != IRQ_TYPE_NONE)
1602 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001603
1604 return 0;
1605}
1606
Linus Walleijc3626fd2014-03-28 20:42:01 +01001607static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1608{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001609 struct gpio_chip *chip = d->host_data;
1610
Linus Walleij1c8732b2014-04-09 13:34:39 +02001611 if (chip->can_sleep)
1612 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001613 irq_set_chip_and_handler(irq, NULL, NULL);
1614 irq_set_chip_data(irq, NULL);
1615}
1616
Linus Walleij14250522014-03-25 10:40:18 +01001617static const struct irq_domain_ops gpiochip_domain_ops = {
1618 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001619 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001620 /* Virtually all GPIO irqchips are twocell:ed */
1621 .xlate = irq_domain_xlate_twocell,
1622};
1623
1624static int gpiochip_irq_reqres(struct irq_data *d)
1625{
1626 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1627
Linus Walleijff2b1352015-10-20 11:10:38 +02001628 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001629 return -ENODEV;
1630
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001631 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001632 chip_err(chip,
1633 "unable to lock HW IRQ %lu for IRQ\n",
1634 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001635 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001636 return -EINVAL;
1637 }
1638 return 0;
1639}
1640
1641static void gpiochip_irq_relres(struct irq_data *d)
1642{
1643 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1644
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001645 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001646 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001647}
1648
1649static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1650{
1651 return irq_find_mapping(chip->irqdomain, offset);
1652}
1653
1654/**
1655 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1656 * @gpiochip: the gpiochip to remove the irqchip from
1657 *
1658 * This is called only from gpiochip_remove()
1659 */
1660static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1661{
Linus Walleijc3626fd2014-03-28 20:42:01 +01001662 unsigned int offset;
1663
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001664 acpi_gpiochip_free_interrupts(gpiochip);
1665
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001666 if (gpiochip->irq_parent) {
1667 irq_set_chained_handler(gpiochip->irq_parent, NULL);
1668 irq_set_handler_data(gpiochip->irq_parent, NULL);
1669 }
1670
Linus Walleijc3626fd2014-03-28 20:42:01 +01001671 /* Remove all IRQ mappings and delete the domain */
1672 if (gpiochip->irqdomain) {
Mika Westerberg79b804c2016-09-20 15:15:21 +03001673 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1674 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1675 continue;
Grygorii Strashkoe3893382014-09-25 19:09:23 +03001676 irq_dispose_mapping(
1677 irq_find_mapping(gpiochip->irqdomain, offset));
Mika Westerberg79b804c2016-09-20 15:15:21 +03001678 }
Linus Walleij14250522014-03-25 10:40:18 +01001679 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001680 }
Linus Walleij14250522014-03-25 10:40:18 +01001681
1682 if (gpiochip->irqchip) {
1683 gpiochip->irqchip->irq_request_resources = NULL;
1684 gpiochip->irqchip->irq_release_resources = NULL;
1685 gpiochip->irqchip = NULL;
1686 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001687
1688 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001689}
1690
1691/**
1692 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
1693 * @gpiochip: the gpiochip to add the irqchip to
1694 * @irqchip: the irqchip to add to the gpiochip
1695 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1696 * allocate gpiochip irqs from
1697 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001698 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1699 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001700 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001701 *
1702 * This function closely associates a certain irqchip with a certain
1703 * gpiochip, providing an irq domain to translate the local IRQs to
1704 * global irqs in the gpiolib core, and making sure that the gpiochip
1705 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001706 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001707 * from the gpiochip passed as chip data. An irqdomain will be stored
1708 * in the gpiochip that shall be used by the driver to handle IRQ number
1709 * translation. The gpiochip will need to be initialized and registered
1710 * before calling this function.
1711 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001712 * This function will handle two cell:ed simple IRQs and assumes all
1713 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001714 * need to be open coded.
1715 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001716int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
1717 struct irq_chip *irqchip,
1718 unsigned int first_irq,
1719 irq_flow_handler_t handler,
1720 unsigned int type,
1721 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001722{
1723 struct device_node *of_node;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001724 bool irq_base_set = false;
Linus Walleij14250522014-03-25 10:40:18 +01001725 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001726 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001727
1728 if (!gpiochip || !irqchip)
1729 return -EINVAL;
1730
Linus Walleij58383c72015-11-04 09:56:26 +01001731 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001732 pr_err("missing gpiochip .dev parent pointer\n");
1733 return -EINVAL;
1734 }
Linus Walleij58383c72015-11-04 09:56:26 +01001735 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001736#ifdef CONFIG_OF_GPIO
1737 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001738 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001739 * FIXME: get rid of this and use gpiochip->parent->of_node
1740 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001741 */
1742 if (gpiochip->of_node)
1743 of_node = gpiochip->of_node;
1744#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001745 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001746 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001747 * used to configure the interrupts, as you may end-up with
1748 * conflicting triggers. Tell the user, and reset to NONE.
1749 */
1750 if (WARN(of_node && type != IRQ_TYPE_NONE,
1751 "%s: Ignoring %d default trigger\n", of_node->full_name, type))
1752 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001753 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1754 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1755 "Ignoring %d default trigger\n", type);
1756 type = IRQ_TYPE_NONE;
1757 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001758
Linus Walleij14250522014-03-25 10:40:18 +01001759 gpiochip->irqchip = irqchip;
1760 gpiochip->irq_handler = handler;
1761 gpiochip->irq_default_type = type;
1762 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001763 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +01001764 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1765 gpiochip->ngpio, first_irq,
1766 &gpiochip_domain_ops, gpiochip);
1767 if (!gpiochip->irqdomain) {
1768 gpiochip->irqchip = NULL;
1769 return -EINVAL;
1770 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001771
1772 /*
1773 * It is possible for a driver to override this, but only if the
1774 * alternative functions are both implemented.
1775 */
1776 if (!irqchip->irq_request_resources &&
1777 !irqchip->irq_release_resources) {
1778 irqchip->irq_request_resources = gpiochip_irq_reqres;
1779 irqchip->irq_release_resources = gpiochip_irq_relres;
1780 }
Linus Walleij14250522014-03-25 10:40:18 +01001781
1782 /*
1783 * Prepare the mapping since the irqchip shall be orthogonal to
1784 * any gpiochip calls. If the first_irq was zero, this is
1785 * necessary to allocate descriptors for all IRQs.
1786 */
Linus Walleijc3626fd2014-03-28 20:42:01 +01001787 for (offset = 0; offset < gpiochip->ngpio; offset++) {
Mika Westerberg79b804c2016-09-20 15:15:21 +03001788 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1789 continue;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001790 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001791 if (!irq_base_set) {
Linus Walleijc3626fd2014-03-28 20:42:01 +01001792 /*
1793 * Store the base into the gpiochip to be used when
1794 * unmapping the irqs.
1795 */
1796 gpiochip->irq_base = irq_base;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001797 irq_base_set = true;
1798 }
Linus Walleijc3626fd2014-03-28 20:42:01 +01001799 }
Linus Walleij14250522014-03-25 10:40:18 +01001800
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001801 acpi_gpiochip_request_interrupts(gpiochip);
1802
Linus Walleij14250522014-03-25 10:40:18 +01001803 return 0;
1804}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001805EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +01001806
1807#else /* CONFIG_GPIOLIB_IRQCHIP */
1808
1809static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03001810static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1811{
1812 return 0;
1813}
1814static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1815{ }
Linus Walleij14250522014-03-25 10:40:18 +01001816
1817#endif /* CONFIG_GPIOLIB_IRQCHIP */
1818
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001819/**
1820 * gpiochip_generic_request() - request the gpio function for a pin
1821 * @chip: the gpiochip owning the GPIO
1822 * @offset: the offset of the GPIO to request for GPIO function
1823 */
1824int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1825{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001826 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001827}
1828EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1829
1830/**
1831 * gpiochip_generic_free() - free the gpio function from a pin
1832 * @chip: the gpiochip to request the gpio function for
1833 * @offset: the offset of the GPIO to free from GPIO function
1834 */
1835void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1836{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001837 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001838}
1839EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1840
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301841#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001842
Linus Walleij3f0f8672012-11-20 12:40:15 +01001843/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001844 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1845 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001846 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001847 * @gpio_offset: the start offset in the current gpio_chip number space
1848 * @pin_group: name of the pin group inside the pin controller
1849 */
1850int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1851 struct pinctrl_dev *pctldev,
1852 unsigned int gpio_offset, const char *pin_group)
1853{
1854 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001855 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001856 int ret;
1857
1858 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1859 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001860 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001861 return -ENOMEM;
1862 }
1863
1864 /* Use local offset as range ID */
1865 pin_range->range.id = gpio_offset;
1866 pin_range->range.gc = chip;
1867 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001868 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001869 pin_range->pctldev = pctldev;
1870
1871 ret = pinctrl_get_group_pins(pctldev, pin_group,
1872 &pin_range->range.pins,
1873 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001874 if (ret < 0) {
1875 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001876 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001877 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001878
1879 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1880
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001881 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1882 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001883 pinctrl_dev_get_devname(pctldev), pin_group);
1884
Linus Walleij20ec3e32016-02-11 11:03:06 +01001885 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001886
1887 return 0;
1888}
1889EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1890
1891/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001892 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1893 * @chip: the gpiochip to add the range for
1894 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001895 * @gpio_offset: the start offset in the current gpio_chip number space
1896 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001897 * @npins: the number of pins from the offset of each pin space (GPIO and
1898 * pin controller) to accumulate in this range
1899 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001900int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001901 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001902 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301903{
1904 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001905 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001906 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301907
Linus Walleij3f0f8672012-11-20 12:40:15 +01001908 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301909 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001910 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001911 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301912 }
1913
Linus Walleij3f0f8672012-11-20 12:40:15 +01001914 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001915 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001916 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301917 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001918 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001919 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301920 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001921 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301922 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001923 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001924 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001925 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001926 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001927 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001928 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001929 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1930 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001931 pinctl_name,
1932 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301933
Linus Walleij20ec3e32016-02-11 11:03:06 +01001934 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001935
1936 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301937}
Linus Walleij165adc92012-11-06 14:49:39 +01001938EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301939
Linus Walleij3f0f8672012-11-20 12:40:15 +01001940/**
1941 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1942 * @chip: the chip to remove all the mappings for
1943 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301944void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1945{
1946 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01001947 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301948
Linus Walleij20ec3e32016-02-11 11:03:06 +01001949 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301950 list_del(&pin_range->node);
1951 pinctrl_remove_gpio_range(pin_range->pctldev,
1952 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001953 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301954 }
1955}
Linus Walleij165adc92012-11-06 14:49:39 +01001956EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1957
1958#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301959
David Brownelld2876d02008-02-04 22:28:20 -08001960/* These "optional" allocation calls help prevent drivers from stomping
1961 * on each other, and help provide better diagnostics in debugfs.
1962 * They're called even less than the "set direction" calls.
1963 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001964static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001965{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001966 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001967 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001968 unsigned long flags;
1969
1970 spin_lock_irqsave(&gpio_lock, flags);
1971
David Brownelld2876d02008-02-04 22:28:20 -08001972 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001973 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001974 */
1975
1976 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1977 desc_set_label(desc, label ? : "?");
1978 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001979 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001980 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001981 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001982 }
1983
1984 if (chip->request) {
1985 /* chip->request may sleep */
1986 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001987 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001988 spin_lock_irqsave(&gpio_lock, flags);
1989
1990 if (status < 0) {
1991 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001992 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001993 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001994 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001995 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001996 if (chip->get_direction) {
1997 /* chip->get_direction may sleep */
1998 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001999 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002000 spin_lock_irqsave(&gpio_lock, flags);
2001 }
David Brownelld2876d02008-02-04 22:28:20 -08002002done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002003 spin_unlock_irqrestore(&gpio_lock, flags);
2004 return status;
2005}
2006
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002007/*
2008 * This descriptor validation needs to be inserted verbatim into each
2009 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002010 * macro to avoid endless duplication. If the desc is NULL it is an
2011 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002012 */
2013#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002014 if (!desc) \
2015 return 0; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002016 if (IS_ERR(desc)) { \
2017 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2018 return PTR_ERR(desc); \
2019 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002020 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002021 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002022 return -EINVAL; \
2023 } \
2024 if ( !desc->gdev->chip ) { \
2025 dev_warn(&desc->gdev->dev, \
2026 "%s: backing chip is gone\n", __func__); \
2027 return 0; \
2028 } } while (0)
2029
2030#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002031 if (!desc) \
2032 return; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002033 if (IS_ERR(desc)) { \
2034 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2035 return; \
2036 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002037 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002038 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002039 return; \
2040 } \
2041 if (!desc->gdev->chip) { \
2042 dev_warn(&desc->gdev->dev, \
2043 "%s: backing chip is gone\n", __func__); \
2044 return; \
2045 } } while (0)
2046
2047
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002048int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002049{
2050 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002051 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002052
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002053 VALIDATE_DESC(desc);
2054 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002055
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002056 if (try_module_get(gdev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002057 status = __gpiod_request(desc, label);
2058 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002059 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002060 else
2061 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002062 }
2063
David Brownelld2876d02008-02-04 22:28:20 -08002064 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002065 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002066
David Brownelld2876d02008-02-04 22:28:20 -08002067 return status;
2068}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002069
Mika Westerberg77c2d792014-03-10 14:54:50 +02002070static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002071{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002072 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002073 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002074 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002075
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002076 might_sleep();
2077
Alexandre Courbot372e7222013-02-03 01:29:29 +09002078 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07002079
David Brownelld2876d02008-02-04 22:28:20 -08002080 spin_lock_irqsave(&gpio_lock, flags);
2081
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002082 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002083 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2084 if (chip->free) {
2085 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002086 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002087 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002088 spin_lock_irqsave(&gpio_lock, flags);
2089 }
David Brownelld2876d02008-02-04 22:28:20 -08002090 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002091 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002092 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302093 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302094 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002095 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002096 ret = true;
2097 }
David Brownelld2876d02008-02-04 22:28:20 -08002098
2099 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002100 return ret;
2101}
2102
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002103void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002104{
Linus Walleij33a68e82016-02-11 10:28:44 +01002105 if (desc && desc->gdev && __gpiod_free(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002106 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002107 put_device(&desc->gdev->dev);
2108 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002109 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002110 }
David Brownelld2876d02008-02-04 22:28:20 -08002111}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002112
David Brownelld2876d02008-02-04 22:28:20 -08002113/**
2114 * gpiochip_is_requested - return string iff signal was requested
2115 * @chip: controller managing the signal
2116 * @offset: of signal within controller's 0..(ngpio - 1) range
2117 *
2118 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002119 * The string returned is the label passed to gpio_request(); if none has been
2120 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002121 *
2122 * This function is for use by GPIO controller drivers. The label can
2123 * help with diagnostics, and knowing that the signal is used as a GPIO
2124 * can help avoid accidentally multiplexing it to another controller.
2125 */
2126const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2127{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002128 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002129
Dirk Behme48b59532015-08-18 18:02:32 +02002130 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002131 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002132
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002133 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002134
Alexandre Courbot372e7222013-02-03 01:29:29 +09002135 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002136 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002137 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002138}
2139EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2140
Mika Westerberg77c2d792014-03-10 14:54:50 +02002141/**
2142 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2143 * @desc: GPIO descriptor to request
2144 * @label: label for the GPIO
2145 *
2146 * Function allows GPIO chip drivers to request and use their own GPIO
2147 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2148 * function will not increase reference count of the GPIO chip module. This
2149 * allows the GPIO chip module to be unloaded as needed (we assume that the
2150 * GPIO chip driver handles freeing the GPIOs it has requested).
2151 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002152struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2153 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002154{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002155 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2156 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002157
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002158 if (IS_ERR(desc)) {
2159 chip_err(chip, "failed to get GPIO descriptor\n");
2160 return desc;
2161 }
2162
2163 err = __gpiod_request(desc, label);
2164 if (err < 0)
2165 return ERR_PTR(err);
2166
2167 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002168}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002169EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002170
2171/**
2172 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2173 * @desc: GPIO descriptor to free
2174 *
2175 * Function frees the given GPIO requested previously with
2176 * gpiochip_request_own_desc().
2177 */
2178void gpiochip_free_own_desc(struct gpio_desc *desc)
2179{
2180 if (desc)
2181 __gpiod_free(desc);
2182}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002183EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002184
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002185/*
2186 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002187 * some cases this is done in early boot, before IRQs are enabled.
2188 *
2189 * As a rule these aren't called more than once (except for drivers
2190 * using the open-drain emulation idiom) so these are natural places
2191 * to accumulate extra debugging checks. Note that we can't (yet)
2192 * rely on gpio_request() having been called beforehand.
2193 */
2194
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002195/**
2196 * gpiod_direction_input - set the GPIO direction to input
2197 * @desc: GPIO to set to input
2198 *
2199 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2200 * be called safely on it.
2201 *
2202 * Return 0 in case of success, else an error code.
2203 */
2204int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002205{
David Brownelld2876d02008-02-04 22:28:20 -08002206 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002207 int status = -EINVAL;
2208
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002209 VALIDATE_DESC(desc);
2210 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002211
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002212 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002213 gpiod_warn(desc,
2214 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002215 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002216 return -EIO;
2217 }
2218
Alexandre Courbotd82da792014-07-22 16:17:43 +09002219 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002220 if (status == 0)
2221 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002222
Alexandre Courbot372e7222013-02-03 01:29:29 +09002223 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002224
David Brownelld2876d02008-02-04 22:28:20 -08002225 return status;
2226}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002227EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002228
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002229static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002230{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002231 struct gpio_chip *gc = desc->gdev->chip;
2232 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002233
Linus Walleijd468bf92013-09-24 11:54:38 +02002234 /* GPIOs used for IRQs shall not be set as output */
2235 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2236 gpiod_err(desc,
2237 "%s: tried to set a GPIO tied to an IRQ as output\n",
2238 __func__);
2239 return -EIO;
2240 }
2241
Linus Walleijc663e5f2016-03-22 10:51:16 +01002242 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2243 /* First see if we can enable open drain in hardware */
2244 if (gc->set_single_ended) {
2245 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2246 LINE_MODE_OPEN_DRAIN);
2247 if (!ret)
2248 goto set_output_value;
2249 }
2250 /* Emulate open drain by not actively driving the line high */
2251 if (value)
2252 return gpiod_direction_input(desc);
2253 }
2254 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2255 if (gc->set_single_ended) {
2256 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2257 LINE_MODE_OPEN_SOURCE);
2258 if (!ret)
2259 goto set_output_value;
2260 }
2261 /* Emulate open source by not actively driving the line low */
2262 if (!value)
2263 return gpiod_direction_input(desc);
2264 } else {
2265 /* Make sure to disable open drain/source hardware, if any */
2266 if (gc->set_single_ended)
2267 gc->set_single_ended(gc,
2268 gpio_chip_hwgpio(desc),
2269 LINE_MODE_PUSH_PULL);
2270 }
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302271
Linus Walleijc663e5f2016-03-22 10:51:16 +01002272set_output_value:
2273 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002274 gpiod_warn(desc,
2275 "%s: missing set() or direction_output() operations\n",
2276 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002277 return -EIO;
2278 }
2279
Linus Walleijc663e5f2016-03-22 10:51:16 +01002280 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), value);
2281 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002282 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002283 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002284 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2285 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002286}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002287
2288/**
2289 * gpiod_direction_output_raw - set the GPIO direction to output
2290 * @desc: GPIO to set to output
2291 * @value: initial output value of the GPIO
2292 *
2293 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2294 * be called safely on it. The initial value of the output must be specified
2295 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2296 *
2297 * Return 0 in case of success, else an error code.
2298 */
2299int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2300{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002301 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002302 return _gpiod_direction_output_raw(desc, value);
2303}
2304EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2305
2306/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302307 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002308 * @desc: GPIO to set to output
2309 * @value: initial output value of the GPIO
2310 *
2311 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2312 * be called safely on it. The initial value of the output must be specified
2313 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2314 * account.
2315 *
2316 * Return 0 in case of success, else an error code.
2317 */
2318int gpiod_direction_output(struct gpio_desc *desc, int value)
2319{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002320 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002321 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2322 value = !value;
2323 return _gpiod_direction_output_raw(desc, value);
2324}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002325EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002326
Felipe Balbic4b5be92010-05-26 14:42:23 -07002327/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002328 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07002329 * @gpio: the gpio to set debounce time
2330 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002331 *
2332 * returns -ENOTSUPP if the controller does not support setting
2333 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002334 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002335int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002336{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002337 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002338
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002339 VALIDATE_DESC(desc);
2340 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002341 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01002342 gpiod_dbg(desc,
2343 "%s: missing set() or set_debounce() operations\n",
2344 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002345 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002346 }
2347
Alexandre Courbotd82da792014-07-22 16:17:43 +09002348 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002349}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002350EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002351
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002352/**
2353 * gpiod_is_active_low - test whether a GPIO is active-low or not
2354 * @desc: the gpio descriptor to test
2355 *
2356 * Returns 1 if the GPIO is active-low, 0 otherwise.
2357 */
2358int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002359{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002360 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002361 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002362}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002363EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002364
2365/* I/O calls are only valid after configuration completed; the relevant
2366 * "is this a valid GPIO" error checks should already have been done.
2367 *
2368 * "Get" operations are often inlinable as reading a pin value register,
2369 * and masking the relevant bit in that register.
2370 *
2371 * When "set" operations are inlinable, they involve writing that mask to
2372 * one register to set a low value, or a different register to set it high.
2373 * Otherwise locking is needed, so there may be little value to inlining.
2374 *
2375 *------------------------------------------------------------------------
2376 *
2377 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2378 * have requested the GPIO. That can include implicit requesting by
2379 * a direction setting call. Marking a gpio as requested locks its chip
2380 * in memory, guaranteeing that these table lookups need no more locking
2381 * and that gpiochip_remove() will fail.
2382 *
2383 * REVISIT when debugging, consider adding some instrumentation to ensure
2384 * that the GPIO was actually requested.
2385 */
2386
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002387static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002388{
2389 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002390 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002391 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002392
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002393 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002394 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002395 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002396 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002397 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002398 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002399}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002400
David Brownelld2876d02008-02-04 22:28:20 -08002401/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002402 * gpiod_get_raw_value() - return a gpio's raw value
2403 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002404 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002405 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002406 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002407 *
2408 * This function should be called from contexts where we cannot sleep, and will
2409 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002410 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002411int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002412{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002413 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002414 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002415 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002416 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002417}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002418EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002419
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002420/**
2421 * gpiod_get_value() - return a gpio's value
2422 * @desc: gpio whose value will be returned
2423 *
2424 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002425 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002426 *
2427 * This function should be called from contexts where we cannot sleep, and will
2428 * complain if the GPIO chip functions potentially sleep.
2429 */
2430int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002431{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002432 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002433
2434 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002435 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002436 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002437
2438 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002439 if (value < 0)
2440 return value;
2441
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002442 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2443 value = !value;
2444
2445 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002446}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002447EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002448
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302449/*
2450 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002451 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002452 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302453 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002454static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302455{
2456 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002457 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002458 int offset = gpio_chip_hwgpio(desc);
2459
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302460 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002461 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302462 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002463 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302464 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002465 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302466 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002467 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302468 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002469 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302470 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002471 gpiod_err(desc,
2472 "%s: Error in set_value for open drain err %d\n",
2473 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302474}
2475
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302476/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002477 * _gpio_set_open_source_value() - Set the open source gpio's value.
2478 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002479 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302480 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002481static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302482{
2483 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002484 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002485 int offset = gpio_chip_hwgpio(desc);
2486
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302487 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002488 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302489 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002490 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302491 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002492 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302493 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002494 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302495 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002496 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302497 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002498 gpiod_err(desc,
2499 "%s: Error in set_value for open source err %d\n",
2500 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302501}
2502
Alexandre Courbot23600962014-03-11 15:52:09 +09002503static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002504{
2505 struct gpio_chip *chip;
2506
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002507 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002508 trace_gpio_value(desc_to_gpio(desc), 0, value);
2509 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2510 _gpio_set_open_drain_value(desc, value);
2511 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2512 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302513 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002514 chip->set(chip, gpio_chip_hwgpio(desc), value);
2515}
2516
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002517/*
2518 * set multiple outputs on the same chip;
2519 * use the chip's set_multiple function if available;
2520 * otherwise set the outputs sequentially;
2521 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2522 * defines which outputs are to be changed
2523 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2524 * defines the values the outputs specified by mask are to be set to
2525 */
2526static void gpio_chip_set_multiple(struct gpio_chip *chip,
2527 unsigned long *mask, unsigned long *bits)
2528{
2529 if (chip->set_multiple) {
2530 chip->set_multiple(chip, mask, bits);
2531 } else {
2532 int i;
2533 for (i = 0; i < chip->ngpio; i++) {
2534 if (mask[BIT_WORD(i)] == 0) {
2535 /* no more set bits in this mask word;
2536 * skip ahead to the next word */
2537 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
2538 continue;
2539 }
2540 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002541 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002542 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002543 }
2544 }
2545}
2546
Linus Walleij44c7288f2016-04-24 11:36:59 +02002547void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2548 unsigned int array_size,
2549 struct gpio_desc **desc_array,
2550 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002551{
2552 int i = 0;
2553
2554 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002555 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002556 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2557 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2558 int count = 0;
2559
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002560 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002561 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002562
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002563 memset(mask, 0, sizeof(mask));
2564 do {
2565 struct gpio_desc *desc = desc_array[i];
2566 int hwgpio = gpio_chip_hwgpio(desc);
2567 int value = value_array[i];
2568
2569 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2570 value = !value;
2571 trace_gpio_value(desc_to_gpio(desc), 0, value);
2572 /*
2573 * collect all normal outputs belonging to the same chip
2574 * open drain and open source outputs are set individually
2575 */
2576 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002577 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002578 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2579 _gpio_set_open_source_value(desc, value);
2580 } else {
2581 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002582 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002583 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002584 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002585 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002586 count++;
2587 }
2588 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002589 } while ((i < array_size) &&
2590 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002591 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002592 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002593 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002594 }
2595}
2596
David Brownelld2876d02008-02-04 22:28:20 -08002597/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002598 * gpiod_set_raw_value() - assign a gpio's raw value
2599 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002600 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002601 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002602 * Set the raw value of the GPIO, i.e. the value of its physical line without
2603 * regard for its ACTIVE_LOW status.
2604 *
2605 * This function should be called from contexts where we cannot sleep, and will
2606 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002607 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002608void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002609{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002610 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002611 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002612 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002613 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002614}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002615EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002616
2617/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002618 * gpiod_set_value() - assign a gpio's value
2619 * @desc: gpio whose value will be assigned
2620 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002621 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002622 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2623 * account
2624 *
2625 * This function should be called from contexts where we cannot sleep, and will
2626 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002627 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002628void gpiod_set_value(struct gpio_desc *desc, int value)
2629{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002630 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002631 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002632 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002633 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2634 value = !value;
2635 _gpiod_set_raw_value(desc, value);
2636}
2637EXPORT_SYMBOL_GPL(gpiod_set_value);
2638
2639/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002640 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002641 * @array_size: number of elements in the descriptor / value arrays
2642 * @desc_array: array of GPIO descriptors whose values will be assigned
2643 * @value_array: array of values to assign
2644 *
2645 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2646 * without regard for their ACTIVE_LOW status.
2647 *
2648 * This function should be called from contexts where we cannot sleep, and will
2649 * complain if the GPIO chip functions potentially sleep.
2650 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002651void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002652 struct gpio_desc **desc_array, int *value_array)
2653{
2654 if (!desc_array)
2655 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002656 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2657 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002658}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002659EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002660
2661/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002662 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002663 * @array_size: number of elements in the descriptor / value arrays
2664 * @desc_array: array of GPIO descriptors whose values will be assigned
2665 * @value_array: array of values to assign
2666 *
2667 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2668 * into account.
2669 *
2670 * This function should be called from contexts where we cannot sleep, and will
2671 * complain if the GPIO chip functions potentially sleep.
2672 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002673void gpiod_set_array_value(unsigned int array_size,
2674 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002675{
2676 if (!desc_array)
2677 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002678 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2679 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002680}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002681EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002682
2683/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002684 * gpiod_cansleep() - report whether gpio value access may sleep
2685 * @desc: gpio to check
2686 *
2687 */
2688int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002689{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002690 VALIDATE_DESC(desc);
2691 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002692}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002693EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002694
David Brownell0f6d5042008-10-15 22:03:14 -07002695/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002696 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2697 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002698 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002699 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2700 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002701 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002702int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002703{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002704 struct gpio_chip *chip;
2705 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002706
Linus Walleij79bb71b2016-06-15 22:57:38 +02002707 /*
2708 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
2709 * requires this function to not return zero on an invalid descriptor
2710 * but rather a negative error number.
2711 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02002712 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02002713 return -EINVAL;
2714
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002715 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002716 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02002717 if (chip->to_irq) {
2718 int retirq = chip->to_irq(chip, offset);
2719
2720 /* Zero means NO_IRQ */
2721 if (!retirq)
2722 return -ENXIO;
2723
2724 return retirq;
2725 }
2726 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002727}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002728EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002729
Linus Walleijd468bf92013-09-24 11:54:38 +02002730/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002731 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002732 * @chip: the chip the GPIO to lock belongs to
2733 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002734 *
2735 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08002736 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08002737 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002738int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08002739{
Linus Walleij9c102802016-05-25 10:56:03 +02002740 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02002741
Linus Walleij9c102802016-05-25 10:56:03 +02002742 desc = gpiochip_get_desc(chip, offset);
2743 if (IS_ERR(desc))
2744 return PTR_ERR(desc);
2745
Linus Walleij60f83392016-11-12 15:01:09 +01002746 /*
2747 * If it's fast: flush the direction setting if something changed
2748 * behind our back
2749 */
2750 if (!chip->can_sleep && chip->get_direction) {
Linus Walleij9c102802016-05-25 10:56:03 +02002751 int dir = chip->get_direction(chip, offset);
2752
2753 if (dir)
2754 clear_bit(FLAG_IS_OUT, &desc->flags);
2755 else
2756 set_bit(FLAG_IS_OUT, &desc->flags);
2757 }
2758
2759 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002760 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02002761 "%s: tried to flag a GPIO set as output for IRQ\n",
2762 __func__);
2763 return -EIO;
2764 }
2765
Linus Walleij9c102802016-05-25 10:56:03 +02002766 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002767 return 0;
2768}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002769EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002770
Linus Walleijd468bf92013-09-24 11:54:38 +02002771/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002772 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002773 * @chip: the chip the GPIO to lock belongs to
2774 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002775 *
2776 * This is used directly by GPIO drivers that want to indicate
2777 * that a certain GPIO is no longer used exclusively for IRQ.
2778 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002779void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02002780{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002781 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02002782 return;
2783
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002784 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002785}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002786EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002787
Linus Walleij6cee3822016-02-11 20:16:45 +01002788bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2789{
2790 if (offset >= chip->ngpio)
2791 return false;
2792
2793 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2794}
2795EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2796
Linus Walleij143b65d2016-02-16 15:41:42 +01002797bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2798{
2799 if (offset >= chip->ngpio)
2800 return false;
2801
2802 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2803}
2804EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2805
2806bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2807{
2808 if (offset >= chip->ngpio)
2809 return false;
2810
2811 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2812}
2813EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2814
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002815/**
2816 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2817 * @desc: gpio whose value will be returned
2818 *
2819 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002820 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002821 *
2822 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002823 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002824int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002825{
David Brownelld2876d02008-02-04 22:28:20 -08002826 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002827 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002828 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002829}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002830EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002831
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002832/**
2833 * gpiod_get_value_cansleep() - return a gpio's value
2834 * @desc: gpio whose value will be returned
2835 *
2836 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002837 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002838 *
2839 * This function is to be called from contexts that can sleep.
2840 */
2841int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002842{
David Brownelld2876d02008-02-04 22:28:20 -08002843 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002844
2845 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002846 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002847 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002848 if (value < 0)
2849 return value;
2850
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002851 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2852 value = !value;
2853
David Brownelld2876d02008-02-04 22:28:20 -08002854 return value;
2855}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002856EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002857
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002858/**
2859 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2860 * @desc: gpio whose value will be assigned
2861 * @value: value to assign
2862 *
2863 * Set the raw value of the GPIO, i.e. the value of its physical line without
2864 * regard for its ACTIVE_LOW status.
2865 *
2866 * This function is to be called from contexts that can sleep.
2867 */
2868void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002869{
David Brownelld2876d02008-02-04 22:28:20 -08002870 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002871 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002872 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002873}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002874EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002875
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002876/**
2877 * gpiod_set_value_cansleep() - assign a gpio's value
2878 * @desc: gpio whose value will be assigned
2879 * @value: value to assign
2880 *
2881 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2882 * account
2883 *
2884 * This function is to be called from contexts that can sleep.
2885 */
2886void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002887{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002888 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002889 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002890 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2891 value = !value;
2892 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002893}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002894EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002895
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002896/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002897 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002898 * @array_size: number of elements in the descriptor / value arrays
2899 * @desc_array: array of GPIO descriptors whose values will be assigned
2900 * @value_array: array of values to assign
2901 *
2902 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2903 * without regard for their ACTIVE_LOW status.
2904 *
2905 * This function is to be called from contexts that can sleep.
2906 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002907void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2908 struct gpio_desc **desc_array,
2909 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002910{
2911 might_sleep_if(extra_checks);
2912 if (!desc_array)
2913 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002914 gpiod_set_array_value_complex(true, true, array_size, desc_array,
2915 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002916}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002917EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002918
2919/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002920 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002921 * @array_size: number of elements in the descriptor / value arrays
2922 * @desc_array: array of GPIO descriptors whose values will be assigned
2923 * @value_array: array of values to assign
2924 *
2925 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2926 * into account.
2927 *
2928 * This function is to be called from contexts that can sleep.
2929 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002930void gpiod_set_array_value_cansleep(unsigned int array_size,
2931 struct gpio_desc **desc_array,
2932 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002933{
2934 might_sleep_if(extra_checks);
2935 if (!desc_array)
2936 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002937 gpiod_set_array_value_complex(false, true, array_size, desc_array,
2938 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002939}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002940EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002941
2942/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002943 * gpiod_add_lookup_table() - register GPIO device consumers
2944 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002945 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002946void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002947{
2948 mutex_lock(&gpio_lookup_lock);
2949
Alexandre Courbotad824782013-12-03 12:20:11 +09002950 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002951
2952 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002953}
2954
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302955/**
2956 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2957 * @table: table of consumers to unregister
2958 */
2959void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2960{
2961 mutex_lock(&gpio_lookup_lock);
2962
2963 list_del(&table->list);
2964
2965 mutex_unlock(&gpio_lookup_lock);
2966}
2967
Alexandre Courbotad824782013-12-03 12:20:11 +09002968static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2969{
2970 const char *dev_id = dev ? dev_name(dev) : NULL;
2971 struct gpiod_lookup_table *table;
2972
2973 mutex_lock(&gpio_lookup_lock);
2974
2975 list_for_each_entry(table, &gpio_lookup_list, list) {
2976 if (table->dev_id && dev_id) {
2977 /*
2978 * Valid strings on both ends, must be identical to have
2979 * a match
2980 */
2981 if (!strcmp(table->dev_id, dev_id))
2982 goto found;
2983 } else {
2984 /*
2985 * One of the pointers is NULL, so both must be to have
2986 * a match
2987 */
2988 if (dev_id == table->dev_id)
2989 goto found;
2990 }
2991 }
2992 table = NULL;
2993
2994found:
2995 mutex_unlock(&gpio_lookup_lock);
2996 return table;
2997}
2998
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002999static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003000 unsigned int idx,
3001 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003002{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003003 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003004 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003005 struct gpiod_lookup *p;
3006
Alexandre Courbotad824782013-12-03 12:20:11 +09003007 table = gpiod_find_lookup_table(dev);
3008 if (!table)
3009 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003010
Alexandre Courbotad824782013-12-03 12:20:11 +09003011 for (p = &table->table[0]; p->chip_label; p++) {
3012 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003013
Alexandre Courbotad824782013-12-03 12:20:11 +09003014 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003015 if (p->idx != idx)
3016 continue;
3017
Alexandre Courbotad824782013-12-03 12:20:11 +09003018 /* If the lookup entry has a con_id, require exact match */
3019 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3020 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003021
Alexandre Courbotad824782013-12-03 12:20:11 +09003022 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003023
Alexandre Courbotad824782013-12-03 12:20:11 +09003024 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003025 dev_err(dev, "cannot find GPIO chip %s\n",
3026 p->chip_label);
3027 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003028 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003029
Alexandre Courbotad824782013-12-03 12:20:11 +09003030 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003031 dev_err(dev,
3032 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3033 idx, chip->ngpio, chip->label);
3034 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003035 }
3036
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003037 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003038 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003039
3040 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003041 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003042
3043 return desc;
3044}
3045
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003046static int dt_gpio_count(struct device *dev, const char *con_id)
3047{
3048 int ret;
3049 char propname[32];
3050 unsigned int i;
3051
3052 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3053 if (con_id)
3054 snprintf(propname, sizeof(propname), "%s-%s",
3055 con_id, gpio_suffixes[i]);
3056 else
3057 snprintf(propname, sizeof(propname), "%s",
3058 gpio_suffixes[i]);
3059
3060 ret = of_gpio_named_count(dev->of_node, propname);
3061 if (ret >= 0)
3062 break;
3063 }
3064 return ret;
3065}
3066
3067static int platform_gpio_count(struct device *dev, const char *con_id)
3068{
3069 struct gpiod_lookup_table *table;
3070 struct gpiod_lookup *p;
3071 unsigned int count = 0;
3072
3073 table = gpiod_find_lookup_table(dev);
3074 if (!table)
3075 return -ENOENT;
3076
3077 for (p = &table->table[0]; p->chip_label; p++) {
3078 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3079 (!con_id && !p->con_id))
3080 count++;
3081 }
3082 if (!count)
3083 return -ENOENT;
3084
3085 return count;
3086}
3087
3088/**
3089 * gpiod_count - return the number of GPIOs associated with a device / function
3090 * or -ENOENT if no GPIO has been assigned to the requested function
3091 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3092 * @con_id: function within the GPIO consumer
3093 */
3094int gpiod_count(struct device *dev, const char *con_id)
3095{
3096 int count = -ENOENT;
3097
3098 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3099 count = dt_gpio_count(dev, con_id);
3100 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3101 count = acpi_gpio_count(dev, con_id);
3102
3103 if (count < 0)
3104 count = platform_gpio_count(dev, con_id);
3105
3106 return count;
3107}
3108EXPORT_SYMBOL_GPL(gpiod_count);
3109
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003110/**
Thierry Reding08791622014-04-25 16:54:22 +02003111 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003112 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003113 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003114 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003115 *
3116 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003117 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003118 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003119 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003120struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003121 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003122{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003123 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003124}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003125EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003126
3127/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003128 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3129 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3130 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003131 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003132 *
3133 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3134 * the requested function it will return NULL. This is convenient for drivers
3135 * that need to handle optional GPIOs.
3136 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003137struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003138 const char *con_id,
3139 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003140{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003141 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003142}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003143EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003144
Benoit Parrotf625d462015-02-02 11:44:44 -06003145
3146/**
3147 * gpiod_configure_flags - helper function to configure a given GPIO
3148 * @desc: gpio whose value will be assigned
3149 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003150 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3151 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003152 * @dflags: gpiod_flags - optional GPIO initialization flags
3153 *
3154 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3155 * requested function and/or index, or another IS_ERR() code if an error
3156 * occurred while trying to acquire the GPIO.
3157 */
3158static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003159 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003160{
3161 int status;
3162
Johan Hovold85b03b32016-07-03 18:32:05 +02003163 if (lflags & GPIO_ACTIVE_LOW)
3164 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3165 if (lflags & GPIO_OPEN_DRAIN)
3166 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3167 if (lflags & GPIO_OPEN_SOURCE)
3168 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3169
Benoit Parrotf625d462015-02-02 11:44:44 -06003170 /* No particular flag request, return here... */
3171 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3172 pr_debug("no flags found for %s\n", con_id);
3173 return 0;
3174 }
3175
3176 /* Process flags */
3177 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3178 status = gpiod_direction_output(desc,
3179 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
3180 else
3181 status = gpiod_direction_input(desc);
3182
3183 return status;
3184}
3185
Thierry Reding29a1f2332014-04-25 17:10:06 +02003186/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003187 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003188 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003189 * @con_id: function within the GPIO consumer
3190 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003191 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003192 *
3193 * This variant of gpiod_get() allows to access GPIOs other than the first
3194 * defined one for functions that define several GPIOs.
3195 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003196 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3197 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003198 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003199 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003200struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003201 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003202 unsigned int idx,
3203 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003204{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003205 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003206 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003207 enum gpio_lookup_flags lookupflags = 0;
Linus Walleijbe1f6052018-01-16 08:29:50 +01003208 /* Maybe we have a device name, maybe not */
3209 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003210
3211 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3212
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003213 if (dev) {
3214 /* Using device tree? */
3215 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3216 dev_dbg(dev, "using device tree for GPIO lookup\n");
3217 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3218 } else if (ACPI_COMPANION(dev)) {
3219 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Dmitry Torokhov25487532016-03-24 10:50:25 -07003220 desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003221 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003222 }
3223
3224 /*
3225 * Either we are not using DT or ACPI, or their lookup did not return
3226 * a result. In that case, use platform lookup as a fallback.
3227 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003228 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003229 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003230 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003231 }
3232
3233 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003234 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003235 return desc;
3236 }
3237
Linus Walleijbe1f6052018-01-16 08:29:50 +01003238 /*
3239 * If a connection label was passed use that, else attempt to use
3240 * the device name as label
3241 */
3242 status = gpiod_request(desc, con_id ? con_id : devname);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003243 if (status < 0)
3244 return ERR_PTR(status);
3245
Johan Hovold85b03b32016-07-03 18:32:05 +02003246 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003247 if (status < 0) {
3248 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3249 gpiod_put(desc);
3250 return ERR_PTR(status);
3251 }
3252
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003253 return desc;
3254}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003255EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003256
3257/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003258 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3259 * @fwnode: handle of the firmware node
3260 * @propname: name of the firmware property representing the GPIO
3261 *
3262 * This function can be used for drivers that get their configuration
3263 * from firmware.
3264 *
3265 * Function properly finds the corresponding GPIO using whatever is the
3266 * underlying firmware interface and then makes sure that the GPIO
3267 * descriptor is requested before it is returned to the caller.
3268 *
3269 * In case of error an ERR_PTR() is returned.
3270 */
3271struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3272 const char *propname)
3273{
3274 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3275 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003276 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003277 int ret;
3278
3279 if (!fwnode)
3280 return ERR_PTR(-EINVAL);
3281
3282 if (is_of_node(fwnode)) {
3283 enum of_gpio_flags flags;
3284
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02003285 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02003286 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003287 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003288 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003289 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3290 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003291 } else if (is_acpi_node(fwnode)) {
3292 struct acpi_gpio_info info;
3293
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02003294 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02003295 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01003296 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003297 }
3298
3299 if (IS_ERR(desc))
3300 return desc;
3301
Johan Hovold85b03b32016-07-03 18:32:05 +02003302 ret = gpiod_request(desc, NULL);
3303 if (ret)
3304 return ERR_PTR(ret);
3305
Mika Westerberg40b73182014-10-21 13:33:59 +02003306 if (active_low)
3307 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3308
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003309 if (single_ended) {
3310 if (active_low)
3311 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3312 else
3313 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3314 }
3315
Mika Westerberg40b73182014-10-21 13:33:59 +02003316 return desc;
3317}
3318EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3319
3320/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003321 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3322 * function
3323 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3324 * @con_id: function within the GPIO consumer
3325 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003326 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003327 *
3328 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3329 * specified index was assigned to the requested function it will return NULL.
3330 * This is convenient for drivers that need to handle optional GPIOs.
3331 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003332struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003333 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003334 unsigned int index,
3335 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003336{
3337 struct gpio_desc *desc;
3338
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003339 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003340 if (IS_ERR(desc)) {
3341 if (PTR_ERR(desc) == -ENOENT)
3342 return NULL;
3343 }
3344
3345 return desc;
3346}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003347EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003348
3349/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003350 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3351 * @desc: gpio whose value will be assigned
3352 * @name: gpio line name
3353 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3354 * of_get_gpio_hog()
3355 * @dflags: gpiod_flags - optional GPIO initialization flags
3356 */
3357int gpiod_hog(struct gpio_desc *desc, const char *name,
3358 unsigned long lflags, enum gpiod_flags dflags)
3359{
3360 struct gpio_chip *chip;
3361 struct gpio_desc *local_desc;
3362 int hwnum;
3363 int status;
3364
3365 chip = gpiod_to_chip(desc);
3366 hwnum = gpio_chip_hwgpio(desc);
3367
3368 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3369 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303370 status = PTR_ERR(local_desc);
3371 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3372 name, chip->label, hwnum, status);
3373 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003374 }
3375
Johan Hovold85b03b32016-07-03 18:32:05 +02003376 status = gpiod_configure_flags(desc, name, lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003377 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303378 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3379 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003380 gpiochip_free_own_desc(desc);
3381 return status;
3382 }
3383
3384 /* Mark GPIO as hogged so it can be identified and removed later */
3385 set_bit(FLAG_IS_HOGGED, &desc->flags);
3386
3387 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3388 desc_to_gpio(desc), name,
3389 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3390 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3391 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3392
3393 return 0;
3394}
3395
3396/**
3397 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3398 * @chip: gpio chip to act on
3399 *
3400 * This is only used by of_gpiochip_remove to free hogged gpios
3401 */
3402static void gpiochip_free_hogs(struct gpio_chip *chip)
3403{
3404 int id;
3405
3406 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003407 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3408 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003409 }
3410}
3411
3412/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003413 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3414 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3415 * @con_id: function within the GPIO consumer
3416 * @flags: optional GPIO initialization flags
3417 *
3418 * This function acquires all the GPIOs defined under a given function.
3419 *
3420 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3421 * no GPIO has been assigned to the requested function, or another IS_ERR()
3422 * code if an error occurred while trying to acquire the GPIOs.
3423 */
3424struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3425 const char *con_id,
3426 enum gpiod_flags flags)
3427{
3428 struct gpio_desc *desc;
3429 struct gpio_descs *descs;
3430 int count;
3431
3432 count = gpiod_count(dev, con_id);
3433 if (count < 0)
3434 return ERR_PTR(count);
3435
3436 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3437 GFP_KERNEL);
3438 if (!descs)
3439 return ERR_PTR(-ENOMEM);
3440
3441 for (descs->ndescs = 0; descs->ndescs < count; ) {
3442 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3443 if (IS_ERR(desc)) {
3444 gpiod_put_array(descs);
3445 return ERR_CAST(desc);
3446 }
3447 descs->desc[descs->ndescs] = desc;
3448 descs->ndescs++;
3449 }
3450 return descs;
3451}
3452EXPORT_SYMBOL_GPL(gpiod_get_array);
3453
3454/**
3455 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3456 * function
3457 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3458 * @con_id: function within the GPIO consumer
3459 * @flags: optional GPIO initialization flags
3460 *
3461 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3462 * assigned to the requested function it will return NULL.
3463 */
3464struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3465 const char *con_id,
3466 enum gpiod_flags flags)
3467{
3468 struct gpio_descs *descs;
3469
3470 descs = gpiod_get_array(dev, con_id, flags);
3471 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3472 return NULL;
3473
3474 return descs;
3475}
3476EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3477
3478/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003479 * gpiod_put - dispose of a GPIO descriptor
3480 * @desc: GPIO descriptor to dispose of
3481 *
3482 * No descriptor can be used after gpiod_put() has been called on it.
3483 */
3484void gpiod_put(struct gpio_desc *desc)
3485{
3486 gpiod_free(desc);
3487}
3488EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003489
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003490/**
3491 * gpiod_put_array - dispose of multiple GPIO descriptors
3492 * @descs: struct gpio_descs containing an array of descriptors
3493 */
3494void gpiod_put_array(struct gpio_descs *descs)
3495{
3496 unsigned int i;
3497
3498 for (i = 0; i < descs->ndescs; i++)
3499 gpiod_put(descs->desc[i]);
3500
3501 kfree(descs);
3502}
3503EXPORT_SYMBOL_GPL(gpiod_put_array);
3504
Linus Walleij3c702e92015-10-21 15:29:53 +02003505static int __init gpiolib_dev_init(void)
3506{
3507 int ret;
3508
3509 /* Register GPIO sysfs bus */
3510 ret = bus_register(&gpio_bus_type);
3511 if (ret < 0) {
3512 pr_err("gpiolib: could not register GPIO bus type\n");
3513 return ret;
3514 }
3515
3516 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3517 if (ret < 0) {
3518 pr_err("gpiolib: failed to allocate char dev region\n");
3519 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003520 } else {
3521 gpiolib_initialized = true;
3522 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003523 }
3524 return ret;
3525}
3526core_initcall(gpiolib_dev_init);
3527
David Brownelld2876d02008-02-04 22:28:20 -08003528#ifdef CONFIG_DEBUG_FS
3529
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003530static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003531{
3532 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003533 struct gpio_chip *chip = gdev->chip;
3534 unsigned gpio = gdev->base;
3535 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003536 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003537 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003538
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003539 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003540 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3541 if (gdesc->name) {
3542 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3543 gpio, gdesc->name);
3544 }
David Brownelld2876d02008-02-04 22:28:20 -08003545 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003546 }
David Brownelld2876d02008-02-04 22:28:20 -08003547
Alexandre Courbot372e7222013-02-03 01:29:29 +09003548 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003549 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003550 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003551 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3552 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003553 is_out ? "out" : "in ",
3554 chip->get
3555 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003556 : "? ",
3557 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003558 seq_printf(s, "\n");
3559 }
3560}
3561
Thierry Redingf9c4a312012-04-12 13:26:01 +02003562static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003563{
Grant Likely362432a2013-02-09 09:41:49 +00003564 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003565 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003566 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003567
3568 s->private = "";
3569
Grant Likely362432a2013-02-09 09:41:49 +00003570 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003571 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003572 if (index-- == 0) {
3573 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003574 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003575 }
3576 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003577
3578 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003579}
3580
3581static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3582{
Grant Likely362432a2013-02-09 09:41:49 +00003583 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003584 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003585 void *ret = NULL;
3586
Grant Likely362432a2013-02-09 09:41:49 +00003587 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003588 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003589 ret = NULL;
3590 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003591 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003592 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003593
3594 s->private = "\n";
3595 ++*pos;
3596
3597 return ret;
3598}
3599
3600static void gpiolib_seq_stop(struct seq_file *s, void *v)
3601{
3602}
3603
3604static int gpiolib_seq_show(struct seq_file *s, void *v)
3605{
Linus Walleijff2b1352015-10-20 11:10:38 +02003606 struct gpio_device *gdev = v;
3607 struct gpio_chip *chip = gdev->chip;
3608 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003609
Linus Walleijff2b1352015-10-20 11:10:38 +02003610 if (!chip) {
3611 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3612 dev_name(&gdev->dev));
3613 return 0;
3614 }
3615
3616 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3617 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003618 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02003619 parent = chip->parent;
3620 if (parent)
3621 seq_printf(s, ", parent: %s/%s",
3622 parent->bus ? parent->bus->name : "no-bus",
3623 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02003624 if (chip->label)
3625 seq_printf(s, ", %s", chip->label);
3626 if (chip->can_sleep)
3627 seq_printf(s, ", can sleep");
3628 seq_printf(s, ":\n");
3629
3630 if (chip->dbg_show)
3631 chip->dbg_show(s, chip);
3632 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003633 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003634
David Brownelld2876d02008-02-04 22:28:20 -08003635 return 0;
3636}
3637
Thierry Redingf9c4a312012-04-12 13:26:01 +02003638static const struct seq_operations gpiolib_seq_ops = {
3639 .start = gpiolib_seq_start,
3640 .next = gpiolib_seq_next,
3641 .stop = gpiolib_seq_stop,
3642 .show = gpiolib_seq_show,
3643};
3644
David Brownelld2876d02008-02-04 22:28:20 -08003645static int gpiolib_open(struct inode *inode, struct file *file)
3646{
Thierry Redingf9c4a312012-04-12 13:26:01 +02003647 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08003648}
3649
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003650static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02003651 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08003652 .open = gpiolib_open,
3653 .read = seq_read,
3654 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02003655 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08003656};
3657
3658static int __init gpiolib_debugfs_init(void)
3659{
3660 /* /sys/kernel/debug/gpio */
3661 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3662 NULL, NULL, &gpiolib_operations);
3663 return 0;
3664}
3665subsys_initcall(gpiolib_debugfs_init);
3666
3667#endif /* DEBUG_FS */