blob: 868128a676bae832090c2c18b1f45e94c2996a5f [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d2008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020018#include <linux/pinctrl/consumer.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020019#include <linux/cdev.h>
20#include <linux/fs.h>
21#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020022#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020023#include <linux/anon_inodes.h>
Lars-Peter Clausen953b9562016-10-24 13:59:15 +020024#include <linux/file.h>
Linus Walleij61f922d2016-06-02 11:30:15 +020025#include <linux/kfifo.h>
26#include <linux/poll.h>
27#include <linux/timekeeping.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020028#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080029
Mika Westerberg664e3e52014-01-08 12:40:54 +020030#include "gpiolib.h"
31
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060032#define CREATE_TRACE_POINTS
33#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080034
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070035/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080036 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070037 * The GPIO programming interface allows for inlining speed-critical
38 * get/set operations for common cases, so that access to SOC-integrated
39 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080040 */
41
42
43/* When debugging, extend minimal trust to callers and platform code.
44 * Also emit diagnostic messages that may help initial bringup, when
45 * board setup or driver bugs are most common.
46 *
47 * Otherwise, minimize overhead in what may be bitbanging codepaths.
48 */
49#ifdef DEBUG
50#define extra_checks 1
51#else
52#define extra_checks 0
53#endif
54
Linus Walleijff2b1352015-10-20 11:10:38 +020055/* Device and char device-related information */
56static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020057static dev_t gpio_devt;
58#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
59static struct bus_type gpio_bus_type = {
60 .name = "gpio",
61};
Linus Walleijff2b1352015-10-20 11:10:38 +020062
David Brownelld2876d02008-02-04 22:28:20 -080063/* gpio_lock prevents conflicts during gpio_desc[] table updates.
64 * While any GPIO is requested, its gpio_chip is not removable;
65 * each GPIO's "requested" flag serves as a lock and refcount.
66 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090067DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080068
Alexandre Courbotbae48da2013-10-17 10:21:38 -070069static DEFINE_MUTEX(gpio_lookup_lock);
70static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020071LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020072
73static void gpiochip_free_hogs(struct gpio_chip *chip);
74static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030075static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
76static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020077
Guenter Roeck159f3cd2016-03-31 08:11:30 -070078static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020079
David Brownelld2876d02008-02-04 22:28:20 -080080static inline void desc_set_label(struct gpio_desc *d, const char *label)
81{
David Brownelld2876d02008-02-04 22:28:20 -080082 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080083}
84
Alexandre Courbot372e7222013-02-03 01:29:29 +090085/**
86 * Convert a GPIO number to its descriptor
87 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070088struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090089{
Linus Walleijff2b1352015-10-20 11:10:38 +020090 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +090091 unsigned long flags;
92
93 spin_lock_irqsave(&gpio_lock, flags);
94
Linus Walleijff2b1352015-10-20 11:10:38 +020095 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +010096 if (gdev->base <= gpio &&
97 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +090098 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +010099 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900100 }
101 }
102
103 spin_unlock_irqrestore(&gpio_lock, flags);
104
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900105 if (!gpio_is_valid(gpio))
106 WARN(1, "invalid GPIO %d\n", gpio);
107
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900108 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900109}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700110EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900111
112/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900113 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +0200114 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900115struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
116 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200117{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100118 struct gpio_device *gdev = chip->gpiodev;
119
120 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900121 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200122
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100123 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200124}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900125
126/**
127 * Convert a GPIO descriptor to the integer namespace.
128 * This should disappear in the future but is needed since we still
129 * use GPIO numbers for error messages and sysfs nodes
130 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700131int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900132{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100133 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900134}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700135EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900136
137
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700138/**
139 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
140 * @desc: descriptor to return the chip of
141 */
142struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900143{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100144 if (!desc || !desc->gdev || !desc->gdev->chip)
145 return NULL;
146 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900147}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700148EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800149
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700150/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
151static int gpiochip_find_base(int ngpio)
152{
Linus Walleijff2b1352015-10-20 11:10:38 +0200153 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900154 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700155
Linus Walleijff2b1352015-10-20 11:10:38 +0200156 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900157 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100158 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900159 break;
160 else
161 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100162 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700163 }
164
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900165 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700166 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900167 return base;
168 } else {
169 pr_err("%s: cannot find free range\n", __func__);
170 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700171 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700172}
173
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700174/**
175 * gpiod_get_direction - return the current direction of a GPIO
176 * @desc: GPIO to get the direction of
177 *
178 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
179 *
180 * This function may sleep if gpiod_cansleep() is true.
181 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900182int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300183{
184 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900185 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300186 int status = -EINVAL;
187
Alexandre Courbot372e7222013-02-03 01:29:29 +0900188 chip = gpiod_to_chip(desc);
189 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300190
191 if (!chip->get_direction)
192 return status;
193
Alexandre Courbot372e7222013-02-03 01:29:29 +0900194 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300195 if (status > 0) {
196 /* GPIOF_DIR_IN, or other positive */
197 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900198 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300199 }
200 if (status == 0) {
201 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900202 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300203 }
204 return status;
205}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700206EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300207
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900208/*
209 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800210 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900211 *
212 * Return -EBUSY if the new chip overlaps with some other chip's integer
213 * space.
214 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200215static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900216{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800217 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200218
219 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800220 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200221 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530222 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900223 }
224
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800225 next = list_entry(gpio_devices.next, struct gpio_device, list);
226 if (gdev->base + gdev->ngpio <= next->base) {
227 /* add before first entry */
228 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500229 return 0;
230 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900231
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800232 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
233 if (prev->base + prev->ngpio <= gdev->base) {
234 /* add behind last entry */
235 list_add_tail(&gdev->list, &gpio_devices);
236 return 0;
237 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800238
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800239 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
240 /* at the end of the list */
241 if (&next->list == &gpio_devices)
242 break;
243
244 /* add between prev and next */
245 if (prev->base + prev->ngpio <= gdev->base
246 && gdev->base + gdev->ngpio <= next->base) {
247 list_add(&gdev->list, &prev->list);
248 return 0;
249 }
250 }
251
252 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
253 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900254}
255
Linus Walleijf881bab2015-09-23 16:20:43 -0700256/**
257 * Convert a GPIO name to its descriptor
258 */
259static struct gpio_desc *gpio_name_to_desc(const char * const name)
260{
Linus Walleijff2b1352015-10-20 11:10:38 +0200261 struct gpio_device *gdev;
Linus Walleijf881bab2015-09-23 16:20:43 -0700262 unsigned long flags;
263
264 spin_lock_irqsave(&gpio_lock, flags);
265
Linus Walleijff2b1352015-10-20 11:10:38 +0200266 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700267 int i;
268
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100269 for (i = 0; i != gdev->ngpio; ++i) {
270 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab2015-09-23 16:20:43 -0700271
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100272 if (!desc->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700273 continue;
274
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100275 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700276 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100277 return desc;
Linus Walleijf881bab2015-09-23 16:20:43 -0700278 }
279 }
280 }
281
282 spin_unlock_irqrestore(&gpio_lock, flags);
283
284 return NULL;
285}
286
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200287/*
288 * Takes the names from gc->names and checks if they are all unique. If they
289 * are, they are assigned to their gpio descriptors.
290 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800291 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200292 */
293static int gpiochip_set_desc_names(struct gpio_chip *gc)
294{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100295 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200296 int i;
297
298 if (!gc->names)
299 return 0;
300
301 /* First check all names if they are unique */
302 for (i = 0; i != gc->ngpio; ++i) {
303 struct gpio_desc *gpio;
304
305 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700306 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100307 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200308 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab2015-09-23 16:20:43 -0700309 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200310 }
311
312 /* Then add all names to the GPIO descriptors */
313 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100314 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200315
316 return 0;
317}
318
Linus Walleijd7c51b42016-04-26 10:35:29 +0200319/*
320 * GPIO line handle management
321 */
322
323/**
324 * struct linehandle_state - contains the state of a userspace handle
325 * @gdev: the GPIO device the handle pertains to
326 * @label: consumer label used to tag descriptors
327 * @descs: the GPIO descriptors held by this handle
328 * @numdescs: the number of descriptors held in the descs array
329 */
330struct linehandle_state {
331 struct gpio_device *gdev;
332 const char *label;
333 struct gpio_desc *descs[GPIOHANDLES_MAX];
334 u32 numdescs;
335};
336
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200337#define GPIOHANDLE_REQUEST_VALID_FLAGS \
338 (GPIOHANDLE_REQUEST_INPUT | \
339 GPIOHANDLE_REQUEST_OUTPUT | \
340 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
341 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
342 GPIOHANDLE_REQUEST_OPEN_SOURCE)
343
Linus Walleijd7c51b42016-04-26 10:35:29 +0200344static long linehandle_ioctl(struct file *filep, unsigned int cmd,
345 unsigned long arg)
346{
347 struct linehandle_state *lh = filep->private_data;
348 void __user *ip = (void __user *)arg;
349 struct gpiohandle_data ghd;
350 int i;
351
352 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
353 int val;
354
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200355 memset(&ghd, 0, sizeof(ghd));
356
Linus Walleijd7c51b42016-04-26 10:35:29 +0200357 /* TODO: check if descriptors are really input */
358 for (i = 0; i < lh->numdescs; i++) {
359 val = gpiod_get_value_cansleep(lh->descs[i]);
360 if (val < 0)
361 return val;
362 ghd.values[i] = val;
363 }
364
365 if (copy_to_user(ip, &ghd, sizeof(ghd)))
366 return -EFAULT;
367
368 return 0;
369 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
370 int vals[GPIOHANDLES_MAX];
371
372 /* TODO: check if descriptors are really output */
373 if (copy_from_user(&ghd, ip, sizeof(ghd)))
374 return -EFAULT;
375
376 /* Clamp all values to [0,1] */
377 for (i = 0; i < lh->numdescs; i++)
378 vals[i] = !!ghd.values[i];
379
380 /* Reuse the array setting function */
381 gpiod_set_array_value_complex(false,
382 true,
383 lh->numdescs,
384 lh->descs,
385 vals);
386 return 0;
387 }
388 return -EINVAL;
389}
390
391#ifdef CONFIG_COMPAT
392static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
393 unsigned long arg)
394{
395 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
396}
397#endif
398
399static int linehandle_release(struct inode *inode, struct file *filep)
400{
401 struct linehandle_state *lh = filep->private_data;
402 struct gpio_device *gdev = lh->gdev;
403 int i;
404
405 for (i = 0; i < lh->numdescs; i++)
406 gpiod_free(lh->descs[i]);
407 kfree(lh->label);
408 kfree(lh);
409 put_device(&gdev->dev);
410 return 0;
411}
412
413static const struct file_operations linehandle_fileops = {
414 .release = linehandle_release,
415 .owner = THIS_MODULE,
416 .llseek = noop_llseek,
417 .unlocked_ioctl = linehandle_ioctl,
418#ifdef CONFIG_COMPAT
419 .compat_ioctl = linehandle_ioctl_compat,
420#endif
421};
422
423static int linehandle_create(struct gpio_device *gdev, void __user *ip)
424{
425 struct gpiohandle_request handlereq;
426 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200427 struct file *file;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200428 int fd, i, ret;
429
430 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
431 return -EFAULT;
432 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
433 return -EINVAL;
434
435 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
436 if (!lh)
437 return -ENOMEM;
438 lh->gdev = gdev;
439 get_device(&gdev->dev);
440
441 /* Make sure this is terminated */
442 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
443 if (strlen(handlereq.consumer_label)) {
444 lh->label = kstrdup(handlereq.consumer_label,
445 GFP_KERNEL);
446 if (!lh->label) {
447 ret = -ENOMEM;
448 goto out_free_lh;
449 }
450 }
451
452 /* Request each GPIO */
453 for (i = 0; i < handlereq.lines; i++) {
454 u32 offset = handlereq.lineoffsets[i];
455 u32 lflags = handlereq.flags;
456 struct gpio_desc *desc;
457
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200458 if (offset >= gdev->ngpio) {
459 ret = -EINVAL;
460 goto out_free_descs;
461 }
462
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200463 /* Return an error if a unknown flag is set */
464 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) {
465 ret = -EINVAL;
466 goto out_free_descs;
467 }
468
Linus Walleijd7c51b42016-04-26 10:35:29 +0200469 desc = &gdev->descs[offset];
470 ret = gpiod_request(desc, lh->label);
471 if (ret)
472 goto out_free_descs;
473 lh->descs[i] = desc;
474
475 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
476 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
477 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
478 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
479 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
480 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
481
482 /*
483 * Lines have to be requested explicitly for input
484 * or output, else the line will be treated "as is".
485 */
486 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
487 int val = !!handlereq.default_values[i];
488
489 ret = gpiod_direction_output(desc, val);
490 if (ret)
491 goto out_free_descs;
492 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
493 ret = gpiod_direction_input(desc);
494 if (ret)
495 goto out_free_descs;
496 }
497 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
498 offset);
499 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200500 /* Let i point at the last handle */
501 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200502 lh->numdescs = handlereq.lines;
503
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200504 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200505 if (fd < 0) {
506 ret = fd;
507 goto out_free_descs;
508 }
509
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200510 file = anon_inode_getfile("gpio-linehandle",
511 &linehandle_fileops,
512 lh,
513 O_RDONLY | O_CLOEXEC);
514 if (IS_ERR(file)) {
515 ret = PTR_ERR(file);
516 goto out_put_unused_fd;
517 }
518
Linus Walleijd7c51b42016-04-26 10:35:29 +0200519 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200520 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200521 /*
522 * fput() will trigger the release() callback, so do not go onto
523 * the regular error cleanup path here.
524 */
525 fput(file);
526 put_unused_fd(fd);
527 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200528 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200529
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200530 fd_install(fd, file);
531
Linus Walleijd7c51b42016-04-26 10:35:29 +0200532 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
533 lh->numdescs);
534
535 return 0;
536
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200537out_put_unused_fd:
538 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200539out_free_descs:
540 for (; i >= 0; i--)
541 gpiod_free(lh->descs[i]);
542 kfree(lh->label);
543out_free_lh:
544 kfree(lh);
545 put_device(&gdev->dev);
546 return ret;
547}
548
Linus Walleij61f922d2016-06-02 11:30:15 +0200549/*
550 * GPIO line event management
551 */
552
553/**
554 * struct lineevent_state - contains the state of a userspace event
555 * @gdev: the GPIO device the event pertains to
556 * @label: consumer label used to tag descriptors
557 * @desc: the GPIO descriptor held by this event
558 * @eflags: the event flags this line was requested with
559 * @irq: the interrupt that trigger in response to events on this GPIO
560 * @wait: wait queue that handles blocking reads of events
561 * @events: KFIFO for the GPIO events
562 * @read_lock: mutex lock to protect reads from colliding with adding
563 * new events to the FIFO
564 */
565struct lineevent_state {
566 struct gpio_device *gdev;
567 const char *label;
568 struct gpio_desc *desc;
569 u32 eflags;
570 int irq;
571 wait_queue_head_t wait;
572 DECLARE_KFIFO(events, struct gpioevent_data, 16);
573 struct mutex read_lock;
574};
575
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200576#define GPIOEVENT_REQUEST_VALID_FLAGS \
577 (GPIOEVENT_REQUEST_RISING_EDGE | \
578 GPIOEVENT_REQUEST_FALLING_EDGE)
579
Linus Walleij61f922d2016-06-02 11:30:15 +0200580static unsigned int lineevent_poll(struct file *filep,
581 struct poll_table_struct *wait)
582{
583 struct lineevent_state *le = filep->private_data;
584 unsigned int events = 0;
585
586 poll_wait(filep, &le->wait, wait);
587
588 if (!kfifo_is_empty(&le->events))
589 events = POLLIN | POLLRDNORM;
590
591 return events;
592}
593
594
595static ssize_t lineevent_read(struct file *filep,
596 char __user *buf,
597 size_t count,
598 loff_t *f_ps)
599{
600 struct lineevent_state *le = filep->private_data;
601 unsigned int copied;
602 int ret;
603
604 if (count < sizeof(struct gpioevent_data))
605 return -EINVAL;
606
607 do {
608 if (kfifo_is_empty(&le->events)) {
609 if (filep->f_flags & O_NONBLOCK)
610 return -EAGAIN;
611
612 ret = wait_event_interruptible(le->wait,
613 !kfifo_is_empty(&le->events));
614 if (ret)
615 return ret;
616 }
617
618 if (mutex_lock_interruptible(&le->read_lock))
619 return -ERESTARTSYS;
620 ret = kfifo_to_user(&le->events, buf, count, &copied);
621 mutex_unlock(&le->read_lock);
622
623 if (ret)
624 return ret;
625
626 /*
627 * If we couldn't read anything from the fifo (a different
628 * thread might have been faster) we either return -EAGAIN if
629 * the file descriptor is non-blocking, otherwise we go back to
630 * sleep and wait for more data to arrive.
631 */
632 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
633 return -EAGAIN;
634
635 } while (copied == 0);
636
637 return copied;
638}
639
640static int lineevent_release(struct inode *inode, struct file *filep)
641{
642 struct lineevent_state *le = filep->private_data;
643 struct gpio_device *gdev = le->gdev;
644
645 free_irq(le->irq, le);
646 gpiod_free(le->desc);
647 kfree(le->label);
648 kfree(le);
649 put_device(&gdev->dev);
650 return 0;
651}
652
653static long lineevent_ioctl(struct file *filep, unsigned int cmd,
654 unsigned long arg)
655{
656 struct lineevent_state *le = filep->private_data;
657 void __user *ip = (void __user *)arg;
658 struct gpiohandle_data ghd;
659
660 /*
661 * We can get the value for an event line but not set it,
662 * because it is input by definition.
663 */
664 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
665 int val;
666
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200667 memset(&ghd, 0, sizeof(ghd));
668
Linus Walleij61f922d2016-06-02 11:30:15 +0200669 val = gpiod_get_value_cansleep(le->desc);
670 if (val < 0)
671 return val;
672 ghd.values[0] = val;
673
674 if (copy_to_user(ip, &ghd, sizeof(ghd)))
675 return -EFAULT;
676
677 return 0;
678 }
679 return -EINVAL;
680}
681
682#ifdef CONFIG_COMPAT
683static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
684 unsigned long arg)
685{
686 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
687}
688#endif
689
690static const struct file_operations lineevent_fileops = {
691 .release = lineevent_release,
692 .read = lineevent_read,
693 .poll = lineevent_poll,
694 .owner = THIS_MODULE,
695 .llseek = noop_llseek,
696 .unlocked_ioctl = lineevent_ioctl,
697#ifdef CONFIG_COMPAT
698 .compat_ioctl = lineevent_ioctl_compat,
699#endif
700};
701
Ben Dooks33265b12016-06-17 16:03:13 +0100702static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200703{
704 struct lineevent_state *le = p;
705 struct gpioevent_data ge;
706 int ret;
707
708 ge.timestamp = ktime_get_real_ns();
709
710 if (le->eflags & GPIOEVENT_REQUEST_BOTH_EDGES) {
711 int level = gpiod_get_value_cansleep(le->desc);
712
713 if (level)
714 /* Emit low-to-high event */
715 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
716 else
717 /* Emit high-to-low event */
718 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
719 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
720 /* Emit low-to-high event */
721 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
722 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
723 /* Emit high-to-low event */
724 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200725 } else {
726 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200727 }
728
729 ret = kfifo_put(&le->events, ge);
730 if (ret != 0)
731 wake_up_poll(&le->wait, POLLIN);
732
733 return IRQ_HANDLED;
734}
735
736static int lineevent_create(struct gpio_device *gdev, void __user *ip)
737{
738 struct gpioevent_request eventreq;
739 struct lineevent_state *le;
740 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200741 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200742 u32 offset;
743 u32 lflags;
744 u32 eflags;
745 int fd;
746 int ret;
747 int irqflags = 0;
748
749 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
750 return -EFAULT;
751
752 le = kzalloc(sizeof(*le), GFP_KERNEL);
753 if (!le)
754 return -ENOMEM;
755 le->gdev = gdev;
756 get_device(&gdev->dev);
757
758 /* Make sure this is terminated */
759 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
760 if (strlen(eventreq.consumer_label)) {
761 le->label = kstrdup(eventreq.consumer_label,
762 GFP_KERNEL);
763 if (!le->label) {
764 ret = -ENOMEM;
765 goto out_free_le;
766 }
767 }
768
769 offset = eventreq.lineoffset;
770 lflags = eventreq.handleflags;
771 eflags = eventreq.eventflags;
772
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200773 if (offset >= gdev->ngpio) {
774 ret = -EINVAL;
775 goto out_free_label;
776 }
777
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200778 /* Return an error if a unknown flag is set */
779 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
780 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
781 ret = -EINVAL;
782 goto out_free_label;
783 }
784
Linus Walleij61f922d2016-06-02 11:30:15 +0200785 /* This is just wrong: we don't look for events on output lines */
786 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
787 ret = -EINVAL;
788 goto out_free_label;
789 }
790
791 desc = &gdev->descs[offset];
792 ret = gpiod_request(desc, le->label);
793 if (ret)
794 goto out_free_desc;
795 le->desc = desc;
796 le->eflags = eflags;
797
798 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
799 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
800 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
801 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
802 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
803 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
804
805 ret = gpiod_direction_input(desc);
806 if (ret)
807 goto out_free_desc;
808
809 le->irq = gpiod_to_irq(desc);
810 if (le->irq <= 0) {
811 ret = -ENODEV;
812 goto out_free_desc;
813 }
814
815 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
816 irqflags |= IRQF_TRIGGER_RISING;
817 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
818 irqflags |= IRQF_TRIGGER_FALLING;
819 irqflags |= IRQF_ONESHOT;
820 irqflags |= IRQF_SHARED;
821
822 INIT_KFIFO(le->events);
823 init_waitqueue_head(&le->wait);
824 mutex_init(&le->read_lock);
825
826 /* Request a thread to read the events */
827 ret = request_threaded_irq(le->irq,
828 NULL,
829 lineevent_irq_thread,
830 irqflags,
831 le->label,
832 le);
833 if (ret)
834 goto out_free_desc;
835
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200836 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200837 if (fd < 0) {
838 ret = fd;
839 goto out_free_irq;
840 }
841
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200842 file = anon_inode_getfile("gpio-event",
843 &lineevent_fileops,
844 le,
845 O_RDONLY | O_CLOEXEC);
846 if (IS_ERR(file)) {
847 ret = PTR_ERR(file);
848 goto out_put_unused_fd;
849 }
850
Linus Walleij61f922d2016-06-02 11:30:15 +0200851 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200852 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200853 /*
854 * fput() will trigger the release() callback, so do not go onto
855 * the regular error cleanup path here.
856 */
857 fput(file);
858 put_unused_fd(fd);
859 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200860 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200861
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200862 fd_install(fd, file);
863
Linus Walleij61f922d2016-06-02 11:30:15 +0200864 return 0;
865
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200866out_put_unused_fd:
867 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +0200868out_free_irq:
869 free_irq(le->irq, le);
870out_free_desc:
871 gpiod_free(le->desc);
872out_free_label:
873 kfree(le->label);
874out_free_le:
875 kfree(le);
876 put_device(&gdev->dev);
877 return ret;
878}
879
Linus Walleij3c702e92015-10-21 15:29:53 +0200880/**
881 * gpio_ioctl() - ioctl handler for the GPIO chardev
882 */
883static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
884{
885 struct gpio_device *gdev = filp->private_data;
886 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200887 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200888
889 /* We fail any subsequent ioctl():s when the chip is gone */
890 if (!chip)
891 return -ENODEV;
892
Linus Walleij521a2ad2016-02-12 22:25:22 +0100893 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200894 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100895 struct gpiochip_info chipinfo;
896
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +0200897 memset(&chipinfo, 0, sizeof(chipinfo));
898
Linus Walleij3c702e92015-10-21 15:29:53 +0200899 strncpy(chipinfo.name, dev_name(&gdev->dev),
900 sizeof(chipinfo.name));
901 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100902 strncpy(chipinfo.label, gdev->label,
903 sizeof(chipinfo.label));
904 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100905 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200906 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
907 return -EFAULT;
908 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100909 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
910 struct gpioline_info lineinfo;
911 struct gpio_desc *desc;
912
913 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
914 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +0200915 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +0100916 return -EINVAL;
917
918 desc = &gdev->descs[lineinfo.line_offset];
919 if (desc->name) {
920 strncpy(lineinfo.name, desc->name,
921 sizeof(lineinfo.name));
922 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
923 } else {
924 lineinfo.name[0] = '\0';
925 }
926 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100927 strncpy(lineinfo.consumer, desc->label,
928 sizeof(lineinfo.consumer));
929 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100930 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100931 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100932 }
933
934 /*
935 * Userspace only need to know that the kernel is using
936 * this GPIO so it can't use it.
937 */
938 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100939 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
940 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
941 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
942 test_bit(FLAG_EXPORT, &desc->flags) ||
943 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100944 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100945 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100946 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100947 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100948 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100949 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100950 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100951 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100952 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
953
954 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
955 return -EFAULT;
956 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200957 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
958 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +0200959 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
960 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +0200961 }
962 return -EINVAL;
963}
964
Linus Walleij8b92e172016-05-27 14:24:04 +0200965#ifdef CONFIG_COMPAT
966static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
967 unsigned long arg)
968{
969 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
970}
971#endif
972
Linus Walleij3c702e92015-10-21 15:29:53 +0200973/**
974 * gpio_chrdev_open() - open the chardev for ioctl operations
975 * @inode: inode for this chardev
976 * @filp: file struct for storing private data
977 * Returns 0 on success
978 */
979static int gpio_chrdev_open(struct inode *inode, struct file *filp)
980{
981 struct gpio_device *gdev = container_of(inode->i_cdev,
982 struct gpio_device, chrdev);
983
984 /* Fail on open if the backing gpiochip is gone */
985 if (!gdev || !gdev->chip)
986 return -ENODEV;
987 get_device(&gdev->dev);
988 filp->private_data = gdev;
989 return 0;
990}
991
992/**
993 * gpio_chrdev_release() - close chardev after ioctl operations
994 * @inode: inode for this chardev
995 * @filp: file struct for storing private data
996 * Returns 0 on success
997 */
998static int gpio_chrdev_release(struct inode *inode, struct file *filp)
999{
1000 struct gpio_device *gdev = container_of(inode->i_cdev,
1001 struct gpio_device, chrdev);
1002
1003 if (!gdev)
1004 return -ENODEV;
1005 put_device(&gdev->dev);
1006 return 0;
1007}
1008
1009
1010static const struct file_operations gpio_fileops = {
1011 .release = gpio_chrdev_release,
1012 .open = gpio_chrdev_open,
1013 .owner = THIS_MODULE,
1014 .llseek = noop_llseek,
1015 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001016#ifdef CONFIG_COMPAT
1017 .compat_ioctl = gpio_ioctl_compat,
1018#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001019};
1020
Linus Walleijff2b1352015-10-20 11:10:38 +02001021static void gpiodevice_release(struct device *dev)
1022{
1023 struct gpio_device *gdev = dev_get_drvdata(dev);
1024
1025 list_del(&gdev->list);
1026 ida_simple_remove(&gpio_ida, gdev->id);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001027 kfree(gdev->label);
1028 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001029 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001030}
1031
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001032static int gpiochip_setup_dev(struct gpio_device *gdev)
1033{
1034 int status;
1035
1036 cdev_init(&gdev->chrdev, &gpio_fileops);
1037 gdev->chrdev.owner = THIS_MODULE;
1038 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
1039 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
1040 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
1041 if (status < 0)
1042 chip_warn(gdev->chip, "failed to add char device %d:%d\n",
1043 MAJOR(gpio_devt), gdev->id);
1044 else
1045 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1046 MAJOR(gpio_devt), gdev->id);
1047 status = device_add(&gdev->dev);
1048 if (status)
1049 goto err_remove_chardev;
1050
1051 status = gpiochip_sysfs_register(gdev);
1052 if (status)
1053 goto err_remove_device;
1054
1055 /* From this point, the .release() function cleans up gpio_device */
1056 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001057 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1058 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1059 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1060
1061 return 0;
1062
1063err_remove_device:
1064 device_del(&gdev->dev);
1065err_remove_chardev:
1066 cdev_del(&gdev->chrdev);
1067 return status;
1068}
1069
1070static void gpiochip_setup_devs(void)
1071{
1072 struct gpio_device *gdev;
1073 int err;
1074
1075 list_for_each_entry(gdev, &gpio_devices, list) {
1076 err = gpiochip_setup_dev(gdev);
1077 if (err)
1078 pr_err("%s: Failed to initialize gpio device (%d)\n",
1079 dev_name(&gdev->dev), err);
1080 }
1081}
1082
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001083/**
Linus Walleijb08ea352015-12-03 15:14:13 +01001084 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -08001085 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001086 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -08001087 *
1088 * Returns a negative errno if the chip can't be registered, such as
1089 * because the chip->base is invalid or already associated with a
1090 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001091 *
Linus Walleijb08ea352015-12-03 15:14:13 +01001092 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001093 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d2008-07-25 01:46:07 -07001094 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1095 * for GPIOs will fail rudely.
1096 *
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001097 * gpiochip_add_data() must only be called after gpiolib initialization,
1098 * ie after core_initcall().
1099 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001100 * If chip->base is negative, this requests dynamic assignment of
1101 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001102 */
Linus Walleijb08ea352015-12-03 15:14:13 +01001103int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -08001104{
1105 unsigned long flags;
1106 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001107 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001108 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001109 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001110
Linus Walleijff2b1352015-10-20 11:10:38 +02001111 /*
1112 * First: allocate and populate the internal stat container, and
1113 * set up the struct device.
1114 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001115 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001116 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001117 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001118 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001119 gdev->chip = chip;
1120 chip->gpiodev = gdev;
1121 if (chip->parent) {
1122 gdev->dev.parent = chip->parent;
1123 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001124 }
1125
Linus Walleijff2b1352015-10-20 11:10:38 +02001126#ifdef CONFIG_OF_GPIO
1127 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001128 if (chip->of_node)
1129 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001130#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001131
Linus Walleijff2b1352015-10-20 11:10:38 +02001132 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1133 if (gdev->id < 0) {
1134 status = gdev->id;
1135 goto err_free_gdev;
1136 }
1137 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1138 device_initialize(&gdev->dev);
1139 dev_set_drvdata(&gdev->dev, gdev);
1140 if (chip->parent && chip->parent->driver)
1141 gdev->owner = chip->parent->driver->owner;
1142 else if (chip->owner)
1143 /* TODO: remove chip->owner */
1144 gdev->owner = chip->owner;
1145 else
1146 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001147
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001148 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001149 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001150 status = -ENOMEM;
1151 goto err_free_gdev;
1152 }
1153
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001154 if (chip->ngpio == 0) {
1155 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001156 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001157 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001158 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001159
1160 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001161 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001162 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001163 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001164 if (!gdev->label) {
1165 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001166 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001167 }
1168
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001169 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001170 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001171
David Brownelld2876d02008-02-04 22:28:20 -08001172 spin_lock_irqsave(&gpio_lock, flags);
1173
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001174 /*
1175 * TODO: this allocates a Linux GPIO number base in the global
1176 * GPIO numberspace for this chip. In the long run we want to
1177 * get *rid* of this numberspace and use only descriptors, but
1178 * it may be a pipe dream. It will not happen before we get rid
1179 * of the sysfs interface anyways.
1180 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001181 if (base < 0) {
1182 base = gpiochip_find_base(chip->ngpio);
1183 if (base < 0) {
1184 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001185 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001186 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001187 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001188 /*
1189 * TODO: it should not be necessary to reflect the assigned
1190 * base outside of the GPIO subsystem. Go over drivers and
1191 * see if anyone makes use of this, else drop this and assign
1192 * a poison instead.
1193 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001194 chip->base = base;
1195 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001196 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001197
Linus Walleijff2b1352015-10-20 11:10:38 +02001198 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001199 if (status) {
1200 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001201 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001202 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001203
Linus Walleij545ebd92016-05-30 17:11:59 +02001204 spin_unlock_irqrestore(&gpio_lock, flags);
1205
Linus Walleijff2b1352015-10-20 11:10:38 +02001206 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001207 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d2008-07-25 01:46:07 -07001208
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001209 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +02001210 /*
1211 * REVISIT: most hardware initializes GPIOs as inputs
1212 * (often with pullups enabled) so power usage is
1213 * minimized. Linux code should set the gpio direction
1214 * first thing; but until it does, and in case
1215 * chip->get_direction is not set, we may expose the
1216 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001217 */
Linus Walleij72d32002016-04-28 13:33:59 +02001218
1219 if (chip->get_direction) {
1220 /*
1221 * If we have .get_direction, set up the initial
1222 * direction flag from the hardware.
1223 */
1224 int dir = chip->get_direction(chip, i);
1225
1226 if (!dir)
1227 set_bit(FLAG_IS_OUT, &desc->flags);
1228 } else if (!chip->direction_input) {
1229 /*
1230 * If the chip lacks the .direction_input callback
1231 * we logically assume all lines are outputs.
1232 */
1233 set_bit(FLAG_IS_OUT, &desc->flags);
1234 }
David Brownelld2876d02008-02-04 22:28:20 -08001235 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001236
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301237#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001238 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301239#endif
1240
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001241 status = gpiochip_set_desc_names(chip);
1242 if (status)
1243 goto err_remove_from_list;
1244
Mika Westerberg79b804c2016-09-20 15:15:21 +03001245 status = gpiochip_irqchip_init_valid_mask(chip);
1246 if (status)
1247 goto err_remove_from_list;
1248
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001249 status = of_gpiochip_add(chip);
1250 if (status)
1251 goto err_remove_chip;
1252
Mika Westerberg664e3e52014-01-08 12:40:54 +02001253 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001254
Linus Walleij3c702e92015-10-21 15:29:53 +02001255 /*
1256 * By first adding the chardev, and then adding the device,
1257 * we get a device node entry in sysfs under
1258 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1259 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001260 * We can do this only if gpiolib has been initialized.
1261 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001262 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001263 if (gpiolib_initialized) {
1264 status = gpiochip_setup_dev(gdev);
1265 if (status)
1266 goto err_remove_chip;
1267 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001268 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001269
Johan Hovold225fce82015-01-12 17:12:25 +01001270err_remove_chip:
1271 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001272 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001273 of_gpiochip_remove(chip);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001274 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001275err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001276 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001277 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001278 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001279err_free_label:
1280 kfree(gdev->label);
1281err_free_descs:
1282 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001283err_free_gdev:
1284 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001285 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001286 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001287 gdev->base, gdev->base + gdev->ngpio - 1,
1288 chip->label ? : "generic");
1289 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001290 return status;
1291}
Linus Walleijb08ea352015-12-03 15:14:13 +01001292EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -08001293
1294/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001295 * gpiochip_get_data() - get per-subdriver data for the chip
1296 */
1297void *gpiochip_get_data(struct gpio_chip *chip)
1298{
1299 return chip->gpiodev->data;
1300}
1301EXPORT_SYMBOL_GPL(gpiochip_get_data);
1302
1303/**
David Brownelld2876d02008-02-04 22:28:20 -08001304 * gpiochip_remove() - unregister a gpio_chip
1305 * @chip: the chip to unregister
1306 *
1307 * A gpio_chip with any GPIOs still requested may not be removed.
1308 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001309void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001310{
Linus Walleijff2b1352015-10-20 11:10:38 +02001311 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001312 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001313 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001314 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001315 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001316
Linus Walleijff2b1352015-10-20 11:10:38 +02001317 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001318 gpiochip_sysfs_unregister(gdev);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001319 /* Numb the device, cancelling all outstanding operations */
1320 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001321 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001322 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001323 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -06001324 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001325 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001326 /*
1327 * We accept no more calls into the driver from this point, so
1328 * NULL the driver data pointer
1329 */
1330 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001331
Johan Hovold6798aca2015-01-12 17:12:28 +01001332 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001333 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001334 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001335 if (test_bit(FLAG_REQUESTED, &desc->flags))
1336 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001337 }
David Brownelld2876d02008-02-04 22:28:20 -08001338 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001339
Johan Hovoldfab28b82015-05-04 17:10:27 +02001340 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001341 dev_crit(&gdev->dev,
Linus Walleij58383c72015-11-04 09:56:26 +01001342 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001343
Linus Walleijff2b1352015-10-20 11:10:38 +02001344 /*
1345 * The gpiochip side puts its use of the device to rest here:
1346 * if there are no userspace clients, the chardev and device will
1347 * be removed, else it will be dangling until the last user is
1348 * gone.
1349 */
Ricardo Ribalda Delgadof4833b82016-06-03 19:10:02 +02001350 cdev_del(&gdev->chrdev);
1351 device_del(&gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001352 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001353}
1354EXPORT_SYMBOL_GPL(gpiochip_remove);
1355
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301356static void devm_gpio_chip_release(struct device *dev, void *res)
1357{
1358 struct gpio_chip *chip = *(struct gpio_chip **)res;
1359
1360 gpiochip_remove(chip);
1361}
1362
1363static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1364
1365{
1366 struct gpio_chip **r = res;
1367
1368 if (!r || !*r) {
1369 WARN_ON(!r || !*r);
1370 return 0;
1371 }
1372
1373 return *r == data;
1374}
1375
1376/**
1377 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1378 * @dev: the device pointer on which irq_chip belongs to.
1379 * @chip: the chip to register, with chip->base initialized
1380 * Context: potentially before irqs will work
1381 *
1382 * Returns a negative errno if the chip can't be registered, such as
1383 * because the chip->base is invalid or already associated with a
1384 * different chip. Otherwise it returns zero as a success code.
1385 *
1386 * The gpio chip automatically be released when the device is unbound.
1387 */
1388int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1389 void *data)
1390{
1391 struct gpio_chip **ptr;
1392 int ret;
1393
1394 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1395 GFP_KERNEL);
1396 if (!ptr)
1397 return -ENOMEM;
1398
1399 ret = gpiochip_add_data(chip, data);
1400 if (ret < 0) {
1401 devres_free(ptr);
1402 return ret;
1403 }
1404
1405 *ptr = chip;
1406 devres_add(dev, ptr);
1407
1408 return 0;
1409}
1410EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1411
1412/**
1413 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1414 * @dev: device for which which resource was allocated
1415 * @chip: the chip to remove
1416 *
1417 * A gpio_chip with any GPIOs still requested may not be removed.
1418 */
1419void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1420{
1421 int ret;
1422
1423 ret = devres_release(dev, devm_gpio_chip_release,
1424 devm_gpio_chip_match, chip);
1425 if (!ret)
1426 WARN_ON(ret);
1427}
1428EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1429
Grant Likely594fa262010-06-08 07:48:16 -06001430/**
1431 * gpiochip_find() - iterator for locating a specific gpio_chip
1432 * @data: data to pass to match function
1433 * @callback: Callback function to check gpio_chip
1434 *
1435 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1436 * determined by a user supplied @match callback. The callback should return
1437 * 0 if the device doesn't match and non-zero if it does. If the callback is
1438 * non-zero, this function will return to the caller and not iterate over any
1439 * more gpio_chips.
1440 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001441struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001442 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001443 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001444{
Linus Walleijff2b1352015-10-20 11:10:38 +02001445 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001446 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001447 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001448
1449 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001450 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001451 if (gdev->chip && match(gdev->chip, data)) {
1452 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001453 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001454 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001455
Grant Likely594fa262010-06-08 07:48:16 -06001456 spin_unlock_irqrestore(&gpio_lock, flags);
1457
1458 return chip;
1459}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001460EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001461
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001462static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1463{
1464 const char *name = data;
1465
1466 return !strcmp(chip->label, name);
1467}
1468
1469static struct gpio_chip *find_chip_by_name(const char *name)
1470{
1471 return gpiochip_find((void *)name, gpiochip_match_name);
1472}
1473
Linus Walleij14250522014-03-25 10:40:18 +01001474#ifdef CONFIG_GPIOLIB_IRQCHIP
1475
1476/*
1477 * The following is irqchip helper code for gpiochips.
1478 */
1479
Mika Westerberg79b804c2016-09-20 15:15:21 +03001480static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1481{
1482 int i;
1483
1484 if (!gpiochip->irq_need_valid_mask)
1485 return 0;
1486
1487 gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
1488 sizeof(long), GFP_KERNEL);
1489 if (!gpiochip->irq_valid_mask)
1490 return -ENOMEM;
1491
1492 /* Assume by default all GPIOs are valid */
1493 for (i = 0; i < gpiochip->ngpio; i++)
1494 set_bit(i, gpiochip->irq_valid_mask);
1495
1496 return 0;
1497}
1498
1499static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1500{
1501 kfree(gpiochip->irq_valid_mask);
1502 gpiochip->irq_valid_mask = NULL;
1503}
1504
1505static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1506 unsigned int offset)
1507{
1508 /* No mask means all valid */
1509 if (likely(!gpiochip->irq_valid_mask))
1510 return true;
1511 return test_bit(offset, gpiochip->irq_valid_mask);
1512}
1513
Linus Walleij14250522014-03-25 10:40:18 +01001514/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001515 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
1516 * @gpiochip: the gpiochip to set the irqchip chain to
1517 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001518 * @parent_irq: the irq number corresponding to the parent IRQ for this
1519 * chained irqchip
1520 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001521 * coming out of the gpiochip. If the interrupt is nested rather than
1522 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001523 */
1524void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1525 struct irq_chip *irqchip,
1526 int parent_irq,
1527 irq_flow_handler_t parent_handler)
1528{
Linus Walleij83141a72014-09-26 13:50:12 +02001529 unsigned int offset;
1530
Linus Walleij83141a72014-09-26 13:50:12 +02001531 if (!gpiochip->irqdomain) {
1532 chip_err(gpiochip, "called %s before setting up irqchip\n",
1533 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001534 return;
1535 }
1536
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001537 if (parent_handler) {
1538 if (gpiochip->can_sleep) {
1539 chip_err(gpiochip,
1540 "you cannot have chained interrupts on a "
1541 "chip that may sleep\n");
1542 return;
1543 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001544 /*
1545 * The parent irqchip is already using the chip_data for this
1546 * irqchip, so our callbacks simply use the handler_data.
1547 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001548 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1549 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001550
1551 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001552 }
Linus Walleij83141a72014-09-26 13:50:12 +02001553
1554 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001555 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1556 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1557 continue;
Linus Walleij83141a72014-09-26 13:50:12 +02001558 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
1559 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001560 }
Linus Walleij14250522014-03-25 10:40:18 +01001561}
1562EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1563
1564/**
1565 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1566 * @d: the irqdomain used by this irqchip
1567 * @irq: the global irq number used by this GPIO irqchip irq
1568 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1569 *
1570 * This function will set up the mapping for a certain IRQ line on a
1571 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1572 * stored inside the gpiochip.
1573 */
1574static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1575 irq_hw_number_t hwirq)
1576{
1577 struct gpio_chip *chip = d->host_data;
1578
Linus Walleij14250522014-03-25 10:40:18 +01001579 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001580 /*
1581 * This lock class tells lockdep that GPIO irqs are in a different
1582 * category than their parents, so it won't report false recursion.
1583 */
1584 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +02001585 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001586 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +03001587 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001588 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001589 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001590
Linus Walleij1333b902014-04-23 16:45:12 +02001591 /*
1592 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1593 * is passed as default type.
1594 */
1595 if (chip->irq_default_type != IRQ_TYPE_NONE)
1596 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001597
1598 return 0;
1599}
1600
Linus Walleijc3626fd2014-03-28 20:42:01 +01001601static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1602{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001603 struct gpio_chip *chip = d->host_data;
1604
Linus Walleij1c8732b2014-04-09 13:34:39 +02001605 if (chip->can_sleep)
1606 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001607 irq_set_chip_and_handler(irq, NULL, NULL);
1608 irq_set_chip_data(irq, NULL);
1609}
1610
Linus Walleij14250522014-03-25 10:40:18 +01001611static const struct irq_domain_ops gpiochip_domain_ops = {
1612 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001613 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001614 /* Virtually all GPIO irqchips are twocell:ed */
1615 .xlate = irq_domain_xlate_twocell,
1616};
1617
1618static int gpiochip_irq_reqres(struct irq_data *d)
1619{
1620 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1621
Linus Walleijff2b1352015-10-20 11:10:38 +02001622 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001623 return -ENODEV;
1624
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001625 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001626 chip_err(chip,
1627 "unable to lock HW IRQ %lu for IRQ\n",
1628 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001629 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001630 return -EINVAL;
1631 }
1632 return 0;
1633}
1634
1635static void gpiochip_irq_relres(struct irq_data *d)
1636{
1637 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1638
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001639 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001640 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001641}
1642
1643static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1644{
1645 return irq_find_mapping(chip->irqdomain, offset);
1646}
1647
1648/**
1649 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1650 * @gpiochip: the gpiochip to remove the irqchip from
1651 *
1652 * This is called only from gpiochip_remove()
1653 */
1654static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1655{
Linus Walleijc3626fd2014-03-28 20:42:01 +01001656 unsigned int offset;
1657
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001658 acpi_gpiochip_free_interrupts(gpiochip);
1659
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001660 if (gpiochip->irq_parent) {
1661 irq_set_chained_handler(gpiochip->irq_parent, NULL);
1662 irq_set_handler_data(gpiochip->irq_parent, NULL);
1663 }
1664
Linus Walleijc3626fd2014-03-28 20:42:01 +01001665 /* Remove all IRQ mappings and delete the domain */
1666 if (gpiochip->irqdomain) {
Mika Westerberg79b804c2016-09-20 15:15:21 +03001667 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1668 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1669 continue;
Grygorii Strashkoe3893382014-09-25 19:09:23 +03001670 irq_dispose_mapping(
1671 irq_find_mapping(gpiochip->irqdomain, offset));
Mika Westerberg79b804c2016-09-20 15:15:21 +03001672 }
Linus Walleij14250522014-03-25 10:40:18 +01001673 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001674 }
Linus Walleij14250522014-03-25 10:40:18 +01001675
1676 if (gpiochip->irqchip) {
1677 gpiochip->irqchip->irq_request_resources = NULL;
1678 gpiochip->irqchip->irq_release_resources = NULL;
1679 gpiochip->irqchip = NULL;
1680 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001681
1682 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001683}
1684
1685/**
1686 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
1687 * @gpiochip: the gpiochip to add the irqchip to
1688 * @irqchip: the irqchip to add to the gpiochip
1689 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1690 * allocate gpiochip irqs from
1691 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001692 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1693 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001694 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001695 *
1696 * This function closely associates a certain irqchip with a certain
1697 * gpiochip, providing an irq domain to translate the local IRQs to
1698 * global irqs in the gpiolib core, and making sure that the gpiochip
1699 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001700 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001701 * from the gpiochip passed as chip data. An irqdomain will be stored
1702 * in the gpiochip that shall be used by the driver to handle IRQ number
1703 * translation. The gpiochip will need to be initialized and registered
1704 * before calling this function.
1705 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001706 * This function will handle two cell:ed simple IRQs and assumes all
1707 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001708 * need to be open coded.
1709 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001710int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
1711 struct irq_chip *irqchip,
1712 unsigned int first_irq,
1713 irq_flow_handler_t handler,
1714 unsigned int type,
1715 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001716{
1717 struct device_node *of_node;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001718 bool irq_base_set = false;
Linus Walleij14250522014-03-25 10:40:18 +01001719 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001720 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001721
1722 if (!gpiochip || !irqchip)
1723 return -EINVAL;
1724
Linus Walleij58383c72015-11-04 09:56:26 +01001725 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001726 pr_err("missing gpiochip .dev parent pointer\n");
1727 return -EINVAL;
1728 }
Linus Walleij58383c72015-11-04 09:56:26 +01001729 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001730#ifdef CONFIG_OF_GPIO
1731 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001732 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001733 * FIXME: get rid of this and use gpiochip->parent->of_node
1734 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001735 */
1736 if (gpiochip->of_node)
1737 of_node = gpiochip->of_node;
1738#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001739 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001740 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001741 * used to configure the interrupts, as you may end-up with
1742 * conflicting triggers. Tell the user, and reset to NONE.
1743 */
1744 if (WARN(of_node && type != IRQ_TYPE_NONE,
1745 "%s: Ignoring %d default trigger\n", of_node->full_name, type))
1746 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001747 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1748 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1749 "Ignoring %d default trigger\n", type);
1750 type = IRQ_TYPE_NONE;
1751 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001752
Linus Walleij14250522014-03-25 10:40:18 +01001753 gpiochip->irqchip = irqchip;
1754 gpiochip->irq_handler = handler;
1755 gpiochip->irq_default_type = type;
1756 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001757 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +01001758 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1759 gpiochip->ngpio, first_irq,
1760 &gpiochip_domain_ops, gpiochip);
1761 if (!gpiochip->irqdomain) {
1762 gpiochip->irqchip = NULL;
1763 return -EINVAL;
1764 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001765
1766 /*
1767 * It is possible for a driver to override this, but only if the
1768 * alternative functions are both implemented.
1769 */
1770 if (!irqchip->irq_request_resources &&
1771 !irqchip->irq_release_resources) {
1772 irqchip->irq_request_resources = gpiochip_irq_reqres;
1773 irqchip->irq_release_resources = gpiochip_irq_relres;
1774 }
Linus Walleij14250522014-03-25 10:40:18 +01001775
1776 /*
1777 * Prepare the mapping since the irqchip shall be orthogonal to
1778 * any gpiochip calls. If the first_irq was zero, this is
1779 * necessary to allocate descriptors for all IRQs.
1780 */
Linus Walleijc3626fd2014-03-28 20:42:01 +01001781 for (offset = 0; offset < gpiochip->ngpio; offset++) {
Mika Westerberg79b804c2016-09-20 15:15:21 +03001782 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1783 continue;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001784 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001785 if (!irq_base_set) {
Linus Walleijc3626fd2014-03-28 20:42:01 +01001786 /*
1787 * Store the base into the gpiochip to be used when
1788 * unmapping the irqs.
1789 */
1790 gpiochip->irq_base = irq_base;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001791 irq_base_set = true;
1792 }
Linus Walleijc3626fd2014-03-28 20:42:01 +01001793 }
Linus Walleij14250522014-03-25 10:40:18 +01001794
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001795 acpi_gpiochip_request_interrupts(gpiochip);
1796
Linus Walleij14250522014-03-25 10:40:18 +01001797 return 0;
1798}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001799EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +01001800
1801#else /* CONFIG_GPIOLIB_IRQCHIP */
1802
1803static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03001804static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1805{
1806 return 0;
1807}
1808static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1809{ }
Linus Walleij14250522014-03-25 10:40:18 +01001810
1811#endif /* CONFIG_GPIOLIB_IRQCHIP */
1812
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001813/**
1814 * gpiochip_generic_request() - request the gpio function for a pin
1815 * @chip: the gpiochip owning the GPIO
1816 * @offset: the offset of the GPIO to request for GPIO function
1817 */
1818int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1819{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001820 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001821}
1822EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1823
1824/**
1825 * gpiochip_generic_free() - free the gpio function from a pin
1826 * @chip: the gpiochip to request the gpio function for
1827 * @offset: the offset of the GPIO to free from GPIO function
1828 */
1829void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1830{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001831 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001832}
1833EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1834
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301835#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001836
Linus Walleij3f0f8672012-11-20 12:40:15 +01001837/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001838 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1839 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001840 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001841 * @gpio_offset: the start offset in the current gpio_chip number space
1842 * @pin_group: name of the pin group inside the pin controller
1843 */
1844int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1845 struct pinctrl_dev *pctldev,
1846 unsigned int gpio_offset, const char *pin_group)
1847{
1848 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001849 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001850 int ret;
1851
1852 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1853 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001854 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001855 return -ENOMEM;
1856 }
1857
1858 /* Use local offset as range ID */
1859 pin_range->range.id = gpio_offset;
1860 pin_range->range.gc = chip;
1861 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001862 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001863 pin_range->pctldev = pctldev;
1864
1865 ret = pinctrl_get_group_pins(pctldev, pin_group,
1866 &pin_range->range.pins,
1867 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001868 if (ret < 0) {
1869 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001870 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001871 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001872
1873 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1874
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001875 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1876 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001877 pinctrl_dev_get_devname(pctldev), pin_group);
1878
Linus Walleij20ec3e32016-02-11 11:03:06 +01001879 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001880
1881 return 0;
1882}
1883EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1884
1885/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001886 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1887 * @chip: the gpiochip to add the range for
1888 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001889 * @gpio_offset: the start offset in the current gpio_chip number space
1890 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001891 * @npins: the number of pins from the offset of each pin space (GPIO and
1892 * pin controller) to accumulate in this range
1893 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001894int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001895 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001896 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301897{
1898 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001899 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001900 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301901
Linus Walleij3f0f8672012-11-20 12:40:15 +01001902 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301903 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001904 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001905 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301906 }
1907
Linus Walleij3f0f8672012-11-20 12:40:15 +01001908 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001909 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001910 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301911 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001912 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001913 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301914 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001915 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301916 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001917 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001918 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001919 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001920 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001921 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001922 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001923 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1924 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001925 pinctl_name,
1926 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301927
Linus Walleij20ec3e32016-02-11 11:03:06 +01001928 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001929
1930 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301931}
Linus Walleij165adc92012-11-06 14:49:39 +01001932EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301933
Linus Walleij3f0f8672012-11-20 12:40:15 +01001934/**
1935 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1936 * @chip: the chip to remove all the mappings for
1937 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301938void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1939{
1940 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01001941 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301942
Linus Walleij20ec3e32016-02-11 11:03:06 +01001943 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301944 list_del(&pin_range->node);
1945 pinctrl_remove_gpio_range(pin_range->pctldev,
1946 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001947 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301948 }
1949}
Linus Walleij165adc92012-11-06 14:49:39 +01001950EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1951
1952#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301953
David Brownelld2876d02008-02-04 22:28:20 -08001954/* These "optional" allocation calls help prevent drivers from stomping
1955 * on each other, and help provide better diagnostics in debugfs.
1956 * They're called even less than the "set direction" calls.
1957 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001958static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001959{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001960 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001961 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001962 unsigned long flags;
1963
1964 spin_lock_irqsave(&gpio_lock, flags);
1965
David Brownelld2876d02008-02-04 22:28:20 -08001966 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001967 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001968 */
1969
1970 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1971 desc_set_label(desc, label ? : "?");
1972 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001973 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001974 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001975 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001976 }
1977
1978 if (chip->request) {
1979 /* chip->request may sleep */
1980 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001981 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001982 spin_lock_irqsave(&gpio_lock, flags);
1983
1984 if (status < 0) {
1985 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001986 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001987 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001988 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001989 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001990 if (chip->get_direction) {
1991 /* chip->get_direction may sleep */
1992 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001993 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001994 spin_lock_irqsave(&gpio_lock, flags);
1995 }
David Brownelld2876d02008-02-04 22:28:20 -08001996done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02001997 spin_unlock_irqrestore(&gpio_lock, flags);
1998 return status;
1999}
2000
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002001/*
2002 * This descriptor validation needs to be inserted verbatim into each
2003 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002004 * macro to avoid endless duplication. If the desc is NULL it is an
2005 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002006 */
2007#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002008 if (!desc) \
2009 return 0; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002010 if (IS_ERR(desc)) { \
2011 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2012 return PTR_ERR(desc); \
2013 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002014 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002015 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002016 return -EINVAL; \
2017 } \
2018 if ( !desc->gdev->chip ) { \
2019 dev_warn(&desc->gdev->dev, \
2020 "%s: backing chip is gone\n", __func__); \
2021 return 0; \
2022 } } while (0)
2023
2024#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002025 if (!desc) \
2026 return; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002027 if (IS_ERR(desc)) { \
2028 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2029 return; \
2030 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002031 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002032 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002033 return; \
2034 } \
2035 if (!desc->gdev->chip) { \
2036 dev_warn(&desc->gdev->dev, \
2037 "%s: backing chip is gone\n", __func__); \
2038 return; \
2039 } } while (0)
2040
2041
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002042int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002043{
2044 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002045 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002046
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002047 VALIDATE_DESC(desc);
2048 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002049
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002050 if (try_module_get(gdev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002051 status = __gpiod_request(desc, label);
2052 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002053 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002054 else
2055 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002056 }
2057
David Brownelld2876d02008-02-04 22:28:20 -08002058 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002059 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002060
David Brownelld2876d02008-02-04 22:28:20 -08002061 return status;
2062}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002063
Mika Westerberg77c2d792014-03-10 14:54:50 +02002064static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002065{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002066 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002067 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002068 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002069
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002070 might_sleep();
2071
Alexandre Courbot372e7222013-02-03 01:29:29 +09002072 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07002073
David Brownelld2876d02008-02-04 22:28:20 -08002074 spin_lock_irqsave(&gpio_lock, flags);
2075
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002076 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002077 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2078 if (chip->free) {
2079 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002080 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002081 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002082 spin_lock_irqsave(&gpio_lock, flags);
2083 }
David Brownelld2876d02008-02-04 22:28:20 -08002084 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002085 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002086 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302087 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302088 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002089 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002090 ret = true;
2091 }
David Brownelld2876d02008-02-04 22:28:20 -08002092
2093 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002094 return ret;
2095}
2096
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002097void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002098{
Linus Walleij33a68e82016-02-11 10:28:44 +01002099 if (desc && desc->gdev && __gpiod_free(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002100 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002101 put_device(&desc->gdev->dev);
2102 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002103 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002104 }
David Brownelld2876d02008-02-04 22:28:20 -08002105}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002106
David Brownelld2876d02008-02-04 22:28:20 -08002107/**
2108 * gpiochip_is_requested - return string iff signal was requested
2109 * @chip: controller managing the signal
2110 * @offset: of signal within controller's 0..(ngpio - 1) range
2111 *
2112 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002113 * The string returned is the label passed to gpio_request(); if none has been
2114 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002115 *
2116 * This function is for use by GPIO controller drivers. The label can
2117 * help with diagnostics, and knowing that the signal is used as a GPIO
2118 * can help avoid accidentally multiplexing it to another controller.
2119 */
2120const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2121{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002122 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002123
Dirk Behme48b59532015-08-18 18:02:32 +02002124 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002125 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002126
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002127 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002128
Alexandre Courbot372e7222013-02-03 01:29:29 +09002129 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002130 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002131 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002132}
2133EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2134
Mika Westerberg77c2d792014-03-10 14:54:50 +02002135/**
2136 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2137 * @desc: GPIO descriptor to request
2138 * @label: label for the GPIO
2139 *
2140 * Function allows GPIO chip drivers to request and use their own GPIO
2141 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2142 * function will not increase reference count of the GPIO chip module. This
2143 * allows the GPIO chip module to be unloaded as needed (we assume that the
2144 * GPIO chip driver handles freeing the GPIOs it has requested).
2145 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002146struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2147 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002148{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002149 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2150 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002151
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002152 if (IS_ERR(desc)) {
2153 chip_err(chip, "failed to get GPIO descriptor\n");
2154 return desc;
2155 }
2156
2157 err = __gpiod_request(desc, label);
2158 if (err < 0)
2159 return ERR_PTR(err);
2160
2161 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002162}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002163EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002164
2165/**
2166 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2167 * @desc: GPIO descriptor to free
2168 *
2169 * Function frees the given GPIO requested previously with
2170 * gpiochip_request_own_desc().
2171 */
2172void gpiochip_free_own_desc(struct gpio_desc *desc)
2173{
2174 if (desc)
2175 __gpiod_free(desc);
2176}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002177EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002178
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002179/*
2180 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002181 * some cases this is done in early boot, before IRQs are enabled.
2182 *
2183 * As a rule these aren't called more than once (except for drivers
2184 * using the open-drain emulation idiom) so these are natural places
2185 * to accumulate extra debugging checks. Note that we can't (yet)
2186 * rely on gpio_request() having been called beforehand.
2187 */
2188
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002189/**
2190 * gpiod_direction_input - set the GPIO direction to input
2191 * @desc: GPIO to set to input
2192 *
2193 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2194 * be called safely on it.
2195 *
2196 * Return 0 in case of success, else an error code.
2197 */
2198int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002199{
David Brownelld2876d02008-02-04 22:28:20 -08002200 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002201 int status = -EINVAL;
2202
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002203 VALIDATE_DESC(desc);
2204 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002205
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002206 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002207 gpiod_warn(desc,
2208 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002209 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002210 return -EIO;
2211 }
2212
Alexandre Courbotd82da792014-07-22 16:17:43 +09002213 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002214 if (status == 0)
2215 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002216
Alexandre Courbot372e7222013-02-03 01:29:29 +09002217 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002218
David Brownelld2876d02008-02-04 22:28:20 -08002219 return status;
2220}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002221EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002222
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002223static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002224{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002225 struct gpio_chip *gc = desc->gdev->chip;
2226 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002227
Linus Walleijd468bf92013-09-24 11:54:38 +02002228 /* GPIOs used for IRQs shall not be set as output */
2229 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2230 gpiod_err(desc,
2231 "%s: tried to set a GPIO tied to an IRQ as output\n",
2232 __func__);
2233 return -EIO;
2234 }
2235
Linus Walleijc663e5f2016-03-22 10:51:16 +01002236 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2237 /* First see if we can enable open drain in hardware */
2238 if (gc->set_single_ended) {
2239 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2240 LINE_MODE_OPEN_DRAIN);
2241 if (!ret)
2242 goto set_output_value;
2243 }
2244 /* Emulate open drain by not actively driving the line high */
2245 if (value)
2246 return gpiod_direction_input(desc);
2247 }
2248 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2249 if (gc->set_single_ended) {
2250 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2251 LINE_MODE_OPEN_SOURCE);
2252 if (!ret)
2253 goto set_output_value;
2254 }
2255 /* Emulate open source by not actively driving the line low */
2256 if (!value)
2257 return gpiod_direction_input(desc);
2258 } else {
2259 /* Make sure to disable open drain/source hardware, if any */
2260 if (gc->set_single_ended)
2261 gc->set_single_ended(gc,
2262 gpio_chip_hwgpio(desc),
2263 LINE_MODE_PUSH_PULL);
2264 }
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302265
Linus Walleijc663e5f2016-03-22 10:51:16 +01002266set_output_value:
2267 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002268 gpiod_warn(desc,
2269 "%s: missing set() or direction_output() operations\n",
2270 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002271 return -EIO;
2272 }
2273
Linus Walleijc663e5f2016-03-22 10:51:16 +01002274 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), value);
2275 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002276 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002277 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002278 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2279 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002280}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002281
2282/**
2283 * gpiod_direction_output_raw - set the GPIO direction to output
2284 * @desc: GPIO to set to output
2285 * @value: initial output value of the GPIO
2286 *
2287 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2288 * be called safely on it. The initial value of the output must be specified
2289 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2290 *
2291 * Return 0 in case of success, else an error code.
2292 */
2293int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2294{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002295 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002296 return _gpiod_direction_output_raw(desc, value);
2297}
2298EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2299
2300/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302301 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002302 * @desc: GPIO to set to output
2303 * @value: initial output value of the GPIO
2304 *
2305 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2306 * be called safely on it. The initial value of the output must be specified
2307 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2308 * account.
2309 *
2310 * Return 0 in case of success, else an error code.
2311 */
2312int gpiod_direction_output(struct gpio_desc *desc, int value)
2313{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002314 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002315 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2316 value = !value;
2317 return _gpiod_direction_output_raw(desc, value);
2318}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002319EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002320
Felipe Balbic4b5be92010-05-26 14:42:23 -07002321/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002322 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07002323 * @gpio: the gpio to set debounce time
2324 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002325 *
2326 * returns -ENOTSUPP if the controller does not support setting
2327 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002328 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002329int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002330{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002331 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002332
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002333 VALIDATE_DESC(desc);
2334 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002335 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01002336 gpiod_dbg(desc,
2337 "%s: missing set() or set_debounce() operations\n",
2338 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002339 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002340 }
2341
Alexandre Courbotd82da792014-07-22 16:17:43 +09002342 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002343}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002344EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002345
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002346/**
2347 * gpiod_is_active_low - test whether a GPIO is active-low or not
2348 * @desc: the gpio descriptor to test
2349 *
2350 * Returns 1 if the GPIO is active-low, 0 otherwise.
2351 */
2352int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002353{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002354 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002355 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002356}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002357EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002358
2359/* I/O calls are only valid after configuration completed; the relevant
2360 * "is this a valid GPIO" error checks should already have been done.
2361 *
2362 * "Get" operations are often inlinable as reading a pin value register,
2363 * and masking the relevant bit in that register.
2364 *
2365 * When "set" operations are inlinable, they involve writing that mask to
2366 * one register to set a low value, or a different register to set it high.
2367 * Otherwise locking is needed, so there may be little value to inlining.
2368 *
2369 *------------------------------------------------------------------------
2370 *
2371 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2372 * have requested the GPIO. That can include implicit requesting by
2373 * a direction setting call. Marking a gpio as requested locks its chip
2374 * in memory, guaranteeing that these table lookups need no more locking
2375 * and that gpiochip_remove() will fail.
2376 *
2377 * REVISIT when debugging, consider adding some instrumentation to ensure
2378 * that the GPIO was actually requested.
2379 */
2380
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002381static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002382{
2383 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002384 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002385 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002386
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002387 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002388 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002389 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002390 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002391 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002392 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002393}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002394
David Brownelld2876d02008-02-04 22:28:20 -08002395/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002396 * gpiod_get_raw_value() - return a gpio's raw value
2397 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002398 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002399 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002400 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002401 *
2402 * This function should be called from contexts where we cannot sleep, and will
2403 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002404 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002405int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002406{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002407 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002408 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002409 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002410 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002411}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002412EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002413
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002414/**
2415 * gpiod_get_value() - return a gpio's value
2416 * @desc: gpio whose value will be returned
2417 *
2418 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002419 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002420 *
2421 * This function should be called from contexts where we cannot sleep, and will
2422 * complain if the GPIO chip functions potentially sleep.
2423 */
2424int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002425{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002426 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002427
2428 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002429 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002430 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002431
2432 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002433 if (value < 0)
2434 return value;
2435
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002436 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2437 value = !value;
2438
2439 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002440}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002441EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002442
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302443/*
2444 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002445 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002446 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302447 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002448static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302449{
2450 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002451 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002452 int offset = gpio_chip_hwgpio(desc);
2453
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302454 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002455 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302456 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002457 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302458 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002459 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302460 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002461 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302462 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002463 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302464 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002465 gpiod_err(desc,
2466 "%s: Error in set_value for open drain err %d\n",
2467 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302468}
2469
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302470/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002471 * _gpio_set_open_source_value() - Set the open source gpio's value.
2472 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002473 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302474 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002475static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302476{
2477 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002478 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002479 int offset = gpio_chip_hwgpio(desc);
2480
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302481 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002482 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302483 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002484 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302485 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002486 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302487 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002488 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302489 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002490 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302491 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002492 gpiod_err(desc,
2493 "%s: Error in set_value for open source err %d\n",
2494 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302495}
2496
Alexandre Courbot23600962014-03-11 15:52:09 +09002497static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002498{
2499 struct gpio_chip *chip;
2500
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002501 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002502 trace_gpio_value(desc_to_gpio(desc), 0, value);
2503 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2504 _gpio_set_open_drain_value(desc, value);
2505 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2506 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302507 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002508 chip->set(chip, gpio_chip_hwgpio(desc), value);
2509}
2510
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002511/*
2512 * set multiple outputs on the same chip;
2513 * use the chip's set_multiple function if available;
2514 * otherwise set the outputs sequentially;
2515 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2516 * defines which outputs are to be changed
2517 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2518 * defines the values the outputs specified by mask are to be set to
2519 */
2520static void gpio_chip_set_multiple(struct gpio_chip *chip,
2521 unsigned long *mask, unsigned long *bits)
2522{
2523 if (chip->set_multiple) {
2524 chip->set_multiple(chip, mask, bits);
2525 } else {
2526 int i;
2527 for (i = 0; i < chip->ngpio; i++) {
2528 if (mask[BIT_WORD(i)] == 0) {
2529 /* no more set bits in this mask word;
2530 * skip ahead to the next word */
2531 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
2532 continue;
2533 }
2534 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002535 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002536 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002537 }
2538 }
2539}
2540
Linus Walleij44c7288f2016-04-24 11:36:59 +02002541void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2542 unsigned int array_size,
2543 struct gpio_desc **desc_array,
2544 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002545{
2546 int i = 0;
2547
2548 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002549 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002550 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2551 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2552 int count = 0;
2553
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002554 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002555 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002556
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002557 memset(mask, 0, sizeof(mask));
2558 do {
2559 struct gpio_desc *desc = desc_array[i];
2560 int hwgpio = gpio_chip_hwgpio(desc);
2561 int value = value_array[i];
2562
2563 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2564 value = !value;
2565 trace_gpio_value(desc_to_gpio(desc), 0, value);
2566 /*
2567 * collect all normal outputs belonging to the same chip
2568 * open drain and open source outputs are set individually
2569 */
2570 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002571 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002572 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2573 _gpio_set_open_source_value(desc, value);
2574 } else {
2575 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002576 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002577 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002578 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002579 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002580 count++;
2581 }
2582 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002583 } while ((i < array_size) &&
2584 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002585 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002586 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002587 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002588 }
2589}
2590
David Brownelld2876d02008-02-04 22:28:20 -08002591/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002592 * gpiod_set_raw_value() - assign a gpio's raw value
2593 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002594 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002595 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002596 * Set the raw value of the GPIO, i.e. the value of its physical line without
2597 * regard for its ACTIVE_LOW status.
2598 *
2599 * This function should be called from contexts where we cannot sleep, and will
2600 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002601 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002602void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002603{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002604 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002605 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002606 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002607 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002608}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002609EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002610
2611/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002612 * gpiod_set_value() - assign a gpio's value
2613 * @desc: gpio whose value will be assigned
2614 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002615 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002616 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2617 * account
2618 *
2619 * This function should be called from contexts where we cannot sleep, and will
2620 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002621 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002622void gpiod_set_value(struct gpio_desc *desc, int value)
2623{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002624 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002625 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002626 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002627 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2628 value = !value;
2629 _gpiod_set_raw_value(desc, value);
2630}
2631EXPORT_SYMBOL_GPL(gpiod_set_value);
2632
2633/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002634 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002635 * @array_size: number of elements in the descriptor / value arrays
2636 * @desc_array: array of GPIO descriptors whose values will be assigned
2637 * @value_array: array of values to assign
2638 *
2639 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2640 * without regard for their ACTIVE_LOW status.
2641 *
2642 * This function should be called from contexts where we cannot sleep, and will
2643 * complain if the GPIO chip functions potentially sleep.
2644 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002645void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002646 struct gpio_desc **desc_array, int *value_array)
2647{
2648 if (!desc_array)
2649 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002650 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2651 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002652}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002653EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002654
2655/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002656 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002657 * @array_size: number of elements in the descriptor / value arrays
2658 * @desc_array: array of GPIO descriptors whose values will be assigned
2659 * @value_array: array of values to assign
2660 *
2661 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2662 * into account.
2663 *
2664 * This function should be called from contexts where we cannot sleep, and will
2665 * complain if the GPIO chip functions potentially sleep.
2666 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002667void gpiod_set_array_value(unsigned int array_size,
2668 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002669{
2670 if (!desc_array)
2671 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002672 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2673 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002674}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002675EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002676
2677/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002678 * gpiod_cansleep() - report whether gpio value access may sleep
2679 * @desc: gpio to check
2680 *
2681 */
2682int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002683{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002684 VALIDATE_DESC(desc);
2685 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002686}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002687EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002688
David Brownell0f6d5042008-10-15 22:03:14 -07002689/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002690 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2691 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002692 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002693 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2694 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002695 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002696int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002697{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002698 struct gpio_chip *chip;
2699 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002700
Linus Walleij79bb71b2016-06-15 22:57:38 +02002701 /*
2702 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
2703 * requires this function to not return zero on an invalid descriptor
2704 * but rather a negative error number.
2705 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02002706 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02002707 return -EINVAL;
2708
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002709 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002710 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02002711 if (chip->to_irq) {
2712 int retirq = chip->to_irq(chip, offset);
2713
2714 /* Zero means NO_IRQ */
2715 if (!retirq)
2716 return -ENXIO;
2717
2718 return retirq;
2719 }
2720 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002721}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002722EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002723
Linus Walleijd468bf92013-09-24 11:54:38 +02002724/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002725 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002726 * @chip: the chip the GPIO to lock belongs to
2727 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002728 *
2729 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08002730 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08002731 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002732int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08002733{
Linus Walleij9c102802016-05-25 10:56:03 +02002734 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02002735
Linus Walleij9c102802016-05-25 10:56:03 +02002736 desc = gpiochip_get_desc(chip, offset);
2737 if (IS_ERR(desc))
2738 return PTR_ERR(desc);
2739
Linus Walleij60f83392016-11-12 15:01:09 +01002740 /*
2741 * If it's fast: flush the direction setting if something changed
2742 * behind our back
2743 */
2744 if (!chip->can_sleep && chip->get_direction) {
Linus Walleij9c102802016-05-25 10:56:03 +02002745 int dir = chip->get_direction(chip, offset);
2746
2747 if (dir)
2748 clear_bit(FLAG_IS_OUT, &desc->flags);
2749 else
2750 set_bit(FLAG_IS_OUT, &desc->flags);
2751 }
2752
2753 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002754 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02002755 "%s: tried to flag a GPIO set as output for IRQ\n",
2756 __func__);
2757 return -EIO;
2758 }
2759
Linus Walleij9c102802016-05-25 10:56:03 +02002760 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002761 return 0;
2762}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002763EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002764
Linus Walleijd468bf92013-09-24 11:54:38 +02002765/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002766 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002767 * @chip: the chip the GPIO to lock belongs to
2768 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002769 *
2770 * This is used directly by GPIO drivers that want to indicate
2771 * that a certain GPIO is no longer used exclusively for IRQ.
2772 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002773void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02002774{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002775 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02002776 return;
2777
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002778 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002779}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002780EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002781
Linus Walleij6cee3822016-02-11 20:16:45 +01002782bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2783{
2784 if (offset >= chip->ngpio)
2785 return false;
2786
2787 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2788}
2789EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2790
Linus Walleij143b65d2016-02-16 15:41:42 +01002791bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2792{
2793 if (offset >= chip->ngpio)
2794 return false;
2795
2796 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2797}
2798EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2799
2800bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2801{
2802 if (offset >= chip->ngpio)
2803 return false;
2804
2805 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2806}
2807EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2808
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002809/**
2810 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2811 * @desc: gpio whose value will be returned
2812 *
2813 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002814 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002815 *
2816 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002817 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002818int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002819{
David Brownelld2876d02008-02-04 22:28:20 -08002820 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002821 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002822 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002823}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002824EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002825
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002826/**
2827 * gpiod_get_value_cansleep() - return a gpio's value
2828 * @desc: gpio whose value will be returned
2829 *
2830 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002831 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002832 *
2833 * This function is to be called from contexts that can sleep.
2834 */
2835int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002836{
David Brownelld2876d02008-02-04 22:28:20 -08002837 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002838
2839 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002840 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002841 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002842 if (value < 0)
2843 return value;
2844
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002845 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2846 value = !value;
2847
David Brownelld2876d02008-02-04 22:28:20 -08002848 return value;
2849}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002850EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002851
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002852/**
2853 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2854 * @desc: gpio whose value will be assigned
2855 * @value: value to assign
2856 *
2857 * Set the raw value of the GPIO, i.e. the value of its physical line without
2858 * regard for its ACTIVE_LOW status.
2859 *
2860 * This function is to be called from contexts that can sleep.
2861 */
2862void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002863{
David Brownelld2876d02008-02-04 22:28:20 -08002864 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002865 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002866 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002867}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002868EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002869
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002870/**
2871 * gpiod_set_value_cansleep() - assign a gpio's value
2872 * @desc: gpio whose value will be assigned
2873 * @value: value to assign
2874 *
2875 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2876 * account
2877 *
2878 * This function is to be called from contexts that can sleep.
2879 */
2880void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002881{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002882 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002883 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002884 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2885 value = !value;
2886 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002887}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002888EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002889
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002890/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002891 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002892 * @array_size: number of elements in the descriptor / value arrays
2893 * @desc_array: array of GPIO descriptors whose values will be assigned
2894 * @value_array: array of values to assign
2895 *
2896 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2897 * without regard for their ACTIVE_LOW status.
2898 *
2899 * This function is to be called from contexts that can sleep.
2900 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002901void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2902 struct gpio_desc **desc_array,
2903 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002904{
2905 might_sleep_if(extra_checks);
2906 if (!desc_array)
2907 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002908 gpiod_set_array_value_complex(true, true, array_size, desc_array,
2909 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002910}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002911EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002912
2913/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002914 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002915 * @array_size: number of elements in the descriptor / value arrays
2916 * @desc_array: array of GPIO descriptors whose values will be assigned
2917 * @value_array: array of values to assign
2918 *
2919 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2920 * into account.
2921 *
2922 * This function is to be called from contexts that can sleep.
2923 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002924void gpiod_set_array_value_cansleep(unsigned int array_size,
2925 struct gpio_desc **desc_array,
2926 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002927{
2928 might_sleep_if(extra_checks);
2929 if (!desc_array)
2930 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002931 gpiod_set_array_value_complex(false, true, array_size, desc_array,
2932 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002933}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002934EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002935
2936/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002937 * gpiod_add_lookup_table() - register GPIO device consumers
2938 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002939 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002940void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002941{
2942 mutex_lock(&gpio_lookup_lock);
2943
Alexandre Courbotad824782013-12-03 12:20:11 +09002944 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002945
2946 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002947}
2948
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302949/**
2950 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2951 * @table: table of consumers to unregister
2952 */
2953void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2954{
2955 mutex_lock(&gpio_lookup_lock);
2956
2957 list_del(&table->list);
2958
2959 mutex_unlock(&gpio_lookup_lock);
2960}
2961
Alexandre Courbotad824782013-12-03 12:20:11 +09002962static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2963{
2964 const char *dev_id = dev ? dev_name(dev) : NULL;
2965 struct gpiod_lookup_table *table;
2966
2967 mutex_lock(&gpio_lookup_lock);
2968
2969 list_for_each_entry(table, &gpio_lookup_list, list) {
2970 if (table->dev_id && dev_id) {
2971 /*
2972 * Valid strings on both ends, must be identical to have
2973 * a match
2974 */
2975 if (!strcmp(table->dev_id, dev_id))
2976 goto found;
2977 } else {
2978 /*
2979 * One of the pointers is NULL, so both must be to have
2980 * a match
2981 */
2982 if (dev_id == table->dev_id)
2983 goto found;
2984 }
2985 }
2986 table = NULL;
2987
2988found:
2989 mutex_unlock(&gpio_lookup_lock);
2990 return table;
2991}
2992
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002993static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002994 unsigned int idx,
2995 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002996{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002997 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002998 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002999 struct gpiod_lookup *p;
3000
Alexandre Courbotad824782013-12-03 12:20:11 +09003001 table = gpiod_find_lookup_table(dev);
3002 if (!table)
3003 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003004
Alexandre Courbotad824782013-12-03 12:20:11 +09003005 for (p = &table->table[0]; p->chip_label; p++) {
3006 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003007
Alexandre Courbotad824782013-12-03 12:20:11 +09003008 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003009 if (p->idx != idx)
3010 continue;
3011
Alexandre Courbotad824782013-12-03 12:20:11 +09003012 /* If the lookup entry has a con_id, require exact match */
3013 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3014 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003015
Alexandre Courbotad824782013-12-03 12:20:11 +09003016 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003017
Alexandre Courbotad824782013-12-03 12:20:11 +09003018 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003019 dev_err(dev, "cannot find GPIO chip %s\n",
3020 p->chip_label);
3021 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003022 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003023
Alexandre Courbotad824782013-12-03 12:20:11 +09003024 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003025 dev_err(dev,
3026 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3027 idx, chip->ngpio, chip->label);
3028 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003029 }
3030
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003031 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003032 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003033
3034 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003035 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003036
3037 return desc;
3038}
3039
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003040static int dt_gpio_count(struct device *dev, const char *con_id)
3041{
3042 int ret;
3043 char propname[32];
3044 unsigned int i;
3045
3046 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3047 if (con_id)
3048 snprintf(propname, sizeof(propname), "%s-%s",
3049 con_id, gpio_suffixes[i]);
3050 else
3051 snprintf(propname, sizeof(propname), "%s",
3052 gpio_suffixes[i]);
3053
3054 ret = of_gpio_named_count(dev->of_node, propname);
3055 if (ret >= 0)
3056 break;
3057 }
3058 return ret;
3059}
3060
3061static int platform_gpio_count(struct device *dev, const char *con_id)
3062{
3063 struct gpiod_lookup_table *table;
3064 struct gpiod_lookup *p;
3065 unsigned int count = 0;
3066
3067 table = gpiod_find_lookup_table(dev);
3068 if (!table)
3069 return -ENOENT;
3070
3071 for (p = &table->table[0]; p->chip_label; p++) {
3072 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3073 (!con_id && !p->con_id))
3074 count++;
3075 }
3076 if (!count)
3077 return -ENOENT;
3078
3079 return count;
3080}
3081
3082/**
3083 * gpiod_count - return the number of GPIOs associated with a device / function
3084 * or -ENOENT if no GPIO has been assigned to the requested function
3085 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3086 * @con_id: function within the GPIO consumer
3087 */
3088int gpiod_count(struct device *dev, const char *con_id)
3089{
3090 int count = -ENOENT;
3091
3092 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3093 count = dt_gpio_count(dev, con_id);
3094 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3095 count = acpi_gpio_count(dev, con_id);
3096
3097 if (count < 0)
3098 count = platform_gpio_count(dev, con_id);
3099
3100 return count;
3101}
3102EXPORT_SYMBOL_GPL(gpiod_count);
3103
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003104/**
Thierry Reding08791622014-04-25 16:54:22 +02003105 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003106 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003107 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003108 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003109 *
3110 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003111 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003112 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003113 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003114struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003115 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003116{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003117 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003118}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003119EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003120
3121/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003122 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3123 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3124 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003125 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003126 *
3127 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3128 * the requested function it will return NULL. This is convenient for drivers
3129 * that need to handle optional GPIOs.
3130 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003131struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003132 const char *con_id,
3133 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003134{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003135 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003136}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003137EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003138
Benoit Parrotf625d462015-02-02 11:44:44 -06003139
3140/**
3141 * gpiod_configure_flags - helper function to configure a given GPIO
3142 * @desc: gpio whose value will be assigned
3143 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003144 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3145 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003146 * @dflags: gpiod_flags - optional GPIO initialization flags
3147 *
3148 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3149 * requested function and/or index, or another IS_ERR() code if an error
3150 * occurred while trying to acquire the GPIO.
3151 */
3152static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003153 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003154{
3155 int status;
3156
Johan Hovold85b03b32016-07-03 18:32:05 +02003157 if (lflags & GPIO_ACTIVE_LOW)
3158 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3159 if (lflags & GPIO_OPEN_DRAIN)
3160 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3161 if (lflags & GPIO_OPEN_SOURCE)
3162 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3163
Benoit Parrotf625d462015-02-02 11:44:44 -06003164 /* No particular flag request, return here... */
3165 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3166 pr_debug("no flags found for %s\n", con_id);
3167 return 0;
3168 }
3169
3170 /* Process flags */
3171 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3172 status = gpiod_direction_output(desc,
3173 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
3174 else
3175 status = gpiod_direction_input(desc);
3176
3177 return status;
3178}
3179
Thierry Reding29a1f2332014-04-25 17:10:06 +02003180/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003181 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003182 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003183 * @con_id: function within the GPIO consumer
3184 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003185 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003186 *
3187 * This variant of gpiod_get() allows to access GPIOs other than the first
3188 * defined one for functions that define several GPIOs.
3189 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003190 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3191 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003192 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003193 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003194struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003195 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003196 unsigned int idx,
3197 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003198{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003199 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003200 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003201 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003202
3203 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3204
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003205 if (dev) {
3206 /* Using device tree? */
3207 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3208 dev_dbg(dev, "using device tree for GPIO lookup\n");
3209 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3210 } else if (ACPI_COMPANION(dev)) {
3211 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Dmitry Torokhov25487532016-03-24 10:50:25 -07003212 desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003213 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003214 }
3215
3216 /*
3217 * Either we are not using DT or ACPI, or their lookup did not return
3218 * a result. In that case, use platform lookup as a fallback.
3219 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003220 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003221 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003222 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003223 }
3224
3225 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003226 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003227 return desc;
3228 }
3229
3230 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003231 if (status < 0)
3232 return ERR_PTR(status);
3233
Johan Hovold85b03b32016-07-03 18:32:05 +02003234 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003235 if (status < 0) {
3236 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3237 gpiod_put(desc);
3238 return ERR_PTR(status);
3239 }
3240
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003241 return desc;
3242}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003243EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003244
3245/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003246 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3247 * @fwnode: handle of the firmware node
3248 * @propname: name of the firmware property representing the GPIO
3249 *
3250 * This function can be used for drivers that get their configuration
3251 * from firmware.
3252 *
3253 * Function properly finds the corresponding GPIO using whatever is the
3254 * underlying firmware interface and then makes sure that the GPIO
3255 * descriptor is requested before it is returned to the caller.
3256 *
3257 * In case of error an ERR_PTR() is returned.
3258 */
3259struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3260 const char *propname)
3261{
3262 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3263 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003264 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003265 int ret;
3266
3267 if (!fwnode)
3268 return ERR_PTR(-EINVAL);
3269
3270 if (is_of_node(fwnode)) {
3271 enum of_gpio_flags flags;
3272
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02003273 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02003274 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003275 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003276 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003277 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3278 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003279 } else if (is_acpi_node(fwnode)) {
3280 struct acpi_gpio_info info;
3281
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02003282 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02003283 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01003284 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003285 }
3286
3287 if (IS_ERR(desc))
3288 return desc;
3289
Johan Hovold85b03b32016-07-03 18:32:05 +02003290 ret = gpiod_request(desc, NULL);
3291 if (ret)
3292 return ERR_PTR(ret);
3293
Mika Westerberg40b73182014-10-21 13:33:59 +02003294 if (active_low)
3295 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3296
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003297 if (single_ended) {
3298 if (active_low)
3299 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3300 else
3301 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3302 }
3303
Mika Westerberg40b73182014-10-21 13:33:59 +02003304 return desc;
3305}
3306EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3307
3308/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003309 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3310 * function
3311 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3312 * @con_id: function within the GPIO consumer
3313 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003314 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003315 *
3316 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3317 * specified index was assigned to the requested function it will return NULL.
3318 * This is convenient for drivers that need to handle optional GPIOs.
3319 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003320struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003321 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003322 unsigned int index,
3323 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003324{
3325 struct gpio_desc *desc;
3326
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003327 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003328 if (IS_ERR(desc)) {
3329 if (PTR_ERR(desc) == -ENOENT)
3330 return NULL;
3331 }
3332
3333 return desc;
3334}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003335EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003336
3337/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003338 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3339 * @desc: gpio whose value will be assigned
3340 * @name: gpio line name
3341 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3342 * of_get_gpio_hog()
3343 * @dflags: gpiod_flags - optional GPIO initialization flags
3344 */
3345int gpiod_hog(struct gpio_desc *desc, const char *name,
3346 unsigned long lflags, enum gpiod_flags dflags)
3347{
3348 struct gpio_chip *chip;
3349 struct gpio_desc *local_desc;
3350 int hwnum;
3351 int status;
3352
3353 chip = gpiod_to_chip(desc);
3354 hwnum = gpio_chip_hwgpio(desc);
3355
3356 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3357 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303358 status = PTR_ERR(local_desc);
3359 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3360 name, chip->label, hwnum, status);
3361 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003362 }
3363
Johan Hovold85b03b32016-07-03 18:32:05 +02003364 status = gpiod_configure_flags(desc, name, lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003365 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303366 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3367 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003368 gpiochip_free_own_desc(desc);
3369 return status;
3370 }
3371
3372 /* Mark GPIO as hogged so it can be identified and removed later */
3373 set_bit(FLAG_IS_HOGGED, &desc->flags);
3374
3375 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3376 desc_to_gpio(desc), name,
3377 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3378 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3379 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3380
3381 return 0;
3382}
3383
3384/**
3385 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3386 * @chip: gpio chip to act on
3387 *
3388 * This is only used by of_gpiochip_remove to free hogged gpios
3389 */
3390static void gpiochip_free_hogs(struct gpio_chip *chip)
3391{
3392 int id;
3393
3394 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003395 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3396 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003397 }
3398}
3399
3400/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003401 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3402 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3403 * @con_id: function within the GPIO consumer
3404 * @flags: optional GPIO initialization flags
3405 *
3406 * This function acquires all the GPIOs defined under a given function.
3407 *
3408 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3409 * no GPIO has been assigned to the requested function, or another IS_ERR()
3410 * code if an error occurred while trying to acquire the GPIOs.
3411 */
3412struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3413 const char *con_id,
3414 enum gpiod_flags flags)
3415{
3416 struct gpio_desc *desc;
3417 struct gpio_descs *descs;
3418 int count;
3419
3420 count = gpiod_count(dev, con_id);
3421 if (count < 0)
3422 return ERR_PTR(count);
3423
3424 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3425 GFP_KERNEL);
3426 if (!descs)
3427 return ERR_PTR(-ENOMEM);
3428
3429 for (descs->ndescs = 0; descs->ndescs < count; ) {
3430 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3431 if (IS_ERR(desc)) {
3432 gpiod_put_array(descs);
3433 return ERR_CAST(desc);
3434 }
3435 descs->desc[descs->ndescs] = desc;
3436 descs->ndescs++;
3437 }
3438 return descs;
3439}
3440EXPORT_SYMBOL_GPL(gpiod_get_array);
3441
3442/**
3443 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3444 * function
3445 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3446 * @con_id: function within the GPIO consumer
3447 * @flags: optional GPIO initialization flags
3448 *
3449 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3450 * assigned to the requested function it will return NULL.
3451 */
3452struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3453 const char *con_id,
3454 enum gpiod_flags flags)
3455{
3456 struct gpio_descs *descs;
3457
3458 descs = gpiod_get_array(dev, con_id, flags);
3459 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3460 return NULL;
3461
3462 return descs;
3463}
3464EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3465
3466/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003467 * gpiod_put - dispose of a GPIO descriptor
3468 * @desc: GPIO descriptor to dispose of
3469 *
3470 * No descriptor can be used after gpiod_put() has been called on it.
3471 */
3472void gpiod_put(struct gpio_desc *desc)
3473{
3474 gpiod_free(desc);
3475}
3476EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003477
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003478/**
3479 * gpiod_put_array - dispose of multiple GPIO descriptors
3480 * @descs: struct gpio_descs containing an array of descriptors
3481 */
3482void gpiod_put_array(struct gpio_descs *descs)
3483{
3484 unsigned int i;
3485
3486 for (i = 0; i < descs->ndescs; i++)
3487 gpiod_put(descs->desc[i]);
3488
3489 kfree(descs);
3490}
3491EXPORT_SYMBOL_GPL(gpiod_put_array);
3492
Linus Walleij3c702e92015-10-21 15:29:53 +02003493static int __init gpiolib_dev_init(void)
3494{
3495 int ret;
3496
3497 /* Register GPIO sysfs bus */
3498 ret = bus_register(&gpio_bus_type);
3499 if (ret < 0) {
3500 pr_err("gpiolib: could not register GPIO bus type\n");
3501 return ret;
3502 }
3503
3504 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3505 if (ret < 0) {
3506 pr_err("gpiolib: failed to allocate char dev region\n");
3507 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003508 } else {
3509 gpiolib_initialized = true;
3510 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003511 }
3512 return ret;
3513}
3514core_initcall(gpiolib_dev_init);
3515
David Brownelld2876d02008-02-04 22:28:20 -08003516#ifdef CONFIG_DEBUG_FS
3517
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003518static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003519{
3520 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003521 struct gpio_chip *chip = gdev->chip;
3522 unsigned gpio = gdev->base;
3523 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003524 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003525 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003526
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003527 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003528 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3529 if (gdesc->name) {
3530 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3531 gpio, gdesc->name);
3532 }
David Brownelld2876d02008-02-04 22:28:20 -08003533 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003534 }
David Brownelld2876d02008-02-04 22:28:20 -08003535
Alexandre Courbot372e7222013-02-03 01:29:29 +09003536 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003537 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003538 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003539 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3540 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003541 is_out ? "out" : "in ",
3542 chip->get
3543 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003544 : "? ",
3545 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003546 seq_printf(s, "\n");
3547 }
3548}
3549
Thierry Redingf9c4a312012-04-12 13:26:01 +02003550static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003551{
Grant Likely362432a2013-02-09 09:41:49 +00003552 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003553 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003554 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003555
3556 s->private = "";
3557
Grant Likely362432a2013-02-09 09:41:49 +00003558 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003559 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003560 if (index-- == 0) {
3561 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003562 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003563 }
3564 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003565
3566 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003567}
3568
3569static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3570{
Grant Likely362432a2013-02-09 09:41:49 +00003571 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003572 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003573 void *ret = NULL;
3574
Grant Likely362432a2013-02-09 09:41:49 +00003575 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003576 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003577 ret = NULL;
3578 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003579 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003580 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003581
3582 s->private = "\n";
3583 ++*pos;
3584
3585 return ret;
3586}
3587
3588static void gpiolib_seq_stop(struct seq_file *s, void *v)
3589{
3590}
3591
3592static int gpiolib_seq_show(struct seq_file *s, void *v)
3593{
Linus Walleijff2b1352015-10-20 11:10:38 +02003594 struct gpio_device *gdev = v;
3595 struct gpio_chip *chip = gdev->chip;
3596 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003597
Linus Walleijff2b1352015-10-20 11:10:38 +02003598 if (!chip) {
3599 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3600 dev_name(&gdev->dev));
3601 return 0;
3602 }
3603
3604 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3605 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003606 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02003607 parent = chip->parent;
3608 if (parent)
3609 seq_printf(s, ", parent: %s/%s",
3610 parent->bus ? parent->bus->name : "no-bus",
3611 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02003612 if (chip->label)
3613 seq_printf(s, ", %s", chip->label);
3614 if (chip->can_sleep)
3615 seq_printf(s, ", can sleep");
3616 seq_printf(s, ":\n");
3617
3618 if (chip->dbg_show)
3619 chip->dbg_show(s, chip);
3620 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003621 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003622
David Brownelld2876d02008-02-04 22:28:20 -08003623 return 0;
3624}
3625
Thierry Redingf9c4a312012-04-12 13:26:01 +02003626static const struct seq_operations gpiolib_seq_ops = {
3627 .start = gpiolib_seq_start,
3628 .next = gpiolib_seq_next,
3629 .stop = gpiolib_seq_stop,
3630 .show = gpiolib_seq_show,
3631};
3632
David Brownelld2876d02008-02-04 22:28:20 -08003633static int gpiolib_open(struct inode *inode, struct file *file)
3634{
Thierry Redingf9c4a312012-04-12 13:26:01 +02003635 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08003636}
3637
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003638static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02003639 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08003640 .open = gpiolib_open,
3641 .read = seq_read,
3642 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02003643 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08003644};
3645
3646static int __init gpiolib_debugfs_init(void)
3647{
3648 /* /sys/kernel/debug/gpio */
3649 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3650 NULL, NULL, &gpiolib_operations);
3651 return 0;
3652}
3653subsys_initcall(gpiolib_debugfs_init);
3654
3655#endif /* DEBUG_FS */