blob: 7347ea1c699a7b328ef6eba859c829276cdfc473 [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{
1606 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1607 parent_handler);
1608}
Linus Walleij14250522014-03-25 10:40:18 +01001609EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1610
1611/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001612 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1613 * @gpiochip: the gpiochip to set the irqchip nested handler to
1614 * @irqchip: the irqchip to nest to the gpiochip
1615 * @parent_irq: the irq number corresponding to the parent IRQ for this
1616 * nested irqchip
1617 */
1618void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1619 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001620 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001621{
Thierry Redingdc6bafe2017-11-07 19:15:51 +01001622 if (!gpiochip->irq.nested) {
Linus Walleijd245b3f2016-11-24 10:57:25 +01001623 chip_err(gpiochip, "tried to nest a chained gpiochip\n");
1624 return;
1625 }
1626 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1627 NULL);
1628}
1629EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1630
1631/**
Linus Walleij14250522014-03-25 10:40:18 +01001632 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1633 * @d: the irqdomain used by this irqchip
1634 * @irq: the global irq number used by this GPIO irqchip irq
1635 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1636 *
1637 * This function will set up the mapping for a certain IRQ line on a
1638 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1639 * stored inside the gpiochip.
1640 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001641int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1642 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01001643{
1644 struct gpio_chip *chip = d->host_data;
Thierry Redinge0d89722017-11-07 19:15:54 +01001645 int err = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001646
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001647 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1648 return -ENXIO;
1649
Linus Walleij14250522014-03-25 10:40:18 +01001650 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001651 /*
1652 * This lock class tells lockdep that GPIO irqs are in a different
1653 * category than their parents, so it won't report false recursion.
1654 */
Thierry Redingca9df052017-11-07 19:15:53 +01001655 irq_set_lockdep_class(irq, chip->irq.lock_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001656 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001657 /* Chips that use nested thread handlers have them marked */
Thierry Redingdc6bafe2017-11-07 19:15:51 +01001658 if (chip->irq.nested)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001659 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001660 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001661
Thierry Redinge0d89722017-11-07 19:15:54 +01001662 if (chip->irq.num_parents == 1)
1663 err = irq_set_parent(irq, chip->irq.parents[0]);
1664 else if (chip->irq.map)
1665 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1666
1667 if (err < 0)
1668 return err;
1669
Linus Walleij1333b902014-04-23 16:45:12 +02001670 /*
1671 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1672 * is passed as default type.
1673 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01001674 if (chip->irq.default_type != IRQ_TYPE_NONE)
1675 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001676
1677 return 0;
1678}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001679EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01001680
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001681void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01001682{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001683 struct gpio_chip *chip = d->host_data;
1684
Thierry Redingdc6bafe2017-11-07 19:15:51 +01001685 if (chip->irq.nested)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001686 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001687 irq_set_chip_and_handler(irq, NULL, NULL);
1688 irq_set_chip_data(irq, NULL);
1689}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001690EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001691
Linus Walleij14250522014-03-25 10:40:18 +01001692static const struct irq_domain_ops gpiochip_domain_ops = {
1693 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001694 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001695 /* Virtually all GPIO irqchips are twocell:ed */
1696 .xlate = irq_domain_xlate_twocell,
1697};
1698
1699static int gpiochip_irq_reqres(struct irq_data *d)
1700{
1701 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1702
Linus Walleijff2b1352015-10-20 11:10:38 +02001703 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001704 return -ENODEV;
1705
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001706 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001707 chip_err(chip,
1708 "unable to lock HW IRQ %lu for IRQ\n",
1709 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001710 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001711 return -EINVAL;
1712 }
1713 return 0;
1714}
1715
1716static void gpiochip_irq_relres(struct irq_data *d)
1717{
1718 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1719
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001720 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001721 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001722}
1723
1724static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1725{
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001726 if (!gpiochip_irqchip_irq_valid(chip, offset))
1727 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01001728
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001729 return irq_create_mapping(chip->irq.domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01001730}
1731
1732/**
Thierry Redinge0d89722017-11-07 19:15:54 +01001733 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1734 * @gpiochip: the GPIO chip to add the IRQ chip to
1735 */
1736static int gpiochip_add_irqchip(struct gpio_chip *gpiochip)
1737{
1738 struct irq_chip *irqchip = gpiochip->irq.chip;
1739 const struct irq_domain_ops *ops;
1740 struct device_node *np;
1741 unsigned int type;
1742 unsigned int i;
1743
1744 if (!irqchip)
1745 return 0;
1746
1747 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
1748 chip_err(gpiochip, "you cannot have chained interrupts on a "
1749 "chip that may sleep\n");
1750 return -EINVAL;
1751 }
1752
1753 np = gpiochip->gpiodev->dev.of_node;
1754 type = gpiochip->irq.default_type;
1755
1756 /*
1757 * Specifying a default trigger is a terrible idea if DT or ACPI is
1758 * used to configure the interrupts, as you may end up with
1759 * conflicting triggers. Tell the user, and reset to NONE.
1760 */
1761 if (WARN(np && type != IRQ_TYPE_NONE,
1762 "%s: Ignoring %u default trigger\n", np->full_name, type))
1763 type = IRQ_TYPE_NONE;
1764
1765 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1766 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1767 "Ignoring %u default trigger\n", type);
1768 type = IRQ_TYPE_NONE;
1769 }
1770
1771 gpiochip->to_irq = gpiochip_to_irq;
1772 gpiochip->irq.default_type = type;
1773
1774 if (gpiochip->irq.domain_ops)
1775 ops = gpiochip->irq.domain_ops;
1776 else
1777 ops = &gpiochip_domain_ops;
1778
1779 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
1780 0, ops, gpiochip);
1781 if (!gpiochip->irq.domain)
1782 return -EINVAL;
1783
1784 /*
1785 * It is possible for a driver to override this, but only if the
1786 * alternative functions are both implemented.
1787 */
1788 if (!irqchip->irq_request_resources &&
1789 !irqchip->irq_release_resources) {
1790 irqchip->irq_request_resources = gpiochip_irq_reqres;
1791 irqchip->irq_release_resources = gpiochip_irq_relres;
1792 }
1793
1794 if (gpiochip->irq.parent_handler) {
1795 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1796
1797 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1798 /*
1799 * The parent IRQ chip is already using the chip_data
1800 * for this IRQ chip, so our callbacks simply use the
1801 * handler_data.
1802 */
1803 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1804 gpiochip->irq.parent_handler,
1805 data);
1806 }
1807
1808 gpiochip->irq.nested = false;
1809 } else {
1810 gpiochip->irq.nested = true;
1811 }
1812
1813 acpi_gpiochip_request_interrupts(gpiochip);
1814
1815 return 0;
1816}
1817
1818/**
Linus Walleij14250522014-03-25 10:40:18 +01001819 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1820 * @gpiochip: the gpiochip to remove the irqchip from
1821 *
1822 * This is called only from gpiochip_remove()
1823 */
1824static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1825{
Thierry Reding39e5f092017-11-07 19:15:50 +01001826 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001827
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001828 acpi_gpiochip_free_interrupts(gpiochip);
1829
Thierry Redinge0d89722017-11-07 19:15:54 +01001830 if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001831 struct gpio_irq_chip *irq = &gpiochip->irq;
1832 unsigned int i;
1833
1834 for (i = 0; i < irq->num_parents; i++)
1835 irq_set_chained_handler_and_data(irq->parents[i],
1836 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001837 }
1838
Linus Walleijc3626fd2014-03-28 20:42:01 +01001839 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001840 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001841 unsigned int irq;
1842
Mika Westerberg79b804c2016-09-20 15:15:21 +03001843 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1844 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1845 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001846
1847 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1848 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001849 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001850
1851 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001852 }
Linus Walleij14250522014-03-25 10:40:18 +01001853
Thierry Redingda80ff82017-11-07 19:15:46 +01001854 if (gpiochip->irq.chip) {
1855 gpiochip->irq.chip->irq_request_resources = NULL;
1856 gpiochip->irq.chip->irq_release_resources = NULL;
1857 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001858 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001859
1860 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001861}
1862
1863/**
Linus Walleij739e6f52017-01-11 13:37:07 +01001864 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001865 * @gpiochip: the gpiochip to add the irqchip to
1866 * @irqchip: the irqchip to add to the gpiochip
1867 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1868 * allocate gpiochip irqs from
1869 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001870 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1871 * to have the core avoid setting up any default type in the hardware.
Linus Walleijd245b3f2016-11-24 10:57:25 +01001872 * @nested: whether this is a nested irqchip calling handle_nested_irq()
1873 * in its IRQ handler
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001874 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001875 *
1876 * This function closely associates a certain irqchip with a certain
1877 * gpiochip, providing an irq domain to translate the local IRQs to
1878 * global irqs in the gpiolib core, and making sure that the gpiochip
1879 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001880 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001881 * from the gpiochip passed as chip data. An irqdomain will be stored
1882 * in the gpiochip that shall be used by the driver to handle IRQ number
1883 * translation. The gpiochip will need to be initialized and registered
1884 * before calling this function.
1885 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001886 * This function will handle two cell:ed simple IRQs and assumes all
1887 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001888 * need to be open coded.
1889 */
Linus Walleij739e6f52017-01-11 13:37:07 +01001890int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1891 struct irq_chip *irqchip,
1892 unsigned int first_irq,
1893 irq_flow_handler_t handler,
1894 unsigned int type,
1895 bool nested,
1896 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001897{
1898 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001899
1900 if (!gpiochip || !irqchip)
1901 return -EINVAL;
1902
Linus Walleij58383c782015-11-04 09:56:26 +01001903 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001904 pr_err("missing gpiochip .dev parent pointer\n");
1905 return -EINVAL;
1906 }
Thierry Redingdc6bafe2017-11-07 19:15:51 +01001907 gpiochip->irq.nested = nested;
Linus Walleij58383c782015-11-04 09:56:26 +01001908 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001909#ifdef CONFIG_OF_GPIO
1910 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001911 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001912 * FIXME: get rid of this and use gpiochip->parent->of_node
1913 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001914 */
1915 if (gpiochip->of_node)
1916 of_node = gpiochip->of_node;
1917#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001918 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001919 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001920 * used to configure the interrupts, as you may end-up with
1921 * conflicting triggers. Tell the user, and reset to NONE.
1922 */
1923 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05001924 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01001925 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001926 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1927 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1928 "Ignoring %d default trigger\n", type);
1929 type = IRQ_TYPE_NONE;
1930 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001931
Thierry Redingda80ff82017-11-07 19:15:46 +01001932 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001933 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01001934 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01001935 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01001936 gpiochip->irq.lock_key = lock_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001937 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01001938 gpiochip->ngpio, first_irq,
1939 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001940 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01001941 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001942 return -EINVAL;
1943 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001944
1945 /*
1946 * It is possible for a driver to override this, but only if the
1947 * alternative functions are both implemented.
1948 */
1949 if (!irqchip->irq_request_resources &&
1950 !irqchip->irq_release_resources) {
1951 irqchip->irq_request_resources = gpiochip_irq_reqres;
1952 irqchip->irq_release_resources = gpiochip_irq_relres;
1953 }
Linus Walleij14250522014-03-25 10:40:18 +01001954
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001955 acpi_gpiochip_request_interrupts(gpiochip);
1956
Linus Walleij14250522014-03-25 10:40:18 +01001957 return 0;
1958}
Linus Walleij739e6f52017-01-11 13:37:07 +01001959EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01001960
1961#else /* CONFIG_GPIOLIB_IRQCHIP */
1962
Thierry Redinge0d89722017-11-07 19:15:54 +01001963static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip)
1964{
1965 return 0;
1966}
1967
Linus Walleij14250522014-03-25 10:40:18 +01001968static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03001969static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1970{
1971 return 0;
1972}
1973static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1974{ }
Linus Walleij14250522014-03-25 10:40:18 +01001975
1976#endif /* CONFIG_GPIOLIB_IRQCHIP */
1977
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001978/**
1979 * gpiochip_generic_request() - request the gpio function for a pin
1980 * @chip: the gpiochip owning the GPIO
1981 * @offset: the offset of the GPIO to request for GPIO function
1982 */
1983int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1984{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001985 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001986}
1987EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1988
1989/**
1990 * gpiochip_generic_free() - free the gpio function from a pin
1991 * @chip: the gpiochip to request the gpio function for
1992 * @offset: the offset of the GPIO to free from GPIO function
1993 */
1994void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1995{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001996 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001997}
1998EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1999
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002000/**
2001 * gpiochip_generic_config() - apply configuration for a pin
2002 * @chip: the gpiochip owning the GPIO
2003 * @offset: the offset of the GPIO to apply the configuration
2004 * @config: the configuration to be applied
2005 */
2006int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2007 unsigned long config)
2008{
2009 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2010}
2011EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2012
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302013#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002014
Linus Walleij3f0f8672012-11-20 12:40:15 +01002015/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002016 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2017 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002018 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002019 * @gpio_offset: the start offset in the current gpio_chip number space
2020 * @pin_group: name of the pin group inside the pin controller
2021 */
2022int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2023 struct pinctrl_dev *pctldev,
2024 unsigned int gpio_offset, const char *pin_group)
2025{
2026 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002027 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002028 int ret;
2029
2030 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2031 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002032 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002033 return -ENOMEM;
2034 }
2035
2036 /* Use local offset as range ID */
2037 pin_range->range.id = gpio_offset;
2038 pin_range->range.gc = chip;
2039 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002040 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002041 pin_range->pctldev = pctldev;
2042
2043 ret = pinctrl_get_group_pins(pctldev, pin_group,
2044 &pin_range->range.pins,
2045 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002046 if (ret < 0) {
2047 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002048 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002049 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002050
2051 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2052
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002053 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2054 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002055 pinctrl_dev_get_devname(pctldev), pin_group);
2056
Linus Walleij20ec3e32016-02-11 11:03:06 +01002057 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002058
2059 return 0;
2060}
2061EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2062
2063/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002064 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2065 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002066 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002067 * @gpio_offset: the start offset in the current gpio_chip number space
2068 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002069 * @npins: the number of pins from the offset of each pin space (GPIO and
2070 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002071 *
2072 * Returns:
2073 * 0 on success, or a negative error-code on failure.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002074 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002075int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002076 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002077 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302078{
2079 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002080 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002081 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302082
Linus Walleij3f0f8672012-11-20 12:40:15 +01002083 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302084 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002085 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002086 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302087 }
2088
Linus Walleij3f0f8672012-11-20 12:40:15 +01002089 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002090 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002091 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302092 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002093 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002094 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302095 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002096 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302097 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002098 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002099 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002100 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002101 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002102 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002103 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002104 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2105 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002106 pinctl_name,
2107 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302108
Linus Walleij20ec3e32016-02-11 11:03:06 +01002109 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002110
2111 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302112}
Linus Walleij165adc92012-11-06 14:49:39 +01002113EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302114
Linus Walleij3f0f8672012-11-20 12:40:15 +01002115/**
2116 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2117 * @chip: the chip to remove all the mappings for
2118 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302119void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2120{
2121 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002122 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302123
Linus Walleij20ec3e32016-02-11 11:03:06 +01002124 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302125 list_del(&pin_range->node);
2126 pinctrl_remove_gpio_range(pin_range->pctldev,
2127 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002128 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302129 }
2130}
Linus Walleij165adc92012-11-06 14:49:39 +01002131EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2132
2133#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302134
David Brownelld2876d02008-02-04 22:28:20 -08002135/* These "optional" allocation calls help prevent drivers from stomping
2136 * on each other, and help provide better diagnostics in debugfs.
2137 * They're called even less than the "set direction" calls.
2138 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002139static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002140{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002141 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002142 int status;
David Brownelld2876d02008-02-04 22:28:20 -08002143 unsigned long flags;
2144
2145 spin_lock_irqsave(&gpio_lock, flags);
2146
David Brownelld2876d02008-02-04 22:28:20 -08002147 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002148 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002149 */
2150
2151 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2152 desc_set_label(desc, label ? : "?");
2153 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002154 } else {
David Brownelld2876d02008-02-04 22:28:20 -08002155 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002156 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002157 }
2158
2159 if (chip->request) {
2160 /* chip->request may sleep */
2161 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002162 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002163 spin_lock_irqsave(&gpio_lock, flags);
2164
2165 if (status < 0) {
2166 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07002167 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002168 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002169 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002170 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002171 if (chip->get_direction) {
2172 /* chip->get_direction may sleep */
2173 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002174 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002175 spin_lock_irqsave(&gpio_lock, flags);
2176 }
David Brownelld2876d02008-02-04 22:28:20 -08002177done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002178 spin_unlock_irqrestore(&gpio_lock, flags);
2179 return status;
2180}
2181
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002182/*
2183 * This descriptor validation needs to be inserted verbatim into each
2184 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002185 * macro to avoid endless duplication. If the desc is NULL it is an
2186 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002187 */
2188#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002189 if (!desc) \
2190 return 0; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002191 if (IS_ERR(desc)) { \
2192 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2193 return PTR_ERR(desc); \
2194 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002195 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002196 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002197 return -EINVAL; \
2198 } \
2199 if ( !desc->gdev->chip ) { \
2200 dev_warn(&desc->gdev->dev, \
2201 "%s: backing chip is gone\n", __func__); \
2202 return 0; \
2203 } } while (0)
2204
2205#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002206 if (!desc) \
2207 return; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002208 if (IS_ERR(desc)) { \
2209 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2210 return; \
2211 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002212 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002213 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002214 return; \
2215 } \
2216 if (!desc->gdev->chip) { \
2217 dev_warn(&desc->gdev->dev, \
2218 "%s: backing chip is gone\n", __func__); \
2219 return; \
2220 } } while (0)
2221
2222
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002223int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002224{
2225 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002226 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002227
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002228 VALIDATE_DESC(desc);
2229 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002230
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002231 if (try_module_get(gdev->owner)) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002232 status = gpiod_request_commit(desc, label);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002233 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002234 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002235 else
2236 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002237 }
2238
David Brownelld2876d02008-02-04 22:28:20 -08002239 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002240 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002241
David Brownelld2876d02008-02-04 22:28:20 -08002242 return status;
2243}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002244
Linus Walleijfac9d882017-09-26 20:58:28 +02002245static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002246{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002247 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002248 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002249 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002250
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002251 might_sleep();
2252
Alexandre Courbot372e7222013-02-03 01:29:29 +09002253 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002254
David Brownelld2876d02008-02-04 22:28:20 -08002255 spin_lock_irqsave(&gpio_lock, flags);
2256
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002257 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002258 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2259 if (chip->free) {
2260 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002261 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002262 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002263 spin_lock_irqsave(&gpio_lock, flags);
2264 }
David Brownelld2876d02008-02-04 22:28:20 -08002265 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002266 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002267 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302268 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302269 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002270 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002271 ret = true;
2272 }
David Brownelld2876d02008-02-04 22:28:20 -08002273
2274 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002275 return ret;
2276}
2277
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002278void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002279{
Linus Walleijfac9d882017-09-26 20:58:28 +02002280 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002281 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002282 put_device(&desc->gdev->dev);
2283 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002284 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002285 }
David Brownelld2876d02008-02-04 22:28:20 -08002286}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002287
David Brownelld2876d02008-02-04 22:28:20 -08002288/**
2289 * gpiochip_is_requested - return string iff signal was requested
2290 * @chip: controller managing the signal
2291 * @offset: of signal within controller's 0..(ngpio - 1) range
2292 *
2293 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002294 * The string returned is the label passed to gpio_request(); if none has been
2295 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002296 *
2297 * This function is for use by GPIO controller drivers. The label can
2298 * help with diagnostics, and knowing that the signal is used as a GPIO
2299 * can help avoid accidentally multiplexing it to another controller.
2300 */
2301const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2302{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002303 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002304
Dirk Behme48b59532015-08-18 18:02:32 +02002305 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002306 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002307
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002308 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002309
Alexandre Courbot372e7222013-02-03 01:29:29 +09002310 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002311 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002312 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002313}
2314EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2315
Mika Westerberg77c2d792014-03-10 14:54:50 +02002316/**
2317 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002318 * @chip: GPIO chip
2319 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002320 * @label: label for the GPIO
2321 *
2322 * Function allows GPIO chip drivers to request and use their own GPIO
2323 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2324 * function will not increase reference count of the GPIO chip module. This
2325 * allows the GPIO chip module to be unloaded as needed (we assume that the
2326 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002327 *
2328 * Returns:
2329 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2330 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002331 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002332struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2333 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002334{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002335 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2336 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002337
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002338 if (IS_ERR(desc)) {
2339 chip_err(chip, "failed to get GPIO descriptor\n");
2340 return desc;
2341 }
2342
Linus Walleijfac9d882017-09-26 20:58:28 +02002343 err = gpiod_request_commit(desc, label);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002344 if (err < 0)
2345 return ERR_PTR(err);
2346
2347 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002348}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002349EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002350
2351/**
2352 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2353 * @desc: GPIO descriptor to free
2354 *
2355 * Function frees the given GPIO requested previously with
2356 * gpiochip_request_own_desc().
2357 */
2358void gpiochip_free_own_desc(struct gpio_desc *desc)
2359{
2360 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002361 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002362}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002363EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002364
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002365/*
2366 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002367 * some cases this is done in early boot, before IRQs are enabled.
2368 *
2369 * As a rule these aren't called more than once (except for drivers
2370 * using the open-drain emulation idiom) so these are natural places
2371 * to accumulate extra debugging checks. Note that we can't (yet)
2372 * rely on gpio_request() having been called beforehand.
2373 */
2374
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002375/**
2376 * gpiod_direction_input - set the GPIO direction to input
2377 * @desc: GPIO to set to input
2378 *
2379 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2380 * be called safely on it.
2381 *
2382 * Return 0 in case of success, else an error code.
2383 */
2384int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002385{
David Brownelld2876d02008-02-04 22:28:20 -08002386 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002387 int status = -EINVAL;
2388
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002389 VALIDATE_DESC(desc);
2390 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002391
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002392 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002393 gpiod_warn(desc,
2394 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002395 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002396 return -EIO;
2397 }
2398
Alexandre Courbotd82da792014-07-22 16:17:43 +09002399 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002400 if (status == 0)
2401 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002402
Alexandre Courbot372e7222013-02-03 01:29:29 +09002403 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002404
David Brownelld2876d02008-02-04 22:28:20 -08002405 return status;
2406}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002407EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002408
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002409static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2410 enum pin_config_param mode)
2411{
2412 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2413
2414 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2415}
2416
Linus Walleijfac9d882017-09-26 20:58:28 +02002417static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002418{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002419 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002420 int val = !!value;
Linus Walleijc663e5f2016-03-22 10:51:16 +01002421 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002422
Linus Walleijc663e5f2016-03-22 10:51:16 +01002423 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002424 gpiod_warn(desc,
2425 "%s: missing set() or direction_output() operations\n",
2426 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002427 return -EIO;
2428 }
2429
Linus Walleijad177312016-11-13 23:02:44 +01002430 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002431 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002432 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002433 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002434 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2435 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002436}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002437
2438/**
2439 * gpiod_direction_output_raw - set the GPIO direction to output
2440 * @desc: GPIO to set to output
2441 * @value: initial output value of the GPIO
2442 *
2443 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2444 * be called safely on it. The initial value of the output must be specified
2445 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2446 *
2447 * Return 0 in case of success, else an error code.
2448 */
2449int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2450{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002451 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002452 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002453}
2454EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2455
2456/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302457 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002458 * @desc: GPIO to set to output
2459 * @value: initial output value of the GPIO
2460 *
2461 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2462 * be called safely on it. The initial value of the output must be specified
2463 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2464 * account.
2465 *
2466 * Return 0 in case of success, else an error code.
2467 */
2468int gpiod_direction_output(struct gpio_desc *desc, int value)
2469{
Linus Walleij02e47982017-09-26 21:20:23 +02002470 struct gpio_chip *gc = desc->gdev->chip;
2471 int ret;
2472
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002473 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002474 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2475 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002476 else
2477 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02002478
2479 /* GPIOs used for IRQs shall not be set as output */
2480 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2481 gpiod_err(desc,
2482 "%s: tried to set a GPIO tied to an IRQ as output\n",
2483 __func__);
2484 return -EIO;
2485 }
2486
2487 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2488 /* First see if we can enable open drain in hardware */
2489 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2490 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2491 if (!ret)
2492 goto set_output_value;
2493 /* Emulate open drain by not actively driving the line high */
2494 if (value)
2495 return gpiod_direction_input(desc);
2496 }
2497 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2498 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2499 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2500 if (!ret)
2501 goto set_output_value;
2502 /* Emulate open source by not actively driving the line low */
2503 if (!value)
2504 return gpiod_direction_input(desc);
2505 } else {
2506 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2507 PIN_CONFIG_DRIVE_PUSH_PULL);
2508 }
2509
2510set_output_value:
Linus Walleijfac9d882017-09-26 20:58:28 +02002511 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002512}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002513EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002514
Felipe Balbic4b5be92010-05-26 14:42:23 -07002515/**
Thierry Reding950d55f52017-07-24 16:57:22 +02002516 * gpiod_set_debounce - sets @debounce time for a GPIO
2517 * @desc: descriptor of the GPIO for which to set debounce time
2518 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002519 *
Thierry Reding950d55f52017-07-24 16:57:22 +02002520 * Returns:
2521 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2522 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002523 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002524int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002525{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002526 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002527 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002528
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002529 VALIDATE_DESC(desc);
2530 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002531 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01002532 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002533 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01002534 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002535 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002536 }
2537
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002538 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2539 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002540}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002541EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002542
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002543/**
2544 * gpiod_is_active_low - test whether a GPIO is active-low or not
2545 * @desc: the gpio descriptor to test
2546 *
2547 * Returns 1 if the GPIO is active-low, 0 otherwise.
2548 */
2549int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002550{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002551 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002552 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002553}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002554EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002555
2556/* I/O calls are only valid after configuration completed; the relevant
2557 * "is this a valid GPIO" error checks should already have been done.
2558 *
2559 * "Get" operations are often inlinable as reading a pin value register,
2560 * and masking the relevant bit in that register.
2561 *
2562 * When "set" operations are inlinable, they involve writing that mask to
2563 * one register to set a low value, or a different register to set it high.
2564 * Otherwise locking is needed, so there may be little value to inlining.
2565 *
2566 *------------------------------------------------------------------------
2567 *
2568 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2569 * have requested the GPIO. That can include implicit requesting by
2570 * a direction setting call. Marking a gpio as requested locks its chip
2571 * in memory, guaranteeing that these table lookups need no more locking
2572 * and that gpiochip_remove() will fail.
2573 *
2574 * REVISIT when debugging, consider adding some instrumentation to ensure
2575 * that the GPIO was actually requested.
2576 */
2577
Linus Walleijfac9d882017-09-26 20:58:28 +02002578static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002579{
2580 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002581 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002582 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002583
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002584 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002585 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002586 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002587 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002588 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002589 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002590}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002591
Lukas Wunnereec1d562017-10-12 12:40:10 +02002592static int gpio_chip_get_multiple(struct gpio_chip *chip,
2593 unsigned long *mask, unsigned long *bits)
2594{
2595 if (chip->get_multiple) {
2596 return chip->get_multiple(chip, mask, bits);
2597 } else if (chip->get) {
2598 int i, value;
2599
2600 for_each_set_bit(i, mask, chip->ngpio) {
2601 value = chip->get(chip, i);
2602 if (value < 0)
2603 return value;
2604 __assign_bit(i, bits, value);
2605 }
2606 return 0;
2607 }
2608 return -EIO;
2609}
2610
2611int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2612 unsigned int array_size,
2613 struct gpio_desc **desc_array,
2614 int *value_array)
2615{
2616 int i = 0;
2617
2618 while (i < array_size) {
2619 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2620 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2621 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2622 int first, j, ret;
2623
2624 if (!can_sleep)
2625 WARN_ON(chip->can_sleep);
2626
2627 /* collect all inputs belonging to the same chip */
2628 first = i;
2629 memset(mask, 0, sizeof(mask));
2630 do {
2631 const struct gpio_desc *desc = desc_array[i];
2632 int hwgpio = gpio_chip_hwgpio(desc);
2633
2634 __set_bit(hwgpio, mask);
2635 i++;
2636 } while ((i < array_size) &&
2637 (desc_array[i]->gdev->chip == chip));
2638
2639 ret = gpio_chip_get_multiple(chip, mask, bits);
2640 if (ret)
2641 return ret;
2642
2643 for (j = first; j < i; j++) {
2644 const struct gpio_desc *desc = desc_array[j];
2645 int hwgpio = gpio_chip_hwgpio(desc);
2646 int value = test_bit(hwgpio, bits);
2647
2648 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2649 value = !value;
2650 value_array[j] = value;
2651 trace_gpio_value(desc_to_gpio(desc), 1, value);
2652 }
2653 }
2654 return 0;
2655}
2656
David Brownelld2876d02008-02-04 22:28:20 -08002657/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002658 * gpiod_get_raw_value() - return a gpio's raw value
2659 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002660 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002661 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002662 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002663 *
2664 * This function should be called from contexts where we cannot sleep, and will
2665 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002666 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002667int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002668{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002669 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002670 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002671 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002672 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002673}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002674EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002675
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002676/**
2677 * gpiod_get_value() - return a gpio's value
2678 * @desc: gpio whose value will be returned
2679 *
2680 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002681 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002682 *
2683 * This function should be called from contexts where we cannot sleep, and will
2684 * complain if the GPIO chip functions potentially sleep.
2685 */
2686int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002687{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002688 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002689
2690 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002691 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002692 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002693
Linus Walleijfac9d882017-09-26 20:58:28 +02002694 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002695 if (value < 0)
2696 return value;
2697
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002698 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2699 value = !value;
2700
2701 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002702}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002703EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002704
Lukas Wunnereec1d562017-10-12 12:40:10 +02002705/**
2706 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2707 * @array_size: number of elements in the descriptor / value arrays
2708 * @desc_array: array of GPIO descriptors whose values will be read
2709 * @value_array: array to store the read values
2710 *
2711 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2712 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2713 * else an error code.
2714 *
2715 * This function should be called from contexts where we cannot sleep,
2716 * and it will complain if the GPIO chip functions potentially sleep.
2717 */
2718int gpiod_get_raw_array_value(unsigned int array_size,
2719 struct gpio_desc **desc_array, int *value_array)
2720{
2721 if (!desc_array)
2722 return -EINVAL;
2723 return gpiod_get_array_value_complex(true, false, array_size,
2724 desc_array, value_array);
2725}
2726EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2727
2728/**
2729 * gpiod_get_array_value() - read values from an array of GPIOs
2730 * @array_size: number of elements in the descriptor / value arrays
2731 * @desc_array: array of GPIO descriptors whose values will be read
2732 * @value_array: array to store the read values
2733 *
2734 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2735 * into account. Return 0 in case of success, else an error code.
2736 *
2737 * This function should be called from contexts where we cannot sleep,
2738 * and it will complain if the GPIO chip functions potentially sleep.
2739 */
2740int gpiod_get_array_value(unsigned int array_size,
2741 struct gpio_desc **desc_array, int *value_array)
2742{
2743 if (!desc_array)
2744 return -EINVAL;
2745 return gpiod_get_array_value_complex(false, false, array_size,
2746 desc_array, value_array);
2747}
2748EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2749
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302750/*
Linus Walleijfac9d882017-09-26 20:58:28 +02002751 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002752 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002753 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302754 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002755static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302756{
2757 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002758 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002759 int offset = gpio_chip_hwgpio(desc);
2760
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302761 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002762 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302763 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002764 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302765 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002766 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302767 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002768 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302769 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002770 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302771 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002772 gpiod_err(desc,
2773 "%s: Error in set_value for open drain err %d\n",
2774 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302775}
2776
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302777/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002778 * _gpio_set_open_source_value() - Set the open source gpio's value.
2779 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002780 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302781 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002782static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302783{
2784 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002785 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002786 int offset = gpio_chip_hwgpio(desc);
2787
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302788 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002789 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302790 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002791 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302792 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002793 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302794 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002795 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302796 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002797 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302798 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002799 gpiod_err(desc,
2800 "%s: Error in set_value for open source err %d\n",
2801 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302802}
2803
Linus Walleijfac9d882017-09-26 20:58:28 +02002804static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002805{
2806 struct gpio_chip *chip;
2807
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002808 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002809 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002810 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002811}
2812
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002813/*
2814 * set multiple outputs on the same chip;
2815 * use the chip's set_multiple function if available;
2816 * otherwise set the outputs sequentially;
2817 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2818 * defines which outputs are to be changed
2819 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2820 * defines the values the outputs specified by mask are to be set to
2821 */
2822static void gpio_chip_set_multiple(struct gpio_chip *chip,
2823 unsigned long *mask, unsigned long *bits)
2824{
2825 if (chip->set_multiple) {
2826 chip->set_multiple(chip, mask, bits);
2827 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02002828 unsigned int i;
2829
2830 /* set outputs if the corresponding mask bit is set */
2831 for_each_set_bit(i, mask, chip->ngpio)
2832 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002833 }
2834}
2835
Linus Walleij44c72882016-04-24 11:36:59 +02002836void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2837 unsigned int array_size,
2838 struct gpio_desc **desc_array,
2839 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002840{
2841 int i = 0;
2842
2843 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002844 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002845 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2846 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2847 int count = 0;
2848
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002849 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002850 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002851
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002852 memset(mask, 0, sizeof(mask));
2853 do {
2854 struct gpio_desc *desc = desc_array[i];
2855 int hwgpio = gpio_chip_hwgpio(desc);
2856 int value = value_array[i];
2857
2858 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2859 value = !value;
2860 trace_gpio_value(desc_to_gpio(desc), 0, value);
2861 /*
2862 * collect all normal outputs belonging to the same chip
2863 * open drain and open source outputs are set individually
2864 */
Linus Walleij02e47982017-09-26 21:20:23 +02002865 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002866 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002867 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002868 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002869 } else {
2870 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002871 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002872 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002873 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002874 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002875 count++;
2876 }
2877 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002878 } while ((i < array_size) &&
2879 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002880 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002881 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002882 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002883 }
2884}
2885
David Brownelld2876d02008-02-04 22:28:20 -08002886/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002887 * gpiod_set_raw_value() - assign a gpio's raw value
2888 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002889 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002890 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002891 * Set the raw value of the GPIO, i.e. the value of its physical line without
2892 * regard for its ACTIVE_LOW status.
2893 *
2894 * This function should be called from contexts where we cannot sleep, and will
2895 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002896 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002897void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002898{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002899 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002900 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002901 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002902 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002903}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002904EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002905
2906/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002907 * gpiod_set_value() - assign a gpio's value
2908 * @desc: gpio whose value will be assigned
2909 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002910 *
Linus Walleij02e47982017-09-26 21:20:23 +02002911 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
2912 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002913 *
2914 * This function should be called from contexts where we cannot sleep, and will
2915 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002916 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002917void gpiod_set_value(struct gpio_desc *desc, int value)
2918{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002919 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002920 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002921 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002922 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2923 value = !value;
Linus Walleij02e47982017-09-26 21:20:23 +02002924 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2925 gpio_set_open_drain_value_commit(desc, value);
2926 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2927 gpio_set_open_source_value_commit(desc, value);
2928 else
2929 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002930}
2931EXPORT_SYMBOL_GPL(gpiod_set_value);
2932
2933/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002934 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002935 * @array_size: number of elements in the descriptor / value arrays
2936 * @desc_array: array of GPIO descriptors whose values will be assigned
2937 * @value_array: array of values to assign
2938 *
2939 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2940 * without regard for their ACTIVE_LOW status.
2941 *
2942 * This function should be called from contexts where we cannot sleep, and will
2943 * complain if the GPIO chip functions potentially sleep.
2944 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002945void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002946 struct gpio_desc **desc_array, int *value_array)
2947{
2948 if (!desc_array)
2949 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002950 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2951 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002952}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002953EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002954
2955/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002956 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002957 * @array_size: number of elements in the descriptor / value arrays
2958 * @desc_array: array of GPIO descriptors whose values will be assigned
2959 * @value_array: array of values to assign
2960 *
2961 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2962 * into account.
2963 *
2964 * This function should be called from contexts where we cannot sleep, and will
2965 * complain if the GPIO chip functions potentially sleep.
2966 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002967void gpiod_set_array_value(unsigned int array_size,
2968 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002969{
2970 if (!desc_array)
2971 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002972 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2973 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002974}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002975EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002976
2977/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002978 * gpiod_cansleep() - report whether gpio value access may sleep
2979 * @desc: gpio to check
2980 *
2981 */
2982int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002983{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002984 VALIDATE_DESC(desc);
2985 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002986}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002987EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002988
David Brownell0f6d5042008-10-15 22:03:14 -07002989/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002990 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2991 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002992 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002993 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2994 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002995 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002996int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002997{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002998 struct gpio_chip *chip;
2999 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07003000
Linus Walleij79bb71b2016-06-15 22:57:38 +02003001 /*
3002 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3003 * requires this function to not return zero on an invalid descriptor
3004 * but rather a negative error number.
3005 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003006 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003007 return -EINVAL;
3008
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003009 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003010 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003011 if (chip->to_irq) {
3012 int retirq = chip->to_irq(chip, offset);
3013
3014 /* Zero means NO_IRQ */
3015 if (!retirq)
3016 return -ENXIO;
3017
3018 return retirq;
3019 }
3020 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003021}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003022EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003023
Linus Walleijd468bf92013-09-24 11:54:38 +02003024/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003025 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003026 * @chip: the chip the GPIO to lock belongs to
3027 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003028 *
3029 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003030 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003031 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003032int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003033{
Linus Walleij9c102802016-05-25 10:56:03 +02003034 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003035
Linus Walleij9c102802016-05-25 10:56:03 +02003036 desc = gpiochip_get_desc(chip, offset);
3037 if (IS_ERR(desc))
3038 return PTR_ERR(desc);
3039
Linus Walleij60f83392016-11-12 15:01:09 +01003040 /*
3041 * If it's fast: flush the direction setting if something changed
3042 * behind our back
3043 */
3044 if (!chip->can_sleep && chip->get_direction) {
Linus Walleij9c102802016-05-25 10:56:03 +02003045 int dir = chip->get_direction(chip, offset);
3046
3047 if (dir)
3048 clear_bit(FLAG_IS_OUT, &desc->flags);
3049 else
3050 set_bit(FLAG_IS_OUT, &desc->flags);
3051 }
3052
3053 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003054 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02003055 "%s: tried to flag a GPIO set as output for IRQ\n",
3056 __func__);
3057 return -EIO;
3058 }
3059
Linus Walleij9c102802016-05-25 10:56:03 +02003060 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003061
3062 /*
3063 * If the consumer has not set up a label (such as when the
3064 * IRQ is referenced from .to_irq()) we set up a label here
3065 * so it is clear this is used as an interrupt.
3066 */
3067 if (!desc->label)
3068 desc_set_label(desc, "interrupt");
3069
Linus Walleijd468bf92013-09-24 11:54:38 +02003070 return 0;
3071}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003072EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003073
Linus Walleijd468bf92013-09-24 11:54:38 +02003074/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003075 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003076 * @chip: the chip the GPIO to lock belongs to
3077 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003078 *
3079 * This is used directly by GPIO drivers that want to indicate
3080 * that a certain GPIO is no longer used exclusively for IRQ.
3081 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003082void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003083{
Linus Walleij3940c342016-11-14 00:09:07 +01003084 struct gpio_desc *desc;
3085
3086 desc = gpiochip_get_desc(chip, offset);
3087 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003088 return;
3089
Linus Walleij3940c342016-11-14 00:09:07 +01003090 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3091
3092 /* If we only had this marking, erase it */
3093 if (desc->label && !strcmp(desc->label, "interrupt"))
3094 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003095}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003096EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003097
Linus Walleij6cee3822016-02-11 20:16:45 +01003098bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3099{
3100 if (offset >= chip->ngpio)
3101 return false;
3102
3103 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3104}
3105EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3106
Linus Walleij143b65d2016-02-16 15:41:42 +01003107bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3108{
3109 if (offset >= chip->ngpio)
3110 return false;
3111
3112 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3113}
3114EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3115
3116bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3117{
3118 if (offset >= chip->ngpio)
3119 return false;
3120
3121 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3122}
3123EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3124
Charles Keepax05f479b2017-05-23 15:47:29 +01003125bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3126{
3127 if (offset >= chip->ngpio)
3128 return false;
3129
Andrew Jeffery2cbfca62017-10-20 13:27:58 +10303130 return !test_bit(FLAG_SLEEP_MAY_LOSE_VALUE,
Charles Keepax05f479b2017-05-23 15:47:29 +01003131 &chip->gpiodev->descs[offset].flags);
3132}
3133EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3134
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003135/**
3136 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3137 * @desc: gpio whose value will be returned
3138 *
3139 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003140 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003141 *
3142 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003143 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003144int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003145{
David Brownelld2876d02008-02-04 22:28:20 -08003146 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003147 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003148 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08003149}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003150EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003151
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003152/**
3153 * gpiod_get_value_cansleep() - return a gpio's value
3154 * @desc: gpio whose value will be returned
3155 *
3156 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003157 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003158 *
3159 * This function is to be called from contexts that can sleep.
3160 */
3161int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003162{
David Brownelld2876d02008-02-04 22:28:20 -08003163 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003164
3165 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003166 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003167 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003168 if (value < 0)
3169 return value;
3170
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003171 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3172 value = !value;
3173
David Brownelld2876d02008-02-04 22:28:20 -08003174 return value;
3175}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003176EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003177
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003178/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02003179 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3180 * @array_size: number of elements in the descriptor / value arrays
3181 * @desc_array: array of GPIO descriptors whose values will be read
3182 * @value_array: array to store the read values
3183 *
3184 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3185 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3186 * else an error code.
3187 *
3188 * This function is to be called from contexts that can sleep.
3189 */
3190int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3191 struct gpio_desc **desc_array,
3192 int *value_array)
3193{
3194 might_sleep_if(extra_checks);
3195 if (!desc_array)
3196 return -EINVAL;
3197 return gpiod_get_array_value_complex(true, true, array_size,
3198 desc_array, value_array);
3199}
3200EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3201
3202/**
3203 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3204 * @array_size: number of elements in the descriptor / value arrays
3205 * @desc_array: array of GPIO descriptors whose values will be read
3206 * @value_array: array to store the read values
3207 *
3208 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3209 * into account. Return 0 in case of success, else an error code.
3210 *
3211 * This function is to be called from contexts that can sleep.
3212 */
3213int gpiod_get_array_value_cansleep(unsigned int array_size,
3214 struct gpio_desc **desc_array,
3215 int *value_array)
3216{
3217 might_sleep_if(extra_checks);
3218 if (!desc_array)
3219 return -EINVAL;
3220 return gpiod_get_array_value_complex(false, true, array_size,
3221 desc_array, value_array);
3222}
3223EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3224
3225/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003226 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3227 * @desc: gpio whose value will be assigned
3228 * @value: value to assign
3229 *
3230 * Set the raw value of the GPIO, i.e. the value of its physical line without
3231 * regard for its ACTIVE_LOW status.
3232 *
3233 * This function is to be called from contexts that can sleep.
3234 */
3235void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003236{
David Brownelld2876d02008-02-04 22:28:20 -08003237 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003238 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003239 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003240}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003241EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003242
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003243/**
3244 * gpiod_set_value_cansleep() - assign a gpio's value
3245 * @desc: gpio whose value will be assigned
3246 * @value: value to assign
3247 *
3248 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3249 * account
3250 *
3251 * This function is to be called from contexts that can sleep.
3252 */
3253void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003254{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003255 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003256 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003257 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3258 value = !value;
Linus Walleijfac9d882017-09-26 20:58:28 +02003259 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003260}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003261EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003262
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003263/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003264 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003265 * @array_size: number of elements in the descriptor / value arrays
3266 * @desc_array: array of GPIO descriptors whose values will be assigned
3267 * @value_array: array of values to assign
3268 *
3269 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3270 * without regard for their ACTIVE_LOW status.
3271 *
3272 * This function is to be called from contexts that can sleep.
3273 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003274void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3275 struct gpio_desc **desc_array,
3276 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003277{
3278 might_sleep_if(extra_checks);
3279 if (!desc_array)
3280 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003281 gpiod_set_array_value_complex(true, true, array_size, desc_array,
3282 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003283}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003284EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003285
3286/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003287 * gpiod_add_lookup_tables() - register GPIO device consumers
3288 * @tables: list of tables of consumers to register
3289 * @n: number of tables in the list
3290 */
3291void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3292{
3293 unsigned int i;
3294
3295 mutex_lock(&gpio_lookup_lock);
3296
3297 for (i = 0; i < n; i++)
3298 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3299
3300 mutex_unlock(&gpio_lookup_lock);
3301}
3302
3303/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003304 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003305 * @array_size: number of elements in the descriptor / value arrays
3306 * @desc_array: array of GPIO descriptors whose values will be assigned
3307 * @value_array: array of values to assign
3308 *
3309 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3310 * into account.
3311 *
3312 * This function is to be called from contexts that can sleep.
3313 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003314void gpiod_set_array_value_cansleep(unsigned int array_size,
3315 struct gpio_desc **desc_array,
3316 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003317{
3318 might_sleep_if(extra_checks);
3319 if (!desc_array)
3320 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003321 gpiod_set_array_value_complex(false, true, array_size, desc_array,
3322 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003323}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003324EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003325
3326/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003327 * gpiod_add_lookup_table() - register GPIO device consumers
3328 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003329 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003330void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003331{
3332 mutex_lock(&gpio_lookup_lock);
3333
Alexandre Courbotad824782013-12-03 12:20:11 +09003334 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003335
3336 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003337}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003338EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003339
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303340/**
3341 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3342 * @table: table of consumers to unregister
3343 */
3344void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3345{
3346 mutex_lock(&gpio_lookup_lock);
3347
3348 list_del(&table->list);
3349
3350 mutex_unlock(&gpio_lookup_lock);
3351}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003352EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303353
Alexandre Courbotad824782013-12-03 12:20:11 +09003354static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3355{
3356 const char *dev_id = dev ? dev_name(dev) : NULL;
3357 struct gpiod_lookup_table *table;
3358
3359 mutex_lock(&gpio_lookup_lock);
3360
3361 list_for_each_entry(table, &gpio_lookup_list, list) {
3362 if (table->dev_id && dev_id) {
3363 /*
3364 * Valid strings on both ends, must be identical to have
3365 * a match
3366 */
3367 if (!strcmp(table->dev_id, dev_id))
3368 goto found;
3369 } else {
3370 /*
3371 * One of the pointers is NULL, so both must be to have
3372 * a match
3373 */
3374 if (dev_id == table->dev_id)
3375 goto found;
3376 }
3377 }
3378 table = NULL;
3379
3380found:
3381 mutex_unlock(&gpio_lookup_lock);
3382 return table;
3383}
3384
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003385static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003386 unsigned int idx,
3387 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003388{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003389 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003390 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003391 struct gpiod_lookup *p;
3392
Alexandre Courbotad824782013-12-03 12:20:11 +09003393 table = gpiod_find_lookup_table(dev);
3394 if (!table)
3395 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003396
Alexandre Courbotad824782013-12-03 12:20:11 +09003397 for (p = &table->table[0]; p->chip_label; p++) {
3398 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003399
Alexandre Courbotad824782013-12-03 12:20:11 +09003400 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003401 if (p->idx != idx)
3402 continue;
3403
Alexandre Courbotad824782013-12-03 12:20:11 +09003404 /* If the lookup entry has a con_id, require exact match */
3405 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3406 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003407
Alexandre Courbotad824782013-12-03 12:20:11 +09003408 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003409
Alexandre Courbotad824782013-12-03 12:20:11 +09003410 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003411 dev_err(dev, "cannot find GPIO chip %s\n",
3412 p->chip_label);
3413 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003414 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003415
Alexandre Courbotad824782013-12-03 12:20:11 +09003416 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003417 dev_err(dev,
3418 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3419 idx, chip->ngpio, chip->label);
3420 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003421 }
3422
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003423 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003424 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003425
3426 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003427 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003428
3429 return desc;
3430}
3431
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003432static int dt_gpio_count(struct device *dev, const char *con_id)
3433{
3434 int ret;
3435 char propname[32];
3436 unsigned int i;
3437
3438 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3439 if (con_id)
3440 snprintf(propname, sizeof(propname), "%s-%s",
3441 con_id, gpio_suffixes[i]);
3442 else
3443 snprintf(propname, sizeof(propname), "%s",
3444 gpio_suffixes[i]);
3445
3446 ret = of_gpio_named_count(dev->of_node, propname);
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003447 if (ret > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003448 break;
3449 }
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003450 return ret ? ret : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003451}
3452
3453static int platform_gpio_count(struct device *dev, const char *con_id)
3454{
3455 struct gpiod_lookup_table *table;
3456 struct gpiod_lookup *p;
3457 unsigned int count = 0;
3458
3459 table = gpiod_find_lookup_table(dev);
3460 if (!table)
3461 return -ENOENT;
3462
3463 for (p = &table->table[0]; p->chip_label; p++) {
3464 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3465 (!con_id && !p->con_id))
3466 count++;
3467 }
3468 if (!count)
3469 return -ENOENT;
3470
3471 return count;
3472}
3473
3474/**
3475 * gpiod_count - return the number of GPIOs associated with a device / function
3476 * or -ENOENT if no GPIO has been assigned to the requested function
3477 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3478 * @con_id: function within the GPIO consumer
3479 */
3480int gpiod_count(struct device *dev, const char *con_id)
3481{
3482 int count = -ENOENT;
3483
3484 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3485 count = dt_gpio_count(dev, con_id);
3486 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3487 count = acpi_gpio_count(dev, con_id);
3488
3489 if (count < 0)
3490 count = platform_gpio_count(dev, con_id);
3491
3492 return count;
3493}
3494EXPORT_SYMBOL_GPL(gpiod_count);
3495
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003496/**
Thierry Reding08791622014-04-25 16:54:22 +02003497 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003498 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003499 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003500 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003501 *
3502 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003503 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003504 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003505 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003506struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003507 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003508{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003509 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003510}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003511EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003512
3513/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003514 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3515 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3516 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003517 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003518 *
3519 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3520 * the requested function it will return NULL. This is convenient for drivers
3521 * that need to handle optional GPIOs.
3522 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003523struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003524 const char *con_id,
3525 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003526{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003527 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003528}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003529EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003530
Benoit Parrotf625d462015-02-02 11:44:44 -06003531
3532/**
3533 * gpiod_configure_flags - helper function to configure a given GPIO
3534 * @desc: gpio whose value will be assigned
3535 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003536 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3537 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003538 * @dflags: gpiod_flags - optional GPIO initialization flags
3539 *
3540 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3541 * requested function and/or index, or another IS_ERR() code if an error
3542 * occurred while trying to acquire the GPIO.
3543 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03003544int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003545 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003546{
3547 int status;
3548
Johan Hovold85b03b32016-07-03 18:32:05 +02003549 if (lflags & GPIO_ACTIVE_LOW)
3550 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3551 if (lflags & GPIO_OPEN_DRAIN)
3552 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3553 if (lflags & GPIO_OPEN_SOURCE)
3554 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jeffery2cbfca62017-10-20 13:27:58 +10303555 if (lflags & GPIO_SLEEP_MAY_LOSE_VALUE)
3556 set_bit(FLAG_SLEEP_MAY_LOSE_VALUE, &desc->flags);
Johan Hovold85b03b32016-07-03 18:32:05 +02003557
Benoit Parrotf625d462015-02-02 11:44:44 -06003558 /* No particular flag request, return here... */
3559 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3560 pr_debug("no flags found for %s\n", con_id);
3561 return 0;
3562 }
3563
3564 /* Process flags */
3565 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3566 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01003567 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06003568 else
3569 status = gpiod_direction_input(desc);
3570
3571 return status;
3572}
3573
Thierry Reding29a1f2332014-04-25 17:10:06 +02003574/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003575 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003576 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003577 * @con_id: function within the GPIO consumer
3578 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003579 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003580 *
3581 * This variant of gpiod_get() allows to access GPIOs other than the first
3582 * defined one for functions that define several GPIOs.
3583 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003584 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3585 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003586 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003587 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003588struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003589 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003590 unsigned int idx,
3591 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003592{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003593 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003594 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003595 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003596
3597 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3598
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003599 if (dev) {
3600 /* Using device tree? */
3601 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3602 dev_dbg(dev, "using device tree for GPIO lookup\n");
3603 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3604 } else if (ACPI_COMPANION(dev)) {
3605 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003606 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003607 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003608 }
3609
3610 /*
3611 * Either we are not using DT or ACPI, or their lookup did not return
3612 * a result. In that case, use platform lookup as a fallback.
3613 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003614 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003615 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003616 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003617 }
3618
3619 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003620 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003621 return desc;
3622 }
3623
3624 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003625 if (status < 0)
3626 return ERR_PTR(status);
3627
Johan Hovold85b03b32016-07-03 18:32:05 +02003628 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003629 if (status < 0) {
3630 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3631 gpiod_put(desc);
3632 return ERR_PTR(status);
3633 }
3634
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003635 return desc;
3636}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003637EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003638
3639/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003640 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3641 * @fwnode: handle of the firmware node
3642 * @propname: name of the firmware property representing the GPIO
Boris Brezillon537b94d2017-02-02 14:53:11 +01003643 * @index: index of the GPIO to obtain in the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003644 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02003645 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02003646 *
3647 * This function can be used for drivers that get their configuration
3648 * from firmware.
3649 *
3650 * Function properly finds the corresponding GPIO using whatever is the
3651 * underlying firmware interface and then makes sure that the GPIO
3652 * descriptor is requested before it is returned to the caller.
3653 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003654 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02003655 * On successful request the GPIO pin is configured in accordance with
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003656 * provided @dflags.
3657 *
Mika Westerberg40b73182014-10-21 13:33:59 +02003658 * In case of error an ERR_PTR() is returned.
3659 */
3660struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Boris Brezillon537b94d2017-02-02 14:53:11 +01003661 const char *propname, int index,
Alexander Steinb2987d72017-01-12 17:39:24 +01003662 enum gpiod_flags dflags,
3663 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02003664{
3665 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003666 unsigned long lflags = 0;
Mika Westerberg40b73182014-10-21 13:33:59 +02003667 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003668 bool single_ended = false;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303669 bool open_drain = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003670 int ret;
3671
3672 if (!fwnode)
3673 return ERR_PTR(-EINVAL);
3674
3675 if (is_of_node(fwnode)) {
3676 enum of_gpio_flags flags;
3677
Boris Brezillon537b94d2017-02-02 14:53:11 +01003678 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname,
3679 index, &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003680 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003681 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003682 single_ended = flags & OF_GPIO_SINGLE_ENDED;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303683 open_drain = flags & OF_GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003684 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003685 } else if (is_acpi_node(fwnode)) {
3686 struct acpi_gpio_info info;
3687
Boris Brezillon537b94d2017-02-02 14:53:11 +01003688 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003689 if (!IS_ERR(desc)) {
Christophe RICARD52044722015-12-23 23:25:34 +01003690 active_low = info.polarity == GPIO_ACTIVE_LOW;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003691 ret = acpi_gpio_update_gpiod_flags(&dflags, info.flags);
3692 if (ret)
3693 pr_debug("Override GPIO initialization flags\n");
3694 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003695 }
3696
3697 if (IS_ERR(desc))
3698 return desc;
3699
Alexander Steinb2987d72017-01-12 17:39:24 +01003700 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02003701 if (ret)
3702 return ERR_PTR(ret);
3703
Mika Westerberg40b73182014-10-21 13:33:59 +02003704 if (active_low)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003705 lflags |= GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003706
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003707 if (single_ended) {
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303708 if (open_drain)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003709 lflags |= GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003710 else
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003711 lflags |= GPIO_OPEN_SOURCE;
3712 }
3713
3714 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3715 if (ret < 0) {
3716 gpiod_put(desc);
3717 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003718 }
3719
Mika Westerberg40b73182014-10-21 13:33:59 +02003720 return desc;
3721}
3722EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3723
3724/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003725 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3726 * function
3727 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3728 * @con_id: function within the GPIO consumer
3729 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003730 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003731 *
3732 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3733 * specified index was assigned to the requested function it will return NULL.
3734 * This is convenient for drivers that need to handle optional GPIOs.
3735 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003736struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003737 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003738 unsigned int index,
3739 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003740{
3741 struct gpio_desc *desc;
3742
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003743 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003744 if (IS_ERR(desc)) {
3745 if (PTR_ERR(desc) == -ENOENT)
3746 return NULL;
3747 }
3748
3749 return desc;
3750}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003751EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003752
3753/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003754 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3755 * @desc: gpio whose value will be assigned
3756 * @name: gpio line name
3757 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3758 * of_get_gpio_hog()
3759 * @dflags: gpiod_flags - optional GPIO initialization flags
3760 */
3761int gpiod_hog(struct gpio_desc *desc, const char *name,
3762 unsigned long lflags, enum gpiod_flags dflags)
3763{
3764 struct gpio_chip *chip;
3765 struct gpio_desc *local_desc;
3766 int hwnum;
3767 int status;
3768
3769 chip = gpiod_to_chip(desc);
3770 hwnum = gpio_chip_hwgpio(desc);
3771
3772 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3773 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303774 status = PTR_ERR(local_desc);
3775 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3776 name, chip->label, hwnum, status);
3777 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003778 }
3779
Johan Hovold85b03b32016-07-03 18:32:05 +02003780 status = gpiod_configure_flags(desc, name, lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003781 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303782 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3783 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003784 gpiochip_free_own_desc(desc);
3785 return status;
3786 }
3787
3788 /* Mark GPIO as hogged so it can be identified and removed later */
3789 set_bit(FLAG_IS_HOGGED, &desc->flags);
3790
3791 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3792 desc_to_gpio(desc), name,
3793 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3794 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3795 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3796
3797 return 0;
3798}
3799
3800/**
3801 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3802 * @chip: gpio chip to act on
3803 *
3804 * This is only used by of_gpiochip_remove to free hogged gpios
3805 */
3806static void gpiochip_free_hogs(struct gpio_chip *chip)
3807{
3808 int id;
3809
3810 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003811 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3812 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003813 }
3814}
3815
3816/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003817 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3818 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3819 * @con_id: function within the GPIO consumer
3820 * @flags: optional GPIO initialization flags
3821 *
3822 * This function acquires all the GPIOs defined under a given function.
3823 *
3824 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3825 * no GPIO has been assigned to the requested function, or another IS_ERR()
3826 * code if an error occurred while trying to acquire the GPIOs.
3827 */
3828struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3829 const char *con_id,
3830 enum gpiod_flags flags)
3831{
3832 struct gpio_desc *desc;
3833 struct gpio_descs *descs;
3834 int count;
3835
3836 count = gpiod_count(dev, con_id);
3837 if (count < 0)
3838 return ERR_PTR(count);
3839
3840 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3841 GFP_KERNEL);
3842 if (!descs)
3843 return ERR_PTR(-ENOMEM);
3844
3845 for (descs->ndescs = 0; descs->ndescs < count; ) {
3846 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3847 if (IS_ERR(desc)) {
3848 gpiod_put_array(descs);
3849 return ERR_CAST(desc);
3850 }
3851 descs->desc[descs->ndescs] = desc;
3852 descs->ndescs++;
3853 }
3854 return descs;
3855}
3856EXPORT_SYMBOL_GPL(gpiod_get_array);
3857
3858/**
3859 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3860 * function
3861 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3862 * @con_id: function within the GPIO consumer
3863 * @flags: optional GPIO initialization flags
3864 *
3865 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3866 * assigned to the requested function it will return NULL.
3867 */
3868struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3869 const char *con_id,
3870 enum gpiod_flags flags)
3871{
3872 struct gpio_descs *descs;
3873
3874 descs = gpiod_get_array(dev, con_id, flags);
3875 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3876 return NULL;
3877
3878 return descs;
3879}
3880EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3881
3882/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003883 * gpiod_put - dispose of a GPIO descriptor
3884 * @desc: GPIO descriptor to dispose of
3885 *
3886 * No descriptor can be used after gpiod_put() has been called on it.
3887 */
3888void gpiod_put(struct gpio_desc *desc)
3889{
3890 gpiod_free(desc);
3891}
3892EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003893
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003894/**
3895 * gpiod_put_array - dispose of multiple GPIO descriptors
3896 * @descs: struct gpio_descs containing an array of descriptors
3897 */
3898void gpiod_put_array(struct gpio_descs *descs)
3899{
3900 unsigned int i;
3901
3902 for (i = 0; i < descs->ndescs; i++)
3903 gpiod_put(descs->desc[i]);
3904
3905 kfree(descs);
3906}
3907EXPORT_SYMBOL_GPL(gpiod_put_array);
3908
Linus Walleij3c702e92015-10-21 15:29:53 +02003909static int __init gpiolib_dev_init(void)
3910{
3911 int ret;
3912
3913 /* Register GPIO sysfs bus */
3914 ret = bus_register(&gpio_bus_type);
3915 if (ret < 0) {
3916 pr_err("gpiolib: could not register GPIO bus type\n");
3917 return ret;
3918 }
3919
3920 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3921 if (ret < 0) {
3922 pr_err("gpiolib: failed to allocate char dev region\n");
3923 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003924 } else {
3925 gpiolib_initialized = true;
3926 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003927 }
3928 return ret;
3929}
3930core_initcall(gpiolib_dev_init);
3931
David Brownelld2876d02008-02-04 22:28:20 -08003932#ifdef CONFIG_DEBUG_FS
3933
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003934static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003935{
3936 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003937 struct gpio_chip *chip = gdev->chip;
3938 unsigned gpio = gdev->base;
3939 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003940 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003941 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003942
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003943 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003944 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3945 if (gdesc->name) {
3946 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3947 gpio, gdesc->name);
3948 }
David Brownelld2876d02008-02-04 22:28:20 -08003949 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003950 }
David Brownelld2876d02008-02-04 22:28:20 -08003951
Alexandre Courbot372e7222013-02-03 01:29:29 +09003952 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003953 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003954 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003955 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3956 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003957 is_out ? "out" : "in ",
3958 chip->get
3959 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003960 : "? ",
3961 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003962 seq_printf(s, "\n");
3963 }
3964}
3965
Thierry Redingf9c4a312012-04-12 13:26:01 +02003966static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003967{
Grant Likely362432a2013-02-09 09:41:49 +00003968 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003969 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003970 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003971
3972 s->private = "";
3973
Grant Likely362432a2013-02-09 09:41:49 +00003974 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003975 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003976 if (index-- == 0) {
3977 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003978 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003979 }
3980 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003981
3982 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003983}
3984
3985static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3986{
Grant Likely362432a2013-02-09 09:41:49 +00003987 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003988 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003989 void *ret = NULL;
3990
Grant Likely362432a2013-02-09 09:41:49 +00003991 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003992 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003993 ret = NULL;
3994 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003995 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003996 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003997
3998 s->private = "\n";
3999 ++*pos;
4000
4001 return ret;
4002}
4003
4004static void gpiolib_seq_stop(struct seq_file *s, void *v)
4005{
4006}
4007
4008static int gpiolib_seq_show(struct seq_file *s, void *v)
4009{
Linus Walleijff2b1352015-10-20 11:10:38 +02004010 struct gpio_device *gdev = v;
4011 struct gpio_chip *chip = gdev->chip;
4012 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004013
Linus Walleijff2b1352015-10-20 11:10:38 +02004014 if (!chip) {
4015 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4016 dev_name(&gdev->dev));
4017 return 0;
4018 }
4019
4020 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4021 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004022 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02004023 parent = chip->parent;
4024 if (parent)
4025 seq_printf(s, ", parent: %s/%s",
4026 parent->bus ? parent->bus->name : "no-bus",
4027 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02004028 if (chip->label)
4029 seq_printf(s, ", %s", chip->label);
4030 if (chip->can_sleep)
4031 seq_printf(s, ", can sleep");
4032 seq_printf(s, ":\n");
4033
4034 if (chip->dbg_show)
4035 chip->dbg_show(s, chip);
4036 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004037 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02004038
David Brownelld2876d02008-02-04 22:28:20 -08004039 return 0;
4040}
4041
Thierry Redingf9c4a312012-04-12 13:26:01 +02004042static const struct seq_operations gpiolib_seq_ops = {
4043 .start = gpiolib_seq_start,
4044 .next = gpiolib_seq_next,
4045 .stop = gpiolib_seq_stop,
4046 .show = gpiolib_seq_show,
4047};
4048
David Brownelld2876d02008-02-04 22:28:20 -08004049static int gpiolib_open(struct inode *inode, struct file *file)
4050{
Thierry Redingf9c4a312012-04-12 13:26:01 +02004051 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08004052}
4053
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004054static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02004055 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08004056 .open = gpiolib_open,
4057 .read = seq_read,
4058 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02004059 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08004060};
4061
4062static int __init gpiolib_debugfs_init(void)
4063{
4064 /* /sys/kernel/debug/gpio */
4065 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4066 NULL, NULL, &gpiolib_operations);
4067 return 0;
4068}
4069subsys_initcall(gpiolib_debugfs_init);
4070
4071#endif /* DEBUG_FS */