blob: 389257f97e457286889429e5d1d8aeacfe0631f4 [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,
1781 0, ops, gpiochip);
1782 if (!gpiochip->irq.domain)
1783 return -EINVAL;
1784
1785 /*
1786 * It is possible for a driver to override this, but only if the
1787 * alternative functions are both implemented.
1788 */
1789 if (!irqchip->irq_request_resources &&
1790 !irqchip->irq_release_resources) {
1791 irqchip->irq_request_resources = gpiochip_irq_reqres;
1792 irqchip->irq_release_resources = gpiochip_irq_relres;
1793 }
1794
1795 if (gpiochip->irq.parent_handler) {
1796 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1797
1798 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1799 /*
1800 * The parent IRQ chip is already using the chip_data
1801 * for this IRQ chip, so our callbacks simply use the
1802 * handler_data.
1803 */
1804 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1805 gpiochip->irq.parent_handler,
1806 data);
1807 }
Thierry Redinge0d89722017-11-07 19:15:54 +01001808 }
1809
1810 acpi_gpiochip_request_interrupts(gpiochip);
1811
1812 return 0;
1813}
1814
1815/**
Linus Walleij14250522014-03-25 10:40:18 +01001816 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1817 * @gpiochip: the gpiochip to remove the irqchip from
1818 *
1819 * This is called only from gpiochip_remove()
1820 */
1821static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1822{
Thierry Reding39e5f092017-11-07 19:15:50 +01001823 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001824
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001825 acpi_gpiochip_free_interrupts(gpiochip);
1826
Thierry Redinge0d89722017-11-07 19:15:54 +01001827 if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001828 struct gpio_irq_chip *irq = &gpiochip->irq;
1829 unsigned int i;
1830
1831 for (i = 0; i < irq->num_parents; i++)
1832 irq_set_chained_handler_and_data(irq->parents[i],
1833 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001834 }
1835
Linus Walleijc3626fd2014-03-28 20:42:01 +01001836 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001837 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001838 unsigned int irq;
1839
Mika Westerberg79b804c2016-09-20 15:15:21 +03001840 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1841 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1842 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001843
1844 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1845 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001846 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001847
1848 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001849 }
Linus Walleij14250522014-03-25 10:40:18 +01001850
Thierry Redingda80ff82017-11-07 19:15:46 +01001851 if (gpiochip->irq.chip) {
1852 gpiochip->irq.chip->irq_request_resources = NULL;
1853 gpiochip->irq.chip->irq_release_resources = NULL;
1854 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001855 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001856
1857 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001858}
1859
1860/**
Linus Walleij739e6f52017-01-11 13:37:07 +01001861 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001862 * @gpiochip: the gpiochip to add the irqchip to
1863 * @irqchip: the irqchip to add to the gpiochip
1864 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1865 * allocate gpiochip irqs from
1866 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001867 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1868 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01001869 * @threaded: whether this irqchip uses a nested thread handler
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001870 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001871 *
1872 * This function closely associates a certain irqchip with a certain
1873 * gpiochip, providing an irq domain to translate the local IRQs to
1874 * global irqs in the gpiolib core, and making sure that the gpiochip
1875 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001876 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001877 * from the gpiochip passed as chip data. An irqdomain will be stored
1878 * in the gpiochip that shall be used by the driver to handle IRQ number
1879 * translation. The gpiochip will need to be initialized and registered
1880 * before calling this function.
1881 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001882 * This function will handle two cell:ed simple IRQs and assumes all
1883 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001884 * need to be open coded.
1885 */
Linus Walleij739e6f52017-01-11 13:37:07 +01001886int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1887 struct irq_chip *irqchip,
1888 unsigned int first_irq,
1889 irq_flow_handler_t handler,
1890 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01001891 bool threaded,
Linus Walleij739e6f52017-01-11 13:37:07 +01001892 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001893{
1894 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001895
1896 if (!gpiochip || !irqchip)
1897 return -EINVAL;
1898
Linus Walleij58383c782015-11-04 09:56:26 +01001899 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001900 pr_err("missing gpiochip .dev parent pointer\n");
1901 return -EINVAL;
1902 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01001903 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01001904 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001905#ifdef CONFIG_OF_GPIO
1906 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001907 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001908 * FIXME: get rid of this and use gpiochip->parent->of_node
1909 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001910 */
1911 if (gpiochip->of_node)
1912 of_node = gpiochip->of_node;
1913#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001914 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001915 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001916 * used to configure the interrupts, as you may end-up with
1917 * conflicting triggers. Tell the user, and reset to NONE.
1918 */
1919 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05001920 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01001921 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001922 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1923 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1924 "Ignoring %d default trigger\n", type);
1925 type = IRQ_TYPE_NONE;
1926 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001927
Thierry Redingda80ff82017-11-07 19:15:46 +01001928 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001929 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01001930 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01001931 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01001932 gpiochip->irq.lock_key = lock_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001933 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01001934 gpiochip->ngpio, first_irq,
1935 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001936 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01001937 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001938 return -EINVAL;
1939 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001940
1941 /*
1942 * It is possible for a driver to override this, but only if the
1943 * alternative functions are both implemented.
1944 */
1945 if (!irqchip->irq_request_resources &&
1946 !irqchip->irq_release_resources) {
1947 irqchip->irq_request_resources = gpiochip_irq_reqres;
1948 irqchip->irq_release_resources = gpiochip_irq_relres;
1949 }
Linus Walleij14250522014-03-25 10:40:18 +01001950
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001951 acpi_gpiochip_request_interrupts(gpiochip);
1952
Linus Walleij14250522014-03-25 10:40:18 +01001953 return 0;
1954}
Linus Walleij739e6f52017-01-11 13:37:07 +01001955EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01001956
1957#else /* CONFIG_GPIOLIB_IRQCHIP */
1958
Thierry Redinge0d89722017-11-07 19:15:54 +01001959static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip)
1960{
1961 return 0;
1962}
1963
Linus Walleij14250522014-03-25 10:40:18 +01001964static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03001965static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1966{
1967 return 0;
1968}
1969static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1970{ }
Linus Walleij14250522014-03-25 10:40:18 +01001971
1972#endif /* CONFIG_GPIOLIB_IRQCHIP */
1973
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001974/**
1975 * gpiochip_generic_request() - request the gpio function for a pin
1976 * @chip: the gpiochip owning the GPIO
1977 * @offset: the offset of the GPIO to request for GPIO function
1978 */
1979int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1980{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001981 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001982}
1983EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1984
1985/**
1986 * gpiochip_generic_free() - free the gpio function from a pin
1987 * @chip: the gpiochip to request the gpio function for
1988 * @offset: the offset of the GPIO to free from GPIO function
1989 */
1990void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1991{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001992 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001993}
1994EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1995
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001996/**
1997 * gpiochip_generic_config() - apply configuration for a pin
1998 * @chip: the gpiochip owning the GPIO
1999 * @offset: the offset of the GPIO to apply the configuration
2000 * @config: the configuration to be applied
2001 */
2002int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2003 unsigned long config)
2004{
2005 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2006}
2007EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2008
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302009#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002010
Linus Walleij3f0f8672012-11-20 12:40:15 +01002011/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002012 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2013 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002014 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002015 * @gpio_offset: the start offset in the current gpio_chip number space
2016 * @pin_group: name of the pin group inside the pin controller
2017 */
2018int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2019 struct pinctrl_dev *pctldev,
2020 unsigned int gpio_offset, const char *pin_group)
2021{
2022 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002023 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002024 int ret;
2025
2026 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2027 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002028 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002029 return -ENOMEM;
2030 }
2031
2032 /* Use local offset as range ID */
2033 pin_range->range.id = gpio_offset;
2034 pin_range->range.gc = chip;
2035 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002036 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002037 pin_range->pctldev = pctldev;
2038
2039 ret = pinctrl_get_group_pins(pctldev, pin_group,
2040 &pin_range->range.pins,
2041 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002042 if (ret < 0) {
2043 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002044 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002045 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002046
2047 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2048
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002049 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2050 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002051 pinctrl_dev_get_devname(pctldev), pin_group);
2052
Linus Walleij20ec3e32016-02-11 11:03:06 +01002053 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002054
2055 return 0;
2056}
2057EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2058
2059/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002060 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2061 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002062 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002063 * @gpio_offset: the start offset in the current gpio_chip number space
2064 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002065 * @npins: the number of pins from the offset of each pin space (GPIO and
2066 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002067 *
2068 * Returns:
2069 * 0 on success, or a negative error-code on failure.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002070 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002071int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002072 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002073 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302074{
2075 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002076 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002077 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302078
Linus Walleij3f0f8672012-11-20 12:40:15 +01002079 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302080 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002081 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002082 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302083 }
2084
Linus Walleij3f0f8672012-11-20 12:40:15 +01002085 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002086 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002087 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302088 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002089 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002090 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302091 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002092 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302093 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002094 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002095 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002096 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002097 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002098 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002099 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002100 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2101 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002102 pinctl_name,
2103 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302104
Linus Walleij20ec3e32016-02-11 11:03:06 +01002105 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002106
2107 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302108}
Linus Walleij165adc92012-11-06 14:49:39 +01002109EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302110
Linus Walleij3f0f8672012-11-20 12:40:15 +01002111/**
2112 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2113 * @chip: the chip to remove all the mappings for
2114 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302115void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2116{
2117 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002118 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302119
Linus Walleij20ec3e32016-02-11 11:03:06 +01002120 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302121 list_del(&pin_range->node);
2122 pinctrl_remove_gpio_range(pin_range->pctldev,
2123 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002124 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302125 }
2126}
Linus Walleij165adc92012-11-06 14:49:39 +01002127EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2128
2129#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302130
David Brownelld2876d02008-02-04 22:28:20 -08002131/* These "optional" allocation calls help prevent drivers from stomping
2132 * on each other, and help provide better diagnostics in debugfs.
2133 * They're called even less than the "set direction" calls.
2134 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002135static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002136{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002137 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002138 int status;
David Brownelld2876d02008-02-04 22:28:20 -08002139 unsigned long flags;
2140
2141 spin_lock_irqsave(&gpio_lock, flags);
2142
David Brownelld2876d02008-02-04 22:28:20 -08002143 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002144 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002145 */
2146
2147 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2148 desc_set_label(desc, label ? : "?");
2149 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002150 } else {
David Brownelld2876d02008-02-04 22:28:20 -08002151 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002152 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002153 }
2154
2155 if (chip->request) {
2156 /* chip->request may sleep */
2157 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002158 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002159 spin_lock_irqsave(&gpio_lock, flags);
2160
2161 if (status < 0) {
2162 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07002163 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002164 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002165 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002166 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002167 if (chip->get_direction) {
2168 /* chip->get_direction may sleep */
2169 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002170 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002171 spin_lock_irqsave(&gpio_lock, flags);
2172 }
David Brownelld2876d02008-02-04 22:28:20 -08002173done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002174 spin_unlock_irqrestore(&gpio_lock, flags);
2175 return status;
2176}
2177
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002178/*
2179 * This descriptor validation needs to be inserted verbatim into each
2180 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002181 * macro to avoid endless duplication. If the desc is NULL it is an
2182 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002183 */
2184#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002185 if (!desc) \
2186 return 0; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002187 if (IS_ERR(desc)) { \
2188 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2189 return PTR_ERR(desc); \
2190 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002191 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002192 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002193 return -EINVAL; \
2194 } \
2195 if ( !desc->gdev->chip ) { \
2196 dev_warn(&desc->gdev->dev, \
2197 "%s: backing chip is gone\n", __func__); \
2198 return 0; \
2199 } } while (0)
2200
2201#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002202 if (!desc) \
2203 return; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002204 if (IS_ERR(desc)) { \
2205 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2206 return; \
2207 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002208 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002209 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002210 return; \
2211 } \
2212 if (!desc->gdev->chip) { \
2213 dev_warn(&desc->gdev->dev, \
2214 "%s: backing chip is gone\n", __func__); \
2215 return; \
2216 } } while (0)
2217
2218
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002219int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002220{
2221 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002222 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002223
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002224 VALIDATE_DESC(desc);
2225 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002226
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002227 if (try_module_get(gdev->owner)) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002228 status = gpiod_request_commit(desc, label);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002229 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002230 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002231 else
2232 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002233 }
2234
David Brownelld2876d02008-02-04 22:28:20 -08002235 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002236 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002237
David Brownelld2876d02008-02-04 22:28:20 -08002238 return status;
2239}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002240
Linus Walleijfac9d882017-09-26 20:58:28 +02002241static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002242{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002243 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002244 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002245 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002246
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002247 might_sleep();
2248
Alexandre Courbot372e7222013-02-03 01:29:29 +09002249 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002250
David Brownelld2876d02008-02-04 22:28:20 -08002251 spin_lock_irqsave(&gpio_lock, flags);
2252
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002253 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002254 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2255 if (chip->free) {
2256 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002257 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002258 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002259 spin_lock_irqsave(&gpio_lock, flags);
2260 }
David Brownelld2876d02008-02-04 22:28:20 -08002261 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002262 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002263 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302264 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302265 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002266 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002267 ret = true;
2268 }
David Brownelld2876d02008-02-04 22:28:20 -08002269
2270 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002271 return ret;
2272}
2273
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002274void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002275{
Linus Walleijfac9d882017-09-26 20:58:28 +02002276 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002277 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002278 put_device(&desc->gdev->dev);
2279 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002280 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002281 }
David Brownelld2876d02008-02-04 22:28:20 -08002282}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002283
David Brownelld2876d02008-02-04 22:28:20 -08002284/**
2285 * gpiochip_is_requested - return string iff signal was requested
2286 * @chip: controller managing the signal
2287 * @offset: of signal within controller's 0..(ngpio - 1) range
2288 *
2289 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002290 * The string returned is the label passed to gpio_request(); if none has been
2291 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002292 *
2293 * This function is for use by GPIO controller drivers. The label can
2294 * help with diagnostics, and knowing that the signal is used as a GPIO
2295 * can help avoid accidentally multiplexing it to another controller.
2296 */
2297const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2298{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002299 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002300
Dirk Behme48b59532015-08-18 18:02:32 +02002301 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002302 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002303
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002304 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002305
Alexandre Courbot372e7222013-02-03 01:29:29 +09002306 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002307 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002308 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002309}
2310EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2311
Mika Westerberg77c2d792014-03-10 14:54:50 +02002312/**
2313 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002314 * @chip: GPIO chip
2315 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002316 * @label: label for the GPIO
2317 *
2318 * Function allows GPIO chip drivers to request and use their own GPIO
2319 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2320 * function will not increase reference count of the GPIO chip module. This
2321 * allows the GPIO chip module to be unloaded as needed (we assume that the
2322 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002323 *
2324 * Returns:
2325 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2326 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002327 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002328struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2329 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002330{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002331 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2332 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002333
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002334 if (IS_ERR(desc)) {
2335 chip_err(chip, "failed to get GPIO descriptor\n");
2336 return desc;
2337 }
2338
Linus Walleijfac9d882017-09-26 20:58:28 +02002339 err = gpiod_request_commit(desc, label);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002340 if (err < 0)
2341 return ERR_PTR(err);
2342
2343 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002344}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002345EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002346
2347/**
2348 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2349 * @desc: GPIO descriptor to free
2350 *
2351 * Function frees the given GPIO requested previously with
2352 * gpiochip_request_own_desc().
2353 */
2354void gpiochip_free_own_desc(struct gpio_desc *desc)
2355{
2356 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002357 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002358}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002359EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002360
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002361/*
2362 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002363 * some cases this is done in early boot, before IRQs are enabled.
2364 *
2365 * As a rule these aren't called more than once (except for drivers
2366 * using the open-drain emulation idiom) so these are natural places
2367 * to accumulate extra debugging checks. Note that we can't (yet)
2368 * rely on gpio_request() having been called beforehand.
2369 */
2370
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002371/**
2372 * gpiod_direction_input - set the GPIO direction to input
2373 * @desc: GPIO to set to input
2374 *
2375 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2376 * be called safely on it.
2377 *
2378 * Return 0 in case of success, else an error code.
2379 */
2380int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002381{
David Brownelld2876d02008-02-04 22:28:20 -08002382 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002383 int status = -EINVAL;
2384
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002385 VALIDATE_DESC(desc);
2386 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002387
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002388 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002389 gpiod_warn(desc,
2390 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002391 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002392 return -EIO;
2393 }
2394
Alexandre Courbotd82da792014-07-22 16:17:43 +09002395 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002396 if (status == 0)
2397 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002398
Alexandre Courbot372e7222013-02-03 01:29:29 +09002399 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002400
David Brownelld2876d02008-02-04 22:28:20 -08002401 return status;
2402}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002403EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002404
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002405static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2406 enum pin_config_param mode)
2407{
2408 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2409
2410 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2411}
2412
Linus Walleijfac9d882017-09-26 20:58:28 +02002413static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002414{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002415 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002416 int val = !!value;
Linus Walleijc663e5f2016-03-22 10:51:16 +01002417 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002418
Linus Walleijc663e5f2016-03-22 10:51:16 +01002419 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002420 gpiod_warn(desc,
2421 "%s: missing set() or direction_output() operations\n",
2422 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002423 return -EIO;
2424 }
2425
Linus Walleijad177312016-11-13 23:02:44 +01002426 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002427 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002428 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002429 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002430 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2431 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002432}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002433
2434/**
2435 * gpiod_direction_output_raw - set the GPIO direction to output
2436 * @desc: GPIO to set to output
2437 * @value: initial output value of the GPIO
2438 *
2439 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2440 * be called safely on it. The initial value of the output must be specified
2441 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2442 *
2443 * Return 0 in case of success, else an error code.
2444 */
2445int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2446{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002447 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002448 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002449}
2450EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2451
2452/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302453 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002454 * @desc: GPIO to set to output
2455 * @value: initial output value of the GPIO
2456 *
2457 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2458 * be called safely on it. The initial value of the output must be specified
2459 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2460 * account.
2461 *
2462 * Return 0 in case of success, else an error code.
2463 */
2464int gpiod_direction_output(struct gpio_desc *desc, int value)
2465{
Linus Walleij02e47982017-09-26 21:20:23 +02002466 struct gpio_chip *gc = desc->gdev->chip;
2467 int ret;
2468
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002469 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002470 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2471 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002472 else
2473 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02002474
2475 /* GPIOs used for IRQs shall not be set as output */
2476 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2477 gpiod_err(desc,
2478 "%s: tried to set a GPIO tied to an IRQ as output\n",
2479 __func__);
2480 return -EIO;
2481 }
2482
2483 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2484 /* First see if we can enable open drain in hardware */
2485 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2486 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2487 if (!ret)
2488 goto set_output_value;
2489 /* Emulate open drain by not actively driving the line high */
2490 if (value)
2491 return gpiod_direction_input(desc);
2492 }
2493 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2494 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2495 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2496 if (!ret)
2497 goto set_output_value;
2498 /* Emulate open source by not actively driving the line low */
2499 if (!value)
2500 return gpiod_direction_input(desc);
2501 } else {
2502 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2503 PIN_CONFIG_DRIVE_PUSH_PULL);
2504 }
2505
2506set_output_value:
Linus Walleijfac9d882017-09-26 20:58:28 +02002507 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002508}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002509EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002510
Felipe Balbic4b5be92010-05-26 14:42:23 -07002511/**
Thierry Reding950d55f52017-07-24 16:57:22 +02002512 * gpiod_set_debounce - sets @debounce time for a GPIO
2513 * @desc: descriptor of the GPIO for which to set debounce time
2514 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002515 *
Thierry Reding950d55f52017-07-24 16:57:22 +02002516 * Returns:
2517 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2518 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002519 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002520int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002521{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002522 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002523 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002524
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002525 VALIDATE_DESC(desc);
2526 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002527 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01002528 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002529 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01002530 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002531 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002532 }
2533
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002534 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2535 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002536}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002537EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002538
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002539/**
2540 * gpiod_is_active_low - test whether a GPIO is active-low or not
2541 * @desc: the gpio descriptor to test
2542 *
2543 * Returns 1 if the GPIO is active-low, 0 otherwise.
2544 */
2545int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002546{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002547 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002548 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002549}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002550EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002551
2552/* I/O calls are only valid after configuration completed; the relevant
2553 * "is this a valid GPIO" error checks should already have been done.
2554 *
2555 * "Get" operations are often inlinable as reading a pin value register,
2556 * and masking the relevant bit in that register.
2557 *
2558 * When "set" operations are inlinable, they involve writing that mask to
2559 * one register to set a low value, or a different register to set it high.
2560 * Otherwise locking is needed, so there may be little value to inlining.
2561 *
2562 *------------------------------------------------------------------------
2563 *
2564 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2565 * have requested the GPIO. That can include implicit requesting by
2566 * a direction setting call. Marking a gpio as requested locks its chip
2567 * in memory, guaranteeing that these table lookups need no more locking
2568 * and that gpiochip_remove() will fail.
2569 *
2570 * REVISIT when debugging, consider adding some instrumentation to ensure
2571 * that the GPIO was actually requested.
2572 */
2573
Linus Walleijfac9d882017-09-26 20:58:28 +02002574static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002575{
2576 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002577 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002578 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002579
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002580 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002581 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002582 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002583 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002584 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002585 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002586}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002587
Lukas Wunnereec1d562017-10-12 12:40:10 +02002588static int gpio_chip_get_multiple(struct gpio_chip *chip,
2589 unsigned long *mask, unsigned long *bits)
2590{
2591 if (chip->get_multiple) {
2592 return chip->get_multiple(chip, mask, bits);
2593 } else if (chip->get) {
2594 int i, value;
2595
2596 for_each_set_bit(i, mask, chip->ngpio) {
2597 value = chip->get(chip, i);
2598 if (value < 0)
2599 return value;
2600 __assign_bit(i, bits, value);
2601 }
2602 return 0;
2603 }
2604 return -EIO;
2605}
2606
2607int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2608 unsigned int array_size,
2609 struct gpio_desc **desc_array,
2610 int *value_array)
2611{
2612 int i = 0;
2613
2614 while (i < array_size) {
2615 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2616 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2617 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2618 int first, j, ret;
2619
2620 if (!can_sleep)
2621 WARN_ON(chip->can_sleep);
2622
2623 /* collect all inputs belonging to the same chip */
2624 first = i;
2625 memset(mask, 0, sizeof(mask));
2626 do {
2627 const struct gpio_desc *desc = desc_array[i];
2628 int hwgpio = gpio_chip_hwgpio(desc);
2629
2630 __set_bit(hwgpio, mask);
2631 i++;
2632 } while ((i < array_size) &&
2633 (desc_array[i]->gdev->chip == chip));
2634
2635 ret = gpio_chip_get_multiple(chip, mask, bits);
2636 if (ret)
2637 return ret;
2638
2639 for (j = first; j < i; j++) {
2640 const struct gpio_desc *desc = desc_array[j];
2641 int hwgpio = gpio_chip_hwgpio(desc);
2642 int value = test_bit(hwgpio, bits);
2643
2644 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2645 value = !value;
2646 value_array[j] = value;
2647 trace_gpio_value(desc_to_gpio(desc), 1, value);
2648 }
2649 }
2650 return 0;
2651}
2652
David Brownelld2876d02008-02-04 22:28:20 -08002653/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002654 * gpiod_get_raw_value() - return a gpio's raw value
2655 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002656 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002657 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002658 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002659 *
2660 * This function should be called from contexts where we cannot sleep, and will
2661 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002662 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002663int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002664{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002665 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002666 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002667 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002668 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002669}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002670EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002671
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002672/**
2673 * gpiod_get_value() - return a gpio's value
2674 * @desc: gpio whose value will be returned
2675 *
2676 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002677 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002678 *
2679 * This function should be called from contexts where we cannot sleep, and will
2680 * complain if the GPIO chip functions potentially sleep.
2681 */
2682int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002683{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002684 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002685
2686 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002687 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002688 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002689
Linus Walleijfac9d882017-09-26 20:58:28 +02002690 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002691 if (value < 0)
2692 return value;
2693
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002694 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2695 value = !value;
2696
2697 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002698}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002699EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002700
Lukas Wunnereec1d562017-10-12 12:40:10 +02002701/**
2702 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2703 * @array_size: number of elements in the descriptor / value arrays
2704 * @desc_array: array of GPIO descriptors whose values will be read
2705 * @value_array: array to store the read values
2706 *
2707 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2708 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2709 * else an error code.
2710 *
2711 * This function should be called from contexts where we cannot sleep,
2712 * and it will complain if the GPIO chip functions potentially sleep.
2713 */
2714int gpiod_get_raw_array_value(unsigned int array_size,
2715 struct gpio_desc **desc_array, int *value_array)
2716{
2717 if (!desc_array)
2718 return -EINVAL;
2719 return gpiod_get_array_value_complex(true, false, array_size,
2720 desc_array, value_array);
2721}
2722EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2723
2724/**
2725 * gpiod_get_array_value() - read values from an array of GPIOs
2726 * @array_size: number of elements in the descriptor / value arrays
2727 * @desc_array: array of GPIO descriptors whose values will be read
2728 * @value_array: array to store the read values
2729 *
2730 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2731 * into account. Return 0 in case of success, else an error code.
2732 *
2733 * This function should be called from contexts where we cannot sleep,
2734 * and it will complain if the GPIO chip functions potentially sleep.
2735 */
2736int gpiod_get_array_value(unsigned int array_size,
2737 struct gpio_desc **desc_array, int *value_array)
2738{
2739 if (!desc_array)
2740 return -EINVAL;
2741 return gpiod_get_array_value_complex(false, false, array_size,
2742 desc_array, value_array);
2743}
2744EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2745
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302746/*
Linus Walleijfac9d882017-09-26 20:58:28 +02002747 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002748 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002749 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302750 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002751static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302752{
2753 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002754 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002755 int offset = gpio_chip_hwgpio(desc);
2756
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302757 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002758 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302759 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002760 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302761 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002762 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302763 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002764 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302765 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002766 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302767 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002768 gpiod_err(desc,
2769 "%s: Error in set_value for open drain err %d\n",
2770 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302771}
2772
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302773/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002774 * _gpio_set_open_source_value() - Set the open source gpio's value.
2775 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002776 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302777 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002778static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302779{
2780 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002781 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002782 int offset = gpio_chip_hwgpio(desc);
2783
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302784 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002785 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302786 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002787 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302788 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002789 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302790 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002791 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302792 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002793 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302794 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002795 gpiod_err(desc,
2796 "%s: Error in set_value for open source err %d\n",
2797 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302798}
2799
Linus Walleijfac9d882017-09-26 20:58:28 +02002800static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002801{
2802 struct gpio_chip *chip;
2803
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002804 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002805 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002806 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002807}
2808
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002809/*
2810 * set multiple outputs on the same chip;
2811 * use the chip's set_multiple function if available;
2812 * otherwise set the outputs sequentially;
2813 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2814 * defines which outputs are to be changed
2815 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2816 * defines the values the outputs specified by mask are to be set to
2817 */
2818static void gpio_chip_set_multiple(struct gpio_chip *chip,
2819 unsigned long *mask, unsigned long *bits)
2820{
2821 if (chip->set_multiple) {
2822 chip->set_multiple(chip, mask, bits);
2823 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02002824 unsigned int i;
2825
2826 /* set outputs if the corresponding mask bit is set */
2827 for_each_set_bit(i, mask, chip->ngpio)
2828 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002829 }
2830}
2831
Linus Walleij44c72882016-04-24 11:36:59 +02002832void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2833 unsigned int array_size,
2834 struct gpio_desc **desc_array,
2835 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002836{
2837 int i = 0;
2838
2839 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002840 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002841 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2842 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2843 int count = 0;
2844
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002845 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002846 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002847
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002848 memset(mask, 0, sizeof(mask));
2849 do {
2850 struct gpio_desc *desc = desc_array[i];
2851 int hwgpio = gpio_chip_hwgpio(desc);
2852 int value = value_array[i];
2853
2854 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2855 value = !value;
2856 trace_gpio_value(desc_to_gpio(desc), 0, value);
2857 /*
2858 * collect all normal outputs belonging to the same chip
2859 * open drain and open source outputs are set individually
2860 */
Linus Walleij02e47982017-09-26 21:20:23 +02002861 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002862 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002863 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002864 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002865 } else {
2866 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002867 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002868 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002869 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002870 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002871 count++;
2872 }
2873 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002874 } while ((i < array_size) &&
2875 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002876 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002877 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002878 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002879 }
2880}
2881
David Brownelld2876d02008-02-04 22:28:20 -08002882/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002883 * gpiod_set_raw_value() - assign a gpio's raw value
2884 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002885 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002886 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002887 * Set the raw value of the GPIO, i.e. the value of its physical line without
2888 * regard for its ACTIVE_LOW status.
2889 *
2890 * This function should be called from contexts where we cannot sleep, and will
2891 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002892 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002893void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002894{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002895 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002896 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002897 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002898 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002899}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002900EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002901
2902/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002903 * gpiod_set_value() - assign a gpio's value
2904 * @desc: gpio whose value will be assigned
2905 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002906 *
Linus Walleij02e47982017-09-26 21:20:23 +02002907 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
2908 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002909 *
2910 * This function should be called from contexts where we cannot sleep, and will
2911 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002912 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002913void gpiod_set_value(struct gpio_desc *desc, int value)
2914{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002915 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002916 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002917 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002918 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2919 value = !value;
Linus Walleij02e47982017-09-26 21:20:23 +02002920 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2921 gpio_set_open_drain_value_commit(desc, value);
2922 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2923 gpio_set_open_source_value_commit(desc, value);
2924 else
2925 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002926}
2927EXPORT_SYMBOL_GPL(gpiod_set_value);
2928
2929/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002930 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002931 * @array_size: number of elements in the descriptor / value arrays
2932 * @desc_array: array of GPIO descriptors whose values will be assigned
2933 * @value_array: array of values to assign
2934 *
2935 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2936 * without regard for their ACTIVE_LOW status.
2937 *
2938 * This function should be called from contexts where we cannot sleep, and will
2939 * complain if the GPIO chip functions potentially sleep.
2940 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002941void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002942 struct gpio_desc **desc_array, int *value_array)
2943{
2944 if (!desc_array)
2945 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002946 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2947 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002948}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002949EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002950
2951/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002952 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002953 * @array_size: number of elements in the descriptor / value arrays
2954 * @desc_array: array of GPIO descriptors whose values will be assigned
2955 * @value_array: array of values to assign
2956 *
2957 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2958 * into account.
2959 *
2960 * This function should be called from contexts where we cannot sleep, and will
2961 * complain if the GPIO chip functions potentially sleep.
2962 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002963void gpiod_set_array_value(unsigned int array_size,
2964 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002965{
2966 if (!desc_array)
2967 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002968 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2969 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002970}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002971EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002972
2973/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002974 * gpiod_cansleep() - report whether gpio value access may sleep
2975 * @desc: gpio to check
2976 *
2977 */
2978int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002979{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002980 VALIDATE_DESC(desc);
2981 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002982}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002983EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002984
David Brownell0f6d5042008-10-15 22:03:14 -07002985/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002986 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2987 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002988 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002989 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2990 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002991 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002992int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002993{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002994 struct gpio_chip *chip;
2995 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002996
Linus Walleij79bb71b2016-06-15 22:57:38 +02002997 /*
2998 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
2999 * requires this function to not return zero on an invalid descriptor
3000 * but rather a negative error number.
3001 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003002 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003003 return -EINVAL;
3004
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003005 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003006 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003007 if (chip->to_irq) {
3008 int retirq = chip->to_irq(chip, offset);
3009
3010 /* Zero means NO_IRQ */
3011 if (!retirq)
3012 return -ENXIO;
3013
3014 return retirq;
3015 }
3016 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003017}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003018EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003019
Linus Walleijd468bf92013-09-24 11:54:38 +02003020/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003021 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003022 * @chip: the chip the GPIO to lock belongs to
3023 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003024 *
3025 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003026 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003027 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003028int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003029{
Linus Walleij9c102802016-05-25 10:56:03 +02003030 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003031
Linus Walleij9c102802016-05-25 10:56:03 +02003032 desc = gpiochip_get_desc(chip, offset);
3033 if (IS_ERR(desc))
3034 return PTR_ERR(desc);
3035
Linus Walleij60f83392016-11-12 15:01:09 +01003036 /*
3037 * If it's fast: flush the direction setting if something changed
3038 * behind our back
3039 */
3040 if (!chip->can_sleep && chip->get_direction) {
Linus Walleij9c102802016-05-25 10:56:03 +02003041 int dir = chip->get_direction(chip, offset);
3042
3043 if (dir)
3044 clear_bit(FLAG_IS_OUT, &desc->flags);
3045 else
3046 set_bit(FLAG_IS_OUT, &desc->flags);
3047 }
3048
3049 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003050 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02003051 "%s: tried to flag a GPIO set as output for IRQ\n",
3052 __func__);
3053 return -EIO;
3054 }
3055
Linus Walleij9c102802016-05-25 10:56:03 +02003056 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003057
3058 /*
3059 * If the consumer has not set up a label (such as when the
3060 * IRQ is referenced from .to_irq()) we set up a label here
3061 * so it is clear this is used as an interrupt.
3062 */
3063 if (!desc->label)
3064 desc_set_label(desc, "interrupt");
3065
Linus Walleijd468bf92013-09-24 11:54:38 +02003066 return 0;
3067}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003068EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003069
Linus Walleijd468bf92013-09-24 11:54:38 +02003070/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003071 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003072 * @chip: the chip the GPIO to lock belongs to
3073 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003074 *
3075 * This is used directly by GPIO drivers that want to indicate
3076 * that a certain GPIO is no longer used exclusively for IRQ.
3077 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003078void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003079{
Linus Walleij3940c342016-11-14 00:09:07 +01003080 struct gpio_desc *desc;
3081
3082 desc = gpiochip_get_desc(chip, offset);
3083 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003084 return;
3085
Linus Walleij3940c342016-11-14 00:09:07 +01003086 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3087
3088 /* If we only had this marking, erase it */
3089 if (desc->label && !strcmp(desc->label, "interrupt"))
3090 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003091}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003092EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003093
Linus Walleij6cee3822016-02-11 20:16:45 +01003094bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3095{
3096 if (offset >= chip->ngpio)
3097 return false;
3098
3099 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3100}
3101EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3102
Linus Walleij143b65d2016-02-16 15:41:42 +01003103bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3104{
3105 if (offset >= chip->ngpio)
3106 return false;
3107
3108 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3109}
3110EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3111
3112bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3113{
3114 if (offset >= chip->ngpio)
3115 return false;
3116
3117 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3118}
3119EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3120
Charles Keepax05f479b2017-05-23 15:47:29 +01003121bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3122{
3123 if (offset >= chip->ngpio)
3124 return false;
3125
Andrew Jeffery2cbfca62017-10-20 13:27:58 +10303126 return !test_bit(FLAG_SLEEP_MAY_LOSE_VALUE,
Charles Keepax05f479b2017-05-23 15:47:29 +01003127 &chip->gpiodev->descs[offset].flags);
3128}
3129EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3130
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003131/**
3132 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3133 * @desc: gpio whose value will be returned
3134 *
3135 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003136 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003137 *
3138 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003139 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003140int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003141{
David Brownelld2876d02008-02-04 22:28:20 -08003142 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003143 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003144 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08003145}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003146EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003147
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003148/**
3149 * gpiod_get_value_cansleep() - return a gpio's value
3150 * @desc: gpio whose value will be returned
3151 *
3152 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003153 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003154 *
3155 * This function is to be called from contexts that can sleep.
3156 */
3157int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003158{
David Brownelld2876d02008-02-04 22:28:20 -08003159 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003160
3161 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003162 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003163 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003164 if (value < 0)
3165 return value;
3166
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003167 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3168 value = !value;
3169
David Brownelld2876d02008-02-04 22:28:20 -08003170 return value;
3171}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003172EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003173
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003174/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02003175 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3176 * @array_size: number of elements in the descriptor / value arrays
3177 * @desc_array: array of GPIO descriptors whose values will be read
3178 * @value_array: array to store the read values
3179 *
3180 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3181 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3182 * else an error code.
3183 *
3184 * This function is to be called from contexts that can sleep.
3185 */
3186int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3187 struct gpio_desc **desc_array,
3188 int *value_array)
3189{
3190 might_sleep_if(extra_checks);
3191 if (!desc_array)
3192 return -EINVAL;
3193 return gpiod_get_array_value_complex(true, true, array_size,
3194 desc_array, value_array);
3195}
3196EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3197
3198/**
3199 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3200 * @array_size: number of elements in the descriptor / value arrays
3201 * @desc_array: array of GPIO descriptors whose values will be read
3202 * @value_array: array to store the read values
3203 *
3204 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3205 * into account. Return 0 in case of success, else an error code.
3206 *
3207 * This function is to be called from contexts that can sleep.
3208 */
3209int gpiod_get_array_value_cansleep(unsigned int array_size,
3210 struct gpio_desc **desc_array,
3211 int *value_array)
3212{
3213 might_sleep_if(extra_checks);
3214 if (!desc_array)
3215 return -EINVAL;
3216 return gpiod_get_array_value_complex(false, true, array_size,
3217 desc_array, value_array);
3218}
3219EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3220
3221/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003222 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3223 * @desc: gpio whose value will be assigned
3224 * @value: value to assign
3225 *
3226 * Set the raw value of the GPIO, i.e. the value of its physical line without
3227 * regard for its ACTIVE_LOW status.
3228 *
3229 * This function is to be called from contexts that can sleep.
3230 */
3231void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003232{
David Brownelld2876d02008-02-04 22:28:20 -08003233 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003234 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003235 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003236}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003237EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003238
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003239/**
3240 * gpiod_set_value_cansleep() - assign a gpio's value
3241 * @desc: gpio whose value will be assigned
3242 * @value: value to assign
3243 *
3244 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3245 * account
3246 *
3247 * This function is to be called from contexts that can sleep.
3248 */
3249void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003250{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003251 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003252 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003253 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3254 value = !value;
Linus Walleijfac9d882017-09-26 20:58:28 +02003255 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003256}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003257EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003258
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003259/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003260 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003261 * @array_size: number of elements in the descriptor / value arrays
3262 * @desc_array: array of GPIO descriptors whose values will be assigned
3263 * @value_array: array of values to assign
3264 *
3265 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3266 * without regard for their ACTIVE_LOW status.
3267 *
3268 * This function is to be called from contexts that can sleep.
3269 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003270void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3271 struct gpio_desc **desc_array,
3272 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003273{
3274 might_sleep_if(extra_checks);
3275 if (!desc_array)
3276 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003277 gpiod_set_array_value_complex(true, true, array_size, desc_array,
3278 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003279}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003280EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003281
3282/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003283 * gpiod_add_lookup_tables() - register GPIO device consumers
3284 * @tables: list of tables of consumers to register
3285 * @n: number of tables in the list
3286 */
3287void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3288{
3289 unsigned int i;
3290
3291 mutex_lock(&gpio_lookup_lock);
3292
3293 for (i = 0; i < n; i++)
3294 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3295
3296 mutex_unlock(&gpio_lookup_lock);
3297}
3298
3299/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003300 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003301 * @array_size: number of elements in the descriptor / value arrays
3302 * @desc_array: array of GPIO descriptors whose values will be assigned
3303 * @value_array: array of values to assign
3304 *
3305 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3306 * into account.
3307 *
3308 * This function is to be called from contexts that can sleep.
3309 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003310void gpiod_set_array_value_cansleep(unsigned int array_size,
3311 struct gpio_desc **desc_array,
3312 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003313{
3314 might_sleep_if(extra_checks);
3315 if (!desc_array)
3316 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003317 gpiod_set_array_value_complex(false, true, array_size, desc_array,
3318 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003319}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003320EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003321
3322/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003323 * gpiod_add_lookup_table() - register GPIO device consumers
3324 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003325 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003326void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003327{
3328 mutex_lock(&gpio_lookup_lock);
3329
Alexandre Courbotad824782013-12-03 12:20:11 +09003330 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003331
3332 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003333}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003334EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003335
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303336/**
3337 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3338 * @table: table of consumers to unregister
3339 */
3340void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3341{
3342 mutex_lock(&gpio_lookup_lock);
3343
3344 list_del(&table->list);
3345
3346 mutex_unlock(&gpio_lookup_lock);
3347}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003348EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303349
Alexandre Courbotad824782013-12-03 12:20:11 +09003350static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3351{
3352 const char *dev_id = dev ? dev_name(dev) : NULL;
3353 struct gpiod_lookup_table *table;
3354
3355 mutex_lock(&gpio_lookup_lock);
3356
3357 list_for_each_entry(table, &gpio_lookup_list, list) {
3358 if (table->dev_id && dev_id) {
3359 /*
3360 * Valid strings on both ends, must be identical to have
3361 * a match
3362 */
3363 if (!strcmp(table->dev_id, dev_id))
3364 goto found;
3365 } else {
3366 /*
3367 * One of the pointers is NULL, so both must be to have
3368 * a match
3369 */
3370 if (dev_id == table->dev_id)
3371 goto found;
3372 }
3373 }
3374 table = NULL;
3375
3376found:
3377 mutex_unlock(&gpio_lookup_lock);
3378 return table;
3379}
3380
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003381static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003382 unsigned int idx,
3383 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003384{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003385 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003386 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003387 struct gpiod_lookup *p;
3388
Alexandre Courbotad824782013-12-03 12:20:11 +09003389 table = gpiod_find_lookup_table(dev);
3390 if (!table)
3391 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003392
Alexandre Courbotad824782013-12-03 12:20:11 +09003393 for (p = &table->table[0]; p->chip_label; p++) {
3394 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003395
Alexandre Courbotad824782013-12-03 12:20:11 +09003396 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003397 if (p->idx != idx)
3398 continue;
3399
Alexandre Courbotad824782013-12-03 12:20:11 +09003400 /* If the lookup entry has a con_id, require exact match */
3401 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3402 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003403
Alexandre Courbotad824782013-12-03 12:20:11 +09003404 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003405
Alexandre Courbotad824782013-12-03 12:20:11 +09003406 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003407 dev_err(dev, "cannot find GPIO chip %s\n",
3408 p->chip_label);
3409 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003410 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003411
Alexandre Courbotad824782013-12-03 12:20:11 +09003412 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003413 dev_err(dev,
3414 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3415 idx, chip->ngpio, chip->label);
3416 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003417 }
3418
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003419 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003420 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003421
3422 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003423 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003424
3425 return desc;
3426}
3427
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003428static int dt_gpio_count(struct device *dev, const char *con_id)
3429{
3430 int ret;
3431 char propname[32];
3432 unsigned int i;
3433
3434 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3435 if (con_id)
3436 snprintf(propname, sizeof(propname), "%s-%s",
3437 con_id, gpio_suffixes[i]);
3438 else
3439 snprintf(propname, sizeof(propname), "%s",
3440 gpio_suffixes[i]);
3441
3442 ret = of_gpio_named_count(dev->of_node, propname);
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003443 if (ret > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003444 break;
3445 }
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003446 return ret ? ret : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003447}
3448
3449static int platform_gpio_count(struct device *dev, const char *con_id)
3450{
3451 struct gpiod_lookup_table *table;
3452 struct gpiod_lookup *p;
3453 unsigned int count = 0;
3454
3455 table = gpiod_find_lookup_table(dev);
3456 if (!table)
3457 return -ENOENT;
3458
3459 for (p = &table->table[0]; p->chip_label; p++) {
3460 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3461 (!con_id && !p->con_id))
3462 count++;
3463 }
3464 if (!count)
3465 return -ENOENT;
3466
3467 return count;
3468}
3469
3470/**
3471 * gpiod_count - return the number of GPIOs associated with a device / function
3472 * or -ENOENT if no GPIO has been assigned to the requested function
3473 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3474 * @con_id: function within the GPIO consumer
3475 */
3476int gpiod_count(struct device *dev, const char *con_id)
3477{
3478 int count = -ENOENT;
3479
3480 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3481 count = dt_gpio_count(dev, con_id);
3482 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3483 count = acpi_gpio_count(dev, con_id);
3484
3485 if (count < 0)
3486 count = platform_gpio_count(dev, con_id);
3487
3488 return count;
3489}
3490EXPORT_SYMBOL_GPL(gpiod_count);
3491
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003492/**
Thierry Reding08791622014-04-25 16:54:22 +02003493 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003494 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003495 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003496 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003497 *
3498 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003499 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003500 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003501 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003502struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003503 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003504{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003505 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003506}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003507EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003508
3509/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003510 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3511 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3512 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003513 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003514 *
3515 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3516 * the requested function it will return NULL. This is convenient for drivers
3517 * that need to handle optional GPIOs.
3518 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003519struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003520 const char *con_id,
3521 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003522{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003523 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003524}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003525EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003526
Benoit Parrotf625d462015-02-02 11:44:44 -06003527
3528/**
3529 * gpiod_configure_flags - helper function to configure a given GPIO
3530 * @desc: gpio whose value will be assigned
3531 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003532 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3533 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003534 * @dflags: gpiod_flags - optional GPIO initialization flags
3535 *
3536 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3537 * requested function and/or index, or another IS_ERR() code if an error
3538 * occurred while trying to acquire the GPIO.
3539 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03003540int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003541 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003542{
3543 int status;
3544
Johan Hovold85b03b32016-07-03 18:32:05 +02003545 if (lflags & GPIO_ACTIVE_LOW)
3546 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3547 if (lflags & GPIO_OPEN_DRAIN)
3548 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3549 if (lflags & GPIO_OPEN_SOURCE)
3550 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jeffery2cbfca62017-10-20 13:27:58 +10303551 if (lflags & GPIO_SLEEP_MAY_LOSE_VALUE)
3552 set_bit(FLAG_SLEEP_MAY_LOSE_VALUE, &desc->flags);
Johan Hovold85b03b32016-07-03 18:32:05 +02003553
Benoit Parrotf625d462015-02-02 11:44:44 -06003554 /* No particular flag request, return here... */
3555 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3556 pr_debug("no flags found for %s\n", con_id);
3557 return 0;
3558 }
3559
3560 /* Process flags */
3561 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3562 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01003563 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06003564 else
3565 status = gpiod_direction_input(desc);
3566
3567 return status;
3568}
3569
Thierry Reding29a1f2332014-04-25 17:10:06 +02003570/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003571 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003572 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003573 * @con_id: function within the GPIO consumer
3574 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003575 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003576 *
3577 * This variant of gpiod_get() allows to access GPIOs other than the first
3578 * defined one for functions that define several GPIOs.
3579 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003580 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3581 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003582 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003583 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003584struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003585 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003586 unsigned int idx,
3587 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003588{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003589 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003590 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003591 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003592
3593 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3594
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003595 if (dev) {
3596 /* Using device tree? */
3597 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3598 dev_dbg(dev, "using device tree for GPIO lookup\n");
3599 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3600 } else if (ACPI_COMPANION(dev)) {
3601 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003602 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003603 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003604 }
3605
3606 /*
3607 * Either we are not using DT or ACPI, or their lookup did not return
3608 * a result. In that case, use platform lookup as a fallback.
3609 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003610 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003611 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003612 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003613 }
3614
3615 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003616 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003617 return desc;
3618 }
3619
3620 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003621 if (status < 0)
3622 return ERR_PTR(status);
3623
Johan Hovold85b03b32016-07-03 18:32:05 +02003624 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003625 if (status < 0) {
3626 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3627 gpiod_put(desc);
3628 return ERR_PTR(status);
3629 }
3630
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003631 return desc;
3632}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003633EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003634
3635/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003636 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3637 * @fwnode: handle of the firmware node
3638 * @propname: name of the firmware property representing the GPIO
Boris Brezillon537b94d2017-02-02 14:53:11 +01003639 * @index: index of the GPIO to obtain in the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003640 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02003641 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02003642 *
3643 * This function can be used for drivers that get their configuration
3644 * from firmware.
3645 *
3646 * Function properly finds the corresponding GPIO using whatever is the
3647 * underlying firmware interface and then makes sure that the GPIO
3648 * descriptor is requested before it is returned to the caller.
3649 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003650 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02003651 * On successful request the GPIO pin is configured in accordance with
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003652 * provided @dflags.
3653 *
Mika Westerberg40b73182014-10-21 13:33:59 +02003654 * In case of error an ERR_PTR() is returned.
3655 */
3656struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Boris Brezillon537b94d2017-02-02 14:53:11 +01003657 const char *propname, int index,
Alexander Steinb2987d72017-01-12 17:39:24 +01003658 enum gpiod_flags dflags,
3659 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02003660{
3661 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003662 unsigned long lflags = 0;
Mika Westerberg40b73182014-10-21 13:33:59 +02003663 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003664 bool single_ended = false;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303665 bool open_drain = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003666 int ret;
3667
3668 if (!fwnode)
3669 return ERR_PTR(-EINVAL);
3670
3671 if (is_of_node(fwnode)) {
3672 enum of_gpio_flags flags;
3673
Boris Brezillon537b94d2017-02-02 14:53:11 +01003674 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname,
3675 index, &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003676 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003677 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003678 single_ended = flags & OF_GPIO_SINGLE_ENDED;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303679 open_drain = flags & OF_GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003680 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003681 } else if (is_acpi_node(fwnode)) {
3682 struct acpi_gpio_info info;
3683
Boris Brezillon537b94d2017-02-02 14:53:11 +01003684 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003685 if (!IS_ERR(desc)) {
Christophe RICARD52044722015-12-23 23:25:34 +01003686 active_low = info.polarity == GPIO_ACTIVE_LOW;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003687 ret = acpi_gpio_update_gpiod_flags(&dflags, info.flags);
3688 if (ret)
3689 pr_debug("Override GPIO initialization flags\n");
3690 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003691 }
3692
3693 if (IS_ERR(desc))
3694 return desc;
3695
Alexander Steinb2987d72017-01-12 17:39:24 +01003696 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02003697 if (ret)
3698 return ERR_PTR(ret);
3699
Mika Westerberg40b73182014-10-21 13:33:59 +02003700 if (active_low)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003701 lflags |= GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003702
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003703 if (single_ended) {
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303704 if (open_drain)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003705 lflags |= GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003706 else
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003707 lflags |= GPIO_OPEN_SOURCE;
3708 }
3709
3710 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3711 if (ret < 0) {
3712 gpiod_put(desc);
3713 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003714 }
3715
Mika Westerberg40b73182014-10-21 13:33:59 +02003716 return desc;
3717}
3718EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3719
3720/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003721 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3722 * function
3723 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3724 * @con_id: function within the GPIO consumer
3725 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003726 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003727 *
3728 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3729 * specified index was assigned to the requested function it will return NULL.
3730 * This is convenient for drivers that need to handle optional GPIOs.
3731 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003732struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003733 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003734 unsigned int index,
3735 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003736{
3737 struct gpio_desc *desc;
3738
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003739 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003740 if (IS_ERR(desc)) {
3741 if (PTR_ERR(desc) == -ENOENT)
3742 return NULL;
3743 }
3744
3745 return desc;
3746}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003747EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003748
3749/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003750 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3751 * @desc: gpio whose value will be assigned
3752 * @name: gpio line name
3753 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3754 * of_get_gpio_hog()
3755 * @dflags: gpiod_flags - optional GPIO initialization flags
3756 */
3757int gpiod_hog(struct gpio_desc *desc, const char *name,
3758 unsigned long lflags, enum gpiod_flags dflags)
3759{
3760 struct gpio_chip *chip;
3761 struct gpio_desc *local_desc;
3762 int hwnum;
3763 int status;
3764
3765 chip = gpiod_to_chip(desc);
3766 hwnum = gpio_chip_hwgpio(desc);
3767
3768 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3769 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303770 status = PTR_ERR(local_desc);
3771 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3772 name, chip->label, hwnum, status);
3773 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003774 }
3775
Johan Hovold85b03b32016-07-03 18:32:05 +02003776 status = gpiod_configure_flags(desc, name, lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003777 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303778 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3779 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003780 gpiochip_free_own_desc(desc);
3781 return status;
3782 }
3783
3784 /* Mark GPIO as hogged so it can be identified and removed later */
3785 set_bit(FLAG_IS_HOGGED, &desc->flags);
3786
3787 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3788 desc_to_gpio(desc), name,
3789 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3790 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3791 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3792
3793 return 0;
3794}
3795
3796/**
3797 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3798 * @chip: gpio chip to act on
3799 *
3800 * This is only used by of_gpiochip_remove to free hogged gpios
3801 */
3802static void gpiochip_free_hogs(struct gpio_chip *chip)
3803{
3804 int id;
3805
3806 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003807 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3808 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003809 }
3810}
3811
3812/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003813 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3814 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3815 * @con_id: function within the GPIO consumer
3816 * @flags: optional GPIO initialization flags
3817 *
3818 * This function acquires all the GPIOs defined under a given function.
3819 *
3820 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3821 * no GPIO has been assigned to the requested function, or another IS_ERR()
3822 * code if an error occurred while trying to acquire the GPIOs.
3823 */
3824struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3825 const char *con_id,
3826 enum gpiod_flags flags)
3827{
3828 struct gpio_desc *desc;
3829 struct gpio_descs *descs;
3830 int count;
3831
3832 count = gpiod_count(dev, con_id);
3833 if (count < 0)
3834 return ERR_PTR(count);
3835
3836 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3837 GFP_KERNEL);
3838 if (!descs)
3839 return ERR_PTR(-ENOMEM);
3840
3841 for (descs->ndescs = 0; descs->ndescs < count; ) {
3842 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3843 if (IS_ERR(desc)) {
3844 gpiod_put_array(descs);
3845 return ERR_CAST(desc);
3846 }
3847 descs->desc[descs->ndescs] = desc;
3848 descs->ndescs++;
3849 }
3850 return descs;
3851}
3852EXPORT_SYMBOL_GPL(gpiod_get_array);
3853
3854/**
3855 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3856 * function
3857 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3858 * @con_id: function within the GPIO consumer
3859 * @flags: optional GPIO initialization flags
3860 *
3861 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3862 * assigned to the requested function it will return NULL.
3863 */
3864struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3865 const char *con_id,
3866 enum gpiod_flags flags)
3867{
3868 struct gpio_descs *descs;
3869
3870 descs = gpiod_get_array(dev, con_id, flags);
3871 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3872 return NULL;
3873
3874 return descs;
3875}
3876EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3877
3878/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003879 * gpiod_put - dispose of a GPIO descriptor
3880 * @desc: GPIO descriptor to dispose of
3881 *
3882 * No descriptor can be used after gpiod_put() has been called on it.
3883 */
3884void gpiod_put(struct gpio_desc *desc)
3885{
3886 gpiod_free(desc);
3887}
3888EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003889
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003890/**
3891 * gpiod_put_array - dispose of multiple GPIO descriptors
3892 * @descs: struct gpio_descs containing an array of descriptors
3893 */
3894void gpiod_put_array(struct gpio_descs *descs)
3895{
3896 unsigned int i;
3897
3898 for (i = 0; i < descs->ndescs; i++)
3899 gpiod_put(descs->desc[i]);
3900
3901 kfree(descs);
3902}
3903EXPORT_SYMBOL_GPL(gpiod_put_array);
3904
Linus Walleij3c702e92015-10-21 15:29:53 +02003905static int __init gpiolib_dev_init(void)
3906{
3907 int ret;
3908
3909 /* Register GPIO sysfs bus */
3910 ret = bus_register(&gpio_bus_type);
3911 if (ret < 0) {
3912 pr_err("gpiolib: could not register GPIO bus type\n");
3913 return ret;
3914 }
3915
3916 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3917 if (ret < 0) {
3918 pr_err("gpiolib: failed to allocate char dev region\n");
3919 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003920 } else {
3921 gpiolib_initialized = true;
3922 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003923 }
3924 return ret;
3925}
3926core_initcall(gpiolib_dev_init);
3927
David Brownelld2876d02008-02-04 22:28:20 -08003928#ifdef CONFIG_DEBUG_FS
3929
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003930static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003931{
3932 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003933 struct gpio_chip *chip = gdev->chip;
3934 unsigned gpio = gdev->base;
3935 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003936 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003937 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003938
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003939 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003940 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3941 if (gdesc->name) {
3942 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3943 gpio, gdesc->name);
3944 }
David Brownelld2876d02008-02-04 22:28:20 -08003945 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003946 }
David Brownelld2876d02008-02-04 22:28:20 -08003947
Alexandre Courbot372e7222013-02-03 01:29:29 +09003948 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003949 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003950 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003951 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3952 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003953 is_out ? "out" : "in ",
3954 chip->get
3955 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003956 : "? ",
3957 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003958 seq_printf(s, "\n");
3959 }
3960}
3961
Thierry Redingf9c4a312012-04-12 13:26:01 +02003962static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003963{
Grant Likely362432a2013-02-09 09:41:49 +00003964 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003965 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003966 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003967
3968 s->private = "";
3969
Grant Likely362432a2013-02-09 09:41:49 +00003970 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003971 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003972 if (index-- == 0) {
3973 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003974 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003975 }
3976 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003977
3978 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003979}
3980
3981static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3982{
Grant Likely362432a2013-02-09 09:41:49 +00003983 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003984 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003985 void *ret = NULL;
3986
Grant Likely362432a2013-02-09 09:41:49 +00003987 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003988 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003989 ret = NULL;
3990 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003991 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003992 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003993
3994 s->private = "\n";
3995 ++*pos;
3996
3997 return ret;
3998}
3999
4000static void gpiolib_seq_stop(struct seq_file *s, void *v)
4001{
4002}
4003
4004static int gpiolib_seq_show(struct seq_file *s, void *v)
4005{
Linus Walleijff2b1352015-10-20 11:10:38 +02004006 struct gpio_device *gdev = v;
4007 struct gpio_chip *chip = gdev->chip;
4008 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004009
Linus Walleijff2b1352015-10-20 11:10:38 +02004010 if (!chip) {
4011 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4012 dev_name(&gdev->dev));
4013 return 0;
4014 }
4015
4016 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4017 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004018 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02004019 parent = chip->parent;
4020 if (parent)
4021 seq_printf(s, ", parent: %s/%s",
4022 parent->bus ? parent->bus->name : "no-bus",
4023 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02004024 if (chip->label)
4025 seq_printf(s, ", %s", chip->label);
4026 if (chip->can_sleep)
4027 seq_printf(s, ", can sleep");
4028 seq_printf(s, ":\n");
4029
4030 if (chip->dbg_show)
4031 chip->dbg_show(s, chip);
4032 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004033 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02004034
David Brownelld2876d02008-02-04 22:28:20 -08004035 return 0;
4036}
4037
Thierry Redingf9c4a312012-04-12 13:26:01 +02004038static const struct seq_operations gpiolib_seq_ops = {
4039 .start = gpiolib_seq_start,
4040 .next = gpiolib_seq_next,
4041 .stop = gpiolib_seq_stop,
4042 .show = gpiolib_seq_show,
4043};
4044
David Brownelld2876d02008-02-04 22:28:20 -08004045static int gpiolib_open(struct inode *inode, struct file *file)
4046{
Thierry Redingf9c4a312012-04-12 13:26:01 +02004047 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08004048}
4049
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004050static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02004051 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08004052 .open = gpiolib_open,
4053 .read = seq_read,
4054 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02004055 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08004056};
4057
4058static int __init gpiolib_debugfs_init(void)
4059{
4060 /* /sys/kernel/debug/gpio */
4061 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4062 NULL, NULL, &gpiolib_operations);
4063 return 0;
4064}
4065subsys_initcall(gpiolib_debugfs_init);
4066
4067#endif /* DEBUG_FS */