blob: aa0b4b46fccc1203037e121fe26e63233c334b5e [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
Laura Abbott30277432018-05-21 10:57:07 -070064/*
65 * Number of GPIOs to use for the fast path in set array
66 */
67#define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
68
David Brownelld2876d02008-02-04 22:28:20 -080069/* gpio_lock prevents conflicts during gpio_desc[] table updates.
70 * While any GPIO is requested, its gpio_chip is not removable;
71 * each GPIO's "requested" flag serves as a lock and refcount.
72 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090073DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080074
Alexandre Courbotbae48da2013-10-17 10:21:38 -070075static DEFINE_MUTEX(gpio_lookup_lock);
76static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020077LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020078
Bartosz Golaszewskia411e812018-04-10 22:30:28 +020079static DEFINE_MUTEX(gpio_machine_hogs_mutex);
80static LIST_HEAD(gpio_machine_hogs);
81
Johan Hovold6d867502015-05-04 17:23:25 +020082static void gpiochip_free_hogs(struct gpio_chip *chip);
Thierry Reding959bc7b2017-11-07 19:15:59 +010083static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +010084 struct lock_class_key *lock_key,
85 struct lock_class_key *request_key);
Johan Hovold6d867502015-05-04 17:23:25 +020086static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030087static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
88static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020089
Guenter Roeck159f3cd2016-03-31 08:11:30 -070090static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020091
David Brownelld2876d02008-02-04 22:28:20 -080092static inline void desc_set_label(struct gpio_desc *d, const char *label)
93{
David Brownelld2876d02008-02-04 22:28:20 -080094 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080095}
96
Alexandre Courbot372e7222013-02-03 01:29:29 +090097/**
Thierry Reding950d55f52017-07-24 16:57:22 +020098 * gpio_to_desc - Convert a GPIO number to its descriptor
99 * @gpio: global GPIO number
100 *
101 * Returns:
102 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
103 * with the given number exists in the system.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900104 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700105struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900106{
Linus Walleijff2b1352015-10-20 11:10:38 +0200107 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900108 unsigned long flags;
109
110 spin_lock_irqsave(&gpio_lock, flags);
111
Linus Walleijff2b1352015-10-20 11:10:38 +0200112 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100113 if (gdev->base <= gpio &&
114 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900115 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100116 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900117 }
118 }
119
120 spin_unlock_irqrestore(&gpio_lock, flags);
121
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900122 if (!gpio_is_valid(gpio))
123 WARN(1, "invalid GPIO %d\n", gpio);
124
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900125 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900126}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700127EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900128
129/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200130 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
131 * hardware number for this chip
132 * @chip: GPIO chip
133 * @hwnum: hardware number of the GPIO for this chip
134 *
135 * Returns:
136 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
137 * in the given chip for the specified hardware number.
Linus Walleijd468bf92013-09-24 11:54:38 +0200138 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900139struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
140 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200141{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100142 struct gpio_device *gdev = chip->gpiodev;
143
144 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900145 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200146
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100147 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200148}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900149
150/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200151 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
152 * @desc: GPIO descriptor
153 *
Alexandre Courbot372e7222013-02-03 01:29:29 +0900154 * This should disappear in the future but is needed since we still
Thierry Reding950d55f52017-07-24 16:57:22 +0200155 * use GPIO numbers for error messages and sysfs nodes.
156 *
157 * Returns:
158 * The global GPIO number for the GPIO specified by its descriptor.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900159 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700160int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900161{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100162 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900163}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700164EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900165
166
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700167/**
168 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
169 * @desc: descriptor to return the chip of
170 */
171struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900172{
Vladimir Zapolskiydd3b9a42017-12-21 18:37:30 +0200173 if (!desc || !desc->gdev)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100174 return NULL;
175 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900176}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700177EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800178
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700179/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
180static int gpiochip_find_base(int ngpio)
181{
Linus Walleijff2b1352015-10-20 11:10:38 +0200182 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900183 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700184
Linus Walleijff2b1352015-10-20 11:10:38 +0200185 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900186 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100187 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900188 break;
189 else
190 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100191 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700192 }
193
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900194 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700195 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900196 return base;
197 } else {
198 pr_err("%s: cannot find free range\n", __func__);
199 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700200 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700201}
202
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700203/**
204 * gpiod_get_direction - return the current direction of a GPIO
205 * @desc: GPIO to get the direction of
206 *
Wolfram Sang94fc7302018-01-09 12:35:53 +0100207 * Returns 0 for output, 1 for input, or an error code in case of error.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700208 *
209 * This function may sleep if gpiod_cansleep() is true.
210 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900211int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300212{
Wolfram Sangd0121b82018-07-11 18:33:19 +0200213 struct gpio_chip *chip;
214 unsigned offset;
215 int status;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300216
Alexandre Courbot372e7222013-02-03 01:29:29 +0900217 chip = gpiod_to_chip(desc);
218 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300219
220 if (!chip->get_direction)
Wolfram Sangd0121b82018-07-11 18:33:19 +0200221 return -ENOTSUPP;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300222
Alexandre Courbot372e7222013-02-03 01:29:29 +0900223 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300224 if (status > 0) {
225 /* GPIOF_DIR_IN, or other positive */
226 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900227 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300228 }
229 if (status == 0) {
230 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900231 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300232 }
233 return status;
234}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700235EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300236
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900237/*
238 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800239 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900240 *
241 * Return -EBUSY if the new chip overlaps with some other chip's integer
242 * space.
243 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200244static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900245{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800246 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200247
248 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800249 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200250 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530251 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900252 }
253
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800254 next = list_entry(gpio_devices.next, struct gpio_device, list);
255 if (gdev->base + gdev->ngpio <= next->base) {
256 /* add before first entry */
257 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500258 return 0;
259 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900260
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800261 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
262 if (prev->base + prev->ngpio <= gdev->base) {
263 /* add behind last entry */
264 list_add_tail(&gdev->list, &gpio_devices);
265 return 0;
266 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800267
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800268 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
269 /* at the end of the list */
270 if (&next->list == &gpio_devices)
271 break;
272
273 /* add between prev and next */
274 if (prev->base + prev->ngpio <= gdev->base
275 && gdev->base + gdev->ngpio <= next->base) {
276 list_add(&gdev->list, &prev->list);
277 return 0;
278 }
279 }
280
281 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
282 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900283}
284
Thierry Reding950d55f52017-07-24 16:57:22 +0200285/*
Linus Walleijf881bab02015-09-23 16:20:43 -0700286 * Convert a GPIO name to its descriptor
287 */
288static struct gpio_desc *gpio_name_to_desc(const char * const name)
289{
Linus Walleijff2b1352015-10-20 11:10:38 +0200290 struct gpio_device *gdev;
Linus Walleijf881bab02015-09-23 16:20:43 -0700291 unsigned long flags;
292
293 spin_lock_irqsave(&gpio_lock, flags);
294
Linus Walleijff2b1352015-10-20 11:10:38 +0200295 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700296 int i;
297
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100298 for (i = 0; i != gdev->ngpio; ++i) {
299 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab02015-09-23 16:20:43 -0700300
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100301 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700302 continue;
303
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100304 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700305 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100306 return desc;
Linus Walleijf881bab02015-09-23 16:20:43 -0700307 }
308 }
309 }
310
311 spin_unlock_irqrestore(&gpio_lock, flags);
312
313 return NULL;
314}
315
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200316/*
317 * Takes the names from gc->names and checks if they are all unique. If they
318 * are, they are assigned to their gpio descriptors.
319 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800320 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200321 */
322static int gpiochip_set_desc_names(struct gpio_chip *gc)
323{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100324 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200325 int i;
326
327 if (!gc->names)
328 return 0;
329
330 /* First check all names if they are unique */
331 for (i = 0; i != gc->ngpio; ++i) {
332 struct gpio_desc *gpio;
333
334 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab02015-09-23 16:20:43 -0700335 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100336 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200337 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab02015-09-23 16:20:43 -0700338 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200339 }
340
341 /* Then add all names to the GPIO descriptors */
342 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100343 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200344
345 return 0;
346}
347
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700348static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
349{
350 unsigned long *p;
351
Stephen Boydace569352018-03-23 09:34:51 -0700352 p = kmalloc_array(BITS_TO_LONGS(chip->ngpio), sizeof(*p), GFP_KERNEL);
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700353 if (!p)
354 return NULL;
355
356 /* Assume by default all GPIOs are valid */
357 bitmap_fill(p, chip->ngpio);
358
359 return p;
360}
361
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700362static int gpiochip_init_valid_mask(struct gpio_chip *gpiochip)
363{
364#ifdef CONFIG_OF_GPIO
365 int size;
366 struct device_node *np = gpiochip->of_node;
367
368 size = of_property_count_u32_elems(np, "gpio-reserved-ranges");
369 if (size > 0 && size % 2 == 0)
370 gpiochip->need_valid_mask = true;
371#endif
372
373 if (!gpiochip->need_valid_mask)
374 return 0;
375
376 gpiochip->valid_mask = gpiochip_allocate_mask(gpiochip);
377 if (!gpiochip->valid_mask)
378 return -ENOMEM;
379
380 return 0;
381}
382
383static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
384{
385 kfree(gpiochip->valid_mask);
386 gpiochip->valid_mask = NULL;
387}
388
389bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
390 unsigned int offset)
391{
392 /* No mask means all valid */
393 if (likely(!gpiochip->valid_mask))
394 return true;
395 return test_bit(offset, gpiochip->valid_mask);
396}
397EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
398
Linus Walleijd7c51b42016-04-26 10:35:29 +0200399/*
400 * GPIO line handle management
401 */
402
403/**
404 * struct linehandle_state - contains the state of a userspace handle
405 * @gdev: the GPIO device the handle pertains to
406 * @label: consumer label used to tag descriptors
407 * @descs: the GPIO descriptors held by this handle
408 * @numdescs: the number of descriptors held in the descs array
409 */
410struct linehandle_state {
411 struct gpio_device *gdev;
412 const char *label;
413 struct gpio_desc *descs[GPIOHANDLES_MAX];
414 u32 numdescs;
415};
416
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200417#define GPIOHANDLE_REQUEST_VALID_FLAGS \
418 (GPIOHANDLE_REQUEST_INPUT | \
419 GPIOHANDLE_REQUEST_OUTPUT | \
420 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
421 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
422 GPIOHANDLE_REQUEST_OPEN_SOURCE)
423
Linus Walleijd7c51b42016-04-26 10:35:29 +0200424static long linehandle_ioctl(struct file *filep, unsigned int cmd,
425 unsigned long arg)
426{
427 struct linehandle_state *lh = filep->private_data;
428 void __user *ip = (void __user *)arg;
429 struct gpiohandle_data ghd;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200430 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200431 int i;
432
433 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Bartosz Golaszewski2b955b32018-07-16 10:34:24 +0200434 /* NOTE: It's ok to read values of output lines. */
Lukas Wunnereec1d562017-10-12 12:40:10 +0200435 int ret = gpiod_get_array_value_complex(false,
436 true,
437 lh->numdescs,
438 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200439 NULL,
Lukas Wunnereec1d562017-10-12 12:40:10 +0200440 vals);
441 if (ret)
442 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200443
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200444 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200445 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200446 ghd.values[i] = test_bit(i, vals);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200447
448 if (copy_to_user(ip, &ghd, sizeof(ghd)))
449 return -EFAULT;
450
451 return 0;
452 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Bartosz Golaszewskie5332d52018-07-16 10:34:23 +0200453 /*
454 * All line descriptors were created at once with the same
455 * flags so just check if the first one is really output.
456 */
457 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
458 return -EPERM;
459
Linus Walleijd7c51b42016-04-26 10:35:29 +0200460 if (copy_from_user(&ghd, ip, sizeof(ghd)))
461 return -EFAULT;
462
463 /* Clamp all values to [0,1] */
464 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200465 __assign_bit(i, vals, ghd.values[i]);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200466
467 /* Reuse the array setting function */
Laura Abbott30277432018-05-21 10:57:07 -0700468 return gpiod_set_array_value_complex(false,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200469 true,
470 lh->numdescs,
471 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200472 NULL,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200473 vals);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200474 }
475 return -EINVAL;
476}
477
478#ifdef CONFIG_COMPAT
479static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
480 unsigned long arg)
481{
482 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
483}
484#endif
485
486static int linehandle_release(struct inode *inode, struct file *filep)
487{
488 struct linehandle_state *lh = filep->private_data;
489 struct gpio_device *gdev = lh->gdev;
490 int i;
491
492 for (i = 0; i < lh->numdescs; i++)
493 gpiod_free(lh->descs[i]);
494 kfree(lh->label);
495 kfree(lh);
496 put_device(&gdev->dev);
497 return 0;
498}
499
500static const struct file_operations linehandle_fileops = {
501 .release = linehandle_release,
502 .owner = THIS_MODULE,
503 .llseek = noop_llseek,
504 .unlocked_ioctl = linehandle_ioctl,
505#ifdef CONFIG_COMPAT
506 .compat_ioctl = linehandle_ioctl_compat,
507#endif
508};
509
510static int linehandle_create(struct gpio_device *gdev, void __user *ip)
511{
512 struct gpiohandle_request handlereq;
513 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200514 struct file *file;
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500515 int fd, i, count = 0, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200516 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200517
518 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
519 return -EFAULT;
520 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
521 return -EINVAL;
522
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200523 lflags = handlereq.flags;
524
525 /* Return an error if an unknown flag is set */
526 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
527 return -EINVAL;
528
Bartosz Golaszewski588fc3b2017-11-15 16:47:43 +0100529 /*
530 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
531 * the hardware actually supports enabling both at the same time the
532 * electrical result would be disastrous.
533 */
534 if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
535 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
536 return -EINVAL;
537
Bartosz Golaszewski609aaf62017-10-16 11:32:30 +0200538 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
539 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
540 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
541 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
542 return -EINVAL;
543
Linus Walleijd7c51b42016-04-26 10:35:29 +0200544 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
545 if (!lh)
546 return -ENOMEM;
547 lh->gdev = gdev;
548 get_device(&gdev->dev);
549
550 /* Make sure this is terminated */
551 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
552 if (strlen(handlereq.consumer_label)) {
553 lh->label = kstrdup(handlereq.consumer_label,
554 GFP_KERNEL);
555 if (!lh->label) {
556 ret = -ENOMEM;
557 goto out_free_lh;
558 }
559 }
560
561 /* Request each GPIO */
562 for (i = 0; i < handlereq.lines; i++) {
563 u32 offset = handlereq.lineoffsets[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200564 struct gpio_desc *desc;
565
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200566 if (offset >= gdev->ngpio) {
567 ret = -EINVAL;
568 goto out_free_descs;
569 }
570
Linus Walleijd7c51b42016-04-26 10:35:29 +0200571 desc = &gdev->descs[offset];
572 ret = gpiod_request(desc, lh->label);
573 if (ret)
574 goto out_free_descs;
575 lh->descs[i] = desc;
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500576 count = i;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200577
578 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
579 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
580 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
581 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
582 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
583 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
584
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030585 ret = gpiod_set_transitory(desc, false);
586 if (ret < 0)
587 goto out_free_descs;
588
Linus Walleijd7c51b42016-04-26 10:35:29 +0200589 /*
590 * Lines have to be requested explicitly for input
591 * or output, else the line will be treated "as is".
592 */
593 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
594 int val = !!handlereq.default_values[i];
595
596 ret = gpiod_direction_output(desc, val);
597 if (ret)
598 goto out_free_descs;
599 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
600 ret = gpiod_direction_input(desc);
601 if (ret)
602 goto out_free_descs;
603 }
604 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
605 offset);
606 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200607 /* Let i point at the last handle */
608 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200609 lh->numdescs = handlereq.lines;
610
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200611 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200612 if (fd < 0) {
613 ret = fd;
614 goto out_free_descs;
615 }
616
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200617 file = anon_inode_getfile("gpio-linehandle",
618 &linehandle_fileops,
619 lh,
620 O_RDONLY | O_CLOEXEC);
621 if (IS_ERR(file)) {
622 ret = PTR_ERR(file);
623 goto out_put_unused_fd;
624 }
625
Linus Walleijd7c51b42016-04-26 10:35:29 +0200626 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200627 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200628 /*
629 * fput() will trigger the release() callback, so do not go onto
630 * the regular error cleanup path here.
631 */
632 fput(file);
633 put_unused_fd(fd);
634 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200635 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200636
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200637 fd_install(fd, file);
638
Linus Walleijd7c51b42016-04-26 10:35:29 +0200639 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
640 lh->numdescs);
641
642 return 0;
643
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200644out_put_unused_fd:
645 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200646out_free_descs:
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500647 for (i = 0; i < count; i++)
Linus Walleijd7c51b42016-04-26 10:35:29 +0200648 gpiod_free(lh->descs[i]);
649 kfree(lh->label);
650out_free_lh:
651 kfree(lh);
652 put_device(&gdev->dev);
653 return ret;
654}
655
Linus Walleij61f922d2016-06-02 11:30:15 +0200656/*
657 * GPIO line event management
658 */
659
660/**
661 * struct lineevent_state - contains the state of a userspace event
662 * @gdev: the GPIO device the event pertains to
663 * @label: consumer label used to tag descriptors
664 * @desc: the GPIO descriptor held by this event
665 * @eflags: the event flags this line was requested with
666 * @irq: the interrupt that trigger in response to events on this GPIO
667 * @wait: wait queue that handles blocking reads of events
668 * @events: KFIFO for the GPIO events
669 * @read_lock: mutex lock to protect reads from colliding with adding
670 * new events to the FIFO
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100671 * @timestamp: cache for the timestamp storing it between hardirq
672 * and IRQ thread, used to bring the timestamp close to the actual
673 * event
Linus Walleij61f922d2016-06-02 11:30:15 +0200674 */
675struct lineevent_state {
676 struct gpio_device *gdev;
677 const char *label;
678 struct gpio_desc *desc;
679 u32 eflags;
680 int irq;
681 wait_queue_head_t wait;
682 DECLARE_KFIFO(events, struct gpioevent_data, 16);
683 struct mutex read_lock;
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100684 u64 timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200685};
686
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200687#define GPIOEVENT_REQUEST_VALID_FLAGS \
688 (GPIOEVENT_REQUEST_RISING_EDGE | \
689 GPIOEVENT_REQUEST_FALLING_EDGE)
690
Al Viroafc9a422017-07-03 06:39:46 -0400691static __poll_t lineevent_poll(struct file *filep,
Linus Walleij61f922d2016-06-02 11:30:15 +0200692 struct poll_table_struct *wait)
693{
694 struct lineevent_state *le = filep->private_data;
Al Viroafc9a422017-07-03 06:39:46 -0400695 __poll_t events = 0;
Linus Walleij61f922d2016-06-02 11:30:15 +0200696
697 poll_wait(filep, &le->wait, wait);
698
699 if (!kfifo_is_empty(&le->events))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800700 events = EPOLLIN | EPOLLRDNORM;
Linus Walleij61f922d2016-06-02 11:30:15 +0200701
702 return events;
703}
704
705
706static ssize_t lineevent_read(struct file *filep,
707 char __user *buf,
708 size_t count,
709 loff_t *f_ps)
710{
711 struct lineevent_state *le = filep->private_data;
712 unsigned int copied;
713 int ret;
714
715 if (count < sizeof(struct gpioevent_data))
716 return -EINVAL;
717
718 do {
719 if (kfifo_is_empty(&le->events)) {
720 if (filep->f_flags & O_NONBLOCK)
721 return -EAGAIN;
722
723 ret = wait_event_interruptible(le->wait,
724 !kfifo_is_empty(&le->events));
725 if (ret)
726 return ret;
727 }
728
729 if (mutex_lock_interruptible(&le->read_lock))
730 return -ERESTARTSYS;
731 ret = kfifo_to_user(&le->events, buf, count, &copied);
732 mutex_unlock(&le->read_lock);
733
734 if (ret)
735 return ret;
736
737 /*
738 * If we couldn't read anything from the fifo (a different
739 * thread might have been faster) we either return -EAGAIN if
740 * the file descriptor is non-blocking, otherwise we go back to
741 * sleep and wait for more data to arrive.
742 */
743 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
744 return -EAGAIN;
745
746 } while (copied == 0);
747
748 return copied;
749}
750
751static int lineevent_release(struct inode *inode, struct file *filep)
752{
753 struct lineevent_state *le = filep->private_data;
754 struct gpio_device *gdev = le->gdev;
755
756 free_irq(le->irq, le);
757 gpiod_free(le->desc);
758 kfree(le->label);
759 kfree(le);
760 put_device(&gdev->dev);
761 return 0;
762}
763
764static long lineevent_ioctl(struct file *filep, unsigned int cmd,
765 unsigned long arg)
766{
767 struct lineevent_state *le = filep->private_data;
768 void __user *ip = (void __user *)arg;
769 struct gpiohandle_data ghd;
770
771 /*
772 * We can get the value for an event line but not set it,
773 * because it is input by definition.
774 */
775 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
776 int val;
777
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200778 memset(&ghd, 0, sizeof(ghd));
779
Linus Walleij61f922d2016-06-02 11:30:15 +0200780 val = gpiod_get_value_cansleep(le->desc);
781 if (val < 0)
782 return val;
783 ghd.values[0] = val;
784
785 if (copy_to_user(ip, &ghd, sizeof(ghd)))
786 return -EFAULT;
787
788 return 0;
789 }
790 return -EINVAL;
791}
792
793#ifdef CONFIG_COMPAT
794static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
795 unsigned long arg)
796{
797 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
798}
799#endif
800
801static const struct file_operations lineevent_fileops = {
802 .release = lineevent_release,
803 .read = lineevent_read,
804 .poll = lineevent_poll,
805 .owner = THIS_MODULE,
806 .llseek = noop_llseek,
807 .unlocked_ioctl = lineevent_ioctl,
808#ifdef CONFIG_COMPAT
809 .compat_ioctl = lineevent_ioctl_compat,
810#endif
811};
812
Ben Dooks33265b12016-06-17 16:03:13 +0100813static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200814{
815 struct lineevent_state *le = p;
816 struct gpioevent_data ge;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200817 int ret;
Linus Walleij61f922d2016-06-02 11:30:15 +0200818
Linus Walleij24bd3ef2018-01-22 13:19:28 +0100819 /* Do not leak kernel stack to userspace */
820 memset(&ge, 0, sizeof(ge));
821
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100822 ge.timestamp = le->timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200823
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200824 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
825 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200826 int level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200827 if (level)
828 /* Emit low-to-high event */
829 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
830 else
831 /* Emit high-to-low event */
832 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200833 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200834 /* Emit low-to-high event */
835 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200836 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200837 /* Emit high-to-low event */
838 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200839 } else {
840 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200841 }
842
843 ret = kfifo_put(&le->events, ge);
844 if (ret != 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800845 wake_up_poll(&le->wait, EPOLLIN);
Linus Walleij61f922d2016-06-02 11:30:15 +0200846
847 return IRQ_HANDLED;
848}
849
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100850static irqreturn_t lineevent_irq_handler(int irq, void *p)
851{
852 struct lineevent_state *le = p;
853
854 /*
855 * Just store the timestamp in hardirq context so we get it as
856 * close in time as possible to the actual event.
857 */
858 le->timestamp = ktime_get_real_ns();
859
860 return IRQ_WAKE_THREAD;
861}
862
Linus Walleij61f922d2016-06-02 11:30:15 +0200863static int lineevent_create(struct gpio_device *gdev, void __user *ip)
864{
865 struct gpioevent_request eventreq;
866 struct lineevent_state *le;
867 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200868 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200869 u32 offset;
870 u32 lflags;
871 u32 eflags;
872 int fd;
873 int ret;
874 int irqflags = 0;
875
876 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
877 return -EFAULT;
878
879 le = kzalloc(sizeof(*le), GFP_KERNEL);
880 if (!le)
881 return -ENOMEM;
882 le->gdev = gdev;
883 get_device(&gdev->dev);
884
885 /* Make sure this is terminated */
886 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
887 if (strlen(eventreq.consumer_label)) {
888 le->label = kstrdup(eventreq.consumer_label,
889 GFP_KERNEL);
890 if (!le->label) {
891 ret = -ENOMEM;
892 goto out_free_le;
893 }
894 }
895
896 offset = eventreq.lineoffset;
897 lflags = eventreq.handleflags;
898 eflags = eventreq.eventflags;
899
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200900 if (offset >= gdev->ngpio) {
901 ret = -EINVAL;
902 goto out_free_label;
903 }
904
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200905 /* Return an error if a unknown flag is set */
906 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
907 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
908 ret = -EINVAL;
909 goto out_free_label;
910 }
911
Linus Walleij61f922d2016-06-02 11:30:15 +0200912 /* This is just wrong: we don't look for events on output lines */
913 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
914 ret = -EINVAL;
915 goto out_free_label;
916 }
917
918 desc = &gdev->descs[offset];
919 ret = gpiod_request(desc, le->label);
920 if (ret)
Uwe Kleine-Königf001cc32018-04-16 13:17:53 +0200921 goto out_free_label;
Linus Walleij61f922d2016-06-02 11:30:15 +0200922 le->desc = desc;
923 le->eflags = eflags;
924
925 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
926 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
927 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
928 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
929 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
930 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
931
932 ret = gpiod_direction_input(desc);
933 if (ret)
934 goto out_free_desc;
935
936 le->irq = gpiod_to_irq(desc);
937 if (le->irq <= 0) {
938 ret = -ENODEV;
939 goto out_free_desc;
940 }
941
942 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
943 irqflags |= IRQF_TRIGGER_RISING;
944 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
945 irqflags |= IRQF_TRIGGER_FALLING;
946 irqflags |= IRQF_ONESHOT;
Linus Walleij61f922d2016-06-02 11:30:15 +0200947
948 INIT_KFIFO(le->events);
949 init_waitqueue_head(&le->wait);
950 mutex_init(&le->read_lock);
951
952 /* Request a thread to read the events */
953 ret = request_threaded_irq(le->irq,
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100954 lineevent_irq_handler,
Linus Walleij61f922d2016-06-02 11:30:15 +0200955 lineevent_irq_thread,
956 irqflags,
957 le->label,
958 le);
959 if (ret)
960 goto out_free_desc;
961
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200962 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200963 if (fd < 0) {
964 ret = fd;
965 goto out_free_irq;
966 }
967
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200968 file = anon_inode_getfile("gpio-event",
969 &lineevent_fileops,
970 le,
971 O_RDONLY | O_CLOEXEC);
972 if (IS_ERR(file)) {
973 ret = PTR_ERR(file);
974 goto out_put_unused_fd;
975 }
976
Linus Walleij61f922d2016-06-02 11:30:15 +0200977 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200978 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200979 /*
980 * fput() will trigger the release() callback, so do not go onto
981 * the regular error cleanup path here.
982 */
983 fput(file);
984 put_unused_fd(fd);
985 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200986 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200987
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200988 fd_install(fd, file);
989
Linus Walleij61f922d2016-06-02 11:30:15 +0200990 return 0;
991
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200992out_put_unused_fd:
993 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +0200994out_free_irq:
995 free_irq(le->irq, le);
996out_free_desc:
997 gpiod_free(le->desc);
998out_free_label:
999 kfree(le->label);
1000out_free_le:
1001 kfree(le);
1002 put_device(&gdev->dev);
1003 return ret;
1004}
1005
Thierry Reding950d55f52017-07-24 16:57:22 +02001006/*
Linus Walleij3c702e92015-10-21 15:29:53 +02001007 * gpio_ioctl() - ioctl handler for the GPIO chardev
1008 */
1009static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1010{
1011 struct gpio_device *gdev = filp->private_data;
1012 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +02001013 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +02001014
1015 /* We fail any subsequent ioctl():s when the chip is gone */
1016 if (!chip)
1017 return -ENODEV;
1018
Linus Walleij521a2ad2016-02-12 22:25:22 +01001019 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +02001020 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +01001021 struct gpiochip_info chipinfo;
1022
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +02001023 memset(&chipinfo, 0, sizeof(chipinfo));
1024
Linus Walleij3c702e92015-10-21 15:29:53 +02001025 strncpy(chipinfo.name, dev_name(&gdev->dev),
1026 sizeof(chipinfo.name));
1027 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +01001028 strncpy(chipinfo.label, gdev->label,
1029 sizeof(chipinfo.label));
1030 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001031 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +02001032 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1033 return -EFAULT;
1034 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001035 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1036 struct gpioline_info lineinfo;
1037 struct gpio_desc *desc;
1038
1039 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1040 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +02001041 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +01001042 return -EINVAL;
1043
1044 desc = &gdev->descs[lineinfo.line_offset];
1045 if (desc->name) {
1046 strncpy(lineinfo.name, desc->name,
1047 sizeof(lineinfo.name));
1048 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
1049 } else {
1050 lineinfo.name[0] = '\0';
1051 }
1052 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +01001053 strncpy(lineinfo.consumer, desc->label,
1054 sizeof(lineinfo.consumer));
1055 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001056 } else {
Linus Walleij214338e2016-02-25 21:01:48 +01001057 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001058 }
1059
1060 /*
1061 * Userspace only need to know that the kernel is using
1062 * this GPIO so it can't use it.
1063 */
1064 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001065 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1066 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1067 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1068 test_bit(FLAG_EXPORT, &desc->flags) ||
1069 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001070 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001071 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001072 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001073 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001074 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001075 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001076 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001077 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001078 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
1079
1080 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1081 return -EFAULT;
1082 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +02001083 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1084 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +02001085 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1086 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +02001087 }
1088 return -EINVAL;
1089}
1090
Linus Walleij8b92e172016-05-27 14:24:04 +02001091#ifdef CONFIG_COMPAT
1092static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1093 unsigned long arg)
1094{
1095 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1096}
1097#endif
1098
Linus Walleij3c702e92015-10-21 15:29:53 +02001099/**
1100 * gpio_chrdev_open() - open the chardev for ioctl operations
1101 * @inode: inode for this chardev
1102 * @filp: file struct for storing private data
1103 * Returns 0 on success
1104 */
1105static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1106{
1107 struct gpio_device *gdev = container_of(inode->i_cdev,
1108 struct gpio_device, chrdev);
1109
1110 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001111 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001112 return -ENODEV;
1113 get_device(&gdev->dev);
1114 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001115
1116 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001117}
1118
1119/**
1120 * gpio_chrdev_release() - close chardev after ioctl operations
1121 * @inode: inode for this chardev
1122 * @filp: file struct for storing private data
1123 * Returns 0 on success
1124 */
1125static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1126{
1127 struct gpio_device *gdev = container_of(inode->i_cdev,
1128 struct gpio_device, chrdev);
1129
Linus Walleij3c702e92015-10-21 15:29:53 +02001130 put_device(&gdev->dev);
1131 return 0;
1132}
1133
1134
1135static const struct file_operations gpio_fileops = {
1136 .release = gpio_chrdev_release,
1137 .open = gpio_chrdev_open,
1138 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001139 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001140 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001141#ifdef CONFIG_COMPAT
1142 .compat_ioctl = gpio_ioctl_compat,
1143#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001144};
1145
Linus Walleijff2b1352015-10-20 11:10:38 +02001146static void gpiodevice_release(struct device *dev)
1147{
1148 struct gpio_device *gdev = dev_get_drvdata(dev);
1149
1150 list_del(&gdev->list);
1151 ida_simple_remove(&gpio_ida, gdev->id);
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001152 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001153 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001154 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001155}
1156
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001157static int gpiochip_setup_dev(struct gpio_device *gdev)
1158{
1159 int status;
1160
1161 cdev_init(&gdev->chrdev, &gpio_fileops);
1162 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001163 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001164
1165 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001166 if (status)
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001167 return status;
1168
1169 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1170 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001171
1172 status = gpiochip_sysfs_register(gdev);
1173 if (status)
1174 goto err_remove_device;
1175
1176 /* From this point, the .release() function cleans up gpio_device */
1177 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001178 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1179 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1180 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1181
1182 return 0;
1183
1184err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001185 cdev_device_del(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001186 return status;
1187}
1188
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001189static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
1190{
1191 struct gpio_desc *desc;
1192 int rv;
1193
1194 desc = gpiochip_get_desc(chip, hog->chip_hwnum);
1195 if (IS_ERR(desc)) {
1196 pr_err("%s: unable to get GPIO desc: %ld\n",
1197 __func__, PTR_ERR(desc));
1198 return;
1199 }
1200
Dan Carpenterba3efdf2018-05-01 10:36:39 +03001201 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001202 return;
1203
1204 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
1205 if (rv)
1206 pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
1207 __func__, chip->label, hog->chip_hwnum, rv);
1208}
1209
1210static void machine_gpiochip_add(struct gpio_chip *chip)
1211{
1212 struct gpiod_hog *hog;
1213
1214 mutex_lock(&gpio_machine_hogs_mutex);
1215
1216 list_for_each_entry(hog, &gpio_machine_hogs, list) {
1217 if (!strcmp(chip->label, hog->chip_label))
1218 gpiochip_machine_hog(chip, hog);
1219 }
1220
1221 mutex_unlock(&gpio_machine_hogs_mutex);
1222}
1223
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001224static void gpiochip_setup_devs(void)
1225{
1226 struct gpio_device *gdev;
1227 int err;
1228
1229 list_for_each_entry(gdev, &gpio_devices, list) {
1230 err = gpiochip_setup_dev(gdev);
1231 if (err)
1232 pr_err("%s: Failed to initialize gpio device (%d)\n",
1233 dev_name(&gdev->dev), err);
1234 }
1235}
1236
Thierry Reding959bc7b2017-11-07 19:15:59 +01001237int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001238 struct lock_class_key *lock_key,
1239 struct lock_class_key *request_key)
David Brownelld2876d02008-02-04 22:28:20 -08001240{
1241 unsigned long flags;
1242 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001243 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001244 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001245 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001246
Linus Walleijff2b1352015-10-20 11:10:38 +02001247 /*
1248 * First: allocate and populate the internal stat container, and
1249 * set up the struct device.
1250 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001251 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001252 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001253 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001254 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001255 gdev->chip = chip;
1256 chip->gpiodev = gdev;
1257 if (chip->parent) {
1258 gdev->dev.parent = chip->parent;
1259 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001260 }
1261
Linus Walleijff2b1352015-10-20 11:10:38 +02001262#ifdef CONFIG_OF_GPIO
1263 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001264 if (chip->of_node)
1265 gdev->dev.of_node = chip->of_node;
Biju Das6ff04972018-08-06 10:48:01 +01001266 else
1267 chip->of_node = gdev->dev.of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001268#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001269
Linus Walleijff2b1352015-10-20 11:10:38 +02001270 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1271 if (gdev->id < 0) {
1272 status = gdev->id;
1273 goto err_free_gdev;
1274 }
1275 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1276 device_initialize(&gdev->dev);
1277 dev_set_drvdata(&gdev->dev, gdev);
1278 if (chip->parent && chip->parent->driver)
1279 gdev->owner = chip->parent->driver->owner;
1280 else if (chip->owner)
1281 /* TODO: remove chip->owner */
1282 gdev->owner = chip->owner;
1283 else
1284 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001285
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001286 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001287 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001288 status = -ENOMEM;
1289 goto err_free_gdev;
1290 }
1291
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001292 if (chip->ngpio == 0) {
1293 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001294 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001295 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001296 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001297
Laura Abbott30277432018-05-21 10:57:07 -07001298 if (chip->ngpio > FASTPATH_NGPIO)
1299 chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n",
1300 chip->ngpio, FASTPATH_NGPIO);
1301
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001302 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001303 if (!gdev->label) {
1304 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001305 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001306 }
1307
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001308 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001309 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001310
David Brownelld2876d02008-02-04 22:28:20 -08001311 spin_lock_irqsave(&gpio_lock, flags);
1312
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001313 /*
1314 * TODO: this allocates a Linux GPIO number base in the global
1315 * GPIO numberspace for this chip. In the long run we want to
1316 * get *rid* of this numberspace and use only descriptors, but
1317 * it may be a pipe dream. It will not happen before we get rid
1318 * of the sysfs interface anyways.
1319 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001320 if (base < 0) {
1321 base = gpiochip_find_base(chip->ngpio);
1322 if (base < 0) {
1323 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001324 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001325 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001326 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001327 /*
1328 * TODO: it should not be necessary to reflect the assigned
1329 * base outside of the GPIO subsystem. Go over drivers and
1330 * see if anyone makes use of this, else drop this and assign
1331 * a poison instead.
1332 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001333 chip->base = base;
1334 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001335 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001336
Linus Walleijff2b1352015-10-20 11:10:38 +02001337 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001338 if (status) {
1339 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001340 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001341 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001342
Linus Walleij545ebd92016-05-30 17:11:59 +02001343 spin_unlock_irqrestore(&gpio_lock, flags);
1344
Linus Walleijff2b1352015-10-20 11:10:38 +02001345 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001346 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001347
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001348 desc->gdev = gdev;
Timur Tabi1ca2a922017-12-20 13:10:31 -06001349
1350 /* REVISIT: most hardware initializes GPIOs as inputs (often
1351 * with pullups enabled) so power usage is minimized. Linux
1352 * code should set the gpio direction first thing; but until
1353 * it does, and in case chip->get_direction is not set, we may
1354 * expose the wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001355 */
Timur Tabi1ca2a922017-12-20 13:10:31 -06001356 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001357 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001358
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301359#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001360 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301361#endif
1362
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001363 status = gpiochip_set_desc_names(chip);
1364 if (status)
1365 goto err_remove_from_list;
1366
Mika Westerberg79b804c2016-09-20 15:15:21 +03001367 status = gpiochip_irqchip_init_valid_mask(chip);
1368 if (status)
1369 goto err_remove_from_list;
1370
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001371 status = gpiochip_init_valid_mask(chip);
1372 if (status)
1373 goto err_remove_irqchip_mask;
1374
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001375 status = gpiochip_add_irqchip(chip, lock_key, request_key);
Thierry Redinge0d89722017-11-07 19:15:54 +01001376 if (status)
1377 goto err_remove_chip;
1378
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001379 status = of_gpiochip_add(chip);
1380 if (status)
1381 goto err_remove_chip;
1382
Mika Westerberg664e3e52014-01-08 12:40:54 +02001383 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001384
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001385 machine_gpiochip_add(chip);
1386
Linus Walleij3c702e92015-10-21 15:29:53 +02001387 /*
1388 * By first adding the chardev, and then adding the device,
1389 * we get a device node entry in sysfs under
1390 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1391 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001392 * We can do this only if gpiolib has been initialized.
1393 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001394 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001395 if (gpiolib_initialized) {
1396 status = gpiochip_setup_dev(gdev);
1397 if (status)
1398 goto err_remove_chip;
1399 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001400 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001401
Johan Hovold225fce82015-01-12 17:12:25 +01001402err_remove_chip:
1403 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001404 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001405 of_gpiochip_remove(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001406 gpiochip_free_valid_mask(chip);
1407err_remove_irqchip_mask:
Mika Westerberg79b804c2016-09-20 15:15:21 +03001408 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001409err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001410 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001411 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001412 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001413err_free_label:
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001414 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001415err_free_descs:
1416 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001417err_free_gdev:
1418 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001419 /* failures here can mean systems won't boot... */
Marcel Ziswiler1777fc92018-07-20 09:54:49 +02001420 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001421 gdev->base, gdev->base + gdev->ngpio - 1,
Marcel Ziswiler1777fc92018-07-20 09:54:49 +02001422 chip->label ? : "generic", status);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001423 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001424 return status;
1425}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001426EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001427
1428/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001429 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001430 * @chip: GPIO chip
1431 *
1432 * Returns:
1433 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001434 */
1435void *gpiochip_get_data(struct gpio_chip *chip)
1436{
1437 return chip->gpiodev->data;
1438}
1439EXPORT_SYMBOL_GPL(gpiochip_get_data);
1440
1441/**
David Brownelld2876d02008-02-04 22:28:20 -08001442 * gpiochip_remove() - unregister a gpio_chip
1443 * @chip: the chip to unregister
1444 *
1445 * A gpio_chip with any GPIOs still requested may not be removed.
1446 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001447void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001448{
Linus Walleijff2b1352015-10-20 11:10:38 +02001449 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001450 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001451 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001452 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001453 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001454
Linus Walleijff2b1352015-10-20 11:10:38 +02001455 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001456 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001457 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001458 /* Numb the device, cancelling all outstanding operations */
1459 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001460 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001461 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001462 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001463 of_gpiochip_remove(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001464 gpiochip_free_valid_mask(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001465 /*
1466 * We accept no more calls into the driver from this point, so
1467 * NULL the driver data pointer
1468 */
1469 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001470
Johan Hovold6798aca2015-01-12 17:12:28 +01001471 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001472 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001473 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001474 if (test_bit(FLAG_REQUESTED, &desc->flags))
1475 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001476 }
David Brownelld2876d02008-02-04 22:28:20 -08001477 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001478
Johan Hovoldfab28b82015-05-04 17:10:27 +02001479 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001480 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001481 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001482
Linus Walleijff2b1352015-10-20 11:10:38 +02001483 /*
1484 * The gpiochip side puts its use of the device to rest here:
1485 * if there are no userspace clients, the chardev and device will
1486 * be removed, else it will be dangling until the last user is
1487 * gone.
1488 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001489 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001490 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001491}
1492EXPORT_SYMBOL_GPL(gpiochip_remove);
1493
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301494static void devm_gpio_chip_release(struct device *dev, void *res)
1495{
1496 struct gpio_chip *chip = *(struct gpio_chip **)res;
1497
1498 gpiochip_remove(chip);
1499}
1500
1501static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1502
1503{
1504 struct gpio_chip **r = res;
1505
1506 if (!r || !*r) {
1507 WARN_ON(!r || !*r);
1508 return 0;
1509 }
1510
1511 return *r == data;
1512}
1513
1514/**
Jonathan Neuschäfer689fd022017-12-21 17:56:34 +01001515 * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301516 * @dev: the device pointer on which irq_chip belongs to.
1517 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001518 * @data: driver-private data associated with this chip
1519 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301520 * Context: potentially before irqs will work
1521 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301522 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001523 *
1524 * Returns:
1525 * A negative errno if the chip can't be registered, such as because the
1526 * chip->base is invalid or already associated with a different chip.
1527 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301528 */
1529int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1530 void *data)
1531{
1532 struct gpio_chip **ptr;
1533 int ret;
1534
1535 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1536 GFP_KERNEL);
1537 if (!ptr)
1538 return -ENOMEM;
1539
1540 ret = gpiochip_add_data(chip, data);
1541 if (ret < 0) {
1542 devres_free(ptr);
1543 return ret;
1544 }
1545
1546 *ptr = chip;
1547 devres_add(dev, ptr);
1548
1549 return 0;
1550}
1551EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1552
1553/**
1554 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1555 * @dev: device for which which resource was allocated
1556 * @chip: the chip to remove
1557 *
1558 * A gpio_chip with any GPIOs still requested may not be removed.
1559 */
1560void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1561{
1562 int ret;
1563
1564 ret = devres_release(dev, devm_gpio_chip_release,
1565 devm_gpio_chip_match, chip);
Christophe JAILLET3988d662017-01-27 12:56:42 +01001566 WARN_ON(ret);
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301567}
1568EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1569
Grant Likely594fa262010-06-08 07:48:16 -06001570/**
1571 * gpiochip_find() - iterator for locating a specific gpio_chip
1572 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001573 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001574 *
1575 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1576 * determined by a user supplied @match callback. The callback should return
1577 * 0 if the device doesn't match and non-zero if it does. If the callback is
1578 * non-zero, this function will return to the caller and not iterate over any
1579 * more gpio_chips.
1580 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001581struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001582 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001583 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001584{
Linus Walleijff2b1352015-10-20 11:10:38 +02001585 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001586 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001587 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001588
1589 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001590 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001591 if (gdev->chip && match(gdev->chip, data)) {
1592 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001593 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001594 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001595
Grant Likely594fa262010-06-08 07:48:16 -06001596 spin_unlock_irqrestore(&gpio_lock, flags);
1597
1598 return chip;
1599}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001600EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001601
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001602static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1603{
1604 const char *name = data;
1605
1606 return !strcmp(chip->label, name);
1607}
1608
1609static struct gpio_chip *find_chip_by_name(const char *name)
1610{
1611 return gpiochip_find((void *)name, gpiochip_match_name);
1612}
1613
Linus Walleij14250522014-03-25 10:40:18 +01001614#ifdef CONFIG_GPIOLIB_IRQCHIP
1615
1616/*
1617 * The following is irqchip helper code for gpiochips.
1618 */
1619
Mika Westerberg79b804c2016-09-20 15:15:21 +03001620static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1621{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001622 if (!gpiochip->irq.need_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001623 return 0;
1624
Stephen Boyde4371f6e2018-03-23 09:34:50 -07001625 gpiochip->irq.valid_mask = gpiochip_allocate_mask(gpiochip);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001626 if (!gpiochip->irq.valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001627 return -ENOMEM;
1628
Mika Westerberg79b804c2016-09-20 15:15:21 +03001629 return 0;
1630}
1631
1632static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1633{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001634 kfree(gpiochip->irq.valid_mask);
1635 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001636}
1637
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001638bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1639 unsigned int offset)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001640{
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001641 if (!gpiochip_line_is_valid(gpiochip, offset))
1642 return false;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001643 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001644 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001645 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001646 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001647}
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001648EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001649
Linus Walleij14250522014-03-25 10:40:18 +01001650/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001651 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001652 * @gpiochip: the gpiochip to set the irqchip chain to
1653 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001654 * @parent_irq: the irq number corresponding to the parent IRQ for this
1655 * chained irqchip
1656 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001657 * coming out of the gpiochip. If the interrupt is nested rather than
1658 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001659 */
Linus Walleijd245b3f2016-11-24 10:57:25 +01001660static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1661 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001662 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001663 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001664{
Linus Walleij83141a72014-09-26 13:50:12 +02001665 unsigned int offset;
1666
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001667 if (!gpiochip->irq.domain) {
Linus Walleij83141a72014-09-26 13:50:12 +02001668 chip_err(gpiochip, "called %s before setting up irqchip\n",
1669 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001670 return;
1671 }
1672
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001673 if (parent_handler) {
1674 if (gpiochip->can_sleep) {
1675 chip_err(gpiochip,
Andy Shevchenkob1911712018-07-03 03:39:03 +03001676 "you cannot have chained interrupts on a chip that may sleep\n");
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001677 return;
1678 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001679 /*
1680 * The parent irqchip is already using the chip_data for this
1681 * irqchip, so our callbacks simply use the handler_data.
1682 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001683 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1684 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001685
Thierry Reding39e5f092017-11-07 19:15:50 +01001686 gpiochip->irq.parents = &parent_irq;
1687 gpiochip->irq.num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001688 }
Linus Walleij83141a72014-09-26 13:50:12 +02001689
1690 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001691 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1692 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1693 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001694 irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
Linus Walleij83141a72014-09-26 13:50:12 +02001695 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001696 }
Linus Walleij14250522014-03-25 10:40:18 +01001697}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001698
1699/**
1700 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1701 * @gpiochip: the gpiochip to set the irqchip chain to
1702 * @irqchip: the irqchip to chain to the gpiochip
1703 * @parent_irq: the irq number corresponding to the parent IRQ for this
1704 * chained irqchip
1705 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1706 * coming out of the gpiochip. If the interrupt is nested rather than
1707 * cascaded, pass NULL in this handler argument
1708 */
1709void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1710 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001711 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001712 irq_flow_handler_t parent_handler)
1713{
Thierry Reding60ed54c2017-11-07 19:15:57 +01001714 if (gpiochip->irq.threaded) {
1715 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1716 return;
1717 }
1718
Linus Walleijd245b3f2016-11-24 10:57:25 +01001719 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1720 parent_handler);
1721}
Linus Walleij14250522014-03-25 10:40:18 +01001722EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1723
1724/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001725 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1726 * @gpiochip: the gpiochip to set the irqchip nested handler to
1727 * @irqchip: the irqchip to nest to the gpiochip
1728 * @parent_irq: the irq number corresponding to the parent IRQ for this
1729 * nested irqchip
1730 */
1731void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1732 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001733 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001734{
Linus Walleijd245b3f2016-11-24 10:57:25 +01001735 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1736 NULL);
1737}
1738EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1739
1740/**
Linus Walleij14250522014-03-25 10:40:18 +01001741 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1742 * @d: the irqdomain used by this irqchip
1743 * @irq: the global irq number used by this GPIO irqchip irq
1744 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1745 *
1746 * This function will set up the mapping for a certain IRQ line on a
1747 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1748 * stored inside the gpiochip.
1749 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001750int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1751 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01001752{
1753 struct gpio_chip *chip = d->host_data;
Thierry Redinge0d89722017-11-07 19:15:54 +01001754 int err = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001755
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001756 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1757 return -ENXIO;
1758
Linus Walleij14250522014-03-25 10:40:18 +01001759 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001760 /*
1761 * This lock class tells lockdep that GPIO irqs are in a different
1762 * category than their parents, so it won't report false recursion.
1763 */
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001764 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001765 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001766 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01001767 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001768 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001769 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001770
Thierry Redinge0d89722017-11-07 19:15:54 +01001771 if (chip->irq.num_parents == 1)
1772 err = irq_set_parent(irq, chip->irq.parents[0]);
1773 else if (chip->irq.map)
1774 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1775
1776 if (err < 0)
1777 return err;
1778
Linus Walleij1333b902014-04-23 16:45:12 +02001779 /*
1780 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1781 * is passed as default type.
1782 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01001783 if (chip->irq.default_type != IRQ_TYPE_NONE)
1784 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001785
1786 return 0;
1787}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001788EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01001789
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001790void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01001791{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001792 struct gpio_chip *chip = d->host_data;
1793
Thierry Reding60ed54c2017-11-07 19:15:57 +01001794 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001795 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001796 irq_set_chip_and_handler(irq, NULL, NULL);
1797 irq_set_chip_data(irq, NULL);
1798}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001799EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001800
Linus Walleij14250522014-03-25 10:40:18 +01001801static const struct irq_domain_ops gpiochip_domain_ops = {
1802 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001803 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001804 /* Virtually all GPIO irqchips are twocell:ed */
1805 .xlate = irq_domain_xlate_twocell,
1806};
1807
Linus Walleij14250522014-03-25 10:40:18 +01001808static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1809{
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001810 if (!gpiochip_irqchip_irq_valid(chip, offset))
1811 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01001812
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001813 return irq_create_mapping(chip->irq.domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01001814}
1815
Hans Verkuil4e6b8232018-09-08 11:23:14 +02001816static int gpiochip_irq_reqres(struct irq_data *d)
1817{
1818 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1819
1820 return gpiochip_reqres_irq(chip, d->hwirq);
1821}
1822
1823static void gpiochip_irq_relres(struct irq_data *d)
1824{
1825 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1826
1827 gpiochip_relres_irq(chip, d->hwirq);
1828}
1829
Hans Verkuil461c1a72018-09-08 11:23:17 +02001830static void gpiochip_irq_enable(struct irq_data *d)
1831{
1832 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1833
1834 gpiochip_enable_irq(chip, d->hwirq);
1835 if (chip->irq.irq_enable)
1836 chip->irq.irq_enable(d);
1837 else
1838 chip->irq.chip->irq_unmask(d);
1839}
1840
1841static void gpiochip_irq_disable(struct irq_data *d)
1842{
1843 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1844
1845 if (chip->irq.irq_disable)
1846 chip->irq.irq_disable(d);
1847 else
1848 chip->irq.chip->irq_mask(d);
1849 gpiochip_disable_irq(chip, d->hwirq);
1850}
1851
Hans Verkuilca620f22018-09-08 11:23:15 +02001852static void gpiochip_set_irq_hooks(struct gpio_chip *gpiochip)
1853{
1854 struct irq_chip *irqchip = gpiochip->irq.chip;
1855
1856 if (!irqchip->irq_request_resources &&
1857 !irqchip->irq_release_resources) {
1858 irqchip->irq_request_resources = gpiochip_irq_reqres;
1859 irqchip->irq_release_resources = gpiochip_irq_relres;
1860 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02001861 if (WARN_ON(gpiochip->irq.irq_enable))
1862 return;
Hans Verkuil171948e2018-09-14 10:36:39 +02001863 /* Check if the irqchip already has this hook... */
1864 if (irqchip->irq_enable == gpiochip_irq_enable) {
1865 /*
1866 * ...and if so, give a gentle warning that this is bad
1867 * practice.
1868 */
1869 chip_info(gpiochip,
1870 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1871 return;
1872 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02001873 gpiochip->irq.irq_enable = irqchip->irq_enable;
1874 gpiochip->irq.irq_disable = irqchip->irq_disable;
1875 irqchip->irq_enable = gpiochip_irq_enable;
1876 irqchip->irq_disable = gpiochip_irq_disable;
Hans Verkuilca620f22018-09-08 11:23:15 +02001877}
1878
Linus Walleij14250522014-03-25 10:40:18 +01001879/**
Thierry Redinge0d89722017-11-07 19:15:54 +01001880 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1881 * @gpiochip: the GPIO chip to add the IRQ chip to
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001882 * @lock_key: lockdep class for IRQ lock
1883 * @request_key: lockdep class for IRQ request
Thierry Redinge0d89722017-11-07 19:15:54 +01001884 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01001885static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001886 struct lock_class_key *lock_key,
1887 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001888{
1889 struct irq_chip *irqchip = gpiochip->irq.chip;
1890 const struct irq_domain_ops *ops;
1891 struct device_node *np;
1892 unsigned int type;
1893 unsigned int i;
1894
1895 if (!irqchip)
1896 return 0;
1897
1898 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
Andy Shevchenkob1911712018-07-03 03:39:03 +03001899 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
Thierry Redinge0d89722017-11-07 19:15:54 +01001900 return -EINVAL;
1901 }
1902
1903 np = gpiochip->gpiodev->dev.of_node;
1904 type = gpiochip->irq.default_type;
1905
1906 /*
1907 * Specifying a default trigger is a terrible idea if DT or ACPI is
1908 * used to configure the interrupts, as you may end up with
1909 * conflicting triggers. Tell the user, and reset to NONE.
1910 */
1911 if (WARN(np && type != IRQ_TYPE_NONE,
1912 "%s: Ignoring %u default trigger\n", np->full_name, type))
1913 type = IRQ_TYPE_NONE;
1914
1915 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1916 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1917 "Ignoring %u default trigger\n", type);
1918 type = IRQ_TYPE_NONE;
1919 }
1920
1921 gpiochip->to_irq = gpiochip_to_irq;
1922 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01001923 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001924 gpiochip->irq.request_key = request_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01001925
1926 if (gpiochip->irq.domain_ops)
1927 ops = gpiochip->irq.domain_ops;
1928 else
1929 ops = &gpiochip_domain_ops;
1930
1931 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
Thierry Reding8302cf52017-11-07 19:15:58 +01001932 gpiochip->irq.first,
1933 ops, gpiochip);
Thierry Redinge0d89722017-11-07 19:15:54 +01001934 if (!gpiochip->irq.domain)
1935 return -EINVAL;
1936
Thierry Redinge0d89722017-11-07 19:15:54 +01001937 if (gpiochip->irq.parent_handler) {
1938 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1939
1940 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1941 /*
1942 * The parent IRQ chip is already using the chip_data
1943 * for this IRQ chip, so our callbacks simply use the
1944 * handler_data.
1945 */
1946 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1947 gpiochip->irq.parent_handler,
1948 data);
1949 }
Thierry Redinge0d89722017-11-07 19:15:54 +01001950 }
1951
Hans Verkuilca620f22018-09-08 11:23:15 +02001952 gpiochip_set_irq_hooks(gpiochip);
1953
Thierry Redinge0d89722017-11-07 19:15:54 +01001954 acpi_gpiochip_request_interrupts(gpiochip);
1955
1956 return 0;
1957}
1958
1959/**
Linus Walleij14250522014-03-25 10:40:18 +01001960 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1961 * @gpiochip: the gpiochip to remove the irqchip from
1962 *
1963 * This is called only from gpiochip_remove()
1964 */
1965static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1966{
Hans Verkuilca620f22018-09-08 11:23:15 +02001967 struct irq_chip *irqchip = gpiochip->irq.chip;
Thierry Reding39e5f092017-11-07 19:15:50 +01001968 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001969
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001970 acpi_gpiochip_free_interrupts(gpiochip);
1971
Hans Verkuilca620f22018-09-08 11:23:15 +02001972 if (irqchip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001973 struct gpio_irq_chip *irq = &gpiochip->irq;
1974 unsigned int i;
1975
1976 for (i = 0; i < irq->num_parents; i++)
1977 irq_set_chained_handler_and_data(irq->parents[i],
1978 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001979 }
1980
Linus Walleijc3626fd2014-03-28 20:42:01 +01001981 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001982 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001983 unsigned int irq;
1984
Mika Westerberg79b804c2016-09-20 15:15:21 +03001985 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1986 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1987 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001988
1989 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1990 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001991 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001992
1993 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001994 }
Linus Walleij14250522014-03-25 10:40:18 +01001995
Hans Verkuil461c1a72018-09-08 11:23:17 +02001996 if (irqchip) {
1997 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1998 irqchip->irq_request_resources = NULL;
1999 irqchip->irq_release_resources = NULL;
2000 }
2001 if (irqchip->irq_enable == gpiochip_irq_enable) {
2002 irqchip->irq_enable = gpiochip->irq.irq_enable;
2003 irqchip->irq_disable = gpiochip->irq.irq_disable;
2004 }
Linus Walleij14250522014-03-25 10:40:18 +01002005 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002006 gpiochip->irq.irq_enable = NULL;
2007 gpiochip->irq.irq_disable = NULL;
Hans Verkuilca620f22018-09-08 11:23:15 +02002008 gpiochip->irq.chip = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03002009
2010 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002011}
2012
2013/**
Linus Walleij739e6f52017-01-11 13:37:07 +01002014 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01002015 * @gpiochip: the gpiochip to add the irqchip to
2016 * @irqchip: the irqchip to add to the gpiochip
2017 * @first_irq: if not dynamically assigned, the base (first) IRQ to
2018 * allocate gpiochip irqs from
2019 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02002020 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
2021 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01002022 * @threaded: whether this irqchip uses a nested thread handler
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002023 * @lock_key: lockdep class for IRQ lock
2024 * @request_key: lockdep class for IRQ request
Linus Walleij14250522014-03-25 10:40:18 +01002025 *
2026 * This function closely associates a certain irqchip with a certain
2027 * gpiochip, providing an irq domain to translate the local IRQs to
2028 * global irqs in the gpiolib core, and making sure that the gpiochip
2029 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01002030 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01002031 * from the gpiochip passed as chip data. An irqdomain will be stored
2032 * in the gpiochip that shall be used by the driver to handle IRQ number
2033 * translation. The gpiochip will need to be initialized and registered
2034 * before calling this function.
2035 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01002036 * This function will handle two cell:ed simple IRQs and assumes all
2037 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01002038 * need to be open coded.
2039 */
Linus Walleij739e6f52017-01-11 13:37:07 +01002040int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
2041 struct irq_chip *irqchip,
2042 unsigned int first_irq,
2043 irq_flow_handler_t handler,
2044 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01002045 bool threaded,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002046 struct lock_class_key *lock_key,
2047 struct lock_class_key *request_key)
Linus Walleij14250522014-03-25 10:40:18 +01002048{
2049 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002050
2051 if (!gpiochip || !irqchip)
2052 return -EINVAL;
2053
Linus Walleij58383c782015-11-04 09:56:26 +01002054 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01002055 pr_err("missing gpiochip .dev parent pointer\n");
2056 return -EINVAL;
2057 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01002058 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01002059 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002060#ifdef CONFIG_OF_GPIO
2061 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07002062 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08002063 * FIXME: get rid of this and use gpiochip->parent->of_node
2064 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01002065 */
2066 if (gpiochip->of_node)
2067 of_node = gpiochip->of_node;
2068#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01002069 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002070 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01002071 * used to configure the interrupts, as you may end-up with
2072 * conflicting triggers. Tell the user, and reset to NONE.
2073 */
2074 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05002075 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01002076 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002077 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2078 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2079 "Ignoring %d default trigger\n", type);
2080 type = IRQ_TYPE_NONE;
2081 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01002082
Thierry Redingda80ff82017-11-07 19:15:46 +01002083 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01002084 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01002085 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01002086 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01002087 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002088 gpiochip->irq.request_key = request_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002089 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01002090 gpiochip->ngpio, first_irq,
2091 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002092 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01002093 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01002094 return -EINVAL;
2095 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02002096
Hans Verkuilca620f22018-09-08 11:23:15 +02002097 gpiochip_set_irq_hooks(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002098
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002099 acpi_gpiochip_request_interrupts(gpiochip);
2100
Linus Walleij14250522014-03-25 10:40:18 +01002101 return 0;
2102}
Linus Walleij739e6f52017-01-11 13:37:07 +01002103EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01002104
2105#else /* CONFIG_GPIOLIB_IRQCHIP */
2106
Thierry Reding959bc7b2017-11-07 19:15:59 +01002107static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002108 struct lock_class_key *lock_key,
2109 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002110{
2111 return 0;
2112}
2113
Linus Walleij14250522014-03-25 10:40:18 +01002114static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03002115static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
2116{
2117 return 0;
2118}
2119static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
2120{ }
Linus Walleij14250522014-03-25 10:40:18 +01002121
2122#endif /* CONFIG_GPIOLIB_IRQCHIP */
2123
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002124/**
2125 * gpiochip_generic_request() - request the gpio function for a pin
2126 * @chip: the gpiochip owning the GPIO
2127 * @offset: the offset of the GPIO to request for GPIO function
2128 */
2129int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
2130{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002131 return pinctrl_gpio_request(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002132}
2133EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2134
2135/**
2136 * gpiochip_generic_free() - free the gpio function from a pin
2137 * @chip: the gpiochip to request the gpio function for
2138 * @offset: the offset of the GPIO to free from GPIO function
2139 */
2140void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2141{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002142 pinctrl_gpio_free(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002143}
2144EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2145
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002146/**
2147 * gpiochip_generic_config() - apply configuration for a pin
2148 * @chip: the gpiochip owning the GPIO
2149 * @offset: the offset of the GPIO to apply the configuration
2150 * @config: the configuration to be applied
2151 */
2152int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2153 unsigned long config)
2154{
2155 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2156}
2157EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2158
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302159#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002160
Linus Walleij3f0f8672012-11-20 12:40:15 +01002161/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002162 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2163 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002164 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002165 * @gpio_offset: the start offset in the current gpio_chip number space
2166 * @pin_group: name of the pin group inside the pin controller
Christian Lamparter973c1712018-05-21 22:57:39 +02002167 *
2168 * Calling this function directly from a DeviceTree-supported
2169 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2170 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2171 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Christian Ruppert586a87e2013-10-15 15:37:54 +02002172 */
2173int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2174 struct pinctrl_dev *pctldev,
2175 unsigned int gpio_offset, const char *pin_group)
2176{
2177 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002178 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002179 int ret;
2180
2181 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2182 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002183 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002184 return -ENOMEM;
2185 }
2186
2187 /* Use local offset as range ID */
2188 pin_range->range.id = gpio_offset;
2189 pin_range->range.gc = chip;
2190 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002191 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002192 pin_range->pctldev = pctldev;
2193
2194 ret = pinctrl_get_group_pins(pctldev, pin_group,
2195 &pin_range->range.pins,
2196 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002197 if (ret < 0) {
2198 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002199 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002200 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002201
2202 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2203
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002204 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2205 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002206 pinctrl_dev_get_devname(pctldev), pin_group);
2207
Linus Walleij20ec3e32016-02-11 11:03:06 +01002208 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002209
2210 return 0;
2211}
2212EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2213
2214/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002215 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2216 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002217 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002218 * @gpio_offset: the start offset in the current gpio_chip number space
2219 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002220 * @npins: the number of pins from the offset of each pin space (GPIO and
2221 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002222 *
2223 * Returns:
2224 * 0 on success, or a negative error-code on failure.
Christian Lamparter973c1712018-05-21 22:57:39 +02002225 *
2226 * Calling this function directly from a DeviceTree-supported
2227 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2228 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2229 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002230 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002231int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002232 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002233 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302234{
2235 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002236 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002237 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302238
Linus Walleij3f0f8672012-11-20 12:40:15 +01002239 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302240 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002241 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002242 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302243 }
2244
Linus Walleij3f0f8672012-11-20 12:40:15 +01002245 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002246 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002247 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302248 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002249 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002250 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302251 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002252 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302253 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002254 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002255 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002256 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002257 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002258 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002259 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002260 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2261 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002262 pinctl_name,
2263 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302264
Linus Walleij20ec3e32016-02-11 11:03:06 +01002265 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002266
2267 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302268}
Linus Walleij165adc92012-11-06 14:49:39 +01002269EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302270
Linus Walleij3f0f8672012-11-20 12:40:15 +01002271/**
2272 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2273 * @chip: the chip to remove all the mappings for
2274 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302275void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2276{
2277 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002278 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302279
Linus Walleij20ec3e32016-02-11 11:03:06 +01002280 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302281 list_del(&pin_range->node);
2282 pinctrl_remove_gpio_range(pin_range->pctldev,
2283 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002284 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302285 }
2286}
Linus Walleij165adc92012-11-06 14:49:39 +01002287EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2288
2289#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302290
David Brownelld2876d02008-02-04 22:28:20 -08002291/* These "optional" allocation calls help prevent drivers from stomping
2292 * on each other, and help provide better diagnostics in debugfs.
2293 * They're called even less than the "set direction" calls.
2294 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002295static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002296{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002297 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002298 int status;
David Brownelld2876d02008-02-04 22:28:20 -08002299 unsigned long flags;
Biju Das3789f5a2018-08-07 08:15:18 +01002300 unsigned offset;
David Brownelld2876d02008-02-04 22:28:20 -08002301
2302 spin_lock_irqsave(&gpio_lock, flags);
2303
David Brownelld2876d02008-02-04 22:28:20 -08002304 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002305 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002306 */
2307
2308 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2309 desc_set_label(desc, label ? : "?");
2310 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002311 } else {
David Brownelld2876d02008-02-04 22:28:20 -08002312 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002313 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002314 }
2315
2316 if (chip->request) {
2317 /* chip->request may sleep */
2318 spin_unlock_irqrestore(&gpio_lock, flags);
Biju Das3789f5a2018-08-07 08:15:18 +01002319 offset = gpio_chip_hwgpio(desc);
2320 if (gpiochip_line_is_valid(chip, offset))
2321 status = chip->request(chip, offset);
2322 else
2323 status = -EINVAL;
David Brownell35e8bb52008-10-15 22:03:16 -07002324 spin_lock_irqsave(&gpio_lock, flags);
2325
2326 if (status < 0) {
2327 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07002328 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002329 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002330 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002331 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002332 if (chip->get_direction) {
2333 /* chip->get_direction may sleep */
2334 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002335 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002336 spin_lock_irqsave(&gpio_lock, flags);
2337 }
David Brownelld2876d02008-02-04 22:28:20 -08002338done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002339 spin_unlock_irqrestore(&gpio_lock, flags);
2340 return status;
2341}
2342
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002343/*
2344 * This descriptor validation needs to be inserted verbatim into each
2345 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002346 * macro to avoid endless duplication. If the desc is NULL it is an
2347 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002348 */
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002349static int validate_desc(const struct gpio_desc *desc, const char *func)
2350{
2351 if (!desc)
2352 return 0;
2353 if (IS_ERR(desc)) {
2354 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2355 return PTR_ERR(desc);
2356 }
2357 if (!desc->gdev) {
2358 pr_warn("%s: invalid GPIO (no device)\n", func);
2359 return -EINVAL;
2360 }
2361 if (!desc->gdev->chip) {
2362 dev_warn(&desc->gdev->dev,
2363 "%s: backing chip is gone\n", func);
2364 return 0;
2365 }
2366 return 1;
2367}
2368
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002369#define VALIDATE_DESC(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002370 int __valid = validate_desc(desc, __func__); \
2371 if (__valid <= 0) \
2372 return __valid; \
2373 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002374
2375#define VALIDATE_DESC_VOID(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002376 int __valid = validate_desc(desc, __func__); \
2377 if (__valid <= 0) \
Linus Walleij54d77192016-05-30 16:48:39 +02002378 return; \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002379 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002380
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002381int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002382{
2383 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002384 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002385
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002386 VALIDATE_DESC(desc);
2387 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002388
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002389 if (try_module_get(gdev->owner)) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002390 status = gpiod_request_commit(desc, label);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002391 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002392 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002393 else
2394 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002395 }
2396
David Brownelld2876d02008-02-04 22:28:20 -08002397 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002398 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002399
David Brownelld2876d02008-02-04 22:28:20 -08002400 return status;
2401}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002402
Linus Walleijfac9d882017-09-26 20:58:28 +02002403static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002404{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002405 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002406 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002407 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002408
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002409 might_sleep();
2410
Alexandre Courbot372e7222013-02-03 01:29:29 +09002411 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002412
David Brownelld2876d02008-02-04 22:28:20 -08002413 spin_lock_irqsave(&gpio_lock, flags);
2414
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002415 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002416 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2417 if (chip->free) {
2418 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002419 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002420 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002421 spin_lock_irqsave(&gpio_lock, flags);
2422 }
David Brownelld2876d02008-02-04 22:28:20 -08002423 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002424 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002425 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302426 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302427 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002428 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002429 ret = true;
2430 }
David Brownelld2876d02008-02-04 22:28:20 -08002431
2432 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002433 return ret;
2434}
2435
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002436void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002437{
Linus Walleijfac9d882017-09-26 20:58:28 +02002438 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002439 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002440 put_device(&desc->gdev->dev);
2441 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002442 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002443 }
David Brownelld2876d02008-02-04 22:28:20 -08002444}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002445
David Brownelld2876d02008-02-04 22:28:20 -08002446/**
2447 * gpiochip_is_requested - return string iff signal was requested
2448 * @chip: controller managing the signal
2449 * @offset: of signal within controller's 0..(ngpio - 1) range
2450 *
2451 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002452 * The string returned is the label passed to gpio_request(); if none has been
2453 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002454 *
2455 * This function is for use by GPIO controller drivers. The label can
2456 * help with diagnostics, and knowing that the signal is used as a GPIO
2457 * can help avoid accidentally multiplexing it to another controller.
2458 */
2459const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2460{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002461 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002462
Dirk Behme48b59532015-08-18 18:02:32 +02002463 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002464 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002465
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002466 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002467
Alexandre Courbot372e7222013-02-03 01:29:29 +09002468 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002469 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002470 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002471}
2472EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2473
Mika Westerberg77c2d792014-03-10 14:54:50 +02002474/**
2475 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002476 * @chip: GPIO chip
2477 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002478 * @label: label for the GPIO
2479 *
2480 * Function allows GPIO chip drivers to request and use their own GPIO
2481 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2482 * function will not increase reference count of the GPIO chip module. This
2483 * allows the GPIO chip module to be unloaded as needed (we assume that the
2484 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002485 *
2486 * Returns:
2487 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2488 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002489 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002490struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2491 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002492{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002493 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2494 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002495
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002496 if (IS_ERR(desc)) {
2497 chip_err(chip, "failed to get GPIO descriptor\n");
2498 return desc;
2499 }
2500
Linus Walleijfac9d882017-09-26 20:58:28 +02002501 err = gpiod_request_commit(desc, label);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002502 if (err < 0)
2503 return ERR_PTR(err);
2504
2505 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002506}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002507EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002508
2509/**
2510 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2511 * @desc: GPIO descriptor to free
2512 *
2513 * Function frees the given GPIO requested previously with
2514 * gpiochip_request_own_desc().
2515 */
2516void gpiochip_free_own_desc(struct gpio_desc *desc)
2517{
2518 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002519 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002520}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002521EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002522
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002523/*
2524 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002525 * some cases this is done in early boot, before IRQs are enabled.
2526 *
2527 * As a rule these aren't called more than once (except for drivers
2528 * using the open-drain emulation idiom) so these are natural places
2529 * to accumulate extra debugging checks. Note that we can't (yet)
2530 * rely on gpio_request() having been called beforehand.
2531 */
2532
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002533/**
2534 * gpiod_direction_input - set the GPIO direction to input
2535 * @desc: GPIO to set to input
2536 *
2537 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2538 * be called safely on it.
2539 *
2540 * Return 0 in case of success, else an error code.
2541 */
2542int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002543{
David Brownelld2876d02008-02-04 22:28:20 -08002544 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002545 int status = -EINVAL;
2546
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002547 VALIDATE_DESC(desc);
2548 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002549
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002550 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002551 gpiod_warn(desc,
2552 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002553 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002554 return -EIO;
2555 }
2556
Alexandre Courbotd82da792014-07-22 16:17:43 +09002557 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002558 if (status == 0)
2559 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002560
Alexandre Courbot372e7222013-02-03 01:29:29 +09002561 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002562
David Brownelld2876d02008-02-04 22:28:20 -08002563 return status;
2564}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002565EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002566
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002567static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2568 enum pin_config_param mode)
2569{
2570 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2571
2572 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2573}
2574
Linus Walleijfac9d882017-09-26 20:58:28 +02002575static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002576{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002577 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002578 int val = !!value;
Linus Walleijc663e5f2016-03-22 10:51:16 +01002579 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002580
Linus Walleijc663e5f2016-03-22 10:51:16 +01002581 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002582 gpiod_warn(desc,
2583 "%s: missing set() or direction_output() operations\n",
2584 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002585 return -EIO;
2586 }
2587
Linus Walleijad177312016-11-13 23:02:44 +01002588 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002589 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002590 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002591 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002592 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2593 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002594}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002595
2596/**
2597 * gpiod_direction_output_raw - set the GPIO direction to output
2598 * @desc: GPIO to set to output
2599 * @value: initial output value of the GPIO
2600 *
2601 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2602 * be called safely on it. The initial value of the output must be specified
2603 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2604 *
2605 * Return 0 in case of success, else an error code.
2606 */
2607int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2608{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002609 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002610 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002611}
2612EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2613
2614/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302615 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002616 * @desc: GPIO to set to output
2617 * @value: initial output value of the GPIO
2618 *
2619 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2620 * be called safely on it. The initial value of the output must be specified
2621 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2622 * account.
2623 *
2624 * Return 0 in case of success, else an error code.
2625 */
2626int gpiod_direction_output(struct gpio_desc *desc, int value)
2627{
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02002628 struct gpio_chip *gc;
Linus Walleij02e47982017-09-26 21:20:23 +02002629 int ret;
2630
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002631 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002632 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2633 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002634 else
2635 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02002636
Hans Verkuil4e9439d2018-09-08 11:23:16 +02002637 /* GPIOs used for enabled IRQs shall not be set as output */
2638 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2639 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
Linus Walleij02e47982017-09-26 21:20:23 +02002640 gpiod_err(desc,
2641 "%s: tried to set a GPIO tied to an IRQ as output\n",
2642 __func__);
2643 return -EIO;
2644 }
2645
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02002646 gc = desc->gdev->chip;
Linus Walleij02e47982017-09-26 21:20:23 +02002647 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2648 /* First see if we can enable open drain in hardware */
2649 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2650 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2651 if (!ret)
2652 goto set_output_value;
2653 /* Emulate open drain by not actively driving the line high */
2654 if (value)
2655 return gpiod_direction_input(desc);
2656 }
2657 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2658 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2659 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2660 if (!ret)
2661 goto set_output_value;
2662 /* Emulate open source by not actively driving the line low */
2663 if (!value)
2664 return gpiod_direction_input(desc);
2665 } else {
2666 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2667 PIN_CONFIG_DRIVE_PUSH_PULL);
2668 }
2669
2670set_output_value:
Linus Walleijfac9d882017-09-26 20:58:28 +02002671 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002672}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002673EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002674
Felipe Balbic4b5be92010-05-26 14:42:23 -07002675/**
Thierry Reding950d55f52017-07-24 16:57:22 +02002676 * gpiod_set_debounce - sets @debounce time for a GPIO
2677 * @desc: descriptor of the GPIO for which to set debounce time
2678 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002679 *
Thierry Reding950d55f52017-07-24 16:57:22 +02002680 * Returns:
2681 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2682 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002683 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002684int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002685{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002686 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002687 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002688
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002689 VALIDATE_DESC(desc);
2690 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002691 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01002692 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002693 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01002694 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002695 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002696 }
2697
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002698 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2699 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002700}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002701EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002702
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002703/**
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302704 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2705 * @desc: descriptor of the GPIO for which to configure persistence
2706 * @transitory: True to lose state on suspend or reset, false for persistence
2707 *
2708 * Returns:
2709 * 0 on success, otherwise a negative error code.
2710 */
2711int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2712{
2713 struct gpio_chip *chip;
2714 unsigned long packed;
2715 int gpio;
2716 int rc;
2717
Vladimir Zapolskiy156dd392017-12-21 18:37:35 +02002718 VALIDATE_DESC(desc);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302719 /*
2720 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2721 * persistence state.
2722 */
2723 if (transitory)
2724 set_bit(FLAG_TRANSITORY, &desc->flags);
2725 else
2726 clear_bit(FLAG_TRANSITORY, &desc->flags);
2727
2728 /* If the driver supports it, set the persistence state now */
2729 chip = desc->gdev->chip;
2730 if (!chip->set_config)
2731 return 0;
2732
2733 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
2734 !transitory);
2735 gpio = gpio_chip_hwgpio(desc);
2736 rc = chip->set_config(chip, gpio, packed);
2737 if (rc == -ENOTSUPP) {
2738 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
2739 gpio);
2740 return 0;
2741 }
2742
2743 return rc;
2744}
2745EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2746
2747/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002748 * gpiod_is_active_low - test whether a GPIO is active-low or not
2749 * @desc: the gpio descriptor to test
2750 *
2751 * Returns 1 if the GPIO is active-low, 0 otherwise.
2752 */
2753int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002754{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002755 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002756 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002757}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002758EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002759
2760/* I/O calls are only valid after configuration completed; the relevant
2761 * "is this a valid GPIO" error checks should already have been done.
2762 *
2763 * "Get" operations are often inlinable as reading a pin value register,
2764 * and masking the relevant bit in that register.
2765 *
2766 * When "set" operations are inlinable, they involve writing that mask to
2767 * one register to set a low value, or a different register to set it high.
2768 * Otherwise locking is needed, so there may be little value to inlining.
2769 *
2770 *------------------------------------------------------------------------
2771 *
2772 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2773 * have requested the GPIO. That can include implicit requesting by
2774 * a direction setting call. Marking a gpio as requested locks its chip
2775 * in memory, guaranteeing that these table lookups need no more locking
2776 * and that gpiochip_remove() will fail.
2777 *
2778 * REVISIT when debugging, consider adding some instrumentation to ensure
2779 * that the GPIO was actually requested.
2780 */
2781
Linus Walleijfac9d882017-09-26 20:58:28 +02002782static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002783{
2784 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002785 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002786 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002787
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002788 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002789 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002790 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002791 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002792 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002793 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002794}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002795
Lukas Wunnereec1d562017-10-12 12:40:10 +02002796static int gpio_chip_get_multiple(struct gpio_chip *chip,
2797 unsigned long *mask, unsigned long *bits)
2798{
2799 if (chip->get_multiple) {
2800 return chip->get_multiple(chip, mask, bits);
2801 } else if (chip->get) {
2802 int i, value;
2803
2804 for_each_set_bit(i, mask, chip->ngpio) {
2805 value = chip->get(chip, i);
2806 if (value < 0)
2807 return value;
2808 __assign_bit(i, bits, value);
2809 }
2810 return 0;
2811 }
2812 return -EIO;
2813}
2814
2815int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2816 unsigned int array_size,
2817 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002818 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002819 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02002820{
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002821 int err, i = 0;
2822
2823 /*
2824 * Validate array_info against desc_array and its size.
2825 * It should immediately follow desc_array if both
2826 * have been obtained from the same gpiod_get_array() call.
2827 */
2828 if (array_info && array_info->desc == desc_array &&
2829 array_size <= array_info->size &&
2830 (void *)array_info == desc_array + array_info->size) {
2831 if (!can_sleep)
2832 WARN_ON(array_info->chip->can_sleep);
2833
2834 err = gpio_chip_get_multiple(array_info->chip,
2835 array_info->get_mask,
2836 value_bitmap);
2837 if (err)
2838 return err;
2839
2840 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2841 bitmap_xor(value_bitmap, value_bitmap,
2842 array_info->invert_mask, array_size);
2843
2844 if (bitmap_full(array_info->get_mask, array_size))
2845 return 0;
2846
2847 i = find_first_zero_bit(array_info->get_mask, array_size);
2848 } else {
2849 array_info = NULL;
2850 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02002851
2852 while (i < array_size) {
2853 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07002854 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
2855 unsigned long *mask, *bits;
Lukas Wunnereec1d562017-10-12 12:40:10 +02002856 int first, j, ret;
2857
Laura Abbott30277432018-05-21 10:57:07 -07002858 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
2859 mask = fastpath;
2860 } else {
2861 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
2862 sizeof(*mask),
2863 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
2864 if (!mask)
2865 return -ENOMEM;
2866 }
2867
2868 bits = mask + BITS_TO_LONGS(chip->ngpio);
2869 bitmap_zero(mask, chip->ngpio);
2870
Lukas Wunnereec1d562017-10-12 12:40:10 +02002871 if (!can_sleep)
2872 WARN_ON(chip->can_sleep);
2873
2874 /* collect all inputs belonging to the same chip */
2875 first = i;
Lukas Wunnereec1d562017-10-12 12:40:10 +02002876 do {
2877 const struct gpio_desc *desc = desc_array[i];
2878 int hwgpio = gpio_chip_hwgpio(desc);
2879
2880 __set_bit(hwgpio, mask);
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002881
2882 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02002883 i = find_next_zero_bit(array_info->get_mask,
2884 array_size, i);
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002885 else
2886 i++;
Lukas Wunnereec1d562017-10-12 12:40:10 +02002887 } while ((i < array_size) &&
2888 (desc_array[i]->gdev->chip == chip));
2889
2890 ret = gpio_chip_get_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07002891 if (ret) {
2892 if (mask != fastpath)
2893 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002894 return ret;
Laura Abbott30277432018-05-21 10:57:07 -07002895 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02002896
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002897 for (j = first; j < i; ) {
Lukas Wunnereec1d562017-10-12 12:40:10 +02002898 const struct gpio_desc *desc = desc_array[j];
2899 int hwgpio = gpio_chip_hwgpio(desc);
2900 int value = test_bit(hwgpio, bits);
2901
2902 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2903 value = !value;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002904 __assign_bit(j, value_bitmap, value);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002905 trace_gpio_value(desc_to_gpio(desc), 1, value);
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002906
2907 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02002908 j = find_next_zero_bit(array_info->get_mask, i,
2909 j);
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002910 else
2911 j++;
Lukas Wunnereec1d562017-10-12 12:40:10 +02002912 }
Laura Abbott30277432018-05-21 10:57:07 -07002913
2914 if (mask != fastpath)
2915 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002916 }
2917 return 0;
2918}
2919
David Brownelld2876d02008-02-04 22:28:20 -08002920/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002921 * gpiod_get_raw_value() - return a gpio's raw value
2922 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002923 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002924 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002925 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002926 *
2927 * This function should be called from contexts where we cannot sleep, and will
2928 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002929 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002930int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002931{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002932 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002933 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002934 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002935 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002936}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002937EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002938
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002939/**
2940 * gpiod_get_value() - return a gpio's value
2941 * @desc: gpio whose value will be returned
2942 *
2943 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002944 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002945 *
2946 * This function should be called from contexts where we cannot sleep, and will
2947 * complain if the GPIO chip functions potentially sleep.
2948 */
2949int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002950{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002951 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002952
2953 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002954 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002955 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002956
Linus Walleijfac9d882017-09-26 20:58:28 +02002957 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002958 if (value < 0)
2959 return value;
2960
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002961 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2962 value = !value;
2963
2964 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002965}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002966EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002967
Lukas Wunnereec1d562017-10-12 12:40:10 +02002968/**
2969 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002970 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02002971 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002972 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002973 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02002974 *
2975 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2976 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2977 * else an error code.
2978 *
2979 * This function should be called from contexts where we cannot sleep,
2980 * and it will complain if the GPIO chip functions potentially sleep.
2981 */
2982int gpiod_get_raw_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002983 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002984 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002985 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02002986{
2987 if (!desc_array)
2988 return -EINVAL;
2989 return gpiod_get_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002990 desc_array, array_info,
2991 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002992}
2993EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2994
2995/**
2996 * gpiod_get_array_value() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002997 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02002998 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002999 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003000 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003001 *
3002 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3003 * into account. Return 0 in case of success, else an error code.
3004 *
3005 * This function should be called from contexts where we cannot sleep,
3006 * and it will complain if the GPIO chip functions potentially sleep.
3007 */
3008int gpiod_get_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003009 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003010 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003011 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003012{
3013 if (!desc_array)
3014 return -EINVAL;
3015 return gpiod_get_array_value_complex(false, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003016 desc_array, array_info,
3017 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003018}
3019EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3020
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303021/*
Linus Walleijfac9d882017-09-26 20:58:28 +02003022 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003023 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003024 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303025 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003026static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303027{
3028 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003029 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003030 int offset = gpio_chip_hwgpio(desc);
3031
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303032 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003033 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303034 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003035 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303036 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003037 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303038 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003039 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303040 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09003041 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303042 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003043 gpiod_err(desc,
3044 "%s: Error in set_value for open drain err %d\n",
3045 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303046}
3047
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303048/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003049 * _gpio_set_open_source_value() - Set the open source gpio's value.
3050 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003051 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303052 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003053static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303054{
3055 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003056 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003057 int offset = gpio_chip_hwgpio(desc);
3058
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303059 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003060 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303061 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003062 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303063 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003064 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303065 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003066 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303067 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09003068 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303069 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003070 gpiod_err(desc,
3071 "%s: Error in set_value for open source err %d\n",
3072 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303073}
3074
Linus Walleijfac9d882017-09-26 20:58:28 +02003075static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08003076{
3077 struct gpio_chip *chip;
3078
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003079 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003080 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003081 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003082}
3083
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003084/*
3085 * set multiple outputs on the same chip;
3086 * use the chip's set_multiple function if available;
3087 * otherwise set the outputs sequentially;
3088 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3089 * defines which outputs are to be changed
3090 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3091 * defines the values the outputs specified by mask are to be set to
3092 */
3093static void gpio_chip_set_multiple(struct gpio_chip *chip,
3094 unsigned long *mask, unsigned long *bits)
3095{
3096 if (chip->set_multiple) {
3097 chip->set_multiple(chip, mask, bits);
3098 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02003099 unsigned int i;
3100
3101 /* set outputs if the corresponding mask bit is set */
3102 for_each_set_bit(i, mask, chip->ngpio)
3103 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003104 }
3105}
3106
Laura Abbott30277432018-05-21 10:57:07 -07003107int gpiod_set_array_value_complex(bool raw, bool can_sleep,
Linus Walleij44c72882016-04-24 11:36:59 +02003108 unsigned int array_size,
3109 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003110 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003111 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003112{
3113 int i = 0;
3114
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003115 /*
3116 * Validate array_info against desc_array and its size.
3117 * It should immediately follow desc_array if both
3118 * have been obtained from the same gpiod_get_array() call.
3119 */
3120 if (array_info && array_info->desc == desc_array &&
3121 array_size <= array_info->size &&
3122 (void *)array_info == desc_array + array_info->size) {
3123 if (!can_sleep)
3124 WARN_ON(array_info->chip->can_sleep);
3125
3126 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3127 bitmap_xor(value_bitmap, value_bitmap,
3128 array_info->invert_mask, array_size);
3129
3130 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3131 value_bitmap);
3132
3133 if (bitmap_full(array_info->set_mask, array_size))
3134 return 0;
3135
3136 i = find_first_zero_bit(array_info->set_mask, array_size);
3137 } else {
3138 array_info = NULL;
3139 }
3140
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003141 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003142 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07003143 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3144 unsigned long *mask, *bits;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003145 int count = 0;
3146
Laura Abbott30277432018-05-21 10:57:07 -07003147 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3148 mask = fastpath;
3149 } else {
3150 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3151 sizeof(*mask),
3152 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3153 if (!mask)
3154 return -ENOMEM;
3155 }
3156
3157 bits = mask + BITS_TO_LONGS(chip->ngpio);
3158 bitmap_zero(mask, chip->ngpio);
3159
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003160 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003161 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003162
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003163 do {
3164 struct gpio_desc *desc = desc_array[i];
3165 int hwgpio = gpio_chip_hwgpio(desc);
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003166 int value = test_bit(i, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003167
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003168 /*
3169 * Pins applicable for fast input but not for
3170 * fast output processing may have been already
3171 * inverted inside the fast path, skip them.
3172 */
3173 if (!raw && !(array_info &&
3174 test_bit(i, array_info->invert_mask)) &&
3175 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003176 value = !value;
3177 trace_gpio_value(desc_to_gpio(desc), 0, value);
3178 /*
3179 * collect all normal outputs belonging to the same chip
3180 * open drain and open source outputs are set individually
3181 */
Linus Walleij02e47982017-09-26 21:20:23 +02003182 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003183 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003184 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003185 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003186 } else {
3187 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003188 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003189 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003190 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003191 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003192 count++;
3193 }
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003194
3195 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003196 i = find_next_zero_bit(array_info->set_mask,
3197 array_size, i);
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003198 else
3199 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003200 } while ((i < array_size) &&
3201 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003202 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003203 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003204 gpio_chip_set_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07003205
3206 if (mask != fastpath)
3207 kfree(mask);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003208 }
Laura Abbott30277432018-05-21 10:57:07 -07003209 return 0;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003210}
3211
David Brownelld2876d02008-02-04 22:28:20 -08003212/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003213 * gpiod_set_raw_value() - assign a gpio's raw value
3214 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08003215 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003216 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003217 * Set the raw value of the GPIO, i.e. the value of its physical line without
3218 * regard for its ACTIVE_LOW status.
3219 *
3220 * This function should be called from contexts where we cannot sleep, and will
3221 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003222 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003223void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003224{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003225 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01003226 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003227 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003228 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003229}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003230EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003231
3232/**
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003233 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3234 * @desc: the descriptor to set the value on
3235 * @value: value to set
3236 *
3237 * This sets the value of a GPIO line backing a descriptor, applying
3238 * different semantic quirks like active low and open drain/source
3239 * handling.
3240 */
3241static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3242{
3243 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3244 value = !value;
3245 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3246 gpio_set_open_drain_value_commit(desc, value);
3247 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3248 gpio_set_open_source_value_commit(desc, value);
3249 else
3250 gpiod_set_raw_value_commit(desc, value);
3251}
3252
3253/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003254 * gpiod_set_value() - assign a gpio's value
3255 * @desc: gpio whose value will be assigned
3256 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003257 *
Linus Walleij02e47982017-09-26 21:20:23 +02003258 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3259 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003260 *
3261 * This function should be called from contexts where we cannot sleep, and will
3262 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003263 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003264void gpiod_set_value(struct gpio_desc *desc, int value)
3265{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003266 VALIDATE_DESC_VOID(desc);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003267 WARN_ON(desc->gdev->chip->can_sleep);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003268 gpiod_set_value_nocheck(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003269}
3270EXPORT_SYMBOL_GPL(gpiod_set_value);
3271
3272/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003273 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003274 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003275 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003276 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003277 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003278 *
3279 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3280 * without regard for their ACTIVE_LOW status.
3281 *
3282 * This function should be called from contexts where we cannot sleep, and will
3283 * complain if the GPIO chip functions potentially sleep.
3284 */
Laura Abbott30277432018-05-21 10:57:07 -07003285int gpiod_set_raw_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003286 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003287 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003288 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003289{
3290 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07003291 return -EINVAL;
3292 return gpiod_set_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003293 desc_array, array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003294}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003295EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003296
3297/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003298 * gpiod_set_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003299 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003300 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003301 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003302 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003303 *
3304 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3305 * into account.
3306 *
3307 * This function should be called from contexts where we cannot sleep, and will
3308 * complain if the GPIO chip functions potentially sleep.
3309 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003310void gpiod_set_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003311 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003312 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003313 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003314{
3315 if (!desc_array)
3316 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003317 gpiod_set_array_value_complex(false, false, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003318 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003319}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003320EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003321
3322/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003323 * gpiod_cansleep() - report whether gpio value access may sleep
3324 * @desc: gpio to check
3325 *
3326 */
3327int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003328{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003329 VALIDATE_DESC(desc);
3330 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003331}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003332EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003333
David Brownell0f6d5042008-10-15 22:03:14 -07003334/**
Linus Walleij90b39402e2018-06-01 13:21:27 +02003335 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3336 * @desc: gpio to set the consumer name on
3337 * @name: the new consumer name
3338 */
3339void gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3340{
3341 VALIDATE_DESC_VOID(desc);
3342 /* Just overwrite whatever the previous name was */
3343 desc->label = name;
3344}
3345EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3346
3347/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003348 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3349 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07003350 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003351 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3352 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07003353 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003354int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07003355{
Linus Walleij4c37ce82016-05-02 13:13:10 +02003356 struct gpio_chip *chip;
3357 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07003358
Linus Walleij79bb71b2016-06-15 22:57:38 +02003359 /*
3360 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3361 * requires this function to not return zero on an invalid descriptor
3362 * but rather a negative error number.
3363 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003364 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003365 return -EINVAL;
3366
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003367 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003368 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003369 if (chip->to_irq) {
3370 int retirq = chip->to_irq(chip, offset);
3371
3372 /* Zero means NO_IRQ */
3373 if (!retirq)
3374 return -ENXIO;
3375
3376 return retirq;
3377 }
3378 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003379}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003380EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003381
Linus Walleijd468bf92013-09-24 11:54:38 +02003382/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003383 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003384 * @chip: the chip the GPIO to lock belongs to
3385 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003386 *
3387 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003388 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003389 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003390int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003391{
Linus Walleij9c102802016-05-25 10:56:03 +02003392 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003393
Linus Walleij9c102802016-05-25 10:56:03 +02003394 desc = gpiochip_get_desc(chip, offset);
3395 if (IS_ERR(desc))
3396 return PTR_ERR(desc);
3397
Linus Walleij60f83392016-11-12 15:01:09 +01003398 /*
3399 * If it's fast: flush the direction setting if something changed
3400 * behind our back
3401 */
3402 if (!chip->can_sleep && chip->get_direction) {
Andy Shevchenko80956792018-07-09 21:47:21 +03003403 int dir = gpiod_get_direction(desc);
Linus Walleij9c102802016-05-25 10:56:03 +02003404
Andy Shevchenko36b31272018-07-03 03:38:31 +03003405 if (dir < 0) {
3406 chip_err(chip, "%s: cannot get GPIO direction\n",
3407 __func__);
3408 return dir;
3409 }
Linus Walleij9c102802016-05-25 10:56:03 +02003410 }
3411
3412 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003413 chip_err(chip,
Andy Shevchenkob1911712018-07-03 03:39:03 +03003414 "%s: tried to flag a GPIO set as output for IRQ\n",
3415 __func__);
Linus Walleijd468bf92013-09-24 11:54:38 +02003416 return -EIO;
3417 }
3418
Linus Walleij9c102802016-05-25 10:56:03 +02003419 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003420 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003421
3422 /*
3423 * If the consumer has not set up a label (such as when the
3424 * IRQ is referenced from .to_irq()) we set up a label here
3425 * so it is clear this is used as an interrupt.
3426 */
3427 if (!desc->label)
3428 desc_set_label(desc, "interrupt");
3429
Linus Walleijd468bf92013-09-24 11:54:38 +02003430 return 0;
3431}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003432EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003433
Linus Walleijd468bf92013-09-24 11:54:38 +02003434/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003435 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003436 * @chip: the chip the GPIO to lock belongs to
3437 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003438 *
3439 * This is used directly by GPIO drivers that want to indicate
3440 * that a certain GPIO is no longer used exclusively for IRQ.
3441 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003442void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003443{
Linus Walleij3940c342016-11-14 00:09:07 +01003444 struct gpio_desc *desc;
3445
3446 desc = gpiochip_get_desc(chip, offset);
3447 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003448 return;
3449
Linus Walleij3940c342016-11-14 00:09:07 +01003450 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003451 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003452
3453 /* If we only had this marking, erase it */
3454 if (desc->label && !strcmp(desc->label, "interrupt"))
3455 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003456}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003457EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003458
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003459void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset)
3460{
3461 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
3462
3463 if (!IS_ERR(desc) &&
3464 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3465 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3466}
3467EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3468
3469void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset)
3470{
3471 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
3472
3473 if (!IS_ERR(desc) &&
3474 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3475 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags));
3476 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3477 }
3478}
3479EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3480
Linus Walleij6cee3822016-02-11 20:16:45 +01003481bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3482{
3483 if (offset >= chip->ngpio)
3484 return false;
3485
3486 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3487}
3488EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3489
Hans Verkuil4e6b8232018-09-08 11:23:14 +02003490int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset)
3491{
3492 int ret;
3493
3494 if (!try_module_get(chip->gpiodev->owner))
3495 return -ENODEV;
3496
3497 ret = gpiochip_lock_as_irq(chip, offset);
3498 if (ret) {
3499 chip_err(chip, "unable to lock HW IRQ %u for IRQ\n", offset);
3500 module_put(chip->gpiodev->owner);
3501 return ret;
3502 }
3503 return 0;
3504}
3505EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3506
3507void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset)
3508{
3509 gpiochip_unlock_as_irq(chip, offset);
3510 module_put(chip->gpiodev->owner);
3511}
3512EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3513
Linus Walleij143b65d2016-02-16 15:41:42 +01003514bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3515{
3516 if (offset >= chip->ngpio)
3517 return false;
3518
3519 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3520}
3521EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3522
3523bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3524{
3525 if (offset >= chip->ngpio)
3526 return false;
3527
3528 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3529}
3530EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3531
Charles Keepax05f479b2017-05-23 15:47:29 +01003532bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3533{
3534 if (offset >= chip->ngpio)
3535 return false;
3536
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303537 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01003538}
3539EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3540
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003541/**
3542 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3543 * @desc: gpio whose value will be returned
3544 *
3545 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003546 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003547 *
3548 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003549 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003550int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003551{
David Brownelld2876d02008-02-04 22:28:20 -08003552 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003553 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003554 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08003555}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003556EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003557
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003558/**
3559 * gpiod_get_value_cansleep() - return a gpio's value
3560 * @desc: gpio whose value will be returned
3561 *
3562 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003563 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003564 *
3565 * This function is to be called from contexts that can sleep.
3566 */
3567int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003568{
David Brownelld2876d02008-02-04 22:28:20 -08003569 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003570
3571 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003572 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003573 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003574 if (value < 0)
3575 return value;
3576
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003577 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3578 value = !value;
3579
David Brownelld2876d02008-02-04 22:28:20 -08003580 return value;
3581}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003582EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003583
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003584/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02003585 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003586 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003587 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003588 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003589 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003590 *
3591 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3592 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3593 * else an error code.
3594 *
3595 * This function is to be called from contexts that can sleep.
3596 */
3597int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3598 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003599 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003600 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003601{
3602 might_sleep_if(extra_checks);
3603 if (!desc_array)
3604 return -EINVAL;
3605 return gpiod_get_array_value_complex(true, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003606 desc_array, array_info,
3607 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003608}
3609EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3610
3611/**
3612 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003613 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003614 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003615 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003616 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003617 *
3618 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3619 * into account. Return 0 in case of success, else an error code.
3620 *
3621 * This function is to be called from contexts that can sleep.
3622 */
3623int gpiod_get_array_value_cansleep(unsigned int array_size,
3624 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003625 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003626 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003627{
3628 might_sleep_if(extra_checks);
3629 if (!desc_array)
3630 return -EINVAL;
3631 return gpiod_get_array_value_complex(false, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003632 desc_array, array_info,
3633 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003634}
3635EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3636
3637/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003638 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3639 * @desc: gpio whose value will be assigned
3640 * @value: value to assign
3641 *
3642 * Set the raw value of the GPIO, i.e. the value of its physical line without
3643 * regard for its ACTIVE_LOW status.
3644 *
3645 * This function is to be called from contexts that can sleep.
3646 */
3647void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003648{
David Brownelld2876d02008-02-04 22:28:20 -08003649 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003650 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003651 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003652}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003653EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003654
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003655/**
3656 * gpiod_set_value_cansleep() - assign a gpio's value
3657 * @desc: gpio whose value will be assigned
3658 * @value: value to assign
3659 *
3660 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3661 * account
3662 *
3663 * This function is to be called from contexts that can sleep.
3664 */
3665void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003666{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003667 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003668 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003669 gpiod_set_value_nocheck(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003670}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003671EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003672
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003673/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003674 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003675 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003676 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003677 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003678 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003679 *
3680 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3681 * without regard for their ACTIVE_LOW status.
3682 *
3683 * This function is to be called from contexts that can sleep.
3684 */
Laura Abbott30277432018-05-21 10:57:07 -07003685int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003686 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003687 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003688 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003689{
3690 might_sleep_if(extra_checks);
3691 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07003692 return -EINVAL;
3693 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003694 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003695}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003696EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003697
3698/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003699 * gpiod_add_lookup_tables() - register GPIO device consumers
3700 * @tables: list of tables of consumers to register
3701 * @n: number of tables in the list
3702 */
3703void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3704{
3705 unsigned int i;
3706
3707 mutex_lock(&gpio_lookup_lock);
3708
3709 for (i = 0; i < n; i++)
3710 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3711
3712 mutex_unlock(&gpio_lookup_lock);
3713}
3714
3715/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003716 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003717 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003718 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003719 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003720 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003721 *
3722 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3723 * into account.
3724 *
3725 * This function is to be called from contexts that can sleep.
3726 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003727void gpiod_set_array_value_cansleep(unsigned int array_size,
3728 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003729 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003730 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003731{
3732 might_sleep_if(extra_checks);
3733 if (!desc_array)
3734 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003735 gpiod_set_array_value_complex(false, true, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003736 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003737}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003738EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003739
3740/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003741 * gpiod_add_lookup_table() - register GPIO device consumers
3742 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003743 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003744void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003745{
3746 mutex_lock(&gpio_lookup_lock);
3747
Alexandre Courbotad824782013-12-03 12:20:11 +09003748 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003749
3750 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003751}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003752EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003753
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303754/**
3755 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3756 * @table: table of consumers to unregister
3757 */
3758void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3759{
3760 mutex_lock(&gpio_lookup_lock);
3761
3762 list_del(&table->list);
3763
3764 mutex_unlock(&gpio_lookup_lock);
3765}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003766EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303767
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02003768/**
3769 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3770 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3771 */
3772void gpiod_add_hogs(struct gpiod_hog *hogs)
3773{
3774 struct gpio_chip *chip;
3775 struct gpiod_hog *hog;
3776
3777 mutex_lock(&gpio_machine_hogs_mutex);
3778
3779 for (hog = &hogs[0]; hog->chip_label; hog++) {
3780 list_add_tail(&hog->list, &gpio_machine_hogs);
3781
3782 /*
3783 * The chip may have been registered earlier, so check if it
3784 * exists and, if so, try to hog the line now.
3785 */
3786 chip = find_chip_by_name(hog->chip_label);
3787 if (chip)
3788 gpiochip_machine_hog(chip, hog);
3789 }
3790
3791 mutex_unlock(&gpio_machine_hogs_mutex);
3792}
3793EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3794
Alexandre Courbotad824782013-12-03 12:20:11 +09003795static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3796{
3797 const char *dev_id = dev ? dev_name(dev) : NULL;
3798 struct gpiod_lookup_table *table;
3799
3800 mutex_lock(&gpio_lookup_lock);
3801
3802 list_for_each_entry(table, &gpio_lookup_list, list) {
3803 if (table->dev_id && dev_id) {
3804 /*
3805 * Valid strings on both ends, must be identical to have
3806 * a match
3807 */
3808 if (!strcmp(table->dev_id, dev_id))
3809 goto found;
3810 } else {
3811 /*
3812 * One of the pointers is NULL, so both must be to have
3813 * a match
3814 */
3815 if (dev_id == table->dev_id)
3816 goto found;
3817 }
3818 }
3819 table = NULL;
3820
3821found:
3822 mutex_unlock(&gpio_lookup_lock);
3823 return table;
3824}
3825
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003826static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003827 unsigned int idx,
3828 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003829{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003830 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003831 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003832 struct gpiod_lookup *p;
3833
Alexandre Courbotad824782013-12-03 12:20:11 +09003834 table = gpiod_find_lookup_table(dev);
3835 if (!table)
3836 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003837
Alexandre Courbotad824782013-12-03 12:20:11 +09003838 for (p = &table->table[0]; p->chip_label; p++) {
3839 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003840
Alexandre Courbotad824782013-12-03 12:20:11 +09003841 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003842 if (p->idx != idx)
3843 continue;
3844
Alexandre Courbotad824782013-12-03 12:20:11 +09003845 /* If the lookup entry has a con_id, require exact match */
3846 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3847 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003848
Alexandre Courbotad824782013-12-03 12:20:11 +09003849 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003850
Alexandre Courbotad824782013-12-03 12:20:11 +09003851 if (!chip) {
Janusz Krzysztofik8853daf2018-07-04 00:18:19 +02003852 /*
3853 * As the lookup table indicates a chip with
3854 * p->chip_label should exist, assume it may
3855 * still appear later and let the interested
3856 * consumer be probed again or let the Deferred
3857 * Probe infrastructure handle the error.
3858 */
3859 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3860 p->chip_label);
3861 return ERR_PTR(-EPROBE_DEFER);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003862 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003863
Alexandre Courbotad824782013-12-03 12:20:11 +09003864 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003865 dev_err(dev,
3866 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3867 idx, chip->ngpio, chip->label);
3868 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003869 }
3870
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003871 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003872 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003873
3874 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003875 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003876
3877 return desc;
3878}
3879
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003880static int dt_gpio_count(struct device *dev, const char *con_id)
3881{
3882 int ret;
3883 char propname[32];
3884 unsigned int i;
3885
3886 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3887 if (con_id)
3888 snprintf(propname, sizeof(propname), "%s-%s",
3889 con_id, gpio_suffixes[i]);
3890 else
3891 snprintf(propname, sizeof(propname), "%s",
3892 gpio_suffixes[i]);
3893
3894 ret = of_gpio_named_count(dev->of_node, propname);
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003895 if (ret > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003896 break;
3897 }
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003898 return ret ? ret : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003899}
3900
3901static int platform_gpio_count(struct device *dev, const char *con_id)
3902{
3903 struct gpiod_lookup_table *table;
3904 struct gpiod_lookup *p;
3905 unsigned int count = 0;
3906
3907 table = gpiod_find_lookup_table(dev);
3908 if (!table)
3909 return -ENOENT;
3910
3911 for (p = &table->table[0]; p->chip_label; p++) {
3912 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3913 (!con_id && !p->con_id))
3914 count++;
3915 }
3916 if (!count)
3917 return -ENOENT;
3918
3919 return count;
3920}
3921
3922/**
3923 * gpiod_count - return the number of GPIOs associated with a device / function
3924 * or -ENOENT if no GPIO has been assigned to the requested function
3925 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3926 * @con_id: function within the GPIO consumer
3927 */
3928int gpiod_count(struct device *dev, const char *con_id)
3929{
3930 int count = -ENOENT;
3931
3932 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3933 count = dt_gpio_count(dev, con_id);
3934 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3935 count = acpi_gpio_count(dev, con_id);
3936
3937 if (count < 0)
3938 count = platform_gpio_count(dev, con_id);
3939
3940 return count;
3941}
3942EXPORT_SYMBOL_GPL(gpiod_count);
3943
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003944/**
Thierry Reding08791622014-04-25 16:54:22 +02003945 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003946 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003947 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003948 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003949 *
3950 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003951 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003952 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003953 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003954struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003955 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003956{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003957 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003958}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003959EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003960
3961/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003962 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3963 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3964 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003965 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003966 *
3967 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3968 * the requested function it will return NULL. This is convenient for drivers
3969 * that need to handle optional GPIOs.
3970 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003971struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003972 const char *con_id,
3973 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003974{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003975 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003976}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003977EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003978
Benoit Parrotf625d462015-02-02 11:44:44 -06003979
3980/**
3981 * gpiod_configure_flags - helper function to configure a given GPIO
3982 * @desc: gpio whose value will be assigned
3983 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003984 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3985 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003986 * @dflags: gpiod_flags - optional GPIO initialization flags
3987 *
3988 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3989 * requested function and/or index, or another IS_ERR() code if an error
3990 * occurred while trying to acquire the GPIO.
3991 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03003992int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003993 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003994{
3995 int status;
3996
Johan Hovold85b03b32016-07-03 18:32:05 +02003997 if (lflags & GPIO_ACTIVE_LOW)
3998 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003999
Johan Hovold85b03b32016-07-03 18:32:05 +02004000 if (lflags & GPIO_OPEN_DRAIN)
4001 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02004002 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4003 /*
4004 * This enforces open drain mode from the consumer side.
4005 * This is necessary for some busses like I2C, but the lookup
4006 * should *REALLY* have specified them as open drain in the
4007 * first place, so print a little warning here.
4008 */
4009 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4010 gpiod_warn(desc,
4011 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4012 }
4013
Johan Hovold85b03b32016-07-03 18:32:05 +02004014 if (lflags & GPIO_OPEN_SOURCE)
4015 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304016
4017 status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4018 if (status < 0)
4019 return status;
Johan Hovold85b03b32016-07-03 18:32:05 +02004020
Benoit Parrotf625d462015-02-02 11:44:44 -06004021 /* No particular flag request, return here... */
4022 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4023 pr_debug("no flags found for %s\n", con_id);
4024 return 0;
4025 }
4026
4027 /* Process flags */
4028 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
4029 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01004030 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06004031 else
4032 status = gpiod_direction_input(desc);
4033
4034 return status;
4035}
4036
Thierry Reding29a1f2332014-04-25 17:10:06 +02004037/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004038 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02004039 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004040 * @con_id: function within the GPIO consumer
4041 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004042 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004043 *
4044 * This variant of gpiod_get() allows to access GPIOs other than the first
4045 * defined one for functions that define several GPIOs.
4046 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004047 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4048 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07004049 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004050 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004051struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004052 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004053 unsigned int idx,
4054 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004055{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004056 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004057 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004058 enum gpio_lookup_flags lookupflags = 0;
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004059 /* Maybe we have a device name, maybe not */
4060 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004061
4062 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
4063
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004064 if (dev) {
4065 /* Using device tree? */
4066 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
4067 dev_dbg(dev, "using device tree for GPIO lookup\n");
4068 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
4069 } else if (ACPI_COMPANION(dev)) {
4070 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03004071 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004072 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004073 }
4074
4075 /*
4076 * Either we are not using DT or ACPI, or their lookup did not return
4077 * a result. In that case, use platform lookup as a fallback.
4078 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004079 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04004080 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004081 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004082 }
4083
4084 if (IS_ERR(desc)) {
Wang Dongsheng9d5a1f22018-02-27 00:12:13 -08004085 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004086 return desc;
4087 }
4088
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004089 /*
4090 * If a connection label was passed use that, else attempt to use
4091 * the device name as label
4092 */
4093 status = gpiod_request(desc, con_id ? con_id : devname);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004094 if (status < 0)
4095 return ERR_PTR(status);
4096
Johan Hovold85b03b32016-07-03 18:32:05 +02004097 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004098 if (status < 0) {
4099 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
4100 gpiod_put(desc);
4101 return ERR_PTR(status);
4102 }
4103
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004104 return desc;
4105}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004106EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004107
4108/**
Linus Walleij6392cca2017-12-29 02:07:54 +01004109 * gpiod_get_from_of_node() - obtain a GPIO from an OF node
4110 * @node: handle of the OF node
4111 * @propname: name of the DT property representing the GPIO
4112 * @index: index of the GPIO to obtain for the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004113 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02004114 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02004115 *
Thierry Reding950d55f52017-07-24 16:57:22 +02004116 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02004117 * On successful request the GPIO pin is configured in accordance with
Linus Walleij6392cca2017-12-29 02:07:54 +01004118 * provided @dflags. If the node does not have the requested GPIO
4119 * property, NULL is returned.
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004120 *
Mika Westerberg40b73182014-10-21 13:33:59 +02004121 * In case of error an ERR_PTR() is returned.
4122 */
Linus Walleij92542ed2017-12-29 22:52:02 +01004123struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
4124 const char *propname, int index,
4125 enum gpiod_flags dflags,
4126 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02004127{
Colin Ian King40a3c9d2018-01-16 11:56:11 +00004128 struct gpio_desc *desc;
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004129 unsigned long lflags = 0;
Linus Walleij6392cca2017-12-29 02:07:54 +01004130 enum of_gpio_flags flags;
Mika Westerberg40b73182014-10-21 13:33:59 +02004131 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004132 bool single_ended = false;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05304133 bool open_drain = false;
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304134 bool transitory = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02004135 int ret;
4136
Linus Walleij6392cca2017-12-29 02:07:54 +01004137 desc = of_get_named_gpiod_flags(node, propname,
4138 index, &flags);
Mika Westerberg40b73182014-10-21 13:33:59 +02004139
Linus Walleij6392cca2017-12-29 02:07:54 +01004140 if (!desc || IS_ERR(desc)) {
4141 /* If it is not there, just return NULL */
4142 if (PTR_ERR(desc) == -ENOENT)
4143 return NULL;
4144 return desc;
Mika Westerberg40b73182014-10-21 13:33:59 +02004145 }
4146
Linus Walleij6392cca2017-12-29 02:07:54 +01004147 active_low = flags & OF_GPIO_ACTIVE_LOW;
4148 single_ended = flags & OF_GPIO_SINGLE_ENDED;
4149 open_drain = flags & OF_GPIO_OPEN_DRAIN;
4150 transitory = flags & OF_GPIO_TRANSITORY;
Mika Westerberg40b73182014-10-21 13:33:59 +02004151
Alexander Steinb2987d72017-01-12 17:39:24 +01004152 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02004153 if (ret)
4154 return ERR_PTR(ret);
4155
Mika Westerberg40b73182014-10-21 13:33:59 +02004156 if (active_low)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004157 lflags |= GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02004158
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004159 if (single_ended) {
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05304160 if (open_drain)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004161 lflags |= GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004162 else
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004163 lflags |= GPIO_OPEN_SOURCE;
4164 }
4165
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304166 if (transitory)
4167 lflags |= GPIO_TRANSITORY;
4168
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004169 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4170 if (ret < 0) {
4171 gpiod_put(desc);
4172 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004173 }
4174
Mika Westerberg40b73182014-10-21 13:33:59 +02004175 return desc;
4176}
Linus Walleij92542ed2017-12-29 22:52:02 +01004177EXPORT_SYMBOL(gpiod_get_from_of_node);
Linus Walleij6392cca2017-12-29 02:07:54 +01004178
4179/**
Mika Westerberg40b73182014-10-21 13:33:59 +02004180 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4181 * @fwnode: handle of the firmware node
4182 * @propname: name of the firmware property representing the GPIO
Linus Walleij6392cca2017-12-29 02:07:54 +01004183 * @index: index of the GPIO to obtain for the consumer
Mika Westerberg40b73182014-10-21 13:33:59 +02004184 * @dflags: GPIO initialization flags
4185 * @label: label to attach to the requested GPIO
4186 *
4187 * This function can be used for drivers that get their configuration
Linus Walleij6392cca2017-12-29 02:07:54 +01004188 * from opaque firmware.
Thierry Reding29a1f2332014-04-25 17:10:06 +02004189 *
Linus Walleij6392cca2017-12-29 02:07:54 +01004190 * The function properly finds the corresponding GPIO using whatever is the
Thierry Reding29a1f2332014-04-25 17:10:06 +02004191 * underlying firmware interface and then makes sure that the GPIO
4192 * descriptor is requested before it is returned to the caller.
4193 *
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004194 * Returns:
Thierry Reding29a1f2332014-04-25 17:10:06 +02004195 * On successful request the GPIO pin is configured in accordance with
4196 * provided @dflags.
4197 *
4198 * In case of error an ERR_PTR() is returned.
4199 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004200struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Thierry Reding29a1f2332014-04-25 17:10:06 +02004201 const char *propname, int index,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004202 enum gpiod_flags dflags,
4203 const char *label)
Thierry Reding29a1f2332014-04-25 17:10:06 +02004204{
4205 struct gpio_desc *desc = ERR_PTR(-ENODEV);
4206 unsigned long lflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004207 int ret;
4208
David Brownelld2876d02008-02-04 22:28:20 -08004209 if (!fwnode)
David Brownelld2876d02008-02-04 22:28:20 -08004210 return ERR_PTR(-EINVAL);
4211
4212 if (is_of_node(fwnode)) {
Linus Walleij6392cca2017-12-29 02:07:54 +01004213 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4214 propname, index,
4215 dflags,
4216 label);
4217 return desc;
David Brownelld2876d02008-02-04 22:28:20 -08004218 } else if (is_acpi_node(fwnode)) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09004219 struct acpi_gpio_info info;
David Brownelld2876d02008-02-04 22:28:20 -08004220
Linus Walleijd468bf92013-09-24 11:54:38 +02004221 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Linus Walleij6392cca2017-12-29 02:07:54 +01004222 if (IS_ERR(desc))
4223 return desc;
4224
4225 acpi_gpio_update_gpiod_flags(&dflags, &info);
4226
4227 if (info.polarity == GPIO_ACTIVE_LOW)
4228 lflags |= GPIO_ACTIVE_LOW;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004229 }
4230
Linus Walleij6392cca2017-12-29 02:07:54 +01004231 /* Currently only ACPI takes this path */
Thierry Redingf9c4a312012-04-12 13:26:01 +02004232 ret = gpiod_request(desc, label);
4233 if (ret)
4234 return ERR_PTR(ret);
Linus Walleijd468bf92013-09-24 11:54:38 +02004235
Thierry Redingf9c4a312012-04-12 13:26:01 +02004236 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4237 if (ret < 0) {
4238 gpiod_put(desc);
4239 return ERR_PTR(ret);
4240 }
4241
4242 return desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02004243}
4244EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
David Brownelld2876d02008-02-04 22:28:20 -08004245
4246/**
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07004247 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
David Brownelld8f388d82008-07-25 01:46:07 -07004248 * function
Linus Walleijd468bf92013-09-24 11:54:38 +02004249 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4250 * @con_id: function within the GPIO consumer
David Brownelld2876d02008-02-04 22:28:20 -08004251 * @index: index of the GPIO to obtain in the consumer
4252 * @flags: optional GPIO initialization flags
4253 *
4254 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4255 * specified index was assigned to the requested function it will return NULL.
4256 * This is convenient for drivers that need to handle optional GPIOs.
Grant Likely362432a2013-02-09 09:41:49 +00004257 */
David Brownelld8f388d82008-07-25 01:46:07 -07004258struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004259 const char *con_id,
Thierry Redingf9c4a312012-04-12 13:26:01 +02004260 unsigned int index,
4261 enum gpiod_flags flags)
4262{
Grant Likely362432a2013-02-09 09:41:49 +00004263 struct gpio_desc *desc;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004264
Grant Likely362432a2013-02-09 09:41:49 +00004265 desc = gpiod_get_index(dev, con_id, index, flags);
4266 if (IS_ERR(desc)) {
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004267 if (PTR_ERR(desc) == -ENOENT)
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004268 return NULL;
David Brownelld2876d02008-02-04 22:28:20 -08004269 }
4270
Benoit Parrotf625d462015-02-02 11:44:44 -06004271 return desc;
4272}
4273EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4274
4275/**
4276 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4277 * @desc: gpio whose value will be assigned
4278 * @name: gpio line name
4279 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
4280 * of_get_gpio_hog()
4281 * @dflags: gpiod_flags - optional GPIO initialization flags
4282 */
4283int gpiod_hog(struct gpio_desc *desc, const char *name,
4284 unsigned long lflags, enum gpiod_flags dflags)
4285{
4286 struct gpio_chip *chip;
4287 struct gpio_desc *local_desc;
4288 int hwnum;
4289 int status;
4290
Laxman Dewanganc31a5712016-03-11 19:13:21 +05304291 chip = gpiod_to_chip(desc);
4292 hwnum = gpio_chip_hwgpio(desc);
4293
4294 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
Benoit Parrotf625d462015-02-02 11:44:44 -06004295 if (IS_ERR(local_desc)) {
4296 status = PTR_ERR(local_desc);
Johan Hovold85b03b32016-07-03 18:32:05 +02004297 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
Benoit Parrotf625d462015-02-02 11:44:44 -06004298 name, chip->label, hwnum, status);
Laxman Dewanganc31a5712016-03-11 19:13:21 +05304299 return status;
4300 }
Benoit Parrotf625d462015-02-02 11:44:44 -06004301
4302 status = gpiod_configure_flags(desc, name, lflags, dflags);
4303 if (status < 0) {
4304 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
4305 name, chip->label, hwnum, status);
4306 gpiochip_free_own_desc(desc);
4307 return status;
4308 }
4309
4310 /* Mark GPIO as hogged so it can be identified and removed later */
4311 set_bit(FLAG_IS_HOGGED, &desc->flags);
4312
4313 pr_info("GPIO line %d (%s) hogged as %s%s\n",
4314 desc_to_gpio(desc), name,
4315 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4316 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
4317 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
4318
4319 return 0;
4320}
4321
4322/**
4323 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4324 * @chip: gpio chip to act on
4325 *
4326 * This is only used by of_gpiochip_remove to free hogged gpios
4327 */
Linus Walleij1c3cdb12016-02-09 13:51:59 +01004328static void gpiochip_free_hogs(struct gpio_chip *chip)
4329{
Benoit Parrotf625d462015-02-02 11:44:44 -06004330 int id;
4331
4332 for (id = 0; id < chip->ngpio; id++) {
4333 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004334 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
4335 }
4336}
4337
4338/**
4339 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4340 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4341 * @con_id: function within the GPIO consumer
4342 * @flags: optional GPIO initialization flags
4343 *
4344 * This function acquires all the GPIOs defined under a given function.
4345 *
4346 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4347 * no GPIO has been assigned to the requested function, or another IS_ERR()
4348 * code if an error occurred while trying to acquire the GPIOs.
4349 */
4350struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4351 const char *con_id,
4352 enum gpiod_flags flags)
4353{
4354 struct gpio_desc *desc;
4355 struct gpio_descs *descs;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004356 struct gpio_array *array_info = NULL;
4357 struct gpio_chip *chip;
4358 int count, bitmap_size;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004359
4360 count = gpiod_count(dev, con_id);
4361 if (count < 0)
4362 return ERR_PTR(count);
4363
Kees Cookacafe7e2018-05-08 13:45:50 -07004364 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004365 if (!descs)
4366 return ERR_PTR(-ENOMEM);
4367
4368 for (descs->ndescs = 0; descs->ndescs < count; ) {
4369 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4370 if (IS_ERR(desc)) {
4371 gpiod_put_array(descs);
4372 return ERR_CAST(desc);
4373 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004374
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004375 descs->desc[descs->ndescs] = desc;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004376
4377 chip = gpiod_to_chip(desc);
4378 /*
4379 * Select a chip of first array member
4380 * whose index matches its pin hardware number
4381 * as a candidate for fast bitmap processing.
4382 */
4383 if (!array_info && gpio_chip_hwgpio(desc) == descs->ndescs) {
4384 struct gpio_descs *array;
4385
4386 bitmap_size = BITS_TO_LONGS(chip->ngpio > count ?
4387 chip->ngpio : count);
4388
4389 array = kzalloc(struct_size(descs, desc, count) +
4390 struct_size(array_info, invert_mask,
4391 3 * bitmap_size), GFP_KERNEL);
4392 if (!array) {
4393 gpiod_put_array(descs);
4394 return ERR_PTR(-ENOMEM);
4395 }
4396
4397 memcpy(array, descs,
4398 struct_size(descs, desc, descs->ndescs + 1));
4399 kfree(descs);
4400
4401 descs = array;
4402 array_info = (void *)(descs->desc + count);
4403 array_info->get_mask = array_info->invert_mask +
4404 bitmap_size;
4405 array_info->set_mask = array_info->get_mask +
4406 bitmap_size;
4407
4408 array_info->desc = descs->desc;
4409 array_info->size = count;
4410 array_info->chip = chip;
4411 bitmap_set(array_info->get_mask, descs->ndescs,
4412 count - descs->ndescs);
4413 bitmap_set(array_info->set_mask, descs->ndescs,
4414 count - descs->ndescs);
4415 descs->info = array_info;
4416 }
4417 /*
4418 * Unmark members which don't qualify for fast bitmap
4419 * processing (different chip, not in hardware order)
4420 */
4421 if (array_info && (chip != array_info->chip ||
4422 gpio_chip_hwgpio(desc) != descs->ndescs)) {
4423 __clear_bit(descs->ndescs, array_info->get_mask);
4424 __clear_bit(descs->ndescs, array_info->set_mask);
4425 } else if (array_info) {
4426 /* Exclude open drain or open source from fast output */
4427 if (gpiochip_line_is_open_drain(chip, descs->ndescs) ||
4428 gpiochip_line_is_open_source(chip, descs->ndescs))
4429 __clear_bit(descs->ndescs,
4430 array_info->set_mask);
4431 /* Identify 'fast' pins which require invertion */
4432 if (gpiod_is_active_low(desc))
4433 __set_bit(descs->ndescs,
4434 array_info->invert_mask);
4435 }
4436
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004437 descs->ndescs++;
4438 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004439 if (array_info)
4440 dev_dbg(dev,
4441 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4442 array_info->chip->label, array_info->size,
4443 *array_info->get_mask, *array_info->set_mask,
4444 *array_info->invert_mask);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004445 return descs;
4446}
4447EXPORT_SYMBOL_GPL(gpiod_get_array);
4448
4449/**
4450 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4451 * function
4452 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4453 * @con_id: function within the GPIO consumer
4454 * @flags: optional GPIO initialization flags
4455 *
4456 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4457 * assigned to the requested function it will return NULL.
4458 */
4459struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4460 const char *con_id,
4461 enum gpiod_flags flags)
4462{
4463 struct gpio_descs *descs;
4464
4465 descs = gpiod_get_array(dev, con_id, flags);
4466 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
4467 return NULL;
4468
4469 return descs;
4470}
4471EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4472
4473/**
David Brownelld2876d02008-02-04 22:28:20 -08004474 * gpiod_put - dispose of a GPIO descriptor
4475 * @desc: GPIO descriptor to dispose of
4476 *
4477 * No descriptor can be used after gpiod_put() has been called on it.
4478 */
4479void gpiod_put(struct gpio_desc *desc)
4480{
4481 gpiod_free(desc);
4482}
4483EXPORT_SYMBOL_GPL(gpiod_put);
4484
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004485/**
4486 * gpiod_put_array - dispose of multiple GPIO descriptors
4487 * @descs: struct gpio_descs containing an array of descriptors
4488 */
4489void gpiod_put_array(struct gpio_descs *descs)
4490{
4491 unsigned int i;
4492
4493 for (i = 0; i < descs->ndescs; i++)
4494 gpiod_put(descs->desc[i]);
4495
4496 kfree(descs);
4497}
4498EXPORT_SYMBOL_GPL(gpiod_put_array);
4499
Linus Walleij3c702e92015-10-21 15:29:53 +02004500static int __init gpiolib_dev_init(void)
4501{
4502 int ret;
4503
4504 /* Register GPIO sysfs bus */
Andy Shevchenkob1911712018-07-03 03:39:03 +03004505 ret = bus_register(&gpio_bus_type);
Linus Walleij3c702e92015-10-21 15:29:53 +02004506 if (ret < 0) {
4507 pr_err("gpiolib: could not register GPIO bus type\n");
4508 return ret;
4509 }
4510
4511 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
4512 if (ret < 0) {
4513 pr_err("gpiolib: failed to allocate char dev region\n");
4514 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07004515 } else {
4516 gpiolib_initialized = true;
4517 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02004518 }
4519 return ret;
4520}
4521core_initcall(gpiolib_dev_init);
4522
David Brownelld2876d02008-02-04 22:28:20 -08004523#ifdef CONFIG_DEBUG_FS
4524
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004525static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08004526{
4527 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004528 struct gpio_chip *chip = gdev->chip;
4529 unsigned gpio = gdev->base;
4530 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08004531 int is_out;
4532 int is_irq;
4533
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004534 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02004535 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4536 if (gdesc->name) {
4537 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4538 gpio, gdesc->name);
4539 }
David Brownelld2876d02008-02-04 22:28:20 -08004540 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02004541 }
David Brownelld2876d02008-02-04 22:28:20 -08004542
4543 gpiod_get_direction(gdesc);
4544 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4545 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02004546 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
4547 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08004548 is_out ? "out" : "in ",
Andy Shevchenko1c22a252018-07-12 20:36:42 +03004549 chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
David Brownelld2876d02008-02-04 22:28:20 -08004550 is_irq ? "IRQ" : " ");
4551 seq_printf(s, "\n");
4552 }
4553}
4554
4555static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4556{
4557 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004558 struct gpio_device *gdev = NULL;
David Brownelld2876d02008-02-04 22:28:20 -08004559 loff_t index = *pos;
4560
4561 s->private = "";
4562
4563 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004564 list_for_each_entry(gdev, &gpio_devices, list)
David Brownelld2876d02008-02-04 22:28:20 -08004565 if (index-- == 0) {
4566 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004567 return gdev;
David Brownelld2876d02008-02-04 22:28:20 -08004568 }
4569 spin_unlock_irqrestore(&gpio_lock, flags);
4570
4571 return NULL;
4572}
4573
4574static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4575{
4576 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004577 struct gpio_device *gdev = v;
David Brownelld2876d02008-02-04 22:28:20 -08004578 void *ret = NULL;
4579
4580 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004581 if (list_is_last(&gdev->list, &gpio_devices))
David Brownelld2876d02008-02-04 22:28:20 -08004582 ret = NULL;
4583 else
Linus Walleijff2b1352015-10-20 11:10:38 +02004584 ret = list_entry(gdev->list.next, struct gpio_device, list);
David Brownelld2876d02008-02-04 22:28:20 -08004585 spin_unlock_irqrestore(&gpio_lock, flags);
4586
4587 s->private = "\n";
4588 ++*pos;
4589
4590 return ret;
4591}
4592
4593static void gpiolib_seq_stop(struct seq_file *s, void *v)
4594{
4595}
4596
4597static int gpiolib_seq_show(struct seq_file *s, void *v)
4598{
Linus Walleijff2b1352015-10-20 11:10:38 +02004599 struct gpio_device *gdev = v;
4600 struct gpio_chip *chip = gdev->chip;
4601 struct device *parent;
David Brownelld2876d02008-02-04 22:28:20 -08004602
Linus Walleijff2b1352015-10-20 11:10:38 +02004603 if (!chip) {
4604 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4605 dev_name(&gdev->dev));
4606 return 0;
4607 }
4608
4609 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4610 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004611 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02004612 parent = chip->parent;
4613 if (parent)
4614 seq_printf(s, ", parent: %s/%s",
4615 parent->bus ? parent->bus->name : "no-bus",
4616 dev_name(parent));
David Brownelld2876d02008-02-04 22:28:20 -08004617 if (chip->label)
4618 seq_printf(s, ", %s", chip->label);
4619 if (chip->can_sleep)
4620 seq_printf(s, ", can sleep");
4621 seq_printf(s, ":\n");
4622
4623 if (chip->dbg_show)
4624 chip->dbg_show(s, chip);
4625 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004626 gpiolib_dbg_show(s, gdev);
David Brownelld2876d02008-02-04 22:28:20 -08004627
4628 return 0;
4629}
4630
4631static const struct seq_operations gpiolib_seq_ops = {
4632 .start = gpiolib_seq_start,
4633 .next = gpiolib_seq_next,
4634 .stop = gpiolib_seq_stop,
4635 .show = gpiolib_seq_show,
4636};
4637
4638static int gpiolib_open(struct inode *inode, struct file *file)
4639{
4640 return seq_open(file, &gpiolib_seq_ops);
4641}
4642
4643static const struct file_operations gpiolib_operations = {
4644 .owner = THIS_MODULE,
4645 .open = gpiolib_open,
4646 .read = seq_read,
4647 .llseek = seq_lseek,
4648 .release = seq_release,
4649};
4650
4651static int __init gpiolib_debugfs_init(void)
4652{
4653 /* /sys/kernel/debug/gpio */
4654 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4655 NULL, NULL, &gpiolib_operations);
4656 return 0;
4657}
4658subsys_initcall(gpiolib_debugfs_init);
4659
4660#endif /* DEBUG_FS */