blob: 6d5c366a1378a4bf7798beb9df071fc5c4bc0c1f [file] [log] [blame]
Andy Shevchenko923a6542017-05-25 16:08:38 +03001#include <linux/bitmap.h>
David Brownelld2876d02008-02-04 22:28:20 -08002#include <linux/kernel.h>
3#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07004#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08005#include <linux/irq.h>
6#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09007#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07008#include <linux/device.h>
9#include <linux/err.h>
10#include <linux/debugfs.h>
11#include <linux/seq_file.h>
12#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060013#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070014#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010016#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090017#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020018#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020019#include <linux/pinctrl/consumer.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020020#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020023#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020024#include <linux/anon_inodes.h>
Lars-Peter Clausen953b9562016-10-24 13:59:15 +020025#include <linux/file.h>
Linus Walleij61f922d2016-06-02 11:30:15 +020026#include <linux/kfifo.h>
27#include <linux/poll.h>
28#include <linux/timekeeping.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020029#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080030
Mika Westerberg664e3e52014-01-08 12:40:54 +020031#include "gpiolib.h"
32
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060033#define CREATE_TRACE_POINTS
34#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080035
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070036/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080037 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070038 * The GPIO programming interface allows for inlining speed-critical
39 * get/set operations for common cases, so that access to SOC-integrated
40 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080041 */
42
43
44/* When debugging, extend minimal trust to callers and platform code.
45 * Also emit diagnostic messages that may help initial bringup, when
46 * board setup or driver bugs are most common.
47 *
48 * Otherwise, minimize overhead in what may be bitbanging codepaths.
49 */
50#ifdef DEBUG
51#define extra_checks 1
52#else
53#define extra_checks 0
54#endif
55
Linus Walleijff2b1352015-10-20 11:10:38 +020056/* Device and char device-related information */
57static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020058static dev_t gpio_devt;
59#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
60static struct bus_type gpio_bus_type = {
61 .name = "gpio",
62};
Linus Walleijff2b1352015-10-20 11:10:38 +020063
David Brownelld2876d02008-02-04 22:28:20 -080064/* gpio_lock prevents conflicts during gpio_desc[] table updates.
65 * While any GPIO is requested, its gpio_chip is not removable;
66 * each GPIO's "requested" flag serves as a lock and refcount.
67 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090068DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080069
Alexandre Courbotbae48da2013-10-17 10:21:38 -070070static DEFINE_MUTEX(gpio_lookup_lock);
71static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020072LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020073
74static void gpiochip_free_hogs(struct gpio_chip *chip);
Thierry Redinge0d89722017-11-07 19:15:54 +010075static int gpiochip_add_irqchip(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020076static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030077static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
78static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020079
Guenter Roeck159f3cd2016-03-31 08:11:30 -070080static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020081
David Brownelld2876d02008-02-04 22:28:20 -080082static inline void desc_set_label(struct gpio_desc *d, const char *label)
83{
David Brownelld2876d02008-02-04 22:28:20 -080084 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080085}
86
Alexandre Courbot372e7222013-02-03 01:29:29 +090087/**
Thierry Reding950d55f52017-07-24 16:57:22 +020088 * gpio_to_desc - Convert a GPIO number to its descriptor
89 * @gpio: global GPIO number
90 *
91 * Returns:
92 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
93 * with the given number exists in the system.
Alexandre Courbot372e7222013-02-03 01:29:29 +090094 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070095struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090096{
Linus Walleijff2b1352015-10-20 11:10:38 +020097 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +090098 unsigned long flags;
99
100 spin_lock_irqsave(&gpio_lock, flags);
101
Linus Walleijff2b1352015-10-20 11:10:38 +0200102 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100103 if (gdev->base <= gpio &&
104 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900105 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100106 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900107 }
108 }
109
110 spin_unlock_irqrestore(&gpio_lock, flags);
111
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900112 if (!gpio_is_valid(gpio))
113 WARN(1, "invalid GPIO %d\n", gpio);
114
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900115 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900116}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700117EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900118
119/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200120 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
121 * hardware number for this chip
122 * @chip: GPIO chip
123 * @hwnum: hardware number of the GPIO for this chip
124 *
125 * Returns:
126 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
127 * in the given chip for the specified hardware number.
Linus Walleijd468bf92013-09-24 11:54:38 +0200128 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900129struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
130 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200131{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100132 struct gpio_device *gdev = chip->gpiodev;
133
134 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900135 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200136
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100137 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200138}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900139
140/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200141 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
142 * @desc: GPIO descriptor
143 *
Alexandre Courbot372e7222013-02-03 01:29:29 +0900144 * This should disappear in the future but is needed since we still
Thierry Reding950d55f52017-07-24 16:57:22 +0200145 * use GPIO numbers for error messages and sysfs nodes.
146 *
147 * Returns:
148 * The global GPIO number for the GPIO specified by its descriptor.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900149 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700150int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900151{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100152 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900153}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700154EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900155
156
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700157/**
158 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
159 * @desc: descriptor to return the chip of
160 */
161struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900162{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100163 if (!desc || !desc->gdev || !desc->gdev->chip)
164 return NULL;
165 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900166}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700167EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800168
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700169/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
170static int gpiochip_find_base(int ngpio)
171{
Linus Walleijff2b1352015-10-20 11:10:38 +0200172 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900173 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700174
Linus Walleijff2b1352015-10-20 11:10:38 +0200175 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900176 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100177 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900178 break;
179 else
180 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100181 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700182 }
183
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900184 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700185 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900186 return base;
187 } else {
188 pr_err("%s: cannot find free range\n", __func__);
189 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700190 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700191}
192
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700193/**
194 * gpiod_get_direction - return the current direction of a GPIO
195 * @desc: GPIO to get the direction of
196 *
197 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
198 *
199 * This function may sleep if gpiod_cansleep() is true.
200 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900201int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300202{
203 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900204 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300205 int status = -EINVAL;
206
Alexandre Courbot372e7222013-02-03 01:29:29 +0900207 chip = gpiod_to_chip(desc);
208 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300209
210 if (!chip->get_direction)
211 return status;
212
Alexandre Courbot372e7222013-02-03 01:29:29 +0900213 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300214 if (status > 0) {
215 /* GPIOF_DIR_IN, or other positive */
216 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900217 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300218 }
219 if (status == 0) {
220 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900221 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300222 }
223 return status;
224}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700225EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300226
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900227/*
228 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800229 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900230 *
231 * Return -EBUSY if the new chip overlaps with some other chip's integer
232 * space.
233 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200234static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900235{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800236 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200237
238 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800239 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200240 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530241 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900242 }
243
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800244 next = list_entry(gpio_devices.next, struct gpio_device, list);
245 if (gdev->base + gdev->ngpio <= next->base) {
246 /* add before first entry */
247 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500248 return 0;
249 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900250
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800251 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
252 if (prev->base + prev->ngpio <= gdev->base) {
253 /* add behind last entry */
254 list_add_tail(&gdev->list, &gpio_devices);
255 return 0;
256 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800257
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800258 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
259 /* at the end of the list */
260 if (&next->list == &gpio_devices)
261 break;
262
263 /* add between prev and next */
264 if (prev->base + prev->ngpio <= gdev->base
265 && gdev->base + gdev->ngpio <= next->base) {
266 list_add(&gdev->list, &prev->list);
267 return 0;
268 }
269 }
270
271 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
272 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900273}
274
Thierry Reding950d55f52017-07-24 16:57:22 +0200275/*
Linus Walleijf881bab02015-09-23 16:20:43 -0700276 * Convert a GPIO name to its descriptor
277 */
278static struct gpio_desc *gpio_name_to_desc(const char * const name)
279{
Linus Walleijff2b1352015-10-20 11:10:38 +0200280 struct gpio_device *gdev;
Linus Walleijf881bab02015-09-23 16:20:43 -0700281 unsigned long flags;
282
283 spin_lock_irqsave(&gpio_lock, flags);
284
Linus Walleijff2b1352015-10-20 11:10:38 +0200285 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700286 int i;
287
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100288 for (i = 0; i != gdev->ngpio; ++i) {
289 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab02015-09-23 16:20:43 -0700290
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100291 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700292 continue;
293
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100294 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700295 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100296 return desc;
Linus Walleijf881bab02015-09-23 16:20:43 -0700297 }
298 }
299 }
300
301 spin_unlock_irqrestore(&gpio_lock, flags);
302
303 return NULL;
304}
305
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200306/*
307 * Takes the names from gc->names and checks if they are all unique. If they
308 * are, they are assigned to their gpio descriptors.
309 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800310 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200311 */
312static int gpiochip_set_desc_names(struct gpio_chip *gc)
313{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100314 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200315 int i;
316
317 if (!gc->names)
318 return 0;
319
320 /* First check all names if they are unique */
321 for (i = 0; i != gc->ngpio; ++i) {
322 struct gpio_desc *gpio;
323
324 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab02015-09-23 16:20:43 -0700325 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100326 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200327 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab02015-09-23 16:20:43 -0700328 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200329 }
330
331 /* Then add all names to the GPIO descriptors */
332 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100333 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200334
335 return 0;
336}
337
Linus Walleijd7c51b42016-04-26 10:35:29 +0200338/*
339 * GPIO line handle management
340 */
341
342/**
343 * struct linehandle_state - contains the state of a userspace handle
344 * @gdev: the GPIO device the handle pertains to
345 * @label: consumer label used to tag descriptors
346 * @descs: the GPIO descriptors held by this handle
347 * @numdescs: the number of descriptors held in the descs array
348 */
349struct linehandle_state {
350 struct gpio_device *gdev;
351 const char *label;
352 struct gpio_desc *descs[GPIOHANDLES_MAX];
353 u32 numdescs;
354};
355
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200356#define GPIOHANDLE_REQUEST_VALID_FLAGS \
357 (GPIOHANDLE_REQUEST_INPUT | \
358 GPIOHANDLE_REQUEST_OUTPUT | \
359 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
360 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
361 GPIOHANDLE_REQUEST_OPEN_SOURCE)
362
Linus Walleijd7c51b42016-04-26 10:35:29 +0200363static long linehandle_ioctl(struct file *filep, unsigned int cmd,
364 unsigned long arg)
365{
366 struct linehandle_state *lh = filep->private_data;
367 void __user *ip = (void __user *)arg;
368 struct gpiohandle_data ghd;
Lukas Wunnereec1d562017-10-12 12:40:10 +0200369 int vals[GPIOHANDLES_MAX];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200370 int i;
371
372 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Lukas Wunnereec1d562017-10-12 12:40:10 +0200373 /* TODO: check if descriptors are really input */
374 int ret = gpiod_get_array_value_complex(false,
375 true,
376 lh->numdescs,
377 lh->descs,
378 vals);
379 if (ret)
380 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200381
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200382 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200383 for (i = 0; i < lh->numdescs; i++)
384 ghd.values[i] = vals[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200385
386 if (copy_to_user(ip, &ghd, sizeof(ghd)))
387 return -EFAULT;
388
389 return 0;
390 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Linus Walleijd7c51b42016-04-26 10:35:29 +0200391 /* TODO: check if descriptors are really output */
392 if (copy_from_user(&ghd, ip, sizeof(ghd)))
393 return -EFAULT;
394
395 /* Clamp all values to [0,1] */
396 for (i = 0; i < lh->numdescs; i++)
397 vals[i] = !!ghd.values[i];
398
399 /* Reuse the array setting function */
400 gpiod_set_array_value_complex(false,
401 true,
402 lh->numdescs,
403 lh->descs,
404 vals);
405 return 0;
406 }
407 return -EINVAL;
408}
409
410#ifdef CONFIG_COMPAT
411static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
412 unsigned long arg)
413{
414 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
415}
416#endif
417
418static int linehandle_release(struct inode *inode, struct file *filep)
419{
420 struct linehandle_state *lh = filep->private_data;
421 struct gpio_device *gdev = lh->gdev;
422 int i;
423
424 for (i = 0; i < lh->numdescs; i++)
425 gpiod_free(lh->descs[i]);
426 kfree(lh->label);
427 kfree(lh);
428 put_device(&gdev->dev);
429 return 0;
430}
431
432static const struct file_operations linehandle_fileops = {
433 .release = linehandle_release,
434 .owner = THIS_MODULE,
435 .llseek = noop_llseek,
436 .unlocked_ioctl = linehandle_ioctl,
437#ifdef CONFIG_COMPAT
438 .compat_ioctl = linehandle_ioctl_compat,
439#endif
440};
441
442static int linehandle_create(struct gpio_device *gdev, void __user *ip)
443{
444 struct gpiohandle_request handlereq;
445 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200446 struct file *file;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200447 int fd, i, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200448 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200449
450 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
451 return -EFAULT;
452 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
453 return -EINVAL;
454
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200455 lflags = handlereq.flags;
456
457 /* Return an error if an unknown flag is set */
458 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
459 return -EINVAL;
460
Bartosz Golaszewski609aaf62017-10-16 11:32:30 +0200461 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
462 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
463 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
464 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
465 return -EINVAL;
466
Linus Walleijd7c51b42016-04-26 10:35:29 +0200467 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
468 if (!lh)
469 return -ENOMEM;
470 lh->gdev = gdev;
471 get_device(&gdev->dev);
472
473 /* Make sure this is terminated */
474 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
475 if (strlen(handlereq.consumer_label)) {
476 lh->label = kstrdup(handlereq.consumer_label,
477 GFP_KERNEL);
478 if (!lh->label) {
479 ret = -ENOMEM;
480 goto out_free_lh;
481 }
482 }
483
484 /* Request each GPIO */
485 for (i = 0; i < handlereq.lines; i++) {
486 u32 offset = handlereq.lineoffsets[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200487 struct gpio_desc *desc;
488
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200489 if (offset >= gdev->ngpio) {
490 ret = -EINVAL;
491 goto out_free_descs;
492 }
493
Linus Walleijd7c51b42016-04-26 10:35:29 +0200494 desc = &gdev->descs[offset];
495 ret = gpiod_request(desc, lh->label);
496 if (ret)
497 goto out_free_descs;
498 lh->descs[i] = desc;
499
500 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
501 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
502 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
503 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
504 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
505 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
506
507 /*
508 * Lines have to be requested explicitly for input
509 * or output, else the line will be treated "as is".
510 */
511 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
512 int val = !!handlereq.default_values[i];
513
514 ret = gpiod_direction_output(desc, val);
515 if (ret)
516 goto out_free_descs;
517 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
518 ret = gpiod_direction_input(desc);
519 if (ret)
520 goto out_free_descs;
521 }
522 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
523 offset);
524 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200525 /* Let i point at the last handle */
526 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200527 lh->numdescs = handlereq.lines;
528
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200529 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200530 if (fd < 0) {
531 ret = fd;
532 goto out_free_descs;
533 }
534
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200535 file = anon_inode_getfile("gpio-linehandle",
536 &linehandle_fileops,
537 lh,
538 O_RDONLY | O_CLOEXEC);
539 if (IS_ERR(file)) {
540 ret = PTR_ERR(file);
541 goto out_put_unused_fd;
542 }
543
Linus Walleijd7c51b42016-04-26 10:35:29 +0200544 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200545 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200546 /*
547 * fput() will trigger the release() callback, so do not go onto
548 * the regular error cleanup path here.
549 */
550 fput(file);
551 put_unused_fd(fd);
552 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200553 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200554
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200555 fd_install(fd, file);
556
Linus Walleijd7c51b42016-04-26 10:35:29 +0200557 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
558 lh->numdescs);
559
560 return 0;
561
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200562out_put_unused_fd:
563 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200564out_free_descs:
565 for (; i >= 0; i--)
566 gpiod_free(lh->descs[i]);
567 kfree(lh->label);
568out_free_lh:
569 kfree(lh);
570 put_device(&gdev->dev);
571 return ret;
572}
573
Linus Walleij61f922d2016-06-02 11:30:15 +0200574/*
575 * GPIO line event management
576 */
577
578/**
579 * struct lineevent_state - contains the state of a userspace event
580 * @gdev: the GPIO device the event pertains to
581 * @label: consumer label used to tag descriptors
582 * @desc: the GPIO descriptor held by this event
583 * @eflags: the event flags this line was requested with
584 * @irq: the interrupt that trigger in response to events on this GPIO
585 * @wait: wait queue that handles blocking reads of events
586 * @events: KFIFO for the GPIO events
587 * @read_lock: mutex lock to protect reads from colliding with adding
588 * new events to the FIFO
589 */
590struct lineevent_state {
591 struct gpio_device *gdev;
592 const char *label;
593 struct gpio_desc *desc;
594 u32 eflags;
595 int irq;
596 wait_queue_head_t wait;
597 DECLARE_KFIFO(events, struct gpioevent_data, 16);
598 struct mutex read_lock;
599};
600
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200601#define GPIOEVENT_REQUEST_VALID_FLAGS \
602 (GPIOEVENT_REQUEST_RISING_EDGE | \
603 GPIOEVENT_REQUEST_FALLING_EDGE)
604
Linus Walleij61f922d2016-06-02 11:30:15 +0200605static unsigned int lineevent_poll(struct file *filep,
606 struct poll_table_struct *wait)
607{
608 struct lineevent_state *le = filep->private_data;
609 unsigned int events = 0;
610
611 poll_wait(filep, &le->wait, wait);
612
613 if (!kfifo_is_empty(&le->events))
614 events = POLLIN | POLLRDNORM;
615
616 return events;
617}
618
619
620static ssize_t lineevent_read(struct file *filep,
621 char __user *buf,
622 size_t count,
623 loff_t *f_ps)
624{
625 struct lineevent_state *le = filep->private_data;
626 unsigned int copied;
627 int ret;
628
629 if (count < sizeof(struct gpioevent_data))
630 return -EINVAL;
631
632 do {
633 if (kfifo_is_empty(&le->events)) {
634 if (filep->f_flags & O_NONBLOCK)
635 return -EAGAIN;
636
637 ret = wait_event_interruptible(le->wait,
638 !kfifo_is_empty(&le->events));
639 if (ret)
640 return ret;
641 }
642
643 if (mutex_lock_interruptible(&le->read_lock))
644 return -ERESTARTSYS;
645 ret = kfifo_to_user(&le->events, buf, count, &copied);
646 mutex_unlock(&le->read_lock);
647
648 if (ret)
649 return ret;
650
651 /*
652 * If we couldn't read anything from the fifo (a different
653 * thread might have been faster) we either return -EAGAIN if
654 * the file descriptor is non-blocking, otherwise we go back to
655 * sleep and wait for more data to arrive.
656 */
657 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
658 return -EAGAIN;
659
660 } while (copied == 0);
661
662 return copied;
663}
664
665static int lineevent_release(struct inode *inode, struct file *filep)
666{
667 struct lineevent_state *le = filep->private_data;
668 struct gpio_device *gdev = le->gdev;
669
670 free_irq(le->irq, le);
671 gpiod_free(le->desc);
672 kfree(le->label);
673 kfree(le);
674 put_device(&gdev->dev);
675 return 0;
676}
677
678static long lineevent_ioctl(struct file *filep, unsigned int cmd,
679 unsigned long arg)
680{
681 struct lineevent_state *le = filep->private_data;
682 void __user *ip = (void __user *)arg;
683 struct gpiohandle_data ghd;
684
685 /*
686 * We can get the value for an event line but not set it,
687 * because it is input by definition.
688 */
689 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
690 int val;
691
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200692 memset(&ghd, 0, sizeof(ghd));
693
Linus Walleij61f922d2016-06-02 11:30:15 +0200694 val = gpiod_get_value_cansleep(le->desc);
695 if (val < 0)
696 return val;
697 ghd.values[0] = val;
698
699 if (copy_to_user(ip, &ghd, sizeof(ghd)))
700 return -EFAULT;
701
702 return 0;
703 }
704 return -EINVAL;
705}
706
707#ifdef CONFIG_COMPAT
708static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
709 unsigned long arg)
710{
711 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
712}
713#endif
714
715static const struct file_operations lineevent_fileops = {
716 .release = lineevent_release,
717 .read = lineevent_read,
718 .poll = lineevent_poll,
719 .owner = THIS_MODULE,
720 .llseek = noop_llseek,
721 .unlocked_ioctl = lineevent_ioctl,
722#ifdef CONFIG_COMPAT
723 .compat_ioctl = lineevent_ioctl_compat,
724#endif
725};
726
Ben Dooks33265b12016-06-17 16:03:13 +0100727static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200728{
729 struct lineevent_state *le = p;
730 struct gpioevent_data ge;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200731 int ret, level;
Linus Walleij61f922d2016-06-02 11:30:15 +0200732
733 ge.timestamp = ktime_get_real_ns();
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200734 level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200735
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200736 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
737 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200738 if (level)
739 /* Emit low-to-high event */
740 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
741 else
742 /* Emit high-to-low event */
743 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200744 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200745 /* Emit low-to-high event */
746 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200747 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200748 /* Emit high-to-low event */
749 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200750 } else {
751 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200752 }
753
754 ret = kfifo_put(&le->events, ge);
755 if (ret != 0)
756 wake_up_poll(&le->wait, POLLIN);
757
758 return IRQ_HANDLED;
759}
760
761static int lineevent_create(struct gpio_device *gdev, void __user *ip)
762{
763 struct gpioevent_request eventreq;
764 struct lineevent_state *le;
765 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200766 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200767 u32 offset;
768 u32 lflags;
769 u32 eflags;
770 int fd;
771 int ret;
772 int irqflags = 0;
773
774 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
775 return -EFAULT;
776
777 le = kzalloc(sizeof(*le), GFP_KERNEL);
778 if (!le)
779 return -ENOMEM;
780 le->gdev = gdev;
781 get_device(&gdev->dev);
782
783 /* Make sure this is terminated */
784 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
785 if (strlen(eventreq.consumer_label)) {
786 le->label = kstrdup(eventreq.consumer_label,
787 GFP_KERNEL);
788 if (!le->label) {
789 ret = -ENOMEM;
790 goto out_free_le;
791 }
792 }
793
794 offset = eventreq.lineoffset;
795 lflags = eventreq.handleflags;
796 eflags = eventreq.eventflags;
797
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200798 if (offset >= gdev->ngpio) {
799 ret = -EINVAL;
800 goto out_free_label;
801 }
802
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200803 /* Return an error if a unknown flag is set */
804 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
805 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
806 ret = -EINVAL;
807 goto out_free_label;
808 }
809
Linus Walleij61f922d2016-06-02 11:30:15 +0200810 /* This is just wrong: we don't look for events on output lines */
811 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
812 ret = -EINVAL;
813 goto out_free_label;
814 }
815
816 desc = &gdev->descs[offset];
817 ret = gpiod_request(desc, le->label);
818 if (ret)
819 goto out_free_desc;
820 le->desc = desc;
821 le->eflags = eflags;
822
823 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
824 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
825 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
826 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
827 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
828 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
829
830 ret = gpiod_direction_input(desc);
831 if (ret)
832 goto out_free_desc;
833
834 le->irq = gpiod_to_irq(desc);
835 if (le->irq <= 0) {
836 ret = -ENODEV;
837 goto out_free_desc;
838 }
839
840 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
841 irqflags |= IRQF_TRIGGER_RISING;
842 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
843 irqflags |= IRQF_TRIGGER_FALLING;
844 irqflags |= IRQF_ONESHOT;
845 irqflags |= IRQF_SHARED;
846
847 INIT_KFIFO(le->events);
848 init_waitqueue_head(&le->wait);
849 mutex_init(&le->read_lock);
850
851 /* Request a thread to read the events */
852 ret = request_threaded_irq(le->irq,
853 NULL,
854 lineevent_irq_thread,
855 irqflags,
856 le->label,
857 le);
858 if (ret)
859 goto out_free_desc;
860
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200861 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200862 if (fd < 0) {
863 ret = fd;
864 goto out_free_irq;
865 }
866
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200867 file = anon_inode_getfile("gpio-event",
868 &lineevent_fileops,
869 le,
870 O_RDONLY | O_CLOEXEC);
871 if (IS_ERR(file)) {
872 ret = PTR_ERR(file);
873 goto out_put_unused_fd;
874 }
875
Linus Walleij61f922d2016-06-02 11:30:15 +0200876 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200877 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200878 /*
879 * fput() will trigger the release() callback, so do not go onto
880 * the regular error cleanup path here.
881 */
882 fput(file);
883 put_unused_fd(fd);
884 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200885 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200886
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200887 fd_install(fd, file);
888
Linus Walleij61f922d2016-06-02 11:30:15 +0200889 return 0;
890
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200891out_put_unused_fd:
892 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +0200893out_free_irq:
894 free_irq(le->irq, le);
895out_free_desc:
896 gpiod_free(le->desc);
897out_free_label:
898 kfree(le->label);
899out_free_le:
900 kfree(le);
901 put_device(&gdev->dev);
902 return ret;
903}
904
Thierry Reding950d55f52017-07-24 16:57:22 +0200905/*
Linus Walleij3c702e92015-10-21 15:29:53 +0200906 * gpio_ioctl() - ioctl handler for the GPIO chardev
907 */
908static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
909{
910 struct gpio_device *gdev = filp->private_data;
911 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200912 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200913
914 /* We fail any subsequent ioctl():s when the chip is gone */
915 if (!chip)
916 return -ENODEV;
917
Linus Walleij521a2ad2016-02-12 22:25:22 +0100918 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200919 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100920 struct gpiochip_info chipinfo;
921
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +0200922 memset(&chipinfo, 0, sizeof(chipinfo));
923
Linus Walleij3c702e92015-10-21 15:29:53 +0200924 strncpy(chipinfo.name, dev_name(&gdev->dev),
925 sizeof(chipinfo.name));
926 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100927 strncpy(chipinfo.label, gdev->label,
928 sizeof(chipinfo.label));
929 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100930 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200931 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
932 return -EFAULT;
933 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100934 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
935 struct gpioline_info lineinfo;
936 struct gpio_desc *desc;
937
938 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
939 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +0200940 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +0100941 return -EINVAL;
942
943 desc = &gdev->descs[lineinfo.line_offset];
944 if (desc->name) {
945 strncpy(lineinfo.name, desc->name,
946 sizeof(lineinfo.name));
947 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
948 } else {
949 lineinfo.name[0] = '\0';
950 }
951 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100952 strncpy(lineinfo.consumer, desc->label,
953 sizeof(lineinfo.consumer));
954 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100955 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100956 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100957 }
958
959 /*
960 * Userspace only need to know that the kernel is using
961 * this GPIO so it can't use it.
962 */
963 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100964 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
965 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
966 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
967 test_bit(FLAG_EXPORT, &desc->flags) ||
968 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100969 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100970 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100971 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100972 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100973 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100974 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100975 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100976 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100977 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
978
979 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
980 return -EFAULT;
981 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200982 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
983 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +0200984 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
985 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +0200986 }
987 return -EINVAL;
988}
989
Linus Walleij8b92e172016-05-27 14:24:04 +0200990#ifdef CONFIG_COMPAT
991static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
992 unsigned long arg)
993{
994 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
995}
996#endif
997
Linus Walleij3c702e92015-10-21 15:29:53 +0200998/**
999 * gpio_chrdev_open() - open the chardev for ioctl operations
1000 * @inode: inode for this chardev
1001 * @filp: file struct for storing private data
1002 * Returns 0 on success
1003 */
1004static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1005{
1006 struct gpio_device *gdev = container_of(inode->i_cdev,
1007 struct gpio_device, chrdev);
1008
1009 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001010 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001011 return -ENODEV;
1012 get_device(&gdev->dev);
1013 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001014
1015 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001016}
1017
1018/**
1019 * gpio_chrdev_release() - close chardev after ioctl operations
1020 * @inode: inode for this chardev
1021 * @filp: file struct for storing private data
1022 * Returns 0 on success
1023 */
1024static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1025{
1026 struct gpio_device *gdev = container_of(inode->i_cdev,
1027 struct gpio_device, chrdev);
1028
Linus Walleij3c702e92015-10-21 15:29:53 +02001029 put_device(&gdev->dev);
1030 return 0;
1031}
1032
1033
1034static const struct file_operations gpio_fileops = {
1035 .release = gpio_chrdev_release,
1036 .open = gpio_chrdev_open,
1037 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001038 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001039 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001040#ifdef CONFIG_COMPAT
1041 .compat_ioctl = gpio_ioctl_compat,
1042#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001043};
1044
Linus Walleijff2b1352015-10-20 11:10:38 +02001045static void gpiodevice_release(struct device *dev)
1046{
1047 struct gpio_device *gdev = dev_get_drvdata(dev);
1048
1049 list_del(&gdev->list);
1050 ida_simple_remove(&gpio_ida, gdev->id);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001051 kfree(gdev->label);
1052 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001053 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001054}
1055
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001056static int gpiochip_setup_dev(struct gpio_device *gdev)
1057{
1058 int status;
1059
1060 cdev_init(&gdev->chrdev, &gpio_fileops);
1061 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001062 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001063
1064 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001065 if (status)
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001066 return status;
1067
1068 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1069 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001070
1071 status = gpiochip_sysfs_register(gdev);
1072 if (status)
1073 goto err_remove_device;
1074
1075 /* From this point, the .release() function cleans up gpio_device */
1076 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001077 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1078 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1079 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1080
1081 return 0;
1082
1083err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001084 cdev_device_del(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001085 return status;
1086}
1087
1088static void gpiochip_setup_devs(void)
1089{
1090 struct gpio_device *gdev;
1091 int err;
1092
1093 list_for_each_entry(gdev, &gpio_devices, list) {
1094 err = gpiochip_setup_dev(gdev);
1095 if (err)
1096 pr_err("%s: Failed to initialize gpio device (%d)\n",
1097 dev_name(&gdev->dev), err);
1098 }
1099}
1100
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001101/**
Linus Walleijb08ea352015-12-03 15:14:13 +01001102 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -08001103 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001104 * @data: driver-private data associated with this chip
David Brownelld2876d02008-02-04 22:28:20 -08001105 *
Thierry Reding950d55f52017-07-24 16:57:22 +02001106 * Context: potentially before irqs will work
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001107 *
Linus Walleijb08ea352015-12-03 15:14:13 +01001108 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001109 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d82008-07-25 01:46:07 -07001110 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1111 * for GPIOs will fail rudely.
1112 *
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001113 * gpiochip_add_data() must only be called after gpiolib initialization,
1114 * ie after core_initcall().
1115 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001116 * If chip->base is negative, this requests dynamic assignment of
1117 * a range of valid GPIOs.
Thierry Reding950d55f52017-07-24 16:57:22 +02001118 *
1119 * Returns:
1120 * A negative errno if the chip can't be registered, such as because the
1121 * chip->base is invalid or already associated with a different chip.
1122 * Otherwise it returns zero as a success code.
David Brownelld2876d02008-02-04 22:28:20 -08001123 */
Linus Walleijb08ea352015-12-03 15:14:13 +01001124int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -08001125{
1126 unsigned long flags;
1127 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001128 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001129 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001130 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001131
Linus Walleijff2b1352015-10-20 11:10:38 +02001132 /*
1133 * First: allocate and populate the internal stat container, and
1134 * set up the struct device.
1135 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001136 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001137 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001138 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001139 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001140 gdev->chip = chip;
1141 chip->gpiodev = gdev;
1142 if (chip->parent) {
1143 gdev->dev.parent = chip->parent;
1144 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001145 }
1146
Linus Walleijff2b1352015-10-20 11:10:38 +02001147#ifdef CONFIG_OF_GPIO
1148 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001149 if (chip->of_node)
1150 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001151#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001152
Linus Walleijff2b1352015-10-20 11:10:38 +02001153 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1154 if (gdev->id < 0) {
1155 status = gdev->id;
1156 goto err_free_gdev;
1157 }
1158 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1159 device_initialize(&gdev->dev);
1160 dev_set_drvdata(&gdev->dev, gdev);
1161 if (chip->parent && chip->parent->driver)
1162 gdev->owner = chip->parent->driver->owner;
1163 else if (chip->owner)
1164 /* TODO: remove chip->owner */
1165 gdev->owner = chip->owner;
1166 else
1167 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001168
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001169 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001170 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001171 status = -ENOMEM;
1172 goto err_free_gdev;
1173 }
1174
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001175 if (chip->ngpio == 0) {
1176 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001177 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001178 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001179 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001180
1181 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001182 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001183 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001184 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001185 if (!gdev->label) {
1186 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001187 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001188 }
1189
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001190 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001191 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001192
David Brownelld2876d02008-02-04 22:28:20 -08001193 spin_lock_irqsave(&gpio_lock, flags);
1194
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001195 /*
1196 * TODO: this allocates a Linux GPIO number base in the global
1197 * GPIO numberspace for this chip. In the long run we want to
1198 * get *rid* of this numberspace and use only descriptors, but
1199 * it may be a pipe dream. It will not happen before we get rid
1200 * of the sysfs interface anyways.
1201 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001202 if (base < 0) {
1203 base = gpiochip_find_base(chip->ngpio);
1204 if (base < 0) {
1205 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001206 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001207 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001208 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001209 /*
1210 * TODO: it should not be necessary to reflect the assigned
1211 * base outside of the GPIO subsystem. Go over drivers and
1212 * see if anyone makes use of this, else drop this and assign
1213 * a poison instead.
1214 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001215 chip->base = base;
1216 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001217 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001218
Linus Walleijff2b1352015-10-20 11:10:38 +02001219 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001220 if (status) {
1221 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001222 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001223 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001224
Linus Walleij545ebd92016-05-30 17:11:59 +02001225 spin_unlock_irqrestore(&gpio_lock, flags);
1226
Linus Walleijff2b1352015-10-20 11:10:38 +02001227 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001228 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001229
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001230 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +02001231 /*
1232 * REVISIT: most hardware initializes GPIOs as inputs
1233 * (often with pullups enabled) so power usage is
1234 * minimized. Linux code should set the gpio direction
1235 * first thing; but until it does, and in case
1236 * chip->get_direction is not set, we may expose the
1237 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001238 */
Linus Walleij72d32002016-04-28 13:33:59 +02001239
1240 if (chip->get_direction) {
1241 /*
1242 * If we have .get_direction, set up the initial
1243 * direction flag from the hardware.
1244 */
1245 int dir = chip->get_direction(chip, i);
1246
1247 if (!dir)
1248 set_bit(FLAG_IS_OUT, &desc->flags);
1249 } else if (!chip->direction_input) {
1250 /*
1251 * If the chip lacks the .direction_input callback
1252 * we logically assume all lines are outputs.
1253 */
1254 set_bit(FLAG_IS_OUT, &desc->flags);
1255 }
David Brownelld2876d02008-02-04 22:28:20 -08001256 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001257
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301258#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001259 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301260#endif
1261
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001262 status = gpiochip_set_desc_names(chip);
1263 if (status)
1264 goto err_remove_from_list;
1265
Mika Westerberg79b804c2016-09-20 15:15:21 +03001266 status = gpiochip_irqchip_init_valid_mask(chip);
1267 if (status)
1268 goto err_remove_from_list;
1269
Thierry Redinge0d89722017-11-07 19:15:54 +01001270 status = gpiochip_add_irqchip(chip);
1271 if (status)
1272 goto err_remove_chip;
1273
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001274 status = of_gpiochip_add(chip);
1275 if (status)
1276 goto err_remove_chip;
1277
Mika Westerberg664e3e52014-01-08 12:40:54 +02001278 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001279
Linus Walleij3c702e92015-10-21 15:29:53 +02001280 /*
1281 * By first adding the chardev, and then adding the device,
1282 * we get a device node entry in sysfs under
1283 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1284 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001285 * We can do this only if gpiolib has been initialized.
1286 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001287 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001288 if (gpiolib_initialized) {
1289 status = gpiochip_setup_dev(gdev);
1290 if (status)
1291 goto err_remove_chip;
1292 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001293 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001294
Johan Hovold225fce82015-01-12 17:12:25 +01001295err_remove_chip:
1296 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001297 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001298 of_gpiochip_remove(chip);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001299 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001300err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001301 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001302 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001303 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001304err_free_label:
1305 kfree(gdev->label);
1306err_free_descs:
1307 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001308err_free_gdev:
1309 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001310 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001311 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001312 gdev->base, gdev->base + gdev->ngpio - 1,
1313 chip->label ? : "generic");
1314 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001315 return status;
1316}
Linus Walleijb08ea352015-12-03 15:14:13 +01001317EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -08001318
1319/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001320 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001321 * @chip: GPIO chip
1322 *
1323 * Returns:
1324 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001325 */
1326void *gpiochip_get_data(struct gpio_chip *chip)
1327{
1328 return chip->gpiodev->data;
1329}
1330EXPORT_SYMBOL_GPL(gpiochip_get_data);
1331
1332/**
David Brownelld2876d02008-02-04 22:28:20 -08001333 * gpiochip_remove() - unregister a gpio_chip
1334 * @chip: the chip to unregister
1335 *
1336 * A gpio_chip with any GPIOs still requested may not be removed.
1337 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001338void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001339{
Linus Walleijff2b1352015-10-20 11:10:38 +02001340 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001341 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001342 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001343 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001344 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001345
Linus Walleijff2b1352015-10-20 11:10:38 +02001346 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001347 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001348 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001349 /* Numb the device, cancelling all outstanding operations */
1350 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001351 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001352 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001353 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001354 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001355 /*
1356 * We accept no more calls into the driver from this point, so
1357 * NULL the driver data pointer
1358 */
1359 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001360
Johan Hovold6798aca2015-01-12 17:12:28 +01001361 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001362 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001363 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001364 if (test_bit(FLAG_REQUESTED, &desc->flags))
1365 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001366 }
David Brownelld2876d02008-02-04 22:28:20 -08001367 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001368
Johan Hovoldfab28b82015-05-04 17:10:27 +02001369 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001370 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001371 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001372
Linus Walleijff2b1352015-10-20 11:10:38 +02001373 /*
1374 * The gpiochip side puts its use of the device to rest here:
1375 * if there are no userspace clients, the chardev and device will
1376 * be removed, else it will be dangling until the last user is
1377 * gone.
1378 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001379 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001380 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001381}
1382EXPORT_SYMBOL_GPL(gpiochip_remove);
1383
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301384static void devm_gpio_chip_release(struct device *dev, void *res)
1385{
1386 struct gpio_chip *chip = *(struct gpio_chip **)res;
1387
1388 gpiochip_remove(chip);
1389}
1390
1391static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1392
1393{
1394 struct gpio_chip **r = res;
1395
1396 if (!r || !*r) {
1397 WARN_ON(!r || !*r);
1398 return 0;
1399 }
1400
1401 return *r == data;
1402}
1403
1404/**
1405 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1406 * @dev: the device pointer on which irq_chip belongs to.
1407 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001408 * @data: driver-private data associated with this chip
1409 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301410 * Context: potentially before irqs will work
1411 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301412 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001413 *
1414 * Returns:
1415 * A negative errno if the chip can't be registered, such as because the
1416 * chip->base is invalid or already associated with a different chip.
1417 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301418 */
1419int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1420 void *data)
1421{
1422 struct gpio_chip **ptr;
1423 int ret;
1424
1425 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1426 GFP_KERNEL);
1427 if (!ptr)
1428 return -ENOMEM;
1429
1430 ret = gpiochip_add_data(chip, data);
1431 if (ret < 0) {
1432 devres_free(ptr);
1433 return ret;
1434 }
1435
1436 *ptr = chip;
1437 devres_add(dev, ptr);
1438
1439 return 0;
1440}
1441EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1442
1443/**
1444 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1445 * @dev: device for which which resource was allocated
1446 * @chip: the chip to remove
1447 *
1448 * A gpio_chip with any GPIOs still requested may not be removed.
1449 */
1450void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1451{
1452 int ret;
1453
1454 ret = devres_release(dev, devm_gpio_chip_release,
1455 devm_gpio_chip_match, chip);
Christophe JAILLET3988d662017-01-27 12:56:42 +01001456 WARN_ON(ret);
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301457}
1458EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1459
Grant Likely594fa262010-06-08 07:48:16 -06001460/**
1461 * gpiochip_find() - iterator for locating a specific gpio_chip
1462 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001463 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001464 *
1465 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1466 * determined by a user supplied @match callback. The callback should return
1467 * 0 if the device doesn't match and non-zero if it does. If the callback is
1468 * non-zero, this function will return to the caller and not iterate over any
1469 * more gpio_chips.
1470 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001471struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001472 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001473 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001474{
Linus Walleijff2b1352015-10-20 11:10:38 +02001475 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001476 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001477 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001478
1479 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001480 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001481 if (gdev->chip && match(gdev->chip, data)) {
1482 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001483 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001484 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001485
Grant Likely594fa262010-06-08 07:48:16 -06001486 spin_unlock_irqrestore(&gpio_lock, flags);
1487
1488 return chip;
1489}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001490EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001491
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001492static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1493{
1494 const char *name = data;
1495
1496 return !strcmp(chip->label, name);
1497}
1498
1499static struct gpio_chip *find_chip_by_name(const char *name)
1500{
1501 return gpiochip_find((void *)name, gpiochip_match_name);
1502}
1503
Linus Walleij14250522014-03-25 10:40:18 +01001504#ifdef CONFIG_GPIOLIB_IRQCHIP
1505
1506/*
1507 * The following is irqchip helper code for gpiochips.
1508 */
1509
Mika Westerberg79b804c2016-09-20 15:15:21 +03001510static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1511{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001512 if (!gpiochip->irq.need_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001513 return 0;
1514
Thierry Redingdc7b0382017-11-07 19:15:52 +01001515 gpiochip->irq.valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
Mika Westerberg79b804c2016-09-20 15:15:21 +03001516 sizeof(long), GFP_KERNEL);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001517 if (!gpiochip->irq.valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001518 return -ENOMEM;
1519
1520 /* Assume by default all GPIOs are valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001521 bitmap_fill(gpiochip->irq.valid_mask, gpiochip->ngpio);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001522
1523 return 0;
1524}
1525
1526static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1527{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001528 kfree(gpiochip->irq.valid_mask);
1529 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001530}
1531
1532static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1533 unsigned int offset)
1534{
1535 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001536 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001537 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001538 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001539}
1540
Linus Walleij14250522014-03-25 10:40:18 +01001541/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001542 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001543 * @gpiochip: the gpiochip to set the irqchip chain to
1544 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001545 * @parent_irq: the irq number corresponding to the parent IRQ for this
1546 * chained irqchip
1547 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001548 * coming out of the gpiochip. If the interrupt is nested rather than
1549 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001550 */
Linus Walleijd245b3f2016-11-24 10:57:25 +01001551static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1552 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001553 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001554 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001555{
Linus Walleij83141a72014-09-26 13:50:12 +02001556 unsigned int offset;
1557
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001558 if (!gpiochip->irq.domain) {
Linus Walleij83141a72014-09-26 13:50:12 +02001559 chip_err(gpiochip, "called %s before setting up irqchip\n",
1560 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001561 return;
1562 }
1563
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001564 if (parent_handler) {
1565 if (gpiochip->can_sleep) {
1566 chip_err(gpiochip,
1567 "you cannot have chained interrupts on a "
1568 "chip that may sleep\n");
1569 return;
1570 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001571 /*
1572 * The parent irqchip is already using the chip_data for this
1573 * irqchip, so our callbacks simply use the handler_data.
1574 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001575 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1576 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001577
Thierry Reding39e5f092017-11-07 19:15:50 +01001578 gpiochip->irq.parents = &parent_irq;
1579 gpiochip->irq.num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001580 }
Linus Walleij83141a72014-09-26 13:50:12 +02001581
1582 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001583 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1584 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1585 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001586 irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
Linus Walleij83141a72014-09-26 13:50:12 +02001587 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001588 }
Linus Walleij14250522014-03-25 10:40:18 +01001589}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001590
1591/**
1592 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1593 * @gpiochip: the gpiochip to set the irqchip chain to
1594 * @irqchip: the irqchip to chain to the gpiochip
1595 * @parent_irq: the irq number corresponding to the parent IRQ for this
1596 * chained irqchip
1597 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1598 * coming out of the gpiochip. If the interrupt is nested rather than
1599 * cascaded, pass NULL in this handler argument
1600 */
1601void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1602 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001603 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001604 irq_flow_handler_t parent_handler)
1605{
Thierry Reding60ed54c2017-11-07 19:15:57 +01001606 if (gpiochip->irq.threaded) {
1607 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1608 return;
1609 }
1610
Linus Walleijd245b3f2016-11-24 10:57:25 +01001611 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1612 parent_handler);
1613}
Linus Walleij14250522014-03-25 10:40:18 +01001614EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1615
1616/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001617 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1618 * @gpiochip: the gpiochip to set the irqchip nested handler to
1619 * @irqchip: the irqchip to nest to the gpiochip
1620 * @parent_irq: the irq number corresponding to the parent IRQ for this
1621 * nested irqchip
1622 */
1623void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1624 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001625 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001626{
Linus Walleijd245b3f2016-11-24 10:57:25 +01001627 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1628 NULL);
1629}
1630EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1631
1632/**
Linus Walleij14250522014-03-25 10:40:18 +01001633 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1634 * @d: the irqdomain used by this irqchip
1635 * @irq: the global irq number used by this GPIO irqchip irq
1636 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1637 *
1638 * This function will set up the mapping for a certain IRQ line on a
1639 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1640 * stored inside the gpiochip.
1641 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001642int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1643 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01001644{
1645 struct gpio_chip *chip = d->host_data;
Thierry Redinge0d89722017-11-07 19:15:54 +01001646 int err = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001647
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001648 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1649 return -ENXIO;
1650
Linus Walleij14250522014-03-25 10:40:18 +01001651 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001652 /*
1653 * This lock class tells lockdep that GPIO irqs are in a different
1654 * category than their parents, so it won't report false recursion.
1655 */
Thierry Redingca9df052017-11-07 19:15:53 +01001656 irq_set_lockdep_class(irq, chip->irq.lock_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001657 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001658 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01001659 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001660 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001661 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001662
Thierry Redinge0d89722017-11-07 19:15:54 +01001663 if (chip->irq.num_parents == 1)
1664 err = irq_set_parent(irq, chip->irq.parents[0]);
1665 else if (chip->irq.map)
1666 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1667
1668 if (err < 0)
1669 return err;
1670
Linus Walleij1333b902014-04-23 16:45:12 +02001671 /*
1672 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1673 * is passed as default type.
1674 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01001675 if (chip->irq.default_type != IRQ_TYPE_NONE)
1676 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001677
1678 return 0;
1679}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001680EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01001681
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001682void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01001683{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001684 struct gpio_chip *chip = d->host_data;
1685
Thierry Reding60ed54c2017-11-07 19:15:57 +01001686 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001687 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001688 irq_set_chip_and_handler(irq, NULL, NULL);
1689 irq_set_chip_data(irq, NULL);
1690}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001691EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001692
Linus Walleij14250522014-03-25 10:40:18 +01001693static const struct irq_domain_ops gpiochip_domain_ops = {
1694 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001695 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001696 /* Virtually all GPIO irqchips are twocell:ed */
1697 .xlate = irq_domain_xlate_twocell,
1698};
1699
1700static int gpiochip_irq_reqres(struct irq_data *d)
1701{
1702 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1703
Linus Walleijff2b1352015-10-20 11:10:38 +02001704 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001705 return -ENODEV;
1706
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001707 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001708 chip_err(chip,
1709 "unable to lock HW IRQ %lu for IRQ\n",
1710 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001711 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001712 return -EINVAL;
1713 }
1714 return 0;
1715}
1716
1717static void gpiochip_irq_relres(struct irq_data *d)
1718{
1719 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1720
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001721 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001722 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001723}
1724
1725static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1726{
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001727 if (!gpiochip_irqchip_irq_valid(chip, offset))
1728 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01001729
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001730 return irq_create_mapping(chip->irq.domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01001731}
1732
1733/**
Thierry Redinge0d89722017-11-07 19:15:54 +01001734 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1735 * @gpiochip: the GPIO chip to add the IRQ chip to
1736 */
1737static int gpiochip_add_irqchip(struct gpio_chip *gpiochip)
1738{
1739 struct irq_chip *irqchip = gpiochip->irq.chip;
1740 const struct irq_domain_ops *ops;
1741 struct device_node *np;
1742 unsigned int type;
1743 unsigned int i;
1744
1745 if (!irqchip)
1746 return 0;
1747
1748 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
1749 chip_err(gpiochip, "you cannot have chained interrupts on a "
1750 "chip that may sleep\n");
1751 return -EINVAL;
1752 }
1753
1754 np = gpiochip->gpiodev->dev.of_node;
1755 type = gpiochip->irq.default_type;
1756
1757 /*
1758 * Specifying a default trigger is a terrible idea if DT or ACPI is
1759 * used to configure the interrupts, as you may end up with
1760 * conflicting triggers. Tell the user, and reset to NONE.
1761 */
1762 if (WARN(np && type != IRQ_TYPE_NONE,
1763 "%s: Ignoring %u default trigger\n", np->full_name, type))
1764 type = IRQ_TYPE_NONE;
1765
1766 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1767 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1768 "Ignoring %u default trigger\n", type);
1769 type = IRQ_TYPE_NONE;
1770 }
1771
1772 gpiochip->to_irq = gpiochip_to_irq;
1773 gpiochip->irq.default_type = type;
1774
1775 if (gpiochip->irq.domain_ops)
1776 ops = gpiochip->irq.domain_ops;
1777 else
1778 ops = &gpiochip_domain_ops;
1779
1780 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
Thierry Reding8302cf52017-11-07 19:15:58 +01001781 gpiochip->irq.first,
1782 ops, gpiochip);
Thierry Redinge0d89722017-11-07 19:15:54 +01001783 if (!gpiochip->irq.domain)
1784 return -EINVAL;
1785
1786 /*
1787 * It is possible for a driver to override this, but only if the
1788 * alternative functions are both implemented.
1789 */
1790 if (!irqchip->irq_request_resources &&
1791 !irqchip->irq_release_resources) {
1792 irqchip->irq_request_resources = gpiochip_irq_reqres;
1793 irqchip->irq_release_resources = gpiochip_irq_relres;
1794 }
1795
1796 if (gpiochip->irq.parent_handler) {
1797 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1798
1799 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1800 /*
1801 * The parent IRQ chip is already using the chip_data
1802 * for this IRQ chip, so our callbacks simply use the
1803 * handler_data.
1804 */
1805 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1806 gpiochip->irq.parent_handler,
1807 data);
1808 }
Thierry Redinge0d89722017-11-07 19:15:54 +01001809 }
1810
1811 acpi_gpiochip_request_interrupts(gpiochip);
1812
1813 return 0;
1814}
1815
1816/**
Linus Walleij14250522014-03-25 10:40:18 +01001817 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1818 * @gpiochip: the gpiochip to remove the irqchip from
1819 *
1820 * This is called only from gpiochip_remove()
1821 */
1822static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1823{
Thierry Reding39e5f092017-11-07 19:15:50 +01001824 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001825
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001826 acpi_gpiochip_free_interrupts(gpiochip);
1827
Thierry Redinge0d89722017-11-07 19:15:54 +01001828 if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001829 struct gpio_irq_chip *irq = &gpiochip->irq;
1830 unsigned int i;
1831
1832 for (i = 0; i < irq->num_parents; i++)
1833 irq_set_chained_handler_and_data(irq->parents[i],
1834 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001835 }
1836
Linus Walleijc3626fd2014-03-28 20:42:01 +01001837 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001838 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001839 unsigned int irq;
1840
Mika Westerberg79b804c2016-09-20 15:15:21 +03001841 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1842 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1843 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001844
1845 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1846 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001847 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001848
1849 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001850 }
Linus Walleij14250522014-03-25 10:40:18 +01001851
Thierry Redingda80ff82017-11-07 19:15:46 +01001852 if (gpiochip->irq.chip) {
1853 gpiochip->irq.chip->irq_request_resources = NULL;
1854 gpiochip->irq.chip->irq_release_resources = NULL;
1855 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001856 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001857
1858 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001859}
1860
1861/**
Linus Walleij739e6f52017-01-11 13:37:07 +01001862 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001863 * @gpiochip: the gpiochip to add the irqchip to
1864 * @irqchip: the irqchip to add to the gpiochip
1865 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1866 * allocate gpiochip irqs from
1867 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001868 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1869 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01001870 * @threaded: whether this irqchip uses a nested thread handler
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001871 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001872 *
1873 * This function closely associates a certain irqchip with a certain
1874 * gpiochip, providing an irq domain to translate the local IRQs to
1875 * global irqs in the gpiolib core, and making sure that the gpiochip
1876 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001877 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001878 * from the gpiochip passed as chip data. An irqdomain will be stored
1879 * in the gpiochip that shall be used by the driver to handle IRQ number
1880 * translation. The gpiochip will need to be initialized and registered
1881 * before calling this function.
1882 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001883 * This function will handle two cell:ed simple IRQs and assumes all
1884 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001885 * need to be open coded.
1886 */
Linus Walleij739e6f52017-01-11 13:37:07 +01001887int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1888 struct irq_chip *irqchip,
1889 unsigned int first_irq,
1890 irq_flow_handler_t handler,
1891 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01001892 bool threaded,
Linus Walleij739e6f52017-01-11 13:37:07 +01001893 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001894{
1895 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001896
1897 if (!gpiochip || !irqchip)
1898 return -EINVAL;
1899
Linus Walleij58383c782015-11-04 09:56:26 +01001900 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001901 pr_err("missing gpiochip .dev parent pointer\n");
1902 return -EINVAL;
1903 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01001904 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01001905 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001906#ifdef CONFIG_OF_GPIO
1907 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001908 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001909 * FIXME: get rid of this and use gpiochip->parent->of_node
1910 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001911 */
1912 if (gpiochip->of_node)
1913 of_node = gpiochip->of_node;
1914#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001915 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001916 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001917 * used to configure the interrupts, as you may end-up with
1918 * conflicting triggers. Tell the user, and reset to NONE.
1919 */
1920 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05001921 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01001922 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001923 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1924 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1925 "Ignoring %d default trigger\n", type);
1926 type = IRQ_TYPE_NONE;
1927 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001928
Thierry Redingda80ff82017-11-07 19:15:46 +01001929 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001930 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01001931 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01001932 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01001933 gpiochip->irq.lock_key = lock_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001934 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01001935 gpiochip->ngpio, first_irq,
1936 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001937 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01001938 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001939 return -EINVAL;
1940 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001941
1942 /*
1943 * It is possible for a driver to override this, but only if the
1944 * alternative functions are both implemented.
1945 */
1946 if (!irqchip->irq_request_resources &&
1947 !irqchip->irq_release_resources) {
1948 irqchip->irq_request_resources = gpiochip_irq_reqres;
1949 irqchip->irq_release_resources = gpiochip_irq_relres;
1950 }
Linus Walleij14250522014-03-25 10:40:18 +01001951
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001952 acpi_gpiochip_request_interrupts(gpiochip);
1953
Linus Walleij14250522014-03-25 10:40:18 +01001954 return 0;
1955}
Linus Walleij739e6f52017-01-11 13:37:07 +01001956EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01001957
1958#else /* CONFIG_GPIOLIB_IRQCHIP */
1959
Thierry Redinge0d89722017-11-07 19:15:54 +01001960static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip)
1961{
1962 return 0;
1963}
1964
Linus Walleij14250522014-03-25 10:40:18 +01001965static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03001966static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1967{
1968 return 0;
1969}
1970static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1971{ }
Linus Walleij14250522014-03-25 10:40:18 +01001972
1973#endif /* CONFIG_GPIOLIB_IRQCHIP */
1974
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001975/**
1976 * gpiochip_generic_request() - request the gpio function for a pin
1977 * @chip: the gpiochip owning the GPIO
1978 * @offset: the offset of the GPIO to request for GPIO function
1979 */
1980int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1981{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001982 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001983}
1984EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1985
1986/**
1987 * gpiochip_generic_free() - free the gpio function from a pin
1988 * @chip: the gpiochip to request the gpio function for
1989 * @offset: the offset of the GPIO to free from GPIO function
1990 */
1991void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1992{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001993 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001994}
1995EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1996
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001997/**
1998 * gpiochip_generic_config() - apply configuration for a pin
1999 * @chip: the gpiochip owning the GPIO
2000 * @offset: the offset of the GPIO to apply the configuration
2001 * @config: the configuration to be applied
2002 */
2003int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2004 unsigned long config)
2005{
2006 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2007}
2008EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2009
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302010#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002011
Linus Walleij3f0f8672012-11-20 12:40:15 +01002012/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002013 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2014 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002015 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002016 * @gpio_offset: the start offset in the current gpio_chip number space
2017 * @pin_group: name of the pin group inside the pin controller
2018 */
2019int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2020 struct pinctrl_dev *pctldev,
2021 unsigned int gpio_offset, const char *pin_group)
2022{
2023 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002024 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002025 int ret;
2026
2027 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2028 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002029 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002030 return -ENOMEM;
2031 }
2032
2033 /* Use local offset as range ID */
2034 pin_range->range.id = gpio_offset;
2035 pin_range->range.gc = chip;
2036 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002037 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002038 pin_range->pctldev = pctldev;
2039
2040 ret = pinctrl_get_group_pins(pctldev, pin_group,
2041 &pin_range->range.pins,
2042 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002043 if (ret < 0) {
2044 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002045 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002046 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002047
2048 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2049
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002050 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2051 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002052 pinctrl_dev_get_devname(pctldev), pin_group);
2053
Linus Walleij20ec3e32016-02-11 11:03:06 +01002054 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002055
2056 return 0;
2057}
2058EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2059
2060/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002061 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2062 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002063 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002064 * @gpio_offset: the start offset in the current gpio_chip number space
2065 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002066 * @npins: the number of pins from the offset of each pin space (GPIO and
2067 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002068 *
2069 * Returns:
2070 * 0 on success, or a negative error-code on failure.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002071 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002072int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002073 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002074 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302075{
2076 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002077 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002078 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302079
Linus Walleij3f0f8672012-11-20 12:40:15 +01002080 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302081 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002082 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002083 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302084 }
2085
Linus Walleij3f0f8672012-11-20 12:40:15 +01002086 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002087 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002088 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302089 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002090 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002091 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302092 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002093 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302094 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002095 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002096 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002097 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002098 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002099 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002100 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002101 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2102 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002103 pinctl_name,
2104 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302105
Linus Walleij20ec3e32016-02-11 11:03:06 +01002106 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002107
2108 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302109}
Linus Walleij165adc92012-11-06 14:49:39 +01002110EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302111
Linus Walleij3f0f8672012-11-20 12:40:15 +01002112/**
2113 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2114 * @chip: the chip to remove all the mappings for
2115 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302116void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2117{
2118 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002119 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302120
Linus Walleij20ec3e32016-02-11 11:03:06 +01002121 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302122 list_del(&pin_range->node);
2123 pinctrl_remove_gpio_range(pin_range->pctldev,
2124 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002125 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302126 }
2127}
Linus Walleij165adc92012-11-06 14:49:39 +01002128EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2129
2130#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302131
David Brownelld2876d02008-02-04 22:28:20 -08002132/* These "optional" allocation calls help prevent drivers from stomping
2133 * on each other, and help provide better diagnostics in debugfs.
2134 * They're called even less than the "set direction" calls.
2135 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002136static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002137{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002138 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002139 int status;
David Brownelld2876d02008-02-04 22:28:20 -08002140 unsigned long flags;
2141
2142 spin_lock_irqsave(&gpio_lock, flags);
2143
David Brownelld2876d02008-02-04 22:28:20 -08002144 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002145 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002146 */
2147
2148 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2149 desc_set_label(desc, label ? : "?");
2150 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002151 } else {
David Brownelld2876d02008-02-04 22:28:20 -08002152 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002153 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002154 }
2155
2156 if (chip->request) {
2157 /* chip->request may sleep */
2158 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002159 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002160 spin_lock_irqsave(&gpio_lock, flags);
2161
2162 if (status < 0) {
2163 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07002164 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002165 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002166 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002167 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002168 if (chip->get_direction) {
2169 /* chip->get_direction may sleep */
2170 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002171 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002172 spin_lock_irqsave(&gpio_lock, flags);
2173 }
David Brownelld2876d02008-02-04 22:28:20 -08002174done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002175 spin_unlock_irqrestore(&gpio_lock, flags);
2176 return status;
2177}
2178
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002179/*
2180 * This descriptor validation needs to be inserted verbatim into each
2181 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002182 * macro to avoid endless duplication. If the desc is NULL it is an
2183 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002184 */
2185#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002186 if (!desc) \
2187 return 0; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002188 if (IS_ERR(desc)) { \
2189 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2190 return PTR_ERR(desc); \
2191 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002192 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002193 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002194 return -EINVAL; \
2195 } \
2196 if ( !desc->gdev->chip ) { \
2197 dev_warn(&desc->gdev->dev, \
2198 "%s: backing chip is gone\n", __func__); \
2199 return 0; \
2200 } } while (0)
2201
2202#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002203 if (!desc) \
2204 return; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002205 if (IS_ERR(desc)) { \
2206 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2207 return; \
2208 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002209 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002210 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002211 return; \
2212 } \
2213 if (!desc->gdev->chip) { \
2214 dev_warn(&desc->gdev->dev, \
2215 "%s: backing chip is gone\n", __func__); \
2216 return; \
2217 } } while (0)
2218
2219
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002220int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002221{
2222 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002223 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002224
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002225 VALIDATE_DESC(desc);
2226 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002227
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002228 if (try_module_get(gdev->owner)) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002229 status = gpiod_request_commit(desc, label);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002230 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002231 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002232 else
2233 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002234 }
2235
David Brownelld2876d02008-02-04 22:28:20 -08002236 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002237 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002238
David Brownelld2876d02008-02-04 22:28:20 -08002239 return status;
2240}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002241
Linus Walleijfac9d882017-09-26 20:58:28 +02002242static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002243{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002244 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002245 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002246 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002247
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002248 might_sleep();
2249
Alexandre Courbot372e7222013-02-03 01:29:29 +09002250 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002251
David Brownelld2876d02008-02-04 22:28:20 -08002252 spin_lock_irqsave(&gpio_lock, flags);
2253
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002254 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002255 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2256 if (chip->free) {
2257 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002258 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002259 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002260 spin_lock_irqsave(&gpio_lock, flags);
2261 }
David Brownelld2876d02008-02-04 22:28:20 -08002262 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002263 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002264 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302265 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302266 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002267 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002268 ret = true;
2269 }
David Brownelld2876d02008-02-04 22:28:20 -08002270
2271 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002272 return ret;
2273}
2274
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002275void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002276{
Linus Walleijfac9d882017-09-26 20:58:28 +02002277 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002278 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002279 put_device(&desc->gdev->dev);
2280 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002281 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002282 }
David Brownelld2876d02008-02-04 22:28:20 -08002283}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002284
David Brownelld2876d02008-02-04 22:28:20 -08002285/**
2286 * gpiochip_is_requested - return string iff signal was requested
2287 * @chip: controller managing the signal
2288 * @offset: of signal within controller's 0..(ngpio - 1) range
2289 *
2290 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002291 * The string returned is the label passed to gpio_request(); if none has been
2292 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002293 *
2294 * This function is for use by GPIO controller drivers. The label can
2295 * help with diagnostics, and knowing that the signal is used as a GPIO
2296 * can help avoid accidentally multiplexing it to another controller.
2297 */
2298const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2299{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002300 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002301
Dirk Behme48b59532015-08-18 18:02:32 +02002302 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002303 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002304
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002305 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002306
Alexandre Courbot372e7222013-02-03 01:29:29 +09002307 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002308 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002309 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002310}
2311EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2312
Mika Westerberg77c2d792014-03-10 14:54:50 +02002313/**
2314 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002315 * @chip: GPIO chip
2316 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002317 * @label: label for the GPIO
2318 *
2319 * Function allows GPIO chip drivers to request and use their own GPIO
2320 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2321 * function will not increase reference count of the GPIO chip module. This
2322 * allows the GPIO chip module to be unloaded as needed (we assume that the
2323 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002324 *
2325 * Returns:
2326 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2327 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002328 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002329struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2330 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002331{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002332 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2333 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002334
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002335 if (IS_ERR(desc)) {
2336 chip_err(chip, "failed to get GPIO descriptor\n");
2337 return desc;
2338 }
2339
Linus Walleijfac9d882017-09-26 20:58:28 +02002340 err = gpiod_request_commit(desc, label);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002341 if (err < 0)
2342 return ERR_PTR(err);
2343
2344 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002345}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002346EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002347
2348/**
2349 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2350 * @desc: GPIO descriptor to free
2351 *
2352 * Function frees the given GPIO requested previously with
2353 * gpiochip_request_own_desc().
2354 */
2355void gpiochip_free_own_desc(struct gpio_desc *desc)
2356{
2357 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002358 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002359}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002360EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002361
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002362/*
2363 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002364 * some cases this is done in early boot, before IRQs are enabled.
2365 *
2366 * As a rule these aren't called more than once (except for drivers
2367 * using the open-drain emulation idiom) so these are natural places
2368 * to accumulate extra debugging checks. Note that we can't (yet)
2369 * rely on gpio_request() having been called beforehand.
2370 */
2371
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002372/**
2373 * gpiod_direction_input - set the GPIO direction to input
2374 * @desc: GPIO to set to input
2375 *
2376 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2377 * be called safely on it.
2378 *
2379 * Return 0 in case of success, else an error code.
2380 */
2381int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002382{
David Brownelld2876d02008-02-04 22:28:20 -08002383 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002384 int status = -EINVAL;
2385
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002386 VALIDATE_DESC(desc);
2387 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002388
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002389 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002390 gpiod_warn(desc,
2391 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002392 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002393 return -EIO;
2394 }
2395
Alexandre Courbotd82da792014-07-22 16:17:43 +09002396 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002397 if (status == 0)
2398 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002399
Alexandre Courbot372e7222013-02-03 01:29:29 +09002400 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002401
David Brownelld2876d02008-02-04 22:28:20 -08002402 return status;
2403}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002404EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002405
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002406static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2407 enum pin_config_param mode)
2408{
2409 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2410
2411 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2412}
2413
Linus Walleijfac9d882017-09-26 20:58:28 +02002414static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002415{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002416 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002417 int val = !!value;
Linus Walleijc663e5f2016-03-22 10:51:16 +01002418 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002419
Linus Walleijc663e5f2016-03-22 10:51:16 +01002420 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002421 gpiod_warn(desc,
2422 "%s: missing set() or direction_output() operations\n",
2423 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002424 return -EIO;
2425 }
2426
Linus Walleijad177312016-11-13 23:02:44 +01002427 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002428 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002429 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002430 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002431 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2432 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002433}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002434
2435/**
2436 * gpiod_direction_output_raw - set the GPIO direction to output
2437 * @desc: GPIO to set to output
2438 * @value: initial output value of the GPIO
2439 *
2440 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2441 * be called safely on it. The initial value of the output must be specified
2442 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2443 *
2444 * Return 0 in case of success, else an error code.
2445 */
2446int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2447{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002448 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002449 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002450}
2451EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2452
2453/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302454 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002455 * @desc: GPIO to set to output
2456 * @value: initial output value of the GPIO
2457 *
2458 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2459 * be called safely on it. The initial value of the output must be specified
2460 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2461 * account.
2462 *
2463 * Return 0 in case of success, else an error code.
2464 */
2465int gpiod_direction_output(struct gpio_desc *desc, int value)
2466{
Linus Walleij02e47982017-09-26 21:20:23 +02002467 struct gpio_chip *gc = desc->gdev->chip;
2468 int ret;
2469
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002470 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002471 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2472 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002473 else
2474 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02002475
2476 /* GPIOs used for IRQs shall not be set as output */
2477 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2478 gpiod_err(desc,
2479 "%s: tried to set a GPIO tied to an IRQ as output\n",
2480 __func__);
2481 return -EIO;
2482 }
2483
2484 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2485 /* First see if we can enable open drain in hardware */
2486 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2487 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2488 if (!ret)
2489 goto set_output_value;
2490 /* Emulate open drain by not actively driving the line high */
2491 if (value)
2492 return gpiod_direction_input(desc);
2493 }
2494 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2495 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2496 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2497 if (!ret)
2498 goto set_output_value;
2499 /* Emulate open source by not actively driving the line low */
2500 if (!value)
2501 return gpiod_direction_input(desc);
2502 } else {
2503 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2504 PIN_CONFIG_DRIVE_PUSH_PULL);
2505 }
2506
2507set_output_value:
Linus Walleijfac9d882017-09-26 20:58:28 +02002508 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002509}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002510EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002511
Felipe Balbic4b5be92010-05-26 14:42:23 -07002512/**
Thierry Reding950d55f52017-07-24 16:57:22 +02002513 * gpiod_set_debounce - sets @debounce time for a GPIO
2514 * @desc: descriptor of the GPIO for which to set debounce time
2515 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002516 *
Thierry Reding950d55f52017-07-24 16:57:22 +02002517 * Returns:
2518 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2519 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002520 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002521int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002522{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002523 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002524 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002525
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002526 VALIDATE_DESC(desc);
2527 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002528 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01002529 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002530 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01002531 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002532 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002533 }
2534
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002535 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2536 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002537}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002538EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002539
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002540/**
2541 * gpiod_is_active_low - test whether a GPIO is active-low or not
2542 * @desc: the gpio descriptor to test
2543 *
2544 * Returns 1 if the GPIO is active-low, 0 otherwise.
2545 */
2546int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002547{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002548 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002549 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002550}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002551EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002552
2553/* I/O calls are only valid after configuration completed; the relevant
2554 * "is this a valid GPIO" error checks should already have been done.
2555 *
2556 * "Get" operations are often inlinable as reading a pin value register,
2557 * and masking the relevant bit in that register.
2558 *
2559 * When "set" operations are inlinable, they involve writing that mask to
2560 * one register to set a low value, or a different register to set it high.
2561 * Otherwise locking is needed, so there may be little value to inlining.
2562 *
2563 *------------------------------------------------------------------------
2564 *
2565 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2566 * have requested the GPIO. That can include implicit requesting by
2567 * a direction setting call. Marking a gpio as requested locks its chip
2568 * in memory, guaranteeing that these table lookups need no more locking
2569 * and that gpiochip_remove() will fail.
2570 *
2571 * REVISIT when debugging, consider adding some instrumentation to ensure
2572 * that the GPIO was actually requested.
2573 */
2574
Linus Walleijfac9d882017-09-26 20:58:28 +02002575static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002576{
2577 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002578 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002579 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002580
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002581 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002582 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002583 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002584 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002585 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002586 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002587}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002588
Lukas Wunnereec1d562017-10-12 12:40:10 +02002589static int gpio_chip_get_multiple(struct gpio_chip *chip,
2590 unsigned long *mask, unsigned long *bits)
2591{
2592 if (chip->get_multiple) {
2593 return chip->get_multiple(chip, mask, bits);
2594 } else if (chip->get) {
2595 int i, value;
2596
2597 for_each_set_bit(i, mask, chip->ngpio) {
2598 value = chip->get(chip, i);
2599 if (value < 0)
2600 return value;
2601 __assign_bit(i, bits, value);
2602 }
2603 return 0;
2604 }
2605 return -EIO;
2606}
2607
2608int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2609 unsigned int array_size,
2610 struct gpio_desc **desc_array,
2611 int *value_array)
2612{
2613 int i = 0;
2614
2615 while (i < array_size) {
2616 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2617 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2618 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2619 int first, j, ret;
2620
2621 if (!can_sleep)
2622 WARN_ON(chip->can_sleep);
2623
2624 /* collect all inputs belonging to the same chip */
2625 first = i;
2626 memset(mask, 0, sizeof(mask));
2627 do {
2628 const struct gpio_desc *desc = desc_array[i];
2629 int hwgpio = gpio_chip_hwgpio(desc);
2630
2631 __set_bit(hwgpio, mask);
2632 i++;
2633 } while ((i < array_size) &&
2634 (desc_array[i]->gdev->chip == chip));
2635
2636 ret = gpio_chip_get_multiple(chip, mask, bits);
2637 if (ret)
2638 return ret;
2639
2640 for (j = first; j < i; j++) {
2641 const struct gpio_desc *desc = desc_array[j];
2642 int hwgpio = gpio_chip_hwgpio(desc);
2643 int value = test_bit(hwgpio, bits);
2644
2645 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2646 value = !value;
2647 value_array[j] = value;
2648 trace_gpio_value(desc_to_gpio(desc), 1, value);
2649 }
2650 }
2651 return 0;
2652}
2653
David Brownelld2876d02008-02-04 22:28:20 -08002654/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002655 * gpiod_get_raw_value() - return a gpio's raw value
2656 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002657 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002658 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002659 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002660 *
2661 * This function should be called from contexts where we cannot sleep, and will
2662 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002663 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002664int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002665{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002666 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002667 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002668 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002669 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002670}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002671EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002672
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002673/**
2674 * gpiod_get_value() - return a gpio's value
2675 * @desc: gpio whose value will be returned
2676 *
2677 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002678 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002679 *
2680 * This function should be called from contexts where we cannot sleep, and will
2681 * complain if the GPIO chip functions potentially sleep.
2682 */
2683int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002684{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002685 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002686
2687 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002688 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002689 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002690
Linus Walleijfac9d882017-09-26 20:58:28 +02002691 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002692 if (value < 0)
2693 return value;
2694
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002695 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2696 value = !value;
2697
2698 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002699}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002700EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002701
Lukas Wunnereec1d562017-10-12 12:40:10 +02002702/**
2703 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2704 * @array_size: number of elements in the descriptor / value arrays
2705 * @desc_array: array of GPIO descriptors whose values will be read
2706 * @value_array: array to store the read values
2707 *
2708 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2709 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2710 * else an error code.
2711 *
2712 * This function should be called from contexts where we cannot sleep,
2713 * and it will complain if the GPIO chip functions potentially sleep.
2714 */
2715int gpiod_get_raw_array_value(unsigned int array_size,
2716 struct gpio_desc **desc_array, int *value_array)
2717{
2718 if (!desc_array)
2719 return -EINVAL;
2720 return gpiod_get_array_value_complex(true, false, array_size,
2721 desc_array, value_array);
2722}
2723EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2724
2725/**
2726 * gpiod_get_array_value() - read values from an array of GPIOs
2727 * @array_size: number of elements in the descriptor / value arrays
2728 * @desc_array: array of GPIO descriptors whose values will be read
2729 * @value_array: array to store the read values
2730 *
2731 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2732 * into account. Return 0 in case of success, else an error code.
2733 *
2734 * This function should be called from contexts where we cannot sleep,
2735 * and it will complain if the GPIO chip functions potentially sleep.
2736 */
2737int gpiod_get_array_value(unsigned int array_size,
2738 struct gpio_desc **desc_array, int *value_array)
2739{
2740 if (!desc_array)
2741 return -EINVAL;
2742 return gpiod_get_array_value_complex(false, false, array_size,
2743 desc_array, value_array);
2744}
2745EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2746
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302747/*
Linus Walleijfac9d882017-09-26 20:58:28 +02002748 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002749 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002750 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302751 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002752static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302753{
2754 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002755 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002756 int offset = gpio_chip_hwgpio(desc);
2757
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302758 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002759 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302760 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002761 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302762 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002763 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302764 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002765 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302766 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002767 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302768 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002769 gpiod_err(desc,
2770 "%s: Error in set_value for open drain err %d\n",
2771 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302772}
2773
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302774/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002775 * _gpio_set_open_source_value() - Set the open source gpio's value.
2776 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002777 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302778 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002779static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302780{
2781 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002782 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002783 int offset = gpio_chip_hwgpio(desc);
2784
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302785 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002786 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302787 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002788 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302789 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002790 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302791 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002792 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302793 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002794 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302795 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002796 gpiod_err(desc,
2797 "%s: Error in set_value for open source err %d\n",
2798 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302799}
2800
Linus Walleijfac9d882017-09-26 20:58:28 +02002801static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002802{
2803 struct gpio_chip *chip;
2804
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002805 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002806 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002807 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002808}
2809
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002810/*
2811 * set multiple outputs on the same chip;
2812 * use the chip's set_multiple function if available;
2813 * otherwise set the outputs sequentially;
2814 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2815 * defines which outputs are to be changed
2816 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2817 * defines the values the outputs specified by mask are to be set to
2818 */
2819static void gpio_chip_set_multiple(struct gpio_chip *chip,
2820 unsigned long *mask, unsigned long *bits)
2821{
2822 if (chip->set_multiple) {
2823 chip->set_multiple(chip, mask, bits);
2824 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02002825 unsigned int i;
2826
2827 /* set outputs if the corresponding mask bit is set */
2828 for_each_set_bit(i, mask, chip->ngpio)
2829 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002830 }
2831}
2832
Linus Walleij44c72882016-04-24 11:36:59 +02002833void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2834 unsigned int array_size,
2835 struct gpio_desc **desc_array,
2836 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002837{
2838 int i = 0;
2839
2840 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002841 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002842 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2843 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2844 int count = 0;
2845
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002846 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002847 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002848
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002849 memset(mask, 0, sizeof(mask));
2850 do {
2851 struct gpio_desc *desc = desc_array[i];
2852 int hwgpio = gpio_chip_hwgpio(desc);
2853 int value = value_array[i];
2854
2855 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2856 value = !value;
2857 trace_gpio_value(desc_to_gpio(desc), 0, value);
2858 /*
2859 * collect all normal outputs belonging to the same chip
2860 * open drain and open source outputs are set individually
2861 */
Linus Walleij02e47982017-09-26 21:20:23 +02002862 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002863 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002864 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002865 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002866 } else {
2867 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002868 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002869 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002870 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002871 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002872 count++;
2873 }
2874 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002875 } while ((i < array_size) &&
2876 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002877 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002878 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002879 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002880 }
2881}
2882
David Brownelld2876d02008-02-04 22:28:20 -08002883/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002884 * gpiod_set_raw_value() - assign a gpio's raw value
2885 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002886 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002887 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002888 * Set the raw value of the GPIO, i.e. the value of its physical line without
2889 * regard for its ACTIVE_LOW status.
2890 *
2891 * This function should be called from contexts where we cannot sleep, and will
2892 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002893 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002894void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002895{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002896 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002897 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002898 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002899 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002900}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002901EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002902
2903/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002904 * gpiod_set_value() - assign a gpio's value
2905 * @desc: gpio whose value will be assigned
2906 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002907 *
Linus Walleij02e47982017-09-26 21:20:23 +02002908 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
2909 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002910 *
2911 * This function should be called from contexts where we cannot sleep, and will
2912 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002913 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002914void gpiod_set_value(struct gpio_desc *desc, int value)
2915{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002916 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002917 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002918 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002919 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2920 value = !value;
Linus Walleij02e47982017-09-26 21:20:23 +02002921 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2922 gpio_set_open_drain_value_commit(desc, value);
2923 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2924 gpio_set_open_source_value_commit(desc, value);
2925 else
2926 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002927}
2928EXPORT_SYMBOL_GPL(gpiod_set_value);
2929
2930/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002931 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002932 * @array_size: number of elements in the descriptor / value arrays
2933 * @desc_array: array of GPIO descriptors whose values will be assigned
2934 * @value_array: array of values to assign
2935 *
2936 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2937 * without regard for their ACTIVE_LOW status.
2938 *
2939 * This function should be called from contexts where we cannot sleep, and will
2940 * complain if the GPIO chip functions potentially sleep.
2941 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002942void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002943 struct gpio_desc **desc_array, int *value_array)
2944{
2945 if (!desc_array)
2946 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002947 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2948 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002949}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002950EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002951
2952/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002953 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002954 * @array_size: number of elements in the descriptor / value arrays
2955 * @desc_array: array of GPIO descriptors whose values will be assigned
2956 * @value_array: array of values to assign
2957 *
2958 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2959 * into account.
2960 *
2961 * This function should be called from contexts where we cannot sleep, and will
2962 * complain if the GPIO chip functions potentially sleep.
2963 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002964void gpiod_set_array_value(unsigned int array_size,
2965 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002966{
2967 if (!desc_array)
2968 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002969 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2970 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002971}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002972EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002973
2974/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002975 * gpiod_cansleep() - report whether gpio value access may sleep
2976 * @desc: gpio to check
2977 *
2978 */
2979int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002980{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002981 VALIDATE_DESC(desc);
2982 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002983}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002984EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002985
David Brownell0f6d5042008-10-15 22:03:14 -07002986/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002987 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2988 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002989 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002990 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2991 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002992 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002993int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002994{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002995 struct gpio_chip *chip;
2996 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002997
Linus Walleij79bb71b2016-06-15 22:57:38 +02002998 /*
2999 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3000 * requires this function to not return zero on an invalid descriptor
3001 * but rather a negative error number.
3002 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003003 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003004 return -EINVAL;
3005
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003006 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003007 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003008 if (chip->to_irq) {
3009 int retirq = chip->to_irq(chip, offset);
3010
3011 /* Zero means NO_IRQ */
3012 if (!retirq)
3013 return -ENXIO;
3014
3015 return retirq;
3016 }
3017 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003018}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003019EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003020
Linus Walleijd468bf92013-09-24 11:54:38 +02003021/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003022 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003023 * @chip: the chip the GPIO to lock belongs to
3024 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003025 *
3026 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003027 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003028 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003029int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003030{
Linus Walleij9c102802016-05-25 10:56:03 +02003031 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003032
Linus Walleij9c102802016-05-25 10:56:03 +02003033 desc = gpiochip_get_desc(chip, offset);
3034 if (IS_ERR(desc))
3035 return PTR_ERR(desc);
3036
Linus Walleij60f83392016-11-12 15:01:09 +01003037 /*
3038 * If it's fast: flush the direction setting if something changed
3039 * behind our back
3040 */
3041 if (!chip->can_sleep && chip->get_direction) {
Linus Walleij9c102802016-05-25 10:56:03 +02003042 int dir = chip->get_direction(chip, offset);
3043
3044 if (dir)
3045 clear_bit(FLAG_IS_OUT, &desc->flags);
3046 else
3047 set_bit(FLAG_IS_OUT, &desc->flags);
3048 }
3049
3050 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003051 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02003052 "%s: tried to flag a GPIO set as output for IRQ\n",
3053 __func__);
3054 return -EIO;
3055 }
3056
Linus Walleij9c102802016-05-25 10:56:03 +02003057 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003058
3059 /*
3060 * If the consumer has not set up a label (such as when the
3061 * IRQ is referenced from .to_irq()) we set up a label here
3062 * so it is clear this is used as an interrupt.
3063 */
3064 if (!desc->label)
3065 desc_set_label(desc, "interrupt");
3066
Linus Walleijd468bf92013-09-24 11:54:38 +02003067 return 0;
3068}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003069EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003070
Linus Walleijd468bf92013-09-24 11:54:38 +02003071/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003072 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003073 * @chip: the chip the GPIO to lock belongs to
3074 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003075 *
3076 * This is used directly by GPIO drivers that want to indicate
3077 * that a certain GPIO is no longer used exclusively for IRQ.
3078 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003079void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003080{
Linus Walleij3940c342016-11-14 00:09:07 +01003081 struct gpio_desc *desc;
3082
3083 desc = gpiochip_get_desc(chip, offset);
3084 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003085 return;
3086
Linus Walleij3940c342016-11-14 00:09:07 +01003087 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3088
3089 /* If we only had this marking, erase it */
3090 if (desc->label && !strcmp(desc->label, "interrupt"))
3091 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003092}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003093EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003094
Linus Walleij6cee3822016-02-11 20:16:45 +01003095bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3096{
3097 if (offset >= chip->ngpio)
3098 return false;
3099
3100 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3101}
3102EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3103
Linus Walleij143b65d2016-02-16 15:41:42 +01003104bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3105{
3106 if (offset >= chip->ngpio)
3107 return false;
3108
3109 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3110}
3111EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3112
3113bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3114{
3115 if (offset >= chip->ngpio)
3116 return false;
3117
3118 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3119}
3120EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3121
Charles Keepax05f479b2017-05-23 15:47:29 +01003122bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3123{
3124 if (offset >= chip->ngpio)
3125 return false;
3126
Andrew Jeffery2cbfca62017-10-20 13:27:58 +10303127 return !test_bit(FLAG_SLEEP_MAY_LOSE_VALUE,
Charles Keepax05f479b2017-05-23 15:47:29 +01003128 &chip->gpiodev->descs[offset].flags);
3129}
3130EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3131
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003132/**
3133 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3134 * @desc: gpio whose value will be returned
3135 *
3136 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003137 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003138 *
3139 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003140 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003141int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003142{
David Brownelld2876d02008-02-04 22:28:20 -08003143 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003144 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003145 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08003146}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003147EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003148
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003149/**
3150 * gpiod_get_value_cansleep() - return a gpio's value
3151 * @desc: gpio whose value will be returned
3152 *
3153 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003154 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003155 *
3156 * This function is to be called from contexts that can sleep.
3157 */
3158int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003159{
David Brownelld2876d02008-02-04 22:28:20 -08003160 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003161
3162 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003163 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003164 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003165 if (value < 0)
3166 return value;
3167
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003168 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3169 value = !value;
3170
David Brownelld2876d02008-02-04 22:28:20 -08003171 return value;
3172}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003173EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003174
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003175/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02003176 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3177 * @array_size: number of elements in the descriptor / value arrays
3178 * @desc_array: array of GPIO descriptors whose values will be read
3179 * @value_array: array to store the read values
3180 *
3181 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3182 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3183 * else an error code.
3184 *
3185 * This function is to be called from contexts that can sleep.
3186 */
3187int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3188 struct gpio_desc **desc_array,
3189 int *value_array)
3190{
3191 might_sleep_if(extra_checks);
3192 if (!desc_array)
3193 return -EINVAL;
3194 return gpiod_get_array_value_complex(true, true, array_size,
3195 desc_array, value_array);
3196}
3197EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3198
3199/**
3200 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3201 * @array_size: number of elements in the descriptor / value arrays
3202 * @desc_array: array of GPIO descriptors whose values will be read
3203 * @value_array: array to store the read values
3204 *
3205 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3206 * into account. Return 0 in case of success, else an error code.
3207 *
3208 * This function is to be called from contexts that can sleep.
3209 */
3210int gpiod_get_array_value_cansleep(unsigned int array_size,
3211 struct gpio_desc **desc_array,
3212 int *value_array)
3213{
3214 might_sleep_if(extra_checks);
3215 if (!desc_array)
3216 return -EINVAL;
3217 return gpiod_get_array_value_complex(false, true, array_size,
3218 desc_array, value_array);
3219}
3220EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3221
3222/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003223 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3224 * @desc: gpio whose value will be assigned
3225 * @value: value to assign
3226 *
3227 * Set the raw value of the GPIO, i.e. the value of its physical line without
3228 * regard for its ACTIVE_LOW status.
3229 *
3230 * This function is to be called from contexts that can sleep.
3231 */
3232void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003233{
David Brownelld2876d02008-02-04 22:28:20 -08003234 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003235 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003236 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003237}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003238EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003239
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003240/**
3241 * gpiod_set_value_cansleep() - assign a gpio's value
3242 * @desc: gpio whose value will be assigned
3243 * @value: value to assign
3244 *
3245 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3246 * account
3247 *
3248 * This function is to be called from contexts that can sleep.
3249 */
3250void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003251{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003252 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003253 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003254 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3255 value = !value;
Linus Walleijfac9d882017-09-26 20:58:28 +02003256 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003257}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003258EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003259
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003260/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003261 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003262 * @array_size: number of elements in the descriptor / value arrays
3263 * @desc_array: array of GPIO descriptors whose values will be assigned
3264 * @value_array: array of values to assign
3265 *
3266 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3267 * without regard for their ACTIVE_LOW status.
3268 *
3269 * This function is to be called from contexts that can sleep.
3270 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003271void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3272 struct gpio_desc **desc_array,
3273 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003274{
3275 might_sleep_if(extra_checks);
3276 if (!desc_array)
3277 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003278 gpiod_set_array_value_complex(true, true, array_size, desc_array,
3279 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003280}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003281EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003282
3283/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003284 * gpiod_add_lookup_tables() - register GPIO device consumers
3285 * @tables: list of tables of consumers to register
3286 * @n: number of tables in the list
3287 */
3288void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3289{
3290 unsigned int i;
3291
3292 mutex_lock(&gpio_lookup_lock);
3293
3294 for (i = 0; i < n; i++)
3295 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3296
3297 mutex_unlock(&gpio_lookup_lock);
3298}
3299
3300/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003301 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003302 * @array_size: number of elements in the descriptor / value arrays
3303 * @desc_array: array of GPIO descriptors whose values will be assigned
3304 * @value_array: array of values to assign
3305 *
3306 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3307 * into account.
3308 *
3309 * This function is to be called from contexts that can sleep.
3310 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003311void gpiod_set_array_value_cansleep(unsigned int array_size,
3312 struct gpio_desc **desc_array,
3313 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003314{
3315 might_sleep_if(extra_checks);
3316 if (!desc_array)
3317 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003318 gpiod_set_array_value_complex(false, true, array_size, desc_array,
3319 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003320}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003321EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003322
3323/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003324 * gpiod_add_lookup_table() - register GPIO device consumers
3325 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003326 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003327void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003328{
3329 mutex_lock(&gpio_lookup_lock);
3330
Alexandre Courbotad824782013-12-03 12:20:11 +09003331 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003332
3333 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003334}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003335EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003336
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303337/**
3338 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3339 * @table: table of consumers to unregister
3340 */
3341void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3342{
3343 mutex_lock(&gpio_lookup_lock);
3344
3345 list_del(&table->list);
3346
3347 mutex_unlock(&gpio_lookup_lock);
3348}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003349EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303350
Alexandre Courbotad824782013-12-03 12:20:11 +09003351static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3352{
3353 const char *dev_id = dev ? dev_name(dev) : NULL;
3354 struct gpiod_lookup_table *table;
3355
3356 mutex_lock(&gpio_lookup_lock);
3357
3358 list_for_each_entry(table, &gpio_lookup_list, list) {
3359 if (table->dev_id && dev_id) {
3360 /*
3361 * Valid strings on both ends, must be identical to have
3362 * a match
3363 */
3364 if (!strcmp(table->dev_id, dev_id))
3365 goto found;
3366 } else {
3367 /*
3368 * One of the pointers is NULL, so both must be to have
3369 * a match
3370 */
3371 if (dev_id == table->dev_id)
3372 goto found;
3373 }
3374 }
3375 table = NULL;
3376
3377found:
3378 mutex_unlock(&gpio_lookup_lock);
3379 return table;
3380}
3381
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003382static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003383 unsigned int idx,
3384 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003385{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003386 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003387 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003388 struct gpiod_lookup *p;
3389
Alexandre Courbotad824782013-12-03 12:20:11 +09003390 table = gpiod_find_lookup_table(dev);
3391 if (!table)
3392 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003393
Alexandre Courbotad824782013-12-03 12:20:11 +09003394 for (p = &table->table[0]; p->chip_label; p++) {
3395 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003396
Alexandre Courbotad824782013-12-03 12:20:11 +09003397 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003398 if (p->idx != idx)
3399 continue;
3400
Alexandre Courbotad824782013-12-03 12:20:11 +09003401 /* If the lookup entry has a con_id, require exact match */
3402 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3403 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003404
Alexandre Courbotad824782013-12-03 12:20:11 +09003405 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003406
Alexandre Courbotad824782013-12-03 12:20:11 +09003407 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003408 dev_err(dev, "cannot find GPIO chip %s\n",
3409 p->chip_label);
3410 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003411 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003412
Alexandre Courbotad824782013-12-03 12:20:11 +09003413 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003414 dev_err(dev,
3415 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3416 idx, chip->ngpio, chip->label);
3417 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003418 }
3419
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003420 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003421 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003422
3423 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003424 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003425
3426 return desc;
3427}
3428
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003429static int dt_gpio_count(struct device *dev, const char *con_id)
3430{
3431 int ret;
3432 char propname[32];
3433 unsigned int i;
3434
3435 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3436 if (con_id)
3437 snprintf(propname, sizeof(propname), "%s-%s",
3438 con_id, gpio_suffixes[i]);
3439 else
3440 snprintf(propname, sizeof(propname), "%s",
3441 gpio_suffixes[i]);
3442
3443 ret = of_gpio_named_count(dev->of_node, propname);
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003444 if (ret > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003445 break;
3446 }
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003447 return ret ? ret : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003448}
3449
3450static int platform_gpio_count(struct device *dev, const char *con_id)
3451{
3452 struct gpiod_lookup_table *table;
3453 struct gpiod_lookup *p;
3454 unsigned int count = 0;
3455
3456 table = gpiod_find_lookup_table(dev);
3457 if (!table)
3458 return -ENOENT;
3459
3460 for (p = &table->table[0]; p->chip_label; p++) {
3461 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3462 (!con_id && !p->con_id))
3463 count++;
3464 }
3465 if (!count)
3466 return -ENOENT;
3467
3468 return count;
3469}
3470
3471/**
3472 * gpiod_count - return the number of GPIOs associated with a device / function
3473 * or -ENOENT if no GPIO has been assigned to the requested function
3474 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3475 * @con_id: function within the GPIO consumer
3476 */
3477int gpiod_count(struct device *dev, const char *con_id)
3478{
3479 int count = -ENOENT;
3480
3481 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3482 count = dt_gpio_count(dev, con_id);
3483 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3484 count = acpi_gpio_count(dev, con_id);
3485
3486 if (count < 0)
3487 count = platform_gpio_count(dev, con_id);
3488
3489 return count;
3490}
3491EXPORT_SYMBOL_GPL(gpiod_count);
3492
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003493/**
Thierry Reding08791622014-04-25 16:54:22 +02003494 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003495 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003496 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003497 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003498 *
3499 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003500 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003501 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003502 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003503struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003504 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003505{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003506 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003507}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003508EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003509
3510/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003511 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3512 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3513 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003514 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003515 *
3516 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3517 * the requested function it will return NULL. This is convenient for drivers
3518 * that need to handle optional GPIOs.
3519 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003520struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003521 const char *con_id,
3522 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003523{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003524 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003525}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003526EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003527
Benoit Parrotf625d462015-02-02 11:44:44 -06003528
3529/**
3530 * gpiod_configure_flags - helper function to configure a given GPIO
3531 * @desc: gpio whose value will be assigned
3532 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003533 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3534 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003535 * @dflags: gpiod_flags - optional GPIO initialization flags
3536 *
3537 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3538 * requested function and/or index, or another IS_ERR() code if an error
3539 * occurred while trying to acquire the GPIO.
3540 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03003541int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003542 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003543{
3544 int status;
3545
Johan Hovold85b03b32016-07-03 18:32:05 +02003546 if (lflags & GPIO_ACTIVE_LOW)
3547 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3548 if (lflags & GPIO_OPEN_DRAIN)
3549 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3550 if (lflags & GPIO_OPEN_SOURCE)
3551 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jeffery2cbfca62017-10-20 13:27:58 +10303552 if (lflags & GPIO_SLEEP_MAY_LOSE_VALUE)
3553 set_bit(FLAG_SLEEP_MAY_LOSE_VALUE, &desc->flags);
Johan Hovold85b03b32016-07-03 18:32:05 +02003554
Benoit Parrotf625d462015-02-02 11:44:44 -06003555 /* No particular flag request, return here... */
3556 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3557 pr_debug("no flags found for %s\n", con_id);
3558 return 0;
3559 }
3560
3561 /* Process flags */
3562 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3563 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01003564 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06003565 else
3566 status = gpiod_direction_input(desc);
3567
3568 return status;
3569}
3570
Thierry Reding29a1f2332014-04-25 17:10:06 +02003571/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003572 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003573 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003574 * @con_id: function within the GPIO consumer
3575 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003576 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003577 *
3578 * This variant of gpiod_get() allows to access GPIOs other than the first
3579 * defined one for functions that define several GPIOs.
3580 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003581 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3582 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003583 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003584 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003585struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003586 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003587 unsigned int idx,
3588 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003589{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003590 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003591 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003592 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003593
3594 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3595
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003596 if (dev) {
3597 /* Using device tree? */
3598 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3599 dev_dbg(dev, "using device tree for GPIO lookup\n");
3600 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3601 } else if (ACPI_COMPANION(dev)) {
3602 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003603 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003604 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003605 }
3606
3607 /*
3608 * Either we are not using DT or ACPI, or their lookup did not return
3609 * a result. In that case, use platform lookup as a fallback.
3610 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003611 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003612 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003613 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003614 }
3615
3616 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003617 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003618 return desc;
3619 }
3620
3621 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003622 if (status < 0)
3623 return ERR_PTR(status);
3624
Johan Hovold85b03b32016-07-03 18:32:05 +02003625 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003626 if (status < 0) {
3627 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3628 gpiod_put(desc);
3629 return ERR_PTR(status);
3630 }
3631
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003632 return desc;
3633}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003634EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003635
3636/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003637 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3638 * @fwnode: handle of the firmware node
3639 * @propname: name of the firmware property representing the GPIO
Boris Brezillon537b94d2017-02-02 14:53:11 +01003640 * @index: index of the GPIO to obtain in the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003641 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02003642 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02003643 *
3644 * This function can be used for drivers that get their configuration
3645 * from firmware.
3646 *
3647 * Function properly finds the corresponding GPIO using whatever is the
3648 * underlying firmware interface and then makes sure that the GPIO
3649 * descriptor is requested before it is returned to the caller.
3650 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003651 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02003652 * On successful request the GPIO pin is configured in accordance with
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003653 * provided @dflags.
3654 *
Mika Westerberg40b73182014-10-21 13:33:59 +02003655 * In case of error an ERR_PTR() is returned.
3656 */
3657struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Boris Brezillon537b94d2017-02-02 14:53:11 +01003658 const char *propname, int index,
Alexander Steinb2987d72017-01-12 17:39:24 +01003659 enum gpiod_flags dflags,
3660 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02003661{
3662 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003663 unsigned long lflags = 0;
Mika Westerberg40b73182014-10-21 13:33:59 +02003664 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003665 bool single_ended = false;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303666 bool open_drain = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003667 int ret;
3668
3669 if (!fwnode)
3670 return ERR_PTR(-EINVAL);
3671
3672 if (is_of_node(fwnode)) {
3673 enum of_gpio_flags flags;
3674
Boris Brezillon537b94d2017-02-02 14:53:11 +01003675 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname,
3676 index, &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003677 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003678 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003679 single_ended = flags & OF_GPIO_SINGLE_ENDED;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303680 open_drain = flags & OF_GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003681 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003682 } else if (is_acpi_node(fwnode)) {
3683 struct acpi_gpio_info info;
3684
Boris Brezillon537b94d2017-02-02 14:53:11 +01003685 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003686 if (!IS_ERR(desc)) {
Christophe RICARD52044722015-12-23 23:25:34 +01003687 active_low = info.polarity == GPIO_ACTIVE_LOW;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003688 ret = acpi_gpio_update_gpiod_flags(&dflags, info.flags);
3689 if (ret)
3690 pr_debug("Override GPIO initialization flags\n");
3691 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003692 }
3693
3694 if (IS_ERR(desc))
3695 return desc;
3696
Alexander Steinb2987d72017-01-12 17:39:24 +01003697 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02003698 if (ret)
3699 return ERR_PTR(ret);
3700
Mika Westerberg40b73182014-10-21 13:33:59 +02003701 if (active_low)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003702 lflags |= GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003703
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003704 if (single_ended) {
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303705 if (open_drain)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003706 lflags |= GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003707 else
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003708 lflags |= GPIO_OPEN_SOURCE;
3709 }
3710
3711 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3712 if (ret < 0) {
3713 gpiod_put(desc);
3714 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003715 }
3716
Mika Westerberg40b73182014-10-21 13:33:59 +02003717 return desc;
3718}
3719EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3720
3721/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003722 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3723 * function
3724 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3725 * @con_id: function within the GPIO consumer
3726 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003727 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003728 *
3729 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3730 * specified index was assigned to the requested function it will return NULL.
3731 * This is convenient for drivers that need to handle optional GPIOs.
3732 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003733struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003734 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003735 unsigned int index,
3736 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003737{
3738 struct gpio_desc *desc;
3739
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003740 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003741 if (IS_ERR(desc)) {
3742 if (PTR_ERR(desc) == -ENOENT)
3743 return NULL;
3744 }
3745
3746 return desc;
3747}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003748EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003749
3750/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003751 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3752 * @desc: gpio whose value will be assigned
3753 * @name: gpio line name
3754 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3755 * of_get_gpio_hog()
3756 * @dflags: gpiod_flags - optional GPIO initialization flags
3757 */
3758int gpiod_hog(struct gpio_desc *desc, const char *name,
3759 unsigned long lflags, enum gpiod_flags dflags)
3760{
3761 struct gpio_chip *chip;
3762 struct gpio_desc *local_desc;
3763 int hwnum;
3764 int status;
3765
3766 chip = gpiod_to_chip(desc);
3767 hwnum = gpio_chip_hwgpio(desc);
3768
3769 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3770 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303771 status = PTR_ERR(local_desc);
3772 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3773 name, chip->label, hwnum, status);
3774 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003775 }
3776
Johan Hovold85b03b32016-07-03 18:32:05 +02003777 status = gpiod_configure_flags(desc, name, lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003778 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303779 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3780 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003781 gpiochip_free_own_desc(desc);
3782 return status;
3783 }
3784
3785 /* Mark GPIO as hogged so it can be identified and removed later */
3786 set_bit(FLAG_IS_HOGGED, &desc->flags);
3787
3788 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3789 desc_to_gpio(desc), name,
3790 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3791 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3792 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3793
3794 return 0;
3795}
3796
3797/**
3798 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3799 * @chip: gpio chip to act on
3800 *
3801 * This is only used by of_gpiochip_remove to free hogged gpios
3802 */
3803static void gpiochip_free_hogs(struct gpio_chip *chip)
3804{
3805 int id;
3806
3807 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003808 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3809 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003810 }
3811}
3812
3813/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003814 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3815 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3816 * @con_id: function within the GPIO consumer
3817 * @flags: optional GPIO initialization flags
3818 *
3819 * This function acquires all the GPIOs defined under a given function.
3820 *
3821 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3822 * no GPIO has been assigned to the requested function, or another IS_ERR()
3823 * code if an error occurred while trying to acquire the GPIOs.
3824 */
3825struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3826 const char *con_id,
3827 enum gpiod_flags flags)
3828{
3829 struct gpio_desc *desc;
3830 struct gpio_descs *descs;
3831 int count;
3832
3833 count = gpiod_count(dev, con_id);
3834 if (count < 0)
3835 return ERR_PTR(count);
3836
3837 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3838 GFP_KERNEL);
3839 if (!descs)
3840 return ERR_PTR(-ENOMEM);
3841
3842 for (descs->ndescs = 0; descs->ndescs < count; ) {
3843 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3844 if (IS_ERR(desc)) {
3845 gpiod_put_array(descs);
3846 return ERR_CAST(desc);
3847 }
3848 descs->desc[descs->ndescs] = desc;
3849 descs->ndescs++;
3850 }
3851 return descs;
3852}
3853EXPORT_SYMBOL_GPL(gpiod_get_array);
3854
3855/**
3856 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3857 * function
3858 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3859 * @con_id: function within the GPIO consumer
3860 * @flags: optional GPIO initialization flags
3861 *
3862 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3863 * assigned to the requested function it will return NULL.
3864 */
3865struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3866 const char *con_id,
3867 enum gpiod_flags flags)
3868{
3869 struct gpio_descs *descs;
3870
3871 descs = gpiod_get_array(dev, con_id, flags);
3872 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3873 return NULL;
3874
3875 return descs;
3876}
3877EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3878
3879/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003880 * gpiod_put - dispose of a GPIO descriptor
3881 * @desc: GPIO descriptor to dispose of
3882 *
3883 * No descriptor can be used after gpiod_put() has been called on it.
3884 */
3885void gpiod_put(struct gpio_desc *desc)
3886{
3887 gpiod_free(desc);
3888}
3889EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003890
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003891/**
3892 * gpiod_put_array - dispose of multiple GPIO descriptors
3893 * @descs: struct gpio_descs containing an array of descriptors
3894 */
3895void gpiod_put_array(struct gpio_descs *descs)
3896{
3897 unsigned int i;
3898
3899 for (i = 0; i < descs->ndescs; i++)
3900 gpiod_put(descs->desc[i]);
3901
3902 kfree(descs);
3903}
3904EXPORT_SYMBOL_GPL(gpiod_put_array);
3905
Linus Walleij3c702e92015-10-21 15:29:53 +02003906static int __init gpiolib_dev_init(void)
3907{
3908 int ret;
3909
3910 /* Register GPIO sysfs bus */
3911 ret = bus_register(&gpio_bus_type);
3912 if (ret < 0) {
3913 pr_err("gpiolib: could not register GPIO bus type\n");
3914 return ret;
3915 }
3916
3917 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3918 if (ret < 0) {
3919 pr_err("gpiolib: failed to allocate char dev region\n");
3920 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003921 } else {
3922 gpiolib_initialized = true;
3923 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003924 }
3925 return ret;
3926}
3927core_initcall(gpiolib_dev_init);
3928
David Brownelld2876d02008-02-04 22:28:20 -08003929#ifdef CONFIG_DEBUG_FS
3930
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003931static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003932{
3933 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003934 struct gpio_chip *chip = gdev->chip;
3935 unsigned gpio = gdev->base;
3936 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003937 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003938 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003939
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003940 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003941 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3942 if (gdesc->name) {
3943 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3944 gpio, gdesc->name);
3945 }
David Brownelld2876d02008-02-04 22:28:20 -08003946 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003947 }
David Brownelld2876d02008-02-04 22:28:20 -08003948
Alexandre Courbot372e7222013-02-03 01:29:29 +09003949 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003950 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003951 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003952 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3953 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003954 is_out ? "out" : "in ",
3955 chip->get
3956 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003957 : "? ",
3958 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003959 seq_printf(s, "\n");
3960 }
3961}
3962
Thierry Redingf9c4a312012-04-12 13:26:01 +02003963static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003964{
Grant Likely362432a2013-02-09 09:41:49 +00003965 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003966 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003967 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003968
3969 s->private = "";
3970
Grant Likely362432a2013-02-09 09:41:49 +00003971 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003972 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003973 if (index-- == 0) {
3974 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003975 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003976 }
3977 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003978
3979 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003980}
3981
3982static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3983{
Grant Likely362432a2013-02-09 09:41:49 +00003984 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003985 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003986 void *ret = NULL;
3987
Grant Likely362432a2013-02-09 09:41:49 +00003988 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003989 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003990 ret = NULL;
3991 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003992 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003993 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003994
3995 s->private = "\n";
3996 ++*pos;
3997
3998 return ret;
3999}
4000
4001static void gpiolib_seq_stop(struct seq_file *s, void *v)
4002{
4003}
4004
4005static int gpiolib_seq_show(struct seq_file *s, void *v)
4006{
Linus Walleijff2b1352015-10-20 11:10:38 +02004007 struct gpio_device *gdev = v;
4008 struct gpio_chip *chip = gdev->chip;
4009 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004010
Linus Walleijff2b1352015-10-20 11:10:38 +02004011 if (!chip) {
4012 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4013 dev_name(&gdev->dev));
4014 return 0;
4015 }
4016
4017 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4018 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004019 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02004020 parent = chip->parent;
4021 if (parent)
4022 seq_printf(s, ", parent: %s/%s",
4023 parent->bus ? parent->bus->name : "no-bus",
4024 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02004025 if (chip->label)
4026 seq_printf(s, ", %s", chip->label);
4027 if (chip->can_sleep)
4028 seq_printf(s, ", can sleep");
4029 seq_printf(s, ":\n");
4030
4031 if (chip->dbg_show)
4032 chip->dbg_show(s, chip);
4033 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004034 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02004035
David Brownelld2876d02008-02-04 22:28:20 -08004036 return 0;
4037}
4038
Thierry Redingf9c4a312012-04-12 13:26:01 +02004039static const struct seq_operations gpiolib_seq_ops = {
4040 .start = gpiolib_seq_start,
4041 .next = gpiolib_seq_next,
4042 .stop = gpiolib_seq_stop,
4043 .show = gpiolib_seq_show,
4044};
4045
David Brownelld2876d02008-02-04 22:28:20 -08004046static int gpiolib_open(struct inode *inode, struct file *file)
4047{
Thierry Redingf9c4a312012-04-12 13:26:01 +02004048 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08004049}
4050
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004051static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02004052 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08004053 .open = gpiolib_open,
4054 .read = seq_read,
4055 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02004056 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08004057};
4058
4059static int __init gpiolib_debugfs_init(void)
4060{
4061 /* /sys/kernel/debug/gpio */
4062 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4063 NULL, NULL, &gpiolib_operations);
4064 return 0;
4065}
4066subsys_initcall(gpiolib_debugfs_init);
4067
4068#endif /* DEBUG_FS */